gdb/
[external/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3    Copyright (C) 1986-2012 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "gdbthread.h"
36 #include "target.h"
37 #include "language.h"
38 #include "gdb_string.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "gdb_assert.h"
51 #include "block.h"
52 #include "solib.h"
53 #include "solist.h"
54 #include "observer.h"
55 #include "exceptions.h"
56 #include "memattr.h"
57 #include "ada-lang.h"
58 #include "top.h"
59 #include "valprint.h"
60 #include "jit.h"
61 #include "xml-syscall.h"
62 #include "parser-defs.h"
63 #include "gdb_regex.h"
64 #include "probe.h"
65 #include "cli/cli-utils.h"
66 #include "continuations.h"
67 #include "stack.h"
68 #include "skip.h"
69 #include "gdb_regex.h"
70 #include "ax-gdb.h"
71
72 /* readline include files */
73 #include "readline/readline.h"
74 #include "readline/history.h"
75
76 /* readline defines this.  */
77 #undef savestring
78
79 #include "mi/mi-common.h"
80 #include "python/python.h"
81
82 /* Prototypes for local functions.  */
83
84 static void enable_delete_command (char *, int);
85
86 static void enable_once_command (char *, int);
87
88 static void enable_count_command (char *, int);
89
90 static void disable_command (char *, int);
91
92 static void enable_command (char *, int);
93
94 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
95                                                       void *),
96                                     void *);
97
98 static void ignore_command (char *, int);
99
100 static int breakpoint_re_set_one (void *);
101
102 static void breakpoint_re_set_default (struct breakpoint *);
103
104 static void create_sals_from_address_default (char **,
105                                               struct linespec_result *,
106                                               enum bptype, char *,
107                                               char **);
108
109 static void create_breakpoints_sal_default (struct gdbarch *,
110                                             struct linespec_result *,
111                                             struct linespec_sals *,
112                                             char *, char *, enum bptype,
113                                             enum bpdisp, int, int,
114                                             int,
115                                             const struct breakpoint_ops *,
116                                             int, int, int, unsigned);
117
118 static void decode_linespec_default (struct breakpoint *, char **,
119                                      struct symtabs_and_lines *);
120
121 static void clear_command (char *, int);
122
123 static void catch_command (char *, int);
124
125 static int can_use_hardware_watchpoint (struct value *);
126
127 static void break_command_1 (char *, int, int);
128
129 static void mention (struct breakpoint *);
130
131 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
132                                                                enum bptype,
133                                                                const struct breakpoint_ops *);
134 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
135                                                        const struct symtab_and_line *);
136
137 /* This function is used in gdbtk sources and thus can not be made
138    static.  */
139 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
140                                        struct symtab_and_line,
141                                        enum bptype,
142                                        const struct breakpoint_ops *);
143
144 static struct breakpoint *
145   momentary_breakpoint_from_master (struct breakpoint *orig,
146                                     enum bptype type,
147                                     const struct breakpoint_ops *ops);
148
149 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
150
151 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
152                                             CORE_ADDR bpaddr,
153                                             enum bptype bptype);
154
155 static void describe_other_breakpoints (struct gdbarch *,
156                                         struct program_space *, CORE_ADDR,
157                                         struct obj_section *, int);
158
159 static int breakpoint_address_match (struct address_space *aspace1,
160                                      CORE_ADDR addr1,
161                                      struct address_space *aspace2,
162                                      CORE_ADDR addr2);
163
164 static int watchpoint_locations_match (struct bp_location *loc1,
165                                        struct bp_location *loc2);
166
167 static int breakpoint_location_address_match (struct bp_location *bl,
168                                               struct address_space *aspace,
169                                               CORE_ADDR addr);
170
171 static void breakpoints_info (char *, int);
172
173 static void watchpoints_info (char *, int);
174
175 static int breakpoint_1 (char *, int, 
176                          int (*) (const struct breakpoint *));
177
178 static int breakpoint_cond_eval (void *);
179
180 static void cleanup_executing_breakpoints (void *);
181
182 static void commands_command (char *, int);
183
184 static void condition_command (char *, int);
185
186 typedef enum
187   {
188     mark_inserted,
189     mark_uninserted
190   }
191 insertion_state_t;
192
193 static int remove_breakpoint (struct bp_location *, insertion_state_t);
194 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
195
196 static enum print_stop_action print_bp_stop_message (bpstat bs);
197
198 static int watchpoint_check (void *);
199
200 static void maintenance_info_breakpoints (char *, int);
201
202 static int hw_breakpoint_used_count (void);
203
204 static int hw_watchpoint_use_count (struct breakpoint *);
205
206 static int hw_watchpoint_used_count_others (struct breakpoint *except,
207                                             enum bptype type,
208                                             int *other_type_used);
209
210 static void hbreak_command (char *, int);
211
212 static void thbreak_command (char *, int);
213
214 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
215                                     int count);
216
217 static void stop_command (char *arg, int from_tty);
218
219 static void stopin_command (char *arg, int from_tty);
220
221 static void stopat_command (char *arg, int from_tty);
222
223 static char *ep_parse_optional_if_clause (char **arg);
224
225 static void catch_exception_command_1 (enum exception_event_kind ex_event, 
226                                        char *arg, int tempflag, int from_tty);
227
228 static void tcatch_command (char *arg, int from_tty);
229
230 static void detach_single_step_breakpoints (void);
231
232 static int single_step_breakpoint_inserted_here_p (struct address_space *,
233                                                    CORE_ADDR pc);
234
235 static void free_bp_location (struct bp_location *loc);
236 static void incref_bp_location (struct bp_location *loc);
237 static void decref_bp_location (struct bp_location **loc);
238
239 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
240
241 static void update_global_location_list (int);
242
243 static void update_global_location_list_nothrow (int);
244
245 static int is_hardware_watchpoint (const struct breakpoint *bpt);
246
247 static void insert_breakpoint_locations (void);
248
249 static int syscall_catchpoint_p (struct breakpoint *b);
250
251 static void tracepoints_info (char *, int);
252
253 static void delete_trace_command (char *, int);
254
255 static void enable_trace_command (char *, int);
256
257 static void disable_trace_command (char *, int);
258
259 static void trace_pass_command (char *, int);
260
261 static int is_masked_watchpoint (const struct breakpoint *b);
262
263 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
264
265 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
266    otherwise.  */
267
268 static int strace_marker_p (struct breakpoint *b);
269
270 static void init_catchpoint (struct breakpoint *b,
271                              struct gdbarch *gdbarch, int tempflag,
272                              char *cond_string,
273                              const struct breakpoint_ops *ops);
274
275 /* The abstract base class all breakpoint_ops structures inherit
276    from.  */
277 static struct breakpoint_ops base_breakpoint_ops;
278
279 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
280    that are implemented on top of software or hardware breakpoints
281    (user breakpoints, internal and momentary breakpoints, etc.).  */
282 static struct breakpoint_ops bkpt_base_breakpoint_ops;
283
284 /* Internal breakpoints class type.  */
285 static struct breakpoint_ops internal_breakpoint_ops;
286
287 /* Momentary breakpoints class type.  */
288 static struct breakpoint_ops momentary_breakpoint_ops;
289
290 /* The breakpoint_ops structure to be used in regular user created
291    breakpoints.  */
292 struct breakpoint_ops bkpt_breakpoint_ops;
293
294 /* Breakpoints set on probes.  */
295 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
296
297 /* Dynamic printf class type.  */
298 static struct breakpoint_ops dprintf_breakpoint_ops;
299
300 /* A reference-counted struct command_line.  This lets multiple
301    breakpoints share a single command list.  */
302 struct counted_command_line
303 {
304   /* The reference count.  */
305   int refc;
306
307   /* The command list.  */
308   struct command_line *commands;
309 };
310
311 struct command_line *
312 breakpoint_commands (struct breakpoint *b)
313 {
314   return b->commands ? b->commands->commands : NULL;
315 }
316
317 /* Flag indicating that a command has proceeded the inferior past the
318    current breakpoint.  */
319
320 static int breakpoint_proceeded;
321
322 const char *
323 bpdisp_text (enum bpdisp disp)
324 {
325   /* NOTE: the following values are a part of MI protocol and
326      represent values of 'disp' field returned when inferior stops at
327      a breakpoint.  */
328   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
329
330   return bpdisps[(int) disp];
331 }
332
333 /* Prototypes for exported functions.  */
334 /* If FALSE, gdb will not use hardware support for watchpoints, even
335    if such is available.  */
336 static int can_use_hw_watchpoints;
337
338 static void
339 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
340                              struct cmd_list_element *c,
341                              const char *value)
342 {
343   fprintf_filtered (file,
344                     _("Debugger's willingness to use "
345                       "watchpoint hardware is %s.\n"),
346                     value);
347 }
348
349 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
350    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
351    for unrecognized breakpoint locations.
352    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
353 static enum auto_boolean pending_break_support;
354 static void
355 show_pending_break_support (struct ui_file *file, int from_tty,
356                             struct cmd_list_element *c,
357                             const char *value)
358 {
359   fprintf_filtered (file,
360                     _("Debugger's behavior regarding "
361                       "pending breakpoints is %s.\n"),
362                     value);
363 }
364
365 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
366    set with "break" but falling in read-only memory.
367    If 0, gdb will warn about such breakpoints, but won't automatically
368    use hardware breakpoints.  */
369 static int automatic_hardware_breakpoints;
370 static void
371 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
372                                      struct cmd_list_element *c,
373                                      const char *value)
374 {
375   fprintf_filtered (file,
376                     _("Automatic usage of hardware breakpoints is %s.\n"),
377                     value);
378 }
379
380 /* If on, gdb will keep breakpoints inserted even as inferior is
381    stopped, and immediately insert any new breakpoints.  If off, gdb
382    will insert breakpoints into inferior only when resuming it, and
383    will remove breakpoints upon stop.  If auto, GDB will behave as ON
384    if in non-stop mode, and as OFF if all-stop mode.*/
385
386 static const char always_inserted_auto[] = "auto";
387 static const char always_inserted_on[] = "on";
388 static const char always_inserted_off[] = "off";
389 static const char *const always_inserted_enums[] = {
390   always_inserted_auto,
391   always_inserted_off,
392   always_inserted_on,
393   NULL
394 };
395 static const char *always_inserted_mode = always_inserted_auto;
396 static void
397 show_always_inserted_mode (struct ui_file *file, int from_tty,
398                      struct cmd_list_element *c, const char *value)
399 {
400   if (always_inserted_mode == always_inserted_auto)
401     fprintf_filtered (file,
402                       _("Always inserted breakpoint "
403                         "mode is %s (currently %s).\n"),
404                       value,
405                       breakpoints_always_inserted_mode () ? "on" : "off");
406   else
407     fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
408                       value);
409 }
410
411 int
412 breakpoints_always_inserted_mode (void)
413 {
414   return (always_inserted_mode == always_inserted_on
415           || (always_inserted_mode == always_inserted_auto && non_stop));
416 }
417
418 static const char condition_evaluation_both[] = "host or target";
419
420 /* Modes for breakpoint condition evaluation.  */
421 static const char condition_evaluation_auto[] = "auto";
422 static const char condition_evaluation_host[] = "host";
423 static const char condition_evaluation_target[] = "target";
424 static const char *const condition_evaluation_enums[] = {
425   condition_evaluation_auto,
426   condition_evaluation_host,
427   condition_evaluation_target,
428   NULL
429 };
430
431 /* Global that holds the current mode for breakpoint condition evaluation.  */
432 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434 /* Global that we use to display information to the user (gets its value from
435    condition_evaluation_mode_1.  */
436 static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438 /* Translate a condition evaluation mode MODE into either "host"
439    or "target".  This is used mostly to translate from "auto" to the
440    real setting that is being used.  It returns the translated
441    evaluation mode.  */
442
443 static const char *
444 translate_condition_evaluation_mode (const char *mode)
445 {
446   if (mode == condition_evaluation_auto)
447     {
448       if (target_supports_evaluation_of_breakpoint_conditions ())
449         return condition_evaluation_target;
450       else
451         return condition_evaluation_host;
452     }
453   else
454     return mode;
455 }
456
457 /* Discovers what condition_evaluation_auto translates to.  */
458
459 static const char *
460 breakpoint_condition_evaluation_mode (void)
461 {
462   return translate_condition_evaluation_mode (condition_evaluation_mode);
463 }
464
465 /* Return true if GDB should evaluate breakpoint conditions or false
466    otherwise.  */
467
468 static int
469 gdb_evaluates_breakpoint_condition_p (void)
470 {
471   const char *mode = breakpoint_condition_evaluation_mode ();
472
473   return (mode == condition_evaluation_host);
474 }
475
476 void _initialize_breakpoint (void);
477
478 /* Are we executing breakpoint commands?  */
479 static int executing_breakpoint_commands;
480
481 /* Are overlay event breakpoints enabled? */
482 static int overlay_events_enabled;
483
484 /* See description in breakpoint.h. */
485 int target_exact_watchpoints = 0;
486
487 /* Walk the following statement or block through all breakpoints.
488    ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
489    current breakpoint.  */
490
491 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
492
493 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
494         for (B = breakpoint_chain;      \
495              B ? (TMP=B->next, 1): 0;   \
496              B = TMP)
497
498 /* Similar iterator for the low-level breakpoints.  SAFE variant is
499    not provided so update_global_location_list must not be called
500    while executing the block of ALL_BP_LOCATIONS.  */
501
502 #define ALL_BP_LOCATIONS(B,BP_TMP)                                      \
503         for (BP_TMP = bp_location;                                      \
504              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
505              BP_TMP++)
506
507 /* Iterates through locations with address ADDRESS for the currently selected
508    program space.  BP_LOCP_TMP points to each object.  BP_LOCP_START points
509    to where the loop should start from.
510    If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
511    appropriate location to start with.  */
512
513 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS)   \
514         for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
515              BP_LOCP_TMP = BP_LOCP_START;                               \
516              BP_LOCP_START                                              \
517              && (BP_LOCP_TMP < bp_location + bp_location_count          \
518              && (*BP_LOCP_TMP)->address == ADDRESS);                    \
519              BP_LOCP_TMP++)
520
521 /* Iterator for tracepoints only.  */
522
523 #define ALL_TRACEPOINTS(B)  \
524   for (B = breakpoint_chain; B; B = B->next)  \
525     if (is_tracepoint (B))
526
527 /* Chains of all breakpoints defined.  */
528
529 struct breakpoint *breakpoint_chain;
530
531 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
532
533 static struct bp_location **bp_location;
534
535 /* Number of elements of BP_LOCATION.  */
536
537 static unsigned bp_location_count;
538
539 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
540    ADDRESS for the current elements of BP_LOCATION which get a valid
541    result from bp_location_has_shadow.  You can use it for roughly
542    limiting the subrange of BP_LOCATION to scan for shadow bytes for
543    an address you need to read.  */
544
545 static CORE_ADDR bp_location_placed_address_before_address_max;
546
547 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
548    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
549    BP_LOCATION which get a valid result from bp_location_has_shadow.
550    You can use it for roughly limiting the subrange of BP_LOCATION to
551    scan for shadow bytes for an address you need to read.  */
552
553 static CORE_ADDR bp_location_shadow_len_after_address_max;
554
555 /* The locations that no longer correspond to any breakpoint, unlinked
556    from bp_location array, but for which a hit may still be reported
557    by a target.  */
558 VEC(bp_location_p) *moribund_locations = NULL;
559
560 /* Number of last breakpoint made.  */
561
562 static int breakpoint_count;
563
564 /* The value of `breakpoint_count' before the last command that
565    created breakpoints.  If the last (break-like) command created more
566    than one breakpoint, then the difference between BREAKPOINT_COUNT
567    and PREV_BREAKPOINT_COUNT is more than one.  */
568 static int prev_breakpoint_count;
569
570 /* Number of last tracepoint made.  */
571
572 static int tracepoint_count;
573
574 static struct cmd_list_element *breakpoint_set_cmdlist;
575 static struct cmd_list_element *breakpoint_show_cmdlist;
576 struct cmd_list_element *save_cmdlist;
577
578 /* Return whether a breakpoint is an active enabled breakpoint.  */
579 static int
580 breakpoint_enabled (struct breakpoint *b)
581 {
582   return (b->enable_state == bp_enabled);
583 }
584
585 /* Set breakpoint count to NUM.  */
586
587 static void
588 set_breakpoint_count (int num)
589 {
590   prev_breakpoint_count = breakpoint_count;
591   breakpoint_count = num;
592   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
593 }
594
595 /* Used by `start_rbreak_breakpoints' below, to record the current
596    breakpoint count before "rbreak" creates any breakpoint.  */
597 static int rbreak_start_breakpoint_count;
598
599 /* Called at the start an "rbreak" command to record the first
600    breakpoint made.  */
601
602 void
603 start_rbreak_breakpoints (void)
604 {
605   rbreak_start_breakpoint_count = breakpoint_count;
606 }
607
608 /* Called at the end of an "rbreak" command to record the last
609    breakpoint made.  */
610
611 void
612 end_rbreak_breakpoints (void)
613 {
614   prev_breakpoint_count = rbreak_start_breakpoint_count;
615 }
616
617 /* Used in run_command to zero the hit count when a new run starts.  */
618
619 void
620 clear_breakpoint_hit_counts (void)
621 {
622   struct breakpoint *b;
623
624   ALL_BREAKPOINTS (b)
625     b->hit_count = 0;
626 }
627
628 /* Allocate a new counted_command_line with reference count of 1.
629    The new structure owns COMMANDS.  */
630
631 static struct counted_command_line *
632 alloc_counted_command_line (struct command_line *commands)
633 {
634   struct counted_command_line *result
635     = xmalloc (sizeof (struct counted_command_line));
636
637   result->refc = 1;
638   result->commands = commands;
639   return result;
640 }
641
642 /* Increment reference count.  This does nothing if CMD is NULL.  */
643
644 static void
645 incref_counted_command_line (struct counted_command_line *cmd)
646 {
647   if (cmd)
648     ++cmd->refc;
649 }
650
651 /* Decrement reference count.  If the reference count reaches 0,
652    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
653    nothing if *CMDP is NULL.  */
654
655 static void
656 decref_counted_command_line (struct counted_command_line **cmdp)
657 {
658   if (*cmdp)
659     {
660       if (--(*cmdp)->refc == 0)
661         {
662           free_command_lines (&(*cmdp)->commands);
663           xfree (*cmdp);
664         }
665       *cmdp = NULL;
666     }
667 }
668
669 /* A cleanup function that calls decref_counted_command_line.  */
670
671 static void
672 do_cleanup_counted_command_line (void *arg)
673 {
674   decref_counted_command_line (arg);
675 }
676
677 /* Create a cleanup that calls decref_counted_command_line on the
678    argument.  */
679
680 static struct cleanup *
681 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
682 {
683   return make_cleanup (do_cleanup_counted_command_line, cmdp);
684 }
685
686 \f
687 /* Return the breakpoint with the specified number, or NULL
688    if the number does not refer to an existing breakpoint.  */
689
690 struct breakpoint *
691 get_breakpoint (int num)
692 {
693   struct breakpoint *b;
694
695   ALL_BREAKPOINTS (b)
696     if (b->number == num)
697       return b;
698   
699   return NULL;
700 }
701
702 \f
703
704 /* Mark locations as "conditions have changed" in case the target supports
705    evaluating conditions on its side.  */
706
707 static void
708 mark_breakpoint_modified (struct breakpoint *b)
709 {
710   struct bp_location *loc;
711
712   /* This is only meaningful if the target is
713      evaluating conditions and if the user has
714      opted for condition evaluation on the target's
715      side.  */
716   if (gdb_evaluates_breakpoint_condition_p ()
717       || !target_supports_evaluation_of_breakpoint_conditions ())
718     return;
719
720   if (!is_breakpoint (b))
721     return;
722
723   for (loc = b->loc; loc; loc = loc->next)
724     loc->condition_changed = condition_modified;
725 }
726
727 /* Mark location as "conditions have changed" in case the target supports
728    evaluating conditions on its side.  */
729
730 static void
731 mark_breakpoint_location_modified (struct bp_location *loc)
732 {
733   /* This is only meaningful if the target is
734      evaluating conditions and if the user has
735      opted for condition evaluation on the target's
736      side.  */
737   if (gdb_evaluates_breakpoint_condition_p ()
738       || !target_supports_evaluation_of_breakpoint_conditions ())
739
740     return;
741
742   if (!is_breakpoint (loc->owner))
743     return;
744
745   loc->condition_changed = condition_modified;
746 }
747
748 /* Sets the condition-evaluation mode using the static global
749    condition_evaluation_mode.  */
750
751 static void
752 set_condition_evaluation_mode (char *args, int from_tty,
753                                struct cmd_list_element *c)
754 {
755   struct breakpoint *b;
756   const char *old_mode, *new_mode;
757
758   if ((condition_evaluation_mode_1 == condition_evaluation_target)
759       && !target_supports_evaluation_of_breakpoint_conditions ())
760     {
761       condition_evaluation_mode_1 = condition_evaluation_mode;
762       warning (_("Target does not support breakpoint condition evaluation.\n"
763                  "Using host evaluation mode instead."));
764       return;
765     }
766
767   new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
768   old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
769
770   /* Flip the switch.  Flip it even if OLD_MODE == NEW_MODE as one of the
771      settings was "auto".  */
772   condition_evaluation_mode = condition_evaluation_mode_1;
773
774   /* Only update the mode if the user picked a different one.  */
775   if (new_mode != old_mode)
776     {
777       struct bp_location *loc, **loc_tmp;
778       /* If the user switched to a different evaluation mode, we
779          need to synch the changes with the target as follows:
780
781          "host" -> "target": Send all (valid) conditions to the target.
782          "target" -> "host": Remove all the conditions from the target.
783       */
784
785       if (new_mode == condition_evaluation_target)
786         {
787           /* Mark everything modified and synch conditions with the
788              target.  */
789           ALL_BP_LOCATIONS (loc, loc_tmp)
790             mark_breakpoint_location_modified (loc);
791         }
792       else
793         {
794           /* Manually mark non-duplicate locations to synch conditions
795              with the target.  We do this to remove all the conditions the
796              target knows about.  */
797           ALL_BP_LOCATIONS (loc, loc_tmp)
798             if (is_breakpoint (loc->owner) && loc->inserted)
799               loc->needs_update = 1;
800         }
801
802       /* Do the update.  */
803       update_global_location_list (1);
804     }
805
806   return;
807 }
808
809 /* Shows the current mode of breakpoint condition evaluation.  Explicitly shows
810    what "auto" is translating to.  */
811
812 static void
813 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
814                                 struct cmd_list_element *c, const char *value)
815 {
816   if (condition_evaluation_mode == condition_evaluation_auto)
817     fprintf_filtered (file,
818                       _("Breakpoint condition evaluation "
819                         "mode is %s (currently %s).\n"),
820                       value,
821                       breakpoint_condition_evaluation_mode ());
822   else
823     fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
824                       value);
825 }
826
827 /* A comparison function for bp_location AP and BP that is used by
828    bsearch.  This comparison function only cares about addresses, unlike
829    the more general bp_location_compare function.  */
830
831 static int
832 bp_location_compare_addrs (const void *ap, const void *bp)
833 {
834   struct bp_location *a = *(void **) ap;
835   struct bp_location *b = *(void **) bp;
836
837   if (a->address == b->address)
838     return 0;
839   else
840     return ((a->address > b->address) - (a->address < b->address));
841 }
842
843 /* Helper function to skip all bp_locations with addresses
844    less than ADDRESS.  It returns the first bp_location that
845    is greater than or equal to ADDRESS.  If none is found, just
846    return NULL.  */
847
848 static struct bp_location **
849 get_first_locp_gte_addr (CORE_ADDR address)
850 {
851   struct bp_location dummy_loc;
852   struct bp_location *dummy_locp = &dummy_loc;
853   struct bp_location **locp_found = NULL;
854
855   /* Initialize the dummy location's address field.  */
856   memset (&dummy_loc, 0, sizeof (struct bp_location));
857   dummy_loc.address = address;
858
859   /* Find a close match to the first location at ADDRESS.  */
860   locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
861                         sizeof (struct bp_location **),
862                         bp_location_compare_addrs);
863
864   /* Nothing was found, nothing left to do.  */
865   if (locp_found == NULL)
866     return NULL;
867
868   /* We may have found a location that is at ADDRESS but is not the first in the
869      location's list.  Go backwards (if possible) and locate the first one.  */
870   while ((locp_found - 1) >= bp_location
871          && (*(locp_found - 1))->address == address)
872     locp_found--;
873
874   return locp_found;
875 }
876
877 void
878 set_breakpoint_condition (struct breakpoint *b, char *exp,
879                           int from_tty)
880 {
881   xfree (b->cond_string);
882   b->cond_string = NULL;
883
884   if (is_watchpoint (b))
885     {
886       struct watchpoint *w = (struct watchpoint *) b;
887
888       xfree (w->cond_exp);
889       w->cond_exp = NULL;
890     }
891   else
892     {
893       struct bp_location *loc;
894
895       for (loc = b->loc; loc; loc = loc->next)
896         {
897           xfree (loc->cond);
898           loc->cond = NULL;
899
900           /* No need to free the condition agent expression
901              bytecode (if we have one).  We will handle this
902              when we go through update_global_location_list.  */
903         }
904     }
905
906   if (*exp == 0)
907     {
908       if (from_tty)
909         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
910     }
911   else
912     {
913       char *arg = exp;
914
915       /* I don't know if it matters whether this is the string the user
916          typed in or the decompiled expression.  */
917       b->cond_string = xstrdup (arg);
918       b->condition_not_parsed = 0;
919
920       if (is_watchpoint (b))
921         {
922           struct watchpoint *w = (struct watchpoint *) b;
923
924           innermost_block = NULL;
925           arg = exp;
926           w->cond_exp = parse_exp_1 (&arg, 0, 0);
927           if (*arg)
928             error (_("Junk at end of expression"));
929           w->cond_exp_valid_block = innermost_block;
930         }
931       else
932         {
933           struct bp_location *loc;
934
935           for (loc = b->loc; loc; loc = loc->next)
936             {
937               arg = exp;
938               loc->cond =
939                 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
940               if (*arg)
941                 error (_("Junk at end of expression"));
942             }
943         }
944     }
945   mark_breakpoint_modified (b);
946
947   breakpoints_changed ();
948   observer_notify_breakpoint_modified (b);
949 }
950
951 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
952
953 static void
954 condition_command (char *arg, int from_tty)
955 {
956   struct breakpoint *b;
957   char *p;
958   int bnum;
959
960   if (arg == 0)
961     error_no_arg (_("breakpoint number"));
962
963   p = arg;
964   bnum = get_number (&p);
965   if (bnum == 0)
966     error (_("Bad breakpoint argument: '%s'"), arg);
967
968   ALL_BREAKPOINTS (b)
969     if (b->number == bnum)
970       {
971         /* Check if this breakpoint has a Python object assigned to
972            it, and if it has a definition of the "stop"
973            method.  This method and conditions entered into GDB from
974            the CLI are mutually exclusive.  */
975         if (b->py_bp_object
976             && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
977           error (_("Cannot set a condition where a Python 'stop' "
978                    "method has been defined in the breakpoint."));
979         set_breakpoint_condition (b, p, from_tty);
980
981         if (is_breakpoint (b))
982           update_global_location_list (1);
983
984         return;
985       }
986
987   error (_("No breakpoint number %d."), bnum);
988 }
989
990 /* Check that COMMAND do not contain commands that are suitable
991    only for tracepoints and not suitable for ordinary breakpoints.
992    Throw if any such commands is found.  */
993
994 static void
995 check_no_tracepoint_commands (struct command_line *commands)
996 {
997   struct command_line *c;
998
999   for (c = commands; c; c = c->next)
1000     {
1001       int i;
1002
1003       if (c->control_type == while_stepping_control)
1004         error (_("The 'while-stepping' command can "
1005                  "only be used for tracepoints"));
1006
1007       for (i = 0; i < c->body_count; ++i)
1008         check_no_tracepoint_commands ((c->body_list)[i]);
1009
1010       /* Not that command parsing removes leading whitespace and comment
1011          lines and also empty lines.  So, we only need to check for
1012          command directly.  */
1013       if (strstr (c->line, "collect ") == c->line)
1014         error (_("The 'collect' command can only be used for tracepoints"));
1015
1016       if (strstr (c->line, "teval ") == c->line)
1017         error (_("The 'teval' command can only be used for tracepoints"));
1018     }
1019 }
1020
1021 /* Encapsulate tests for different types of tracepoints.  */
1022
1023 static int
1024 is_tracepoint_type (enum bptype type)
1025 {
1026   return (type == bp_tracepoint
1027           || type == bp_fast_tracepoint
1028           || type == bp_static_tracepoint);
1029 }
1030
1031 int
1032 is_tracepoint (const struct breakpoint *b)
1033 {
1034   return is_tracepoint_type (b->type);
1035 }
1036
1037 /* A helper function that validates that COMMANDS are valid for a
1038    breakpoint.  This function will throw an exception if a problem is
1039    found.  */
1040
1041 static void
1042 validate_commands_for_breakpoint (struct breakpoint *b,
1043                                   struct command_line *commands)
1044 {
1045   if (is_tracepoint (b))
1046     {
1047       /* We need to verify that each top-level element of commands is
1048          valid for tracepoints, that there's at most one
1049          while-stepping element, and that while-stepping's body has
1050          valid tracing commands excluding nested while-stepping.  */
1051       struct command_line *c;
1052       struct command_line *while_stepping = 0;
1053       for (c = commands; c; c = c->next)
1054         {
1055           if (c->control_type == while_stepping_control)
1056             {
1057               if (b->type == bp_fast_tracepoint)
1058                 error (_("The 'while-stepping' command "
1059                          "cannot be used for fast tracepoint"));
1060               else if (b->type == bp_static_tracepoint)
1061                 error (_("The 'while-stepping' command "
1062                          "cannot be used for static tracepoint"));
1063
1064               if (while_stepping)
1065                 error (_("The 'while-stepping' command "
1066                          "can be used only once"));
1067               else
1068                 while_stepping = c;
1069             }
1070         }
1071       if (while_stepping)
1072         {
1073           struct command_line *c2;
1074
1075           gdb_assert (while_stepping->body_count == 1);
1076           c2 = while_stepping->body_list[0];
1077           for (; c2; c2 = c2->next)
1078             {
1079               if (c2->control_type == while_stepping_control)
1080                 error (_("The 'while-stepping' command cannot be nested"));
1081             }
1082         }
1083     }
1084   else
1085     {
1086       check_no_tracepoint_commands (commands);
1087     }
1088 }
1089
1090 /* Return a vector of all the static tracepoints set at ADDR.  The
1091    caller is responsible for releasing the vector.  */
1092
1093 VEC(breakpoint_p) *
1094 static_tracepoints_here (CORE_ADDR addr)
1095 {
1096   struct breakpoint *b;
1097   VEC(breakpoint_p) *found = 0;
1098   struct bp_location *loc;
1099
1100   ALL_BREAKPOINTS (b)
1101     if (b->type == bp_static_tracepoint)
1102       {
1103         for (loc = b->loc; loc; loc = loc->next)
1104           if (loc->address == addr)
1105             VEC_safe_push(breakpoint_p, found, b);
1106       }
1107
1108   return found;
1109 }
1110
1111 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
1112    validate that only allowed commands are included.  */
1113
1114 void
1115 breakpoint_set_commands (struct breakpoint *b, 
1116                          struct command_line *commands)
1117 {
1118   validate_commands_for_breakpoint (b, commands);
1119
1120   decref_counted_command_line (&b->commands);
1121   b->commands = alloc_counted_command_line (commands);
1122   breakpoints_changed ();
1123   observer_notify_breakpoint_modified (b);
1124 }
1125
1126 /* Set the internal `silent' flag on the breakpoint.  Note that this
1127    is not the same as the "silent" that may appear in the breakpoint's
1128    commands.  */
1129
1130 void
1131 breakpoint_set_silent (struct breakpoint *b, int silent)
1132 {
1133   int old_silent = b->silent;
1134
1135   b->silent = silent;
1136   if (old_silent != silent)
1137     observer_notify_breakpoint_modified (b);
1138 }
1139
1140 /* Set the thread for this breakpoint.  If THREAD is -1, make the
1141    breakpoint work for any thread.  */
1142
1143 void
1144 breakpoint_set_thread (struct breakpoint *b, int thread)
1145 {
1146   int old_thread = b->thread;
1147
1148   b->thread = thread;
1149   if (old_thread != thread)
1150     observer_notify_breakpoint_modified (b);
1151 }
1152
1153 /* Set the task for this breakpoint.  If TASK is 0, make the
1154    breakpoint work for any task.  */
1155
1156 void
1157 breakpoint_set_task (struct breakpoint *b, int task)
1158 {
1159   int old_task = b->task;
1160
1161   b->task = task;
1162   if (old_task != task)
1163     observer_notify_breakpoint_modified (b);
1164 }
1165
1166 void
1167 check_tracepoint_command (char *line, void *closure)
1168 {
1169   struct breakpoint *b = closure;
1170
1171   validate_actionline (&line, b);
1172 }
1173
1174 /* A structure used to pass information through
1175    map_breakpoint_numbers.  */
1176
1177 struct commands_info
1178 {
1179   /* True if the command was typed at a tty.  */
1180   int from_tty;
1181
1182   /* The breakpoint range spec.  */
1183   char *arg;
1184
1185   /* Non-NULL if the body of the commands are being read from this
1186      already-parsed command.  */
1187   struct command_line *control;
1188
1189   /* The command lines read from the user, or NULL if they have not
1190      yet been read.  */
1191   struct counted_command_line *cmd;
1192 };
1193
1194 /* A callback for map_breakpoint_numbers that sets the commands for
1195    commands_command.  */
1196
1197 static void
1198 do_map_commands_command (struct breakpoint *b, void *data)
1199 {
1200   struct commands_info *info = data;
1201
1202   if (info->cmd == NULL)
1203     {
1204       struct command_line *l;
1205
1206       if (info->control != NULL)
1207         l = copy_command_lines (info->control->body_list[0]);
1208       else
1209         {
1210           struct cleanup *old_chain;
1211           char *str;
1212
1213           str = xstrprintf (_("Type commands for breakpoint(s) "
1214                               "%s, one per line."),
1215                             info->arg);
1216
1217           old_chain = make_cleanup (xfree, str);
1218
1219           l = read_command_lines (str,
1220                                   info->from_tty, 1,
1221                                   (is_tracepoint (b)
1222                                    ? check_tracepoint_command : 0),
1223                                   b);
1224
1225           do_cleanups (old_chain);
1226         }
1227
1228       info->cmd = alloc_counted_command_line (l);
1229     }
1230
1231   /* If a breakpoint was on the list more than once, we don't need to
1232      do anything.  */
1233   if (b->commands != info->cmd)
1234     {
1235       validate_commands_for_breakpoint (b, info->cmd->commands);
1236       incref_counted_command_line (info->cmd);
1237       decref_counted_command_line (&b->commands);
1238       b->commands = info->cmd;
1239       breakpoints_changed ();
1240       observer_notify_breakpoint_modified (b);
1241     }
1242 }
1243
1244 static void
1245 commands_command_1 (char *arg, int from_tty, 
1246                     struct command_line *control)
1247 {
1248   struct cleanup *cleanups;
1249   struct commands_info info;
1250
1251   info.from_tty = from_tty;
1252   info.control = control;
1253   info.cmd = NULL;
1254   /* If we read command lines from the user, then `info' will hold an
1255      extra reference to the commands that we must clean up.  */
1256   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1257
1258   if (arg == NULL || !*arg)
1259     {
1260       if (breakpoint_count - prev_breakpoint_count > 1)
1261         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, 
1262                           breakpoint_count);
1263       else if (breakpoint_count > 0)
1264         arg = xstrprintf ("%d", breakpoint_count);
1265       else
1266         {
1267           /* So that we don't try to free the incoming non-NULL
1268              argument in the cleanup below.  Mapping breakpoint
1269              numbers will fail in this case.  */
1270           arg = NULL;
1271         }
1272     }
1273   else
1274     /* The command loop has some static state, so we need to preserve
1275        our argument.  */
1276     arg = xstrdup (arg);
1277
1278   if (arg != NULL)
1279     make_cleanup (xfree, arg);
1280
1281   info.arg = arg;
1282
1283   map_breakpoint_numbers (arg, do_map_commands_command, &info);
1284
1285   if (info.cmd == NULL)
1286     error (_("No breakpoints specified."));
1287
1288   do_cleanups (cleanups);
1289 }
1290
1291 static void
1292 commands_command (char *arg, int from_tty)
1293 {
1294   commands_command_1 (arg, from_tty, NULL);
1295 }
1296
1297 /* Like commands_command, but instead of reading the commands from
1298    input stream, takes them from an already parsed command structure.
1299
1300    This is used by cli-script.c to DTRT with breakpoint commands
1301    that are part of if and while bodies.  */
1302 enum command_control_type
1303 commands_from_control_command (char *arg, struct command_line *cmd)
1304 {
1305   commands_command_1 (arg, 0, cmd);
1306   return simple_control;
1307 }
1308
1309 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
1310
1311 static int
1312 bp_location_has_shadow (struct bp_location *bl)
1313 {
1314   if (bl->loc_type != bp_loc_software_breakpoint)
1315     return 0;
1316   if (!bl->inserted)
1317     return 0;
1318   if (bl->target_info.shadow_len == 0)
1319     /* BL isn't valid, or doesn't shadow memory.  */
1320     return 0;
1321   return 1;
1322 }
1323
1324 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1325    by replacing any memory breakpoints with their shadowed contents.
1326
1327    If READBUF is not NULL, this buffer must not overlap with any of
1328    the breakpoint location's shadow_contents buffers.  Otherwise,
1329    a failed assertion internal error will be raised.
1330
1331    The range of shadowed area by each bp_location is:
1332      bl->address - bp_location_placed_address_before_address_max
1333      up to bl->address + bp_location_shadow_len_after_address_max
1334    The range we were requested to resolve shadows for is:
1335      memaddr ... memaddr + len
1336    Thus the safe cutoff boundaries for performance optimization are
1337      memaddr + len <= (bl->address
1338                        - bp_location_placed_address_before_address_max)
1339    and:
1340      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
1341
1342 void
1343 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1344                         const gdb_byte *writebuf_org,
1345                         ULONGEST memaddr, LONGEST len)
1346 {
1347   /* Left boundary, right boundary and median element of our binary
1348      search.  */
1349   unsigned bc_l, bc_r, bc;
1350
1351   /* Find BC_L which is a leftmost element which may affect BUF
1352      content.  It is safe to report lower value but a failure to
1353      report higher one.  */
1354
1355   bc_l = 0;
1356   bc_r = bp_location_count;
1357   while (bc_l + 1 < bc_r)
1358     {
1359       struct bp_location *bl;
1360
1361       bc = (bc_l + bc_r) / 2;
1362       bl = bp_location[bc];
1363
1364       /* Check first BL->ADDRESS will not overflow due to the added
1365          constant.  Then advance the left boundary only if we are sure
1366          the BC element can in no way affect the BUF content (MEMADDR
1367          to MEMADDR + LEN range).
1368
1369          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1370          offset so that we cannot miss a breakpoint with its shadow
1371          range tail still reaching MEMADDR.  */
1372
1373       if ((bl->address + bp_location_shadow_len_after_address_max
1374            >= bl->address)
1375           && (bl->address + bp_location_shadow_len_after_address_max
1376               <= memaddr))
1377         bc_l = bc;
1378       else
1379         bc_r = bc;
1380     }
1381
1382   /* Due to the binary search above, we need to make sure we pick the
1383      first location that's at BC_L's address.  E.g., if there are
1384      multiple locations at the same address, BC_L may end up pointing
1385      at a duplicate location, and miss the "master"/"inserted"
1386      location.  Say, given locations L1, L2 and L3 at addresses A and
1387      B:
1388
1389       L1@A, L2@A, L3@B, ...
1390
1391      BC_L could end up pointing at location L2, while the "master"
1392      location could be L1.  Since the `loc->inserted' flag is only set
1393      on "master" locations, we'd forget to restore the shadow of L1
1394      and L2.  */
1395   while (bc_l > 0
1396          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1397     bc_l--;
1398
1399   /* Now do full processing of the found relevant range of elements.  */
1400
1401   for (bc = bc_l; bc < bp_location_count; bc++)
1402   {
1403     struct bp_location *bl = bp_location[bc];
1404     CORE_ADDR bp_addr = 0;
1405     int bp_size = 0;
1406     int bptoffset = 0;
1407
1408     /* bp_location array has BL->OWNER always non-NULL.  */
1409     if (bl->owner->type == bp_none)
1410       warning (_("reading through apparently deleted breakpoint #%d?"),
1411                bl->owner->number);
1412
1413     /* Performance optimization: any further element can no longer affect BUF
1414        content.  */
1415
1416     if (bl->address >= bp_location_placed_address_before_address_max
1417         && memaddr + len <= (bl->address
1418                              - bp_location_placed_address_before_address_max))
1419       break;
1420
1421     if (!bp_location_has_shadow (bl))
1422       continue;
1423     if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1424                                    current_program_space->aspace, 0))
1425       continue;
1426
1427     /* Addresses and length of the part of the breakpoint that
1428        we need to copy.  */
1429     bp_addr = bl->target_info.placed_address;
1430     bp_size = bl->target_info.shadow_len;
1431
1432     if (bp_addr + bp_size <= memaddr)
1433       /* The breakpoint is entirely before the chunk of memory we
1434          are reading.  */
1435       continue;
1436
1437     if (bp_addr >= memaddr + len)
1438       /* The breakpoint is entirely after the chunk of memory we are
1439          reading.  */
1440       continue;
1441
1442     /* Offset within shadow_contents.  */
1443     if (bp_addr < memaddr)
1444       {
1445         /* Only copy the second part of the breakpoint.  */
1446         bp_size -= memaddr - bp_addr;
1447         bptoffset = memaddr - bp_addr;
1448         bp_addr = memaddr;
1449       }
1450
1451     if (bp_addr + bp_size > memaddr + len)
1452       {
1453         /* Only copy the first part of the breakpoint.  */
1454         bp_size -= (bp_addr + bp_size) - (memaddr + len);
1455       }
1456
1457     if (readbuf != NULL)
1458       {
1459         /* Verify that the readbuf buffer does not overlap with
1460            the shadow_contents buffer.  */
1461         gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1462                     || readbuf >= (bl->target_info.shadow_contents
1463                                    + bl->target_info.shadow_len));
1464
1465         /* Update the read buffer with this inserted breakpoint's
1466            shadow.  */
1467         memcpy (readbuf + bp_addr - memaddr,
1468                 bl->target_info.shadow_contents + bptoffset, bp_size);
1469       }
1470     else
1471       {
1472         struct gdbarch *gdbarch = bl->gdbarch;
1473         const unsigned char *bp;
1474         CORE_ADDR placed_address = bl->target_info.placed_address;
1475         unsigned placed_size = bl->target_info.placed_size;
1476
1477         /* Update the shadow with what we want to write to memory.  */
1478         memcpy (bl->target_info.shadow_contents + bptoffset,
1479                 writebuf_org + bp_addr - memaddr, bp_size);
1480
1481         /* Determine appropriate breakpoint contents and size for this
1482            address.  */
1483         bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1484
1485         /* Update the final write buffer with this inserted
1486            breakpoint's INSN.  */
1487         memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1488       }
1489   }
1490 }
1491 \f
1492
1493 /* Return true if BPT is either a software breakpoint or a hardware
1494    breakpoint.  */
1495
1496 int
1497 is_breakpoint (const struct breakpoint *bpt)
1498 {
1499   return (bpt->type == bp_breakpoint
1500           || bpt->type == bp_hardware_breakpoint
1501           || bpt->type == bp_dprintf);
1502 }
1503
1504 /* Return true if BPT is of any hardware watchpoint kind.  */
1505
1506 static int
1507 is_hardware_watchpoint (const struct breakpoint *bpt)
1508 {
1509   return (bpt->type == bp_hardware_watchpoint
1510           || bpt->type == bp_read_watchpoint
1511           || bpt->type == bp_access_watchpoint);
1512 }
1513
1514 /* Return true if BPT is of any watchpoint kind, hardware or
1515    software.  */
1516
1517 int
1518 is_watchpoint (const struct breakpoint *bpt)
1519 {
1520   return (is_hardware_watchpoint (bpt)
1521           || bpt->type == bp_watchpoint);
1522 }
1523
1524 /* Returns true if the current thread and its running state are safe
1525    to evaluate or update watchpoint B.  Watchpoints on local
1526    expressions need to be evaluated in the context of the thread that
1527    was current when the watchpoint was created, and, that thread needs
1528    to be stopped to be able to select the correct frame context.
1529    Watchpoints on global expressions can be evaluated on any thread,
1530    and in any state.  It is presently left to the target allowing
1531    memory accesses when threads are running.  */
1532
1533 static int
1534 watchpoint_in_thread_scope (struct watchpoint *b)
1535 {
1536   return (b->base.pspace == current_program_space
1537           && (ptid_equal (b->watchpoint_thread, null_ptid)
1538               || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1539                   && !is_executing (inferior_ptid))));
1540 }
1541
1542 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1543    associated bp_watchpoint_scope breakpoint.  */
1544
1545 static void
1546 watchpoint_del_at_next_stop (struct watchpoint *w)
1547 {
1548   struct breakpoint *b = &w->base;
1549
1550   if (b->related_breakpoint != b)
1551     {
1552       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1553       gdb_assert (b->related_breakpoint->related_breakpoint == b);
1554       b->related_breakpoint->disposition = disp_del_at_next_stop;
1555       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1556       b->related_breakpoint = b;
1557     }
1558   b->disposition = disp_del_at_next_stop;
1559 }
1560
1561 /* Assuming that B is a watchpoint:
1562    - Reparse watchpoint expression, if REPARSE is non-zero
1563    - Evaluate expression and store the result in B->val
1564    - Evaluate the condition if there is one, and store the result
1565      in b->loc->cond.
1566    - Update the list of values that must be watched in B->loc.
1567
1568    If the watchpoint disposition is disp_del_at_next_stop, then do
1569    nothing.  If this is local watchpoint that is out of scope, delete
1570    it.
1571
1572    Even with `set breakpoint always-inserted on' the watchpoints are
1573    removed + inserted on each stop here.  Normal breakpoints must
1574    never be removed because they might be missed by a running thread
1575    when debugging in non-stop mode.  On the other hand, hardware
1576    watchpoints (is_hardware_watchpoint; processed here) are specific
1577    to each LWP since they are stored in each LWP's hardware debug
1578    registers.  Therefore, such LWP must be stopped first in order to
1579    be able to modify its hardware watchpoints.
1580
1581    Hardware watchpoints must be reset exactly once after being
1582    presented to the user.  It cannot be done sooner, because it would
1583    reset the data used to present the watchpoint hit to the user.  And
1584    it must not be done later because it could display the same single
1585    watchpoint hit during multiple GDB stops.  Note that the latter is
1586    relevant only to the hardware watchpoint types bp_read_watchpoint
1587    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1588    not user-visible - its hit is suppressed if the memory content has
1589    not changed.
1590
1591    The following constraints influence the location where we can reset
1592    hardware watchpoints:
1593
1594    * target_stopped_by_watchpoint and target_stopped_data_address are
1595      called several times when GDB stops.
1596
1597    [linux] 
1598    * Multiple hardware watchpoints can be hit at the same time,
1599      causing GDB to stop.  GDB only presents one hardware watchpoint
1600      hit at a time as the reason for stopping, and all the other hits
1601      are presented later, one after the other, each time the user
1602      requests the execution to be resumed.  Execution is not resumed
1603      for the threads still having pending hit event stored in
1604      LWP_INFO->STATUS.  While the watchpoint is already removed from
1605      the inferior on the first stop the thread hit event is kept being
1606      reported from its cached value by linux_nat_stopped_data_address
1607      until the real thread resume happens after the watchpoint gets
1608      presented and thus its LWP_INFO->STATUS gets reset.
1609
1610    Therefore the hardware watchpoint hit can get safely reset on the
1611    watchpoint removal from inferior.  */
1612
1613 static void
1614 update_watchpoint (struct watchpoint *b, int reparse)
1615 {
1616   int within_current_scope;
1617   struct frame_id saved_frame_id;
1618   int frame_saved;
1619
1620   /* If this is a local watchpoint, we only want to check if the
1621      watchpoint frame is in scope if the current thread is the thread
1622      that was used to create the watchpoint.  */
1623   if (!watchpoint_in_thread_scope (b))
1624     return;
1625
1626   if (b->base.disposition == disp_del_at_next_stop)
1627     return;
1628  
1629   frame_saved = 0;
1630
1631   /* Determine if the watchpoint is within scope.  */
1632   if (b->exp_valid_block == NULL)
1633     within_current_scope = 1;
1634   else
1635     {
1636       struct frame_info *fi = get_current_frame ();
1637       struct gdbarch *frame_arch = get_frame_arch (fi);
1638       CORE_ADDR frame_pc = get_frame_pc (fi);
1639
1640       /* If we're in a function epilogue, unwinding may not work
1641          properly, so do not attempt to recreate locations at this
1642          point.  See similar comments in watchpoint_check.  */
1643       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1644         return;
1645
1646       /* Save the current frame's ID so we can restore it after
1647          evaluating the watchpoint expression on its own frame.  */
1648       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1649          took a frame parameter, so that we didn't have to change the
1650          selected frame.  */
1651       frame_saved = 1;
1652       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1653
1654       fi = frame_find_by_id (b->watchpoint_frame);
1655       within_current_scope = (fi != NULL);
1656       if (within_current_scope)
1657         select_frame (fi);
1658     }
1659
1660   /* We don't free locations.  They are stored in the bp_location array
1661      and update_global_location_list will eventually delete them and
1662      remove breakpoints if needed.  */
1663   b->base.loc = NULL;
1664
1665   if (within_current_scope && reparse)
1666     {
1667       char *s;
1668
1669       if (b->exp)
1670         {
1671           xfree (b->exp);
1672           b->exp = NULL;
1673         }
1674       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1675       b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1676       /* If the meaning of expression itself changed, the old value is
1677          no longer relevant.  We don't want to report a watchpoint hit
1678          to the user when the old value and the new value may actually
1679          be completely different objects.  */
1680       value_free (b->val);
1681       b->val = NULL;
1682       b->val_valid = 0;
1683
1684       /* Note that unlike with breakpoints, the watchpoint's condition
1685          expression is stored in the breakpoint object, not in the
1686          locations (re)created below.  */
1687       if (b->base.cond_string != NULL)
1688         {
1689           if (b->cond_exp != NULL)
1690             {
1691               xfree (b->cond_exp);
1692               b->cond_exp = NULL;
1693             }
1694
1695           s = b->base.cond_string;
1696           b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1697         }
1698     }
1699
1700   /* If we failed to parse the expression, for example because
1701      it refers to a global variable in a not-yet-loaded shared library,
1702      don't try to insert watchpoint.  We don't automatically delete
1703      such watchpoint, though, since failure to parse expression
1704      is different from out-of-scope watchpoint.  */
1705   if ( !target_has_execution)
1706     {
1707       /* Without execution, memory can't change.  No use to try and
1708          set watchpoint locations.  The watchpoint will be reset when
1709          the target gains execution, through breakpoint_re_set.  */
1710     }
1711   else if (within_current_scope && b->exp)
1712     {
1713       int pc = 0;
1714       struct value *val_chain, *v, *result, *next;
1715       struct program_space *frame_pspace;
1716
1717       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1718
1719       /* Avoid setting b->val if it's already set.  The meaning of
1720          b->val is 'the last value' user saw, and we should update
1721          it only if we reported that last value to user.  As it
1722          happens, the code that reports it updates b->val directly.
1723          We don't keep track of the memory value for masked
1724          watchpoints.  */
1725       if (!b->val_valid && !is_masked_watchpoint (&b->base))
1726         {
1727           b->val = v;
1728           b->val_valid = 1;
1729         }
1730
1731       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1732
1733       /* Look at each value on the value chain.  */
1734       for (v = val_chain; v; v = value_next (v))
1735         {
1736           /* If it's a memory location, and GDB actually needed
1737              its contents to evaluate the expression, then we
1738              must watch it.  If the first value returned is
1739              still lazy, that means an error occurred reading it;
1740              watch it anyway in case it becomes readable.  */
1741           if (VALUE_LVAL (v) == lval_memory
1742               && (v == val_chain || ! value_lazy (v)))
1743             {
1744               struct type *vtype = check_typedef (value_type (v));
1745
1746               /* We only watch structs and arrays if user asked
1747                  for it explicitly, never if they just happen to
1748                  appear in the middle of some value chain.  */
1749               if (v == result
1750                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1751                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1752                 {
1753                   CORE_ADDR addr;
1754                   int len, type;
1755                   struct bp_location *loc, **tmp;
1756
1757                   addr = value_address (v);
1758                   len = TYPE_LENGTH (value_type (v));
1759                   type = hw_write;
1760                   if (b->base.type == bp_read_watchpoint)
1761                     type = hw_read;
1762                   else if (b->base.type == bp_access_watchpoint)
1763                     type = hw_access;
1764
1765                   loc = allocate_bp_location (&b->base);
1766                   for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1767                     ;
1768                   *tmp = loc;
1769                   loc->gdbarch = get_type_arch (value_type (v));
1770
1771                   loc->pspace = frame_pspace;
1772                   loc->address = addr;
1773                   loc->length = len;
1774                   loc->watchpoint_type = type;
1775                 }
1776             }
1777         }
1778
1779       /* Change the type of breakpoint between hardware assisted or
1780          an ordinary watchpoint depending on the hardware support
1781          and free hardware slots.  REPARSE is set when the inferior
1782          is started.  */
1783       if (reparse)
1784         {
1785           int reg_cnt;
1786           enum bp_loc_type loc_type;
1787           struct bp_location *bl;
1788
1789           reg_cnt = can_use_hardware_watchpoint (val_chain);
1790
1791           if (reg_cnt)
1792             {
1793               int i, target_resources_ok, other_type_used;
1794               enum bptype type;
1795
1796               /* Use an exact watchpoint when there's only one memory region to be
1797                  watched, and only one debug register is needed to watch it.  */
1798               b->exact = target_exact_watchpoints && reg_cnt == 1;
1799
1800               /* We need to determine how many resources are already
1801                  used for all other hardware watchpoints plus this one
1802                  to see if we still have enough resources to also fit
1803                  this watchpoint in as well.  */
1804
1805               /* If this is a software watchpoint, we try to turn it
1806                  to a hardware one -- count resources as if B was of
1807                  hardware watchpoint type.  */
1808               type = b->base.type;
1809               if (type == bp_watchpoint)
1810                 type = bp_hardware_watchpoint;
1811
1812               /* This watchpoint may or may not have been placed on
1813                  the list yet at this point (it won't be in the list
1814                  if we're trying to create it for the first time,
1815                  through watch_command), so always account for it
1816                  manually.  */
1817
1818               /* Count resources used by all watchpoints except B.  */
1819               i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1820
1821               /* Add in the resources needed for B.  */
1822               i += hw_watchpoint_use_count (&b->base);
1823
1824               target_resources_ok
1825                 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1826               if (target_resources_ok <= 0)
1827                 {
1828                   int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1829
1830                   if (target_resources_ok == 0 && !sw_mode)
1831                     error (_("Target does not support this type of "
1832                              "hardware watchpoint."));
1833                   else if (target_resources_ok < 0 && !sw_mode)
1834                     error (_("There are not enough available hardware "
1835                              "resources for this watchpoint."));
1836
1837                   /* Downgrade to software watchpoint.  */
1838                   b->base.type = bp_watchpoint;
1839                 }
1840               else
1841                 {
1842                   /* If this was a software watchpoint, we've just
1843                      found we have enough resources to turn it to a
1844                      hardware watchpoint.  Otherwise, this is a
1845                      nop.  */
1846                   b->base.type = type;
1847                 }
1848             }
1849           else if (!b->base.ops->works_in_software_mode (&b->base))
1850             error (_("Expression cannot be implemented with "
1851                      "read/access watchpoint."));
1852           else
1853             b->base.type = bp_watchpoint;
1854
1855           loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1856                       : bp_loc_hardware_watchpoint);
1857           for (bl = b->base.loc; bl; bl = bl->next)
1858             bl->loc_type = loc_type;
1859         }
1860
1861       for (v = val_chain; v; v = next)
1862         {
1863           next = value_next (v);
1864           if (v != b->val)
1865             value_free (v);
1866         }
1867
1868       /* If a software watchpoint is not watching any memory, then the
1869          above left it without any location set up.  But,
1870          bpstat_stop_status requires a location to be able to report
1871          stops, so make sure there's at least a dummy one.  */
1872       if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1873         {
1874           struct breakpoint *base = &b->base;
1875           base->loc = allocate_bp_location (base);
1876           base->loc->pspace = frame_pspace;
1877           base->loc->address = -1;
1878           base->loc->length = -1;
1879           base->loc->watchpoint_type = -1;
1880         }
1881     }
1882   else if (!within_current_scope)
1883     {
1884       printf_filtered (_("\
1885 Watchpoint %d deleted because the program has left the block\n\
1886 in which its expression is valid.\n"),
1887                        b->base.number);
1888       watchpoint_del_at_next_stop (b);
1889     }
1890
1891   /* Restore the selected frame.  */
1892   if (frame_saved)
1893     select_frame (frame_find_by_id (saved_frame_id));
1894 }
1895
1896
1897 /* Returns 1 iff breakpoint location should be
1898    inserted in the inferior.  We don't differentiate the type of BL's owner
1899    (breakpoint vs. tracepoint), although insert_location in tracepoint's
1900    breakpoint_ops is not defined, because in insert_bp_location,
1901    tracepoint's insert_location will not be called.  */
1902 static int
1903 should_be_inserted (struct bp_location *bl)
1904 {
1905   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1906     return 0;
1907
1908   if (bl->owner->disposition == disp_del_at_next_stop)
1909     return 0;
1910
1911   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1912     return 0;
1913
1914   if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1915     return 0;
1916
1917   /* This is set for example, when we're attached to the parent of a
1918      vfork, and have detached from the child.  The child is running
1919      free, and we expect it to do an exec or exit, at which point the
1920      OS makes the parent schedulable again (and the target reports
1921      that the vfork is done).  Until the child is done with the shared
1922      memory region, do not insert breakpoints in the parent, otherwise
1923      the child could still trip on the parent's breakpoints.  Since
1924      the parent is blocked anyway, it won't miss any breakpoint.  */
1925   if (bl->pspace->breakpoints_not_allowed)
1926     return 0;
1927
1928   return 1;
1929 }
1930
1931 /* Same as should_be_inserted but does the check assuming
1932    that the location is not duplicated.  */
1933
1934 static int
1935 unduplicated_should_be_inserted (struct bp_location *bl)
1936 {
1937   int result;
1938   const int save_duplicate = bl->duplicate;
1939
1940   bl->duplicate = 0;
1941   result = should_be_inserted (bl);
1942   bl->duplicate = save_duplicate;
1943   return result;
1944 }
1945
1946 /* Parses a conditional described by an expression COND into an
1947    agent expression bytecode suitable for evaluation
1948    by the bytecode interpreter.  Return NULL if there was
1949    any error during parsing.  */
1950
1951 static struct agent_expr *
1952 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
1953 {
1954   struct agent_expr *aexpr = NULL;
1955   struct cleanup *old_chain = NULL;
1956   volatile struct gdb_exception ex;
1957
1958   if (!cond)
1959     return NULL;
1960
1961   /* We don't want to stop processing, so catch any errors
1962      that may show up.  */
1963   TRY_CATCH (ex, RETURN_MASK_ERROR)
1964     {
1965       aexpr = gen_eval_for_expr (scope, cond);
1966     }
1967
1968   if (ex.reason < 0)
1969     {
1970       /* If we got here, it means the condition could not be parsed to a valid
1971          bytecode expression and thus can't be evaluated on the target's side.
1972          It's no use iterating through the conditions.  */
1973       return NULL;
1974     }
1975
1976   /* We have a valid agent expression.  */
1977   return aexpr;
1978 }
1979
1980 /* Based on location BL, create a list of breakpoint conditions to be
1981    passed on to the target.  If we have duplicated locations with different
1982    conditions, we will add such conditions to the list.  The idea is that the
1983    target will evaluate the list of conditions and will only notify GDB when
1984    one of them is true.  */
1985
1986 static void
1987 build_target_condition_list (struct bp_location *bl)
1988 {
1989   struct bp_location **locp = NULL, **loc2p;
1990   int null_condition_or_parse_error = 0;
1991   int modified = bl->needs_update;
1992   struct bp_location *loc;
1993
1994   /* This is only meaningful if the target is
1995      evaluating conditions and if the user has
1996      opted for condition evaluation on the target's
1997      side.  */
1998   if (gdb_evaluates_breakpoint_condition_p ()
1999       || !target_supports_evaluation_of_breakpoint_conditions ())
2000     return;
2001
2002   /* Do a first pass to check for locations with no assigned
2003      conditions or conditions that fail to parse to a valid agent expression
2004      bytecode.  If any of these happen, then it's no use to send conditions
2005      to the target since this location will always trigger and generate a
2006      response back to GDB.  */
2007   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2008     {
2009       loc = (*loc2p);
2010       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2011         {
2012           if (modified)
2013             {
2014               struct agent_expr *aexpr;
2015
2016               /* Re-parse the conditions since something changed.  In that
2017                  case we already freed the condition bytecodes (see
2018                  force_breakpoint_reinsertion).  We just
2019                  need to parse the condition to bytecodes again.  */
2020               aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2021               loc->cond_bytecode = aexpr;
2022
2023               /* Check if we managed to parse the conditional expression
2024                  correctly.  If not, we will not send this condition
2025                  to the target.  */
2026               if (aexpr)
2027                 continue;
2028             }
2029
2030           /* If we have a NULL bytecode expression, it means something
2031              went wrong or we have a null condition expression.  */
2032           if (!loc->cond_bytecode)
2033             {
2034               null_condition_or_parse_error = 1;
2035               break;
2036             }
2037         }
2038     }
2039
2040   /* If any of these happened, it means we will have to evaluate the conditions
2041      for the location's address on gdb's side.  It is no use keeping bytecodes
2042      for all the other duplicate locations, thus we free all of them here.
2043
2044      This is so we have a finer control over which locations' conditions are
2045      being evaluated by GDB or the remote stub.  */
2046   if (null_condition_or_parse_error)
2047     {
2048       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2049         {
2050           loc = (*loc2p);
2051           if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2052             {
2053               /* Only go as far as the first NULL bytecode is
2054                  located.  */
2055               if (!loc->cond_bytecode)
2056                 return;
2057
2058               free_agent_expr (loc->cond_bytecode);
2059               loc->cond_bytecode = NULL;
2060             }
2061         }
2062     }
2063
2064   /* No NULL conditions or failed bytecode generation.  Build a condition list
2065      for this location's address.  */
2066   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2067     {
2068       loc = (*loc2p);
2069       if (loc->cond
2070           && is_breakpoint (loc->owner)
2071           && loc->pspace->num == bl->pspace->num
2072           && loc->owner->enable_state == bp_enabled
2073           && loc->enabled)
2074         /* Add the condition to the vector.  This will be used later to send the
2075            conditions to the target.  */
2076         VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2077                        loc->cond_bytecode);
2078     }
2079
2080   return;
2081 }
2082
2083 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
2084    location.  Any error messages are printed to TMP_ERROR_STREAM; and
2085    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2086    Returns 0 for success, 1 if the bp_location type is not supported or
2087    -1 for failure.
2088
2089    NOTE drow/2003-09-09: This routine could be broken down to an
2090    object-style method for each breakpoint or catchpoint type.  */
2091 static int
2092 insert_bp_location (struct bp_location *bl,
2093                     struct ui_file *tmp_error_stream,
2094                     int *disabled_breaks,
2095                     int *hw_breakpoint_error)
2096 {
2097   int val = 0;
2098
2099   if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2100     return 0;
2101
2102   /* Note we don't initialize bl->target_info, as that wipes out
2103      the breakpoint location's shadow_contents if the breakpoint
2104      is still inserted at that location.  This in turn breaks
2105      target_read_memory which depends on these buffers when
2106      a memory read is requested at the breakpoint location:
2107      Once the target_info has been wiped, we fail to see that
2108      we have a breakpoint inserted at that address and thus
2109      read the breakpoint instead of returning the data saved in
2110      the breakpoint location's shadow contents.  */
2111   bl->target_info.placed_address = bl->address;
2112   bl->target_info.placed_address_space = bl->pspace->aspace;
2113   bl->target_info.length = bl->length;
2114
2115   /* When working with target-side conditions, we must pass all the conditions
2116      for the same breakpoint address down to the target since GDB will not
2117      insert those locations.  With a list of breakpoint conditions, the target
2118      can decide when to stop and notify GDB.  */
2119
2120   if (is_breakpoint (bl->owner))
2121     {
2122       build_target_condition_list (bl);
2123       /* Reset the condition modification marker.  */
2124       bl->needs_update = 0;
2125     }
2126
2127   if (bl->loc_type == bp_loc_software_breakpoint
2128       || bl->loc_type == bp_loc_hardware_breakpoint)
2129     {
2130       if (bl->owner->type != bp_hardware_breakpoint)
2131         {
2132           /* If the explicitly specified breakpoint type
2133              is not hardware breakpoint, check the memory map to see
2134              if the breakpoint address is in read only memory or not.
2135
2136              Two important cases are:
2137              - location type is not hardware breakpoint, memory
2138              is readonly.  We change the type of the location to
2139              hardware breakpoint.
2140              - location type is hardware breakpoint, memory is
2141              read-write.  This means we've previously made the
2142              location hardware one, but then the memory map changed,
2143              so we undo.
2144              
2145              When breakpoints are removed, remove_breakpoints will use
2146              location types we've just set here, the only possible
2147              problem is that memory map has changed during running
2148              program, but it's not going to work anyway with current
2149              gdb.  */
2150           struct mem_region *mr 
2151             = lookup_mem_region (bl->target_info.placed_address);
2152           
2153           if (mr)
2154             {
2155               if (automatic_hardware_breakpoints)
2156                 {
2157                   enum bp_loc_type new_type;
2158                   
2159                   if (mr->attrib.mode != MEM_RW)
2160                     new_type = bp_loc_hardware_breakpoint;
2161                   else 
2162                     new_type = bp_loc_software_breakpoint;
2163                   
2164                   if (new_type != bl->loc_type)
2165                     {
2166                       static int said = 0;
2167
2168                       bl->loc_type = new_type;
2169                       if (!said)
2170                         {
2171                           fprintf_filtered (gdb_stdout,
2172                                             _("Note: automatically using "
2173                                               "hardware breakpoints for "
2174                                               "read-only addresses.\n"));
2175                           said = 1;
2176                         }
2177                     }
2178                 }
2179               else if (bl->loc_type == bp_loc_software_breakpoint
2180                        && mr->attrib.mode != MEM_RW)        
2181                 warning (_("cannot set software breakpoint "
2182                            "at readonly address %s"),
2183                          paddress (bl->gdbarch, bl->address));
2184             }
2185         }
2186         
2187       /* First check to see if we have to handle an overlay.  */
2188       if (overlay_debugging == ovly_off
2189           || bl->section == NULL
2190           || !(section_is_overlay (bl->section)))
2191         {
2192           /* No overlay handling: just set the breakpoint.  */
2193
2194           val = bl->owner->ops->insert_location (bl);
2195         }
2196       else
2197         {
2198           /* This breakpoint is in an overlay section.
2199              Shall we set a breakpoint at the LMA?  */
2200           if (!overlay_events_enabled)
2201             {
2202               /* Yes -- overlay event support is not active, 
2203                  so we must try to set a breakpoint at the LMA.
2204                  This will not work for a hardware breakpoint.  */
2205               if (bl->loc_type == bp_loc_hardware_breakpoint)
2206                 warning (_("hardware breakpoint %d not supported in overlay!"),
2207                          bl->owner->number);
2208               else
2209                 {
2210                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
2211                                                              bl->section);
2212                   /* Set a software (trap) breakpoint at the LMA.  */
2213                   bl->overlay_target_info = bl->target_info;
2214                   bl->overlay_target_info.placed_address = addr;
2215                   val = target_insert_breakpoint (bl->gdbarch,
2216                                                   &bl->overlay_target_info);
2217                   if (val != 0)
2218                     fprintf_unfiltered (tmp_error_stream,
2219                                         "Overlay breakpoint %d "
2220                                         "failed: in ROM?\n",
2221                                         bl->owner->number);
2222                 }
2223             }
2224           /* Shall we set a breakpoint at the VMA? */
2225           if (section_is_mapped (bl->section))
2226             {
2227               /* Yes.  This overlay section is mapped into memory.  */
2228               val = bl->owner->ops->insert_location (bl);
2229             }
2230           else
2231             {
2232               /* No.  This breakpoint will not be inserted.  
2233                  No error, but do not mark the bp as 'inserted'.  */
2234               return 0;
2235             }
2236         }
2237
2238       if (val)
2239         {
2240           /* Can't set the breakpoint.  */
2241           if (solib_name_from_address (bl->pspace, bl->address))
2242             {
2243               /* See also: disable_breakpoints_in_shlibs.  */
2244               val = 0;
2245               bl->shlib_disabled = 1;
2246               observer_notify_breakpoint_modified (bl->owner);
2247               if (!*disabled_breaks)
2248                 {
2249                   fprintf_unfiltered (tmp_error_stream, 
2250                                       "Cannot insert breakpoint %d.\n", 
2251                                       bl->owner->number);
2252                   fprintf_unfiltered (tmp_error_stream, 
2253                                       "Temporarily disabling shared "
2254                                       "library breakpoints:\n");
2255                 }
2256               *disabled_breaks = 1;
2257               fprintf_unfiltered (tmp_error_stream,
2258                                   "breakpoint #%d\n", bl->owner->number);
2259             }
2260           else
2261             {
2262               if (bl->loc_type == bp_loc_hardware_breakpoint)
2263                 {
2264                   *hw_breakpoint_error = 1;
2265                   fprintf_unfiltered (tmp_error_stream,
2266                                       "Cannot insert hardware "
2267                                       "breakpoint %d.\n",
2268                                       bl->owner->number);
2269                 }
2270               else
2271                 {
2272                   fprintf_unfiltered (tmp_error_stream, 
2273                                       "Cannot insert breakpoint %d.\n", 
2274                                       bl->owner->number);
2275                   fprintf_filtered (tmp_error_stream, 
2276                                     "Error accessing memory address ");
2277                   fputs_filtered (paddress (bl->gdbarch, bl->address),
2278                                   tmp_error_stream);
2279                   fprintf_filtered (tmp_error_stream, ": %s.\n",
2280                                     safe_strerror (val));
2281                 }
2282
2283             }
2284         }
2285       else
2286         bl->inserted = 1;
2287
2288       return val;
2289     }
2290
2291   else if (bl->loc_type == bp_loc_hardware_watchpoint
2292            /* NOTE drow/2003-09-08: This state only exists for removing
2293               watchpoints.  It's not clear that it's necessary...  */
2294            && bl->owner->disposition != disp_del_at_next_stop)
2295     {
2296       gdb_assert (bl->owner->ops != NULL
2297                   && bl->owner->ops->insert_location != NULL);
2298
2299       val = bl->owner->ops->insert_location (bl);
2300
2301       /* If trying to set a read-watchpoint, and it turns out it's not
2302          supported, try emulating one with an access watchpoint.  */
2303       if (val == 1 && bl->watchpoint_type == hw_read)
2304         {
2305           struct bp_location *loc, **loc_temp;
2306
2307           /* But don't try to insert it, if there's already another
2308              hw_access location that would be considered a duplicate
2309              of this one.  */
2310           ALL_BP_LOCATIONS (loc, loc_temp)
2311             if (loc != bl
2312                 && loc->watchpoint_type == hw_access
2313                 && watchpoint_locations_match (bl, loc))
2314               {
2315                 bl->duplicate = 1;
2316                 bl->inserted = 1;
2317                 bl->target_info = loc->target_info;
2318                 bl->watchpoint_type = hw_access;
2319                 val = 0;
2320                 break;
2321               }
2322
2323           if (val == 1)
2324             {
2325               bl->watchpoint_type = hw_access;
2326               val = bl->owner->ops->insert_location (bl);
2327
2328               if (val)
2329                 /* Back to the original value.  */
2330                 bl->watchpoint_type = hw_read;
2331             }
2332         }
2333
2334       bl->inserted = (val == 0);
2335     }
2336
2337   else if (bl->owner->type == bp_catchpoint)
2338     {
2339       gdb_assert (bl->owner->ops != NULL
2340                   && bl->owner->ops->insert_location != NULL);
2341
2342       val = bl->owner->ops->insert_location (bl);
2343       if (val)
2344         {
2345           bl->owner->enable_state = bp_disabled;
2346
2347           if (val == 1)
2348             warning (_("\
2349 Error inserting catchpoint %d: Your system does not support this type\n\
2350 of catchpoint."), bl->owner->number);
2351           else
2352             warning (_("Error inserting catchpoint %d."), bl->owner->number);
2353         }
2354
2355       bl->inserted = (val == 0);
2356
2357       /* We've already printed an error message if there was a problem
2358          inserting this catchpoint, and we've disabled the catchpoint,
2359          so just return success.  */
2360       return 0;
2361     }
2362
2363   return 0;
2364 }
2365
2366 /* This function is called when program space PSPACE is about to be
2367    deleted.  It takes care of updating breakpoints to not reference
2368    PSPACE anymore.  */
2369
2370 void
2371 breakpoint_program_space_exit (struct program_space *pspace)
2372 {
2373   struct breakpoint *b, *b_temp;
2374   struct bp_location *loc, **loc_temp;
2375
2376   /* Remove any breakpoint that was set through this program space.  */
2377   ALL_BREAKPOINTS_SAFE (b, b_temp)
2378     {
2379       if (b->pspace == pspace)
2380         delete_breakpoint (b);
2381     }
2382
2383   /* Breakpoints set through other program spaces could have locations
2384      bound to PSPACE as well.  Remove those.  */
2385   ALL_BP_LOCATIONS (loc, loc_temp)
2386     {
2387       struct bp_location *tmp;
2388
2389       if (loc->pspace == pspace)
2390         {
2391           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
2392           if (loc->owner->loc == loc)
2393             loc->owner->loc = loc->next;
2394           else
2395             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2396               if (tmp->next == loc)
2397                 {
2398                   tmp->next = loc->next;
2399                   break;
2400                 }
2401         }
2402     }
2403
2404   /* Now update the global location list to permanently delete the
2405      removed locations above.  */
2406   update_global_location_list (0);
2407 }
2408
2409 /* Make sure all breakpoints are inserted in inferior.
2410    Throws exception on any error.
2411    A breakpoint that is already inserted won't be inserted
2412    again, so calling this function twice is safe.  */
2413 void
2414 insert_breakpoints (void)
2415 {
2416   struct breakpoint *bpt;
2417
2418   ALL_BREAKPOINTS (bpt)
2419     if (is_hardware_watchpoint (bpt))
2420       {
2421         struct watchpoint *w = (struct watchpoint *) bpt;
2422
2423         update_watchpoint (w, 0 /* don't reparse.  */);
2424       }
2425
2426   update_global_location_list (1);
2427
2428   /* update_global_location_list does not insert breakpoints when
2429      always_inserted_mode is not enabled.  Explicitly insert them
2430      now.  */
2431   if (!breakpoints_always_inserted_mode ())
2432     insert_breakpoint_locations ();
2433 }
2434
2435 /* Invoke CALLBACK for each of bp_location.  */
2436
2437 void
2438 iterate_over_bp_locations (walk_bp_location_callback callback)
2439 {
2440   struct bp_location *loc, **loc_tmp;
2441
2442   ALL_BP_LOCATIONS (loc, loc_tmp)
2443     {
2444       callback (loc, NULL);
2445     }
2446 }
2447
2448 /* This is used when we need to synch breakpoint conditions between GDB and the
2449    target.  It is the case with deleting and disabling of breakpoints when using
2450    always-inserted mode.  */
2451
2452 static void
2453 update_inserted_breakpoint_locations (void)
2454 {
2455   struct bp_location *bl, **blp_tmp;
2456   int error_flag = 0;
2457   int val = 0;
2458   int disabled_breaks = 0;
2459   int hw_breakpoint_error = 0;
2460
2461   struct ui_file *tmp_error_stream = mem_fileopen ();
2462   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2463
2464   /* Explicitly mark the warning -- this will only be printed if
2465      there was an error.  */
2466   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2467
2468   save_current_space_and_thread ();
2469
2470   ALL_BP_LOCATIONS (bl, blp_tmp)
2471     {
2472       /* We only want to update software breakpoints and hardware
2473          breakpoints.  */
2474       if (!is_breakpoint (bl->owner))
2475         continue;
2476
2477       /* We only want to update locations that are already inserted
2478          and need updating.  This is to avoid unwanted insertion during
2479          deletion of breakpoints.  */
2480       if (!bl->inserted || (bl->inserted && !bl->needs_update))
2481         continue;
2482
2483       switch_to_program_space_and_thread (bl->pspace);
2484
2485       /* For targets that support global breakpoints, there's no need
2486          to select an inferior to insert breakpoint to.  In fact, even
2487          if we aren't attached to any process yet, we should still
2488          insert breakpoints.  */
2489       if (!gdbarch_has_global_breakpoints (target_gdbarch)
2490           && ptid_equal (inferior_ptid, null_ptid))
2491         continue;
2492
2493       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2494                                     &hw_breakpoint_error);
2495       if (val)
2496         error_flag = val;
2497     }
2498
2499   if (error_flag)
2500     {
2501       target_terminal_ours_for_output ();
2502       error_stream (tmp_error_stream);
2503     }
2504
2505   do_cleanups (cleanups);
2506 }
2507
2508 /* Used when starting or continuing the program.  */
2509
2510 static void
2511 insert_breakpoint_locations (void)
2512 {
2513   struct breakpoint *bpt;
2514   struct bp_location *bl, **blp_tmp;
2515   int error_flag = 0;
2516   int val = 0;
2517   int disabled_breaks = 0;
2518   int hw_breakpoint_error = 0;
2519
2520   struct ui_file *tmp_error_stream = mem_fileopen ();
2521   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2522   
2523   /* Explicitly mark the warning -- this will only be printed if
2524      there was an error.  */
2525   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2526
2527   save_current_space_and_thread ();
2528
2529   ALL_BP_LOCATIONS (bl, blp_tmp)
2530     {
2531       if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2532         continue;
2533
2534       /* There is no point inserting thread-specific breakpoints if
2535          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
2536          has BL->OWNER always non-NULL.  */
2537       if (bl->owner->thread != -1
2538           && !valid_thread_id (bl->owner->thread))
2539         continue;
2540
2541       switch_to_program_space_and_thread (bl->pspace);
2542
2543       /* For targets that support global breakpoints, there's no need
2544          to select an inferior to insert breakpoint to.  In fact, even
2545          if we aren't attached to any process yet, we should still
2546          insert breakpoints.  */
2547       if (!gdbarch_has_global_breakpoints (target_gdbarch)
2548           && ptid_equal (inferior_ptid, null_ptid))
2549         continue;
2550
2551       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2552                                     &hw_breakpoint_error);
2553       if (val)
2554         error_flag = val;
2555     }
2556
2557   /* If we failed to insert all locations of a watchpoint, remove
2558      them, as half-inserted watchpoint is of limited use.  */
2559   ALL_BREAKPOINTS (bpt)  
2560     {
2561       int some_failed = 0;
2562       struct bp_location *loc;
2563
2564       if (!is_hardware_watchpoint (bpt))
2565         continue;
2566
2567       if (!breakpoint_enabled (bpt))
2568         continue;
2569
2570       if (bpt->disposition == disp_del_at_next_stop)
2571         continue;
2572       
2573       for (loc = bpt->loc; loc; loc = loc->next)
2574         if (!loc->inserted && should_be_inserted (loc))
2575           {
2576             some_failed = 1;
2577             break;
2578           }
2579       if (some_failed)
2580         {
2581           for (loc = bpt->loc; loc; loc = loc->next)
2582             if (loc->inserted)
2583               remove_breakpoint (loc, mark_uninserted);
2584
2585           hw_breakpoint_error = 1;
2586           fprintf_unfiltered (tmp_error_stream,
2587                               "Could not insert hardware watchpoint %d.\n", 
2588                               bpt->number);
2589           error_flag = -1;
2590         }
2591     }
2592
2593   if (error_flag)
2594     {
2595       /* If a hardware breakpoint or watchpoint was inserted, add a
2596          message about possibly exhausted resources.  */
2597       if (hw_breakpoint_error)
2598         {
2599           fprintf_unfiltered (tmp_error_stream, 
2600                               "Could not insert hardware breakpoints:\n\
2601 You may have requested too many hardware breakpoints/watchpoints.\n");
2602         }
2603       target_terminal_ours_for_output ();
2604       error_stream (tmp_error_stream);
2605     }
2606
2607   do_cleanups (cleanups);
2608 }
2609
2610 /* Used when the program stops.
2611    Returns zero if successful, or non-zero if there was a problem
2612    removing a breakpoint location.  */
2613
2614 int
2615 remove_breakpoints (void)
2616 {
2617   struct bp_location *bl, **blp_tmp;
2618   int val = 0;
2619
2620   ALL_BP_LOCATIONS (bl, blp_tmp)
2621   {
2622     if (bl->inserted && !is_tracepoint (bl->owner))
2623       val |= remove_breakpoint (bl, mark_uninserted);
2624   }
2625   return val;
2626 }
2627
2628 /* Remove breakpoints of process PID.  */
2629
2630 int
2631 remove_breakpoints_pid (int pid)
2632 {
2633   struct bp_location *bl, **blp_tmp;
2634   int val;
2635   struct inferior *inf = find_inferior_pid (pid);
2636
2637   ALL_BP_LOCATIONS (bl, blp_tmp)
2638   {
2639     if (bl->pspace != inf->pspace)
2640       continue;
2641
2642     if (bl->inserted)
2643       {
2644         val = remove_breakpoint (bl, mark_uninserted);
2645         if (val != 0)
2646           return val;
2647       }
2648   }
2649   return 0;
2650 }
2651
2652 int
2653 reattach_breakpoints (int pid)
2654 {
2655   struct cleanup *old_chain;
2656   struct bp_location *bl, **blp_tmp;
2657   int val;
2658   struct ui_file *tmp_error_stream;
2659   int dummy1 = 0, dummy2 = 0;
2660   struct inferior *inf;
2661   struct thread_info *tp;
2662
2663   tp = any_live_thread_of_process (pid);
2664   if (tp == NULL)
2665     return 1;
2666
2667   inf = find_inferior_pid (pid);
2668   old_chain = save_inferior_ptid ();
2669
2670   inferior_ptid = tp->ptid;
2671
2672   tmp_error_stream = mem_fileopen ();
2673   make_cleanup_ui_file_delete (tmp_error_stream);
2674
2675   ALL_BP_LOCATIONS (bl, blp_tmp)
2676   {
2677     if (bl->pspace != inf->pspace)
2678       continue;
2679
2680     if (bl->inserted)
2681       {
2682         bl->inserted = 0;
2683         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2684         if (val != 0)
2685           {
2686             do_cleanups (old_chain);
2687             return val;
2688           }
2689       }
2690   }
2691   do_cleanups (old_chain);
2692   return 0;
2693 }
2694
2695 static int internal_breakpoint_number = -1;
2696
2697 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2698    If INTERNAL is non-zero, the breakpoint number will be populated
2699    from internal_breakpoint_number and that variable decremented.
2700    Otherwise the breakpoint number will be populated from
2701    breakpoint_count and that value incremented.  Internal breakpoints
2702    do not set the internal var bpnum.  */
2703 static void
2704 set_breakpoint_number (int internal, struct breakpoint *b)
2705 {
2706   if (internal)
2707     b->number = internal_breakpoint_number--;
2708   else
2709     {
2710       set_breakpoint_count (breakpoint_count + 1);
2711       b->number = breakpoint_count;
2712     }
2713 }
2714
2715 static struct breakpoint *
2716 create_internal_breakpoint (struct gdbarch *gdbarch,
2717                             CORE_ADDR address, enum bptype type,
2718                             const struct breakpoint_ops *ops)
2719 {
2720   struct symtab_and_line sal;
2721   struct breakpoint *b;
2722
2723   init_sal (&sal);              /* Initialize to zeroes.  */
2724
2725   sal.pc = address;
2726   sal.section = find_pc_overlay (sal.pc);
2727   sal.pspace = current_program_space;
2728
2729   b = set_raw_breakpoint (gdbarch, sal, type, ops);
2730   b->number = internal_breakpoint_number--;
2731   b->disposition = disp_donttouch;
2732
2733   return b;
2734 }
2735
2736 static const char *const longjmp_names[] =
2737   {
2738     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2739   };
2740 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2741
2742 /* Per-objfile data private to breakpoint.c.  */
2743 struct breakpoint_objfile_data
2744 {
2745   /* Minimal symbol for "_ovly_debug_event" (if any).  */
2746   struct minimal_symbol *overlay_msym;
2747
2748   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
2749   struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2750
2751   /* True if we have looked for longjmp probes.  */
2752   int longjmp_searched;
2753
2754   /* SystemTap probe points for longjmp (if any).  */
2755   VEC (probe_p) *longjmp_probes;
2756
2757   /* Minimal symbol for "std::terminate()" (if any).  */
2758   struct minimal_symbol *terminate_msym;
2759
2760   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
2761   struct minimal_symbol *exception_msym;
2762
2763   /* True if we have looked for exception probes.  */
2764   int exception_searched;
2765
2766   /* SystemTap probe points for unwinding (if any).  */
2767   VEC (probe_p) *exception_probes;
2768 };
2769
2770 static const struct objfile_data *breakpoint_objfile_key;
2771
2772 /* Minimal symbol not found sentinel.  */
2773 static struct minimal_symbol msym_not_found;
2774
2775 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
2776
2777 static int
2778 msym_not_found_p (const struct minimal_symbol *msym)
2779 {
2780   return msym == &msym_not_found;
2781 }
2782
2783 /* Return per-objfile data needed by breakpoint.c.
2784    Allocate the data if necessary.  */
2785
2786 static struct breakpoint_objfile_data *
2787 get_breakpoint_objfile_data (struct objfile *objfile)
2788 {
2789   struct breakpoint_objfile_data *bp_objfile_data;
2790
2791   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2792   if (bp_objfile_data == NULL)
2793     {
2794       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2795                                        sizeof (*bp_objfile_data));
2796
2797       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2798       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2799     }
2800   return bp_objfile_data;
2801 }
2802
2803 static void
2804 free_breakpoint_probes (struct objfile *obj, void *data)
2805 {
2806   struct breakpoint_objfile_data *bp_objfile_data = data;
2807
2808   VEC_free (probe_p, bp_objfile_data->longjmp_probes);
2809   VEC_free (probe_p, bp_objfile_data->exception_probes);
2810 }
2811
2812 static void
2813 create_overlay_event_breakpoint (void)
2814 {
2815   struct objfile *objfile;
2816   const char *const func_name = "_ovly_debug_event";
2817
2818   ALL_OBJFILES (objfile)
2819     {
2820       struct breakpoint *b;
2821       struct breakpoint_objfile_data *bp_objfile_data;
2822       CORE_ADDR addr;
2823
2824       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2825
2826       if (msym_not_found_p (bp_objfile_data->overlay_msym))
2827         continue;
2828
2829       if (bp_objfile_data->overlay_msym == NULL)
2830         {
2831           struct minimal_symbol *m;
2832
2833           m = lookup_minimal_symbol_text (func_name, objfile);
2834           if (m == NULL)
2835             {
2836               /* Avoid future lookups in this objfile.  */
2837               bp_objfile_data->overlay_msym = &msym_not_found;
2838               continue;
2839             }
2840           bp_objfile_data->overlay_msym = m;
2841         }
2842
2843       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2844       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2845                                       bp_overlay_event,
2846                                       &internal_breakpoint_ops);
2847       b->addr_string = xstrdup (func_name);
2848
2849       if (overlay_debugging == ovly_auto)
2850         {
2851           b->enable_state = bp_enabled;
2852           overlay_events_enabled = 1;
2853         }
2854       else
2855        {
2856          b->enable_state = bp_disabled;
2857          overlay_events_enabled = 0;
2858        }
2859     }
2860   update_global_location_list (1);
2861 }
2862
2863 static void
2864 create_longjmp_master_breakpoint (void)
2865 {
2866   struct program_space *pspace;
2867   struct cleanup *old_chain;
2868
2869   old_chain = save_current_program_space ();
2870
2871   ALL_PSPACES (pspace)
2872   {
2873     struct objfile *objfile;
2874
2875     set_current_program_space (pspace);
2876
2877     ALL_OBJFILES (objfile)
2878     {
2879       int i;
2880       struct gdbarch *gdbarch;
2881       struct breakpoint_objfile_data *bp_objfile_data;
2882
2883       gdbarch = get_objfile_arch (objfile);
2884       if (!gdbarch_get_longjmp_target_p (gdbarch))
2885         continue;
2886
2887       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2888
2889       if (!bp_objfile_data->longjmp_searched)
2890         {
2891           bp_objfile_data->longjmp_probes
2892             = find_probes_in_objfile (objfile, "libc", "longjmp");
2893           bp_objfile_data->longjmp_searched = 1;
2894         }
2895
2896       if (bp_objfile_data->longjmp_probes != NULL)
2897         {
2898           int i;
2899           struct probe *probe;
2900           struct gdbarch *gdbarch = get_objfile_arch (objfile);
2901
2902           for (i = 0;
2903                VEC_iterate (probe_p,
2904                             bp_objfile_data->longjmp_probes,
2905                             i, probe);
2906                ++i)
2907             {
2908               struct breakpoint *b;
2909
2910               b = create_internal_breakpoint (gdbarch, probe->address,
2911                                               bp_longjmp_master,
2912                                               &internal_breakpoint_ops);
2913               b->addr_string = xstrdup ("-probe-stap libc:longjmp");
2914               b->enable_state = bp_disabled;
2915             }
2916
2917           continue;
2918         }
2919
2920       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2921         {
2922           struct breakpoint *b;
2923           const char *func_name;
2924           CORE_ADDR addr;
2925
2926           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2927             continue;
2928
2929           func_name = longjmp_names[i];
2930           if (bp_objfile_data->longjmp_msym[i] == NULL)
2931             {
2932               struct minimal_symbol *m;
2933
2934               m = lookup_minimal_symbol_text (func_name, objfile);
2935               if (m == NULL)
2936                 {
2937                   /* Prevent future lookups in this objfile.  */
2938                   bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2939                   continue;
2940                 }
2941               bp_objfile_data->longjmp_msym[i] = m;
2942             }
2943
2944           addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2945           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2946                                           &internal_breakpoint_ops);
2947           b->addr_string = xstrdup (func_name);
2948           b->enable_state = bp_disabled;
2949         }
2950     }
2951   }
2952   update_global_location_list (1);
2953
2954   do_cleanups (old_chain);
2955 }
2956
2957 /* Create a master std::terminate breakpoint.  */
2958 static void
2959 create_std_terminate_master_breakpoint (void)
2960 {
2961   struct program_space *pspace;
2962   struct cleanup *old_chain;
2963   const char *const func_name = "std::terminate()";
2964
2965   old_chain = save_current_program_space ();
2966
2967   ALL_PSPACES (pspace)
2968   {
2969     struct objfile *objfile;
2970     CORE_ADDR addr;
2971
2972     set_current_program_space (pspace);
2973
2974     ALL_OBJFILES (objfile)
2975     {
2976       struct breakpoint *b;
2977       struct breakpoint_objfile_data *bp_objfile_data;
2978
2979       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2980
2981       if (msym_not_found_p (bp_objfile_data->terminate_msym))
2982         continue;
2983
2984       if (bp_objfile_data->terminate_msym == NULL)
2985         {
2986           struct minimal_symbol *m;
2987
2988           m = lookup_minimal_symbol (func_name, NULL, objfile);
2989           if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2990                             && MSYMBOL_TYPE (m) != mst_file_text))
2991             {
2992               /* Prevent future lookups in this objfile.  */
2993               bp_objfile_data->terminate_msym = &msym_not_found;
2994               continue;
2995             }
2996           bp_objfile_data->terminate_msym = m;
2997         }
2998
2999       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3000       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3001                                       bp_std_terminate_master,
3002                                       &internal_breakpoint_ops);
3003       b->addr_string = xstrdup (func_name);
3004       b->enable_state = bp_disabled;
3005     }
3006   }
3007
3008   update_global_location_list (1);
3009
3010   do_cleanups (old_chain);
3011 }
3012
3013 /* Install a master breakpoint on the unwinder's debug hook.  */
3014
3015 static void
3016 create_exception_master_breakpoint (void)
3017 {
3018   struct objfile *objfile;
3019   const char *const func_name = "_Unwind_DebugHook";
3020
3021   ALL_OBJFILES (objfile)
3022     {
3023       struct breakpoint *b;
3024       struct gdbarch *gdbarch;
3025       struct breakpoint_objfile_data *bp_objfile_data;
3026       CORE_ADDR addr;
3027
3028       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3029
3030       /* We prefer the SystemTap probe point if it exists.  */
3031       if (!bp_objfile_data->exception_searched)
3032         {
3033           bp_objfile_data->exception_probes
3034             = find_probes_in_objfile (objfile, "libgcc", "unwind");
3035           bp_objfile_data->exception_searched = 1;
3036         }
3037
3038       if (bp_objfile_data->exception_probes != NULL)
3039         {
3040           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3041           int i;
3042           struct probe *probe;
3043
3044           for (i = 0;
3045                VEC_iterate (probe_p,
3046                             bp_objfile_data->exception_probes,
3047                             i, probe);
3048                ++i)
3049             {
3050               struct breakpoint *b;
3051
3052               b = create_internal_breakpoint (gdbarch, probe->address,
3053                                               bp_exception_master,
3054                                               &internal_breakpoint_ops);
3055               b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3056               b->enable_state = bp_disabled;
3057             }
3058
3059           continue;
3060         }
3061
3062       /* Otherwise, try the hook function.  */
3063
3064       if (msym_not_found_p (bp_objfile_data->exception_msym))
3065         continue;
3066
3067       gdbarch = get_objfile_arch (objfile);
3068
3069       if (bp_objfile_data->exception_msym == NULL)
3070         {
3071           struct minimal_symbol *debug_hook;
3072
3073           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3074           if (debug_hook == NULL)
3075             {
3076               bp_objfile_data->exception_msym = &msym_not_found;
3077               continue;
3078             }
3079
3080           bp_objfile_data->exception_msym = debug_hook;
3081         }
3082
3083       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3084       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3085                                                  &current_target);
3086       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3087                                       &internal_breakpoint_ops);
3088       b->addr_string = xstrdup (func_name);
3089       b->enable_state = bp_disabled;
3090     }
3091
3092   update_global_location_list (1);
3093 }
3094
3095 void
3096 update_breakpoints_after_exec (void)
3097 {
3098   struct breakpoint *b, *b_tmp;
3099   struct bp_location *bploc, **bplocp_tmp;
3100
3101   /* We're about to delete breakpoints from GDB's lists.  If the
3102      INSERTED flag is true, GDB will try to lift the breakpoints by
3103      writing the breakpoints' "shadow contents" back into memory.  The
3104      "shadow contents" are NOT valid after an exec, so GDB should not
3105      do that.  Instead, the target is responsible from marking
3106      breakpoints out as soon as it detects an exec.  We don't do that
3107      here instead, because there may be other attempts to delete
3108      breakpoints after detecting an exec and before reaching here.  */
3109   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3110     if (bploc->pspace == current_program_space)
3111       gdb_assert (!bploc->inserted);
3112
3113   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3114   {
3115     if (b->pspace != current_program_space)
3116       continue;
3117
3118     /* Solib breakpoints must be explicitly reset after an exec().  */
3119     if (b->type == bp_shlib_event)
3120       {
3121         delete_breakpoint (b);
3122         continue;
3123       }
3124
3125     /* JIT breakpoints must be explicitly reset after an exec().  */
3126     if (b->type == bp_jit_event)
3127       {
3128         delete_breakpoint (b);
3129         continue;
3130       }
3131
3132     /* Thread event breakpoints must be set anew after an exec(),
3133        as must overlay event and longjmp master breakpoints.  */
3134     if (b->type == bp_thread_event || b->type == bp_overlay_event
3135         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3136         || b->type == bp_exception_master)
3137       {
3138         delete_breakpoint (b);
3139         continue;
3140       }
3141
3142     /* Step-resume breakpoints are meaningless after an exec().  */
3143     if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3144       {
3145         delete_breakpoint (b);
3146         continue;
3147       }
3148
3149     /* Longjmp and longjmp-resume breakpoints are also meaningless
3150        after an exec.  */
3151     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3152         || b->type == bp_exception || b->type == bp_exception_resume)
3153       {
3154         delete_breakpoint (b);
3155         continue;
3156       }
3157
3158     if (b->type == bp_catchpoint)
3159       {
3160         /* For now, none of the bp_catchpoint breakpoints need to
3161            do anything at this point.  In the future, if some of
3162            the catchpoints need to something, we will need to add
3163            a new method, and call this method from here.  */
3164         continue;
3165       }
3166
3167     /* bp_finish is a special case.  The only way we ought to be able
3168        to see one of these when an exec() has happened, is if the user
3169        caught a vfork, and then said "finish".  Ordinarily a finish just
3170        carries them to the call-site of the current callee, by setting
3171        a temporary bp there and resuming.  But in this case, the finish
3172        will carry them entirely through the vfork & exec.
3173
3174        We don't want to allow a bp_finish to remain inserted now.  But
3175        we can't safely delete it, 'cause finish_command has a handle to
3176        the bp on a bpstat, and will later want to delete it.  There's a
3177        chance (and I've seen it happen) that if we delete the bp_finish
3178        here, that its storage will get reused by the time finish_command
3179        gets 'round to deleting the "use to be a bp_finish" breakpoint.
3180        We really must allow finish_command to delete a bp_finish.
3181
3182        In the absence of a general solution for the "how do we know
3183        it's safe to delete something others may have handles to?"
3184        problem, what we'll do here is just uninsert the bp_finish, and
3185        let finish_command delete it.
3186
3187        (We know the bp_finish is "doomed" in the sense that it's
3188        momentary, and will be deleted as soon as finish_command sees
3189        the inferior stopped.  So it doesn't matter that the bp's
3190        address is probably bogus in the new a.out, unlike e.g., the
3191        solib breakpoints.)  */
3192
3193     if (b->type == bp_finish)
3194       {
3195         continue;
3196       }
3197
3198     /* Without a symbolic address, we have little hope of the
3199        pre-exec() address meaning the same thing in the post-exec()
3200        a.out.  */
3201     if (b->addr_string == NULL)
3202       {
3203         delete_breakpoint (b);
3204         continue;
3205       }
3206   }
3207   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
3208   create_overlay_event_breakpoint ();
3209   create_longjmp_master_breakpoint ();
3210   create_std_terminate_master_breakpoint ();
3211   create_exception_master_breakpoint ();
3212 }
3213
3214 int
3215 detach_breakpoints (int pid)
3216 {
3217   struct bp_location *bl, **blp_tmp;
3218   int val = 0;
3219   struct cleanup *old_chain = save_inferior_ptid ();
3220   struct inferior *inf = current_inferior ();
3221
3222   if (pid == PIDGET (inferior_ptid))
3223     error (_("Cannot detach breakpoints of inferior_ptid"));
3224
3225   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
3226   inferior_ptid = pid_to_ptid (pid);
3227   ALL_BP_LOCATIONS (bl, blp_tmp)
3228   {
3229     if (bl->pspace != inf->pspace)
3230       continue;
3231
3232     if (bl->inserted)
3233       val |= remove_breakpoint_1 (bl, mark_inserted);
3234   }
3235
3236   /* Detach single-step breakpoints as well.  */
3237   detach_single_step_breakpoints ();
3238
3239   do_cleanups (old_chain);
3240   return val;
3241 }
3242
3243 /* Remove the breakpoint location BL from the current address space.
3244    Note that this is used to detach breakpoints from a child fork.
3245    When we get here, the child isn't in the inferior list, and neither
3246    do we have objects to represent its address space --- we should
3247    *not* look at bl->pspace->aspace here.  */
3248
3249 static int
3250 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3251 {
3252   int val;
3253
3254   /* BL is never in moribund_locations by our callers.  */
3255   gdb_assert (bl->owner != NULL);
3256
3257   if (bl->owner->enable_state == bp_permanent)
3258     /* Permanent breakpoints cannot be inserted or removed.  */
3259     return 0;
3260
3261   /* The type of none suggests that owner is actually deleted.
3262      This should not ever happen.  */
3263   gdb_assert (bl->owner->type != bp_none);
3264
3265   if (bl->loc_type == bp_loc_software_breakpoint
3266       || bl->loc_type == bp_loc_hardware_breakpoint)
3267     {
3268       /* "Normal" instruction breakpoint: either the standard
3269          trap-instruction bp (bp_breakpoint), or a
3270          bp_hardware_breakpoint.  */
3271
3272       /* First check to see if we have to handle an overlay.  */
3273       if (overlay_debugging == ovly_off
3274           || bl->section == NULL
3275           || !(section_is_overlay (bl->section)))
3276         {
3277           /* No overlay handling: just remove the breakpoint.  */
3278           val = bl->owner->ops->remove_location (bl);
3279         }
3280       else
3281         {
3282           /* This breakpoint is in an overlay section.
3283              Did we set a breakpoint at the LMA?  */
3284           if (!overlay_events_enabled)
3285               {
3286                 /* Yes -- overlay event support is not active, so we
3287                    should have set a breakpoint at the LMA.  Remove it.  
3288                 */
3289                 /* Ignore any failures: if the LMA is in ROM, we will
3290                    have already warned when we failed to insert it.  */
3291                 if (bl->loc_type == bp_loc_hardware_breakpoint)
3292                   target_remove_hw_breakpoint (bl->gdbarch,
3293                                                &bl->overlay_target_info);
3294                 else
3295                   target_remove_breakpoint (bl->gdbarch,
3296                                             &bl->overlay_target_info);
3297               }
3298           /* Did we set a breakpoint at the VMA? 
3299              If so, we will have marked the breakpoint 'inserted'.  */
3300           if (bl->inserted)
3301             {
3302               /* Yes -- remove it.  Previously we did not bother to
3303                  remove the breakpoint if the section had been
3304                  unmapped, but let's not rely on that being safe.  We
3305                  don't know what the overlay manager might do.  */
3306
3307               /* However, we should remove *software* breakpoints only
3308                  if the section is still mapped, or else we overwrite
3309                  wrong code with the saved shadow contents.  */
3310               if (bl->loc_type == bp_loc_hardware_breakpoint
3311                   || section_is_mapped (bl->section))
3312                 val = bl->owner->ops->remove_location (bl);
3313               else
3314                 val = 0;
3315             }
3316           else
3317             {
3318               /* No -- not inserted, so no need to remove.  No error.  */
3319               val = 0;
3320             }
3321         }
3322
3323       /* In some cases, we might not be able to remove a breakpoint
3324          in a shared library that has already been removed, but we
3325          have not yet processed the shlib unload event.  */
3326       if (val && solib_name_from_address (bl->pspace, bl->address))
3327         val = 0;
3328
3329       if (val)
3330         return val;
3331       bl->inserted = (is == mark_inserted);
3332     }
3333   else if (bl->loc_type == bp_loc_hardware_watchpoint)
3334     {
3335       gdb_assert (bl->owner->ops != NULL
3336                   && bl->owner->ops->remove_location != NULL);
3337
3338       bl->inserted = (is == mark_inserted);
3339       bl->owner->ops->remove_location (bl);
3340
3341       /* Failure to remove any of the hardware watchpoints comes here.  */
3342       if ((is == mark_uninserted) && (bl->inserted))
3343         warning (_("Could not remove hardware watchpoint %d."),
3344                  bl->owner->number);
3345     }
3346   else if (bl->owner->type == bp_catchpoint
3347            && breakpoint_enabled (bl->owner)
3348            && !bl->duplicate)
3349     {
3350       gdb_assert (bl->owner->ops != NULL
3351                   && bl->owner->ops->remove_location != NULL);
3352
3353       val = bl->owner->ops->remove_location (bl);
3354       if (val)
3355         return val;
3356
3357       bl->inserted = (is == mark_inserted);
3358     }
3359
3360   return 0;
3361 }
3362
3363 static int
3364 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3365 {
3366   int ret;
3367   struct cleanup *old_chain;
3368
3369   /* BL is never in moribund_locations by our callers.  */
3370   gdb_assert (bl->owner != NULL);
3371
3372   if (bl->owner->enable_state == bp_permanent)
3373     /* Permanent breakpoints cannot be inserted or removed.  */
3374     return 0;
3375
3376   /* The type of none suggests that owner is actually deleted.
3377      This should not ever happen.  */
3378   gdb_assert (bl->owner->type != bp_none);
3379
3380   old_chain = save_current_space_and_thread ();
3381
3382   switch_to_program_space_and_thread (bl->pspace);
3383
3384   ret = remove_breakpoint_1 (bl, is);
3385
3386   do_cleanups (old_chain);
3387   return ret;
3388 }
3389
3390 /* Clear the "inserted" flag in all breakpoints.  */
3391
3392 void
3393 mark_breakpoints_out (void)
3394 {
3395   struct bp_location *bl, **blp_tmp;
3396
3397   ALL_BP_LOCATIONS (bl, blp_tmp)
3398     if (bl->pspace == current_program_space)
3399       bl->inserted = 0;
3400 }
3401
3402 /* Clear the "inserted" flag in all breakpoints and delete any
3403    breakpoints which should go away between runs of the program.
3404
3405    Plus other such housekeeping that has to be done for breakpoints
3406    between runs.
3407
3408    Note: this function gets called at the end of a run (by
3409    generic_mourn_inferior) and when a run begins (by
3410    init_wait_for_inferior).  */
3411
3412
3413
3414 void
3415 breakpoint_init_inferior (enum inf_context context)
3416 {
3417   struct breakpoint *b, *b_tmp;
3418   struct bp_location *bl, **blp_tmp;
3419   int ix;
3420   struct program_space *pspace = current_program_space;
3421
3422   /* If breakpoint locations are shared across processes, then there's
3423      nothing to do.  */
3424   if (gdbarch_has_global_breakpoints (target_gdbarch))
3425     return;
3426
3427   ALL_BP_LOCATIONS (bl, blp_tmp)
3428   {
3429     /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3430     if (bl->pspace == pspace
3431         && bl->owner->enable_state != bp_permanent)
3432       bl->inserted = 0;
3433   }
3434
3435   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3436   {
3437     if (b->loc && b->loc->pspace != pspace)
3438       continue;
3439
3440     switch (b->type)
3441       {
3442       case bp_call_dummy:
3443
3444         /* If the call dummy breakpoint is at the entry point it will
3445            cause problems when the inferior is rerun, so we better get
3446            rid of it.  */
3447
3448       case bp_watchpoint_scope:
3449
3450         /* Also get rid of scope breakpoints.  */
3451
3452       case bp_shlib_event:
3453
3454         /* Also remove solib event breakpoints.  Their addresses may
3455            have changed since the last time we ran the program.
3456            Actually we may now be debugging against different target;
3457            and so the solib backend that installed this breakpoint may
3458            not be used in by the target.  E.g.,
3459
3460            (gdb) file prog-linux
3461            (gdb) run               # native linux target
3462            ...
3463            (gdb) kill
3464            (gdb) file prog-win.exe
3465            (gdb) tar rem :9999     # remote Windows gdbserver.
3466         */
3467
3468       case bp_step_resume:
3469
3470         /* Also remove step-resume breakpoints.  */
3471
3472         delete_breakpoint (b);
3473         break;
3474
3475       case bp_watchpoint:
3476       case bp_hardware_watchpoint:
3477       case bp_read_watchpoint:
3478       case bp_access_watchpoint:
3479         {
3480           struct watchpoint *w = (struct watchpoint *) b;
3481
3482           /* Likewise for watchpoints on local expressions.  */
3483           if (w->exp_valid_block != NULL)
3484             delete_breakpoint (b);
3485           else if (context == inf_starting)
3486             {
3487               /* Reset val field to force reread of starting value in
3488                  insert_breakpoints.  */
3489               if (w->val)
3490                 value_free (w->val);
3491               w->val = NULL;
3492               w->val_valid = 0;
3493           }
3494         }
3495         break;
3496       default:
3497         break;
3498       }
3499   }
3500
3501   /* Get rid of the moribund locations.  */
3502   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3503     decref_bp_location (&bl);
3504   VEC_free (bp_location_p, moribund_locations);
3505 }
3506
3507 /* These functions concern about actual breakpoints inserted in the
3508    target --- to e.g. check if we need to do decr_pc adjustment or if
3509    we need to hop over the bkpt --- so we check for address space
3510    match, not program space.  */
3511
3512 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3513    exists at PC.  It returns ordinary_breakpoint_here if it's an
3514    ordinary breakpoint, or permanent_breakpoint_here if it's a
3515    permanent breakpoint.
3516    - When continuing from a location with an ordinary breakpoint, we
3517      actually single step once before calling insert_breakpoints.
3518    - When continuing from a location with a permanent breakpoint, we
3519      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3520      the target, to advance the PC past the breakpoint.  */
3521
3522 enum breakpoint_here
3523 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3524 {
3525   struct bp_location *bl, **blp_tmp;
3526   int any_breakpoint_here = 0;
3527
3528   ALL_BP_LOCATIONS (bl, blp_tmp)
3529     {
3530       if (bl->loc_type != bp_loc_software_breakpoint
3531           && bl->loc_type != bp_loc_hardware_breakpoint)
3532         continue;
3533
3534       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3535       if ((breakpoint_enabled (bl->owner)
3536            || bl->owner->enable_state == bp_permanent)
3537           && breakpoint_location_address_match (bl, aspace, pc))
3538         {
3539           if (overlay_debugging 
3540               && section_is_overlay (bl->section)
3541               && !section_is_mapped (bl->section))
3542             continue;           /* unmapped overlay -- can't be a match */
3543           else if (bl->owner->enable_state == bp_permanent)
3544             return permanent_breakpoint_here;
3545           else
3546             any_breakpoint_here = 1;
3547         }
3548     }
3549
3550   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3551 }
3552
3553 /* Return true if there's a moribund breakpoint at PC.  */
3554
3555 int
3556 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3557 {
3558   struct bp_location *loc;
3559   int ix;
3560
3561   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3562     if (breakpoint_location_address_match (loc, aspace, pc))
3563       return 1;
3564
3565   return 0;
3566 }
3567
3568 /* Returns non-zero if there's a breakpoint inserted at PC, which is
3569    inserted using regular breakpoint_chain / bp_location array
3570    mechanism.  This does not check for single-step breakpoints, which
3571    are inserted and removed using direct target manipulation.  */
3572
3573 int
3574 regular_breakpoint_inserted_here_p (struct address_space *aspace, 
3575                                     CORE_ADDR pc)
3576 {
3577   struct bp_location *bl, **blp_tmp;
3578
3579   ALL_BP_LOCATIONS (bl, blp_tmp)
3580     {
3581       if (bl->loc_type != bp_loc_software_breakpoint
3582           && bl->loc_type != bp_loc_hardware_breakpoint)
3583         continue;
3584
3585       if (bl->inserted
3586           && breakpoint_location_address_match (bl, aspace, pc))
3587         {
3588           if (overlay_debugging 
3589               && section_is_overlay (bl->section)
3590               && !section_is_mapped (bl->section))
3591             continue;           /* unmapped overlay -- can't be a match */
3592           else
3593             return 1;
3594         }
3595     }
3596   return 0;
3597 }
3598
3599 /* Returns non-zero iff there's either regular breakpoint
3600    or a single step breakpoint inserted at PC.  */
3601
3602 int
3603 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
3604 {
3605   if (regular_breakpoint_inserted_here_p (aspace, pc))
3606     return 1;
3607
3608   if (single_step_breakpoint_inserted_here_p (aspace, pc))
3609     return 1;
3610
3611   return 0;
3612 }
3613
3614 /* This function returns non-zero iff there is a software breakpoint
3615    inserted at PC.  */
3616
3617 int
3618 software_breakpoint_inserted_here_p (struct address_space *aspace,
3619                                      CORE_ADDR pc)
3620 {
3621   struct bp_location *bl, **blp_tmp;
3622
3623   ALL_BP_LOCATIONS (bl, blp_tmp)
3624     {
3625       if (bl->loc_type != bp_loc_software_breakpoint)
3626         continue;
3627
3628       if (bl->inserted
3629           && breakpoint_address_match (bl->pspace->aspace, bl->address,
3630                                        aspace, pc))
3631         {
3632           if (overlay_debugging 
3633               && section_is_overlay (bl->section)
3634               && !section_is_mapped (bl->section))
3635             continue;           /* unmapped overlay -- can't be a match */
3636           else
3637             return 1;
3638         }
3639     }
3640
3641   /* Also check for software single-step breakpoints.  */
3642   if (single_step_breakpoint_inserted_here_p (aspace, pc))
3643     return 1;
3644
3645   return 0;
3646 }
3647
3648 int
3649 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3650                                        CORE_ADDR addr, ULONGEST len)
3651 {
3652   struct breakpoint *bpt;
3653
3654   ALL_BREAKPOINTS (bpt)
3655     {
3656       struct bp_location *loc;
3657
3658       if (bpt->type != bp_hardware_watchpoint
3659           && bpt->type != bp_access_watchpoint)
3660         continue;
3661
3662       if (!breakpoint_enabled (bpt))
3663         continue;
3664
3665       for (loc = bpt->loc; loc; loc = loc->next)
3666         if (loc->pspace->aspace == aspace && loc->inserted)
3667           {
3668             CORE_ADDR l, h;
3669
3670             /* Check for intersection.  */
3671             l = max (loc->address, addr);
3672             h = min (loc->address + loc->length, addr + len);
3673             if (l < h)
3674               return 1;
3675           }
3676     }
3677   return 0;
3678 }
3679
3680 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3681    PC is valid for process/thread PTID.  */
3682
3683 int
3684 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3685                          ptid_t ptid)
3686 {
3687   struct bp_location *bl, **blp_tmp;
3688   /* The thread and task IDs associated to PTID, computed lazily.  */
3689   int thread = -1;
3690   int task = 0;
3691   
3692   ALL_BP_LOCATIONS (bl, blp_tmp)
3693     {
3694       if (bl->loc_type != bp_loc_software_breakpoint
3695           && bl->loc_type != bp_loc_hardware_breakpoint)
3696         continue;
3697
3698       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
3699       if (!breakpoint_enabled (bl->owner)
3700           && bl->owner->enable_state != bp_permanent)
3701         continue;
3702
3703       if (!breakpoint_location_address_match (bl, aspace, pc))
3704         continue;
3705
3706       if (bl->owner->thread != -1)
3707         {
3708           /* This is a thread-specific breakpoint.  Check that ptid
3709              matches that thread.  If thread hasn't been computed yet,
3710              it is now time to do so.  */
3711           if (thread == -1)
3712             thread = pid_to_thread_id (ptid);
3713           if (bl->owner->thread != thread)
3714             continue;
3715         }
3716
3717       if (bl->owner->task != 0)
3718         {
3719           /* This is a task-specific breakpoint.  Check that ptid
3720              matches that task.  If task hasn't been computed yet,
3721              it is now time to do so.  */
3722           if (task == 0)
3723             task = ada_get_task_number (ptid);
3724           if (bl->owner->task != task)
3725             continue;
3726         }
3727
3728       if (overlay_debugging 
3729           && section_is_overlay (bl->section)
3730           && !section_is_mapped (bl->section))
3731         continue;           /* unmapped overlay -- can't be a match */
3732
3733       return 1;
3734     }
3735
3736   return 0;
3737 }
3738 \f
3739
3740 /* bpstat stuff.  External routines' interfaces are documented
3741    in breakpoint.h.  */
3742
3743 int
3744 is_catchpoint (struct breakpoint *ep)
3745 {
3746   return (ep->type == bp_catchpoint);
3747 }
3748
3749 /* Frees any storage that is part of a bpstat.  Does not walk the
3750    'next' chain.  */
3751
3752 static void
3753 bpstat_free (bpstat bs)
3754 {
3755   if (bs->old_val != NULL)
3756     value_free (bs->old_val);
3757   decref_counted_command_line (&bs->commands);
3758   decref_bp_location (&bs->bp_location_at);
3759   xfree (bs);
3760 }
3761
3762 /* Clear a bpstat so that it says we are not at any breakpoint.
3763    Also free any storage that is part of a bpstat.  */
3764
3765 void
3766 bpstat_clear (bpstat *bsp)
3767 {
3768   bpstat p;
3769   bpstat q;
3770
3771   if (bsp == 0)
3772     return;
3773   p = *bsp;
3774   while (p != NULL)
3775     {
3776       q = p->next;
3777       bpstat_free (p);
3778       p = q;
3779     }
3780   *bsp = NULL;
3781 }
3782
3783 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
3784    is part of the bpstat is copied as well.  */
3785
3786 bpstat
3787 bpstat_copy (bpstat bs)
3788 {
3789   bpstat p = NULL;
3790   bpstat tmp;
3791   bpstat retval = NULL;
3792
3793   if (bs == NULL)
3794     return bs;
3795
3796   for (; bs != NULL; bs = bs->next)
3797     {
3798       tmp = (bpstat) xmalloc (sizeof (*tmp));
3799       memcpy (tmp, bs, sizeof (*tmp));
3800       incref_counted_command_line (tmp->commands);
3801       incref_bp_location (tmp->bp_location_at);
3802       if (bs->old_val != NULL)
3803         {
3804           tmp->old_val = value_copy (bs->old_val);
3805           release_value (tmp->old_val);
3806         }
3807
3808       if (p == NULL)
3809         /* This is the first thing in the chain.  */
3810         retval = tmp;
3811       else
3812         p->next = tmp;
3813       p = tmp;
3814     }
3815   p->next = NULL;
3816   return retval;
3817 }
3818
3819 /* Find the bpstat associated with this breakpoint.  */
3820
3821 bpstat
3822 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3823 {
3824   if (bsp == NULL)
3825     return NULL;
3826
3827   for (; bsp != NULL; bsp = bsp->next)
3828     {
3829       if (bsp->breakpoint_at == breakpoint)
3830         return bsp;
3831     }
3832   return NULL;
3833 }
3834
3835 /* Put in *NUM the breakpoint number of the first breakpoint we are
3836    stopped at.  *BSP upon return is a bpstat which points to the
3837    remaining breakpoints stopped at (but which is not guaranteed to be
3838    good for anything but further calls to bpstat_num).
3839
3840    Return 0 if passed a bpstat which does not indicate any breakpoints.
3841    Return -1 if stopped at a breakpoint that has been deleted since
3842    we set it.
3843    Return 1 otherwise.  */
3844
3845 int
3846 bpstat_num (bpstat *bsp, int *num)
3847 {
3848   struct breakpoint *b;
3849
3850   if ((*bsp) == NULL)
3851     return 0;                   /* No more breakpoint values */
3852
3853   /* We assume we'll never have several bpstats that correspond to a
3854      single breakpoint -- otherwise, this function might return the
3855      same number more than once and this will look ugly.  */
3856   b = (*bsp)->breakpoint_at;
3857   *bsp = (*bsp)->next;
3858   if (b == NULL)
3859     return -1;                  /* breakpoint that's been deleted since */
3860
3861   *num = b->number;             /* We have its number */
3862   return 1;
3863 }
3864
3865 /* See breakpoint.h.  */
3866
3867 void
3868 bpstat_clear_actions (void)
3869 {
3870   struct thread_info *tp;
3871   bpstat bs;
3872
3873   if (ptid_equal (inferior_ptid, null_ptid))
3874     return;
3875
3876   tp = find_thread_ptid (inferior_ptid);
3877   if (tp == NULL)
3878     return;
3879
3880   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
3881     {
3882       decref_counted_command_line (&bs->commands);
3883
3884       if (bs->old_val != NULL)
3885         {
3886           value_free (bs->old_val);
3887           bs->old_val = NULL;
3888         }
3889     }
3890 }
3891
3892 /* Called when a command is about to proceed the inferior.  */
3893
3894 static void
3895 breakpoint_about_to_proceed (void)
3896 {
3897   if (!ptid_equal (inferior_ptid, null_ptid))
3898     {
3899       struct thread_info *tp = inferior_thread ();
3900
3901       /* Allow inferior function calls in breakpoint commands to not
3902          interrupt the command list.  When the call finishes
3903          successfully, the inferior will be standing at the same
3904          breakpoint as if nothing happened.  */
3905       if (tp->control.in_infcall)
3906         return;
3907     }
3908
3909   breakpoint_proceeded = 1;
3910 }
3911
3912 /* Stub for cleaning up our state if we error-out of a breakpoint
3913    command.  */
3914 static void
3915 cleanup_executing_breakpoints (void *ignore)
3916 {
3917   executing_breakpoint_commands = 0;
3918 }
3919
3920 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
3921    or its equivalent.  */
3922
3923 static int
3924 command_line_is_silent (struct command_line *cmd)
3925 {
3926   return cmd && (strcmp ("silent", cmd->line) == 0
3927                  || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3928 }
3929
3930 /* Execute all the commands associated with all the breakpoints at
3931    this location.  Any of these commands could cause the process to
3932    proceed beyond this point, etc.  We look out for such changes by
3933    checking the global "breakpoint_proceeded" after each command.
3934
3935    Returns true if a breakpoint command resumed the inferior.  In that
3936    case, it is the caller's responsibility to recall it again with the
3937    bpstat of the current thread.  */
3938
3939 static int
3940 bpstat_do_actions_1 (bpstat *bsp)
3941 {
3942   bpstat bs;
3943   struct cleanup *old_chain;
3944   int again = 0;
3945
3946   /* Avoid endless recursion if a `source' command is contained
3947      in bs->commands.  */
3948   if (executing_breakpoint_commands)
3949     return 0;
3950
3951   executing_breakpoint_commands = 1;
3952   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3953
3954   prevent_dont_repeat ();
3955
3956   /* This pointer will iterate over the list of bpstat's.  */
3957   bs = *bsp;
3958
3959   breakpoint_proceeded = 0;
3960   for (; bs != NULL; bs = bs->next)
3961     {
3962       struct counted_command_line *ccmd;
3963       struct command_line *cmd;
3964       struct cleanup *this_cmd_tree_chain;
3965
3966       /* Take ownership of the BSP's command tree, if it has one.
3967
3968          The command tree could legitimately contain commands like
3969          'step' and 'next', which call clear_proceed_status, which
3970          frees stop_bpstat's command tree.  To make sure this doesn't
3971          free the tree we're executing out from under us, we need to
3972          take ownership of the tree ourselves.  Since a given bpstat's
3973          commands are only executed once, we don't need to copy it; we
3974          can clear the pointer in the bpstat, and make sure we free
3975          the tree when we're done.  */
3976       ccmd = bs->commands;
3977       bs->commands = NULL;
3978       this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3979       cmd = ccmd ? ccmd->commands : NULL;
3980       if (command_line_is_silent (cmd))
3981         {
3982           /* The action has been already done by bpstat_stop_status.  */
3983           cmd = cmd->next;
3984         }
3985
3986       while (cmd != NULL)
3987         {
3988           execute_control_command (cmd);
3989
3990           if (breakpoint_proceeded)
3991             break;
3992           else
3993             cmd = cmd->next;
3994         }
3995
3996       /* We can free this command tree now.  */
3997       do_cleanups (this_cmd_tree_chain);
3998
3999       if (breakpoint_proceeded)
4000         {
4001           if (target_can_async_p ())
4002             /* If we are in async mode, then the target might be still
4003                running, not stopped at any breakpoint, so nothing for
4004                us to do here -- just return to the event loop.  */
4005             ;
4006           else
4007             /* In sync mode, when execute_control_command returns
4008                we're already standing on the next breakpoint.
4009                Breakpoint commands for that stop were not run, since
4010                execute_command does not run breakpoint commands --
4011                only command_line_handler does, but that one is not
4012                involved in execution of breakpoint commands.  So, we
4013                can now execute breakpoint commands.  It should be
4014                noted that making execute_command do bpstat actions is
4015                not an option -- in this case we'll have recursive
4016                invocation of bpstat for each breakpoint with a
4017                command, and can easily blow up GDB stack.  Instead, we
4018                return true, which will trigger the caller to recall us
4019                with the new stop_bpstat.  */
4020             again = 1;
4021           break;
4022         }
4023     }
4024   do_cleanups (old_chain);
4025   return again;
4026 }
4027
4028 void
4029 bpstat_do_actions (void)
4030 {
4031   struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4032
4033   /* Do any commands attached to breakpoint we are stopped at.  */
4034   while (!ptid_equal (inferior_ptid, null_ptid)
4035          && target_has_execution
4036          && !is_exited (inferior_ptid)
4037          && !is_executing (inferior_ptid))
4038     /* Since in sync mode, bpstat_do_actions may resume the inferior,
4039        and only return when it is stopped at the next breakpoint, we
4040        keep doing breakpoint actions until it returns false to
4041        indicate the inferior was not resumed.  */
4042     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4043       break;
4044
4045   discard_cleanups (cleanup_if_error);
4046 }
4047
4048 /* Print out the (old or new) value associated with a watchpoint.  */
4049
4050 static void
4051 watchpoint_value_print (struct value *val, struct ui_file *stream)
4052 {
4053   if (val == NULL)
4054     fprintf_unfiltered (stream, _("<unreadable>"));
4055   else
4056     {
4057       struct value_print_options opts;
4058       get_user_print_options (&opts);
4059       value_print (val, stream, &opts);
4060     }
4061 }
4062
4063 /* Generic routine for printing messages indicating why we
4064    stopped.  The behavior of this function depends on the value
4065    'print_it' in the bpstat structure.  Under some circumstances we
4066    may decide not to print anything here and delegate the task to
4067    normal_stop().  */
4068
4069 static enum print_stop_action
4070 print_bp_stop_message (bpstat bs)
4071 {
4072   switch (bs->print_it)
4073     {
4074     case print_it_noop:
4075       /* Nothing should be printed for this bpstat entry.  */
4076       return PRINT_UNKNOWN;
4077       break;
4078
4079     case print_it_done:
4080       /* We still want to print the frame, but we already printed the
4081          relevant messages.  */
4082       return PRINT_SRC_AND_LOC;
4083       break;
4084
4085     case print_it_normal:
4086       {
4087         struct breakpoint *b = bs->breakpoint_at;
4088
4089         /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4090            which has since been deleted.  */
4091         if (b == NULL)
4092           return PRINT_UNKNOWN;
4093
4094         /* Normal case.  Call the breakpoint's print_it method.  */
4095         return b->ops->print_it (bs);
4096       }
4097       break;
4098
4099     default:
4100       internal_error (__FILE__, __LINE__,
4101                       _("print_bp_stop_message: unrecognized enum value"));
4102       break;
4103     }
4104 }
4105
4106 /* A helper function that prints a shared library stopped event.  */
4107
4108 static void
4109 print_solib_event (int is_catchpoint)
4110 {
4111   int any_deleted
4112     = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4113   int any_added
4114     = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4115
4116   if (!is_catchpoint)
4117     {
4118       if (any_added || any_deleted)
4119         ui_out_text (current_uiout,
4120                      _("Stopped due to shared library event:\n"));
4121       else
4122         ui_out_text (current_uiout,
4123                      _("Stopped due to shared library event (no "
4124                        "libraries added or removed)\n"));
4125     }
4126
4127   if (ui_out_is_mi_like_p (current_uiout))
4128     ui_out_field_string (current_uiout, "reason",
4129                          async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4130
4131   if (any_deleted)
4132     {
4133       struct cleanup *cleanup;
4134       char *name;
4135       int ix;
4136
4137       ui_out_text (current_uiout, _("  Inferior unloaded "));
4138       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4139                                                     "removed");
4140       for (ix = 0;
4141            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4142                         ix, name);
4143            ++ix)
4144         {
4145           if (ix > 0)
4146             ui_out_text (current_uiout, "    ");
4147           ui_out_field_string (current_uiout, "library", name);
4148           ui_out_text (current_uiout, "\n");
4149         }
4150
4151       do_cleanups (cleanup);
4152     }
4153
4154   if (any_added)
4155     {
4156       struct so_list *iter;
4157       int ix;
4158       struct cleanup *cleanup;
4159
4160       ui_out_text (current_uiout, _("  Inferior loaded "));
4161       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4162                                                     "added");
4163       for (ix = 0;
4164            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4165                         ix, iter);
4166            ++ix)
4167         {
4168           if (ix > 0)
4169             ui_out_text (current_uiout, "    ");
4170           ui_out_field_string (current_uiout, "library", iter->so_name);
4171           ui_out_text (current_uiout, "\n");
4172         }
4173
4174       do_cleanups (cleanup);
4175     }
4176 }
4177
4178 /* Print a message indicating what happened.  This is called from
4179    normal_stop().  The input to this routine is the head of the bpstat
4180    list - a list of the eventpoints that caused this stop.  KIND is
4181    the target_waitkind for the stopping event.  This
4182    routine calls the generic print routine for printing a message
4183    about reasons for stopping.  This will print (for example) the
4184    "Breakpoint n," part of the output.  The return value of this
4185    routine is one of:
4186
4187    PRINT_UNKNOWN: Means we printed nothing.
4188    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4189    code to print the location.  An example is 
4190    "Breakpoint 1, " which should be followed by
4191    the location.
4192    PRINT_SRC_ONLY: Means we printed something, but there is no need
4193    to also print the location part of the message.
4194    An example is the catch/throw messages, which
4195    don't require a location appended to the end.
4196    PRINT_NOTHING: We have done some printing and we don't need any 
4197    further info to be printed.  */
4198
4199 enum print_stop_action
4200 bpstat_print (bpstat bs, int kind)
4201 {
4202   int val;
4203
4204   /* Maybe another breakpoint in the chain caused us to stop.
4205      (Currently all watchpoints go on the bpstat whether hit or not.
4206      That probably could (should) be changed, provided care is taken
4207      with respect to bpstat_explains_signal).  */
4208   for (; bs; bs = bs->next)
4209     {
4210       val = print_bp_stop_message (bs);
4211       if (val == PRINT_SRC_ONLY 
4212           || val == PRINT_SRC_AND_LOC 
4213           || val == PRINT_NOTHING)
4214         return val;
4215     }
4216
4217   /* If we had hit a shared library event breakpoint,
4218      print_bp_stop_message would print out this message.  If we hit an
4219      OS-level shared library event, do the same thing.  */
4220   if (kind == TARGET_WAITKIND_LOADED)
4221     {
4222       print_solib_event (0);
4223       return PRINT_NOTHING;
4224     }
4225
4226   /* We reached the end of the chain, or we got a null BS to start
4227      with and nothing was printed.  */
4228   return PRINT_UNKNOWN;
4229 }
4230
4231 /* Evaluate the expression EXP and return 1 if value is zero.  This is
4232    used inside a catch_errors to evaluate the breakpoint condition.
4233    The argument is a "struct expression *" that has been cast to a
4234    "char *" to make it pass through catch_errors.  */
4235
4236 static int
4237 breakpoint_cond_eval (void *exp)
4238 {
4239   struct value *mark = value_mark ();
4240   int i = !value_true (evaluate_expression ((struct expression *) exp));
4241
4242   value_free_to_mark (mark);
4243   return i;
4244 }
4245
4246 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
4247
4248 static bpstat
4249 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4250 {
4251   bpstat bs;
4252
4253   bs = (bpstat) xmalloc (sizeof (*bs));
4254   bs->next = NULL;
4255   **bs_link_pointer = bs;
4256   *bs_link_pointer = &bs->next;
4257   bs->breakpoint_at = bl->owner;
4258   bs->bp_location_at = bl;
4259   incref_bp_location (bl);
4260   /* If the condition is false, etc., don't do the commands.  */
4261   bs->commands = NULL;
4262   bs->old_val = NULL;
4263   bs->print_it = print_it_normal;
4264   return bs;
4265 }
4266 \f
4267 /* The target has stopped with waitstatus WS.  Check if any hardware
4268    watchpoints have triggered, according to the target.  */
4269
4270 int
4271 watchpoints_triggered (struct target_waitstatus *ws)
4272 {
4273   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4274   CORE_ADDR addr;
4275   struct breakpoint *b;
4276
4277   if (!stopped_by_watchpoint)
4278     {
4279       /* We were not stopped by a watchpoint.  Mark all watchpoints
4280          as not triggered.  */
4281       ALL_BREAKPOINTS (b)
4282         if (is_hardware_watchpoint (b))
4283           {
4284             struct watchpoint *w = (struct watchpoint *) b;
4285
4286             w->watchpoint_triggered = watch_triggered_no;
4287           }
4288
4289       return 0;
4290     }
4291
4292   if (!target_stopped_data_address (&current_target, &addr))
4293     {
4294       /* We were stopped by a watchpoint, but we don't know where.
4295          Mark all watchpoints as unknown.  */
4296       ALL_BREAKPOINTS (b)
4297         if (is_hardware_watchpoint (b))
4298           {
4299             struct watchpoint *w = (struct watchpoint *) b;
4300
4301             w->watchpoint_triggered = watch_triggered_unknown;
4302           }
4303
4304       return stopped_by_watchpoint;
4305     }
4306
4307   /* The target could report the data address.  Mark watchpoints
4308      affected by this data address as triggered, and all others as not
4309      triggered.  */
4310
4311   ALL_BREAKPOINTS (b)
4312     if (is_hardware_watchpoint (b))
4313       {
4314         struct watchpoint *w = (struct watchpoint *) b;
4315         struct bp_location *loc;
4316
4317         w->watchpoint_triggered = watch_triggered_no;
4318         for (loc = b->loc; loc; loc = loc->next)
4319           {
4320             if (is_masked_watchpoint (b))
4321               {
4322                 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4323                 CORE_ADDR start = loc->address & w->hw_wp_mask;
4324
4325                 if (newaddr == start)
4326                   {
4327                     w->watchpoint_triggered = watch_triggered_yes;
4328                     break;
4329                   }
4330               }
4331             /* Exact match not required.  Within range is sufficient.  */
4332             else if (target_watchpoint_addr_within_range (&current_target,
4333                                                          addr, loc->address,
4334                                                          loc->length))
4335               {
4336                 w->watchpoint_triggered = watch_triggered_yes;
4337                 break;
4338               }
4339           }
4340       }
4341
4342   return 1;
4343 }
4344
4345 /* Possible return values for watchpoint_check (this can't be an enum
4346    because of check_errors).  */
4347 /* The watchpoint has been deleted.  */
4348 #define WP_DELETED 1
4349 /* The value has changed.  */
4350 #define WP_VALUE_CHANGED 2
4351 /* The value has not changed.  */
4352 #define WP_VALUE_NOT_CHANGED 3
4353 /* Ignore this watchpoint, no matter if the value changed or not.  */
4354 #define WP_IGNORE 4
4355
4356 #define BP_TEMPFLAG 1
4357 #define BP_HARDWAREFLAG 2
4358
4359 /* Evaluate watchpoint condition expression and check if its value
4360    changed.
4361
4362    P should be a pointer to struct bpstat, but is defined as a void *
4363    in order for this function to be usable with catch_errors.  */
4364
4365 static int
4366 watchpoint_check (void *p)
4367 {
4368   bpstat bs = (bpstat) p;
4369   struct watchpoint *b;
4370   struct frame_info *fr;
4371   int within_current_scope;
4372
4373   /* BS is built from an existing struct breakpoint.  */
4374   gdb_assert (bs->breakpoint_at != NULL);
4375   b = (struct watchpoint *) bs->breakpoint_at;
4376
4377   /* If this is a local watchpoint, we only want to check if the
4378      watchpoint frame is in scope if the current thread is the thread
4379      that was used to create the watchpoint.  */
4380   if (!watchpoint_in_thread_scope (b))
4381     return WP_IGNORE;
4382
4383   if (b->exp_valid_block == NULL)
4384     within_current_scope = 1;
4385   else
4386     {
4387       struct frame_info *frame = get_current_frame ();
4388       struct gdbarch *frame_arch = get_frame_arch (frame);
4389       CORE_ADDR frame_pc = get_frame_pc (frame);
4390
4391       /* in_function_epilogue_p() returns a non-zero value if we're
4392          still in the function but the stack frame has already been
4393          invalidated.  Since we can't rely on the values of local
4394          variables after the stack has been destroyed, we are treating
4395          the watchpoint in that state as `not changed' without further
4396          checking.  Don't mark watchpoints as changed if the current
4397          frame is in an epilogue - even if they are in some other
4398          frame, our view of the stack is likely to be wrong and
4399          frame_find_by_id could error out.  */
4400       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4401         return WP_IGNORE;
4402
4403       fr = frame_find_by_id (b->watchpoint_frame);
4404       within_current_scope = (fr != NULL);
4405
4406       /* If we've gotten confused in the unwinder, we might have
4407          returned a frame that can't describe this variable.  */
4408       if (within_current_scope)
4409         {
4410           struct symbol *function;
4411
4412           function = get_frame_function (fr);
4413           if (function == NULL
4414               || !contained_in (b->exp_valid_block,
4415                                 SYMBOL_BLOCK_VALUE (function)))
4416             within_current_scope = 0;
4417         }
4418
4419       if (within_current_scope)
4420         /* If we end up stopping, the current frame will get selected
4421            in normal_stop.  So this call to select_frame won't affect
4422            the user.  */
4423         select_frame (fr);
4424     }
4425
4426   if (within_current_scope)
4427     {
4428       /* We use value_{,free_to_}mark because it could be a *long*
4429          time before we return to the command level and call
4430          free_all_values.  We can't call free_all_values because we
4431          might be in the middle of evaluating a function call.  */
4432
4433       int pc = 0;
4434       struct value *mark;
4435       struct value *new_val;
4436
4437       if (is_masked_watchpoint (&b->base))
4438         /* Since we don't know the exact trigger address (from
4439            stopped_data_address), just tell the user we've triggered
4440            a mask watchpoint.  */
4441         return WP_VALUE_CHANGED;
4442
4443       mark = value_mark ();
4444       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
4445
4446       /* We use value_equal_contents instead of value_equal because
4447          the latter coerces an array to a pointer, thus comparing just
4448          the address of the array instead of its contents.  This is
4449          not what we want.  */
4450       if ((b->val != NULL) != (new_val != NULL)
4451           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4452         {
4453           if (new_val != NULL)
4454             {
4455               release_value (new_val);
4456               value_free_to_mark (mark);
4457             }
4458           bs->old_val = b->val;
4459           b->val = new_val;
4460           b->val_valid = 1;
4461           return WP_VALUE_CHANGED;
4462         }
4463       else
4464         {
4465           /* Nothing changed.  */
4466           value_free_to_mark (mark);
4467           return WP_VALUE_NOT_CHANGED;
4468         }
4469     }
4470   else
4471     {
4472       struct ui_out *uiout = current_uiout;
4473
4474       /* This seems like the only logical thing to do because
4475          if we temporarily ignored the watchpoint, then when
4476          we reenter the block in which it is valid it contains
4477          garbage (in the case of a function, it may have two
4478          garbage values, one before and one after the prologue).
4479          So we can't even detect the first assignment to it and
4480          watch after that (since the garbage may or may not equal
4481          the first value assigned).  */
4482       /* We print all the stop information in
4483          breakpoint_ops->print_it, but in this case, by the time we
4484          call breakpoint_ops->print_it this bp will be deleted
4485          already.  So we have no choice but print the information
4486          here.  */
4487       if (ui_out_is_mi_like_p (uiout))
4488         ui_out_field_string
4489           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4490       ui_out_text (uiout, "\nWatchpoint ");
4491       ui_out_field_int (uiout, "wpnum", b->base.number);
4492       ui_out_text (uiout,
4493                    " deleted because the program has left the block in\n\
4494 which its expression is valid.\n");     
4495
4496       /* Make sure the watchpoint's commands aren't executed.  */
4497       decref_counted_command_line (&b->base.commands);
4498       watchpoint_del_at_next_stop (b);
4499
4500       return WP_DELETED;
4501     }
4502 }
4503
4504 /* Return true if it looks like target has stopped due to hitting
4505    breakpoint location BL.  This function does not check if we should
4506    stop, only if BL explains the stop.  */
4507
4508 static int
4509 bpstat_check_location (const struct bp_location *bl,
4510                        struct address_space *aspace, CORE_ADDR bp_addr,
4511                        const struct target_waitstatus *ws)
4512 {
4513   struct breakpoint *b = bl->owner;
4514
4515   /* BL is from an existing breakpoint.  */
4516   gdb_assert (b != NULL);
4517
4518   return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4519 }
4520
4521 /* Determine if the watched values have actually changed, and we
4522    should stop.  If not, set BS->stop to 0.  */
4523
4524 static void
4525 bpstat_check_watchpoint (bpstat bs)
4526 {
4527   const struct bp_location *bl;
4528   struct watchpoint *b;
4529
4530   /* BS is built for existing struct breakpoint.  */
4531   bl = bs->bp_location_at;
4532   gdb_assert (bl != NULL);
4533   b = (struct watchpoint *) bs->breakpoint_at;
4534   gdb_assert (b != NULL);
4535
4536     {
4537       int must_check_value = 0;
4538       
4539       if (b->base.type == bp_watchpoint)
4540         /* For a software watchpoint, we must always check the
4541            watched value.  */
4542         must_check_value = 1;
4543       else if (b->watchpoint_triggered == watch_triggered_yes)
4544         /* We have a hardware watchpoint (read, write, or access)
4545            and the target earlier reported an address watched by
4546            this watchpoint.  */
4547         must_check_value = 1;
4548       else if (b->watchpoint_triggered == watch_triggered_unknown
4549                && b->base.type == bp_hardware_watchpoint)
4550         /* We were stopped by a hardware watchpoint, but the target could
4551            not report the data address.  We must check the watchpoint's
4552            value.  Access and read watchpoints are out of luck; without
4553            a data address, we can't figure it out.  */
4554         must_check_value = 1;
4555
4556       if (must_check_value)
4557         {
4558           char *message
4559             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4560                           b->base.number);
4561           struct cleanup *cleanups = make_cleanup (xfree, message);
4562           int e = catch_errors (watchpoint_check, bs, message,
4563                                 RETURN_MASK_ALL);
4564           do_cleanups (cleanups);
4565           switch (e)
4566             {
4567             case WP_DELETED:
4568               /* We've already printed what needs to be printed.  */
4569               bs->print_it = print_it_done;
4570               /* Stop.  */
4571               break;
4572             case WP_IGNORE:
4573               bs->print_it = print_it_noop;
4574               bs->stop = 0;
4575               break;
4576             case WP_VALUE_CHANGED:
4577               if (b->base.type == bp_read_watchpoint)
4578                 {
4579                   /* There are two cases to consider here:
4580
4581                      1. We're watching the triggered memory for reads.
4582                      In that case, trust the target, and always report
4583                      the watchpoint hit to the user.  Even though
4584                      reads don't cause value changes, the value may
4585                      have changed since the last time it was read, and
4586                      since we're not trapping writes, we will not see
4587                      those, and as such we should ignore our notion of
4588                      old value.
4589
4590                      2. We're watching the triggered memory for both
4591                      reads and writes.  There are two ways this may
4592                      happen:
4593
4594                      2.1. This is a target that can't break on data
4595                      reads only, but can break on accesses (reads or
4596                      writes), such as e.g., x86.  We detect this case
4597                      at the time we try to insert read watchpoints.
4598
4599                      2.2. Otherwise, the target supports read
4600                      watchpoints, but, the user set an access or write
4601                      watchpoint watching the same memory as this read
4602                      watchpoint.
4603
4604                      If we're watching memory writes as well as reads,
4605                      ignore watchpoint hits when we find that the
4606                      value hasn't changed, as reads don't cause
4607                      changes.  This still gives false positives when
4608                      the program writes the same value to memory as
4609                      what there was already in memory (we will confuse
4610                      it for a read), but it's much better than
4611                      nothing.  */
4612
4613                   int other_write_watchpoint = 0;
4614
4615                   if (bl->watchpoint_type == hw_read)
4616                     {
4617                       struct breakpoint *other_b;
4618
4619                       ALL_BREAKPOINTS (other_b)
4620                         if (other_b->type == bp_hardware_watchpoint
4621                             || other_b->type == bp_access_watchpoint)
4622                           {
4623                             struct watchpoint *other_w =
4624                               (struct watchpoint *) other_b;
4625
4626                             if (other_w->watchpoint_triggered
4627                                 == watch_triggered_yes)
4628                               {
4629                                 other_write_watchpoint = 1;
4630                                 break;
4631                               }
4632                           }
4633                     }
4634
4635                   if (other_write_watchpoint
4636                       || bl->watchpoint_type == hw_access)
4637                     {
4638                       /* We're watching the same memory for writes,
4639                          and the value changed since the last time we
4640                          updated it, so this trap must be for a write.
4641                          Ignore it.  */
4642                       bs->print_it = print_it_noop;
4643                       bs->stop = 0;
4644                     }
4645                 }
4646               break;
4647             case WP_VALUE_NOT_CHANGED:
4648               if (b->base.type == bp_hardware_watchpoint
4649                   || b->base.type == bp_watchpoint)
4650                 {
4651                   /* Don't stop: write watchpoints shouldn't fire if
4652                      the value hasn't changed.  */
4653                   bs->print_it = print_it_noop;
4654                   bs->stop = 0;
4655                 }
4656               /* Stop.  */
4657               break;
4658             default:
4659               /* Can't happen.  */
4660             case 0:
4661               /* Error from catch_errors.  */
4662               printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
4663               watchpoint_del_at_next_stop (b);
4664               /* We've already printed what needs to be printed.  */
4665               bs->print_it = print_it_done;
4666               break;
4667             }
4668         }
4669       else      /* must_check_value == 0 */
4670         {
4671           /* This is a case where some watchpoint(s) triggered, but
4672              not at the address of this watchpoint, or else no
4673              watchpoint triggered after all.  So don't print
4674              anything for this watchpoint.  */
4675           bs->print_it = print_it_noop;
4676           bs->stop = 0;
4677         }
4678     }
4679 }
4680
4681
4682 /* Check conditions (condition proper, frame, thread and ignore count)
4683    of breakpoint referred to by BS.  If we should not stop for this
4684    breakpoint, set BS->stop to 0.  */
4685
4686 static void
4687 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4688 {
4689   int thread_id = pid_to_thread_id (ptid);
4690   const struct bp_location *bl;
4691   struct breakpoint *b;
4692
4693   /* BS is built for existing struct breakpoint.  */
4694   bl = bs->bp_location_at;
4695   gdb_assert (bl != NULL);
4696   b = bs->breakpoint_at;
4697   gdb_assert (b != NULL);
4698
4699   /* Even if the target evaluated the condition on its end and notified GDB, we
4700      need to do so again since GDB does not know if we stopped due to a
4701      breakpoint or a single step breakpoint.  */
4702
4703   if (frame_id_p (b->frame_id)
4704       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
4705     bs->stop = 0;
4706   else if (bs->stop)
4707     {
4708       int value_is_zero = 0;
4709       struct expression *cond;
4710
4711       /* Evaluate Python breakpoints that have a "stop"
4712          method implemented.  */
4713       if (b->py_bp_object)
4714         bs->stop = gdbpy_should_stop (b->py_bp_object);
4715
4716       if (is_watchpoint (b))
4717         {
4718           struct watchpoint *w = (struct watchpoint *) b;
4719
4720           cond = w->cond_exp;
4721         }
4722       else
4723         cond = bl->cond;
4724
4725       if (cond && b->disposition != disp_del_at_next_stop)
4726         {
4727           int within_current_scope = 1;
4728           struct watchpoint * w;
4729
4730           /* We use value_mark and value_free_to_mark because it could
4731              be a long time before we return to the command level and
4732              call free_all_values.  We can't call free_all_values
4733              because we might be in the middle of evaluating a
4734              function call.  */
4735           struct value *mark = value_mark ();
4736
4737           if (is_watchpoint (b))
4738             w = (struct watchpoint *) b;
4739           else
4740             w = NULL;
4741
4742           /* Need to select the frame, with all that implies so that
4743              the conditions will have the right context.  Because we
4744              use the frame, we will not see an inlined function's
4745              variables when we arrive at a breakpoint at the start
4746              of the inlined function; the current frame will be the
4747              call site.  */
4748           if (w == NULL || w->cond_exp_valid_block == NULL)
4749             select_frame (get_current_frame ());
4750           else
4751             {
4752               struct frame_info *frame;
4753
4754               /* For local watchpoint expressions, which particular
4755                  instance of a local is being watched matters, so we
4756                  keep track of the frame to evaluate the expression
4757                  in.  To evaluate the condition however, it doesn't
4758                  really matter which instantiation of the function
4759                  where the condition makes sense triggers the
4760                  watchpoint.  This allows an expression like "watch
4761                  global if q > 10" set in `func', catch writes to
4762                  global on all threads that call `func', or catch
4763                  writes on all recursive calls of `func' by a single
4764                  thread.  We simply always evaluate the condition in
4765                  the innermost frame that's executing where it makes
4766                  sense to evaluate the condition.  It seems
4767                  intuitive.  */
4768               frame = block_innermost_frame (w->cond_exp_valid_block);
4769               if (frame != NULL)
4770                 select_frame (frame);
4771               else
4772                 within_current_scope = 0;
4773             }
4774           if (within_current_scope)
4775             value_is_zero
4776               = catch_errors (breakpoint_cond_eval, cond,
4777                               "Error in testing breakpoint condition:\n",
4778                               RETURN_MASK_ALL);
4779           else
4780             {
4781               warning (_("Watchpoint condition cannot be tested "
4782                          "in the current scope"));
4783               /* If we failed to set the right context for this
4784                  watchpoint, unconditionally report it.  */
4785               value_is_zero = 0;
4786             }
4787           /* FIXME-someday, should give breakpoint #.  */
4788           value_free_to_mark (mark);
4789         }
4790
4791       if (cond && value_is_zero)
4792         {
4793           bs->stop = 0;
4794         }
4795       else if (b->thread != -1 && b->thread != thread_id)
4796         {
4797           bs->stop = 0;
4798         }
4799       else if (b->ignore_count > 0)
4800         {
4801           b->ignore_count--;
4802           annotate_ignore_count_change ();
4803           bs->stop = 0;
4804           /* Increase the hit count even though we don't stop.  */
4805           ++(b->hit_count);
4806           observer_notify_breakpoint_modified (b);
4807         }       
4808     }
4809 }
4810
4811
4812 /* Get a bpstat associated with having just stopped at address
4813    BP_ADDR in thread PTID.
4814
4815    Determine whether we stopped at a breakpoint, etc, or whether we
4816    don't understand this stop.  Result is a chain of bpstat's such
4817    that:
4818
4819    if we don't understand the stop, the result is a null pointer.
4820
4821    if we understand why we stopped, the result is not null.
4822
4823    Each element of the chain refers to a particular breakpoint or
4824    watchpoint at which we have stopped.  (We may have stopped for
4825    several reasons concurrently.)
4826
4827    Each element of the chain has valid next, breakpoint_at,
4828    commands, FIXME??? fields.  */
4829
4830 bpstat
4831 bpstat_stop_status (struct address_space *aspace,
4832                     CORE_ADDR bp_addr, ptid_t ptid,
4833                     const struct target_waitstatus *ws)
4834 {
4835   struct breakpoint *b = NULL;
4836   struct bp_location *bl;
4837   struct bp_location *loc;
4838   /* First item of allocated bpstat's.  */
4839   bpstat bs_head = NULL, *bs_link = &bs_head;
4840   /* Pointer to the last thing in the chain currently.  */
4841   bpstat bs;
4842   int ix;
4843   int need_remove_insert;
4844   int removed_any;
4845
4846   /* First, build the bpstat chain with locations that explain a
4847      target stop, while being careful to not set the target running,
4848      as that may invalidate locations (in particular watchpoint
4849      locations are recreated).  Resuming will happen here with
4850      breakpoint conditions or watchpoint expressions that include
4851      inferior function calls.  */
4852
4853   ALL_BREAKPOINTS (b)
4854     {
4855       if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4856         continue;
4857
4858       for (bl = b->loc; bl != NULL; bl = bl->next)
4859         {
4860           /* For hardware watchpoints, we look only at the first
4861              location.  The watchpoint_check function will work on the
4862              entire expression, not the individual locations.  For
4863              read watchpoints, the watchpoints_triggered function has
4864              checked all locations already.  */
4865           if (b->type == bp_hardware_watchpoint && bl != b->loc)
4866             break;
4867
4868           if (bl->shlib_disabled)
4869             continue;
4870
4871           if (!bpstat_check_location (bl, aspace, bp_addr, ws))
4872             continue;
4873
4874           /* Come here if it's a watchpoint, or if the break address
4875              matches.  */
4876
4877           bs = bpstat_alloc (bl, &bs_link);     /* Alloc a bpstat to
4878                                                    explain stop.  */
4879
4880           /* Assume we stop.  Should we find a watchpoint that is not
4881              actually triggered, or if the condition of the breakpoint
4882              evaluates as false, we'll reset 'stop' to 0.  */
4883           bs->stop = 1;
4884           bs->print = 1;
4885
4886           /* If this is a scope breakpoint, mark the associated
4887              watchpoint as triggered so that we will handle the
4888              out-of-scope event.  We'll get to the watchpoint next
4889              iteration.  */
4890           if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
4891             {
4892               struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4893
4894               w->watchpoint_triggered = watch_triggered_yes;
4895             }
4896         }
4897     }
4898
4899   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4900     {
4901       if (breakpoint_location_address_match (loc, aspace, bp_addr))
4902         {
4903           bs = bpstat_alloc (loc, &bs_link);
4904           /* For hits of moribund locations, we should just proceed.  */
4905           bs->stop = 0;
4906           bs->print = 0;
4907           bs->print_it = print_it_noop;
4908         }
4909     }
4910
4911   /* A bit of special processing for shlib breakpoints.  We need to
4912      process solib loading here, so that the lists of loaded and
4913      unloaded libraries are correct before we handle "catch load" and
4914      "catch unload".  */
4915   for (bs = bs_head; bs != NULL; bs = bs->next)
4916     {
4917       if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
4918         {
4919           handle_solib_event ();
4920           break;
4921         }
4922     }
4923
4924   /* Now go through the locations that caused the target to stop, and
4925      check whether we're interested in reporting this stop to higher
4926      layers, or whether we should resume the target transparently.  */
4927
4928   removed_any = 0;
4929
4930   for (bs = bs_head; bs != NULL; bs = bs->next)
4931     {
4932       if (!bs->stop)
4933         continue;
4934
4935       b = bs->breakpoint_at;
4936       b->ops->check_status (bs);
4937       if (bs->stop)
4938         {
4939           bpstat_check_breakpoint_conditions (bs, ptid);
4940
4941           if (bs->stop)
4942             {
4943               ++(b->hit_count);
4944               observer_notify_breakpoint_modified (b);
4945
4946               /* We will stop here.  */
4947               if (b->disposition == disp_disable)
4948                 {
4949                   --(b->enable_count);
4950                   if (b->enable_count <= 0
4951                       && b->enable_state != bp_permanent)
4952                     b->enable_state = bp_disabled;
4953                   removed_any = 1;
4954                 }
4955               if (b->silent)
4956                 bs->print = 0;
4957               bs->commands = b->commands;
4958               incref_counted_command_line (bs->commands);
4959               if (command_line_is_silent (bs->commands
4960                                           ? bs->commands->commands : NULL))
4961                 bs->print = 0;
4962             }
4963
4964         }
4965
4966       /* Print nothing for this entry if we don't stop or don't
4967          print.  */
4968       if (!bs->stop || !bs->print)
4969         bs->print_it = print_it_noop;
4970     }
4971
4972   /* If we aren't stopping, the value of some hardware watchpoint may
4973      not have changed, but the intermediate memory locations we are
4974      watching may have.  Don't bother if we're stopping; this will get
4975      done later.  */
4976   need_remove_insert = 0;
4977   if (! bpstat_causes_stop (bs_head))
4978     for (bs = bs_head; bs != NULL; bs = bs->next)
4979       if (!bs->stop
4980           && bs->breakpoint_at
4981           && is_hardware_watchpoint (bs->breakpoint_at))
4982         {
4983           struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4984
4985           update_watchpoint (w, 0 /* don't reparse.  */);
4986           need_remove_insert = 1;
4987         }
4988
4989   if (need_remove_insert)
4990     update_global_location_list (1);
4991   else if (removed_any)
4992     update_global_location_list (0);
4993
4994   return bs_head;
4995 }
4996
4997 static void
4998 handle_jit_event (void)
4999 {
5000   struct frame_info *frame;
5001   struct gdbarch *gdbarch;
5002
5003   /* Switch terminal for any messages produced by
5004      breakpoint_re_set.  */
5005   target_terminal_ours_for_output ();
5006
5007   frame = get_current_frame ();
5008   gdbarch = get_frame_arch (frame);
5009
5010   jit_event_handler (gdbarch);
5011
5012   target_terminal_inferior ();
5013 }
5014
5015 /* Handle an solib event by calling solib_add.  */
5016
5017 void
5018 handle_solib_event (void)
5019 {
5020   clear_program_space_solib_cache (current_inferior ()->pspace);
5021
5022   /* Check for any newly added shared libraries if we're supposed to
5023      be adding them automatically.  Switch terminal for any messages
5024      produced by breakpoint_re_set.  */
5025   target_terminal_ours_for_output ();
5026 #ifdef SOLIB_ADD
5027   SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5028 #else
5029   solib_add (NULL, 0, &current_target, auto_solib_add);
5030 #endif
5031   target_terminal_inferior ();
5032 }
5033
5034 /* Prepare WHAT final decision for infrun.  */
5035
5036 /* Decide what infrun needs to do with this bpstat.  */
5037
5038 struct bpstat_what
5039 bpstat_what (bpstat bs_head)
5040 {
5041   struct bpstat_what retval;
5042   int jit_event = 0;
5043   bpstat bs;
5044
5045   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5046   retval.call_dummy = STOP_NONE;
5047   retval.is_longjmp = 0;
5048
5049   for (bs = bs_head; bs != NULL; bs = bs->next)
5050     {
5051       /* Extract this BS's action.  After processing each BS, we check
5052          if its action overrides all we've seem so far.  */
5053       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5054       enum bptype bptype;
5055
5056       if (bs->breakpoint_at == NULL)
5057         {
5058           /* I suspect this can happen if it was a momentary
5059              breakpoint which has since been deleted.  */
5060           bptype = bp_none;
5061         }
5062       else
5063         bptype = bs->breakpoint_at->type;
5064
5065       switch (bptype)
5066         {
5067         case bp_none:
5068           break;
5069         case bp_breakpoint:
5070         case bp_hardware_breakpoint:
5071         case bp_until:
5072         case bp_finish:
5073         case bp_shlib_event:
5074           if (bs->stop)
5075             {
5076               if (bs->print)
5077                 this_action = BPSTAT_WHAT_STOP_NOISY;
5078               else
5079                 this_action = BPSTAT_WHAT_STOP_SILENT;
5080             }
5081           else
5082             this_action = BPSTAT_WHAT_SINGLE;
5083           break;
5084         case bp_watchpoint:
5085         case bp_hardware_watchpoint:
5086         case bp_read_watchpoint:
5087         case bp_access_watchpoint:
5088           if (bs->stop)
5089             {
5090               if (bs->print)
5091                 this_action = BPSTAT_WHAT_STOP_NOISY;
5092               else
5093                 this_action = BPSTAT_WHAT_STOP_SILENT;
5094             }
5095           else
5096             {
5097               /* There was a watchpoint, but we're not stopping.
5098                  This requires no further action.  */
5099             }
5100           break;
5101         case bp_longjmp:
5102         case bp_exception:
5103           this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5104           retval.is_longjmp = bptype == bp_longjmp;
5105           break;
5106         case bp_longjmp_resume:
5107         case bp_exception_resume:
5108           this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5109           retval.is_longjmp = bptype == bp_longjmp_resume;
5110           break;
5111         case bp_step_resume:
5112           if (bs->stop)
5113             this_action = BPSTAT_WHAT_STEP_RESUME;
5114           else
5115             {
5116               /* It is for the wrong frame.  */
5117               this_action = BPSTAT_WHAT_SINGLE;
5118             }
5119           break;
5120         case bp_hp_step_resume:
5121           if (bs->stop)
5122             this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5123           else
5124             {
5125               /* It is for the wrong frame.  */
5126               this_action = BPSTAT_WHAT_SINGLE;
5127             }
5128           break;
5129         case bp_watchpoint_scope:
5130         case bp_thread_event:
5131         case bp_overlay_event:
5132         case bp_longjmp_master:
5133         case bp_std_terminate_master:
5134         case bp_exception_master:
5135           this_action = BPSTAT_WHAT_SINGLE;
5136           break;
5137         case bp_catchpoint:
5138           if (bs->stop)
5139             {
5140               if (bs->print)
5141                 this_action = BPSTAT_WHAT_STOP_NOISY;
5142               else
5143                 this_action = BPSTAT_WHAT_STOP_SILENT;
5144             }
5145           else
5146             {
5147               /* There was a catchpoint, but we're not stopping.
5148                  This requires no further action.  */
5149             }
5150           break;
5151         case bp_jit_event:
5152           jit_event = 1;
5153           this_action = BPSTAT_WHAT_SINGLE;
5154           break;
5155         case bp_call_dummy:
5156           /* Make sure the action is stop (silent or noisy),
5157              so infrun.c pops the dummy frame.  */
5158           retval.call_dummy = STOP_STACK_DUMMY;
5159           this_action = BPSTAT_WHAT_STOP_SILENT;
5160           break;
5161         case bp_std_terminate:
5162           /* Make sure the action is stop (silent or noisy),
5163              so infrun.c pops the dummy frame.  */
5164           retval.call_dummy = STOP_STD_TERMINATE;
5165           this_action = BPSTAT_WHAT_STOP_SILENT;
5166           break;
5167         case bp_tracepoint:
5168         case bp_fast_tracepoint:
5169         case bp_static_tracepoint:
5170           /* Tracepoint hits should not be reported back to GDB, and
5171              if one got through somehow, it should have been filtered
5172              out already.  */
5173           internal_error (__FILE__, __LINE__,
5174                           _("bpstat_what: tracepoint encountered"));
5175           break;
5176         case bp_gnu_ifunc_resolver:
5177           /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
5178           this_action = BPSTAT_WHAT_SINGLE;
5179           break;
5180         case bp_gnu_ifunc_resolver_return:
5181           /* The breakpoint will be removed, execution will restart from the
5182              PC of the former breakpoint.  */
5183           this_action = BPSTAT_WHAT_KEEP_CHECKING;
5184           break;
5185
5186         case bp_dprintf:
5187           this_action = BPSTAT_WHAT_STOP_SILENT;
5188           break;
5189
5190         default:
5191           internal_error (__FILE__, __LINE__,
5192                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
5193         }
5194
5195       retval.main_action = max (retval.main_action, this_action);
5196     }
5197
5198   /* These operations may affect the bs->breakpoint_at state so they are
5199      delayed after MAIN_ACTION is decided above.  */
5200
5201   if (jit_event)
5202     {
5203       if (debug_infrun)
5204         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5205
5206       handle_jit_event ();
5207     }
5208
5209   for (bs = bs_head; bs != NULL; bs = bs->next)
5210     {
5211       struct breakpoint *b = bs->breakpoint_at;
5212
5213       if (b == NULL)
5214         continue;
5215       switch (b->type)
5216         {
5217         case bp_gnu_ifunc_resolver:
5218           gnu_ifunc_resolver_stop (b);
5219           break;
5220         case bp_gnu_ifunc_resolver_return:
5221           gnu_ifunc_resolver_return_stop (b);
5222           break;
5223         }
5224     }
5225
5226   return retval;
5227 }
5228
5229 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5230    without hardware support).  This isn't related to a specific bpstat,
5231    just to things like whether watchpoints are set.  */
5232
5233 int
5234 bpstat_should_step (void)
5235 {
5236   struct breakpoint *b;
5237
5238   ALL_BREAKPOINTS (b)
5239     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5240       return 1;
5241   return 0;
5242 }
5243
5244 int
5245 bpstat_causes_stop (bpstat bs)
5246 {
5247   for (; bs != NULL; bs = bs->next)
5248     if (bs->stop)
5249       return 1;
5250
5251   return 0;
5252 }
5253
5254 \f
5255
5256 /* Compute a string of spaces suitable to indent the next line
5257    so it starts at the position corresponding to the table column
5258    named COL_NAME in the currently active table of UIOUT.  */
5259
5260 static char *
5261 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5262 {
5263   static char wrap_indent[80];
5264   int i, total_width, width, align;
5265   char *text;
5266
5267   total_width = 0;
5268   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5269     {
5270       if (strcmp (text, col_name) == 0)
5271         {
5272           gdb_assert (total_width < sizeof wrap_indent);
5273           memset (wrap_indent, ' ', total_width);
5274           wrap_indent[total_width] = 0;
5275
5276           return wrap_indent;
5277         }
5278
5279       total_width += width + 1;
5280     }
5281
5282   return NULL;
5283 }
5284
5285 /* Determine if the locations of this breakpoint will have their conditions
5286    evaluated by the target, host or a mix of both.  Returns the following:
5287
5288     "host": Host evals condition.
5289     "host or target": Host or Target evals condition.
5290     "target": Target evals condition.
5291 */
5292
5293 static const char *
5294 bp_condition_evaluator (struct breakpoint *b)
5295 {
5296   struct bp_location *bl;
5297   char host_evals = 0;
5298   char target_evals = 0;
5299
5300   if (!b)
5301     return NULL;
5302
5303   if (!is_breakpoint (b))
5304     return NULL;
5305
5306   if (gdb_evaluates_breakpoint_condition_p ()
5307       || !target_supports_evaluation_of_breakpoint_conditions ())
5308     return condition_evaluation_host;
5309
5310   for (bl = b->loc; bl; bl = bl->next)
5311     {
5312       if (bl->cond_bytecode)
5313         target_evals++;
5314       else
5315         host_evals++;
5316     }
5317
5318   if (host_evals && target_evals)
5319     return condition_evaluation_both;
5320   else if (target_evals)
5321     return condition_evaluation_target;
5322   else
5323     return condition_evaluation_host;
5324 }
5325
5326 /* Determine the breakpoint location's condition evaluator.  This is
5327    similar to bp_condition_evaluator, but for locations.  */
5328
5329 static const char *
5330 bp_location_condition_evaluator (struct bp_location *bl)
5331 {
5332   if (bl && !is_breakpoint (bl->owner))
5333     return NULL;
5334
5335   if (gdb_evaluates_breakpoint_condition_p ()
5336       || !target_supports_evaluation_of_breakpoint_conditions ())
5337     return condition_evaluation_host;
5338
5339   if (bl && bl->cond_bytecode)
5340     return condition_evaluation_target;
5341   else
5342     return condition_evaluation_host;
5343 }
5344
5345 /* Print the LOC location out of the list of B->LOC locations.  */
5346
5347 static void
5348 print_breakpoint_location (struct breakpoint *b,
5349                            struct bp_location *loc)
5350 {
5351   struct ui_out *uiout = current_uiout;
5352   struct cleanup *old_chain = save_current_program_space ();
5353
5354   if (loc != NULL && loc->shlib_disabled)
5355     loc = NULL;
5356
5357   if (loc != NULL)
5358     set_current_program_space (loc->pspace);
5359
5360   if (b->display_canonical)
5361     ui_out_field_string (uiout, "what", b->addr_string);
5362   else if (loc && loc->source_file)
5363     {
5364       struct symbol *sym 
5365         = find_pc_sect_function (loc->address, loc->section);
5366       if (sym)
5367         {
5368           ui_out_text (uiout, "in ");
5369           ui_out_field_string (uiout, "func",
5370                                SYMBOL_PRINT_NAME (sym));
5371           ui_out_text (uiout, " ");
5372           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5373           ui_out_text (uiout, "at ");
5374         }
5375       ui_out_field_string (uiout, "file", loc->source_file);
5376       ui_out_text (uiout, ":");
5377       
5378       if (ui_out_is_mi_like_p (uiout))
5379         {
5380           struct symtab_and_line sal = find_pc_line (loc->address, 0);
5381           char *fullname = symtab_to_fullname (sal.symtab);
5382           
5383           if (fullname)
5384             ui_out_field_string (uiout, "fullname", fullname);
5385         }
5386       
5387       ui_out_field_int (uiout, "line", loc->line_number);
5388     }
5389   else if (loc)
5390     {
5391       struct ui_file *stb = mem_fileopen ();
5392       struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5393
5394       print_address_symbolic (loc->gdbarch, loc->address, stb,
5395                               demangle, "");
5396       ui_out_field_stream (uiout, "at", stb);
5397
5398       do_cleanups (stb_chain);
5399     }
5400   else
5401     ui_out_field_string (uiout, "pending", b->addr_string);
5402
5403   if (loc && is_breakpoint (b)
5404       && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5405       && bp_condition_evaluator (b) == condition_evaluation_both)
5406     {
5407       ui_out_text (uiout, " (");
5408       ui_out_field_string (uiout, "evaluated-by",
5409                            bp_location_condition_evaluator (loc));
5410       ui_out_text (uiout, ")");
5411     }
5412
5413   do_cleanups (old_chain);
5414 }
5415
5416 static const char *
5417 bptype_string (enum bptype type)
5418 {
5419   struct ep_type_description
5420     {
5421       enum bptype type;
5422       char *description;
5423     };
5424   static struct ep_type_description bptypes[] =
5425   {
5426     {bp_none, "?deleted?"},
5427     {bp_breakpoint, "breakpoint"},
5428     {bp_hardware_breakpoint, "hw breakpoint"},
5429     {bp_until, "until"},
5430     {bp_finish, "finish"},
5431     {bp_watchpoint, "watchpoint"},
5432     {bp_hardware_watchpoint, "hw watchpoint"},
5433     {bp_read_watchpoint, "read watchpoint"},
5434     {bp_access_watchpoint, "acc watchpoint"},
5435     {bp_longjmp, "longjmp"},
5436     {bp_longjmp_resume, "longjmp resume"},
5437     {bp_exception, "exception"},
5438     {bp_exception_resume, "exception resume"},
5439     {bp_step_resume, "step resume"},
5440     {bp_hp_step_resume, "high-priority step resume"},
5441     {bp_watchpoint_scope, "watchpoint scope"},
5442     {bp_call_dummy, "call dummy"},
5443     {bp_std_terminate, "std::terminate"},
5444     {bp_shlib_event, "shlib events"},
5445     {bp_thread_event, "thread events"},
5446     {bp_overlay_event, "overlay events"},
5447     {bp_longjmp_master, "longjmp master"},
5448     {bp_std_terminate_master, "std::terminate master"},
5449     {bp_exception_master, "exception master"},
5450     {bp_catchpoint, "catchpoint"},
5451     {bp_tracepoint, "tracepoint"},
5452     {bp_fast_tracepoint, "fast tracepoint"},
5453     {bp_static_tracepoint, "static tracepoint"},
5454     {bp_dprintf, "dprintf"},
5455     {bp_jit_event, "jit events"},
5456     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5457     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5458   };
5459
5460   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5461       || ((int) type != bptypes[(int) type].type))
5462     internal_error (__FILE__, __LINE__,
5463                     _("bptypes table does not describe type #%d."),
5464                     (int) type);
5465
5466   return bptypes[(int) type].description;
5467 }
5468
5469 /* Print B to gdb_stdout.  */
5470
5471 static void
5472 print_one_breakpoint_location (struct breakpoint *b,
5473                                struct bp_location *loc,
5474                                int loc_number,
5475                                struct bp_location **last_loc,
5476                                int allflag)
5477 {
5478   struct command_line *l;
5479   static char bpenables[] = "nynny";
5480
5481   struct ui_out *uiout = current_uiout;
5482   int header_of_multiple = 0;
5483   int part_of_multiple = (loc != NULL);
5484   struct value_print_options opts;
5485
5486   get_user_print_options (&opts);
5487
5488   gdb_assert (!loc || loc_number != 0);
5489   /* See comment in print_one_breakpoint concerning treatment of
5490      breakpoints with single disabled location.  */
5491   if (loc == NULL 
5492       && (b->loc != NULL 
5493           && (b->loc->next != NULL || !b->loc->enabled)))
5494     header_of_multiple = 1;
5495   if (loc == NULL)
5496     loc = b->loc;
5497
5498   annotate_record ();
5499
5500   /* 1 */
5501   annotate_field (0);
5502   if (part_of_multiple)
5503     {
5504       char *formatted;
5505       formatted = xstrprintf ("%d.%d", b->number, loc_number);
5506       ui_out_field_string (uiout, "number", formatted);
5507       xfree (formatted);
5508     }
5509   else
5510     {
5511       ui_out_field_int (uiout, "number", b->number);
5512     }
5513
5514   /* 2 */
5515   annotate_field (1);
5516   if (part_of_multiple)
5517     ui_out_field_skip (uiout, "type");
5518   else
5519     ui_out_field_string (uiout, "type", bptype_string (b->type));
5520
5521   /* 3 */
5522   annotate_field (2);
5523   if (part_of_multiple)
5524     ui_out_field_skip (uiout, "disp");
5525   else
5526     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
5527
5528
5529   /* 4 */
5530   annotate_field (3);
5531   if (part_of_multiple)
5532     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
5533   else
5534     ui_out_field_fmt (uiout, "enabled", "%c", 
5535                       bpenables[(int) b->enable_state]);
5536   ui_out_spaces (uiout, 2);
5537
5538   
5539   /* 5 and 6 */
5540   if (b->ops != NULL && b->ops->print_one != NULL)
5541     {
5542       /* Although the print_one can possibly print all locations,
5543          calling it here is not likely to get any nice result.  So,
5544          make sure there's just one location.  */
5545       gdb_assert (b->loc == NULL || b->loc->next == NULL);
5546       b->ops->print_one (b, last_loc);
5547     }
5548   else
5549     switch (b->type)
5550       {
5551       case bp_none:
5552         internal_error (__FILE__, __LINE__,
5553                         _("print_one_breakpoint: bp_none encountered\n"));
5554         break;
5555
5556       case bp_watchpoint:
5557       case bp_hardware_watchpoint:
5558       case bp_read_watchpoint:
5559       case bp_access_watchpoint:
5560         {
5561           struct watchpoint *w = (struct watchpoint *) b;
5562
5563           /* Field 4, the address, is omitted (which makes the columns
5564              not line up too nicely with the headers, but the effect
5565              is relatively readable).  */
5566           if (opts.addressprint)
5567             ui_out_field_skip (uiout, "addr");
5568           annotate_field (5);
5569           ui_out_field_string (uiout, "what", w->exp_string);
5570         }
5571         break;
5572
5573       case bp_breakpoint:
5574       case bp_hardware_breakpoint:
5575       case bp_until:
5576       case bp_finish:
5577       case bp_longjmp:
5578       case bp_longjmp_resume:
5579       case bp_exception:
5580       case bp_exception_resume:
5581       case bp_step_resume:
5582       case bp_hp_step_resume:
5583       case bp_watchpoint_scope:
5584       case bp_call_dummy:
5585       case bp_std_terminate:
5586       case bp_shlib_event:
5587       case bp_thread_event:
5588       case bp_overlay_event:
5589       case bp_longjmp_master:
5590       case bp_std_terminate_master:
5591       case bp_exception_master:
5592       case bp_tracepoint:
5593       case bp_fast_tracepoint:
5594       case bp_static_tracepoint:
5595       case bp_dprintf:
5596       case bp_jit_event:
5597       case bp_gnu_ifunc_resolver:
5598       case bp_gnu_ifunc_resolver_return:
5599         if (opts.addressprint)
5600           {
5601             annotate_field (4);
5602             if (header_of_multiple)
5603               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
5604             else if (b->loc == NULL || loc->shlib_disabled)
5605               ui_out_field_string (uiout, "addr", "<PENDING>");
5606             else
5607               ui_out_field_core_addr (uiout, "addr",
5608                                       loc->gdbarch, loc->address);
5609           }
5610         annotate_field (5);
5611         if (!header_of_multiple)
5612           print_breakpoint_location (b, loc);
5613         if (b->loc)
5614           *last_loc = b->loc;
5615         break;
5616       }
5617
5618
5619   /* For backward compatibility, don't display inferiors unless there
5620      are several.  */
5621   if (loc != NULL
5622       && !header_of_multiple
5623       && (allflag
5624           || (!gdbarch_has_global_breakpoints (target_gdbarch)
5625               && (number_of_program_spaces () > 1
5626                   || number_of_inferiors () > 1)
5627               /* LOC is for existing B, it cannot be in
5628                  moribund_locations and thus having NULL OWNER.  */
5629               && loc->owner->type != bp_catchpoint)))
5630     {
5631       struct inferior *inf;
5632       int first = 1;
5633
5634       for (inf = inferior_list; inf != NULL; inf = inf->next)
5635         {
5636           if (inf->pspace == loc->pspace)
5637             {
5638               if (first)
5639                 {
5640                   first = 0;
5641                   ui_out_text (uiout, " inf ");
5642                 }
5643               else
5644                 ui_out_text (uiout, ", ");
5645               ui_out_text (uiout, plongest (inf->num));
5646             }
5647         }
5648     }
5649
5650   if (!part_of_multiple)
5651     {
5652       if (b->thread != -1)
5653         {
5654           /* FIXME: This seems to be redundant and lost here; see the
5655              "stop only in" line a little further down.  */
5656           ui_out_text (uiout, " thread ");
5657           ui_out_field_int (uiout, "thread", b->thread);
5658         }
5659       else if (b->task != 0)
5660         {
5661           ui_out_text (uiout, " task ");
5662           ui_out_field_int (uiout, "task", b->task);
5663         }
5664     }
5665
5666   ui_out_text (uiout, "\n");
5667
5668   if (!part_of_multiple)
5669     b->ops->print_one_detail (b, uiout);
5670
5671   if (part_of_multiple && frame_id_p (b->frame_id))
5672     {
5673       annotate_field (6);
5674       ui_out_text (uiout, "\tstop only in stack frame at ");
5675       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
5676          the frame ID.  */
5677       ui_out_field_core_addr (uiout, "frame",
5678                               b->gdbarch, b->frame_id.stack_addr);
5679       ui_out_text (uiout, "\n");
5680     }
5681   
5682   if (!part_of_multiple && b->cond_string)
5683     {
5684       annotate_field (7);
5685       if (is_tracepoint (b))
5686         ui_out_text (uiout, "\ttrace only if ");
5687       else
5688         ui_out_text (uiout, "\tstop only if ");
5689       ui_out_field_string (uiout, "cond", b->cond_string);
5690
5691       /* Print whether the target is doing the breakpoint's condition
5692          evaluation.  If GDB is doing the evaluation, don't print anything.  */
5693       if (is_breakpoint (b)
5694           && breakpoint_condition_evaluation_mode ()
5695           == condition_evaluation_target)
5696         {
5697           ui_out_text (uiout, " (");
5698           ui_out_field_string (uiout, "evaluated-by",
5699                                bp_condition_evaluator (b));
5700           ui_out_text (uiout, " evals)");
5701         }
5702       ui_out_text (uiout, "\n");
5703     }
5704
5705   if (!part_of_multiple && b->thread != -1)
5706     {
5707       /* FIXME should make an annotation for this.  */
5708       ui_out_text (uiout, "\tstop only in thread ");
5709       ui_out_field_int (uiout, "thread", b->thread);
5710       ui_out_text (uiout, "\n");
5711     }
5712   
5713   if (!part_of_multiple && b->hit_count)
5714     {
5715       /* FIXME should make an annotation for this.  */
5716       if (is_catchpoint (b))
5717         ui_out_text (uiout, "\tcatchpoint");
5718       else if (is_tracepoint (b))
5719         ui_out_text (uiout, "\ttracepoint");
5720       else
5721         ui_out_text (uiout, "\tbreakpoint");
5722       ui_out_text (uiout, " already hit ");
5723       ui_out_field_int (uiout, "times", b->hit_count);
5724       if (b->hit_count == 1)
5725         ui_out_text (uiout, " time\n");
5726       else
5727         ui_out_text (uiout, " times\n");
5728     }
5729   
5730   /* Output the count also if it is zero, but only if this is mi.
5731      FIXME: Should have a better test for this.  */
5732   if (ui_out_is_mi_like_p (uiout))
5733     if (!part_of_multiple && b->hit_count == 0)
5734       ui_out_field_int (uiout, "times", b->hit_count);
5735
5736   if (!part_of_multiple && b->ignore_count)
5737     {
5738       annotate_field (8);
5739       ui_out_text (uiout, "\tignore next ");
5740       ui_out_field_int (uiout, "ignore", b->ignore_count);
5741       ui_out_text (uiout, " hits\n");
5742     }
5743
5744   /* Note that an enable count of 1 corresponds to "enable once"
5745      behavior, which is reported by the combination of enablement and
5746      disposition, so we don't need to mention it here.  */
5747   if (!part_of_multiple && b->enable_count > 1)
5748     {
5749       annotate_field (8);
5750       ui_out_text (uiout, "\tdisable after ");
5751       /* Tweak the wording to clarify that ignore and enable counts
5752          are distinct, and have additive effect.  */
5753       if (b->ignore_count)
5754         ui_out_text (uiout, "additional ");
5755       else
5756         ui_out_text (uiout, "next ");
5757       ui_out_field_int (uiout, "enable", b->enable_count);
5758       ui_out_text (uiout, " hits\n");
5759     }
5760
5761   if (!part_of_multiple && is_tracepoint (b))
5762     {
5763       struct tracepoint *tp = (struct tracepoint *) b;
5764
5765       if (tp->traceframe_usage)
5766         {
5767           ui_out_text (uiout, "\ttrace buffer usage ");
5768           ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5769           ui_out_text (uiout, " bytes\n");
5770         }
5771     }
5772   
5773   l = b->commands ? b->commands->commands : NULL;
5774   if (!part_of_multiple && l)
5775     {
5776       struct cleanup *script_chain;
5777
5778       annotate_field (9);
5779       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
5780       print_command_lines (uiout, l, 4);
5781       do_cleanups (script_chain);
5782     }
5783
5784   if (is_tracepoint (b))
5785     {
5786       struct tracepoint *t = (struct tracepoint *) b;
5787
5788       if (!part_of_multiple && t->pass_count)
5789         {
5790           annotate_field (10);
5791           ui_out_text (uiout, "\tpass count ");
5792           ui_out_field_int (uiout, "pass", t->pass_count);
5793           ui_out_text (uiout, " \n");
5794         }
5795     }
5796
5797   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5798     {
5799       if (is_watchpoint (b))
5800         {
5801           struct watchpoint *w = (struct watchpoint *) b;
5802
5803           ui_out_field_string (uiout, "original-location", w->exp_string);
5804         }
5805       else if (b->addr_string)
5806         ui_out_field_string (uiout, "original-location", b->addr_string);
5807     }
5808 }
5809
5810 static void
5811 print_one_breakpoint (struct breakpoint *b,
5812                       struct bp_location **last_loc, 
5813                       int allflag)
5814 {
5815   struct cleanup *bkpt_chain;
5816   struct ui_out *uiout = current_uiout;
5817
5818   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5819
5820   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
5821   do_cleanups (bkpt_chain);
5822
5823   /* If this breakpoint has custom print function,
5824      it's already printed.  Otherwise, print individual
5825      locations, if any.  */
5826   if (b->ops == NULL || b->ops->print_one == NULL)
5827     {
5828       /* If breakpoint has a single location that is disabled, we
5829          print it as if it had several locations, since otherwise it's
5830          hard to represent "breakpoint enabled, location disabled"
5831          situation.
5832
5833          Note that while hardware watchpoints have several locations
5834          internally, that's not a property exposed to user.  */
5835       if (b->loc 
5836           && !is_hardware_watchpoint (b)
5837           && (b->loc->next || !b->loc->enabled))
5838         {
5839           struct bp_location *loc;
5840           int n = 1;
5841
5842           for (loc = b->loc; loc; loc = loc->next, ++n)
5843             {
5844               struct cleanup *inner2 =
5845                 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5846               print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5847               do_cleanups (inner2);
5848             }
5849         }
5850     }
5851 }
5852
5853 static int
5854 breakpoint_address_bits (struct breakpoint *b)
5855 {
5856   int print_address_bits = 0;
5857   struct bp_location *loc;
5858
5859   for (loc = b->loc; loc; loc = loc->next)
5860     {
5861       int addr_bit;
5862
5863       /* Software watchpoints that aren't watching memory don't have
5864          an address to print.  */
5865       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5866         continue;
5867
5868       addr_bit = gdbarch_addr_bit (loc->gdbarch);
5869       if (addr_bit > print_address_bits)
5870         print_address_bits = addr_bit;
5871     }
5872
5873   return print_address_bits;
5874 }
5875
5876 struct captured_breakpoint_query_args
5877   {
5878     int bnum;
5879   };
5880
5881 static int
5882 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5883 {
5884   struct captured_breakpoint_query_args *args = data;
5885   struct breakpoint *b;
5886   struct bp_location *dummy_loc = NULL;
5887
5888   ALL_BREAKPOINTS (b)
5889     {
5890       if (args->bnum == b->number)
5891         {
5892           print_one_breakpoint (b, &dummy_loc, 0);
5893           return GDB_RC_OK;
5894         }
5895     }
5896   return GDB_RC_NONE;
5897 }
5898
5899 enum gdb_rc
5900 gdb_breakpoint_query (struct ui_out *uiout, int bnum, 
5901                       char **error_message)
5902 {
5903   struct captured_breakpoint_query_args args;
5904
5905   args.bnum = bnum;
5906   /* For the moment we don't trust print_one_breakpoint() to not throw
5907      an error.  */
5908   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5909                                  error_message, RETURN_MASK_ALL) < 0)
5910     return GDB_RC_FAIL;
5911   else
5912     return GDB_RC_OK;
5913 }
5914
5915 /* Return true if this breakpoint was set by the user, false if it is
5916    internal or momentary.  */
5917
5918 int
5919 user_breakpoint_p (struct breakpoint *b)
5920 {
5921   return b->number > 0;
5922 }
5923
5924 /* Print information on user settable breakpoint (watchpoint, etc)
5925    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
5926    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
5927    FILTER is non-NULL, call it on each breakpoint and only include the
5928    ones for which it returns non-zero.  Return the total number of
5929    breakpoints listed.  */
5930
5931 static int
5932 breakpoint_1 (char *args, int allflag, 
5933               int (*filter) (const struct breakpoint *))
5934 {
5935   struct breakpoint *b;
5936   struct bp_location *last_loc = NULL;
5937   int nr_printable_breakpoints;
5938   struct cleanup *bkpttbl_chain;
5939   struct value_print_options opts;
5940   int print_address_bits = 0;
5941   int print_type_col_width = 14;
5942   struct ui_out *uiout = current_uiout;
5943
5944   get_user_print_options (&opts);
5945
5946   /* Compute the number of rows in the table, as well as the size
5947      required for address fields.  */
5948   nr_printable_breakpoints = 0;
5949   ALL_BREAKPOINTS (b)
5950     {
5951       /* If we have a filter, only list the breakpoints it accepts.  */
5952       if (filter && !filter (b))
5953         continue;
5954
5955       /* If we have an "args" string, it is a list of breakpoints to 
5956          accept.  Skip the others.  */
5957       if (args != NULL && *args != '\0')
5958         {
5959           if (allflag && parse_and_eval_long (args) != b->number)
5960             continue;
5961           if (!allflag && !number_is_in_list (args, b->number))
5962             continue;
5963         }
5964
5965       if (allflag || user_breakpoint_p (b))
5966         {
5967           int addr_bit, type_len;
5968
5969           addr_bit = breakpoint_address_bits (b);
5970           if (addr_bit > print_address_bits)
5971             print_address_bits = addr_bit;
5972
5973           type_len = strlen (bptype_string (b->type));
5974           if (type_len > print_type_col_width)
5975             print_type_col_width = type_len;
5976
5977           nr_printable_breakpoints++;
5978         }
5979     }
5980
5981   if (opts.addressprint)
5982     bkpttbl_chain 
5983       = make_cleanup_ui_out_table_begin_end (uiout, 6,
5984                                              nr_printable_breakpoints,
5985                                              "BreakpointTable");
5986   else
5987     bkpttbl_chain 
5988       = make_cleanup_ui_out_table_begin_end (uiout, 5,
5989                                              nr_printable_breakpoints,
5990                                              "BreakpointTable");
5991
5992   if (nr_printable_breakpoints > 0)
5993     annotate_breakpoints_headers ();
5994   if (nr_printable_breakpoints > 0)
5995     annotate_field (0);
5996   ui_out_table_header (uiout, 7, ui_left, "number", "Num");     /* 1 */
5997   if (nr_printable_breakpoints > 0)
5998     annotate_field (1);
5999   ui_out_table_header (uiout, print_type_col_width, ui_left,
6000                        "type", "Type");                         /* 2 */
6001   if (nr_printable_breakpoints > 0)
6002     annotate_field (2);
6003   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");      /* 3 */
6004   if (nr_printable_breakpoints > 0)
6005     annotate_field (3);
6006   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
6007   if (opts.addressprint)
6008     {
6009       if (nr_printable_breakpoints > 0)
6010         annotate_field (4);
6011       if (print_address_bits <= 32)
6012         ui_out_table_header (uiout, 10, ui_left, 
6013                              "addr", "Address");                /* 5 */
6014       else
6015         ui_out_table_header (uiout, 18, ui_left, 
6016                              "addr", "Address");                /* 5 */
6017     }
6018   if (nr_printable_breakpoints > 0)
6019     annotate_field (5);
6020   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
6021   ui_out_table_body (uiout);
6022   if (nr_printable_breakpoints > 0)
6023     annotate_breakpoints_table ();
6024
6025   ALL_BREAKPOINTS (b)
6026     {
6027       QUIT;
6028       /* If we have a filter, only list the breakpoints it accepts.  */
6029       if (filter && !filter (b))
6030         continue;
6031
6032       /* If we have an "args" string, it is a list of breakpoints to 
6033          accept.  Skip the others.  */
6034
6035       if (args != NULL && *args != '\0')
6036         {
6037           if (allflag)  /* maintenance info breakpoint */
6038             {
6039               if (parse_and_eval_long (args) != b->number)
6040                 continue;
6041             }
6042           else          /* all others */
6043             {
6044               if (!number_is_in_list (args, b->number))
6045                 continue;
6046             }
6047         }
6048       /* We only print out user settable breakpoints unless the
6049          allflag is set.  */
6050       if (allflag || user_breakpoint_p (b))
6051         print_one_breakpoint (b, &last_loc, allflag);
6052     }
6053
6054   do_cleanups (bkpttbl_chain);
6055
6056   if (nr_printable_breakpoints == 0)
6057     {
6058       /* If there's a filter, let the caller decide how to report
6059          empty list.  */
6060       if (!filter)
6061         {
6062           if (args == NULL || *args == '\0')
6063             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6064           else
6065             ui_out_message (uiout, 0, 
6066                             "No breakpoint or watchpoint matching '%s'.\n",
6067                             args);
6068         }
6069     }
6070   else
6071     {
6072       if (last_loc && !server_command)
6073         set_next_address (last_loc->gdbarch, last_loc->address);
6074     }
6075
6076   /* FIXME?  Should this be moved up so that it is only called when
6077      there have been breakpoints? */
6078   annotate_breakpoints_table_end ();
6079
6080   return nr_printable_breakpoints;
6081 }
6082
6083 /* Display the value of default-collect in a way that is generally
6084    compatible with the breakpoint list.  */
6085
6086 static void
6087 default_collect_info (void)
6088 {
6089   struct ui_out *uiout = current_uiout;
6090
6091   /* If it has no value (which is frequently the case), say nothing; a
6092      message like "No default-collect." gets in user's face when it's
6093      not wanted.  */
6094   if (!*default_collect)
6095     return;
6096
6097   /* The following phrase lines up nicely with per-tracepoint collect
6098      actions.  */
6099   ui_out_text (uiout, "default collect ");
6100   ui_out_field_string (uiout, "default-collect", default_collect);
6101   ui_out_text (uiout, " \n");
6102 }
6103   
6104 static void
6105 breakpoints_info (char *args, int from_tty)
6106 {
6107   breakpoint_1 (args, 0, NULL);
6108
6109   default_collect_info ();
6110 }
6111
6112 static void
6113 watchpoints_info (char *args, int from_tty)
6114 {
6115   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6116   struct ui_out *uiout = current_uiout;
6117
6118   if (num_printed == 0)
6119     {
6120       if (args == NULL || *args == '\0')
6121         ui_out_message (uiout, 0, "No watchpoints.\n");
6122       else
6123         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6124     }
6125 }
6126
6127 static void
6128 maintenance_info_breakpoints (char *args, int from_tty)
6129 {
6130   breakpoint_1 (args, 1, NULL);
6131
6132   default_collect_info ();
6133 }
6134
6135 static int
6136 breakpoint_has_pc (struct breakpoint *b,
6137                    struct program_space *pspace,
6138                    CORE_ADDR pc, struct obj_section *section)
6139 {
6140   struct bp_location *bl = b->loc;
6141
6142   for (; bl; bl = bl->next)
6143     {
6144       if (bl->pspace == pspace
6145           && bl->address == pc
6146           && (!overlay_debugging || bl->section == section))
6147         return 1;         
6148     }
6149   return 0;
6150 }
6151
6152 /* Print a message describing any user-breakpoints set at PC.  This
6153    concerns with logical breakpoints, so we match program spaces, not
6154    address spaces.  */
6155
6156 static void
6157 describe_other_breakpoints (struct gdbarch *gdbarch,
6158                             struct program_space *pspace, CORE_ADDR pc,
6159                             struct obj_section *section, int thread)
6160 {
6161   int others = 0;
6162   struct breakpoint *b;
6163
6164   ALL_BREAKPOINTS (b)
6165     others += (user_breakpoint_p (b)
6166                && breakpoint_has_pc (b, pspace, pc, section));
6167   if (others > 0)
6168     {
6169       if (others == 1)
6170         printf_filtered (_("Note: breakpoint "));
6171       else /* if (others == ???) */
6172         printf_filtered (_("Note: breakpoints "));
6173       ALL_BREAKPOINTS (b)
6174         if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6175           {
6176             others--;
6177             printf_filtered ("%d", b->number);
6178             if (b->thread == -1 && thread != -1)
6179               printf_filtered (" (all threads)");
6180             else if (b->thread != -1)
6181               printf_filtered (" (thread %d)", b->thread);
6182             printf_filtered ("%s%s ",
6183                              ((b->enable_state == bp_disabled
6184                                || b->enable_state == bp_call_disabled)
6185                               ? " (disabled)"
6186                               : b->enable_state == bp_permanent 
6187                               ? " (permanent)"
6188                               : ""),
6189                              (others > 1) ? "," 
6190                              : ((others == 1) ? " and" : ""));
6191           }
6192       printf_filtered (_("also set at pc "));
6193       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6194       printf_filtered (".\n");
6195     }
6196 }
6197 \f
6198
6199 /* Return true iff it is meaningful to use the address member of
6200    BPT.  For some breakpoint types, the address member is irrelevant
6201    and it makes no sense to attempt to compare it to other addresses
6202    (or use it for any other purpose either).
6203
6204    More specifically, each of the following breakpoint types will
6205    always have a zero valued address and we don't want to mark
6206    breakpoints of any of these types to be a duplicate of an actual
6207    breakpoint at address zero:
6208
6209       bp_watchpoint
6210       bp_catchpoint
6211
6212 */
6213
6214 static int
6215 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6216 {
6217   enum bptype type = bpt->type;
6218
6219   return (type != bp_watchpoint && type != bp_catchpoint);
6220 }
6221
6222 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6223    true if LOC1 and LOC2 represent the same watchpoint location.  */
6224
6225 static int
6226 watchpoint_locations_match (struct bp_location *loc1, 
6227                             struct bp_location *loc2)
6228 {
6229   struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6230   struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6231
6232   /* Both of them must exist.  */
6233   gdb_assert (w1 != NULL);
6234   gdb_assert (w2 != NULL);
6235
6236   /* If the target can evaluate the condition expression in hardware,
6237      then we we need to insert both watchpoints even if they are at
6238      the same place.  Otherwise the watchpoint will only trigger when
6239      the condition of whichever watchpoint was inserted evaluates to
6240      true, not giving a chance for GDB to check the condition of the
6241      other watchpoint.  */
6242   if ((w1->cond_exp
6243        && target_can_accel_watchpoint_condition (loc1->address, 
6244                                                  loc1->length,
6245                                                  loc1->watchpoint_type,
6246                                                  w1->cond_exp))
6247       || (w2->cond_exp
6248           && target_can_accel_watchpoint_condition (loc2->address, 
6249                                                     loc2->length,
6250                                                     loc2->watchpoint_type,
6251                                                     w2->cond_exp)))
6252     return 0;
6253
6254   /* Note that this checks the owner's type, not the location's.  In
6255      case the target does not support read watchpoints, but does
6256      support access watchpoints, we'll have bp_read_watchpoint
6257      watchpoints with hw_access locations.  Those should be considered
6258      duplicates of hw_read locations.  The hw_read locations will
6259      become hw_access locations later.  */
6260   return (loc1->owner->type == loc2->owner->type
6261           && loc1->pspace->aspace == loc2->pspace->aspace
6262           && loc1->address == loc2->address
6263           && loc1->length == loc2->length);
6264 }
6265
6266 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6267    same breakpoint location.  In most targets, this can only be true
6268    if ASPACE1 matches ASPACE2.  On targets that have global
6269    breakpoints, the address space doesn't really matter.  */
6270
6271 static int
6272 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6273                           struct address_space *aspace2, CORE_ADDR addr2)
6274 {
6275   return ((gdbarch_has_global_breakpoints (target_gdbarch)
6276            || aspace1 == aspace2)
6277           && addr1 == addr2);
6278 }
6279
6280 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6281    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
6282    matches ASPACE2.  On targets that have global breakpoints, the address
6283    space doesn't really matter.  */
6284
6285 static int
6286 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6287                                 int len1, struct address_space *aspace2,
6288                                 CORE_ADDR addr2)
6289 {
6290   return ((gdbarch_has_global_breakpoints (target_gdbarch)
6291            || aspace1 == aspace2)
6292           && addr2 >= addr1 && addr2 < addr1 + len1);
6293 }
6294
6295 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
6296    a ranged breakpoint.  In most targets, a match happens only if ASPACE
6297    matches the breakpoint's address space.  On targets that have global
6298    breakpoints, the address space doesn't really matter.  */
6299
6300 static int
6301 breakpoint_location_address_match (struct bp_location *bl,
6302                                    struct address_space *aspace,
6303                                    CORE_ADDR addr)
6304 {
6305   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6306                                     aspace, addr)
6307           || (bl->length
6308               && breakpoint_address_match_range (bl->pspace->aspace,
6309                                                  bl->address, bl->length,
6310                                                  aspace, addr)));
6311 }
6312
6313 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6314    Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6315    true, otherwise returns false.  */
6316
6317 static int
6318 tracepoint_locations_match (struct bp_location *loc1,
6319                             struct bp_location *loc2)
6320 {
6321   if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6322     /* Since tracepoint locations are never duplicated with others', tracepoint
6323        locations at the same address of different tracepoints are regarded as
6324        different locations.  */
6325     return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6326   else
6327     return 0;
6328 }
6329
6330 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6331    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6332    represent the same location.  */
6333
6334 static int
6335 breakpoint_locations_match (struct bp_location *loc1, 
6336                             struct bp_location *loc2)
6337 {
6338   int hw_point1, hw_point2;
6339
6340   /* Both of them must not be in moribund_locations.  */
6341   gdb_assert (loc1->owner != NULL);
6342   gdb_assert (loc2->owner != NULL);
6343
6344   hw_point1 = is_hardware_watchpoint (loc1->owner);
6345   hw_point2 = is_hardware_watchpoint (loc2->owner);
6346
6347   if (hw_point1 != hw_point2)
6348     return 0;
6349   else if (hw_point1)
6350     return watchpoint_locations_match (loc1, loc2);
6351   else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6352     return tracepoint_locations_match (loc1, loc2);
6353   else
6354     /* We compare bp_location.length in order to cover ranged breakpoints.  */
6355     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6356                                      loc2->pspace->aspace, loc2->address)
6357             && loc1->length == loc2->length);
6358 }
6359
6360 static void
6361 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6362                                int bnum, int have_bnum)
6363 {
6364   /* The longest string possibly returned by hex_string_custom
6365      is 50 chars.  These must be at least that big for safety.  */
6366   char astr1[64];
6367   char astr2[64];
6368
6369   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6370   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6371   if (have_bnum)
6372     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6373              bnum, astr1, astr2);
6374   else
6375     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6376 }
6377
6378 /* Adjust a breakpoint's address to account for architectural
6379    constraints on breakpoint placement.  Return the adjusted address.
6380    Note: Very few targets require this kind of adjustment.  For most
6381    targets, this function is simply the identity function.  */
6382
6383 static CORE_ADDR
6384 adjust_breakpoint_address (struct gdbarch *gdbarch,
6385                            CORE_ADDR bpaddr, enum bptype bptype)
6386 {
6387   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6388     {
6389       /* Very few targets need any kind of breakpoint adjustment.  */
6390       return bpaddr;
6391     }
6392   else if (bptype == bp_watchpoint
6393            || bptype == bp_hardware_watchpoint
6394            || bptype == bp_read_watchpoint
6395            || bptype == bp_access_watchpoint
6396            || bptype == bp_catchpoint)
6397     {
6398       /* Watchpoints and the various bp_catch_* eventpoints should not
6399          have their addresses modified.  */
6400       return bpaddr;
6401     }
6402   else
6403     {
6404       CORE_ADDR adjusted_bpaddr;
6405
6406       /* Some targets have architectural constraints on the placement
6407          of breakpoint instructions.  Obtain the adjusted address.  */
6408       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6409
6410       /* An adjusted breakpoint address can significantly alter
6411          a user's expectations.  Print a warning if an adjustment
6412          is required.  */
6413       if (adjusted_bpaddr != bpaddr)
6414         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6415
6416       return adjusted_bpaddr;
6417     }
6418 }
6419
6420 void
6421 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6422                   struct breakpoint *owner)
6423 {
6424   memset (loc, 0, sizeof (*loc));
6425
6426   gdb_assert (ops != NULL);
6427
6428   loc->ops = ops;
6429   loc->owner = owner;
6430   loc->cond = NULL;
6431   loc->cond_bytecode = NULL;
6432   loc->shlib_disabled = 0;
6433   loc->enabled = 1;
6434
6435   switch (owner->type)
6436     {
6437     case bp_breakpoint:
6438     case bp_until:
6439     case bp_finish:
6440     case bp_longjmp:
6441     case bp_longjmp_resume:
6442     case bp_exception:
6443     case bp_exception_resume:
6444     case bp_step_resume:
6445     case bp_hp_step_resume:
6446     case bp_watchpoint_scope:
6447     case bp_call_dummy:
6448     case bp_std_terminate:
6449     case bp_shlib_event:
6450     case bp_thread_event:
6451     case bp_overlay_event:
6452     case bp_jit_event:
6453     case bp_longjmp_master:
6454     case bp_std_terminate_master:
6455     case bp_exception_master:
6456     case bp_gnu_ifunc_resolver:
6457     case bp_gnu_ifunc_resolver_return:
6458     case bp_dprintf:
6459       loc->loc_type = bp_loc_software_breakpoint;
6460       mark_breakpoint_location_modified (loc);
6461       break;
6462     case bp_hardware_breakpoint:
6463       loc->loc_type = bp_loc_hardware_breakpoint;
6464       mark_breakpoint_location_modified (loc);
6465       break;
6466     case bp_hardware_watchpoint:
6467     case bp_read_watchpoint:
6468     case bp_access_watchpoint:
6469       loc->loc_type = bp_loc_hardware_watchpoint;
6470       break;
6471     case bp_watchpoint:
6472     case bp_catchpoint:
6473     case bp_tracepoint:
6474     case bp_fast_tracepoint:
6475     case bp_static_tracepoint:
6476       loc->loc_type = bp_loc_other;
6477       break;
6478     default:
6479       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6480     }
6481
6482   loc->refc = 1;
6483 }
6484
6485 /* Allocate a struct bp_location.  */
6486
6487 static struct bp_location *
6488 allocate_bp_location (struct breakpoint *bpt)
6489 {
6490   return bpt->ops->allocate_location (bpt);
6491 }
6492
6493 static void
6494 free_bp_location (struct bp_location *loc)
6495 {
6496   loc->ops->dtor (loc);
6497   xfree (loc);
6498 }
6499
6500 /* Increment reference count.  */
6501
6502 static void
6503 incref_bp_location (struct bp_location *bl)
6504 {
6505   ++bl->refc;
6506 }
6507
6508 /* Decrement reference count.  If the reference count reaches 0,
6509    destroy the bp_location.  Sets *BLP to NULL.  */
6510
6511 static void
6512 decref_bp_location (struct bp_location **blp)
6513 {
6514   gdb_assert ((*blp)->refc > 0);
6515
6516   if (--(*blp)->refc == 0)
6517     free_bp_location (*blp);
6518   *blp = NULL;
6519 }
6520
6521 /* Add breakpoint B at the end of the global breakpoint chain.  */
6522
6523 static void
6524 add_to_breakpoint_chain (struct breakpoint *b)
6525 {
6526   struct breakpoint *b1;
6527
6528   /* Add this breakpoint to the end of the chain so that a list of
6529      breakpoints will come out in order of increasing numbers.  */
6530
6531   b1 = breakpoint_chain;
6532   if (b1 == 0)
6533     breakpoint_chain = b;
6534   else
6535     {
6536       while (b1->next)
6537         b1 = b1->next;
6538       b1->next = b;
6539     }
6540 }
6541
6542 /* Initializes breakpoint B with type BPTYPE and no locations yet.  */
6543
6544 static void
6545 init_raw_breakpoint_without_location (struct breakpoint *b,
6546                                       struct gdbarch *gdbarch,
6547                                       enum bptype bptype,
6548                                       const struct breakpoint_ops *ops)
6549 {
6550   memset (b, 0, sizeof (*b));
6551
6552   gdb_assert (ops != NULL);
6553
6554   b->ops = ops;
6555   b->type = bptype;
6556   b->gdbarch = gdbarch;
6557   b->language = current_language->la_language;
6558   b->input_radix = input_radix;
6559   b->thread = -1;
6560   b->enable_state = bp_enabled;
6561   b->next = 0;
6562   b->silent = 0;
6563   b->ignore_count = 0;
6564   b->commands = NULL;
6565   b->frame_id = null_frame_id;
6566   b->condition_not_parsed = 0;
6567   b->py_bp_object = NULL;
6568   b->related_breakpoint = b;
6569 }
6570
6571 /* Helper to set_raw_breakpoint below.  Creates a breakpoint
6572    that has type BPTYPE and has no locations as yet.  */
6573
6574 static struct breakpoint *
6575 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
6576                                      enum bptype bptype,
6577                                      const struct breakpoint_ops *ops)
6578 {
6579   struct breakpoint *b = XNEW (struct breakpoint);
6580
6581   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6582   add_to_breakpoint_chain (b);
6583   return b;
6584 }
6585
6586 /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
6587    resolutions should be made as the user specified the location explicitly
6588    enough.  */
6589
6590 static void
6591 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
6592 {
6593   gdb_assert (loc->owner != NULL);
6594
6595   if (loc->owner->type == bp_breakpoint
6596       || loc->owner->type == bp_hardware_breakpoint
6597       || is_tracepoint (loc->owner))
6598     {
6599       int is_gnu_ifunc;
6600       const char *function_name;
6601       CORE_ADDR func_addr;
6602
6603       find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6604                                           &func_addr, NULL, &is_gnu_ifunc);
6605
6606       if (is_gnu_ifunc && !explicit_loc)
6607         {
6608           struct breakpoint *b = loc->owner;
6609
6610           gdb_assert (loc->pspace == current_program_space);
6611           if (gnu_ifunc_resolve_name (function_name,
6612                                       &loc->requested_address))
6613             {
6614               /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
6615               loc->address = adjust_breakpoint_address (loc->gdbarch,
6616                                                         loc->requested_address,
6617                                                         b->type);
6618             }
6619           else if (b->type == bp_breakpoint && b->loc == loc
6620                    && loc->next == NULL && b->related_breakpoint == b)
6621             {
6622               /* Create only the whole new breakpoint of this type but do not
6623                  mess more complicated breakpoints with multiple locations.  */
6624               b->type = bp_gnu_ifunc_resolver;
6625               /* Remember the resolver's address for use by the return
6626                  breakpoint.  */
6627               loc->related_address = func_addr;
6628             }
6629         }
6630
6631       if (function_name)
6632         loc->function_name = xstrdup (function_name);
6633     }
6634 }
6635
6636 /* Attempt to determine architecture of location identified by SAL.  */
6637 struct gdbarch *
6638 get_sal_arch (struct symtab_and_line sal)
6639 {
6640   if (sal.section)
6641     return get_objfile_arch (sal.section->objfile);
6642   if (sal.symtab)
6643     return get_objfile_arch (sal.symtab->objfile);
6644
6645   return NULL;
6646 }
6647
6648 /* Low level routine for partially initializing a breakpoint of type
6649    BPTYPE.  The newly created breakpoint's address, section, source
6650    file name, and line number are provided by SAL.
6651
6652    It is expected that the caller will complete the initialization of
6653    the newly created breakpoint struct as well as output any status
6654    information regarding the creation of a new breakpoint.  */
6655
6656 static void
6657 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
6658                      struct symtab_and_line sal, enum bptype bptype,
6659                      const struct breakpoint_ops *ops)
6660 {
6661   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6662
6663   add_location_to_breakpoint (b, &sal);
6664
6665   if (bptype != bp_catchpoint)
6666     gdb_assert (sal.pspace != NULL);
6667
6668   /* Store the program space that was used to set the breakpoint,
6669      except for ordinary breakpoints, which are independent of the
6670      program space.  */
6671   if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6672     b->pspace = sal.pspace;
6673
6674   breakpoints_changed ();
6675 }
6676
6677 /* set_raw_breakpoint is a low level routine for allocating and
6678    partially initializing a breakpoint of type BPTYPE.  The newly
6679    created breakpoint's address, section, source file name, and line
6680    number are provided by SAL.  The newly created and partially
6681    initialized breakpoint is added to the breakpoint chain and
6682    is also returned as the value of this function.
6683
6684    It is expected that the caller will complete the initialization of
6685    the newly created breakpoint struct as well as output any status
6686    information regarding the creation of a new breakpoint.  In
6687    particular, set_raw_breakpoint does NOT set the breakpoint
6688    number!  Care should be taken to not allow an error to occur
6689    prior to completing the initialization of the breakpoint.  If this
6690    should happen, a bogus breakpoint will be left on the chain.  */
6691
6692 struct breakpoint *
6693 set_raw_breakpoint (struct gdbarch *gdbarch,
6694                     struct symtab_and_line sal, enum bptype bptype,
6695                     const struct breakpoint_ops *ops)
6696 {
6697   struct breakpoint *b = XNEW (struct breakpoint);
6698
6699   init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
6700   add_to_breakpoint_chain (b);
6701   return b;
6702 }
6703
6704
6705 /* Note that the breakpoint object B describes a permanent breakpoint
6706    instruction, hard-wired into the inferior's code.  */
6707 void
6708 make_breakpoint_permanent (struct breakpoint *b)
6709 {
6710   struct bp_location *bl;
6711
6712   b->enable_state = bp_permanent;
6713
6714   /* By definition, permanent breakpoints are already present in the
6715      code.  Mark all locations as inserted.  For now,
6716      make_breakpoint_permanent is called in just one place, so it's
6717      hard to say if it's reasonable to have permanent breakpoint with
6718      multiple locations or not, but it's easy to implement.  */
6719   for (bl = b->loc; bl; bl = bl->next)
6720     bl->inserted = 1;
6721 }
6722
6723 /* Call this routine when stepping and nexting to enable a breakpoint
6724    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
6725    initiated the operation.  */
6726
6727 void
6728 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
6729 {
6730   struct breakpoint *b, *b_tmp;
6731   int thread = tp->num;
6732
6733   /* To avoid having to rescan all objfile symbols at every step,
6734      we maintain a list of continually-inserted but always disabled
6735      longjmp "master" breakpoints.  Here, we simply create momentary
6736      clones of those and enable them for the requested thread.  */
6737   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6738     if (b->pspace == current_program_space
6739         && (b->type == bp_longjmp_master
6740             || b->type == bp_exception_master))
6741       {
6742         enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6743         struct breakpoint *clone;
6744
6745         clone = momentary_breakpoint_from_master (b, type,
6746                                                   &momentary_breakpoint_ops);
6747         clone->thread = thread;
6748       }
6749
6750   tp->initiating_frame = frame;
6751 }
6752
6753 /* Delete all longjmp breakpoints from THREAD.  */
6754 void
6755 delete_longjmp_breakpoint (int thread)
6756 {
6757   struct breakpoint *b, *b_tmp;
6758
6759   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6760     if (b->type == bp_longjmp || b->type == bp_exception)
6761       {
6762         if (b->thread == thread)
6763           delete_breakpoint (b);
6764       }
6765 }
6766
6767 void
6768 delete_longjmp_breakpoint_at_next_stop (int thread)
6769 {
6770   struct breakpoint *b, *b_tmp;
6771
6772   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6773     if (b->type == bp_longjmp || b->type == bp_exception)
6774       {
6775         if (b->thread == thread)
6776           b->disposition = disp_del_at_next_stop;
6777       }
6778 }
6779
6780 void
6781 enable_overlay_breakpoints (void)
6782 {
6783   struct breakpoint *b;
6784
6785   ALL_BREAKPOINTS (b)
6786     if (b->type == bp_overlay_event)
6787     {
6788       b->enable_state = bp_enabled;
6789       update_global_location_list (1);
6790       overlay_events_enabled = 1;
6791     }
6792 }
6793
6794 void
6795 disable_overlay_breakpoints (void)
6796 {
6797   struct breakpoint *b;
6798
6799   ALL_BREAKPOINTS (b)
6800     if (b->type == bp_overlay_event)
6801     {
6802       b->enable_state = bp_disabled;
6803       update_global_location_list (0);
6804       overlay_events_enabled = 0;
6805     }
6806 }
6807
6808 /* Set an active std::terminate breakpoint for each std::terminate
6809    master breakpoint.  */
6810 void
6811 set_std_terminate_breakpoint (void)
6812 {
6813   struct breakpoint *b, *b_tmp;
6814
6815   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6816     if (b->pspace == current_program_space
6817         && b->type == bp_std_terminate_master)
6818       {
6819         momentary_breakpoint_from_master (b, bp_std_terminate,
6820                                           &momentary_breakpoint_ops);
6821       }
6822 }
6823
6824 /* Delete all the std::terminate breakpoints.  */
6825 void
6826 delete_std_terminate_breakpoint (void)
6827 {
6828   struct breakpoint *b, *b_tmp;
6829
6830   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6831     if (b->type == bp_std_terminate)
6832       delete_breakpoint (b);
6833 }
6834
6835 struct breakpoint *
6836 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6837 {
6838   struct breakpoint *b;
6839
6840   b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6841                                   &internal_breakpoint_ops);
6842
6843   b->enable_state = bp_enabled;
6844   /* addr_string has to be used or breakpoint_re_set will delete me.  */
6845   b->addr_string
6846     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
6847
6848   update_global_location_list_nothrow (1);
6849
6850   return b;
6851 }
6852
6853 void
6854 remove_thread_event_breakpoints (void)
6855 {
6856   struct breakpoint *b, *b_tmp;
6857
6858   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6859     if (b->type == bp_thread_event
6860         && b->loc->pspace == current_program_space)
6861       delete_breakpoint (b);
6862 }
6863
6864 struct lang_and_radix
6865   {
6866     enum language lang;
6867     int radix;
6868   };
6869
6870 /* Create a breakpoint for JIT code registration and unregistration.  */
6871
6872 struct breakpoint *
6873 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6874 {
6875   struct breakpoint *b;
6876
6877   b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6878                                   &internal_breakpoint_ops);
6879   update_global_location_list_nothrow (1);
6880   return b;
6881 }
6882
6883 /* Remove JIT code registration and unregistration breakpoint(s).  */
6884
6885 void
6886 remove_jit_event_breakpoints (void)
6887 {
6888   struct breakpoint *b, *b_tmp;
6889
6890   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6891     if (b->type == bp_jit_event
6892         && b->loc->pspace == current_program_space)
6893       delete_breakpoint (b);
6894 }
6895
6896 void
6897 remove_solib_event_breakpoints (void)
6898 {
6899   struct breakpoint *b, *b_tmp;
6900
6901   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6902     if (b->type == bp_shlib_event
6903         && b->loc->pspace == current_program_space)
6904       delete_breakpoint (b);
6905 }
6906
6907 struct breakpoint *
6908 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6909 {
6910   struct breakpoint *b;
6911
6912   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6913                                   &internal_breakpoint_ops);
6914   update_global_location_list_nothrow (1);
6915   return b;
6916 }
6917
6918 /* Disable any breakpoints that are on code in shared libraries.  Only
6919    apply to enabled breakpoints, disabled ones can just stay disabled.  */
6920
6921 void
6922 disable_breakpoints_in_shlibs (void)
6923 {
6924   struct bp_location *loc, **locp_tmp;
6925
6926   ALL_BP_LOCATIONS (loc, locp_tmp)
6927   {
6928     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
6929     struct breakpoint *b = loc->owner;
6930
6931     /* We apply the check to all breakpoints, including disabled for
6932        those with loc->duplicate set.  This is so that when breakpoint
6933        becomes enabled, or the duplicate is removed, gdb will try to
6934        insert all breakpoints.  If we don't set shlib_disabled here,
6935        we'll try to insert those breakpoints and fail.  */
6936     if (((b->type == bp_breakpoint)
6937          || (b->type == bp_jit_event)
6938          || (b->type == bp_hardware_breakpoint)
6939          || (is_tracepoint (b)))
6940         && loc->pspace == current_program_space
6941         && !loc->shlib_disabled
6942 #ifdef PC_SOLIB
6943         && PC_SOLIB (loc->address)
6944 #else
6945         && solib_name_from_address (loc->pspace, loc->address)
6946 #endif
6947         )
6948       {
6949         loc->shlib_disabled = 1;
6950       }
6951   }
6952 }
6953
6954 /* Disable any breakpoints and tracepoints that are in an unloaded shared
6955    library.  Only apply to enabled breakpoints, disabled ones can just stay
6956    disabled.  */
6957
6958 static void
6959 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6960 {
6961   struct bp_location *loc, **locp_tmp;
6962   int disabled_shlib_breaks = 0;
6963
6964   /* SunOS a.out shared libraries are always mapped, so do not
6965      disable breakpoints; they will only be reported as unloaded
6966      through clear_solib when GDB discards its shared library
6967      list.  See clear_solib for more information.  */
6968   if (exec_bfd != NULL
6969       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6970     return;
6971
6972   ALL_BP_LOCATIONS (loc, locp_tmp)
6973   {
6974     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
6975     struct breakpoint *b = loc->owner;
6976
6977     if (solib->pspace == loc->pspace
6978         && !loc->shlib_disabled
6979         && (((b->type == bp_breakpoint
6980               || b->type == bp_jit_event
6981               || b->type == bp_hardware_breakpoint)
6982              && (loc->loc_type == bp_loc_hardware_breakpoint
6983                  || loc->loc_type == bp_loc_software_breakpoint))
6984             || is_tracepoint (b))
6985         && solib_contains_address_p (solib, loc->address))
6986       {
6987         loc->shlib_disabled = 1;
6988         /* At this point, we cannot rely on remove_breakpoint
6989            succeeding so we must mark the breakpoint as not inserted
6990            to prevent future errors occurring in remove_breakpoints.  */
6991         loc->inserted = 0;
6992
6993         /* This may cause duplicate notifications for the same breakpoint.  */
6994         observer_notify_breakpoint_modified (b);
6995
6996         if (!disabled_shlib_breaks)
6997           {
6998             target_terminal_ours_for_output ();
6999             warning (_("Temporarily disabling breakpoints "
7000                        "for unloaded shared library \"%s\""),
7001                      solib->so_name);
7002           }
7003         disabled_shlib_breaks = 1;
7004       }
7005   }
7006 }
7007
7008 /* FORK & VFORK catchpoints.  */
7009
7010 /* An instance of this type is used to represent a fork or vfork
7011    catchpoint.  It includes a "struct breakpoint" as a kind of base
7012    class; users downcast to "struct breakpoint *" when needed.  A
7013    breakpoint is really of this type iff its ops pointer points to
7014    CATCH_FORK_BREAKPOINT_OPS.  */
7015
7016 struct fork_catchpoint
7017 {
7018   /* The base class.  */
7019   struct breakpoint base;
7020
7021   /* Process id of a child process whose forking triggered this
7022      catchpoint.  This field is only valid immediately after this
7023      catchpoint has triggered.  */
7024   ptid_t forked_inferior_pid;
7025 };
7026
7027 /* Implement the "insert" breakpoint_ops method for fork
7028    catchpoints.  */
7029
7030 static int
7031 insert_catch_fork (struct bp_location *bl)
7032 {
7033   return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
7034 }
7035
7036 /* Implement the "remove" breakpoint_ops method for fork
7037    catchpoints.  */
7038
7039 static int
7040 remove_catch_fork (struct bp_location *bl)
7041 {
7042   return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7043 }
7044
7045 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7046    catchpoints.  */
7047
7048 static int
7049 breakpoint_hit_catch_fork (const struct bp_location *bl,
7050                            struct address_space *aspace, CORE_ADDR bp_addr,
7051                            const struct target_waitstatus *ws)
7052 {
7053   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7054
7055   if (ws->kind != TARGET_WAITKIND_FORKED)
7056     return 0;
7057
7058   c->forked_inferior_pid = ws->value.related_pid;
7059   return 1;
7060 }
7061
7062 /* Implement the "print_it" breakpoint_ops method for fork
7063    catchpoints.  */
7064
7065 static enum print_stop_action
7066 print_it_catch_fork (bpstat bs)
7067 {
7068   struct ui_out *uiout = current_uiout;
7069   struct breakpoint *b = bs->breakpoint_at;
7070   struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7071
7072   annotate_catchpoint (b->number);
7073   if (b->disposition == disp_del)
7074     ui_out_text (uiout, "\nTemporary catchpoint ");
7075   else
7076     ui_out_text (uiout, "\nCatchpoint ");
7077   if (ui_out_is_mi_like_p (uiout))
7078     {
7079       ui_out_field_string (uiout, "reason",
7080                            async_reason_lookup (EXEC_ASYNC_FORK));
7081       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7082     }
7083   ui_out_field_int (uiout, "bkptno", b->number);
7084   ui_out_text (uiout, " (forked process ");
7085   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7086   ui_out_text (uiout, "), ");
7087   return PRINT_SRC_AND_LOC;
7088 }
7089
7090 /* Implement the "print_one" breakpoint_ops method for fork
7091    catchpoints.  */
7092
7093 static void
7094 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7095 {
7096   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7097   struct value_print_options opts;
7098   struct ui_out *uiout = current_uiout;
7099
7100   get_user_print_options (&opts);
7101
7102   /* Field 4, the address, is omitted (which makes the columns not
7103      line up too nicely with the headers, but the effect is relatively
7104      readable).  */
7105   if (opts.addressprint)
7106     ui_out_field_skip (uiout, "addr");
7107   annotate_field (5);
7108   ui_out_text (uiout, "fork");
7109   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7110     {
7111       ui_out_text (uiout, ", process ");
7112       ui_out_field_int (uiout, "what",
7113                         ptid_get_pid (c->forked_inferior_pid));
7114       ui_out_spaces (uiout, 1);
7115     }
7116 }
7117
7118 /* Implement the "print_mention" breakpoint_ops method for fork
7119    catchpoints.  */
7120
7121 static void
7122 print_mention_catch_fork (struct breakpoint *b)
7123 {
7124   printf_filtered (_("Catchpoint %d (fork)"), b->number);
7125 }
7126
7127 /* Implement the "print_recreate" breakpoint_ops method for fork
7128    catchpoints.  */
7129
7130 static void
7131 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7132 {
7133   fprintf_unfiltered (fp, "catch fork");
7134   print_recreate_thread (b, fp);
7135 }
7136
7137 /* The breakpoint_ops structure to be used in fork catchpoints.  */
7138
7139 static struct breakpoint_ops catch_fork_breakpoint_ops;
7140
7141 /* Implement the "insert" breakpoint_ops method for vfork
7142    catchpoints.  */
7143
7144 static int
7145 insert_catch_vfork (struct bp_location *bl)
7146 {
7147   return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
7148 }
7149
7150 /* Implement the "remove" breakpoint_ops method for vfork
7151    catchpoints.  */
7152
7153 static int
7154 remove_catch_vfork (struct bp_location *bl)
7155 {
7156   return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7157 }
7158
7159 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7160    catchpoints.  */
7161
7162 static int
7163 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7164                             struct address_space *aspace, CORE_ADDR bp_addr,
7165                             const struct target_waitstatus *ws)
7166 {
7167   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7168
7169   if (ws->kind != TARGET_WAITKIND_VFORKED)
7170     return 0;
7171
7172   c->forked_inferior_pid = ws->value.related_pid;
7173   return 1;
7174 }
7175
7176 /* Implement the "print_it" breakpoint_ops method for vfork
7177    catchpoints.  */
7178
7179 static enum print_stop_action
7180 print_it_catch_vfork (bpstat bs)
7181 {
7182   struct ui_out *uiout = current_uiout;
7183   struct breakpoint *b = bs->breakpoint_at;
7184   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7185
7186   annotate_catchpoint (b->number);
7187   if (b->disposition == disp_del)
7188     ui_out_text (uiout, "\nTemporary catchpoint ");
7189   else
7190     ui_out_text (uiout, "\nCatchpoint ");
7191   if (ui_out_is_mi_like_p (uiout))
7192     {
7193       ui_out_field_string (uiout, "reason",
7194                            async_reason_lookup (EXEC_ASYNC_VFORK));
7195       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7196     }
7197   ui_out_field_int (uiout, "bkptno", b->number);
7198   ui_out_text (uiout, " (vforked process ");
7199   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7200   ui_out_text (uiout, "), ");
7201   return PRINT_SRC_AND_LOC;
7202 }
7203
7204 /* Implement the "print_one" breakpoint_ops method for vfork
7205    catchpoints.  */
7206
7207 static void
7208 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7209 {
7210   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7211   struct value_print_options opts;
7212   struct ui_out *uiout = current_uiout;
7213
7214   get_user_print_options (&opts);
7215   /* Field 4, the address, is omitted (which makes the columns not
7216      line up too nicely with the headers, but the effect is relatively
7217      readable).  */
7218   if (opts.addressprint)
7219     ui_out_field_skip (uiout, "addr");
7220   annotate_field (5);
7221   ui_out_text (uiout, "vfork");
7222   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7223     {
7224       ui_out_text (uiout, ", process ");
7225       ui_out_field_int (uiout, "what",
7226                         ptid_get_pid (c->forked_inferior_pid));
7227       ui_out_spaces (uiout, 1);
7228     }
7229 }
7230
7231 /* Implement the "print_mention" breakpoint_ops method for vfork
7232    catchpoints.  */
7233
7234 static void
7235 print_mention_catch_vfork (struct breakpoint *b)
7236 {
7237   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7238 }
7239
7240 /* Implement the "print_recreate" breakpoint_ops method for vfork
7241    catchpoints.  */
7242
7243 static void
7244 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7245 {
7246   fprintf_unfiltered (fp, "catch vfork");
7247   print_recreate_thread (b, fp);
7248 }
7249
7250 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
7251
7252 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7253
7254 /* An instance of this type is used to represent an solib catchpoint.
7255    It includes a "struct breakpoint" as a kind of base class; users
7256    downcast to "struct breakpoint *" when needed.  A breakpoint is
7257    really of this type iff its ops pointer points to
7258    CATCH_SOLIB_BREAKPOINT_OPS.  */
7259
7260 struct solib_catchpoint
7261 {
7262   /* The base class.  */
7263   struct breakpoint base;
7264
7265   /* True for "catch load", false for "catch unload".  */
7266   unsigned char is_load;
7267
7268   /* Regular expression to match, if any.  COMPILED is only valid when
7269      REGEX is non-NULL.  */
7270   char *regex;
7271   regex_t compiled;
7272 };
7273
7274 static void
7275 dtor_catch_solib (struct breakpoint *b)
7276 {
7277   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7278
7279   if (self->regex)
7280     regfree (&self->compiled);
7281   xfree (self->regex);
7282
7283   base_breakpoint_ops.dtor (b);
7284 }
7285
7286 static int
7287 insert_catch_solib (struct bp_location *ignore)
7288 {
7289   return 0;
7290 }
7291
7292 static int
7293 remove_catch_solib (struct bp_location *ignore)
7294 {
7295   return 0;
7296 }
7297
7298 static int
7299 breakpoint_hit_catch_solib (const struct bp_location *bl,
7300                             struct address_space *aspace,
7301                             CORE_ADDR bp_addr,
7302                             const struct target_waitstatus *ws)
7303 {
7304   struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7305   struct breakpoint *other;
7306
7307   if (ws->kind == TARGET_WAITKIND_LOADED)
7308     return 1;
7309
7310   ALL_BREAKPOINTS (other)
7311   {
7312     struct bp_location *other_bl;
7313
7314     if (other == bl->owner)
7315       continue;
7316
7317     if (other->type != bp_shlib_event)
7318       continue;
7319
7320     if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7321       continue;
7322
7323     for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7324       {
7325         if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7326           return 1;
7327       }
7328   }
7329
7330   return 0;
7331 }
7332
7333 static void
7334 check_status_catch_solib (struct bpstats *bs)
7335 {
7336   struct solib_catchpoint *self
7337     = (struct solib_catchpoint *) bs->breakpoint_at;
7338   int ix;
7339
7340   if (self->is_load)
7341     {
7342       struct so_list *iter;
7343
7344       for (ix = 0;
7345            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7346                         ix, iter);
7347            ++ix)
7348         {
7349           if (!self->regex
7350               || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7351             return;
7352         }
7353     }
7354   else
7355     {
7356       char *iter;
7357
7358       for (ix = 0;
7359            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7360                         ix, iter);
7361            ++ix)
7362         {
7363           if (!self->regex
7364               || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7365             return;
7366         }
7367     }
7368
7369   bs->stop = 0;
7370   bs->print_it = print_it_noop;
7371 }
7372
7373 static enum print_stop_action
7374 print_it_catch_solib (bpstat bs)
7375 {
7376   struct breakpoint *b = bs->breakpoint_at;
7377   struct ui_out *uiout = current_uiout;
7378
7379   annotate_catchpoint (b->number);
7380   if (b->disposition == disp_del)
7381     ui_out_text (uiout, "\nTemporary catchpoint ");
7382   else
7383     ui_out_text (uiout, "\nCatchpoint ");
7384   ui_out_field_int (uiout, "bkptno", b->number);
7385   ui_out_text (uiout, "\n");
7386   if (ui_out_is_mi_like_p (uiout))
7387     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7388   print_solib_event (1);
7389   return PRINT_SRC_AND_LOC;
7390 }
7391
7392 static void
7393 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7394 {
7395   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7396   struct value_print_options opts;
7397   struct ui_out *uiout = current_uiout;
7398   char *msg;
7399
7400   get_user_print_options (&opts);
7401   /* Field 4, the address, is omitted (which makes the columns not
7402      line up too nicely with the headers, but the effect is relatively
7403      readable).  */
7404   if (opts.addressprint)
7405     {
7406       annotate_field (4);
7407       ui_out_field_skip (uiout, "addr");
7408     }
7409
7410   annotate_field (5);
7411   if (self->is_load)
7412     {
7413       if (self->regex)
7414         msg = xstrprintf (_("load of library matching %s"), self->regex);
7415       else
7416         msg = xstrdup (_("load of library"));
7417     }
7418   else
7419     {
7420       if (self->regex)
7421         msg = xstrprintf (_("unload of library matching %s"), self->regex);
7422       else
7423         msg = xstrdup (_("unload of library"));
7424     }
7425   ui_out_field_string (uiout, "what", msg);
7426   xfree (msg);
7427 }
7428
7429 static void
7430 print_mention_catch_solib (struct breakpoint *b)
7431 {
7432   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7433
7434   printf_filtered (_("Catchpoint %d (%s)"), b->number,
7435                    self->is_load ? "load" : "unload");
7436 }
7437
7438 static void
7439 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7440 {
7441   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7442
7443   fprintf_unfiltered (fp, "%s %s",
7444                       b->disposition == disp_del ? "tcatch" : "catch",
7445                       self->is_load ? "load" : "unload");
7446   if (self->regex)
7447     fprintf_unfiltered (fp, " %s", self->regex);
7448   fprintf_unfiltered (fp, "\n");
7449 }
7450
7451 static struct breakpoint_ops catch_solib_breakpoint_ops;
7452
7453 /* A helper function that does all the work for "catch load" and
7454    "catch unload".  */
7455
7456 static void
7457 catch_load_or_unload (char *arg, int from_tty, int is_load,
7458                       struct cmd_list_element *command)
7459 {
7460   struct solib_catchpoint *c;
7461   struct gdbarch *gdbarch = get_current_arch ();
7462   int tempflag;
7463   regex_t compiled;
7464   struct cleanup *cleanup;
7465
7466   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7467
7468   if (!arg)
7469     arg = "";
7470   arg = skip_spaces (arg);
7471
7472   c = XCNEW (struct solib_catchpoint);
7473   cleanup = make_cleanup (xfree, c);
7474
7475   if (*arg != '\0')
7476     {
7477       int errcode;
7478
7479       errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7480       if (errcode != 0)
7481         {
7482           char *err = get_regcomp_error (errcode, &c->compiled);
7483
7484           make_cleanup (xfree, err);
7485           error (_("Invalid regexp (%s): %s"), err, arg);
7486         }
7487       c->regex = xstrdup (arg);
7488     }
7489
7490   c->is_load = is_load;
7491   init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7492                    &catch_solib_breakpoint_ops);
7493
7494   discard_cleanups (cleanup);
7495   install_breakpoint (0, &c->base, 1);
7496 }
7497
7498 static void
7499 catch_load_command_1 (char *arg, int from_tty,
7500                       struct cmd_list_element *command)
7501 {
7502   catch_load_or_unload (arg, from_tty, 1, command);
7503 }
7504
7505 static void
7506 catch_unload_command_1 (char *arg, int from_tty,
7507                         struct cmd_list_element *command)
7508 {
7509   catch_load_or_unload (arg, from_tty, 0, command);
7510 }
7511
7512 DEF_VEC_I(int);
7513
7514 /* An instance of this type is used to represent a syscall catchpoint.
7515    It includes a "struct breakpoint" as a kind of base class; users
7516    downcast to "struct breakpoint *" when needed.  A breakpoint is
7517    really of this type iff its ops pointer points to
7518    CATCH_SYSCALL_BREAKPOINT_OPS.  */
7519
7520 struct syscall_catchpoint
7521 {
7522   /* The base class.  */
7523   struct breakpoint base;
7524
7525   /* Syscall numbers used for the 'catch syscall' feature.  If no
7526      syscall has been specified for filtering, its value is NULL.
7527      Otherwise, it holds a list of all syscalls to be caught.  The
7528      list elements are allocated with xmalloc.  */
7529   VEC(int) *syscalls_to_be_caught;
7530 };
7531
7532 /* Implement the "dtor" breakpoint_ops method for syscall
7533    catchpoints.  */
7534
7535 static void
7536 dtor_catch_syscall (struct breakpoint *b)
7537 {
7538   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7539
7540   VEC_free (int, c->syscalls_to_be_caught);
7541
7542   base_breakpoint_ops.dtor (b);
7543 }
7544
7545 static const struct inferior_data *catch_syscall_inferior_data = NULL;
7546
7547 struct catch_syscall_inferior_data
7548 {
7549   /* We keep a count of the number of times the user has requested a
7550      particular syscall to be tracked, and pass this information to the
7551      target.  This lets capable targets implement filtering directly.  */
7552
7553   /* Number of times that "any" syscall is requested.  */
7554   int any_syscall_count;
7555
7556   /* Count of each system call.  */
7557   VEC(int) *syscalls_counts;
7558
7559   /* This counts all syscall catch requests, so we can readily determine
7560      if any catching is necessary.  */
7561   int total_syscalls_count;
7562 };
7563
7564 static struct catch_syscall_inferior_data*
7565 get_catch_syscall_inferior_data (struct inferior *inf)
7566 {
7567   struct catch_syscall_inferior_data *inf_data;
7568
7569   inf_data = inferior_data (inf, catch_syscall_inferior_data);
7570   if (inf_data == NULL)
7571     {
7572       inf_data = XZALLOC (struct catch_syscall_inferior_data);
7573       set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7574     }
7575
7576   return inf_data;
7577 }
7578
7579 static void
7580 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7581 {
7582   xfree (arg);
7583 }
7584
7585
7586 /* Implement the "insert" breakpoint_ops method for syscall
7587    catchpoints.  */
7588
7589 static int
7590 insert_catch_syscall (struct bp_location *bl)
7591 {
7592   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
7593   struct inferior *inf = current_inferior ();
7594   struct catch_syscall_inferior_data *inf_data
7595     = get_catch_syscall_inferior_data (inf);
7596
7597   ++inf_data->total_syscalls_count;
7598   if (!c->syscalls_to_be_caught)
7599     ++inf_data->any_syscall_count;
7600   else
7601     {
7602       int i, iter;
7603
7604       for (i = 0;
7605            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7606            i++)
7607         {
7608           int elem;
7609
7610           if (iter >= VEC_length (int, inf_data->syscalls_counts))
7611             {
7612               int old_size = VEC_length (int, inf_data->syscalls_counts);
7613               uintptr_t vec_addr_offset
7614                 = old_size * ((uintptr_t) sizeof (int));
7615               uintptr_t vec_addr;
7616               VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7617               vec_addr = ((uintptr_t) VEC_address (int,
7618                                                   inf_data->syscalls_counts)
7619                           + vec_addr_offset);
7620               memset ((void *) vec_addr, 0,
7621                       (iter + 1 - old_size) * sizeof (int));
7622             }
7623           elem = VEC_index (int, inf_data->syscalls_counts, iter);
7624           VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
7625         }
7626     }
7627
7628   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
7629                                         inf_data->total_syscalls_count != 0,
7630                                         inf_data->any_syscall_count,
7631                                         VEC_length (int,
7632                                                     inf_data->syscalls_counts),
7633                                         VEC_address (int,
7634                                                      inf_data->syscalls_counts));
7635 }
7636
7637 /* Implement the "remove" breakpoint_ops method for syscall
7638    catchpoints.  */
7639
7640 static int
7641 remove_catch_syscall (struct bp_location *bl)
7642 {
7643   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
7644   struct inferior *inf = current_inferior ();
7645   struct catch_syscall_inferior_data *inf_data
7646     = get_catch_syscall_inferior_data (inf);
7647
7648   --inf_data->total_syscalls_count;
7649   if (!c->syscalls_to_be_caught)
7650     --inf_data->any_syscall_count;
7651   else
7652     {
7653       int i, iter;
7654
7655       for (i = 0;
7656            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7657            i++)
7658         {
7659           int elem;
7660           if (iter >= VEC_length (int, inf_data->syscalls_counts))
7661             /* Shouldn't happen.  */
7662             continue;
7663           elem = VEC_index (int, inf_data->syscalls_counts, iter);
7664           VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
7665         }
7666     }
7667
7668   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
7669                                         inf_data->total_syscalls_count != 0,
7670                                         inf_data->any_syscall_count,
7671                                         VEC_length (int,
7672                                                     inf_data->syscalls_counts),
7673                                         VEC_address (int,
7674                                                      inf_data->syscalls_counts));
7675 }
7676
7677 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7678    catchpoints.  */
7679
7680 static int
7681 breakpoint_hit_catch_syscall (const struct bp_location *bl,
7682                               struct address_space *aspace, CORE_ADDR bp_addr,
7683                               const struct target_waitstatus *ws)
7684 {
7685   /* We must check if we are catching specific syscalls in this
7686      breakpoint.  If we are, then we must guarantee that the called
7687      syscall is the same syscall we are catching.  */
7688   int syscall_number = 0;
7689   const struct syscall_catchpoint *c
7690     = (const struct syscall_catchpoint *) bl->owner;
7691
7692   if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7693       && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
7694     return 0;
7695
7696   syscall_number = ws->value.syscall_number;
7697
7698   /* Now, checking if the syscall is the same.  */
7699   if (c->syscalls_to_be_caught)
7700     {
7701       int i, iter;
7702
7703       for (i = 0;
7704            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7705            i++)
7706         if (syscall_number == iter)
7707           break;
7708       /* Not the same.  */
7709       if (!iter)
7710         return 0;
7711     }
7712
7713   return 1;
7714 }
7715
7716 /* Implement the "print_it" breakpoint_ops method for syscall
7717    catchpoints.  */
7718
7719 static enum print_stop_action
7720 print_it_catch_syscall (bpstat bs)
7721 {
7722   struct ui_out *uiout = current_uiout;
7723   struct breakpoint *b = bs->breakpoint_at;
7724   /* These are needed because we want to know in which state a
7725      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7726      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7727      must print "called syscall" or "returned from syscall".  */
7728   ptid_t ptid;
7729   struct target_waitstatus last;
7730   struct syscall s;
7731   char *syscall_id;
7732
7733   get_last_target_status (&ptid, &last);
7734
7735   get_syscall_by_number (last.value.syscall_number, &s);
7736
7737   annotate_catchpoint (b->number);
7738
7739   if (b->disposition == disp_del)
7740     ui_out_text (uiout, "\nTemporary catchpoint ");
7741   else
7742     ui_out_text (uiout, "\nCatchpoint ");
7743   if (ui_out_is_mi_like_p (uiout))
7744     {
7745       ui_out_field_string (uiout, "reason",
7746                            async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7747                                                 ? EXEC_ASYNC_SYSCALL_ENTRY
7748                                                 : EXEC_ASYNC_SYSCALL_RETURN));
7749       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7750     }
7751   ui_out_field_int (uiout, "bkptno", b->number);
7752
7753   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
7754     ui_out_text (uiout, " (call to syscall ");
7755   else
7756     ui_out_text (uiout, " (returned from syscall ");
7757
7758   if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7759     ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7760   if (s.name != NULL)
7761     ui_out_field_string (uiout, "syscall-name", s.name);
7762
7763   ui_out_text (uiout, "), ");
7764
7765   return PRINT_SRC_AND_LOC;
7766 }
7767
7768 /* Implement the "print_one" breakpoint_ops method for syscall
7769    catchpoints.  */
7770
7771 static void
7772 print_one_catch_syscall (struct breakpoint *b,
7773                          struct bp_location **last_loc)
7774 {
7775   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7776   struct value_print_options opts;
7777   struct ui_out *uiout = current_uiout;
7778
7779   get_user_print_options (&opts);
7780   /* Field 4, the address, is omitted (which makes the columns not
7781      line up too nicely with the headers, but the effect is relatively
7782      readable).  */
7783   if (opts.addressprint)
7784     ui_out_field_skip (uiout, "addr");
7785   annotate_field (5);
7786
7787   if (c->syscalls_to_be_caught
7788       && VEC_length (int, c->syscalls_to_be_caught) > 1)
7789     ui_out_text (uiout, "syscalls \"");
7790   else
7791     ui_out_text (uiout, "syscall \"");
7792
7793   if (c->syscalls_to_be_caught)
7794     {
7795       int i, iter;
7796       char *text = xstrprintf ("%s", "");
7797
7798       for (i = 0;
7799            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7800            i++)
7801         {
7802           char *x = text;
7803           struct syscall s;
7804           get_syscall_by_number (iter, &s);
7805
7806           if (s.name != NULL)
7807             text = xstrprintf ("%s%s, ", text, s.name);
7808           else
7809             text = xstrprintf ("%s%d, ", text, iter);
7810
7811           /* We have to xfree the last 'text' (now stored at 'x')
7812              because xstrprintf dynamically allocates new space for it
7813              on every call.  */
7814           xfree (x);
7815         }
7816       /* Remove the last comma.  */
7817       text[strlen (text) - 2] = '\0';
7818       ui_out_field_string (uiout, "what", text);
7819     }
7820   else
7821     ui_out_field_string (uiout, "what", "<any syscall>");
7822   ui_out_text (uiout, "\" ");
7823 }
7824
7825 /* Implement the "print_mention" breakpoint_ops method for syscall
7826    catchpoints.  */
7827
7828 static void
7829 print_mention_catch_syscall (struct breakpoint *b)
7830 {
7831   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7832
7833   if (c->syscalls_to_be_caught)
7834     {
7835       int i, iter;
7836
7837       if (VEC_length (int, c->syscalls_to_be_caught) > 1)
7838         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7839       else
7840         printf_filtered (_("Catchpoint %d (syscall"), b->number);
7841
7842       for (i = 0;
7843            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7844            i++)
7845         {
7846           struct syscall s;
7847           get_syscall_by_number (iter, &s);
7848
7849           if (s.name)
7850             printf_filtered (" '%s' [%d]", s.name, s.number);
7851           else
7852             printf_filtered (" %d", s.number);
7853         }
7854       printf_filtered (")");
7855     }
7856   else
7857     printf_filtered (_("Catchpoint %d (any syscall)"),
7858                      b->number);
7859 }
7860
7861 /* Implement the "print_recreate" breakpoint_ops method for syscall
7862    catchpoints.  */
7863
7864 static void
7865 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
7866 {
7867   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7868
7869   fprintf_unfiltered (fp, "catch syscall");
7870
7871   if (c->syscalls_to_be_caught)
7872     {
7873       int i, iter;
7874
7875       for (i = 0;
7876            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7877            i++)
7878         {
7879           struct syscall s;
7880
7881           get_syscall_by_number (iter, &s);
7882           if (s.name)
7883             fprintf_unfiltered (fp, " %s", s.name);
7884           else
7885             fprintf_unfiltered (fp, " %d", s.number);
7886         }
7887     }
7888   print_recreate_thread (b, fp);
7889 }
7890
7891 /* The breakpoint_ops structure to be used in syscall catchpoints.  */
7892
7893 static struct breakpoint_ops catch_syscall_breakpoint_ops;
7894
7895 /* Returns non-zero if 'b' is a syscall catchpoint.  */
7896
7897 static int
7898 syscall_catchpoint_p (struct breakpoint *b)
7899 {
7900   return (b->ops == &catch_syscall_breakpoint_ops);
7901 }
7902
7903 /* Initialize a new breakpoint of the bp_catchpoint kind.  If TEMPFLAG
7904    is non-zero, then make the breakpoint temporary.  If COND_STRING is
7905    not NULL, then store it in the breakpoint.  OPS, if not NULL, is
7906    the breakpoint_ops structure associated to the catchpoint.  */
7907
7908 static void
7909 init_catchpoint (struct breakpoint *b,
7910                  struct gdbarch *gdbarch, int tempflag,
7911                  char *cond_string,
7912                  const struct breakpoint_ops *ops)
7913 {
7914   struct symtab_and_line sal;
7915
7916   init_sal (&sal);
7917   sal.pspace = current_program_space;
7918
7919   init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
7920
7921   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
7922   b->disposition = tempflag ? disp_del : disp_donttouch;
7923 }
7924
7925 void
7926 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
7927 {
7928   add_to_breakpoint_chain (b);
7929   set_breakpoint_number (internal, b);
7930   if (!internal)
7931     mention (b);
7932   observer_notify_breakpoint_created (b);
7933
7934   if (update_gll)
7935     update_global_location_list (1);
7936 }
7937
7938 static void
7939 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
7940                                     int tempflag, char *cond_string,
7941                                     const struct breakpoint_ops *ops)
7942 {
7943   struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
7944
7945   init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
7946
7947   c->forked_inferior_pid = null_ptid;
7948
7949   install_breakpoint (0, &c->base, 1);
7950 }
7951
7952 /* Exec catchpoints.  */
7953
7954 /* An instance of this type is used to represent an exec catchpoint.
7955    It includes a "struct breakpoint" as a kind of base class; users
7956    downcast to "struct breakpoint *" when needed.  A breakpoint is
7957    really of this type iff its ops pointer points to
7958    CATCH_EXEC_BREAKPOINT_OPS.  */
7959
7960 struct exec_catchpoint
7961 {
7962   /* The base class.  */
7963   struct breakpoint base;
7964
7965   /* Filename of a program whose exec triggered this catchpoint.
7966      This field is only valid immediately after this catchpoint has
7967      triggered.  */
7968   char *exec_pathname;
7969 };
7970
7971 /* Implement the "dtor" breakpoint_ops method for exec
7972    catchpoints.  */
7973
7974 static void
7975 dtor_catch_exec (struct breakpoint *b)
7976 {
7977   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
7978
7979   xfree (c->exec_pathname);
7980
7981   base_breakpoint_ops.dtor (b);
7982 }
7983
7984 static int
7985 insert_catch_exec (struct bp_location *bl)
7986 {
7987   return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
7988 }
7989
7990 static int
7991 remove_catch_exec (struct bp_location *bl)
7992 {
7993   return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
7994 }
7995
7996 static int
7997 breakpoint_hit_catch_exec (const struct bp_location *bl,
7998                            struct address_space *aspace, CORE_ADDR bp_addr,
7999                            const struct target_waitstatus *ws)
8000 {
8001   struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8002
8003   if (ws->kind != TARGET_WAITKIND_EXECD)
8004     return 0;
8005
8006   c->exec_pathname = xstrdup (ws->value.execd_pathname);
8007   return 1;
8008 }
8009
8010 static enum print_stop_action
8011 print_it_catch_exec (bpstat bs)
8012 {
8013   struct ui_out *uiout = current_uiout;
8014   struct breakpoint *b = bs->breakpoint_at;
8015   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8016
8017   annotate_catchpoint (b->number);
8018   if (b->disposition == disp_del)
8019     ui_out_text (uiout, "\nTemporary catchpoint ");
8020   else
8021     ui_out_text (uiout, "\nCatchpoint ");
8022   if (ui_out_is_mi_like_p (uiout))
8023     {
8024       ui_out_field_string (uiout, "reason",
8025                            async_reason_lookup (EXEC_ASYNC_EXEC));
8026       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8027     }
8028   ui_out_field_int (uiout, "bkptno", b->number);
8029   ui_out_text (uiout, " (exec'd ");
8030   ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8031   ui_out_text (uiout, "), ");
8032
8033   return PRINT_SRC_AND_LOC;
8034 }
8035
8036 static void
8037 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8038 {
8039   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8040   struct value_print_options opts;
8041   struct ui_out *uiout = current_uiout;
8042
8043   get_user_print_options (&opts);
8044
8045   /* Field 4, the address, is omitted (which makes the columns
8046      not line up too nicely with the headers, but the effect
8047      is relatively readable).  */
8048   if (opts.addressprint)
8049     ui_out_field_skip (uiout, "addr");
8050   annotate_field (5);
8051   ui_out_text (uiout, "exec");
8052   if (c->exec_pathname != NULL)
8053     {
8054       ui_out_text (uiout, ", program \"");
8055       ui_out_field_string (uiout, "what", c->exec_pathname);
8056       ui_out_text (uiout, "\" ");
8057     }
8058 }
8059
8060 static void
8061 print_mention_catch_exec (struct breakpoint *b)
8062 {
8063   printf_filtered (_("Catchpoint %d (exec)"), b->number);
8064 }
8065
8066 /* Implement the "print_recreate" breakpoint_ops method for exec
8067    catchpoints.  */
8068
8069 static void
8070 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8071 {
8072   fprintf_unfiltered (fp, "catch exec");
8073   print_recreate_thread (b, fp);
8074 }
8075
8076 static struct breakpoint_ops catch_exec_breakpoint_ops;
8077
8078 static void
8079 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8080                                  const struct breakpoint_ops *ops)
8081 {
8082   struct syscall_catchpoint *c;
8083   struct gdbarch *gdbarch = get_current_arch ();
8084
8085   c = XNEW (struct syscall_catchpoint);
8086   init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8087   c->syscalls_to_be_caught = filter;
8088
8089   install_breakpoint (0, &c->base, 1);
8090 }
8091
8092 static int
8093 hw_breakpoint_used_count (void)
8094 {
8095   int i = 0;
8096   struct breakpoint *b;
8097   struct bp_location *bl;
8098
8099   ALL_BREAKPOINTS (b)
8100   {
8101     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8102       for (bl = b->loc; bl; bl = bl->next)
8103         {
8104           /* Special types of hardware breakpoints may use more than
8105              one register.  */
8106           i += b->ops->resources_needed (bl);
8107         }
8108   }
8109
8110   return i;
8111 }
8112
8113 /* Returns the resources B would use if it were a hardware
8114    watchpoint.  */
8115
8116 static int
8117 hw_watchpoint_use_count (struct breakpoint *b)
8118 {
8119   int i = 0;
8120   struct bp_location *bl;
8121
8122   if (!breakpoint_enabled (b))
8123     return 0;
8124
8125   for (bl = b->loc; bl; bl = bl->next)
8126     {
8127       /* Special types of hardware watchpoints may use more than
8128          one register.  */
8129       i += b->ops->resources_needed (bl);
8130     }
8131
8132   return i;
8133 }
8134
8135 /* Returns the sum the used resources of all hardware watchpoints of
8136    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
8137    the sum of the used resources of all hardware watchpoints of other
8138    types _not_ TYPE.  */
8139
8140 static int
8141 hw_watchpoint_used_count_others (struct breakpoint *except,
8142                                  enum bptype type, int *other_type_used)
8143 {
8144   int i = 0;
8145   struct breakpoint *b;
8146
8147   *other_type_used = 0;
8148   ALL_BREAKPOINTS (b)
8149     {
8150       if (b == except)
8151         continue;
8152       if (!breakpoint_enabled (b))
8153         continue;
8154
8155       if (b->type == type)
8156         i += hw_watchpoint_use_count (b);
8157       else if (is_hardware_watchpoint (b))
8158         *other_type_used = 1;
8159     }
8160
8161   return i;
8162 }
8163
8164 void
8165 disable_watchpoints_before_interactive_call_start (void)
8166 {
8167   struct breakpoint *b;
8168
8169   ALL_BREAKPOINTS (b)
8170   {
8171     if (is_watchpoint (b) && breakpoint_enabled (b))
8172       {
8173         b->enable_state = bp_call_disabled;
8174         update_global_location_list (0);
8175       }
8176   }
8177 }
8178
8179 void
8180 enable_watchpoints_after_interactive_call_stop (void)
8181 {
8182   struct breakpoint *b;
8183
8184   ALL_BREAKPOINTS (b)
8185   {
8186     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8187       {
8188         b->enable_state = bp_enabled;
8189         update_global_location_list (1);
8190       }
8191   }
8192 }
8193
8194 void
8195 disable_breakpoints_before_startup (void)
8196 {
8197   current_program_space->executing_startup = 1;
8198   update_global_location_list (0);
8199 }
8200
8201 void
8202 enable_breakpoints_after_startup (void)
8203 {
8204   current_program_space->executing_startup = 0;
8205   breakpoint_re_set ();
8206 }
8207
8208
8209 /* Set a breakpoint that will evaporate an end of command
8210    at address specified by SAL.
8211    Restrict it to frame FRAME if FRAME is nonzero.  */
8212
8213 struct breakpoint *
8214 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8215                           struct frame_id frame_id, enum bptype type)
8216 {
8217   struct breakpoint *b;
8218
8219   /* If FRAME_ID is valid, it should be a real frame, not an inlined
8220      one.  */
8221   gdb_assert (!frame_id_inlined_p (frame_id));
8222
8223   b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8224   b->enable_state = bp_enabled;
8225   b->disposition = disp_donttouch;
8226   b->frame_id = frame_id;
8227
8228   /* If we're debugging a multi-threaded program, then we want
8229      momentary breakpoints to be active in only a single thread of
8230      control.  */
8231   if (in_thread_list (inferior_ptid))
8232     b->thread = pid_to_thread_id (inferior_ptid);
8233
8234   update_global_location_list_nothrow (1);
8235
8236   return b;
8237 }
8238
8239 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8240    The new breakpoint will have type TYPE, and use OPS as it
8241    breakpoint_ops.  */
8242
8243 static struct breakpoint *
8244 momentary_breakpoint_from_master (struct breakpoint *orig,
8245                                   enum bptype type,
8246                                   const struct breakpoint_ops *ops)
8247 {
8248   struct breakpoint *copy;
8249
8250   copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8251   copy->loc = allocate_bp_location (copy);
8252   set_breakpoint_location_function (copy->loc, 1);
8253
8254   copy->loc->gdbarch = orig->loc->gdbarch;
8255   copy->loc->requested_address = orig->loc->requested_address;
8256   copy->loc->address = orig->loc->address;
8257   copy->loc->section = orig->loc->section;
8258   copy->loc->pspace = orig->loc->pspace;
8259   copy->loc->probe = orig->loc->probe;
8260
8261   if (orig->loc->source_file != NULL)
8262     copy->loc->source_file = xstrdup (orig->loc->source_file);
8263
8264   copy->loc->line_number = orig->loc->line_number;
8265   copy->frame_id = orig->frame_id;
8266   copy->thread = orig->thread;
8267   copy->pspace = orig->pspace;
8268
8269   copy->enable_state = bp_enabled;
8270   copy->disposition = disp_donttouch;
8271   copy->number = internal_breakpoint_number--;
8272
8273   update_global_location_list_nothrow (0);
8274   return copy;
8275 }
8276
8277 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
8278    ORIG is NULL.  */
8279
8280 struct breakpoint *
8281 clone_momentary_breakpoint (struct breakpoint *orig)
8282 {
8283   /* If there's nothing to clone, then return nothing.  */
8284   if (orig == NULL)
8285     return NULL;
8286
8287   return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8288 }
8289
8290 struct breakpoint *
8291 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8292                                 enum bptype type)
8293 {
8294   struct symtab_and_line sal;
8295
8296   sal = find_pc_line (pc, 0);
8297   sal.pc = pc;
8298   sal.section = find_pc_overlay (pc);
8299   sal.explicit_pc = 1;
8300
8301   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8302 }
8303 \f
8304
8305 /* Tell the user we have just set a breakpoint B.  */
8306
8307 static void
8308 mention (struct breakpoint *b)
8309 {
8310   b->ops->print_mention (b);
8311   if (ui_out_is_mi_like_p (current_uiout))
8312     return;
8313   printf_filtered ("\n");
8314 }
8315 \f
8316
8317 static struct bp_location *
8318 add_location_to_breakpoint (struct breakpoint *b,
8319                             const struct symtab_and_line *sal)
8320 {
8321   struct bp_location *loc, **tmp;
8322   CORE_ADDR adjusted_address;
8323   struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8324
8325   if (loc_gdbarch == NULL)
8326     loc_gdbarch = b->gdbarch;
8327
8328   /* Adjust the breakpoint's address prior to allocating a location.
8329      Once we call allocate_bp_location(), that mostly uninitialized
8330      location will be placed on the location chain.  Adjustment of the
8331      breakpoint may cause target_read_memory() to be called and we do
8332      not want its scan of the location chain to find a breakpoint and
8333      location that's only been partially initialized.  */
8334   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8335                                                 sal->pc, b->type);
8336
8337   loc = allocate_bp_location (b);
8338   for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8339     ;
8340   *tmp = loc;
8341
8342   loc->requested_address = sal->pc;
8343   loc->address = adjusted_address;
8344   loc->pspace = sal->pspace;
8345   loc->probe = sal->probe;
8346   gdb_assert (loc->pspace != NULL);
8347   loc->section = sal->section;
8348   loc->gdbarch = loc_gdbarch;
8349
8350   if (sal->symtab != NULL)
8351     loc->source_file = xstrdup (sal->symtab->filename);
8352   loc->line_number = sal->line;
8353
8354   set_breakpoint_location_function (loc,
8355                                     sal->explicit_pc || sal->explicit_line);
8356   return loc;
8357 }
8358 \f
8359
8360 /* Return 1 if LOC is pointing to a permanent breakpoint, 
8361    return 0 otherwise.  */
8362
8363 static int
8364 bp_loc_is_permanent (struct bp_location *loc)
8365 {
8366   int len;
8367   CORE_ADDR addr;
8368   const gdb_byte *bpoint;
8369   gdb_byte *target_mem;
8370   struct cleanup *cleanup;
8371   int retval = 0;
8372
8373   gdb_assert (loc != NULL);
8374
8375   addr = loc->address;
8376   bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
8377
8378   /* Software breakpoints unsupported?  */
8379   if (bpoint == NULL)
8380     return 0;
8381
8382   target_mem = alloca (len);
8383
8384   /* Enable the automatic memory restoration from breakpoints while
8385      we read the memory.  Otherwise we could say about our temporary
8386      breakpoints they are permanent.  */
8387   cleanup = save_current_space_and_thread ();
8388
8389   switch_to_program_space_and_thread (loc->pspace);
8390   make_show_memory_breakpoints_cleanup (0);
8391
8392   if (target_read_memory (loc->address, target_mem, len) == 0
8393       && memcmp (target_mem, bpoint, len) == 0)
8394     retval = 1;
8395
8396   do_cleanups (cleanup);
8397
8398   return retval;
8399 }
8400
8401 /* The style in which to perform a dynamic printf.  This is a user
8402    option because different output options have different tradeoffs;
8403    if GDB does the printing, there is better error handling if there
8404    is a problem with any of the arguments, but using an inferior
8405    function lets you have special-purpose printers and sending of
8406    output to the same place as compiled-in print functions.  (Future
8407    styles may include the ability to do a target-side printf.)  */
8408
8409 static const char dprintf_style_gdb[] = "gdb";
8410 static const char dprintf_style_call[] = "call";
8411 static const char *const dprintf_style_enums[] = {
8412   dprintf_style_gdb,
8413   dprintf_style_call,
8414   NULL
8415 };
8416 static const char *dprintf_style = dprintf_style_gdb;
8417
8418 /* The function to use for dynamic printf if the preferred style is to
8419    call into the inferior.  The value is simply a string that is
8420    copied into the command, so it can be anything that GDB can
8421    evaluate to a callable address, not necessarily a function name.  */
8422
8423 static char *dprintf_function = "";
8424
8425 /* The channel to use for dynamic printf if the preferred style is to
8426    call into the inferior; if a nonempty string, it will be passed to
8427    the call as the first argument, with the format string as the
8428    second.  As with the dprintf function, this can be anything that
8429    GDB knows how to evaluate, so in addition to common choices like
8430    "stderr", this could be an app-specific expression like
8431    "mystreams[curlogger]".  */
8432
8433 static char *dprintf_channel = "";
8434
8435 /* Build a command list for the dprintf corresponding to the current
8436    settings of the dprintf style options.  */
8437
8438 static void
8439 update_dprintf_command_list (struct breakpoint *b)
8440 {
8441   char *dprintf_args = b->extra_string;
8442   char *printf_line = NULL;
8443
8444   if (!dprintf_args)
8445     return;
8446
8447   dprintf_args = skip_spaces (dprintf_args);
8448
8449   /* Allow a comma, as it may have terminated a location, but don't
8450      insist on it.  */
8451   if (*dprintf_args == ',')
8452     ++dprintf_args;
8453   dprintf_args = skip_spaces (dprintf_args);
8454
8455   if (*dprintf_args != '"')
8456     error (_("Bad format string, missing '\"'."));
8457
8458   if (strcmp (dprintf_style, "gdb") == 0)
8459     printf_line = xstrprintf ("printf %s", dprintf_args);
8460   else if (strcmp (dprintf_style, "call") == 0)
8461     {
8462       if (!dprintf_function)
8463         error (_("No function supplied for dprintf call"));
8464
8465       if (dprintf_channel && strlen (dprintf_channel) > 0)
8466         printf_line = xstrprintf ("call (void) %s (%s,%s)",
8467                                   dprintf_function,
8468                                   dprintf_channel,
8469                                   dprintf_args);
8470       else
8471         printf_line = xstrprintf ("call (void) %s (%s)",
8472                                   dprintf_function,
8473                                   dprintf_args);
8474     }
8475   else
8476     internal_error (__FILE__, __LINE__,
8477                     _("Invalid dprintf style."));
8478
8479   /* Manufacture a printf/continue sequence.  */
8480   if (printf_line)
8481     {
8482       struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8483
8484       cont_cmd_line = xmalloc (sizeof (struct command_line));
8485       cont_cmd_line->control_type = simple_control;
8486       cont_cmd_line->body_count = 0;
8487       cont_cmd_line->body_list = NULL;
8488       cont_cmd_line->next = NULL;
8489       cont_cmd_line->line = xstrdup ("continue");
8490
8491       printf_cmd_line = xmalloc (sizeof (struct command_line));
8492       printf_cmd_line->control_type = simple_control;
8493       printf_cmd_line->body_count = 0;
8494       printf_cmd_line->body_list = NULL;
8495       printf_cmd_line->next = cont_cmd_line;
8496       printf_cmd_line->line = printf_line;
8497
8498       breakpoint_set_commands (b, printf_cmd_line);
8499     }
8500 }
8501
8502 /* Update all dprintf commands, making their command lists reflect
8503    current style settings.  */
8504
8505 static void
8506 update_dprintf_commands (char *args, int from_tty,
8507                          struct cmd_list_element *c)
8508 {
8509   struct breakpoint *b;
8510
8511   ALL_BREAKPOINTS (b)
8512     {
8513       if (b->type == bp_dprintf)
8514         update_dprintf_command_list (b);
8515     }
8516 }
8517
8518 /* Create a breakpoint with SAL as location.  Use ADDR_STRING
8519    as textual description of the location, and COND_STRING
8520    as condition expression.  */
8521
8522 static void
8523 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8524                      struct symtabs_and_lines sals, char *addr_string,
8525                      char *filter, char *cond_string,
8526                      char *extra_string,
8527                      enum bptype type, enum bpdisp disposition,
8528                      int thread, int task, int ignore_count,
8529                      const struct breakpoint_ops *ops, int from_tty,
8530                      int enabled, int internal, unsigned flags,
8531                      int display_canonical)
8532 {
8533   int i;
8534
8535   if (type == bp_hardware_breakpoint)
8536     {
8537       int target_resources_ok;
8538
8539       i = hw_breakpoint_used_count ();
8540       target_resources_ok =
8541         target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8542                                             i + 1, 0);
8543       if (target_resources_ok == 0)
8544         error (_("No hardware breakpoint support in the target."));
8545       else if (target_resources_ok < 0)
8546         error (_("Hardware breakpoints used exceeds limit."));
8547     }
8548
8549   gdb_assert (sals.nelts > 0);
8550
8551   for (i = 0; i < sals.nelts; ++i)
8552     {
8553       struct symtab_and_line sal = sals.sals[i];
8554       struct bp_location *loc;
8555
8556       if (from_tty)
8557         {
8558           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8559           if (!loc_gdbarch)
8560             loc_gdbarch = gdbarch;
8561
8562           describe_other_breakpoints (loc_gdbarch,
8563                                       sal.pspace, sal.pc, sal.section, thread);
8564         }
8565
8566       if (i == 0)
8567         {
8568           init_raw_breakpoint (b, gdbarch, sal, type, ops);
8569           b->thread = thread;
8570           b->task = task;
8571
8572           b->cond_string = cond_string;
8573           b->extra_string = extra_string;
8574           b->ignore_count = ignore_count;
8575           b->enable_state = enabled ? bp_enabled : bp_disabled;
8576           b->disposition = disposition;
8577
8578           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8579             b->loc->inserted = 1;
8580
8581           if (type == bp_static_tracepoint)
8582             {
8583               struct tracepoint *t = (struct tracepoint *) b;
8584               struct static_tracepoint_marker marker;
8585
8586               if (strace_marker_p (b))
8587                 {
8588                   /* We already know the marker exists, otherwise, we
8589                      wouldn't see a sal for it.  */
8590                   char *p = &addr_string[3];
8591                   char *endp;
8592                   char *marker_str;
8593
8594                   p = skip_spaces (p);
8595
8596                   endp = skip_to_space (p);
8597
8598                   marker_str = savestring (p, endp - p);
8599                   t->static_trace_marker_id = marker_str;
8600
8601                   printf_filtered (_("Probed static tracepoint "
8602                                      "marker \"%s\"\n"),
8603                                    t->static_trace_marker_id);
8604                 }
8605               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8606                 {
8607                   t->static_trace_marker_id = xstrdup (marker.str_id);
8608                   release_static_tracepoint_marker (&marker);
8609
8610                   printf_filtered (_("Probed static tracepoint "
8611                                      "marker \"%s\"\n"),
8612                                    t->static_trace_marker_id);
8613                 }
8614               else
8615                 warning (_("Couldn't determine the static "
8616                            "tracepoint marker to probe"));
8617             }
8618
8619           loc = b->loc;
8620         }
8621       else
8622         {
8623           loc = add_location_to_breakpoint (b, &sal);
8624           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8625             loc->inserted = 1;
8626         }
8627
8628       if (bp_loc_is_permanent (loc))
8629         make_breakpoint_permanent (b);
8630
8631       if (b->cond_string)
8632         {
8633           char *arg = b->cond_string;
8634           loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
8635           if (*arg)
8636               error (_("Garbage '%s' follows condition"), arg);
8637         }
8638
8639       /* Dynamic printf requires and uses additional arguments on the
8640          command line, otherwise it's an error.  */
8641       if (type == bp_dprintf)
8642         {
8643           if (b->extra_string)
8644             update_dprintf_command_list (b);
8645           else
8646             error (_("Format string required"));
8647         }
8648       else if (b->extra_string)
8649         error (_("Garbage '%s' at end of command"), b->extra_string);
8650     }
8651
8652   b->display_canonical = display_canonical;
8653   if (addr_string)
8654     b->addr_string = addr_string;
8655   else
8656     /* addr_string has to be used or breakpoint_re_set will delete
8657        me.  */
8658     b->addr_string
8659       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
8660   b->filter = filter;
8661 }
8662
8663 static void
8664 create_breakpoint_sal (struct gdbarch *gdbarch,
8665                        struct symtabs_and_lines sals, char *addr_string,
8666                        char *filter, char *cond_string,
8667                        char *extra_string,
8668                        enum bptype type, enum bpdisp disposition,
8669                        int thread, int task, int ignore_count,
8670                        const struct breakpoint_ops *ops, int from_tty,
8671                        int enabled, int internal, unsigned flags,
8672                        int display_canonical)
8673 {
8674   struct breakpoint *b;
8675   struct cleanup *old_chain;
8676
8677   if (is_tracepoint_type (type))
8678     {
8679       struct tracepoint *t;
8680
8681       t = XCNEW (struct tracepoint);
8682       b = &t->base;
8683     }
8684   else
8685     b = XNEW (struct breakpoint);
8686
8687   old_chain = make_cleanup (xfree, b);
8688
8689   init_breakpoint_sal (b, gdbarch,
8690                        sals, addr_string,
8691                        filter, cond_string, extra_string,
8692                        type, disposition,
8693                        thread, task, ignore_count,
8694                        ops, from_tty,
8695                        enabled, internal, flags,
8696                        display_canonical);
8697   discard_cleanups (old_chain);
8698
8699   install_breakpoint (internal, b, 0);
8700 }
8701
8702 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
8703    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8704    value.  COND_STRING, if not NULL, specified the condition to be
8705    used for all breakpoints.  Essentially the only case where
8706    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8707    function.  In that case, it's still not possible to specify
8708    separate conditions for different overloaded functions, so
8709    we take just a single condition string.
8710    
8711    NOTE: If the function succeeds, the caller is expected to cleanup
8712    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8713    array contents).  If the function fails (error() is called), the
8714    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8715    COND and SALS arrays and each of those arrays contents.  */
8716
8717 static void
8718 create_breakpoints_sal (struct gdbarch *gdbarch,
8719                         struct linespec_result *canonical,
8720                         char *cond_string, char *extra_string,
8721                         enum bptype type, enum bpdisp disposition,
8722                         int thread, int task, int ignore_count,
8723                         const struct breakpoint_ops *ops, int from_tty,
8724                         int enabled, int internal, unsigned flags)
8725 {
8726   int i;
8727   struct linespec_sals *lsal;
8728
8729   if (canonical->pre_expanded)
8730     gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8731
8732   for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
8733     {
8734       /* Note that 'addr_string' can be NULL in the case of a plain
8735          'break', without arguments.  */
8736       char *addr_string = (canonical->addr_string
8737                            ? xstrdup (canonical->addr_string)
8738                            : NULL);
8739       char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8740       struct cleanup *inner = make_cleanup (xfree, addr_string);
8741
8742       make_cleanup (xfree, filter_string);
8743       create_breakpoint_sal (gdbarch, lsal->sals,
8744                              addr_string,
8745                              filter_string,
8746                              cond_string, extra_string,
8747                              type, disposition,
8748                              thread, task, ignore_count, ops,
8749                              from_tty, enabled, internal, flags,
8750                              canonical->special_display);
8751       discard_cleanups (inner);
8752     }
8753 }
8754
8755 /* Parse ADDRESS which is assumed to be a SAL specification possibly
8756    followed by conditionals.  On return, SALS contains an array of SAL
8757    addresses found.  ADDR_STRING contains a vector of (canonical)
8758    address strings.  ADDRESS points to the end of the SAL.
8759
8760    The array and the line spec strings are allocated on the heap, it is
8761    the caller's responsibility to free them.  */
8762
8763 static void
8764 parse_breakpoint_sals (char **address,
8765                        struct linespec_result *canonical)
8766 {
8767   char *addr_start = *address;
8768
8769   /* If no arg given, or if first arg is 'if ', use the default
8770      breakpoint.  */
8771   if ((*address) == NULL
8772       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
8773     {
8774       /* The last displayed codepoint, if it's valid, is our default breakpoint
8775          address.  */
8776       if (last_displayed_sal_is_valid ())
8777         {
8778           struct linespec_sals lsal;
8779           struct symtab_and_line sal;
8780
8781           init_sal (&sal);              /* Initialize to zeroes.  */
8782           lsal.sals.sals = (struct symtab_and_line *)
8783             xmalloc (sizeof (struct symtab_and_line));
8784
8785           /* Set sal's pspace, pc, symtab, and line to the values
8786              corresponding to the last call to print_frame_info.  */
8787           get_last_displayed_sal (&sal);
8788           sal.section = find_pc_overlay (sal.pc);
8789
8790           /* "break" without arguments is equivalent to "break *PC"
8791              where PC is the last displayed codepoint's address.  So
8792              make sure to set sal.explicit_pc to prevent GDB from
8793              trying to expand the list of sals to include all other
8794              instances with the same symtab and line.  */
8795           sal.explicit_pc = 1;
8796
8797           lsal.sals.sals[0] = sal;
8798           lsal.sals.nelts = 1;
8799           lsal.canonical = NULL;
8800
8801           VEC_safe_push (linespec_sals, canonical->sals, &lsal);
8802         }
8803       else
8804         error (_("No default breakpoint address now."));
8805     }
8806   else
8807     {
8808       /* Force almost all breakpoints to be in terms of the
8809          current_source_symtab (which is decode_line_1's default).
8810          This should produce the results we want almost all of the
8811          time while leaving default_breakpoint_* alone.  */
8812       if (last_displayed_sal_is_valid ())
8813         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8814                           get_last_displayed_symtab (),
8815                           get_last_displayed_line (),
8816                           canonical, NULL, NULL);
8817       else
8818         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8819                           (struct symtab *) NULL, 0,
8820                           canonical, NULL, NULL);
8821     }
8822 }
8823
8824
8825 /* Convert each SAL into a real PC.  Verify that the PC can be
8826    inserted as a breakpoint.  If it can't throw an error.  */
8827
8828 static void
8829 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
8830 {    
8831   int i;
8832
8833   for (i = 0; i < sals->nelts; i++)
8834     resolve_sal_pc (&sals->sals[i]);
8835 }
8836
8837 /* Fast tracepoints may have restrictions on valid locations.  For
8838    instance, a fast tracepoint using a jump instead of a trap will
8839    likely have to overwrite more bytes than a trap would, and so can
8840    only be placed where the instruction is longer than the jump, or a
8841    multi-instruction sequence does not have a jump into the middle of
8842    it, etc.  */
8843
8844 static void
8845 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8846                             struct symtabs_and_lines *sals)
8847 {
8848   int i, rslt;
8849   struct symtab_and_line *sal;
8850   char *msg;
8851   struct cleanup *old_chain;
8852
8853   for (i = 0; i < sals->nelts; i++)
8854     {
8855       struct gdbarch *sarch;
8856
8857       sal = &sals->sals[i];
8858
8859       sarch = get_sal_arch (*sal);
8860       /* We fall back to GDBARCH if there is no architecture
8861          associated with SAL.  */
8862       if (sarch == NULL)
8863         sarch = gdbarch;
8864       rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
8865                                                NULL, &msg);
8866       old_chain = make_cleanup (xfree, msg);
8867
8868       if (!rslt)
8869         error (_("May not have a fast tracepoint at 0x%s%s"),
8870                paddress (sarch, sal->pc), (msg ? msg : ""));
8871
8872       do_cleanups (old_chain);
8873     }
8874 }
8875
8876 /* Given TOK, a string specification of condition and thread, as
8877    accepted by the 'break' command, extract the condition
8878    string and thread number and set *COND_STRING and *THREAD.
8879    PC identifies the context at which the condition should be parsed.
8880    If no condition is found, *COND_STRING is set to NULL.
8881    If no thread is found, *THREAD is set to -1.  */
8882
8883 static void
8884 find_condition_and_thread (char *tok, CORE_ADDR pc,
8885                            char **cond_string, int *thread, int *task,
8886                            char **rest)
8887 {
8888   *cond_string = NULL;
8889   *thread = -1;
8890   while (tok && *tok)
8891     {
8892       char *end_tok;
8893       int toklen;
8894       char *cond_start = NULL;
8895       char *cond_end = NULL;
8896
8897       tok = skip_spaces (tok);
8898
8899       if ((*tok == '"' || *tok == ',') && rest)
8900         {
8901           *rest = savestring (tok, strlen (tok));
8902           return;
8903         }
8904
8905       end_tok = skip_to_space (tok);
8906
8907       toklen = end_tok - tok;
8908
8909       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8910         {
8911           struct expression *expr;
8912
8913           tok = cond_start = end_tok + 1;
8914           expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
8915           xfree (expr);
8916           cond_end = tok;
8917           *cond_string = savestring (cond_start, cond_end - cond_start);
8918         }
8919       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8920         {
8921           char *tmptok;
8922
8923           tok = end_tok + 1;
8924           tmptok = tok;
8925           *thread = strtol (tok, &tok, 0);
8926           if (tok == tmptok)
8927             error (_("Junk after thread keyword."));
8928           if (!valid_thread_id (*thread))
8929             error (_("Unknown thread %d."), *thread);
8930         }
8931       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8932         {
8933           char *tmptok;
8934
8935           tok = end_tok + 1;
8936           tmptok = tok;
8937           *task = strtol (tok, &tok, 0);
8938           if (tok == tmptok)
8939             error (_("Junk after task keyword."));
8940           if (!valid_task_id (*task))
8941             error (_("Unknown task %d."), *task);
8942         }
8943       else if (rest)
8944         {
8945           *rest = savestring (tok, strlen (tok));
8946           tok += toklen;
8947         }
8948       else
8949         error (_("Junk at end of arguments."));
8950     }
8951 }
8952
8953 /* Decode a static tracepoint marker spec.  */
8954
8955 static struct symtabs_and_lines
8956 decode_static_tracepoint_spec (char **arg_p)
8957 {
8958   VEC(static_tracepoint_marker_p) *markers = NULL;
8959   struct symtabs_and_lines sals;
8960   struct symtab_and_line sal;
8961   struct symbol *sym;
8962   struct cleanup *old_chain;
8963   char *p = &(*arg_p)[3];
8964   char *endp;
8965   char *marker_str;
8966   int i;
8967
8968   p = skip_spaces (p);
8969
8970   endp = skip_to_space (p);
8971
8972   marker_str = savestring (p, endp - p);
8973   old_chain = make_cleanup (xfree, marker_str);
8974
8975   markers = target_static_tracepoint_markers_by_strid (marker_str);
8976   if (VEC_empty(static_tracepoint_marker_p, markers))
8977     error (_("No known static tracepoint marker named %s"), marker_str);
8978
8979   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8980   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
8981
8982   for (i = 0; i < sals.nelts; i++)
8983     {
8984       struct static_tracepoint_marker *marker;
8985
8986       marker = VEC_index (static_tracepoint_marker_p, markers, i);
8987
8988       init_sal (&sals.sals[i]);
8989
8990       sals.sals[i] = find_pc_line (marker->address, 0);
8991       sals.sals[i].pc = marker->address;
8992
8993       release_static_tracepoint_marker (marker);
8994     }
8995
8996   do_cleanups (old_chain);
8997
8998   *arg_p = endp;
8999   return sals;
9000 }
9001
9002 /* Set a breakpoint.  This function is shared between CLI and MI
9003    functions for setting a breakpoint.  This function has two major
9004    modes of operations, selected by the PARSE_CONDITION_AND_THREAD
9005    parameter.  If non-zero, the function will parse arg, extracting
9006    breakpoint location, address and thread.  Otherwise, ARG is just
9007    the location of breakpoint, with condition and thread specified by
9008    the COND_STRING and THREAD parameters.  If INTERNAL is non-zero,
9009    the breakpoint number will be allocated from the internal
9010    breakpoint count.  Returns true if any breakpoint was created;
9011    false otherwise.  */
9012
9013 int
9014 create_breakpoint (struct gdbarch *gdbarch,
9015                    char *arg, char *cond_string,
9016                    int thread, char *extra_string,
9017                    int parse_condition_and_thread,
9018                    int tempflag, enum bptype type_wanted,
9019                    int ignore_count,
9020                    enum auto_boolean pending_break_support,
9021                    const struct breakpoint_ops *ops,
9022                    int from_tty, int enabled, int internal,
9023                    unsigned flags)
9024 {
9025   volatile struct gdb_exception e;
9026   char *copy_arg = NULL;
9027   char *addr_start = arg;
9028   struct linespec_result canonical;
9029   struct cleanup *old_chain;
9030   struct cleanup *bkpt_chain = NULL;
9031   int i;
9032   int pending = 0;
9033   int task = 0;
9034   int prev_bkpt_count = breakpoint_count;
9035
9036   gdb_assert (ops != NULL);
9037
9038   init_linespec_result (&canonical);
9039
9040   TRY_CATCH (e, RETURN_MASK_ALL)
9041     {
9042       ops->create_sals_from_address (&arg, &canonical, type_wanted,
9043                                      addr_start, &copy_arg);
9044     }
9045
9046   /* If caller is interested in rc value from parse, set value.  */
9047   switch (e.reason)
9048     {
9049     case GDB_NO_ERROR:
9050       if (VEC_empty (linespec_sals, canonical.sals))
9051         return 0;
9052       break;
9053     case RETURN_ERROR:
9054       switch (e.error)
9055         {
9056         case NOT_FOUND_ERROR:
9057
9058           /* If pending breakpoint support is turned off, throw
9059              error.  */
9060
9061           if (pending_break_support == AUTO_BOOLEAN_FALSE)
9062             throw_exception (e);
9063
9064           exception_print (gdb_stderr, e);
9065
9066           /* If pending breakpoint support is auto query and the user
9067              selects no, then simply return the error code.  */
9068           if (pending_break_support == AUTO_BOOLEAN_AUTO
9069               && !nquery (_("Make %s pending on future shared library load? "),
9070                           bptype_string (type_wanted)))
9071             return 0;
9072
9073           /* At this point, either the user was queried about setting
9074              a pending breakpoint and selected yes, or pending
9075              breakpoint behavior is on and thus a pending breakpoint
9076              is defaulted on behalf of the user.  */
9077           {
9078             struct linespec_sals lsal;
9079
9080             copy_arg = xstrdup (addr_start);
9081             lsal.canonical = xstrdup (copy_arg);
9082             lsal.sals.nelts = 1;
9083             lsal.sals.sals = XNEW (struct symtab_and_line);
9084             init_sal (&lsal.sals.sals[0]);
9085             pending = 1;
9086             VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9087           }
9088           break;
9089         default:
9090           throw_exception (e);
9091         }
9092       break;
9093     default:
9094       throw_exception (e);
9095     }
9096
9097   /* Create a chain of things that always need to be cleaned up.  */
9098   old_chain = make_cleanup_destroy_linespec_result (&canonical);
9099
9100   /* ----------------------------- SNIP -----------------------------
9101      Anything added to the cleanup chain beyond this point is assumed
9102      to be part of a breakpoint.  If the breakpoint create succeeds
9103      then the memory is not reclaimed.  */
9104   bkpt_chain = make_cleanup (null_cleanup, 0);
9105
9106   /* Resolve all line numbers to PC's and verify that the addresses
9107      are ok for the target.  */
9108   if (!pending)
9109     {
9110       int ix;
9111       struct linespec_sals *iter;
9112
9113       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9114         breakpoint_sals_to_pc (&iter->sals);
9115     }
9116
9117   /* Fast tracepoints may have additional restrictions on location.  */
9118   if (!pending && type_wanted == bp_fast_tracepoint)
9119     {
9120       int ix;
9121       struct linespec_sals *iter;
9122
9123       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9124         check_fast_tracepoint_sals (gdbarch, &iter->sals);
9125     }
9126
9127   /* Verify that condition can be parsed, before setting any
9128      breakpoints.  Allocate a separate condition expression for each
9129      breakpoint.  */
9130   if (!pending)
9131     {
9132       struct linespec_sals *lsal;
9133
9134       lsal = VEC_index (linespec_sals, canonical.sals, 0);
9135
9136       if (parse_condition_and_thread)
9137         {
9138             char *rest;
9139             /* Here we only parse 'arg' to separate condition
9140                from thread number, so parsing in context of first
9141                sal is OK.  When setting the breakpoint we'll 
9142                re-parse it in context of each sal.  */
9143             cond_string = NULL;
9144             thread = -1;
9145             rest = NULL;
9146             find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9147                                        &thread, &task, &rest);
9148             if (cond_string)
9149                 make_cleanup (xfree, cond_string);
9150             if (rest)
9151               make_cleanup (xfree, rest);
9152             if (rest)
9153               extra_string = rest;
9154         }
9155       else
9156         {
9157             /* Create a private copy of condition string.  */
9158             if (cond_string)
9159             {
9160                 cond_string = xstrdup (cond_string);
9161                 make_cleanup (xfree, cond_string);
9162             }
9163             /* Create a private copy of any extra string.  */
9164             if (extra_string)
9165               {
9166                 extra_string = xstrdup (extra_string);
9167                 make_cleanup (xfree, extra_string);
9168               }
9169         }
9170
9171       ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
9172                                    cond_string, extra_string, type_wanted,
9173                                    tempflag ? disp_del : disp_donttouch,
9174                                    thread, task, ignore_count, ops,
9175                                    from_tty, enabled, internal, flags);
9176     }
9177   else
9178     {
9179       struct breakpoint *b;
9180
9181       make_cleanup (xfree, copy_arg);
9182
9183       if (is_tracepoint_type (type_wanted))
9184         {
9185           struct tracepoint *t;
9186
9187           t = XCNEW (struct tracepoint);
9188           b = &t->base;
9189         }
9190       else
9191         b = XNEW (struct breakpoint);
9192
9193       init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9194
9195       b->addr_string = copy_arg;
9196       b->cond_string = NULL;
9197       b->extra_string = NULL;
9198       b->ignore_count = ignore_count;
9199       b->disposition = tempflag ? disp_del : disp_donttouch;
9200       b->condition_not_parsed = 1;
9201       b->enable_state = enabled ? bp_enabled : bp_disabled;
9202       if ((type_wanted != bp_breakpoint
9203            && type_wanted != bp_hardware_breakpoint) || thread != -1)
9204         b->pspace = current_program_space;
9205
9206       install_breakpoint (internal, b, 0);
9207     }
9208   
9209   if (VEC_length (linespec_sals, canonical.sals) > 1)
9210     {
9211       warning (_("Multiple breakpoints were set.\nUse the "
9212                  "\"delete\" command to delete unwanted breakpoints."));
9213       prev_breakpoint_count = prev_bkpt_count;
9214     }
9215
9216   /* That's it.  Discard the cleanups for data inserted into the
9217      breakpoint.  */
9218   discard_cleanups (bkpt_chain);
9219   /* But cleanup everything else.  */
9220   do_cleanups (old_chain);
9221
9222   /* error call may happen here - have BKPT_CHAIN already discarded.  */
9223   update_global_location_list (1);
9224
9225   return 1;
9226 }
9227
9228 /* Set a breakpoint.
9229    ARG is a string describing breakpoint address,
9230    condition, and thread.
9231    FLAG specifies if a breakpoint is hardware on,
9232    and if breakpoint is temporary, using BP_HARDWARE_FLAG
9233    and BP_TEMPFLAG.  */
9234
9235 static void
9236 break_command_1 (char *arg, int flag, int from_tty)
9237 {
9238   int tempflag = flag & BP_TEMPFLAG;
9239   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9240                              ? bp_hardware_breakpoint
9241                              : bp_breakpoint);
9242   struct breakpoint_ops *ops;
9243   const char *arg_cp = arg;
9244
9245   /* Matching breakpoints on probes.  */
9246   if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9247     ops = &bkpt_probe_breakpoint_ops;
9248   else
9249     ops = &bkpt_breakpoint_ops;
9250
9251   create_breakpoint (get_current_arch (),
9252                      arg,
9253                      NULL, 0, NULL, 1 /* parse arg */,
9254                      tempflag, type_wanted,
9255                      0 /* Ignore count */,
9256                      pending_break_support,
9257                      ops,
9258                      from_tty,
9259                      1 /* enabled */,
9260                      0 /* internal */,
9261                      0);
9262 }
9263
9264 /* Helper function for break_command_1 and disassemble_command.  */
9265
9266 void
9267 resolve_sal_pc (struct symtab_and_line *sal)
9268 {
9269   CORE_ADDR pc;
9270
9271   if (sal->pc == 0 && sal->symtab != NULL)
9272     {
9273       if (!find_line_pc (sal->symtab, sal->line, &pc))
9274         error (_("No line %d in file \"%s\"."),
9275                sal->line, sal->symtab->filename);
9276       sal->pc = pc;
9277
9278       /* If this SAL corresponds to a breakpoint inserted using a line
9279          number, then skip the function prologue if necessary.  */
9280       if (sal->explicit_line)
9281         skip_prologue_sal (sal);
9282     }
9283
9284   if (sal->section == 0 && sal->symtab != NULL)
9285     {
9286       struct blockvector *bv;
9287       struct block *b;
9288       struct symbol *sym;
9289
9290       bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9291       if (bv != NULL)
9292         {
9293           sym = block_linkage_function (b);
9294           if (sym != NULL)
9295             {
9296               fixup_symbol_section (sym, sal->symtab->objfile);
9297               sal->section = SYMBOL_OBJ_SECTION (sym);
9298             }
9299           else
9300             {
9301               /* It really is worthwhile to have the section, so we'll
9302                  just have to look harder. This case can be executed
9303                  if we have line numbers but no functions (as can
9304                  happen in assembly source).  */
9305
9306               struct minimal_symbol *msym;
9307               struct cleanup *old_chain = save_current_space_and_thread ();
9308
9309               switch_to_program_space_and_thread (sal->pspace);
9310
9311               msym = lookup_minimal_symbol_by_pc (sal->pc);
9312               if (msym)
9313                 sal->section = SYMBOL_OBJ_SECTION (msym);
9314
9315               do_cleanups (old_chain);
9316             }
9317         }
9318     }
9319 }
9320
9321 void
9322 break_command (char *arg, int from_tty)
9323 {
9324   break_command_1 (arg, 0, from_tty);
9325 }
9326
9327 void
9328 tbreak_command (char *arg, int from_tty)
9329 {
9330   break_command_1 (arg, BP_TEMPFLAG, from_tty);
9331 }
9332
9333 static void
9334 hbreak_command (char *arg, int from_tty)
9335 {
9336   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9337 }
9338
9339 static void
9340 thbreak_command (char *arg, int from_tty)
9341 {
9342   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9343 }
9344
9345 static void
9346 stop_command (char *arg, int from_tty)
9347 {
9348   printf_filtered (_("Specify the type of breakpoint to set.\n\
9349 Usage: stop in <function | address>\n\
9350        stop at <line>\n"));
9351 }
9352
9353 static void
9354 stopin_command (char *arg, int from_tty)
9355 {
9356   int badInput = 0;
9357
9358   if (arg == (char *) NULL)
9359     badInput = 1;
9360   else if (*arg != '*')
9361     {
9362       char *argptr = arg;
9363       int hasColon = 0;
9364
9365       /* Look for a ':'.  If this is a line number specification, then
9366          say it is bad, otherwise, it should be an address or
9367          function/method name.  */
9368       while (*argptr && !hasColon)
9369         {
9370           hasColon = (*argptr == ':');
9371           argptr++;
9372         }
9373
9374       if (hasColon)
9375         badInput = (*argptr != ':');    /* Not a class::method */
9376       else
9377         badInput = isdigit (*arg);      /* a simple line number */
9378     }
9379
9380   if (badInput)
9381     printf_filtered (_("Usage: stop in <function | address>\n"));
9382   else
9383     break_command_1 (arg, 0, from_tty);
9384 }
9385
9386 static void
9387 stopat_command (char *arg, int from_tty)
9388 {
9389   int badInput = 0;
9390
9391   if (arg == (char *) NULL || *arg == '*')      /* no line number */
9392     badInput = 1;
9393   else
9394     {
9395       char *argptr = arg;
9396       int hasColon = 0;
9397
9398       /* Look for a ':'.  If there is a '::' then get out, otherwise
9399          it is probably a line number.  */
9400       while (*argptr && !hasColon)
9401         {
9402           hasColon = (*argptr == ':');
9403           argptr++;
9404         }
9405
9406       if (hasColon)
9407         badInput = (*argptr == ':');    /* we have class::method */
9408       else
9409         badInput = !isdigit (*arg);     /* not a line number */
9410     }
9411
9412   if (badInput)
9413     printf_filtered (_("Usage: stop at <line>\n"));
9414   else
9415     break_command_1 (arg, 0, from_tty);
9416 }
9417
9418 void dprintf_command (char *arg, int from_tty);
9419
9420 /* The dynamic printf command is mostly like a regular breakpoint, but
9421    with a prewired command list consisting of a single output command,
9422    built from extra arguments supplied on the dprintf command
9423    line.  */
9424
9425 void
9426 dprintf_command (char *arg, int from_tty)
9427 {
9428   create_breakpoint (get_current_arch (),
9429                      arg,
9430                      NULL, 0, NULL, 1 /* parse arg */,
9431                      0, bp_dprintf,
9432                      0 /* Ignore count */,
9433                      pending_break_support,
9434                      &dprintf_breakpoint_ops,
9435                      from_tty,
9436                      1 /* enabled */,
9437                      0 /* internal */,
9438                      0);
9439 }
9440
9441 /* Implement the "breakpoint_hit" breakpoint_ops method for
9442    ranged breakpoints.  */
9443
9444 static int
9445 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9446                                   struct address_space *aspace,
9447                                   CORE_ADDR bp_addr,
9448                                   const struct target_waitstatus *ws)
9449 {
9450   if (ws->kind != TARGET_WAITKIND_STOPPED
9451       || ws->value.sig != TARGET_SIGNAL_TRAP)
9452     return 0;
9453
9454   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9455                                          bl->length, aspace, bp_addr);
9456 }
9457
9458 /* Implement the "resources_needed" breakpoint_ops method for
9459    ranged breakpoints.  */
9460
9461 static int
9462 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9463 {
9464   return target_ranged_break_num_registers ();
9465 }
9466
9467 /* Implement the "print_it" breakpoint_ops method for
9468    ranged breakpoints.  */
9469
9470 static enum print_stop_action
9471 print_it_ranged_breakpoint (bpstat bs)
9472 {
9473   struct breakpoint *b = bs->breakpoint_at;
9474   struct bp_location *bl = b->loc;
9475   struct ui_out *uiout = current_uiout;
9476
9477   gdb_assert (b->type == bp_hardware_breakpoint);
9478
9479   /* Ranged breakpoints have only one location.  */
9480   gdb_assert (bl && bl->next == NULL);
9481
9482   annotate_breakpoint (b->number);
9483   if (b->disposition == disp_del)
9484     ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9485   else
9486     ui_out_text (uiout, "\nRanged breakpoint ");
9487   if (ui_out_is_mi_like_p (uiout))
9488     {
9489       ui_out_field_string (uiout, "reason",
9490                       async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9491       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9492     }
9493   ui_out_field_int (uiout, "bkptno", b->number);
9494   ui_out_text (uiout, ", ");
9495
9496   return PRINT_SRC_AND_LOC;
9497 }
9498
9499 /* Implement the "print_one" breakpoint_ops method for
9500    ranged breakpoints.  */
9501
9502 static void
9503 print_one_ranged_breakpoint (struct breakpoint *b,
9504                              struct bp_location **last_loc)
9505 {
9506   struct bp_location *bl = b->loc;
9507   struct value_print_options opts;
9508   struct ui_out *uiout = current_uiout;
9509
9510   /* Ranged breakpoints have only one location.  */
9511   gdb_assert (bl && bl->next == NULL);
9512
9513   get_user_print_options (&opts);
9514
9515   if (opts.addressprint)
9516     /* We don't print the address range here, it will be printed later
9517        by print_one_detail_ranged_breakpoint.  */
9518     ui_out_field_skip (uiout, "addr");
9519   annotate_field (5);
9520   print_breakpoint_location (b, bl);
9521   *last_loc = bl;
9522 }
9523
9524 /* Implement the "print_one_detail" breakpoint_ops method for
9525    ranged breakpoints.  */
9526
9527 static void
9528 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9529                                     struct ui_out *uiout)
9530 {
9531   CORE_ADDR address_start, address_end;
9532   struct bp_location *bl = b->loc;
9533   struct ui_file *stb = mem_fileopen ();
9534   struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
9535
9536   gdb_assert (bl);
9537
9538   address_start = bl->address;
9539   address_end = address_start + bl->length - 1;
9540
9541   ui_out_text (uiout, "\taddress range: ");
9542   fprintf_unfiltered (stb, "[%s, %s]",
9543                       print_core_address (bl->gdbarch, address_start),
9544                       print_core_address (bl->gdbarch, address_end));
9545   ui_out_field_stream (uiout, "addr", stb);
9546   ui_out_text (uiout, "\n");
9547
9548   do_cleanups (cleanup);
9549 }
9550
9551 /* Implement the "print_mention" breakpoint_ops method for
9552    ranged breakpoints.  */
9553
9554 static void
9555 print_mention_ranged_breakpoint (struct breakpoint *b)
9556 {
9557   struct bp_location *bl = b->loc;
9558   struct ui_out *uiout = current_uiout;
9559
9560   gdb_assert (bl);
9561   gdb_assert (b->type == bp_hardware_breakpoint);
9562
9563   if (ui_out_is_mi_like_p (uiout))
9564     return;
9565
9566   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9567                    b->number, paddress (bl->gdbarch, bl->address),
9568                    paddress (bl->gdbarch, bl->address + bl->length - 1));
9569 }
9570
9571 /* Implement the "print_recreate" breakpoint_ops method for
9572    ranged breakpoints.  */
9573
9574 static void
9575 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9576 {
9577   fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9578                       b->addr_string_range_end);
9579   print_recreate_thread (b, fp);
9580 }
9581
9582 /* The breakpoint_ops structure to be used in ranged breakpoints.  */
9583
9584 static struct breakpoint_ops ranged_breakpoint_ops;
9585
9586 /* Find the address where the end of the breakpoint range should be
9587    placed, given the SAL of the end of the range.  This is so that if
9588    the user provides a line number, the end of the range is set to the
9589    last instruction of the given line.  */
9590
9591 static CORE_ADDR
9592 find_breakpoint_range_end (struct symtab_and_line sal)
9593 {
9594   CORE_ADDR end;
9595
9596   /* If the user provided a PC value, use it.  Otherwise,
9597      find the address of the end of the given location.  */
9598   if (sal.explicit_pc)
9599     end = sal.pc;
9600   else
9601     {
9602       int ret;
9603       CORE_ADDR start;
9604
9605       ret = find_line_pc_range (sal, &start, &end);
9606       if (!ret)
9607         error (_("Could not find location of the end of the range."));
9608
9609       /* find_line_pc_range returns the start of the next line.  */
9610       end--;
9611     }
9612
9613   return end;
9614 }
9615
9616 /* Implement the "break-range" CLI command.  */
9617
9618 static void
9619 break_range_command (char *arg, int from_tty)
9620 {
9621   char *arg_start, *addr_string_start, *addr_string_end;
9622   struct linespec_result canonical_start, canonical_end;
9623   int bp_count, can_use_bp, length;
9624   CORE_ADDR end;
9625   struct breakpoint *b;
9626   struct symtab_and_line sal_start, sal_end;
9627   struct cleanup *cleanup_bkpt;
9628   struct linespec_sals *lsal_start, *lsal_end;
9629
9630   /* We don't support software ranged breakpoints.  */
9631   if (target_ranged_break_num_registers () < 0)
9632     error (_("This target does not support hardware ranged breakpoints."));
9633
9634   bp_count = hw_breakpoint_used_count ();
9635   bp_count += target_ranged_break_num_registers ();
9636   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9637                                                    bp_count, 0);
9638   if (can_use_bp < 0)
9639     error (_("Hardware breakpoints used exceeds limit."));
9640
9641   arg = skip_spaces (arg);
9642   if (arg == NULL || arg[0] == '\0')
9643     error(_("No address range specified."));
9644
9645   init_linespec_result (&canonical_start);
9646
9647   arg_start = arg;
9648   parse_breakpoint_sals (&arg, &canonical_start);
9649
9650   cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
9651
9652   if (arg[0] != ',')
9653     error (_("Too few arguments."));
9654   else if (VEC_empty (linespec_sals, canonical_start.sals))
9655     error (_("Could not find location of the beginning of the range."));
9656
9657   lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9658
9659   if (VEC_length (linespec_sals, canonical_start.sals) > 1
9660       || lsal_start->sals.nelts != 1)
9661     error (_("Cannot create a ranged breakpoint with multiple locations."));
9662
9663   sal_start = lsal_start->sals.sals[0];
9664   addr_string_start = savestring (arg_start, arg - arg_start);
9665   make_cleanup (xfree, addr_string_start);
9666
9667   arg++;        /* Skip the comma.  */
9668   arg = skip_spaces (arg);
9669
9670   /* Parse the end location.  */
9671
9672   init_linespec_result (&canonical_end);
9673   arg_start = arg;
9674
9675   /* We call decode_line_full directly here instead of using
9676      parse_breakpoint_sals because we need to specify the start location's
9677      symtab and line as the default symtab and line for the end of the
9678      range.  This makes it possible to have ranges like "foo.c:27, +14",
9679      where +14 means 14 lines from the start location.  */
9680   decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9681                     sal_start.symtab, sal_start.line,
9682                     &canonical_end, NULL, NULL);
9683
9684   make_cleanup_destroy_linespec_result (&canonical_end);
9685
9686   if (VEC_empty (linespec_sals, canonical_end.sals))
9687     error (_("Could not find location of the end of the range."));
9688
9689   lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9690   if (VEC_length (linespec_sals, canonical_end.sals) > 1
9691       || lsal_end->sals.nelts != 1)
9692     error (_("Cannot create a ranged breakpoint with multiple locations."));
9693
9694   sal_end = lsal_end->sals.sals[0];
9695   addr_string_end = savestring (arg_start, arg - arg_start);
9696   make_cleanup (xfree, addr_string_end);
9697
9698   end = find_breakpoint_range_end (sal_end);
9699   if (sal_start.pc > end)
9700     error (_("Invalid address range, end precedes start."));
9701
9702   length = end - sal_start.pc + 1;
9703   if (length < 0)
9704     /* Length overflowed.  */
9705     error (_("Address range too large."));
9706   else if (length == 1)
9707     {
9708       /* This range is simple enough to be handled by
9709          the `hbreak' command.  */
9710       hbreak_command (addr_string_start, 1);
9711
9712       do_cleanups (cleanup_bkpt);
9713
9714       return;
9715     }
9716
9717   /* Now set up the breakpoint.  */
9718   b = set_raw_breakpoint (get_current_arch (), sal_start,
9719                           bp_hardware_breakpoint, &ranged_breakpoint_ops);
9720   set_breakpoint_count (breakpoint_count + 1);
9721   b->number = breakpoint_count;
9722   b->disposition = disp_donttouch;
9723   b->addr_string = xstrdup (addr_string_start);
9724   b->addr_string_range_end = xstrdup (addr_string_end);
9725   b->loc->length = length;
9726
9727   do_cleanups (cleanup_bkpt);
9728
9729   mention (b);
9730   observer_notify_breakpoint_created (b);
9731   update_global_location_list (1);
9732 }
9733
9734 /*  Return non-zero if EXP is verified as constant.  Returned zero
9735     means EXP is variable.  Also the constant detection may fail for
9736     some constant expressions and in such case still falsely return
9737     zero.  */
9738
9739 static int
9740 watchpoint_exp_is_const (const struct expression *exp)
9741 {
9742   int i = exp->nelts;
9743
9744   while (i > 0)
9745     {
9746       int oplenp, argsp;
9747
9748       /* We are only interested in the descriptor of each element.  */
9749       operator_length (exp, i, &oplenp, &argsp);
9750       i -= oplenp;
9751
9752       switch (exp->elts[i].opcode)
9753         {
9754         case BINOP_ADD:
9755         case BINOP_SUB:
9756         case BINOP_MUL:
9757         case BINOP_DIV:
9758         case BINOP_REM:
9759         case BINOP_MOD:
9760         case BINOP_LSH:
9761         case BINOP_RSH:
9762         case BINOP_LOGICAL_AND:
9763         case BINOP_LOGICAL_OR:
9764         case BINOP_BITWISE_AND:
9765         case BINOP_BITWISE_IOR:
9766         case BINOP_BITWISE_XOR:
9767         case BINOP_EQUAL:
9768         case BINOP_NOTEQUAL:
9769         case BINOP_LESS:
9770         case BINOP_GTR:
9771         case BINOP_LEQ:
9772         case BINOP_GEQ:
9773         case BINOP_REPEAT:
9774         case BINOP_COMMA:
9775         case BINOP_EXP:
9776         case BINOP_MIN:
9777         case BINOP_MAX:
9778         case BINOP_INTDIV:
9779         case BINOP_CONCAT:
9780         case BINOP_IN:
9781         case BINOP_RANGE:
9782         case TERNOP_COND:
9783         case TERNOP_SLICE:
9784         case TERNOP_SLICE_COUNT:
9785
9786         case OP_LONG:
9787         case OP_DOUBLE:
9788         case OP_DECFLOAT:
9789         case OP_LAST:
9790         case OP_COMPLEX:
9791         case OP_STRING:
9792         case OP_BITSTRING:
9793         case OP_ARRAY:
9794         case OP_TYPE:
9795         case OP_NAME:
9796         case OP_OBJC_NSSTRING:
9797
9798         case UNOP_NEG:
9799         case UNOP_LOGICAL_NOT:
9800         case UNOP_COMPLEMENT:
9801         case UNOP_ADDR:
9802         case UNOP_HIGH:
9803         case UNOP_CAST:
9804           /* Unary, binary and ternary operators: We have to check
9805              their operands.  If they are constant, then so is the
9806              result of that operation.  For instance, if A and B are
9807              determined to be constants, then so is "A + B".
9808
9809              UNOP_IND is one exception to the rule above, because the
9810              value of *ADDR is not necessarily a constant, even when
9811              ADDR is.  */
9812           break;
9813
9814         case OP_VAR_VALUE:
9815           /* Check whether the associated symbol is a constant.
9816
9817              We use SYMBOL_CLASS rather than TYPE_CONST because it's
9818              possible that a buggy compiler could mark a variable as
9819              constant even when it is not, and TYPE_CONST would return
9820              true in this case, while SYMBOL_CLASS wouldn't.
9821
9822              We also have to check for function symbols because they
9823              are always constant.  */
9824           {
9825             struct symbol *s = exp->elts[i + 2].symbol;
9826
9827             if (SYMBOL_CLASS (s) != LOC_BLOCK
9828                 && SYMBOL_CLASS (s) != LOC_CONST
9829                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9830               return 0;
9831             break;
9832           }
9833
9834         /* The default action is to return 0 because we are using
9835            the optimistic approach here: If we don't know something,
9836            then it is not a constant.  */
9837         default:
9838           return 0;
9839         }
9840     }
9841
9842   return 1;
9843 }
9844
9845 /* Implement the "dtor" breakpoint_ops method for watchpoints.  */
9846
9847 static void
9848 dtor_watchpoint (struct breakpoint *self)
9849 {
9850   struct watchpoint *w = (struct watchpoint *) self;
9851
9852   xfree (w->cond_exp);
9853   xfree (w->exp);
9854   xfree (w->exp_string);
9855   xfree (w->exp_string_reparse);
9856   value_free (w->val);
9857
9858   base_breakpoint_ops.dtor (self);
9859 }
9860
9861 /* Implement the "re_set" breakpoint_ops method for watchpoints.  */
9862
9863 static void
9864 re_set_watchpoint (struct breakpoint *b)
9865 {
9866   struct watchpoint *w = (struct watchpoint *) b;
9867
9868   /* Watchpoint can be either on expression using entirely global
9869      variables, or it can be on local variables.
9870
9871      Watchpoints of the first kind are never auto-deleted, and even
9872      persist across program restarts.  Since they can use variables
9873      from shared libraries, we need to reparse expression as libraries
9874      are loaded and unloaded.
9875
9876      Watchpoints on local variables can also change meaning as result
9877      of solib event.  For example, if a watchpoint uses both a local
9878      and a global variables in expression, it's a local watchpoint,
9879      but unloading of a shared library will make the expression
9880      invalid.  This is not a very common use case, but we still
9881      re-evaluate expression, to avoid surprises to the user.
9882
9883      Note that for local watchpoints, we re-evaluate it only if
9884      watchpoints frame id is still valid.  If it's not, it means the
9885      watchpoint is out of scope and will be deleted soon.  In fact,
9886      I'm not sure we'll ever be called in this case.
9887
9888      If a local watchpoint's frame id is still valid, then
9889      w->exp_valid_block is likewise valid, and we can safely use it.
9890
9891      Don't do anything about disabled watchpoints, since they will be
9892      reevaluated again when enabled.  */
9893   update_watchpoint (w, 1 /* reparse */);
9894 }
9895
9896 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
9897
9898 static int
9899 insert_watchpoint (struct bp_location *bl)
9900 {
9901   struct watchpoint *w = (struct watchpoint *) bl->owner;
9902   int length = w->exact ? 1 : bl->length;
9903
9904   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9905                                    w->cond_exp);
9906 }
9907
9908 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
9909
9910 static int
9911 remove_watchpoint (struct bp_location *bl)
9912 {
9913   struct watchpoint *w = (struct watchpoint *) bl->owner;
9914   int length = w->exact ? 1 : bl->length;
9915
9916   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9917                                    w->cond_exp);
9918 }
9919
9920 static int
9921 breakpoint_hit_watchpoint (const struct bp_location *bl,
9922                            struct address_space *aspace, CORE_ADDR bp_addr,
9923                            const struct target_waitstatus *ws)
9924 {
9925   struct breakpoint *b = bl->owner;
9926   struct watchpoint *w = (struct watchpoint *) b;
9927
9928   /* Continuable hardware watchpoints are treated as non-existent if the
9929      reason we stopped wasn't a hardware watchpoint (we didn't stop on
9930      some data address).  Otherwise gdb won't stop on a break instruction
9931      in the code (not from a breakpoint) when a hardware watchpoint has
9932      been defined.  Also skip watchpoints which we know did not trigger
9933      (did not match the data address).  */
9934   if (is_hardware_watchpoint (b)
9935       && w->watchpoint_triggered == watch_triggered_no)
9936     return 0;
9937
9938   return 1;
9939 }
9940
9941 static void
9942 check_status_watchpoint (bpstat bs)
9943 {
9944   gdb_assert (is_watchpoint (bs->breakpoint_at));
9945
9946   bpstat_check_watchpoint (bs);
9947 }
9948
9949 /* Implement the "resources_needed" breakpoint_ops method for
9950    hardware watchpoints.  */
9951
9952 static int
9953 resources_needed_watchpoint (const struct bp_location *bl)
9954 {
9955   struct watchpoint *w = (struct watchpoint *) bl->owner;
9956   int length = w->exact? 1 : bl->length;
9957
9958   return target_region_ok_for_hw_watchpoint (bl->address, length);
9959 }
9960
9961 /* Implement the "works_in_software_mode" breakpoint_ops method for
9962    hardware watchpoints.  */
9963
9964 static int
9965 works_in_software_mode_watchpoint (const struct breakpoint *b)
9966 {
9967   /* Read and access watchpoints only work with hardware support.  */
9968   return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9969 }
9970
9971 static enum print_stop_action
9972 print_it_watchpoint (bpstat bs)
9973 {
9974   struct cleanup *old_chain;
9975   struct breakpoint *b;
9976   const struct bp_location *bl;
9977   struct ui_file *stb;
9978   enum print_stop_action result;
9979   struct watchpoint *w;
9980   struct ui_out *uiout = current_uiout;
9981
9982   gdb_assert (bs->bp_location_at != NULL);
9983
9984   bl = bs->bp_location_at;
9985   b = bs->breakpoint_at;
9986   w = (struct watchpoint *) b;
9987
9988   stb = mem_fileopen ();
9989   old_chain = make_cleanup_ui_file_delete (stb);
9990
9991   switch (b->type)
9992     {
9993     case bp_watchpoint:
9994     case bp_hardware_watchpoint:
9995       annotate_watchpoint (b->number);
9996       if (ui_out_is_mi_like_p (uiout))
9997         ui_out_field_string
9998           (uiout, "reason",
9999            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10000       mention (b);
10001       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10002       ui_out_text (uiout, "\nOld value = ");
10003       watchpoint_value_print (bs->old_val, stb);
10004       ui_out_field_stream (uiout, "old", stb);
10005       ui_out_text (uiout, "\nNew value = ");
10006       watchpoint_value_print (w->val, stb);
10007       ui_out_field_stream (uiout, "new", stb);
10008       ui_out_text (uiout, "\n");
10009       /* More than one watchpoint may have been triggered.  */
10010       result = PRINT_UNKNOWN;
10011       break;
10012
10013     case bp_read_watchpoint:
10014       if (ui_out_is_mi_like_p (uiout))
10015         ui_out_field_string
10016           (uiout, "reason",
10017            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10018       mention (b);
10019       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10020       ui_out_text (uiout, "\nValue = ");
10021       watchpoint_value_print (w->val, stb);
10022       ui_out_field_stream (uiout, "value", stb);
10023       ui_out_text (uiout, "\n");
10024       result = PRINT_UNKNOWN;
10025       break;
10026
10027     case bp_access_watchpoint:
10028       if (bs->old_val != NULL)
10029         {
10030           annotate_watchpoint (b->number);
10031           if (ui_out_is_mi_like_p (uiout))
10032             ui_out_field_string
10033               (uiout, "reason",
10034                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10035           mention (b);
10036           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10037           ui_out_text (uiout, "\nOld value = ");
10038           watchpoint_value_print (bs->old_val, stb);
10039           ui_out_field_stream (uiout, "old", stb);
10040           ui_out_text (uiout, "\nNew value = ");
10041         }
10042       else
10043         {
10044           mention (b);
10045           if (ui_out_is_mi_like_p (uiout))
10046             ui_out_field_string
10047               (uiout, "reason",
10048                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10049           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10050           ui_out_text (uiout, "\nValue = ");
10051         }
10052       watchpoint_value_print (w->val, stb);
10053       ui_out_field_stream (uiout, "new", stb);
10054       ui_out_text (uiout, "\n");
10055       result = PRINT_UNKNOWN;
10056       break;
10057     default:
10058       result = PRINT_UNKNOWN;
10059     }
10060
10061   do_cleanups (old_chain);
10062   return result;
10063 }
10064
10065 /* Implement the "print_mention" breakpoint_ops method for hardware
10066    watchpoints.  */
10067
10068 static void
10069 print_mention_watchpoint (struct breakpoint *b)
10070 {
10071   struct cleanup *ui_out_chain;
10072   struct watchpoint *w = (struct watchpoint *) b;
10073   struct ui_out *uiout = current_uiout;
10074
10075   switch (b->type)
10076     {
10077     case bp_watchpoint:
10078       ui_out_text (uiout, "Watchpoint ");
10079       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10080       break;
10081     case bp_hardware_watchpoint:
10082       ui_out_text (uiout, "Hardware watchpoint ");
10083       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10084       break;
10085     case bp_read_watchpoint:
10086       ui_out_text (uiout, "Hardware read watchpoint ");
10087       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10088       break;
10089     case bp_access_watchpoint:
10090       ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10091       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10092       break;
10093     default:
10094       internal_error (__FILE__, __LINE__,
10095                       _("Invalid hardware watchpoint type."));
10096     }
10097
10098   ui_out_field_int (uiout, "number", b->number);
10099   ui_out_text (uiout, ": ");
10100   ui_out_field_string (uiout, "exp", w->exp_string);
10101   do_cleanups (ui_out_chain);
10102 }
10103
10104 /* Implement the "print_recreate" breakpoint_ops method for
10105    watchpoints.  */
10106
10107 static void
10108 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10109 {
10110   struct watchpoint *w = (struct watchpoint *) b;
10111
10112   switch (b->type)
10113     {
10114     case bp_watchpoint:
10115     case bp_hardware_watchpoint:
10116       fprintf_unfiltered (fp, "watch");
10117       break;
10118     case bp_read_watchpoint:
10119       fprintf_unfiltered (fp, "rwatch");
10120       break;
10121     case bp_access_watchpoint:
10122       fprintf_unfiltered (fp, "awatch");
10123       break;
10124     default:
10125       internal_error (__FILE__, __LINE__,
10126                       _("Invalid watchpoint type."));
10127     }
10128
10129   fprintf_unfiltered (fp, " %s", w->exp_string);
10130   print_recreate_thread (b, fp);
10131 }
10132
10133 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
10134
10135 static struct breakpoint_ops watchpoint_breakpoint_ops;
10136
10137 /* Implement the "insert" breakpoint_ops method for
10138    masked hardware watchpoints.  */
10139
10140 static int
10141 insert_masked_watchpoint (struct bp_location *bl)
10142 {
10143   struct watchpoint *w = (struct watchpoint *) bl->owner;
10144
10145   return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10146                                         bl->watchpoint_type);
10147 }
10148
10149 /* Implement the "remove" breakpoint_ops method for
10150    masked hardware watchpoints.  */
10151
10152 static int
10153 remove_masked_watchpoint (struct bp_location *bl)
10154 {
10155   struct watchpoint *w = (struct watchpoint *) bl->owner;
10156
10157   return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10158                                         bl->watchpoint_type);
10159 }
10160
10161 /* Implement the "resources_needed" breakpoint_ops method for
10162    masked hardware watchpoints.  */
10163
10164 static int
10165 resources_needed_masked_watchpoint (const struct bp_location *bl)
10166 {
10167   struct watchpoint *w = (struct watchpoint *) bl->owner;
10168
10169   return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10170 }
10171
10172 /* Implement the "works_in_software_mode" breakpoint_ops method for
10173    masked hardware watchpoints.  */
10174
10175 static int
10176 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10177 {
10178   return 0;
10179 }
10180
10181 /* Implement the "print_it" breakpoint_ops method for
10182    masked hardware watchpoints.  */
10183
10184 static enum print_stop_action
10185 print_it_masked_watchpoint (bpstat bs)
10186 {
10187   struct breakpoint *b = bs->breakpoint_at;
10188   struct ui_out *uiout = current_uiout;
10189
10190   /* Masked watchpoints have only one location.  */
10191   gdb_assert (b->loc && b->loc->next == NULL);
10192
10193   switch (b->type)
10194     {
10195     case bp_hardware_watchpoint:
10196       annotate_watchpoint (b->number);
10197       if (ui_out_is_mi_like_p (uiout))
10198         ui_out_field_string
10199           (uiout, "reason",
10200            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10201       break;
10202
10203     case bp_read_watchpoint:
10204       if (ui_out_is_mi_like_p (uiout))
10205         ui_out_field_string
10206           (uiout, "reason",
10207            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10208       break;
10209
10210     case bp_access_watchpoint:
10211       if (ui_out_is_mi_like_p (uiout))
10212         ui_out_field_string
10213           (uiout, "reason",
10214            async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10215       break;
10216     default:
10217       internal_error (__FILE__, __LINE__,
10218                       _("Invalid hardware watchpoint type."));
10219     }
10220
10221   mention (b);
10222   ui_out_text (uiout, _("\n\
10223 Check the underlying instruction at PC for the memory\n\
10224 address and value which triggered this watchpoint.\n"));
10225   ui_out_text (uiout, "\n");
10226
10227   /* More than one watchpoint may have been triggered.  */
10228   return PRINT_UNKNOWN;
10229 }
10230
10231 /* Implement the "print_one_detail" breakpoint_ops method for
10232    masked hardware watchpoints.  */
10233
10234 static void
10235 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10236                                     struct ui_out *uiout)
10237 {
10238   struct watchpoint *w = (struct watchpoint *) b;
10239
10240   /* Masked watchpoints have only one location.  */
10241   gdb_assert (b->loc && b->loc->next == NULL);
10242
10243   ui_out_text (uiout, "\tmask ");
10244   ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10245   ui_out_text (uiout, "\n");
10246 }
10247
10248 /* Implement the "print_mention" breakpoint_ops method for
10249    masked hardware watchpoints.  */
10250
10251 static void
10252 print_mention_masked_watchpoint (struct breakpoint *b)
10253 {
10254   struct watchpoint *w = (struct watchpoint *) b;
10255   struct ui_out *uiout = current_uiout;
10256   struct cleanup *ui_out_chain;
10257
10258   switch (b->type)
10259     {
10260     case bp_hardware_watchpoint:
10261       ui_out_text (uiout, "Masked hardware watchpoint ");
10262       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10263       break;
10264     case bp_read_watchpoint:
10265       ui_out_text (uiout, "Masked hardware read watchpoint ");
10266       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10267       break;
10268     case bp_access_watchpoint:
10269       ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10270       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10271       break;
10272     default:
10273       internal_error (__FILE__, __LINE__,
10274                       _("Invalid hardware watchpoint type."));
10275     }
10276
10277   ui_out_field_int (uiout, "number", b->number);
10278   ui_out_text (uiout, ": ");
10279   ui_out_field_string (uiout, "exp", w->exp_string);
10280   do_cleanups (ui_out_chain);
10281 }
10282
10283 /* Implement the "print_recreate" breakpoint_ops method for
10284    masked hardware watchpoints.  */
10285
10286 static void
10287 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10288 {
10289   struct watchpoint *w = (struct watchpoint *) b;
10290   char tmp[40];
10291
10292   switch (b->type)
10293     {
10294     case bp_hardware_watchpoint:
10295       fprintf_unfiltered (fp, "watch");
10296       break;
10297     case bp_read_watchpoint:
10298       fprintf_unfiltered (fp, "rwatch");
10299       break;
10300     case bp_access_watchpoint:
10301       fprintf_unfiltered (fp, "awatch");
10302       break;
10303     default:
10304       internal_error (__FILE__, __LINE__,
10305                       _("Invalid hardware watchpoint type."));
10306     }
10307
10308   sprintf_vma (tmp, w->hw_wp_mask);
10309   fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10310   print_recreate_thread (b, fp);
10311 }
10312
10313 /* The breakpoint_ops structure to be used in masked hardware watchpoints.  */
10314
10315 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10316
10317 /* Tell whether the given watchpoint is a masked hardware watchpoint.  */
10318
10319 static int
10320 is_masked_watchpoint (const struct breakpoint *b)
10321 {
10322   return b->ops == &masked_watchpoint_breakpoint_ops;
10323 }
10324
10325 /* accessflag:  hw_write:  watch write, 
10326                 hw_read:   watch read, 
10327                 hw_access: watch access (read or write) */
10328 static void
10329 watch_command_1 (char *arg, int accessflag, int from_tty,
10330                  int just_location, int internal)
10331 {
10332   volatile struct gdb_exception e;
10333   struct breakpoint *b, *scope_breakpoint = NULL;
10334   struct expression *exp;
10335   struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10336   struct value *val, *mark, *result;
10337   struct frame_info *frame;
10338   char *exp_start = NULL;
10339   char *exp_end = NULL;
10340   char *tok, *end_tok;
10341   int toklen = -1;
10342   char *cond_start = NULL;
10343   char *cond_end = NULL;
10344   enum bptype bp_type;
10345   int thread = -1;
10346   int pc = 0;
10347   /* Flag to indicate whether we are going to use masks for
10348      the hardware watchpoint.  */
10349   int use_mask = 0;
10350   CORE_ADDR mask = 0;
10351   struct watchpoint *w;
10352
10353   /* Make sure that we actually have parameters to parse.  */
10354   if (arg != NULL && arg[0] != '\0')
10355     {
10356       char *value_start;
10357
10358       /* Look for "parameter value" pairs at the end
10359          of the arguments string.  */
10360       for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10361         {
10362           /* Skip whitespace at the end of the argument list.  */
10363           while (tok > arg && (*tok == ' ' || *tok == '\t'))
10364             tok--;
10365
10366           /* Find the beginning of the last token.
10367              This is the value of the parameter.  */
10368           while (tok > arg && (*tok != ' ' && *tok != '\t'))
10369             tok--;
10370           value_start = tok + 1;
10371
10372           /* Skip whitespace.  */
10373           while (tok > arg && (*tok == ' ' || *tok == '\t'))
10374             tok--;
10375
10376           end_tok = tok;
10377
10378           /* Find the beginning of the second to last token.
10379              This is the parameter itself.  */
10380           while (tok > arg && (*tok != ' ' && *tok != '\t'))
10381             tok--;
10382           tok++;
10383           toklen = end_tok - tok + 1;
10384
10385           if (toklen == 6 && !strncmp (tok, "thread", 6))
10386             {
10387               /* At this point we've found a "thread" token, which means
10388                  the user is trying to set a watchpoint that triggers
10389                  only in a specific thread.  */
10390               char *endp;
10391
10392               if (thread != -1)
10393                 error(_("You can specify only one thread."));
10394
10395               /* Extract the thread ID from the next token.  */
10396               thread = strtol (value_start, &endp, 0);
10397
10398               /* Check if the user provided a valid numeric value for the
10399                  thread ID.  */
10400               if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10401                 error (_("Invalid thread ID specification %s."), value_start);
10402
10403               /* Check if the thread actually exists.  */
10404               if (!valid_thread_id (thread))
10405                 error (_("Unknown thread %d."), thread);
10406             }
10407           else if (toklen == 4 && !strncmp (tok, "mask", 4))
10408             {
10409               /* We've found a "mask" token, which means the user wants to
10410                  create a hardware watchpoint that is going to have the mask
10411                  facility.  */
10412               struct value *mask_value, *mark;
10413
10414               if (use_mask)
10415                 error(_("You can specify only one mask."));
10416
10417               use_mask = just_location = 1;
10418
10419               mark = value_mark ();
10420               mask_value = parse_to_comma_and_eval (&value_start);
10421               mask = value_as_address (mask_value);
10422               value_free_to_mark (mark);
10423             }
10424           else
10425             /* We didn't recognize what we found.  We should stop here.  */
10426             break;
10427
10428           /* Truncate the string and get rid of the "parameter value" pair before
10429              the arguments string is parsed by the parse_exp_1 function.  */
10430           *tok = '\0';
10431         }
10432     }
10433
10434   /* Parse the rest of the arguments.  */
10435   innermost_block = NULL;
10436   exp_start = arg;
10437   exp = parse_exp_1 (&arg, 0, 0);
10438   exp_end = arg;
10439   /* Remove trailing whitespace from the expression before saving it.
10440      This makes the eventual display of the expression string a bit
10441      prettier.  */
10442   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10443     --exp_end;
10444
10445   /* Checking if the expression is not constant.  */
10446   if (watchpoint_exp_is_const (exp))
10447     {
10448       int len;
10449
10450       len = exp_end - exp_start;
10451       while (len > 0 && isspace (exp_start[len - 1]))
10452         len--;
10453       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10454     }
10455
10456   exp_valid_block = innermost_block;
10457   mark = value_mark ();
10458   fetch_subexp_value (exp, &pc, &val, &result, NULL);
10459
10460   if (just_location)
10461     {
10462       int ret;
10463
10464       exp_valid_block = NULL;
10465       val = value_addr (result);
10466       release_value (val);
10467       value_free_to_mark (mark);
10468
10469       if (use_mask)
10470         {
10471           ret = target_masked_watch_num_registers (value_as_address (val),
10472                                                    mask);
10473           if (ret == -1)
10474             error (_("This target does not support masked watchpoints."));
10475           else if (ret == -2)
10476             error (_("Invalid mask or memory region."));
10477         }
10478     }
10479   else if (val != NULL)
10480     release_value (val);
10481
10482   tok = skip_spaces (arg);
10483   end_tok = skip_to_space (tok);
10484
10485   toklen = end_tok - tok;
10486   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10487     {
10488       struct expression *cond;
10489
10490       innermost_block = NULL;
10491       tok = cond_start = end_tok + 1;
10492       cond = parse_exp_1 (&tok, 0, 0);
10493
10494       /* The watchpoint expression may not be local, but the condition
10495          may still be.  E.g.: `watch global if local > 0'.  */
10496       cond_exp_valid_block = innermost_block;
10497
10498       xfree (cond);
10499       cond_end = tok;
10500     }
10501   if (*tok)
10502     error (_("Junk at end of command."));
10503
10504   if (accessflag == hw_read)
10505     bp_type = bp_read_watchpoint;
10506   else if (accessflag == hw_access)
10507     bp_type = bp_access_watchpoint;
10508   else
10509     bp_type = bp_hardware_watchpoint;
10510
10511   frame = block_innermost_frame (exp_valid_block);
10512
10513   /* If the expression is "local", then set up a "watchpoint scope"
10514      breakpoint at the point where we've left the scope of the watchpoint
10515      expression.  Create the scope breakpoint before the watchpoint, so
10516      that we will encounter it first in bpstat_stop_status.  */
10517   if (exp_valid_block && frame)
10518     {
10519       if (frame_id_p (frame_unwind_caller_id (frame)))
10520         {
10521           scope_breakpoint
10522             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10523                                           frame_unwind_caller_pc (frame),
10524                                           bp_watchpoint_scope,
10525                                           &momentary_breakpoint_ops);
10526
10527           scope_breakpoint->enable_state = bp_enabled;
10528
10529           /* Automatically delete the breakpoint when it hits.  */
10530           scope_breakpoint->disposition = disp_del;
10531
10532           /* Only break in the proper frame (help with recursion).  */
10533           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
10534
10535           /* Set the address at which we will stop.  */
10536           scope_breakpoint->loc->gdbarch
10537             = frame_unwind_caller_arch (frame);
10538           scope_breakpoint->loc->requested_address
10539             = frame_unwind_caller_pc (frame);
10540           scope_breakpoint->loc->address
10541             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10542                                          scope_breakpoint->loc->requested_address,
10543                                          scope_breakpoint->type);
10544         }
10545     }
10546
10547   /* Now set up the breakpoint.  */
10548
10549   w = XCNEW (struct watchpoint);
10550   b = &w->base;
10551   if (use_mask)
10552     init_raw_breakpoint_without_location (b, NULL, bp_type,
10553                                           &masked_watchpoint_breakpoint_ops);
10554   else
10555     init_raw_breakpoint_without_location (b, NULL, bp_type,
10556                                           &watchpoint_breakpoint_ops);
10557   b->thread = thread;
10558   b->disposition = disp_donttouch;
10559   b->pspace = current_program_space;
10560   w->exp = exp;
10561   w->exp_valid_block = exp_valid_block;
10562   w->cond_exp_valid_block = cond_exp_valid_block;
10563   if (just_location)
10564     {
10565       struct type *t = value_type (val);
10566       CORE_ADDR addr = value_as_address (val);
10567       char *name;
10568
10569       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10570       name = type_to_string (t);
10571
10572       w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
10573                                           core_addr_to_string (addr));
10574       xfree (name);
10575
10576       w->exp_string = xstrprintf ("-location %.*s",
10577                                   (int) (exp_end - exp_start), exp_start);
10578
10579       /* The above expression is in C.  */
10580       b->language = language_c;
10581     }
10582   else
10583     w->exp_string = savestring (exp_start, exp_end - exp_start);
10584
10585   if (use_mask)
10586     {
10587       w->hw_wp_mask = mask;
10588     }
10589   else
10590     {
10591       w->val = val;
10592       w->val_valid = 1;
10593     }
10594
10595   if (cond_start)
10596     b->cond_string = savestring (cond_start, cond_end - cond_start);
10597   else
10598     b->cond_string = 0;
10599
10600   if (frame)
10601     {
10602       w->watchpoint_frame = get_frame_id (frame);
10603       w->watchpoint_thread = inferior_ptid;
10604     }
10605   else
10606     {
10607       w->watchpoint_frame = null_frame_id;
10608       w->watchpoint_thread = null_ptid;
10609     }
10610
10611   if (scope_breakpoint != NULL)
10612     {
10613       /* The scope breakpoint is related to the watchpoint.  We will
10614          need to act on them together.  */
10615       b->related_breakpoint = scope_breakpoint;
10616       scope_breakpoint->related_breakpoint = b;
10617     }
10618
10619   if (!just_location)
10620     value_free_to_mark (mark);
10621
10622   TRY_CATCH (e, RETURN_MASK_ALL)
10623     {
10624       /* Finally update the new watchpoint.  This creates the locations
10625          that should be inserted.  */
10626       update_watchpoint (w, 1);
10627     }
10628   if (e.reason < 0)
10629     {
10630       delete_breakpoint (b);
10631       throw_exception (e);
10632     }
10633
10634   install_breakpoint (internal, b, 1);
10635 }
10636
10637 /* Return count of debug registers needed to watch the given expression.
10638    If the watchpoint cannot be handled in hardware return zero.  */
10639
10640 static int
10641 can_use_hardware_watchpoint (struct value *v)
10642 {
10643   int found_memory_cnt = 0;
10644   struct value *head = v;
10645
10646   /* Did the user specifically forbid us to use hardware watchpoints? */
10647   if (!can_use_hw_watchpoints)
10648     return 0;
10649
10650   /* Make sure that the value of the expression depends only upon
10651      memory contents, and values computed from them within GDB.  If we
10652      find any register references or function calls, we can't use a
10653      hardware watchpoint.
10654
10655      The idea here is that evaluating an expression generates a series
10656      of values, one holding the value of every subexpression.  (The
10657      expression a*b+c has five subexpressions: a, b, a*b, c, and
10658      a*b+c.)  GDB's values hold almost enough information to establish
10659      the criteria given above --- they identify memory lvalues,
10660      register lvalues, computed values, etcetera.  So we can evaluate
10661      the expression, and then scan the chain of values that leaves
10662      behind to decide whether we can detect any possible change to the
10663      expression's final value using only hardware watchpoints.
10664
10665      However, I don't think that the values returned by inferior
10666      function calls are special in any way.  So this function may not
10667      notice that an expression involving an inferior function call
10668      can't be watched with hardware watchpoints.  FIXME.  */
10669   for (; v; v = value_next (v))
10670     {
10671       if (VALUE_LVAL (v) == lval_memory)
10672         {
10673           if (v != head && value_lazy (v))
10674             /* A lazy memory lvalue in the chain is one that GDB never
10675                needed to fetch; we either just used its address (e.g.,
10676                `a' in `a.b') or we never needed it at all (e.g., `a'
10677                in `a,b').  This doesn't apply to HEAD; if that is
10678                lazy then it was not readable, but watch it anyway.  */
10679             ;
10680           else
10681             {
10682               /* Ahh, memory we actually used!  Check if we can cover
10683                  it with hardware watchpoints.  */
10684               struct type *vtype = check_typedef (value_type (v));
10685
10686               /* We only watch structs and arrays if user asked for it
10687                  explicitly, never if they just happen to appear in a
10688                  middle of some value chain.  */
10689               if (v == head
10690                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10691                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10692                 {
10693                   CORE_ADDR vaddr = value_address (v);
10694                   int len;
10695                   int num_regs;
10696
10697                   len = (target_exact_watchpoints
10698                          && is_scalar_type_recursive (vtype))?
10699                     1 : TYPE_LENGTH (value_type (v));
10700
10701                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10702                   if (!num_regs)
10703                     return 0;
10704                   else
10705                     found_memory_cnt += num_regs;
10706                 }
10707             }
10708         }
10709       else if (VALUE_LVAL (v) != not_lval
10710                && deprecated_value_modifiable (v) == 0)
10711         return 0;       /* These are values from the history (e.g., $1).  */
10712       else if (VALUE_LVAL (v) == lval_register)
10713         return 0;       /* Cannot watch a register with a HW watchpoint.  */
10714     }
10715
10716   /* The expression itself looks suitable for using a hardware
10717      watchpoint, but give the target machine a chance to reject it.  */
10718   return found_memory_cnt;
10719 }
10720
10721 void
10722 watch_command_wrapper (char *arg, int from_tty, int internal)
10723 {
10724   watch_command_1 (arg, hw_write, from_tty, 0, internal);
10725 }
10726
10727 /* A helper function that looks for an argument at the start of a
10728    string.  The argument must also either be at the end of the string,
10729    or be followed by whitespace.  Returns 1 if it finds the argument,
10730    0 otherwise.  If the argument is found, it updates *STR.  */
10731
10732 static int
10733 check_for_argument (char **str, char *arg, int arg_len)
10734 {
10735   if (strncmp (*str, arg, arg_len) == 0
10736       && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10737     {
10738       *str += arg_len;
10739       return 1;
10740     }
10741   return 0;
10742 }
10743
10744 /* A helper function that looks for the "-location" argument and then
10745    calls watch_command_1.  */
10746
10747 static void
10748 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10749 {
10750   int just_location = 0;
10751
10752   if (arg
10753       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10754           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10755     {
10756       arg = skip_spaces (arg);
10757       just_location = 1;
10758     }
10759
10760   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10761 }
10762
10763 static void
10764 watch_command (char *arg, int from_tty)
10765 {
10766   watch_maybe_just_location (arg, hw_write, from_tty);
10767 }
10768
10769 void
10770 rwatch_command_wrapper (char *arg, int from_tty, int internal)
10771 {
10772   watch_command_1 (arg, hw_read, from_tty, 0, internal);
10773 }
10774
10775 static void
10776 rwatch_command (char *arg, int from_tty)
10777 {
10778   watch_maybe_just_location (arg, hw_read, from_tty);
10779 }
10780
10781 void
10782 awatch_command_wrapper (char *arg, int from_tty, int internal)
10783 {
10784   watch_command_1 (arg, hw_access, from_tty, 0, internal);
10785 }
10786
10787 static void
10788 awatch_command (char *arg, int from_tty)
10789 {
10790   watch_maybe_just_location (arg, hw_access, from_tty);
10791 }
10792 \f
10793
10794 /* Helper routines for the until_command routine in infcmd.c.  Here
10795    because it uses the mechanisms of breakpoints.  */
10796
10797 struct until_break_command_continuation_args
10798 {
10799   struct breakpoint *breakpoint;
10800   struct breakpoint *breakpoint2;
10801   int thread_num;
10802 };
10803
10804 /* This function is called by fetch_inferior_event via the
10805    cmd_continuation pointer, to complete the until command.  It takes
10806    care of cleaning up the temporary breakpoints set up by the until
10807    command.  */
10808 static void
10809 until_break_command_continuation (void *arg, int err)
10810 {
10811   struct until_break_command_continuation_args *a = arg;
10812
10813   delete_breakpoint (a->breakpoint);
10814   if (a->breakpoint2)
10815     delete_breakpoint (a->breakpoint2);
10816   delete_longjmp_breakpoint (a->thread_num);
10817 }
10818
10819 void
10820 until_break_command (char *arg, int from_tty, int anywhere)
10821 {
10822   struct symtabs_and_lines sals;
10823   struct symtab_and_line sal;
10824   struct frame_info *frame = get_selected_frame (NULL);
10825   struct gdbarch *frame_gdbarch = get_frame_arch (frame);
10826   struct frame_id stack_frame_id = get_stack_frame_id (frame);
10827   struct frame_id caller_frame_id = frame_unwind_caller_id (frame);
10828   struct breakpoint *breakpoint;
10829   struct breakpoint *breakpoint2 = NULL;
10830   struct cleanup *old_chain;
10831   int thread;
10832   struct thread_info *tp;
10833
10834   clear_proceed_status ();
10835
10836   /* Set a breakpoint where the user wants it and at return from
10837      this function.  */
10838
10839   if (last_displayed_sal_is_valid ())
10840     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10841                           get_last_displayed_symtab (),
10842                           get_last_displayed_line ());
10843   else
10844     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10845                           (struct symtab *) NULL, 0);
10846
10847   if (sals.nelts != 1)
10848     error (_("Couldn't get information on specified line."));
10849
10850   sal = sals.sals[0];
10851   xfree (sals.sals);    /* malloc'd, so freed.  */
10852
10853   if (*arg)
10854     error (_("Junk at end of arguments."));
10855
10856   resolve_sal_pc (&sal);
10857
10858   tp = inferior_thread ();
10859   thread = tp->num;
10860
10861   old_chain = make_cleanup (null_cleanup, NULL);
10862
10863   /* Installing a breakpoint invalidates the frame chain (as it may
10864      need to switch threads), so do any frame handling first.  */
10865
10866   /* Keep within the current frame, or in frames called by the current
10867      one.  */
10868
10869   if (frame_id_p (caller_frame_id))
10870     {
10871       struct symtab_and_line sal2;
10872
10873       sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10874       sal2.pc = frame_unwind_caller_pc (frame);
10875       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
10876                                               sal2,
10877                                               caller_frame_id,
10878                                               bp_until);
10879       make_cleanup_delete_breakpoint (breakpoint2);
10880
10881       set_longjmp_breakpoint (tp, caller_frame_id);
10882       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
10883     }
10884
10885   /* set_momentary_breakpoint could invalidate FRAME.  */
10886   frame = NULL;
10887
10888   if (anywhere)
10889     /* If the user told us to continue until a specified location,
10890        we don't specify a frame at which we need to stop.  */
10891     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10892                                            null_frame_id, bp_until);
10893   else
10894     /* Otherwise, specify the selected frame, because we want to stop
10895        only at the very same frame.  */
10896     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10897                                            stack_frame_id, bp_until);
10898   make_cleanup_delete_breakpoint (breakpoint);
10899
10900   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
10901
10902   /* If we are running asynchronously, and proceed call above has
10903      actually managed to start the target, arrange for breakpoints to
10904      be deleted when the target stops.  Otherwise, we're already
10905      stopped and delete breakpoints via cleanup chain.  */
10906
10907   if (target_can_async_p () && is_running (inferior_ptid))
10908     {
10909       struct until_break_command_continuation_args *args;
10910       args = xmalloc (sizeof (*args));
10911
10912       args->breakpoint = breakpoint;
10913       args->breakpoint2 = breakpoint2;
10914       args->thread_num = thread;
10915
10916       discard_cleanups (old_chain);
10917       add_continuation (inferior_thread (),
10918                         until_break_command_continuation, args,
10919                         xfree);
10920     }
10921   else
10922     do_cleanups (old_chain);
10923 }
10924
10925 /* This function attempts to parse an optional "if <cond>" clause
10926    from the arg string.  If one is not found, it returns NULL.
10927
10928    Else, it returns a pointer to the condition string.  (It does not
10929    attempt to evaluate the string against a particular block.)  And,
10930    it updates arg to point to the first character following the parsed
10931    if clause in the arg string.  */
10932
10933 static char *
10934 ep_parse_optional_if_clause (char **arg)
10935 {
10936   char *cond_string;
10937
10938   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
10939     return NULL;
10940
10941   /* Skip the "if" keyword.  */
10942   (*arg) += 2;
10943
10944   /* Skip any extra leading whitespace, and record the start of the
10945      condition string.  */
10946   *arg = skip_spaces (*arg);
10947   cond_string = *arg;
10948
10949   /* Assume that the condition occupies the remainder of the arg
10950      string.  */
10951   (*arg) += strlen (cond_string);
10952
10953   return cond_string;
10954 }
10955
10956 /* Commands to deal with catching events, such as signals, exceptions,
10957    process start/exit, etc.  */
10958
10959 typedef enum
10960 {
10961   catch_fork_temporary, catch_vfork_temporary,
10962   catch_fork_permanent, catch_vfork_permanent
10963 }
10964 catch_fork_kind;
10965
10966 static void
10967 catch_fork_command_1 (char *arg, int from_tty, 
10968                       struct cmd_list_element *command)
10969 {
10970   struct gdbarch *gdbarch = get_current_arch ();
10971   char *cond_string = NULL;
10972   catch_fork_kind fork_kind;
10973   int tempflag;
10974
10975   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
10976   tempflag = (fork_kind == catch_fork_temporary
10977               || fork_kind == catch_vfork_temporary);
10978
10979   if (!arg)
10980     arg = "";
10981   arg = skip_spaces (arg);
10982
10983   /* The allowed syntax is:
10984      catch [v]fork
10985      catch [v]fork if <cond>
10986
10987      First, check if there's an if clause.  */
10988   cond_string = ep_parse_optional_if_clause (&arg);
10989
10990   if ((*arg != '\0') && !isspace (*arg))
10991     error (_("Junk at end of arguments."));
10992
10993   /* If this target supports it, create a fork or vfork catchpoint
10994      and enable reporting of such events.  */
10995   switch (fork_kind)
10996     {
10997     case catch_fork_temporary:
10998     case catch_fork_permanent:
10999       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11000                                           &catch_fork_breakpoint_ops);
11001       break;
11002     case catch_vfork_temporary:
11003     case catch_vfork_permanent:
11004       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11005                                           &catch_vfork_breakpoint_ops);
11006       break;
11007     default:
11008       error (_("unsupported or unknown fork kind; cannot catch it"));
11009       break;
11010     }
11011 }
11012
11013 static void
11014 catch_exec_command_1 (char *arg, int from_tty, 
11015                       struct cmd_list_element *command)
11016 {
11017   struct exec_catchpoint *c;
11018   struct gdbarch *gdbarch = get_current_arch ();
11019   int tempflag;
11020   char *cond_string = NULL;
11021
11022   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11023
11024   if (!arg)
11025     arg = "";
11026   arg = skip_spaces (arg);
11027
11028   /* The allowed syntax is:
11029      catch exec
11030      catch exec if <cond>
11031
11032      First, check if there's an if clause.  */
11033   cond_string = ep_parse_optional_if_clause (&arg);
11034
11035   if ((*arg != '\0') && !isspace (*arg))
11036     error (_("Junk at end of arguments."));
11037
11038   c = XNEW (struct exec_catchpoint);
11039   init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11040                    &catch_exec_breakpoint_ops);
11041   c->exec_pathname = NULL;
11042
11043   install_breakpoint (0, &c->base, 1);
11044 }
11045
11046 static enum print_stop_action
11047 print_it_exception_catchpoint (bpstat bs)
11048 {
11049   struct ui_out *uiout = current_uiout;
11050   struct breakpoint *b = bs->breakpoint_at;
11051   int bp_temp, bp_throw;
11052
11053   annotate_catchpoint (b->number);
11054
11055   bp_throw = strstr (b->addr_string, "throw") != NULL;
11056   if (b->loc->address != b->loc->requested_address)
11057     breakpoint_adjustment_warning (b->loc->requested_address,
11058                                    b->loc->address,
11059                                    b->number, 1);
11060   bp_temp = b->disposition == disp_del;
11061   ui_out_text (uiout, 
11062                bp_temp ? "Temporary catchpoint "
11063                        : "Catchpoint ");
11064   if (!ui_out_is_mi_like_p (uiout))
11065     ui_out_field_int (uiout, "bkptno", b->number);
11066   ui_out_text (uiout,
11067                bp_throw ? " (exception thrown), "
11068                         : " (exception caught), ");
11069   if (ui_out_is_mi_like_p (uiout))
11070     {
11071       ui_out_field_string (uiout, "reason", 
11072                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11073       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11074       ui_out_field_int (uiout, "bkptno", b->number);
11075     }
11076   return PRINT_SRC_AND_LOC;
11077 }
11078
11079 static void
11080 print_one_exception_catchpoint (struct breakpoint *b, 
11081                                 struct bp_location **last_loc)
11082 {
11083   struct value_print_options opts;
11084   struct ui_out *uiout = current_uiout;
11085
11086   get_user_print_options (&opts);
11087   if (opts.addressprint)
11088     {
11089       annotate_field (4);
11090       if (b->loc == NULL || b->loc->shlib_disabled)
11091         ui_out_field_string (uiout, "addr", "<PENDING>");
11092       else
11093         ui_out_field_core_addr (uiout, "addr",
11094                                 b->loc->gdbarch, b->loc->address);
11095     }
11096   annotate_field (5);
11097   if (b->loc)
11098     *last_loc = b->loc;
11099   if (strstr (b->addr_string, "throw") != NULL)
11100     ui_out_field_string (uiout, "what", "exception throw");
11101   else
11102     ui_out_field_string (uiout, "what", "exception catch");
11103 }
11104
11105 static void
11106 print_mention_exception_catchpoint (struct breakpoint *b)
11107 {
11108   struct ui_out *uiout = current_uiout;
11109   int bp_temp;
11110   int bp_throw;
11111
11112   bp_temp = b->disposition == disp_del;
11113   bp_throw = strstr (b->addr_string, "throw") != NULL;
11114   ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11115                               : _("Catchpoint "));
11116   ui_out_field_int (uiout, "bkptno", b->number);
11117   ui_out_text (uiout, bp_throw ? _(" (throw)")
11118                                : _(" (catch)"));
11119 }
11120
11121 /* Implement the "print_recreate" breakpoint_ops method for throw and
11122    catch catchpoints.  */
11123
11124 static void
11125 print_recreate_exception_catchpoint (struct breakpoint *b, 
11126                                      struct ui_file *fp)
11127 {
11128   int bp_temp;
11129   int bp_throw;
11130
11131   bp_temp = b->disposition == disp_del;
11132   bp_throw = strstr (b->addr_string, "throw") != NULL;
11133   fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11134   fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
11135   print_recreate_thread (b, fp);
11136 }
11137
11138 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
11139
11140 static int
11141 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11142                           enum exception_event_kind ex_event, int from_tty)
11143 {
11144   char *trigger_func_name;
11145  
11146   if (ex_event == EX_EVENT_CATCH)
11147     trigger_func_name = "__cxa_begin_catch";
11148   else
11149     trigger_func_name = "__cxa_throw";
11150
11151   create_breakpoint (get_current_arch (),
11152                      trigger_func_name, cond_string, -1, NULL,
11153                      0 /* condition and thread are valid.  */,
11154                      tempflag, bp_breakpoint,
11155                      0,
11156                      AUTO_BOOLEAN_TRUE /* pending */,
11157                      &gnu_v3_exception_catchpoint_ops, from_tty,
11158                      1 /* enabled */,
11159                      0 /* internal */,
11160                      0);
11161
11162   return 1;
11163 }
11164
11165 /* Deal with "catch catch" and "catch throw" commands.  */
11166
11167 static void
11168 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11169                            int tempflag, int from_tty)
11170 {
11171   char *cond_string = NULL;
11172
11173   if (!arg)
11174     arg = "";
11175   arg = skip_spaces (arg);
11176
11177   cond_string = ep_parse_optional_if_clause (&arg);
11178
11179   if ((*arg != '\0') && !isspace (*arg))
11180     error (_("Junk at end of arguments."));
11181
11182   if (ex_event != EX_EVENT_THROW
11183       && ex_event != EX_EVENT_CATCH)
11184     error (_("Unsupported or unknown exception event; cannot catch it"));
11185
11186   if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11187     return;
11188
11189   warning (_("Unsupported with this platform/compiler combination."));
11190 }
11191
11192 /* Implementation of "catch catch" command.  */
11193
11194 static void
11195 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11196 {
11197   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11198
11199   catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11200 }
11201
11202 /* Implementation of "catch throw" command.  */
11203
11204 static void
11205 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11206 {
11207   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11208
11209   catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11210 }
11211
11212 void
11213 init_ada_exception_breakpoint (struct breakpoint *b,
11214                                struct gdbarch *gdbarch,
11215                                struct symtab_and_line sal,
11216                                char *addr_string,
11217                                const struct breakpoint_ops *ops,
11218                                int tempflag,
11219                                int from_tty)
11220 {
11221   if (from_tty)
11222     {
11223       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11224       if (!loc_gdbarch)
11225         loc_gdbarch = gdbarch;
11226
11227       describe_other_breakpoints (loc_gdbarch,
11228                                   sal.pspace, sal.pc, sal.section, -1);
11229       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11230          version for exception catchpoints, because two catchpoints
11231          used for different exception names will use the same address.
11232          In this case, a "breakpoint ... also set at..." warning is
11233          unproductive.  Besides, the warning phrasing is also a bit
11234          inappropriate, we should use the word catchpoint, and tell
11235          the user what type of catchpoint it is.  The above is good
11236          enough for now, though.  */
11237     }
11238
11239   init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11240
11241   b->enable_state = bp_enabled;
11242   b->disposition = tempflag ? disp_del : disp_donttouch;
11243   b->addr_string = addr_string;
11244   b->language = language_ada;
11245 }
11246
11247 /* Splits the argument using space as delimiter.  Returns an xmalloc'd
11248    filter list, or NULL if no filtering is required.  */
11249 static VEC(int) *
11250 catch_syscall_split_args (char *arg)
11251 {
11252   VEC(int) *result = NULL;
11253   struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
11254
11255   while (*arg != '\0')
11256     {
11257       int i, syscall_number;
11258       char *endptr;
11259       char cur_name[128];
11260       struct syscall s;
11261
11262       /* Skip whitespace.  */
11263       while (isspace (*arg))
11264         arg++;
11265
11266       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11267         cur_name[i] = arg[i];
11268       cur_name[i] = '\0';
11269       arg += i;
11270
11271       /* Check if the user provided a syscall name or a number.  */
11272       syscall_number = (int) strtol (cur_name, &endptr, 0);
11273       if (*endptr == '\0')
11274         get_syscall_by_number (syscall_number, &s);
11275       else
11276         {
11277           /* We have a name.  Let's check if it's valid and convert it
11278              to a number.  */
11279           get_syscall_by_name (cur_name, &s);
11280
11281           if (s.number == UNKNOWN_SYSCALL)
11282             /* Here we have to issue an error instead of a warning,
11283                because GDB cannot do anything useful if there's no
11284                syscall number to be caught.  */
11285             error (_("Unknown syscall name '%s'."), cur_name);
11286         }
11287
11288       /* Ok, it's valid.  */
11289       VEC_safe_push (int, result, s.number);
11290     }
11291
11292   discard_cleanups (cleanup);
11293   return result;
11294 }
11295
11296 /* Implement the "catch syscall" command.  */
11297
11298 static void
11299 catch_syscall_command_1 (char *arg, int from_tty, 
11300                          struct cmd_list_element *command)
11301 {
11302   int tempflag;
11303   VEC(int) *filter;
11304   struct syscall s;
11305   struct gdbarch *gdbarch = get_current_arch ();
11306
11307   /* Checking if the feature if supported.  */
11308   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11309     error (_("The feature 'catch syscall' is not supported on \
11310 this architecture yet."));
11311
11312   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11313
11314   arg = skip_spaces (arg);
11315
11316   /* We need to do this first "dummy" translation in order
11317      to get the syscall XML file loaded or, most important,
11318      to display a warning to the user if there's no XML file
11319      for his/her architecture.  */
11320   get_syscall_by_number (0, &s);
11321
11322   /* The allowed syntax is:
11323      catch syscall
11324      catch syscall <name | number> [<name | number> ... <name | number>]
11325
11326      Let's check if there's a syscall name.  */
11327
11328   if (arg != NULL)
11329     filter = catch_syscall_split_args (arg);
11330   else
11331     filter = NULL;
11332
11333   create_syscall_event_catchpoint (tempflag, filter,
11334                                    &catch_syscall_breakpoint_ops);
11335 }
11336
11337 static void
11338 catch_command (char *arg, int from_tty)
11339 {
11340   error (_("Catch requires an event name."));
11341 }
11342 \f
11343
11344 static void
11345 tcatch_command (char *arg, int from_tty)
11346 {
11347   error (_("Catch requires an event name."));
11348 }
11349
11350 /* A qsort comparison function that sorts breakpoints in order.  */
11351
11352 static int
11353 compare_breakpoints (const void *a, const void *b)
11354 {
11355   const breakpoint_p *ba = a;
11356   uintptr_t ua = (uintptr_t) *ba;
11357   const breakpoint_p *bb = b;
11358   uintptr_t ub = (uintptr_t) *bb;
11359
11360   if ((*ba)->number < (*bb)->number)
11361     return -1;
11362   else if ((*ba)->number > (*bb)->number)
11363     return 1;
11364
11365   /* Now sort by address, in case we see, e..g, two breakpoints with
11366      the number 0.  */
11367   if (ua < ub)
11368     return -1;
11369   return ub > ub ? 1 : 0;
11370 }
11371
11372 /* Delete breakpoints by address or line.  */
11373
11374 static void
11375 clear_command (char *arg, int from_tty)
11376 {
11377   struct breakpoint *b, *prev;
11378   VEC(breakpoint_p) *found = 0;
11379   int ix;
11380   int default_match;
11381   struct symtabs_and_lines sals;
11382   struct symtab_and_line sal;
11383   int i;
11384   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11385
11386   if (arg)
11387     {
11388       sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
11389                                      | DECODE_LINE_LIST_MODE));
11390       default_match = 0;
11391     }
11392   else
11393     {
11394       sals.sals = (struct symtab_and_line *)
11395         xmalloc (sizeof (struct symtab_and_line));
11396       make_cleanup (xfree, sals.sals);
11397       init_sal (&sal);          /* Initialize to zeroes.  */
11398
11399       /* Set sal's line, symtab, pc, and pspace to the values
11400          corresponding to the last call to print_frame_info.  If the
11401          codepoint is not valid, this will set all the fields to 0.  */
11402       get_last_displayed_sal (&sal);
11403       if (sal.symtab == 0)
11404         error (_("No source file specified."));
11405
11406       sals.sals[0] = sal;
11407       sals.nelts = 1;
11408
11409       default_match = 1;
11410     }
11411
11412   /* We don't call resolve_sal_pc here.  That's not as bad as it
11413      seems, because all existing breakpoints typically have both
11414      file/line and pc set.  So, if clear is given file/line, we can
11415      match this to existing breakpoint without obtaining pc at all.
11416
11417      We only support clearing given the address explicitly 
11418      present in breakpoint table.  Say, we've set breakpoint 
11419      at file:line.  There were several PC values for that file:line,
11420      due to optimization, all in one block.
11421
11422      We've picked one PC value.  If "clear" is issued with another
11423      PC corresponding to the same file:line, the breakpoint won't
11424      be cleared.  We probably can still clear the breakpoint, but 
11425      since the other PC value is never presented to user, user
11426      can only find it by guessing, and it does not seem important
11427      to support that.  */
11428
11429   /* For each line spec given, delete bps which correspond to it.  Do
11430      it in two passes, solely to preserve the current behavior that
11431      from_tty is forced true if we delete more than one
11432      breakpoint.  */
11433
11434   found = NULL;
11435   make_cleanup (VEC_cleanup (breakpoint_p), &found);
11436   for (i = 0; i < sals.nelts; i++)
11437     {
11438       int is_abs, sal_name_len;
11439
11440       /* If exact pc given, clear bpts at that pc.
11441          If line given (pc == 0), clear all bpts on specified line.
11442          If defaulting, clear all bpts on default line
11443          or at default pc.
11444
11445          defaulting    sal.pc != 0    tests to do
11446
11447          0              1             pc
11448          1              1             pc _and_ line
11449          0              0             line
11450          1              0             <can't happen> */
11451
11452       sal = sals.sals[i];
11453       is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11454       sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
11455
11456       /* Find all matching breakpoints and add them to 'found'.  */
11457       ALL_BREAKPOINTS (b)
11458         {
11459           int match = 0;
11460           /* Are we going to delete b?  */
11461           if (b->type != bp_none && !is_watchpoint (b))
11462             {
11463               struct bp_location *loc = b->loc;
11464               for (; loc; loc = loc->next)
11465                 {
11466                   /* If the user specified file:line, don't allow a PC
11467                      match.  This matches historical gdb behavior.  */
11468                   int pc_match = (!sal.explicit_line
11469                                   && sal.pc
11470                                   && (loc->pspace == sal.pspace)
11471                                   && (loc->address == sal.pc)
11472                                   && (!section_is_overlay (loc->section)
11473                                       || loc->section == sal.section));
11474                   int line_match = 0;
11475
11476                   if ((default_match || sal.explicit_line)
11477                       && loc->source_file != NULL
11478                       && sal.symtab != NULL
11479                       && sal.pspace == loc->pspace
11480                       && loc->line_number == sal.line)
11481                     {
11482                       if (filename_cmp (loc->source_file,
11483                                         sal.symtab->filename) == 0)
11484                         line_match = 1;
11485                       else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11486                                && compare_filenames_for_search (loc->source_file,
11487                                                                 sal.symtab->filename,
11488                                                                 sal_name_len))
11489                         line_match = 1;
11490                     }
11491
11492                   if (pc_match || line_match)
11493                     {
11494                       match = 1;
11495                       break;
11496                     }
11497                 }
11498             }
11499
11500           if (match)
11501             VEC_safe_push(breakpoint_p, found, b);
11502         }
11503     }
11504
11505   /* Now go thru the 'found' chain and delete them.  */
11506   if (VEC_empty(breakpoint_p, found))
11507     {
11508       if (arg)
11509         error (_("No breakpoint at %s."), arg);
11510       else
11511         error (_("No breakpoint at this line."));
11512     }
11513
11514   /* Remove duplicates from the vec.  */
11515   qsort (VEC_address (breakpoint_p, found),
11516          VEC_length (breakpoint_p, found),
11517          sizeof (breakpoint_p),
11518          compare_breakpoints);
11519   prev = VEC_index (breakpoint_p, found, 0);
11520   for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11521     {
11522       if (b == prev)
11523         {
11524           VEC_ordered_remove (breakpoint_p, found, ix);
11525           --ix;
11526         }
11527     }
11528
11529   if (VEC_length(breakpoint_p, found) > 1)
11530     from_tty = 1;       /* Always report if deleted more than one.  */
11531   if (from_tty)
11532     {
11533       if (VEC_length(breakpoint_p, found) == 1)
11534         printf_unfiltered (_("Deleted breakpoint "));
11535       else
11536         printf_unfiltered (_("Deleted breakpoints "));
11537     }
11538   breakpoints_changed ();
11539
11540   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11541     {
11542       if (from_tty)
11543         printf_unfiltered ("%d ", b->number);
11544       delete_breakpoint (b);
11545     }
11546   if (from_tty)
11547     putchar_unfiltered ('\n');
11548
11549   do_cleanups (cleanups);
11550 }
11551 \f
11552 /* Delete breakpoint in BS if they are `delete' breakpoints and
11553    all breakpoints that are marked for deletion, whether hit or not.
11554    This is called after any breakpoint is hit, or after errors.  */
11555
11556 void
11557 breakpoint_auto_delete (bpstat bs)
11558 {
11559   struct breakpoint *b, *b_tmp;
11560
11561   for (; bs; bs = bs->next)
11562     if (bs->breakpoint_at
11563         && bs->breakpoint_at->disposition == disp_del
11564         && bs->stop)
11565       delete_breakpoint (bs->breakpoint_at);
11566
11567   ALL_BREAKPOINTS_SAFE (b, b_tmp)
11568   {
11569     if (b->disposition == disp_del_at_next_stop)
11570       delete_breakpoint (b);
11571   }
11572 }
11573
11574 /* A comparison function for bp_location AP and BP being interfaced to
11575    qsort.  Sort elements primarily by their ADDRESS (no matter what
11576    does breakpoint_address_is_meaningful say for its OWNER),
11577    secondarily by ordering first bp_permanent OWNERed elements and
11578    terciarily just ensuring the array is sorted stable way despite
11579    qsort being an unstable algorithm.  */
11580
11581 static int
11582 bp_location_compare (const void *ap, const void *bp)
11583 {
11584   struct bp_location *a = *(void **) ap;
11585   struct bp_location *b = *(void **) bp;
11586   /* A and B come from existing breakpoints having non-NULL OWNER.  */
11587   int a_perm = a->owner->enable_state == bp_permanent;
11588   int b_perm = b->owner->enable_state == bp_permanent;
11589
11590   if (a->address != b->address)
11591     return (a->address > b->address) - (a->address < b->address);
11592
11593   /* Sort locations at the same address by their pspace number, keeping
11594      locations of the same inferior (in a multi-inferior environment)
11595      grouped.  */
11596
11597   if (a->pspace->num != b->pspace->num)
11598     return ((a->pspace->num > b->pspace->num)
11599             - (a->pspace->num < b->pspace->num));
11600
11601   /* Sort permanent breakpoints first.  */
11602   if (a_perm != b_perm)
11603     return (a_perm < b_perm) - (a_perm > b_perm);
11604
11605   /* Make the internal GDB representation stable across GDB runs
11606      where A and B memory inside GDB can differ.  Breakpoint locations of
11607      the same type at the same address can be sorted in arbitrary order.  */
11608
11609   if (a->owner->number != b->owner->number)
11610     return ((a->owner->number > b->owner->number)
11611             - (a->owner->number < b->owner->number));
11612
11613   return (a > b) - (a < b);
11614 }
11615
11616 /* Set bp_location_placed_address_before_address_max and
11617    bp_location_shadow_len_after_address_max according to the current
11618    content of the bp_location array.  */
11619
11620 static void
11621 bp_location_target_extensions_update (void)
11622 {
11623   struct bp_location *bl, **blp_tmp;
11624
11625   bp_location_placed_address_before_address_max = 0;
11626   bp_location_shadow_len_after_address_max = 0;
11627
11628   ALL_BP_LOCATIONS (bl, blp_tmp)
11629     {
11630       CORE_ADDR start, end, addr;
11631
11632       if (!bp_location_has_shadow (bl))
11633         continue;
11634
11635       start = bl->target_info.placed_address;
11636       end = start + bl->target_info.shadow_len;
11637
11638       gdb_assert (bl->address >= start);
11639       addr = bl->address - start;
11640       if (addr > bp_location_placed_address_before_address_max)
11641         bp_location_placed_address_before_address_max = addr;
11642
11643       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
11644
11645       gdb_assert (bl->address < end);
11646       addr = end - bl->address;
11647       if (addr > bp_location_shadow_len_after_address_max)
11648         bp_location_shadow_len_after_address_max = addr;
11649     }
11650 }
11651
11652 /* Download tracepoint locations if they haven't been.  */
11653
11654 static void
11655 download_tracepoint_locations (void)
11656 {
11657   struct bp_location *bl, **blp_tmp;
11658   struct cleanup *old_chain;
11659
11660   if (!target_can_download_tracepoint ())
11661     return;
11662
11663   old_chain = save_current_space_and_thread ();
11664
11665   ALL_BP_LOCATIONS (bl, blp_tmp)
11666     {
11667       struct tracepoint *t;
11668
11669       if (!is_tracepoint (bl->owner))
11670         continue;
11671
11672       if ((bl->owner->type == bp_fast_tracepoint
11673            ? !may_insert_fast_tracepoints
11674            : !may_insert_tracepoints))
11675         continue;
11676
11677       /* In tracepoint, locations are _never_ duplicated, so
11678          should_be_inserted is equivalent to
11679          unduplicated_should_be_inserted.  */
11680       if (!should_be_inserted (bl) || bl->inserted)
11681         continue;
11682
11683       switch_to_program_space_and_thread (bl->pspace);
11684
11685       target_download_tracepoint (bl);
11686
11687       bl->inserted = 1;
11688       t = (struct tracepoint *) bl->owner;
11689       t->number_on_target = bl->owner->number;
11690     }
11691
11692   do_cleanups (old_chain);
11693 }
11694
11695 /* Swap the insertion/duplication state between two locations.  */
11696
11697 static void
11698 swap_insertion (struct bp_location *left, struct bp_location *right)
11699 {
11700   const int left_inserted = left->inserted;
11701   const int left_duplicate = left->duplicate;
11702   const int left_needs_update = left->needs_update;
11703   const struct bp_target_info left_target_info = left->target_info;
11704
11705   /* Locations of tracepoints can never be duplicated.  */
11706   if (is_tracepoint (left->owner))
11707     gdb_assert (!left->duplicate);
11708   if (is_tracepoint (right->owner))
11709     gdb_assert (!right->duplicate);
11710
11711   left->inserted = right->inserted;
11712   left->duplicate = right->duplicate;
11713   left->needs_update = right->needs_update;
11714   left->target_info = right->target_info;
11715   right->inserted = left_inserted;
11716   right->duplicate = left_duplicate;
11717   right->needs_update = left_needs_update;
11718   right->target_info = left_target_info;
11719 }
11720
11721 /* Force the re-insertion of the locations at ADDRESS.  This is called
11722    once a new/deleted/modified duplicate location is found and we are evaluating
11723    conditions on the target's side.  Such conditions need to be updated on
11724    the target.  */
11725
11726 static void
11727 force_breakpoint_reinsertion (struct bp_location *bl)
11728 {
11729   struct bp_location **locp = NULL, **loc2p;
11730   struct bp_location *loc;
11731   CORE_ADDR address = 0;
11732   int pspace_num;
11733
11734   address = bl->address;
11735   pspace_num = bl->pspace->num;
11736
11737   /* This is only meaningful if the target is
11738      evaluating conditions and if the user has
11739      opted for condition evaluation on the target's
11740      side.  */
11741   if (gdb_evaluates_breakpoint_condition_p ()
11742       || !target_supports_evaluation_of_breakpoint_conditions ())
11743     return;
11744
11745   /* Flag all breakpoint locations with this address and
11746      the same program space as the location
11747      as "its condition has changed".  We need to
11748      update the conditions on the target's side.  */
11749   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11750     {
11751       loc = *loc2p;
11752
11753       if (!is_breakpoint (loc->owner)
11754           || pspace_num != loc->pspace->num)
11755         continue;
11756
11757       /* Flag the location appropriately.  We use a different state to
11758          let everyone know that we already updated the set of locations
11759          with addr bl->address and program space bl->pspace.  This is so
11760          we don't have to keep calling these functions just to mark locations
11761          that have already been marked.  */
11762       loc->condition_changed = condition_updated;
11763
11764       /* Free the agent expression bytecode as well.  We will compute
11765          it later on.  */
11766       if (loc->cond_bytecode)
11767         {
11768           free_agent_expr (loc->cond_bytecode);
11769           loc->cond_bytecode = NULL;
11770         }
11771     }
11772 }
11773
11774 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
11775    into the inferior, only remove already-inserted locations that no
11776    longer should be inserted.  Functions that delete a breakpoint or
11777    breakpoints should pass false, so that deleting a breakpoint
11778    doesn't have the side effect of inserting the locations of other
11779    breakpoints that are marked not-inserted, but should_be_inserted
11780    returns true on them.
11781
11782    This behaviour is useful is situations close to tear-down -- e.g.,
11783    after an exec, while the target still has execution, but breakpoint
11784    shadows of the previous executable image should *NOT* be restored
11785    to the new image; or before detaching, where the target still has
11786    execution and wants to delete breakpoints from GDB's lists, and all
11787    breakpoints had already been removed from the inferior.  */
11788
11789 static void
11790 update_global_location_list (int should_insert)
11791 {
11792   struct breakpoint *b;
11793   struct bp_location **locp, *loc;
11794   struct cleanup *cleanups;
11795   /* Last breakpoint location address that was marked for update.  */
11796   CORE_ADDR last_addr = 0;
11797   /* Last breakpoint location program space that was marked for update.  */
11798   int last_pspace_num = -1;
11799
11800   /* Used in the duplicates detection below.  When iterating over all
11801      bp_locations, points to the first bp_location of a given address.
11802      Breakpoints and watchpoints of different types are never
11803      duplicates of each other.  Keep one pointer for each type of
11804      breakpoint/watchpoint, so we only need to loop over all locations
11805      once.  */
11806   struct bp_location *bp_loc_first;  /* breakpoint */
11807   struct bp_location *wp_loc_first;  /* hardware watchpoint */
11808   struct bp_location *awp_loc_first; /* access watchpoint */
11809   struct bp_location *rwp_loc_first; /* read watchpoint */
11810
11811   /* Saved former bp_location array which we compare against the newly
11812      built bp_location from the current state of ALL_BREAKPOINTS.  */
11813   struct bp_location **old_location, **old_locp;
11814   unsigned old_location_count;
11815
11816   old_location = bp_location;
11817   old_location_count = bp_location_count;
11818   bp_location = NULL;
11819   bp_location_count = 0;
11820   cleanups = make_cleanup (xfree, old_location);
11821
11822   ALL_BREAKPOINTS (b)
11823     for (loc = b->loc; loc; loc = loc->next)
11824       bp_location_count++;
11825
11826   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11827   locp = bp_location;
11828   ALL_BREAKPOINTS (b)
11829     for (loc = b->loc; loc; loc = loc->next)
11830       *locp++ = loc;
11831   qsort (bp_location, bp_location_count, sizeof (*bp_location),
11832          bp_location_compare);
11833
11834   bp_location_target_extensions_update ();
11835
11836   /* Identify bp_location instances that are no longer present in the
11837      new list, and therefore should be freed.  Note that it's not
11838      necessary that those locations should be removed from inferior --
11839      if there's another location at the same address (previously
11840      marked as duplicate), we don't need to remove/insert the
11841      location.
11842      
11843      LOCP is kept in sync with OLD_LOCP, each pointing to the current
11844      and former bp_location array state respectively.  */
11845
11846   locp = bp_location;
11847   for (old_locp = old_location; old_locp < old_location + old_location_count;
11848        old_locp++)
11849     {
11850       struct bp_location *old_loc = *old_locp;
11851       struct bp_location **loc2p;
11852
11853       /* Tells if 'old_loc' is found among the new locations.  If
11854          not, we have to free it.  */
11855       int found_object = 0;
11856       /* Tells if the location should remain inserted in the target.  */
11857       int keep_in_target = 0;
11858       int removed = 0;
11859
11860       /* Skip LOCP entries which will definitely never be needed.
11861          Stop either at or being the one matching OLD_LOC.  */
11862       while (locp < bp_location + bp_location_count
11863              && (*locp)->address < old_loc->address)
11864         locp++;
11865
11866       for (loc2p = locp;
11867            (loc2p < bp_location + bp_location_count
11868             && (*loc2p)->address == old_loc->address);
11869            loc2p++)
11870         {
11871           /* Check if this is a new/duplicated location or a duplicated
11872              location that had its condition modified.  If so, we want to send
11873              its condition to the target if evaluation of conditions is taking
11874              place there.  */
11875           if ((*loc2p)->condition_changed == condition_modified
11876               && (last_addr != old_loc->address
11877                   || last_pspace_num != old_loc->pspace->num))
11878             {
11879               force_breakpoint_reinsertion (*loc2p);
11880               last_pspace_num = old_loc->pspace->num;
11881             }
11882
11883           if (*loc2p == old_loc)
11884             found_object = 1;
11885         }
11886
11887       /* We have already handled this address, update it so that we don't
11888          have to go through updates again.  */
11889       last_addr = old_loc->address;
11890
11891       /* Target-side condition evaluation: Handle deleted locations.  */
11892       if (!found_object)
11893         force_breakpoint_reinsertion (old_loc);
11894
11895       /* If this location is no longer present, and inserted, look if
11896          there's maybe a new location at the same address.  If so,
11897          mark that one inserted, and don't remove this one.  This is
11898          needed so that we don't have a time window where a breakpoint
11899          at certain location is not inserted.  */
11900
11901       if (old_loc->inserted)
11902         {
11903           /* If the location is inserted now, we might have to remove
11904              it.  */
11905
11906           if (found_object && should_be_inserted (old_loc))
11907             {
11908               /* The location is still present in the location list,
11909                  and still should be inserted.  Don't do anything.  */
11910               keep_in_target = 1;
11911             }
11912           else
11913             {
11914               /* This location still exists, but it won't be kept in the
11915                  target since it may have been disabled.  We proceed to
11916                  remove its target-side condition.  */
11917
11918               /* The location is either no longer present, or got
11919                  disabled.  See if there's another location at the
11920                  same address, in which case we don't need to remove
11921                  this one from the target.  */
11922
11923               /* OLD_LOC comes from existing struct breakpoint.  */
11924               if (breakpoint_address_is_meaningful (old_loc->owner))
11925                 {
11926                   for (loc2p = locp;
11927                        (loc2p < bp_location + bp_location_count
11928                         && (*loc2p)->address == old_loc->address);
11929                        loc2p++)
11930                     {
11931                       struct bp_location *loc2 = *loc2p;
11932
11933                       if (breakpoint_locations_match (loc2, old_loc))
11934                         {
11935                           /* Read watchpoint locations are switched to
11936                              access watchpoints, if the former are not
11937                              supported, but the latter are.  */
11938                           if (is_hardware_watchpoint (old_loc->owner))
11939                             {
11940                               gdb_assert (is_hardware_watchpoint (loc2->owner));
11941                               loc2->watchpoint_type = old_loc->watchpoint_type;
11942                             }
11943
11944                           /* loc2 is a duplicated location. We need to check
11945                              if it should be inserted in case it will be
11946                              unduplicated.  */
11947                           if (loc2 != old_loc
11948                               && unduplicated_should_be_inserted (loc2))
11949                             {
11950                               swap_insertion (old_loc, loc2);
11951                               keep_in_target = 1;
11952                               break;
11953                             }
11954                         }
11955                     }
11956                 }
11957             }
11958
11959           if (!keep_in_target)
11960             {
11961               if (remove_breakpoint (old_loc, mark_uninserted))
11962                 {
11963                   /* This is just about all we can do.  We could keep
11964                      this location on the global list, and try to
11965                      remove it next time, but there's no particular
11966                      reason why we will succeed next time.
11967                      
11968                      Note that at this point, old_loc->owner is still
11969                      valid, as delete_breakpoint frees the breakpoint
11970                      only after calling us.  */
11971                   printf_filtered (_("warning: Error removing "
11972                                      "breakpoint %d\n"), 
11973                                    old_loc->owner->number);
11974                 }
11975               removed = 1;
11976             }
11977         }
11978
11979       if (!found_object)
11980         {
11981           if (removed && non_stop
11982               && breakpoint_address_is_meaningful (old_loc->owner)
11983               && !is_hardware_watchpoint (old_loc->owner))
11984             {
11985               /* This location was removed from the target.  In
11986                  non-stop mode, a race condition is possible where
11987                  we've removed a breakpoint, but stop events for that
11988                  breakpoint are already queued and will arrive later.
11989                  We apply an heuristic to be able to distinguish such
11990                  SIGTRAPs from other random SIGTRAPs: we keep this
11991                  breakpoint location for a bit, and will retire it
11992                  after we see some number of events.  The theory here
11993                  is that reporting of events should, "on the average",
11994                  be fair, so after a while we'll see events from all
11995                  threads that have anything of interest, and no longer
11996                  need to keep this breakpoint location around.  We
11997                  don't hold locations forever so to reduce chances of
11998                  mistaking a non-breakpoint SIGTRAP for a breakpoint
11999                  SIGTRAP.
12000
12001                  The heuristic failing can be disastrous on
12002                  decr_pc_after_break targets.
12003
12004                  On decr_pc_after_break targets, like e.g., x86-linux,
12005                  if we fail to recognize a late breakpoint SIGTRAP,
12006                  because events_till_retirement has reached 0 too
12007                  soon, we'll fail to do the PC adjustment, and report
12008                  a random SIGTRAP to the user.  When the user resumes
12009                  the inferior, it will most likely immediately crash
12010                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12011                  corrupted, because of being resumed e.g., in the
12012                  middle of a multi-byte instruction, or skipped a
12013                  one-byte instruction.  This was actually seen happen
12014                  on native x86-linux, and should be less rare on
12015                  targets that do not support new thread events, like
12016                  remote, due to the heuristic depending on
12017                  thread_count.
12018
12019                  Mistaking a random SIGTRAP for a breakpoint trap
12020                  causes similar symptoms (PC adjustment applied when
12021                  it shouldn't), but then again, playing with SIGTRAPs
12022                  behind the debugger's back is asking for trouble.
12023
12024                  Since hardware watchpoint traps are always
12025                  distinguishable from other traps, so we don't need to
12026                  apply keep hardware watchpoint moribund locations
12027                  around.  We simply always ignore hardware watchpoint
12028                  traps we can no longer explain.  */
12029
12030               old_loc->events_till_retirement = 3 * (thread_count () + 1);
12031               old_loc->owner = NULL;
12032
12033               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12034             }
12035           else
12036             {
12037               old_loc->owner = NULL;
12038               decref_bp_location (&old_loc);
12039             }
12040         }
12041     }
12042
12043   /* Rescan breakpoints at the same address and section, marking the
12044      first one as "first" and any others as "duplicates".  This is so
12045      that the bpt instruction is only inserted once.  If we have a
12046      permanent breakpoint at the same place as BPT, make that one the
12047      official one, and the rest as duplicates.  Permanent breakpoints
12048      are sorted first for the same address.
12049
12050      Do the same for hardware watchpoints, but also considering the
12051      watchpoint's type (regular/access/read) and length.  */
12052
12053   bp_loc_first = NULL;
12054   wp_loc_first = NULL;
12055   awp_loc_first = NULL;
12056   rwp_loc_first = NULL;
12057   ALL_BP_LOCATIONS (loc, locp)
12058     {
12059       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12060          non-NULL.  */
12061       struct bp_location **loc_first_p;
12062       b = loc->owner;
12063
12064       if (!should_be_inserted (loc)
12065           || !breakpoint_address_is_meaningful (b)
12066           /* Don't detect duplicate for tracepoint locations because they are
12067            never duplicated.  See the comments in field `duplicate' of
12068            `struct bp_location'.  */
12069           || is_tracepoint (b))
12070         {
12071           /* Clear the condition modification flag.  */
12072           loc->condition_changed = condition_unchanged;
12073           continue;
12074         }
12075
12076       /* Permanent breakpoint should always be inserted.  */
12077       if (b->enable_state == bp_permanent && ! loc->inserted)
12078         internal_error (__FILE__, __LINE__,
12079                         _("allegedly permanent breakpoint is not "
12080                         "actually inserted"));
12081
12082       if (b->type == bp_hardware_watchpoint)
12083         loc_first_p = &wp_loc_first;
12084       else if (b->type == bp_read_watchpoint)
12085         loc_first_p = &rwp_loc_first;
12086       else if (b->type == bp_access_watchpoint)
12087         loc_first_p = &awp_loc_first;
12088       else
12089         loc_first_p = &bp_loc_first;
12090
12091       if (*loc_first_p == NULL
12092           || (overlay_debugging && loc->section != (*loc_first_p)->section)
12093           || !breakpoint_locations_match (loc, *loc_first_p))
12094         {
12095           *loc_first_p = loc;
12096           loc->duplicate = 0;
12097
12098           if (is_breakpoint (loc->owner) && loc->condition_changed)
12099             {
12100               loc->needs_update = 1;
12101               /* Clear the condition modification flag.  */
12102               loc->condition_changed = condition_unchanged;
12103             }
12104           continue;
12105         }
12106
12107
12108       /* This and the above ensure the invariant that the first location
12109          is not duplicated, and is the inserted one.
12110          All following are marked as duplicated, and are not inserted.  */
12111       if (loc->inserted)
12112         swap_insertion (loc, *loc_first_p);
12113       loc->duplicate = 1;
12114
12115       /* Clear the condition modification flag.  */
12116       loc->condition_changed = condition_unchanged;
12117
12118       if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12119           && b->enable_state != bp_permanent)
12120         internal_error (__FILE__, __LINE__,
12121                         _("another breakpoint was inserted on top of "
12122                         "a permanent breakpoint"));
12123     }
12124
12125   if (breakpoints_always_inserted_mode ()
12126       && (have_live_inferiors ()
12127           || (gdbarch_has_global_breakpoints (target_gdbarch))))
12128     {
12129       if (should_insert)
12130         insert_breakpoint_locations ();
12131       else
12132         {
12133           /* Though should_insert is false, we may need to update conditions
12134              on the target's side if it is evaluating such conditions.  We
12135              only update conditions for locations that are marked
12136              "needs_update".  */
12137           update_inserted_breakpoint_locations ();
12138         }
12139     }
12140
12141   if (should_insert)
12142     download_tracepoint_locations ();
12143
12144   do_cleanups (cleanups);
12145 }
12146
12147 void
12148 breakpoint_retire_moribund (void)
12149 {
12150   struct bp_location *loc;
12151   int ix;
12152
12153   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12154     if (--(loc->events_till_retirement) == 0)
12155       {
12156         decref_bp_location (&loc);
12157         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12158         --ix;
12159       }
12160 }
12161
12162 static void
12163 update_global_location_list_nothrow (int inserting)
12164 {
12165   volatile struct gdb_exception e;
12166
12167   TRY_CATCH (e, RETURN_MASK_ERROR)
12168     update_global_location_list (inserting);
12169 }
12170
12171 /* Clear BKP from a BPS.  */
12172
12173 static void
12174 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12175 {
12176   bpstat bs;
12177
12178   for (bs = bps; bs; bs = bs->next)
12179     if (bs->breakpoint_at == bpt)
12180       {
12181         bs->breakpoint_at = NULL;
12182         bs->old_val = NULL;
12183         /* bs->commands will be freed later.  */
12184       }
12185 }
12186
12187 /* Callback for iterate_over_threads.  */
12188 static int
12189 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12190 {
12191   struct breakpoint *bpt = data;
12192
12193   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12194   return 0;
12195 }
12196
12197 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12198    callbacks.  */
12199
12200 static void
12201 say_where (struct breakpoint *b)
12202 {
12203   struct ui_out *uiout = current_uiout;
12204   struct value_print_options opts;
12205
12206   get_user_print_options (&opts);
12207
12208   /* i18n: cagney/2005-02-11: Below needs to be merged into a
12209      single string.  */
12210   if (b->loc == NULL)
12211     {
12212       printf_filtered (_(" (%s) pending."), b->addr_string);
12213     }
12214   else
12215     {
12216       if (opts.addressprint || b->loc->source_file == NULL)
12217         {
12218           printf_filtered (" at ");
12219           fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12220                           gdb_stdout);
12221         }
12222       if (b->loc->source_file)
12223         {
12224           /* If there is a single location, we can print the location
12225              more nicely.  */
12226           if (b->loc->next == NULL)
12227             printf_filtered (": file %s, line %d.",
12228                              b->loc->source_file, b->loc->line_number);
12229           else
12230             /* This is not ideal, but each location may have a
12231                different file name, and this at least reflects the
12232                real situation somewhat.  */
12233             printf_filtered (": %s.", b->addr_string);
12234         }
12235
12236       if (b->loc->next)
12237         {
12238           struct bp_location *loc = b->loc;
12239           int n = 0;
12240           for (; loc; loc = loc->next)
12241             ++n;
12242           printf_filtered (" (%d locations)", n);
12243         }
12244     }
12245 }
12246
12247 /* Default bp_location_ops methods.  */
12248
12249 static void
12250 bp_location_dtor (struct bp_location *self)
12251 {
12252   xfree (self->cond);
12253   if (self->cond_bytecode)
12254     free_agent_expr (self->cond_bytecode);
12255   xfree (self->function_name);
12256   xfree (self->source_file);
12257 }
12258
12259 static const struct bp_location_ops bp_location_ops =
12260 {
12261   bp_location_dtor
12262 };
12263
12264 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12265    inherit from.  */
12266
12267 static void
12268 base_breakpoint_dtor (struct breakpoint *self)
12269 {
12270   decref_counted_command_line (&self->commands);
12271   xfree (self->cond_string);
12272   xfree (self->addr_string);
12273   xfree (self->filter);
12274   xfree (self->addr_string_range_end);
12275 }
12276
12277 static struct bp_location *
12278 base_breakpoint_allocate_location (struct breakpoint *self)
12279 {
12280   struct bp_location *loc;
12281
12282   loc = XNEW (struct bp_location);
12283   init_bp_location (loc, &bp_location_ops, self);
12284   return loc;
12285 }
12286
12287 static void
12288 base_breakpoint_re_set (struct breakpoint *b)
12289 {
12290   /* Nothing to re-set. */
12291 }
12292
12293 #define internal_error_pure_virtual_called() \
12294   gdb_assert_not_reached ("pure virtual function called")
12295
12296 static int
12297 base_breakpoint_insert_location (struct bp_location *bl)
12298 {
12299   internal_error_pure_virtual_called ();
12300 }
12301
12302 static int
12303 base_breakpoint_remove_location (struct bp_location *bl)
12304 {
12305   internal_error_pure_virtual_called ();
12306 }
12307
12308 static int
12309 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12310                                 struct address_space *aspace,
12311                                 CORE_ADDR bp_addr,
12312                                 const struct target_waitstatus *ws)
12313 {
12314   internal_error_pure_virtual_called ();
12315 }
12316
12317 static void
12318 base_breakpoint_check_status (bpstat bs)
12319 {
12320   /* Always stop.   */
12321 }
12322
12323 /* A "works_in_software_mode" breakpoint_ops method that just internal
12324    errors.  */
12325
12326 static int
12327 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12328 {
12329   internal_error_pure_virtual_called ();
12330 }
12331
12332 /* A "resources_needed" breakpoint_ops method that just internal
12333    errors.  */
12334
12335 static int
12336 base_breakpoint_resources_needed (const struct bp_location *bl)
12337 {
12338   internal_error_pure_virtual_called ();
12339 }
12340
12341 static enum print_stop_action
12342 base_breakpoint_print_it (bpstat bs)
12343 {
12344   internal_error_pure_virtual_called ();
12345 }
12346
12347 static void
12348 base_breakpoint_print_one_detail (const struct breakpoint *self,
12349                                   struct ui_out *uiout)
12350 {
12351   /* nothing */
12352 }
12353
12354 static void
12355 base_breakpoint_print_mention (struct breakpoint *b)
12356 {
12357   internal_error_pure_virtual_called ();
12358 }
12359
12360 static void
12361 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12362 {
12363   internal_error_pure_virtual_called ();
12364 }
12365
12366 static void
12367 base_breakpoint_create_sals_from_address (char **arg,
12368                                           struct linespec_result *canonical,
12369                                           enum bptype type_wanted,
12370                                           char *addr_start,
12371                                           char **copy_arg)
12372 {
12373   internal_error_pure_virtual_called ();
12374 }
12375
12376 static void
12377 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12378                                         struct linespec_result *c,
12379                                         struct linespec_sals *lsal,
12380                                         char *cond_string,
12381                                         char *extra_string,
12382                                         enum bptype type_wanted,
12383                                         enum bpdisp disposition,
12384                                         int thread,
12385                                         int task, int ignore_count,
12386                                         const struct breakpoint_ops *o,
12387                                         int from_tty, int enabled,
12388                                         int internal, unsigned flags)
12389 {
12390   internal_error_pure_virtual_called ();
12391 }
12392
12393 static void
12394 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12395                                  struct symtabs_and_lines *sals)
12396 {
12397   internal_error_pure_virtual_called ();
12398 }
12399
12400 static struct breakpoint_ops base_breakpoint_ops =
12401 {
12402   base_breakpoint_dtor,
12403   base_breakpoint_allocate_location,
12404   base_breakpoint_re_set,
12405   base_breakpoint_insert_location,
12406   base_breakpoint_remove_location,
12407   base_breakpoint_breakpoint_hit,
12408   base_breakpoint_check_status,
12409   base_breakpoint_resources_needed,
12410   base_breakpoint_works_in_software_mode,
12411   base_breakpoint_print_it,
12412   NULL,
12413   base_breakpoint_print_one_detail,
12414   base_breakpoint_print_mention,
12415   base_breakpoint_print_recreate,
12416   base_breakpoint_create_sals_from_address,
12417   base_breakpoint_create_breakpoints_sal,
12418   base_breakpoint_decode_linespec,
12419 };
12420
12421 /* Default breakpoint_ops methods.  */
12422
12423 static void
12424 bkpt_re_set (struct breakpoint *b)
12425 {
12426   /* FIXME: is this still reachable?  */
12427   if (b->addr_string == NULL)
12428     {
12429       /* Anything without a string can't be re-set.  */
12430       delete_breakpoint (b);
12431       return;
12432     }
12433
12434   breakpoint_re_set_default (b);
12435 }
12436
12437 static int
12438 bkpt_insert_location (struct bp_location *bl)
12439 {
12440   if (bl->loc_type == bp_loc_hardware_breakpoint)
12441     return target_insert_hw_breakpoint (bl->gdbarch,
12442                                         &bl->target_info);
12443   else
12444     return target_insert_breakpoint (bl->gdbarch,
12445                                      &bl->target_info);
12446 }
12447
12448 static int
12449 bkpt_remove_location (struct bp_location *bl)
12450 {
12451   if (bl->loc_type == bp_loc_hardware_breakpoint)
12452     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12453   else
12454     return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12455 }
12456
12457 static int
12458 bkpt_breakpoint_hit (const struct bp_location *bl,
12459                      struct address_space *aspace, CORE_ADDR bp_addr,
12460                      const struct target_waitstatus *ws)
12461 {
12462   struct breakpoint *b = bl->owner;
12463
12464   if (ws->kind != TARGET_WAITKIND_STOPPED
12465       || ws->value.sig != TARGET_SIGNAL_TRAP)
12466     return 0;
12467
12468   if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12469                                  aspace, bp_addr))
12470     return 0;
12471
12472   if (overlay_debugging         /* unmapped overlay section */
12473       && section_is_overlay (bl->section)
12474       && !section_is_mapped (bl->section))
12475     return 0;
12476
12477   return 1;
12478 }
12479
12480 static int
12481 bkpt_resources_needed (const struct bp_location *bl)
12482 {
12483   gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12484
12485   return 1;
12486 }
12487
12488 static enum print_stop_action
12489 bkpt_print_it (bpstat bs)
12490 {
12491   struct breakpoint *b;
12492   const struct bp_location *bl;
12493   int bp_temp;
12494   struct ui_out *uiout = current_uiout;
12495
12496   gdb_assert (bs->bp_location_at != NULL);
12497
12498   bl = bs->bp_location_at;
12499   b = bs->breakpoint_at;
12500
12501   bp_temp = b->disposition == disp_del;
12502   if (bl->address != bl->requested_address)
12503     breakpoint_adjustment_warning (bl->requested_address,
12504                                    bl->address,
12505                                    b->number, 1);
12506   annotate_breakpoint (b->number);
12507   if (bp_temp)
12508     ui_out_text (uiout, "\nTemporary breakpoint ");
12509   else
12510     ui_out_text (uiout, "\nBreakpoint ");
12511   if (ui_out_is_mi_like_p (uiout))
12512     {
12513       ui_out_field_string (uiout, "reason",
12514                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12515       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
12516     }
12517   ui_out_field_int (uiout, "bkptno", b->number);
12518   ui_out_text (uiout, ", ");
12519
12520   return PRINT_SRC_AND_LOC;
12521 }
12522
12523 static void
12524 bkpt_print_mention (struct breakpoint *b)
12525 {
12526   if (ui_out_is_mi_like_p (current_uiout))
12527     return;
12528
12529   switch (b->type)
12530     {
12531     case bp_breakpoint:
12532     case bp_gnu_ifunc_resolver:
12533       if (b->disposition == disp_del)
12534         printf_filtered (_("Temporary breakpoint"));
12535       else
12536         printf_filtered (_("Breakpoint"));
12537       printf_filtered (_(" %d"), b->number);
12538       if (b->type == bp_gnu_ifunc_resolver)
12539         printf_filtered (_(" at gnu-indirect-function resolver"));
12540       break;
12541     case bp_hardware_breakpoint:
12542       printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12543       break;
12544     case bp_dprintf:
12545       printf_filtered (_("Dprintf %d"), b->number);
12546       break;
12547     }
12548
12549   say_where (b);
12550 }
12551
12552 static void
12553 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12554 {
12555   if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12556     fprintf_unfiltered (fp, "tbreak");
12557   else if (tp->type == bp_breakpoint)
12558     fprintf_unfiltered (fp, "break");
12559   else if (tp->type == bp_hardware_breakpoint
12560            && tp->disposition == disp_del)
12561     fprintf_unfiltered (fp, "thbreak");
12562   else if (tp->type == bp_hardware_breakpoint)
12563     fprintf_unfiltered (fp, "hbreak");
12564   else
12565     internal_error (__FILE__, __LINE__,
12566                     _("unhandled breakpoint type %d"), (int) tp->type);
12567
12568   fprintf_unfiltered (fp, " %s", tp->addr_string);
12569   print_recreate_thread (tp, fp);
12570 }
12571
12572 static void
12573 bkpt_create_sals_from_address (char **arg,
12574                                struct linespec_result *canonical,
12575                                enum bptype type_wanted,
12576                                char *addr_start, char **copy_arg)
12577 {
12578   create_sals_from_address_default (arg, canonical, type_wanted,
12579                                     addr_start, copy_arg);
12580 }
12581
12582 static void
12583 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12584                              struct linespec_result *canonical,
12585                              struct linespec_sals *lsal,
12586                              char *cond_string,
12587                              char *extra_string,
12588                              enum bptype type_wanted,
12589                              enum bpdisp disposition,
12590                              int thread,
12591                              int task, int ignore_count,
12592                              const struct breakpoint_ops *ops,
12593                              int from_tty, int enabled,
12594                              int internal, unsigned flags)
12595 {
12596   create_breakpoints_sal_default (gdbarch, canonical, lsal,
12597                                   cond_string, extra_string,
12598                                   type_wanted,
12599                                   disposition, thread, task,
12600                                   ignore_count, ops, from_tty,
12601                                   enabled, internal, flags);
12602 }
12603
12604 static void
12605 bkpt_decode_linespec (struct breakpoint *b, char **s,
12606                       struct symtabs_and_lines *sals)
12607 {
12608   decode_linespec_default (b, s, sals);
12609 }
12610
12611 /* Virtual table for internal breakpoints.  */
12612
12613 static void
12614 internal_bkpt_re_set (struct breakpoint *b)
12615 {
12616   switch (b->type)
12617     {
12618       /* Delete overlay event and longjmp master breakpoints; they
12619          will be reset later by breakpoint_re_set.  */
12620     case bp_overlay_event:
12621     case bp_longjmp_master:
12622     case bp_std_terminate_master:
12623     case bp_exception_master:
12624       delete_breakpoint (b);
12625       break;
12626
12627       /* This breakpoint is special, it's set up when the inferior
12628          starts and we really don't want to touch it.  */
12629     case bp_shlib_event:
12630
12631       /* Like bp_shlib_event, this breakpoint type is special.  Once
12632          it is set up, we do not want to touch it.  */
12633     case bp_thread_event:
12634       break;
12635     }
12636 }
12637
12638 static void
12639 internal_bkpt_check_status (bpstat bs)
12640 {
12641   if (bs->breakpoint_at->type == bp_shlib_event)
12642     {
12643       /* If requested, stop when the dynamic linker notifies GDB of
12644          events.  This allows the user to get control and place
12645          breakpoints in initializer routines for dynamically loaded
12646          objects (among other things).  */
12647       bs->stop = stop_on_solib_events;
12648       bs->print = stop_on_solib_events;
12649     }
12650   else
12651     bs->stop = 0;
12652 }
12653
12654 static enum print_stop_action
12655 internal_bkpt_print_it (bpstat bs)
12656 {
12657   struct ui_out *uiout = current_uiout;
12658   struct breakpoint *b;
12659
12660   b = bs->breakpoint_at;
12661
12662   switch (b->type)
12663     {
12664     case bp_shlib_event:
12665       /* Did we stop because the user set the stop_on_solib_events
12666          variable?  (If so, we report this as a generic, "Stopped due
12667          to shlib event" message.) */
12668       print_solib_event (0);
12669       break;
12670
12671     case bp_thread_event:
12672       /* Not sure how we will get here.
12673          GDB should not stop for these breakpoints.  */
12674       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12675       break;
12676
12677     case bp_overlay_event:
12678       /* By analogy with the thread event, GDB should not stop for these.  */
12679       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12680       break;
12681
12682     case bp_longjmp_master:
12683       /* These should never be enabled.  */
12684       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12685       break;
12686
12687     case bp_std_terminate_master:
12688       /* These should never be enabled.  */
12689       printf_filtered (_("std::terminate Master Breakpoint: "
12690                          "gdb should not stop!\n"));
12691       break;
12692
12693     case bp_exception_master:
12694       /* These should never be enabled.  */
12695       printf_filtered (_("Exception Master Breakpoint: "
12696                          "gdb should not stop!\n"));
12697       break;
12698     }
12699
12700   return PRINT_NOTHING;
12701 }
12702
12703 static void
12704 internal_bkpt_print_mention (struct breakpoint *b)
12705 {
12706   /* Nothing to mention.  These breakpoints are internal.  */
12707 }
12708
12709 /* Virtual table for momentary breakpoints  */
12710
12711 static void
12712 momentary_bkpt_re_set (struct breakpoint *b)
12713 {
12714   /* Keep temporary breakpoints, which can be encountered when we step
12715      over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12716      Otherwise these should have been blown away via the cleanup chain
12717      or by breakpoint_init_inferior when we rerun the executable.  */
12718 }
12719
12720 static void
12721 momentary_bkpt_check_status (bpstat bs)
12722 {
12723   /* Nothing.  The point of these breakpoints is causing a stop.  */
12724 }
12725
12726 static enum print_stop_action
12727 momentary_bkpt_print_it (bpstat bs)
12728 {
12729   struct ui_out *uiout = current_uiout;
12730
12731   if (ui_out_is_mi_like_p (uiout))
12732     {
12733       struct breakpoint *b = bs->breakpoint_at;
12734
12735       switch (b->type)
12736         {
12737         case bp_finish:
12738           ui_out_field_string
12739             (uiout, "reason",
12740              async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12741           break;
12742
12743         case bp_until:
12744           ui_out_field_string
12745             (uiout, "reason",
12746              async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12747           break;
12748         }
12749     }
12750
12751   return PRINT_UNKNOWN;
12752 }
12753
12754 static void
12755 momentary_bkpt_print_mention (struct breakpoint *b)
12756 {
12757   /* Nothing to mention.  These breakpoints are internal.  */
12758 }
12759
12760 /* Specific methods for probe breakpoints.  */
12761
12762 static int
12763 bkpt_probe_insert_location (struct bp_location *bl)
12764 {
12765   int v = bkpt_insert_location (bl);
12766
12767   if (v == 0)
12768     {
12769       /* The insertion was successful, now let's set the probe's semaphore
12770          if needed.  */
12771       bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
12772     }
12773
12774   return v;
12775 }
12776
12777 static int
12778 bkpt_probe_remove_location (struct bp_location *bl)
12779 {
12780   /* Let's clear the semaphore before removing the location.  */
12781   bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
12782
12783   return bkpt_remove_location (bl);
12784 }
12785
12786 static void
12787 bkpt_probe_create_sals_from_address (char **arg,
12788                                      struct linespec_result *canonical,
12789                                      enum bptype type_wanted,
12790                                      char *addr_start, char **copy_arg)
12791 {
12792   struct linespec_sals lsal;
12793
12794   lsal.sals = parse_probes (arg, canonical);
12795
12796   *copy_arg = xstrdup (canonical->addr_string);
12797   lsal.canonical = xstrdup (*copy_arg);
12798
12799   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12800 }
12801
12802 static void
12803 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
12804                             struct symtabs_and_lines *sals)
12805 {
12806   *sals = parse_probes (s, NULL);
12807   if (!sals->sals)
12808     error (_("probe not found"));
12809 }
12810
12811 /* The breakpoint_ops structure to be used in tracepoints.  */
12812
12813 static void
12814 tracepoint_re_set (struct breakpoint *b)
12815 {
12816   breakpoint_re_set_default (b);
12817 }
12818
12819 static int
12820 tracepoint_breakpoint_hit (const struct bp_location *bl,
12821                            struct address_space *aspace, CORE_ADDR bp_addr,
12822                            const struct target_waitstatus *ws)
12823 {
12824   /* By definition, the inferior does not report stops at
12825      tracepoints.  */
12826   return 0;
12827 }
12828
12829 static void
12830 tracepoint_print_one_detail (const struct breakpoint *self,
12831                              struct ui_out *uiout)
12832 {
12833   struct tracepoint *tp = (struct tracepoint *) self;
12834   if (tp->static_trace_marker_id)
12835     {
12836       gdb_assert (self->type == bp_static_tracepoint);
12837
12838       ui_out_text (uiout, "\tmarker id is ");
12839       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
12840                            tp->static_trace_marker_id);
12841       ui_out_text (uiout, "\n");
12842     }
12843 }
12844
12845 static void
12846 tracepoint_print_mention (struct breakpoint *b)
12847 {
12848   if (ui_out_is_mi_like_p (current_uiout))
12849     return;
12850
12851   switch (b->type)
12852     {
12853     case bp_tracepoint:
12854       printf_filtered (_("Tracepoint"));
12855       printf_filtered (_(" %d"), b->number);
12856       break;
12857     case bp_fast_tracepoint:
12858       printf_filtered (_("Fast tracepoint"));
12859       printf_filtered (_(" %d"), b->number);
12860       break;
12861     case bp_static_tracepoint:
12862       printf_filtered (_("Static tracepoint"));
12863       printf_filtered (_(" %d"), b->number);
12864       break;
12865     default:
12866       internal_error (__FILE__, __LINE__,
12867                       _("unhandled tracepoint type %d"), (int) b->type);
12868     }
12869
12870   say_where (b);
12871 }
12872
12873 static void
12874 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12875 {
12876   struct tracepoint *tp = (struct tracepoint *) self;
12877
12878   if (self->type == bp_fast_tracepoint)
12879     fprintf_unfiltered (fp, "ftrace");
12880   if (self->type == bp_static_tracepoint)
12881     fprintf_unfiltered (fp, "strace");
12882   else if (self->type == bp_tracepoint)
12883     fprintf_unfiltered (fp, "trace");
12884   else
12885     internal_error (__FILE__, __LINE__,
12886                     _("unhandled tracepoint type %d"), (int) self->type);
12887
12888   fprintf_unfiltered (fp, " %s", self->addr_string);
12889   print_recreate_thread (self, fp);
12890
12891   if (tp->pass_count)
12892     fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
12893 }
12894
12895 static void
12896 tracepoint_create_sals_from_address (char **arg,
12897                                      struct linespec_result *canonical,
12898                                      enum bptype type_wanted,
12899                                      char *addr_start, char **copy_arg)
12900 {
12901   create_sals_from_address_default (arg, canonical, type_wanted,
12902                                     addr_start, copy_arg);
12903 }
12904
12905 static void
12906 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12907                                    struct linespec_result *canonical,
12908                                    struct linespec_sals *lsal,
12909                                    char *cond_string,
12910                                    char *extra_string,
12911                                    enum bptype type_wanted,
12912                                    enum bpdisp disposition,
12913                                    int thread,
12914                                    int task, int ignore_count,
12915                                    const struct breakpoint_ops *ops,
12916                                    int from_tty, int enabled,
12917                                    int internal, unsigned flags)
12918 {
12919   create_breakpoints_sal_default (gdbarch, canonical, lsal,
12920                                   cond_string, extra_string,
12921                                   type_wanted,
12922                                   disposition, thread, task,
12923                                   ignore_count, ops, from_tty,
12924                                   enabled, internal, flags);
12925 }
12926
12927 static void
12928 tracepoint_decode_linespec (struct breakpoint *b, char **s,
12929                             struct symtabs_and_lines *sals)
12930 {
12931   decode_linespec_default (b, s, sals);
12932 }
12933
12934 struct breakpoint_ops tracepoint_breakpoint_ops;
12935
12936 /* The breakpoint_ops structure to be use on tracepoints placed in a
12937    static probe.  */
12938
12939 static void
12940 tracepoint_probe_create_sals_from_address (char **arg,
12941                                            struct linespec_result *canonical,
12942                                            enum bptype type_wanted,
12943                                            char *addr_start, char **copy_arg)
12944 {
12945   /* We use the same method for breakpoint on probes.  */
12946   bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
12947                                        addr_start, copy_arg);
12948 }
12949
12950 static void
12951 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
12952                                   struct symtabs_and_lines *sals)
12953 {
12954   /* We use the same method for breakpoint on probes.  */
12955   bkpt_probe_decode_linespec (b, s, sals);
12956 }
12957
12958 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
12959
12960 /* The breakpoint_ops structure to be used on static tracepoints with
12961    markers (`-m').  */
12962
12963 static void
12964 strace_marker_create_sals_from_address (char **arg,
12965                                         struct linespec_result *canonical,
12966                                         enum bptype type_wanted,
12967                                         char *addr_start, char **copy_arg)
12968 {
12969   struct linespec_sals lsal;
12970
12971   lsal.sals = decode_static_tracepoint_spec (arg);
12972
12973   *copy_arg = savestring (addr_start, *arg - addr_start);
12974
12975   canonical->addr_string = xstrdup (*copy_arg);
12976   lsal.canonical = xstrdup (*copy_arg);
12977   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12978 }
12979
12980 static void
12981 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12982                                       struct linespec_result *canonical,
12983                                       struct linespec_sals *lsal,
12984                                       char *cond_string,
12985                                       char *extra_string,
12986                                       enum bptype type_wanted,
12987                                       enum bpdisp disposition,
12988                                       int thread,
12989                                       int task, int ignore_count,
12990                                       const struct breakpoint_ops *ops,
12991                                       int from_tty, int enabled,
12992                                       int internal, unsigned flags)
12993 {
12994   int i;
12995
12996   /* If the user is creating a static tracepoint by marker id
12997      (strace -m MARKER_ID), then store the sals index, so that
12998      breakpoint_re_set can try to match up which of the newly
12999      found markers corresponds to this one, and, don't try to
13000      expand multiple locations for each sal, given than SALS
13001      already should contain all sals for MARKER_ID.  */
13002
13003   for (i = 0; i < lsal->sals.nelts; ++i)
13004     {
13005       struct symtabs_and_lines expanded;
13006       struct tracepoint *tp;
13007       struct cleanup *old_chain;
13008       char *addr_string;
13009
13010       expanded.nelts = 1;
13011       expanded.sals = &lsal->sals.sals[i];
13012
13013       addr_string = xstrdup (canonical->addr_string);
13014       old_chain = make_cleanup (xfree, addr_string);
13015
13016       tp = XCNEW (struct tracepoint);
13017       init_breakpoint_sal (&tp->base, gdbarch, expanded,
13018                            addr_string, NULL,
13019                            cond_string, extra_string,
13020                            type_wanted, disposition,
13021                            thread, task, ignore_count, ops,
13022                            from_tty, enabled, internal, flags,
13023                            canonical->special_display);
13024       /* Given that its possible to have multiple markers with
13025          the same string id, if the user is creating a static
13026          tracepoint by marker id ("strace -m MARKER_ID"), then
13027          store the sals index, so that breakpoint_re_set can
13028          try to match up which of the newly found markers
13029          corresponds to this one  */
13030       tp->static_trace_marker_id_idx = i;
13031
13032       install_breakpoint (internal, &tp->base, 0);
13033
13034       discard_cleanups (old_chain);
13035     }
13036 }
13037
13038 static void
13039 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13040                                struct symtabs_and_lines *sals)
13041 {
13042   struct tracepoint *tp = (struct tracepoint *) b;
13043
13044   *sals = decode_static_tracepoint_spec (s);
13045   if (sals->nelts > tp->static_trace_marker_id_idx)
13046     {
13047       sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13048       sals->nelts = 1;
13049     }
13050   else
13051     error (_("marker %s not found"), tp->static_trace_marker_id);
13052 }
13053
13054 static struct breakpoint_ops strace_marker_breakpoint_ops;
13055
13056 static int
13057 strace_marker_p (struct breakpoint *b)
13058 {
13059   return b->ops == &strace_marker_breakpoint_ops;
13060 }
13061
13062 /* Delete a breakpoint and clean up all traces of it in the data
13063    structures.  */
13064
13065 void
13066 delete_breakpoint (struct breakpoint *bpt)
13067 {
13068   struct breakpoint *b;
13069
13070   gdb_assert (bpt != NULL);
13071
13072   /* Has this bp already been deleted?  This can happen because
13073      multiple lists can hold pointers to bp's.  bpstat lists are
13074      especial culprits.
13075
13076      One example of this happening is a watchpoint's scope bp.  When
13077      the scope bp triggers, we notice that the watchpoint is out of
13078      scope, and delete it.  We also delete its scope bp.  But the
13079      scope bp is marked "auto-deleting", and is already on a bpstat.
13080      That bpstat is then checked for auto-deleting bp's, which are
13081      deleted.
13082
13083      A real solution to this problem might involve reference counts in
13084      bp's, and/or giving them pointers back to their referencing
13085      bpstat's, and teaching delete_breakpoint to only free a bp's
13086      storage when no more references were extent.  A cheaper bandaid
13087      was chosen.  */
13088   if (bpt->type == bp_none)
13089     return;
13090
13091   /* At least avoid this stale reference until the reference counting
13092      of breakpoints gets resolved.  */
13093   if (bpt->related_breakpoint != bpt)
13094     {
13095       struct breakpoint *related;
13096       struct watchpoint *w;
13097
13098       if (bpt->type == bp_watchpoint_scope)
13099         w = (struct watchpoint *) bpt->related_breakpoint;
13100       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13101         w = (struct watchpoint *) bpt;
13102       else
13103         w = NULL;
13104       if (w != NULL)
13105         watchpoint_del_at_next_stop (w);
13106
13107       /* Unlink bpt from the bpt->related_breakpoint ring.  */
13108       for (related = bpt; related->related_breakpoint != bpt;
13109            related = related->related_breakpoint);
13110       related->related_breakpoint = bpt->related_breakpoint;
13111       bpt->related_breakpoint = bpt;
13112     }
13113
13114   /* watch_command_1 creates a watchpoint but only sets its number if
13115      update_watchpoint succeeds in creating its bp_locations.  If there's
13116      a problem in that process, we'll be asked to delete the half-created
13117      watchpoint.  In that case, don't announce the deletion.  */
13118   if (bpt->number)
13119     observer_notify_breakpoint_deleted (bpt);
13120
13121   if (breakpoint_chain == bpt)
13122     breakpoint_chain = bpt->next;
13123
13124   ALL_BREAKPOINTS (b)
13125     if (b->next == bpt)
13126     {
13127       b->next = bpt->next;
13128       break;
13129     }
13130
13131   /* Be sure no bpstat's are pointing at the breakpoint after it's
13132      been freed.  */
13133   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
13134      in all threads for now.  Note that we cannot just remove bpstats
13135      pointing at bpt from the stop_bpstat list entirely, as breakpoint
13136      commands are associated with the bpstat; if we remove it here,
13137      then the later call to bpstat_do_actions (&stop_bpstat); in
13138      event-top.c won't do anything, and temporary breakpoints with
13139      commands won't work.  */
13140
13141   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13142
13143   /* Now that breakpoint is removed from breakpoint list, update the
13144      global location list.  This will remove locations that used to
13145      belong to this breakpoint.  Do this before freeing the breakpoint
13146      itself, since remove_breakpoint looks at location's owner.  It
13147      might be better design to have location completely
13148      self-contained, but it's not the case now.  */
13149   update_global_location_list (0);
13150
13151   bpt->ops->dtor (bpt);
13152   /* On the chance that someone will soon try again to delete this
13153      same bp, we mark it as deleted before freeing its storage.  */
13154   bpt->type = bp_none;
13155   xfree (bpt);
13156 }
13157
13158 static void
13159 do_delete_breakpoint_cleanup (void *b)
13160 {
13161   delete_breakpoint (b);
13162 }
13163
13164 struct cleanup *
13165 make_cleanup_delete_breakpoint (struct breakpoint *b)
13166 {
13167   return make_cleanup (do_delete_breakpoint_cleanup, b);
13168 }
13169
13170 /* Iterator function to call a user-provided callback function once
13171    for each of B and its related breakpoints.  */
13172
13173 static void
13174 iterate_over_related_breakpoints (struct breakpoint *b,
13175                                   void (*function) (struct breakpoint *,
13176                                                     void *),
13177                                   void *data)
13178 {
13179   struct breakpoint *related;
13180
13181   related = b;
13182   do
13183     {
13184       struct breakpoint *next;
13185
13186       /* FUNCTION may delete RELATED.  */
13187       next = related->related_breakpoint;
13188
13189       if (next == related)
13190         {
13191           /* RELATED is the last ring entry.  */
13192           function (related, data);
13193
13194           /* FUNCTION may have deleted it, so we'd never reach back to
13195              B.  There's nothing left to do anyway, so just break
13196              out.  */
13197           break;
13198         }
13199       else
13200         function (related, data);
13201
13202       related = next;
13203     }
13204   while (related != b);
13205 }
13206
13207 static void
13208 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13209 {
13210   delete_breakpoint (b);
13211 }
13212
13213 /* A callback for map_breakpoint_numbers that calls
13214    delete_breakpoint.  */
13215
13216 static void
13217 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13218 {
13219   iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13220 }
13221
13222 void
13223 delete_command (char *arg, int from_tty)
13224 {
13225   struct breakpoint *b, *b_tmp;
13226
13227   dont_repeat ();
13228
13229   if (arg == 0)
13230     {
13231       int breaks_to_delete = 0;
13232
13233       /* Delete all breakpoints if no argument.  Do not delete
13234          internal breakpoints, these have to be deleted with an
13235          explicit breakpoint number argument.  */
13236       ALL_BREAKPOINTS (b)
13237         if (user_breakpoint_p (b))
13238           {
13239             breaks_to_delete = 1;
13240             break;
13241           }
13242
13243       /* Ask user only if there are some breakpoints to delete.  */
13244       if (!from_tty
13245           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13246         {
13247           ALL_BREAKPOINTS_SAFE (b, b_tmp)
13248             if (user_breakpoint_p (b))
13249               delete_breakpoint (b);
13250         }
13251     }
13252   else
13253     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13254 }
13255
13256 static int
13257 all_locations_are_pending (struct bp_location *loc)
13258 {
13259   for (; loc; loc = loc->next)
13260     if (!loc->shlib_disabled
13261         && !loc->pspace->executing_startup)
13262       return 0;
13263   return 1;
13264 }
13265
13266 /* Subroutine of update_breakpoint_locations to simplify it.
13267    Return non-zero if multiple fns in list LOC have the same name.
13268    Null names are ignored.  */
13269
13270 static int
13271 ambiguous_names_p (struct bp_location *loc)
13272 {
13273   struct bp_location *l;
13274   htab_t htab = htab_create_alloc (13, htab_hash_string,
13275                                    (int (*) (const void *, 
13276                                              const void *)) streq,
13277                                    NULL, xcalloc, xfree);
13278
13279   for (l = loc; l != NULL; l = l->next)
13280     {
13281       const char **slot;
13282       const char *name = l->function_name;
13283
13284       /* Allow for some names to be NULL, ignore them.  */
13285       if (name == NULL)
13286         continue;
13287
13288       slot = (const char **) htab_find_slot (htab, (const void *) name,
13289                                              INSERT);
13290       /* NOTE: We can assume slot != NULL here because xcalloc never
13291          returns NULL.  */
13292       if (*slot != NULL)
13293         {
13294           htab_delete (htab);
13295           return 1;
13296         }
13297       *slot = name;
13298     }
13299
13300   htab_delete (htab);
13301   return 0;
13302 }
13303
13304 /* When symbols change, it probably means the sources changed as well,
13305    and it might mean the static tracepoint markers are no longer at
13306    the same address or line numbers they used to be at last we
13307    checked.  Losing your static tracepoints whenever you rebuild is
13308    undesirable.  This function tries to resync/rematch gdb static
13309    tracepoints with the markers on the target, for static tracepoints
13310    that have not been set by marker id.  Static tracepoint that have
13311    been set by marker id are reset by marker id in breakpoint_re_set.
13312    The heuristic is:
13313
13314    1) For a tracepoint set at a specific address, look for a marker at
13315    the old PC.  If one is found there, assume to be the same marker.
13316    If the name / string id of the marker found is different from the
13317    previous known name, assume that means the user renamed the marker
13318    in the sources, and output a warning.
13319
13320    2) For a tracepoint set at a given line number, look for a marker
13321    at the new address of the old line number.  If one is found there,
13322    assume to be the same marker.  If the name / string id of the
13323    marker found is different from the previous known name, assume that
13324    means the user renamed the marker in the sources, and output a
13325    warning.
13326
13327    3) If a marker is no longer found at the same address or line, it
13328    may mean the marker no longer exists.  But it may also just mean
13329    the code changed a bit.  Maybe the user added a few lines of code
13330    that made the marker move up or down (in line number terms).  Ask
13331    the target for info about the marker with the string id as we knew
13332    it.  If found, update line number and address in the matching
13333    static tracepoint.  This will get confused if there's more than one
13334    marker with the same ID (possible in UST, although unadvised
13335    precisely because it confuses tools).  */
13336
13337 static struct symtab_and_line
13338 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13339 {
13340   struct tracepoint *tp = (struct tracepoint *) b;
13341   struct static_tracepoint_marker marker;
13342   CORE_ADDR pc;
13343   int i;
13344
13345   pc = sal.pc;
13346   if (sal.line)
13347     find_line_pc (sal.symtab, sal.line, &pc);
13348
13349   if (target_static_tracepoint_marker_at (pc, &marker))
13350     {
13351       if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13352         warning (_("static tracepoint %d changed probed marker from %s to %s"),
13353                  b->number,
13354                  tp->static_trace_marker_id, marker.str_id);
13355
13356       xfree (tp->static_trace_marker_id);
13357       tp->static_trace_marker_id = xstrdup (marker.str_id);
13358       release_static_tracepoint_marker (&marker);
13359
13360       return sal;
13361     }
13362
13363   /* Old marker wasn't found on target at lineno.  Try looking it up
13364      by string ID.  */
13365   if (!sal.explicit_pc
13366       && sal.line != 0
13367       && sal.symtab != NULL
13368       && tp->static_trace_marker_id != NULL)
13369     {
13370       VEC(static_tracepoint_marker_p) *markers;
13371
13372       markers
13373         = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13374
13375       if (!VEC_empty(static_tracepoint_marker_p, markers))
13376         {
13377           struct symtab_and_line sal2;
13378           struct symbol *sym;
13379           struct static_tracepoint_marker *tpmarker;
13380           struct ui_out *uiout = current_uiout;
13381
13382           tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13383
13384           xfree (tp->static_trace_marker_id);
13385           tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13386
13387           warning (_("marker for static tracepoint %d (%s) not "
13388                      "found at previous line number"),
13389                    b->number, tp->static_trace_marker_id);
13390
13391           init_sal (&sal2);
13392
13393           sal2.pc = tpmarker->address;
13394
13395           sal2 = find_pc_line (tpmarker->address, 0);
13396           sym = find_pc_sect_function (tpmarker->address, NULL);
13397           ui_out_text (uiout, "Now in ");
13398           if (sym)
13399             {
13400               ui_out_field_string (uiout, "func",
13401                                    SYMBOL_PRINT_NAME (sym));
13402               ui_out_text (uiout, " at ");
13403             }
13404           ui_out_field_string (uiout, "file", sal2.symtab->filename);
13405           ui_out_text (uiout, ":");
13406
13407           if (ui_out_is_mi_like_p (uiout))
13408             {
13409               char *fullname = symtab_to_fullname (sal2.symtab);
13410
13411               if (fullname)
13412                 ui_out_field_string (uiout, "fullname", fullname);
13413             }
13414
13415           ui_out_field_int (uiout, "line", sal2.line);
13416           ui_out_text (uiout, "\n");
13417
13418           b->loc->line_number = sal2.line;
13419
13420           xfree (b->loc->source_file);
13421           if (sym)
13422             b->loc->source_file = xstrdup (sal2.symtab->filename);
13423           else
13424             b->loc->source_file = NULL;
13425
13426           xfree (b->addr_string);
13427           b->addr_string = xstrprintf ("%s:%d",
13428                                        sal2.symtab->filename,
13429                                        b->loc->line_number);
13430
13431           /* Might be nice to check if function changed, and warn if
13432              so.  */
13433
13434           release_static_tracepoint_marker (tpmarker);
13435         }
13436     }
13437   return sal;
13438 }
13439
13440 /* Returns 1 iff locations A and B are sufficiently same that
13441    we don't need to report breakpoint as changed.  */
13442
13443 static int
13444 locations_are_equal (struct bp_location *a, struct bp_location *b)
13445 {
13446   while (a && b)
13447     {
13448       if (a->address != b->address)
13449         return 0;
13450
13451       if (a->shlib_disabled != b->shlib_disabled)
13452         return 0;
13453
13454       if (a->enabled != b->enabled)
13455         return 0;
13456
13457       a = a->next;
13458       b = b->next;
13459     }
13460
13461   if ((a == NULL) != (b == NULL))
13462     return 0;
13463
13464   return 1;
13465 }
13466
13467 /* Create new breakpoint locations for B (a hardware or software breakpoint)
13468    based on SALS and SALS_END.  If SALS_END.NELTS is not zero, then B is
13469    a ranged breakpoint.  */
13470
13471 void
13472 update_breakpoint_locations (struct breakpoint *b,
13473                              struct symtabs_and_lines sals,
13474                              struct symtabs_and_lines sals_end)
13475 {
13476   int i;
13477   struct bp_location *existing_locations = b->loc;
13478
13479   if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13480     {
13481       /* Ranged breakpoints have only one start location and one end
13482          location.  */
13483       b->enable_state = bp_disabled;
13484       update_global_location_list (1);
13485       printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13486                            "multiple locations found\n"),
13487                          b->number);
13488       return;
13489     }
13490
13491   /* If there's no new locations, and all existing locations are
13492      pending, don't do anything.  This optimizes the common case where
13493      all locations are in the same shared library, that was unloaded.
13494      We'd like to retain the location, so that when the library is
13495      loaded again, we don't loose the enabled/disabled status of the
13496      individual locations.  */
13497   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
13498     return;
13499
13500   b->loc = NULL;
13501
13502   for (i = 0; i < sals.nelts; ++i)
13503     {
13504       struct bp_location *new_loc;
13505
13506       switch_to_program_space_and_thread (sals.sals[i].pspace);
13507
13508       new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
13509
13510       /* Reparse conditions, they might contain references to the
13511          old symtab.  */
13512       if (b->cond_string != NULL)
13513         {
13514           char *s;
13515           volatile struct gdb_exception e;
13516
13517           s = b->cond_string;
13518           TRY_CATCH (e, RETURN_MASK_ERROR)
13519             {
13520               new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 
13521                                            0);
13522             }
13523           if (e.reason < 0)
13524             {
13525               warning (_("failed to reevaluate condition "
13526                          "for breakpoint %d: %s"), 
13527                        b->number, e.message);
13528               new_loc->enabled = 0;
13529             }
13530         }
13531
13532       if (sals_end.nelts)
13533         {
13534           CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13535
13536           new_loc->length = end - sals.sals[0].pc + 1;
13537         }
13538     }
13539
13540   /* Update locations of permanent breakpoints.  */
13541   if (b->enable_state == bp_permanent)
13542     make_breakpoint_permanent (b);
13543
13544   /* If possible, carry over 'disable' status from existing
13545      breakpoints.  */
13546   {
13547     struct bp_location *e = existing_locations;
13548     /* If there are multiple breakpoints with the same function name,
13549        e.g. for inline functions, comparing function names won't work.
13550        Instead compare pc addresses; this is just a heuristic as things
13551        may have moved, but in practice it gives the correct answer
13552        often enough until a better solution is found.  */
13553     int have_ambiguous_names = ambiguous_names_p (b->loc);
13554
13555     for (; e; e = e->next)
13556       {
13557         if (!e->enabled && e->function_name)
13558           {
13559             struct bp_location *l = b->loc;
13560             if (have_ambiguous_names)
13561               {
13562                 for (; l; l = l->next)
13563                   if (breakpoint_locations_match (e, l))
13564                     {
13565                       l->enabled = 0;
13566                       break;
13567                     }
13568               }
13569             else
13570               {
13571                 for (; l; l = l->next)
13572                   if (l->function_name
13573                       && strcmp (e->function_name, l->function_name) == 0)
13574                     {
13575                       l->enabled = 0;
13576                       break;
13577                     }
13578               }
13579           }
13580       }
13581   }
13582
13583   if (!locations_are_equal (existing_locations, b->loc))
13584     observer_notify_breakpoint_modified (b);
13585
13586   update_global_location_list (1);
13587 }
13588
13589 /* Find the SaL locations corresponding to the given ADDR_STRING.
13590    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
13591
13592 static struct symtabs_and_lines
13593 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13594 {
13595   char *s;
13596   struct symtabs_and_lines sals = {0};
13597   volatile struct gdb_exception e;
13598
13599   gdb_assert (b->ops != NULL);
13600   s = addr_string;
13601
13602   TRY_CATCH (e, RETURN_MASK_ERROR)
13603     {
13604       b->ops->decode_linespec (b, &s, &sals);
13605     }
13606   if (e.reason < 0)
13607     {
13608       int not_found_and_ok = 0;
13609       /* For pending breakpoints, it's expected that parsing will
13610          fail until the right shared library is loaded.  User has
13611          already told to create pending breakpoints and don't need
13612          extra messages.  If breakpoint is in bp_shlib_disabled
13613          state, then user already saw the message about that
13614          breakpoint being disabled, and don't want to see more
13615          errors.  */
13616       if (e.error == NOT_FOUND_ERROR
13617           && (b->condition_not_parsed 
13618               || (b->loc && b->loc->shlib_disabled)
13619               || (b->loc && b->loc->pspace->executing_startup)
13620               || b->enable_state == bp_disabled))
13621         not_found_and_ok = 1;
13622
13623       if (!not_found_and_ok)
13624         {
13625           /* We surely don't want to warn about the same breakpoint
13626              10 times.  One solution, implemented here, is disable
13627              the breakpoint on error.  Another solution would be to
13628              have separate 'warning emitted' flag.  Since this
13629              happens only when a binary has changed, I don't know
13630              which approach is better.  */
13631           b->enable_state = bp_disabled;
13632           throw_exception (e);
13633         }
13634     }
13635
13636   if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
13637     {
13638       int i;
13639
13640       for (i = 0; i < sals.nelts; ++i)
13641         resolve_sal_pc (&sals.sals[i]);
13642       if (b->condition_not_parsed && s && s[0])
13643         {
13644           char *cond_string = 0;
13645           int thread = -1;
13646           int task = 0;
13647           char *extra_string = NULL;
13648
13649           find_condition_and_thread (s, sals.sals[0].pc,
13650                                      &cond_string, &thread, &task,
13651                                      &extra_string);
13652           if (cond_string)
13653             b->cond_string = cond_string;
13654           b->thread = thread;
13655           b->task = task;
13656           if (extra_string)
13657             b->extra_string = extra_string;
13658           b->condition_not_parsed = 0;
13659         }
13660
13661       if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13662         sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
13663
13664       *found = 1;
13665     }
13666   else
13667     *found = 0;
13668
13669   return sals;
13670 }
13671
13672 /* The default re_set method, for typical hardware or software
13673    breakpoints.  Reevaluate the breakpoint and recreate its
13674    locations.  */
13675
13676 static void
13677 breakpoint_re_set_default (struct breakpoint *b)
13678 {
13679   int found;
13680   struct symtabs_and_lines sals, sals_end;
13681   struct symtabs_and_lines expanded = {0};
13682   struct symtabs_and_lines expanded_end = {0};
13683
13684   sals = addr_string_to_sals (b, b->addr_string, &found);
13685   if (found)
13686     {
13687       make_cleanup (xfree, sals.sals);
13688       expanded = sals;
13689     }
13690
13691   if (b->addr_string_range_end)
13692     {
13693       sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13694       if (found)
13695         {
13696           make_cleanup (xfree, sals_end.sals);
13697           expanded_end = sals_end;
13698         }
13699     }
13700
13701   update_breakpoint_locations (b, expanded, expanded_end);
13702 }
13703
13704 /* Default method for creating SALs from an address string.  It basically
13705    calls parse_breakpoint_sals.  Return 1 for success, zero for failure.  */
13706
13707 static void
13708 create_sals_from_address_default (char **arg,
13709                                   struct linespec_result *canonical,
13710                                   enum bptype type_wanted,
13711                                   char *addr_start, char **copy_arg)
13712 {
13713   parse_breakpoint_sals (arg, canonical);
13714 }
13715
13716 /* Call create_breakpoints_sal for the given arguments.  This is the default
13717    function for the `create_breakpoints_sal' method of
13718    breakpoint_ops.  */
13719
13720 static void
13721 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13722                                 struct linespec_result *canonical,
13723                                 struct linespec_sals *lsal,
13724                                 char *cond_string,
13725                                 char *extra_string,
13726                                 enum bptype type_wanted,
13727                                 enum bpdisp disposition,
13728                                 int thread,
13729                                 int task, int ignore_count,
13730                                 const struct breakpoint_ops *ops,
13731                                 int from_tty, int enabled,
13732                                 int internal, unsigned flags)
13733 {
13734   create_breakpoints_sal (gdbarch, canonical, cond_string,
13735                           extra_string,
13736                           type_wanted, disposition,
13737                           thread, task, ignore_count, ops, from_tty,
13738                           enabled, internal, flags);
13739 }
13740
13741 /* Decode the line represented by S by calling decode_line_full.  This is the
13742    default function for the `decode_linespec' method of breakpoint_ops.  */
13743
13744 static void
13745 decode_linespec_default (struct breakpoint *b, char **s,
13746                          struct symtabs_and_lines *sals)
13747 {
13748   struct linespec_result canonical;
13749
13750   init_linespec_result (&canonical);
13751   decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13752                     (struct symtab *) NULL, 0,
13753                     &canonical, multiple_symbols_all,
13754                     b->filter);
13755
13756   /* We should get 0 or 1 resulting SALs.  */
13757   gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13758
13759   if (VEC_length (linespec_sals, canonical.sals) > 0)
13760     {
13761       struct linespec_sals *lsal;
13762
13763       lsal = VEC_index (linespec_sals, canonical.sals, 0);
13764       *sals = lsal->sals;
13765       /* Arrange it so the destructor does not free the
13766          contents.  */
13767       lsal->sals.sals = NULL;
13768     }
13769
13770   destroy_linespec_result (&canonical);
13771 }
13772
13773 /* Prepare the global context for a re-set of breakpoint B.  */
13774
13775 static struct cleanup *
13776 prepare_re_set_context (struct breakpoint *b)
13777 {
13778   struct cleanup *cleanups;
13779
13780   input_radix = b->input_radix;
13781   cleanups = save_current_space_and_thread ();
13782   if (b->pspace != NULL)
13783     switch_to_program_space_and_thread (b->pspace);
13784   set_language (b->language);
13785
13786   return cleanups;
13787 }
13788
13789 /* Reset a breakpoint given it's struct breakpoint * BINT.
13790    The value we return ends up being the return value from catch_errors.
13791    Unused in this case.  */
13792
13793 static int
13794 breakpoint_re_set_one (void *bint)
13795 {
13796   /* Get past catch_errs.  */
13797   struct breakpoint *b = (struct breakpoint *) bint;
13798   struct cleanup *cleanups;
13799
13800   cleanups = prepare_re_set_context (b);
13801   b->ops->re_set (b);
13802   do_cleanups (cleanups);
13803   return 0;
13804 }
13805
13806 /* Re-set all breakpoints after symbols have been re-loaded.  */
13807 void
13808 breakpoint_re_set (void)
13809 {
13810   struct breakpoint *b, *b_tmp;
13811   enum language save_language;
13812   int save_input_radix;
13813   struct cleanup *old_chain;
13814
13815   save_language = current_language->la_language;
13816   save_input_radix = input_radix;
13817   old_chain = save_current_program_space ();
13818
13819   ALL_BREAKPOINTS_SAFE (b, b_tmp)
13820   {
13821     /* Format possible error msg.  */
13822     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
13823                                 b->number);
13824     struct cleanup *cleanups = make_cleanup (xfree, message);
13825     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
13826     do_cleanups (cleanups);
13827   }
13828   set_language (save_language);
13829   input_radix = save_input_radix;
13830
13831   jit_breakpoint_re_set ();
13832
13833   do_cleanups (old_chain);
13834
13835   create_overlay_event_breakpoint ();
13836   create_longjmp_master_breakpoint ();
13837   create_std_terminate_master_breakpoint ();
13838   create_exception_master_breakpoint ();
13839
13840   /* While we're at it, reset the skip list too.  */
13841   skip_re_set ();
13842 }
13843 \f
13844 /* Reset the thread number of this breakpoint:
13845
13846    - If the breakpoint is for all threads, leave it as-is.
13847    - Else, reset it to the current thread for inferior_ptid.  */
13848 void
13849 breakpoint_re_set_thread (struct breakpoint *b)
13850 {
13851   if (b->thread != -1)
13852     {
13853       if (in_thread_list (inferior_ptid))
13854         b->thread = pid_to_thread_id (inferior_ptid);
13855
13856       /* We're being called after following a fork.  The new fork is
13857          selected as current, and unless this was a vfork will have a
13858          different program space from the original thread.  Reset that
13859          as well.  */
13860       b->loc->pspace = current_program_space;
13861     }
13862 }
13863
13864 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13865    If from_tty is nonzero, it prints a message to that effect,
13866    which ends with a period (no newline).  */
13867
13868 void
13869 set_ignore_count (int bptnum, int count, int from_tty)
13870 {
13871   struct breakpoint *b;
13872
13873   if (count < 0)
13874     count = 0;
13875
13876   ALL_BREAKPOINTS (b)
13877     if (b->number == bptnum)
13878     {
13879       if (is_tracepoint (b))
13880         {
13881           if (from_tty && count != 0)
13882             printf_filtered (_("Ignore count ignored for tracepoint %d."),
13883                              bptnum);
13884           return;
13885         }
13886       
13887       b->ignore_count = count;
13888       if (from_tty)
13889         {
13890           if (count == 0)
13891             printf_filtered (_("Will stop next time "
13892                                "breakpoint %d is reached."),
13893                              bptnum);
13894           else if (count == 1)
13895             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13896                              bptnum);
13897           else
13898             printf_filtered (_("Will ignore next %d "
13899                                "crossings of breakpoint %d."),
13900                              count, bptnum);
13901         }
13902       breakpoints_changed ();
13903       observer_notify_breakpoint_modified (b);
13904       return;
13905     }
13906
13907   error (_("No breakpoint number %d."), bptnum);
13908 }
13909
13910 /* Command to set ignore-count of breakpoint N to COUNT.  */
13911
13912 static void
13913 ignore_command (char *args, int from_tty)
13914 {
13915   char *p = args;
13916   int num;
13917
13918   if (p == 0)
13919     error_no_arg (_("a breakpoint number"));
13920
13921   num = get_number (&p);
13922   if (num == 0)
13923     error (_("bad breakpoint number: '%s'"), args);
13924   if (*p == 0)
13925     error (_("Second argument (specified ignore-count) is missing."));
13926
13927   set_ignore_count (num,
13928                     longest_to_int (value_as_long (parse_and_eval (p))),
13929                     from_tty);
13930   if (from_tty)
13931     printf_filtered ("\n");
13932 }
13933 \f
13934 /* Call FUNCTION on each of the breakpoints
13935    whose numbers are given in ARGS.  */
13936
13937 static void
13938 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
13939                                                       void *),
13940                         void *data)
13941 {
13942   int num;
13943   struct breakpoint *b, *tmp;
13944   int match;
13945   struct get_number_or_range_state state;
13946
13947   if (args == 0)
13948     error_no_arg (_("one or more breakpoint numbers"));
13949
13950   init_number_or_range (&state, args);
13951
13952   while (!state.finished)
13953     {
13954       char *p = state.string;
13955
13956       match = 0;
13957
13958       num = get_number_or_range (&state);
13959       if (num == 0)
13960         {
13961           warning (_("bad breakpoint number at or near '%s'"), p);
13962         }
13963       else
13964         {
13965           ALL_BREAKPOINTS_SAFE (b, tmp)
13966             if (b->number == num)
13967               {
13968                 match = 1;
13969                 function (b, data);
13970                 break;
13971               }
13972           if (match == 0)
13973             printf_unfiltered (_("No breakpoint number %d.\n"), num);
13974         }
13975     }
13976 }
13977
13978 static struct bp_location *
13979 find_location_by_number (char *number)
13980 {
13981   char *dot = strchr (number, '.');
13982   char *p1;
13983   int bp_num;
13984   int loc_num;
13985   struct breakpoint *b;
13986   struct bp_location *loc;  
13987
13988   *dot = '\0';
13989
13990   p1 = number;
13991   bp_num = get_number (&p1);
13992   if (bp_num == 0)
13993     error (_("Bad breakpoint number '%s'"), number);
13994
13995   ALL_BREAKPOINTS (b)
13996     if (b->number == bp_num)
13997       {
13998         break;
13999       }
14000
14001   if (!b || b->number != bp_num)
14002     error (_("Bad breakpoint number '%s'"), number);
14003   
14004   p1 = dot+1;
14005   loc_num = get_number (&p1);
14006   if (loc_num == 0)
14007     error (_("Bad breakpoint location number '%s'"), number);
14008
14009   --loc_num;
14010   loc = b->loc;
14011   for (;loc_num && loc; --loc_num, loc = loc->next)
14012     ;
14013   if (!loc)
14014     error (_("Bad breakpoint location number '%s'"), dot+1);
14015     
14016   return loc;  
14017 }
14018
14019
14020 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14021    If from_tty is nonzero, it prints a message to that effect,
14022    which ends with a period (no newline).  */
14023
14024 void
14025 disable_breakpoint (struct breakpoint *bpt)
14026 {
14027   /* Never disable a watchpoint scope breakpoint; we want to
14028      hit them when we leave scope so we can delete both the
14029      watchpoint and its scope breakpoint at that time.  */
14030   if (bpt->type == bp_watchpoint_scope)
14031     return;
14032
14033   /* You can't disable permanent breakpoints.  */
14034   if (bpt->enable_state == bp_permanent)
14035     return;
14036
14037   bpt->enable_state = bp_disabled;
14038
14039   /* Mark breakpoint locations modified.  */
14040   mark_breakpoint_modified (bpt);
14041
14042   if (target_supports_enable_disable_tracepoint ()
14043       && current_trace_status ()->running && is_tracepoint (bpt))
14044     {
14045       struct bp_location *location;
14046      
14047       for (location = bpt->loc; location; location = location->next)
14048         target_disable_tracepoint (location);
14049     }
14050
14051   update_global_location_list (0);
14052
14053   observer_notify_breakpoint_modified (bpt);
14054 }
14055
14056 /* A callback for iterate_over_related_breakpoints.  */
14057
14058 static void
14059 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14060 {
14061   disable_breakpoint (b);
14062 }
14063
14064 /* A callback for map_breakpoint_numbers that calls
14065    disable_breakpoint.  */
14066
14067 static void
14068 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14069 {
14070   iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14071 }
14072
14073 static void
14074 disable_command (char *args, int from_tty)
14075 {
14076   if (args == 0)
14077     {
14078       struct breakpoint *bpt;
14079
14080       ALL_BREAKPOINTS (bpt)
14081         if (user_breakpoint_p (bpt))
14082           disable_breakpoint (bpt);
14083     }
14084   else if (strchr (args, '.'))
14085     {
14086       struct bp_location *loc = find_location_by_number (args);
14087       if (loc)
14088         {
14089           if (loc->enabled)
14090             {
14091               loc->enabled = 0;
14092               mark_breakpoint_location_modified (loc);
14093             }
14094           if (target_supports_enable_disable_tracepoint ()
14095               && current_trace_status ()->running && loc->owner
14096               && is_tracepoint (loc->owner))
14097             target_disable_tracepoint (loc);
14098         }
14099       update_global_location_list (0);
14100     }
14101   else
14102     map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
14103 }
14104
14105 static void
14106 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14107                         int count)
14108 {
14109   int target_resources_ok;
14110
14111   if (bpt->type == bp_hardware_breakpoint)
14112     {
14113       int i;
14114       i = hw_breakpoint_used_count ();
14115       target_resources_ok = 
14116         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
14117                                             i + 1, 0);
14118       if (target_resources_ok == 0)
14119         error (_("No hardware breakpoint support in the target."));
14120       else if (target_resources_ok < 0)
14121         error (_("Hardware breakpoints used exceeds limit."));
14122     }
14123
14124   if (is_watchpoint (bpt))
14125     {
14126       /* Initialize it just to avoid a GCC false warning.  */
14127       enum enable_state orig_enable_state = 0;
14128       volatile struct gdb_exception e;
14129
14130       TRY_CATCH (e, RETURN_MASK_ALL)
14131         {
14132           struct watchpoint *w = (struct watchpoint *) bpt;
14133
14134           orig_enable_state = bpt->enable_state;
14135           bpt->enable_state = bp_enabled;
14136           update_watchpoint (w, 1 /* reparse */);
14137         }
14138       if (e.reason < 0)
14139         {
14140           bpt->enable_state = orig_enable_state;
14141           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14142                              bpt->number);
14143           return;
14144         }
14145     }
14146
14147   if (bpt->enable_state != bp_permanent)
14148     bpt->enable_state = bp_enabled;
14149
14150   bpt->enable_state = bp_enabled;
14151
14152   /* Mark breakpoint locations modified.  */
14153   mark_breakpoint_modified (bpt);
14154
14155   if (target_supports_enable_disable_tracepoint ()
14156       && current_trace_status ()->running && is_tracepoint (bpt))
14157     {
14158       struct bp_location *location;
14159
14160       for (location = bpt->loc; location; location = location->next)
14161         target_enable_tracepoint (location);
14162     }
14163
14164   bpt->disposition = disposition;
14165   bpt->enable_count = count;
14166   update_global_location_list (1);
14167   breakpoints_changed ();
14168   
14169   observer_notify_breakpoint_modified (bpt);
14170 }
14171
14172
14173 void
14174 enable_breakpoint (struct breakpoint *bpt)
14175 {
14176   enable_breakpoint_disp (bpt, bpt->disposition, 0);
14177 }
14178
14179 static void
14180 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14181 {
14182   enable_breakpoint (bpt);
14183 }
14184
14185 /* A callback for map_breakpoint_numbers that calls
14186    enable_breakpoint.  */
14187
14188 static void
14189 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14190 {
14191   iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14192 }
14193
14194 /* The enable command enables the specified breakpoints (or all defined
14195    breakpoints) so they once again become (or continue to be) effective
14196    in stopping the inferior.  */
14197
14198 static void
14199 enable_command (char *args, int from_tty)
14200 {
14201   if (args == 0)
14202     {
14203       struct breakpoint *bpt;
14204
14205       ALL_BREAKPOINTS (bpt)
14206         if (user_breakpoint_p (bpt))
14207           enable_breakpoint (bpt);
14208     }
14209   else if (strchr (args, '.'))
14210     {
14211       struct bp_location *loc = find_location_by_number (args);
14212       if (loc)
14213         {
14214           if (!loc->enabled)
14215             {
14216               loc->enabled = 1;
14217               mark_breakpoint_location_modified (loc);
14218             }
14219           if (target_supports_enable_disable_tracepoint ()
14220               && current_trace_status ()->running && loc->owner
14221               && is_tracepoint (loc->owner))
14222             target_enable_tracepoint (loc);
14223         }
14224       update_global_location_list (1);
14225     }
14226   else
14227     map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
14228 }
14229
14230 /* This struct packages up disposition data for application to multiple
14231    breakpoints.  */
14232
14233 struct disp_data
14234 {
14235   enum bpdisp disp;
14236   int count;
14237 };
14238
14239 static void
14240 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14241 {
14242   struct disp_data disp_data = *(struct disp_data *) arg;
14243
14244   enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14245 }
14246
14247 static void
14248 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14249 {
14250   struct disp_data disp = { disp_disable, 1 };
14251
14252   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14253 }
14254
14255 static void
14256 enable_once_command (char *args, int from_tty)
14257 {
14258   map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14259 }
14260
14261 static void
14262 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14263 {
14264   struct disp_data disp = { disp_disable, *(int *) countptr };
14265
14266   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14267 }
14268
14269 static void
14270 enable_count_command (char *args, int from_tty)
14271 {
14272   int count = get_number (&args);
14273
14274   map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14275 }
14276
14277 static void
14278 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14279 {
14280   struct disp_data disp = { disp_del, 1 };
14281
14282   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14283 }
14284
14285 static void
14286 enable_delete_command (char *args, int from_tty)
14287 {
14288   map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14289 }
14290 \f
14291 static void
14292 set_breakpoint_cmd (char *args, int from_tty)
14293 {
14294 }
14295
14296 static void
14297 show_breakpoint_cmd (char *args, int from_tty)
14298 {
14299 }
14300
14301 /* Invalidate last known value of any hardware watchpoint if
14302    the memory which that value represents has been written to by
14303    GDB itself.  */
14304
14305 static void
14306 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
14307                                       const bfd_byte *data)
14308 {
14309   struct breakpoint *bp;
14310
14311   ALL_BREAKPOINTS (bp)
14312     if (bp->enable_state == bp_enabled
14313         && bp->type == bp_hardware_watchpoint)
14314       {
14315         struct watchpoint *wp = (struct watchpoint *) bp;
14316
14317         if (wp->val_valid && wp->val)
14318           {
14319             struct bp_location *loc;
14320
14321             for (loc = bp->loc; loc != NULL; loc = loc->next)
14322               if (loc->loc_type == bp_loc_hardware_watchpoint
14323                   && loc->address + loc->length > addr
14324                   && addr + len > loc->address)
14325                 {
14326                   value_free (wp->val);
14327                   wp->val = NULL;
14328                   wp->val_valid = 0;
14329                 }
14330           }
14331       }
14332 }
14333
14334 /* Use the last displayed codepoint's values, or nothing
14335    if they aren't valid.  */
14336
14337 struct symtabs_and_lines
14338 decode_line_spec_1 (char *string, int flags)
14339 {
14340   struct symtabs_and_lines sals;
14341
14342   if (string == 0)
14343     error (_("Empty line specification."));
14344   if (last_displayed_sal_is_valid ())
14345     sals = decode_line_1 (&string, flags,
14346                           get_last_displayed_symtab (),
14347                           get_last_displayed_line ());
14348   else
14349     sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
14350   if (*string)
14351     error (_("Junk at end of line specification: %s"), string);
14352   return sals;
14353 }
14354
14355 /* Create and insert a raw software breakpoint at PC.  Return an
14356    identifier, which should be used to remove the breakpoint later.
14357    In general, places which call this should be using something on the
14358    breakpoint chain instead; this function should be eliminated
14359    someday.  */
14360
14361 void *
14362 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14363                                   struct address_space *aspace, CORE_ADDR pc)
14364 {
14365   struct bp_target_info *bp_tgt;
14366
14367   bp_tgt = XZALLOC (struct bp_target_info);
14368
14369   bp_tgt->placed_address_space = aspace;
14370   bp_tgt->placed_address = pc;
14371
14372   if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
14373     {
14374       /* Could not insert the breakpoint.  */
14375       xfree (bp_tgt);
14376       return NULL;
14377     }
14378
14379   return bp_tgt;
14380 }
14381
14382 /* Remove a breakpoint BP inserted by
14383    deprecated_insert_raw_breakpoint.  */
14384
14385 int
14386 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
14387 {
14388   struct bp_target_info *bp_tgt = bp;
14389   int ret;
14390
14391   ret = target_remove_breakpoint (gdbarch, bp_tgt);
14392   xfree (bp_tgt);
14393
14394   return ret;
14395 }
14396
14397 /* One (or perhaps two) breakpoints used for software single
14398    stepping.  */
14399
14400 static void *single_step_breakpoints[2];
14401 static struct gdbarch *single_step_gdbarch[2];
14402
14403 /* Create and insert a breakpoint for software single step.  */
14404
14405 void
14406 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14407                                struct address_space *aspace, 
14408                                CORE_ADDR next_pc)
14409 {
14410   void **bpt_p;
14411
14412   if (single_step_breakpoints[0] == NULL)
14413     {
14414       bpt_p = &single_step_breakpoints[0];
14415       single_step_gdbarch[0] = gdbarch;
14416     }
14417   else
14418     {
14419       gdb_assert (single_step_breakpoints[1] == NULL);
14420       bpt_p = &single_step_breakpoints[1];
14421       single_step_gdbarch[1] = gdbarch;
14422     }
14423
14424   /* NOTE drow/2006-04-11: A future improvement to this function would
14425      be to only create the breakpoints once, and actually put them on
14426      the breakpoint chain.  That would let us use set_raw_breakpoint.
14427      We could adjust the addresses each time they were needed.  Doing
14428      this requires corresponding changes elsewhere where single step
14429      breakpoints are handled, however.  So, for now, we use this.  */
14430
14431   *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
14432   if (*bpt_p == NULL)
14433     error (_("Could not insert single-step breakpoint at %s"),
14434              paddress (gdbarch, next_pc));
14435 }
14436
14437 /* Check if the breakpoints used for software single stepping
14438    were inserted or not.  */
14439
14440 int
14441 single_step_breakpoints_inserted (void)
14442 {
14443   return (single_step_breakpoints[0] != NULL
14444           || single_step_breakpoints[1] != NULL);
14445 }
14446
14447 /* Remove and delete any breakpoints used for software single step.  */
14448
14449 void
14450 remove_single_step_breakpoints (void)
14451 {
14452   gdb_assert (single_step_breakpoints[0] != NULL);
14453
14454   /* See insert_single_step_breakpoint for more about this deprecated
14455      call.  */
14456   deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14457                                     single_step_breakpoints[0]);
14458   single_step_gdbarch[0] = NULL;
14459   single_step_breakpoints[0] = NULL;
14460
14461   if (single_step_breakpoints[1] != NULL)
14462     {
14463       deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14464                                         single_step_breakpoints[1]);
14465       single_step_gdbarch[1] = NULL;
14466       single_step_breakpoints[1] = NULL;
14467     }
14468 }
14469
14470 /* Delete software single step breakpoints without removing them from
14471    the inferior.  This is intended to be used if the inferior's address
14472    space where they were inserted is already gone, e.g. after exit or
14473    exec.  */
14474
14475 void
14476 cancel_single_step_breakpoints (void)
14477 {
14478   int i;
14479
14480   for (i = 0; i < 2; i++)
14481     if (single_step_breakpoints[i])
14482       {
14483         xfree (single_step_breakpoints[i]);
14484         single_step_breakpoints[i] = NULL;
14485         single_step_gdbarch[i] = NULL;
14486       }
14487 }
14488
14489 /* Detach software single-step breakpoints from INFERIOR_PTID without
14490    removing them.  */
14491
14492 static void
14493 detach_single_step_breakpoints (void)
14494 {
14495   int i;
14496
14497   for (i = 0; i < 2; i++)
14498     if (single_step_breakpoints[i])
14499       target_remove_breakpoint (single_step_gdbarch[i],
14500                                 single_step_breakpoints[i]);
14501 }
14502
14503 /* Check whether a software single-step breakpoint is inserted at
14504    PC.  */
14505
14506 static int
14507 single_step_breakpoint_inserted_here_p (struct address_space *aspace, 
14508                                         CORE_ADDR pc)
14509 {
14510   int i;
14511
14512   for (i = 0; i < 2; i++)
14513     {
14514       struct bp_target_info *bp_tgt = single_step_breakpoints[i];
14515       if (bp_tgt
14516           && breakpoint_address_match (bp_tgt->placed_address_space,
14517                                        bp_tgt->placed_address,
14518                                        aspace, pc))
14519         return 1;
14520     }
14521
14522   return 0;
14523 }
14524
14525 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
14526    non-zero otherwise.  */
14527 static int
14528 is_syscall_catchpoint_enabled (struct breakpoint *bp)
14529 {
14530   if (syscall_catchpoint_p (bp)
14531       && bp->enable_state != bp_disabled
14532       && bp->enable_state != bp_call_disabled)
14533     return 1;
14534   else
14535     return 0;
14536 }
14537
14538 int
14539 catch_syscall_enabled (void)
14540 {
14541   struct catch_syscall_inferior_data *inf_data
14542     = get_catch_syscall_inferior_data (current_inferior ());
14543
14544   return inf_data->total_syscalls_count != 0;
14545 }
14546
14547 int
14548 catching_syscall_number (int syscall_number)
14549 {
14550   struct breakpoint *bp;
14551
14552   ALL_BREAKPOINTS (bp)
14553     if (is_syscall_catchpoint_enabled (bp))
14554       {
14555         struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14556
14557         if (c->syscalls_to_be_caught)
14558           {
14559             int i, iter;
14560             for (i = 0;
14561                  VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
14562                  i++)
14563               if (syscall_number == iter)
14564                 return 1;
14565           }
14566         else
14567           return 1;
14568       }
14569
14570   return 0;
14571 }
14572
14573 /* Complete syscall names.  Used by "catch syscall".  */
14574 static char **
14575 catch_syscall_completer (struct cmd_list_element *cmd,
14576                          char *text, char *word)
14577 {
14578   const char **list = get_syscall_names ();
14579   char **retlist
14580     = (list == NULL) ? NULL : complete_on_enum (list, text, word);
14581
14582   xfree (list);
14583   return retlist;
14584 }
14585
14586 /* Tracepoint-specific operations.  */
14587
14588 /* Set tracepoint count to NUM.  */
14589 static void
14590 set_tracepoint_count (int num)
14591 {
14592   tracepoint_count = num;
14593   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14594 }
14595
14596 static void
14597 trace_command (char *arg, int from_tty)
14598 {
14599   struct breakpoint_ops *ops;
14600   const char *arg_cp = arg;
14601
14602   if (arg && probe_linespec_to_ops (&arg_cp))
14603     ops = &tracepoint_probe_breakpoint_ops;
14604   else
14605     ops = &tracepoint_breakpoint_ops;
14606
14607   if (create_breakpoint (get_current_arch (),
14608                          arg,
14609                          NULL, 0, NULL, 1 /* parse arg */,
14610                          0 /* tempflag */,
14611                          bp_tracepoint /* type_wanted */,
14612                          0 /* Ignore count */,
14613                          pending_break_support,
14614                          ops,
14615                          from_tty,
14616                          1 /* enabled */,
14617                          0 /* internal */, 0))
14618     set_tracepoint_count (breakpoint_count);
14619 }
14620
14621 static void
14622 ftrace_command (char *arg, int from_tty)
14623 {
14624   if (create_breakpoint (get_current_arch (),
14625                          arg,
14626                          NULL, 0, NULL, 1 /* parse arg */,
14627                          0 /* tempflag */,
14628                          bp_fast_tracepoint /* type_wanted */,
14629                          0 /* Ignore count */,
14630                          pending_break_support,
14631                          &tracepoint_breakpoint_ops,
14632                          from_tty,
14633                          1 /* enabled */,
14634                          0 /* internal */, 0))
14635     set_tracepoint_count (breakpoint_count);
14636 }
14637
14638 /* strace command implementation.  Creates a static tracepoint.  */
14639
14640 static void
14641 strace_command (char *arg, int from_tty)
14642 {
14643   struct breakpoint_ops *ops;
14644
14645   /* Decide if we are dealing with a static tracepoint marker (`-m'),
14646      or with a normal static tracepoint.  */
14647   if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14648     ops = &strace_marker_breakpoint_ops;
14649   else
14650     ops = &tracepoint_breakpoint_ops;
14651
14652   if (create_breakpoint (get_current_arch (),
14653                          arg,
14654                          NULL, 0, NULL, 1 /* parse arg */,
14655                          0 /* tempflag */,
14656                          bp_static_tracepoint /* type_wanted */,
14657                          0 /* Ignore count */,
14658                          pending_break_support,
14659                          ops,
14660                          from_tty,
14661                          1 /* enabled */,
14662                          0 /* internal */, 0))
14663     set_tracepoint_count (breakpoint_count);
14664 }
14665
14666 /* Set up a fake reader function that gets command lines from a linked
14667    list that was acquired during tracepoint uploading.  */
14668
14669 static struct uploaded_tp *this_utp;
14670 static int next_cmd;
14671
14672 static char *
14673 read_uploaded_action (void)
14674 {
14675   char *rslt;
14676
14677   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14678
14679   next_cmd++;
14680
14681   return rslt;
14682 }
14683
14684 /* Given information about a tracepoint as recorded on a target (which
14685    can be either a live system or a trace file), attempt to create an
14686    equivalent GDB tracepoint.  This is not a reliable process, since
14687    the target does not necessarily have all the information used when
14688    the tracepoint was originally defined.  */
14689   
14690 struct tracepoint *
14691 create_tracepoint_from_upload (struct uploaded_tp *utp)
14692 {
14693   char *addr_str, small_buf[100];
14694   struct tracepoint *tp;
14695
14696   if (utp->at_string)
14697     addr_str = utp->at_string;
14698   else
14699     {
14700       /* In the absence of a source location, fall back to raw
14701          address.  Since there is no way to confirm that the address
14702          means the same thing as when the trace was started, warn the
14703          user.  */
14704       warning (_("Uploaded tracepoint %d has no "
14705                  "source location, using raw address"),
14706                utp->number);
14707       sprintf (small_buf, "*%s", hex_string (utp->addr));
14708       addr_str = small_buf;
14709     }
14710
14711   /* There's not much we can do with a sequence of bytecodes.  */
14712   if (utp->cond && !utp->cond_string)
14713     warning (_("Uploaded tracepoint %d condition "
14714                "has no source form, ignoring it"),
14715              utp->number);
14716
14717   if (!create_breakpoint (get_current_arch (),
14718                           addr_str,
14719                           utp->cond_string, -1, NULL,
14720                           0 /* parse cond/thread */,
14721                           0 /* tempflag */,
14722                           utp->type /* type_wanted */,
14723                           0 /* Ignore count */,
14724                           pending_break_support,
14725                           &tracepoint_breakpoint_ops,
14726                           0 /* from_tty */,
14727                           utp->enabled /* enabled */,
14728                           0 /* internal */,
14729                           CREATE_BREAKPOINT_FLAGS_INSERTED))
14730     return NULL;
14731
14732   set_tracepoint_count (breakpoint_count);
14733   
14734   /* Get the tracepoint we just created.  */
14735   tp = get_tracepoint (tracepoint_count);
14736   gdb_assert (tp != NULL);
14737
14738   if (utp->pass > 0)
14739     {
14740       sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
14741
14742       trace_pass_command (small_buf, 0);
14743     }
14744
14745   /* If we have uploaded versions of the original commands, set up a
14746      special-purpose "reader" function and call the usual command line
14747      reader, then pass the result to the breakpoint command-setting
14748      function.  */
14749   if (!VEC_empty (char_ptr, utp->cmd_strings))
14750     {
14751       struct command_line *cmd_list;
14752
14753       this_utp = utp;
14754       next_cmd = 0;
14755
14756       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14757
14758       breakpoint_set_commands (&tp->base, cmd_list);
14759     }
14760   else if (!VEC_empty (char_ptr, utp->actions)
14761            || !VEC_empty (char_ptr, utp->step_actions))
14762     warning (_("Uploaded tracepoint %d actions "
14763                "have no source form, ignoring them"),
14764              utp->number);
14765
14766   /* Copy any status information that might be available.  */
14767   tp->base.hit_count = utp->hit_count;
14768   tp->traceframe_usage = utp->traceframe_usage;
14769
14770   return tp;
14771 }
14772   
14773 /* Print information on tracepoint number TPNUM_EXP, or all if
14774    omitted.  */
14775
14776 static void
14777 tracepoints_info (char *args, int from_tty)
14778 {
14779   struct ui_out *uiout = current_uiout;
14780   int num_printed;
14781
14782   num_printed = breakpoint_1 (args, 0, is_tracepoint);
14783
14784   if (num_printed == 0)
14785     {
14786       if (args == NULL || *args == '\0')
14787         ui_out_message (uiout, 0, "No tracepoints.\n");
14788       else
14789         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
14790     }
14791
14792   default_collect_info ();
14793 }
14794
14795 /* The 'enable trace' command enables tracepoints.
14796    Not supported by all targets.  */
14797 static void
14798 enable_trace_command (char *args, int from_tty)
14799 {
14800   enable_command (args, from_tty);
14801 }
14802
14803 /* The 'disable trace' command disables tracepoints.
14804    Not supported by all targets.  */
14805 static void
14806 disable_trace_command (char *args, int from_tty)
14807 {
14808   disable_command (args, from_tty);
14809 }
14810
14811 /* Remove a tracepoint (or all if no argument).  */
14812 static void
14813 delete_trace_command (char *arg, int from_tty)
14814 {
14815   struct breakpoint *b, *b_tmp;
14816
14817   dont_repeat ();
14818
14819   if (arg == 0)
14820     {
14821       int breaks_to_delete = 0;
14822
14823       /* Delete all breakpoints if no argument.
14824          Do not delete internal or call-dummy breakpoints, these
14825          have to be deleted with an explicit breakpoint number 
14826          argument.  */
14827       ALL_TRACEPOINTS (b)
14828         if (is_tracepoint (b) && user_breakpoint_p (b))
14829           {
14830             breaks_to_delete = 1;
14831             break;
14832           }
14833
14834       /* Ask user only if there are some breakpoints to delete.  */
14835       if (!from_tty
14836           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14837         {
14838           ALL_BREAKPOINTS_SAFE (b, b_tmp)
14839             if (is_tracepoint (b) && user_breakpoint_p (b))
14840               delete_breakpoint (b);
14841         }
14842     }
14843   else
14844     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14845 }
14846
14847 /* Helper function for trace_pass_command.  */
14848
14849 static void
14850 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14851 {
14852   tp->pass_count = count;
14853   observer_notify_tracepoint_modified (tp->base.number);
14854   if (from_tty)
14855     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14856                      tp->base.number, count);
14857 }
14858
14859 /* Set passcount for tracepoint.
14860
14861    First command argument is passcount, second is tracepoint number.
14862    If tracepoint number omitted, apply to most recently defined.
14863    Also accepts special argument "all".  */
14864
14865 static void
14866 trace_pass_command (char *args, int from_tty)
14867 {
14868   struct tracepoint *t1;
14869   unsigned int count;
14870
14871   if (args == 0 || *args == 0)
14872     error (_("passcount command requires an "
14873              "argument (count + optional TP num)"));
14874
14875   count = strtoul (args, &args, 10);    /* Count comes first, then TP num.  */
14876
14877   while (*args && isspace ((int) *args))
14878     args++;
14879
14880   if (*args && strncasecmp (args, "all", 3) == 0)
14881     {
14882       struct breakpoint *b;
14883
14884       args += 3;                        /* Skip special argument "all".  */
14885       if (*args)
14886         error (_("Junk at end of arguments."));
14887
14888       ALL_TRACEPOINTS (b)
14889       {
14890         t1 = (struct tracepoint *) b;
14891         trace_pass_set_count (t1, count, from_tty);
14892       }
14893     }
14894   else if (*args == '\0')
14895     {
14896       t1 = get_tracepoint_by_number (&args, NULL, 1);
14897       if (t1)
14898         trace_pass_set_count (t1, count, from_tty);
14899     }
14900   else
14901     {
14902       struct get_number_or_range_state state;
14903
14904       init_number_or_range (&state, args);
14905       while (!state.finished)
14906         {
14907           t1 = get_tracepoint_by_number (&args, &state, 1);
14908           if (t1)
14909             trace_pass_set_count (t1, count, from_tty);
14910         }
14911     }
14912 }
14913
14914 struct tracepoint *
14915 get_tracepoint (int num)
14916 {
14917   struct breakpoint *t;
14918
14919   ALL_TRACEPOINTS (t)
14920     if (t->number == num)
14921       return (struct tracepoint *) t;
14922
14923   return NULL;
14924 }
14925
14926 /* Find the tracepoint with the given target-side number (which may be
14927    different from the tracepoint number after disconnecting and
14928    reconnecting).  */
14929
14930 struct tracepoint *
14931 get_tracepoint_by_number_on_target (int num)
14932 {
14933   struct breakpoint *b;
14934
14935   ALL_TRACEPOINTS (b)
14936     {
14937       struct tracepoint *t = (struct tracepoint *) b;
14938
14939       if (t->number_on_target == num)
14940         return t;
14941     }
14942
14943   return NULL;
14944 }
14945
14946 /* Utility: parse a tracepoint number and look it up in the list.
14947    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14948    If OPTIONAL_P is true, then if the argument is missing, the most
14949    recent tracepoint (tracepoint_count) is returned.  */
14950 struct tracepoint *
14951 get_tracepoint_by_number (char **arg,
14952                           struct get_number_or_range_state *state,
14953                           int optional_p)
14954 {
14955   extern int tracepoint_count;
14956   struct breakpoint *t;
14957   int tpnum;
14958   char *instring = arg == NULL ? NULL : *arg;
14959
14960   if (state)
14961     {
14962       gdb_assert (!state->finished);
14963       tpnum = get_number_or_range (state);
14964     }
14965   else if (arg == NULL || *arg == NULL || ! **arg)
14966     {
14967       if (optional_p)
14968         tpnum = tracepoint_count;
14969       else
14970         error_no_arg (_("tracepoint number"));
14971     }
14972   else
14973     tpnum = get_number (arg);
14974
14975   if (tpnum <= 0)
14976     {
14977       if (instring && *instring)
14978         printf_filtered (_("bad tracepoint number at or near '%s'\n"), 
14979                          instring);
14980       else
14981         printf_filtered (_("Tracepoint argument missing "
14982                            "and no previous tracepoint\n"));
14983       return NULL;
14984     }
14985
14986   ALL_TRACEPOINTS (t)
14987     if (t->number == tpnum)
14988     {
14989       return (struct tracepoint *) t;
14990     }
14991
14992   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14993   return NULL;
14994 }
14995
14996 void
14997 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14998 {
14999   if (b->thread != -1)
15000     fprintf_unfiltered (fp, " thread %d", b->thread);
15001
15002   if (b->task != 0)
15003     fprintf_unfiltered (fp, " task %d", b->task);
15004
15005   fprintf_unfiltered (fp, "\n");
15006 }
15007
15008 /* Save information on user settable breakpoints (watchpoints, etc) to
15009    a new script file named FILENAME.  If FILTER is non-NULL, call it
15010    on each breakpoint and only include the ones for which it returns
15011    non-zero.  */
15012
15013 static void
15014 save_breakpoints (char *filename, int from_tty,
15015                   int (*filter) (const struct breakpoint *))
15016 {
15017   struct breakpoint *tp;
15018   int any = 0;
15019   char *pathname;
15020   struct cleanup *cleanup;
15021   struct ui_file *fp;
15022   int extra_trace_bits = 0;
15023
15024   if (filename == 0 || *filename == 0)
15025     error (_("Argument required (file name in which to save)"));
15026
15027   /* See if we have anything to save.  */
15028   ALL_BREAKPOINTS (tp)
15029   {
15030     /* Skip internal and momentary breakpoints.  */
15031     if (!user_breakpoint_p (tp))
15032       continue;
15033
15034     /* If we have a filter, only save the breakpoints it accepts.  */
15035     if (filter && !filter (tp))
15036       continue;
15037
15038     any = 1;
15039
15040     if (is_tracepoint (tp))
15041       {
15042         extra_trace_bits = 1;
15043
15044         /* We can stop searching.  */
15045         break;
15046       }
15047   }
15048
15049   if (!any)
15050     {
15051       warning (_("Nothing to save."));
15052       return;
15053     }
15054
15055   pathname = tilde_expand (filename);
15056   cleanup = make_cleanup (xfree, pathname);
15057   fp = gdb_fopen (pathname, "w");
15058   if (!fp)
15059     error (_("Unable to open file '%s' for saving (%s)"),
15060            filename, safe_strerror (errno));
15061   make_cleanup_ui_file_delete (fp);
15062
15063   if (extra_trace_bits)
15064     save_trace_state_variables (fp);
15065
15066   ALL_BREAKPOINTS (tp)
15067   {
15068     /* Skip internal and momentary breakpoints.  */
15069     if (!user_breakpoint_p (tp))
15070       continue;
15071
15072     /* If we have a filter, only save the breakpoints it accepts.  */
15073     if (filter && !filter (tp))
15074       continue;
15075
15076     tp->ops->print_recreate (tp, fp);
15077
15078     /* Note, we can't rely on tp->number for anything, as we can't
15079        assume the recreated breakpoint numbers will match.  Use $bpnum
15080        instead.  */
15081
15082     if (tp->cond_string)
15083       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
15084
15085     if (tp->ignore_count)
15086       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
15087
15088     if (tp->commands)
15089       {
15090         volatile struct gdb_exception ex;       
15091
15092         fprintf_unfiltered (fp, "  commands\n");
15093         
15094         ui_out_redirect (current_uiout, fp);
15095         TRY_CATCH (ex, RETURN_MASK_ALL)
15096           {
15097             print_command_lines (current_uiout, tp->commands->commands, 2);
15098           }
15099         ui_out_redirect (current_uiout, NULL);
15100
15101         if (ex.reason < 0)
15102           throw_exception (ex);
15103
15104         fprintf_unfiltered (fp, "  end\n");
15105       }
15106
15107     if (tp->enable_state == bp_disabled)
15108       fprintf_unfiltered (fp, "disable\n");
15109
15110     /* If this is a multi-location breakpoint, check if the locations
15111        should be individually disabled.  Watchpoint locations are
15112        special, and not user visible.  */
15113     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15114       {
15115         struct bp_location *loc;
15116         int n = 1;
15117
15118         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15119           if (!loc->enabled)
15120             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15121       }
15122   }
15123
15124   if (extra_trace_bits && *default_collect)
15125     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15126
15127   do_cleanups (cleanup);
15128   if (from_tty)
15129     printf_filtered (_("Saved to file '%s'.\n"), filename);
15130 }
15131
15132 /* The `save breakpoints' command.  */
15133
15134 static void
15135 save_breakpoints_command (char *args, int from_tty)
15136 {
15137   save_breakpoints (args, from_tty, NULL);
15138 }
15139
15140 /* The `save tracepoints' command.  */
15141
15142 static void
15143 save_tracepoints_command (char *args, int from_tty)
15144 {
15145   save_breakpoints (args, from_tty, is_tracepoint);
15146 }
15147
15148 /* Create a vector of all tracepoints.  */
15149
15150 VEC(breakpoint_p) *
15151 all_tracepoints (void)
15152 {
15153   VEC(breakpoint_p) *tp_vec = 0;
15154   struct breakpoint *tp;
15155
15156   ALL_TRACEPOINTS (tp)
15157   {
15158     VEC_safe_push (breakpoint_p, tp_vec, tp);
15159   }
15160
15161   return tp_vec;
15162 }
15163
15164 \f
15165 /* This help string is used for the break, hbreak, tbreak and thbreak
15166    commands.  It is defined as a macro to prevent duplication.
15167    COMMAND should be a string constant containing the name of the
15168    command.  */
15169 #define BREAK_ARGS_HELP(command) \
15170 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15171 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15172 If a line number is specified, break at start of code for that line.\n\
15173 If a function is specified, break at start of code for that function.\n\
15174 If an address is specified, break at that exact address.\n\
15175 With no LOCATION, uses current execution address of the selected\n\
15176 stack frame.  This is useful for breaking on return to a stack frame.\n\
15177 \n\
15178 THREADNUM is the number from \"info threads\".\n\
15179 CONDITION is a boolean expression.\n\
15180 \n\
15181 Multiple breakpoints at one place are permitted, and useful if their\n\
15182 conditions are different.\n\
15183 \n\
15184 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15185
15186 /* List of subcommands for "catch".  */
15187 static struct cmd_list_element *catch_cmdlist;
15188
15189 /* List of subcommands for "tcatch".  */
15190 static struct cmd_list_element *tcatch_cmdlist;
15191
15192 void
15193 add_catch_command (char *name, char *docstring,
15194                    void (*sfunc) (char *args, int from_tty,
15195                                   struct cmd_list_element *command),
15196                    char **(*completer) (struct cmd_list_element *cmd,
15197                                          char *text, char *word),
15198                    void *user_data_catch,
15199                    void *user_data_tcatch)
15200 {
15201   struct cmd_list_element *command;
15202
15203   command = add_cmd (name, class_breakpoint, NULL, docstring,
15204                      &catch_cmdlist);
15205   set_cmd_sfunc (command, sfunc);
15206   set_cmd_context (command, user_data_catch);
15207   set_cmd_completer (command, completer);
15208
15209   command = add_cmd (name, class_breakpoint, NULL, docstring,
15210                      &tcatch_cmdlist);
15211   set_cmd_sfunc (command, sfunc);
15212   set_cmd_context (command, user_data_tcatch);
15213   set_cmd_completer (command, completer);
15214 }
15215
15216 static void
15217 clear_syscall_counts (struct inferior *inf)
15218 {
15219   struct catch_syscall_inferior_data *inf_data
15220     = get_catch_syscall_inferior_data (inf);
15221
15222   inf_data->total_syscalls_count = 0;
15223   inf_data->any_syscall_count = 0;
15224   VEC_free (int, inf_data->syscalls_counts);
15225 }
15226
15227 static void
15228 save_command (char *arg, int from_tty)
15229 {
15230   printf_unfiltered (_("\"save\" must be followed by "
15231                        "the name of a save subcommand.\n"));
15232   help_list (save_cmdlist, "save ", -1, gdb_stdout);
15233 }
15234
15235 struct breakpoint *
15236 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15237                           void *data)
15238 {
15239   struct breakpoint *b, *b_tmp;
15240
15241   ALL_BREAKPOINTS_SAFE (b, b_tmp)
15242     {
15243       if ((*callback) (b, data))
15244         return b;
15245     }
15246
15247   return NULL;
15248 }
15249
15250 /* Zero if any of the breakpoint's locations could be a location where
15251    functions have been inlined, nonzero otherwise.  */
15252
15253 static int
15254 is_non_inline_function (struct breakpoint *b)
15255 {
15256   /* The shared library event breakpoint is set on the address of a
15257      non-inline function.  */
15258   if (b->type == bp_shlib_event)
15259     return 1;
15260
15261   return 0;
15262 }
15263
15264 /* Nonzero if the specified PC cannot be a location where functions
15265    have been inlined.  */
15266
15267 int
15268 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15269                            const struct target_waitstatus *ws)
15270 {
15271   struct breakpoint *b;
15272   struct bp_location *bl;
15273
15274   ALL_BREAKPOINTS (b)
15275     {
15276       if (!is_non_inline_function (b))
15277         continue;
15278
15279       for (bl = b->loc; bl != NULL; bl = bl->next)
15280         {
15281           if (!bl->shlib_disabled
15282               && bpstat_check_location (bl, aspace, pc, ws))
15283             return 1;
15284         }
15285     }
15286
15287   return 0;
15288 }
15289
15290 void
15291 initialize_breakpoint_ops (void)
15292 {
15293   static int initialized = 0;
15294
15295   struct breakpoint_ops *ops;
15296
15297   if (initialized)
15298     return;
15299   initialized = 1;
15300
15301   /* The breakpoint_ops structure to be inherit by all kinds of
15302      breakpoints (real breakpoints, i.e., user "break" breakpoints,
15303      internal and momentary breakpoints, etc.).  */
15304   ops = &bkpt_base_breakpoint_ops;
15305   *ops = base_breakpoint_ops;
15306   ops->re_set = bkpt_re_set;
15307   ops->insert_location = bkpt_insert_location;
15308   ops->remove_location = bkpt_remove_location;
15309   ops->breakpoint_hit = bkpt_breakpoint_hit;
15310   ops->create_sals_from_address = bkpt_create_sals_from_address;
15311   ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15312   ops->decode_linespec = bkpt_decode_linespec;
15313
15314   /* The breakpoint_ops structure to be used in regular breakpoints.  */
15315   ops = &bkpt_breakpoint_ops;
15316   *ops = bkpt_base_breakpoint_ops;
15317   ops->re_set = bkpt_re_set;
15318   ops->resources_needed = bkpt_resources_needed;
15319   ops->print_it = bkpt_print_it;
15320   ops->print_mention = bkpt_print_mention;
15321   ops->print_recreate = bkpt_print_recreate;
15322
15323   /* Ranged breakpoints.  */
15324   ops = &ranged_breakpoint_ops;
15325   *ops = bkpt_breakpoint_ops;
15326   ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15327   ops->resources_needed = resources_needed_ranged_breakpoint;
15328   ops->print_it = print_it_ranged_breakpoint;
15329   ops->print_one = print_one_ranged_breakpoint;
15330   ops->print_one_detail = print_one_detail_ranged_breakpoint;
15331   ops->print_mention = print_mention_ranged_breakpoint;
15332   ops->print_recreate = print_recreate_ranged_breakpoint;
15333
15334   /* Internal breakpoints.  */
15335   ops = &internal_breakpoint_ops;
15336   *ops = bkpt_base_breakpoint_ops;
15337   ops->re_set = internal_bkpt_re_set;
15338   ops->check_status = internal_bkpt_check_status;
15339   ops->print_it = internal_bkpt_print_it;
15340   ops->print_mention = internal_bkpt_print_mention;
15341
15342   /* Momentary breakpoints.  */
15343   ops = &momentary_breakpoint_ops;
15344   *ops = bkpt_base_breakpoint_ops;
15345   ops->re_set = momentary_bkpt_re_set;
15346   ops->check_status = momentary_bkpt_check_status;
15347   ops->print_it = momentary_bkpt_print_it;
15348   ops->print_mention = momentary_bkpt_print_mention;
15349
15350   /* Probe breakpoints.  */
15351   ops = &bkpt_probe_breakpoint_ops;
15352   *ops = bkpt_breakpoint_ops;
15353   ops->insert_location = bkpt_probe_insert_location;
15354   ops->remove_location = bkpt_probe_remove_location;
15355   ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15356   ops->decode_linespec = bkpt_probe_decode_linespec;
15357
15358   /* GNU v3 exception catchpoints.  */
15359   ops = &gnu_v3_exception_catchpoint_ops;
15360   *ops = bkpt_breakpoint_ops;
15361   ops->print_it = print_it_exception_catchpoint;
15362   ops->print_one = print_one_exception_catchpoint;
15363   ops->print_mention = print_mention_exception_catchpoint;
15364   ops->print_recreate = print_recreate_exception_catchpoint;
15365
15366   /* Watchpoints.  */
15367   ops = &watchpoint_breakpoint_ops;
15368   *ops = base_breakpoint_ops;
15369   ops->dtor = dtor_watchpoint;
15370   ops->re_set = re_set_watchpoint;
15371   ops->insert_location = insert_watchpoint;
15372   ops->remove_location = remove_watchpoint;
15373   ops->breakpoint_hit = breakpoint_hit_watchpoint;
15374   ops->check_status = check_status_watchpoint;
15375   ops->resources_needed = resources_needed_watchpoint;
15376   ops->works_in_software_mode = works_in_software_mode_watchpoint;
15377   ops->print_it = print_it_watchpoint;
15378   ops->print_mention = print_mention_watchpoint;
15379   ops->print_recreate = print_recreate_watchpoint;
15380
15381   /* Masked watchpoints.  */
15382   ops = &masked_watchpoint_breakpoint_ops;
15383   *ops = watchpoint_breakpoint_ops;
15384   ops->insert_location = insert_masked_watchpoint;
15385   ops->remove_location = remove_masked_watchpoint;
15386   ops->resources_needed = resources_needed_masked_watchpoint;
15387   ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15388   ops->print_it = print_it_masked_watchpoint;
15389   ops->print_one_detail = print_one_detail_masked_watchpoint;
15390   ops->print_mention = print_mention_masked_watchpoint;
15391   ops->print_recreate = print_recreate_masked_watchpoint;
15392
15393   /* Tracepoints.  */
15394   ops = &tracepoint_breakpoint_ops;
15395   *ops = base_breakpoint_ops;
15396   ops->re_set = tracepoint_re_set;
15397   ops->breakpoint_hit = tracepoint_breakpoint_hit;
15398   ops->print_one_detail = tracepoint_print_one_detail;
15399   ops->print_mention = tracepoint_print_mention;
15400   ops->print_recreate = tracepoint_print_recreate;
15401   ops->create_sals_from_address = tracepoint_create_sals_from_address;
15402   ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15403   ops->decode_linespec = tracepoint_decode_linespec;
15404
15405   /* Probe tracepoints.  */
15406   ops = &tracepoint_probe_breakpoint_ops;
15407   *ops = tracepoint_breakpoint_ops;
15408   ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15409   ops->decode_linespec = tracepoint_probe_decode_linespec;
15410
15411   /* Static tracepoints with marker (`-m').  */
15412   ops = &strace_marker_breakpoint_ops;
15413   *ops = tracepoint_breakpoint_ops;
15414   ops->create_sals_from_address = strace_marker_create_sals_from_address;
15415   ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15416   ops->decode_linespec = strace_marker_decode_linespec;
15417
15418   /* Fork catchpoints.  */
15419   ops = &catch_fork_breakpoint_ops;
15420   *ops = base_breakpoint_ops;
15421   ops->insert_location = insert_catch_fork;
15422   ops->remove_location = remove_catch_fork;
15423   ops->breakpoint_hit = breakpoint_hit_catch_fork;
15424   ops->print_it = print_it_catch_fork;
15425   ops->print_one = print_one_catch_fork;
15426   ops->print_mention = print_mention_catch_fork;
15427   ops->print_recreate = print_recreate_catch_fork;
15428
15429   /* Vfork catchpoints.  */
15430   ops = &catch_vfork_breakpoint_ops;
15431   *ops = base_breakpoint_ops;
15432   ops->insert_location = insert_catch_vfork;
15433   ops->remove_location = remove_catch_vfork;
15434   ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15435   ops->print_it = print_it_catch_vfork;
15436   ops->print_one = print_one_catch_vfork;
15437   ops->print_mention = print_mention_catch_vfork;
15438   ops->print_recreate = print_recreate_catch_vfork;
15439
15440   /* Exec catchpoints.  */
15441   ops = &catch_exec_breakpoint_ops;
15442   *ops = base_breakpoint_ops;
15443   ops->dtor = dtor_catch_exec;
15444   ops->insert_location = insert_catch_exec;
15445   ops->remove_location = remove_catch_exec;
15446   ops->breakpoint_hit = breakpoint_hit_catch_exec;
15447   ops->print_it = print_it_catch_exec;
15448   ops->print_one = print_one_catch_exec;
15449   ops->print_mention = print_mention_catch_exec;
15450   ops->print_recreate = print_recreate_catch_exec;
15451
15452   /* Syscall catchpoints.  */
15453   ops = &catch_syscall_breakpoint_ops;
15454   *ops = base_breakpoint_ops;
15455   ops->dtor = dtor_catch_syscall;
15456   ops->insert_location = insert_catch_syscall;
15457   ops->remove_location = remove_catch_syscall;
15458   ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15459   ops->print_it = print_it_catch_syscall;
15460   ops->print_one = print_one_catch_syscall;
15461   ops->print_mention = print_mention_catch_syscall;
15462   ops->print_recreate = print_recreate_catch_syscall;
15463
15464   /* Solib-related catchpoints.  */
15465   ops = &catch_solib_breakpoint_ops;
15466   *ops = base_breakpoint_ops;
15467   ops->dtor = dtor_catch_solib;
15468   ops->insert_location = insert_catch_solib;
15469   ops->remove_location = remove_catch_solib;
15470   ops->breakpoint_hit = breakpoint_hit_catch_solib;
15471   ops->check_status = check_status_catch_solib;
15472   ops->print_it = print_it_catch_solib;
15473   ops->print_one = print_one_catch_solib;
15474   ops->print_mention = print_mention_catch_solib;
15475   ops->print_recreate = print_recreate_catch_solib;
15476
15477   ops = &dprintf_breakpoint_ops;
15478   *ops = bkpt_base_breakpoint_ops;
15479   ops->re_set = bkpt_re_set;
15480   ops->resources_needed = bkpt_resources_needed;
15481   ops->print_it = bkpt_print_it;
15482   ops->print_mention = bkpt_print_mention;
15483   ops->print_recreate = bkpt_print_recreate;
15484 }
15485
15486 void
15487 _initialize_breakpoint (void)
15488 {
15489   struct cmd_list_element *c;
15490
15491   initialize_breakpoint_ops ();
15492
15493   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15494   observer_attach_inferior_exit (clear_syscall_counts);
15495   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15496
15497   breakpoint_objfile_key
15498     = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15499
15500   catch_syscall_inferior_data
15501     = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15502
15503   breakpoint_chain = 0;
15504   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
15505      before a breakpoint is set.  */
15506   breakpoint_count = 0;
15507
15508   tracepoint_count = 0;
15509
15510   add_com ("ignore", class_breakpoint, ignore_command, _("\
15511 Set ignore-count of breakpoint number N to COUNT.\n\
15512 Usage is `ignore N COUNT'."));
15513   if (xdb_commands)
15514     add_com_alias ("bc", "ignore", class_breakpoint, 1);
15515
15516   add_com ("commands", class_breakpoint, commands_command, _("\
15517 Set commands to be executed when a breakpoint is hit.\n\
15518 Give breakpoint number as argument after \"commands\".\n\
15519 With no argument, the targeted breakpoint is the last one set.\n\
15520 The commands themselves follow starting on the next line.\n\
15521 Type a line containing \"end\" to indicate the end of them.\n\
15522 Give \"silent\" as the first line to make the breakpoint silent;\n\
15523 then no output is printed when it is hit, except what the commands print."));
15524
15525   add_com ("condition", class_breakpoint, condition_command, _("\
15526 Specify breakpoint number N to break only if COND is true.\n\
15527 Usage is `condition N COND', where N is an integer and COND is an\n\
15528 expression to be evaluated whenever breakpoint N is reached."));
15529
15530   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15531 Set a temporary breakpoint.\n\
15532 Like \"break\" except the breakpoint is only temporary,\n\
15533 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
15534 by using \"enable delete\" on the breakpoint number.\n\
15535 \n"
15536 BREAK_ARGS_HELP ("tbreak")));
15537   set_cmd_completer (c, location_completer);
15538
15539   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15540 Set a hardware assisted breakpoint.\n\
15541 Like \"break\" except the breakpoint requires hardware support,\n\
15542 some target hardware may not have this support.\n\
15543 \n"
15544 BREAK_ARGS_HELP ("hbreak")));
15545   set_cmd_completer (c, location_completer);
15546
15547   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15548 Set a temporary hardware assisted breakpoint.\n\
15549 Like \"hbreak\" except the breakpoint is only temporary,\n\
15550 so it will be deleted when hit.\n\
15551 \n"
15552 BREAK_ARGS_HELP ("thbreak")));
15553   set_cmd_completer (c, location_completer);
15554
15555   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15556 Enable some breakpoints.\n\
15557 Give breakpoint numbers (separated by spaces) as arguments.\n\
15558 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15559 This is used to cancel the effect of the \"disable\" command.\n\
15560 With a subcommand you can enable temporarily."),
15561                   &enablelist, "enable ", 1, &cmdlist);
15562   if (xdb_commands)
15563     add_com ("ab", class_breakpoint, enable_command, _("\
15564 Enable some breakpoints.\n\
15565 Give breakpoint numbers (separated by spaces) as arguments.\n\
15566 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15567 This is used to cancel the effect of the \"disable\" command.\n\
15568 With a subcommand you can enable temporarily."));
15569
15570   add_com_alias ("en", "enable", class_breakpoint, 1);
15571
15572   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15573 Enable some breakpoints.\n\
15574 Give breakpoint numbers (separated by spaces) as arguments.\n\
15575 This is used to cancel the effect of the \"disable\" command.\n\
15576 May be abbreviated to simply \"enable\".\n"),
15577                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15578
15579   add_cmd ("once", no_class, enable_once_command, _("\
15580 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
15581 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15582            &enablebreaklist);
15583
15584   add_cmd ("delete", no_class, enable_delete_command, _("\
15585 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
15586 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15587            &enablebreaklist);
15588
15589   add_cmd ("count", no_class, enable_count_command, _("\
15590 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
15591 If a breakpoint is hit while enabled in this fashion,\n\
15592 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15593            &enablebreaklist);
15594
15595   add_cmd ("delete", no_class, enable_delete_command, _("\
15596 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
15597 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15598            &enablelist);
15599
15600   add_cmd ("once", no_class, enable_once_command, _("\
15601 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
15602 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15603            &enablelist);
15604
15605   add_cmd ("count", no_class, enable_count_command, _("\
15606 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
15607 If a breakpoint is hit while enabled in this fashion,\n\
15608 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15609            &enablelist);
15610
15611   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15612 Disable some breakpoints.\n\
15613 Arguments are breakpoint numbers with spaces in between.\n\
15614 To disable all breakpoints, give no argument.\n\
15615 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15616                   &disablelist, "disable ", 1, &cmdlist);
15617   add_com_alias ("dis", "disable", class_breakpoint, 1);
15618   add_com_alias ("disa", "disable", class_breakpoint, 1);
15619   if (xdb_commands)
15620     add_com ("sb", class_breakpoint, disable_command, _("\
15621 Disable some breakpoints.\n\
15622 Arguments are breakpoint numbers with spaces in between.\n\
15623 To disable all breakpoints, give no argument.\n\
15624 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
15625
15626   add_cmd ("breakpoints", class_alias, disable_command, _("\
15627 Disable some breakpoints.\n\
15628 Arguments are breakpoint numbers with spaces in between.\n\
15629 To disable all breakpoints, give no argument.\n\
15630 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15631 This command may be abbreviated \"disable\"."),
15632            &disablelist);
15633
15634   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15635 Delete some breakpoints or auto-display expressions.\n\
15636 Arguments are breakpoint numbers with spaces in between.\n\
15637 To delete all breakpoints, give no argument.\n\
15638 \n\
15639 Also a prefix command for deletion of other GDB objects.\n\
15640 The \"unset\" command is also an alias for \"delete\"."),
15641                   &deletelist, "delete ", 1, &cmdlist);
15642   add_com_alias ("d", "delete", class_breakpoint, 1);
15643   add_com_alias ("del", "delete", class_breakpoint, 1);
15644   if (xdb_commands)
15645     add_com ("db", class_breakpoint, delete_command, _("\
15646 Delete some breakpoints.\n\
15647 Arguments are breakpoint numbers with spaces in between.\n\
15648 To delete all breakpoints, give no argument.\n"));
15649
15650   add_cmd ("breakpoints", class_alias, delete_command, _("\
15651 Delete some breakpoints or auto-display expressions.\n\
15652 Arguments are breakpoint numbers with spaces in between.\n\
15653 To delete all breakpoints, give no argument.\n\
15654 This command may be abbreviated \"delete\"."),
15655            &deletelist);
15656
15657   add_com ("clear", class_breakpoint, clear_command, _("\
15658 Clear breakpoint at specified line or function.\n\
15659 Argument may be line number, function name, or \"*\" and an address.\n\
15660 If line number is specified, all breakpoints in that line are cleared.\n\
15661 If function is specified, breakpoints at beginning of function are cleared.\n\
15662 If an address is specified, breakpoints at that address are cleared.\n\
15663 \n\
15664 With no argument, clears all breakpoints in the line that the selected frame\n\
15665 is executing in.\n\
15666 \n\
15667 See also the \"delete\" command which clears breakpoints by number."));
15668   add_com_alias ("cl", "clear", class_breakpoint, 1);
15669
15670   c = add_com ("break", class_breakpoint, break_command, _("\
15671 Set breakpoint at specified line or function.\n"
15672 BREAK_ARGS_HELP ("break")));
15673   set_cmd_completer (c, location_completer);
15674
15675   add_com_alias ("b", "break", class_run, 1);
15676   add_com_alias ("br", "break", class_run, 1);
15677   add_com_alias ("bre", "break", class_run, 1);
15678   add_com_alias ("brea", "break", class_run, 1);
15679
15680   if (xdb_commands)
15681    add_com_alias ("ba", "break", class_breakpoint, 1);
15682
15683   if (dbx_commands)
15684     {
15685       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15686 Break in function/address or break at a line in the current file."),
15687                              &stoplist, "stop ", 1, &cmdlist);
15688       add_cmd ("in", class_breakpoint, stopin_command,
15689                _("Break in function or address."), &stoplist);
15690       add_cmd ("at", class_breakpoint, stopat_command,
15691                _("Break at a line in the current file."), &stoplist);
15692       add_com ("status", class_info, breakpoints_info, _("\
15693 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15694 The \"Type\" column indicates one of:\n\
15695 \tbreakpoint     - normal breakpoint\n\
15696 \twatchpoint     - watchpoint\n\
15697 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15698 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
15699 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
15700 address and file/line number respectively.\n\
15701 \n\
15702 Convenience variable \"$_\" and default examine address for \"x\"\n\
15703 are set to the address of the last breakpoint listed unless the command\n\
15704 is prefixed with \"server \".\n\n\
15705 Convenience variable \"$bpnum\" contains the number of the last\n\
15706 breakpoint set."));
15707     }
15708
15709   add_info ("breakpoints", breakpoints_info, _("\
15710 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15711 The \"Type\" column indicates one of:\n\
15712 \tbreakpoint     - normal breakpoint\n\
15713 \twatchpoint     - watchpoint\n\
15714 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15715 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
15716 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
15717 address and file/line number respectively.\n\
15718 \n\
15719 Convenience variable \"$_\" and default examine address for \"x\"\n\
15720 are set to the address of the last breakpoint listed unless the command\n\
15721 is prefixed with \"server \".\n\n\
15722 Convenience variable \"$bpnum\" contains the number of the last\n\
15723 breakpoint set."));
15724
15725   add_info_alias ("b", "breakpoints", 1);
15726
15727   if (xdb_commands)
15728     add_com ("lb", class_breakpoint, breakpoints_info, _("\
15729 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15730 The \"Type\" column indicates one of:\n\
15731 \tbreakpoint     - normal breakpoint\n\
15732 \twatchpoint     - watchpoint\n\
15733 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15734 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
15735 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
15736 address and file/line number respectively.\n\
15737 \n\
15738 Convenience variable \"$_\" and default examine address for \"x\"\n\
15739 are set to the address of the last breakpoint listed unless the command\n\
15740 is prefixed with \"server \".\n\n\
15741 Convenience variable \"$bpnum\" contains the number of the last\n\
15742 breakpoint set."));
15743
15744   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15745 Status of all breakpoints, or breakpoint number NUMBER.\n\
15746 The \"Type\" column indicates one of:\n\
15747 \tbreakpoint     - normal breakpoint\n\
15748 \twatchpoint     - watchpoint\n\
15749 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
15750 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15751 \tuntil          - internal breakpoint used by the \"until\" command\n\
15752 \tfinish         - internal breakpoint used by the \"finish\" command\n\
15753 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15754 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
15755 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
15756 address and file/line number respectively.\n\
15757 \n\
15758 Convenience variable \"$_\" and default examine address for \"x\"\n\
15759 are set to the address of the last breakpoint listed unless the command\n\
15760 is prefixed with \"server \".\n\n\
15761 Convenience variable \"$bpnum\" contains the number of the last\n\
15762 breakpoint set."),
15763            &maintenanceinfolist);
15764
15765   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15766 Set catchpoints to catch events."),
15767                   &catch_cmdlist, "catch ",
15768                   0/*allow-unknown*/, &cmdlist);
15769
15770   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15771 Set temporary catchpoints to catch events."),
15772                   &tcatch_cmdlist, "tcatch ",
15773                   0/*allow-unknown*/, &cmdlist);
15774
15775   /* Add catch and tcatch sub-commands.  */
15776   add_catch_command ("catch", _("\
15777 Catch an exception, when caught."),
15778                      catch_catch_command,
15779                      NULL,
15780                      CATCH_PERMANENT,
15781                      CATCH_TEMPORARY);
15782   add_catch_command ("throw", _("\
15783 Catch an exception, when thrown."),
15784                      catch_throw_command,
15785                      NULL,
15786                      CATCH_PERMANENT,
15787                      CATCH_TEMPORARY);
15788   add_catch_command ("fork", _("Catch calls to fork."),
15789                      catch_fork_command_1,
15790                      NULL,
15791                      (void *) (uintptr_t) catch_fork_permanent,
15792                      (void *) (uintptr_t) catch_fork_temporary);
15793   add_catch_command ("vfork", _("Catch calls to vfork."),
15794                      catch_fork_command_1,
15795                      NULL,
15796                      (void *) (uintptr_t) catch_vfork_permanent,
15797                      (void *) (uintptr_t) catch_vfork_temporary);
15798   add_catch_command ("exec", _("Catch calls to exec."),
15799                      catch_exec_command_1,
15800                      NULL,
15801                      CATCH_PERMANENT,
15802                      CATCH_TEMPORARY);
15803   add_catch_command ("load", _("Catch loads of shared libraries.\n\
15804 Usage: catch load [REGEX]\n\
15805 If REGEX is given, only stop for libraries matching the regular expression."),
15806                      catch_load_command_1,
15807                      NULL,
15808                      CATCH_PERMANENT,
15809                      CATCH_TEMPORARY);
15810   add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15811 Usage: catch unload [REGEX]\n\
15812 If REGEX is given, only stop for libraries matching the regular expression."),
15813                      catch_unload_command_1,
15814                      NULL,
15815                      CATCH_PERMANENT,
15816                      CATCH_TEMPORARY);
15817   add_catch_command ("syscall", _("\
15818 Catch system calls by their names and/or numbers.\n\
15819 Arguments say which system calls to catch.  If no arguments\n\
15820 are given, every system call will be caught.\n\
15821 Arguments, if given, should be one or more system call names\n\
15822 (if your system supports that), or system call numbers."),
15823                      catch_syscall_command_1,
15824                      catch_syscall_completer,
15825                      CATCH_PERMANENT,
15826                      CATCH_TEMPORARY);
15827
15828   c = add_com ("watch", class_breakpoint, watch_command, _("\
15829 Set a watchpoint for an expression.\n\
15830 Usage: watch [-l|-location] EXPRESSION\n\
15831 A watchpoint stops execution of your program whenever the value of\n\
15832 an expression changes.\n\
15833 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15834 the memory to which it refers."));
15835   set_cmd_completer (c, expression_completer);
15836
15837   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15838 Set a read watchpoint for an expression.\n\
15839 Usage: rwatch [-l|-location] EXPRESSION\n\
15840 A watchpoint stops execution of your program whenever the value of\n\
15841 an expression is read.\n\
15842 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15843 the memory to which it refers."));
15844   set_cmd_completer (c, expression_completer);
15845
15846   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15847 Set a watchpoint for an expression.\n\
15848 Usage: awatch [-l|-location] EXPRESSION\n\
15849 A watchpoint stops execution of your program whenever the value of\n\
15850 an expression is either read or written.\n\
15851 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15852 the memory to which it refers."));
15853   set_cmd_completer (c, expression_completer);
15854
15855   add_info ("watchpoints", watchpoints_info, _("\
15856 Status of specified watchpoints (all watchpoints if no argument)."));
15857
15858   /* XXX: cagney/2005-02-23: This should be a boolean, and should
15859      respond to changes - contrary to the description.  */
15860   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15861                             &can_use_hw_watchpoints, _("\
15862 Set debugger's willingness to use watchpoint hardware."), _("\
15863 Show debugger's willingness to use watchpoint hardware."), _("\
15864 If zero, gdb will not use hardware for new watchpoints, even if\n\
15865 such is available.  (However, any hardware watchpoints that were\n\
15866 created before setting this to nonzero, will continue to use watchpoint\n\
15867 hardware.)"),
15868                             NULL,
15869                             show_can_use_hw_watchpoints,
15870                             &setlist, &showlist);
15871
15872   can_use_hw_watchpoints = 1;
15873
15874   /* Tracepoint manipulation commands.  */
15875
15876   c = add_com ("trace", class_breakpoint, trace_command, _("\
15877 Set a tracepoint at specified line or function.\n\
15878 \n"
15879 BREAK_ARGS_HELP ("trace") "\n\
15880 Do \"help tracepoints\" for info on other tracepoint commands."));
15881   set_cmd_completer (c, location_completer);
15882
15883   add_com_alias ("tp", "trace", class_alias, 0);
15884   add_com_alias ("tr", "trace", class_alias, 1);
15885   add_com_alias ("tra", "trace", class_alias, 1);
15886   add_com_alias ("trac", "trace", class_alias, 1);
15887
15888   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15889 Set a fast tracepoint at specified line or function.\n\
15890 \n"
15891 BREAK_ARGS_HELP ("ftrace") "\n\
15892 Do \"help tracepoints\" for info on other tracepoint commands."));
15893   set_cmd_completer (c, location_completer);
15894
15895   c = add_com ("strace", class_breakpoint, strace_command, _("\
15896 Set a static tracepoint at specified line, function or marker.\n\
15897 \n\
15898 strace [LOCATION] [if CONDITION]\n\
15899 LOCATION may be a line number, function name, \"*\" and an address,\n\
15900 or -m MARKER_ID.\n\
15901 If a line number is specified, probe the marker at start of code\n\
15902 for that line.  If a function is specified, probe the marker at start\n\
15903 of code for that function.  If an address is specified, probe the marker\n\
15904 at that exact address.  If a marker id is specified, probe the marker\n\
15905 with that name.  With no LOCATION, uses current execution address of\n\
15906 the selected stack frame.\n\
15907 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15908 This collects arbitrary user data passed in the probe point call to the\n\
15909 tracing library.  You can inspect it when analyzing the trace buffer,\n\
15910 by printing the $_sdata variable like any other convenience variable.\n\
15911 \n\
15912 CONDITION is a boolean expression.\n\
15913 \n\
15914 Multiple tracepoints at one place are permitted, and useful if their\n\
15915 conditions are different.\n\
15916 \n\
15917 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15918 Do \"help tracepoints\" for info on other tracepoint commands."));
15919   set_cmd_completer (c, location_completer);
15920
15921   add_info ("tracepoints", tracepoints_info, _("\
15922 Status of specified tracepoints (all tracepoints if no argument).\n\
15923 Convenience variable \"$tpnum\" contains the number of the\n\
15924 last tracepoint set."));
15925
15926   add_info_alias ("tp", "tracepoints", 1);
15927
15928   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15929 Delete specified tracepoints.\n\
15930 Arguments are tracepoint numbers, separated by spaces.\n\
15931 No argument means delete all tracepoints."),
15932            &deletelist);
15933
15934   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15935 Disable specified tracepoints.\n\
15936 Arguments are tracepoint numbers, separated by spaces.\n\
15937 No argument means disable all tracepoints."),
15938            &disablelist);
15939   deprecate_cmd (c, "disable");
15940
15941   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15942 Enable specified tracepoints.\n\
15943 Arguments are tracepoint numbers, separated by spaces.\n\
15944 No argument means enable all tracepoints."),
15945            &enablelist);
15946   deprecate_cmd (c, "enable");
15947
15948   add_com ("passcount", class_trace, trace_pass_command, _("\
15949 Set the passcount for a tracepoint.\n\
15950 The trace will end when the tracepoint has been passed 'count' times.\n\
15951 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15952 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15953
15954   add_prefix_cmd ("save", class_breakpoint, save_command,
15955                   _("Save breakpoint definitions as a script."),
15956                   &save_cmdlist, "save ",
15957                   0/*allow-unknown*/, &cmdlist);
15958
15959   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15960 Save current breakpoint definitions as a script.\n\
15961 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15962 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
15963 session to restore them."),
15964                &save_cmdlist);
15965   set_cmd_completer (c, filename_completer);
15966
15967   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15968 Save current tracepoint definitions as a script.\n\
15969 Use the 'source' command in another debug session to restore them."),
15970                &save_cmdlist);
15971   set_cmd_completer (c, filename_completer);
15972
15973   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15974   deprecate_cmd (c, "save tracepoints");
15975
15976   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15977 Breakpoint specific settings\n\
15978 Configure various breakpoint-specific variables such as\n\
15979 pending breakpoint behavior"),
15980                   &breakpoint_set_cmdlist, "set breakpoint ",
15981                   0/*allow-unknown*/, &setlist);
15982   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15983 Breakpoint specific settings\n\
15984 Configure various breakpoint-specific variables such as\n\
15985 pending breakpoint behavior"),
15986                   &breakpoint_show_cmdlist, "show breakpoint ",
15987                   0/*allow-unknown*/, &showlist);
15988
15989   add_setshow_auto_boolean_cmd ("pending", no_class,
15990                                 &pending_break_support, _("\
15991 Set debugger's behavior regarding pending breakpoints."), _("\
15992 Show debugger's behavior regarding pending breakpoints."), _("\
15993 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15994 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
15995 an error.  If auto, an unrecognized breakpoint location results in a\n\
15996 user-query to see if a pending breakpoint should be created."),
15997                                 NULL,
15998                                 show_pending_break_support,
15999                                 &breakpoint_set_cmdlist,
16000                                 &breakpoint_show_cmdlist);
16001
16002   pending_break_support = AUTO_BOOLEAN_AUTO;
16003
16004   add_setshow_boolean_cmd ("auto-hw", no_class,
16005                            &automatic_hardware_breakpoints, _("\
16006 Set automatic usage of hardware breakpoints."), _("\
16007 Show automatic usage of hardware breakpoints."), _("\
16008 If set, the debugger will automatically use hardware breakpoints for\n\
16009 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
16010 a warning will be emitted for such breakpoints."),
16011                            NULL,
16012                            show_automatic_hardware_breakpoints,
16013                            &breakpoint_set_cmdlist,
16014                            &breakpoint_show_cmdlist);
16015
16016   add_setshow_enum_cmd ("always-inserted", class_support,
16017                         always_inserted_enums, &always_inserted_mode, _("\
16018 Set mode for inserting breakpoints."), _("\
16019 Show mode for inserting breakpoints."), _("\
16020 When this mode is off, breakpoints are inserted in inferior when it is\n\
16021 resumed, and removed when execution stops.  When this mode is on,\n\
16022 breakpoints are inserted immediately and removed only when the user\n\
16023 deletes the breakpoint.  When this mode is auto (which is the default),\n\
16024 the behaviour depends on the non-stop setting (see help set non-stop).\n\
16025 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16026 behaves as if always-inserted mode is on; if gdb is controlling the\n\
16027 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
16028                            NULL,
16029                            &show_always_inserted_mode,
16030                            &breakpoint_set_cmdlist,
16031                            &breakpoint_show_cmdlist);
16032
16033   add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16034                         condition_evaluation_enums,
16035                         &condition_evaluation_mode_1, _("\
16036 Set mode of breakpoint condition evaluation."), _("\
16037 Show mode of breakpoint condition evaluation."), _("\
16038 When this is set to \"host\", breakpoint conditions will be\n\
16039 evaluated on the host's side by GDB.  When it is set to \"target\",\n\
16040 breakpoint conditions will be downloaded to the target (if the target\n\
16041 supports such feature) and conditions will be evaluated on the target's side.\n\
16042 If this is set to \"auto\" (default), this will be automatically set to\n\
16043 \"target\" if it supports condition evaluation, otherwise it will\n\
16044 be set to \"gdb\""),
16045                            &set_condition_evaluation_mode,
16046                            &show_condition_evaluation_mode,
16047                            &breakpoint_set_cmdlist,
16048                            &breakpoint_show_cmdlist);
16049
16050   add_com ("break-range", class_breakpoint, break_range_command, _("\
16051 Set a breakpoint for an address range.\n\
16052 break-range START-LOCATION, END-LOCATION\n\
16053 where START-LOCATION and END-LOCATION can be one of the following:\n\
16054   LINENUM, for that line in the current file,\n\
16055   FILE:LINENUM, for that line in that file,\n\
16056   +OFFSET, for that number of lines after the current line\n\
16057            or the start of the range\n\
16058   FUNCTION, for the first line in that function,\n\
16059   FILE:FUNCTION, to distinguish among like-named static functions.\n\
16060   *ADDRESS, for the instruction at that address.\n\
16061 \n\
16062 The breakpoint will stop execution of the inferior whenever it executes\n\
16063 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16064 range (including START-LOCATION and END-LOCATION)."));
16065
16066   c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16067 Set a dynamic printf at specified line or function.\n\
16068 dprintf location,format string,arg1,arg2,...\n\
16069 location may be a line number, function name, or \"*\" and an address.\n\
16070 If a line number is specified, break at start of code for that line.\n\
16071 If a function is specified, break at start of code for that function.\n\
16072 "));
16073   set_cmd_completer (c, location_completer);
16074
16075   add_setshow_enum_cmd ("dprintf-style", class_support,
16076                         dprintf_style_enums, &dprintf_style, _("\
16077 Set the style of usage for dynamic printf."), _("\
16078 Show the style of usage for dynamic printf."), _("\
16079 This setting chooses how GDB will do a dynamic printf.\n\
16080 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16081 console, as with the \"printf\" command.\n\
16082 If the value is \"call\", the print is done by calling a function in your\n\
16083 program; by default printf(), but you can choose a different function or\n\
16084 output stream by setting dprintf-function and dprintf-channel."),
16085                         update_dprintf_commands, NULL,
16086                         &setlist, &showlist);
16087
16088   dprintf_function = xstrdup ("printf");
16089   add_setshow_string_cmd ("dprintf-function", class_support,
16090                           &dprintf_function, _("\
16091 Set the function to use for dynamic printf"), _("\
16092 Show the function to use for dynamic printf"), NULL,
16093                           update_dprintf_commands, NULL,
16094                           &setlist, &showlist);
16095
16096   dprintf_channel = xstrdup ("");
16097   add_setshow_string_cmd ("dprintf-channel", class_support,
16098                           &dprintf_channel, _("\
16099 Set the channel to use for dynamic printf"), _("\
16100 Show the channel to use for dynamic printf"), NULL,
16101                           update_dprintf_commands, NULL,
16102                           &setlist, &showlist);
16103
16104   automatic_hardware_breakpoints = 1;
16105
16106   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16107 }