OSDN Git Service

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