OSDN Git Service

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