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