OSDN Git Service

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