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