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