931f433cf1b1ba0c85044d39de7aa0cf0de69a88
[platform/upstream/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5    2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include <ctype.h>
25 #include "hashtab.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "breakpoint.h"
29 #include "tracepoint.h"
30 #include "gdbtypes.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "value.h"
35 #include "command.h"
36 #include "inferior.h"
37 #include "gdbthread.h"
38 #include "target.h"
39 #include "language.h"
40 #include "gdb_string.h"
41 #include "gdb-demangle.h"
42 #include "filenames.h"
43 #include "annotate.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "source.h"
47 #include "linespec.h"
48 #include "completer.h"
49 #include "gdb.h"
50 #include "ui-out.h"
51 #include "cli/cli-script.h"
52 #include "gdb_assert.h"
53 #include "block.h"
54 #include "solib.h"
55 #include "solist.h"
56 #include "observer.h"
57 #include "exceptions.h"
58 #include "memattr.h"
59 #include "ada-lang.h"
60 #include "top.h"
61 #include "wrapper.h"
62 #include "valprint.h"
63 #include "jit.h"
64 #include "xml-syscall.h"
65 #include "parser-defs.h"
66 #include "cli/cli-utils.h"
67 #include "continuations.h"
68 #include "stack.h"
69 #include "skip.h"
70
71 /* readline include files */
72 #include "readline/readline.h"
73 #include "readline/history.h"
74
75 /* readline defines this.  */
76 #undef savestring
77
78 #include "mi/mi-common.h"
79 #include "python/python.h"
80
81 /* Prototypes for local functions.  */
82
83 static void enable_delete_command (char *, int);
84
85 static void enable_once_command (char *, int);
86
87 static void disable_command (char *, int);
88
89 static void enable_command (char *, int);
90
91 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
92                                                       void *),
93                                     void *);
94
95 static void ignore_command (char *, int);
96
97 static int breakpoint_re_set_one (void *);
98
99 static void breakpoint_re_set_default (struct breakpoint *);
100
101 static void clear_command (char *, int);
102
103 static void catch_command (char *, int);
104
105 static int can_use_hardware_watchpoint (struct value *);
106
107 static void break_command_1 (char *, int, int);
108
109 static void mention (struct breakpoint *);
110
111 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
112                                                                enum bptype,
113                                                                const struct breakpoint_ops *);
114 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
115                                                        const struct symtab_and_line *);
116
117 /* This function is used in gdbtk sources and thus can not be made
118    static.  */
119 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
120                                        struct symtab_and_line,
121                                        enum bptype,
122                                        const struct breakpoint_ops *);
123
124 static struct breakpoint *
125   momentary_breakpoint_from_master (struct breakpoint *orig,
126                                     enum bptype type,
127                                     const struct breakpoint_ops *ops);
128
129 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
130
131 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
132                                             CORE_ADDR bpaddr,
133                                             enum bptype bptype);
134
135 static void describe_other_breakpoints (struct gdbarch *,
136                                         struct program_space *, CORE_ADDR,
137                                         struct obj_section *, int);
138
139 static int breakpoint_address_match (struct address_space *aspace1,
140                                      CORE_ADDR addr1,
141                                      struct address_space *aspace2,
142                                      CORE_ADDR addr2);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145                                        struct bp_location *loc2);
146
147 static int breakpoint_location_address_match (struct bp_location *bl,
148                                               struct address_space *aspace,
149                                               CORE_ADDR addr);
150
151 static void breakpoints_info (char *, int);
152
153 static void watchpoints_info (char *, int);
154
155 static int breakpoint_1 (char *, int, 
156                          int (*) (const struct breakpoint *));
157
158 static int breakpoint_cond_eval (void *);
159
160 static void cleanup_executing_breakpoints (void *);
161
162 static void commands_command (char *, int);
163
164 static void condition_command (char *, int);
165
166 typedef enum
167   {
168     mark_inserted,
169     mark_uninserted
170   }
171 insertion_state_t;
172
173 static int remove_breakpoint (struct bp_location *, insertion_state_t);
174 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
175
176 static enum print_stop_action print_bp_stop_message (bpstat bs);
177
178 static int watchpoint_check (void *);
179
180 static void maintenance_info_breakpoints (char *, int);
181
182 static int hw_breakpoint_used_count (void);
183
184 static int hw_watchpoint_use_count (struct breakpoint *);
185
186 static int hw_watchpoint_used_count_others (struct breakpoint *except,
187                                             enum bptype type,
188                                             int *other_type_used);
189
190 static void hbreak_command (char *, int);
191
192 static void thbreak_command (char *, int);
193
194 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
195
196 static void stop_command (char *arg, int from_tty);
197
198 static void stopin_command (char *arg, int from_tty);
199
200 static void stopat_command (char *arg, int from_tty);
201
202 static char *ep_parse_optional_if_clause (char **arg);
203
204 static void catch_exception_command_1 (enum exception_event_kind ex_event, 
205                                        char *arg, int tempflag, int from_tty);
206
207 static void tcatch_command (char *arg, int from_tty);
208
209 static void detach_single_step_breakpoints (void);
210
211 static int single_step_breakpoint_inserted_here_p (struct address_space *,
212                                                    CORE_ADDR pc);
213
214 static void free_bp_location (struct bp_location *loc);
215 static void incref_bp_location (struct bp_location *loc);
216 static void decref_bp_location (struct bp_location **loc);
217
218 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
219
220 static void update_global_location_list (int);
221
222 static void update_global_location_list_nothrow (int);
223
224 static int is_hardware_watchpoint (const struct breakpoint *bpt);
225
226 static void insert_breakpoint_locations (void);
227
228 static int syscall_catchpoint_p (struct breakpoint *b);
229
230 static void tracepoints_info (char *, int);
231
232 static void delete_trace_command (char *, int);
233
234 static void enable_trace_command (char *, int);
235
236 static void disable_trace_command (char *, int);
237
238 static void trace_pass_command (char *, int);
239
240 static int is_masked_watchpoint (const struct breakpoint *b);
241
242 /* Assuming we're creating a static tracepoint, does S look like a
243    static tracepoint marker spec ("-m MARKER_ID")?  */
244 #define is_marker_spec(s)                                               \
245   (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
246
247 /* The abstract base class all breakpoint_ops structures inherit
248    from.  */
249 static struct breakpoint_ops base_breakpoint_ops;
250
251 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
252    that are implemented on top of software or hardware breakpoints
253    (user breakpoints, internal and momentary breakpoints, etc.).  */
254 static struct breakpoint_ops bkpt_base_breakpoint_ops;
255
256 /* Internal breakpoints class type.  */
257 static struct breakpoint_ops internal_breakpoint_ops;
258
259 /* Momentary breakpoints class type.  */
260 static struct breakpoint_ops momentary_breakpoint_ops;
261
262 /* The breakpoint_ops structure to be used in regular user created
263    breakpoints.  */
264 struct breakpoint_ops bkpt_breakpoint_ops;
265
266 /* A reference-counted struct command_line.  This lets multiple
267    breakpoints share a single command list.  */
268 struct counted_command_line
269 {
270   /* The reference count.  */
271   int refc;
272
273   /* The command list.  */
274   struct command_line *commands;
275 };
276
277 struct command_line *
278 breakpoint_commands (struct breakpoint *b)
279 {
280   return b->commands ? b->commands->commands : NULL;
281 }
282
283 /* Flag indicating that a command has proceeded the inferior past the
284    current breakpoint.  */
285
286 static int breakpoint_proceeded;
287
288 const char *
289 bpdisp_text (enum bpdisp disp)
290 {
291   /* NOTE: the following values are a part of MI protocol and
292      represent values of 'disp' field returned when inferior stops at
293      a breakpoint.  */
294   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
295
296   return bpdisps[(int) disp];
297 }
298
299 /* Prototypes for exported functions.  */
300 /* If FALSE, gdb will not use hardware support for watchpoints, even
301    if such is available.  */
302 static int can_use_hw_watchpoints;
303
304 static void
305 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
306                              struct cmd_list_element *c,
307                              const char *value)
308 {
309   fprintf_filtered (file,
310                     _("Debugger's willingness to use "
311                       "watchpoint hardware is %s.\n"),
312                     value);
313 }
314
315 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
316    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
317    for unrecognized breakpoint locations.
318    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
319 static enum auto_boolean pending_break_support;
320 static void
321 show_pending_break_support (struct ui_file *file, int from_tty,
322                             struct cmd_list_element *c,
323                             const char *value)
324 {
325   fprintf_filtered (file,
326                     _("Debugger's behavior regarding "
327                       "pending breakpoints is %s.\n"),
328                     value);
329 }
330
331 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
332    set with "break" but falling in read-only memory.
333    If 0, gdb will warn about such breakpoints, but won't automatically
334    use hardware breakpoints.  */
335 static int automatic_hardware_breakpoints;
336 static void
337 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
338                                      struct cmd_list_element *c,
339                                      const char *value)
340 {
341   fprintf_filtered (file,
342                     _("Automatic usage of hardware breakpoints is %s.\n"),
343                     value);
344 }
345
346 /* If on, gdb will keep breakpoints inserted even as inferior is
347    stopped, and immediately insert any new breakpoints.  If off, gdb
348    will insert breakpoints into inferior only when resuming it, and
349    will remove breakpoints upon stop.  If auto, GDB will behave as ON
350    if in non-stop mode, and as OFF if all-stop mode.*/
351
352 static const char always_inserted_auto[] = "auto";
353 static const char always_inserted_on[] = "on";
354 static const char always_inserted_off[] = "off";
355 static const char *always_inserted_enums[] = {
356   always_inserted_auto,
357   always_inserted_off,
358   always_inserted_on,
359   NULL
360 };
361 static const char *always_inserted_mode = always_inserted_auto;
362 static void
363 show_always_inserted_mode (struct ui_file *file, int from_tty,
364                      struct cmd_list_element *c, const char *value)
365 {
366   if (always_inserted_mode == always_inserted_auto)
367     fprintf_filtered (file,
368                       _("Always inserted breakpoint "
369                         "mode is %s (currently %s).\n"),
370                       value,
371                       breakpoints_always_inserted_mode () ? "on" : "off");
372   else
373     fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
374                       value);
375 }
376
377 int
378 breakpoints_always_inserted_mode (void)
379 {
380   return (always_inserted_mode == always_inserted_on
381           || (always_inserted_mode == always_inserted_auto && non_stop));
382 }
383
384 void _initialize_breakpoint (void);
385
386 /* Are we executing breakpoint commands?  */
387 static int executing_breakpoint_commands;
388
389 /* Are overlay event breakpoints enabled? */
390 static int overlay_events_enabled;
391
392 /* See description in breakpoint.h. */
393 int target_exact_watchpoints = 0;
394
395 /* Walk the following statement or block through all breakpoints.
396    ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
397    current breakpoint.  */
398
399 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
400
401 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
402         for (B = breakpoint_chain;      \
403              B ? (TMP=B->next, 1): 0;   \
404              B = TMP)
405
406 /* Similar iterator for the low-level breakpoints.  SAFE variant is
407    not provided so update_global_location_list must not be called
408    while executing the block of ALL_BP_LOCATIONS.  */
409
410 #define ALL_BP_LOCATIONS(B,BP_TMP)                                      \
411         for (BP_TMP = bp_location;                                      \
412              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
413              BP_TMP++)
414
415 /* Iterator for tracepoints only.  */
416
417 #define ALL_TRACEPOINTS(B)  \
418   for (B = breakpoint_chain; B; B = B->next)  \
419     if (is_tracepoint (B))
420
421 /* Chains of all breakpoints defined.  */
422
423 struct breakpoint *breakpoint_chain;
424
425 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
426
427 static struct bp_location **bp_location;
428
429 /* Number of elements of BP_LOCATION.  */
430
431 static unsigned bp_location_count;
432
433 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
434    ADDRESS for the current elements of BP_LOCATION which get a valid
435    result from bp_location_has_shadow.  You can use it for roughly
436    limiting the subrange of BP_LOCATION to scan for shadow bytes for
437    an address you need to read.  */
438
439 static CORE_ADDR bp_location_placed_address_before_address_max;
440
441 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
442    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
443    BP_LOCATION which get a valid result from bp_location_has_shadow.
444    You can use it for roughly limiting the subrange of BP_LOCATION to
445    scan for shadow bytes for an address you need to read.  */
446
447 static CORE_ADDR bp_location_shadow_len_after_address_max;
448
449 /* The locations that no longer correspond to any breakpoint, unlinked
450    from bp_location array, but for which a hit may still be reported
451    by a target.  */
452 VEC(bp_location_p) *moribund_locations = NULL;
453
454 /* Number of last breakpoint made.  */
455
456 static int breakpoint_count;
457
458 /* The value of `breakpoint_count' before the last command that
459    created breakpoints.  If the last (break-like) command created more
460    than one breakpoint, then the difference between BREAKPOINT_COUNT
461    and PREV_BREAKPOINT_COUNT is more than one.  */
462 static int prev_breakpoint_count;
463
464 /* Number of last tracepoint made.  */
465
466 static int tracepoint_count;
467
468 static struct cmd_list_element *breakpoint_set_cmdlist;
469 static struct cmd_list_element *breakpoint_show_cmdlist;
470 struct cmd_list_element *save_cmdlist;
471
472 /* Return whether a breakpoint is an active enabled breakpoint.  */
473 static int
474 breakpoint_enabled (struct breakpoint *b)
475 {
476   return (b->enable_state == bp_enabled);
477 }
478
479 /* Set breakpoint count to NUM.  */
480
481 static void
482 set_breakpoint_count (int num)
483 {
484   prev_breakpoint_count = breakpoint_count;
485   breakpoint_count = num;
486   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
487 }
488
489 /* Used by `start_rbreak_breakpoints' below, to record the current
490    breakpoint count before "rbreak" creates any breakpoint.  */
491 static int rbreak_start_breakpoint_count;
492
493 /* Called at the start an "rbreak" command to record the first
494    breakpoint made.  */
495
496 void
497 start_rbreak_breakpoints (void)
498 {
499   rbreak_start_breakpoint_count = breakpoint_count;
500 }
501
502 /* Called at the end of an "rbreak" command to record the last
503    breakpoint made.  */
504
505 void
506 end_rbreak_breakpoints (void)
507 {
508   prev_breakpoint_count = rbreak_start_breakpoint_count;
509 }
510
511 /* Used in run_command to zero the hit count when a new run starts.  */
512
513 void
514 clear_breakpoint_hit_counts (void)
515 {
516   struct breakpoint *b;
517
518   ALL_BREAKPOINTS (b)
519     b->hit_count = 0;
520 }
521
522 /* Allocate a new counted_command_line with reference count of 1.
523    The new structure owns COMMANDS.  */
524
525 static struct counted_command_line *
526 alloc_counted_command_line (struct command_line *commands)
527 {
528   struct counted_command_line *result
529     = xmalloc (sizeof (struct counted_command_line));
530
531   result->refc = 1;
532   result->commands = commands;
533   return result;
534 }
535
536 /* Increment reference count.  This does nothing if CMD is NULL.  */
537
538 static void
539 incref_counted_command_line (struct counted_command_line *cmd)
540 {
541   if (cmd)
542     ++cmd->refc;
543 }
544
545 /* Decrement reference count.  If the reference count reaches 0,
546    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
547    nothing if *CMDP is NULL.  */
548
549 static void
550 decref_counted_command_line (struct counted_command_line **cmdp)
551 {
552   if (*cmdp)
553     {
554       if (--(*cmdp)->refc == 0)
555         {
556           free_command_lines (&(*cmdp)->commands);
557           xfree (*cmdp);
558         }
559       *cmdp = NULL;
560     }
561 }
562
563 /* A cleanup function that calls decref_counted_command_line.  */
564
565 static void
566 do_cleanup_counted_command_line (void *arg)
567 {
568   decref_counted_command_line (arg);
569 }
570
571 /* Create a cleanup that calls decref_counted_command_line on the
572    argument.  */
573
574 static struct cleanup *
575 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
576 {
577   return make_cleanup (do_cleanup_counted_command_line, cmdp);
578 }
579
580 \f
581 /* Return the breakpoint with the specified number, or NULL
582    if the number does not refer to an existing breakpoint.  */
583
584 struct breakpoint *
585 get_breakpoint (int num)
586 {
587   struct breakpoint *b;
588
589   ALL_BREAKPOINTS (b)
590     if (b->number == num)
591       return b;
592   
593   return NULL;
594 }
595
596 \f
597
598 void
599 set_breakpoint_condition (struct breakpoint *b, char *exp,
600                           int from_tty)
601 {
602   xfree (b->cond_string);
603   b->cond_string = NULL;
604
605   if (is_watchpoint (b))
606     {
607       struct watchpoint *w = (struct watchpoint *) b;
608
609       xfree (w->cond_exp);
610       w->cond_exp = NULL;
611     }
612   else
613     {
614       struct bp_location *loc;
615
616       for (loc = b->loc; loc; loc = loc->next)
617         {
618           xfree (loc->cond);
619           loc->cond = NULL;
620         }
621     }
622
623   if (*exp == 0)
624     {
625       if (from_tty)
626         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
627     }
628   else
629     {
630       char *arg = exp;
631
632       /* I don't know if it matters whether this is the string the user
633          typed in or the decompiled expression.  */
634       b->cond_string = xstrdup (arg);
635       b->condition_not_parsed = 0;
636
637       if (is_watchpoint (b))
638         {
639           struct watchpoint *w = (struct watchpoint *) b;
640
641           innermost_block = NULL;
642           arg = exp;
643           w->cond_exp = parse_exp_1 (&arg, 0, 0);
644           if (*arg)
645             error (_("Junk at end of expression"));
646           w->cond_exp_valid_block = innermost_block;
647         }
648       else
649         {
650           struct bp_location *loc;
651
652           for (loc = b->loc; loc; loc = loc->next)
653             {
654               arg = exp;
655               loc->cond =
656                 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
657               if (*arg)
658                 error (_("Junk at end of expression"));
659             }
660         }
661     }
662   breakpoints_changed ();
663   observer_notify_breakpoint_modified (b);
664 }
665
666 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
667
668 static void
669 condition_command (char *arg, int from_tty)
670 {
671   struct breakpoint *b;
672   char *p;
673   int bnum;
674
675   if (arg == 0)
676     error_no_arg (_("breakpoint number"));
677
678   p = arg;
679   bnum = get_number (&p);
680   if (bnum == 0)
681     error (_("Bad breakpoint argument: '%s'"), arg);
682
683   ALL_BREAKPOINTS (b)
684     if (b->number == bnum)
685       {
686         /* Check if this breakpoint has a Python object assigned to
687            it, and if it has a definition of the "stop"
688            method.  This method and conditions entered into GDB from
689            the CLI are mutually exclusive.  */
690         if (b->py_bp_object
691             && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
692           error (_("Cannot set a condition where a Python 'stop' "
693                    "method has been defined in the breakpoint."));
694         set_breakpoint_condition (b, p, from_tty);
695         return;
696       }
697
698   error (_("No breakpoint number %d."), bnum);
699 }
700
701 /* Check that COMMAND do not contain commands that are suitable
702    only for tracepoints and not suitable for ordinary breakpoints.
703    Throw if any such commands is found.  */
704
705 static void
706 check_no_tracepoint_commands (struct command_line *commands)
707 {
708   struct command_line *c;
709
710   for (c = commands; c; c = c->next)
711     {
712       int i;
713
714       if (c->control_type == while_stepping_control)
715         error (_("The 'while-stepping' command can "
716                  "only be used for tracepoints"));
717
718       for (i = 0; i < c->body_count; ++i)
719         check_no_tracepoint_commands ((c->body_list)[i]);
720
721       /* Not that command parsing removes leading whitespace and comment
722          lines and also empty lines.  So, we only need to check for
723          command directly.  */
724       if (strstr (c->line, "collect ") == c->line)
725         error (_("The 'collect' command can only be used for tracepoints"));
726
727       if (strstr (c->line, "teval ") == c->line)
728         error (_("The 'teval' command can only be used for tracepoints"));
729     }
730 }
731
732 /* Encapsulate tests for different types of tracepoints.  */
733
734 static int
735 is_tracepoint_type (enum bptype type)
736 {
737   return (type == bp_tracepoint
738           || type == bp_fast_tracepoint
739           || type == bp_static_tracepoint);
740 }
741
742 int
743 is_tracepoint (const struct breakpoint *b)
744 {
745   return is_tracepoint_type (b->type);
746 }
747
748 /* A helper function that validates that COMMANDS are valid for a
749    breakpoint.  This function will throw an exception if a problem is
750    found.  */
751
752 static void
753 validate_commands_for_breakpoint (struct breakpoint *b,
754                                   struct command_line *commands)
755 {
756   if (is_tracepoint (b))
757     {
758       /* We need to verify that each top-level element of commands is
759          valid for tracepoints, that there's at most one
760          while-stepping element, and that while-stepping's body has
761          valid tracing commands excluding nested while-stepping.  */
762       struct command_line *c;
763       struct command_line *while_stepping = 0;
764       for (c = commands; c; c = c->next)
765         {
766           if (c->control_type == while_stepping_control)
767             {
768               if (b->type == bp_fast_tracepoint)
769                 error (_("The 'while-stepping' command "
770                          "cannot be used for fast tracepoint"));
771               else if (b->type == bp_static_tracepoint)
772                 error (_("The 'while-stepping' command "
773                          "cannot be used for static tracepoint"));
774
775               if (while_stepping)
776                 error (_("The 'while-stepping' command "
777                          "can be used only once"));
778               else
779                 while_stepping = c;
780             }
781         }
782       if (while_stepping)
783         {
784           struct command_line *c2;
785
786           gdb_assert (while_stepping->body_count == 1);
787           c2 = while_stepping->body_list[0];
788           for (; c2; c2 = c2->next)
789             {
790               if (c2->control_type == while_stepping_control)
791                 error (_("The 'while-stepping' command cannot be nested"));
792             }
793         }
794     }
795   else
796     {
797       check_no_tracepoint_commands (commands);
798     }
799 }
800
801 /* Return a vector of all the static tracepoints set at ADDR.  The
802    caller is responsible for releasing the vector.  */
803
804 VEC(breakpoint_p) *
805 static_tracepoints_here (CORE_ADDR addr)
806 {
807   struct breakpoint *b;
808   VEC(breakpoint_p) *found = 0;
809   struct bp_location *loc;
810
811   ALL_BREAKPOINTS (b)
812     if (b->type == bp_static_tracepoint)
813       {
814         for (loc = b->loc; loc; loc = loc->next)
815           if (loc->address == addr)
816             VEC_safe_push(breakpoint_p, found, b);
817       }
818
819   return found;
820 }
821
822 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
823    validate that only allowed commands are included.  */
824
825 void
826 breakpoint_set_commands (struct breakpoint *b, 
827                          struct command_line *commands)
828 {
829   validate_commands_for_breakpoint (b, commands);
830
831   decref_counted_command_line (&b->commands);
832   b->commands = alloc_counted_command_line (commands);
833   breakpoints_changed ();
834   observer_notify_breakpoint_modified (b);
835 }
836
837 /* Set the internal `silent' flag on the breakpoint.  Note that this
838    is not the same as the "silent" that may appear in the breakpoint's
839    commands.  */
840
841 void
842 breakpoint_set_silent (struct breakpoint *b, int silent)
843 {
844   int old_silent = b->silent;
845
846   b->silent = silent;
847   if (old_silent != silent)
848     observer_notify_breakpoint_modified (b);
849 }
850
851 /* Set the thread for this breakpoint.  If THREAD is -1, make the
852    breakpoint work for any thread.  */
853
854 void
855 breakpoint_set_thread (struct breakpoint *b, int thread)
856 {
857   int old_thread = b->thread;
858
859   b->thread = thread;
860   if (old_thread != thread)
861     observer_notify_breakpoint_modified (b);
862 }
863
864 /* Set the task for this breakpoint.  If TASK is 0, make the
865    breakpoint work for any task.  */
866
867 void
868 breakpoint_set_task (struct breakpoint *b, int task)
869 {
870   int old_task = b->task;
871
872   b->task = task;
873   if (old_task != task)
874     observer_notify_breakpoint_modified (b);
875 }
876
877 void
878 check_tracepoint_command (char *line, void *closure)
879 {
880   struct breakpoint *b = closure;
881
882   validate_actionline (&line, b);
883 }
884
885 /* A structure used to pass information through
886    map_breakpoint_numbers.  */
887
888 struct commands_info
889 {
890   /* True if the command was typed at a tty.  */
891   int from_tty;
892
893   /* The breakpoint range spec.  */
894   char *arg;
895
896   /* Non-NULL if the body of the commands are being read from this
897      already-parsed command.  */
898   struct command_line *control;
899
900   /* The command lines read from the user, or NULL if they have not
901      yet been read.  */
902   struct counted_command_line *cmd;
903 };
904
905 /* A callback for map_breakpoint_numbers that sets the commands for
906    commands_command.  */
907
908 static void
909 do_map_commands_command (struct breakpoint *b, void *data)
910 {
911   struct commands_info *info = data;
912
913   if (info->cmd == NULL)
914     {
915       struct command_line *l;
916
917       if (info->control != NULL)
918         l = copy_command_lines (info->control->body_list[0]);
919       else
920         {
921           struct cleanup *old_chain;
922           char *str;
923
924           str = xstrprintf (_("Type commands for breakpoint(s) "
925                               "%s, one per line."),
926                             info->arg);
927
928           old_chain = make_cleanup (xfree, str);
929
930           l = read_command_lines (str,
931                                   info->from_tty, 1,
932                                   (is_tracepoint (b)
933                                    ? check_tracepoint_command : 0),
934                                   b);
935
936           do_cleanups (old_chain);
937         }
938
939       info->cmd = alloc_counted_command_line (l);
940     }
941
942   /* If a breakpoint was on the list more than once, we don't need to
943      do anything.  */
944   if (b->commands != info->cmd)
945     {
946       validate_commands_for_breakpoint (b, info->cmd->commands);
947       incref_counted_command_line (info->cmd);
948       decref_counted_command_line (&b->commands);
949       b->commands = info->cmd;
950       breakpoints_changed ();
951       observer_notify_breakpoint_modified (b);
952     }
953 }
954
955 static void
956 commands_command_1 (char *arg, int from_tty, 
957                     struct command_line *control)
958 {
959   struct cleanup *cleanups;
960   struct commands_info info;
961
962   info.from_tty = from_tty;
963   info.control = control;
964   info.cmd = NULL;
965   /* If we read command lines from the user, then `info' will hold an
966      extra reference to the commands that we must clean up.  */
967   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
968
969   if (arg == NULL || !*arg)
970     {
971       if (breakpoint_count - prev_breakpoint_count > 1)
972         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, 
973                           breakpoint_count);
974       else if (breakpoint_count > 0)
975         arg = xstrprintf ("%d", breakpoint_count);
976       else
977         {
978           /* So that we don't try to free the incoming non-NULL
979              argument in the cleanup below.  Mapping breakpoint
980              numbers will fail in this case.  */
981           arg = NULL;
982         }
983     }
984   else
985     /* The command loop has some static state, so we need to preserve
986        our argument.  */
987     arg = xstrdup (arg);
988
989   if (arg != NULL)
990     make_cleanup (xfree, arg);
991
992   info.arg = arg;
993
994   map_breakpoint_numbers (arg, do_map_commands_command, &info);
995
996   if (info.cmd == NULL)
997     error (_("No breakpoints specified."));
998
999   do_cleanups (cleanups);
1000 }
1001
1002 static void
1003 commands_command (char *arg, int from_tty)
1004 {
1005   commands_command_1 (arg, from_tty, NULL);
1006 }
1007
1008 /* Like commands_command, but instead of reading the commands from
1009    input stream, takes them from an already parsed command structure.
1010
1011    This is used by cli-script.c to DTRT with breakpoint commands
1012    that are part of if and while bodies.  */
1013 enum command_control_type
1014 commands_from_control_command (char *arg, struct command_line *cmd)
1015 {
1016   commands_command_1 (arg, 0, cmd);
1017   return simple_control;
1018 }
1019
1020 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
1021
1022 static int
1023 bp_location_has_shadow (struct bp_location *bl)
1024 {
1025   if (bl->loc_type != bp_loc_software_breakpoint)
1026     return 0;
1027   if (!bl->inserted)
1028     return 0;
1029   if (bl->target_info.shadow_len == 0)
1030     /* BL isn't valid, or doesn't shadow memory.  */
1031     return 0;
1032   return 1;
1033 }
1034
1035 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1036    by replacing any memory breakpoints with their shadowed contents.
1037
1038    The range of shadowed area by each bp_location is:
1039      bl->address - bp_location_placed_address_before_address_max
1040      up to bl->address + bp_location_shadow_len_after_address_max
1041    The range we were requested to resolve shadows for is:
1042      memaddr ... memaddr + len
1043    Thus the safe cutoff boundaries for performance optimization are
1044      memaddr + len <= (bl->address
1045                        - bp_location_placed_address_before_address_max)
1046    and:
1047      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
1048
1049 void
1050 breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
1051 {
1052   /* Left boundary, right boundary and median element of our binary
1053      search.  */
1054   unsigned bc_l, bc_r, bc;
1055
1056   /* Find BC_L which is a leftmost element which may affect BUF
1057      content.  It is safe to report lower value but a failure to
1058      report higher one.  */
1059
1060   bc_l = 0;
1061   bc_r = bp_location_count;
1062   while (bc_l + 1 < bc_r)
1063     {
1064       struct bp_location *bl;
1065
1066       bc = (bc_l + bc_r) / 2;
1067       bl = bp_location[bc];
1068
1069       /* Check first BL->ADDRESS will not overflow due to the added
1070          constant.  Then advance the left boundary only if we are sure
1071          the BC element can in no way affect the BUF content (MEMADDR
1072          to MEMADDR + LEN range).
1073
1074          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1075          offset so that we cannot miss a breakpoint with its shadow
1076          range tail still reaching MEMADDR.  */
1077
1078       if ((bl->address + bp_location_shadow_len_after_address_max
1079            >= bl->address)
1080           && (bl->address + bp_location_shadow_len_after_address_max
1081               <= memaddr))
1082         bc_l = bc;
1083       else
1084         bc_r = bc;
1085     }
1086
1087   /* Due to the binary search above, we need to make sure we pick the
1088      first location that's at BC_L's address.  E.g., if there are
1089      multiple locations at the same address, BC_L may end up pointing
1090      at a duplicate location, and miss the "master"/"inserted"
1091      location.  Say, given locations L1, L2 and L3 at addresses A and
1092      B:
1093
1094       L1@A, L2@A, L3@B, ...
1095
1096      BC_L could end up pointing at location L2, while the "master"
1097      location could be L1.  Since the `loc->inserted' flag is only set
1098      on "master" locations, we'd forget to restore the shadow of L1
1099      and L2.  */
1100   while (bc_l > 0
1101          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1102     bc_l--;
1103
1104   /* Now do full processing of the found relevant range of elements.  */
1105
1106   for (bc = bc_l; bc < bp_location_count; bc++)
1107   {
1108     struct bp_location *bl = bp_location[bc];
1109     CORE_ADDR bp_addr = 0;
1110     int bp_size = 0;
1111     int bptoffset = 0;
1112
1113     /* bp_location array has BL->OWNER always non-NULL.  */
1114     if (bl->owner->type == bp_none)
1115       warning (_("reading through apparently deleted breakpoint #%d?"),
1116                bl->owner->number);
1117
1118     /* Performance optimization: any further element can no longer affect BUF
1119        content.  */
1120
1121     if (bl->address >= bp_location_placed_address_before_address_max
1122         && memaddr + len <= (bl->address
1123                              - bp_location_placed_address_before_address_max))
1124       break;
1125
1126     if (!bp_location_has_shadow (bl))
1127       continue;
1128     if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1129                                    current_program_space->aspace, 0))
1130       continue;
1131
1132     /* Addresses and length of the part of the breakpoint that
1133        we need to copy.  */
1134     bp_addr = bl->target_info.placed_address;
1135     bp_size = bl->target_info.shadow_len;
1136
1137     if (bp_addr + bp_size <= memaddr)
1138       /* The breakpoint is entirely before the chunk of memory we
1139          are reading.  */
1140       continue;
1141
1142     if (bp_addr >= memaddr + len)
1143       /* The breakpoint is entirely after the chunk of memory we are
1144          reading.  */
1145       continue;
1146
1147     /* Offset within shadow_contents.  */
1148     if (bp_addr < memaddr)
1149       {
1150         /* Only copy the second part of the breakpoint.  */
1151         bp_size -= memaddr - bp_addr;
1152         bptoffset = memaddr - bp_addr;
1153         bp_addr = memaddr;
1154       }
1155
1156     if (bp_addr + bp_size > memaddr + len)
1157       {
1158         /* Only copy the first part of the breakpoint.  */
1159         bp_size -= (bp_addr + bp_size) - (memaddr + len);
1160       }
1161
1162     memcpy (buf + bp_addr - memaddr,
1163             bl->target_info.shadow_contents + bptoffset, bp_size);
1164   }
1165 }
1166 \f
1167
1168 /* Return true if BPT is of any hardware watchpoint kind.  */
1169
1170 static int
1171 is_hardware_watchpoint (const struct breakpoint *bpt)
1172 {
1173   return (bpt->type == bp_hardware_watchpoint
1174           || bpt->type == bp_read_watchpoint
1175           || bpt->type == bp_access_watchpoint);
1176 }
1177
1178 /* Return true if BPT is of any watchpoint kind, hardware or
1179    software.  */
1180
1181 int
1182 is_watchpoint (const struct breakpoint *bpt)
1183 {
1184   return (is_hardware_watchpoint (bpt)
1185           || bpt->type == bp_watchpoint);
1186 }
1187
1188 /* Returns true if the current thread and its running state are safe
1189    to evaluate or update watchpoint B.  Watchpoints on local
1190    expressions need to be evaluated in the context of the thread that
1191    was current when the watchpoint was created, and, that thread needs
1192    to be stopped to be able to select the correct frame context.
1193    Watchpoints on global expressions can be evaluated on any thread,
1194    and in any state.  It is presently left to the target allowing
1195    memory accesses when threads are running.  */
1196
1197 static int
1198 watchpoint_in_thread_scope (struct watchpoint *b)
1199 {
1200   return (ptid_equal (b->watchpoint_thread, null_ptid)
1201           || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1202               && !is_executing (inferior_ptid)));
1203 }
1204
1205 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1206    associated bp_watchpoint_scope breakpoint.  */
1207
1208 static void
1209 watchpoint_del_at_next_stop (struct watchpoint *w)
1210 {
1211   struct breakpoint *b = &w->base;
1212
1213   if (b->related_breakpoint != b)
1214     {
1215       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1216       gdb_assert (b->related_breakpoint->related_breakpoint == b);
1217       b->related_breakpoint->disposition = disp_del_at_next_stop;
1218       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1219       b->related_breakpoint = b;
1220     }
1221   b->disposition = disp_del_at_next_stop;
1222 }
1223
1224 /* Assuming that B is a watchpoint:
1225    - Reparse watchpoint expression, if REPARSE is non-zero
1226    - Evaluate expression and store the result in B->val
1227    - Evaluate the condition if there is one, and store the result
1228      in b->loc->cond.
1229    - Update the list of values that must be watched in B->loc.
1230
1231    If the watchpoint disposition is disp_del_at_next_stop, then do
1232    nothing.  If this is local watchpoint that is out of scope, delete
1233    it.
1234
1235    Even with `set breakpoint always-inserted on' the watchpoints are
1236    removed + inserted on each stop here.  Normal breakpoints must
1237    never be removed because they might be missed by a running thread
1238    when debugging in non-stop mode.  On the other hand, hardware
1239    watchpoints (is_hardware_watchpoint; processed here) are specific
1240    to each LWP since they are stored in each LWP's hardware debug
1241    registers.  Therefore, such LWP must be stopped first in order to
1242    be able to modify its hardware watchpoints.
1243
1244    Hardware watchpoints must be reset exactly once after being
1245    presented to the user.  It cannot be done sooner, because it would
1246    reset the data used to present the watchpoint hit to the user.  And
1247    it must not be done later because it could display the same single
1248    watchpoint hit during multiple GDB stops.  Note that the latter is
1249    relevant only to the hardware watchpoint types bp_read_watchpoint
1250    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1251    not user-visible - its hit is suppressed if the memory content has
1252    not changed.
1253
1254    The following constraints influence the location where we can reset
1255    hardware watchpoints:
1256
1257    * target_stopped_by_watchpoint and target_stopped_data_address are
1258      called several times when GDB stops.
1259
1260    [linux] 
1261    * Multiple hardware watchpoints can be hit at the same time,
1262      causing GDB to stop.  GDB only presents one hardware watchpoint
1263      hit at a time as the reason for stopping, and all the other hits
1264      are presented later, one after the other, each time the user
1265      requests the execution to be resumed.  Execution is not resumed
1266      for the threads still having pending hit event stored in
1267      LWP_INFO->STATUS.  While the watchpoint is already removed from
1268      the inferior on the first stop the thread hit event is kept being
1269      reported from its cached value by linux_nat_stopped_data_address
1270      until the real thread resume happens after the watchpoint gets
1271      presented and thus its LWP_INFO->STATUS gets reset.
1272
1273    Therefore the hardware watchpoint hit can get safely reset on the
1274    watchpoint removal from inferior.  */
1275
1276 static void
1277 update_watchpoint (struct watchpoint *b, int reparse)
1278 {
1279   int within_current_scope;
1280   struct frame_id saved_frame_id;
1281   int frame_saved;
1282
1283   /* If this is a local watchpoint, we only want to check if the
1284      watchpoint frame is in scope if the current thread is the thread
1285      that was used to create the watchpoint.  */
1286   if (!watchpoint_in_thread_scope (b))
1287     return;
1288
1289   if (b->base.disposition == disp_del_at_next_stop)
1290     return;
1291  
1292   frame_saved = 0;
1293
1294   /* Determine if the watchpoint is within scope.  */
1295   if (b->exp_valid_block == NULL)
1296     within_current_scope = 1;
1297   else
1298     {
1299       struct frame_info *fi = get_current_frame ();
1300       struct gdbarch *frame_arch = get_frame_arch (fi);
1301       CORE_ADDR frame_pc = get_frame_pc (fi);
1302
1303       /* If we're in a function epilogue, unwinding may not work
1304          properly, so do not attempt to recreate locations at this
1305          point.  See similar comments in watchpoint_check.  */
1306       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1307         return;
1308
1309       /* Save the current frame's ID so we can restore it after
1310          evaluating the watchpoint expression on its own frame.  */
1311       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1312          took a frame parameter, so that we didn't have to change the
1313          selected frame.  */
1314       frame_saved = 1;
1315       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1316
1317       fi = frame_find_by_id (b->watchpoint_frame);
1318       within_current_scope = (fi != NULL);
1319       if (within_current_scope)
1320         select_frame (fi);
1321     }
1322
1323   /* We don't free locations.  They are stored in the bp_location array
1324      and update_global_location_list will eventually delete them and
1325      remove breakpoints if needed.  */
1326   b->base.loc = NULL;
1327
1328   if (within_current_scope && reparse)
1329     {
1330       char *s;
1331
1332       if (b->exp)
1333         {
1334           xfree (b->exp);
1335           b->exp = NULL;
1336         }
1337       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1338       b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1339       /* If the meaning of expression itself changed, the old value is
1340          no longer relevant.  We don't want to report a watchpoint hit
1341          to the user when the old value and the new value may actually
1342          be completely different objects.  */
1343       value_free (b->val);
1344       b->val = NULL;
1345       b->val_valid = 0;
1346
1347       /* Note that unlike with breakpoints, the watchpoint's condition
1348          expression is stored in the breakpoint object, not in the
1349          locations (re)created below.  */
1350       if (b->base.cond_string != NULL)
1351         {
1352           if (b->cond_exp != NULL)
1353             {
1354               xfree (b->cond_exp);
1355               b->cond_exp = NULL;
1356             }
1357
1358           s = b->base.cond_string;
1359           b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1360         }
1361     }
1362
1363   /* If we failed to parse the expression, for example because
1364      it refers to a global variable in a not-yet-loaded shared library,
1365      don't try to insert watchpoint.  We don't automatically delete
1366      such watchpoint, though, since failure to parse expression
1367      is different from out-of-scope watchpoint.  */
1368   if ( !target_has_execution)
1369     {
1370       /* Without execution, memory can't change.  No use to try and
1371          set watchpoint locations.  The watchpoint will be reset when
1372          the target gains execution, through breakpoint_re_set.  */
1373     }
1374   else if (within_current_scope && b->exp)
1375     {
1376       int pc = 0;
1377       struct value *val_chain, *v, *result, *next;
1378       struct program_space *frame_pspace;
1379
1380       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1381
1382       /* Avoid setting b->val if it's already set.  The meaning of
1383          b->val is 'the last value' user saw, and we should update
1384          it only if we reported that last value to user.  As it
1385          happens, the code that reports it updates b->val directly.
1386          We don't keep track of the memory value for masked
1387          watchpoints.  */
1388       if (!b->val_valid && !is_masked_watchpoint (&b->base))
1389         {
1390           b->val = v;
1391           b->val_valid = 1;
1392         }
1393
1394       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1395
1396       /* Look at each value on the value chain.  */
1397       for (v = val_chain; v; v = value_next (v))
1398         {
1399           /* If it's a memory location, and GDB actually needed
1400              its contents to evaluate the expression, then we
1401              must watch it.  If the first value returned is
1402              still lazy, that means an error occurred reading it;
1403              watch it anyway in case it becomes readable.  */
1404           if (VALUE_LVAL (v) == lval_memory
1405               && (v == val_chain || ! value_lazy (v)))
1406             {
1407               struct type *vtype = check_typedef (value_type (v));
1408
1409               /* We only watch structs and arrays if user asked
1410                  for it explicitly, never if they just happen to
1411                  appear in the middle of some value chain.  */
1412               if (v == result
1413                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1414                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1415                 {
1416                   CORE_ADDR addr;
1417                   int len, type;
1418                   struct bp_location *loc, **tmp;
1419
1420                   addr = value_address (v);
1421                   len = TYPE_LENGTH (value_type (v));
1422                   type = hw_write;
1423                   if (b->base.type == bp_read_watchpoint)
1424                     type = hw_read;
1425                   else if (b->base.type == bp_access_watchpoint)
1426                     type = hw_access;
1427
1428                   loc = allocate_bp_location (&b->base);
1429                   for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1430                     ;
1431                   *tmp = loc;
1432                   loc->gdbarch = get_type_arch (value_type (v));
1433
1434                   loc->pspace = frame_pspace;
1435                   loc->address = addr;
1436                   loc->length = len;
1437                   loc->watchpoint_type = type;
1438                 }
1439             }
1440         }
1441
1442       /* Change the type of breakpoint between hardware assisted or
1443          an ordinary watchpoint depending on the hardware support
1444          and free hardware slots.  REPARSE is set when the inferior
1445          is started.  */
1446       if (reparse)
1447         {
1448           int reg_cnt;
1449           enum bp_loc_type loc_type;
1450           struct bp_location *bl;
1451
1452           reg_cnt = can_use_hardware_watchpoint (val_chain);
1453
1454           if (reg_cnt)
1455             {
1456               int i, target_resources_ok, other_type_used;
1457               enum bptype type;
1458
1459               /* Use an exact watchpoint when there's only one memory region to be
1460                  watched, and only one debug register is needed to watch it.  */
1461               b->exact = target_exact_watchpoints && reg_cnt == 1;
1462
1463               /* We need to determine how many resources are already
1464                  used for all other hardware watchpoints plus this one
1465                  to see if we still have enough resources to also fit
1466                  this watchpoint in as well.  */
1467
1468               /* If this is a software watchpoint, we try to turn it
1469                  to a hardware one -- count resources as if B was of
1470                  hardware watchpoint type.  */
1471               type = b->base.type;
1472               if (type == bp_watchpoint)
1473                 type = bp_hardware_watchpoint;
1474
1475               /* This watchpoint may or may not have been placed on
1476                  the list yet at this point (it won't be in the list
1477                  if we're trying to create it for the first time,
1478                  through watch_command), so always account for it
1479                  manually.  */
1480
1481               /* Count resources used by all watchpoints except B.  */
1482               i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1483
1484               /* Add in the resources needed for B.  */
1485               i += hw_watchpoint_use_count (&b->base);
1486
1487               target_resources_ok
1488                 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1489               if (target_resources_ok <= 0)
1490                 {
1491                   int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1492
1493                   if (target_resources_ok == 0 && !sw_mode)
1494                     error (_("Target does not support this type of "
1495                              "hardware watchpoint."));
1496                   else if (target_resources_ok < 0 && !sw_mode)
1497                     error (_("There are not enough available hardware "
1498                              "resources for this watchpoint."));
1499
1500                   /* Downgrade to software watchpoint.  */
1501                   b->base.type = bp_watchpoint;
1502                 }
1503               else
1504                 {
1505                   /* If this was a software watchpoint, we've just
1506                      found we have enough resources to turn it to a
1507                      hardware watchpoint.  Otherwise, this is a
1508                      nop.  */
1509                   b->base.type = type;
1510                 }
1511             }
1512           else if (!b->base.ops->works_in_software_mode (&b->base))
1513             error (_("Expression cannot be implemented with "
1514                      "read/access watchpoint."));
1515           else
1516             b->base.type = bp_watchpoint;
1517
1518           loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1519                       : bp_loc_hardware_watchpoint);
1520           for (bl = b->base.loc; bl; bl = bl->next)
1521             bl->loc_type = loc_type;
1522         }
1523
1524       for (v = val_chain; v; v = next)
1525         {
1526           next = value_next (v);
1527           if (v != b->val)
1528             value_free (v);
1529         }
1530
1531       /* If a software watchpoint is not watching any memory, then the
1532          above left it without any location set up.  But,
1533          bpstat_stop_status requires a location to be able to report
1534          stops, so make sure there's at least a dummy one.  */
1535       if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1536         {
1537           struct breakpoint *base = &b->base;
1538           base->loc = allocate_bp_location (base);
1539           base->loc->pspace = frame_pspace;
1540           base->loc->address = -1;
1541           base->loc->length = -1;
1542           base->loc->watchpoint_type = -1;
1543         }
1544     }
1545   else if (!within_current_scope)
1546     {
1547       printf_filtered (_("\
1548 Watchpoint %d deleted because the program has left the block\n\
1549 in which its expression is valid.\n"),
1550                        b->base.number);
1551       watchpoint_del_at_next_stop (b);
1552     }
1553
1554   /* Restore the selected frame.  */
1555   if (frame_saved)
1556     select_frame (frame_find_by_id (saved_frame_id));
1557 }
1558
1559
1560 /* Returns 1 iff breakpoint location should be
1561    inserted in the inferior.  */
1562 static int
1563 should_be_inserted (struct bp_location *bl)
1564 {
1565   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1566     return 0;
1567
1568   if (bl->owner->disposition == disp_del_at_next_stop)
1569     return 0;
1570
1571   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1572     return 0;
1573
1574   /* This is set for example, when we're attached to the parent of a
1575      vfork, and have detached from the child.  The child is running
1576      free, and we expect it to do an exec or exit, at which point the
1577      OS makes the parent schedulable again (and the target reports
1578      that the vfork is done).  Until the child is done with the shared
1579      memory region, do not insert breakpoints in the parent, otherwise
1580      the child could still trip on the parent's breakpoints.  Since
1581      the parent is blocked anyway, it won't miss any breakpoint.  */
1582   if (bl->pspace->breakpoints_not_allowed)
1583     return 0;
1584
1585   /* Tracepoints are inserted by the target at a time of its choosing,
1586      not by us.  */
1587   if (is_tracepoint (bl->owner))
1588     return 0;
1589
1590   return 1;
1591 }
1592
1593 /* Same as should_be_inserted but does the check assuming
1594    that the location is not duplicated.  */
1595
1596 static int
1597 unduplicated_should_be_inserted (struct bp_location *bl)
1598 {
1599   int result;
1600   const int save_duplicate = bl->duplicate;
1601
1602   bl->duplicate = 0;
1603   result = should_be_inserted (bl);
1604   bl->duplicate = save_duplicate;
1605   return result;
1606 }
1607
1608 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
1609    location.  Any error messages are printed to TMP_ERROR_STREAM; and
1610    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
1611    Returns 0 for success, 1 if the bp_location type is not supported or
1612    -1 for failure.
1613
1614    NOTE drow/2003-09-09: This routine could be broken down to an
1615    object-style method for each breakpoint or catchpoint type.  */
1616 static int
1617 insert_bp_location (struct bp_location *bl,
1618                     struct ui_file *tmp_error_stream,
1619                     int *disabled_breaks,
1620                     int *hw_breakpoint_error)
1621 {
1622   int val = 0;
1623
1624   if (!should_be_inserted (bl) || bl->inserted)
1625     return 0;
1626
1627   /* Initialize the target-specific information.  */
1628   memset (&bl->target_info, 0, sizeof (bl->target_info));
1629   bl->target_info.placed_address = bl->address;
1630   bl->target_info.placed_address_space = bl->pspace->aspace;
1631   bl->target_info.length = bl->length;
1632
1633   if (bl->loc_type == bp_loc_software_breakpoint
1634       || bl->loc_type == bp_loc_hardware_breakpoint)
1635     {
1636       if (bl->owner->type != bp_hardware_breakpoint)
1637         {
1638           /* If the explicitly specified breakpoint type
1639              is not hardware breakpoint, check the memory map to see
1640              if the breakpoint address is in read only memory or not.
1641
1642              Two important cases are:
1643              - location type is not hardware breakpoint, memory
1644              is readonly.  We change the type of the location to
1645              hardware breakpoint.
1646              - location type is hardware breakpoint, memory is
1647              read-write.  This means we've previously made the
1648              location hardware one, but then the memory map changed,
1649              so we undo.
1650              
1651              When breakpoints are removed, remove_breakpoints will use
1652              location types we've just set here, the only possible
1653              problem is that memory map has changed during running
1654              program, but it's not going to work anyway with current
1655              gdb.  */
1656           struct mem_region *mr 
1657             = lookup_mem_region (bl->target_info.placed_address);
1658           
1659           if (mr)
1660             {
1661               if (automatic_hardware_breakpoints)
1662                 {
1663                   enum bp_loc_type new_type;
1664                   
1665                   if (mr->attrib.mode != MEM_RW)
1666                     new_type = bp_loc_hardware_breakpoint;
1667                   else 
1668                     new_type = bp_loc_software_breakpoint;
1669                   
1670                   if (new_type != bl->loc_type)
1671                     {
1672                       static int said = 0;
1673
1674                       bl->loc_type = new_type;
1675                       if (!said)
1676                         {
1677                           fprintf_filtered (gdb_stdout,
1678                                             _("Note: automatically using "
1679                                               "hardware breakpoints for "
1680                                               "read-only addresses.\n"));
1681                           said = 1;
1682                         }
1683                     }
1684                 }
1685               else if (bl->loc_type == bp_loc_software_breakpoint
1686                        && mr->attrib.mode != MEM_RW)        
1687                 warning (_("cannot set software breakpoint "
1688                            "at readonly address %s"),
1689                          paddress (bl->gdbarch, bl->address));
1690             }
1691         }
1692         
1693       /* First check to see if we have to handle an overlay.  */
1694       if (overlay_debugging == ovly_off
1695           || bl->section == NULL
1696           || !(section_is_overlay (bl->section)))
1697         {
1698           /* No overlay handling: just set the breakpoint.  */
1699
1700           val = bl->owner->ops->insert_location (bl);
1701         }
1702       else
1703         {
1704           /* This breakpoint is in an overlay section.
1705              Shall we set a breakpoint at the LMA?  */
1706           if (!overlay_events_enabled)
1707             {
1708               /* Yes -- overlay event support is not active, 
1709                  so we must try to set a breakpoint at the LMA.
1710                  This will not work for a hardware breakpoint.  */
1711               if (bl->loc_type == bp_loc_hardware_breakpoint)
1712                 warning (_("hardware breakpoint %d not supported in overlay!"),
1713                          bl->owner->number);
1714               else
1715                 {
1716                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
1717                                                              bl->section);
1718                   /* Set a software (trap) breakpoint at the LMA.  */
1719                   bl->overlay_target_info = bl->target_info;
1720                   bl->overlay_target_info.placed_address = addr;
1721                   val = target_insert_breakpoint (bl->gdbarch,
1722                                                   &bl->overlay_target_info);
1723                   if (val != 0)
1724                     fprintf_unfiltered (tmp_error_stream,
1725                                         "Overlay breakpoint %d "
1726                                         "failed: in ROM?\n",
1727                                         bl->owner->number);
1728                 }
1729             }
1730           /* Shall we set a breakpoint at the VMA? */
1731           if (section_is_mapped (bl->section))
1732             {
1733               /* Yes.  This overlay section is mapped into memory.  */
1734               val = bl->owner->ops->insert_location (bl);
1735             }
1736           else
1737             {
1738               /* No.  This breakpoint will not be inserted.  
1739                  No error, but do not mark the bp as 'inserted'.  */
1740               return 0;
1741             }
1742         }
1743
1744       if (val)
1745         {
1746           /* Can't set the breakpoint.  */
1747           if (solib_name_from_address (bl->pspace, bl->address))
1748             {
1749               /* See also: disable_breakpoints_in_shlibs.  */
1750               val = 0;
1751               bl->shlib_disabled = 1;
1752               observer_notify_breakpoint_modified (bl->owner);
1753               if (!*disabled_breaks)
1754                 {
1755                   fprintf_unfiltered (tmp_error_stream, 
1756                                       "Cannot insert breakpoint %d.\n", 
1757                                       bl->owner->number);
1758                   fprintf_unfiltered (tmp_error_stream, 
1759                                       "Temporarily disabling shared "
1760                                       "library breakpoints:\n");
1761                 }
1762               *disabled_breaks = 1;
1763               fprintf_unfiltered (tmp_error_stream,
1764                                   "breakpoint #%d\n", bl->owner->number);
1765             }
1766           else
1767             {
1768               if (bl->loc_type == bp_loc_hardware_breakpoint)
1769                 {
1770                   *hw_breakpoint_error = 1;
1771                   fprintf_unfiltered (tmp_error_stream,
1772                                       "Cannot insert hardware "
1773                                       "breakpoint %d.\n",
1774                                       bl->owner->number);
1775                 }
1776               else
1777                 {
1778                   fprintf_unfiltered (tmp_error_stream, 
1779                                       "Cannot insert breakpoint %d.\n", 
1780                                       bl->owner->number);
1781                   fprintf_filtered (tmp_error_stream, 
1782                                     "Error accessing memory address ");
1783                   fputs_filtered (paddress (bl->gdbarch, bl->address),
1784                                   tmp_error_stream);
1785                   fprintf_filtered (tmp_error_stream, ": %s.\n",
1786                                     safe_strerror (val));
1787                 }
1788
1789             }
1790         }
1791       else
1792         bl->inserted = 1;
1793
1794       return val;
1795     }
1796
1797   else if (bl->loc_type == bp_loc_hardware_watchpoint
1798            /* NOTE drow/2003-09-08: This state only exists for removing
1799               watchpoints.  It's not clear that it's necessary...  */
1800            && bl->owner->disposition != disp_del_at_next_stop)
1801     {
1802       gdb_assert (bl->owner->ops != NULL
1803                   && bl->owner->ops->insert_location != NULL);
1804
1805       val = bl->owner->ops->insert_location (bl);
1806
1807       /* If trying to set a read-watchpoint, and it turns out it's not
1808          supported, try emulating one with an access watchpoint.  */
1809       if (val == 1 && bl->watchpoint_type == hw_read)
1810         {
1811           struct bp_location *loc, **loc_temp;
1812
1813           /* But don't try to insert it, if there's already another
1814              hw_access location that would be considered a duplicate
1815              of this one.  */
1816           ALL_BP_LOCATIONS (loc, loc_temp)
1817             if (loc != bl
1818                 && loc->watchpoint_type == hw_access
1819                 && watchpoint_locations_match (bl, loc))
1820               {
1821                 bl->duplicate = 1;
1822                 bl->inserted = 1;
1823                 bl->target_info = loc->target_info;
1824                 bl->watchpoint_type = hw_access;
1825                 val = 0;
1826                 break;
1827               }
1828
1829           if (val == 1)
1830             {
1831               bl->watchpoint_type = hw_access;
1832               val = bl->owner->ops->insert_location (bl);
1833
1834               if (val)
1835                 /* Back to the original value.  */
1836                 bl->watchpoint_type = hw_read;
1837             }
1838         }
1839
1840       bl->inserted = (val == 0);
1841     }
1842
1843   else if (bl->owner->type == bp_catchpoint)
1844     {
1845       gdb_assert (bl->owner->ops != NULL
1846                   && bl->owner->ops->insert_location != NULL);
1847
1848       val = bl->owner->ops->insert_location (bl);
1849       if (val)
1850         {
1851           bl->owner->enable_state = bp_disabled;
1852
1853           if (val == 1)
1854             warning (_("\
1855 Error inserting catchpoint %d: Your system does not support this type\n\
1856 of catchpoint."), bl->owner->number);
1857           else
1858             warning (_("Error inserting catchpoint %d."), bl->owner->number);
1859         }
1860
1861       bl->inserted = (val == 0);
1862
1863       /* We've already printed an error message if there was a problem
1864          inserting this catchpoint, and we've disabled the catchpoint,
1865          so just return success.  */
1866       return 0;
1867     }
1868
1869   return 0;
1870 }
1871
1872 /* This function is called when program space PSPACE is about to be
1873    deleted.  It takes care of updating breakpoints to not reference
1874    PSPACE anymore.  */
1875
1876 void
1877 breakpoint_program_space_exit (struct program_space *pspace)
1878 {
1879   struct breakpoint *b, *b_temp;
1880   struct bp_location *loc, **loc_temp;
1881
1882   /* Remove any breakpoint that was set through this program space.  */
1883   ALL_BREAKPOINTS_SAFE (b, b_temp)
1884     {
1885       if (b->pspace == pspace)
1886         delete_breakpoint (b);
1887     }
1888
1889   /* Breakpoints set through other program spaces could have locations
1890      bound to PSPACE as well.  Remove those.  */
1891   ALL_BP_LOCATIONS (loc, loc_temp)
1892     {
1893       struct bp_location *tmp;
1894
1895       if (loc->pspace == pspace)
1896         {
1897           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
1898           if (loc->owner->loc == loc)
1899             loc->owner->loc = loc->next;
1900           else
1901             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1902               if (tmp->next == loc)
1903                 {
1904                   tmp->next = loc->next;
1905                   break;
1906                 }
1907         }
1908     }
1909
1910   /* Now update the global location list to permanently delete the
1911      removed locations above.  */
1912   update_global_location_list (0);
1913 }
1914
1915 /* Make sure all breakpoints are inserted in inferior.
1916    Throws exception on any error.
1917    A breakpoint that is already inserted won't be inserted
1918    again, so calling this function twice is safe.  */
1919 void
1920 insert_breakpoints (void)
1921 {
1922   struct breakpoint *bpt;
1923
1924   ALL_BREAKPOINTS (bpt)
1925     if (is_hardware_watchpoint (bpt))
1926       {
1927         struct watchpoint *w = (struct watchpoint *) bpt;
1928
1929         update_watchpoint (w, 0 /* don't reparse.  */);
1930       }
1931
1932   update_global_location_list (1);
1933
1934   /* update_global_location_list does not insert breakpoints when
1935      always_inserted_mode is not enabled.  Explicitly insert them
1936      now.  */
1937   if (!breakpoints_always_inserted_mode ())
1938     insert_breakpoint_locations ();
1939 }
1940
1941 /* Used when starting or continuing the program.  */
1942
1943 static void
1944 insert_breakpoint_locations (void)
1945 {
1946   struct breakpoint *bpt;
1947   struct bp_location *bl, **blp_tmp;
1948   int error = 0;
1949   int val = 0;
1950   int disabled_breaks = 0;
1951   int hw_breakpoint_error = 0;
1952
1953   struct ui_file *tmp_error_stream = mem_fileopen ();
1954   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
1955   
1956   /* Explicitly mark the warning -- this will only be printed if
1957      there was an error.  */
1958   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1959
1960   save_current_space_and_thread ();
1961
1962   ALL_BP_LOCATIONS (bl, blp_tmp)
1963     {
1964       if (!should_be_inserted (bl) || bl->inserted)
1965         continue;
1966
1967       /* There is no point inserting thread-specific breakpoints if
1968          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
1969          has BL->OWNER always non-NULL.  */
1970       if (bl->owner->thread != -1
1971           && !valid_thread_id (bl->owner->thread))
1972         continue;
1973
1974       switch_to_program_space_and_thread (bl->pspace);
1975
1976       /* For targets that support global breakpoints, there's no need
1977          to select an inferior to insert breakpoint to.  In fact, even
1978          if we aren't attached to any process yet, we should still
1979          insert breakpoints.  */
1980       if (!gdbarch_has_global_breakpoints (target_gdbarch)
1981           && ptid_equal (inferior_ptid, null_ptid))
1982         continue;
1983
1984       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1985                                     &hw_breakpoint_error);
1986       if (val)
1987         error = val;
1988     }
1989
1990   /* If we failed to insert all locations of a watchpoint, remove
1991      them, as half-inserted watchpoint is of limited use.  */
1992   ALL_BREAKPOINTS (bpt)  
1993     {
1994       int some_failed = 0;
1995       struct bp_location *loc;
1996
1997       if (!is_hardware_watchpoint (bpt))
1998         continue;
1999
2000       if (!breakpoint_enabled (bpt))
2001         continue;
2002
2003       if (bpt->disposition == disp_del_at_next_stop)
2004         continue;
2005       
2006       for (loc = bpt->loc; loc; loc = loc->next)
2007         if (!loc->inserted && should_be_inserted (loc))
2008           {
2009             some_failed = 1;
2010             break;
2011           }
2012       if (some_failed)
2013         {
2014           for (loc = bpt->loc; loc; loc = loc->next)
2015             if (loc->inserted)
2016               remove_breakpoint (loc, mark_uninserted);
2017
2018           hw_breakpoint_error = 1;
2019           fprintf_unfiltered (tmp_error_stream,
2020                               "Could not insert hardware watchpoint %d.\n", 
2021                               bpt->number);
2022           error = -1;
2023         }
2024     }
2025
2026   if (error)
2027     {
2028       /* If a hardware breakpoint or watchpoint was inserted, add a
2029          message about possibly exhausted resources.  */
2030       if (hw_breakpoint_error)
2031         {
2032           fprintf_unfiltered (tmp_error_stream, 
2033                               "Could not insert hardware breakpoints:\n\
2034 You may have requested too many hardware breakpoints/watchpoints.\n");
2035         }
2036       target_terminal_ours_for_output ();
2037       error_stream (tmp_error_stream);
2038     }
2039
2040   do_cleanups (cleanups);
2041 }
2042
2043 /* Used when the program stops.
2044    Returns zero if successful, or non-zero if there was a problem
2045    removing a breakpoint location.  */
2046
2047 int
2048 remove_breakpoints (void)
2049 {
2050   struct bp_location *bl, **blp_tmp;
2051   int val = 0;
2052
2053   ALL_BP_LOCATIONS (bl, blp_tmp)
2054   {
2055     if (bl->inserted)
2056       val |= remove_breakpoint (bl, mark_uninserted);
2057   }
2058   return val;
2059 }
2060
2061 /* Remove breakpoints of process PID.  */
2062
2063 int
2064 remove_breakpoints_pid (int pid)
2065 {
2066   struct bp_location *bl, **blp_tmp;
2067   int val;
2068   struct inferior *inf = find_inferior_pid (pid);
2069
2070   ALL_BP_LOCATIONS (bl, blp_tmp)
2071   {
2072     if (bl->pspace != inf->pspace)
2073       continue;
2074
2075     if (bl->inserted)
2076       {
2077         val = remove_breakpoint (bl, mark_uninserted);
2078         if (val != 0)
2079           return val;
2080       }
2081   }
2082   return 0;
2083 }
2084
2085 int
2086 reattach_breakpoints (int pid)
2087 {
2088   struct cleanup *old_chain;
2089   struct bp_location *bl, **blp_tmp;
2090   int val;
2091   struct ui_file *tmp_error_stream;
2092   int dummy1 = 0, dummy2 = 0;
2093   struct inferior *inf;
2094   struct thread_info *tp;
2095
2096   tp = any_live_thread_of_process (pid);
2097   if (tp == NULL)
2098     return 1;
2099
2100   inf = find_inferior_pid (pid);
2101   old_chain = save_inferior_ptid ();
2102
2103   inferior_ptid = tp->ptid;
2104
2105   tmp_error_stream = mem_fileopen ();
2106   make_cleanup_ui_file_delete (tmp_error_stream);
2107
2108   ALL_BP_LOCATIONS (bl, blp_tmp)
2109   {
2110     if (bl->pspace != inf->pspace)
2111       continue;
2112
2113     if (bl->inserted)
2114       {
2115         bl->inserted = 0;
2116         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2117         if (val != 0)
2118           {
2119             do_cleanups (old_chain);
2120             return val;
2121           }
2122       }
2123   }
2124   do_cleanups (old_chain);
2125   return 0;
2126 }
2127
2128 static int internal_breakpoint_number = -1;
2129
2130 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2131    If INTERNAL is non-zero, the breakpoint number will be populated
2132    from internal_breakpoint_number and that variable decremented.
2133    Otherwise the breakpoint number will be populated from
2134    breakpoint_count and that value incremented.  Internal breakpoints
2135    do not set the internal var bpnum.  */
2136 static void
2137 set_breakpoint_number (int internal, struct breakpoint *b)
2138 {
2139   if (internal)
2140     b->number = internal_breakpoint_number--;
2141   else
2142     {
2143       set_breakpoint_count (breakpoint_count + 1);
2144       b->number = breakpoint_count;
2145     }
2146 }
2147
2148 static struct breakpoint *
2149 create_internal_breakpoint (struct gdbarch *gdbarch,
2150                             CORE_ADDR address, enum bptype type,
2151                             const struct breakpoint_ops *ops)
2152 {
2153   struct symtab_and_line sal;
2154   struct breakpoint *b;
2155
2156   init_sal (&sal);              /* Initialize to zeroes.  */
2157
2158   sal.pc = address;
2159   sal.section = find_pc_overlay (sal.pc);
2160   sal.pspace = current_program_space;
2161
2162   b = set_raw_breakpoint (gdbarch, sal, type, ops);
2163   b->number = internal_breakpoint_number--;
2164   b->disposition = disp_donttouch;
2165
2166   return b;
2167 }
2168
2169 static const char *const longjmp_names[] =
2170   {
2171     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2172   };
2173 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2174
2175 /* Per-objfile data private to breakpoint.c.  */
2176 struct breakpoint_objfile_data
2177 {
2178   /* Minimal symbol for "_ovly_debug_event" (if any).  */
2179   struct minimal_symbol *overlay_msym;
2180
2181   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
2182   struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2183
2184   /* Minimal symbol for "std::terminate()" (if any).  */
2185   struct minimal_symbol *terminate_msym;
2186
2187   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
2188   struct minimal_symbol *exception_msym;
2189 };
2190
2191 static const struct objfile_data *breakpoint_objfile_key;
2192
2193 /* Minimal symbol not found sentinel.  */
2194 static struct minimal_symbol msym_not_found;
2195
2196 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
2197
2198 static int
2199 msym_not_found_p (const struct minimal_symbol *msym)
2200 {
2201   return msym == &msym_not_found;
2202 }
2203
2204 /* Return per-objfile data needed by breakpoint.c.
2205    Allocate the data if necessary.  */
2206
2207 static struct breakpoint_objfile_data *
2208 get_breakpoint_objfile_data (struct objfile *objfile)
2209 {
2210   struct breakpoint_objfile_data *bp_objfile_data;
2211
2212   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2213   if (bp_objfile_data == NULL)
2214     {
2215       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2216                                        sizeof (*bp_objfile_data));
2217
2218       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2219       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2220     }
2221   return bp_objfile_data;
2222 }
2223
2224 static void
2225 create_overlay_event_breakpoint (void)
2226 {
2227   struct objfile *objfile;
2228   const char *const func_name = "_ovly_debug_event";
2229
2230   ALL_OBJFILES (objfile)
2231     {
2232       struct breakpoint *b;
2233       struct breakpoint_objfile_data *bp_objfile_data;
2234       CORE_ADDR addr;
2235
2236       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2237
2238       if (msym_not_found_p (bp_objfile_data->overlay_msym))
2239         continue;
2240
2241       if (bp_objfile_data->overlay_msym == NULL)
2242         {
2243           struct minimal_symbol *m;
2244
2245           m = lookup_minimal_symbol_text (func_name, objfile);
2246           if (m == NULL)
2247             {
2248               /* Avoid future lookups in this objfile.  */
2249               bp_objfile_data->overlay_msym = &msym_not_found;
2250               continue;
2251             }
2252           bp_objfile_data->overlay_msym = m;
2253         }
2254
2255       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2256       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2257                                       bp_overlay_event,
2258                                       &internal_breakpoint_ops);
2259       b->addr_string = xstrdup (func_name);
2260
2261       if (overlay_debugging == ovly_auto)
2262         {
2263           b->enable_state = bp_enabled;
2264           overlay_events_enabled = 1;
2265         }
2266       else
2267        {
2268          b->enable_state = bp_disabled;
2269          overlay_events_enabled = 0;
2270        }
2271     }
2272   update_global_location_list (1);
2273 }
2274
2275 static void
2276 create_longjmp_master_breakpoint (void)
2277 {
2278   struct program_space *pspace;
2279   struct cleanup *old_chain;
2280
2281   old_chain = save_current_program_space ();
2282
2283   ALL_PSPACES (pspace)
2284   {
2285     struct objfile *objfile;
2286
2287     set_current_program_space (pspace);
2288
2289     ALL_OBJFILES (objfile)
2290     {
2291       int i;
2292       struct gdbarch *gdbarch;
2293       struct breakpoint_objfile_data *bp_objfile_data;
2294
2295       gdbarch = get_objfile_arch (objfile);
2296       if (!gdbarch_get_longjmp_target_p (gdbarch))
2297         continue;
2298
2299       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2300
2301       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2302         {
2303           struct breakpoint *b;
2304           const char *func_name;
2305           CORE_ADDR addr;
2306
2307           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2308             continue;
2309
2310           func_name = longjmp_names[i];
2311           if (bp_objfile_data->longjmp_msym[i] == NULL)
2312             {
2313               struct minimal_symbol *m;
2314
2315               m = lookup_minimal_symbol_text (func_name, objfile);
2316               if (m == NULL)
2317                 {
2318                   /* Prevent future lookups in this objfile.  */
2319                   bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2320                   continue;
2321                 }
2322               bp_objfile_data->longjmp_msym[i] = m;
2323             }
2324
2325           addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2326           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2327                                           &internal_breakpoint_ops);
2328           b->addr_string = xstrdup (func_name);
2329           b->enable_state = bp_disabled;
2330         }
2331     }
2332   }
2333   update_global_location_list (1);
2334
2335   do_cleanups (old_chain);
2336 }
2337
2338 /* Create a master std::terminate breakpoint.  */
2339 static void
2340 create_std_terminate_master_breakpoint (void)
2341 {
2342   struct program_space *pspace;
2343   struct cleanup *old_chain;
2344   const char *const func_name = "std::terminate()";
2345
2346   old_chain = save_current_program_space ();
2347
2348   ALL_PSPACES (pspace)
2349   {
2350     struct objfile *objfile;
2351     CORE_ADDR addr;
2352
2353     set_current_program_space (pspace);
2354
2355     ALL_OBJFILES (objfile)
2356     {
2357       struct breakpoint *b;
2358       struct breakpoint_objfile_data *bp_objfile_data;
2359
2360       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2361
2362       if (msym_not_found_p (bp_objfile_data->terminate_msym))
2363         continue;
2364
2365       if (bp_objfile_data->terminate_msym == NULL)
2366         {
2367           struct minimal_symbol *m;
2368
2369           m = lookup_minimal_symbol (func_name, NULL, objfile);
2370           if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2371                             && MSYMBOL_TYPE (m) != mst_file_text))
2372             {
2373               /* Prevent future lookups in this objfile.  */
2374               bp_objfile_data->terminate_msym = &msym_not_found;
2375               continue;
2376             }
2377           bp_objfile_data->terminate_msym = m;
2378         }
2379
2380       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2381       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2382                                       bp_std_terminate_master,
2383                                       &internal_breakpoint_ops);
2384       b->addr_string = xstrdup (func_name);
2385       b->enable_state = bp_disabled;
2386     }
2387   }
2388
2389   update_global_location_list (1);
2390
2391   do_cleanups (old_chain);
2392 }
2393
2394 /* Install a master breakpoint on the unwinder's debug hook.  */
2395
2396 void
2397 create_exception_master_breakpoint (void)
2398 {
2399   struct objfile *objfile;
2400   const char *const func_name = "_Unwind_DebugHook";
2401
2402   ALL_OBJFILES (objfile)
2403     {
2404       struct breakpoint *b;
2405       struct gdbarch *gdbarch;
2406       struct breakpoint_objfile_data *bp_objfile_data;
2407       CORE_ADDR addr;
2408
2409       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2410
2411       if (msym_not_found_p (bp_objfile_data->exception_msym))
2412         continue;
2413
2414       gdbarch = get_objfile_arch (objfile);
2415
2416       if (bp_objfile_data->exception_msym == NULL)
2417         {
2418           struct minimal_symbol *debug_hook;
2419
2420           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2421           if (debug_hook == NULL)
2422             {
2423               bp_objfile_data->exception_msym = &msym_not_found;
2424               continue;
2425             }
2426
2427           bp_objfile_data->exception_msym = debug_hook;
2428         }
2429
2430       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2431       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2432                                                  &current_target);
2433       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2434                                       &internal_breakpoint_ops);
2435       b->addr_string = xstrdup (func_name);
2436       b->enable_state = bp_disabled;
2437     }
2438
2439   update_global_location_list (1);
2440 }
2441
2442 void
2443 update_breakpoints_after_exec (void)
2444 {
2445   struct breakpoint *b, *b_tmp;
2446   struct bp_location *bploc, **bplocp_tmp;
2447
2448   /* We're about to delete breakpoints from GDB's lists.  If the
2449      INSERTED flag is true, GDB will try to lift the breakpoints by
2450      writing the breakpoints' "shadow contents" back into memory.  The
2451      "shadow contents" are NOT valid after an exec, so GDB should not
2452      do that.  Instead, the target is responsible from marking
2453      breakpoints out as soon as it detects an exec.  We don't do that
2454      here instead, because there may be other attempts to delete
2455      breakpoints after detecting an exec and before reaching here.  */
2456   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
2457     if (bploc->pspace == current_program_space)
2458       gdb_assert (!bploc->inserted);
2459
2460   ALL_BREAKPOINTS_SAFE (b, b_tmp)
2461   {
2462     if (b->pspace != current_program_space)
2463       continue;
2464
2465     /* Solib breakpoints must be explicitly reset after an exec().  */
2466     if (b->type == bp_shlib_event)
2467       {
2468         delete_breakpoint (b);
2469         continue;
2470       }
2471
2472     /* JIT breakpoints must be explicitly reset after an exec().  */
2473     if (b->type == bp_jit_event)
2474       {
2475         delete_breakpoint (b);
2476         continue;
2477       }
2478
2479     /* Thread event breakpoints must be set anew after an exec(),
2480        as must overlay event and longjmp master breakpoints.  */
2481     if (b->type == bp_thread_event || b->type == bp_overlay_event
2482         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2483         || b->type == bp_exception_master)
2484       {
2485         delete_breakpoint (b);
2486         continue;
2487       }
2488
2489     /* Step-resume breakpoints are meaningless after an exec().  */
2490     if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
2491       {
2492         delete_breakpoint (b);
2493         continue;
2494       }
2495
2496     /* Longjmp and longjmp-resume breakpoints are also meaningless
2497        after an exec.  */
2498     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2499         || b->type == bp_exception || b->type == bp_exception_resume)
2500       {
2501         delete_breakpoint (b);
2502         continue;
2503       }
2504
2505     if (b->type == bp_catchpoint)
2506       {
2507         /* For now, none of the bp_catchpoint breakpoints need to
2508            do anything at this point.  In the future, if some of
2509            the catchpoints need to something, we will need to add
2510            a new method, and call this method from here.  */
2511         continue;
2512       }
2513
2514     /* bp_finish is a special case.  The only way we ought to be able
2515        to see one of these when an exec() has happened, is if the user
2516        caught a vfork, and then said "finish".  Ordinarily a finish just
2517        carries them to the call-site of the current callee, by setting
2518        a temporary bp there and resuming.  But in this case, the finish
2519        will carry them entirely through the vfork & exec.
2520
2521        We don't want to allow a bp_finish to remain inserted now.  But
2522        we can't safely delete it, 'cause finish_command has a handle to
2523        the bp on a bpstat, and will later want to delete it.  There's a
2524        chance (and I've seen it happen) that if we delete the bp_finish
2525        here, that its storage will get reused by the time finish_command
2526        gets 'round to deleting the "use to be a bp_finish" breakpoint.
2527        We really must allow finish_command to delete a bp_finish.
2528
2529        In the absence of a general solution for the "how do we know
2530        it's safe to delete something others may have handles to?"
2531        problem, what we'll do here is just uninsert the bp_finish, and
2532        let finish_command delete it.
2533
2534        (We know the bp_finish is "doomed" in the sense that it's
2535        momentary, and will be deleted as soon as finish_command sees
2536        the inferior stopped.  So it doesn't matter that the bp's
2537        address is probably bogus in the new a.out, unlike e.g., the
2538        solib breakpoints.)  */
2539
2540     if (b->type == bp_finish)
2541       {
2542         continue;
2543       }
2544
2545     /* Without a symbolic address, we have little hope of the
2546        pre-exec() address meaning the same thing in the post-exec()
2547        a.out.  */
2548     if (b->addr_string == NULL)
2549       {
2550         delete_breakpoint (b);
2551         continue;
2552       }
2553   }
2554   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
2555   create_overlay_event_breakpoint ();
2556   create_longjmp_master_breakpoint ();
2557   create_std_terminate_master_breakpoint ();
2558   create_exception_master_breakpoint ();
2559 }
2560
2561 int
2562 detach_breakpoints (int pid)
2563 {
2564   struct bp_location *bl, **blp_tmp;
2565   int val = 0;
2566   struct cleanup *old_chain = save_inferior_ptid ();
2567   struct inferior *inf = current_inferior ();
2568
2569   if (pid == PIDGET (inferior_ptid))
2570     error (_("Cannot detach breakpoints of inferior_ptid"));
2571
2572   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
2573   inferior_ptid = pid_to_ptid (pid);
2574   ALL_BP_LOCATIONS (bl, blp_tmp)
2575   {
2576     if (bl->pspace != inf->pspace)
2577       continue;
2578
2579     if (bl->inserted)
2580       val |= remove_breakpoint_1 (bl, mark_inserted);
2581   }
2582
2583   /* Detach single-step breakpoints as well.  */
2584   detach_single_step_breakpoints ();
2585
2586   do_cleanups (old_chain);
2587   return val;
2588 }
2589
2590 /* Remove the breakpoint location BL from the current address space.
2591    Note that this is used to detach breakpoints from a child fork.
2592    When we get here, the child isn't in the inferior list, and neither
2593    do we have objects to represent its address space --- we should
2594    *not* look at bl->pspace->aspace here.  */
2595
2596 static int
2597 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
2598 {
2599   int val;
2600
2601   /* BL is never in moribund_locations by our callers.  */
2602   gdb_assert (bl->owner != NULL);
2603
2604   if (bl->owner->enable_state == bp_permanent)
2605     /* Permanent breakpoints cannot be inserted or removed.  */
2606     return 0;
2607
2608   /* The type of none suggests that owner is actually deleted.
2609      This should not ever happen.  */
2610   gdb_assert (bl->owner->type != bp_none);
2611
2612   if (bl->loc_type == bp_loc_software_breakpoint
2613       || bl->loc_type == bp_loc_hardware_breakpoint)
2614     {
2615       /* "Normal" instruction breakpoint: either the standard
2616          trap-instruction bp (bp_breakpoint), or a
2617          bp_hardware_breakpoint.  */
2618
2619       /* First check to see if we have to handle an overlay.  */
2620       if (overlay_debugging == ovly_off
2621           || bl->section == NULL
2622           || !(section_is_overlay (bl->section)))
2623         {
2624           /* No overlay handling: just remove the breakpoint.  */
2625           val = bl->owner->ops->remove_location (bl);
2626         }
2627       else
2628         {
2629           /* This breakpoint is in an overlay section.
2630              Did we set a breakpoint at the LMA?  */
2631           if (!overlay_events_enabled)
2632               {
2633                 /* Yes -- overlay event support is not active, so we
2634                    should have set a breakpoint at the LMA.  Remove it.  
2635                 */
2636                 /* Ignore any failures: if the LMA is in ROM, we will
2637                    have already warned when we failed to insert it.  */
2638                 if (bl->loc_type == bp_loc_hardware_breakpoint)
2639                   target_remove_hw_breakpoint (bl->gdbarch,
2640                                                &bl->overlay_target_info);
2641                 else
2642                   target_remove_breakpoint (bl->gdbarch,
2643                                             &bl->overlay_target_info);
2644               }
2645           /* Did we set a breakpoint at the VMA? 
2646              If so, we will have marked the breakpoint 'inserted'.  */
2647           if (bl->inserted)
2648             {
2649               /* Yes -- remove it.  Previously we did not bother to
2650                  remove the breakpoint if the section had been
2651                  unmapped, but let's not rely on that being safe.  We
2652                  don't know what the overlay manager might do.  */
2653
2654               /* However, we should remove *software* breakpoints only
2655                  if the section is still mapped, or else we overwrite
2656                  wrong code with the saved shadow contents.  */
2657               if (bl->loc_type == bp_loc_hardware_breakpoint
2658                   || section_is_mapped (bl->section))
2659                 val = bl->owner->ops->remove_location (bl);
2660               else
2661                 val = 0;
2662             }
2663           else
2664             {
2665               /* No -- not inserted, so no need to remove.  No error.  */
2666               val = 0;
2667             }
2668         }
2669
2670       /* In some cases, we might not be able to remove a breakpoint
2671          in a shared library that has already been removed, but we
2672          have not yet processed the shlib unload event.  */
2673       if (val && solib_name_from_address (bl->pspace, bl->address))
2674         val = 0;
2675
2676       if (val)
2677         return val;
2678       bl->inserted = (is == mark_inserted);
2679     }
2680   else if (bl->loc_type == bp_loc_hardware_watchpoint)
2681     {
2682       gdb_assert (bl->owner->ops != NULL
2683                   && bl->owner->ops->remove_location != NULL);
2684
2685       bl->inserted = (is == mark_inserted);
2686       bl->owner->ops->remove_location (bl);
2687
2688       /* Failure to remove any of the hardware watchpoints comes here.  */
2689       if ((is == mark_uninserted) && (bl->inserted))
2690         warning (_("Could not remove hardware watchpoint %d."),
2691                  bl->owner->number);
2692     }
2693   else if (bl->owner->type == bp_catchpoint
2694            && breakpoint_enabled (bl->owner)
2695            && !bl->duplicate)
2696     {
2697       gdb_assert (bl->owner->ops != NULL
2698                   && bl->owner->ops->remove_location != NULL);
2699
2700       val = bl->owner->ops->remove_location (bl);
2701       if (val)
2702         return val;
2703
2704       bl->inserted = (is == mark_inserted);
2705     }
2706
2707   return 0;
2708 }
2709
2710 static int
2711 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
2712 {
2713   int ret;
2714   struct cleanup *old_chain;
2715
2716   /* BL is never in moribund_locations by our callers.  */
2717   gdb_assert (bl->owner != NULL);
2718
2719   if (bl->owner->enable_state == bp_permanent)
2720     /* Permanent breakpoints cannot be inserted or removed.  */
2721     return 0;
2722
2723   /* The type of none suggests that owner is actually deleted.
2724      This should not ever happen.  */
2725   gdb_assert (bl->owner->type != bp_none);
2726
2727   old_chain = save_current_space_and_thread ();
2728
2729   switch_to_program_space_and_thread (bl->pspace);
2730
2731   ret = remove_breakpoint_1 (bl, is);
2732
2733   do_cleanups (old_chain);
2734   return ret;
2735 }
2736
2737 /* Clear the "inserted" flag in all breakpoints.  */
2738
2739 void
2740 mark_breakpoints_out (void)
2741 {
2742   struct bp_location *bl, **blp_tmp;
2743
2744   ALL_BP_LOCATIONS (bl, blp_tmp)
2745     if (bl->pspace == current_program_space)
2746       bl->inserted = 0;
2747 }
2748
2749 /* Clear the "inserted" flag in all breakpoints and delete any
2750    breakpoints which should go away between runs of the program.
2751
2752    Plus other such housekeeping that has to be done for breakpoints
2753    between runs.
2754
2755    Note: this function gets called at the end of a run (by
2756    generic_mourn_inferior) and when a run begins (by
2757    init_wait_for_inferior).  */
2758
2759
2760
2761 void
2762 breakpoint_init_inferior (enum inf_context context)
2763 {
2764   struct breakpoint *b, *b_tmp;
2765   struct bp_location *bl, **blp_tmp;
2766   int ix;
2767   struct program_space *pspace = current_program_space;
2768
2769   /* If breakpoint locations are shared across processes, then there's
2770      nothing to do.  */
2771   if (gdbarch_has_global_breakpoints (target_gdbarch))
2772     return;
2773
2774   ALL_BP_LOCATIONS (bl, blp_tmp)
2775   {
2776     /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
2777     if (bl->pspace == pspace
2778         && bl->owner->enable_state != bp_permanent)
2779       bl->inserted = 0;
2780   }
2781
2782   ALL_BREAKPOINTS_SAFE (b, b_tmp)
2783   {
2784     if (b->loc && b->loc->pspace != pspace)
2785       continue;
2786
2787     switch (b->type)
2788       {
2789       case bp_call_dummy:
2790
2791         /* If the call dummy breakpoint is at the entry point it will
2792            cause problems when the inferior is rerun, so we better get
2793            rid of it.  */
2794
2795       case bp_watchpoint_scope:
2796
2797         /* Also get rid of scope breakpoints.  */
2798
2799       case bp_shlib_event:
2800
2801         /* Also remove solib event breakpoints.  Their addresses may
2802            have changed since the last time we ran the program.
2803            Actually we may now be debugging against different target;
2804            and so the solib backend that installed this breakpoint may
2805            not be used in by the target.  E.g.,
2806
2807            (gdb) file prog-linux
2808            (gdb) run               # native linux target
2809            ...
2810            (gdb) kill
2811            (gdb) file prog-win.exe
2812            (gdb) tar rem :9999     # remote Windows gdbserver.
2813         */
2814
2815         delete_breakpoint (b);
2816         break;
2817
2818       case bp_watchpoint:
2819       case bp_hardware_watchpoint:
2820       case bp_read_watchpoint:
2821       case bp_access_watchpoint:
2822         {
2823           struct watchpoint *w = (struct watchpoint *) b;
2824
2825           /* Likewise for watchpoints on local expressions.  */
2826           if (w->exp_valid_block != NULL)
2827             delete_breakpoint (b);
2828           else if (context == inf_starting)
2829             {
2830               /* Reset val field to force reread of starting value in
2831                  insert_breakpoints.  */
2832               if (w->val)
2833                 value_free (w->val);
2834               w->val = NULL;
2835               w->val_valid = 0;
2836           }
2837         }
2838         break;
2839       default:
2840         break;
2841       }
2842   }
2843
2844   /* Get rid of the moribund locations.  */
2845   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2846     decref_bp_location (&bl);
2847   VEC_free (bp_location_p, moribund_locations);
2848 }
2849
2850 /* These functions concern about actual breakpoints inserted in the
2851    target --- to e.g. check if we need to do decr_pc adjustment or if
2852    we need to hop over the bkpt --- so we check for address space
2853    match, not program space.  */
2854
2855 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2856    exists at PC.  It returns ordinary_breakpoint_here if it's an
2857    ordinary breakpoint, or permanent_breakpoint_here if it's a
2858    permanent breakpoint.
2859    - When continuing from a location with an ordinary breakpoint, we
2860      actually single step once before calling insert_breakpoints.
2861    - When continuing from a location with a permanent breakpoint, we
2862      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2863      the target, to advance the PC past the breakpoint.  */
2864
2865 enum breakpoint_here
2866 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2867 {
2868   struct bp_location *bl, **blp_tmp;
2869   int any_breakpoint_here = 0;
2870
2871   ALL_BP_LOCATIONS (bl, blp_tmp)
2872     {
2873       if (bl->loc_type != bp_loc_software_breakpoint
2874           && bl->loc_type != bp_loc_hardware_breakpoint)
2875         continue;
2876
2877       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
2878       if ((breakpoint_enabled (bl->owner)
2879            || bl->owner->enable_state == bp_permanent)
2880           && breakpoint_location_address_match (bl, aspace, pc))
2881         {
2882           if (overlay_debugging 
2883               && section_is_overlay (bl->section)
2884               && !section_is_mapped (bl->section))
2885             continue;           /* unmapped overlay -- can't be a match */
2886           else if (bl->owner->enable_state == bp_permanent)
2887             return permanent_breakpoint_here;
2888           else
2889             any_breakpoint_here = 1;
2890         }
2891     }
2892
2893   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
2894 }
2895
2896 /* Return true if there's a moribund breakpoint at PC.  */
2897
2898 int
2899 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2900 {
2901   struct bp_location *loc;
2902   int ix;
2903
2904   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
2905     if (breakpoint_location_address_match (loc, aspace, pc))
2906       return 1;
2907
2908   return 0;
2909 }
2910
2911 /* Returns non-zero if there's a breakpoint inserted at PC, which is
2912    inserted using regular breakpoint_chain / bp_location array
2913    mechanism.  This does not check for single-step breakpoints, which
2914    are inserted and removed using direct target manipulation.  */
2915
2916 int
2917 regular_breakpoint_inserted_here_p (struct address_space *aspace, 
2918                                     CORE_ADDR pc)
2919 {
2920   struct bp_location *bl, **blp_tmp;
2921
2922   ALL_BP_LOCATIONS (bl, blp_tmp)
2923     {
2924       if (bl->loc_type != bp_loc_software_breakpoint
2925           && bl->loc_type != bp_loc_hardware_breakpoint)
2926         continue;
2927
2928       if (bl->inserted
2929           && breakpoint_location_address_match (bl, aspace, pc))
2930         {
2931           if (overlay_debugging 
2932               && section_is_overlay (bl->section)
2933               && !section_is_mapped (bl->section))
2934             continue;           /* unmapped overlay -- can't be a match */
2935           else
2936             return 1;
2937         }
2938     }
2939   return 0;
2940 }
2941
2942 /* Returns non-zero iff there's either regular breakpoint
2943    or a single step breakpoint inserted at PC.  */
2944
2945 int
2946 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
2947 {
2948   if (regular_breakpoint_inserted_here_p (aspace, pc))
2949     return 1;
2950
2951   if (single_step_breakpoint_inserted_here_p (aspace, pc))
2952     return 1;
2953
2954   return 0;
2955 }
2956
2957 /* This function returns non-zero iff there is a software breakpoint
2958    inserted at PC.  */
2959
2960 int
2961 software_breakpoint_inserted_here_p (struct address_space *aspace,
2962                                      CORE_ADDR pc)
2963 {
2964   struct bp_location *bl, **blp_tmp;
2965
2966   ALL_BP_LOCATIONS (bl, blp_tmp)
2967     {
2968       if (bl->loc_type != bp_loc_software_breakpoint)
2969         continue;
2970
2971       if (bl->inserted
2972           && breakpoint_address_match (bl->pspace->aspace, bl->address,
2973                                        aspace, pc))
2974         {
2975           if (overlay_debugging 
2976               && section_is_overlay (bl->section)
2977               && !section_is_mapped (bl->section))
2978             continue;           /* unmapped overlay -- can't be a match */
2979           else
2980             return 1;
2981         }
2982     }
2983
2984   /* Also check for software single-step breakpoints.  */
2985   if (single_step_breakpoint_inserted_here_p (aspace, pc))
2986     return 1;
2987
2988   return 0;
2989 }
2990
2991 int
2992 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2993                                        CORE_ADDR addr, ULONGEST len)
2994 {
2995   struct breakpoint *bpt;
2996
2997   ALL_BREAKPOINTS (bpt)
2998     {
2999       struct bp_location *loc;
3000
3001       if (bpt->type != bp_hardware_watchpoint
3002           && bpt->type != bp_access_watchpoint)
3003         continue;
3004
3005       if (!breakpoint_enabled (bpt))
3006         continue;
3007
3008       for (loc = bpt->loc; loc; loc = loc->next)
3009         if (loc->pspace->aspace == aspace && loc->inserted)
3010           {
3011             CORE_ADDR l, h;
3012
3013             /* Check for intersection.  */
3014             l = max (loc->address, addr);
3015             h = min (loc->address + loc->length, addr + len);
3016             if (l < h)
3017               return 1;
3018           }
3019     }
3020   return 0;
3021 }
3022
3023 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3024    PC is valid for process/thread PTID.  */
3025
3026 int
3027 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3028                          ptid_t ptid)
3029 {
3030   struct bp_location *bl, **blp_tmp;
3031   /* The thread and task IDs associated to PTID, computed lazily.  */
3032   int thread = -1;
3033   int task = 0;
3034   
3035   ALL_BP_LOCATIONS (bl, blp_tmp)
3036     {
3037       if (bl->loc_type != bp_loc_software_breakpoint
3038           && bl->loc_type != bp_loc_hardware_breakpoint)
3039         continue;
3040
3041       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
3042       if (!breakpoint_enabled (bl->owner)
3043           && bl->owner->enable_state != bp_permanent)
3044         continue;
3045
3046       if (!breakpoint_location_address_match (bl, aspace, pc))
3047         continue;
3048
3049       if (bl->owner->thread != -1)
3050         {
3051           /* This is a thread-specific breakpoint.  Check that ptid
3052              matches that thread.  If thread hasn't been computed yet,
3053              it is now time to do so.  */
3054           if (thread == -1)
3055             thread = pid_to_thread_id (ptid);
3056           if (bl->owner->thread != thread)
3057             continue;
3058         }
3059
3060       if (bl->owner->task != 0)
3061         {
3062           /* This is a task-specific breakpoint.  Check that ptid
3063              matches that task.  If task hasn't been computed yet,
3064              it is now time to do so.  */
3065           if (task == 0)
3066             task = ada_get_task_number (ptid);
3067           if (bl->owner->task != task)
3068             continue;
3069         }
3070
3071       if (overlay_debugging 
3072           && section_is_overlay (bl->section)
3073           && !section_is_mapped (bl->section))
3074         continue;           /* unmapped overlay -- can't be a match */
3075
3076       return 1;
3077     }
3078
3079   return 0;
3080 }
3081 \f
3082
3083 /* bpstat stuff.  External routines' interfaces are documented
3084    in breakpoint.h.  */
3085
3086 int
3087 ep_is_catchpoint (struct breakpoint *ep)
3088 {
3089   return (ep->type == bp_catchpoint);
3090 }
3091
3092 /* Frees any storage that is part of a bpstat.  Does not walk the
3093    'next' chain.  */
3094
3095 static void
3096 bpstat_free (bpstat bs)
3097 {
3098   if (bs->old_val != NULL)
3099     value_free (bs->old_val);
3100   decref_counted_command_line (&bs->commands);
3101   decref_bp_location (&bs->bp_location_at);
3102   xfree (bs);
3103 }
3104
3105 /* Clear a bpstat so that it says we are not at any breakpoint.
3106    Also free any storage that is part of a bpstat.  */
3107
3108 void
3109 bpstat_clear (bpstat *bsp)
3110 {
3111   bpstat p;
3112   bpstat q;
3113
3114   if (bsp == 0)
3115     return;
3116   p = *bsp;
3117   while (p != NULL)
3118     {
3119       q = p->next;
3120       bpstat_free (p);
3121       p = q;
3122     }
3123   *bsp = NULL;
3124 }
3125
3126 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
3127    is part of the bpstat is copied as well.  */
3128
3129 bpstat
3130 bpstat_copy (bpstat bs)
3131 {
3132   bpstat p = NULL;
3133   bpstat tmp;
3134   bpstat retval = NULL;
3135
3136   if (bs == NULL)
3137     return bs;
3138
3139   for (; bs != NULL; bs = bs->next)
3140     {
3141       tmp = (bpstat) xmalloc (sizeof (*tmp));
3142       memcpy (tmp, bs, sizeof (*tmp));
3143       incref_counted_command_line (tmp->commands);
3144       incref_bp_location (tmp->bp_location_at);
3145       if (bs->old_val != NULL)
3146         {
3147           tmp->old_val = value_copy (bs->old_val);
3148           release_value (tmp->old_val);
3149         }
3150
3151       if (p == NULL)
3152         /* This is the first thing in the chain.  */
3153         retval = tmp;
3154       else
3155         p->next = tmp;
3156       p = tmp;
3157     }
3158   p->next = NULL;
3159   return retval;
3160 }
3161
3162 /* Find the bpstat associated with this breakpoint.  */
3163
3164 bpstat
3165 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3166 {
3167   if (bsp == NULL)
3168     return NULL;
3169
3170   for (; bsp != NULL; bsp = bsp->next)
3171     {
3172       if (bsp->breakpoint_at == breakpoint)
3173         return bsp;
3174     }
3175   return NULL;
3176 }
3177
3178 /* Put in *NUM the breakpoint number of the first breakpoint we are
3179    stopped at.  *BSP upon return is a bpstat which points to the
3180    remaining breakpoints stopped at (but which is not guaranteed to be
3181    good for anything but further calls to bpstat_num).
3182
3183    Return 0 if passed a bpstat which does not indicate any breakpoints.
3184    Return -1 if stopped at a breakpoint that has been deleted since
3185    we set it.
3186    Return 1 otherwise.  */
3187
3188 int
3189 bpstat_num (bpstat *bsp, int *num)
3190 {
3191   struct breakpoint *b;
3192
3193   if ((*bsp) == NULL)
3194     return 0;                   /* No more breakpoint values */
3195
3196   /* We assume we'll never have several bpstats that correspond to a
3197      single breakpoint -- otherwise, this function might return the
3198      same number more than once and this will look ugly.  */
3199   b = (*bsp)->breakpoint_at;
3200   *bsp = (*bsp)->next;
3201   if (b == NULL)
3202     return -1;                  /* breakpoint that's been deleted since */
3203
3204   *num = b->number;             /* We have its number */
3205   return 1;
3206 }
3207
3208 /* See breakpoint.h.  */
3209
3210 void
3211 bpstat_clear_actions (void)
3212 {
3213   struct thread_info *tp;
3214   bpstat bs;
3215
3216   if (ptid_equal (inferior_ptid, null_ptid))
3217     return;
3218
3219   tp = find_thread_ptid (inferior_ptid);
3220   if (tp == NULL)
3221     return;
3222
3223   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
3224     {
3225       decref_counted_command_line (&bs->commands);
3226
3227       if (bs->old_val != NULL)
3228         {
3229           value_free (bs->old_val);
3230           bs->old_val = NULL;
3231         }
3232     }
3233 }
3234
3235 /* Called when a command is about to proceed the inferior.  */
3236
3237 static void
3238 breakpoint_about_to_proceed (void)
3239 {
3240   if (!ptid_equal (inferior_ptid, null_ptid))
3241     {
3242       struct thread_info *tp = inferior_thread ();
3243
3244       /* Allow inferior function calls in breakpoint commands to not
3245          interrupt the command list.  When the call finishes
3246          successfully, the inferior will be standing at the same
3247          breakpoint as if nothing happened.  */
3248       if (tp->control.in_infcall)
3249         return;
3250     }
3251
3252   breakpoint_proceeded = 1;
3253 }
3254
3255 /* Stub for cleaning up our state if we error-out of a breakpoint
3256    command.  */
3257 static void
3258 cleanup_executing_breakpoints (void *ignore)
3259 {
3260   executing_breakpoint_commands = 0;
3261 }
3262
3263 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
3264    or its equivalent.  */
3265
3266 static int
3267 command_line_is_silent (struct command_line *cmd)
3268 {
3269   return cmd && (strcmp ("silent", cmd->line) == 0
3270                  || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3271 }
3272
3273 /* Execute all the commands associated with all the breakpoints at
3274    this location.  Any of these commands could cause the process to
3275    proceed beyond this point, etc.  We look out for such changes by
3276    checking the global "breakpoint_proceeded" after each command.
3277
3278    Returns true if a breakpoint command resumed the inferior.  In that
3279    case, it is the caller's responsibility to recall it again with the
3280    bpstat of the current thread.  */
3281
3282 static int
3283 bpstat_do_actions_1 (bpstat *bsp)
3284 {
3285   bpstat bs;
3286   struct cleanup *old_chain;
3287   int again = 0;
3288
3289   /* Avoid endless recursion if a `source' command is contained
3290      in bs->commands.  */
3291   if (executing_breakpoint_commands)
3292     return 0;
3293
3294   executing_breakpoint_commands = 1;
3295   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3296
3297   prevent_dont_repeat ();
3298
3299   /* This pointer will iterate over the list of bpstat's.  */
3300   bs = *bsp;
3301
3302   breakpoint_proceeded = 0;
3303   for (; bs != NULL; bs = bs->next)
3304     {
3305       struct counted_command_line *ccmd;
3306       struct command_line *cmd;
3307       struct cleanup *this_cmd_tree_chain;
3308
3309       /* Take ownership of the BSP's command tree, if it has one.
3310
3311          The command tree could legitimately contain commands like
3312          'step' and 'next', which call clear_proceed_status, which
3313          frees stop_bpstat's command tree.  To make sure this doesn't
3314          free the tree we're executing out from under us, we need to
3315          take ownership of the tree ourselves.  Since a given bpstat's
3316          commands are only executed once, we don't need to copy it; we
3317          can clear the pointer in the bpstat, and make sure we free
3318          the tree when we're done.  */
3319       ccmd = bs->commands;
3320       bs->commands = NULL;
3321       this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3322       cmd = ccmd ? ccmd->commands : NULL;
3323       if (command_line_is_silent (cmd))
3324         {
3325           /* The action has been already done by bpstat_stop_status.  */
3326           cmd = cmd->next;
3327         }
3328
3329       while (cmd != NULL)
3330         {
3331           execute_control_command (cmd);
3332
3333           if (breakpoint_proceeded)
3334             break;
3335           else
3336             cmd = cmd->next;
3337         }
3338
3339       /* We can free this command tree now.  */
3340       do_cleanups (this_cmd_tree_chain);
3341
3342       if (breakpoint_proceeded)
3343         {
3344           if (target_can_async_p ())
3345             /* If we are in async mode, then the target might be still
3346                running, not stopped at any breakpoint, so nothing for
3347                us to do here -- just return to the event loop.  */
3348             ;
3349           else
3350             /* In sync mode, when execute_control_command returns
3351                we're already standing on the next breakpoint.
3352                Breakpoint commands for that stop were not run, since
3353                execute_command does not run breakpoint commands --
3354                only command_line_handler does, but that one is not
3355                involved in execution of breakpoint commands.  So, we
3356                can now execute breakpoint commands.  It should be
3357                noted that making execute_command do bpstat actions is
3358                not an option -- in this case we'll have recursive
3359                invocation of bpstat for each breakpoint with a
3360                command, and can easily blow up GDB stack.  Instead, we
3361                return true, which will trigger the caller to recall us
3362                with the new stop_bpstat.  */
3363             again = 1;
3364           break;
3365         }
3366     }
3367   do_cleanups (old_chain);
3368   return again;
3369 }
3370
3371 void
3372 bpstat_do_actions (void)
3373 {
3374   struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3375
3376   /* Do any commands attached to breakpoint we are stopped at.  */
3377   while (!ptid_equal (inferior_ptid, null_ptid)
3378          && target_has_execution
3379          && !is_exited (inferior_ptid)
3380          && !is_executing (inferior_ptid))
3381     /* Since in sync mode, bpstat_do_actions may resume the inferior,
3382        and only return when it is stopped at the next breakpoint, we
3383        keep doing breakpoint actions until it returns false to
3384        indicate the inferior was not resumed.  */
3385     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
3386       break;
3387
3388   discard_cleanups (cleanup_if_error);
3389 }
3390
3391 /* Print out the (old or new) value associated with a watchpoint.  */
3392
3393 static void
3394 watchpoint_value_print (struct value *val, struct ui_file *stream)
3395 {
3396   if (val == NULL)
3397     fprintf_unfiltered (stream, _("<unreadable>"));
3398   else
3399     {
3400       struct value_print_options opts;
3401       get_user_print_options (&opts);
3402       value_print (val, stream, &opts);
3403     }
3404 }
3405
3406 /* Generic routine for printing messages indicating why we
3407    stopped.  The behavior of this function depends on the value
3408    'print_it' in the bpstat structure.  Under some circumstances we
3409    may decide not to print anything here and delegate the task to
3410    normal_stop().  */
3411
3412 static enum print_stop_action
3413 print_bp_stop_message (bpstat bs)
3414 {
3415   switch (bs->print_it)
3416     {
3417     case print_it_noop:
3418       /* Nothing should be printed for this bpstat entry.  */
3419       return PRINT_UNKNOWN;
3420       break;
3421
3422     case print_it_done:
3423       /* We still want to print the frame, but we already printed the
3424          relevant messages.  */
3425       return PRINT_SRC_AND_LOC;
3426       break;
3427
3428     case print_it_normal:
3429       {
3430         struct breakpoint *b = bs->breakpoint_at;
3431
3432         /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3433            which has since been deleted.  */
3434         if (b == NULL)
3435           return PRINT_UNKNOWN;
3436
3437         /* Normal case.  Call the breakpoint's print_it method.  */
3438         return b->ops->print_it (bs);
3439       }
3440       break;
3441
3442     default:
3443       internal_error (__FILE__, __LINE__,
3444                       _("print_bp_stop_message: unrecognized enum value"));
3445       break;
3446     }
3447 }
3448
3449 /* Print a message indicating what happened.  This is called from
3450    normal_stop().  The input to this routine is the head of the bpstat
3451    list - a list of the eventpoints that caused this stop.  This
3452    routine calls the generic print routine for printing a message
3453    about reasons for stopping.  This will print (for example) the
3454    "Breakpoint n," part of the output.  The return value of this
3455    routine is one of:
3456
3457    PRINT_UNKNOWN: Means we printed nothing.
3458    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
3459    code to print the location.  An example is 
3460    "Breakpoint 1, " which should be followed by
3461    the location.
3462    PRINT_SRC_ONLY: Means we printed something, but there is no need
3463    to also print the location part of the message.
3464    An example is the catch/throw messages, which
3465    don't require a location appended to the end.
3466    PRINT_NOTHING: We have done some printing and we don't need any 
3467    further info to be printed.  */
3468
3469 enum print_stop_action
3470 bpstat_print (bpstat bs)
3471 {
3472   int val;
3473
3474   /* Maybe another breakpoint in the chain caused us to stop.
3475      (Currently all watchpoints go on the bpstat whether hit or not.
3476      That probably could (should) be changed, provided care is taken
3477      with respect to bpstat_explains_signal).  */
3478   for (; bs; bs = bs->next)
3479     {
3480       val = print_bp_stop_message (bs);
3481       if (val == PRINT_SRC_ONLY 
3482           || val == PRINT_SRC_AND_LOC 
3483           || val == PRINT_NOTHING)
3484         return val;
3485     }
3486
3487   /* We reached the end of the chain, or we got a null BS to start
3488      with and nothing was printed.  */
3489   return PRINT_UNKNOWN;
3490 }
3491
3492 /* Evaluate the expression EXP and return 1 if value is zero.  This is
3493    used inside a catch_errors to evaluate the breakpoint condition.
3494    The argument is a "struct expression *" that has been cast to a
3495    "char *" to make it pass through catch_errors.  */
3496
3497 static int
3498 breakpoint_cond_eval (void *exp)
3499 {
3500   struct value *mark = value_mark ();
3501   int i = !value_true (evaluate_expression ((struct expression *) exp));
3502
3503   value_free_to_mark (mark);
3504   return i;
3505 }
3506
3507 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
3508
3509 static bpstat
3510 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
3511 {
3512   bpstat bs;
3513
3514   bs = (bpstat) xmalloc (sizeof (*bs));
3515   bs->next = NULL;
3516   **bs_link_pointer = bs;
3517   *bs_link_pointer = &bs->next;
3518   bs->breakpoint_at = bl->owner;
3519   bs->bp_location_at = bl;
3520   incref_bp_location (bl);
3521   /* If the condition is false, etc., don't do the commands.  */
3522   bs->commands = NULL;
3523   bs->old_val = NULL;
3524   bs->print_it = print_it_normal;
3525   return bs;
3526 }
3527 \f
3528 /* The target has stopped with waitstatus WS.  Check if any hardware
3529    watchpoints have triggered, according to the target.  */
3530
3531 int
3532 watchpoints_triggered (struct target_waitstatus *ws)
3533 {
3534   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
3535   CORE_ADDR addr;
3536   struct breakpoint *b;
3537
3538   if (!stopped_by_watchpoint)
3539     {
3540       /* We were not stopped by a watchpoint.  Mark all watchpoints
3541          as not triggered.  */
3542       ALL_BREAKPOINTS (b)
3543         if (is_hardware_watchpoint (b))
3544           {
3545             struct watchpoint *w = (struct watchpoint *) b;
3546
3547             w->watchpoint_triggered = watch_triggered_no;
3548           }
3549
3550       return 0;
3551     }
3552
3553   if (!target_stopped_data_address (&current_target, &addr))
3554     {
3555       /* We were stopped by a watchpoint, but we don't know where.
3556          Mark all watchpoints as unknown.  */
3557       ALL_BREAKPOINTS (b)
3558         if (is_hardware_watchpoint (b))
3559           {
3560             struct watchpoint *w = (struct watchpoint *) b;
3561
3562             w->watchpoint_triggered = watch_triggered_unknown;
3563           }
3564
3565       return stopped_by_watchpoint;
3566     }
3567
3568   /* The target could report the data address.  Mark watchpoints
3569      affected by this data address as triggered, and all others as not
3570      triggered.  */
3571
3572   ALL_BREAKPOINTS (b)
3573     if (is_hardware_watchpoint (b))
3574       {
3575         struct watchpoint *w = (struct watchpoint *) b;
3576         struct bp_location *loc;
3577
3578         w->watchpoint_triggered = watch_triggered_no;
3579         for (loc = b->loc; loc; loc = loc->next)
3580           {
3581             if (is_masked_watchpoint (b))
3582               {
3583                 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3584                 CORE_ADDR start = loc->address & w->hw_wp_mask;
3585
3586                 if (newaddr == start)
3587                   {
3588                     w->watchpoint_triggered = watch_triggered_yes;
3589                     break;
3590                   }
3591               }
3592             /* Exact match not required.  Within range is sufficient.  */
3593             else if (target_watchpoint_addr_within_range (&current_target,
3594                                                          addr, loc->address,
3595                                                          loc->length))
3596               {
3597                 w->watchpoint_triggered = watch_triggered_yes;
3598                 break;
3599               }
3600           }
3601       }
3602
3603   return 1;
3604 }
3605
3606 /* Possible return values for watchpoint_check (this can't be an enum
3607    because of check_errors).  */
3608 /* The watchpoint has been deleted.  */
3609 #define WP_DELETED 1
3610 /* The value has changed.  */
3611 #define WP_VALUE_CHANGED 2
3612 /* The value has not changed.  */
3613 #define WP_VALUE_NOT_CHANGED 3
3614 /* Ignore this watchpoint, no matter if the value changed or not.  */
3615 #define WP_IGNORE 4
3616
3617 #define BP_TEMPFLAG 1
3618 #define BP_HARDWAREFLAG 2
3619
3620 /* Evaluate watchpoint condition expression and check if its value
3621    changed.
3622
3623    P should be a pointer to struct bpstat, but is defined as a void *
3624    in order for this function to be usable with catch_errors.  */
3625
3626 static int
3627 watchpoint_check (void *p)
3628 {
3629   bpstat bs = (bpstat) p;
3630   struct watchpoint *b;
3631   struct frame_info *fr;
3632   int within_current_scope;
3633
3634   /* BS is built from an existing struct breakpoint.  */
3635   gdb_assert (bs->breakpoint_at != NULL);
3636   b = (struct watchpoint *) bs->breakpoint_at;
3637
3638   /* If this is a local watchpoint, we only want to check if the
3639      watchpoint frame is in scope if the current thread is the thread
3640      that was used to create the watchpoint.  */
3641   if (!watchpoint_in_thread_scope (b))
3642     return WP_IGNORE;
3643
3644   if (b->exp_valid_block == NULL)
3645     within_current_scope = 1;
3646   else
3647     {
3648       struct frame_info *frame = get_current_frame ();
3649       struct gdbarch *frame_arch = get_frame_arch (frame);
3650       CORE_ADDR frame_pc = get_frame_pc (frame);
3651
3652       /* in_function_epilogue_p() returns a non-zero value if we're
3653          still in the function but the stack frame has already been
3654          invalidated.  Since we can't rely on the values of local
3655          variables after the stack has been destroyed, we are treating
3656          the watchpoint in that state as `not changed' without further
3657          checking.  Don't mark watchpoints as changed if the current
3658          frame is in an epilogue - even if they are in some other
3659          frame, our view of the stack is likely to be wrong and
3660          frame_find_by_id could error out.  */
3661       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
3662         return WP_IGNORE;
3663
3664       fr = frame_find_by_id (b->watchpoint_frame);
3665       within_current_scope = (fr != NULL);
3666
3667       /* If we've gotten confused in the unwinder, we might have
3668          returned a frame that can't describe this variable.  */
3669       if (within_current_scope)
3670         {
3671           struct symbol *function;
3672
3673           function = get_frame_function (fr);
3674           if (function == NULL
3675               || !contained_in (b->exp_valid_block,
3676                                 SYMBOL_BLOCK_VALUE (function)))
3677             within_current_scope = 0;
3678         }
3679
3680       if (within_current_scope)
3681         /* If we end up stopping, the current frame will get selected
3682            in normal_stop.  So this call to select_frame won't affect
3683            the user.  */
3684         select_frame (fr);
3685     }
3686
3687   if (within_current_scope)
3688     {
3689       /* We use value_{,free_to_}mark because it could be a *long*
3690          time before we return to the command level and call
3691          free_all_values.  We can't call free_all_values because we
3692          might be in the middle of evaluating a function call.  */
3693
3694       int pc = 0;
3695       struct value *mark;
3696       struct value *new_val;
3697
3698       if (is_masked_watchpoint (&b->base))
3699         /* Since we don't know the exact trigger address (from
3700            stopped_data_address), just tell the user we've triggered
3701            a mask watchpoint.  */
3702         return WP_VALUE_CHANGED;
3703
3704       mark = value_mark ();
3705       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
3706
3707       /* We use value_equal_contents instead of value_equal because
3708          the latter coerces an array to a pointer, thus comparing just
3709          the address of the array instead of its contents.  This is
3710          not what we want.  */
3711       if ((b->val != NULL) != (new_val != NULL)
3712           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
3713         {
3714           if (new_val != NULL)
3715             {
3716               release_value (new_val);
3717               value_free_to_mark (mark);
3718             }
3719           bs->old_val = b->val;
3720           b->val = new_val;
3721           b->val_valid = 1;
3722           return WP_VALUE_CHANGED;
3723         }
3724       else
3725         {
3726           /* Nothing changed.  */
3727           value_free_to_mark (mark);
3728           return WP_VALUE_NOT_CHANGED;
3729         }
3730     }
3731   else
3732     {
3733       struct ui_out *uiout = current_uiout;
3734
3735       /* This seems like the only logical thing to do because
3736          if we temporarily ignored the watchpoint, then when
3737          we reenter the block in which it is valid it contains
3738          garbage (in the case of a function, it may have two
3739          garbage values, one before and one after the prologue).
3740          So we can't even detect the first assignment to it and
3741          watch after that (since the garbage may or may not equal
3742          the first value assigned).  */
3743       /* We print all the stop information in
3744          breakpoint_ops->print_it, but in this case, by the time we
3745          call breakpoint_ops->print_it this bp will be deleted
3746          already.  So we have no choice but print the information
3747          here.  */
3748       if (ui_out_is_mi_like_p (uiout))
3749         ui_out_field_string
3750           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
3751       ui_out_text (uiout, "\nWatchpoint ");
3752       ui_out_field_int (uiout, "wpnum", b->base.number);
3753       ui_out_text (uiout,
3754                    " deleted because the program has left the block in\n\
3755 which its expression is valid.\n");     
3756
3757       /* Make sure the watchpoint's commands aren't executed.  */
3758       decref_counted_command_line (&b->base.commands);
3759       watchpoint_del_at_next_stop (b);
3760
3761       return WP_DELETED;
3762     }
3763 }
3764
3765 /* Return true if it looks like target has stopped due to hitting
3766    breakpoint location BL.  This function does not check if we should
3767    stop, only if BL explains the stop.  */
3768
3769 static int
3770 bpstat_check_location (const struct bp_location *bl,
3771                        struct address_space *aspace, CORE_ADDR bp_addr)
3772 {
3773   struct breakpoint *b = bl->owner;
3774
3775   /* BL is from an existing breakpoint.  */
3776   gdb_assert (b != NULL);
3777
3778   return b->ops->breakpoint_hit (bl, aspace, bp_addr);
3779 }
3780
3781 /* Determine if the watched values have actually changed, and we
3782    should stop.  If not, set BS->stop to 0.  */
3783
3784 static void
3785 bpstat_check_watchpoint (bpstat bs)
3786 {
3787   const struct bp_location *bl;
3788   struct watchpoint *b;
3789
3790   /* BS is built for existing struct breakpoint.  */
3791   bl = bs->bp_location_at;
3792   gdb_assert (bl != NULL);
3793   b = (struct watchpoint *) bs->breakpoint_at;
3794   gdb_assert (b != NULL);
3795
3796     {
3797       int must_check_value = 0;
3798       
3799       if (b->base.type == bp_watchpoint)
3800         /* For a software watchpoint, we must always check the
3801            watched value.  */
3802         must_check_value = 1;
3803       else if (b->watchpoint_triggered == watch_triggered_yes)
3804         /* We have a hardware watchpoint (read, write, or access)
3805            and the target earlier reported an address watched by
3806            this watchpoint.  */
3807         must_check_value = 1;
3808       else if (b->watchpoint_triggered == watch_triggered_unknown
3809                && b->base.type == bp_hardware_watchpoint)
3810         /* We were stopped by a hardware watchpoint, but the target could
3811            not report the data address.  We must check the watchpoint's
3812            value.  Access and read watchpoints are out of luck; without
3813            a data address, we can't figure it out.  */
3814         must_check_value = 1;
3815
3816       if (must_check_value)
3817         {
3818           char *message
3819             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3820                           b->base.number);
3821           struct cleanup *cleanups = make_cleanup (xfree, message);
3822           int e = catch_errors (watchpoint_check, bs, message,
3823                                 RETURN_MASK_ALL);
3824           do_cleanups (cleanups);
3825           switch (e)
3826             {
3827             case WP_DELETED:
3828               /* We've already printed what needs to be printed.  */
3829               bs->print_it = print_it_done;
3830               /* Stop.  */
3831               break;
3832             case WP_IGNORE:
3833               bs->print_it = print_it_noop;
3834               bs->stop = 0;
3835               break;
3836             case WP_VALUE_CHANGED:
3837               if (b->base.type == bp_read_watchpoint)
3838                 {
3839                   /* There are two cases to consider here:
3840
3841                      1. We're watching the triggered memory for reads.
3842                      In that case, trust the target, and always report
3843                      the watchpoint hit to the user.  Even though
3844                      reads don't cause value changes, the value may
3845                      have changed since the last time it was read, and
3846                      since we're not trapping writes, we will not see
3847                      those, and as such we should ignore our notion of
3848                      old value.
3849
3850                      2. We're watching the triggered memory for both
3851                      reads and writes.  There are two ways this may
3852                      happen:
3853
3854                      2.1. This is a target that can't break on data
3855                      reads only, but can break on accesses (reads or
3856                      writes), such as e.g., x86.  We detect this case
3857                      at the time we try to insert read watchpoints.
3858
3859                      2.2. Otherwise, the target supports read
3860                      watchpoints, but, the user set an access or write
3861                      watchpoint watching the same memory as this read
3862                      watchpoint.
3863
3864                      If we're watching memory writes as well as reads,
3865                      ignore watchpoint hits when we find that the
3866                      value hasn't changed, as reads don't cause
3867                      changes.  This still gives false positives when
3868                      the program writes the same value to memory as
3869                      what there was already in memory (we will confuse
3870                      it for a read), but it's much better than
3871                      nothing.  */
3872
3873                   int other_write_watchpoint = 0;
3874
3875                   if (bl->watchpoint_type == hw_read)
3876                     {
3877                       struct breakpoint *other_b;
3878
3879                       ALL_BREAKPOINTS (other_b)
3880                         if (other_b->type == bp_hardware_watchpoint
3881                             || other_b->type == bp_access_watchpoint)
3882                           {
3883                             struct watchpoint *other_w =
3884                               (struct watchpoint *) other_b;
3885
3886                             if (other_w->watchpoint_triggered
3887                                 == watch_triggered_yes)
3888                               {
3889                                 other_write_watchpoint = 1;
3890                                 break;
3891                               }
3892                           }
3893                     }
3894
3895                   if (other_write_watchpoint
3896                       || bl->watchpoint_type == hw_access)
3897                     {
3898                       /* We're watching the same memory for writes,
3899                          and the value changed since the last time we
3900                          updated it, so this trap must be for a write.
3901                          Ignore it.  */
3902                       bs->print_it = print_it_noop;
3903                       bs->stop = 0;
3904                     }
3905                 }
3906               break;
3907             case WP_VALUE_NOT_CHANGED:
3908               if (b->base.type == bp_hardware_watchpoint
3909                   || b->base.type == bp_watchpoint)
3910                 {
3911                   /* Don't stop: write watchpoints shouldn't fire if
3912                      the value hasn't changed.  */
3913                   bs->print_it = print_it_noop;
3914                   bs->stop = 0;
3915                 }
3916               /* Stop.  */
3917               break;
3918             default:
3919               /* Can't happen.  */
3920             case 0:
3921               /* Error from catch_errors.  */
3922               printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
3923               watchpoint_del_at_next_stop (b);
3924               /* We've already printed what needs to be printed.  */
3925               bs->print_it = print_it_done;
3926               break;
3927             }
3928         }
3929       else      /* must_check_value == 0 */
3930         {
3931           /* This is a case where some watchpoint(s) triggered, but
3932              not at the address of this watchpoint, or else no
3933              watchpoint triggered after all.  So don't print
3934              anything for this watchpoint.  */
3935           bs->print_it = print_it_noop;
3936           bs->stop = 0;
3937         }
3938     }
3939 }
3940
3941
3942 /* Check conditions (condition proper, frame, thread and ignore count)
3943    of breakpoint referred to by BS.  If we should not stop for this
3944    breakpoint, set BS->stop to 0.  */
3945
3946 static void
3947 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3948 {
3949   int thread_id = pid_to_thread_id (ptid);
3950   const struct bp_location *bl;
3951   struct breakpoint *b;
3952
3953   /* BS is built for existing struct breakpoint.  */
3954   bl = bs->bp_location_at;
3955   gdb_assert (bl != NULL);
3956   b = bs->breakpoint_at;
3957   gdb_assert (b != NULL);
3958
3959   if (frame_id_p (b->frame_id)
3960       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
3961     bs->stop = 0;
3962   else if (bs->stop)
3963     {
3964       int value_is_zero = 0;
3965       struct expression *cond;
3966
3967       /* Evaluate Python breakpoints that have a "stop"
3968          method implemented.  */
3969       if (b->py_bp_object)
3970         bs->stop = gdbpy_should_stop (b->py_bp_object);
3971
3972       if (is_watchpoint (b))
3973         {
3974           struct watchpoint *w = (struct watchpoint *) b;
3975
3976           cond = w->cond_exp;
3977         }
3978       else
3979         cond = bl->cond;
3980
3981       if (cond && b->disposition != disp_del_at_next_stop)
3982         {
3983           int within_current_scope = 1;
3984           struct watchpoint * w;
3985
3986           /* We use value_mark and value_free_to_mark because it could
3987              be a long time before we return to the command level and
3988              call free_all_values.  We can't call free_all_values
3989              because we might be in the middle of evaluating a
3990              function call.  */
3991           struct value *mark = value_mark ();
3992
3993           if (is_watchpoint (b))
3994             w = (struct watchpoint *) b;
3995           else
3996             w = NULL;
3997
3998           /* Need to select the frame, with all that implies so that
3999              the conditions will have the right context.  Because we
4000              use the frame, we will not see an inlined function's
4001              variables when we arrive at a breakpoint at the start
4002              of the inlined function; the current frame will be the
4003              call site.  */
4004           if (w == NULL || w->cond_exp_valid_block == NULL)
4005             select_frame (get_current_frame ());
4006           else
4007             {
4008               struct frame_info *frame;
4009
4010               /* For local watchpoint expressions, which particular
4011                  instance of a local is being watched matters, so we
4012                  keep track of the frame to evaluate the expression
4013                  in.  To evaluate the condition however, it doesn't
4014                  really matter which instantiation of the function
4015                  where the condition makes sense triggers the
4016                  watchpoint.  This allows an expression like "watch
4017                  global if q > 10" set in `func', catch writes to
4018                  global on all threads that call `func', or catch
4019                  writes on all recursive calls of `func' by a single
4020                  thread.  We simply always evaluate the condition in
4021                  the innermost frame that's executing where it makes
4022                  sense to evaluate the condition.  It seems
4023                  intuitive.  */
4024               frame = block_innermost_frame (w->cond_exp_valid_block);
4025               if (frame != NULL)
4026                 select_frame (frame);
4027               else
4028                 within_current_scope = 0;
4029             }
4030           if (within_current_scope)
4031             value_is_zero
4032               = catch_errors (breakpoint_cond_eval, cond,
4033                               "Error in testing breakpoint condition:\n",
4034                               RETURN_MASK_ALL);
4035           else
4036             {
4037               warning (_("Watchpoint condition cannot be tested "
4038                          "in the current scope"));
4039               /* If we failed to set the right context for this
4040                  watchpoint, unconditionally report it.  */
4041               value_is_zero = 0;
4042             }
4043           /* FIXME-someday, should give breakpoint #.  */
4044           value_free_to_mark (mark);
4045         }
4046
4047       if (cond && value_is_zero)
4048         {
4049           bs->stop = 0;
4050         }
4051       else if (b->thread != -1 && b->thread != thread_id)
4052         {
4053           bs->stop = 0;
4054         }
4055       else if (b->ignore_count > 0)
4056         {
4057           b->ignore_count--;
4058           annotate_ignore_count_change ();
4059           bs->stop = 0;
4060           /* Increase the hit count even though we don't stop.  */
4061           ++(b->hit_count);
4062           observer_notify_breakpoint_modified (b);
4063         }       
4064     }
4065 }
4066
4067
4068 /* Get a bpstat associated with having just stopped at address
4069    BP_ADDR in thread PTID.
4070
4071    Determine whether we stopped at a breakpoint, etc, or whether we
4072    don't understand this stop.  Result is a chain of bpstat's such
4073    that:
4074
4075    if we don't understand the stop, the result is a null pointer.
4076
4077    if we understand why we stopped, the result is not null.
4078
4079    Each element of the chain refers to a particular breakpoint or
4080    watchpoint at which we have stopped.  (We may have stopped for
4081    several reasons concurrently.)
4082
4083    Each element of the chain has valid next, breakpoint_at,
4084    commands, FIXME??? fields.  */
4085
4086 bpstat
4087 bpstat_stop_status (struct address_space *aspace,
4088                     CORE_ADDR bp_addr, ptid_t ptid)
4089 {
4090   struct breakpoint *b = NULL;
4091   struct bp_location *bl;
4092   struct bp_location *loc;
4093   /* First item of allocated bpstat's.  */
4094   bpstat bs_head = NULL, *bs_link = &bs_head;
4095   /* Pointer to the last thing in the chain currently.  */
4096   bpstat bs;
4097   int ix;
4098   int need_remove_insert;
4099   int removed_any;
4100
4101   /* First, build the bpstat chain with locations that explain a
4102      target stop, while being careful to not set the target running,
4103      as that may invalidate locations (in particular watchpoint
4104      locations are recreated).  Resuming will happen here with
4105      breakpoint conditions or watchpoint expressions that include
4106      inferior function calls.  */
4107
4108   ALL_BREAKPOINTS (b)
4109     {
4110       if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4111         continue;
4112
4113       for (bl = b->loc; bl != NULL; bl = bl->next)
4114         {
4115           /* For hardware watchpoints, we look only at the first
4116              location.  The watchpoint_check function will work on the
4117              entire expression, not the individual locations.  For
4118              read watchpoints, the watchpoints_triggered function has
4119              checked all locations already.  */
4120           if (b->type == bp_hardware_watchpoint && bl != b->loc)
4121             break;
4122
4123           if (bl->shlib_disabled)
4124             continue;
4125
4126           if (!bpstat_check_location (bl, aspace, bp_addr))
4127             continue;
4128
4129           /* Come here if it's a watchpoint, or if the break address
4130              matches.  */
4131
4132           bs = bpstat_alloc (bl, &bs_link);     /* Alloc a bpstat to
4133                                                    explain stop.  */
4134
4135           /* Assume we stop.  Should we find a watchpoint that is not
4136              actually triggered, or if the condition of the breakpoint
4137              evaluates as false, we'll reset 'stop' to 0.  */
4138           bs->stop = 1;
4139           bs->print = 1;
4140
4141           /* If this is a scope breakpoint, mark the associated
4142              watchpoint as triggered so that we will handle the
4143              out-of-scope event.  We'll get to the watchpoint next
4144              iteration.  */
4145           if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
4146             {
4147               struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4148
4149               w->watchpoint_triggered = watch_triggered_yes;
4150             }
4151         }
4152     }
4153
4154   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4155     {
4156       if (breakpoint_location_address_match (loc, aspace, bp_addr))
4157         {
4158           bs = bpstat_alloc (loc, &bs_link);
4159           /* For hits of moribund locations, we should just proceed.  */
4160           bs->stop = 0;
4161           bs->print = 0;
4162           bs->print_it = print_it_noop;
4163         }
4164     }
4165
4166   /* Now go through the locations that caused the target to stop, and
4167      check whether we're interested in reporting this stop to higher
4168      layers, or whether we should resume the target transparently.  */
4169
4170   removed_any = 0;
4171
4172   for (bs = bs_head; bs != NULL; bs = bs->next)
4173     {
4174       if (!bs->stop)
4175         continue;
4176
4177       b = bs->breakpoint_at;
4178       b->ops->check_status (bs);
4179       if (bs->stop)
4180         {
4181           bpstat_check_breakpoint_conditions (bs, ptid);
4182
4183           if (bs->stop)
4184             {
4185               ++(b->hit_count);
4186               observer_notify_breakpoint_modified (b);
4187
4188               /* We will stop here.  */
4189               if (b->disposition == disp_disable)
4190                 {
4191                   if (b->enable_state != bp_permanent)
4192                     b->enable_state = bp_disabled;
4193                   removed_any = 1;
4194                 }
4195               if (b->silent)
4196                 bs->print = 0;
4197               bs->commands = b->commands;
4198               incref_counted_command_line (bs->commands);
4199               if (command_line_is_silent (bs->commands
4200                                           ? bs->commands->commands : NULL))
4201                 bs->print = 0;
4202             }
4203
4204           /* Print nothing for this entry if we don't stop or don't print.  */
4205           if (bs->stop == 0 || bs->print == 0)
4206             bs->print_it = print_it_noop;
4207         }
4208     }
4209
4210   /* If we aren't stopping, the value of some hardware watchpoint may
4211      not have changed, but the intermediate memory locations we are
4212      watching may have.  Don't bother if we're stopping; this will get
4213      done later.  */
4214   need_remove_insert = 0;
4215   if (! bpstat_causes_stop (bs_head))
4216     for (bs = bs_head; bs != NULL; bs = bs->next)
4217       if (!bs->stop
4218           && bs->breakpoint_at
4219           && is_hardware_watchpoint (bs->breakpoint_at))
4220         {
4221           struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4222
4223           update_watchpoint (w, 0 /* don't reparse.  */);
4224           need_remove_insert = 1;
4225         }
4226
4227   if (need_remove_insert)
4228     update_global_location_list (1);
4229   else if (removed_any)
4230     update_global_location_list (0);
4231
4232   return bs_head;
4233 }
4234
4235 static void
4236 handle_jit_event (void)
4237 {
4238   struct frame_info *frame;
4239   struct gdbarch *gdbarch;
4240
4241   /* Switch terminal for any messages produced by
4242      breakpoint_re_set.  */
4243   target_terminal_ours_for_output ();
4244
4245   frame = get_current_frame ();
4246   gdbarch = get_frame_arch (frame);
4247
4248   jit_event_handler (gdbarch);
4249
4250   target_terminal_inferior ();
4251 }
4252
4253 /* Prepare WHAT final decision for infrun.  */
4254
4255 /* Decide what infrun needs to do with this bpstat.  */
4256
4257 struct bpstat_what
4258 bpstat_what (bpstat bs_head)
4259 {
4260   struct bpstat_what retval;
4261   /* We need to defer calling `solib_add', as adding new symbols
4262      resets breakpoints, which in turn deletes breakpoint locations,
4263      and hence may clear unprocessed entries in the BS chain.  */
4264   int shlib_event = 0;
4265   int jit_event = 0;
4266   bpstat bs;
4267
4268   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
4269   retval.call_dummy = STOP_NONE;
4270   retval.is_longjmp = 0;
4271
4272   for (bs = bs_head; bs != NULL; bs = bs->next)
4273     {
4274       /* Extract this BS's action.  After processing each BS, we check
4275          if its action overrides all we've seem so far.  */
4276       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4277       enum bptype bptype;
4278
4279       if (bs->breakpoint_at == NULL)
4280         {
4281           /* I suspect this can happen if it was a momentary
4282              breakpoint which has since been deleted.  */
4283           bptype = bp_none;
4284         }
4285       else
4286         bptype = bs->breakpoint_at->type;
4287
4288       switch (bptype)
4289         {
4290         case bp_none:
4291           break;
4292         case bp_breakpoint:
4293         case bp_hardware_breakpoint:
4294         case bp_until:
4295         case bp_finish:
4296           if (bs->stop)
4297             {
4298               if (bs->print)
4299                 this_action = BPSTAT_WHAT_STOP_NOISY;
4300               else
4301                 this_action = BPSTAT_WHAT_STOP_SILENT;
4302             }
4303           else
4304             this_action = BPSTAT_WHAT_SINGLE;
4305           break;
4306         case bp_watchpoint:
4307         case bp_hardware_watchpoint:
4308         case bp_read_watchpoint:
4309         case bp_access_watchpoint:
4310           if (bs->stop)
4311             {
4312               if (bs->print)
4313                 this_action = BPSTAT_WHAT_STOP_NOISY;
4314               else
4315                 this_action = BPSTAT_WHAT_STOP_SILENT;
4316             }
4317           else
4318             {
4319               /* There was a watchpoint, but we're not stopping.
4320                  This requires no further action.  */
4321             }
4322           break;
4323         case bp_longjmp:
4324         case bp_exception:
4325           this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
4326           retval.is_longjmp = bptype == bp_longjmp;
4327           break;
4328         case bp_longjmp_resume:
4329         case bp_exception_resume:
4330           this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
4331           retval.is_longjmp = bptype == bp_longjmp_resume;
4332           break;
4333         case bp_step_resume:
4334           if (bs->stop)
4335             this_action = BPSTAT_WHAT_STEP_RESUME;
4336           else
4337             {
4338               /* It is for the wrong frame.  */
4339               this_action = BPSTAT_WHAT_SINGLE;
4340             }
4341           break;
4342         case bp_hp_step_resume:
4343           if (bs->stop)
4344             this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4345           else
4346             {
4347               /* It is for the wrong frame.  */
4348               this_action = BPSTAT_WHAT_SINGLE;
4349             }
4350           break;
4351         case bp_watchpoint_scope:
4352         case bp_thread_event:
4353         case bp_overlay_event:
4354         case bp_longjmp_master:
4355         case bp_std_terminate_master:
4356         case bp_exception_master:
4357           this_action = BPSTAT_WHAT_SINGLE;
4358           break;
4359         case bp_catchpoint:
4360           if (bs->stop)
4361             {
4362               if (bs->print)
4363                 this_action = BPSTAT_WHAT_STOP_NOISY;
4364               else
4365                 this_action = BPSTAT_WHAT_STOP_SILENT;
4366             }
4367           else
4368             {
4369               /* There was a catchpoint, but we're not stopping.
4370                  This requires no further action.  */
4371             }
4372           break;
4373         case bp_shlib_event:
4374           shlib_event = 1;
4375
4376           /* If requested, stop when the dynamic linker notifies GDB
4377              of events.  This allows the user to get control and place
4378              breakpoints in initializer routines for dynamically
4379              loaded objects (among other things).  */
4380           if (stop_on_solib_events)
4381             this_action = BPSTAT_WHAT_STOP_NOISY;
4382           else
4383             this_action = BPSTAT_WHAT_SINGLE;
4384           break;
4385         case bp_jit_event:
4386           jit_event = 1;
4387           this_action = BPSTAT_WHAT_SINGLE;
4388           break;
4389         case bp_call_dummy:
4390           /* Make sure the action is stop (silent or noisy),
4391              so infrun.c pops the dummy frame.  */
4392           retval.call_dummy = STOP_STACK_DUMMY;
4393           this_action = BPSTAT_WHAT_STOP_SILENT;
4394           break;
4395         case bp_std_terminate:
4396           /* Make sure the action is stop (silent or noisy),
4397              so infrun.c pops the dummy frame.  */
4398           retval.call_dummy = STOP_STD_TERMINATE;
4399           this_action = BPSTAT_WHAT_STOP_SILENT;
4400           break;
4401         case bp_tracepoint:
4402         case bp_fast_tracepoint:
4403         case bp_static_tracepoint:
4404           /* Tracepoint hits should not be reported back to GDB, and
4405              if one got through somehow, it should have been filtered
4406              out already.  */
4407           internal_error (__FILE__, __LINE__,
4408                           _("bpstat_what: tracepoint encountered"));
4409           break;
4410         case bp_gnu_ifunc_resolver:
4411           /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
4412           this_action = BPSTAT_WHAT_SINGLE;
4413           break;
4414         case bp_gnu_ifunc_resolver_return:
4415           /* The breakpoint will be removed, execution will restart from the
4416              PC of the former breakpoint.  */
4417           this_action = BPSTAT_WHAT_KEEP_CHECKING;
4418           break;
4419         default:
4420           internal_error (__FILE__, __LINE__,
4421                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
4422         }
4423
4424       retval.main_action = max (retval.main_action, this_action);
4425     }
4426
4427   /* These operations may affect the bs->breakpoint_at state so they are
4428      delayed after MAIN_ACTION is decided above.  */
4429
4430   if (shlib_event)
4431     {
4432       if (debug_infrun)
4433         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4434
4435       /* Check for any newly added shared libraries if we're supposed
4436          to be adding them automatically.  */
4437
4438       /* Switch terminal for any messages produced by
4439          breakpoint_re_set.  */
4440       target_terminal_ours_for_output ();
4441
4442 #ifdef SOLIB_ADD
4443       SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4444 #else
4445       solib_add (NULL, 0, &current_target, auto_solib_add);
4446 #endif
4447
4448       target_terminal_inferior ();
4449     }
4450
4451   if (jit_event)
4452     {
4453       if (debug_infrun)
4454         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4455
4456       handle_jit_event ();
4457     }
4458
4459   for (bs = bs_head; bs != NULL; bs = bs->next)
4460     {
4461       struct breakpoint *b = bs->breakpoint_at;
4462
4463       if (b == NULL)
4464         continue;
4465       switch (b->type)
4466         {
4467         case bp_gnu_ifunc_resolver:
4468           gnu_ifunc_resolver_stop (b);
4469           break;
4470         case bp_gnu_ifunc_resolver_return:
4471           gnu_ifunc_resolver_return_stop (b);
4472           break;
4473         }
4474     }
4475
4476   return retval;
4477 }
4478
4479 /* Nonzero if we should step constantly (e.g. watchpoints on machines
4480    without hardware support).  This isn't related to a specific bpstat,
4481    just to things like whether watchpoints are set.  */
4482
4483 int
4484 bpstat_should_step (void)
4485 {
4486   struct breakpoint *b;
4487
4488   ALL_BREAKPOINTS (b)
4489     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4490       return 1;
4491   return 0;
4492 }
4493
4494 int
4495 bpstat_causes_stop (bpstat bs)
4496 {
4497   for (; bs != NULL; bs = bs->next)
4498     if (bs->stop)
4499       return 1;
4500
4501   return 0;
4502 }
4503
4504 \f
4505
4506 /* Compute a string of spaces suitable to indent the next line
4507    so it starts at the position corresponding to the table column
4508    named COL_NAME in the currently active table of UIOUT.  */
4509
4510 static char *
4511 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4512 {
4513   static char wrap_indent[80];
4514   int i, total_width, width, align;
4515   char *text;
4516
4517   total_width = 0;
4518   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4519     {
4520       if (strcmp (text, col_name) == 0)
4521         {
4522           gdb_assert (total_width < sizeof wrap_indent);
4523           memset (wrap_indent, ' ', total_width);
4524           wrap_indent[total_width] = 0;
4525
4526           return wrap_indent;
4527         }
4528
4529       total_width += width + 1;
4530     }
4531
4532   return NULL;
4533 }
4534
4535 /* Print the LOC location out of the list of B->LOC locations.  */
4536
4537 static void
4538 print_breakpoint_location (struct breakpoint *b,
4539                            struct bp_location *loc)
4540 {
4541   struct ui_out *uiout = current_uiout;
4542   struct cleanup *old_chain = save_current_program_space ();
4543
4544   if (loc != NULL && loc->shlib_disabled)
4545     loc = NULL;
4546
4547   if (loc != NULL)
4548     set_current_program_space (loc->pspace);
4549
4550   if (b->display_canonical)
4551     ui_out_field_string (uiout, "what", b->addr_string);
4552   else if (b->source_file && loc)
4553     {
4554       struct symbol *sym 
4555         = find_pc_sect_function (loc->address, loc->section);
4556       if (sym)
4557         {
4558           ui_out_text (uiout, "in ");
4559           ui_out_field_string (uiout, "func",
4560                                SYMBOL_PRINT_NAME (sym));
4561           ui_out_text (uiout, " ");
4562           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4563           ui_out_text (uiout, "at ");
4564         }
4565       ui_out_field_string (uiout, "file", b->source_file);
4566       ui_out_text (uiout, ":");
4567       
4568       if (ui_out_is_mi_like_p (uiout))
4569         {
4570           struct symtab_and_line sal = find_pc_line (loc->address, 0);
4571           char *fullname = symtab_to_fullname (sal.symtab);
4572           
4573           if (fullname)
4574             ui_out_field_string (uiout, "fullname", fullname);
4575         }
4576       
4577       ui_out_field_int (uiout, "line", b->line_number);
4578     }
4579   else if (loc)
4580     {
4581       struct ui_stream *stb = ui_out_stream_new (uiout);
4582       struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4583
4584       print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4585                               demangle, "");
4586       ui_out_field_stream (uiout, "at", stb);
4587
4588       do_cleanups (stb_chain);
4589     }
4590   else
4591     ui_out_field_string (uiout, "pending", b->addr_string);
4592
4593   do_cleanups (old_chain);
4594 }
4595
4596 static const char *
4597 bptype_string (enum bptype type)
4598 {
4599   struct ep_type_description
4600     {
4601       enum bptype type;
4602       char *description;
4603     };
4604   static struct ep_type_description bptypes[] =
4605   {
4606     {bp_none, "?deleted?"},
4607     {bp_breakpoint, "breakpoint"},
4608     {bp_hardware_breakpoint, "hw breakpoint"},
4609     {bp_until, "until"},
4610     {bp_finish, "finish"},
4611     {bp_watchpoint, "watchpoint"},
4612     {bp_hardware_watchpoint, "hw watchpoint"},
4613     {bp_read_watchpoint, "read watchpoint"},
4614     {bp_access_watchpoint, "acc watchpoint"},
4615     {bp_longjmp, "longjmp"},
4616     {bp_longjmp_resume, "longjmp resume"},
4617     {bp_exception, "exception"},
4618     {bp_exception_resume, "exception resume"},
4619     {bp_step_resume, "step resume"},
4620     {bp_hp_step_resume, "high-priority step resume"},
4621     {bp_watchpoint_scope, "watchpoint scope"},
4622     {bp_call_dummy, "call dummy"},
4623     {bp_std_terminate, "std::terminate"},
4624     {bp_shlib_event, "shlib events"},
4625     {bp_thread_event, "thread events"},
4626     {bp_overlay_event, "overlay events"},
4627     {bp_longjmp_master, "longjmp master"},
4628     {bp_std_terminate_master, "std::terminate master"},
4629     {bp_exception_master, "exception master"},
4630     {bp_catchpoint, "catchpoint"},
4631     {bp_tracepoint, "tracepoint"},
4632     {bp_fast_tracepoint, "fast tracepoint"},
4633     {bp_static_tracepoint, "static tracepoint"},
4634     {bp_jit_event, "jit events"},
4635     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4636     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
4637   };
4638
4639   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4640       || ((int) type != bptypes[(int) type].type))
4641     internal_error (__FILE__, __LINE__,
4642                     _("bptypes table does not describe type #%d."),
4643                     (int) type);
4644
4645   return bptypes[(int) type].description;
4646 }
4647
4648 /* Print B to gdb_stdout.  */
4649
4650 static void
4651 print_one_breakpoint_location (struct breakpoint *b,
4652                                struct bp_location *loc,
4653                                int loc_number,
4654                                struct bp_location **last_loc,
4655                                int allflag)
4656 {
4657   struct command_line *l;
4658   static char bpenables[] = "nynny";
4659
4660   struct ui_out *uiout = current_uiout;
4661   int header_of_multiple = 0;
4662   int part_of_multiple = (loc != NULL);
4663   struct value_print_options opts;
4664
4665   get_user_print_options (&opts);
4666
4667   gdb_assert (!loc || loc_number != 0);
4668   /* See comment in print_one_breakpoint concerning treatment of
4669      breakpoints with single disabled location.  */
4670   if (loc == NULL 
4671       && (b->loc != NULL 
4672           && (b->loc->next != NULL || !b->loc->enabled)))
4673     header_of_multiple = 1;
4674   if (loc == NULL)
4675     loc = b->loc;
4676
4677   annotate_record ();
4678
4679   /* 1 */
4680   annotate_field (0);
4681   if (part_of_multiple)
4682     {
4683       char *formatted;
4684       formatted = xstrprintf ("%d.%d", b->number, loc_number);
4685       ui_out_field_string (uiout, "number", formatted);
4686       xfree (formatted);
4687     }
4688   else
4689     {
4690       ui_out_field_int (uiout, "number", b->number);
4691     }
4692
4693   /* 2 */
4694   annotate_field (1);
4695   if (part_of_multiple)
4696     ui_out_field_skip (uiout, "type");
4697   else
4698     ui_out_field_string (uiout, "type", bptype_string (b->type));
4699
4700   /* 3 */
4701   annotate_field (2);
4702   if (part_of_multiple)
4703     ui_out_field_skip (uiout, "disp");
4704   else
4705     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
4706
4707
4708   /* 4 */
4709   annotate_field (3);
4710   if (part_of_multiple)
4711     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
4712   else
4713     ui_out_field_fmt (uiout, "enabled", "%c", 
4714                       bpenables[(int) b->enable_state]);
4715   ui_out_spaces (uiout, 2);
4716
4717   
4718   /* 5 and 6 */
4719   if (b->ops != NULL && b->ops->print_one != NULL)
4720     {
4721       /* Although the print_one can possibly print all locations,
4722          calling it here is not likely to get any nice result.  So,
4723          make sure there's just one location.  */
4724       gdb_assert (b->loc == NULL || b->loc->next == NULL);
4725       b->ops->print_one (b, last_loc);
4726     }
4727   else
4728     switch (b->type)
4729       {
4730       case bp_none:
4731         internal_error (__FILE__, __LINE__,
4732                         _("print_one_breakpoint: bp_none encountered\n"));
4733         break;
4734
4735       case bp_watchpoint:
4736       case bp_hardware_watchpoint:
4737       case bp_read_watchpoint:
4738       case bp_access_watchpoint:
4739         {
4740           struct watchpoint *w = (struct watchpoint *) b;
4741
4742           /* Field 4, the address, is omitted (which makes the columns
4743              not line up too nicely with the headers, but the effect
4744              is relatively readable).  */
4745           if (opts.addressprint)
4746             ui_out_field_skip (uiout, "addr");
4747           annotate_field (5);
4748           ui_out_field_string (uiout, "what", w->exp_string);
4749         }
4750         break;
4751
4752       case bp_breakpoint:
4753       case bp_hardware_breakpoint:
4754       case bp_until:
4755       case bp_finish:
4756       case bp_longjmp:
4757       case bp_longjmp_resume:
4758       case bp_exception:
4759       case bp_exception_resume:
4760       case bp_step_resume:
4761       case bp_hp_step_resume:
4762       case bp_watchpoint_scope:
4763       case bp_call_dummy:
4764       case bp_std_terminate:
4765       case bp_shlib_event:
4766       case bp_thread_event:
4767       case bp_overlay_event:
4768       case bp_longjmp_master:
4769       case bp_std_terminate_master:
4770       case bp_exception_master:
4771       case bp_tracepoint:
4772       case bp_fast_tracepoint:
4773       case bp_static_tracepoint:
4774       case bp_jit_event:
4775       case bp_gnu_ifunc_resolver:
4776       case bp_gnu_ifunc_resolver_return:
4777         if (opts.addressprint)
4778           {
4779             annotate_field (4);
4780             if (header_of_multiple)
4781               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
4782             else if (b->loc == NULL || loc->shlib_disabled)
4783               ui_out_field_string (uiout, "addr", "<PENDING>");
4784             else
4785               ui_out_field_core_addr (uiout, "addr",
4786                                       loc->gdbarch, loc->address);
4787           }
4788         annotate_field (5);
4789         if (!header_of_multiple)
4790           print_breakpoint_location (b, loc);
4791         if (b->loc)
4792           *last_loc = b->loc;
4793         break;
4794       }
4795
4796
4797   /* For backward compatibility, don't display inferiors unless there
4798      are several.  */
4799   if (loc != NULL
4800       && !header_of_multiple
4801       && (allflag
4802           || (!gdbarch_has_global_breakpoints (target_gdbarch)
4803               && (number_of_program_spaces () > 1
4804                   || number_of_inferiors () > 1)
4805               /* LOC is for existing B, it cannot be in
4806                  moribund_locations and thus having NULL OWNER.  */
4807               && loc->owner->type != bp_catchpoint)))
4808     {
4809       struct inferior *inf;
4810       int first = 1;
4811
4812       for (inf = inferior_list; inf != NULL; inf = inf->next)
4813         {
4814           if (inf->pspace == loc->pspace)
4815             {
4816               if (first)
4817                 {
4818                   first = 0;
4819                   ui_out_text (uiout, " inf ");
4820                 }
4821               else
4822                 ui_out_text (uiout, ", ");
4823               ui_out_text (uiout, plongest (inf->num));
4824             }
4825         }
4826     }
4827
4828   if (!part_of_multiple)
4829     {
4830       if (b->thread != -1)
4831         {
4832           /* FIXME: This seems to be redundant and lost here; see the
4833              "stop only in" line a little further down.  */
4834           ui_out_text (uiout, " thread ");
4835           ui_out_field_int (uiout, "thread", b->thread);
4836         }
4837       else if (b->task != 0)
4838         {
4839           ui_out_text (uiout, " task ");
4840           ui_out_field_int (uiout, "task", b->task);
4841         }
4842     }
4843
4844   ui_out_text (uiout, "\n");
4845
4846   if (!part_of_multiple)
4847     b->ops->print_one_detail (b, uiout);
4848
4849   if (part_of_multiple && frame_id_p (b->frame_id))
4850     {
4851       annotate_field (6);
4852       ui_out_text (uiout, "\tstop only in stack frame at ");
4853       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
4854          the frame ID.  */
4855       ui_out_field_core_addr (uiout, "frame",
4856                               b->gdbarch, b->frame_id.stack_addr);
4857       ui_out_text (uiout, "\n");
4858     }
4859   
4860   if (!part_of_multiple && b->cond_string)
4861     {
4862       annotate_field (7);
4863       if (is_tracepoint (b))
4864         ui_out_text (uiout, "\ttrace only if ");
4865       else
4866         ui_out_text (uiout, "\tstop only if ");
4867       ui_out_field_string (uiout, "cond", b->cond_string);
4868       ui_out_text (uiout, "\n");
4869     }
4870
4871   if (!part_of_multiple && b->thread != -1)
4872     {
4873       /* FIXME should make an annotation for this.  */
4874       ui_out_text (uiout, "\tstop only in thread ");
4875       ui_out_field_int (uiout, "thread", b->thread);
4876       ui_out_text (uiout, "\n");
4877     }
4878   
4879   if (!part_of_multiple && b->hit_count)
4880     {
4881       /* FIXME should make an annotation for this.  */
4882       if (ep_is_catchpoint (b))
4883         ui_out_text (uiout, "\tcatchpoint");
4884       else
4885         ui_out_text (uiout, "\tbreakpoint");
4886       ui_out_text (uiout, " already hit ");
4887       ui_out_field_int (uiout, "times", b->hit_count);
4888       if (b->hit_count == 1)
4889         ui_out_text (uiout, " time\n");
4890       else
4891         ui_out_text (uiout, " times\n");
4892     }
4893   
4894   /* Output the count also if it is zero, but only if this is mi.
4895      FIXME: Should have a better test for this.  */
4896   if (ui_out_is_mi_like_p (uiout))
4897     if (!part_of_multiple && b->hit_count == 0)
4898       ui_out_field_int (uiout, "times", b->hit_count);
4899
4900   if (!part_of_multiple && b->ignore_count)
4901     {
4902       annotate_field (8);
4903       ui_out_text (uiout, "\tignore next ");
4904       ui_out_field_int (uiout, "ignore", b->ignore_count);
4905       ui_out_text (uiout, " hits\n");
4906     }
4907
4908   l = b->commands ? b->commands->commands : NULL;
4909   if (!part_of_multiple && l)
4910     {
4911       struct cleanup *script_chain;
4912
4913       annotate_field (9);
4914       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
4915       print_command_lines (uiout, l, 4);
4916       do_cleanups (script_chain);
4917     }
4918
4919   if (is_tracepoint (b))
4920     {
4921       struct tracepoint *t = (struct tracepoint *) b;
4922
4923       if (!part_of_multiple && t->pass_count)
4924         {
4925           annotate_field (10);
4926           ui_out_text (uiout, "\tpass count ");
4927           ui_out_field_int (uiout, "pass", t->pass_count);
4928           ui_out_text (uiout, " \n");
4929         }
4930     }
4931
4932   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4933     {
4934       if (is_watchpoint (b))
4935         {
4936           struct watchpoint *w = (struct watchpoint *) b;
4937
4938           ui_out_field_string (uiout, "original-location", w->exp_string);
4939         }
4940       else if (b->addr_string)
4941         ui_out_field_string (uiout, "original-location", b->addr_string);
4942     }
4943 }
4944
4945 static void
4946 print_one_breakpoint (struct breakpoint *b,
4947                       struct bp_location **last_loc, 
4948                       int allflag)
4949 {
4950   struct cleanup *bkpt_chain;
4951   struct ui_out *uiout = current_uiout;
4952
4953   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4954
4955   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
4956   do_cleanups (bkpt_chain);
4957
4958   /* If this breakpoint has custom print function,
4959      it's already printed.  Otherwise, print individual
4960      locations, if any.  */
4961   if (b->ops == NULL || b->ops->print_one == NULL)
4962     {
4963       /* If breakpoint has a single location that is disabled, we
4964          print it as if it had several locations, since otherwise it's
4965          hard to represent "breakpoint enabled, location disabled"
4966          situation.
4967
4968          Note that while hardware watchpoints have several locations
4969          internally, that's not a property exposed to user.  */
4970       if (b->loc 
4971           && !is_hardware_watchpoint (b)
4972           && (b->loc->next || !b->loc->enabled))
4973         {
4974           struct bp_location *loc;
4975           int n = 1;
4976
4977           for (loc = b->loc; loc; loc = loc->next, ++n)
4978             {
4979               struct cleanup *inner2 =
4980                 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
4981               print_one_breakpoint_location (b, loc, n, last_loc, allflag);
4982               do_cleanups (inner2);
4983             }
4984         }
4985     }
4986 }
4987
4988 static int
4989 breakpoint_address_bits (struct breakpoint *b)
4990 {
4991   int print_address_bits = 0;
4992   struct bp_location *loc;
4993
4994   for (loc = b->loc; loc; loc = loc->next)
4995     {
4996       int addr_bit;
4997
4998       /* Software watchpoints that aren't watching memory don't have
4999          an address to print.  */
5000       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5001         continue;
5002
5003       addr_bit = gdbarch_addr_bit (loc->gdbarch);
5004       if (addr_bit > print_address_bits)
5005         print_address_bits = addr_bit;
5006     }
5007
5008   return print_address_bits;
5009 }
5010
5011 struct captured_breakpoint_query_args
5012   {
5013     int bnum;
5014   };
5015
5016 static int
5017 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5018 {
5019   struct captured_breakpoint_query_args *args = data;
5020   struct breakpoint *b;
5021   struct bp_location *dummy_loc = NULL;
5022
5023   ALL_BREAKPOINTS (b)
5024     {
5025       if (args->bnum == b->number)
5026         {
5027           print_one_breakpoint (b, &dummy_loc, 0);
5028           return GDB_RC_OK;
5029         }
5030     }
5031   return GDB_RC_NONE;
5032 }
5033
5034 enum gdb_rc
5035 gdb_breakpoint_query (struct ui_out *uiout, int bnum, 
5036                       char **error_message)
5037 {
5038   struct captured_breakpoint_query_args args;
5039
5040   args.bnum = bnum;
5041   /* For the moment we don't trust print_one_breakpoint() to not throw
5042      an error.  */
5043   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5044                                  error_message, RETURN_MASK_ALL) < 0)
5045     return GDB_RC_FAIL;
5046   else
5047     return GDB_RC_OK;
5048 }
5049
5050 /* Return true if this breakpoint was set by the user, false if it is
5051    internal or momentary.  */
5052
5053 int
5054 user_breakpoint_p (struct breakpoint *b)
5055 {
5056   return b->number > 0;
5057 }
5058
5059 /* Print information on user settable breakpoint (watchpoint, etc)
5060    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
5061    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
5062    FILTER is non-NULL, call it on each breakpoint and only include the
5063    ones for which it returns non-zero.  Return the total number of
5064    breakpoints listed.  */
5065
5066 static int
5067 breakpoint_1 (char *args, int allflag, 
5068               int (*filter) (const struct breakpoint *))
5069 {
5070   struct breakpoint *b;
5071   struct bp_location *last_loc = NULL;
5072   int nr_printable_breakpoints;
5073   struct cleanup *bkpttbl_chain;
5074   struct value_print_options opts;
5075   int print_address_bits = 0;
5076   int print_type_col_width = 14;
5077   struct ui_out *uiout = current_uiout;
5078
5079   get_user_print_options (&opts);
5080
5081   /* Compute the number of rows in the table, as well as the size
5082      required for address fields.  */
5083   nr_printable_breakpoints = 0;
5084   ALL_BREAKPOINTS (b)
5085     {
5086       /* If we have a filter, only list the breakpoints it accepts.  */
5087       if (filter && !filter (b))
5088         continue;
5089
5090       /* If we have an "args" string, it is a list of breakpoints to 
5091          accept.  Skip the others.  */
5092       if (args != NULL && *args != '\0')
5093         {
5094           if (allflag && parse_and_eval_long (args) != b->number)
5095             continue;
5096           if (!allflag && !number_is_in_list (args, b->number))
5097             continue;
5098         }
5099
5100       if (allflag || user_breakpoint_p (b))
5101         {
5102           int addr_bit, type_len;
5103
5104           addr_bit = breakpoint_address_bits (b);
5105           if (addr_bit > print_address_bits)
5106             print_address_bits = addr_bit;
5107
5108           type_len = strlen (bptype_string (b->type));
5109           if (type_len > print_type_col_width)
5110             print_type_col_width = type_len;
5111
5112           nr_printable_breakpoints++;
5113         }
5114     }
5115
5116   if (opts.addressprint)
5117     bkpttbl_chain 
5118       = make_cleanup_ui_out_table_begin_end (uiout, 6,
5119                                              nr_printable_breakpoints,
5120                                              "BreakpointTable");
5121   else
5122     bkpttbl_chain 
5123       = make_cleanup_ui_out_table_begin_end (uiout, 5,
5124                                              nr_printable_breakpoints,
5125                                              "BreakpointTable");
5126
5127   if (nr_printable_breakpoints > 0)
5128     annotate_breakpoints_headers ();
5129   if (nr_printable_breakpoints > 0)
5130     annotate_field (0);
5131   ui_out_table_header (uiout, 7, ui_left, "number", "Num");     /* 1 */
5132   if (nr_printable_breakpoints > 0)
5133     annotate_field (1);
5134   ui_out_table_header (uiout, print_type_col_width, ui_left,
5135                        "type", "Type");                         /* 2 */
5136   if (nr_printable_breakpoints > 0)
5137     annotate_field (2);
5138   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");      /* 3 */
5139   if (nr_printable_breakpoints > 0)
5140     annotate_field (3);
5141   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
5142   if (opts.addressprint)
5143     {
5144       if (nr_printable_breakpoints > 0)
5145         annotate_field (4);
5146       if (print_address_bits <= 32)
5147         ui_out_table_header (uiout, 10, ui_left, 
5148                              "addr", "Address");                /* 5 */
5149       else
5150         ui_out_table_header (uiout, 18, ui_left, 
5151                              "addr", "Address");                /* 5 */
5152     }
5153   if (nr_printable_breakpoints > 0)
5154     annotate_field (5);
5155   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
5156   ui_out_table_body (uiout);
5157   if (nr_printable_breakpoints > 0)
5158     annotate_breakpoints_table ();
5159
5160   ALL_BREAKPOINTS (b)
5161     {
5162       QUIT;
5163       /* If we have a filter, only list the breakpoints it accepts.  */
5164       if (filter && !filter (b))
5165         continue;
5166
5167       /* If we have an "args" string, it is a list of breakpoints to 
5168          accept.  Skip the others.  */
5169
5170       if (args != NULL && *args != '\0')
5171         {
5172           if (allflag)  /* maintenance info breakpoint */
5173             {
5174               if (parse_and_eval_long (args) != b->number)
5175                 continue;
5176             }
5177           else          /* all others */
5178             {
5179               if (!number_is_in_list (args, b->number))
5180                 continue;
5181             }
5182         }
5183       /* We only print out user settable breakpoints unless the
5184          allflag is set.  */
5185       if (allflag || user_breakpoint_p (b))
5186         print_one_breakpoint (b, &last_loc, allflag);
5187     }
5188
5189   do_cleanups (bkpttbl_chain);
5190
5191   if (nr_printable_breakpoints == 0)
5192     {
5193       /* If there's a filter, let the caller decide how to report
5194          empty list.  */
5195       if (!filter)
5196         {
5197           if (args == NULL || *args == '\0')
5198             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5199           else
5200             ui_out_message (uiout, 0, 
5201                             "No breakpoint or watchpoint matching '%s'.\n",
5202                             args);
5203         }
5204     }
5205   else
5206     {
5207       if (last_loc && !server_command)
5208         set_next_address (last_loc->gdbarch, last_loc->address);
5209     }
5210
5211   /* FIXME?  Should this be moved up so that it is only called when
5212      there have been breakpoints? */
5213   annotate_breakpoints_table_end ();
5214
5215   return nr_printable_breakpoints;
5216 }
5217
5218 /* Display the value of default-collect in a way that is generally
5219    compatible with the breakpoint list.  */
5220
5221 static void
5222 default_collect_info (void)
5223 {
5224   struct ui_out *uiout = current_uiout;
5225
5226   /* If it has no value (which is frequently the case), say nothing; a
5227      message like "No default-collect." gets in user's face when it's
5228      not wanted.  */
5229   if (!*default_collect)
5230     return;
5231
5232   /* The following phrase lines up nicely with per-tracepoint collect
5233      actions.  */
5234   ui_out_text (uiout, "default collect ");
5235   ui_out_field_string (uiout, "default-collect", default_collect);
5236   ui_out_text (uiout, " \n");
5237 }
5238   
5239 static void
5240 breakpoints_info (char *args, int from_tty)
5241 {
5242   breakpoint_1 (args, 0, NULL);
5243
5244   default_collect_info ();
5245 }
5246
5247 static void
5248 watchpoints_info (char *args, int from_tty)
5249 {
5250   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
5251   struct ui_out *uiout = current_uiout;
5252
5253   if (num_printed == 0)
5254     {
5255       if (args == NULL || *args == '\0')
5256         ui_out_message (uiout, 0, "No watchpoints.\n");
5257       else
5258         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
5259     }
5260 }
5261
5262 static void
5263 maintenance_info_breakpoints (char *args, int from_tty)
5264 {
5265   breakpoint_1 (args, 1, NULL);
5266
5267   default_collect_info ();
5268 }
5269
5270 static int
5271 breakpoint_has_pc (struct breakpoint *b,
5272                    struct program_space *pspace,
5273                    CORE_ADDR pc, struct obj_section *section)
5274 {
5275   struct bp_location *bl = b->loc;
5276
5277   for (; bl; bl = bl->next)
5278     {
5279       if (bl->pspace == pspace
5280           && bl->address == pc
5281           && (!overlay_debugging || bl->section == section))
5282         return 1;         
5283     }
5284   return 0;
5285 }
5286
5287 /* Print a message describing any user-breakpoints set at PC.  This
5288    concerns with logical breakpoints, so we match program spaces, not
5289    address spaces.  */
5290
5291 static void
5292 describe_other_breakpoints (struct gdbarch *gdbarch,
5293                             struct program_space *pspace, CORE_ADDR pc,
5294                             struct obj_section *section, int thread)
5295 {
5296   int others = 0;
5297   struct breakpoint *b;
5298
5299   ALL_BREAKPOINTS (b)
5300     others += (user_breakpoint_p (b)
5301                && breakpoint_has_pc (b, pspace, pc, section));
5302   if (others > 0)
5303     {
5304       if (others == 1)
5305         printf_filtered (_("Note: breakpoint "));
5306       else /* if (others == ???) */
5307         printf_filtered (_("Note: breakpoints "));
5308       ALL_BREAKPOINTS (b)
5309         if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
5310           {
5311             others--;
5312             printf_filtered ("%d", b->number);
5313             if (b->thread == -1 && thread != -1)
5314               printf_filtered (" (all threads)");
5315             else if (b->thread != -1)
5316               printf_filtered (" (thread %d)", b->thread);
5317             printf_filtered ("%s%s ",
5318                              ((b->enable_state == bp_disabled
5319                                || b->enable_state == bp_call_disabled
5320                                || b->enable_state == bp_startup_disabled)
5321                               ? " (disabled)"
5322                               : b->enable_state == bp_permanent 
5323                               ? " (permanent)"
5324                               : ""),
5325                              (others > 1) ? "," 
5326                              : ((others == 1) ? " and" : ""));
5327           }
5328       printf_filtered (_("also set at pc "));
5329       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
5330       printf_filtered (".\n");
5331     }
5332 }
5333 \f
5334
5335 /* Return true iff it is meaningful to use the address member of
5336    BPT.  For some breakpoint types, the address member is irrelevant
5337    and it makes no sense to attempt to compare it to other addresses
5338    (or use it for any other purpose either).
5339
5340    More specifically, each of the following breakpoint types will
5341    always have a zero valued address and we don't want to mark
5342    breakpoints of any of these types to be a duplicate of an actual
5343    breakpoint at address zero:
5344
5345       bp_watchpoint
5346       bp_catchpoint
5347
5348 */
5349
5350 static int
5351 breakpoint_address_is_meaningful (struct breakpoint *bpt)
5352 {
5353   enum bptype type = bpt->type;
5354
5355   return (type != bp_watchpoint && type != bp_catchpoint);
5356 }
5357
5358 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5359    true if LOC1 and LOC2 represent the same watchpoint location.  */
5360
5361 static int
5362 watchpoint_locations_match (struct bp_location *loc1, 
5363                             struct bp_location *loc2)
5364 {
5365   struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5366   struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5367
5368   /* Both of them must exist.  */
5369   gdb_assert (w1 != NULL);
5370   gdb_assert (w2 != NULL);
5371
5372   /* If the target can evaluate the condition expression in hardware,
5373      then we we need to insert both watchpoints even if they are at
5374      the same place.  Otherwise the watchpoint will only trigger when
5375      the condition of whichever watchpoint was inserted evaluates to
5376      true, not giving a chance for GDB to check the condition of the
5377      other watchpoint.  */
5378   if ((w1->cond_exp
5379        && target_can_accel_watchpoint_condition (loc1->address, 
5380                                                  loc1->length,
5381                                                  loc1->watchpoint_type,
5382                                                  w1->cond_exp))
5383       || (w2->cond_exp
5384           && target_can_accel_watchpoint_condition (loc2->address, 
5385                                                     loc2->length,
5386                                                     loc2->watchpoint_type,
5387                                                     w2->cond_exp)))
5388     return 0;
5389
5390   /* Note that this checks the owner's type, not the location's.  In
5391      case the target does not support read watchpoints, but does
5392      support access watchpoints, we'll have bp_read_watchpoint
5393      watchpoints with hw_access locations.  Those should be considered
5394      duplicates of hw_read locations.  The hw_read locations will
5395      become hw_access locations later.  */
5396   return (loc1->owner->type == loc2->owner->type
5397           && loc1->pspace->aspace == loc2->pspace->aspace
5398           && loc1->address == loc2->address
5399           && loc1->length == loc2->length);
5400 }
5401
5402 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5403    same breakpoint location.  In most targets, this can only be true
5404    if ASPACE1 matches ASPACE2.  On targets that have global
5405    breakpoints, the address space doesn't really matter.  */
5406
5407 static int
5408 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5409                           struct address_space *aspace2, CORE_ADDR addr2)
5410 {
5411   return ((gdbarch_has_global_breakpoints (target_gdbarch)
5412            || aspace1 == aspace2)
5413           && addr1 == addr2);
5414 }
5415
5416 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5417    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
5418    matches ASPACE2.  On targets that have global breakpoints, the address
5419    space doesn't really matter.  */
5420
5421 static int
5422 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5423                                 int len1, struct address_space *aspace2,
5424                                 CORE_ADDR addr2)
5425 {
5426   return ((gdbarch_has_global_breakpoints (target_gdbarch)
5427            || aspace1 == aspace2)
5428           && addr2 >= addr1 && addr2 < addr1 + len1);
5429 }
5430
5431 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
5432    a ranged breakpoint.  In most targets, a match happens only if ASPACE
5433    matches the breakpoint's address space.  On targets that have global
5434    breakpoints, the address space doesn't really matter.  */
5435
5436 static int
5437 breakpoint_location_address_match (struct bp_location *bl,
5438                                    struct address_space *aspace,
5439                                    CORE_ADDR addr)
5440 {
5441   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5442                                     aspace, addr)
5443           || (bl->length
5444               && breakpoint_address_match_range (bl->pspace->aspace,
5445                                                  bl->address, bl->length,
5446                                                  aspace, addr)));
5447 }
5448
5449 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
5450    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5451    represent the same location.  */
5452
5453 static int
5454 breakpoint_locations_match (struct bp_location *loc1, 
5455                             struct bp_location *loc2)
5456 {
5457   int hw_point1, hw_point2;
5458
5459   /* Both of them must not be in moribund_locations.  */
5460   gdb_assert (loc1->owner != NULL);
5461   gdb_assert (loc2->owner != NULL);
5462
5463   hw_point1 = is_hardware_watchpoint (loc1->owner);
5464   hw_point2 = is_hardware_watchpoint (loc2->owner);
5465
5466   if (hw_point1 != hw_point2)
5467     return 0;
5468   else if (hw_point1)
5469     return watchpoint_locations_match (loc1, loc2);
5470   else
5471     /* We compare bp_location.length in order to cover ranged breakpoints.  */
5472     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5473                                      loc2->pspace->aspace, loc2->address)
5474             && loc1->length == loc2->length);
5475 }
5476
5477 static void
5478 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5479                                int bnum, int have_bnum)
5480 {
5481   /* The longest string possibly returned by hex_string_custom
5482      is 50 chars.  These must be at least that big for safety.  */
5483   char astr1[64];
5484   char astr2[64];
5485
5486   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5487   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
5488   if (have_bnum)
5489     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
5490              bnum, astr1, astr2);
5491   else
5492     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
5493 }
5494
5495 /* Adjust a breakpoint's address to account for architectural
5496    constraints on breakpoint placement.  Return the adjusted address.
5497    Note: Very few targets require this kind of adjustment.  For most
5498    targets, this function is simply the identity function.  */
5499
5500 static CORE_ADDR
5501 adjust_breakpoint_address (struct gdbarch *gdbarch,
5502                            CORE_ADDR bpaddr, enum bptype bptype)
5503 {
5504   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
5505     {
5506       /* Very few targets need any kind of breakpoint adjustment.  */
5507       return bpaddr;
5508     }
5509   else if (bptype == bp_watchpoint
5510            || bptype == bp_hardware_watchpoint
5511            || bptype == bp_read_watchpoint
5512            || bptype == bp_access_watchpoint
5513            || bptype == bp_catchpoint)
5514     {
5515       /* Watchpoints and the various bp_catch_* eventpoints should not
5516          have their addresses modified.  */
5517       return bpaddr;
5518     }
5519   else
5520     {
5521       CORE_ADDR adjusted_bpaddr;
5522
5523       /* Some targets have architectural constraints on the placement
5524          of breakpoint instructions.  Obtain the adjusted address.  */
5525       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
5526
5527       /* An adjusted breakpoint address can significantly alter
5528          a user's expectations.  Print a warning if an adjustment
5529          is required.  */
5530       if (adjusted_bpaddr != bpaddr)
5531         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5532
5533       return adjusted_bpaddr;
5534     }
5535 }
5536
5537 void
5538 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5539                   struct breakpoint *owner)
5540 {
5541   memset (loc, 0, sizeof (*loc));
5542
5543   gdb_assert (ops != NULL);
5544
5545   loc->ops = ops;
5546   loc->owner = owner;
5547   loc->cond = NULL;
5548   loc->shlib_disabled = 0;
5549   loc->enabled = 1;
5550
5551   switch (owner->type)
5552     {
5553     case bp_breakpoint:
5554     case bp_until:
5555     case bp_finish:
5556     case bp_longjmp:
5557     case bp_longjmp_resume:
5558     case bp_exception:
5559     case bp_exception_resume:
5560     case bp_step_resume:
5561     case bp_hp_step_resume:
5562     case bp_watchpoint_scope:
5563     case bp_call_dummy:
5564     case bp_std_terminate:
5565     case bp_shlib_event:
5566     case bp_thread_event:
5567     case bp_overlay_event:
5568     case bp_jit_event:
5569     case bp_longjmp_master:
5570     case bp_std_terminate_master:
5571     case bp_exception_master:
5572     case bp_gnu_ifunc_resolver:
5573     case bp_gnu_ifunc_resolver_return:
5574       loc->loc_type = bp_loc_software_breakpoint;
5575       break;
5576     case bp_hardware_breakpoint:
5577       loc->loc_type = bp_loc_hardware_breakpoint;
5578       break;
5579     case bp_hardware_watchpoint:
5580     case bp_read_watchpoint:
5581     case bp_access_watchpoint:
5582       loc->loc_type = bp_loc_hardware_watchpoint;
5583       break;
5584     case bp_watchpoint:
5585     case bp_catchpoint:
5586     case bp_tracepoint:
5587     case bp_fast_tracepoint:
5588     case bp_static_tracepoint:
5589       loc->loc_type = bp_loc_other;
5590       break;
5591     default:
5592       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
5593     }
5594
5595   loc->refc = 1;
5596 }
5597
5598 /* Allocate a struct bp_location.  */
5599
5600 static struct bp_location *
5601 allocate_bp_location (struct breakpoint *bpt)
5602 {
5603   return bpt->ops->allocate_location (bpt);
5604 }
5605
5606 static void
5607 free_bp_location (struct bp_location *loc)
5608 {
5609   loc->ops->dtor (loc);
5610   xfree (loc);
5611 }
5612
5613 /* Increment reference count.  */
5614
5615 static void
5616 incref_bp_location (struct bp_location *bl)
5617 {
5618   ++bl->refc;
5619 }
5620
5621 /* Decrement reference count.  If the reference count reaches 0,
5622    destroy the bp_location.  Sets *BLP to NULL.  */
5623
5624 static void
5625 decref_bp_location (struct bp_location **blp)
5626 {
5627   gdb_assert ((*blp)->refc > 0);
5628
5629   if (--(*blp)->refc == 0)
5630     free_bp_location (*blp);
5631   *blp = NULL;
5632 }
5633
5634 /* Add breakpoint B at the end of the global breakpoint chain.  */
5635
5636 static void
5637 add_to_breakpoint_chain (struct breakpoint *b)
5638 {
5639   struct breakpoint *b1;
5640
5641   /* Add this breakpoint to the end of the chain so that a list of
5642      breakpoints will come out in order of increasing numbers.  */
5643
5644   b1 = breakpoint_chain;
5645   if (b1 == 0)
5646     breakpoint_chain = b;
5647   else
5648     {
5649       while (b1->next)
5650         b1 = b1->next;
5651       b1->next = b;
5652     }
5653 }
5654
5655 /* Initializes breakpoint B with type BPTYPE and no locations yet.  */
5656
5657 static void
5658 init_raw_breakpoint_without_location (struct breakpoint *b,
5659                                       struct gdbarch *gdbarch,
5660                                       enum bptype bptype,
5661                                       const struct breakpoint_ops *ops)
5662 {
5663   memset (b, 0, sizeof (*b));
5664
5665   gdb_assert (ops != NULL);
5666
5667   b->ops = ops;
5668   b->type = bptype;
5669   b->gdbarch = gdbarch;
5670   b->language = current_language->la_language;
5671   b->input_radix = input_radix;
5672   b->thread = -1;
5673   b->enable_state = bp_enabled;
5674   b->next = 0;
5675   b->silent = 0;
5676   b->ignore_count = 0;
5677   b->commands = NULL;
5678   b->frame_id = null_frame_id;
5679   b->condition_not_parsed = 0;
5680   b->py_bp_object = NULL;
5681   b->related_breakpoint = b;
5682 }
5683
5684 /* Helper to set_raw_breakpoint below.  Creates a breakpoint
5685    that has type BPTYPE and has no locations as yet.  */
5686
5687 static struct breakpoint *
5688 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5689                                      enum bptype bptype,
5690                                      const struct breakpoint_ops *ops)
5691 {
5692   struct breakpoint *b = XNEW (struct breakpoint);
5693
5694   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
5695   add_to_breakpoint_chain (b);
5696   return b;
5697 }
5698
5699 /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
5700    resolutions should be made as the user specified the location explicitly
5701    enough.  */
5702
5703 static void
5704 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
5705 {
5706   gdb_assert (loc->owner != NULL);
5707
5708   if (loc->owner->type == bp_breakpoint
5709       || loc->owner->type == bp_hardware_breakpoint
5710       || is_tracepoint (loc->owner))
5711     {
5712       int is_gnu_ifunc;
5713
5714       find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5715                                           NULL, NULL, &is_gnu_ifunc);
5716
5717       if (is_gnu_ifunc && !explicit_loc)
5718         {
5719           struct breakpoint *b = loc->owner;
5720
5721           gdb_assert (loc->pspace == current_program_space);
5722           if (gnu_ifunc_resolve_name (loc->function_name,
5723                                       &loc->requested_address))
5724             {
5725               /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
5726               loc->address = adjust_breakpoint_address (loc->gdbarch,
5727                                                         loc->requested_address,
5728                                                         b->type);
5729             }
5730           else if (b->type == bp_breakpoint && b->loc == loc
5731                    && loc->next == NULL && b->related_breakpoint == b)
5732             {
5733               /* Create only the whole new breakpoint of this type but do not
5734                  mess more complicated breakpoints with multiple locations.  */
5735               b->type = bp_gnu_ifunc_resolver;
5736             }
5737         }
5738
5739       if (loc->function_name)
5740         loc->function_name = xstrdup (loc->function_name);
5741     }
5742 }
5743
5744 /* Attempt to determine architecture of location identified by SAL.  */
5745 struct gdbarch *
5746 get_sal_arch (struct symtab_and_line sal)
5747 {
5748   if (sal.section)
5749     return get_objfile_arch (sal.section->objfile);
5750   if (sal.symtab)
5751     return get_objfile_arch (sal.symtab->objfile);
5752
5753   return NULL;
5754 }
5755
5756 /* Low level routine for partially initializing a breakpoint of type
5757    BPTYPE.  The newly created breakpoint's address, section, source
5758    file name, and line number are provided by SAL.
5759
5760    It is expected that the caller will complete the initialization of
5761    the newly created breakpoint struct as well as output any status
5762    information regarding the creation of a new breakpoint.  */
5763
5764 static void
5765 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
5766                      struct symtab_and_line sal, enum bptype bptype,
5767                      const struct breakpoint_ops *ops)
5768 {
5769   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
5770
5771   add_location_to_breakpoint (b, &sal);
5772
5773   if (bptype != bp_catchpoint)
5774     gdb_assert (sal.pspace != NULL);
5775
5776   /* Store the program space that was used to set the breakpoint, for
5777      breakpoint resetting.  */
5778   b->pspace = sal.pspace;
5779
5780   if (sal.symtab == NULL)
5781     b->source_file = NULL;
5782   else
5783     b->source_file = xstrdup (sal.symtab->filename);
5784   b->line_number = sal.line;
5785
5786   breakpoints_changed ();
5787 }
5788
5789 /* set_raw_breakpoint is a low level routine for allocating and
5790    partially initializing a breakpoint of type BPTYPE.  The newly
5791    created breakpoint's address, section, source file name, and line
5792    number are provided by SAL.  The newly created and partially
5793    initialized breakpoint is added to the breakpoint chain and
5794    is also returned as the value of this function.
5795
5796    It is expected that the caller will complete the initialization of
5797    the newly created breakpoint struct as well as output any status
5798    information regarding the creation of a new breakpoint.  In
5799    particular, set_raw_breakpoint does NOT set the breakpoint
5800    number!  Care should be taken to not allow an error to occur
5801    prior to completing the initialization of the breakpoint.  If this
5802    should happen, a bogus breakpoint will be left on the chain.  */
5803
5804 struct breakpoint *
5805 set_raw_breakpoint (struct gdbarch *gdbarch,
5806                     struct symtab_and_line sal, enum bptype bptype,
5807                     const struct breakpoint_ops *ops)
5808 {
5809   struct breakpoint *b = XNEW (struct breakpoint);
5810
5811   init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
5812   add_to_breakpoint_chain (b);
5813   return b;
5814 }
5815
5816
5817 /* Note that the breakpoint object B describes a permanent breakpoint
5818    instruction, hard-wired into the inferior's code.  */
5819 void
5820 make_breakpoint_permanent (struct breakpoint *b)
5821 {
5822   struct bp_location *bl;
5823
5824   b->enable_state = bp_permanent;
5825
5826   /* By definition, permanent breakpoints are already present in the
5827      code.  Mark all locations as inserted.  For now,
5828      make_breakpoint_permanent is called in just one place, so it's
5829      hard to say if it's reasonable to have permanent breakpoint with
5830      multiple locations or not, but it's easy to implement.  */
5831   for (bl = b->loc; bl; bl = bl->next)
5832     bl->inserted = 1;
5833 }
5834
5835 /* Call this routine when stepping and nexting to enable a breakpoint
5836    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
5837    initiated the operation.  */
5838
5839 void
5840 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
5841 {
5842   struct breakpoint *b, *b_tmp;
5843   int thread = tp->num;
5844
5845   /* To avoid having to rescan all objfile symbols at every step,
5846      we maintain a list of continually-inserted but always disabled
5847      longjmp "master" breakpoints.  Here, we simply create momentary
5848      clones of those and enable them for the requested thread.  */
5849   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5850     if (b->pspace == current_program_space
5851         && (b->type == bp_longjmp_master
5852             || b->type == bp_exception_master))
5853       {
5854         enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5855         struct breakpoint *clone;
5856
5857         clone = momentary_breakpoint_from_master (b, type,
5858                                                   &momentary_breakpoint_ops);
5859         clone->thread = thread;
5860       }
5861
5862   tp->initiating_frame = frame;
5863 }
5864
5865 /* Delete all longjmp breakpoints from THREAD.  */
5866 void
5867 delete_longjmp_breakpoint (int thread)
5868 {
5869   struct breakpoint *b, *b_tmp;
5870
5871   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5872     if (b->type == bp_longjmp || b->type == bp_exception)
5873       {
5874         if (b->thread == thread)
5875           delete_breakpoint (b);
5876       }
5877 }
5878
5879 void
5880 enable_overlay_breakpoints (void)
5881 {
5882   struct breakpoint *b;
5883
5884   ALL_BREAKPOINTS (b)
5885     if (b->type == bp_overlay_event)
5886     {
5887       b->enable_state = bp_enabled;
5888       update_global_location_list (1);
5889       overlay_events_enabled = 1;
5890     }
5891 }
5892
5893 void
5894 disable_overlay_breakpoints (void)
5895 {
5896   struct breakpoint *b;
5897
5898   ALL_BREAKPOINTS (b)
5899     if (b->type == bp_overlay_event)
5900     {
5901       b->enable_state = bp_disabled;
5902       update_global_location_list (0);
5903       overlay_events_enabled = 0;
5904     }
5905 }
5906
5907 /* Set an active std::terminate breakpoint for each std::terminate
5908    master breakpoint.  */
5909 void
5910 set_std_terminate_breakpoint (void)
5911 {
5912   struct breakpoint *b, *b_tmp;
5913
5914   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5915     if (b->pspace == current_program_space
5916         && b->type == bp_std_terminate_master)
5917       {
5918         momentary_breakpoint_from_master (b, bp_std_terminate,
5919                                           &momentary_breakpoint_ops);
5920       }
5921 }
5922
5923 /* Delete all the std::terminate breakpoints.  */
5924 void
5925 delete_std_terminate_breakpoint (void)
5926 {
5927   struct breakpoint *b, *b_tmp;
5928
5929   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5930     if (b->type == bp_std_terminate)
5931       delete_breakpoint (b);
5932 }
5933
5934 struct breakpoint *
5935 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5936 {
5937   struct breakpoint *b;
5938
5939   b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5940                                   &internal_breakpoint_ops);
5941
5942   b->enable_state = bp_enabled;
5943   /* addr_string has to be used or breakpoint_re_set will delete me.  */
5944   b->addr_string
5945     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
5946
5947   update_global_location_list_nothrow (1);
5948
5949   return b;
5950 }
5951
5952 void
5953 remove_thread_event_breakpoints (void)
5954 {
5955   struct breakpoint *b, *b_tmp;
5956
5957   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5958     if (b->type == bp_thread_event
5959         && b->loc->pspace == current_program_space)
5960       delete_breakpoint (b);
5961 }
5962
5963 struct lang_and_radix
5964   {
5965     enum language lang;
5966     int radix;
5967   };
5968
5969 /* Create a breakpoint for JIT code registration and unregistration.  */
5970
5971 struct breakpoint *
5972 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5973 {
5974   struct breakpoint *b;
5975
5976   b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
5977                                   &internal_breakpoint_ops);
5978   update_global_location_list_nothrow (1);
5979   return b;
5980 }
5981
5982 /* Remove JIT code registration and unregistration breakpoint(s).  */
5983
5984 void
5985 remove_jit_event_breakpoints (void)
5986 {
5987   struct breakpoint *b, *b_tmp;
5988
5989   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5990     if (b->type == bp_jit_event
5991         && b->loc->pspace == current_program_space)
5992       delete_breakpoint (b);
5993 }
5994
5995 void
5996 remove_solib_event_breakpoints (void)
5997 {
5998   struct breakpoint *b, *b_tmp;
5999
6000   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6001     if (b->type == bp_shlib_event
6002         && b->loc->pspace == current_program_space)
6003       delete_breakpoint (b);
6004 }
6005
6006 struct breakpoint *
6007 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6008 {
6009   struct breakpoint *b;
6010
6011   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6012                                   &internal_breakpoint_ops);
6013   update_global_location_list_nothrow (1);
6014   return b;
6015 }
6016
6017 /* Disable any breakpoints that are on code in shared libraries.  Only
6018    apply to enabled breakpoints, disabled ones can just stay disabled.  */
6019
6020 void
6021 disable_breakpoints_in_shlibs (void)
6022 {
6023   struct bp_location *loc, **locp_tmp;
6024
6025   ALL_BP_LOCATIONS (loc, locp_tmp)
6026   {
6027     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
6028     struct breakpoint *b = loc->owner;
6029
6030     /* We apply the check to all breakpoints, including disabled for
6031        those with loc->duplicate set.  This is so that when breakpoint
6032        becomes enabled, or the duplicate is removed, gdb will try to
6033        insert all breakpoints.  If we don't set shlib_disabled here,
6034        we'll try to insert those breakpoints and fail.  */
6035     if (((b->type == bp_breakpoint)
6036          || (b->type == bp_jit_event)
6037          || (b->type == bp_hardware_breakpoint)
6038          || (is_tracepoint (b)))
6039         && loc->pspace == current_program_space
6040         && !loc->shlib_disabled
6041 #ifdef PC_SOLIB
6042         && PC_SOLIB (loc->address)
6043 #else
6044         && solib_name_from_address (loc->pspace, loc->address)
6045 #endif
6046         )
6047       {
6048         loc->shlib_disabled = 1;
6049       }
6050   }
6051 }
6052
6053 /* Disable any breakpoints that are in an unloaded shared library.
6054    Only apply to enabled breakpoints, disabled ones can just stay
6055    disabled.  */
6056
6057 static void
6058 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6059 {
6060   struct bp_location *loc, **locp_tmp;
6061   int disabled_shlib_breaks = 0;
6062
6063   /* SunOS a.out shared libraries are always mapped, so do not
6064      disable breakpoints; they will only be reported as unloaded
6065      through clear_solib when GDB discards its shared library
6066      list.  See clear_solib for more information.  */
6067   if (exec_bfd != NULL
6068       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6069     return;
6070
6071   ALL_BP_LOCATIONS (loc, locp_tmp)
6072   {
6073     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
6074     struct breakpoint *b = loc->owner;
6075
6076     if ((loc->loc_type == bp_loc_hardware_breakpoint
6077          || loc->loc_type == bp_loc_software_breakpoint)
6078         && solib->pspace == loc->pspace
6079         && !loc->shlib_disabled
6080         && (b->type == bp_breakpoint
6081             || b->type == bp_jit_event
6082             || b->type == bp_hardware_breakpoint)
6083         && solib_contains_address_p (solib, loc->address))
6084       {
6085         loc->shlib_disabled = 1;
6086         /* At this point, we cannot rely on remove_breakpoint
6087            succeeding so we must mark the breakpoint as not inserted
6088            to prevent future errors occurring in remove_breakpoints.  */
6089         loc->inserted = 0;
6090
6091         /* This may cause duplicate notifications for the same breakpoint.  */
6092         observer_notify_breakpoint_modified (b);
6093
6094         if (!disabled_shlib_breaks)
6095           {
6096             target_terminal_ours_for_output ();
6097             warning (_("Temporarily disabling breakpoints "
6098                        "for unloaded shared library \"%s\""),
6099                      solib->so_name);
6100           }
6101         disabled_shlib_breaks = 1;
6102       }
6103   }
6104 }
6105
6106 /* FORK & VFORK catchpoints.  */
6107
6108 /* An instance of this type is used to represent a fork or vfork
6109    catchpoint.  It includes a "struct breakpoint" as a kind of base
6110    class; users downcast to "struct breakpoint *" when needed.  A
6111    breakpoint is really of this type iff its ops pointer points to
6112    CATCH_FORK_BREAKPOINT_OPS.  */
6113
6114 struct fork_catchpoint
6115 {
6116   /* The base class.  */
6117   struct breakpoint base;
6118
6119   /* Process id of a child process whose forking triggered this
6120      catchpoint.  This field is only valid immediately after this
6121      catchpoint has triggered.  */
6122   ptid_t forked_inferior_pid;
6123 };
6124
6125 /* Implement the "insert" breakpoint_ops method for fork
6126    catchpoints.  */
6127
6128 static int
6129 insert_catch_fork (struct bp_location *bl)
6130 {
6131   return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
6132 }
6133
6134 /* Implement the "remove" breakpoint_ops method for fork
6135    catchpoints.  */
6136
6137 static int
6138 remove_catch_fork (struct bp_location *bl)
6139 {
6140   return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6141 }
6142
6143 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
6144    catchpoints.  */
6145
6146 static int
6147 breakpoint_hit_catch_fork (const struct bp_location *bl,
6148                            struct address_space *aspace, CORE_ADDR bp_addr)
6149 {
6150   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6151
6152   return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
6153 }
6154
6155 /* Implement the "print_it" breakpoint_ops method for fork
6156    catchpoints.  */
6157
6158 static enum print_stop_action
6159 print_it_catch_fork (bpstat bs)
6160 {
6161   struct breakpoint *b = bs->breakpoint_at;
6162   struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
6163
6164   annotate_catchpoint (b->number);
6165   printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6166                    b->number, ptid_get_pid (c->forked_inferior_pid));
6167   return PRINT_SRC_AND_LOC;
6168 }
6169
6170 /* Implement the "print_one" breakpoint_ops method for fork
6171    catchpoints.  */
6172
6173 static void
6174 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
6175 {
6176   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6177   struct value_print_options opts;
6178   struct ui_out *uiout = current_uiout;
6179
6180   get_user_print_options (&opts);
6181
6182   /* Field 4, the address, is omitted (which makes the columns not
6183      line up too nicely with the headers, but the effect is relatively
6184      readable).  */
6185   if (opts.addressprint)
6186     ui_out_field_skip (uiout, "addr");
6187   annotate_field (5);
6188   ui_out_text (uiout, "fork");
6189   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
6190     {
6191       ui_out_text (uiout, ", process ");
6192       ui_out_field_int (uiout, "what",
6193                         ptid_get_pid (c->forked_inferior_pid));
6194       ui_out_spaces (uiout, 1);
6195     }
6196 }
6197
6198 /* Implement the "print_mention" breakpoint_ops method for fork
6199    catchpoints.  */
6200
6201 static void
6202 print_mention_catch_fork (struct breakpoint *b)
6203 {
6204   printf_filtered (_("Catchpoint %d (fork)"), b->number);
6205 }
6206
6207 /* Implement the "print_recreate" breakpoint_ops method for fork
6208    catchpoints.  */
6209
6210 static void
6211 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6212 {
6213   fprintf_unfiltered (fp, "catch fork");
6214   print_recreate_thread (b, fp);
6215 }
6216
6217 /* The breakpoint_ops structure to be used in fork catchpoints.  */
6218
6219 static struct breakpoint_ops catch_fork_breakpoint_ops;
6220
6221 /* Implement the "insert" breakpoint_ops method for vfork
6222    catchpoints.  */
6223
6224 static int
6225 insert_catch_vfork (struct bp_location *bl)
6226 {
6227   return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
6228 }
6229
6230 /* Implement the "remove" breakpoint_ops method for vfork
6231    catchpoints.  */
6232
6233 static int
6234 remove_catch_vfork (struct bp_location *bl)
6235 {
6236   return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6237 }
6238
6239 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6240    catchpoints.  */
6241
6242 static int
6243 breakpoint_hit_catch_vfork (const struct bp_location *bl,
6244                             struct address_space *aspace, CORE_ADDR bp_addr)
6245 {
6246   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6247
6248   return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
6249 }
6250
6251 /* Implement the "print_it" breakpoint_ops method for vfork
6252    catchpoints.  */
6253
6254 static enum print_stop_action
6255 print_it_catch_vfork (bpstat bs)
6256 {
6257   struct breakpoint *b = bs->breakpoint_at;
6258   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6259
6260   annotate_catchpoint (b->number);
6261   printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6262                    b->number, ptid_get_pid (c->forked_inferior_pid));
6263   return PRINT_SRC_AND_LOC;
6264 }
6265
6266 /* Implement the "print_one" breakpoint_ops method for vfork
6267    catchpoints.  */
6268
6269 static void
6270 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
6271 {
6272   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6273   struct value_print_options opts;
6274   struct ui_out *uiout = current_uiout;
6275
6276   get_user_print_options (&opts);
6277   /* Field 4, the address, is omitted (which makes the columns not
6278      line up too nicely with the headers, but the effect is relatively
6279      readable).  */
6280   if (opts.addressprint)
6281     ui_out_field_skip (uiout, "addr");
6282   annotate_field (5);
6283   ui_out_text (uiout, "vfork");
6284   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
6285     {
6286       ui_out_text (uiout, ", process ");
6287       ui_out_field_int (uiout, "what",
6288                         ptid_get_pid (c->forked_inferior_pid));
6289       ui_out_spaces (uiout, 1);
6290     }
6291 }
6292
6293 /* Implement the "print_mention" breakpoint_ops method for vfork
6294    catchpoints.  */
6295
6296 static void
6297 print_mention_catch_vfork (struct breakpoint *b)
6298 {
6299   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6300 }
6301
6302 /* Implement the "print_recreate" breakpoint_ops method for vfork
6303    catchpoints.  */
6304
6305 static void
6306 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6307 {
6308   fprintf_unfiltered (fp, "catch vfork");
6309   print_recreate_thread (b, fp);
6310 }
6311
6312 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
6313
6314 static struct breakpoint_ops catch_vfork_breakpoint_ops;
6315
6316 /* An instance of this type is used to represent a syscall catchpoint.
6317    It includes a "struct breakpoint" as a kind of base class; users
6318    downcast to "struct breakpoint *" when needed.  A breakpoint is
6319    really of this type iff its ops pointer points to
6320    CATCH_SYSCALL_BREAKPOINT_OPS.  */
6321
6322 struct syscall_catchpoint
6323 {
6324   /* The base class.  */
6325   struct breakpoint base;
6326
6327   /* Syscall numbers used for the 'catch syscall' feature.  If no
6328      syscall has been specified for filtering, its value is NULL.
6329      Otherwise, it holds a list of all syscalls to be caught.  The
6330      list elements are allocated with xmalloc.  */
6331   VEC(int) *syscalls_to_be_caught;
6332 };
6333
6334 /* Implement the "dtor" breakpoint_ops method for syscall
6335    catchpoints.  */
6336
6337 static void
6338 dtor_catch_syscall (struct breakpoint *b)
6339 {
6340   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6341
6342   VEC_free (int, c->syscalls_to_be_caught);
6343
6344   base_breakpoint_ops.dtor (b);
6345 }
6346
6347 /* Implement the "insert" breakpoint_ops method for syscall
6348    catchpoints.  */
6349
6350 static int
6351 insert_catch_syscall (struct bp_location *bl)
6352 {
6353   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
6354   struct inferior *inf = current_inferior ();
6355
6356   ++inf->total_syscalls_count;
6357   if (!c->syscalls_to_be_caught)
6358     ++inf->any_syscall_count;
6359   else
6360     {
6361       int i, iter;
6362
6363       for (i = 0;
6364            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6365            i++)
6366         {
6367           int elem;
6368
6369           if (iter >= VEC_length (int, inf->syscalls_counts))
6370             {
6371               int old_size = VEC_length (int, inf->syscalls_counts);
6372               uintptr_t vec_addr_offset
6373                 = old_size * ((uintptr_t) sizeof (int));
6374               uintptr_t vec_addr;
6375               VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6376               vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6377                 vec_addr_offset;
6378               memset ((void *) vec_addr, 0,
6379                       (iter + 1 - old_size) * sizeof (int));
6380             }
6381           elem = VEC_index (int, inf->syscalls_counts, iter);
6382           VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6383         }
6384     }
6385
6386   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6387                                         inf->total_syscalls_count != 0,
6388                                         inf->any_syscall_count,
6389                                         VEC_length (int, inf->syscalls_counts),
6390                                         VEC_address (int, inf->syscalls_counts));
6391 }
6392
6393 /* Implement the "remove" breakpoint_ops method for syscall
6394    catchpoints.  */
6395
6396 static int
6397 remove_catch_syscall (struct bp_location *bl)
6398 {
6399   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
6400   struct inferior *inf = current_inferior ();
6401
6402   --inf->total_syscalls_count;
6403   if (!c->syscalls_to_be_caught)
6404     --inf->any_syscall_count;
6405   else
6406     {
6407       int i, iter;
6408
6409       for (i = 0;
6410            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6411            i++)
6412         {
6413           int elem;
6414           if (iter >= VEC_length (int, inf->syscalls_counts))
6415             /* Shouldn't happen.  */
6416             continue;
6417           elem = VEC_index (int, inf->syscalls_counts, iter);
6418           VEC_replace (int, inf->syscalls_counts, iter, --elem);
6419         }
6420     }
6421
6422   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6423                                         inf->total_syscalls_count != 0,
6424                                         inf->any_syscall_count,
6425                                         VEC_length (int, inf->syscalls_counts),
6426                                         VEC_address (int,
6427                                                      inf->syscalls_counts));
6428 }
6429
6430 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6431    catchpoints.  */
6432
6433 static int
6434 breakpoint_hit_catch_syscall (const struct bp_location *bl,
6435                               struct address_space *aspace, CORE_ADDR bp_addr)
6436 {
6437   /* We must check if we are catching specific syscalls in this
6438      breakpoint.  If we are, then we must guarantee that the called
6439      syscall is the same syscall we are catching.  */
6440   int syscall_number = 0;
6441   const struct syscall_catchpoint *c
6442     = (const struct syscall_catchpoint *) bl->owner;
6443
6444   if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6445     return 0;
6446
6447   /* Now, checking if the syscall is the same.  */
6448   if (c->syscalls_to_be_caught)
6449     {
6450       int i, iter;
6451
6452       for (i = 0;
6453            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6454            i++)
6455         if (syscall_number == iter)
6456           break;
6457       /* Not the same.  */
6458       if (!iter)
6459         return 0;
6460     }
6461
6462   return 1;
6463 }
6464
6465 /* Implement the "print_it" breakpoint_ops method for syscall
6466    catchpoints.  */
6467
6468 static enum print_stop_action
6469 print_it_catch_syscall (bpstat bs)
6470 {
6471   struct breakpoint *b = bs->breakpoint_at;
6472   /* These are needed because we want to know in which state a
6473      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6474      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6475      must print "called syscall" or "returned from syscall".  */
6476   ptid_t ptid;
6477   struct target_waitstatus last;
6478   struct syscall s;
6479   struct cleanup *old_chain;
6480   char *syscall_id;
6481
6482   get_last_target_status (&ptid, &last);
6483
6484   get_syscall_by_number (last.value.syscall_number, &s);
6485
6486   annotate_catchpoint (b->number);
6487
6488   if (s.name == NULL)
6489     syscall_id = xstrprintf ("%d", last.value.syscall_number);
6490   else
6491     syscall_id = xstrprintf ("'%s'", s.name);
6492
6493   old_chain = make_cleanup (xfree, syscall_id);
6494
6495   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6496     printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6497                      b->number, syscall_id);
6498   else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6499     printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6500                      b->number, syscall_id);
6501
6502   do_cleanups (old_chain);
6503
6504   return PRINT_SRC_AND_LOC;
6505 }
6506
6507 /* Implement the "print_one" breakpoint_ops method for syscall
6508    catchpoints.  */
6509
6510 static void
6511 print_one_catch_syscall (struct breakpoint *b,
6512                          struct bp_location **last_loc)
6513 {
6514   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6515   struct value_print_options opts;
6516   struct ui_out *uiout = current_uiout;
6517
6518   get_user_print_options (&opts);
6519   /* Field 4, the address, is omitted (which makes the columns not
6520      line up too nicely with the headers, but the effect is relatively
6521      readable).  */
6522   if (opts.addressprint)
6523     ui_out_field_skip (uiout, "addr");
6524   annotate_field (5);
6525
6526   if (c->syscalls_to_be_caught
6527       && VEC_length (int, c->syscalls_to_be_caught) > 1)
6528     ui_out_text (uiout, "syscalls \"");
6529   else
6530     ui_out_text (uiout, "syscall \"");
6531
6532   if (c->syscalls_to_be_caught)
6533     {
6534       int i, iter;
6535       char *text = xstrprintf ("%s", "");
6536
6537       for (i = 0;
6538            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6539            i++)
6540         {
6541           char *x = text;
6542           struct syscall s;
6543           get_syscall_by_number (iter, &s);
6544
6545           if (s.name != NULL)
6546             text = xstrprintf ("%s%s, ", text, s.name);
6547           else
6548             text = xstrprintf ("%s%d, ", text, iter);
6549
6550           /* We have to xfree the last 'text' (now stored at 'x')
6551              because xstrprintf dynamically allocates new space for it
6552              on every call.  */
6553           xfree (x);
6554         }
6555       /* Remove the last comma.  */
6556       text[strlen (text) - 2] = '\0';
6557       ui_out_field_string (uiout, "what", text);
6558     }
6559   else
6560     ui_out_field_string (uiout, "what", "<any syscall>");
6561   ui_out_text (uiout, "\" ");
6562 }
6563
6564 /* Implement the "print_mention" breakpoint_ops method for syscall
6565    catchpoints.  */
6566
6567 static void
6568 print_mention_catch_syscall (struct breakpoint *b)
6569 {
6570   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6571
6572   if (c->syscalls_to_be_caught)
6573     {
6574       int i, iter;
6575
6576       if (VEC_length (int, c->syscalls_to_be_caught) > 1)
6577         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6578       else
6579         printf_filtered (_("Catchpoint %d (syscall"), b->number);
6580
6581       for (i = 0;
6582            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6583            i++)
6584         {
6585           struct syscall s;
6586           get_syscall_by_number (iter, &s);
6587
6588           if (s.name)
6589             printf_filtered (" '%s' [%d]", s.name, s.number);
6590           else
6591             printf_filtered (" %d", s.number);
6592         }
6593       printf_filtered (")");
6594     }
6595   else
6596     printf_filtered (_("Catchpoint %d (any syscall)"),
6597                      b->number);
6598 }
6599
6600 /* Implement the "print_recreate" breakpoint_ops method for syscall
6601    catchpoints.  */
6602
6603 static void
6604 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6605 {
6606   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6607
6608   fprintf_unfiltered (fp, "catch syscall");
6609
6610   if (c->syscalls_to_be_caught)
6611     {
6612       int i, iter;
6613
6614       for (i = 0;
6615            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6616            i++)
6617         {
6618           struct syscall s;
6619
6620           get_syscall_by_number (iter, &s);
6621           if (s.name)
6622             fprintf_unfiltered (fp, " %s", s.name);
6623           else
6624             fprintf_unfiltered (fp, " %d", s.number);
6625         }
6626     }
6627   print_recreate_thread (b, fp);
6628 }
6629
6630 /* The breakpoint_ops structure to be used in syscall catchpoints.  */
6631
6632 static struct breakpoint_ops catch_syscall_breakpoint_ops;
6633
6634 /* Returns non-zero if 'b' is a syscall catchpoint.  */
6635
6636 static int
6637 syscall_catchpoint_p (struct breakpoint *b)
6638 {
6639   return (b->ops == &catch_syscall_breakpoint_ops);
6640 }
6641
6642 /* Initialize a new breakpoint of the bp_catchpoint kind.  If TEMPFLAG
6643    is non-zero, then make the breakpoint temporary.  If COND_STRING is
6644    not NULL, then store it in the breakpoint.  OPS, if not NULL, is
6645    the breakpoint_ops structure associated to the catchpoint.  */
6646
6647 static void
6648 init_catchpoint (struct breakpoint *b,
6649                  struct gdbarch *gdbarch, int tempflag,
6650                  char *cond_string,
6651                  const struct breakpoint_ops *ops)
6652 {
6653   struct symtab_and_line sal;
6654
6655   init_sal (&sal);
6656   sal.pspace = current_program_space;
6657
6658   init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
6659
6660   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
6661   b->disposition = tempflag ? disp_del : disp_donttouch;
6662 }
6663
6664 void
6665 install_breakpoint (int internal, struct breakpoint *b)
6666 {
6667   add_to_breakpoint_chain (b);
6668   set_breakpoint_number (internal, b);
6669   if (!internal)
6670     mention (b);
6671   observer_notify_breakpoint_created (b);
6672   update_global_location_list (1);
6673 }
6674
6675 static void
6676 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6677                                     int tempflag, char *cond_string,
6678                                     const struct breakpoint_ops *ops)
6679 {
6680   struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
6681
6682   init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6683
6684   c->forked_inferior_pid = null_ptid;
6685
6686   install_breakpoint (0, &c->base);
6687 }
6688
6689 /* Exec catchpoints.  */
6690
6691 /* An instance of this type is used to represent an exec catchpoint.
6692    It includes a "struct breakpoint" as a kind of base class; users
6693    downcast to "struct breakpoint *" when needed.  A breakpoint is
6694    really of this type iff its ops pointer points to
6695    CATCH_EXEC_BREAKPOINT_OPS.  */
6696
6697 struct exec_catchpoint
6698 {
6699   /* The base class.  */
6700   struct breakpoint base;
6701
6702   /* Filename of a program whose exec triggered this catchpoint.
6703      This field is only valid immediately after this catchpoint has
6704      triggered.  */
6705   char *exec_pathname;
6706 };
6707
6708 /* Implement the "dtor" breakpoint_ops method for exec
6709    catchpoints.  */
6710
6711 static void
6712 dtor_catch_exec (struct breakpoint *b)
6713 {
6714   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6715
6716   xfree (c->exec_pathname);
6717
6718   base_breakpoint_ops.dtor (b);
6719 }
6720
6721 static int
6722 insert_catch_exec (struct bp_location *bl)
6723 {
6724   return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
6725 }
6726
6727 static int
6728 remove_catch_exec (struct bp_location *bl)
6729 {
6730   return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6731 }
6732
6733 static int
6734 breakpoint_hit_catch_exec (const struct bp_location *bl,
6735                            struct address_space *aspace, CORE_ADDR bp_addr)
6736 {
6737   struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6738
6739   return inferior_has_execd (inferior_ptid, &c->exec_pathname);
6740 }
6741
6742 static enum print_stop_action
6743 print_it_catch_exec (bpstat bs)
6744 {
6745   struct breakpoint *b = bs->breakpoint_at;
6746   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6747
6748   annotate_catchpoint (b->number);
6749   printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6750                    c->exec_pathname);
6751   return PRINT_SRC_AND_LOC;
6752 }
6753
6754 static void
6755 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
6756 {
6757   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6758   struct value_print_options opts;
6759   struct ui_out *uiout = current_uiout;
6760
6761   get_user_print_options (&opts);
6762
6763   /* Field 4, the address, is omitted (which makes the columns
6764      not line up too nicely with the headers, but the effect
6765      is relatively readable).  */
6766   if (opts.addressprint)
6767     ui_out_field_skip (uiout, "addr");
6768   annotate_field (5);
6769   ui_out_text (uiout, "exec");
6770   if (c->exec_pathname != NULL)
6771     {
6772       ui_out_text (uiout, ", program \"");
6773       ui_out_field_string (uiout, "what", c->exec_pathname);
6774       ui_out_text (uiout, "\" ");
6775     }
6776 }
6777
6778 static void
6779 print_mention_catch_exec (struct breakpoint *b)
6780 {
6781   printf_filtered (_("Catchpoint %d (exec)"), b->number);
6782 }
6783
6784 /* Implement the "print_recreate" breakpoint_ops method for exec
6785    catchpoints.  */
6786
6787 static void
6788 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6789 {
6790   fprintf_unfiltered (fp, "catch exec");
6791   print_recreate_thread (b, fp);
6792 }
6793
6794 static struct breakpoint_ops catch_exec_breakpoint_ops;
6795
6796 static void
6797 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6798                                  const struct breakpoint_ops *ops)
6799 {
6800   struct syscall_catchpoint *c;
6801   struct gdbarch *gdbarch = get_current_arch ();
6802
6803   c = XNEW (struct syscall_catchpoint);
6804   init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6805   c->syscalls_to_be_caught = filter;
6806
6807   install_breakpoint (0, &c->base);
6808 }
6809
6810 static int
6811 hw_breakpoint_used_count (void)
6812 {
6813   int i = 0;
6814   struct breakpoint *b;
6815   struct bp_location *bl;
6816
6817   ALL_BREAKPOINTS (b)
6818   {
6819     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
6820       for (bl = b->loc; bl; bl = bl->next)
6821         {
6822           /* Special types of hardware breakpoints may use more than
6823              one register.  */
6824           i += b->ops->resources_needed (bl);
6825         }
6826   }
6827
6828   return i;
6829 }
6830
6831 /* Returns the resources B would use if it were a hardware
6832    watchpoint.  */
6833
6834 static int
6835 hw_watchpoint_use_count (struct breakpoint *b)
6836 {
6837   int i = 0;
6838   struct bp_location *bl;
6839
6840   if (!breakpoint_enabled (b))
6841     return 0;
6842
6843   for (bl = b->loc; bl; bl = bl->next)
6844     {
6845       /* Special types of hardware watchpoints may use more than
6846          one register.  */
6847       i += b->ops->resources_needed (bl);
6848     }
6849
6850   return i;
6851 }
6852
6853 /* Returns the sum the used resources of all hardware watchpoints of
6854    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
6855    the sum of the used resources of all hardware watchpoints of other
6856    types _not_ TYPE.  */
6857
6858 static int
6859 hw_watchpoint_used_count_others (struct breakpoint *except,
6860                                  enum bptype type, int *other_type_used)
6861 {
6862   int i = 0;
6863   struct breakpoint *b;
6864
6865   *other_type_used = 0;
6866   ALL_BREAKPOINTS (b)
6867     {
6868       if (b == except)
6869         continue;
6870       if (!breakpoint_enabled (b))
6871         continue;
6872
6873       if (b->type == type)
6874         i += hw_watchpoint_use_count (b);
6875       else if (is_hardware_watchpoint (b))
6876         *other_type_used = 1;
6877     }
6878
6879   return i;
6880 }
6881
6882 void
6883 disable_watchpoints_before_interactive_call_start (void)
6884 {
6885   struct breakpoint *b;
6886
6887   ALL_BREAKPOINTS (b)
6888   {
6889     if (is_watchpoint (b) && breakpoint_enabled (b))
6890       {
6891         b->enable_state = bp_call_disabled;
6892         update_global_location_list (0);
6893       }
6894   }
6895 }
6896
6897 void
6898 enable_watchpoints_after_interactive_call_stop (void)
6899 {
6900   struct breakpoint *b;
6901
6902   ALL_BREAKPOINTS (b)
6903   {
6904     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
6905       {
6906         b->enable_state = bp_enabled;
6907         update_global_location_list (1);
6908       }
6909   }
6910 }
6911
6912 void
6913 disable_breakpoints_before_startup (void)
6914 {
6915   struct breakpoint *b;
6916   int found = 0;
6917
6918   ALL_BREAKPOINTS (b)
6919     {
6920       if (b->pspace != current_program_space)
6921         continue;
6922
6923       if ((b->type == bp_breakpoint
6924            || b->type == bp_hardware_breakpoint)
6925           && breakpoint_enabled (b))
6926         {
6927           b->enable_state = bp_startup_disabled;
6928           found = 1;
6929         }
6930     }
6931
6932   if (found)
6933     update_global_location_list (0);
6934
6935   current_program_space->executing_startup = 1;
6936 }
6937
6938 void
6939 enable_breakpoints_after_startup (void)
6940 {
6941   struct breakpoint *b;
6942   int found = 0;
6943
6944   current_program_space->executing_startup = 0;
6945
6946   ALL_BREAKPOINTS (b)
6947     {
6948       if (b->pspace != current_program_space)
6949         continue;
6950
6951       if ((b->type == bp_breakpoint
6952            || b->type == bp_hardware_breakpoint)
6953           && b->enable_state == bp_startup_disabled)
6954         {
6955           b->enable_state = bp_enabled;
6956           found = 1;
6957         }
6958     }
6959
6960   if (found)
6961     breakpoint_re_set ();
6962 }
6963
6964
6965 /* Set a breakpoint that will evaporate an end of command
6966    at address specified by SAL.
6967    Restrict it to frame FRAME if FRAME is nonzero.  */
6968
6969 struct breakpoint *
6970 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6971                           struct frame_id frame_id, enum bptype type)
6972 {
6973   struct breakpoint *b;
6974
6975   /* If FRAME_ID is valid, it should be a real frame, not an inlined
6976      one.  */
6977   gdb_assert (!frame_id_inlined_p (frame_id));
6978
6979   b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
6980   b->enable_state = bp_enabled;
6981   b->disposition = disp_donttouch;
6982   b->frame_id = frame_id;
6983
6984   /* If we're debugging a multi-threaded program, then we want
6985      momentary breakpoints to be active in only a single thread of
6986      control.  */
6987   if (in_thread_list (inferior_ptid))
6988     b->thread = pid_to_thread_id (inferior_ptid);
6989
6990   update_global_location_list_nothrow (1);
6991
6992   return b;
6993 }
6994
6995 /* Make a momentary breakpoint based on the master breakpoint ORIG.
6996    The new breakpoint will have type TYPE, and use OPS as it
6997    breakpoint_ops.  */
6998
6999 static struct breakpoint *
7000 momentary_breakpoint_from_master (struct breakpoint *orig,
7001                                   enum bptype type,
7002                                   const struct breakpoint_ops *ops)
7003 {
7004   struct breakpoint *copy;
7005
7006   copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
7007   copy->loc = allocate_bp_location (copy);
7008   set_breakpoint_location_function (copy->loc, 1);
7009
7010   copy->loc->gdbarch = orig->loc->gdbarch;
7011   copy->loc->requested_address = orig->loc->requested_address;
7012   copy->loc->address = orig->loc->address;
7013   copy->loc->section = orig->loc->section;
7014   copy->loc->pspace = orig->loc->pspace;
7015
7016   if (orig->source_file == NULL)
7017     copy->source_file = NULL;
7018   else
7019     copy->source_file = xstrdup (orig->source_file);
7020
7021   copy->line_number = orig->line_number;
7022   copy->frame_id = orig->frame_id;
7023   copy->thread = orig->thread;
7024   copy->pspace = orig->pspace;
7025
7026   copy->enable_state = bp_enabled;
7027   copy->disposition = disp_donttouch;
7028   copy->number = internal_breakpoint_number--;
7029
7030   update_global_location_list_nothrow (0);
7031   return copy;
7032 }
7033
7034 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
7035    ORIG is NULL.  */
7036
7037 struct breakpoint *
7038 clone_momentary_breakpoint (struct breakpoint *orig)
7039 {
7040   /* If there's nothing to clone, then return nothing.  */
7041   if (orig == NULL)
7042     return NULL;
7043
7044   return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7045 }
7046
7047 struct breakpoint *
7048 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7049                                 enum bptype type)
7050 {
7051   struct symtab_and_line sal;
7052
7053   sal = find_pc_line (pc, 0);
7054   sal.pc = pc;
7055   sal.section = find_pc_overlay (pc);
7056   sal.explicit_pc = 1;
7057
7058   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
7059 }
7060 \f
7061
7062 /* Tell the user we have just set a breakpoint B.  */
7063
7064 static void
7065 mention (struct breakpoint *b)
7066 {
7067   b->ops->print_mention (b);
7068   if (ui_out_is_mi_like_p (current_uiout))
7069     return;
7070   printf_filtered ("\n");
7071 }
7072 \f
7073
7074 static struct bp_location *
7075 add_location_to_breakpoint (struct breakpoint *b,
7076                             const struct symtab_and_line *sal)
7077 {
7078   struct bp_location *loc, **tmp;
7079   CORE_ADDR adjusted_address;
7080   struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
7081
7082   if (loc_gdbarch == NULL)
7083     loc_gdbarch = b->gdbarch;
7084
7085   /* Adjust the breakpoint's address prior to allocating a location.
7086      Once we call allocate_bp_location(), that mostly uninitialized
7087      location will be placed on the location chain.  Adjustment of the
7088      breakpoint may cause target_read_memory() to be called and we do
7089      not want its scan of the location chain to find a breakpoint and
7090      location that's only been partially initialized.  */
7091   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
7092                                                 sal->pc, b->type);
7093
7094   loc = allocate_bp_location (b);
7095   for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7096     ;
7097   *tmp = loc;
7098
7099   loc->requested_address = sal->pc;
7100   loc->address = adjusted_address;
7101   loc->pspace = sal->pspace;
7102   gdb_assert (loc->pspace != NULL);
7103   loc->section = sal->section;
7104   loc->gdbarch = loc_gdbarch;
7105   set_breakpoint_location_function (loc,
7106                                     sal->explicit_pc || sal->explicit_line);
7107   return loc;
7108 }
7109 \f
7110
7111 /* Return 1 if LOC is pointing to a permanent breakpoint, 
7112    return 0 otherwise.  */
7113
7114 static int
7115 bp_loc_is_permanent (struct bp_location *loc)
7116 {
7117   int len;
7118   CORE_ADDR addr;
7119   const gdb_byte *brk;
7120   gdb_byte *target_mem;
7121   struct cleanup *cleanup;
7122   int retval = 0;
7123
7124   gdb_assert (loc != NULL);
7125
7126   addr = loc->address;
7127   brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
7128
7129   /* Software breakpoints unsupported?  */
7130   if (brk == NULL)
7131     return 0;
7132
7133   target_mem = alloca (len);
7134
7135   /* Enable the automatic memory restoration from breakpoints while
7136      we read the memory.  Otherwise we could say about our temporary
7137      breakpoints they are permanent.  */
7138   cleanup = save_current_space_and_thread ();
7139
7140   switch_to_program_space_and_thread (loc->pspace);
7141   make_show_memory_breakpoints_cleanup (0);
7142
7143   if (target_read_memory (loc->address, target_mem, len) == 0
7144       && memcmp (target_mem, brk, len) == 0)
7145     retval = 1;
7146
7147   do_cleanups (cleanup);
7148
7149   return retval;
7150 }
7151
7152
7153
7154 /* Create a breakpoint with SAL as location.  Use ADDR_STRING
7155    as textual description of the location, and COND_STRING
7156    as condition expression.  */
7157
7158 static void
7159 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7160                      struct symtabs_and_lines sals, char *addr_string,
7161                      char *cond_string,
7162                      enum bptype type, enum bpdisp disposition,
7163                      int thread, int task, int ignore_count,
7164                      const struct breakpoint_ops *ops, int from_tty,
7165                      int enabled, int internal, int display_canonical)
7166 {
7167   int i;
7168
7169   if (type == bp_hardware_breakpoint)
7170     {
7171       int i = hw_breakpoint_used_count ();
7172       int target_resources_ok = 
7173         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
7174                                             i + 1, 0);
7175       if (target_resources_ok == 0)
7176         error (_("No hardware breakpoint support in the target."));
7177       else if (target_resources_ok < 0)
7178         error (_("Hardware breakpoints used exceeds limit."));
7179     }
7180
7181   gdb_assert (sals.nelts > 0);
7182
7183   for (i = 0; i < sals.nelts; ++i)
7184     {
7185       struct symtab_and_line sal = sals.sals[i];
7186       struct bp_location *loc;
7187
7188       if (from_tty)
7189         {
7190           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7191           if (!loc_gdbarch)
7192             loc_gdbarch = gdbarch;
7193
7194           describe_other_breakpoints (loc_gdbarch,
7195                                       sal.pspace, sal.pc, sal.section, thread);
7196         }
7197
7198       if (i == 0)
7199         {
7200           init_raw_breakpoint (b, gdbarch, sal, type, ops);
7201           b->thread = thread;
7202           b->task = task;
7203   
7204           b->cond_string = cond_string;
7205           b->ignore_count = ignore_count;
7206           b->enable_state = enabled ? bp_enabled : bp_disabled;
7207           b->disposition = disposition;
7208           b->pspace = sals.sals[0].pspace;
7209
7210           if (type == bp_static_tracepoint)
7211             {
7212               struct tracepoint *t = (struct tracepoint *) b;
7213               struct static_tracepoint_marker marker;
7214
7215               if (is_marker_spec (addr_string))
7216                 {
7217                   /* We already know the marker exists, otherwise, we
7218                      wouldn't see a sal for it.  */
7219                   char *p = &addr_string[3];
7220                   char *endp;
7221                   char *marker_str;
7222                   int i;
7223
7224                   p = skip_spaces (p);
7225
7226                   endp = skip_to_space (p);
7227
7228                   marker_str = savestring (p, endp - p);
7229                   t->static_trace_marker_id = marker_str;
7230
7231                   printf_filtered (_("Probed static tracepoint "
7232                                      "marker \"%s\"\n"),
7233                                    t->static_trace_marker_id);
7234                 }
7235               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7236                 {
7237                   t->static_trace_marker_id = xstrdup (marker.str_id);
7238                   release_static_tracepoint_marker (&marker);
7239
7240                   printf_filtered (_("Probed static tracepoint "
7241                                      "marker \"%s\"\n"),
7242                                    t->static_trace_marker_id);
7243                 }
7244               else
7245                 warning (_("Couldn't determine the static "
7246                            "tracepoint marker to probe"));
7247             }
7248
7249           if (enabled && b->pspace->executing_startup
7250               && (b->type == bp_breakpoint
7251                   || b->type == bp_hardware_breakpoint))
7252             b->enable_state = bp_startup_disabled;
7253
7254           loc = b->loc;
7255         }
7256       else
7257         {
7258           loc = add_location_to_breakpoint (b, &sal);
7259         }
7260
7261       if (bp_loc_is_permanent (loc))
7262         make_breakpoint_permanent (b);
7263
7264       if (b->cond_string)
7265         {
7266           char *arg = b->cond_string;
7267           loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
7268           if (*arg)
7269               error (_("Garbage %s follows condition"), arg);
7270         }
7271     }   
7272
7273   b->display_canonical = display_canonical;
7274   if (addr_string)
7275     b->addr_string = addr_string;
7276   else
7277     /* addr_string has to be used or breakpoint_re_set will delete
7278        me.  */
7279     b->addr_string
7280       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7281 }
7282
7283 static void
7284 create_breakpoint_sal (struct gdbarch *gdbarch,
7285                        struct symtabs_and_lines sals, char *addr_string,
7286                        char *cond_string,
7287                        enum bptype type, enum bpdisp disposition,
7288                        int thread, int task, int ignore_count,
7289                        const struct breakpoint_ops *ops, int from_tty,
7290                        int enabled, int internal, int display_canonical)
7291 {
7292   struct breakpoint *b;
7293   struct cleanup *old_chain;
7294
7295   if (is_tracepoint_type (type))
7296     {
7297       struct tracepoint *t;
7298
7299       t = XCNEW (struct tracepoint);
7300       b = &t->base;
7301     }
7302   else
7303     b = XNEW (struct breakpoint);
7304
7305   old_chain = make_cleanup (xfree, b);
7306
7307   init_breakpoint_sal (b, gdbarch,
7308                        sals, addr_string,
7309                        cond_string,
7310                        type, disposition,
7311                        thread, task, ignore_count,
7312                        ops, from_tty,
7313                        enabled, internal, display_canonical);
7314   discard_cleanups (old_chain);
7315
7316   install_breakpoint (internal, b);
7317 }
7318
7319 /* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7320    elements to fill the void space.  */
7321 static void
7322 remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
7323 {
7324   int i = index_to_remove+1;
7325   int last_index = sal->nelts-1;
7326
7327   for (;i <= last_index; ++i)
7328     sal->sals[i-1] = sal->sals[i];
7329
7330   --(sal->nelts);
7331 }
7332
7333 /* If appropriate, obtains all sals that correspond to the same file
7334    and line as SAL, in all program spaces.  Users debugging with IDEs,
7335    will want to set a breakpoint at foo.c:line, and not really care
7336    about program spaces.  This is done only if SAL does not have
7337    explicit PC and has line and file information.  If we got just a
7338    single expanded sal, return the original.
7339
7340    Otherwise, if SAL.explicit_line is not set, filter out all sals for
7341    which the name of enclosing function is different from SAL.  This
7342    makes sure that if we have breakpoint originally set in template
7343    instantiation, say foo<int>(), we won't expand SAL to locations at
7344    the same line in all existing instantiations of 'foo'.  */
7345
7346 static struct symtabs_and_lines
7347 expand_line_sal_maybe (struct symtab_and_line sal)
7348 {
7349   struct symtabs_and_lines expanded;
7350   CORE_ADDR original_pc = sal.pc;
7351   char *original_function = NULL;
7352   int found;
7353   int i;
7354   struct cleanup *old_chain;
7355
7356   /* If we have explicit pc, don't expand.
7357      If we have no line number, we can't expand.  */
7358   if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7359     {
7360       expanded.nelts = 1;
7361       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7362       expanded.sals[0] = sal;
7363       return expanded;
7364     }
7365
7366   sal.pc = 0;
7367
7368   old_chain = save_current_space_and_thread ();
7369
7370   switch_to_program_space_and_thread (sal.pspace);
7371
7372   find_pc_partial_function (original_pc, &original_function, NULL, NULL);
7373
7374   /* Note that expand_line_sal visits *all* program spaces.  */
7375   expanded = expand_line_sal (sal);
7376
7377   if (expanded.nelts == 1)
7378     {
7379       /* We had one sal, we got one sal.  Return that sal, adjusting it
7380          past the function prologue if necessary.  */
7381       xfree (expanded.sals);
7382       expanded.nelts = 1;
7383       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7384       sal.pc = original_pc;
7385       expanded.sals[0] = sal;
7386       skip_prologue_sal (&expanded.sals[0]);
7387       do_cleanups (old_chain);
7388       return expanded;      
7389     }
7390
7391   if (!sal.explicit_line)
7392     {
7393       CORE_ADDR func_addr, func_end;
7394       for (i = 0; i < expanded.nelts; ++i)
7395         {
7396           CORE_ADDR pc = expanded.sals[i].pc;
7397           char *this_function;
7398
7399           /* We need to switch threads as well since we're about to
7400              read memory.  */
7401           switch_to_program_space_and_thread (expanded.sals[i].pspace);
7402
7403           if (find_pc_partial_function (pc, &this_function, 
7404                                         &func_addr, &func_end))
7405             {
7406               if (this_function
7407                   && strcmp (this_function, original_function) != 0)
7408                 {
7409                   remove_sal (&expanded, i);
7410                   --i;
7411                 }
7412             }
7413         }
7414     }
7415
7416   /* Skip the function prologue if necessary.  */
7417   for (i = 0; i < expanded.nelts; ++i)
7418     skip_prologue_sal (&expanded.sals[i]);
7419
7420   do_cleanups (old_chain);
7421
7422   if (expanded.nelts <= 1)
7423     {
7424       /* This is an ugly workaround.  If we get zero expanded sals
7425          then something is really wrong.  Fix that by returning the
7426          original sal.  */
7427
7428       xfree (expanded.sals);
7429       expanded.nelts = 1;
7430       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7431       sal.pc = original_pc;
7432       expanded.sals[0] = sal;
7433       return expanded;      
7434     }
7435
7436   if (original_pc)
7437     {
7438       found = 0;
7439       for (i = 0; i < expanded.nelts; ++i)
7440         if (expanded.sals[i].pc == original_pc)
7441           {
7442             found = 1;
7443             break;
7444           }
7445       gdb_assert (found);
7446     }
7447
7448   return expanded;
7449 }
7450
7451 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
7452    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7453    value.  COND_STRING, if not NULL, specified the condition to be
7454    used for all breakpoints.  Essentially the only case where
7455    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7456    function.  In that case, it's still not possible to specify
7457    separate conditions for different overloaded functions, so
7458    we take just a single condition string.
7459    
7460    NOTE: If the function succeeds, the caller is expected to cleanup
7461    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
7462    array contents).  If the function fails (error() is called), the
7463    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
7464    COND and SALS arrays and each of those arrays contents.  */
7465
7466 static void
7467 create_breakpoints_sal (struct gdbarch *gdbarch,
7468                         struct symtabs_and_lines sals,
7469                         struct linespec_result *canonical,
7470                         char *cond_string,
7471                         enum bptype type, enum bpdisp disposition,
7472                         int thread, int task, int ignore_count,
7473                         const struct breakpoint_ops *ops, int from_tty,
7474                         int enabled, int internal)
7475 {
7476   int i;
7477
7478   for (i = 0; i < sals.nelts; ++i)
7479     {
7480       struct symtabs_and_lines expanded = 
7481         expand_line_sal_maybe (sals.sals[i]);
7482
7483       create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
7484                              cond_string, type, disposition,
7485                              thread, task, ignore_count, ops,
7486                              from_tty, enabled, internal,
7487                              canonical->special_display);
7488     }
7489 }
7490
7491 /* Parse ADDRESS which is assumed to be a SAL specification possibly
7492    followed by conditionals.  On return, SALS contains an array of SAL
7493    addresses found.  ADDR_STRING contains a vector of (canonical)
7494    address strings.  ADDRESS points to the end of the SAL.
7495
7496    The array and the line spec strings are allocated on the heap, it is
7497    the caller's responsibility to free them.  */
7498
7499 static void
7500 parse_breakpoint_sals (char **address,
7501                        struct symtabs_and_lines *sals,
7502                        struct linespec_result *canonical)
7503 {
7504   char *addr_start = *address;
7505
7506   /* If no arg given, or if first arg is 'if ', use the default
7507      breakpoint.  */
7508   if ((*address) == NULL
7509       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
7510     {
7511       /* The last displayed codepoint, if it's valid, is our default breakpoint
7512          address.  */
7513       if (last_displayed_sal_is_valid ())
7514         {
7515           struct symtab_and_line sal;
7516
7517           init_sal (&sal);              /* Initialize to zeroes.  */
7518           sals->sals = (struct symtab_and_line *)
7519             xmalloc (sizeof (struct symtab_and_line));
7520
7521           /* Set sal's pspace, pc, symtab, and line to the values
7522              corresponding to the last call to print_frame_info.  */
7523           get_last_displayed_sal (&sal);
7524           sal.section = find_pc_overlay (sal.pc);
7525
7526           /* "break" without arguments is equivalent to "break *PC"
7527              where PC is the last displayed codepoint's address.  So
7528              make sure to set sal.explicit_pc to prevent GDB from
7529              trying to expand the list of sals to include all other
7530              instances with the same symtab and line.  */
7531           sal.explicit_pc = 1;
7532
7533           sals->sals[0] = sal;
7534           sals->nelts = 1;
7535         }
7536       else
7537         error (_("No default breakpoint address now."));
7538     }
7539   else
7540     {
7541       /* Force almost all breakpoints to be in terms of the
7542          current_source_symtab (which is decode_line_1's default).
7543          This should produce the results we want almost all of the
7544          time while leaving the last displayed codepoint pointers
7545          alone.
7546
7547          ObjC: However, don't match an Objective-C method name which
7548          may have a '+' or '-' succeeded by a '[' */
7549          
7550       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
7551                         
7552       if (last_displayed_sal_is_valid ()
7553           && (!cursal.symtab
7554               || ((strchr ("+-", (*address)[0]) != NULL)
7555                   && ((*address)[1] != '['))))
7556         *sals = decode_line_1 (address, 1,
7557                                get_last_displayed_symtab (),
7558                                get_last_displayed_line (),
7559                                canonical);
7560       else
7561         *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7562                                canonical);
7563     }
7564   /* For any SAL that didn't have a canonical string, fill one in.  */
7565   if (sals->nelts > 0 && canonical->canonical == NULL)
7566     canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
7567   if (addr_start != (*address))
7568     {
7569       int i;
7570
7571       for (i = 0; i < sals->nelts; i++)
7572         {
7573           /* Add the string if not present.  */
7574           if (canonical->canonical[i] == NULL)
7575             canonical->canonical[i] = savestring (addr_start, 
7576                                                   (*address) - addr_start);
7577         }
7578     }
7579 }
7580
7581
7582 /* Convert each SAL into a real PC.  Verify that the PC can be
7583    inserted as a breakpoint.  If it can't throw an error.  */
7584
7585 static void
7586 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
7587 {    
7588   int i;
7589
7590   for (i = 0; i < sals->nelts; i++)
7591     resolve_sal_pc (&sals->sals[i]);
7592 }
7593
7594 /* Fast tracepoints may have restrictions on valid locations.  For
7595    instance, a fast tracepoint using a jump instead of a trap will
7596    likely have to overwrite more bytes than a trap would, and so can
7597    only be placed where the instruction is longer than the jump, or a
7598    multi-instruction sequence does not have a jump into the middle of
7599    it, etc.  */
7600
7601 static void
7602 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7603                             struct symtabs_and_lines *sals)
7604 {
7605   int i, rslt;
7606   struct symtab_and_line *sal;
7607   char *msg;
7608   struct cleanup *old_chain;
7609
7610   for (i = 0; i < sals->nelts; i++)
7611     {
7612       sal = &sals->sals[i];
7613
7614       rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7615                                                NULL, &msg);
7616       old_chain = make_cleanup (xfree, msg);
7617
7618       if (!rslt)
7619         error (_("May not have a fast tracepoint at 0x%s%s"),
7620                paddress (gdbarch, sal->pc), (msg ? msg : ""));
7621
7622       do_cleanups (old_chain);
7623     }
7624 }
7625
7626 /* Given TOK, a string specification of condition and thread, as
7627    accepted by the 'break' command, extract the condition
7628    string and thread number and set *COND_STRING and *THREAD.
7629    PC identifies the context at which the condition should be parsed.
7630    If no condition is found, *COND_STRING is set to NULL.
7631    If no thread is found, *THREAD is set to -1.  */
7632 static void 
7633 find_condition_and_thread (char *tok, CORE_ADDR pc, 
7634                            char **cond_string, int *thread, int *task)
7635 {
7636   *cond_string = NULL;
7637   *thread = -1;
7638   while (tok && *tok)
7639     {
7640       char *end_tok;
7641       int toklen;
7642       char *cond_start = NULL;
7643       char *cond_end = NULL;
7644
7645       tok = skip_spaces (tok);
7646       
7647       end_tok = skip_to_space (tok);
7648       
7649       toklen = end_tok - tok;
7650       
7651       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7652         {
7653           struct expression *expr;
7654
7655           tok = cond_start = end_tok + 1;
7656           expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7657           xfree (expr);
7658           cond_end = tok;
7659           *cond_string = savestring (cond_start, 
7660                                      cond_end - cond_start);
7661         }
7662       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7663         {
7664           char *tmptok;
7665           
7666           tok = end_tok + 1;
7667           tmptok = tok;
7668           *thread = strtol (tok, &tok, 0);
7669           if (tok == tmptok)
7670             error (_("Junk after thread keyword."));
7671           if (!valid_thread_id (*thread))
7672             error (_("Unknown thread %d."), *thread);
7673         }
7674       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7675         {
7676           char *tmptok;
7677
7678           tok = end_tok + 1;
7679           tmptok = tok;
7680           *task = strtol (tok, &tok, 0);
7681           if (tok == tmptok)
7682             error (_("Junk after task keyword."));
7683           if (!valid_task_id (*task))
7684             error (_("Unknown task %d."), *task);
7685         }
7686       else
7687         error (_("Junk at end of arguments."));
7688     }
7689 }
7690
7691 /* Decode a static tracepoint marker spec.  */
7692
7693 static struct symtabs_and_lines
7694 decode_static_tracepoint_spec (char **arg_p)
7695 {
7696   VEC(static_tracepoint_marker_p) *markers = NULL;
7697   struct symtabs_and_lines sals;
7698   struct symtab_and_line sal;
7699   struct symbol *sym;
7700   struct cleanup *old_chain;
7701   char *p = &(*arg_p)[3];
7702   char *endp;
7703   char *marker_str;
7704   int i;
7705
7706   p = skip_spaces (p);
7707
7708   endp = skip_to_space (p);
7709
7710   marker_str = savestring (p, endp - p);
7711   old_chain = make_cleanup (xfree, marker_str);
7712
7713   markers = target_static_tracepoint_markers_by_strid (marker_str);
7714   if (VEC_empty(static_tracepoint_marker_p, markers))
7715     error (_("No known static tracepoint marker named %s"), marker_str);
7716
7717   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7718   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7719
7720   for (i = 0; i < sals.nelts; i++)
7721     {
7722       struct static_tracepoint_marker *marker;
7723
7724       marker = VEC_index (static_tracepoint_marker_p, markers, i);
7725
7726       init_sal (&sals.sals[i]);
7727
7728       sals.sals[i] = find_pc_line (marker->address, 0);
7729       sals.sals[i].pc = marker->address;
7730
7731       release_static_tracepoint_marker (marker);
7732     }
7733
7734   do_cleanups (old_chain);
7735
7736   *arg_p = endp;
7737   return sals;
7738 }
7739
7740 /* Set a breakpoint.  This function is shared between CLI and MI
7741    functions for setting a breakpoint.  This function has two major
7742    modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7743    parameter.  If non-zero, the function will parse arg, extracting
7744    breakpoint location, address and thread.  Otherwise, ARG is just
7745    the location of breakpoint, with condition and thread specified by
7746    the COND_STRING and THREAD parameters.  If INTERNAL is non-zero,
7747    the breakpoint number will be allocated from the internal
7748    breakpoint count.  Returns true if any breakpoint was created;
7749    false otherwise.  */
7750
7751 int
7752 create_breakpoint (struct gdbarch *gdbarch,
7753                    char *arg, char *cond_string, int thread,
7754                    int parse_condition_and_thread,
7755                    int tempflag, enum bptype type_wanted,
7756                    int ignore_count,
7757                    enum auto_boolean pending_break_support,
7758                    const struct breakpoint_ops *ops,
7759                    int from_tty, int enabled, int internal)
7760 {
7761   volatile struct gdb_exception e;
7762   struct symtabs_and_lines sals;
7763   struct symtab_and_line pending_sal;
7764   char *copy_arg;
7765   char *addr_start = arg;
7766   struct linespec_result canonical;
7767   struct cleanup *old_chain;
7768   struct cleanup *bkpt_chain = NULL;
7769   int i;
7770   int pending = 0;
7771   int task = 0;
7772   int prev_bkpt_count = breakpoint_count;
7773
7774   gdb_assert (ops != NULL);
7775
7776   sals.sals = NULL;
7777   sals.nelts = 0;
7778   init_linespec_result (&canonical);
7779
7780   if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7781     {
7782       int i;
7783
7784       sals = decode_static_tracepoint_spec (&arg);
7785
7786       copy_arg = savestring (addr_start, arg - addr_start);
7787       canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
7788       for (i = 0; i < sals.nelts; i++)
7789         canonical.canonical[i] = xstrdup (copy_arg);
7790       goto done;
7791     }
7792
7793   TRY_CATCH (e, RETURN_MASK_ALL)
7794     {
7795       parse_breakpoint_sals (&arg, &sals, &canonical);
7796     }
7797
7798   /* If caller is interested in rc value from parse, set value.  */
7799   switch (e.reason)
7800     {
7801     case RETURN_QUIT:
7802       throw_exception (e);
7803     case RETURN_ERROR:
7804       switch (e.error)
7805         {
7806         case NOT_FOUND_ERROR:
7807
7808           /* If pending breakpoint support is turned off, throw
7809              error.  */
7810
7811           if (pending_break_support == AUTO_BOOLEAN_FALSE)
7812             throw_exception (e);
7813
7814           exception_print (gdb_stderr, e);
7815
7816           /* If pending breakpoint support is auto query and the user
7817              selects no, then simply return the error code.  */
7818           if (pending_break_support == AUTO_BOOLEAN_AUTO
7819               && !nquery (_("Make breakpoint pending on "
7820                             "future shared library load? ")))
7821             return 0;
7822
7823           /* At this point, either the user was queried about setting
7824              a pending breakpoint and selected yes, or pending
7825              breakpoint behavior is on and thus a pending breakpoint
7826              is defaulted on behalf of the user.  */
7827           copy_arg = xstrdup (addr_start);
7828           canonical.canonical = &copy_arg;
7829           sals.nelts = 1;
7830           sals.sals = &pending_sal;
7831           pending_sal.pc = 0;
7832           pending = 1;
7833           break;
7834         default:
7835           throw_exception (e);
7836         }
7837       break;
7838     default:
7839       if (!sals.nelts)
7840         return 0;
7841     }
7842
7843   done:
7844
7845   /* Create a chain of things that always need to be cleaned up.  */
7846   old_chain = make_cleanup (null_cleanup, 0);
7847
7848   if (!pending)
7849     {
7850       /* Make sure that all storage allocated to SALS gets freed.  */
7851       make_cleanup (xfree, sals.sals);
7852       
7853       /* Cleanup the canonical array but not its contents.  */
7854       make_cleanup (xfree, canonical.canonical);
7855     }
7856
7857   /* ----------------------------- SNIP -----------------------------
7858      Anything added to the cleanup chain beyond this point is assumed
7859      to be part of a breakpoint.  If the breakpoint create succeeds
7860      then the memory is not reclaimed.  */
7861   bkpt_chain = make_cleanup (null_cleanup, 0);
7862
7863   /* Mark the contents of the canonical for cleanup.  These go on
7864      the bkpt_chain and only occur if the breakpoint create fails.  */
7865   for (i = 0; i < sals.nelts; i++)
7866     {
7867       if (canonical.canonical[i] != NULL)
7868         make_cleanup (xfree, canonical.canonical[i]);
7869     }
7870
7871   /* Resolve all line numbers to PC's and verify that the addresses
7872      are ok for the target.  */
7873   if (!pending)
7874     breakpoint_sals_to_pc (&sals);
7875
7876   /* Fast tracepoints may have additional restrictions on location.  */
7877   if (type_wanted == bp_fast_tracepoint)
7878     check_fast_tracepoint_sals (gdbarch, &sals);
7879
7880   /* Verify that condition can be parsed, before setting any
7881      breakpoints.  Allocate a separate condition expression for each
7882      breakpoint.  */
7883   if (!pending)
7884     {
7885       if (parse_condition_and_thread)
7886         {
7887             /* Here we only parse 'arg' to separate condition
7888                from thread number, so parsing in context of first
7889                sal is OK.  When setting the breakpoint we'll 
7890                re-parse it in context of each sal.  */
7891             cond_string = NULL;
7892             thread = -1;
7893             find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7894                                        &thread, &task);
7895             if (cond_string)
7896                 make_cleanup (xfree, cond_string);
7897         }
7898       else
7899         {
7900             /* Create a private copy of condition string.  */
7901             if (cond_string)
7902             {
7903                 cond_string = xstrdup (cond_string);
7904                 make_cleanup (xfree, cond_string);
7905             }
7906         }
7907
7908       /* If the user is creating a static tracepoint by marker id
7909          (strace -m MARKER_ID), then store the sals index, so that
7910          breakpoint_re_set can try to match up which of the newly
7911          found markers corresponds to this one, and, don't try to
7912          expand multiple locations for each sal, given than SALS
7913          already should contain all sals for MARKER_ID.  */
7914       if (type_wanted == bp_static_tracepoint
7915           && is_marker_spec (canonical.canonical[0]))
7916         {
7917           int i;
7918
7919           for (i = 0; i < sals.nelts; ++i)
7920             {
7921               struct symtabs_and_lines expanded;
7922               struct tracepoint *tp;
7923               struct cleanup *old_chain;
7924
7925               expanded.nelts = 1;
7926               expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7927               expanded.sals[0] = sals.sals[i];
7928               old_chain = make_cleanup (xfree, expanded.sals);
7929
7930               tp = XCNEW (struct tracepoint);
7931               init_breakpoint_sal (&tp->base, gdbarch, expanded,
7932                                    canonical.canonical[i],
7933                                    cond_string, type_wanted,
7934                                    tempflag ? disp_del : disp_donttouch,
7935                                    thread, task, ignore_count, ops,
7936                                    from_tty, enabled, internal,
7937                                    canonical.special_display);
7938               /* Given that its possible to have multiple markers with
7939                  the same string id, if the user is creating a static
7940                  tracepoint by marker id ("strace -m MARKER_ID"), then
7941                  store the sals index, so that breakpoint_re_set can
7942                  try to match up which of the newly found markers
7943                  corresponds to this one  */
7944               tp->static_trace_marker_id_idx = i;
7945
7946               install_breakpoint (internal, &tp->base);
7947
7948               do_cleanups (old_chain);
7949             }
7950         }
7951       else
7952         create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
7953                                 type_wanted,
7954                                 tempflag ? disp_del : disp_donttouch,
7955                                 thread, task, ignore_count, ops, from_tty,
7956                                 enabled, internal);
7957     }
7958   else
7959     {
7960       struct breakpoint *b;
7961
7962       make_cleanup (xfree, copy_arg);
7963
7964       b = set_raw_breakpoint_without_location (gdbarch, type_wanted, ops);
7965       set_breakpoint_number (internal, b);
7966       b->thread = -1;
7967       b->addr_string = canonical.canonical[0];
7968       b->cond_string = NULL;
7969       b->ignore_count = ignore_count;
7970       b->disposition = tempflag ? disp_del : disp_donttouch;
7971       b->condition_not_parsed = 1;
7972       b->enable_state = enabled ? bp_enabled : bp_disabled;
7973       b->pspace = current_program_space;
7974       b->py_bp_object = NULL;
7975
7976       if (enabled && b->pspace->executing_startup
7977           && (b->type == bp_breakpoint
7978               || b->type == bp_hardware_breakpoint))
7979         b->enable_state = bp_startup_disabled;
7980
7981       if (!internal)
7982         /* Do not mention breakpoints with a negative number, 
7983            but do notify observers.  */
7984         mention (b);
7985       observer_notify_breakpoint_created (b);
7986     }
7987   
7988   if (sals.nelts > 1)
7989     {
7990       warning (_("Multiple breakpoints were set.\nUse the "
7991                  "\"delete\" command to delete unwanted breakpoints."));
7992       prev_breakpoint_count = prev_bkpt_count;
7993     }
7994
7995   /* That's it.  Discard the cleanups for data inserted into the
7996      breakpoint.  */
7997   discard_cleanups (bkpt_chain);
7998   /* But cleanup everything else.  */
7999   do_cleanups (old_chain);
8000
8001   /* error call may happen here - have BKPT_CHAIN already discarded.  */
8002   update_global_location_list (1);
8003
8004   return 1;
8005 }
8006
8007 /* Set a breakpoint.
8008    ARG is a string describing breakpoint address,
8009    condition, and thread.
8010    FLAG specifies if a breakpoint is hardware on,
8011    and if breakpoint is temporary, using BP_HARDWARE_FLAG
8012    and BP_TEMPFLAG.  */
8013
8014 static void
8015 break_command_1 (char *arg, int flag, int from_tty)
8016 {
8017   int tempflag = flag & BP_TEMPFLAG;
8018   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8019                              ? bp_hardware_breakpoint
8020                              : bp_breakpoint);
8021
8022   create_breakpoint (get_current_arch (),
8023                      arg,
8024                      NULL, 0, 1 /* parse arg */,
8025                      tempflag, type_wanted,
8026                      0 /* Ignore count */,
8027                      pending_break_support,
8028                      &bkpt_breakpoint_ops,
8029                      from_tty,
8030                      1 /* enabled */,
8031                      0 /* internal */);
8032 }
8033
8034 /* Helper function for break_command_1 and disassemble_command.  */
8035
8036 void
8037 resolve_sal_pc (struct symtab_and_line *sal)
8038 {
8039   CORE_ADDR pc;
8040
8041   if (sal->pc == 0 && sal->symtab != NULL)
8042     {
8043       if (!find_line_pc (sal->symtab, sal->line, &pc))
8044         error (_("No line %d in file \"%s\"."),
8045                sal->line, sal->symtab->filename);
8046       sal->pc = pc;
8047
8048       /* If this SAL corresponds to a breakpoint inserted using a line
8049          number, then skip the function prologue if necessary.  */
8050       if (sal->explicit_line)
8051         skip_prologue_sal (sal);
8052     }
8053
8054   if (sal->section == 0 && sal->symtab != NULL)
8055     {
8056       struct blockvector *bv;
8057       struct block *b;
8058       struct symbol *sym;
8059
8060       bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
8061       if (bv != NULL)
8062         {
8063           sym = block_linkage_function (b);
8064           if (sym != NULL)
8065             {
8066               fixup_symbol_section (sym, sal->symtab->objfile);
8067               sal->section = SYMBOL_OBJ_SECTION (sym);
8068             }
8069           else
8070             {
8071               /* It really is worthwhile to have the section, so we'll
8072                  just have to look harder. This case can be executed
8073                  if we have line numbers but no functions (as can
8074                  happen in assembly source).  */
8075
8076               struct minimal_symbol *msym;
8077               struct cleanup *old_chain = save_current_space_and_thread ();
8078
8079               switch_to_program_space_and_thread (sal->pspace);
8080
8081               msym = lookup_minimal_symbol_by_pc (sal->pc);
8082               if (msym)
8083                 sal->section = SYMBOL_OBJ_SECTION (msym);
8084
8085               do_cleanups (old_chain);
8086             }
8087         }
8088     }
8089 }
8090
8091 void
8092 break_command (char *arg, int from_tty)
8093 {
8094   break_command_1 (arg, 0, from_tty);
8095 }
8096
8097 void
8098 tbreak_command (char *arg, int from_tty)
8099 {
8100   break_command_1 (arg, BP_TEMPFLAG, from_tty);
8101 }
8102
8103 static void
8104 hbreak_command (char *arg, int from_tty)
8105 {
8106   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
8107 }
8108
8109 static void
8110 thbreak_command (char *arg, int from_tty)
8111 {
8112   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
8113 }
8114
8115 static void
8116 stop_command (char *arg, int from_tty)
8117 {
8118   printf_filtered (_("Specify the type of breakpoint to set.\n\
8119 Usage: stop in <function | address>\n\
8120        stop at <line>\n"));
8121 }
8122
8123 static void
8124 stopin_command (char *arg, int from_tty)
8125 {
8126   int badInput = 0;
8127
8128   if (arg == (char *) NULL)
8129     badInput = 1;
8130   else if (*arg != '*')
8131     {
8132       char *argptr = arg;
8133       int hasColon = 0;
8134
8135       /* Look for a ':'.  If this is a line number specification, then
8136          say it is bad, otherwise, it should be an address or
8137          function/method name.  */
8138       while (*argptr && !hasColon)
8139         {
8140           hasColon = (*argptr == ':');
8141           argptr++;
8142         }
8143
8144       if (hasColon)
8145         badInput = (*argptr != ':');    /* Not a class::method */
8146       else
8147         badInput = isdigit (*arg);      /* a simple line number */
8148     }
8149
8150   if (badInput)
8151     printf_filtered (_("Usage: stop in <function | address>\n"));
8152   else
8153     break_command_1 (arg, 0, from_tty);
8154 }
8155
8156 static void
8157 stopat_command (char *arg, int from_tty)
8158 {
8159   int badInput = 0;
8160
8161   if (arg == (char *) NULL || *arg == '*')      /* no line number */
8162     badInput = 1;
8163   else
8164     {
8165       char *argptr = arg;
8166       int hasColon = 0;
8167
8168       /* Look for a ':'.  If there is a '::' then get out, otherwise
8169          it is probably a line number.  */
8170       while (*argptr && !hasColon)
8171         {
8172           hasColon = (*argptr == ':');
8173           argptr++;
8174         }
8175
8176       if (hasColon)
8177         badInput = (*argptr == ':');    /* we have class::method */
8178       else
8179         badInput = !isdigit (*arg);     /* not a line number */
8180     }
8181
8182   if (badInput)
8183     printf_filtered (_("Usage: stop at <line>\n"));
8184   else
8185     break_command_1 (arg, 0, from_tty);
8186 }
8187
8188 /* Implement the "breakpoint_hit" breakpoint_ops method for
8189    ranged breakpoints.  */
8190
8191 static int
8192 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8193                                   struct address_space *aspace,
8194                                   CORE_ADDR bp_addr)
8195 {
8196   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8197                                          bl->length, aspace, bp_addr);
8198 }
8199
8200 /* Implement the "resources_needed" breakpoint_ops method for
8201    ranged breakpoints.  */
8202
8203 static int
8204 resources_needed_ranged_breakpoint (const struct bp_location *bl)
8205 {
8206   return target_ranged_break_num_registers ();
8207 }
8208
8209 /* Implement the "print_it" breakpoint_ops method for
8210    ranged breakpoints.  */
8211
8212 static enum print_stop_action
8213 print_it_ranged_breakpoint (bpstat bs)
8214 {
8215   struct breakpoint *b = bs->breakpoint_at;
8216   struct bp_location *bl = b->loc;
8217   struct ui_out *uiout = current_uiout;
8218
8219   gdb_assert (b->type == bp_hardware_breakpoint);
8220
8221   /* Ranged breakpoints have only one location.  */
8222   gdb_assert (bl && bl->next == NULL);
8223
8224   annotate_breakpoint (b->number);
8225   if (b->disposition == disp_del)
8226     ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8227   else
8228     ui_out_text (uiout, "\nRanged breakpoint ");
8229   if (ui_out_is_mi_like_p (uiout))
8230     {
8231       ui_out_field_string (uiout, "reason",
8232                       async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8233       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8234     }
8235   ui_out_field_int (uiout, "bkptno", b->number);
8236   ui_out_text (uiout, ", ");
8237
8238   return PRINT_SRC_AND_LOC;
8239 }
8240
8241 /* Implement the "print_one" breakpoint_ops method for
8242    ranged breakpoints.  */
8243
8244 static void
8245 print_one_ranged_breakpoint (struct breakpoint *b,
8246                              struct bp_location **last_loc)
8247 {
8248   struct bp_location *bl = b->loc;
8249   struct value_print_options opts;
8250   struct ui_out *uiout = current_uiout;
8251
8252   /* Ranged breakpoints have only one location.  */
8253   gdb_assert (bl && bl->next == NULL);
8254
8255   get_user_print_options (&opts);
8256
8257   if (opts.addressprint)
8258     /* We don't print the address range here, it will be printed later
8259        by print_one_detail_ranged_breakpoint.  */
8260     ui_out_field_skip (uiout, "addr");
8261   annotate_field (5);
8262   print_breakpoint_location (b, bl);
8263   *last_loc = bl;
8264 }
8265
8266 /* Implement the "print_one_detail" breakpoint_ops method for
8267    ranged breakpoints.  */
8268
8269 static void
8270 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8271                                     struct ui_out *uiout)
8272 {
8273   CORE_ADDR address_start, address_end;
8274   struct bp_location *bl = b->loc;
8275   struct ui_stream *stb = ui_out_stream_new (uiout);
8276   struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8277
8278   gdb_assert (bl);
8279
8280   address_start = bl->address;
8281   address_end = address_start + bl->length - 1;
8282
8283   ui_out_text (uiout, "\taddress range: ");
8284   fprintf_unfiltered (stb->stream, "[%s, %s]",
8285                       print_core_address (bl->gdbarch, address_start),
8286                       print_core_address (bl->gdbarch, address_end));
8287   ui_out_field_stream (uiout, "addr", stb);
8288   ui_out_text (uiout, "\n");
8289
8290   do_cleanups (cleanup);
8291 }
8292
8293 /* Implement the "print_mention" breakpoint_ops method for
8294    ranged breakpoints.  */
8295
8296 static void
8297 print_mention_ranged_breakpoint (struct breakpoint *b)
8298 {
8299   struct bp_location *bl = b->loc;
8300   struct ui_out *uiout = current_uiout;
8301
8302   gdb_assert (bl);
8303   gdb_assert (b->type == bp_hardware_breakpoint);
8304
8305   if (ui_out_is_mi_like_p (uiout))
8306     return;
8307
8308   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8309                    b->number, paddress (bl->gdbarch, bl->address),
8310                    paddress (bl->gdbarch, bl->address + bl->length - 1));
8311 }
8312
8313 /* Implement the "print_recreate" breakpoint_ops method for
8314    ranged breakpoints.  */
8315
8316 static void
8317 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8318 {
8319   fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8320                       b->addr_string_range_end);
8321   print_recreate_thread (b, fp);
8322 }
8323
8324 /* The breakpoint_ops structure to be used in ranged breakpoints.  */
8325
8326 static struct breakpoint_ops ranged_breakpoint_ops;
8327
8328 /* Find the address where the end of the breakpoint range should be
8329    placed, given the SAL of the end of the range.  This is so that if
8330    the user provides a line number, the end of the range is set to the
8331    last instruction of the given line.  */
8332
8333 static CORE_ADDR
8334 find_breakpoint_range_end (struct symtab_and_line sal)
8335 {
8336   CORE_ADDR end;
8337
8338   /* If the user provided a PC value, use it.  Otherwise,
8339      find the address of the end of the given location.  */
8340   if (sal.explicit_pc)
8341     end = sal.pc;
8342   else
8343     {
8344       int ret;
8345       CORE_ADDR start;
8346
8347       ret = find_line_pc_range (sal, &start, &end);
8348       if (!ret)
8349         error (_("Could not find location of the end of the range."));
8350
8351       /* find_line_pc_range returns the start of the next line.  */
8352       end--;
8353     }
8354
8355   return end;
8356 }
8357
8358 /* Implement the "break-range" CLI command.  */
8359
8360 static void
8361 break_range_command (char *arg, int from_tty)
8362 {
8363   char *arg_start, *addr_string_start, *addr_string_end;
8364   struct linespec_result canonical_start, canonical_end;
8365   int bp_count, can_use_bp, length;
8366   CORE_ADDR end;
8367   struct breakpoint *b;
8368   struct symtab_and_line sal_start, sal_end;
8369   struct symtabs_and_lines sals_start, sals_end;
8370   struct cleanup *cleanup_bkpt;
8371
8372   /* We don't support software ranged breakpoints.  */
8373   if (target_ranged_break_num_registers () < 0)
8374     error (_("This target does not support hardware ranged breakpoints."));
8375
8376   bp_count = hw_breakpoint_used_count ();
8377   bp_count += target_ranged_break_num_registers ();
8378   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8379                                                    bp_count, 0);
8380   if (can_use_bp < 0)
8381     error (_("Hardware breakpoints used exceeds limit."));
8382
8383   if (arg == NULL || arg[0] == '\0')
8384     error(_("No address range specified."));
8385
8386   sals_start.sals = NULL;
8387   sals_start.nelts = 0;
8388   init_linespec_result (&canonical_start);
8389
8390   while (*arg == ' ' || *arg == '\t')
8391     arg++;
8392
8393   parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
8394
8395   sal_start = sals_start.sals[0];
8396   addr_string_start = canonical_start.canonical[0];
8397   cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8398   xfree (sals_start.sals);
8399   xfree (canonical_start.canonical);
8400
8401   if (arg[0] != ',')
8402     error (_("Too few arguments."));
8403   else if (sals_start.nelts == 0)
8404     error (_("Could not find location of the beginning of the range."));
8405   else if (sals_start.nelts != 1)
8406     error (_("Cannot create a ranged breakpoint with multiple locations."));
8407
8408   resolve_sal_pc (&sal_start);
8409
8410   arg++;        /* Skip the comma.  */
8411   while (*arg == ' ' || *arg == '\t')
8412     arg++;
8413
8414   /* Parse the end location.  */
8415
8416   sals_end.sals = NULL;
8417   sals_end.nelts = 0;
8418   init_linespec_result (&canonical_end);
8419   arg_start = arg;
8420
8421   /* We call decode_line_1 directly here instead of using
8422      parse_breakpoint_sals because we need to specify the start location's
8423      symtab and line as the default symtab and line for the end of the
8424      range.  This makes it possible to have ranges like "foo.c:27, +14",
8425      where +14 means 14 lines from the start location.  */
8426   sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
8427                             &canonical_end);
8428
8429   /* canonical_end can be NULL if it was of the form "*0xdeadbeef".  */
8430   if (canonical_end.canonical == NULL)
8431     canonical_end.canonical = xcalloc (1, sizeof (char *));
8432   /* Add the string if not present.  */
8433   if (arg_start != arg && canonical_end.canonical[0] == NULL)
8434     canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8435
8436   sal_end = sals_end.sals[0];
8437   addr_string_end = canonical_end.canonical[0];
8438   make_cleanup (xfree, addr_string_end);
8439   xfree (sals_end.sals);
8440   xfree (canonical_end.canonical);
8441
8442   if (sals_end.nelts == 0)
8443     error (_("Could not find location of the end of the range."));
8444   else if (sals_end.nelts != 1)
8445     error (_("Cannot create a ranged breakpoint with multiple locations."));
8446
8447   resolve_sal_pc (&sal_end);
8448
8449   end = find_breakpoint_range_end (sal_end);
8450   if (sal_start.pc > end)
8451     error (_("Invalid address range, end precedes start."));
8452
8453   length = end - sal_start.pc + 1;
8454   if (length < 0)
8455     /* Length overflowed.  */
8456     error (_("Address range too large."));
8457   else if (length == 1)
8458     {
8459       /* This range is simple enough to be handled by
8460          the `hbreak' command.  */
8461       hbreak_command (addr_string_start, 1);
8462
8463       do_cleanups (cleanup_bkpt);
8464
8465       return;
8466     }
8467
8468   /* Now set up the breakpoint.  */
8469   b = set_raw_breakpoint (get_current_arch (), sal_start,
8470                           bp_hardware_breakpoint, &ranged_breakpoint_ops);
8471   set_breakpoint_count (breakpoint_count + 1);
8472   b->number = breakpoint_count;
8473   b->disposition = disp_donttouch;
8474   b->addr_string = addr_string_start;
8475   b->addr_string_range_end = addr_string_end;
8476   b->loc->length = length;
8477
8478   discard_cleanups (cleanup_bkpt);
8479
8480   mention (b);
8481   observer_notify_breakpoint_created (b);
8482   update_global_location_list (1);
8483 }
8484
8485 /*  Return non-zero if EXP is verified as constant.  Returned zero
8486     means EXP is variable.  Also the constant detection may fail for
8487     some constant expressions and in such case still falsely return
8488     zero.  */
8489 static int
8490 watchpoint_exp_is_const (const struct expression *exp)
8491 {
8492   int i = exp->nelts;
8493
8494   while (i > 0)
8495     {
8496       int oplenp, argsp;
8497
8498       /* We are only interested in the descriptor of each element.  */
8499       operator_length (exp, i, &oplenp, &argsp);
8500       i -= oplenp;
8501
8502       switch (exp->elts[i].opcode)
8503         {
8504         case BINOP_ADD:
8505         case BINOP_SUB:
8506         case BINOP_MUL:
8507         case BINOP_DIV:
8508         case BINOP_REM:
8509         case BINOP_MOD:
8510         case BINOP_LSH:
8511         case BINOP_RSH:
8512         case BINOP_LOGICAL_AND:
8513         case BINOP_LOGICAL_OR:
8514         case BINOP_BITWISE_AND:
8515         case BINOP_BITWISE_IOR:
8516         case BINOP_BITWISE_XOR:
8517         case BINOP_EQUAL:
8518         case BINOP_NOTEQUAL:
8519         case BINOP_LESS:
8520         case BINOP_GTR:
8521         case BINOP_LEQ:
8522         case BINOP_GEQ:
8523         case BINOP_REPEAT:
8524         case BINOP_COMMA:
8525         case BINOP_EXP:
8526         case BINOP_MIN:
8527         case BINOP_MAX:
8528         case BINOP_INTDIV:
8529         case BINOP_CONCAT:
8530         case BINOP_IN:
8531         case BINOP_RANGE:
8532         case TERNOP_COND:
8533         case TERNOP_SLICE:
8534         case TERNOP_SLICE_COUNT:
8535
8536         case OP_LONG:
8537         case OP_DOUBLE:
8538         case OP_DECFLOAT:
8539         case OP_LAST:
8540         case OP_COMPLEX:
8541         case OP_STRING:
8542         case OP_BITSTRING:
8543         case OP_ARRAY:
8544         case OP_TYPE:
8545         case OP_NAME:
8546         case OP_OBJC_NSSTRING:
8547
8548         case UNOP_NEG:
8549         case UNOP_LOGICAL_NOT:
8550         case UNOP_COMPLEMENT:
8551         case UNOP_ADDR:
8552         case UNOP_HIGH:
8553           /* Unary, binary and ternary operators: We have to check
8554              their operands.  If they are constant, then so is the
8555              result of that operation.  For instance, if A and B are
8556              determined to be constants, then so is "A + B".
8557
8558              UNOP_IND is one exception to the rule above, because the
8559              value of *ADDR is not necessarily a constant, even when
8560              ADDR is.  */
8561           break;
8562
8563         case OP_VAR_VALUE:
8564           /* Check whether the associated symbol is a constant.
8565
8566              We use SYMBOL_CLASS rather than TYPE_CONST because it's
8567              possible that a buggy compiler could mark a variable as
8568              constant even when it is not, and TYPE_CONST would return
8569              true in this case, while SYMBOL_CLASS wouldn't.
8570
8571              We also have to check for function symbols because they
8572              are always constant.  */
8573           {
8574             struct symbol *s = exp->elts[i + 2].symbol;
8575
8576             if (SYMBOL_CLASS (s) != LOC_BLOCK
8577                 && SYMBOL_CLASS (s) != LOC_CONST
8578                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8579               return 0;
8580             break;
8581           }
8582
8583         /* The default action is to return 0 because we are using
8584            the optimistic approach here: If we don't know something,
8585            then it is not a constant.  */
8586         default:
8587           return 0;
8588         }
8589     }
8590
8591   return 1;
8592 }
8593
8594 /* Implement the "dtor" breakpoint_ops method for watchpoints.  */
8595
8596 static void
8597 dtor_watchpoint (struct breakpoint *self)
8598 {
8599   struct watchpoint *w = (struct watchpoint *) self;
8600
8601   xfree (w->cond_exp);
8602   xfree (w->exp);
8603   xfree (w->exp_string);
8604   xfree (w->exp_string_reparse);
8605   value_free (w->val);
8606
8607   base_breakpoint_ops.dtor (self);
8608 }
8609
8610 /* Implement the "re_set" breakpoint_ops method for watchpoints.  */
8611
8612 static void
8613 re_set_watchpoint (struct breakpoint *b)
8614 {
8615   struct watchpoint *w = (struct watchpoint *) b;
8616
8617   /* Watchpoint can be either on expression using entirely global
8618      variables, or it can be on local variables.
8619
8620      Watchpoints of the first kind are never auto-deleted, and even
8621      persist across program restarts.  Since they can use variables
8622      from shared libraries, we need to reparse expression as libraries
8623      are loaded and unloaded.
8624
8625      Watchpoints on local variables can also change meaning as result
8626      of solib event.  For example, if a watchpoint uses both a local
8627      and a global variables in expression, it's a local watchpoint,
8628      but unloading of a shared library will make the expression
8629      invalid.  This is not a very common use case, but we still
8630      re-evaluate expression, to avoid surprises to the user.
8631
8632      Note that for local watchpoints, we re-evaluate it only if
8633      watchpoints frame id is still valid.  If it's not, it means the
8634      watchpoint is out of scope and will be deleted soon.  In fact,
8635      I'm not sure we'll ever be called in this case.
8636
8637      If a local watchpoint's frame id is still valid, then
8638      w->exp_valid_block is likewise valid, and we can safely use it.
8639
8640      Don't do anything about disabled watchpoints, since they will be
8641      reevaluated again when enabled.  */
8642   update_watchpoint (w, 1 /* reparse */);
8643 }
8644
8645 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
8646
8647 static int
8648 insert_watchpoint (struct bp_location *bl)
8649 {
8650   struct watchpoint *w = (struct watchpoint *) bl->owner;
8651   int length = w->exact ? 1 : bl->length;
8652
8653   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8654                                    w->cond_exp);
8655 }
8656
8657 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
8658
8659 static int
8660 remove_watchpoint (struct bp_location *bl)
8661 {
8662   struct watchpoint *w = (struct watchpoint *) bl->owner;
8663   int length = w->exact ? 1 : bl->length;
8664
8665   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8666                                    w->cond_exp);
8667 }
8668
8669 static int
8670 breakpoint_hit_watchpoint (const struct bp_location *bl,
8671                            struct address_space *aspace, CORE_ADDR bp_addr)
8672 {
8673   struct breakpoint *b = bl->owner;
8674   struct watchpoint *w = (struct watchpoint *) b;
8675
8676   /* Continuable hardware watchpoints are treated as non-existent if the
8677      reason we stopped wasn't a hardware watchpoint (we didn't stop on
8678      some data address).  Otherwise gdb won't stop on a break instruction
8679      in the code (not from a breakpoint) when a hardware watchpoint has
8680      been defined.  Also skip watchpoints which we know did not trigger
8681      (did not match the data address).  */
8682   if (is_hardware_watchpoint (b)
8683       && w->watchpoint_triggered == watch_triggered_no)
8684     return 0;
8685
8686   return 1;
8687 }
8688
8689 static void
8690 check_status_watchpoint (bpstat bs)
8691 {
8692   gdb_assert (is_watchpoint (bs->breakpoint_at));
8693
8694   bpstat_check_watchpoint (bs);
8695 }
8696
8697 /* Implement the "resources_needed" breakpoint_ops method for
8698    hardware watchpoints.  */
8699
8700 static int
8701 resources_needed_watchpoint (const struct bp_location *bl)
8702 {
8703   struct watchpoint *w = (struct watchpoint *) bl->owner;
8704   int length = w->exact? 1 : bl->length;
8705
8706   return target_region_ok_for_hw_watchpoint (bl->address, length);
8707 }
8708
8709 /* Implement the "works_in_software_mode" breakpoint_ops method for
8710    hardware watchpoints.  */
8711
8712 static int
8713 works_in_software_mode_watchpoint (const struct breakpoint *b)
8714 {
8715   /* Read and access watchpoints only work with hardware support.  */
8716   return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
8717 }
8718
8719 static enum print_stop_action
8720 print_it_watchpoint (bpstat bs)
8721 {
8722   struct cleanup *old_chain;
8723   struct breakpoint *b;
8724   const struct bp_location *bl;
8725   struct ui_stream *stb;
8726   enum print_stop_action result;
8727   struct watchpoint *w;
8728   struct ui_out *uiout = current_uiout;
8729
8730   gdb_assert (bs->bp_location_at != NULL);
8731
8732   bl = bs->bp_location_at;
8733   b = bs->breakpoint_at;
8734   w = (struct watchpoint *) b;
8735
8736   stb = ui_out_stream_new (uiout);
8737   old_chain = make_cleanup_ui_out_stream_delete (stb);
8738
8739   switch (b->type)
8740     {
8741     case bp_watchpoint:
8742     case bp_hardware_watchpoint:
8743       annotate_watchpoint (b->number);
8744       if (ui_out_is_mi_like_p (uiout))
8745         ui_out_field_string
8746           (uiout, "reason",
8747            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8748       mention (b);
8749       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8750       ui_out_text (uiout, "\nOld value = ");
8751       watchpoint_value_print (bs->old_val, stb->stream);
8752       ui_out_field_stream (uiout, "old", stb);
8753       ui_out_text (uiout, "\nNew value = ");
8754       watchpoint_value_print (w->val, stb->stream);
8755       ui_out_field_stream (uiout, "new", stb);
8756       ui_out_text (uiout, "\n");
8757       /* More than one watchpoint may have been triggered.  */
8758       result = PRINT_UNKNOWN;
8759       break;
8760
8761     case bp_read_watchpoint:
8762       if (ui_out_is_mi_like_p (uiout))
8763         ui_out_field_string
8764           (uiout, "reason",
8765            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8766       mention (b);
8767       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8768       ui_out_text (uiout, "\nValue = ");
8769       watchpoint_value_print (w->val, stb->stream);
8770       ui_out_field_stream (uiout, "value", stb);
8771       ui_out_text (uiout, "\n");
8772       result = PRINT_UNKNOWN;
8773       break;
8774
8775     case bp_access_watchpoint:
8776       if (bs->old_val != NULL)
8777         {
8778           annotate_watchpoint (b->number);
8779           if (ui_out_is_mi_like_p (uiout))
8780             ui_out_field_string
8781               (uiout, "reason",
8782                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8783           mention (b);
8784           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8785           ui_out_text (uiout, "\nOld value = ");
8786           watchpoint_value_print (bs->old_val, stb->stream);
8787           ui_out_field_stream (uiout, "old", stb);
8788           ui_out_text (uiout, "\nNew value = ");
8789         }
8790       else
8791         {
8792           mention (b);
8793           if (ui_out_is_mi_like_p (uiout))
8794             ui_out_field_string
8795               (uiout, "reason",
8796                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8797           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8798           ui_out_text (uiout, "\nValue = ");
8799         }
8800       watchpoint_value_print (w->val, stb->stream);
8801       ui_out_field_stream (uiout, "new", stb);
8802       ui_out_text (uiout, "\n");
8803       result = PRINT_UNKNOWN;
8804       break;
8805     default:
8806       result = PRINT_UNKNOWN;
8807     }
8808
8809   do_cleanups (old_chain);
8810   return result;
8811 }
8812
8813 /* Implement the "print_mention" breakpoint_ops method for hardware
8814    watchpoints.  */
8815
8816 static void
8817 print_mention_watchpoint (struct breakpoint *b)
8818 {
8819   struct cleanup *ui_out_chain;
8820   struct watchpoint *w = (struct watchpoint *) b;
8821   struct ui_out *uiout = current_uiout;
8822
8823   switch (b->type)
8824     {
8825     case bp_watchpoint:
8826       ui_out_text (uiout, "Watchpoint ");
8827       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8828       break;
8829     case bp_hardware_watchpoint:
8830       ui_out_text (uiout, "Hardware watchpoint ");
8831       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8832       break;
8833     case bp_read_watchpoint:
8834       ui_out_text (uiout, "Hardware read watchpoint ");
8835       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8836       break;
8837     case bp_access_watchpoint:
8838       ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8839       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8840       break;
8841     default:
8842       internal_error (__FILE__, __LINE__,
8843                       _("Invalid hardware watchpoint type."));
8844     }
8845
8846   ui_out_field_int (uiout, "number", b->number);
8847   ui_out_text (uiout, ": ");
8848   ui_out_field_string (uiout, "exp", w->exp_string);
8849   do_cleanups (ui_out_chain);
8850 }
8851
8852 /* Implement the "print_recreate" breakpoint_ops method for
8853    watchpoints.  */
8854
8855 static void
8856 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8857 {
8858   struct watchpoint *w = (struct watchpoint *) b;
8859
8860   switch (b->type)
8861     {
8862     case bp_watchpoint:
8863     case bp_hardware_watchpoint:
8864       fprintf_unfiltered (fp, "watch");
8865       break;
8866     case bp_read_watchpoint:
8867       fprintf_unfiltered (fp, "rwatch");
8868       break;
8869     case bp_access_watchpoint:
8870       fprintf_unfiltered (fp, "awatch");
8871       break;
8872     default:
8873       internal_error (__FILE__, __LINE__,
8874                       _("Invalid watchpoint type."));
8875     }
8876
8877   fprintf_unfiltered (fp, " %s", w->exp_string);
8878   print_recreate_thread (b, fp);
8879 }
8880
8881 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
8882
8883 static struct breakpoint_ops watchpoint_breakpoint_ops;
8884
8885 /* Implement the "insert" breakpoint_ops method for
8886    masked hardware watchpoints.  */
8887
8888 static int
8889 insert_masked_watchpoint (struct bp_location *bl)
8890 {
8891   struct watchpoint *w = (struct watchpoint *) bl->owner;
8892
8893   return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
8894                                         bl->watchpoint_type);
8895 }
8896
8897 /* Implement the "remove" breakpoint_ops method for
8898    masked hardware watchpoints.  */
8899
8900 static int
8901 remove_masked_watchpoint (struct bp_location *bl)
8902 {
8903   struct watchpoint *w = (struct watchpoint *) bl->owner;
8904
8905   return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
8906                                         bl->watchpoint_type);
8907 }
8908
8909 /* Implement the "resources_needed" breakpoint_ops method for
8910    masked hardware watchpoints.  */
8911
8912 static int
8913 resources_needed_masked_watchpoint (const struct bp_location *bl)
8914 {
8915   struct watchpoint *w = (struct watchpoint *) bl->owner;
8916
8917   return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
8918 }
8919
8920 /* Implement the "works_in_software_mode" breakpoint_ops method for
8921    masked hardware watchpoints.  */
8922
8923 static int
8924 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8925 {
8926   return 0;
8927 }
8928
8929 /* Implement the "print_it" breakpoint_ops method for
8930    masked hardware watchpoints.  */
8931
8932 static enum print_stop_action
8933 print_it_masked_watchpoint (bpstat bs)
8934 {
8935   struct breakpoint *b = bs->breakpoint_at;
8936   struct ui_out *uiout = current_uiout;
8937
8938   /* Masked watchpoints have only one location.  */
8939   gdb_assert (b->loc && b->loc->next == NULL);
8940
8941   switch (b->type)
8942     {
8943     case bp_hardware_watchpoint:
8944       annotate_watchpoint (b->number);
8945       if (ui_out_is_mi_like_p (uiout))
8946         ui_out_field_string
8947           (uiout, "reason",
8948            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8949       break;
8950
8951     case bp_read_watchpoint:
8952       if (ui_out_is_mi_like_p (uiout))
8953         ui_out_field_string
8954           (uiout, "reason",
8955            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8956       break;
8957
8958     case bp_access_watchpoint:
8959       if (ui_out_is_mi_like_p (uiout))
8960         ui_out_field_string
8961           (uiout, "reason",
8962            async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8963       break;
8964     default:
8965       internal_error (__FILE__, __LINE__,
8966                       _("Invalid hardware watchpoint type."));
8967     }
8968
8969   mention (b);
8970   ui_out_text (uiout, _("\n\
8971 Check the underlying instruction at PC for the memory\n\
8972 address and value which triggered this watchpoint.\n"));
8973   ui_out_text (uiout, "\n");
8974
8975   /* More than one watchpoint may have been triggered.  */
8976   return PRINT_UNKNOWN;
8977 }
8978
8979 /* Implement the "print_one_detail" breakpoint_ops method for
8980    masked hardware watchpoints.  */
8981
8982 static void
8983 print_one_detail_masked_watchpoint (const struct breakpoint *b,
8984                                     struct ui_out *uiout)
8985 {
8986   struct watchpoint *w = (struct watchpoint *) b;
8987
8988   /* Masked watchpoints have only one location.  */
8989   gdb_assert (b->loc && b->loc->next == NULL);
8990
8991   ui_out_text (uiout, "\tmask ");
8992   ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
8993   ui_out_text (uiout, "\n");
8994 }
8995
8996 /* Implement the "print_mention" breakpoint_ops method for
8997    masked hardware watchpoints.  */
8998
8999 static void
9000 print_mention_masked_watchpoint (struct breakpoint *b)
9001 {
9002   struct watchpoint *w = (struct watchpoint *) b;
9003   struct ui_out *uiout = current_uiout;
9004   struct cleanup *ui_out_chain;
9005
9006   switch (b->type)
9007     {
9008     case bp_hardware_watchpoint:
9009       ui_out_text (uiout, "Masked hardware watchpoint ");
9010       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9011       break;
9012     case bp_read_watchpoint:
9013       ui_out_text (uiout, "Masked hardware read watchpoint ");
9014       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9015       break;
9016     case bp_access_watchpoint:
9017       ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
9018       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9019       break;
9020     default:
9021       internal_error (__FILE__, __LINE__,
9022                       _("Invalid hardware watchpoint type."));
9023     }
9024
9025   ui_out_field_int (uiout, "number", b->number);
9026   ui_out_text (uiout, ": ");
9027   ui_out_field_string (uiout, "exp", w->exp_string);
9028   do_cleanups (ui_out_chain);
9029 }
9030
9031 /* Implement the "print_recreate" breakpoint_ops method for
9032    masked hardware watchpoints.  */
9033
9034 static void
9035 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
9036 {
9037   struct watchpoint *w = (struct watchpoint *) b;
9038   char tmp[40];
9039
9040   switch (b->type)
9041     {
9042     case bp_hardware_watchpoint:
9043       fprintf_unfiltered (fp, "watch");
9044       break;
9045     case bp_read_watchpoint:
9046       fprintf_unfiltered (fp, "rwatch");
9047       break;
9048     case bp_access_watchpoint:
9049       fprintf_unfiltered (fp, "awatch");
9050       break;
9051     default:
9052       internal_error (__FILE__, __LINE__,
9053                       _("Invalid hardware watchpoint type."));
9054     }
9055
9056   sprintf_vma (tmp, w->hw_wp_mask);
9057   fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
9058   print_recreate_thread (b, fp);
9059 }
9060
9061 /* The breakpoint_ops structure to be used in masked hardware watchpoints.  */
9062
9063 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9064
9065 /* Tell whether the given watchpoint is a masked hardware watchpoint.  */
9066
9067 static int
9068 is_masked_watchpoint (const struct breakpoint *b)
9069 {
9070   return b->ops == &masked_watchpoint_breakpoint_ops;
9071 }
9072
9073 /* accessflag:  hw_write:  watch write, 
9074                 hw_read:   watch read, 
9075                 hw_access: watch access (read or write) */
9076 static void
9077 watch_command_1 (char *arg, int accessflag, int from_tty,
9078                  int just_location, int internal)
9079 {
9080   volatile struct gdb_exception e;
9081   struct breakpoint *b, *scope_breakpoint = NULL;
9082   struct expression *exp;
9083   struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9084   struct value *val, *mark, *result;
9085   struct frame_info *frame;
9086   char *exp_start = NULL;
9087   char *exp_end = NULL;
9088   char *tok, *end_tok;
9089   int toklen = -1;
9090   char *cond_start = NULL;
9091   char *cond_end = NULL;
9092   enum bptype bp_type;
9093   int thread = -1;
9094   int pc = 0;
9095   /* Flag to indicate whether we are going to use masks for
9096      the hardware watchpoint.  */
9097   int use_mask = 0;
9098   CORE_ADDR mask = 0;
9099   struct watchpoint *w;
9100
9101   /* Make sure that we actually have parameters to parse.  */
9102   if (arg != NULL && arg[0] != '\0')
9103     {
9104       char *value_start;
9105
9106       /* Look for "parameter value" pairs at the end
9107          of the arguments string.  */
9108       for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9109         {
9110           /* Skip whitespace at the end of the argument list.  */
9111           while (tok > arg && (*tok == ' ' || *tok == '\t'))
9112             tok--;
9113
9114           /* Find the beginning of the last token.
9115              This is the value of the parameter.  */
9116           while (tok > arg && (*tok != ' ' && *tok != '\t'))
9117             tok--;
9118           value_start = tok + 1;
9119
9120           /* Skip whitespace.  */
9121           while (tok > arg && (*tok == ' ' || *tok == '\t'))
9122             tok--;
9123
9124           end_tok = tok;
9125
9126           /* Find the beginning of the second to last token.
9127              This is the parameter itself.  */
9128           while (tok > arg && (*tok != ' ' && *tok != '\t'))
9129             tok--;
9130           tok++;
9131           toklen = end_tok - tok + 1;
9132
9133           if (toklen == 6 && !strncmp (tok, "thread", 6))
9134             {
9135               /* At this point we've found a "thread" token, which means
9136                  the user is trying to set a watchpoint that triggers
9137                  only in a specific thread.  */
9138               char *endp;
9139
9140               if (thread != -1)
9141                 error(_("You can specify only one thread."));
9142
9143               /* Extract the thread ID from the next token.  */
9144               thread = strtol (value_start, &endp, 0);
9145
9146               /* Check if the user provided a valid numeric value for the
9147                  thread ID.  */
9148               if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9149                 error (_("Invalid thread ID specification %s."), value_start);
9150
9151               /* Check if the thread actually exists.  */
9152               if (!valid_thread_id (thread))
9153                 error (_("Unknown thread %d."), thread);
9154             }
9155           else if (toklen == 4 && !strncmp (tok, "mask", 4))
9156             {
9157               /* We've found a "mask" token, which means the user wants to
9158                  create a hardware watchpoint that is going to have the mask
9159                  facility.  */
9160               struct value *mask_value, *mark;
9161
9162               if (use_mask)
9163                 error(_("You can specify only one mask."));
9164
9165               use_mask = just_location = 1;
9166
9167               mark = value_mark ();
9168               mask_value = parse_to_comma_and_eval (&value_start);
9169               mask = value_as_address (mask_value);
9170               value_free_to_mark (mark);
9171             }
9172           else
9173             /* We didn't recognize what we found.  We should stop here.  */
9174             break;
9175
9176           /* Truncate the string and get rid of the "parameter value" pair before
9177              the arguments string is parsed by the parse_exp_1 function.  */
9178           *tok = '\0';
9179         }
9180     }
9181
9182   /* Parse the rest of the arguments.  */
9183   innermost_block = NULL;
9184   exp_start = arg;
9185   exp = parse_exp_1 (&arg, 0, 0);
9186   exp_end = arg;
9187   /* Remove trailing whitespace from the expression before saving it.
9188      This makes the eventual display of the expression string a bit
9189      prettier.  */
9190   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9191     --exp_end;
9192
9193   /* Checking if the expression is not constant.  */
9194   if (watchpoint_exp_is_const (exp))
9195     {
9196       int len;
9197
9198       len = exp_end - exp_start;
9199       while (len > 0 && isspace (exp_start[len - 1]))
9200         len--;
9201       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9202     }
9203
9204   exp_valid_block = innermost_block;
9205   mark = value_mark ();
9206   fetch_subexp_value (exp, &pc, &val, &result, NULL);
9207
9208   if (just_location)
9209     {
9210       int ret;
9211
9212       exp_valid_block = NULL;
9213       val = value_addr (result);
9214       release_value (val);
9215       value_free_to_mark (mark);
9216
9217       if (use_mask)
9218         {
9219           ret = target_masked_watch_num_registers (value_as_address (val),
9220                                                    mask);
9221           if (ret == -1)
9222             error (_("This target does not support masked watchpoints."));
9223           else if (ret == -2)
9224             error (_("Invalid mask or memory region."));
9225         }
9226     }
9227   else if (val != NULL)
9228     release_value (val);
9229
9230   tok = skip_spaces (arg);
9231   end_tok = skip_to_space (tok);
9232
9233   toklen = end_tok - tok;
9234   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9235     {
9236       struct expression *cond;
9237
9238       innermost_block = NULL;
9239       tok = cond_start = end_tok + 1;
9240       cond = parse_exp_1 (&tok, 0, 0);
9241
9242       /* The watchpoint expression may not be local, but the condition
9243          may still be.  E.g.: `watch global if local > 0'.  */
9244       cond_exp_valid_block = innermost_block;
9245
9246       xfree (cond);
9247       cond_end = tok;
9248     }
9249   if (*tok)
9250     error (_("Junk at end of command."));
9251
9252   if (accessflag == hw_read)
9253     bp_type = bp_read_watchpoint;
9254   else if (accessflag == hw_access)
9255     bp_type = bp_access_watchpoint;
9256   else
9257     bp_type = bp_hardware_watchpoint;
9258
9259   frame = block_innermost_frame (exp_valid_block);
9260
9261   /* If the expression is "local", then set up a "watchpoint scope"
9262      breakpoint at the point where we've left the scope of the watchpoint
9263      expression.  Create the scope breakpoint before the watchpoint, so
9264      that we will encounter it first in bpstat_stop_status.  */
9265   if (exp_valid_block && frame)
9266     {
9267       if (frame_id_p (frame_unwind_caller_id (frame)))
9268         {
9269           scope_breakpoint
9270             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9271                                           frame_unwind_caller_pc (frame),
9272                                           bp_watchpoint_scope,
9273                                           &momentary_breakpoint_ops);
9274
9275           scope_breakpoint->enable_state = bp_enabled;
9276
9277           /* Automatically delete the breakpoint when it hits.  */
9278           scope_breakpoint->disposition = disp_del;
9279
9280           /* Only break in the proper frame (help with recursion).  */
9281           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
9282
9283           /* Set the address at which we will stop.  */
9284           scope_breakpoint->loc->gdbarch
9285             = frame_unwind_caller_arch (frame);
9286           scope_breakpoint->loc->requested_address
9287             = frame_unwind_caller_pc (frame);
9288           scope_breakpoint->loc->address
9289             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9290                                          scope_breakpoint->loc->requested_address,
9291                                          scope_breakpoint->type);
9292         }
9293     }
9294
9295   /* Now set up the breakpoint.  */
9296
9297   w = XCNEW (struct watchpoint);
9298   b = &w->base;
9299   if (use_mask)
9300     init_raw_breakpoint_without_location (b, NULL, bp_type,
9301                                           &masked_watchpoint_breakpoint_ops);
9302   else
9303     init_raw_breakpoint_without_location (b, NULL, bp_type,
9304                                           &watchpoint_breakpoint_ops);
9305   b->thread = thread;
9306   b->disposition = disp_donttouch;
9307   b->pspace = current_program_space;
9308   w->exp = exp;
9309   w->exp_valid_block = exp_valid_block;
9310   w->cond_exp_valid_block = cond_exp_valid_block;
9311   if (just_location)
9312     {
9313       struct type *t = value_type (val);
9314       CORE_ADDR addr = value_as_address (val);
9315       char *name;
9316
9317       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9318       name = type_to_string (t);
9319
9320       w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
9321                                           core_addr_to_string (addr));
9322       xfree (name);
9323
9324       w->exp_string = xstrprintf ("-location %.*s",
9325                                   (int) (exp_end - exp_start), exp_start);
9326
9327       /* The above expression is in C.  */
9328       b->language = language_c;
9329     }
9330   else
9331     w->exp_string = savestring (exp_start, exp_end - exp_start);
9332
9333   if (use_mask)
9334     {
9335       w->hw_wp_mask = mask;
9336     }
9337   else
9338     {
9339       w->val = val;
9340       w->val_valid = 1;
9341     }
9342
9343   if (cond_start)
9344     b->cond_string = savestring (cond_start, cond_end - cond_start);
9345   else
9346     b->cond_string = 0;
9347
9348   if (frame)
9349     {
9350       w->watchpoint_frame = get_frame_id (frame);
9351       w->watchpoint_thread = inferior_ptid;
9352     }
9353   else
9354     {
9355       w->watchpoint_frame = null_frame_id;
9356       w->watchpoint_thread = null_ptid;
9357     }
9358
9359   if (scope_breakpoint != NULL)
9360     {
9361       /* The scope breakpoint is related to the watchpoint.  We will
9362          need to act on them together.  */
9363       b->related_breakpoint = scope_breakpoint;
9364       scope_breakpoint->related_breakpoint = b;
9365     }
9366
9367   if (!just_location)
9368     value_free_to_mark (mark);
9369
9370   TRY_CATCH (e, RETURN_MASK_ALL)
9371     {
9372       /* Finally update the new watchpoint.  This creates the locations
9373          that should be inserted.  */
9374       update_watchpoint (w, 1);
9375     }
9376   if (e.reason < 0)
9377     {
9378       delete_breakpoint (b);
9379       throw_exception (e);
9380     }
9381
9382   install_breakpoint (internal, b);
9383 }
9384
9385 /* Return count of debug registers needed to watch the given expression.
9386    If the watchpoint cannot be handled in hardware return zero.  */
9387
9388 static int
9389 can_use_hardware_watchpoint (struct value *v)
9390 {
9391   int found_memory_cnt = 0;
9392   struct value *head = v;
9393
9394   /* Did the user specifically forbid us to use hardware watchpoints? */
9395   if (!can_use_hw_watchpoints)
9396     return 0;
9397
9398   /* Make sure that the value of the expression depends only upon
9399      memory contents, and values computed from them within GDB.  If we
9400      find any register references or function calls, we can't use a
9401      hardware watchpoint.
9402
9403      The idea here is that evaluating an expression generates a series
9404      of values, one holding the value of every subexpression.  (The
9405      expression a*b+c has five subexpressions: a, b, a*b, c, and
9406      a*b+c.)  GDB's values hold almost enough information to establish
9407      the criteria given above --- they identify memory lvalues,
9408      register lvalues, computed values, etcetera.  So we can evaluate
9409      the expression, and then scan the chain of values that leaves
9410      behind to decide whether we can detect any possible change to the
9411      expression's final value using only hardware watchpoints.
9412
9413      However, I don't think that the values returned by inferior
9414      function calls are special in any way.  So this function may not
9415      notice that an expression involving an inferior function call
9416      can't be watched with hardware watchpoints.  FIXME.  */
9417   for (; v; v = value_next (v))
9418     {
9419       if (VALUE_LVAL (v) == lval_memory)
9420         {
9421           if (v != head && value_lazy (v))
9422             /* A lazy memory lvalue in the chain is one that GDB never
9423                needed to fetch; we either just used its address (e.g.,
9424                `a' in `a.b') or we never needed it at all (e.g., `a'
9425                in `a,b').  This doesn't apply to HEAD; if that is
9426                lazy then it was not readable, but watch it anyway.  */
9427             ;
9428           else
9429             {
9430               /* Ahh, memory we actually used!  Check if we can cover
9431                  it with hardware watchpoints.  */
9432               struct type *vtype = check_typedef (value_type (v));
9433
9434               /* We only watch structs and arrays if user asked for it
9435                  explicitly, never if they just happen to appear in a
9436                  middle of some value chain.  */
9437               if (v == head
9438                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9439                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9440                 {
9441                   CORE_ADDR vaddr = value_address (v);
9442                   int len;
9443                   int num_regs;
9444
9445                   len = (target_exact_watchpoints
9446                          && is_scalar_type_recursive (vtype))?
9447                     1 : TYPE_LENGTH (value_type (v));
9448
9449                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9450                   if (!num_regs)
9451                     return 0;
9452                   else
9453                     found_memory_cnt += num_regs;
9454                 }
9455             }
9456         }
9457       else if (VALUE_LVAL (v) != not_lval
9458                && deprecated_value_modifiable (v) == 0)
9459         return 0;       /* These are values from the history (e.g., $1).  */
9460       else if (VALUE_LVAL (v) == lval_register)
9461         return 0;       /* Cannot watch a register with a HW watchpoint.  */
9462     }
9463
9464   /* The expression itself looks suitable for using a hardware
9465      watchpoint, but give the target machine a chance to reject it.  */
9466   return found_memory_cnt;
9467 }
9468
9469 void
9470 watch_command_wrapper (char *arg, int from_tty, int internal)
9471 {
9472   watch_command_1 (arg, hw_write, from_tty, 0, internal);
9473 }
9474
9475 /* A helper function that looks for an argument at the start of a
9476    string.  The argument must also either be at the end of the string,
9477    or be followed by whitespace.  Returns 1 if it finds the argument,
9478    0 otherwise.  If the argument is found, it updates *STR.  */
9479
9480 static int
9481 check_for_argument (char **str, char *arg, int arg_len)
9482 {
9483   if (strncmp (*str, arg, arg_len) == 0
9484       && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9485     {
9486       *str += arg_len;
9487       return 1;
9488     }
9489   return 0;
9490 }
9491
9492 /* A helper function that looks for the "-location" argument and then
9493    calls watch_command_1.  */
9494
9495 static void
9496 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9497 {
9498   int just_location = 0;
9499
9500   if (arg
9501       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9502           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9503     {
9504       arg = skip_spaces (arg);
9505       just_location = 1;
9506     }
9507
9508   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
9509 }
9510
9511 static void
9512 watch_command (char *arg, int from_tty)
9513 {
9514   watch_maybe_just_location (arg, hw_write, from_tty);
9515 }
9516
9517 void
9518 rwatch_command_wrapper (char *arg, int from_tty, int internal)
9519 {
9520   watch_command_1 (arg, hw_read, from_tty, 0, internal);
9521 }
9522
9523 static void
9524 rwatch_command (char *arg, int from_tty)
9525 {
9526   watch_maybe_just_location (arg, hw_read, from_tty);
9527 }
9528
9529 void
9530 awatch_command_wrapper (char *arg, int from_tty, int internal)
9531 {
9532   watch_command_1 (arg, hw_access, from_tty, 0, internal);
9533 }
9534
9535 static void
9536 awatch_command (char *arg, int from_tty)
9537 {
9538   watch_maybe_just_location (arg, hw_access, from_tty);
9539 }
9540 \f
9541
9542 /* Helper routines for the until_command routine in infcmd.c.  Here
9543    because it uses the mechanisms of breakpoints.  */
9544
9545 struct until_break_command_continuation_args
9546 {
9547   struct breakpoint *breakpoint;
9548   struct breakpoint *breakpoint2;
9549   int thread_num;
9550 };
9551
9552 /* This function is called by fetch_inferior_event via the
9553    cmd_continuation pointer, to complete the until command.  It takes
9554    care of cleaning up the temporary breakpoints set up by the until
9555    command.  */
9556 static void
9557 until_break_command_continuation (void *arg, int err)
9558 {
9559   struct until_break_command_continuation_args *a = arg;
9560
9561   delete_breakpoint (a->breakpoint);
9562   if (a->breakpoint2)
9563     delete_breakpoint (a->breakpoint2);
9564   delete_longjmp_breakpoint (a->thread_num);
9565 }
9566
9567 void
9568 until_break_command (char *arg, int from_tty, int anywhere)
9569 {
9570   struct symtabs_and_lines sals;
9571   struct symtab_and_line sal;
9572   struct frame_info *frame = get_selected_frame (NULL);
9573   struct breakpoint *breakpoint;
9574   struct breakpoint *breakpoint2 = NULL;
9575   struct cleanup *old_chain;
9576   int thread;
9577   struct thread_info *tp;
9578
9579   clear_proceed_status ();
9580
9581   /* Set a breakpoint where the user wants it and at return from
9582      this function.  */
9583
9584   if (last_displayed_sal_is_valid ())
9585     sals = decode_line_1 (&arg, 1,
9586                           get_last_displayed_symtab (),
9587                           get_last_displayed_line (),
9588                           NULL);
9589   else
9590     sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
9591
9592   if (sals.nelts != 1)
9593     error (_("Couldn't get information on specified line."));
9594
9595   sal = sals.sals[0];
9596   xfree (sals.sals);    /* malloc'd, so freed.  */
9597
9598   if (*arg)
9599     error (_("Junk at end of arguments."));
9600
9601   resolve_sal_pc (&sal);
9602
9603   if (anywhere)
9604     /* If the user told us to continue until a specified location,
9605        we don't specify a frame at which we need to stop.  */
9606     breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9607                                            null_frame_id, bp_until);
9608   else
9609     /* Otherwise, specify the selected frame, because we want to stop
9610        only at the very same frame.  */
9611     breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9612                                            get_stack_frame_id (frame),
9613                                            bp_until);
9614
9615   old_chain = make_cleanup_delete_breakpoint (breakpoint);
9616
9617   tp = inferior_thread ();
9618   thread = tp->num;
9619
9620   /* Keep within the current frame, or in frames called by the current
9621      one.  */
9622
9623   if (frame_id_p (frame_unwind_caller_id (frame)))
9624     {
9625       sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9626       sal.pc = frame_unwind_caller_pc (frame);
9627       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9628                                               sal,
9629                                               frame_unwind_caller_id (frame),
9630                                               bp_until);
9631       make_cleanup_delete_breakpoint (breakpoint2);
9632
9633       set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9634       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
9635     }
9636
9637   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
9638
9639   /* If we are running asynchronously, and proceed call above has
9640      actually managed to start the target, arrange for breakpoints to
9641      be deleted when the target stops.  Otherwise, we're already
9642      stopped and delete breakpoints via cleanup chain.  */
9643
9644   if (target_can_async_p () && is_running (inferior_ptid))
9645     {
9646       struct until_break_command_continuation_args *args;
9647       args = xmalloc (sizeof (*args));
9648
9649       args->breakpoint = breakpoint;
9650       args->breakpoint2 = breakpoint2;
9651       args->thread_num = thread;
9652
9653       discard_cleanups (old_chain);
9654       add_continuation (inferior_thread (),
9655                         until_break_command_continuation, args,
9656                         xfree);
9657     }
9658   else
9659     do_cleanups (old_chain);
9660 }
9661
9662 /* This function attempts to parse an optional "if <cond>" clause
9663    from the arg string.  If one is not found, it returns NULL.
9664
9665    Else, it returns a pointer to the condition string.  (It does not
9666    attempt to evaluate the string against a particular block.)  And,
9667    it updates arg to point to the first character following the parsed
9668    if clause in the arg string.  */
9669
9670 static char *
9671 ep_parse_optional_if_clause (char **arg)
9672 {
9673   char *cond_string;
9674
9675   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
9676     return NULL;
9677
9678   /* Skip the "if" keyword.  */
9679   (*arg) += 2;
9680
9681   /* Skip any extra leading whitespace, and record the start of the
9682      condition string.  */
9683   *arg = skip_spaces (*arg);
9684   cond_string = *arg;
9685
9686   /* Assume that the condition occupies the remainder of the arg
9687      string.  */
9688   (*arg) += strlen (cond_string);
9689
9690   return cond_string;
9691 }
9692
9693 /* Commands to deal with catching events, such as signals, exceptions,
9694    process start/exit, etc.  */
9695
9696 typedef enum
9697 {
9698   catch_fork_temporary, catch_vfork_temporary,
9699   catch_fork_permanent, catch_vfork_permanent
9700 }
9701 catch_fork_kind;
9702
9703 static void
9704 catch_fork_command_1 (char *arg, int from_tty, 
9705                       struct cmd_list_element *command)
9706 {
9707   struct gdbarch *gdbarch = get_current_arch ();
9708   char *cond_string = NULL;
9709   catch_fork_kind fork_kind;
9710   int tempflag;
9711
9712   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9713   tempflag = (fork_kind == catch_fork_temporary
9714               || fork_kind == catch_vfork_temporary);
9715
9716   if (!arg)
9717     arg = "";
9718   arg = skip_spaces (arg);
9719
9720   /* The allowed syntax is:
9721      catch [v]fork
9722      catch [v]fork if <cond>
9723
9724      First, check if there's an if clause.  */
9725   cond_string = ep_parse_optional_if_clause (&arg);
9726
9727   if ((*arg != '\0') && !isspace (*arg))
9728     error (_("Junk at end of arguments."));
9729
9730   /* If this target supports it, create a fork or vfork catchpoint
9731      and enable reporting of such events.  */
9732   switch (fork_kind)
9733     {
9734     case catch_fork_temporary:
9735     case catch_fork_permanent:
9736       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
9737                                           &catch_fork_breakpoint_ops);
9738       break;
9739     case catch_vfork_temporary:
9740     case catch_vfork_permanent:
9741       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
9742                                           &catch_vfork_breakpoint_ops);
9743       break;
9744     default:
9745       error (_("unsupported or unknown fork kind; cannot catch it"));
9746       break;
9747     }
9748 }
9749
9750 static void
9751 catch_exec_command_1 (char *arg, int from_tty, 
9752                       struct cmd_list_element *command)
9753 {
9754   struct exec_catchpoint *c;
9755   struct gdbarch *gdbarch = get_current_arch ();
9756   int tempflag;
9757   char *cond_string = NULL;
9758
9759   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9760
9761   if (!arg)
9762     arg = "";
9763   arg = skip_spaces (arg);
9764
9765   /* The allowed syntax is:
9766      catch exec
9767      catch exec if <cond>
9768
9769      First, check if there's an if clause.  */
9770   cond_string = ep_parse_optional_if_clause (&arg);
9771
9772   if ((*arg != '\0') && !isspace (*arg))
9773     error (_("Junk at end of arguments."));
9774
9775   c = XNEW (struct exec_catchpoint);
9776   init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9777                    &catch_exec_breakpoint_ops);
9778   c->exec_pathname = NULL;
9779
9780   install_breakpoint (0, &c->base);
9781 }
9782
9783 static enum print_stop_action
9784 print_it_exception_catchpoint (bpstat bs)
9785 {
9786   struct ui_out *uiout = current_uiout;
9787   struct breakpoint *b = bs->breakpoint_at;
9788   int bp_temp, bp_throw;
9789
9790   annotate_catchpoint (b->number);
9791
9792   bp_throw = strstr (b->addr_string, "throw") != NULL;
9793   if (b->loc->address != b->loc->requested_address)
9794     breakpoint_adjustment_warning (b->loc->requested_address,
9795                                    b->loc->address,
9796                                    b->number, 1);
9797   bp_temp = b->disposition == disp_del;
9798   ui_out_text (uiout, 
9799                bp_temp ? "Temporary catchpoint "
9800                        : "Catchpoint ");
9801   if (!ui_out_is_mi_like_p (uiout))
9802     ui_out_field_int (uiout, "bkptno", b->number);
9803   ui_out_text (uiout,
9804                bp_throw ? " (exception thrown), "
9805                         : " (exception caught), ");
9806   if (ui_out_is_mi_like_p (uiout))
9807     {
9808       ui_out_field_string (uiout, "reason", 
9809                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9810       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9811       ui_out_field_int (uiout, "bkptno", b->number);
9812     }
9813   return PRINT_SRC_AND_LOC;
9814 }
9815
9816 static void
9817 print_one_exception_catchpoint (struct breakpoint *b, 
9818                                 struct bp_location **last_loc)
9819 {
9820   struct value_print_options opts;
9821   struct ui_out *uiout = current_uiout;
9822
9823   get_user_print_options (&opts);
9824   if (opts.addressprint)
9825     {
9826       annotate_field (4);
9827       if (b->loc == NULL || b->loc->shlib_disabled)
9828         ui_out_field_string (uiout, "addr", "<PENDING>");
9829       else
9830         ui_out_field_core_addr (uiout, "addr",
9831                                 b->loc->gdbarch, b->loc->address);
9832     }
9833   annotate_field (5);
9834   if (b->loc)
9835     *last_loc = b->loc;
9836   if (strstr (b->addr_string, "throw") != NULL)
9837     ui_out_field_string (uiout, "what", "exception throw");
9838   else
9839     ui_out_field_string (uiout, "what", "exception catch");
9840 }
9841
9842 static void
9843 print_mention_exception_catchpoint (struct breakpoint *b)
9844 {
9845   struct ui_out *uiout = current_uiout;
9846   int bp_temp;
9847   int bp_throw;
9848
9849   bp_temp = b->disposition == disp_del;
9850   bp_throw = strstr (b->addr_string, "throw") != NULL;
9851   ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9852                               : _("Catchpoint "));
9853   ui_out_field_int (uiout, "bkptno", b->number);
9854   ui_out_text (uiout, bp_throw ? _(" (throw)")
9855                                : _(" (catch)"));
9856 }
9857
9858 /* Implement the "print_recreate" breakpoint_ops method for throw and
9859    catch catchpoints.  */
9860
9861 static void
9862 print_recreate_exception_catchpoint (struct breakpoint *b, 
9863                                      struct ui_file *fp)
9864 {
9865   int bp_temp;
9866   int bp_throw;
9867
9868   bp_temp = b->disposition == disp_del;
9869   bp_throw = strstr (b->addr_string, "throw") != NULL;
9870   fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9871   fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9872   print_recreate_thread (b, fp);
9873 }
9874
9875 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
9876
9877 static int
9878 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9879                           enum exception_event_kind ex_event, int from_tty)
9880 {
9881   char *trigger_func_name;
9882  
9883   if (ex_event == EX_EVENT_CATCH)
9884     trigger_func_name = "__cxa_begin_catch";
9885   else
9886     trigger_func_name = "__cxa_throw";
9887
9888   create_breakpoint (get_current_arch (),
9889                      trigger_func_name, cond_string, -1,
9890                      0 /* condition and thread are valid.  */,
9891                      tempflag, bp_breakpoint,
9892                      0,
9893                      AUTO_BOOLEAN_TRUE /* pending */,
9894                      &gnu_v3_exception_catchpoint_ops, from_tty,
9895                      1 /* enabled */,
9896                      0 /* internal */);
9897
9898   return 1;
9899 }
9900
9901 /* Deal with "catch catch" and "catch throw" commands.  */
9902
9903 static void
9904 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9905                            int tempflag, int from_tty)
9906 {
9907   char *cond_string = NULL;
9908
9909   if (!arg)
9910     arg = "";
9911   arg = skip_spaces (arg);
9912
9913   cond_string = ep_parse_optional_if_clause (&arg);
9914
9915   if ((*arg != '\0') && !isspace (*arg))
9916     error (_("Junk at end of arguments."));
9917
9918   if (ex_event != EX_EVENT_THROW
9919       && ex_event != EX_EVENT_CATCH)
9920     error (_("Unsupported or unknown exception event; cannot catch it"));
9921
9922   if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9923     return;
9924
9925   warning (_("Unsupported with this platform/compiler combination."));
9926 }
9927
9928 /* Implementation of "catch catch" command.  */
9929
9930 static void
9931 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9932 {
9933   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9934
9935   catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9936 }
9937
9938 /* Implementation of "catch throw" command.  */
9939
9940 static void
9941 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9942 {
9943   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9944
9945   catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9946 }
9947
9948 void
9949 init_ada_exception_breakpoint (struct breakpoint *b,
9950                                struct gdbarch *gdbarch,
9951                                struct symtab_and_line sal,
9952                                char *addr_string,
9953                                const struct breakpoint_ops *ops,
9954                                int tempflag,
9955                                int from_tty)
9956 {
9957   if (from_tty)
9958     {
9959       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9960       if (!loc_gdbarch)
9961         loc_gdbarch = gdbarch;
9962
9963       describe_other_breakpoints (loc_gdbarch,
9964                                   sal.pspace, sal.pc, sal.section, -1);
9965       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9966          version for exception catchpoints, because two catchpoints
9967          used for different exception names will use the same address.
9968          In this case, a "breakpoint ... also set at..." warning is
9969          unproductive.  Besides, the warning phrasing is also a bit
9970          inappropriate, we should use the word catchpoint, and tell
9971          the user what type of catchpoint it is.  The above is good
9972          enough for now, though.  */
9973     }
9974
9975   init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
9976
9977   b->enable_state = bp_enabled;
9978   b->disposition = tempflag ? disp_del : disp_donttouch;
9979   b->addr_string = addr_string;
9980   b->language = language_ada;
9981 }
9982
9983 /* Splits the argument using space as delimiter.  Returns an xmalloc'd
9984    filter list, or NULL if no filtering is required.  */
9985 static VEC(int) *
9986 catch_syscall_split_args (char *arg)
9987 {
9988   VEC(int) *result = NULL;
9989   struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
9990
9991   while (*arg != '\0')
9992     {
9993       int i, syscall_number;
9994       char *endptr;
9995       char cur_name[128];
9996       struct syscall s;
9997
9998       /* Skip whitespace.  */
9999       while (isspace (*arg))
10000         arg++;
10001
10002       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
10003         cur_name[i] = arg[i];
10004       cur_name[i] = '\0';
10005       arg += i;
10006
10007       /* Check if the user provided a syscall name or a number.  */
10008       syscall_number = (int) strtol (cur_name, &endptr, 0);
10009       if (*endptr == '\0')
10010         get_syscall_by_number (syscall_number, &s);
10011       else
10012         {
10013           /* We have a name.  Let's check if it's valid and convert it
10014              to a number.  */
10015           get_syscall_by_name (cur_name, &s);
10016
10017           if (s.number == UNKNOWN_SYSCALL)
10018             /* Here we have to issue an error instead of a warning,
10019                because GDB cannot do anything useful if there's no
10020                syscall number to be caught.  */
10021             error (_("Unknown syscall name '%s'."), cur_name);
10022         }
10023
10024       /* Ok, it's valid.  */
10025       VEC_safe_push (int, result, s.number);
10026     }
10027
10028   discard_cleanups (cleanup);
10029   return result;
10030 }
10031
10032 /* Implement the "catch syscall" command.  */
10033
10034 static void
10035 catch_syscall_command_1 (char *arg, int from_tty, 
10036                          struct cmd_list_element *command)
10037 {
10038   int tempflag;
10039   VEC(int) *filter;
10040   struct syscall s;
10041   struct gdbarch *gdbarch = get_current_arch ();
10042
10043   /* Checking if the feature if supported.  */
10044   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
10045     error (_("The feature 'catch syscall' is not supported on \
10046 this architecture yet."));
10047
10048   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10049
10050   arg = skip_spaces (arg);
10051
10052   /* We need to do this first "dummy" translation in order
10053      to get the syscall XML file loaded or, most important,
10054      to display a warning to the user if there's no XML file
10055      for his/her architecture.  */
10056   get_syscall_by_number (0, &s);
10057
10058   /* The allowed syntax is:
10059      catch syscall
10060      catch syscall <name | number> [<name | number> ... <name | number>]
10061
10062      Let's check if there's a syscall name.  */
10063
10064   if (arg != NULL)
10065     filter = catch_syscall_split_args (arg);
10066   else
10067     filter = NULL;
10068
10069   create_syscall_event_catchpoint (tempflag, filter,
10070                                    &catch_syscall_breakpoint_ops);
10071 }
10072
10073 static void
10074 catch_command (char *arg, int from_tty)
10075 {
10076   error (_("Catch requires an event name."));
10077 }
10078 \f
10079
10080 static void
10081 tcatch_command (char *arg, int from_tty)
10082 {
10083   error (_("Catch requires an event name."));
10084 }
10085
10086 /* Delete breakpoints by address or line.  */
10087
10088 static void
10089 clear_command (char *arg, int from_tty)
10090 {
10091   struct breakpoint *b;
10092   VEC(breakpoint_p) *found = 0;
10093   int ix;
10094   int default_match;
10095   struct symtabs_and_lines sals;
10096   struct symtab_and_line sal;
10097   int i;
10098
10099   if (arg)
10100     {
10101       sals = decode_line_spec (arg, 1);
10102       default_match = 0;
10103     }
10104   else
10105     {
10106       sals.sals = (struct symtab_and_line *)
10107         xmalloc (sizeof (struct symtab_and_line));
10108       make_cleanup (xfree, sals.sals);
10109       init_sal (&sal);          /* Initialize to zeroes.  */
10110
10111       /* Set sal's line, symtab, pc, and pspace to the values
10112          corresponding to the last call to print_frame_info.  If the
10113          codepoint is not valid, this will set all the fields to 0.  */
10114       get_last_displayed_sal (&sal);
10115       if (sal.symtab == 0)
10116         error (_("No source file specified."));
10117
10118       sals.sals[0] = sal;
10119       sals.nelts = 1;
10120
10121       default_match = 1;
10122     }
10123
10124   /* We don't call resolve_sal_pc here.  That's not as bad as it
10125      seems, because all existing breakpoints typically have both
10126      file/line and pc set.  So, if clear is given file/line, we can
10127      match this to existing breakpoint without obtaining pc at all.
10128
10129      We only support clearing given the address explicitly 
10130      present in breakpoint table.  Say, we've set breakpoint 
10131      at file:line.  There were several PC values for that file:line,
10132      due to optimization, all in one block.
10133
10134      We've picked one PC value.  If "clear" is issued with another
10135      PC corresponding to the same file:line, the breakpoint won't
10136      be cleared.  We probably can still clear the breakpoint, but 
10137      since the other PC value is never presented to user, user
10138      can only find it by guessing, and it does not seem important
10139      to support that.  */
10140
10141   /* For each line spec given, delete bps which correspond to it.  Do
10142      it in two passes, solely to preserve the current behavior that
10143      from_tty is forced true if we delete more than one
10144      breakpoint.  */
10145
10146   found = NULL;
10147   for (i = 0; i < sals.nelts; i++)
10148     {
10149       /* If exact pc given, clear bpts at that pc.
10150          If line given (pc == 0), clear all bpts on specified line.
10151          If defaulting, clear all bpts on default line
10152          or at default pc.
10153
10154          defaulting    sal.pc != 0    tests to do
10155
10156          0              1             pc
10157          1              1             pc _and_ line
10158          0              0             line
10159          1              0             <can't happen> */
10160
10161       sal = sals.sals[i];
10162
10163       /* Find all matching breakpoints and add them to 'found'.  */
10164       ALL_BREAKPOINTS (b)
10165         {
10166           int match = 0;
10167           /* Are we going to delete b?  */
10168           if (b->type != bp_none && !is_watchpoint (b))
10169             {
10170               struct bp_location *loc = b->loc;
10171               for (; loc; loc = loc->next)
10172                 {
10173                   int pc_match = sal.pc
10174                     && (loc->pspace == sal.pspace)
10175                     && (loc->address == sal.pc)
10176                     && (!section_is_overlay (loc->section)
10177                         || loc->section == sal.section);
10178                   int line_match = ((default_match || (0 == sal.pc))
10179                                     && b->source_file != NULL
10180                                     && sal.symtab != NULL
10181                                     && sal.pspace == loc->pspace
10182                                     && filename_cmp (b->source_file,
10183                                                      sal.symtab->filename) == 0
10184                                     && b->line_number == sal.line);
10185                   if (pc_match || line_match)
10186                     {
10187                       match = 1;
10188                       break;
10189                     }
10190                 }
10191             }
10192
10193           if (match)
10194             VEC_safe_push(breakpoint_p, found, b);
10195         }
10196     }
10197   /* Now go thru the 'found' chain and delete them.  */
10198   if (VEC_empty(breakpoint_p, found))
10199     {
10200       if (arg)
10201         error (_("No breakpoint at %s."), arg);
10202       else
10203         error (_("No breakpoint at this line."));
10204     }
10205
10206   if (VEC_length(breakpoint_p, found) > 1)
10207     from_tty = 1;       /* Always report if deleted more than one.  */
10208   if (from_tty)
10209     {
10210       if (VEC_length(breakpoint_p, found) == 1)
10211         printf_unfiltered (_("Deleted breakpoint "));
10212       else
10213         printf_unfiltered (_("Deleted breakpoints "));
10214     }
10215   breakpoints_changed ();
10216
10217   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
10218     {
10219       if (from_tty)
10220         printf_unfiltered ("%d ", b->number);
10221       delete_breakpoint (b);
10222     }
10223   if (from_tty)
10224     putchar_unfiltered ('\n');
10225 }
10226 \f
10227 /* Delete breakpoint in BS if they are `delete' breakpoints and
10228    all breakpoints that are marked for deletion, whether hit or not.
10229    This is called after any breakpoint is hit, or after errors.  */
10230
10231 void
10232 breakpoint_auto_delete (bpstat bs)
10233 {
10234   struct breakpoint *b, *b_tmp;
10235
10236   for (; bs; bs = bs->next)
10237     if (bs->breakpoint_at
10238         && bs->breakpoint_at->disposition == disp_del
10239         && bs->stop)
10240       delete_breakpoint (bs->breakpoint_at);
10241
10242   ALL_BREAKPOINTS_SAFE (b, b_tmp)
10243   {
10244     if (b->disposition == disp_del_at_next_stop)
10245       delete_breakpoint (b);
10246   }
10247 }
10248
10249 /* A comparison function for bp_location AP and BP being interfaced to
10250    qsort.  Sort elements primarily by their ADDRESS (no matter what
10251    does breakpoint_address_is_meaningful say for its OWNER),
10252    secondarily by ordering first bp_permanent OWNERed elements and
10253    terciarily just ensuring the array is sorted stable way despite
10254    qsort being an unstable algorithm.  */
10255
10256 static int
10257 bp_location_compare (const void *ap, const void *bp)
10258 {
10259   struct bp_location *a = *(void **) ap;
10260   struct bp_location *b = *(void **) bp;
10261   /* A and B come from existing breakpoints having non-NULL OWNER.  */
10262   int a_perm = a->owner->enable_state == bp_permanent;
10263   int b_perm = b->owner->enable_state == bp_permanent;
10264
10265   if (a->address != b->address)
10266     return (a->address > b->address) - (a->address < b->address);
10267
10268   /* Sort permanent breakpoints first.  */
10269   if (a_perm != b_perm)
10270     return (a_perm < b_perm) - (a_perm > b_perm);
10271
10272   /* Make the user-visible order stable across GDB runs.  Locations of
10273      the same breakpoint can be sorted in arbitrary order.  */
10274
10275   if (a->owner->number != b->owner->number)
10276     return (a->owner->number > b->owner->number)
10277            - (a->owner->number < b->owner->number);
10278
10279   return (a > b) - (a < b);
10280 }
10281
10282 /* Set bp_location_placed_address_before_address_max and
10283    bp_location_shadow_len_after_address_max according to the current
10284    content of the bp_location array.  */
10285
10286 static void
10287 bp_location_target_extensions_update (void)
10288 {
10289   struct bp_location *bl, **blp_tmp;
10290
10291   bp_location_placed_address_before_address_max = 0;
10292   bp_location_shadow_len_after_address_max = 0;
10293
10294   ALL_BP_LOCATIONS (bl, blp_tmp)
10295     {
10296       CORE_ADDR start, end, addr;
10297
10298       if (!bp_location_has_shadow (bl))
10299         continue;
10300
10301       start = bl->target_info.placed_address;
10302       end = start + bl->target_info.shadow_len;
10303
10304       gdb_assert (bl->address >= start);
10305       addr = bl->address - start;
10306       if (addr > bp_location_placed_address_before_address_max)
10307         bp_location_placed_address_before_address_max = addr;
10308
10309       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
10310
10311       gdb_assert (bl->address < end);
10312       addr = end - bl->address;
10313       if (addr > bp_location_shadow_len_after_address_max)
10314         bp_location_shadow_len_after_address_max = addr;
10315     }
10316 }
10317
10318 /* Swap the insertion/duplication state between two locations.  */
10319
10320 static void
10321 swap_insertion (struct bp_location *left, struct bp_location *right)
10322 {
10323   const int left_inserted = left->inserted;
10324   const int left_duplicate = left->duplicate;
10325   const struct bp_target_info left_target_info = left->target_info;
10326
10327   left->inserted = right->inserted;
10328   left->duplicate = right->duplicate;
10329   left->target_info = right->target_info;
10330   right->inserted = left_inserted;
10331   right->duplicate = left_duplicate;
10332   right->target_info = left_target_info;
10333 }
10334
10335 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
10336    into the inferior, only remove already-inserted locations that no
10337    longer should be inserted.  Functions that delete a breakpoint or
10338    breakpoints should pass false, so that deleting a breakpoint
10339    doesn't have the side effect of inserting the locations of other
10340    breakpoints that are marked not-inserted, but should_be_inserted
10341    returns true on them.
10342
10343    This behaviour is useful is situations close to tear-down -- e.g.,
10344    after an exec, while the target still has execution, but breakpoint
10345    shadows of the previous executable image should *NOT* be restored
10346    to the new image; or before detaching, where the target still has
10347    execution and wants to delete breakpoints from GDB's lists, and all
10348    breakpoints had already been removed from the inferior.  */
10349
10350 static void
10351 update_global_location_list (int should_insert)
10352 {
10353   struct breakpoint *b;
10354   struct bp_location **locp, *loc;
10355   struct cleanup *cleanups;
10356
10357   /* Used in the duplicates detection below.  When iterating over all
10358      bp_locations, points to the first bp_location of a given address.
10359      Breakpoints and watchpoints of different types are never
10360      duplicates of each other.  Keep one pointer for each type of
10361      breakpoint/watchpoint, so we only need to loop over all locations
10362      once.  */
10363   struct bp_location *bp_loc_first;  /* breakpoint */
10364   struct bp_location *wp_loc_first;  /* hardware watchpoint */
10365   struct bp_location *awp_loc_first; /* access watchpoint */
10366   struct bp_location *rwp_loc_first; /* read watchpoint */
10367
10368   /* Saved former bp_location array which we compare against the newly
10369      built bp_location from the current state of ALL_BREAKPOINTS.  */
10370   struct bp_location **old_location, **old_locp;
10371   unsigned old_location_count;
10372
10373   old_location = bp_location;
10374   old_location_count = bp_location_count;
10375   bp_location = NULL;
10376   bp_location_count = 0;
10377   cleanups = make_cleanup (xfree, old_location);
10378
10379   ALL_BREAKPOINTS (b)
10380     for (loc = b->loc; loc; loc = loc->next)
10381       bp_location_count++;
10382
10383   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10384   locp = bp_location;
10385   ALL_BREAKPOINTS (b)
10386     for (loc = b->loc; loc; loc = loc->next)
10387       *locp++ = loc;
10388   qsort (bp_location, bp_location_count, sizeof (*bp_location),
10389          bp_location_compare);
10390
10391   bp_location_target_extensions_update ();
10392
10393   /* Identify bp_location instances that are no longer present in the
10394      new list, and therefore should be freed.  Note that it's not
10395      necessary that those locations should be removed from inferior --
10396      if there's another location at the same address (previously
10397      marked as duplicate), we don't need to remove/insert the
10398      location.
10399      
10400      LOCP is kept in sync with OLD_LOCP, each pointing to the current
10401      and former bp_location array state respectively.  */
10402
10403   locp = bp_location;
10404   for (old_locp = old_location; old_locp < old_location + old_location_count;
10405        old_locp++)
10406     {
10407       struct bp_location *old_loc = *old_locp;
10408       struct bp_location **loc2p;
10409
10410       /* Tells if 'old_loc' is found among the new locations.  If
10411          not, we have to free it.  */
10412       int found_object = 0;
10413       /* Tells if the location should remain inserted in the target.  */
10414       int keep_in_target = 0;
10415       int removed = 0;
10416
10417       /* Skip LOCP entries which will definitely never be needed.
10418          Stop either at or being the one matching OLD_LOC.  */
10419       while (locp < bp_location + bp_location_count
10420              && (*locp)->address < old_loc->address)
10421         locp++;
10422
10423       for (loc2p = locp;
10424            (loc2p < bp_location + bp_location_count
10425             && (*loc2p)->address == old_loc->address);
10426            loc2p++)
10427         {
10428           if (*loc2p == old_loc)
10429             {
10430               found_object = 1;
10431               break;
10432             }
10433         }
10434
10435       /* If this location is no longer present, and inserted, look if
10436          there's maybe a new location at the same address.  If so,
10437          mark that one inserted, and don't remove this one.  This is
10438          needed so that we don't have a time window where a breakpoint
10439          at certain location is not inserted.  */
10440
10441       if (old_loc->inserted)
10442         {
10443           /* If the location is inserted now, we might have to remove
10444              it.  */
10445
10446           if (found_object && should_be_inserted (old_loc))
10447             {
10448               /* The location is still present in the location list,
10449                  and still should be inserted.  Don't do anything.  */
10450               keep_in_target = 1;
10451             }
10452           else
10453             {
10454               /* The location is either no longer present, or got
10455                  disabled.  See if there's another location at the
10456                  same address, in which case we don't need to remove
10457                  this one from the target.  */
10458
10459               /* OLD_LOC comes from existing struct breakpoint.  */
10460               if (breakpoint_address_is_meaningful (old_loc->owner))
10461                 {
10462                   for (loc2p = locp;
10463                        (loc2p < bp_location + bp_location_count
10464                         && (*loc2p)->address == old_loc->address);
10465                        loc2p++)
10466                     {
10467                       struct bp_location *loc2 = *loc2p;
10468
10469                       if (breakpoint_locations_match (loc2, old_loc))
10470                         {
10471                           /* Read watchpoint locations are switched to
10472                              access watchpoints, if the former are not
10473                              supported, but the latter are.  */
10474                           if (is_hardware_watchpoint (old_loc->owner))
10475                             {
10476                               gdb_assert (is_hardware_watchpoint (loc2->owner));
10477                               loc2->watchpoint_type = old_loc->watchpoint_type;
10478                             }
10479
10480                           /* loc2 is a duplicated location. We need to check
10481                              if it should be inserted in case it will be
10482                              unduplicated.  */
10483                           if (loc2 != old_loc
10484                               && unduplicated_should_be_inserted (loc2))
10485                             {
10486                               swap_insertion (old_loc, loc2);
10487                               keep_in_target = 1;
10488                               break;
10489                             }
10490                         }
10491                     }
10492                 }
10493             }
10494
10495           if (!keep_in_target)
10496             {
10497               if (remove_breakpoint (old_loc, mark_uninserted))
10498                 {
10499                   /* This is just about all we can do.  We could keep
10500                      this location on the global list, and try to
10501                      remove it next time, but there's no particular
10502                      reason why we will succeed next time.
10503                      
10504                      Note that at this point, old_loc->owner is still
10505                      valid, as delete_breakpoint frees the breakpoint
10506                      only after calling us.  */
10507                   printf_filtered (_("warning: Error removing "
10508                                      "breakpoint %d\n"), 
10509                                    old_loc->owner->number);
10510                 }
10511               removed = 1;
10512             }
10513         }
10514
10515       if (!found_object)
10516         {
10517           if (removed && non_stop
10518               && breakpoint_address_is_meaningful (old_loc->owner)
10519               && !is_hardware_watchpoint (old_loc->owner))
10520             {
10521               /* This location was removed from the target.  In
10522                  non-stop mode, a race condition is possible where
10523                  we've removed a breakpoint, but stop events for that
10524                  breakpoint are already queued and will arrive later.
10525                  We apply an heuristic to be able to distinguish such
10526                  SIGTRAPs from other random SIGTRAPs: we keep this
10527                  breakpoint location for a bit, and will retire it
10528                  after we see some number of events.  The theory here
10529                  is that reporting of events should, "on the average",
10530                  be fair, so after a while we'll see events from all
10531                  threads that have anything of interest, and no longer
10532                  need to keep this breakpoint location around.  We
10533                  don't hold locations forever so to reduce chances of
10534                  mistaking a non-breakpoint SIGTRAP for a breakpoint
10535                  SIGTRAP.
10536
10537                  The heuristic failing can be disastrous on
10538                  decr_pc_after_break targets.
10539
10540                  On decr_pc_after_break targets, like e.g., x86-linux,
10541                  if we fail to recognize a late breakpoint SIGTRAP,
10542                  because events_till_retirement has reached 0 too
10543                  soon, we'll fail to do the PC adjustment, and report
10544                  a random SIGTRAP to the user.  When the user resumes
10545                  the inferior, it will most likely immediately crash
10546                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
10547                  corrupted, because of being resumed e.g., in the
10548                  middle of a multi-byte instruction, or skipped a
10549                  one-byte instruction.  This was actually seen happen
10550                  on native x86-linux, and should be less rare on
10551                  targets that do not support new thread events, like
10552                  remote, due to the heuristic depending on
10553                  thread_count.
10554
10555                  Mistaking a random SIGTRAP for a breakpoint trap
10556                  causes similar symptoms (PC adjustment applied when
10557                  it shouldn't), but then again, playing with SIGTRAPs
10558                  behind the debugger's back is asking for trouble.
10559
10560                  Since hardware watchpoint traps are always
10561                  distinguishable from other traps, so we don't need to
10562                  apply keep hardware watchpoint moribund locations
10563                  around.  We simply always ignore hardware watchpoint
10564                  traps we can no longer explain.  */
10565
10566               old_loc->events_till_retirement = 3 * (thread_count () + 1);
10567               old_loc->owner = NULL;
10568
10569               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
10570             }
10571           else
10572             {
10573               old_loc->owner = NULL;
10574               decref_bp_location (&old_loc);
10575             }
10576         }
10577     }
10578
10579   /* Rescan breakpoints at the same address and section, marking the
10580      first one as "first" and any others as "duplicates".  This is so
10581      that the bpt instruction is only inserted once.  If we have a
10582      permanent breakpoint at the same place as BPT, make that one the
10583      official one, and the rest as duplicates.  Permanent breakpoints
10584      are sorted first for the same address.
10585
10586      Do the same for hardware watchpoints, but also considering the
10587      watchpoint's type (regular/access/read) and length.  */
10588
10589   bp_loc_first = NULL;
10590   wp_loc_first = NULL;
10591   awp_loc_first = NULL;
10592   rwp_loc_first = NULL;
10593   ALL_BP_LOCATIONS (loc, locp)
10594     {
10595       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10596          non-NULL.  */
10597       struct breakpoint *b = loc->owner;
10598       struct bp_location **loc_first_p;
10599
10600       if (b->enable_state == bp_disabled
10601           || b->enable_state == bp_call_disabled
10602           || b->enable_state == bp_startup_disabled
10603           || !loc->enabled
10604           || loc->shlib_disabled
10605           || !breakpoint_address_is_meaningful (b)
10606           || is_tracepoint (b))
10607         continue;
10608
10609       /* Permanent breakpoint should always be inserted.  */
10610       if (b->enable_state == bp_permanent && ! loc->inserted)
10611         internal_error (__FILE__, __LINE__,
10612                         _("allegedly permanent breakpoint is not "
10613                         "actually inserted"));
10614
10615       if (b->type == bp_hardware_watchpoint)
10616         loc_first_p = &wp_loc_first;
10617       else if (b->type == bp_read_watchpoint)
10618         loc_first_p = &rwp_loc_first;
10619       else if (b->type == bp_access_watchpoint)
10620         loc_first_p = &awp_loc_first;
10621       else
10622         loc_first_p = &bp_loc_first;
10623
10624       if (*loc_first_p == NULL
10625           || (overlay_debugging && loc->section != (*loc_first_p)->section)
10626           || !breakpoint_locations_match (loc, *loc_first_p))
10627         {
10628           *loc_first_p = loc;
10629           loc->duplicate = 0;
10630           continue;
10631         }
10632
10633
10634       /* This and the above ensure the invariant that the first location
10635          is not duplicated, and is the inserted one.
10636          All following are marked as duplicated, and are not inserted.  */
10637       if (loc->inserted)
10638         swap_insertion (loc, *loc_first_p);
10639       loc->duplicate = 1;
10640
10641       if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10642           && b->enable_state != bp_permanent)
10643         internal_error (__FILE__, __LINE__,
10644                         _("another breakpoint was inserted on top of "
10645                         "a permanent breakpoint"));
10646     }
10647
10648   if (breakpoints_always_inserted_mode () && should_insert
10649       && (have_live_inferiors ()
10650           || (gdbarch_has_global_breakpoints (target_gdbarch))))
10651     insert_breakpoint_locations ();
10652
10653   do_cleanups (cleanups);
10654 }
10655
10656 void
10657 breakpoint_retire_moribund (void)
10658 {
10659   struct bp_location *loc;
10660   int ix;
10661
10662   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10663     if (--(loc->events_till_retirement) == 0)
10664       {
10665         decref_bp_location (&loc);
10666         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10667         --ix;
10668       }
10669 }
10670
10671 static void
10672 update_global_location_list_nothrow (int inserting)
10673 {
10674   struct gdb_exception e;
10675
10676   TRY_CATCH (e, RETURN_MASK_ERROR)
10677     update_global_location_list (inserting);
10678 }
10679
10680 /* Clear BKP from a BPS.  */
10681
10682 static void
10683 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10684 {
10685   bpstat bs;
10686
10687   for (bs = bps; bs; bs = bs->next)
10688     if (bs->breakpoint_at == bpt)
10689       {
10690         bs->breakpoint_at = NULL;
10691         bs->old_val = NULL;
10692         /* bs->commands will be freed later.  */
10693       }
10694 }
10695
10696 /* Callback for iterate_over_threads.  */
10697 static int
10698 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10699 {
10700   struct breakpoint *bpt = data;
10701
10702   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10703   return 0;
10704 }
10705
10706 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
10707    callbacks.  */
10708
10709 static void
10710 say_where (struct breakpoint *b)
10711 {
10712   struct ui_out *uiout = current_uiout;
10713   struct value_print_options opts;
10714
10715   get_user_print_options (&opts);
10716
10717   /* i18n: cagney/2005-02-11: Below needs to be merged into a
10718      single string.  */
10719   if (b->loc == NULL)
10720     {
10721       printf_filtered (_(" (%s) pending."), b->addr_string);
10722     }
10723   else
10724     {
10725       if (opts.addressprint || b->source_file == NULL)
10726         {
10727           printf_filtered (" at ");
10728           fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10729                           gdb_stdout);
10730         }
10731       if (b->source_file)
10732         printf_filtered (": file %s, line %d.",
10733                          b->source_file, b->line_number);
10734
10735       if (b->loc->next)
10736         {
10737           struct bp_location *loc = b->loc;
10738           int n = 0;
10739           for (; loc; loc = loc->next)
10740             ++n;
10741           printf_filtered (" (%d locations)", n);
10742         }
10743     }
10744 }
10745
10746 /* Default bp_location_ops methods.  */
10747
10748 static void
10749 bp_location_dtor (struct bp_location *self)
10750 {
10751   xfree (self->cond);
10752   xfree (self->function_name);
10753 }
10754
10755 static const struct bp_location_ops bp_location_ops =
10756 {
10757   bp_location_dtor
10758 };
10759
10760 /* Default breakpoint_ops methods all breakpoint_ops ultimately
10761    inherit from.  */
10762
10763 static void
10764 base_breakpoint_dtor (struct breakpoint *self)
10765 {
10766   decref_counted_command_line (&self->commands);
10767   xfree (self->cond_string);
10768   xfree (self->addr_string);
10769   xfree (self->addr_string_range_end);
10770   xfree (self->source_file);
10771 }
10772
10773 static struct bp_location *
10774 base_breakpoint_allocate_location (struct breakpoint *self)
10775 {
10776   struct bp_location *loc;
10777
10778   loc = XNEW (struct bp_location);
10779   init_bp_location (loc, &bp_location_ops, self);
10780   return loc;
10781 }
10782
10783 static void
10784 base_breakpoint_re_set (struct breakpoint *b)
10785 {
10786   /* Nothing to re-set. */
10787 }
10788
10789 #define internal_error_pure_virtual_called() \
10790   gdb_assert_not_reached ("pure virtual function called")
10791
10792 static int
10793 base_breakpoint_insert_location (struct bp_location *bl)
10794 {
10795   internal_error_pure_virtual_called ();
10796 }
10797
10798 static int
10799 base_breakpoint_remove_location (struct bp_location *bl)
10800 {
10801   internal_error_pure_virtual_called ();
10802 }
10803
10804 static int
10805 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10806                                 struct address_space *aspace,
10807                                 CORE_ADDR bp_addr)
10808 {
10809   internal_error_pure_virtual_called ();
10810 }
10811
10812 static void
10813 base_breakpoint_check_status (bpstat bs)
10814 {
10815   /* Always stop.   */
10816 }
10817
10818 /* A "works_in_software_mode" breakpoint_ops method that just internal
10819    errors.  */
10820
10821 static int
10822 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10823 {
10824   internal_error_pure_virtual_called ();
10825 }
10826
10827 /* A "resources_needed" breakpoint_ops method that just internal
10828    errors.  */
10829
10830 static int
10831 base_breakpoint_resources_needed (const struct bp_location *bl)
10832 {
10833   internal_error_pure_virtual_called ();
10834 }
10835
10836 static enum print_stop_action
10837 base_breakpoint_print_it (bpstat bs)
10838 {
10839   internal_error_pure_virtual_called ();
10840 }
10841
10842 static void
10843 base_breakpoint_print_one_detail (const struct breakpoint *self,
10844                                   struct ui_out *uiout)
10845 {
10846   /* nothing */
10847 }
10848
10849 static void
10850 base_breakpoint_print_mention (struct breakpoint *b)
10851 {
10852   internal_error_pure_virtual_called ();
10853 }
10854
10855 static void
10856 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10857 {
10858   internal_error_pure_virtual_called ();
10859 }
10860
10861 static struct breakpoint_ops base_breakpoint_ops =
10862 {
10863   base_breakpoint_dtor,
10864   base_breakpoint_allocate_location,
10865   base_breakpoint_re_set,
10866   base_breakpoint_insert_location,
10867   base_breakpoint_remove_location,
10868   base_breakpoint_breakpoint_hit,
10869   base_breakpoint_check_status,
10870   base_breakpoint_resources_needed,
10871   base_breakpoint_works_in_software_mode,
10872   base_breakpoint_print_it,
10873   NULL,
10874   base_breakpoint_print_one_detail,
10875   base_breakpoint_print_mention,
10876   base_breakpoint_print_recreate
10877 };
10878
10879 /* Default breakpoint_ops methods.  */
10880
10881 static void
10882 bkpt_re_set (struct breakpoint *b)
10883 {
10884   /* Do not attempt to re-set breakpoints disabled during startup.  */
10885   if (b->enable_state == bp_startup_disabled)
10886     return;
10887
10888   /* FIXME: is this still reachable?  */
10889   if (b->addr_string == NULL)
10890     {
10891       /* Anything without a string can't be re-set.  */
10892       delete_breakpoint (b);
10893       return;
10894     }
10895
10896   breakpoint_re_set_default (b);
10897 }
10898
10899 static int
10900 bkpt_insert_location (struct bp_location *bl)
10901 {
10902   if (bl->loc_type == bp_loc_hardware_breakpoint)
10903     return target_insert_hw_breakpoint (bl->gdbarch,
10904                                         &bl->target_info);
10905   else
10906     return target_insert_breakpoint (bl->gdbarch,
10907                                      &bl->target_info);
10908 }
10909
10910 static int
10911 bkpt_remove_location (struct bp_location *bl)
10912 {
10913   if (bl->loc_type == bp_loc_hardware_breakpoint)
10914     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10915   else
10916     return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10917 }
10918
10919 static int
10920 bkpt_breakpoint_hit (const struct bp_location *bl,
10921                      struct address_space *aspace, CORE_ADDR bp_addr)
10922 {
10923   struct breakpoint *b = bl->owner;
10924
10925   if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10926                                  aspace, bp_addr))
10927     return 0;
10928
10929   if (overlay_debugging         /* unmapped overlay section */
10930       && section_is_overlay (bl->section)
10931       && !section_is_mapped (bl->section))
10932     return 0;
10933
10934   return 1;
10935 }
10936
10937 static int
10938 bkpt_resources_needed (const struct bp_location *bl)
10939 {
10940   gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10941
10942   return 1;
10943 }
10944
10945 static enum print_stop_action
10946 bkpt_print_it (bpstat bs)
10947 {
10948   struct breakpoint *b;
10949   const struct bp_location *bl;
10950   int bp_temp;
10951   struct ui_out *uiout = current_uiout;
10952
10953   gdb_assert (bs->bp_location_at != NULL);
10954
10955   bl = bs->bp_location_at;
10956   b = bs->breakpoint_at;
10957
10958   bp_temp = b->disposition == disp_del;
10959   if (bl->address != bl->requested_address)
10960     breakpoint_adjustment_warning (bl->requested_address,
10961                                    bl->address,
10962                                    b->number, 1);
10963   annotate_breakpoint (b->number);
10964   if (bp_temp)
10965     ui_out_text (uiout, "\nTemporary breakpoint ");
10966   else
10967     ui_out_text (uiout, "\nBreakpoint ");
10968   if (ui_out_is_mi_like_p (uiout))
10969     {
10970       ui_out_field_string (uiout, "reason",
10971                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10972       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10973     }
10974   ui_out_field_int (uiout, "bkptno", b->number);
10975   ui_out_text (uiout, ", ");
10976
10977   return PRINT_SRC_AND_LOC;
10978 }
10979
10980 static void
10981 bkpt_print_mention (struct breakpoint *b)
10982 {
10983   if (ui_out_is_mi_like_p (current_uiout))
10984     return;
10985
10986   switch (b->type)
10987     {
10988     case bp_breakpoint:
10989     case bp_gnu_ifunc_resolver:
10990       if (b->disposition == disp_del)
10991         printf_filtered (_("Temporary breakpoint"));
10992       else
10993         printf_filtered (_("Breakpoint"));
10994       printf_filtered (_(" %d"), b->number);
10995       if (b->type == bp_gnu_ifunc_resolver)
10996         printf_filtered (_(" at gnu-indirect-function resolver"));
10997       break;
10998     case bp_hardware_breakpoint:
10999       printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
11000       break;
11001     }
11002
11003   say_where (b);
11004 }
11005
11006 static void
11007 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11008 {
11009   if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11010     fprintf_unfiltered (fp, "tbreak");
11011   else if (tp->type == bp_breakpoint)
11012     fprintf_unfiltered (fp, "break");
11013   else if (tp->type == bp_hardware_breakpoint
11014            && tp->disposition == disp_del)
11015     fprintf_unfiltered (fp, "thbreak");
11016   else if (tp->type == bp_hardware_breakpoint)
11017     fprintf_unfiltered (fp, "hbreak");
11018   else
11019     internal_error (__FILE__, __LINE__,
11020                     _("unhandled breakpoint type %d"), (int) tp->type);
11021
11022   fprintf_unfiltered (fp, " %s", tp->addr_string);
11023   print_recreate_thread (tp, fp);
11024 }
11025
11026 /* Virtual table for internal breakpoints.  */
11027
11028 static void
11029 internal_bkpt_re_set (struct breakpoint *b)
11030 {
11031   switch (b->type)
11032     {
11033       /* Delete overlay event and longjmp master breakpoints; they
11034          will be reset later by breakpoint_re_set.  */
11035     case bp_overlay_event:
11036     case bp_longjmp_master:
11037     case bp_std_terminate_master:
11038     case bp_exception_master:
11039       delete_breakpoint (b);
11040       break;
11041
11042       /* This breakpoint is special, it's set up when the inferior
11043          starts and we really don't want to touch it.  */
11044     case bp_shlib_event:
11045
11046       /* Like bp_shlib_event, this breakpoint type is special.  Once
11047          it is set up, we do not want to touch it.  */
11048     case bp_thread_event:
11049       break;
11050     }
11051 }
11052
11053 static void
11054 internal_bkpt_check_status (bpstat bs)
11055 {
11056   /* We do not stop for these.  */
11057   bs->stop = 0;
11058 }
11059
11060 static enum print_stop_action
11061 internal_bkpt_print_it (bpstat bs)
11062 {
11063   struct breakpoint *b;
11064
11065   b = bs->breakpoint_at;
11066
11067   switch (b->type)
11068     {
11069     case bp_shlib_event:
11070       /* Did we stop because the user set the stop_on_solib_events
11071          variable?  (If so, we report this as a generic, "Stopped due
11072          to shlib event" message.) */
11073       printf_filtered (_("Stopped due to shared library event\n"));
11074       break;
11075
11076     case bp_thread_event:
11077       /* Not sure how we will get here.
11078          GDB should not stop for these breakpoints.  */
11079       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
11080       break;
11081
11082     case bp_overlay_event:
11083       /* By analogy with the thread event, GDB should not stop for these.  */
11084       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11085       break;
11086
11087     case bp_longjmp_master:
11088       /* These should never be enabled.  */
11089       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11090       break;
11091
11092     case bp_std_terminate_master:
11093       /* These should never be enabled.  */
11094       printf_filtered (_("std::terminate Master Breakpoint: "
11095                          "gdb should not stop!\n"));
11096       break;
11097
11098     case bp_exception_master:
11099       /* These should never be enabled.  */
11100       printf_filtered (_("Exception Master Breakpoint: "
11101                          "gdb should not stop!\n"));
11102       break;
11103     }
11104
11105   return PRINT_NOTHING;
11106 }
11107
11108 static void
11109 internal_bkpt_print_mention (struct breakpoint *b)
11110 {
11111   /* Nothing to mention.  These breakpoints are internal.  */
11112 }
11113
11114 /* Virtual table for momentary breakpoints  */
11115
11116 static void
11117 momentary_bkpt_re_set (struct breakpoint *b)
11118 {
11119   /* Keep temporary breakpoints, which can be encountered when we step
11120      over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11121      Otherwise these should have been blown away via the cleanup chain
11122      or by breakpoint_init_inferior when we rerun the executable.  */
11123 }
11124
11125 static void
11126 momentary_bkpt_check_status (bpstat bs)
11127 {
11128   /* Nothing.  The point of these breakpoints is causing a stop.  */
11129 }
11130
11131 static enum print_stop_action
11132 momentary_bkpt_print_it (bpstat bs)
11133 {
11134   struct ui_out *uiout = current_uiout;
11135
11136   if (ui_out_is_mi_like_p (uiout))
11137     {
11138       struct breakpoint *b = bs->breakpoint_at;
11139
11140       switch (b->type)
11141         {
11142         case bp_finish:
11143           ui_out_field_string
11144             (uiout, "reason",
11145              async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11146           break;
11147
11148         case bp_until:
11149           ui_out_field_string
11150             (uiout, "reason",
11151              async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11152           break;
11153         }
11154     }
11155
11156   return PRINT_UNKNOWN;
11157 }
11158
11159 static void
11160 momentary_bkpt_print_mention (struct breakpoint *b)
11161 {
11162   /* Nothing to mention.  These breakpoints are internal.  */
11163 }
11164
11165 /* The breakpoint_ops structure to be used in tracepoints.  */
11166
11167 static void
11168 tracepoint_re_set (struct breakpoint *b)
11169 {
11170   breakpoint_re_set_default (b);
11171 }
11172
11173 static int
11174 tracepoint_breakpoint_hit (const struct bp_location *bl,
11175                            struct address_space *aspace, CORE_ADDR bp_addr)
11176 {
11177   /* By definition, the inferior does not report stops at
11178      tracepoints.  */
11179   return 0;
11180 }
11181
11182 static void
11183 tracepoint_print_one_detail (const struct breakpoint *self,
11184                              struct ui_out *uiout)
11185 {
11186   struct tracepoint *tp = (struct tracepoint *) self;
11187   if (tp->static_trace_marker_id)
11188     {
11189       gdb_assert (self->type == bp_static_tracepoint);
11190
11191       ui_out_text (uiout, "\tmarker id is ");
11192       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
11193                            tp->static_trace_marker_id);
11194       ui_out_text (uiout, "\n");
11195     }
11196 }
11197
11198 static void
11199 tracepoint_print_mention (struct breakpoint *b)
11200 {
11201   if (ui_out_is_mi_like_p (current_uiout))
11202     return;
11203
11204   switch (b->type)
11205     {
11206     case bp_tracepoint:
11207       printf_filtered (_("Tracepoint"));
11208       printf_filtered (_(" %d"), b->number);
11209       break;
11210     case bp_fast_tracepoint:
11211       printf_filtered (_("Fast tracepoint"));
11212       printf_filtered (_(" %d"), b->number);
11213       break;
11214     case bp_static_tracepoint:
11215       printf_filtered (_("Static tracepoint"));
11216       printf_filtered (_(" %d"), b->number);
11217       break;
11218     default:
11219       internal_error (__FILE__, __LINE__,
11220                       _("unhandled tracepoint type %d"), (int) b->type);
11221     }
11222
11223   say_where (b);
11224 }
11225
11226 static void
11227 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
11228 {
11229   struct tracepoint *tp = (struct tracepoint *) self;
11230
11231   if (self->type == bp_fast_tracepoint)
11232     fprintf_unfiltered (fp, "ftrace");
11233   if (self->type == bp_static_tracepoint)
11234     fprintf_unfiltered (fp, "strace");
11235   else if (self->type == bp_tracepoint)
11236     fprintf_unfiltered (fp, "trace");
11237   else
11238     internal_error (__FILE__, __LINE__,
11239                     _("unhandled tracepoint type %d"), (int) self->type);
11240
11241   fprintf_unfiltered (fp, " %s", self->addr_string);
11242   print_recreate_thread (self, fp);
11243
11244   if (tp->pass_count)
11245     fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
11246 }
11247
11248 struct breakpoint_ops tracepoint_breakpoint_ops;
11249
11250 /* Delete a breakpoint and clean up all traces of it in the data
11251    structures.  */
11252
11253 void
11254 delete_breakpoint (struct breakpoint *bpt)
11255 {
11256   struct breakpoint *b;
11257
11258   gdb_assert (bpt != NULL);
11259
11260   /* Has this bp already been deleted?  This can happen because
11261      multiple lists can hold pointers to bp's.  bpstat lists are
11262      especial culprits.
11263
11264      One example of this happening is a watchpoint's scope bp.  When
11265      the scope bp triggers, we notice that the watchpoint is out of
11266      scope, and delete it.  We also delete its scope bp.  But the
11267      scope bp is marked "auto-deleting", and is already on a bpstat.
11268      That bpstat is then checked for auto-deleting bp's, which are
11269      deleted.
11270
11271      A real solution to this problem might involve reference counts in
11272      bp's, and/or giving them pointers back to their referencing
11273      bpstat's, and teaching delete_breakpoint to only free a bp's
11274      storage when no more references were extent.  A cheaper bandaid
11275      was chosen.  */
11276   if (bpt->type == bp_none)
11277     return;
11278
11279   /* At least avoid this stale reference until the reference counting
11280      of breakpoints gets resolved.  */
11281   if (bpt->related_breakpoint != bpt)
11282     {
11283       struct breakpoint *related;
11284       struct watchpoint *w;
11285
11286       if (bpt->type == bp_watchpoint_scope)
11287         w = (struct watchpoint *) bpt->related_breakpoint;
11288       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
11289         w = (struct watchpoint *) bpt;
11290       else
11291         w = NULL;
11292       if (w != NULL)
11293         watchpoint_del_at_next_stop (w);
11294
11295       /* Unlink bpt from the bpt->related_breakpoint ring.  */
11296       for (related = bpt; related->related_breakpoint != bpt;
11297            related = related->related_breakpoint);
11298       related->related_breakpoint = bpt->related_breakpoint;
11299       bpt->related_breakpoint = bpt;
11300     }
11301
11302   /* watch_command_1 creates a watchpoint but only sets its number if
11303      update_watchpoint succeeds in creating its bp_locations.  If there's
11304      a problem in that process, we'll be asked to delete the half-created
11305      watchpoint.  In that case, don't announce the deletion.  */
11306   if (bpt->number)
11307     observer_notify_breakpoint_deleted (bpt);
11308
11309   if (breakpoint_chain == bpt)
11310     breakpoint_chain = bpt->next;
11311
11312   ALL_BREAKPOINTS (b)
11313     if (b->next == bpt)
11314     {
11315       b->next = bpt->next;
11316       break;
11317     }
11318
11319   /* Be sure no bpstat's are pointing at the breakpoint after it's
11320      been freed.  */
11321   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
11322      in all threads for now.  Note that we cannot just remove bpstats
11323      pointing at bpt from the stop_bpstat list entirely, as breakpoint
11324      commands are associated with the bpstat; if we remove it here,
11325      then the later call to bpstat_do_actions (&stop_bpstat); in
11326      event-top.c won't do anything, and temporary breakpoints with
11327      commands won't work.  */
11328
11329   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11330
11331   /* Now that breakpoint is removed from breakpoint list, update the
11332      global location list.  This will remove locations that used to
11333      belong to this breakpoint.  Do this before freeing the breakpoint
11334      itself, since remove_breakpoint looks at location's owner.  It
11335      might be better design to have location completely
11336      self-contained, but it's not the case now.  */
11337   update_global_location_list (0);
11338
11339   bpt->ops->dtor (bpt);
11340   /* On the chance that someone will soon try again to delete this
11341      same bp, we mark it as deleted before freeing its storage.  */
11342   bpt->type = bp_none;
11343   xfree (bpt);
11344 }
11345
11346 static void
11347 do_delete_breakpoint_cleanup (void *b)
11348 {
11349   delete_breakpoint (b);
11350 }
11351
11352 struct cleanup *
11353 make_cleanup_delete_breakpoint (struct breakpoint *b)
11354 {
11355   return make_cleanup (do_delete_breakpoint_cleanup, b);
11356 }
11357
11358 /* Iterator function to call a user-provided callback function once
11359    for each of B and its related breakpoints.  */
11360
11361 static void
11362 iterate_over_related_breakpoints (struct breakpoint *b,
11363                                   void (*function) (struct breakpoint *,
11364                                                     void *),
11365                                   void *data)
11366 {
11367   struct breakpoint *related;
11368
11369   related = b;
11370   do
11371     {
11372       struct breakpoint *next;
11373
11374       /* FUNCTION may delete RELATED.  */
11375       next = related->related_breakpoint;
11376
11377       if (next == related)
11378         {
11379           /* RELATED is the last ring entry.  */
11380           function (related, data);
11381
11382           /* FUNCTION may have deleted it, so we'd never reach back to
11383              B.  There's nothing left to do anyway, so just break
11384              out.  */
11385           break;
11386         }
11387       else
11388         function (related, data);
11389
11390       related = next;
11391     }
11392   while (related != b);
11393 }
11394
11395 static void
11396 do_delete_breakpoint (struct breakpoint *b, void *ignore)
11397 {
11398   delete_breakpoint (b);
11399 }
11400
11401 /* A callback for map_breakpoint_numbers that calls
11402    delete_breakpoint.  */
11403
11404 static void
11405 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11406 {
11407   iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11408 }
11409
11410 void
11411 delete_command (char *arg, int from_tty)
11412 {
11413   struct breakpoint *b, *b_tmp;
11414
11415   dont_repeat ();
11416
11417   if (arg == 0)
11418     {
11419       int breaks_to_delete = 0;
11420
11421       /* Delete all breakpoints if no argument.  Do not delete
11422          internal breakpoints, these have to be deleted with an
11423          explicit breakpoint number argument.  */
11424       ALL_BREAKPOINTS (b)
11425         if (user_breakpoint_p (b))
11426           {
11427             breaks_to_delete = 1;
11428             break;
11429           }
11430
11431       /* Ask user only if there are some breakpoints to delete.  */
11432       if (!from_tty
11433           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
11434         {
11435           ALL_BREAKPOINTS_SAFE (b, b_tmp)
11436             if (user_breakpoint_p (b))
11437               delete_breakpoint (b);
11438         }
11439     }
11440   else
11441     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
11442 }
11443
11444 static int
11445 all_locations_are_pending (struct bp_location *loc)
11446 {
11447   for (; loc; loc = loc->next)
11448     if (!loc->shlib_disabled)
11449       return 0;
11450   return 1;
11451 }
11452
11453 /* Subroutine of update_breakpoint_locations to simplify it.
11454    Return non-zero if multiple fns in list LOC have the same name.
11455    Null names are ignored.  */
11456
11457 static int
11458 ambiguous_names_p (struct bp_location *loc)
11459 {
11460   struct bp_location *l;
11461   htab_t htab = htab_create_alloc (13, htab_hash_string,
11462                                    (int (*) (const void *, 
11463                                              const void *)) streq,
11464                                    NULL, xcalloc, xfree);
11465
11466   for (l = loc; l != NULL; l = l->next)
11467     {
11468       const char **slot;
11469       const char *name = l->function_name;
11470
11471       /* Allow for some names to be NULL, ignore them.  */
11472       if (name == NULL)
11473         continue;
11474
11475       slot = (const char **) htab_find_slot (htab, (const void *) name,
11476                                              INSERT);
11477       /* NOTE: We can assume slot != NULL here because xcalloc never
11478          returns NULL.  */
11479       if (*slot != NULL)
11480         {
11481           htab_delete (htab);
11482           return 1;
11483         }
11484       *slot = name;
11485     }
11486
11487   htab_delete (htab);
11488   return 0;
11489 }
11490
11491 /* When symbols change, it probably means the sources changed as well,
11492    and it might mean the static tracepoint markers are no longer at
11493    the same address or line numbers they used to be at last we
11494    checked.  Losing your static tracepoints whenever you rebuild is
11495    undesirable.  This function tries to resync/rematch gdb static
11496    tracepoints with the markers on the target, for static tracepoints
11497    that have not been set by marker id.  Static tracepoint that have
11498    been set by marker id are reset by marker id in breakpoint_re_set.
11499    The heuristic is:
11500
11501    1) For a tracepoint set at a specific address, look for a marker at
11502    the old PC.  If one is found there, assume to be the same marker.
11503    If the name / string id of the marker found is different from the
11504    previous known name, assume that means the user renamed the marker
11505    in the sources, and output a warning.
11506
11507    2) For a tracepoint set at a given line number, look for a marker
11508    at the new address of the old line number.  If one is found there,
11509    assume to be the same marker.  If the name / string id of the
11510    marker found is different from the previous known name, assume that
11511    means the user renamed the marker in the sources, and output a
11512    warning.
11513
11514    3) If a marker is no longer found at the same address or line, it
11515    may mean the marker no longer exists.  But it may also just mean
11516    the code changed a bit.  Maybe the user added a few lines of code
11517    that made the marker move up or down (in line number terms).  Ask
11518    the target for info about the marker with the string id as we knew
11519    it.  If found, update line number and address in the matching
11520    static tracepoint.  This will get confused if there's more than one
11521    marker with the same ID (possible in UST, although unadvised
11522    precisely because it confuses tools).  */
11523
11524 static struct symtab_and_line
11525 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11526 {
11527   struct tracepoint *tp = (struct tracepoint *) b;
11528   struct static_tracepoint_marker marker;
11529   CORE_ADDR pc;
11530   int i;
11531
11532   pc = sal.pc;
11533   if (sal.line)
11534     find_line_pc (sal.symtab, sal.line, &pc);
11535
11536   if (target_static_tracepoint_marker_at (pc, &marker))
11537     {
11538       if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
11539         warning (_("static tracepoint %d changed probed marker from %s to %s"),
11540                  b->number,
11541                  tp->static_trace_marker_id, marker.str_id);
11542
11543       xfree (tp->static_trace_marker_id);
11544       tp->static_trace_marker_id = xstrdup (marker.str_id);
11545       release_static_tracepoint_marker (&marker);
11546
11547       return sal;
11548     }
11549
11550   /* Old marker wasn't found on target at lineno.  Try looking it up
11551      by string ID.  */
11552   if (!sal.explicit_pc
11553       && sal.line != 0
11554       && sal.symtab != NULL
11555       && tp->static_trace_marker_id != NULL)
11556     {
11557       VEC(static_tracepoint_marker_p) *markers;
11558
11559       markers
11560         = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
11561
11562       if (!VEC_empty(static_tracepoint_marker_p, markers))
11563         {
11564           struct symtab_and_line sal;
11565           struct symbol *sym;
11566           struct static_tracepoint_marker *marker;
11567           struct ui_out *uiout = current_uiout;
11568
11569           marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11570
11571           xfree (tp->static_trace_marker_id);
11572           tp->static_trace_marker_id = xstrdup (marker->str_id);
11573
11574           warning (_("marker for static tracepoint %d (%s) not "
11575                      "found at previous line number"),
11576                    b->number, tp->static_trace_marker_id);
11577
11578           init_sal (&sal);
11579
11580           sal.pc = marker->address;
11581
11582           sal = find_pc_line (marker->address, 0);
11583           sym = find_pc_sect_function (marker->address, NULL);
11584           ui_out_text (uiout, "Now in ");
11585           if (sym)
11586             {
11587               ui_out_field_string (uiout, "func",
11588                                    SYMBOL_PRINT_NAME (sym));
11589               ui_out_text (uiout, " at ");
11590             }
11591           ui_out_field_string (uiout, "file", sal.symtab->filename);
11592           ui_out_text (uiout, ":");
11593
11594           if (ui_out_is_mi_like_p (uiout))
11595             {
11596               char *fullname = symtab_to_fullname (sal.symtab);
11597
11598               if (fullname)
11599                 ui_out_field_string (uiout, "fullname", fullname);
11600             }
11601
11602           ui_out_field_int (uiout, "line", sal.line);
11603           ui_out_text (uiout, "\n");
11604
11605           b->line_number = sal.line;
11606
11607           xfree (b->source_file);
11608           if (sym)
11609             b->source_file = xstrdup (sal.symtab->filename);
11610           else
11611             b->source_file = NULL;
11612
11613           xfree (b->addr_string);
11614           b->addr_string = xstrprintf ("%s:%d",
11615                                        sal.symtab->filename, b->line_number);
11616
11617           /* Might be nice to check if function changed, and warn if
11618              so.  */
11619
11620           release_static_tracepoint_marker (marker);
11621         }
11622     }
11623   return sal;
11624 }
11625
11626 /* Returns 1 iff locations A and B are sufficiently same that
11627    we don't need to report breakpoint as changed.  */
11628
11629 static int
11630 locations_are_equal (struct bp_location *a, struct bp_location *b)
11631 {
11632   while (a && b)
11633     {
11634       if (a->address != b->address)
11635         return 0;
11636
11637       if (a->shlib_disabled != b->shlib_disabled)
11638         return 0;
11639
11640       if (a->enabled != b->enabled)
11641         return 0;
11642
11643       a = a->next;
11644       b = b->next;
11645     }
11646
11647   if ((a == NULL) != (b == NULL))
11648     return 0;
11649
11650   return 1;
11651 }
11652
11653 /* Create new breakpoint locations for B (a hardware or software breakpoint)
11654    based on SALS and SALS_END.  If SALS_END.NELTS is not zero, then B is
11655    a ranged breakpoint.  */
11656
11657 void
11658 update_breakpoint_locations (struct breakpoint *b,
11659                              struct symtabs_and_lines sals,
11660                              struct symtabs_and_lines sals_end)
11661 {
11662   int i;
11663   struct bp_location *existing_locations = b->loc;
11664
11665   /* Ranged breakpoints have only one start location and one end location.  */
11666   gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11667
11668   /* If there's no new locations, and all existing locations are
11669      pending, don't do anything.  This optimizes the common case where
11670      all locations are in the same shared library, that was unloaded.
11671      We'd like to retain the location, so that when the library is
11672      loaded again, we don't loose the enabled/disabled status of the
11673      individual locations.  */
11674   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
11675     return;
11676
11677   b->loc = NULL;
11678
11679   for (i = 0; i < sals.nelts; ++i)
11680     {
11681       struct bp_location *new_loc = 
11682         add_location_to_breakpoint (b, &(sals.sals[i]));
11683
11684       /* Reparse conditions, they might contain references to the
11685          old symtab.  */
11686       if (b->cond_string != NULL)
11687         {
11688           char *s;
11689           struct gdb_exception e;
11690
11691           s = b->cond_string;
11692           TRY_CATCH (e, RETURN_MASK_ERROR)
11693             {
11694               new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 
11695                                            0);
11696             }
11697           if (e.reason < 0)
11698             {
11699               warning (_("failed to reevaluate condition "
11700                          "for breakpoint %d: %s"), 
11701                        b->number, e.message);
11702               new_loc->enabled = 0;
11703             }
11704         }
11705
11706       if (b->source_file != NULL)
11707         xfree (b->source_file);
11708       if (sals.sals[i].symtab == NULL)
11709         b->source_file = NULL;
11710       else
11711         b->source_file = xstrdup (sals.sals[i].symtab->filename);
11712
11713       if (b->line_number == 0)
11714         b->line_number = sals.sals[i].line;
11715
11716       if (sals_end.nelts)
11717         {
11718           CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11719
11720           new_loc->length = end - sals.sals[0].pc + 1;
11721         }
11722     }
11723
11724   /* Update locations of permanent breakpoints.  */
11725   if (b->enable_state == bp_permanent)
11726     make_breakpoint_permanent (b);
11727
11728   /* If possible, carry over 'disable' status from existing
11729      breakpoints.  */
11730   {
11731     struct bp_location *e = existing_locations;
11732     /* If there are multiple breakpoints with the same function name,
11733        e.g. for inline functions, comparing function names won't work.
11734        Instead compare pc addresses; this is just a heuristic as things
11735        may have moved, but in practice it gives the correct answer
11736        often enough until a better solution is found.  */
11737     int have_ambiguous_names = ambiguous_names_p (b->loc);
11738
11739     for (; e; e = e->next)
11740       {
11741         if (!e->enabled && e->function_name)
11742           {
11743             struct bp_location *l = b->loc;
11744             if (have_ambiguous_names)
11745               {
11746                 for (; l; l = l->next)
11747                   if (breakpoint_locations_match (e, l))
11748                     {
11749                       l->enabled = 0;
11750                       break;
11751                     }
11752               }
11753             else
11754               {
11755                 for (; l; l = l->next)
11756                   if (l->function_name
11757                       && strcmp (e->function_name, l->function_name) == 0)
11758                     {
11759                       l->enabled = 0;
11760                       break;
11761                     }
11762               }
11763           }
11764       }
11765   }
11766
11767   if (!locations_are_equal (existing_locations, b->loc))
11768     observer_notify_breakpoint_modified (b);
11769
11770   update_global_location_list (1);
11771 }
11772
11773 /* Find the SaL locations corresponding to the given ADDR_STRING.
11774    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
11775
11776 static struct symtabs_and_lines
11777 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11778 {
11779   char *s;
11780   int marker_spec;
11781   struct symtabs_and_lines sals = {0};
11782   struct gdb_exception e;
11783
11784   s = addr_string;
11785   marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11786
11787   TRY_CATCH (e, RETURN_MASK_ERROR)
11788     {
11789       if (marker_spec)
11790         {
11791           struct tracepoint *tp = (struct tracepoint *) b;
11792
11793           sals = decode_static_tracepoint_spec (&s);
11794           if (sals.nelts > tp->static_trace_marker_id_idx)
11795             {
11796               sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
11797               sals.nelts = 1;
11798             }
11799           else
11800             error (_("marker %s not found"), tp->static_trace_marker_id);
11801         }
11802       else
11803         sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
11804     }
11805   if (e.reason < 0)
11806     {
11807       int not_found_and_ok = 0;
11808       /* For pending breakpoints, it's expected that parsing will
11809          fail until the right shared library is loaded.  User has
11810          already told to create pending breakpoints and don't need
11811          extra messages.  If breakpoint is in bp_shlib_disabled
11812          state, then user already saw the message about that
11813          breakpoint being disabled, and don't want to see more
11814          errors.  */
11815       if (e.error == NOT_FOUND_ERROR
11816           && (b->condition_not_parsed 
11817               || (b->loc && b->loc->shlib_disabled)
11818               || b->enable_state == bp_disabled))
11819         not_found_and_ok = 1;
11820
11821       if (!not_found_and_ok)
11822         {
11823           /* We surely don't want to warn about the same breakpoint
11824              10 times.  One solution, implemented here, is disable
11825              the breakpoint on error.  Another solution would be to
11826              have separate 'warning emitted' flag.  Since this
11827              happens only when a binary has changed, I don't know
11828              which approach is better.  */
11829           b->enable_state = bp_disabled;
11830           throw_exception (e);
11831         }
11832     }
11833
11834   if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
11835     {
11836       gdb_assert (sals.nelts == 1);
11837
11838       resolve_sal_pc (&sals.sals[0]);
11839       if (b->condition_not_parsed && s && s[0])
11840         {
11841           char *cond_string = 0;
11842           int thread = -1;
11843           int task = 0;
11844
11845           find_condition_and_thread (s, sals.sals[0].pc,
11846                                      &cond_string, &thread, &task);
11847           if (cond_string)
11848             b->cond_string = cond_string;
11849           b->thread = thread;
11850           b->task = task;
11851           b->condition_not_parsed = 0;
11852         }
11853
11854       if (b->type == bp_static_tracepoint && !marker_spec)
11855         sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
11856
11857       *found = 1;
11858     }
11859   else
11860     *found = 0;
11861
11862   return sals;
11863 }
11864
11865 /* The default re_set method, for typical hardware or software
11866    breakpoints.  Reevaluate the breakpoint and recreate its
11867    locations.  */
11868
11869 static void
11870 breakpoint_re_set_default (struct breakpoint *b)
11871 {
11872   int found;
11873   struct symtabs_and_lines sals, sals_end;
11874   struct symtabs_and_lines expanded = {0};
11875   struct symtabs_and_lines expanded_end = {0};
11876
11877   sals = addr_string_to_sals (b, b->addr_string, &found);
11878   if (found)
11879     {
11880       make_cleanup (xfree, sals.sals);
11881       expanded = expand_line_sal_maybe (sals.sals[0]);
11882     }
11883
11884   if (b->addr_string_range_end)
11885     {
11886       sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11887       if (found)
11888         {
11889           make_cleanup (xfree, sals_end.sals);
11890           expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11891         }
11892     }
11893
11894   update_breakpoint_locations (b, expanded, expanded_end);
11895 }
11896
11897 /* Prepare the global context for a re-set of breakpoint B.  */
11898
11899 static struct cleanup *
11900 prepare_re_set_context (struct breakpoint *b)
11901 {
11902   struct cleanup *cleanups;
11903
11904   input_radix = b->input_radix;
11905   cleanups = save_current_space_and_thread ();
11906   switch_to_program_space_and_thread (b->pspace);
11907   set_language (b->language);
11908
11909   return cleanups;
11910 }
11911
11912 /* Reset a breakpoint given it's struct breakpoint * BINT.
11913    The value we return ends up being the return value from catch_errors.
11914    Unused in this case.  */
11915
11916 static int
11917 breakpoint_re_set_one (void *bint)
11918 {
11919   /* Get past catch_errs.  */
11920   struct breakpoint *b = (struct breakpoint *) bint;
11921   struct cleanup *cleanups;
11922
11923   cleanups = prepare_re_set_context (b);
11924   b->ops->re_set (b);
11925   do_cleanups (cleanups);
11926   return 0;
11927 }
11928
11929 /* Re-set all breakpoints after symbols have been re-loaded.  */
11930 void
11931 breakpoint_re_set (void)
11932 {
11933   struct breakpoint *b, *b_tmp;
11934   enum language save_language;
11935   int save_input_radix;
11936   struct cleanup *old_chain;
11937
11938   save_language = current_language->la_language;
11939   save_input_radix = input_radix;
11940   old_chain = save_current_program_space ();
11941
11942   ALL_BREAKPOINTS_SAFE (b, b_tmp)
11943   {
11944     /* Format possible error msg.  */
11945     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
11946                                 b->number);
11947     struct cleanup *cleanups = make_cleanup (xfree, message);
11948     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
11949     do_cleanups (cleanups);
11950   }
11951   set_language (save_language);
11952   input_radix = save_input_radix;
11953
11954   jit_breakpoint_re_set ();
11955
11956   do_cleanups (old_chain);
11957
11958   create_overlay_event_breakpoint ();
11959   create_longjmp_master_breakpoint ();
11960   create_std_terminate_master_breakpoint ();
11961   create_exception_master_breakpoint ();
11962
11963   /* While we're at it, reset the skip list too.  */
11964   skip_re_set ();
11965 }
11966 \f
11967 /* Reset the thread number of this breakpoint:
11968
11969    - If the breakpoint is for all threads, leave it as-is.
11970    - Else, reset it to the current thread for inferior_ptid.  */
11971 void
11972 breakpoint_re_set_thread (struct breakpoint *b)
11973 {
11974   if (b->thread != -1)
11975     {
11976       if (in_thread_list (inferior_ptid))
11977         b->thread = pid_to_thread_id (inferior_ptid);
11978
11979       /* We're being called after following a fork.  The new fork is
11980          selected as current, and unless this was a vfork will have a
11981          different program space from the original thread.  Reset that
11982          as well.  */
11983       b->loc->pspace = current_program_space;
11984     }
11985 }
11986
11987 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
11988    If from_tty is nonzero, it prints a message to that effect,
11989    which ends with a period (no newline).  */
11990
11991 void
11992 set_ignore_count (int bptnum, int count, int from_tty)
11993 {
11994   struct breakpoint *b;
11995
11996   if (count < 0)
11997     count = 0;
11998
11999   ALL_BREAKPOINTS (b)
12000     if (b->number == bptnum)
12001     {
12002       if (is_tracepoint (b))
12003         {
12004           if (from_tty && count != 0)
12005             printf_filtered (_("Ignore count ignored for tracepoint %d."),
12006                              bptnum);
12007           return;
12008         }
12009       
12010       b->ignore_count = count;
12011       if (from_tty)
12012         {
12013           if (count == 0)
12014             printf_filtered (_("Will stop next time "
12015                                "breakpoint %d is reached."),
12016                              bptnum);
12017           else if (count == 1)
12018             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
12019                              bptnum);
12020           else
12021             printf_filtered (_("Will ignore next %d "
12022                                "crossings of breakpoint %d."),
12023                              count, bptnum);
12024         }
12025       breakpoints_changed ();
12026       observer_notify_breakpoint_modified (b);
12027       return;
12028     }
12029
12030   error (_("No breakpoint number %d."), bptnum);
12031 }
12032
12033 /* Command to set ignore-count of breakpoint N to COUNT.  */
12034
12035 static void
12036 ignore_command (char *args, int from_tty)
12037 {
12038   char *p = args;
12039   int num;
12040
12041   if (p == 0)
12042     error_no_arg (_("a breakpoint number"));
12043
12044   num = get_number (&p);
12045   if (num == 0)
12046     error (_("bad breakpoint number: '%s'"), args);
12047   if (*p == 0)
12048     error (_("Second argument (specified ignore-count) is missing."));
12049
12050   set_ignore_count (num,
12051                     longest_to_int (value_as_long (parse_and_eval (p))),
12052                     from_tty);
12053   if (from_tty)
12054     printf_filtered ("\n");
12055 }
12056 \f
12057 /* Call FUNCTION on each of the breakpoints
12058    whose numbers are given in ARGS.  */
12059
12060 static void
12061 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12062                                                       void *),
12063                         void *data)
12064 {
12065   int num;
12066   struct breakpoint *b, *tmp;
12067   int match;
12068   struct get_number_or_range_state state;
12069
12070   if (args == 0)
12071     error_no_arg (_("one or more breakpoint numbers"));
12072
12073   init_number_or_range (&state, args);
12074
12075   while (!state.finished)
12076     {
12077       char *p = state.string;
12078
12079       match = 0;
12080
12081       num = get_number_or_range (&state);
12082       if (num == 0)
12083         {
12084           warning (_("bad breakpoint number at or near '%s'"), p);
12085         }
12086       else
12087         {
12088           ALL_BREAKPOINTS_SAFE (b, tmp)
12089             if (b->number == num)
12090               {
12091                 match = 1;
12092                 function (b, data);
12093                 break;
12094               }
12095           if (match == 0)
12096             printf_unfiltered (_("No breakpoint number %d.\n"), num);
12097         }
12098     }
12099 }
12100
12101 static struct bp_location *
12102 find_location_by_number (char *number)
12103 {
12104   char *dot = strchr (number, '.');
12105   char *p1;
12106   int bp_num;
12107   int loc_num;
12108   struct breakpoint *b;
12109   struct bp_location *loc;  
12110
12111   *dot = '\0';
12112
12113   p1 = number;
12114   bp_num = get_number (&p1);
12115   if (bp_num == 0)
12116     error (_("Bad breakpoint number '%s'"), number);
12117
12118   ALL_BREAKPOINTS (b)
12119     if (b->number == bp_num)
12120       {
12121         break;
12122       }
12123
12124   if (!b || b->number != bp_num)
12125     error (_("Bad breakpoint number '%s'"), number);
12126   
12127   p1 = dot+1;
12128   loc_num = get_number (&p1);
12129   if (loc_num == 0)
12130     error (_("Bad breakpoint location number '%s'"), number);
12131
12132   --loc_num;
12133   loc = b->loc;
12134   for (;loc_num && loc; --loc_num, loc = loc->next)
12135     ;
12136   if (!loc)
12137     error (_("Bad breakpoint location number '%s'"), dot+1);
12138     
12139   return loc;  
12140 }
12141
12142
12143 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
12144    If from_tty is nonzero, it prints a message to that effect,
12145    which ends with a period (no newline).  */
12146
12147 void
12148 disable_breakpoint (struct breakpoint *bpt)
12149 {
12150   /* Never disable a watchpoint scope breakpoint; we want to
12151      hit them when we leave scope so we can delete both the
12152      watchpoint and its scope breakpoint at that time.  */
12153   if (bpt->type == bp_watchpoint_scope)
12154     return;
12155
12156   /* You can't disable permanent breakpoints.  */
12157   if (bpt->enable_state == bp_permanent)
12158     return;
12159
12160   bpt->enable_state = bp_disabled;
12161
12162   if (target_supports_enable_disable_tracepoint ()
12163       && current_trace_status ()->running && is_tracepoint (bpt))
12164     {
12165       struct bp_location *location;
12166      
12167       for (location = bpt->loc; location; location = location->next)
12168         target_disable_tracepoint (location);
12169     }
12170
12171   update_global_location_list (0);
12172
12173   observer_notify_breakpoint_modified (bpt);
12174 }
12175
12176 /* A callback for iterate_over_related_breakpoints.  */
12177
12178 static void
12179 do_disable_breakpoint (struct breakpoint *b, void *ignore)
12180 {
12181   disable_breakpoint (b);
12182 }
12183
12184 /* A callback for map_breakpoint_numbers that calls
12185    disable_breakpoint.  */
12186
12187 static void
12188 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12189 {
12190   iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
12191 }
12192
12193 static void
12194 disable_command (char *args, int from_tty)
12195 {
12196   if (args == 0)
12197     {
12198       struct breakpoint *bpt;
12199
12200       ALL_BREAKPOINTS (bpt)
12201         if (user_breakpoint_p (bpt))
12202           disable_breakpoint (bpt);
12203     }
12204   else if (strchr (args, '.'))
12205     {
12206       struct bp_location *loc = find_location_by_number (args);
12207       if (loc)
12208         {
12209           loc->enabled = 0;
12210           if (target_supports_enable_disable_tracepoint ()
12211               && current_trace_status ()->running && loc->owner
12212               && is_tracepoint (loc->owner))
12213             target_disable_tracepoint (loc);
12214         }
12215       update_global_location_list (0);
12216     }
12217   else
12218     map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
12219 }
12220
12221 static void
12222 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
12223 {
12224   int target_resources_ok;
12225
12226   if (bpt->type == bp_hardware_breakpoint)
12227     {
12228       int i;
12229       i = hw_breakpoint_used_count ();
12230       target_resources_ok = 
12231         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
12232                                             i + 1, 0);
12233       if (target_resources_ok == 0)
12234         error (_("No hardware breakpoint support in the target."));
12235       else if (target_resources_ok < 0)
12236         error (_("Hardware breakpoints used exceeds limit."));
12237     }
12238
12239   if (is_watchpoint (bpt))
12240     {
12241       /* Initialize it just to avoid a GCC false warning.  */
12242       enum enable_state orig_enable_state = 0;
12243       struct gdb_exception e;
12244
12245       TRY_CATCH (e, RETURN_MASK_ALL)
12246         {
12247           struct watchpoint *w = (struct watchpoint *) bpt;
12248
12249           orig_enable_state = bpt->enable_state;
12250           bpt->enable_state = bp_enabled;
12251           update_watchpoint (w, 1 /* reparse */);
12252         }
12253       if (e.reason < 0)
12254         {
12255           bpt->enable_state = orig_enable_state;
12256           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12257                              bpt->number);
12258           return;
12259         }
12260     }
12261
12262   if (bpt->enable_state != bp_permanent)
12263     bpt->enable_state = bp_enabled;
12264
12265   if (target_supports_enable_disable_tracepoint ()
12266       && current_trace_status ()->running && is_tracepoint (bpt))
12267     {
12268       struct bp_location *location;
12269
12270       for (location = bpt->loc; location; location = location->next)
12271         target_enable_tracepoint (location);
12272     }
12273
12274   bpt->disposition = disposition;
12275   update_global_location_list (1);
12276   breakpoints_changed ();
12277   
12278   observer_notify_breakpoint_modified (bpt);
12279 }
12280
12281
12282 void
12283 enable_breakpoint (struct breakpoint *bpt)
12284 {
12285   enable_breakpoint_disp (bpt, bpt->disposition);
12286 }
12287
12288 static void
12289 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12290 {
12291   enable_breakpoint (bpt);
12292 }
12293
12294 /* A callback for map_breakpoint_numbers that calls
12295    enable_breakpoint.  */
12296
12297 static void
12298 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12299 {
12300   iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
12301 }
12302
12303 /* The enable command enables the specified breakpoints (or all defined
12304    breakpoints) so they once again become (or continue to be) effective
12305    in stopping the inferior.  */
12306
12307 static void
12308 enable_command (char *args, int from_tty)
12309 {
12310   if (args == 0)
12311     {
12312       struct breakpoint *bpt;
12313
12314       ALL_BREAKPOINTS (bpt)
12315         if (user_breakpoint_p (bpt))
12316           enable_breakpoint (bpt);
12317     }
12318   else if (strchr (args, '.'))
12319     {
12320       struct bp_location *loc = find_location_by_number (args);
12321       if (loc)
12322         {
12323           loc->enabled = 1;
12324           if (target_supports_enable_disable_tracepoint ()
12325               && current_trace_status ()->running && loc->owner
12326               && is_tracepoint (loc->owner))
12327             target_enable_tracepoint (loc);
12328         }
12329       update_global_location_list (1);
12330     }
12331   else
12332     map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
12333 }
12334
12335 static void
12336 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12337 {
12338   enum bpdisp disp = *(enum bpdisp *) arg;
12339
12340   enable_breakpoint_disp (bpt, disp);
12341 }
12342
12343 static void
12344 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
12345 {
12346   enum bpdisp disp = disp_disable;
12347
12348   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
12349 }
12350
12351 static void
12352 enable_once_command (char *args, int from_tty)
12353 {
12354   map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
12355 }
12356
12357 static void
12358 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
12359 {
12360   enum bpdisp disp = disp_del;
12361
12362   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
12363 }
12364
12365 static void
12366 enable_delete_command (char *args, int from_tty)
12367 {
12368   map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
12369 }
12370 \f
12371 static void
12372 set_breakpoint_cmd (char *args, int from_tty)
12373 {
12374 }
12375
12376 static void
12377 show_breakpoint_cmd (char *args, int from_tty)
12378 {
12379 }
12380
12381 /* Invalidate last known value of any hardware watchpoint if
12382    the memory which that value represents has been written to by
12383    GDB itself.  */
12384
12385 static void
12386 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12387                                       const bfd_byte *data)
12388 {
12389   struct breakpoint *bp;
12390
12391   ALL_BREAKPOINTS (bp)
12392     if (bp->enable_state == bp_enabled
12393         && bp->type == bp_hardware_watchpoint)
12394       {
12395         struct watchpoint *wp = (struct watchpoint *) bp;
12396
12397         if (wp->val_valid && wp->val)
12398           {
12399             struct bp_location *loc;
12400
12401             for (loc = bp->loc; loc != NULL; loc = loc->next)
12402               if (loc->loc_type == bp_loc_hardware_watchpoint
12403                   && loc->address + loc->length > addr
12404                   && addr + len > loc->address)
12405                 {
12406                   value_free (wp->val);
12407                   wp->val = NULL;
12408                   wp->val_valid = 0;
12409                 }
12410           }
12411       }
12412 }
12413
12414 /* Use the last displayed codepoint's values, or nothing
12415    if they aren't valid.  */
12416
12417 struct symtabs_and_lines
12418 decode_line_spec_1 (char *string, int funfirstline)
12419 {
12420   struct symtabs_and_lines sals;
12421
12422   if (string == 0)
12423     error (_("Empty line specification."));
12424   if (last_displayed_sal_is_valid ())
12425     sals = decode_line_1 (&string, funfirstline,
12426                           get_last_displayed_symtab (),
12427                           get_last_displayed_line (),
12428                           NULL);
12429   else
12430     sals = decode_line_1 (&string, funfirstline,
12431                           (struct symtab *) NULL, 0, NULL);
12432   if (*string)
12433     error (_("Junk at end of line specification: %s"), string);
12434   return sals;
12435 }
12436
12437 /* Create and insert a raw software breakpoint at PC.  Return an
12438    identifier, which should be used to remove the breakpoint later.
12439    In general, places which call this should be using something on the
12440    breakpoint chain instead; this function should be eliminated
12441    someday.  */
12442
12443 void *
12444 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12445                                   struct address_space *aspace, CORE_ADDR pc)
12446 {
12447   struct bp_target_info *bp_tgt;
12448
12449   bp_tgt = XZALLOC (struct bp_target_info);
12450
12451   bp_tgt->placed_address_space = aspace;
12452   bp_tgt->placed_address = pc;
12453
12454   if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
12455     {
12456       /* Could not insert the breakpoint.  */
12457       xfree (bp_tgt);
12458       return NULL;
12459     }
12460
12461   return bp_tgt;
12462 }
12463
12464 /* Remove a breakpoint BP inserted by
12465    deprecated_insert_raw_breakpoint.  */
12466
12467 int
12468 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
12469 {
12470   struct bp_target_info *bp_tgt = bp;
12471   int ret;
12472
12473   ret = target_remove_breakpoint (gdbarch, bp_tgt);
12474   xfree (bp_tgt);
12475
12476   return ret;
12477 }
12478
12479 /* One (or perhaps two) breakpoints used for software single
12480    stepping.  */
12481
12482 static void *single_step_breakpoints[2];
12483 static struct gdbarch *single_step_gdbarch[2];
12484
12485 /* Create and insert a breakpoint for software single step.  */
12486
12487 void
12488 insert_single_step_breakpoint (struct gdbarch *gdbarch,
12489                                struct address_space *aspace, 
12490                                CORE_ADDR next_pc)
12491 {
12492   void **bpt_p;
12493
12494   if (single_step_breakpoints[0] == NULL)
12495     {
12496       bpt_p = &single_step_breakpoints[0];
12497       single_step_gdbarch[0] = gdbarch;
12498     }
12499   else
12500     {
12501       gdb_assert (single_step_breakpoints[1] == NULL);
12502       bpt_p = &single_step_breakpoints[1];
12503       single_step_gdbarch[1] = gdbarch;
12504     }
12505
12506   /* NOTE drow/2006-04-11: A future improvement to this function would
12507      be to only create the breakpoints once, and actually put them on
12508      the breakpoint chain.  That would let us use set_raw_breakpoint.
12509      We could adjust the addresses each time they were needed.  Doing
12510      this requires corresponding changes elsewhere where single step
12511      breakpoints are handled, however.  So, for now, we use this.  */
12512
12513   *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
12514   if (*bpt_p == NULL)
12515     error (_("Could not insert single-step breakpoint at %s"),
12516              paddress (gdbarch, next_pc));
12517 }
12518
12519 /* Check if the breakpoints used for software single stepping
12520    were inserted or not.  */
12521
12522 int
12523 single_step_breakpoints_inserted (void)
12524 {
12525   return (single_step_breakpoints[0] != NULL
12526           || single_step_breakpoints[1] != NULL);
12527 }
12528
12529 /* Remove and delete any breakpoints used for software single step.  */
12530
12531 void
12532 remove_single_step_breakpoints (void)
12533 {
12534   gdb_assert (single_step_breakpoints[0] != NULL);
12535
12536   /* See insert_single_step_breakpoint for more about this deprecated
12537      call.  */
12538   deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12539                                     single_step_breakpoints[0]);
12540   single_step_gdbarch[0] = NULL;
12541   single_step_breakpoints[0] = NULL;
12542
12543   if (single_step_breakpoints[1] != NULL)
12544     {
12545       deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12546                                         single_step_breakpoints[1]);
12547       single_step_gdbarch[1] = NULL;
12548       single_step_breakpoints[1] = NULL;
12549     }
12550 }
12551
12552 /* Delete software single step breakpoints without removing them from
12553    the inferior.  This is intended to be used if the inferior's address
12554    space where they were inserted is already gone, e.g. after exit or
12555    exec.  */
12556
12557 void
12558 cancel_single_step_breakpoints (void)
12559 {
12560   int i;
12561
12562   for (i = 0; i < 2; i++)
12563     if (single_step_breakpoints[i])
12564       {
12565         xfree (single_step_breakpoints[i]);
12566         single_step_breakpoints[i] = NULL;
12567         single_step_gdbarch[i] = NULL;
12568       }
12569 }
12570
12571 /* Detach software single-step breakpoints from INFERIOR_PTID without
12572    removing them.  */
12573
12574 static void
12575 detach_single_step_breakpoints (void)
12576 {
12577   int i;
12578
12579   for (i = 0; i < 2; i++)
12580     if (single_step_breakpoints[i])
12581       target_remove_breakpoint (single_step_gdbarch[i],
12582                                 single_step_breakpoints[i]);
12583 }
12584
12585 /* Check whether a software single-step breakpoint is inserted at
12586    PC.  */
12587
12588 static int
12589 single_step_breakpoint_inserted_here_p (struct address_space *aspace, 
12590                                         CORE_ADDR pc)
12591 {
12592   int i;
12593
12594   for (i = 0; i < 2; i++)
12595     {
12596       struct bp_target_info *bp_tgt = single_step_breakpoints[i];
12597       if (bp_tgt
12598           && breakpoint_address_match (bp_tgt->placed_address_space,
12599                                        bp_tgt->placed_address,
12600                                        aspace, pc))
12601         return 1;
12602     }
12603
12604   return 0;
12605 }
12606
12607 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
12608    non-zero otherwise.  */
12609 static int
12610 is_syscall_catchpoint_enabled (struct breakpoint *bp)
12611 {
12612   if (syscall_catchpoint_p (bp)
12613       && bp->enable_state != bp_disabled
12614       && bp->enable_state != bp_call_disabled)
12615     return 1;
12616   else
12617     return 0;
12618 }
12619
12620 int
12621 catch_syscall_enabled (void)
12622 {
12623   struct inferior *inf = current_inferior ();
12624
12625   return inf->total_syscalls_count != 0;
12626 }
12627
12628 int
12629 catching_syscall_number (int syscall_number)
12630 {
12631   struct breakpoint *bp;
12632
12633   ALL_BREAKPOINTS (bp)
12634     if (is_syscall_catchpoint_enabled (bp))
12635       {
12636         struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12637
12638         if (c->syscalls_to_be_caught)
12639           {
12640             int i, iter;
12641             for (i = 0;
12642                  VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
12643                  i++)
12644               if (syscall_number == iter)
12645                 return 1;
12646           }
12647         else
12648           return 1;
12649       }
12650
12651   return 0;
12652 }
12653
12654 /* Complete syscall names.  Used by "catch syscall".  */
12655 static char **
12656 catch_syscall_completer (struct cmd_list_element *cmd,
12657                          char *text, char *word)
12658 {
12659   const char **list = get_syscall_names ();
12660   char **retlist
12661     = (list == NULL) ? NULL : complete_on_enum (list, text, word);
12662
12663   xfree (list);
12664   return retlist;
12665 }
12666
12667 /* Tracepoint-specific operations.  */
12668
12669 /* Set tracepoint count to NUM.  */
12670 static void
12671 set_tracepoint_count (int num)
12672 {
12673   tracepoint_count = num;
12674   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
12675 }
12676
12677 void
12678 trace_command (char *arg, int from_tty)
12679 {
12680   if (create_breakpoint (get_current_arch (),
12681                          arg,
12682                          NULL, 0, 1 /* parse arg */,
12683                          0 /* tempflag */,
12684                          bp_tracepoint /* type_wanted */,
12685                          0 /* Ignore count */,
12686                          pending_break_support,
12687                          &tracepoint_breakpoint_ops,
12688                          from_tty,
12689                          1 /* enabled */,
12690                          0 /* internal */))
12691     set_tracepoint_count (breakpoint_count);
12692 }
12693
12694 void
12695 ftrace_command (char *arg, int from_tty)
12696 {
12697   if (create_breakpoint (get_current_arch (),
12698                          arg,
12699                          NULL, 0, 1 /* parse arg */,
12700                          0 /* tempflag */,
12701                          bp_fast_tracepoint /* type_wanted */,
12702                          0 /* Ignore count */,
12703                          pending_break_support,
12704                          &tracepoint_breakpoint_ops,
12705                          from_tty,
12706                          1 /* enabled */,
12707                          0 /* internal */))
12708     set_tracepoint_count (breakpoint_count);
12709 }
12710
12711 /* strace command implementation.  Creates a static tracepoint.  */
12712
12713 void
12714 strace_command (char *arg, int from_tty)
12715 {
12716   if (create_breakpoint (get_current_arch (),
12717                          arg,
12718                          NULL, 0, 1 /* parse arg */,
12719                          0 /* tempflag */,
12720                          bp_static_tracepoint /* type_wanted */,
12721                          0 /* Ignore count */,
12722                          pending_break_support,
12723                          &tracepoint_breakpoint_ops,
12724                          from_tty,
12725                          1 /* enabled */,
12726                          0 /* internal */))
12727     set_tracepoint_count (breakpoint_count);
12728 }
12729
12730 /* Set up a fake reader function that gets command lines from a linked
12731    list that was acquired during tracepoint uploading.  */
12732
12733 static struct uploaded_tp *this_utp;
12734 static int next_cmd;
12735
12736 static char *
12737 read_uploaded_action (void)
12738 {
12739   char *rslt;
12740
12741   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
12742
12743   next_cmd++;
12744
12745   return rslt;
12746 }
12747
12748 /* Given information about a tracepoint as recorded on a target (which
12749    can be either a live system or a trace file), attempt to create an
12750    equivalent GDB tracepoint.  This is not a reliable process, since
12751    the target does not necessarily have all the information used when
12752    the tracepoint was originally defined.  */
12753   
12754 struct tracepoint *
12755 create_tracepoint_from_upload (struct uploaded_tp *utp)
12756 {
12757   char *addr_str, small_buf[100];
12758   struct tracepoint *tp;
12759
12760   if (utp->at_string)
12761     addr_str = utp->at_string;
12762   else
12763     {
12764       /* In the absence of a source location, fall back to raw
12765          address.  Since there is no way to confirm that the address
12766          means the same thing as when the trace was started, warn the
12767          user.  */
12768       warning (_("Uploaded tracepoint %d has no "
12769                  "source location, using raw address"),
12770                utp->number);
12771       sprintf (small_buf, "*%s", hex_string (utp->addr));
12772       addr_str = small_buf;
12773     }
12774
12775   /* There's not much we can do with a sequence of bytecodes.  */
12776   if (utp->cond && !utp->cond_string)
12777     warning (_("Uploaded tracepoint %d condition "
12778                "has no source form, ignoring it"),
12779              utp->number);
12780
12781   if (!create_breakpoint (get_current_arch (),
12782                           addr_str,
12783                           utp->cond_string, -1, 0 /* parse cond/thread */,
12784                           0 /* tempflag */,
12785                           utp->type /* type_wanted */,
12786                           0 /* Ignore count */,
12787                           pending_break_support,
12788                           &tracepoint_breakpoint_ops,
12789                           0 /* from_tty */,
12790                           utp->enabled /* enabled */,
12791                           0 /* internal */))
12792     return NULL;
12793
12794   set_tracepoint_count (breakpoint_count);
12795   
12796   /* Get the tracepoint we just created.  */
12797   tp = get_tracepoint (tracepoint_count);
12798   gdb_assert (tp != NULL);
12799
12800   if (utp->pass > 0)
12801     {
12802       sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
12803
12804       trace_pass_command (small_buf, 0);
12805     }
12806
12807   /* If we have uploaded versions of the original commands, set up a
12808      special-purpose "reader" function and call the usual command line
12809      reader, then pass the result to the breakpoint command-setting
12810      function.  */
12811   if (!VEC_empty (char_ptr, utp->cmd_strings))
12812     {
12813       struct command_line *cmd_list;
12814
12815       this_utp = utp;
12816       next_cmd = 0;
12817
12818       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12819
12820       breakpoint_set_commands (&tp->base, cmd_list);
12821     }
12822   else if (!VEC_empty (char_ptr, utp->actions)
12823            || !VEC_empty (char_ptr, utp->step_actions))
12824     warning (_("Uploaded tracepoint %d actions "
12825                "have no source form, ignoring them"),
12826              utp->number);
12827
12828   return tp;
12829 }
12830   
12831 /* Print information on tracepoint number TPNUM_EXP, or all if
12832    omitted.  */
12833
12834 static void
12835 tracepoints_info (char *args, int from_tty)
12836 {
12837   struct ui_out *uiout = current_uiout;
12838   int num_printed;
12839
12840   num_printed = breakpoint_1 (args, 0, is_tracepoint);
12841
12842   if (num_printed == 0)
12843     {
12844       if (args == NULL || *args == '\0')
12845         ui_out_message (uiout, 0, "No tracepoints.\n");
12846       else
12847         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
12848     }
12849
12850   default_collect_info ();
12851 }
12852
12853 /* The 'enable trace' command enables tracepoints.
12854    Not supported by all targets.  */
12855 static void
12856 enable_trace_command (char *args, int from_tty)
12857 {
12858   enable_command (args, from_tty);
12859 }
12860
12861 /* The 'disable trace' command disables tracepoints.
12862    Not supported by all targets.  */
12863 static void
12864 disable_trace_command (char *args, int from_tty)
12865 {
12866   disable_command (args, from_tty);
12867 }
12868
12869 /* Remove a tracepoint (or all if no argument).  */
12870 static void
12871 delete_trace_command (char *arg, int from_tty)
12872 {
12873   struct breakpoint *b, *b_tmp;
12874
12875   dont_repeat ();
12876
12877   if (arg == 0)
12878     {
12879       int breaks_to_delete = 0;
12880
12881       /* Delete all breakpoints if no argument.
12882          Do not delete internal or call-dummy breakpoints, these
12883          have to be deleted with an explicit breakpoint number 
12884          argument.  */
12885       ALL_TRACEPOINTS (b)
12886         if (is_tracepoint (b) && user_breakpoint_p (b))
12887           {
12888             breaks_to_delete = 1;
12889             break;
12890           }
12891
12892       /* Ask user only if there are some breakpoints to delete.  */
12893       if (!from_tty
12894           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12895         {
12896           ALL_BREAKPOINTS_SAFE (b, b_tmp)
12897             if (is_tracepoint (b) && user_breakpoint_p (b))
12898               delete_breakpoint (b);
12899         }
12900     }
12901   else
12902     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
12903 }
12904
12905 /* Helper function for trace_pass_command.  */
12906
12907 static void
12908 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
12909 {
12910   tp->pass_count = count;
12911   observer_notify_tracepoint_modified (tp->base.number);
12912   if (from_tty)
12913     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
12914                      tp->base.number, count);
12915 }
12916
12917 /* Set passcount for tracepoint.
12918
12919    First command argument is passcount, second is tracepoint number.
12920    If tracepoint number omitted, apply to most recently defined.
12921    Also accepts special argument "all".  */
12922
12923 static void
12924 trace_pass_command (char *args, int from_tty)
12925 {
12926   struct tracepoint *t1;
12927   unsigned int count;
12928
12929   if (args == 0 || *args == 0)
12930     error (_("passcount command requires an "
12931              "argument (count + optional TP num)"));
12932
12933   count = strtoul (args, &args, 10);    /* Count comes first, then TP num.  */
12934
12935   while (*args && isspace ((int) *args))
12936     args++;
12937
12938   if (*args && strncasecmp (args, "all", 3) == 0)
12939     {
12940       struct breakpoint *b;
12941
12942       args += 3;                        /* Skip special argument "all".  */
12943       if (*args)
12944         error (_("Junk at end of arguments."));
12945
12946       ALL_TRACEPOINTS (b)
12947       {
12948         t1 = (struct tracepoint *) b;
12949         trace_pass_set_count (t1, count, from_tty);
12950       }
12951     }
12952   else if (*args == '\0')
12953     {
12954       t1 = get_tracepoint_by_number (&args, NULL, 1);
12955       if (t1)
12956         trace_pass_set_count (t1, count, from_tty);
12957     }
12958   else
12959     {
12960       struct get_number_or_range_state state;
12961
12962       init_number_or_range (&state, args);
12963       while (!state.finished)
12964         {
12965           t1 = get_tracepoint_by_number (&args, &state, 1);
12966           if (t1)
12967             trace_pass_set_count (t1, count, from_tty);
12968         }
12969     }
12970 }
12971
12972 struct tracepoint *
12973 get_tracepoint (int num)
12974 {
12975   struct breakpoint *t;
12976
12977   ALL_TRACEPOINTS (t)
12978     if (t->number == num)
12979       return (struct tracepoint *) t;
12980
12981   return NULL;
12982 }
12983
12984 /* Find the tracepoint with the given target-side number (which may be
12985    different from the tracepoint number after disconnecting and
12986    reconnecting).  */
12987
12988 struct tracepoint *
12989 get_tracepoint_by_number_on_target (int num)
12990 {
12991   struct breakpoint *b;
12992
12993   ALL_TRACEPOINTS (b)
12994     {
12995       struct tracepoint *t = (struct tracepoint *) b;
12996
12997       if (t->number_on_target == num)
12998         return t;
12999     }
13000
13001   return NULL;
13002 }
13003
13004 /* Utility: parse a tracepoint number and look it up in the list.
13005    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13006    If OPTIONAL_P is true, then if the argument is missing, the most
13007    recent tracepoint (tracepoint_count) is returned.  */
13008 struct tracepoint *
13009 get_tracepoint_by_number (char **arg,
13010                           struct get_number_or_range_state *state,
13011                           int optional_p)
13012 {
13013   extern int tracepoint_count;
13014   struct breakpoint *t;
13015   int tpnum;
13016   char *instring = arg == NULL ? NULL : *arg;
13017
13018   if (state)
13019     {
13020       gdb_assert (!state->finished);
13021       tpnum = get_number_or_range (state);
13022     }
13023   else if (arg == NULL || *arg == NULL || ! **arg)
13024     {
13025       if (optional_p)
13026         tpnum = tracepoint_count;
13027       else
13028         error_no_arg (_("tracepoint number"));
13029     }
13030   else
13031     tpnum = get_number (arg);
13032
13033   if (tpnum <= 0)
13034     {
13035       if (instring && *instring)
13036         printf_filtered (_("bad tracepoint number at or near '%s'\n"), 
13037                          instring);
13038       else
13039         printf_filtered (_("Tracepoint argument missing "
13040                            "and no previous tracepoint\n"));
13041       return NULL;
13042     }
13043
13044   ALL_TRACEPOINTS (t)
13045     if (t->number == tpnum)
13046     {
13047       return (struct tracepoint *) t;
13048     }
13049
13050   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13051   return NULL;
13052 }
13053
13054 void
13055 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13056 {
13057   if (b->thread != -1)
13058     fprintf_unfiltered (fp, " thread %d", b->thread);
13059
13060   if (b->task != 0)
13061     fprintf_unfiltered (fp, " task %d", b->task);
13062
13063   fprintf_unfiltered (fp, "\n");
13064 }
13065
13066 /* Save information on user settable breakpoints (watchpoints, etc) to
13067    a new script file named FILENAME.  If FILTER is non-NULL, call it
13068    on each breakpoint and only include the ones for which it returns
13069    non-zero.  */
13070
13071 static void
13072 save_breakpoints (char *filename, int from_tty,
13073                   int (*filter) (const struct breakpoint *))
13074 {
13075   struct breakpoint *tp;
13076   int any = 0;
13077   char *pathname;
13078   struct cleanup *cleanup;
13079   struct ui_file *fp;
13080   int extra_trace_bits = 0;
13081
13082   if (filename == 0 || *filename == 0)
13083     error (_("Argument required (file name in which to save)"));
13084
13085   /* See if we have anything to save.  */
13086   ALL_BREAKPOINTS (tp)
13087   {
13088     /* Skip internal and momentary breakpoints.  */
13089     if (!user_breakpoint_p (tp))
13090       continue;
13091
13092     /* If we have a filter, only save the breakpoints it accepts.  */
13093     if (filter && !filter (tp))
13094       continue;
13095
13096     any = 1;
13097
13098     if (is_tracepoint (tp))
13099       {
13100         extra_trace_bits = 1;
13101
13102         /* We can stop searching.  */
13103         break;
13104       }
13105   }
13106
13107   if (!any)
13108     {
13109       warning (_("Nothing to save."));
13110       return;
13111     }
13112
13113   pathname = tilde_expand (filename);
13114   cleanup = make_cleanup (xfree, pathname);
13115   fp = gdb_fopen (pathname, "w");
13116   if (!fp)
13117     error (_("Unable to open file '%s' for saving (%s)"),
13118            filename, safe_strerror (errno));
13119   make_cleanup_ui_file_delete (fp);
13120
13121   if (extra_trace_bits)
13122     save_trace_state_variables (fp);
13123
13124   ALL_BREAKPOINTS (tp)
13125   {
13126     /* Skip internal and momentary breakpoints.  */
13127     if (!user_breakpoint_p (tp))
13128       continue;
13129
13130     /* If we have a filter, only save the breakpoints it accepts.  */
13131     if (filter && !filter (tp))
13132       continue;
13133
13134     tp->ops->print_recreate (tp, fp);
13135
13136     /* Note, we can't rely on tp->number for anything, as we can't
13137        assume the recreated breakpoint numbers will match.  Use $bpnum
13138        instead.  */
13139
13140     if (tp->cond_string)
13141       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
13142
13143     if (tp->ignore_count)
13144       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
13145
13146     if (tp->commands)
13147       {
13148         volatile struct gdb_exception ex;       
13149
13150         fprintf_unfiltered (fp, "  commands\n");
13151         
13152         ui_out_redirect (current_uiout, fp);
13153         TRY_CATCH (ex, RETURN_MASK_ALL)
13154           {
13155             print_command_lines (current_uiout, tp->commands->commands, 2);
13156           }
13157         ui_out_redirect (current_uiout, NULL);
13158
13159         if (ex.reason < 0)
13160           throw_exception (ex);
13161
13162         fprintf_unfiltered (fp, "  end\n");
13163       }
13164
13165     if (tp->enable_state == bp_disabled)
13166       fprintf_unfiltered (fp, "disable\n");
13167
13168     /* If this is a multi-location breakpoint, check if the locations
13169        should be individually disabled.  Watchpoint locations are
13170        special, and not user visible.  */
13171     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13172       {
13173         struct bp_location *loc;
13174         int n = 1;
13175
13176         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13177           if (!loc->enabled)
13178             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13179       }
13180   }
13181
13182   if (extra_trace_bits && *default_collect)
13183     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13184
13185   do_cleanups (cleanup);
13186   if (from_tty)
13187     printf_filtered (_("Saved to file '%s'.\n"), filename);
13188 }
13189
13190 /* The `save breakpoints' command.  */
13191
13192 static void
13193 save_breakpoints_command (char *args, int from_tty)
13194 {
13195   save_breakpoints (args, from_tty, NULL);
13196 }
13197
13198 /* The `save tracepoints' command.  */
13199
13200 static void
13201 save_tracepoints_command (char *args, int from_tty)
13202 {
13203   save_breakpoints (args, from_tty, is_tracepoint);
13204 }
13205
13206 /* Create a vector of all tracepoints.  */
13207
13208 VEC(breakpoint_p) *
13209 all_tracepoints (void)
13210 {
13211   VEC(breakpoint_p) *tp_vec = 0;
13212   struct breakpoint *tp;
13213
13214   ALL_TRACEPOINTS (tp)
13215   {
13216     VEC_safe_push (breakpoint_p, tp_vec, tp);
13217   }
13218
13219   return tp_vec;
13220 }
13221
13222 \f
13223 /* This help string is used for the break, hbreak, tbreak and thbreak
13224    commands.  It is defined as a macro to prevent duplication.
13225    COMMAND should be a string constant containing the name of the
13226    command.  */
13227 #define BREAK_ARGS_HELP(command) \
13228 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13229 LOCATION may be a line number, function name, or \"*\" and an address.\n\
13230 If a line number is specified, break at start of code for that line.\n\
13231 If a function is specified, break at start of code for that function.\n\
13232 If an address is specified, break at that exact address.\n\
13233 With no LOCATION, uses current execution address of the selected\n\
13234 stack frame.  This is useful for breaking on return to a stack frame.\n\
13235 \n\
13236 THREADNUM is the number from \"info threads\".\n\
13237 CONDITION is a boolean expression.\n\
13238 \n\
13239 Multiple breakpoints at one place are permitted, and useful if their\n\
13240 conditions are different.\n\
13241 \n\
13242 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13243
13244 /* List of subcommands for "catch".  */
13245 static struct cmd_list_element *catch_cmdlist;
13246
13247 /* List of subcommands for "tcatch".  */
13248 static struct cmd_list_element *tcatch_cmdlist;
13249
13250 void
13251 add_catch_command (char *name, char *docstring,
13252                    void (*sfunc) (char *args, int from_tty,
13253                                   struct cmd_list_element *command),
13254                    char **(*completer) (struct cmd_list_element *cmd,
13255                                          char *text, char *word),
13256                    void *user_data_catch,
13257                    void *user_data_tcatch)
13258 {
13259   struct cmd_list_element *command;
13260
13261   command = add_cmd (name, class_breakpoint, NULL, docstring,
13262                      &catch_cmdlist);
13263   set_cmd_sfunc (command, sfunc);
13264   set_cmd_context (command, user_data_catch);
13265   set_cmd_completer (command, completer);
13266
13267   command = add_cmd (name, class_breakpoint, NULL, docstring,
13268                      &tcatch_cmdlist);
13269   set_cmd_sfunc (command, sfunc);
13270   set_cmd_context (command, user_data_tcatch);
13271   set_cmd_completer (command, completer);
13272 }
13273
13274 static void
13275 clear_syscall_counts (struct inferior *inf)
13276 {
13277   inf->total_syscalls_count = 0;
13278   inf->any_syscall_count = 0;
13279   VEC_free (int, inf->syscalls_counts);
13280 }
13281
13282 static void
13283 save_command (char *arg, int from_tty)
13284 {
13285   printf_unfiltered (_("\"save\" must be followed by "
13286                        "the name of a save subcommand.\n"));
13287   help_list (save_cmdlist, "save ", -1, gdb_stdout);
13288 }
13289
13290 struct breakpoint *
13291 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13292                           void *data)
13293 {
13294   struct breakpoint *b, *b_tmp;
13295
13296   ALL_BREAKPOINTS_SAFE (b, b_tmp)
13297     {
13298       if ((*callback) (b, data))
13299         return b;
13300     }
13301
13302   return NULL;
13303 }
13304
13305 /* Zero if any of the breakpoint's locations could be a location where
13306    functions have been inlined, nonzero otherwise.  */
13307
13308 static int
13309 is_non_inline_function (struct breakpoint *b)
13310 {
13311   /* The shared library event breakpoint is set on the address of a
13312      non-inline function.  */
13313   if (b->type == bp_shlib_event)
13314     return 1;
13315
13316   return 0;
13317 }
13318
13319 /* Nonzero if the specified PC cannot be a location where functions
13320    have been inlined.  */
13321
13322 int
13323 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc)
13324 {
13325   struct breakpoint *b;
13326   struct bp_location *bl;
13327
13328   ALL_BREAKPOINTS (b)
13329     {
13330       if (!is_non_inline_function (b))
13331         continue;
13332
13333       for (bl = b->loc; bl != NULL; bl = bl->next)
13334         {
13335           if (!bl->shlib_disabled
13336               && bpstat_check_location (bl, aspace, pc))
13337             return 1;
13338         }
13339     }
13340
13341   return 0;
13342 }
13343
13344 void
13345 initialize_breakpoint_ops (void)
13346 {
13347   static int initialized = 0;
13348
13349   struct breakpoint_ops *ops;
13350
13351   if (initialized)
13352     return;
13353   initialized = 1;
13354
13355   /* The breakpoint_ops structure to be inherit by all kinds of
13356      breakpoints (real breakpoints, i.e., user "break" breakpoints,
13357      internal and momentary breakpoints, etc.).  */
13358   ops = &bkpt_base_breakpoint_ops;
13359   *ops = base_breakpoint_ops;
13360   ops->re_set = bkpt_re_set;
13361   ops->insert_location = bkpt_insert_location;
13362   ops->remove_location = bkpt_remove_location;
13363   ops->breakpoint_hit = bkpt_breakpoint_hit;
13364
13365   /* The breakpoint_ops structure to be used in regular breakpoints.  */
13366   ops = &bkpt_breakpoint_ops;
13367   *ops = bkpt_base_breakpoint_ops;
13368   ops->re_set = bkpt_re_set;
13369   ops->resources_needed = bkpt_resources_needed;
13370   ops->print_it = bkpt_print_it;
13371   ops->print_mention = bkpt_print_mention;
13372   ops->print_recreate = bkpt_print_recreate;
13373
13374   /* Ranged breakpoints.  */
13375   ops = &ranged_breakpoint_ops;
13376   *ops = bkpt_breakpoint_ops;
13377   ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13378   ops->resources_needed = resources_needed_ranged_breakpoint;
13379   ops->print_it = print_it_ranged_breakpoint;
13380   ops->print_one = print_one_ranged_breakpoint;
13381   ops->print_one_detail = print_one_detail_ranged_breakpoint;
13382   ops->print_mention = print_mention_ranged_breakpoint;
13383   ops->print_recreate = print_recreate_ranged_breakpoint;
13384
13385   /* Internal breakpoints.  */
13386   ops = &internal_breakpoint_ops;
13387   *ops = bkpt_base_breakpoint_ops;
13388   ops->re_set = internal_bkpt_re_set;
13389   ops->check_status = internal_bkpt_check_status;
13390   ops->print_it = internal_bkpt_print_it;
13391   ops->print_mention = internal_bkpt_print_mention;
13392
13393   /* Momentary breakpoints.  */
13394   ops = &momentary_breakpoint_ops;
13395   *ops = bkpt_base_breakpoint_ops;
13396   ops->re_set = momentary_bkpt_re_set;
13397   ops->check_status = momentary_bkpt_check_status;
13398   ops->print_it = momentary_bkpt_print_it;
13399   ops->print_mention = momentary_bkpt_print_mention;
13400
13401   /* GNU v3 exception catchpoints.  */
13402   ops = &gnu_v3_exception_catchpoint_ops;
13403   *ops = bkpt_breakpoint_ops;
13404   ops->print_it = print_it_exception_catchpoint;
13405   ops->print_one = print_one_exception_catchpoint;
13406   ops->print_mention = print_mention_exception_catchpoint;
13407   ops->print_recreate = print_recreate_exception_catchpoint;
13408
13409   /* Watchpoints.  */
13410   ops = &watchpoint_breakpoint_ops;
13411   *ops = base_breakpoint_ops;
13412   ops->dtor = dtor_watchpoint;
13413   ops->re_set = re_set_watchpoint;
13414   ops->insert_location = insert_watchpoint;
13415   ops->remove_location = remove_watchpoint;
13416   ops->breakpoint_hit = breakpoint_hit_watchpoint;
13417   ops->check_status = check_status_watchpoint;
13418   ops->resources_needed = resources_needed_watchpoint;
13419   ops->works_in_software_mode = works_in_software_mode_watchpoint;
13420   ops->print_it = print_it_watchpoint;
13421   ops->print_mention = print_mention_watchpoint;
13422   ops->print_recreate = print_recreate_watchpoint;
13423
13424   /* Masked watchpoints.  */
13425   ops = &masked_watchpoint_breakpoint_ops;
13426   *ops = watchpoint_breakpoint_ops;
13427   ops->insert_location = insert_masked_watchpoint;
13428   ops->remove_location = remove_masked_watchpoint;
13429   ops->resources_needed = resources_needed_masked_watchpoint;
13430   ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13431   ops->print_it = print_it_masked_watchpoint;
13432   ops->print_one_detail = print_one_detail_masked_watchpoint;
13433   ops->print_mention = print_mention_masked_watchpoint;
13434   ops->print_recreate = print_recreate_masked_watchpoint;
13435
13436   /* Tracepoints.  */
13437   ops = &tracepoint_breakpoint_ops;
13438   *ops = base_breakpoint_ops;
13439   ops->re_set = tracepoint_re_set;
13440   ops->breakpoint_hit = tracepoint_breakpoint_hit;
13441   ops->print_one_detail = tracepoint_print_one_detail;
13442   ops->print_mention = tracepoint_print_mention;
13443   ops->print_recreate = tracepoint_print_recreate;
13444
13445   /* Fork catchpoints.  */
13446   ops = &catch_fork_breakpoint_ops;
13447   *ops = base_breakpoint_ops;
13448   ops->insert_location = insert_catch_fork;
13449   ops->remove_location = remove_catch_fork;
13450   ops->breakpoint_hit = breakpoint_hit_catch_fork;
13451   ops->print_it = print_it_catch_fork;
13452   ops->print_one = print_one_catch_fork;
13453   ops->print_mention = print_mention_catch_fork;
13454   ops->print_recreate = print_recreate_catch_fork;
13455
13456   /* Vfork catchpoints.  */
13457   ops = &catch_vfork_breakpoint_ops;
13458   *ops = base_breakpoint_ops;
13459   ops->insert_location = insert_catch_vfork;
13460   ops->remove_location = remove_catch_vfork;
13461   ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13462   ops->print_it = print_it_catch_vfork;
13463   ops->print_one = print_one_catch_vfork;
13464   ops->print_mention = print_mention_catch_vfork;
13465   ops->print_recreate = print_recreate_catch_vfork;
13466
13467   /* Exec catchpoints.  */
13468   ops = &catch_exec_breakpoint_ops;
13469   *ops = base_breakpoint_ops;
13470   ops->dtor = dtor_catch_exec;
13471   ops->insert_location = insert_catch_exec;
13472   ops->remove_location = remove_catch_exec;
13473   ops->breakpoint_hit = breakpoint_hit_catch_exec;
13474   ops->print_it = print_it_catch_exec;
13475   ops->print_one = print_one_catch_exec;
13476   ops->print_mention = print_mention_catch_exec;
13477   ops->print_recreate = print_recreate_catch_exec;
13478
13479   /* Syscall catchpoints.  */
13480   ops = &catch_syscall_breakpoint_ops;
13481   *ops = base_breakpoint_ops;
13482   ops->dtor = dtor_catch_syscall;
13483   ops->insert_location = insert_catch_syscall;
13484   ops->remove_location = remove_catch_syscall;
13485   ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13486   ops->print_it = print_it_catch_syscall;
13487   ops->print_one = print_one_catch_syscall;
13488   ops->print_mention = print_mention_catch_syscall;
13489   ops->print_recreate = print_recreate_catch_syscall;
13490 }
13491
13492 void
13493 _initialize_breakpoint (void)
13494 {
13495   struct cmd_list_element *c;
13496
13497   initialize_breakpoint_ops ();
13498
13499   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
13500   observer_attach_inferior_exit (clear_syscall_counts);
13501   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
13502
13503   breakpoint_objfile_key = register_objfile_data ();
13504
13505   breakpoint_chain = 0;
13506   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
13507      before a breakpoint is set.  */
13508   breakpoint_count = 0;
13509
13510   tracepoint_count = 0;
13511
13512   add_com ("ignore", class_breakpoint, ignore_command, _("\
13513 Set ignore-count of breakpoint number N to COUNT.\n\
13514 Usage is `ignore N COUNT'."));
13515   if (xdb_commands)
13516     add_com_alias ("bc", "ignore", class_breakpoint, 1);
13517
13518   add_com ("commands", class_breakpoint, commands_command, _("\
13519 Set commands to be executed when a breakpoint is hit.\n\
13520 Give breakpoint number as argument after \"commands\".\n\
13521 With no argument, the targeted breakpoint is the last one set.\n\
13522 The commands themselves follow starting on the next line.\n\
13523 Type a line containing \"end\" to indicate the end of them.\n\
13524 Give \"silent\" as the first line to make the breakpoint silent;\n\
13525 then no output is printed when it is hit, except what the commands print."));
13526
13527   add_com ("condition", class_breakpoint, condition_command, _("\
13528 Specify breakpoint number N to break only if COND is true.\n\
13529 Usage is `condition N COND', where N is an integer and COND is an\n\
13530 expression to be evaluated whenever breakpoint N is reached."));
13531
13532   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
13533 Set a temporary breakpoint.\n\
13534 Like \"break\" except the breakpoint is only temporary,\n\
13535 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
13536 by using \"enable delete\" on the breakpoint number.\n\
13537 \n"
13538 BREAK_ARGS_HELP ("tbreak")));
13539   set_cmd_completer (c, location_completer);
13540
13541   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
13542 Set a hardware assisted breakpoint.\n\
13543 Like \"break\" except the breakpoint requires hardware support,\n\
13544 some target hardware may not have this support.\n\
13545 \n"
13546 BREAK_ARGS_HELP ("hbreak")));
13547   set_cmd_completer (c, location_completer);
13548
13549   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
13550 Set a temporary hardware assisted breakpoint.\n\
13551 Like \"hbreak\" except the breakpoint is only temporary,\n\
13552 so it will be deleted when hit.\n\
13553 \n"
13554 BREAK_ARGS_HELP ("thbreak")));
13555   set_cmd_completer (c, location_completer);
13556
13557   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13558 Enable some breakpoints.\n\
13559 Give breakpoint numbers (separated by spaces) as arguments.\n\
13560 With no subcommand, breakpoints are enabled until you command otherwise.\n\
13561 This is used to cancel the effect of the \"disable\" command.\n\
13562 With a subcommand you can enable temporarily."),
13563                   &enablelist, "enable ", 1, &cmdlist);
13564   if (xdb_commands)
13565     add_com ("ab", class_breakpoint, enable_command, _("\
13566 Enable some breakpoints.\n\
13567 Give breakpoint numbers (separated by spaces) as arguments.\n\
13568 With no subcommand, breakpoints are enabled until you command otherwise.\n\
13569 This is used to cancel the effect of the \"disable\" command.\n\
13570 With a subcommand you can enable temporarily."));
13571
13572   add_com_alias ("en", "enable", class_breakpoint, 1);
13573
13574   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
13575 Enable some breakpoints.\n\
13576 Give breakpoint numbers (separated by spaces) as arguments.\n\
13577 This is used to cancel the effect of the \"disable\" command.\n\
13578 May be abbreviated to simply \"enable\".\n"),
13579                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
13580
13581   add_cmd ("once", no_class, enable_once_command, _("\
13582 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
13583 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
13584            &enablebreaklist);
13585
13586   add_cmd ("delete", no_class, enable_delete_command, _("\
13587 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
13588 If a breakpoint is hit while enabled in this fashion, it is deleted."),
13589            &enablebreaklist);
13590
13591   add_cmd ("delete", no_class, enable_delete_command, _("\
13592 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
13593 If a breakpoint is hit while enabled in this fashion, it is deleted."),
13594            &enablelist);
13595
13596   add_cmd ("once", no_class, enable_once_command, _("\
13597 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
13598 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
13599            &enablelist);
13600
13601   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13602 Disable some breakpoints.\n\
13603 Arguments are breakpoint numbers with spaces in between.\n\
13604 To disable all breakpoints, give no argument.\n\
13605 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
13606                   &disablelist, "disable ", 1, &cmdlist);
13607   add_com_alias ("dis", "disable", class_breakpoint, 1);
13608   add_com_alias ("disa", "disable", class_breakpoint, 1);
13609   if (xdb_commands)
13610     add_com ("sb", class_breakpoint, disable_command, _("\
13611 Disable some breakpoints.\n\
13612 Arguments are breakpoint numbers with spaces in between.\n\
13613 To disable all breakpoints, give no argument.\n\
13614 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
13615
13616   add_cmd ("breakpoints", class_alias, disable_command, _("\
13617 Disable some breakpoints.\n\
13618 Arguments are breakpoint numbers with spaces in between.\n\
13619 To disable all breakpoints, give no argument.\n\
13620 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
13621 This command may be abbreviated \"disable\"."),
13622            &disablelist);
13623
13624   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13625 Delete some breakpoints or auto-display expressions.\n\
13626 Arguments are breakpoint numbers with spaces in between.\n\
13627 To delete all breakpoints, give no argument.\n\
13628 \n\
13629 Also a prefix command for deletion of other GDB objects.\n\
13630 The \"unset\" command is also an alias for \"delete\"."),
13631                   &deletelist, "delete ", 1, &cmdlist);
13632   add_com_alias ("d", "delete", class_breakpoint, 1);
13633   add_com_alias ("del", "delete", class_breakpoint, 1);
13634   if (xdb_commands)
13635     add_com ("db", class_breakpoint, delete_command, _("\
13636 Delete some breakpoints.\n\
13637 Arguments are breakpoint numbers with spaces in between.\n\
13638 To delete all breakpoints, give no argument.\n"));
13639
13640   add_cmd ("breakpoints", class_alias, delete_command, _("\
13641 Delete some breakpoints or auto-display expressions.\n\
13642 Arguments are breakpoint numbers with spaces in between.\n\
13643 To delete all breakpoints, give no argument.\n\
13644 This command may be abbreviated \"delete\"."),
13645            &deletelist);
13646
13647   add_com ("clear", class_breakpoint, clear_command, _("\
13648 Clear breakpoint at specified line or function.\n\
13649 Argument may be line number, function name, or \"*\" and an address.\n\
13650 If line number is specified, all breakpoints in that line are cleared.\n\
13651 If function is specified, breakpoints at beginning of function are cleared.\n\
13652 If an address is specified, breakpoints at that address are cleared.\n\
13653 \n\
13654 With no argument, clears all breakpoints in the line that the selected frame\n\
13655 is executing in.\n\
13656 \n\
13657 See also the \"delete\" command which clears breakpoints by number."));
13658   add_com_alias ("cl", "clear", class_breakpoint, 1);
13659
13660   c = add_com ("break", class_breakpoint, break_command, _("\
13661 Set breakpoint at specified line or function.\n"
13662 BREAK_ARGS_HELP ("break")));
13663   set_cmd_completer (c, location_completer);
13664
13665   add_com_alias ("b", "break", class_run, 1);
13666   add_com_alias ("br", "break", class_run, 1);
13667   add_com_alias ("bre", "break", class_run, 1);
13668   add_com_alias ("brea", "break", class_run, 1);
13669
13670   if (xdb_commands)
13671    add_com_alias ("ba", "break", class_breakpoint, 1);
13672
13673   if (dbx_commands)
13674     {
13675       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13676 Break in function/address or break at a line in the current file."),
13677                              &stoplist, "stop ", 1, &cmdlist);
13678       add_cmd ("in", class_breakpoint, stopin_command,
13679                _("Break in function or address."), &stoplist);
13680       add_cmd ("at", class_breakpoint, stopat_command,
13681                _("Break at a line in the current file."), &stoplist);
13682       add_com ("status", class_info, breakpoints_info, _("\
13683 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
13684 The \"Type\" column indicates one of:\n\
13685 \tbreakpoint     - normal breakpoint\n\
13686 \twatchpoint     - watchpoint\n\
13687 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13688 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
13689 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
13690 address and file/line number respectively.\n\
13691 \n\
13692 Convenience variable \"$_\" and default examine address for \"x\"\n\
13693 are set to the address of the last breakpoint listed unless the command\n\
13694 is prefixed with \"server \".\n\n\
13695 Convenience variable \"$bpnum\" contains the number of the last\n\
13696 breakpoint set."));
13697     }
13698
13699   add_info ("breakpoints", breakpoints_info, _("\
13700 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
13701 The \"Type\" column indicates one of:\n\
13702 \tbreakpoint     - normal breakpoint\n\
13703 \twatchpoint     - watchpoint\n\
13704 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13705 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
13706 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
13707 address and file/line number respectively.\n\
13708 \n\
13709 Convenience variable \"$_\" and default examine address for \"x\"\n\
13710 are set to the address of the last breakpoint listed unless the command\n\
13711 is prefixed with \"server \".\n\n\
13712 Convenience variable \"$bpnum\" contains the number of the last\n\
13713 breakpoint set."));
13714
13715   add_info_alias ("b", "breakpoints", 1);
13716
13717   if (xdb_commands)
13718     add_com ("lb", class_breakpoint, breakpoints_info, _("\
13719 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
13720 The \"Type\" column indicates one of:\n\
13721 \tbreakpoint     - normal breakpoint\n\
13722 \twatchpoint     - watchpoint\n\
13723 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13724 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
13725 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
13726 address and file/line number respectively.\n\
13727 \n\
13728 Convenience variable \"$_\" and default examine address for \"x\"\n\
13729 are set to the address of the last breakpoint listed unless the command\n\
13730 is prefixed with \"server \".\n\n\
13731 Convenience variable \"$bpnum\" contains the number of the last\n\
13732 breakpoint set."));
13733
13734   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13735 Status of all breakpoints, or breakpoint number NUMBER.\n\
13736 The \"Type\" column indicates one of:\n\
13737 \tbreakpoint     - normal breakpoint\n\
13738 \twatchpoint     - watchpoint\n\
13739 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
13740 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13741 \tuntil          - internal breakpoint used by the \"until\" command\n\
13742 \tfinish         - internal breakpoint used by the \"finish\" command\n\
13743 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13744 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
13745 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
13746 address and file/line number respectively.\n\
13747 \n\
13748 Convenience variable \"$_\" and default examine address for \"x\"\n\
13749 are set to the address of the last breakpoint listed unless the command\n\
13750 is prefixed with \"server \".\n\n\
13751 Convenience variable \"$bpnum\" contains the number of the last\n\
13752 breakpoint set."),
13753            &maintenanceinfolist);
13754
13755   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13756 Set catchpoints to catch events."),
13757                   &catch_cmdlist, "catch ",
13758                   0/*allow-unknown*/, &cmdlist);
13759
13760   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13761 Set temporary catchpoints to catch events."),
13762                   &tcatch_cmdlist, "tcatch ",
13763                   0/*allow-unknown*/, &cmdlist);
13764
13765   /* Add catch and tcatch sub-commands.  */
13766   add_catch_command ("catch", _("\
13767 Catch an exception, when caught.\n\
13768 With an argument, catch only exceptions with the given name."),
13769                      catch_catch_command,
13770                      NULL,
13771                      CATCH_PERMANENT,
13772                      CATCH_TEMPORARY);
13773   add_catch_command ("throw", _("\
13774 Catch an exception, when thrown.\n\
13775 With an argument, catch only exceptions with the given name."),
13776                      catch_throw_command,
13777                      NULL,
13778                      CATCH_PERMANENT,
13779                      CATCH_TEMPORARY);
13780   add_catch_command ("fork", _("Catch calls to fork."),
13781                      catch_fork_command_1,
13782                      NULL,
13783                      (void *) (uintptr_t) catch_fork_permanent,
13784                      (void *) (uintptr_t) catch_fork_temporary);
13785   add_catch_command ("vfork", _("Catch calls to vfork."),
13786                      catch_fork_command_1,
13787                      NULL,
13788                      (void *) (uintptr_t) catch_vfork_permanent,
13789                      (void *) (uintptr_t) catch_vfork_temporary);
13790   add_catch_command ("exec", _("Catch calls to exec."),
13791                      catch_exec_command_1,
13792                      NULL,
13793                      CATCH_PERMANENT,
13794                      CATCH_TEMPORARY);
13795   add_catch_command ("syscall", _("\
13796 Catch system calls by their names and/or numbers.\n\
13797 Arguments say which system calls to catch.  If no arguments\n\
13798 are given, every system call will be caught.\n\
13799 Arguments, if given, should be one or more system call names\n\
13800 (if your system supports that), or system call numbers."),
13801                      catch_syscall_command_1,
13802                      catch_syscall_completer,
13803                      CATCH_PERMANENT,
13804                      CATCH_TEMPORARY);
13805
13806   c = add_com ("watch", class_breakpoint, watch_command, _("\
13807 Set a watchpoint for an expression.\n\
13808 Usage: watch [-l|-location] EXPRESSION\n\
13809 A watchpoint stops execution of your program whenever the value of\n\
13810 an expression changes.\n\
13811 If -l or -location is given, this evaluates EXPRESSION and watches\n\
13812 the memory to which it refers."));
13813   set_cmd_completer (c, expression_completer);
13814
13815   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13816 Set a read watchpoint for an expression.\n\
13817 Usage: rwatch [-l|-location] EXPRESSION\n\
13818 A watchpoint stops execution of your program whenever the value of\n\
13819 an expression is read.\n\
13820 If -l or -location is given, this evaluates EXPRESSION and watches\n\
13821 the memory to which it refers."));
13822   set_cmd_completer (c, expression_completer);
13823
13824   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13825 Set a watchpoint for an expression.\n\
13826 Usage: awatch [-l|-location] EXPRESSION\n\
13827 A watchpoint stops execution of your program whenever the value of\n\
13828 an expression is either read or written.\n\
13829 If -l or -location is given, this evaluates EXPRESSION and watches\n\
13830 the memory to which it refers."));
13831   set_cmd_completer (c, expression_completer);
13832
13833   add_info ("watchpoints", watchpoints_info, _("\
13834 Status of specified watchpoints (all watchpoints if no argument)."));
13835
13836   /* XXX: cagney/2005-02-23: This should be a boolean, and should
13837      respond to changes - contrary to the description.  */
13838   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13839                             &can_use_hw_watchpoints, _("\
13840 Set debugger's willingness to use watchpoint hardware."), _("\
13841 Show debugger's willingness to use watchpoint hardware."), _("\
13842 If zero, gdb will not use hardware for new watchpoints, even if\n\
13843 such is available.  (However, any hardware watchpoints that were\n\
13844 created before setting this to nonzero, will continue to use watchpoint\n\
13845 hardware.)"),
13846                             NULL,
13847                             show_can_use_hw_watchpoints,
13848                             &setlist, &showlist);
13849
13850   can_use_hw_watchpoints = 1;
13851
13852   /* Tracepoint manipulation commands.  */
13853
13854   c = add_com ("trace", class_breakpoint, trace_command, _("\
13855 Set a tracepoint at specified line or function.\n\
13856 \n"
13857 BREAK_ARGS_HELP ("trace") "\n\
13858 Do \"help tracepoints\" for info on other tracepoint commands."));
13859   set_cmd_completer (c, location_completer);
13860
13861   add_com_alias ("tp", "trace", class_alias, 0);
13862   add_com_alias ("tr", "trace", class_alias, 1);
13863   add_com_alias ("tra", "trace", class_alias, 1);
13864   add_com_alias ("trac", "trace", class_alias, 1);
13865
13866   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13867 Set a fast tracepoint at specified line or function.\n\
13868 \n"
13869 BREAK_ARGS_HELP ("ftrace") "\n\
13870 Do \"help tracepoints\" for info on other tracepoint commands."));
13871   set_cmd_completer (c, location_completer);
13872
13873   c = add_com ("strace", class_breakpoint, strace_command, _("\
13874 Set a static tracepoint at specified line, function or marker.\n\
13875 \n\
13876 strace [LOCATION] [if CONDITION]\n\
13877 LOCATION may be a line number, function name, \"*\" and an address,\n\
13878 or -m MARKER_ID.\n\
13879 If a line number is specified, probe the marker at start of code\n\
13880 for that line.  If a function is specified, probe the marker at start\n\
13881 of code for that function.  If an address is specified, probe the marker\n\
13882 at that exact address.  If a marker id is specified, probe the marker\n\
13883 with that name.  With no LOCATION, uses current execution address of\n\
13884 the selected stack frame.\n\
13885 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13886 This collects arbitrary user data passed in the probe point call to the\n\
13887 tracing library.  You can inspect it when analyzing the trace buffer,\n\
13888 by printing the $_sdata variable like any other convenience variable.\n\
13889 \n\
13890 CONDITION is a boolean expression.\n\
13891 \n\
13892 Multiple tracepoints at one place are permitted, and useful if their\n\
13893 conditions are different.\n\
13894 \n\
13895 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13896 Do \"help tracepoints\" for info on other tracepoint commands."));
13897   set_cmd_completer (c, location_completer);
13898
13899   add_info ("tracepoints", tracepoints_info, _("\
13900 Status of specified tracepoints (all tracepoints if no argument).\n\
13901 Convenience variable \"$tpnum\" contains the number of the\n\
13902 last tracepoint set."));
13903
13904   add_info_alias ("tp", "tracepoints", 1);
13905
13906   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13907 Delete specified tracepoints.\n\
13908 Arguments are tracepoint numbers, separated by spaces.\n\
13909 No argument means delete all tracepoints."),
13910            &deletelist);
13911
13912   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13913 Disable specified tracepoints.\n\
13914 Arguments are tracepoint numbers, separated by spaces.\n\
13915 No argument means disable all tracepoints."),
13916            &disablelist);
13917   deprecate_cmd (c, "disable");
13918
13919   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13920 Enable specified tracepoints.\n\
13921 Arguments are tracepoint numbers, separated by spaces.\n\
13922 No argument means enable all tracepoints."),
13923            &enablelist);
13924   deprecate_cmd (c, "enable");
13925
13926   add_com ("passcount", class_trace, trace_pass_command, _("\
13927 Set the passcount for a tracepoint.\n\
13928 The trace will end when the tracepoint has been passed 'count' times.\n\
13929 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13930 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13931
13932   add_prefix_cmd ("save", class_breakpoint, save_command,
13933                   _("Save breakpoint definitions as a script."),
13934                   &save_cmdlist, "save ",
13935                   0/*allow-unknown*/, &cmdlist);
13936
13937   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13938 Save current breakpoint definitions as a script.\n\
13939 This includes all types of breakpoints (breakpoints, watchpoints,\n\
13940 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
13941 session to restore them."),
13942                &save_cmdlist);
13943   set_cmd_completer (c, filename_completer);
13944
13945   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
13946 Save current tracepoint definitions as a script.\n\
13947 Use the 'source' command in another debug session to restore them."),
13948                &save_cmdlist);
13949   set_cmd_completer (c, filename_completer);
13950
13951   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13952   deprecate_cmd (c, "save tracepoints");
13953
13954   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
13955 Breakpoint specific settings\n\
13956 Configure various breakpoint-specific variables such as\n\
13957 pending breakpoint behavior"),
13958                   &breakpoint_set_cmdlist, "set breakpoint ",
13959                   0/*allow-unknown*/, &setlist);
13960   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
13961 Breakpoint specific settings\n\
13962 Configure various breakpoint-specific variables such as\n\
13963 pending breakpoint behavior"),
13964                   &breakpoint_show_cmdlist, "show breakpoint ",
13965                   0/*allow-unknown*/, &showlist);
13966
13967   add_setshow_auto_boolean_cmd ("pending", no_class,
13968                                 &pending_break_support, _("\
13969 Set debugger's behavior regarding pending breakpoints."), _("\
13970 Show debugger's behavior regarding pending breakpoints."), _("\
13971 If on, an unrecognized breakpoint location will cause gdb to create a\n\
13972 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
13973 an error.  If auto, an unrecognized breakpoint location results in a\n\
13974 user-query to see if a pending breakpoint should be created."),
13975                                 NULL,
13976                                 show_pending_break_support,
13977                                 &breakpoint_set_cmdlist,
13978                                 &breakpoint_show_cmdlist);
13979
13980   pending_break_support = AUTO_BOOLEAN_AUTO;
13981
13982   add_setshow_boolean_cmd ("auto-hw", no_class,
13983                            &automatic_hardware_breakpoints, _("\
13984 Set automatic usage of hardware breakpoints."), _("\
13985 Show automatic usage of hardware breakpoints."), _("\
13986 If set, the debugger will automatically use hardware breakpoints for\n\
13987 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
13988 a warning will be emitted for such breakpoints."),
13989                            NULL,
13990                            show_automatic_hardware_breakpoints,
13991                            &breakpoint_set_cmdlist,
13992                            &breakpoint_show_cmdlist);
13993
13994   add_setshow_enum_cmd ("always-inserted", class_support,
13995                         always_inserted_enums, &always_inserted_mode, _("\
13996 Set mode for inserting breakpoints."), _("\
13997 Show mode for inserting breakpoints."), _("\
13998 When this mode is off, breakpoints are inserted in inferior when it is\n\
13999 resumed, and removed when execution stops.  When this mode is on,\n\
14000 breakpoints are inserted immediately and removed only when the user\n\
14001 deletes the breakpoint.  When this mode is auto (which is the default),\n\
14002 the behaviour depends on the non-stop setting (see help set non-stop).\n\
14003 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14004 behaves as if always-inserted mode is on; if gdb is controlling the\n\
14005 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
14006                            NULL,
14007                            &show_always_inserted_mode,
14008                            &breakpoint_set_cmdlist,
14009                            &breakpoint_show_cmdlist);
14010
14011   add_com ("break-range", class_breakpoint, break_range_command, _("\
14012 Set a breakpoint for an address range.\n\
14013 break-range START-LOCATION, END-LOCATION\n\
14014 where START-LOCATION and END-LOCATION can be one of the following:\n\
14015   LINENUM, for that line in the current file,\n\
14016   FILE:LINENUM, for that line in that file,\n\
14017   +OFFSET, for that number of lines after the current line\n\
14018            or the start of the range\n\
14019   FUNCTION, for the first line in that function,\n\
14020   FILE:FUNCTION, to distinguish among like-named static functions.\n\
14021   *ADDRESS, for the instruction at that address.\n\
14022 \n\
14023 The breakpoint will stop execution of the inferior whenever it executes\n\
14024 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14025 range (including START-LOCATION and END-LOCATION)."));
14026
14027   automatic_hardware_breakpoints = 1;
14028
14029   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
14030 }