* NEWS: Mention Windows cross debugging support.
[platform/upstream/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
5    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 2 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, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor,
22    Boston, MA 02110-1301, USA.  */
23
24 #include "defs.h"
25 #include <ctype.h>
26 #include "symtab.h"
27 #include "frame.h"
28 #include "breakpoint.h"
29 #include "gdbtypes.h"
30 #include "expression.h"
31 #include "gdbcore.h"
32 #include "gdbcmd.h"
33 #include "value.h"
34 #include "command.h"
35 #include "inferior.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb_string.h"
40 #include "demangle.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "gdb_assert.h"
51 #include "block.h"
52 #include "solib.h"
53 #include "solist.h"
54 #include "observer.h"
55 #include "exceptions.h"
56
57 #include "gdb-events.h"
58 #include "mi/mi-common.h"
59
60 /* Prototypes for local functions. */
61
62 static void until_break_command_continuation (struct continuation_arg *arg);
63
64 static void catch_command_1 (char *, int, int);
65
66 static void enable_delete_command (char *, int);
67
68 static void enable_delete_breakpoint (struct breakpoint *);
69
70 static void enable_once_command (char *, int);
71
72 static void enable_once_breakpoint (struct breakpoint *);
73
74 static void disable_command (char *, int);
75
76 static void enable_command (char *, int);
77
78 static void map_breakpoint_numbers (char *, void (*)(struct breakpoint *));
79
80 static void ignore_command (char *, int);
81
82 static int breakpoint_re_set_one (void *);
83
84 static void clear_command (char *, int);
85
86 static void catch_command (char *, int);
87
88 static void watch_command (char *, int);
89
90 static int can_use_hardware_watchpoint (struct value *);
91
92 static int break_command_1 (char *, int, int, struct breakpoint *);
93
94 static void mention (struct breakpoint *);
95
96 struct breakpoint *set_raw_breakpoint (struct symtab_and_line, enum bptype);
97
98 static void check_duplicates (struct breakpoint *);
99
100 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
101
102 static CORE_ADDR adjust_breakpoint_address (CORE_ADDR bpaddr,
103                                             enum bptype bptype);
104
105 static void describe_other_breakpoints (CORE_ADDR, asection *);
106
107 static void breakpoints_info (char *, int);
108
109 static void breakpoint_1 (int, int);
110
111 static bpstat bpstat_alloc (struct breakpoint *, bpstat);
112
113 static int breakpoint_cond_eval (void *);
114
115 static void cleanup_executing_breakpoints (void *);
116
117 static void commands_command (char *, int);
118
119 static void condition_command (char *, int);
120
121 static int get_number_trailer (char **, int);
122
123 void set_breakpoint_count (int);
124
125 typedef enum
126   {
127     mark_inserted,
128     mark_uninserted
129   }
130 insertion_state_t;
131
132 static int remove_breakpoint (struct bp_location *, insertion_state_t);
133
134 static enum print_stop_action print_it_typical (bpstat);
135
136 static enum print_stop_action print_bp_stop_message (bpstat bs);
137
138 typedef struct
139   {
140     enum exception_event_kind kind;
141     int enable_p;
142   }
143 args_for_catchpoint_enable;
144
145 static int watchpoint_check (void *);
146
147 static int cover_target_enable_exception_callback (void *);
148
149 static void maintenance_info_breakpoints (char *, int);
150
151 static void create_longjmp_breakpoint (char *);
152
153 static void create_overlay_event_breakpoint (char *);
154
155 static int hw_breakpoint_used_count (void);
156
157 static int hw_watchpoint_used_count (enum bptype, int *);
158
159 static void hbreak_command (char *, int);
160
161 static void thbreak_command (char *, int);
162
163 static void watch_command_1 (char *, int, int);
164
165 static void rwatch_command (char *, int);
166
167 static void awatch_command (char *, int);
168
169 static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
170
171 static void solib_load_unload_1 (char *hookname,
172                                  int tempflag,
173                                  char *dll_pathname,
174                                  char *cond_string, enum bptype bp_kind);
175
176 static void create_fork_vfork_event_catchpoint (int tempflag,
177                                                 char *cond_string,
178                                                 enum bptype bp_kind);
179
180 static void stop_command (char *arg, int from_tty);
181
182 static void stopin_command (char *arg, int from_tty);
183
184 static void stopat_command (char *arg, int from_tty);
185
186 static char *ep_find_event_name_end (char *arg);
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 create_exception_catchpoint (int tempflag, char *cond_string,
193                                          enum exception_event_kind ex_event,
194                                          struct symtab_and_line *sal);
195
196 static void catch_exception_command_1 (enum exception_event_kind ex_event, 
197                                        char *arg, int tempflag, int from_tty);
198
199 static void tcatch_command (char *arg, int from_tty);
200
201 static void ep_skip_leading_whitespace (char **s);
202
203 /* Prototypes for exported functions. */
204
205 /* If FALSE, gdb will not use hardware support for watchpoints, even
206    if such is available. */
207 static int can_use_hw_watchpoints;
208
209 static void
210 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
211                              struct cmd_list_element *c,
212                              const char *value)
213 {
214   fprintf_filtered (file, _("\
215 Debugger's willingness to use watchpoint hardware is %s.\n"),
216                     value);
217 }
218
219 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
220    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
221    for unrecognized breakpoint locations.  
222    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
223 static enum auto_boolean pending_break_support;
224 static void
225 show_pending_break_support (struct ui_file *file, int from_tty,
226                             struct cmd_list_element *c,
227                             const char *value)
228 {
229   fprintf_filtered (file, _("\
230 Debugger's behavior regarding pending breakpoints is %s.\n"),
231                     value);
232 }
233
234 void _initialize_breakpoint (void);
235
236 extern int addressprint;        /* Print machine addresses? */
237
238 /* Are we executing breakpoint commands?  */
239 static int executing_breakpoint_commands;
240
241 /* Are overlay event breakpoints enabled? */
242 static int overlay_events_enabled;
243
244 /* Walk the following statement or block through all breakpoints.
245    ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
246    breakpoint.  */
247
248 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
249
250 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
251         for (B = breakpoint_chain;      \
252              B ? (TMP=B->next, 1): 0;   \
253              B = TMP)
254
255 /* Similar iterators for the low-level breakpoints.  */
256
257 #define ALL_BP_LOCATIONS(B)  for (B = bp_location_chain; B; B = B->next)
258
259 #define ALL_BP_LOCATIONS_SAFE(B,TMP)    \
260         for (B = bp_location_chain;     \
261              B ? (TMP=B->next, 1): 0;   \
262              B = TMP)
263
264 /* True if breakpoint hit counts should be displayed in breakpoint info.  */
265
266 int show_breakpoint_hit_counts = 1;
267
268 /* Chains of all breakpoints defined.  */
269
270 struct breakpoint *breakpoint_chain;
271
272 struct bp_location *bp_location_chain;
273
274 /* Number of last breakpoint made.  */
275
276 int breakpoint_count;
277
278 /* Pointer to current exception event record */
279 static struct exception_event_record *current_exception_event;
280
281 /* Indicator of whether exception catchpoints should be nuked between
282    runs of a program.  */
283 int deprecated_exception_catchpoints_are_fragile = 0;
284
285 /* Indicator of when exception catchpoints set-up should be
286    reinitialized -- e.g. when program is re-run.  */
287 int deprecated_exception_support_initialized = 0;
288
289 /* This function returns a pointer to the string representation of the
290    pathname of the dynamically-linked library that has just been
291    loaded.
292
293    This function must be used only when SOLIB_HAVE_LOAD_EVENT is TRUE,
294    or undefined results are guaranteed.
295
296    This string's contents are only valid immediately after the
297    inferior has stopped in the dynamic linker hook, and becomes
298    invalid as soon as the inferior is continued.  Clients should make
299    a copy of this string if they wish to continue the inferior and
300    then access the string.  */
301
302 #ifndef SOLIB_LOADED_LIBRARY_PATHNAME
303 #define SOLIB_LOADED_LIBRARY_PATHNAME(pid) ""
304 #endif
305
306 /* This function returns a pointer to the string representation of the
307    pathname of the dynamically-linked library that has just been
308    unloaded.
309
310    This function must be used only when SOLIB_HAVE_UNLOAD_EVENT is
311    TRUE, or undefined results are guaranteed.
312
313    This string's contents are only valid immediately after the
314    inferior has stopped in the dynamic linker hook, and becomes
315    invalid as soon as the inferior is continued.  Clients should make
316    a copy of this string if they wish to continue the inferior and
317    then access the string.  */
318
319 #ifndef SOLIB_UNLOADED_LIBRARY_PATHNAME
320 #define SOLIB_UNLOADED_LIBRARY_PATHNAME(pid) ""
321 #endif
322
323 /* This function is called by the "catch load" command.  It allows the
324    debugger to be notified by the dynamic linker when a specified
325    library file (or any library file, if filename is NULL) is loaded.  */
326
327 #ifndef SOLIB_CREATE_CATCH_LOAD_HOOK
328 #define SOLIB_CREATE_CATCH_LOAD_HOOK(pid,tempflag,filename,cond_string) \
329    error (_("catch of library loads not yet implemented on this platform"))
330 #endif
331
332 /* This function is called by the "catch unload" command.  It allows
333    the debugger to be notified by the dynamic linker when a specified
334    library file (or any library file, if filename is NULL) is
335    unloaded.  */
336
337 #ifndef SOLIB_CREATE_CATCH_UNLOAD_HOOK
338 #define SOLIB_CREATE_CATCH_UNLOAD_HOOK(pid, tempflag, filename, cond_string) \
339    error (_("catch of library unloads not yet implemented on this platform"))
340 #endif
341
342 /* Return whether a breakpoint is an active enabled breakpoint.  */
343 static int
344 breakpoint_enabled (struct breakpoint *b)
345 {
346   return (b->enable_state == bp_enabled && !b->pending);
347 }
348
349 /* Set breakpoint count to NUM.  */
350
351 void
352 set_breakpoint_count (int num)
353 {
354   breakpoint_count = num;
355   set_internalvar (lookup_internalvar ("bpnum"),
356                    value_from_longest (builtin_type_int, (LONGEST) num));
357 }
358
359 /* Used in run_command to zero the hit count when a new run starts. */
360
361 void
362 clear_breakpoint_hit_counts (void)
363 {
364   struct breakpoint *b;
365
366   ALL_BREAKPOINTS (b)
367     b->hit_count = 0;
368 }
369
370 /* Default address, symtab and line to put a breakpoint at
371    for "break" command with no arg.
372    if default_breakpoint_valid is zero, the other three are
373    not valid, and "break" with no arg is an error.
374
375    This set by print_stack_frame, which calls set_default_breakpoint.  */
376
377 int default_breakpoint_valid;
378 CORE_ADDR default_breakpoint_address;
379 struct symtab *default_breakpoint_symtab;
380 int default_breakpoint_line;
381 \f
382 /* *PP is a string denoting a breakpoint.  Get the number of the breakpoint.
383    Advance *PP after the string and any trailing whitespace.
384
385    Currently the string can either be a number or "$" followed by the name
386    of a convenience variable.  Making it an expression wouldn't work well
387    for map_breakpoint_numbers (e.g. "4 + 5 + 6").
388    
389    TRAILER is a character which can be found after the number; most
390    commonly this is `-'.  If you don't want a trailer, use \0.  */ 
391 static int
392 get_number_trailer (char **pp, int trailer)
393 {
394   int retval = 0;       /* default */
395   char *p = *pp;
396
397   if (p == NULL)
398     /* Empty line means refer to the last breakpoint.  */
399     return breakpoint_count;
400   else if (*p == '$')
401     {
402       /* Make a copy of the name, so we can null-terminate it
403          to pass to lookup_internalvar().  */
404       char *varname;
405       char *start = ++p;
406       struct value *val;
407
408       while (isalnum (*p) || *p == '_')
409         p++;
410       varname = (char *) alloca (p - start + 1);
411       strncpy (varname, start, p - start);
412       varname[p - start] = '\0';
413       val = value_of_internalvar (lookup_internalvar (varname));
414       if (TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
415         retval = (int) value_as_long (val);
416       else
417         {
418           printf_filtered (_("Convenience variable must have integer value.\n"));
419           retval = 0;
420         }
421     }
422   else
423     {
424       if (*p == '-')
425         ++p;
426       while (*p >= '0' && *p <= '9')
427         ++p;
428       if (p == *pp)
429         /* There is no number here.  (e.g. "cond a == b").  */
430         {
431           /* Skip non-numeric token */
432           while (*p && !isspace((int) *p))
433             ++p;
434           /* Return zero, which caller must interpret as error. */
435           retval = 0;
436         }
437       else
438         retval = atoi (*pp);
439     }
440   if (!(isspace (*p) || *p == '\0' || *p == trailer))
441     {
442       /* Trailing junk: return 0 and let caller print error msg. */
443       while (!(isspace (*p) || *p == '\0' || *p == trailer))
444         ++p;
445       retval = 0;
446     }
447   while (isspace (*p))
448     p++;
449   *pp = p;
450   return retval;
451 }
452
453
454 /* Like get_number_trailer, but don't allow a trailer.  */
455 int
456 get_number (char **pp)
457 {
458   return get_number_trailer (pp, '\0');
459 }
460
461 /* Parse a number or a range.
462  * A number will be of the form handled by get_number.
463  * A range will be of the form <number1> - <number2>, and 
464  * will represent all the integers between number1 and number2,
465  * inclusive.
466  *
467  * While processing a range, this fuction is called iteratively;
468  * At each call it will return the next value in the range.
469  *
470  * At the beginning of parsing a range, the char pointer PP will
471  * be advanced past <number1> and left pointing at the '-' token.
472  * Subsequent calls will not advance the pointer until the range
473  * is completed.  The call that completes the range will advance
474  * pointer PP past <number2>.
475  */
476
477 int 
478 get_number_or_range (char **pp)
479 {
480   static int last_retval, end_value;
481   static char *end_ptr;
482   static int in_range = 0;
483
484   if (**pp != '-')
485     {
486       /* Default case: pp is pointing either to a solo number, 
487          or to the first number of a range.  */
488       last_retval = get_number_trailer (pp, '-');
489       if (**pp == '-')
490         {
491           char **temp;
492
493           /* This is the start of a range (<number1> - <number2>).
494              Skip the '-', parse and remember the second number,
495              and also remember the end of the final token.  */
496
497           temp = &end_ptr; 
498           end_ptr = *pp + 1; 
499           while (isspace ((int) *end_ptr))
500             end_ptr++;  /* skip white space */
501           end_value = get_number (temp);
502           if (end_value < last_retval) 
503             {
504               error (_("inverted range"));
505             }
506           else if (end_value == last_retval)
507             {
508               /* degenerate range (number1 == number2).  Advance the
509                  token pointer so that the range will be treated as a
510                  single number.  */ 
511               *pp = end_ptr;
512             }
513           else
514             in_range = 1;
515         }
516     }
517   else if (! in_range)
518     error (_("negative value"));
519   else
520     {
521       /* pp points to the '-' that betokens a range.  All
522          number-parsing has already been done.  Return the next
523          integer value (one greater than the saved previous value).
524          Do not advance the token pointer 'pp' until the end of range
525          is reached.  */
526
527       if (++last_retval == end_value)
528         {
529           /* End of range reached; advance token pointer.  */
530           *pp = end_ptr;
531           in_range = 0;
532         }
533     }
534   return last_retval;
535 }
536
537
538 \f
539 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
540
541 static void
542 condition_command (char *arg, int from_tty)
543 {
544   struct breakpoint *b;
545   char *p;
546   int bnum;
547
548   if (arg == 0)
549     error_no_arg (_("breakpoint number"));
550
551   p = arg;
552   bnum = get_number (&p);
553   if (bnum == 0)
554     error (_("Bad breakpoint argument: '%s'"), arg);
555
556   ALL_BREAKPOINTS (b)
557     if (b->number == bnum)
558     {
559       if (b->cond)
560         {
561           xfree (b->cond);
562           b->cond = 0;
563         }
564       if (b->cond_string != NULL)
565         xfree (b->cond_string);
566
567       if (*p == 0)
568         {
569           b->cond = 0;
570           b->cond_string = NULL;
571           if (from_tty)
572             printf_filtered (_("Breakpoint %d now unconditional.\n"), bnum);
573         }
574       else
575         {
576           arg = p;
577           /* I don't know if it matters whether this is the string the user
578              typed in or the decompiled expression.  */
579           b->cond_string = savestring (arg, strlen (arg));
580           if (!b->pending)
581             {
582               b->cond = parse_exp_1 (&arg, block_for_pc (b->loc->address), 0);
583               if (*arg)
584                 error (_("Junk at end of expression"));
585             }
586         }
587       breakpoints_changed ();
588       breakpoint_modify_event (b->number);
589       return;
590     }
591
592   error (_("No breakpoint number %d."), bnum);
593 }
594
595 static void
596 commands_command (char *arg, int from_tty)
597 {
598   struct breakpoint *b;
599   char *p;
600   int bnum;
601   struct command_line *l;
602
603   /* If we allowed this, we would have problems with when to
604      free the storage, if we change the commands currently
605      being read from.  */
606
607   if (executing_breakpoint_commands)
608     error (_("Can't use the \"commands\" command among a breakpoint's commands."));
609
610   p = arg;
611   bnum = get_number (&p);
612
613   if (p && *p)
614     error (_("Unexpected extra arguments following breakpoint number."));
615
616   ALL_BREAKPOINTS (b)
617     if (b->number == bnum)
618       {
619         char *tmpbuf = xstrprintf ("Type commands for when breakpoint %d is hit, one per line.", 
620                                  bnum);
621         struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
622         l = read_command_lines (tmpbuf, from_tty);
623         do_cleanups (cleanups);
624         free_command_lines (&b->commands);
625         b->commands = l;
626         breakpoints_changed ();
627         breakpoint_modify_event (b->number);
628         return;
629     }
630   error (_("No breakpoint number %d."), bnum);
631 }
632 \f
633 /* Like target_read_memory() but if breakpoints are inserted, return
634    the shadow contents instead of the breakpoints themselves.
635
636    Read "memory data" from whatever target or inferior we have. 
637    Returns zero if successful, errno value if not.  EIO is used
638    for address out of bounds.  If breakpoints are inserted, returns
639    shadow contents, not the breakpoints themselves.  From breakpoint.c.  */
640
641 int
642 deprecated_read_memory_nobpt (CORE_ADDR memaddr, gdb_byte *myaddr,
643                               unsigned len)
644 {
645   int status;
646   struct bp_location *b;
647   CORE_ADDR bp_addr = 0;
648   int bp_size = 0;
649
650   if (BREAKPOINT_FROM_PC (&bp_addr, &bp_size) == NULL)
651     /* No breakpoints on this machine. */
652     return target_read_memory (memaddr, myaddr, len);
653
654   ALL_BP_LOCATIONS (b)
655   {
656     if (b->owner->type == bp_none)
657       warning (_("reading through apparently deleted breakpoint #%d?"),
658               b->owner->number);
659
660     if (b->loc_type != bp_loc_software_breakpoint)
661       continue;
662     if (!b->inserted)
663       continue;
664     /* Addresses and length of the part of the breakpoint that
665        we need to copy.  */
666     bp_addr = b->target_info.placed_address;
667     bp_size = b->target_info.shadow_len;
668     if (bp_size == 0)
669       /* bp isn't valid, or doesn't shadow memory.  */
670       continue;
671     if (bp_addr + bp_size <= memaddr)
672       /* The breakpoint is entirely before the chunk of memory we
673          are reading.  */
674       continue;
675     if (bp_addr >= memaddr + len)
676       /* The breakpoint is entirely after the chunk of memory we are
677          reading. */
678       continue;
679     /* Copy the breakpoint from the shadow contents, and recurse for
680        the things before and after.  */
681     {
682       /* Offset within shadow_contents.  */
683       int bptoffset = 0;
684
685       if (bp_addr < memaddr)
686         {
687           /* Only copy the second part of the breakpoint.  */
688           bp_size -= memaddr - bp_addr;
689           bptoffset = memaddr - bp_addr;
690           bp_addr = memaddr;
691         }
692
693       if (bp_addr + bp_size > memaddr + len)
694         {
695           /* Only copy the first part of the breakpoint.  */
696           bp_size -= (bp_addr + bp_size) - (memaddr + len);
697         }
698
699       memcpy (myaddr + bp_addr - memaddr,
700               b->target_info.shadow_contents + bptoffset, bp_size);
701
702       if (bp_addr > memaddr)
703         {
704           /* Copy the section of memory before the breakpoint.  */
705           status = deprecated_read_memory_nobpt (memaddr, myaddr, bp_addr - memaddr);
706           if (status != 0)
707             return status;
708         }
709
710       if (bp_addr + bp_size < memaddr + len)
711         {
712           /* Copy the section of memory after the breakpoint.  */
713           status = deprecated_read_memory_nobpt (bp_addr + bp_size,
714                                       myaddr + bp_addr + bp_size - memaddr,
715                                       memaddr + len - (bp_addr + bp_size));
716           if (status != 0)
717             return status;
718         }
719       return 0;
720     }
721   }
722   /* Nothing overlaps.  Just call read_memory_noerr.  */
723   return target_read_memory (memaddr, myaddr, len);
724 }
725 \f
726
727 /* A wrapper function for inserting catchpoints.  */
728 static void
729 insert_catchpoint (struct ui_out *uo, void *args)
730 {
731   struct breakpoint *b = (struct breakpoint *) args;
732   int val = -1;
733
734   switch (b->type)
735     {
736     case bp_catch_fork:
737       target_insert_fork_catchpoint (PIDGET (inferior_ptid));
738       break;
739     case bp_catch_vfork:
740       target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
741       break;
742     case bp_catch_exec:
743       target_insert_exec_catchpoint (PIDGET (inferior_ptid));
744       break;
745     default:
746       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
747       break;
748     }
749 }
750
751 /* Helper routine: free the value chain for a breakpoint (watchpoint).  */
752
753 static void free_valchain (struct bp_location *b)
754 {
755   struct value *v;
756   struct value *n;
757
758   /* Free the saved value chain.  We will construct a new one
759      the next time the watchpoint is inserted.  */
760   for (v = b->owner->val_chain; v; v = n)
761     {
762       n = value_next (v);
763       value_free (v);
764     }
765   b->owner->val_chain = NULL;
766 }
767
768 /* Insert a low-level "breakpoint" of some type.  BPT is the breakpoint.
769    Any error messages are printed to TMP_ERROR_STREAM; and DISABLED_BREAKS,
770    PROCESS_WARNING, and HW_BREAKPOINT_ERROR are used to report problems.
771
772    NOTE drow/2003-09-09: This routine could be broken down to an object-style
773    method for each breakpoint or catchpoint type.  */
774 static int
775 insert_bp_location (struct bp_location *bpt,
776                     struct ui_file *tmp_error_stream,
777                     int *disabled_breaks, int *process_warning,
778                     int *hw_breakpoint_error)
779 {
780   int val = 0;
781
782   /* Permanent breakpoints cannot be inserted or removed.  Disabled
783      breakpoints should not be inserted.  */
784   if (!breakpoint_enabled (bpt->owner))
785     return 0;
786
787   if (bpt->inserted || bpt->duplicate)
788     return 0;
789
790   /* Initialize the target-specific information.  */
791   memset (&bpt->target_info, 0, sizeof (bpt->target_info));
792   bpt->target_info.placed_address = bpt->address;
793
794   if (bpt->loc_type == bp_loc_software_breakpoint
795       || bpt->loc_type == bp_loc_hardware_breakpoint)
796     {
797       /* First check to see if we have to handle an overlay.  */
798       if (overlay_debugging == ovly_off
799           || bpt->section == NULL
800           || !(section_is_overlay (bpt->section)))
801         {
802           /* No overlay handling: just set the breakpoint.  */
803
804           if (bpt->loc_type == bp_loc_hardware_breakpoint)
805             val = target_insert_hw_breakpoint (&bpt->target_info);
806           else
807             val = target_insert_breakpoint (&bpt->target_info);
808         }
809       else
810         {
811           /* This breakpoint is in an overlay section.  
812              Shall we set a breakpoint at the LMA?  */
813           if (!overlay_events_enabled)
814             {
815               /* Yes -- overlay event support is not active, 
816                  so we must try to set a breakpoint at the LMA.
817                  This will not work for a hardware breakpoint.  */
818               if (bpt->loc_type == bp_loc_hardware_breakpoint)
819                 warning (_("hardware breakpoint %d not supported in overlay!"),
820                          bpt->owner->number);
821               else
822                 {
823                   CORE_ADDR addr = overlay_unmapped_address (bpt->address,
824                                                              bpt->section);
825                   /* Set a software (trap) breakpoint at the LMA.  */
826                   bpt->overlay_target_info = bpt->target_info;
827                   bpt->overlay_target_info.placed_address = addr;
828                   val = target_insert_breakpoint (&bpt->overlay_target_info);
829                   if (val != 0)
830                     fprintf_unfiltered (tmp_error_stream, 
831                                         "Overlay breakpoint %d failed: in ROM?", 
832                                         bpt->owner->number);
833                 }
834             }
835           /* Shall we set a breakpoint at the VMA? */
836           if (section_is_mapped (bpt->section))
837             {
838               /* Yes.  This overlay section is mapped into memory.  */
839               if (bpt->loc_type == bp_loc_hardware_breakpoint)
840                 val = target_insert_hw_breakpoint (&bpt->target_info);
841               else
842                 val = target_insert_breakpoint (&bpt->target_info);
843             }
844           else
845             {
846               /* No.  This breakpoint will not be inserted.  
847                  No error, but do not mark the bp as 'inserted'.  */
848               return 0;
849             }
850         }
851
852       if (val)
853         {
854           /* Can't set the breakpoint.  */
855           if (
856 #if defined (DISABLE_UNSETTABLE_BREAK)
857               DISABLE_UNSETTABLE_BREAK (bpt->address)
858 #else
859               solib_address (bpt->address)
860 #endif
861               )
862             {
863               /* See also: disable_breakpoints_in_shlibs. */
864               val = 0;
865               bpt->owner->enable_state = bp_shlib_disabled;
866               if (!*disabled_breaks)
867                 {
868                   fprintf_unfiltered (tmp_error_stream, 
869                                       "Cannot insert breakpoint %d.\n", 
870                                       bpt->owner->number);
871                   fprintf_unfiltered (tmp_error_stream, 
872                                       "Temporarily disabling shared library breakpoints:\n");
873                 }
874               *disabled_breaks = 1;
875               fprintf_unfiltered (tmp_error_stream,
876                                   "breakpoint #%d\n", bpt->owner->number);
877             }
878           else
879             {
880 #ifdef ONE_PROCESS_WRITETEXT
881               *process_warning = 1;
882 #endif
883               if (bpt->loc_type == bp_loc_hardware_breakpoint)
884                 {
885                   *hw_breakpoint_error = 1;
886                   fprintf_unfiltered (tmp_error_stream, 
887                                       "Cannot insert hardware breakpoint %d.\n",
888                                       bpt->owner->number);
889                 }
890               else
891                 {
892                   fprintf_unfiltered (tmp_error_stream, 
893                                       "Cannot insert breakpoint %d.\n", 
894                                       bpt->owner->number);
895                   fprintf_filtered (tmp_error_stream, 
896                                     "Error accessing memory address ");
897                   deprecated_print_address_numeric (bpt->address, 1, tmp_error_stream);
898                   fprintf_filtered (tmp_error_stream, ": %s.\n",
899                                     safe_strerror (val));
900                 }
901
902             }
903         }
904       else
905         bpt->inserted = 1;
906
907       return val;
908     }
909
910   else if (bpt->loc_type == bp_loc_hardware_watchpoint
911            /* NOTE drow/2003-09-08: This state only exists for removing
912               watchpoints.  It's not clear that it's necessary... */
913            && bpt->owner->disposition != disp_del_at_next_stop)
914     {
915       /* FIXME drow/2003-09-08: This code sets multiple hardware watchpoints
916          based on the expression.  Ideally this should happen at a higher level,
917          and there should be one bp_location for each computed address we
918          must watch.  As soon as a many-to-one mapping is available I'll
919          convert this.  */
920
921       int within_current_scope;
922       struct value *mark = value_mark ();
923       struct value *v;
924       struct frame_id saved_frame_id;
925
926       /* Save the current frame's ID so we can restore it after
927          evaluating the watchpoint expression on its own frame.  */
928       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
929          took a frame parameter, so that we didn't have to change the
930          selected frame.  */
931       saved_frame_id = get_frame_id (deprecated_selected_frame);
932
933       /* Determine if the watchpoint is within scope.  */
934       if (bpt->owner->exp_valid_block == NULL)
935         within_current_scope = 1;
936       else
937         {
938           struct frame_info *fi;
939           fi = frame_find_by_id (bpt->owner->watchpoint_frame);
940           within_current_scope = (fi != NULL);
941           if (within_current_scope)
942             select_frame (fi);
943         }
944
945       if (within_current_scope)
946         {
947           free_valchain (bpt);
948
949           /* Evaluate the expression and cut the chain of values
950              produced off from the value chain.
951
952              Make sure the value returned isn't lazy; we use
953              laziness to determine what memory GDB actually needed
954              in order to compute the value of the expression.  */
955           v = evaluate_expression (bpt->owner->exp);
956           value_contents (v);
957           value_release_to_mark (mark);
958
959           bpt->owner->val_chain = v;
960           bpt->inserted = 1;
961
962           /* Look at each value on the value chain.  */
963           for (; v; v = value_next (v))
964             {
965               /* If it's a memory location, and GDB actually needed
966                  its contents to evaluate the expression, then we
967                  must watch it.  */
968               if (VALUE_LVAL (v) == lval_memory
969                   && ! value_lazy (v))
970                 {
971                   struct type *vtype = check_typedef (value_type (v));
972
973                   /* We only watch structs and arrays if user asked
974                      for it explicitly, never if they just happen to
975                      appear in the middle of some value chain.  */
976                   if (v == bpt->owner->val_chain
977                       || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
978                           && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
979                     {
980                       CORE_ADDR addr;
981                       int len, type;
982
983                       addr = VALUE_ADDRESS (v) + value_offset (v);
984                       len = TYPE_LENGTH (value_type (v));
985                       type = hw_write;
986                       if (bpt->owner->type == bp_read_watchpoint)
987                         type = hw_read;
988                       else if (bpt->owner->type == bp_access_watchpoint)
989                         type = hw_access;
990
991                       val = target_insert_watchpoint (addr, len, type);
992                       if (val == -1)
993                         {
994                           /* Don't exit the loop, try to insert
995                              every value on the value chain.  That's
996                              because we will be removing all the
997                              watches below, and removing a
998                              watchpoint we didn't insert could have
999                              adverse effects.  */
1000                           bpt->inserted = 0;
1001                         }
1002                       val = 0;
1003                     }
1004                 }
1005             }
1006           /* Failure to insert a watchpoint on any memory value in the
1007              value chain brings us here.  */
1008           if (!bpt->inserted)
1009             {
1010               remove_breakpoint (bpt, mark_uninserted);
1011               *hw_breakpoint_error = 1;
1012               fprintf_unfiltered (tmp_error_stream,
1013                                   "Could not insert hardware watchpoint %d.\n", 
1014                                   bpt->owner->number);
1015               val = -1;
1016             }               
1017         }
1018       else
1019         {
1020           printf_filtered (_("\
1021 Hardware watchpoint %d deleted because the program has left the block \n\
1022 in which its expression is valid.\n"),
1023                            bpt->owner->number);
1024           if (bpt->owner->related_breakpoint)
1025             bpt->owner->related_breakpoint->disposition = disp_del_at_next_stop;
1026           bpt->owner->disposition = disp_del_at_next_stop;
1027         }
1028
1029       /* Restore the selected frame.  */
1030       select_frame (frame_find_by_id (saved_frame_id));
1031
1032       return val;
1033     }
1034
1035   else if (ep_is_exception_catchpoint (bpt->owner))
1036     {
1037       /* FIXME drow/2003-09-09: This code sets both a catchpoint and a
1038          breakpoint.  Once again, it would be better if this was represented
1039          as two bp_locations.  */
1040
1041       /* If we get here, we must have a callback mechanism for exception
1042          events -- with g++ style embedded label support, we insert
1043          ordinary breakpoints and not catchpoints. */
1044       val = target_insert_breakpoint (&bpt->target_info);
1045       if (val)
1046         {
1047           /* Couldn't set breakpoint for some reason */
1048           fprintf_unfiltered (tmp_error_stream, 
1049                               "Cannot insert catchpoint %d; disabling it.\n",
1050                               bpt->owner->number);
1051           fprintf_filtered (tmp_error_stream, 
1052                             "Error accessing memory address ");
1053           deprecated_print_address_numeric (bpt->address, 1, tmp_error_stream);
1054           fprintf_filtered (tmp_error_stream, ": %s.\n",
1055                             safe_strerror (val));
1056           bpt->owner->enable_state = bp_disabled;
1057         }
1058       else
1059         {
1060           /* Bp set, now make sure callbacks are enabled */
1061           /* Format possible error msg */
1062           char *message = xstrprintf ("Error inserting catchpoint %d:\n",
1063                                       bpt->owner->number);
1064           struct cleanup *cleanups = make_cleanup (xfree, message);
1065           int val;
1066           args_for_catchpoint_enable args;
1067           args.kind = bpt->owner->type == bp_catch_catch ? 
1068             EX_EVENT_CATCH : EX_EVENT_THROW;
1069           args.enable_p = 1;
1070           val = catch_errors (cover_target_enable_exception_callback,
1071                               &args, message, RETURN_MASK_ALL);
1072           do_cleanups (cleanups);
1073           if (val != 0 && val != -1)
1074             bpt->inserted = 1;
1075
1076           /* Check if something went wrong; val == 0 can be ignored */
1077           if (val == -1)
1078             {
1079               /* something went wrong */
1080               fprintf_unfiltered (tmp_error_stream, 
1081                                   "Cannot insert catchpoint %d; disabling it.\n",
1082                                   bpt->owner->number);
1083               bpt->owner->enable_state = bp_disabled;
1084             }
1085         }
1086
1087       return val;
1088     }
1089
1090   else if (bpt->owner->type == bp_catch_fork
1091            || bpt->owner->type == bp_catch_vfork
1092            || bpt->owner->type == bp_catch_exec)
1093     {
1094       struct gdb_exception e = catch_exception (uiout, insert_catchpoint,
1095                                                 bpt->owner, RETURN_MASK_ERROR);
1096       exception_fprintf (gdb_stderr, e, "warning: inserting catchpoint %d: ",
1097                          bpt->owner->number);
1098       if (e.reason < 0)
1099         bpt->owner->enable_state = bp_disabled;
1100       else
1101         bpt->inserted = 1;
1102
1103       /* We've already printed an error message if there was a problem
1104          inserting this catchpoint, and we've disabled the catchpoint,
1105          so just return success.  */
1106       return 0;
1107     }
1108
1109   return 0;
1110 }
1111
1112 /* insert_breakpoints is used when starting or continuing the program.
1113    remove_breakpoints is used when the program stops.
1114    Both return zero if successful,
1115    or an `errno' value if could not write the inferior.  */
1116
1117 int
1118 insert_breakpoints (void)
1119 {
1120   struct bp_location *b, *temp;
1121   int return_val = 0;   /* return success code. */
1122   int val = 0;
1123   int disabled_breaks = 0;
1124   int hw_breakpoint_error = 0;
1125   int process_warning = 0;
1126
1127   struct ui_file *tmp_error_stream = mem_fileopen ();
1128   make_cleanup_ui_file_delete (tmp_error_stream);
1129
1130   /* Explicitly mark the warning -- this will only be printed if
1131      there was an error.  */
1132   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1133
1134   ALL_BP_LOCATIONS_SAFE (b, temp)
1135     {
1136       /* Permanent breakpoints cannot be inserted or removed.  Disabled
1137          breakpoints should not be inserted.  */
1138       if (!breakpoint_enabled (b->owner))
1139         continue;
1140
1141       /* There is no point inserting thread-specific breakpoints if the
1142          thread no longer exists.  */
1143       if (b->owner->thread != -1
1144           && !valid_thread_id (b->owner->thread))
1145         continue;
1146
1147       /* FIXME drow/2003-10-07: This code should be pushed elsewhere when
1148          hardware watchpoints are split into multiple loc breakpoints.  */
1149       if ((b->loc_type == bp_loc_hardware_watchpoint
1150            || b->owner->type == bp_watchpoint) && !b->owner->val)
1151         {
1152           struct value *val;
1153           val = evaluate_expression (b->owner->exp);
1154           release_value (val);
1155           if (value_lazy (val))
1156             value_fetch_lazy (val);
1157           b->owner->val = val;
1158         }
1159
1160       val = insert_bp_location (b, tmp_error_stream,
1161                                     &disabled_breaks, &process_warning,
1162                                     &hw_breakpoint_error);
1163       if (val)
1164         return_val = val;
1165     }
1166
1167   if (return_val)
1168     {
1169       /* If a hardware breakpoint or watchpoint was inserted, add a
1170          message about possibly exhausted resources.  */
1171       if (hw_breakpoint_error)
1172         {
1173           fprintf_unfiltered (tmp_error_stream, 
1174                               "Could not insert hardware breakpoints:\n\
1175 You may have requested too many hardware breakpoints/watchpoints.\n");
1176         }
1177 #ifdef ONE_PROCESS_WRITETEXT
1178       if (process_warning)
1179         fprintf_unfiltered (tmp_error_stream,
1180                             "The same program may be running in another process.");
1181 #endif
1182       target_terminal_ours_for_output ();
1183       error_stream (tmp_error_stream);
1184     }
1185   return return_val;
1186 }
1187
1188 int
1189 remove_breakpoints (void)
1190 {
1191   struct bp_location *b;
1192   int val;
1193
1194   ALL_BP_LOCATIONS (b)
1195   {
1196     if (b->inserted)
1197       {
1198         val = remove_breakpoint (b, mark_uninserted);
1199         if (val != 0)
1200           return val;
1201       }
1202   }
1203   return 0;
1204 }
1205
1206 int
1207 remove_hw_watchpoints (void)
1208 {
1209   struct bp_location *b;
1210   int val;
1211
1212   ALL_BP_LOCATIONS (b)
1213   {
1214     if (b->inserted && b->loc_type == bp_loc_hardware_watchpoint)
1215       {
1216         val = remove_breakpoint (b, mark_uninserted);
1217         if (val != 0)
1218           return val;
1219       }
1220   }
1221   return 0;
1222 }
1223
1224 int
1225 reattach_breakpoints (int pid)
1226 {
1227   struct bp_location *b;
1228   int val;
1229   struct cleanup *old_chain = save_inferior_ptid ();
1230
1231   /* Set inferior_ptid; remove_breakpoint uses this global.  */
1232   inferior_ptid = pid_to_ptid (pid);
1233   ALL_BP_LOCATIONS (b)
1234   {
1235     if (b->inserted)
1236       {
1237         remove_breakpoint (b, mark_inserted);
1238         if (b->loc_type == bp_loc_hardware_breakpoint)
1239           val = target_insert_hw_breakpoint (&b->target_info);
1240         else
1241           val = target_insert_breakpoint (&b->target_info);
1242         /* FIXME drow/2003-10-07: This doesn't handle any other kinds of
1243            breakpoints.  It's wrong for watchpoints, for example.  */
1244         if (val != 0)
1245           {
1246             do_cleanups (old_chain);
1247             return val;
1248           }
1249       }
1250   }
1251   do_cleanups (old_chain);
1252   return 0;
1253 }
1254
1255 void
1256 update_breakpoints_after_exec (void)
1257 {
1258   struct breakpoint *b;
1259   struct breakpoint *temp;
1260
1261   /* Doing this first prevents the badness of having delete_breakpoint()
1262      write a breakpoint's current "shadow contents" to lift the bp.  That
1263      shadow is NOT valid after an exec()! */
1264   mark_breakpoints_out ();
1265
1266   ALL_BREAKPOINTS_SAFE (b, temp)
1267   {
1268     /* Solib breakpoints must be explicitly reset after an exec(). */
1269     if (b->type == bp_shlib_event)
1270       {
1271         delete_breakpoint (b);
1272         continue;
1273       }
1274
1275     /* Thread event breakpoints must be set anew after an exec(),
1276        as must overlay event breakpoints.  */
1277     if (b->type == bp_thread_event || b->type == bp_overlay_event)
1278       {
1279         delete_breakpoint (b);
1280         continue;
1281       }
1282
1283     /* Step-resume breakpoints are meaningless after an exec(). */
1284     if (b->type == bp_step_resume)
1285       {
1286         delete_breakpoint (b);
1287         continue;
1288       }
1289
1290     /* Ditto the sigtramp handler breakpoints. */
1291     if (b->type == bp_through_sigtramp)
1292       {
1293         delete_breakpoint (b);
1294         continue;
1295       }
1296
1297     /* Ditto the exception-handling catchpoints. */
1298     if ((b->type == bp_catch_catch) || (b->type == bp_catch_throw))
1299       {
1300         delete_breakpoint (b);
1301         continue;
1302       }
1303
1304     /* Don't delete an exec catchpoint, because else the inferior
1305        won't stop when it ought!
1306
1307        Similarly, we probably ought to keep vfork catchpoints, 'cause
1308        on this target, we may not be able to stop when the vfork is
1309        seen, but only when the subsequent exec is seen.  (And because
1310        deleting fork catchpoints here but not vfork catchpoints will
1311        seem mysterious to users, keep those too.)
1312
1313        ??rehrauer: Let's hope that merely clearing out this catchpoint's
1314        target address field, if any, is sufficient to have it be reset
1315        automagically.  Certainly on HP-UX that's true.
1316
1317        Jim Blandy <jimb@redhat.com>: Actually, zero is a perfectly
1318        valid code address on some platforms (like the mn10300
1319        simulators).  We shouldn't assign any special interpretation to
1320        a breakpoint with a zero address.  And in fact, GDB doesn't ---
1321        I can't see what that comment above is talking about.  As far
1322        as I can tell, setting the address of a
1323        bp_catch_exec/bp_catch_vfork/bp_catch_fork breakpoint to zero
1324        is meaningless, since those are implemented with HP-UX kernel
1325        hackery, not by storing breakpoint instructions somewhere.  */
1326     if ((b->type == bp_catch_exec) ||
1327         (b->type == bp_catch_vfork) ||
1328         (b->type == bp_catch_fork))
1329       {
1330         b->loc->address = (CORE_ADDR) NULL;
1331         continue;
1332       }
1333
1334     /* bp_finish is a special case.  The only way we ought to be able
1335        to see one of these when an exec() has happened, is if the user
1336        caught a vfork, and then said "finish".  Ordinarily a finish just
1337        carries them to the call-site of the current callee, by setting
1338        a temporary bp there and resuming.  But in this case, the finish
1339        will carry them entirely through the vfork & exec.
1340
1341        We don't want to allow a bp_finish to remain inserted now.  But
1342        we can't safely delete it, 'cause finish_command has a handle to
1343        the bp on a bpstat, and will later want to delete it.  There's a
1344        chance (and I've seen it happen) that if we delete the bp_finish
1345        here, that its storage will get reused by the time finish_command
1346        gets 'round to deleting the "use to be a bp_finish" breakpoint.
1347        We really must allow finish_command to delete a bp_finish.
1348
1349        In the absense of a general solution for the "how do we know
1350        it's safe to delete something others may have handles to?"
1351        problem, what we'll do here is just uninsert the bp_finish, and
1352        let finish_command delete it.
1353
1354        (We know the bp_finish is "doomed" in the sense that it's
1355        momentary, and will be deleted as soon as finish_command sees
1356        the inferior stopped.  So it doesn't matter that the bp's
1357        address is probably bogus in the new a.out, unlike e.g., the
1358        solib breakpoints.)  */
1359
1360     if (b->type == bp_finish)
1361       {
1362         continue;
1363       }
1364
1365     /* Without a symbolic address, we have little hope of the
1366        pre-exec() address meaning the same thing in the post-exec()
1367        a.out. */
1368     if (b->addr_string == NULL)
1369       {
1370         delete_breakpoint (b);
1371         continue;
1372       }
1373
1374     /* If this breakpoint has survived the above battery of checks, then
1375        it must have a symbolic address.  Be sure that it gets reevaluated
1376        to a target address, rather than reusing the old evaluation.
1377
1378        Jim Blandy <jimb@redhat.com>: As explained above in the comment
1379        for bp_catch_exec and friends, I'm pretty sure this is entirely
1380        unnecessary.  A call to breakpoint_re_set_one always recomputes
1381        the breakpoint's address from scratch, or deletes it if it can't.
1382        So I think this assignment could be deleted without effect.  */
1383     b->loc->address = (CORE_ADDR) NULL;
1384   }
1385   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
1386   create_overlay_event_breakpoint ("_ovly_debug_event");
1387 }
1388
1389 int
1390 detach_breakpoints (int pid)
1391 {
1392   struct bp_location *b;
1393   int val;
1394   struct cleanup *old_chain = save_inferior_ptid ();
1395
1396   if (pid == PIDGET (inferior_ptid))
1397     error (_("Cannot detach breakpoints of inferior_ptid"));
1398
1399   /* Set inferior_ptid; remove_breakpoint uses this global.  */
1400   inferior_ptid = pid_to_ptid (pid);
1401   ALL_BP_LOCATIONS (b)
1402   {
1403     if (b->inserted)
1404       {
1405         val = remove_breakpoint (b, mark_inserted);
1406         if (val != 0)
1407           {
1408             do_cleanups (old_chain);
1409             return val;
1410           }
1411       }
1412   }
1413   do_cleanups (old_chain);
1414   return 0;
1415 }
1416
1417 static int
1418 remove_breakpoint (struct bp_location *b, insertion_state_t is)
1419 {
1420   int val;
1421
1422   if (b->owner->enable_state == bp_permanent)
1423     /* Permanent breakpoints cannot be inserted or removed.  */
1424     return 0;
1425
1426   if (b->owner->type == bp_none)
1427     warning (_("attempted to remove apparently deleted breakpoint #%d?"), 
1428              b->owner->number);
1429
1430   if (b->loc_type == bp_loc_software_breakpoint
1431       || b->loc_type == bp_loc_hardware_breakpoint)
1432     {
1433       /* "Normal" instruction breakpoint: either the standard
1434          trap-instruction bp (bp_breakpoint), or a
1435          bp_hardware_breakpoint.  */
1436
1437       /* First check to see if we have to handle an overlay.  */
1438       if (overlay_debugging == ovly_off
1439           || b->section == NULL
1440           || !(section_is_overlay (b->section)))
1441         {
1442           /* No overlay handling: just remove the breakpoint.  */
1443
1444           if (b->loc_type == bp_loc_hardware_breakpoint)
1445             val = target_remove_hw_breakpoint (&b->target_info);
1446           else
1447             val = target_remove_breakpoint (&b->target_info);
1448         }
1449       else
1450         {
1451           /* This breakpoint is in an overlay section.  
1452              Did we set a breakpoint at the LMA?  */
1453           if (!overlay_events_enabled)
1454               {
1455                 /* Yes -- overlay event support is not active, so we
1456                    should have set a breakpoint at the LMA.  Remove it.  
1457                 */
1458                 /* Ignore any failures: if the LMA is in ROM, we will
1459                    have already warned when we failed to insert it.  */
1460                 if (b->loc_type == bp_loc_hardware_breakpoint)
1461                   target_remove_hw_breakpoint (&b->overlay_target_info);
1462                 else
1463                   target_remove_breakpoint (&b->overlay_target_info);
1464               }
1465           /* Did we set a breakpoint at the VMA? 
1466              If so, we will have marked the breakpoint 'inserted'.  */
1467           if (b->inserted)
1468             {
1469               /* Yes -- remove it.  Previously we did not bother to
1470                  remove the breakpoint if the section had been
1471                  unmapped, but let's not rely on that being safe.  We
1472                  don't know what the overlay manager might do.  */
1473               if (b->loc_type == bp_loc_hardware_breakpoint)
1474                 val = target_remove_hw_breakpoint (&b->target_info);
1475               else
1476                 val = target_remove_breakpoint (&b->target_info);
1477             }
1478           else
1479             {
1480               /* No -- not inserted, so no need to remove.  No error.  */
1481               val = 0;
1482             }
1483         }
1484       if (val)
1485         return val;
1486       b->inserted = (is == mark_inserted);
1487     }
1488   else if (b->loc_type == bp_loc_hardware_watchpoint
1489            && breakpoint_enabled (b->owner)
1490            && !b->duplicate)
1491     {
1492       struct value *v;
1493       struct value *n;
1494
1495       b->inserted = (is == mark_inserted);
1496       /* Walk down the saved value chain.  */
1497       for (v = b->owner->val_chain; v; v = value_next (v))
1498         {
1499           /* For each memory reference remove the watchpoint
1500              at that address.  */
1501           if (VALUE_LVAL (v) == lval_memory
1502               && ! value_lazy (v))
1503             {
1504               struct type *vtype = check_typedef (value_type (v));
1505
1506               if (v == b->owner->val_chain
1507                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1508                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1509                 {
1510                   CORE_ADDR addr;
1511                   int len, type;
1512
1513                   addr = VALUE_ADDRESS (v) + value_offset (v);
1514                   len = TYPE_LENGTH (value_type (v));
1515                   type   = hw_write;
1516                   if (b->owner->type == bp_read_watchpoint)
1517                     type = hw_read;
1518                   else if (b->owner->type == bp_access_watchpoint)
1519                     type = hw_access;
1520
1521                   val = target_remove_watchpoint (addr, len, type);
1522                   if (val == -1)
1523                     b->inserted = 1;
1524                   val = 0;
1525                 }
1526             }
1527         }
1528       /* Failure to remove any of the hardware watchpoints comes here.  */
1529       if ((is == mark_uninserted) && (b->inserted))
1530         warning (_("Could not remove hardware watchpoint %d."),
1531                  b->owner->number);
1532     }
1533   else if ((b->owner->type == bp_catch_fork ||
1534             b->owner->type == bp_catch_vfork ||
1535             b->owner->type == bp_catch_exec)
1536            && breakpoint_enabled (b->owner)
1537            && !b->duplicate)
1538     {
1539       val = -1;
1540       switch (b->owner->type)
1541         {
1542         case bp_catch_fork:
1543           val = target_remove_fork_catchpoint (PIDGET (inferior_ptid));
1544           break;
1545         case bp_catch_vfork:
1546           val = target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
1547           break;
1548         case bp_catch_exec:
1549           val = target_remove_exec_catchpoint (PIDGET (inferior_ptid));
1550           break;
1551         default:
1552           warning (_("Internal error, %s line %d."), __FILE__, __LINE__);
1553           break;
1554         }
1555       if (val)
1556         return val;
1557       b->inserted = (is == mark_inserted);
1558     }
1559   else if ((b->owner->type == bp_catch_catch ||
1560             b->owner->type == bp_catch_throw)
1561            && breakpoint_enabled (b->owner)
1562            && !b->duplicate)
1563     {
1564       val = target_remove_breakpoint (&b->target_info);
1565       if (val)
1566         return val;
1567       b->inserted = (is == mark_inserted);
1568     }
1569   else if (ep_is_exception_catchpoint (b->owner)
1570            && b->inserted       /* sometimes previous insert doesn't happen */
1571            && breakpoint_enabled (b->owner)
1572            && !b->duplicate)
1573     {
1574       val = target_remove_breakpoint (&b->target_info);
1575       if (val)
1576         return val;
1577
1578       b->inserted = (is == mark_inserted);
1579     }
1580
1581   return 0;
1582 }
1583
1584 /* Clear the "inserted" flag in all breakpoints.  */
1585
1586 void
1587 mark_breakpoints_out (void)
1588 {
1589   struct bp_location *bpt;
1590
1591   ALL_BP_LOCATIONS (bpt)
1592     bpt->inserted = 0;
1593 }
1594
1595 /* Clear the "inserted" flag in all breakpoints and delete any
1596    breakpoints which should go away between runs of the program.
1597
1598    Plus other such housekeeping that has to be done for breakpoints
1599    between runs.
1600
1601    Note: this function gets called at the end of a run (by
1602    generic_mourn_inferior) and when a run begins (by
1603    init_wait_for_inferior). */
1604
1605
1606
1607 void
1608 breakpoint_init_inferior (enum inf_context context)
1609 {
1610   struct breakpoint *b, *temp;
1611   struct bp_location *bpt;
1612   static int warning_needed = 0;
1613
1614   ALL_BP_LOCATIONS (bpt)
1615     bpt->inserted = 0;
1616
1617   ALL_BREAKPOINTS_SAFE (b, temp)
1618   {
1619     switch (b->type)
1620       {
1621       case bp_call_dummy:
1622       case bp_watchpoint_scope:
1623
1624         /* If the call dummy breakpoint is at the entry point it will
1625            cause problems when the inferior is rerun, so we better
1626            get rid of it. 
1627
1628            Also get rid of scope breakpoints.  */
1629         delete_breakpoint (b);
1630         break;
1631
1632       case bp_watchpoint:
1633       case bp_hardware_watchpoint:
1634       case bp_read_watchpoint:
1635       case bp_access_watchpoint:
1636
1637         /* Likewise for watchpoints on local expressions.  */
1638         if (b->exp_valid_block != NULL)
1639           delete_breakpoint (b);
1640         if (context == inf_starting) 
1641           {
1642             /* Reset val field to force reread of starting value
1643                in insert_breakpoints.  */
1644             if (b->val)
1645               value_free (b->val);
1646             b->val = NULL;
1647           }
1648         break;
1649       default:
1650         /* Likewise for exception catchpoints in dynamic-linked
1651            executables where required */
1652         if (ep_is_exception_catchpoint (b)
1653             && deprecated_exception_catchpoints_are_fragile)
1654           {
1655             warning_needed = 1;
1656             delete_breakpoint (b);
1657           }
1658         break;
1659       }
1660   }
1661
1662   if (deprecated_exception_catchpoints_are_fragile)
1663     deprecated_exception_support_initialized = 0;
1664
1665   /* Don't issue the warning unless it's really needed... */
1666   if (warning_needed && (context != inf_exited))
1667     {
1668       warning (_("Exception catchpoints from last run were deleted.\n"
1669                  "You must reinsert them explicitly."));
1670       warning_needed = 0;
1671     }
1672 }
1673
1674 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
1675    exists at PC.  It returns ordinary_breakpoint_here if it's an
1676    ordinary breakpoint, or permanent_breakpoint_here if it's a
1677    permanent breakpoint.
1678    - When continuing from a location with an ordinary breakpoint, we
1679      actually single step once before calling insert_breakpoints.
1680    - When continuing from a localion with a permanent breakpoint, we
1681      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
1682      the target, to advance the PC past the breakpoint.  */
1683
1684 enum breakpoint_here
1685 breakpoint_here_p (CORE_ADDR pc)
1686 {
1687   struct bp_location *bpt;
1688   int any_breakpoint_here = 0;
1689
1690   ALL_BP_LOCATIONS (bpt)
1691     {
1692       if (bpt->loc_type != bp_loc_software_breakpoint
1693           && bpt->loc_type != bp_loc_hardware_breakpoint)
1694         continue;
1695
1696       if ((breakpoint_enabled (bpt->owner)
1697            || bpt->owner->enable_state == bp_permanent)
1698           && bpt->address == pc)        /* bp is enabled and matches pc */
1699         {
1700           if (overlay_debugging 
1701               && section_is_overlay (bpt->section) 
1702               && !section_is_mapped (bpt->section))
1703             continue;           /* unmapped overlay -- can't be a match */
1704           else if (bpt->owner->enable_state == bp_permanent)
1705             return permanent_breakpoint_here;
1706           else
1707             any_breakpoint_here = 1;
1708         }
1709     }
1710
1711   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
1712 }
1713
1714
1715 /* breakpoint_inserted_here_p (PC) is just like breakpoint_here_p(),
1716    but it only returns true if there is actually a breakpoint inserted
1717    at PC.  */
1718
1719 int
1720 breakpoint_inserted_here_p (CORE_ADDR pc)
1721 {
1722   struct bp_location *bpt;
1723
1724   ALL_BP_LOCATIONS (bpt)
1725     {
1726       if (bpt->loc_type != bp_loc_software_breakpoint
1727           && bpt->loc_type != bp_loc_hardware_breakpoint)
1728         continue;
1729
1730       if (bpt->inserted
1731           && bpt->address == pc)        /* bp is inserted and matches pc */
1732         {
1733           if (overlay_debugging 
1734               && section_is_overlay (bpt->section) 
1735               && !section_is_mapped (bpt->section))
1736             continue;           /* unmapped overlay -- can't be a match */
1737           else
1738             return 1;
1739         }
1740     }
1741
1742   return 0;
1743 }
1744
1745 /* This function returns non-zero iff there is a software breakpoint
1746    inserted at PC.  */
1747
1748 int
1749 software_breakpoint_inserted_here_p (CORE_ADDR pc)
1750 {
1751   struct bp_location *bpt;
1752   int any_breakpoint_here = 0;
1753
1754   ALL_BP_LOCATIONS (bpt)
1755     {
1756       if (bpt->loc_type != bp_loc_software_breakpoint)
1757         continue;
1758
1759       if ((breakpoint_enabled (bpt->owner)
1760            || bpt->owner->enable_state == bp_permanent)
1761           && bpt->inserted
1762           && bpt->address == pc)        /* bp is enabled and matches pc */
1763         {
1764           if (overlay_debugging 
1765               && section_is_overlay (bpt->section) 
1766               && !section_is_mapped (bpt->section))
1767             continue;           /* unmapped overlay -- can't be a match */
1768           else
1769             return 1;
1770         }
1771     }
1772
1773   return 0;
1774 }
1775
1776 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
1777    PC is valid for process/thread PTID.  */
1778
1779 int
1780 breakpoint_thread_match (CORE_ADDR pc, ptid_t ptid)
1781 {
1782   struct bp_location *bpt;
1783   int thread;
1784
1785   thread = pid_to_thread_id (ptid);
1786
1787   ALL_BP_LOCATIONS (bpt)
1788     {
1789       if (bpt->loc_type != bp_loc_software_breakpoint
1790           && bpt->loc_type != bp_loc_hardware_breakpoint)
1791         continue;
1792
1793       if ((breakpoint_enabled (bpt->owner)
1794            || bpt->owner->enable_state == bp_permanent)
1795           && bpt->address == pc
1796           && (bpt->owner->thread == -1 || bpt->owner->thread == thread))
1797         {
1798           if (overlay_debugging 
1799               && section_is_overlay (bpt->section) 
1800               && !section_is_mapped (bpt->section))
1801             continue;           /* unmapped overlay -- can't be a match */
1802           else
1803             return 1;
1804         }
1805     }
1806
1807   return 0;
1808 }
1809 \f
1810
1811 /* bpstat stuff.  External routines' interfaces are documented
1812    in breakpoint.h.  */
1813
1814 int
1815 ep_is_catchpoint (struct breakpoint *ep)
1816 {
1817   return
1818     (ep->type == bp_catch_load)
1819     || (ep->type == bp_catch_unload)
1820     || (ep->type == bp_catch_fork)
1821     || (ep->type == bp_catch_vfork)
1822     || (ep->type == bp_catch_exec)
1823     || (ep->type == bp_catch_catch)
1824     || (ep->type == bp_catch_throw);
1825
1826   /* ??rehrauer: Add more kinds here, as are implemented... */
1827 }
1828
1829 int
1830 ep_is_shlib_catchpoint (struct breakpoint *ep)
1831 {
1832   return
1833     (ep->type == bp_catch_load)
1834     || (ep->type == bp_catch_unload);
1835 }
1836
1837 int
1838 ep_is_exception_catchpoint (struct breakpoint *ep)
1839 {
1840   return
1841     (ep->type == bp_catch_catch)
1842     || (ep->type == bp_catch_throw);
1843 }
1844
1845 /* Clear a bpstat so that it says we are not at any breakpoint.
1846    Also free any storage that is part of a bpstat.  */
1847
1848 void
1849 bpstat_clear (bpstat *bsp)
1850 {
1851   bpstat p;
1852   bpstat q;
1853
1854   if (bsp == 0)
1855     return;
1856   p = *bsp;
1857   while (p != NULL)
1858     {
1859       q = p->next;
1860       if (p->old_val != NULL)
1861         value_free (p->old_val);
1862       free_command_lines (&p->commands);
1863       xfree (p);
1864       p = q;
1865     }
1866   *bsp = NULL;
1867 }
1868
1869 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
1870    is part of the bpstat is copied as well.  */
1871
1872 bpstat
1873 bpstat_copy (bpstat bs)
1874 {
1875   bpstat p = NULL;
1876   bpstat tmp;
1877   bpstat retval = NULL;
1878
1879   if (bs == NULL)
1880     return bs;
1881
1882   for (; bs != NULL; bs = bs->next)
1883     {
1884       tmp = (bpstat) xmalloc (sizeof (*tmp));
1885       memcpy (tmp, bs, sizeof (*tmp));
1886       if (bs->commands != NULL)
1887         tmp->commands = copy_command_lines (bs->commands);
1888       if (bs->old_val != NULL)
1889         tmp->old_val = value_copy (bs->old_val);
1890
1891       if (p == NULL)
1892         /* This is the first thing in the chain.  */
1893         retval = tmp;
1894       else
1895         p->next = tmp;
1896       p = tmp;
1897     }
1898   p->next = NULL;
1899   return retval;
1900 }
1901
1902 /* Find the bpstat associated with this breakpoint */
1903
1904 bpstat
1905 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
1906 {
1907   if (bsp == NULL)
1908     return NULL;
1909
1910   for (; bsp != NULL; bsp = bsp->next)
1911     {
1912       if (bsp->breakpoint_at == breakpoint)
1913         return bsp;
1914     }
1915   return NULL;
1916 }
1917
1918 /* Find a step_resume breakpoint associated with this bpstat.
1919    (If there are multiple step_resume bp's on the list, this function
1920    will arbitrarily pick one.)
1921
1922    It is an error to use this function if BPSTAT doesn't contain a
1923    step_resume breakpoint.
1924
1925    See wait_for_inferior's use of this function.  */
1926 struct breakpoint *
1927 bpstat_find_step_resume_breakpoint (bpstat bsp)
1928 {
1929   int current_thread;
1930
1931   gdb_assert (bsp != NULL);
1932
1933   current_thread = pid_to_thread_id (inferior_ptid);
1934
1935   for (; bsp != NULL; bsp = bsp->next)
1936     {
1937       if ((bsp->breakpoint_at != NULL) &&
1938           (bsp->breakpoint_at->type == bp_step_resume) &&
1939           (bsp->breakpoint_at->thread == current_thread || 
1940            bsp->breakpoint_at->thread == -1))
1941         return bsp->breakpoint_at;
1942     }
1943
1944   internal_error (__FILE__, __LINE__, _("No step_resume breakpoint found."));
1945 }
1946
1947
1948 /* Return the breakpoint number of the first breakpoint we are stopped
1949    at.  *BSP upon return is a bpstat which points to the remaining
1950    breakpoints stopped at (but which is not guaranteed to be good for
1951    anything but further calls to bpstat_num).
1952    Return 0 if passed a bpstat which does not indicate any breakpoints.  */
1953
1954 int
1955 bpstat_num (bpstat *bsp)
1956 {
1957   struct breakpoint *b;
1958
1959   if ((*bsp) == NULL)
1960     return 0;                   /* No more breakpoint values */
1961   else
1962     {
1963       b = (*bsp)->breakpoint_at;
1964       *bsp = (*bsp)->next;
1965       if (b == NULL)
1966         return -1;              /* breakpoint that's been deleted since */
1967       else
1968         return b->number;       /* We have its number */
1969     }
1970 }
1971
1972 /* Modify BS so that the actions will not be performed.  */
1973
1974 void
1975 bpstat_clear_actions (bpstat bs)
1976 {
1977   for (; bs != NULL; bs = bs->next)
1978     {
1979       free_command_lines (&bs->commands);
1980       if (bs->old_val != NULL)
1981         {
1982           value_free (bs->old_val);
1983           bs->old_val = NULL;
1984         }
1985     }
1986 }
1987
1988 /* Stub for cleaning up our state if we error-out of a breakpoint command */
1989 static void
1990 cleanup_executing_breakpoints (void *ignore)
1991 {
1992   executing_breakpoint_commands = 0;
1993 }
1994
1995 /* Execute all the commands associated with all the breakpoints at this
1996    location.  Any of these commands could cause the process to proceed
1997    beyond this point, etc.  We look out for such changes by checking
1998    the global "breakpoint_proceeded" after each command.  */
1999
2000 void
2001 bpstat_do_actions (bpstat *bsp)
2002 {
2003   bpstat bs;
2004   struct cleanup *old_chain;
2005
2006   /* Avoid endless recursion if a `source' command is contained
2007      in bs->commands.  */
2008   if (executing_breakpoint_commands)
2009     return;
2010
2011   executing_breakpoint_commands = 1;
2012   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
2013
2014 top:
2015   /* Note that (as of this writing), our callers all appear to
2016      be passing us the address of global stop_bpstat.  And, if
2017      our calls to execute_control_command cause the inferior to
2018      proceed, that global (and hence, *bsp) will change.
2019
2020      We must be careful to not touch *bsp unless the inferior
2021      has not proceeded. */
2022
2023   /* This pointer will iterate over the list of bpstat's. */
2024   bs = *bsp;
2025
2026   breakpoint_proceeded = 0;
2027   for (; bs != NULL; bs = bs->next)
2028     {
2029       struct command_line *cmd;
2030       struct cleanup *this_cmd_tree_chain;
2031
2032       /* Take ownership of the BSP's command tree, if it has one.
2033
2034          The command tree could legitimately contain commands like
2035          'step' and 'next', which call clear_proceed_status, which
2036          frees stop_bpstat's command tree.  To make sure this doesn't
2037          free the tree we're executing out from under us, we need to
2038          take ownership of the tree ourselves.  Since a given bpstat's
2039          commands are only executed once, we don't need to copy it; we
2040          can clear the pointer in the bpstat, and make sure we free
2041          the tree when we're done.  */
2042       cmd = bs->commands;
2043       bs->commands = 0;
2044       this_cmd_tree_chain = make_cleanup_free_command_lines (&cmd);
2045
2046       while (cmd != NULL)
2047         {
2048           execute_control_command (cmd);
2049
2050           if (breakpoint_proceeded)
2051             break;
2052           else
2053             cmd = cmd->next;
2054         }
2055
2056       /* We can free this command tree now.  */
2057       do_cleanups (this_cmd_tree_chain);
2058
2059       if (breakpoint_proceeded)
2060         /* The inferior is proceeded by the command; bomb out now.
2061            The bpstat chain has been blown away by wait_for_inferior.
2062            But since execution has stopped again, there is a new bpstat
2063            to look at, so start over.  */
2064         goto top;
2065     }
2066   do_cleanups (old_chain);
2067 }
2068
2069 /* This is the normal print function for a bpstat.  In the future,
2070    much of this logic could (should?) be moved to bpstat_stop_status,
2071    by having it set different print_it values.
2072
2073    Current scheme: When we stop, bpstat_print() is called.  It loops
2074    through the bpstat list of things causing this stop, calling the
2075    print_bp_stop_message function on each one. The behavior of the
2076    print_bp_stop_message function depends on the print_it field of
2077    bpstat. If such field so indicates, call this function here.
2078
2079    Return values from this routine (ultimately used by bpstat_print()
2080    and normal_stop() to decide what to do): 
2081    PRINT_NOTHING: Means we already printed all we needed to print,
2082    don't print anything else.
2083    PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
2084    that something to be followed by a location.
2085    PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
2086    that something to be followed by a location.
2087    PRINT_UNKNOWN: Means we printed nothing or we need to do some more
2088    analysis.  */
2089
2090 static enum print_stop_action
2091 print_it_typical (bpstat bs)
2092 {
2093   struct cleanup *old_chain, *ui_out_chain;
2094   struct ui_stream *stb;
2095   stb = ui_out_stream_new (uiout);
2096   old_chain = make_cleanup_ui_out_stream_delete (stb);
2097   /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
2098      which has since been deleted.  */
2099   if (bs->breakpoint_at == NULL)
2100     return PRINT_UNKNOWN;
2101
2102   switch (bs->breakpoint_at->type)
2103     {
2104     case bp_breakpoint:
2105     case bp_hardware_breakpoint:
2106       if (bs->breakpoint_at->loc->address != bs->breakpoint_at->loc->requested_address)
2107         breakpoint_adjustment_warning (bs->breakpoint_at->loc->requested_address,
2108                                        bs->breakpoint_at->loc->address,
2109                                        bs->breakpoint_at->number, 1);
2110       annotate_breakpoint (bs->breakpoint_at->number);
2111       ui_out_text (uiout, "\nBreakpoint ");
2112       if (ui_out_is_mi_like_p (uiout))
2113         ui_out_field_string (uiout, "reason", 
2114                              async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
2115       ui_out_field_int (uiout, "bkptno", bs->breakpoint_at->number);
2116       ui_out_text (uiout, ", ");
2117       return PRINT_SRC_AND_LOC;
2118       break;
2119
2120     case bp_shlib_event:
2121       /* Did we stop because the user set the stop_on_solib_events
2122          variable?  (If so, we report this as a generic, "Stopped due
2123          to shlib event" message.) */
2124       printf_filtered (_("Stopped due to shared library event\n"));
2125       return PRINT_NOTHING;
2126       break;
2127
2128     case bp_thread_event:
2129       /* Not sure how we will get here. 
2130          GDB should not stop for these breakpoints.  */
2131       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
2132       return PRINT_NOTHING;
2133       break;
2134
2135     case bp_overlay_event:
2136       /* By analogy with the thread event, GDB should not stop for these. */
2137       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
2138       return PRINT_NOTHING;
2139       break;
2140
2141     case bp_catch_load:
2142       annotate_catchpoint (bs->breakpoint_at->number);
2143       printf_filtered (_("\nCatchpoint %d (loaded %s), "),
2144                        bs->breakpoint_at->number,
2145                        bs->breakpoint_at->triggered_dll_pathname);
2146       return PRINT_SRC_AND_LOC;
2147       break;
2148
2149     case bp_catch_unload:
2150       annotate_catchpoint (bs->breakpoint_at->number);
2151       printf_filtered (_("\nCatchpoint %d (unloaded %s), "),
2152                        bs->breakpoint_at->number,
2153                        bs->breakpoint_at->triggered_dll_pathname);
2154       return PRINT_SRC_AND_LOC;
2155       break;
2156
2157     case bp_catch_fork:
2158       annotate_catchpoint (bs->breakpoint_at->number);
2159       printf_filtered (_("\nCatchpoint %d (forked process %d), "),
2160                        bs->breakpoint_at->number, 
2161                        bs->breakpoint_at->forked_inferior_pid);
2162       return PRINT_SRC_AND_LOC;
2163       break;
2164
2165     case bp_catch_vfork:
2166       annotate_catchpoint (bs->breakpoint_at->number);
2167       printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
2168                        bs->breakpoint_at->number, 
2169                        bs->breakpoint_at->forked_inferior_pid);
2170       return PRINT_SRC_AND_LOC;
2171       break;
2172
2173     case bp_catch_exec:
2174       annotate_catchpoint (bs->breakpoint_at->number);
2175       printf_filtered (_("\nCatchpoint %d (exec'd %s), "),
2176                        bs->breakpoint_at->number,
2177                        bs->breakpoint_at->exec_pathname);
2178       return PRINT_SRC_AND_LOC;
2179       break;
2180
2181     case bp_catch_catch:
2182       if (current_exception_event && 
2183           (CURRENT_EXCEPTION_KIND == EX_EVENT_CATCH))
2184         {
2185           annotate_catchpoint (bs->breakpoint_at->number);
2186           printf_filtered (_("\nCatchpoint %d (exception caught), "), 
2187                            bs->breakpoint_at->number);
2188           if (CURRENT_EXCEPTION_THROW_PC && CURRENT_EXCEPTION_THROW_LINE)
2189             printf_filtered (_("throw location %s:%d, "),
2190                              CURRENT_EXCEPTION_THROW_FILE,
2191                              CURRENT_EXCEPTION_THROW_LINE);
2192           else
2193             printf_filtered (_("throw location unknown, "));
2194
2195           if (CURRENT_EXCEPTION_CATCH_PC && CURRENT_EXCEPTION_CATCH_LINE)
2196             printf_filtered (_("catch location %s:%d\n"),
2197                              CURRENT_EXCEPTION_CATCH_FILE,
2198                              CURRENT_EXCEPTION_CATCH_LINE);
2199           else
2200             printf_filtered (_("catch location unknown\n"));
2201
2202           /* don't bother to print location frame info */
2203           return PRINT_SRC_ONLY;
2204         }
2205       else
2206         {
2207           /* really throw, some other bpstat will handle it */
2208           return PRINT_UNKNOWN; 
2209         }
2210       break;
2211
2212     case bp_catch_throw:
2213       if (current_exception_event && 
2214           (CURRENT_EXCEPTION_KIND == EX_EVENT_THROW))
2215         {
2216           annotate_catchpoint (bs->breakpoint_at->number);
2217           printf_filtered (_("\nCatchpoint %d (exception thrown), "),
2218                            bs->breakpoint_at->number);
2219           if (CURRENT_EXCEPTION_THROW_PC && CURRENT_EXCEPTION_THROW_LINE)
2220             printf_filtered (_("throw location %s:%d, "),
2221                              CURRENT_EXCEPTION_THROW_FILE,
2222                              CURRENT_EXCEPTION_THROW_LINE);
2223           else
2224             printf_filtered (_("throw location unknown, "));
2225
2226           if (CURRENT_EXCEPTION_CATCH_PC && CURRENT_EXCEPTION_CATCH_LINE)
2227             printf_filtered (_("catch location %s:%d\n"),
2228                              CURRENT_EXCEPTION_CATCH_FILE,
2229                              CURRENT_EXCEPTION_CATCH_LINE);
2230           else
2231             printf_filtered (_("catch location unknown\n"));
2232
2233           /* don't bother to print location frame info */
2234           return PRINT_SRC_ONLY; 
2235         }
2236       else
2237         {
2238           /* really catch, some other bpstat will handle it */
2239           return PRINT_UNKNOWN; 
2240         }
2241       break;
2242
2243     case bp_watchpoint:
2244     case bp_hardware_watchpoint:
2245       if (bs->old_val != NULL)
2246         {
2247           annotate_watchpoint (bs->breakpoint_at->number);
2248           if (ui_out_is_mi_like_p (uiout))
2249             ui_out_field_string
2250               (uiout, "reason",
2251                async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
2252           mention (bs->breakpoint_at);
2253           ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2254           ui_out_text (uiout, "\nOld value = ");
2255           value_print (bs->old_val, stb->stream, 0, Val_pretty_default);
2256           ui_out_field_stream (uiout, "old", stb);
2257           ui_out_text (uiout, "\nNew value = ");
2258           value_print (bs->breakpoint_at->val, stb->stream, 0, Val_pretty_default);
2259           ui_out_field_stream (uiout, "new", stb);
2260           do_cleanups (ui_out_chain);
2261           ui_out_text (uiout, "\n");
2262           value_free (bs->old_val);
2263           bs->old_val = NULL;
2264         }
2265       /* More than one watchpoint may have been triggered.  */
2266       return PRINT_UNKNOWN;
2267       break;
2268
2269     case bp_read_watchpoint:
2270       if (ui_out_is_mi_like_p (uiout))
2271         ui_out_field_string
2272           (uiout, "reason",
2273            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
2274       mention (bs->breakpoint_at);
2275       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2276       ui_out_text (uiout, "\nValue = ");
2277       value_print (bs->breakpoint_at->val, stb->stream, 0, Val_pretty_default);
2278       ui_out_field_stream (uiout, "value", stb);
2279       do_cleanups (ui_out_chain);
2280       ui_out_text (uiout, "\n");
2281       return PRINT_UNKNOWN;
2282       break;
2283
2284     case bp_access_watchpoint:
2285       if (bs->old_val != NULL)     
2286         {
2287           annotate_watchpoint (bs->breakpoint_at->number);
2288           if (ui_out_is_mi_like_p (uiout))
2289             ui_out_field_string
2290               (uiout, "reason",
2291                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
2292           mention (bs->breakpoint_at);
2293           ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2294           ui_out_text (uiout, "\nOld value = ");
2295           value_print (bs->old_val, stb->stream, 0, Val_pretty_default);
2296           ui_out_field_stream (uiout, "old", stb);
2297           value_free (bs->old_val);
2298           bs->old_val = NULL;
2299           ui_out_text (uiout, "\nNew value = ");
2300         }
2301       else 
2302         {
2303           mention (bs->breakpoint_at);
2304           if (ui_out_is_mi_like_p (uiout))
2305             ui_out_field_string
2306               (uiout, "reason",
2307                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
2308           ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2309           ui_out_text (uiout, "\nValue = ");
2310         }
2311       value_print (bs->breakpoint_at->val, stb->stream, 0,Val_pretty_default);
2312       ui_out_field_stream (uiout, "new", stb);
2313       do_cleanups (ui_out_chain);
2314       ui_out_text (uiout, "\n");
2315       return PRINT_UNKNOWN;
2316       break;
2317
2318     /* Fall through, we don't deal with these types of breakpoints
2319        here. */
2320
2321     case bp_finish:
2322       if (ui_out_is_mi_like_p (uiout))
2323         ui_out_field_string
2324           (uiout, "reason",
2325            async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
2326       return PRINT_UNKNOWN;
2327       break;
2328
2329     case bp_until:
2330       if (ui_out_is_mi_like_p (uiout))
2331         ui_out_field_string
2332           (uiout, "reason",
2333            async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
2334       return PRINT_UNKNOWN;
2335       break;
2336
2337     case bp_none:
2338     case bp_longjmp:
2339     case bp_longjmp_resume:
2340     case bp_step_resume:
2341     case bp_through_sigtramp:
2342     case bp_watchpoint_scope:
2343     case bp_call_dummy:
2344     default:
2345       return PRINT_UNKNOWN;
2346     }
2347 }
2348
2349 /* Generic routine for printing messages indicating why we
2350    stopped. The behavior of this function depends on the value
2351    'print_it' in the bpstat structure.  Under some circumstances we
2352    may decide not to print anything here and delegate the task to
2353    normal_stop(). */
2354
2355 static enum print_stop_action
2356 print_bp_stop_message (bpstat bs)
2357 {
2358   switch (bs->print_it)
2359     {
2360     case print_it_noop:
2361       /* Nothing should be printed for this bpstat entry. */
2362       return PRINT_UNKNOWN;
2363       break;
2364
2365     case print_it_done:
2366       /* We still want to print the frame, but we already printed the
2367          relevant messages. */
2368       return PRINT_SRC_AND_LOC;
2369       break;
2370
2371     case print_it_normal:
2372       /* Normal case.  Call the breakpoint's print_it method, or
2373          print_it_typical.  */
2374       if (bs->breakpoint_at != NULL && bs->breakpoint_at->ops != NULL
2375           && bs->breakpoint_at->ops->print_it != NULL)
2376         return bs->breakpoint_at->ops->print_it (bs->breakpoint_at);
2377       else
2378         return print_it_typical (bs);
2379       break;
2380
2381     default:
2382       internal_error (__FILE__, __LINE__,
2383                       _("print_bp_stop_message: unrecognized enum value"));
2384       break;
2385     }
2386 }
2387
2388 /* Print a message indicating what happened.  This is called from
2389    normal_stop().  The input to this routine is the head of the bpstat
2390    list - a list of the eventpoints that caused this stop.  This
2391    routine calls the generic print routine for printing a message
2392    about reasons for stopping.  This will print (for example) the
2393    "Breakpoint n," part of the output.  The return value of this
2394    routine is one of:
2395
2396    PRINT_UNKNOWN: Means we printed nothing
2397    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
2398    code to print the location. An example is 
2399    "Breakpoint 1, " which should be followed by
2400    the location.
2401    PRINT_SRC_ONLY: Means we printed something, but there is no need
2402    to also print the location part of the message.
2403    An example is the catch/throw messages, which
2404    don't require a location appended to the end.  
2405    PRINT_NOTHING: We have done some printing and we don't need any 
2406    further info to be printed.*/
2407
2408 enum print_stop_action
2409 bpstat_print (bpstat bs)
2410 {
2411   int val;
2412
2413   /* Maybe another breakpoint in the chain caused us to stop.
2414      (Currently all watchpoints go on the bpstat whether hit or not.
2415      That probably could (should) be changed, provided care is taken
2416      with respect to bpstat_explains_signal).  */
2417   for (; bs; bs = bs->next)
2418     {
2419       val = print_bp_stop_message (bs);
2420       if (val == PRINT_SRC_ONLY 
2421           || val == PRINT_SRC_AND_LOC 
2422           || val == PRINT_NOTHING)
2423         return val;
2424     }
2425
2426   /* We reached the end of the chain, or we got a null BS to start
2427      with and nothing was printed. */
2428   return PRINT_UNKNOWN;
2429 }
2430
2431 /* Evaluate the expression EXP and return 1 if value is zero.
2432    This is used inside a catch_errors to evaluate the breakpoint condition. 
2433    The argument is a "struct expression *" that has been cast to char * to 
2434    make it pass through catch_errors.  */
2435
2436 static int
2437 breakpoint_cond_eval (void *exp)
2438 {
2439   struct value *mark = value_mark ();
2440   int i = !value_true (evaluate_expression ((struct expression *) exp));
2441   value_free_to_mark (mark);
2442   return i;
2443 }
2444
2445 /* Allocate a new bpstat and chain it to the current one.  */
2446
2447 static bpstat
2448 bpstat_alloc (struct breakpoint *b, bpstat cbs /* Current "bs" value */ )
2449 {
2450   bpstat bs;
2451
2452   bs = (bpstat) xmalloc (sizeof (*bs));
2453   cbs->next = bs;
2454   bs->breakpoint_at = b;
2455   /* If the condition is false, etc., don't do the commands.  */
2456   bs->commands = NULL;
2457   bs->old_val = NULL;
2458   bs->print_it = print_it_normal;
2459   return bs;
2460 }
2461 \f
2462 /* Possible return values for watchpoint_check (this can't be an enum
2463    because of check_errors).  */
2464 /* The watchpoint has been deleted.  */
2465 #define WP_DELETED 1
2466 /* The value has changed.  */
2467 #define WP_VALUE_CHANGED 2
2468 /* The value has not changed.  */
2469 #define WP_VALUE_NOT_CHANGED 3
2470
2471 #define BP_TEMPFLAG 1
2472 #define BP_HARDWAREFLAG 2
2473
2474 /* Check watchpoint condition.  */
2475
2476 static int
2477 watchpoint_check (void *p)
2478 {
2479   bpstat bs = (bpstat) p;
2480   struct breakpoint *b;
2481   struct frame_info *fr;
2482   int within_current_scope;
2483
2484   b = bs->breakpoint_at;
2485
2486   if (b->exp_valid_block == NULL)
2487     within_current_scope = 1;
2488   else
2489     {
2490       /* There is no current frame at this moment.  If we're going to have
2491          any chance of handling watchpoints on local variables, we'll need
2492          the frame chain (so we can determine if we're in scope).  */
2493       reinit_frame_cache ();
2494       fr = frame_find_by_id (b->watchpoint_frame);
2495       within_current_scope = (fr != NULL);
2496       /* in_function_epilogue_p() returns a non-zero value if we're still
2497          in the function but the stack frame has already been invalidated.
2498          Since we can't rely on the values of local variables after the
2499          stack has been destroyed, we are treating the watchpoint in that
2500          state as `not changed' without further checking.
2501          
2502          vinschen/2003-09-04: The former implementation left out the case
2503          that the watchpoint frame couldn't be found by frame_find_by_id()
2504          because the current PC is currently in an epilogue.  Calling
2505          gdbarch_in_function_epilogue_p() also when fr == NULL fixes that. */
2506       if ((!within_current_scope || fr == get_current_frame ())
2507           && gdbarch_in_function_epilogue_p (current_gdbarch, read_pc ()))
2508         return WP_VALUE_NOT_CHANGED;
2509       if (fr && within_current_scope)
2510         /* If we end up stopping, the current frame will get selected
2511            in normal_stop.  So this call to select_frame won't affect
2512            the user.  */
2513         select_frame (fr);
2514     }
2515
2516   if (within_current_scope)
2517     {
2518       /* We use value_{,free_to_}mark because it could be a
2519          *long* time before we return to the command level and
2520          call free_all_values.  We can't call free_all_values because
2521          we might be in the middle of evaluating a function call.  */
2522
2523       struct value *mark = value_mark ();
2524       struct value *new_val = evaluate_expression (bs->breakpoint_at->exp);
2525       if (!value_equal (b->val, new_val))
2526         {
2527           release_value (new_val);
2528           value_free_to_mark (mark);
2529           bs->old_val = b->val;
2530           b->val = new_val;
2531           /* We will stop here */
2532           return WP_VALUE_CHANGED;
2533         }
2534       else
2535         {
2536           /* Nothing changed, don't do anything.  */
2537           value_free_to_mark (mark);
2538           /* We won't stop here */
2539           return WP_VALUE_NOT_CHANGED;
2540         }
2541     }
2542   else
2543     {
2544       /* This seems like the only logical thing to do because
2545          if we temporarily ignored the watchpoint, then when
2546          we reenter the block in which it is valid it contains
2547          garbage (in the case of a function, it may have two
2548          garbage values, one before and one after the prologue).
2549          So we can't even detect the first assignment to it and
2550          watch after that (since the garbage may or may not equal
2551          the first value assigned).  */
2552       /* We print all the stop information in print_it_typical(), but
2553          in this case, by the time we call print_it_typical() this bp
2554          will be deleted already. So we have no choice but print the
2555          information here. */
2556       if (ui_out_is_mi_like_p (uiout))
2557         ui_out_field_string
2558           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
2559       ui_out_text (uiout, "\nWatchpoint ");
2560       ui_out_field_int (uiout, "wpnum", bs->breakpoint_at->number);
2561       ui_out_text (uiout, " deleted because the program has left the block in\n\
2562 which its expression is valid.\n");     
2563
2564       if (b->related_breakpoint)
2565         b->related_breakpoint->disposition = disp_del_at_next_stop;
2566       b->disposition = disp_del_at_next_stop;
2567
2568       return WP_DELETED;
2569     }
2570 }
2571
2572 /* Get a bpstat associated with having just stopped at address
2573    BP_ADDR in thread PTID.  STOPPED_BY_WATCHPOINT is 1 if the
2574    target thinks we stopped due to a hardware watchpoint, 0 if we
2575    know we did not trigger a hardware watchpoint, and -1 if we do not know.  */
2576
2577 /* Determine whether we stopped at a breakpoint, etc, or whether we
2578    don't understand this stop.  Result is a chain of bpstat's such that:
2579
2580    if we don't understand the stop, the result is a null pointer.
2581
2582    if we understand why we stopped, the result is not null.
2583
2584    Each element of the chain refers to a particular breakpoint or
2585    watchpoint at which we have stopped.  (We may have stopped for
2586    several reasons concurrently.)
2587
2588    Each element of the chain has valid next, breakpoint_at,
2589    commands, FIXME??? fields.  */
2590
2591 bpstat
2592 bpstat_stop_status (CORE_ADDR bp_addr, ptid_t ptid, int stopped_by_watchpoint)
2593 {
2594   struct breakpoint *b, *temp;
2595   /* True if we've hit a breakpoint (as opposed to a watchpoint).  */
2596   int real_breakpoint = 0;
2597   /* Root of the chain of bpstat's */
2598   struct bpstats root_bs[1];
2599   /* Pointer to the last thing in the chain currently.  */
2600   bpstat bs = root_bs;
2601   int thread_id = pid_to_thread_id (ptid);
2602
2603   ALL_BREAKPOINTS_SAFE (b, temp)
2604   {
2605     if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
2606       continue;
2607
2608     if (b->type != bp_watchpoint
2609         && b->type != bp_hardware_watchpoint
2610         && b->type != bp_read_watchpoint
2611         && b->type != bp_access_watchpoint
2612         && b->type != bp_hardware_breakpoint
2613         && b->type != bp_catch_fork
2614         && b->type != bp_catch_vfork
2615         && b->type != bp_catch_exec
2616         && b->type != bp_catch_catch
2617         && b->type != bp_catch_throw)   /* a non-watchpoint bp */
2618       {
2619         if (b->loc->address != bp_addr)         /* address doesn't match */
2620           continue;
2621         if (overlay_debugging           /* unmapped overlay section */
2622             && section_is_overlay (b->loc->section) 
2623             && !section_is_mapped (b->loc->section))
2624           continue;
2625       }
2626
2627     /* Continuable hardware watchpoints are treated as non-existent if the 
2628        reason we stopped wasn't a hardware watchpoint (we didn't stop on 
2629        some data address).  Otherwise gdb won't stop on a break instruction 
2630        in the code (not from a breakpoint) when a hardware watchpoint has 
2631        been defined.  */
2632
2633     if ((b->type == bp_hardware_watchpoint
2634          || b->type == bp_read_watchpoint
2635          || b->type == bp_access_watchpoint)
2636         && !stopped_by_watchpoint)
2637       continue;
2638
2639     if (b->type == bp_hardware_breakpoint)
2640       {
2641         if (b->loc->address != bp_addr)
2642           continue;
2643         if (overlay_debugging           /* unmapped overlay section */
2644             && section_is_overlay (b->loc->section) 
2645             && !section_is_mapped (b->loc->section))
2646           continue;
2647       }
2648
2649     /* Is this a catchpoint of a load or unload?  If so, did we
2650        get a load or unload of the specified library?  If not,
2651        ignore it. */
2652     if ((b->type == bp_catch_load)
2653 #if defined(SOLIB_HAVE_LOAD_EVENT)
2654         && (!SOLIB_HAVE_LOAD_EVENT (PIDGET (inferior_ptid))
2655             || ((b->dll_pathname != NULL)
2656                 && (strcmp (b->dll_pathname, 
2657                             SOLIB_LOADED_LIBRARY_PATHNAME (
2658                               PIDGET (inferior_ptid)))
2659                     != 0)))
2660 #endif
2661       )
2662       continue;
2663
2664     if ((b->type == bp_catch_unload)
2665 #if defined(SOLIB_HAVE_UNLOAD_EVENT)
2666         && (!SOLIB_HAVE_UNLOAD_EVENT (PIDGET (inferior_ptid))
2667             || ((b->dll_pathname != NULL)
2668                 && (strcmp (b->dll_pathname, 
2669                             SOLIB_UNLOADED_LIBRARY_PATHNAME (
2670                               PIDGET (inferior_ptid)))
2671                     != 0)))
2672 #endif
2673       )
2674       continue;
2675
2676     if ((b->type == bp_catch_fork)
2677         && !inferior_has_forked (PIDGET (inferior_ptid),
2678                                  &b->forked_inferior_pid))
2679       continue;
2680
2681     if ((b->type == bp_catch_vfork)
2682         && !inferior_has_vforked (PIDGET (inferior_ptid),
2683                                   &b->forked_inferior_pid))
2684       continue;
2685
2686     if ((b->type == bp_catch_exec)
2687         && !inferior_has_execd (PIDGET (inferior_ptid), &b->exec_pathname))
2688       continue;
2689
2690     if (ep_is_exception_catchpoint (b) &&
2691         !(current_exception_event = target_get_current_exception_event ()))
2692       continue;
2693
2694     /* Come here if it's a watchpoint, or if the break address matches */
2695
2696     bs = bpstat_alloc (b, bs);  /* Alloc a bpstat to explain stop */
2697
2698     /* Watchpoints may change this, if not found to have triggered. */
2699     bs->stop = 1;
2700     bs->print = 1;
2701
2702     if (b->type == bp_watchpoint ||
2703         b->type == bp_hardware_watchpoint)
2704       {
2705         char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
2706                                     b->number);
2707         struct cleanup *cleanups = make_cleanup (xfree, message);
2708         int e = catch_errors (watchpoint_check, bs, message, 
2709                               RETURN_MASK_ALL);
2710         do_cleanups (cleanups);
2711         switch (e)
2712           {
2713           case WP_DELETED:
2714             /* We've already printed what needs to be printed.  */
2715             /* Actually this is superfluous, because by the time we
2716                call print_it_typical() the wp will be already deleted,
2717                and the function will return immediately. */
2718             bs->print_it = print_it_done;
2719             /* Stop.  */
2720             break;
2721           case WP_VALUE_CHANGED:
2722             /* Stop.  */
2723             ++(b->hit_count);
2724             break;
2725           case WP_VALUE_NOT_CHANGED:
2726             /* Don't stop.  */
2727             bs->print_it = print_it_noop;
2728             bs->stop = 0;
2729             continue;
2730           default:
2731             /* Can't happen.  */
2732             /* FALLTHROUGH */
2733           case 0:
2734             /* Error from catch_errors.  */
2735             printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
2736             if (b->related_breakpoint)
2737               b->related_breakpoint->disposition = disp_del_at_next_stop;
2738             b->disposition = disp_del_at_next_stop;
2739             /* We've already printed what needs to be printed.  */
2740             bs->print_it = print_it_done;
2741
2742             /* Stop.  */
2743             break;
2744           }
2745       }
2746     else if (b->type == bp_read_watchpoint || 
2747              b->type == bp_access_watchpoint)
2748       {
2749         CORE_ADDR addr;
2750         struct value *v;
2751         int found = 0;
2752
2753         if (!target_stopped_data_address (&current_target, &addr))
2754           continue;
2755         for (v = b->val_chain; v; v = value_next (v))
2756           {
2757             if (VALUE_LVAL (v) == lval_memory
2758                 && ! value_lazy (v))
2759               {
2760                 struct type *vtype = check_typedef (value_type (v));
2761
2762                 if (v == b->val_chain
2763                     || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2764                         && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2765                   {
2766                     CORE_ADDR vaddr;
2767
2768                     vaddr = VALUE_ADDRESS (v) + value_offset (v);
2769                     /* Exact match not required.  Within range is
2770                        sufficient.  */
2771                     if (addr >= vaddr &&
2772                         addr < vaddr + TYPE_LENGTH (value_type (v)))
2773                       found = 1;
2774                   }
2775               }
2776           }
2777         if (found)
2778           {
2779             char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
2780                                         b->number);
2781             struct cleanup *cleanups = make_cleanup (xfree, message);
2782             int e = catch_errors (watchpoint_check, bs, message,
2783                                   RETURN_MASK_ALL);
2784             do_cleanups (cleanups);
2785             switch (e)
2786               {
2787               case WP_DELETED:
2788                 /* We've already printed what needs to be printed.  */
2789                 bs->print_it = print_it_done;
2790                 /* Stop.  */
2791                 break;
2792               case WP_VALUE_CHANGED:
2793                 if (b->type == bp_read_watchpoint)
2794                   {
2795                     /* Don't stop: read watchpoints shouldn't fire if
2796                        the value has changed.  This is for targets
2797                        which cannot set read-only watchpoints.  */
2798                     bs->print_it = print_it_noop;
2799                     bs->stop = 0;
2800                     continue;
2801                   }
2802                 ++(b->hit_count);
2803                 break;
2804               case WP_VALUE_NOT_CHANGED:
2805                 /* Stop.  */
2806                 ++(b->hit_count);
2807                 break;
2808               default:
2809                 /* Can't happen.  */
2810               case 0:
2811                 /* Error from catch_errors.  */
2812                 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
2813                 if (b->related_breakpoint)
2814                   b->related_breakpoint->disposition = disp_del_at_next_stop;
2815                 b->disposition = disp_del_at_next_stop;
2816                 /* We've already printed what needs to be printed.  */
2817                 bs->print_it = print_it_done;
2818                 break;
2819               }
2820           }
2821         else    /* found == 0 */
2822           {
2823             /* This is a case where some watchpoint(s) triggered,
2824                but not at the address of this watchpoint (FOUND
2825                was left zero).  So don't print anything for this
2826                watchpoint.  */
2827             bs->print_it = print_it_noop;
2828             bs->stop = 0;
2829             continue;
2830           }
2831       }
2832     else
2833       {
2834         /* By definition, an encountered breakpoint is a triggered
2835            breakpoint. */
2836         ++(b->hit_count);
2837
2838         real_breakpoint = 1;
2839       }
2840
2841     if (frame_id_p (b->frame_id)
2842         && !frame_id_eq (b->frame_id, get_frame_id (get_current_frame ())))
2843       bs->stop = 0;
2844     else
2845       {
2846         int value_is_zero = 0;
2847
2848         if (b->cond)
2849           {
2850             /* Need to select the frame, with all that implies
2851                so that the conditions will have the right context.  */
2852             select_frame (get_current_frame ());
2853             value_is_zero
2854               = catch_errors (breakpoint_cond_eval, (b->cond),
2855                               "Error in testing breakpoint condition:\n",
2856                               RETURN_MASK_ALL);
2857             /* FIXME-someday, should give breakpoint # */
2858             free_all_values ();
2859           }
2860         if (b->cond && value_is_zero)
2861           {
2862             bs->stop = 0;
2863             /* Don't consider this a hit.  */
2864             --(b->hit_count);
2865           }
2866         else if (b->thread != -1 && b->thread != thread_id)
2867           {
2868             bs->stop = 0;
2869             /* Don't consider this a hit.  */
2870             --(b->hit_count);
2871           }
2872         else if (b->ignore_count > 0)
2873           {
2874             b->ignore_count--;
2875             annotate_ignore_count_change ();
2876             bs->stop = 0;
2877           }
2878         else
2879           {
2880             /* We will stop here */
2881             if (b->disposition == disp_disable)
2882               b->enable_state = bp_disabled;
2883             if (b->silent)
2884               bs->print = 0;
2885             bs->commands = b->commands;
2886             if (bs->commands &&
2887                 (strcmp ("silent", bs->commands->line) == 0
2888                  || (xdb_commands && strcmp ("Q", bs->commands->line) == 0)))
2889               {
2890                 bs->commands = bs->commands->next;
2891                 bs->print = 0;
2892               }
2893             bs->commands = copy_command_lines (bs->commands);
2894           }
2895       }
2896     /* Print nothing for this entry if we dont stop or if we dont print.  */
2897     if (bs->stop == 0 || bs->print == 0)
2898       bs->print_it = print_it_noop;
2899   }
2900
2901   bs->next = NULL;              /* Terminate the chain */
2902   bs = root_bs->next;           /* Re-grab the head of the chain */
2903
2904   /* The value of a hardware watchpoint hasn't changed, but the
2905      intermediate memory locations we are watching may have.  */
2906   if (bs && !bs->stop &&
2907       (bs->breakpoint_at->type == bp_hardware_watchpoint ||
2908        bs->breakpoint_at->type == bp_read_watchpoint ||
2909        bs->breakpoint_at->type == bp_access_watchpoint))
2910     {
2911       remove_breakpoints ();
2912       insert_breakpoints ();
2913     }
2914   return bs;
2915 }
2916 \f
2917 /* Tell what to do about this bpstat.  */
2918 struct bpstat_what
2919 bpstat_what (bpstat bs)
2920 {
2921   /* Classify each bpstat as one of the following.  */
2922   enum class
2923     {
2924       /* This bpstat element has no effect on the main_action.  */
2925       no_effect = 0,
2926
2927       /* There was a watchpoint, stop but don't print.  */
2928       wp_silent,
2929
2930       /* There was a watchpoint, stop and print.  */
2931       wp_noisy,
2932
2933       /* There was a breakpoint but we're not stopping.  */
2934       bp_nostop,
2935
2936       /* There was a breakpoint, stop but don't print.  */
2937       bp_silent,
2938
2939       /* There was a breakpoint, stop and print.  */
2940       bp_noisy,
2941
2942       /* We hit the longjmp breakpoint.  */
2943       long_jump,
2944
2945       /* We hit the longjmp_resume breakpoint.  */
2946       long_resume,
2947
2948       /* We hit the step_resume breakpoint.  */
2949       step_resume,
2950
2951       /* We hit the through_sigtramp breakpoint.  */
2952       through_sig,
2953
2954       /* We hit the shared library event breakpoint.  */
2955       shlib_event,
2956
2957       /* We caught a shared library event.  */
2958       catch_shlib_event,
2959
2960       /* This is just used to count how many enums there are.  */
2961       class_last
2962     };
2963
2964   /* Here is the table which drives this routine.  So that we can
2965      format it pretty, we define some abbreviations for the
2966      enum bpstat_what codes.  */
2967 #define kc BPSTAT_WHAT_KEEP_CHECKING
2968 #define ss BPSTAT_WHAT_STOP_SILENT
2969 #define sn BPSTAT_WHAT_STOP_NOISY
2970 #define sgl BPSTAT_WHAT_SINGLE
2971 #define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
2972 #define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
2973 #define clrs BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE
2974 #define sr BPSTAT_WHAT_STEP_RESUME
2975 #define ts BPSTAT_WHAT_THROUGH_SIGTRAMP
2976 #define shl BPSTAT_WHAT_CHECK_SHLIBS
2977 #define shlr BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK
2978
2979 /* "Can't happen."  Might want to print an error message.
2980    abort() is not out of the question, but chances are GDB is just
2981    a bit confused, not unusable.  */
2982 #define err BPSTAT_WHAT_STOP_NOISY
2983
2984   /* Given an old action and a class, come up with a new action.  */
2985   /* One interesting property of this table is that wp_silent is the same
2986      as bp_silent and wp_noisy is the same as bp_noisy.  That is because
2987      after stopping, the check for whether to step over a breakpoint
2988      (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
2989      reference to how we stopped.  We retain separate wp_silent and
2990      bp_silent codes in case we want to change that someday. 
2991
2992      Another possibly interesting property of this table is that
2993      there's a partial ordering, priority-like, of the actions.  Once
2994      you've decided that some action is appropriate, you'll never go
2995      back and decide something of a lower priority is better.  The
2996      ordering is:
2997
2998      kc   < clr sgl shl shlr slr sn sr ss ts
2999      sgl  < clrs shl shlr slr sn sr ss ts
3000      slr  < err shl shlr sn sr ss ts
3001      clr  < clrs err shl shlr sn sr ss ts
3002      clrs < err shl shlr sn sr ss ts
3003      ss   < shl shlr sn sr ts
3004      sn   < shl shlr sr ts
3005      sr   < shl shlr ts
3006      shl  < shlr
3007      ts   < 
3008      shlr <
3009
3010      What I think this means is that we don't need a damned table
3011      here.  If you just put the rows and columns in the right order,
3012      it'd look awfully regular.  We could simply walk the bpstat list
3013      and choose the highest priority action we find, with a little
3014      logic to handle the 'err' cases, and the CLEAR_LONGJMP_RESUME/
3015      CLEAR_LONGJMP_RESUME_SINGLE distinction (which breakpoint.h says
3016      is messy anyway).  */
3017
3018   /* step_resume entries: a step resume breakpoint overrides another
3019      breakpoint of signal handling (see comment in wait_for_inferior
3020      at where we set the step_resume breakpoint).  */
3021   /* We handle the through_sigtramp_breakpoint the same way; having both
3022      one of those and a step_resume_breakpoint is probably very rare (?).  */
3023
3024   static const enum bpstat_what_main_action
3025     table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
3026   {
3027   /*                              old action */
3028   /*       kc    ss    sn    sgl    slr   clr    clrs   sr    ts   shl   shlr
3029    */
3030 /*no_effect */
3031     {kc, ss, sn, sgl, slr, clr, clrs, sr, ts, shl, shlr},
3032 /*wp_silent */
3033     {ss, ss, sn, ss, ss, ss, ss, sr, ts, shl, shlr},
3034 /*wp_noisy */
3035     {sn, sn, sn, sn, sn, sn, sn, sr, ts, shl, shlr},
3036 /*bp_nostop */
3037     {sgl, ss, sn, sgl, slr, clrs, clrs, sr, ts, shl, shlr},
3038 /*bp_silent */
3039     {ss, ss, sn, ss, ss, ss, ss, sr, ts, shl, shlr},
3040 /*bp_noisy */
3041     {sn, sn, sn, sn, sn, sn, sn, sr, ts, shl, shlr},
3042 /*long_jump */
3043     {slr, ss, sn, slr, slr, err, err, sr, ts, shl, shlr},
3044 /*long_resume */
3045     {clr, ss, sn, clrs, err, err, err, sr, ts, shl, shlr},
3046 /*step_resume */
3047     {sr, sr, sr, sr, sr, sr, sr, sr, ts, shl, shlr},
3048 /*through_sig */
3049     {ts, ts, ts, ts, ts, ts, ts, ts, ts, shl, shlr},
3050 /*shlib */
3051     {shl, shl, shl, shl, shl, shl, shl, shl, ts, shl, shlr},
3052 /*catch_shlib */
3053     {shlr, shlr, shlr, shlr, shlr, shlr, shlr, shlr, ts, shlr, shlr}
3054   };
3055
3056 #undef kc
3057 #undef ss
3058 #undef sn
3059 #undef sgl
3060 #undef slr
3061 #undef clr
3062 #undef clrs
3063 #undef err
3064 #undef sr
3065 #undef ts
3066 #undef shl
3067 #undef shlr
3068   enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
3069   struct bpstat_what retval;
3070
3071   retval.call_dummy = 0;
3072   for (; bs != NULL; bs = bs->next)
3073     {
3074       enum class bs_class = no_effect;
3075       if (bs->breakpoint_at == NULL)
3076         /* I suspect this can happen if it was a momentary breakpoint
3077            which has since been deleted.  */
3078         continue;
3079       switch (bs->breakpoint_at->type)
3080         {
3081         case bp_none:
3082           continue;
3083
3084         case bp_breakpoint:
3085         case bp_hardware_breakpoint:
3086         case bp_until:
3087         case bp_finish:
3088           if (bs->stop)
3089             {
3090               if (bs->print)
3091                 bs_class = bp_noisy;
3092               else
3093                 bs_class = bp_silent;
3094             }
3095           else
3096             bs_class = bp_nostop;
3097           break;
3098         case bp_watchpoint:
3099         case bp_hardware_watchpoint:
3100         case bp_read_watchpoint:
3101         case bp_access_watchpoint:
3102           if (bs->stop)
3103             {
3104               if (bs->print)
3105                 bs_class = wp_noisy;
3106               else
3107                 bs_class = wp_silent;
3108             }
3109           else
3110             /* There was a watchpoint, but we're not stopping. 
3111                This requires no further action.  */
3112             bs_class = no_effect;
3113           break;
3114         case bp_longjmp:
3115           bs_class = long_jump;
3116           break;
3117         case bp_longjmp_resume:
3118           bs_class = long_resume;
3119           break;
3120         case bp_step_resume:
3121           if (bs->stop)
3122             {
3123               bs_class = step_resume;
3124             }
3125           else
3126             /* It is for the wrong frame.  */
3127             bs_class = bp_nostop;
3128           break;
3129         case bp_through_sigtramp:
3130           bs_class = through_sig;
3131           break;
3132         case bp_watchpoint_scope:
3133           bs_class = bp_nostop;
3134           break;
3135         case bp_shlib_event:
3136           bs_class = shlib_event;
3137           break;
3138         case bp_thread_event:
3139         case bp_overlay_event:
3140           bs_class = bp_nostop;
3141           break;
3142         case bp_catch_load:
3143         case bp_catch_unload:
3144           /* Only if this catchpoint triggered should we cause the
3145              step-out-of-dld behaviour.  Otherwise, we ignore this
3146              catchpoint.  */
3147           if (bs->stop)
3148             bs_class = catch_shlib_event;
3149           else
3150             bs_class = no_effect;
3151           break;
3152         case bp_catch_fork:
3153         case bp_catch_vfork:
3154         case bp_catch_exec:
3155           if (bs->stop)
3156             {
3157               if (bs->print)
3158                 bs_class = bp_noisy;
3159               else
3160                 bs_class = bp_silent;
3161             }
3162           else
3163             /* There was a catchpoint, but we're not stopping.  
3164                This requires no further action.  */
3165             bs_class = no_effect;
3166           break;
3167         case bp_catch_catch:
3168           if (!bs->stop || CURRENT_EXCEPTION_KIND != EX_EVENT_CATCH)
3169             bs_class = bp_nostop;
3170           else if (bs->stop)
3171             bs_class = bs->print ? bp_noisy : bp_silent;
3172           break;
3173         case bp_catch_throw:
3174           if (!bs->stop || CURRENT_EXCEPTION_KIND != EX_EVENT_THROW)
3175             bs_class = bp_nostop;
3176           else if (bs->stop)
3177             bs_class = bs->print ? bp_noisy : bp_silent;
3178           break;
3179         case bp_call_dummy:
3180           /* Make sure the action is stop (silent or noisy),
3181              so infrun.c pops the dummy frame.  */
3182           bs_class = bp_silent;
3183           retval.call_dummy = 1;
3184           break;
3185         }
3186       current_action = table[(int) bs_class][(int) current_action];
3187     }
3188   retval.main_action = current_action;
3189   return retval;
3190 }
3191
3192 /* Nonzero if we should step constantly (e.g. watchpoints on machines
3193    without hardware support).  This isn't related to a specific bpstat,
3194    just to things like whether watchpoints are set.  */
3195
3196 int
3197 bpstat_should_step (void)
3198 {
3199   struct breakpoint *b;
3200   ALL_BREAKPOINTS (b)
3201     if (breakpoint_enabled (b) && b->type == bp_watchpoint)
3202       return 1;
3203   return 0;
3204 }
3205
3206 /* Nonzero if there are enabled hardware watchpoints. */
3207 int
3208 bpstat_have_active_hw_watchpoints (void)
3209 {
3210   struct bp_location *bpt;
3211   ALL_BP_LOCATIONS (bpt)
3212     if (breakpoint_enabled (bpt->owner)
3213         && bpt->inserted
3214         && bpt->loc_type == bp_loc_hardware_watchpoint)
3215       return 1;
3216   return 0;
3217 }
3218 \f
3219
3220 /* Given a bpstat that records zero or more triggered eventpoints, this
3221    function returns another bpstat which contains only the catchpoints
3222    on that first list, if any. */
3223 void
3224 bpstat_get_triggered_catchpoints (bpstat ep_list, bpstat *cp_list)
3225 {
3226   struct bpstats root_bs[1];
3227   bpstat bs = root_bs;
3228   struct breakpoint *ep;
3229   char *dll_pathname;
3230
3231   bpstat_clear (cp_list);
3232   root_bs->next = NULL;
3233
3234   for (; ep_list != NULL; ep_list = ep_list->next)
3235     {
3236       /* Is this eventpoint a catchpoint?  If not, ignore it. */
3237       ep = ep_list->breakpoint_at;
3238       if (ep == NULL)
3239         break;
3240       if ((ep->type != bp_catch_load) &&
3241           (ep->type != bp_catch_unload) &&
3242           (ep->type != bp_catch_catch) &&
3243           (ep->type != bp_catch_throw))         
3244         /* pai: (temp) ADD fork/vfork here!!  */
3245         continue;
3246
3247       /* Yes; add it to the list. */
3248       bs = bpstat_alloc (ep, bs);
3249       *bs = *ep_list;
3250       bs->next = NULL;
3251       bs = root_bs->next;
3252
3253 #if defined(SOLIB_ADD)
3254       /* Also, for each triggered catchpoint, tag it with the name of
3255          the library that caused this trigger.  (We copy the name now,
3256          because it's only guaranteed to be available NOW, when the
3257          catchpoint triggers.  Clients who may wish to know the name
3258          later must get it from the catchpoint itself.) */
3259       if (ep->triggered_dll_pathname != NULL)
3260         xfree (ep->triggered_dll_pathname);
3261       if (ep->type == bp_catch_load)
3262         dll_pathname = SOLIB_LOADED_LIBRARY_PATHNAME (
3263                          PIDGET (inferior_ptid));
3264       else
3265         dll_pathname = SOLIB_UNLOADED_LIBRARY_PATHNAME (
3266                          PIDGET (inferior_ptid));
3267 #else
3268       dll_pathname = NULL;
3269 #endif
3270       if (dll_pathname)
3271         {
3272           ep->triggered_dll_pathname = (char *) 
3273             xmalloc (strlen (dll_pathname) + 1);
3274           strcpy (ep->triggered_dll_pathname, dll_pathname);
3275         }
3276       else
3277         ep->triggered_dll_pathname = NULL;
3278     }
3279
3280   *cp_list = bs;
3281 }
3282
3283 /* Print B to gdb_stdout. */
3284 static void
3285 print_one_breakpoint (struct breakpoint *b,
3286                       CORE_ADDR *last_addr)
3287 {
3288   struct command_line *l;
3289   struct symbol *sym;
3290   struct ep_type_description
3291     {
3292       enum bptype type;
3293       char *description;
3294     };
3295   static struct ep_type_description bptypes[] =
3296   {
3297     {bp_none, "?deleted?"},
3298     {bp_breakpoint, "breakpoint"},
3299     {bp_hardware_breakpoint, "hw breakpoint"},
3300     {bp_until, "until"},
3301     {bp_finish, "finish"},
3302     {bp_watchpoint, "watchpoint"},
3303     {bp_hardware_watchpoint, "hw watchpoint"},
3304     {bp_read_watchpoint, "read watchpoint"},
3305     {bp_access_watchpoint, "acc watchpoint"},
3306     {bp_longjmp, "longjmp"},
3307     {bp_longjmp_resume, "longjmp resume"},
3308     {bp_step_resume, "step resume"},
3309     {bp_through_sigtramp, "sigtramp"},
3310     {bp_watchpoint_scope, "watchpoint scope"},
3311     {bp_call_dummy, "call dummy"},
3312     {bp_shlib_event, "shlib events"},
3313     {bp_thread_event, "thread events"},
3314     {bp_overlay_event, "overlay events"},
3315     {bp_catch_load, "catch load"},
3316     {bp_catch_unload, "catch unload"},
3317     {bp_catch_fork, "catch fork"},
3318     {bp_catch_vfork, "catch vfork"},
3319     {bp_catch_exec, "catch exec"},
3320     {bp_catch_catch, "catch catch"},
3321     {bp_catch_throw, "catch throw"}
3322   };
3323   
3324   static char *bpdisps[] =
3325   {"del", "dstp", "dis", "keep"};
3326   static char bpenables[] = "nynny";
3327   char wrap_indent[80];
3328   struct ui_stream *stb = ui_out_stream_new (uiout);
3329   struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3330   struct cleanup *bkpt_chain;
3331
3332   annotate_record ();
3333   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
3334
3335   /* 1 */
3336   annotate_field (0);
3337   ui_out_field_int (uiout, "number", b->number);
3338
3339   /* 2 */
3340   annotate_field (1);
3341   if (((int) b->type > (sizeof (bptypes) / sizeof (bptypes[0])))
3342       || ((int) b->type != bptypes[(int) b->type].type))
3343     internal_error (__FILE__, __LINE__,
3344                     _("bptypes table does not describe type #%d."),
3345                     (int) b->type);
3346   ui_out_field_string (uiout, "type", bptypes[(int) b->type].description);
3347
3348   /* 3 */
3349   annotate_field (2);
3350   ui_out_field_string (uiout, "disp", bpdisps[(int) b->disposition]);
3351
3352   /* 4 */
3353   annotate_field (3);
3354   ui_out_field_fmt (uiout, "enabled", "%c", bpenables[(int) b->enable_state]);
3355   ui_out_spaces (uiout, 2);
3356   
3357   /* 5 and 6 */
3358   strcpy (wrap_indent, "                           ");
3359   if (addressprint)
3360     {
3361       if (TARGET_ADDR_BIT <= 32)
3362         strcat (wrap_indent, "           ");
3363       else
3364         strcat (wrap_indent, "                   ");
3365     }
3366
3367   if (b->ops != NULL && b->ops->print_one != NULL)
3368     b->ops->print_one (b, last_addr);
3369   else
3370     switch (b->type)
3371       {
3372       case bp_none:
3373         internal_error (__FILE__, __LINE__,
3374                         _("print_one_breakpoint: bp_none encountered\n"));
3375         break;
3376
3377       case bp_watchpoint:
3378       case bp_hardware_watchpoint:
3379       case bp_read_watchpoint:
3380       case bp_access_watchpoint:
3381         /* Field 4, the address, is omitted (which makes the columns
3382            not line up too nicely with the headers, but the effect
3383            is relatively readable).  */
3384         if (addressprint)
3385           ui_out_field_skip (uiout, "addr");
3386         annotate_field (5);
3387         print_expression (b->exp, stb->stream);
3388         ui_out_field_stream (uiout, "what", stb);
3389         break;
3390
3391       case bp_catch_load:
3392       case bp_catch_unload:
3393         /* Field 4, the address, is omitted (which makes the columns
3394            not line up too nicely with the headers, but the effect
3395            is relatively readable).  */
3396         if (addressprint)
3397           ui_out_field_skip (uiout, "addr");
3398         annotate_field (5);
3399         if (b->dll_pathname == NULL)
3400           {
3401             ui_out_field_string (uiout, "what", "<any library>");
3402             ui_out_spaces (uiout, 1);
3403           }
3404         else
3405           {
3406             ui_out_text (uiout, "library \"");
3407             ui_out_field_string (uiout, "what", b->dll_pathname);
3408             ui_out_text (uiout, "\" ");
3409           }
3410         break;
3411
3412       case bp_catch_fork:
3413       case bp_catch_vfork:
3414         /* Field 4, the address, is omitted (which makes the columns
3415            not line up too nicely with the headers, but the effect
3416            is relatively readable).  */
3417         if (addressprint)
3418           ui_out_field_skip (uiout, "addr");
3419         annotate_field (5);
3420         if (b->forked_inferior_pid != 0)
3421           {
3422             ui_out_text (uiout, "process ");
3423             ui_out_field_int (uiout, "what", b->forked_inferior_pid);
3424             ui_out_spaces (uiout, 1);
3425           }
3426         break;
3427
3428       case bp_catch_exec:
3429         /* Field 4, the address, is omitted (which makes the columns
3430            not line up too nicely with the headers, but the effect
3431            is relatively readable).  */
3432         if (addressprint)
3433           ui_out_field_skip (uiout, "addr");
3434         annotate_field (5);
3435         if (b->exec_pathname != NULL)
3436           {
3437             ui_out_text (uiout, "program \"");
3438             ui_out_field_string (uiout, "what", b->exec_pathname);
3439             ui_out_text (uiout, "\" ");
3440           }
3441         break;
3442
3443       case bp_catch_catch:
3444         /* Field 4, the address, is omitted (which makes the columns
3445            not line up too nicely with the headers, but the effect
3446            is relatively readable).  */
3447         if (addressprint)
3448           ui_out_field_skip (uiout, "addr");
3449         annotate_field (5);
3450         ui_out_field_string (uiout, "what", "exception catch");
3451         ui_out_spaces (uiout, 1);
3452         break;
3453
3454       case bp_catch_throw:
3455         /* Field 4, the address, is omitted (which makes the columns
3456            not line up too nicely with the headers, but the effect
3457            is relatively readable).  */
3458         if (addressprint)
3459           ui_out_field_skip (uiout, "addr");
3460         annotate_field (5);
3461         ui_out_field_string (uiout, "what", "exception throw");
3462         ui_out_spaces (uiout, 1);
3463         break;
3464
3465       case bp_breakpoint:
3466       case bp_hardware_breakpoint:
3467       case bp_until:
3468       case bp_finish:
3469       case bp_longjmp:
3470       case bp_longjmp_resume:
3471       case bp_step_resume:
3472       case bp_through_sigtramp:
3473       case bp_watchpoint_scope:
3474       case bp_call_dummy:
3475       case bp_shlib_event:
3476       case bp_thread_event:
3477       case bp_overlay_event:
3478         if (addressprint)
3479           {
3480             annotate_field (4);
3481             if (b->pending)
3482               ui_out_field_string (uiout, "addr", "<PENDING>");
3483             else
3484               ui_out_field_core_addr (uiout, "addr", b->loc->address);
3485           }
3486         annotate_field (5);
3487         *last_addr = b->loc->address;
3488         if (b->source_file)
3489           {
3490             sym = find_pc_sect_function (b->loc->address, b->loc->section);
3491             if (sym)
3492               {
3493                 ui_out_text (uiout, "in ");
3494                 ui_out_field_string (uiout, "func",
3495                                      SYMBOL_PRINT_NAME (sym));
3496                 ui_out_wrap_hint (uiout, wrap_indent);
3497                 ui_out_text (uiout, " at ");
3498               }
3499             ui_out_field_string (uiout, "file", b->source_file);
3500             ui_out_text (uiout, ":");
3501
3502             if (ui_out_is_mi_like_p (uiout))
3503               {
3504                 struct symtab_and_line sal = find_pc_line (b->loc->address, 0);
3505                 char *fullname = symtab_to_fullname (sal.symtab);
3506
3507                 if (fullname)
3508                   ui_out_field_string (uiout, "fullname", fullname);
3509               }
3510
3511             ui_out_field_int (uiout, "line", b->line_number);
3512           }
3513         else if (b->pending)
3514           {
3515             ui_out_field_string (uiout, "pending", b->addr_string);
3516           }
3517         else
3518           {
3519             print_address_symbolic (b->loc->address, stb->stream, demangle, "");
3520             ui_out_field_stream (uiout, "at", stb);
3521           }
3522         break;
3523       }
3524
3525   if (b->thread != -1)
3526     {
3527       /* FIXME: This seems to be redundant and lost here; see the
3528          "stop only in" line a little further down. */
3529       ui_out_text (uiout, " thread ");
3530       ui_out_field_int (uiout, "thread", b->thread);
3531     }
3532   
3533   ui_out_text (uiout, "\n");
3534   
3535   if (frame_id_p (b->frame_id))
3536     {
3537       annotate_field (6);
3538       ui_out_text (uiout, "\tstop only in stack frame at ");
3539       /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
3540          the frame ID.  */
3541       ui_out_field_core_addr (uiout, "frame", b->frame_id.stack_addr);
3542       ui_out_text (uiout, "\n");
3543     }
3544   
3545   if (b->cond)
3546     {
3547       annotate_field (7);
3548       ui_out_text (uiout, "\tstop only if ");
3549       print_expression (b->cond, stb->stream);
3550       ui_out_field_stream (uiout, "cond", stb);
3551       ui_out_text (uiout, "\n");
3552     }
3553
3554   if (b->pending && b->cond_string)
3555     {
3556       annotate_field (7);
3557       ui_out_text (uiout, "\tstop only if ");
3558       ui_out_field_string (uiout, "cond", b->cond_string);
3559       ui_out_text (uiout, "\n");
3560     }
3561
3562   if (b->thread != -1)
3563     {
3564       /* FIXME should make an annotation for this */
3565       ui_out_text (uiout, "\tstop only in thread ");
3566       ui_out_field_int (uiout, "thread", b->thread);
3567       ui_out_text (uiout, "\n");
3568     }
3569   
3570   if (show_breakpoint_hit_counts && b->hit_count)
3571     {
3572       /* FIXME should make an annotation for this */
3573       if (ep_is_catchpoint (b))
3574         ui_out_text (uiout, "\tcatchpoint");
3575       else
3576         ui_out_text (uiout, "\tbreakpoint");
3577       ui_out_text (uiout, " already hit ");
3578       ui_out_field_int (uiout, "times", b->hit_count);
3579       if (b->hit_count == 1)
3580         ui_out_text (uiout, " time\n");
3581       else
3582         ui_out_text (uiout, " times\n");
3583     }
3584   
3585   /* Output the count also if it is zero, but only if this is
3586      mi. FIXME: Should have a better test for this. */
3587   if (ui_out_is_mi_like_p (uiout))
3588     if (show_breakpoint_hit_counts && b->hit_count == 0)
3589       ui_out_field_int (uiout, "times", b->hit_count);
3590
3591   if (b->ignore_count)
3592     {
3593       annotate_field (8);
3594       ui_out_text (uiout, "\tignore next ");
3595       ui_out_field_int (uiout, "ignore", b->ignore_count);
3596       ui_out_text (uiout, " hits\n");
3597     }
3598   
3599   if ((l = b->commands))
3600     {
3601       struct cleanup *script_chain;
3602
3603       annotate_field (9);
3604       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
3605       print_command_lines (uiout, l, 4);
3606       do_cleanups (script_chain);
3607     }
3608   do_cleanups (bkpt_chain);
3609   do_cleanups (old_chain);
3610 }
3611
3612 struct captured_breakpoint_query_args
3613   {
3614     int bnum;
3615   };
3616
3617 static int
3618 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
3619 {
3620   struct captured_breakpoint_query_args *args = data;
3621   struct breakpoint *b;
3622   CORE_ADDR dummy_addr = 0;
3623   ALL_BREAKPOINTS (b)
3624     {
3625       if (args->bnum == b->number)
3626         {
3627           print_one_breakpoint (b, &dummy_addr);
3628           return GDB_RC_OK;
3629         }
3630     }
3631   return GDB_RC_NONE;
3632 }
3633
3634 enum gdb_rc
3635 gdb_breakpoint_query (struct ui_out *uiout, int bnum, char **error_message)
3636 {
3637   struct captured_breakpoint_query_args args;
3638   args.bnum = bnum;
3639   /* For the moment we don't trust print_one_breakpoint() to not throw
3640      an error. */
3641   return catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
3642                                     error_message, RETURN_MASK_ALL);
3643 }
3644
3645 /* Return non-zero if B is user settable (breakpoints, watchpoints,
3646    catchpoints, et.al.). */
3647
3648 static int
3649 user_settable_breakpoint (const struct breakpoint *b)
3650 {
3651   return (b->type == bp_breakpoint
3652           || b->type == bp_catch_load
3653           || b->type == bp_catch_unload
3654           || b->type == bp_catch_fork
3655           || b->type == bp_catch_vfork
3656           || b->type == bp_catch_exec
3657           || b->type == bp_catch_catch
3658           || b->type == bp_catch_throw
3659           || b->type == bp_hardware_breakpoint
3660           || b->type == bp_watchpoint
3661           || b->type == bp_read_watchpoint
3662           || b->type == bp_access_watchpoint
3663           || b->type == bp_hardware_watchpoint);
3664 }
3665         
3666 /* Print information on user settable breakpoint (watchpoint, etc)
3667    number BNUM.  If BNUM is -1 print all user settable breakpoints.
3668    If ALLFLAG is non-zero, include non- user settable breakpoints. */
3669
3670 static void
3671 breakpoint_1 (int bnum, int allflag)
3672 {
3673   struct breakpoint *b;
3674   CORE_ADDR last_addr = (CORE_ADDR) -1;
3675   int nr_printable_breakpoints;
3676   struct cleanup *bkpttbl_chain;
3677   
3678   /* Compute the number of rows in the table. */
3679   nr_printable_breakpoints = 0;
3680   ALL_BREAKPOINTS (b)
3681     if (bnum == -1
3682         || bnum == b->number)
3683       {
3684         if (allflag || user_settable_breakpoint (b))
3685           nr_printable_breakpoints++;
3686       }
3687
3688   if (addressprint)
3689     bkpttbl_chain 
3690       = make_cleanup_ui_out_table_begin_end (uiout, 6, nr_printable_breakpoints,
3691                                              "BreakpointTable");
3692   else
3693     bkpttbl_chain 
3694       = make_cleanup_ui_out_table_begin_end (uiout, 5, nr_printable_breakpoints,
3695                                              "BreakpointTable");
3696
3697   if (nr_printable_breakpoints > 0)
3698     annotate_breakpoints_headers ();
3699   if (nr_printable_breakpoints > 0)
3700     annotate_field (0);
3701   ui_out_table_header (uiout, 3, ui_left, "number", "Num");             /* 1 */
3702   if (nr_printable_breakpoints > 0)
3703     annotate_field (1);
3704   ui_out_table_header (uiout, 14, ui_left, "type", "Type");             /* 2 */
3705   if (nr_printable_breakpoints > 0)
3706     annotate_field (2);
3707   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");              /* 3 */
3708   if (nr_printable_breakpoints > 0)
3709     annotate_field (3);
3710   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
3711   if (addressprint)
3712         {
3713           if (nr_printable_breakpoints > 0)
3714             annotate_field (4);
3715           if (TARGET_ADDR_BIT <= 32)
3716             ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
3717           else
3718             ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
3719         }
3720   if (nr_printable_breakpoints > 0)
3721     annotate_field (5);
3722   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
3723   ui_out_table_body (uiout);
3724   if (nr_printable_breakpoints > 0)
3725     annotate_breakpoints_table ();
3726
3727   ALL_BREAKPOINTS (b)
3728     if (bnum == -1
3729         || bnum == b->number)
3730       {
3731         /* We only print out user settable breakpoints unless the
3732            allflag is set. */
3733         if (allflag || user_settable_breakpoint (b))
3734           print_one_breakpoint (b, &last_addr);
3735       }
3736   
3737   do_cleanups (bkpttbl_chain);
3738
3739   if (nr_printable_breakpoints == 0)
3740     {
3741       if (bnum == -1)
3742         ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
3743       else
3744         ui_out_message (uiout, 0, "No breakpoint or watchpoint number %d.\n",
3745                         bnum);
3746     }
3747   else
3748     {
3749       /* Compare against (CORE_ADDR)-1 in case some compiler decides
3750          that a comparison of an unsigned with -1 is always false.  */
3751       if (last_addr != (CORE_ADDR) -1)
3752         set_next_address (last_addr);
3753     }
3754
3755   /* FIXME? Should this be moved up so that it is only called when
3756      there have been breakpoints? */
3757   annotate_breakpoints_table_end ();
3758 }
3759
3760 static void
3761 breakpoints_info (char *bnum_exp, int from_tty)
3762 {
3763   int bnum = -1;
3764
3765   if (bnum_exp)
3766     bnum = parse_and_eval_long (bnum_exp);
3767
3768   breakpoint_1 (bnum, 0);
3769 }
3770
3771 static void
3772 maintenance_info_breakpoints (char *bnum_exp, int from_tty)
3773 {
3774   int bnum = -1;
3775
3776   if (bnum_exp)
3777     bnum = parse_and_eval_long (bnum_exp);
3778
3779   breakpoint_1 (bnum, 1);
3780 }
3781
3782 /* Print a message describing any breakpoints set at PC.  */
3783
3784 static void
3785 describe_other_breakpoints (CORE_ADDR pc, asection *section)
3786 {
3787   int others = 0;
3788   struct breakpoint *b;
3789
3790   ALL_BREAKPOINTS (b)
3791     if (b->loc->address == pc)  /* address match / overlay match */
3792       if (!b->pending && (!overlay_debugging || b->loc->section == section))
3793         others++;
3794   if (others > 0)
3795     {
3796       if (others == 1)
3797         printf_filtered (_("Note: breakpoint "));
3798       else /* if (others == ???) */
3799         printf_filtered (_("Note: breakpoints "));
3800       ALL_BREAKPOINTS (b)
3801         if (b->loc->address == pc)      /* address match / overlay match */
3802           if (!b->pending && (!overlay_debugging || b->loc->section == section))
3803             {
3804               others--;
3805               printf_filtered ("%d%s%s ",
3806                                b->number,
3807                                ((b->enable_state == bp_disabled || 
3808                                  b->enable_state == bp_shlib_disabled || 
3809                                  b->enable_state == bp_call_disabled) 
3810                                 ? " (disabled)" 
3811                                 : b->enable_state == bp_permanent 
3812                                 ? " (permanent)"
3813                                 : ""),
3814                                (others > 1) ? "," 
3815                                : ((others == 1) ? " and" : ""));
3816             }
3817       printf_filtered (_("also set at pc "));
3818       deprecated_print_address_numeric (pc, 1, gdb_stdout);
3819       printf_filtered (".\n");
3820     }
3821 }
3822 \f
3823 /* Set the default place to put a breakpoint
3824    for the `break' command with no arguments.  */
3825
3826 void
3827 set_default_breakpoint (int valid, CORE_ADDR addr, struct symtab *symtab,
3828                         int line)
3829 {
3830   default_breakpoint_valid = valid;
3831   default_breakpoint_address = addr;
3832   default_breakpoint_symtab = symtab;
3833   default_breakpoint_line = line;
3834 }
3835
3836 /* Return true iff it is meaningful to use the address member of
3837    BPT.  For some breakpoint types, the address member is irrelevant
3838    and it makes no sense to attempt to compare it to other addresses
3839    (or use it for any other purpose either).
3840
3841    More specifically, each of the following breakpoint types will always
3842    have a zero valued address and we don't want check_duplicates() to mark
3843    breakpoints of any of these types to be a duplicate of an actual
3844    breakpoint at address zero:
3845
3846       bp_watchpoint
3847       bp_hardware_watchpoint
3848       bp_read_watchpoint
3849       bp_access_watchpoint
3850       bp_catch_exec
3851       bp_longjmp_resume
3852       bp_catch_fork
3853       bp_catch_vork */
3854
3855 static int
3856 breakpoint_address_is_meaningful (struct breakpoint *bpt)
3857 {
3858   enum bptype type = bpt->type;
3859
3860   return (type != bp_watchpoint
3861           && type != bp_hardware_watchpoint
3862           && type != bp_read_watchpoint
3863           && type != bp_access_watchpoint
3864           && type != bp_catch_exec
3865           && type != bp_longjmp_resume
3866           && type != bp_catch_fork
3867           && type != bp_catch_vfork);
3868 }
3869
3870 /* Rescan breakpoints at the same address and section as BPT,
3871    marking the first one as "first" and any others as "duplicates".
3872    This is so that the bpt instruction is only inserted once.
3873    If we have a permanent breakpoint at the same place as BPT, make
3874    that one the official one, and the rest as duplicates.  */
3875
3876 static void
3877 check_duplicates (struct breakpoint *bpt)
3878 {
3879   struct bp_location *b;
3880   int count = 0;
3881   struct bp_location *perm_bp = 0;
3882   CORE_ADDR address = bpt->loc->address;
3883   asection *section = bpt->loc->section;
3884
3885   if (! breakpoint_address_is_meaningful (bpt))
3886     return;
3887
3888   ALL_BP_LOCATIONS (b)
3889     if (b->owner->enable_state != bp_disabled
3890         && b->owner->enable_state != bp_shlib_disabled
3891         && !b->owner->pending
3892         && b->owner->enable_state != bp_call_disabled
3893         && b->address == address        /* address / overlay match */
3894         && (!overlay_debugging || b->section == section)
3895         && breakpoint_address_is_meaningful (b->owner))
3896     {
3897       /* Have we found a permanent breakpoint?  */
3898       if (b->owner->enable_state == bp_permanent)
3899         {
3900           perm_bp = b;
3901           break;
3902         }
3903         
3904       count++;
3905       b->duplicate = count > 1;
3906     }
3907
3908   /* If we found a permanent breakpoint at this address, go over the
3909      list again and declare all the other breakpoints there to be the
3910      duplicates.  */
3911   if (perm_bp)
3912     {
3913       perm_bp->duplicate = 0;
3914
3915       /* Permanent breakpoint should always be inserted.  */
3916       if (! perm_bp->inserted)
3917         internal_error (__FILE__, __LINE__,
3918                         _("allegedly permanent breakpoint is not "
3919                         "actually inserted"));
3920
3921       ALL_BP_LOCATIONS (b)
3922         if (b != perm_bp)
3923           {
3924             if (b->owner->enable_state != bp_disabled
3925                 && b->owner->enable_state != bp_shlib_disabled
3926                 && !b->owner->pending
3927                 && b->owner->enable_state != bp_call_disabled
3928                 && b->address == address        /* address / overlay match */
3929                 && (!overlay_debugging || b->section == section)
3930                 && breakpoint_address_is_meaningful (b->owner))
3931               {
3932                 if (b->inserted)
3933                   internal_error (__FILE__, __LINE__,
3934                                   _("another breakpoint was inserted on top of "
3935                                   "a permanent breakpoint"));
3936
3937                 b->duplicate = 1;
3938               }
3939           }
3940     }
3941 }
3942
3943 static void
3944 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
3945                                int bnum, int have_bnum)
3946 {
3947   char astr1[40];
3948   char astr2[40];
3949
3950   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
3951   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
3952   if (have_bnum)
3953     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
3954              bnum, astr1, astr2);
3955   else
3956     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
3957 }
3958
3959 /* Adjust a breakpoint's address to account for architectural constraints
3960    on breakpoint placement.  Return the adjusted address.  Note: Very
3961    few targets require this kind of adjustment.  For most targets,
3962    this function is simply the identity function.  */
3963
3964 static CORE_ADDR
3965 adjust_breakpoint_address (CORE_ADDR bpaddr, enum bptype bptype)
3966 {
3967   if (!gdbarch_adjust_breakpoint_address_p (current_gdbarch))
3968     {
3969       /* Very few targets need any kind of breakpoint adjustment.  */
3970       return bpaddr;
3971     }
3972   else if (bptype == bp_watchpoint
3973            || bptype == bp_hardware_watchpoint
3974            || bptype == bp_read_watchpoint
3975            || bptype == bp_access_watchpoint
3976            || bptype == bp_catch_fork
3977            || bptype == bp_catch_vfork
3978            || bptype == bp_catch_exec)
3979     {
3980       /* Watchpoints and the various bp_catch_* eventpoints should not
3981          have their addresses modified.  */
3982       return bpaddr;
3983     }
3984   else
3985     {
3986       CORE_ADDR adjusted_bpaddr;
3987
3988       /* Some targets have architectural constraints on the placement
3989          of breakpoint instructions.  Obtain the adjusted address.  */
3990       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (current_gdbarch,
3991                                                            bpaddr);
3992
3993       /* An adjusted breakpoint address can significantly alter
3994          a user's expectations.  Print a warning if an adjustment
3995          is required.  */
3996       if (adjusted_bpaddr != bpaddr)
3997         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
3998
3999       return adjusted_bpaddr;
4000     }
4001 }
4002
4003 /* Allocate a struct bp_location.  */
4004
4005 static struct bp_location *
4006 allocate_bp_location (struct breakpoint *bpt, enum bptype bp_type)
4007 {
4008   struct bp_location *loc, *loc_p;
4009
4010   loc = xmalloc (sizeof (struct bp_location));
4011   memset (loc, 0, sizeof (*loc));
4012
4013   loc->owner = bpt;
4014
4015   switch (bp_type)
4016     {
4017     case bp_breakpoint:
4018     case bp_until:
4019     case bp_finish:
4020     case bp_longjmp:
4021     case bp_longjmp_resume:
4022     case bp_step_resume:
4023     case bp_through_sigtramp:
4024     case bp_watchpoint_scope:
4025     case bp_call_dummy:
4026     case bp_shlib_event:
4027     case bp_thread_event:
4028     case bp_overlay_event:
4029     case bp_catch_load:
4030     case bp_catch_unload:
4031       loc->loc_type = bp_loc_software_breakpoint;
4032       break;
4033     case bp_hardware_breakpoint:
4034       loc->loc_type = bp_loc_hardware_breakpoint;
4035       break;
4036     case bp_hardware_watchpoint:
4037     case bp_read_watchpoint:
4038     case bp_access_watchpoint:
4039       loc->loc_type = bp_loc_hardware_watchpoint;
4040       break;
4041     case bp_watchpoint:
4042     case bp_catch_fork:
4043     case bp_catch_vfork:
4044     case bp_catch_exec:
4045     case bp_catch_catch:
4046     case bp_catch_throw:
4047       loc->loc_type = bp_loc_other;
4048       break;
4049     default:
4050       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
4051     }
4052
4053   /* Add this breakpoint to the end of the chain.  */
4054
4055   loc_p = bp_location_chain;
4056   if (loc_p == 0)
4057     bp_location_chain = loc;
4058   else
4059     {
4060       while (loc_p->next)
4061         loc_p = loc_p->next;
4062       loc_p->next = loc;
4063     }
4064
4065   return loc;
4066 }
4067
4068 /* set_raw_breakpoint() is a low level routine for allocating and
4069    partially initializing a breakpoint of type BPTYPE.  The newly
4070    created breakpoint's address, section, source file name, and line
4071    number are provided by SAL.  The newly created and partially
4072    initialized breakpoint is added to the breakpoint chain and
4073    is also returned as the value of this function.
4074
4075    It is expected that the caller will complete the initialization of
4076    the newly created breakpoint struct as well as output any status
4077    information regarding the creation of a new breakpoint.  In
4078    particular, set_raw_breakpoint() does NOT set the breakpoint
4079    number!  Care should be taken to not allow an error() to occur
4080    prior to completing the initialization of the breakpoint.  If this
4081    should happen, a bogus breakpoint will be left on the chain.  */
4082
4083 struct breakpoint *
4084 set_raw_breakpoint (struct symtab_and_line sal, enum bptype bptype)
4085 {
4086   struct breakpoint *b, *b1;
4087
4088   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
4089   memset (b, 0, sizeof (*b));
4090   b->loc = allocate_bp_location (b, bptype);
4091   b->loc->requested_address = sal.pc;
4092   b->loc->address = adjust_breakpoint_address (b->loc->requested_address,
4093                                                bptype);
4094   if (sal.symtab == NULL)
4095     b->source_file = NULL;
4096   else
4097     b->source_file = savestring (sal.symtab->filename,
4098                                  strlen (sal.symtab->filename));
4099   b->loc->section = sal.section;
4100   b->type = bptype;
4101   b->language = current_language->la_language;
4102   b->input_radix = input_radix;
4103   b->thread = -1;
4104   b->line_number = sal.line;
4105   b->enable_state = bp_enabled;
4106   b->next = 0;
4107   b->silent = 0;
4108   b->ignore_count = 0;
4109   b->commands = NULL;
4110   b->frame_id = null_frame_id;
4111   b->dll_pathname = NULL;
4112   b->triggered_dll_pathname = NULL;
4113   b->forked_inferior_pid = 0;
4114   b->exec_pathname = NULL;
4115   b->ops = NULL;
4116   b->pending = 0;
4117
4118   /* Add this breakpoint to the end of the chain
4119      so that a list of breakpoints will come out in order
4120      of increasing numbers.  */
4121
4122   b1 = breakpoint_chain;
4123   if (b1 == 0)
4124     breakpoint_chain = b;
4125   else
4126     {
4127       while (b1->next)
4128         b1 = b1->next;
4129       b1->next = b;
4130     }
4131
4132   check_duplicates (b);
4133   breakpoints_changed ();
4134
4135   return b;
4136 }
4137
4138
4139 /* Note that the breakpoint object B describes a permanent breakpoint
4140    instruction, hard-wired into the inferior's code.  */
4141 void
4142 make_breakpoint_permanent (struct breakpoint *b)
4143 {
4144   b->enable_state = bp_permanent;
4145
4146   /* By definition, permanent breakpoints are already present in the code.  */
4147   b->loc->inserted = 1;
4148 }
4149
4150 static struct breakpoint *
4151 create_internal_breakpoint (CORE_ADDR address, enum bptype type)
4152 {
4153   static int internal_breakpoint_number = -1;
4154   struct symtab_and_line sal;
4155   struct breakpoint *b;
4156
4157   init_sal (&sal);              /* initialize to zeroes */
4158
4159   sal.pc = address;
4160   sal.section = find_pc_overlay (sal.pc);
4161
4162   b = set_raw_breakpoint (sal, type);
4163   b->number = internal_breakpoint_number--;
4164   b->disposition = disp_donttouch;
4165
4166   return b;
4167 }
4168
4169
4170 static void
4171 create_longjmp_breakpoint (char *func_name)
4172 {
4173   struct breakpoint *b;
4174   struct minimal_symbol *m;
4175
4176   if (func_name == NULL)
4177     b = create_internal_breakpoint (0, bp_longjmp_resume);
4178   else
4179     {
4180       if ((m = lookup_minimal_symbol_text (func_name, NULL)) == NULL)
4181         return;
4182  
4183       b = create_internal_breakpoint (SYMBOL_VALUE_ADDRESS (m), bp_longjmp);
4184     }
4185
4186   b->enable_state = bp_disabled;
4187   b->silent = 1;
4188   if (func_name)
4189     b->addr_string = xstrdup (func_name);
4190 }
4191
4192 /* Call this routine when stepping and nexting to enable a breakpoint
4193    if we do a longjmp().  When we hit that breakpoint, call
4194    set_longjmp_resume_breakpoint() to figure out where we are going. */
4195
4196 void
4197 enable_longjmp_breakpoint (void)
4198 {
4199   struct breakpoint *b;
4200
4201   ALL_BREAKPOINTS (b)
4202     if (b->type == bp_longjmp)
4203     {
4204       b->enable_state = bp_enabled;
4205       check_duplicates (b);
4206     }
4207 }
4208
4209 void
4210 disable_longjmp_breakpoint (void)
4211 {
4212   struct breakpoint *b;
4213
4214   ALL_BREAKPOINTS (b)
4215     if (b->type == bp_longjmp
4216         || b->type == bp_longjmp_resume)
4217     {
4218       b->enable_state = bp_disabled;
4219       check_duplicates (b);
4220     }
4221 }
4222
4223 static void
4224 create_overlay_event_breakpoint (char *func_name)
4225 {
4226   struct breakpoint *b;
4227   struct minimal_symbol *m;
4228
4229   if ((m = lookup_minimal_symbol_text (func_name, NULL)) == NULL)
4230     return;
4231  
4232   b = create_internal_breakpoint (SYMBOL_VALUE_ADDRESS (m), 
4233                                   bp_overlay_event);
4234   b->addr_string = xstrdup (func_name);
4235
4236   if (overlay_debugging == ovly_auto)
4237     {
4238       b->enable_state = bp_enabled;
4239       overlay_events_enabled = 1;
4240     }
4241   else 
4242     {
4243       b->enable_state = bp_disabled;
4244       overlay_events_enabled = 0;
4245     }
4246 }
4247
4248 void
4249 enable_overlay_breakpoints (void)
4250 {
4251   struct breakpoint *b;
4252
4253   ALL_BREAKPOINTS (b)
4254     if (b->type == bp_overlay_event)
4255     {
4256       b->enable_state = bp_enabled;
4257       check_duplicates (b);
4258       overlay_events_enabled = 1;
4259     }
4260 }
4261
4262 void
4263 disable_overlay_breakpoints (void)
4264 {
4265   struct breakpoint *b;
4266
4267   ALL_BREAKPOINTS (b)
4268     if (b->type == bp_overlay_event)
4269     {
4270       b->enable_state = bp_disabled;
4271       check_duplicates (b);
4272       overlay_events_enabled = 0;
4273     }
4274 }
4275
4276 struct breakpoint *
4277 create_thread_event_breakpoint (CORE_ADDR address)
4278 {
4279   struct breakpoint *b;
4280
4281   b = create_internal_breakpoint (address, bp_thread_event);
4282   
4283   b->enable_state = bp_enabled;
4284   /* addr_string has to be used or breakpoint_re_set will delete me.  */
4285   b->addr_string = xstrprintf ("*0x%s", paddr (b->loc->address));
4286
4287   return b;
4288 }
4289
4290 void
4291 remove_thread_event_breakpoints (void)
4292 {
4293   struct breakpoint *b, *temp;
4294
4295   ALL_BREAKPOINTS_SAFE (b, temp)
4296     if (b->type == bp_thread_event)
4297       delete_breakpoint (b);
4298 }
4299
4300 struct captured_parse_breakpoint_args
4301   {
4302     char **arg_p;
4303     struct symtabs_and_lines *sals_p;
4304     char ***addr_string_p;
4305     int *not_found_ptr;
4306   };
4307
4308 struct lang_and_radix
4309   {
4310     enum language lang;
4311     int radix;
4312   };
4313
4314 /* Cleanup helper routine to restore the current language and
4315    input radix.  */
4316 static void
4317 do_restore_lang_radix_cleanup (void *old)
4318 {
4319   struct lang_and_radix *p = old;
4320   set_language (p->lang);
4321   input_radix = p->radix;
4322 }
4323
4324 /* Try and resolve a pending breakpoint.  */
4325 static int
4326 resolve_pending_breakpoint (struct breakpoint *b)
4327 {
4328   /* Try and reparse the breakpoint in case the shared library
4329      is now loaded.  */
4330   struct symtabs_and_lines sals;
4331   struct symtab_and_line pending_sal;
4332   char **cond_string = (char **) NULL;
4333   char *copy_arg = b->addr_string;
4334   char **addr_string;
4335   char *errmsg;
4336   int rc;
4337   int not_found = 0;
4338   struct ui_file *old_gdb_stderr;
4339   struct lang_and_radix old_lr;
4340   struct cleanup *old_chain;
4341   
4342   /* Set language, input-radix, then reissue breakpoint command. 
4343      Ensure the language and input-radix are restored afterwards.  */
4344   old_lr.lang = current_language->la_language;
4345   old_lr.radix = input_radix;
4346   old_chain = make_cleanup (do_restore_lang_radix_cleanup, &old_lr);
4347   
4348   set_language (b->language);
4349   input_radix = b->input_radix;
4350   rc = break_command_1 (b->addr_string, b->flag, b->from_tty, b);
4351   
4352   if (rc == GDB_RC_OK)
4353     /* Pending breakpoint has been resolved.  */
4354     printf_filtered (_("Pending breakpoint \"%s\" resolved\n"), b->addr_string);
4355
4356   do_cleanups (old_chain);
4357   return rc;
4358 }
4359
4360 void
4361 remove_solib_event_breakpoints (void)
4362 {
4363   struct breakpoint *b, *temp;
4364
4365   ALL_BREAKPOINTS_SAFE (b, temp)
4366     if (b->type == bp_shlib_event)
4367       delete_breakpoint (b);
4368 }
4369
4370 struct breakpoint *
4371 create_solib_event_breakpoint (CORE_ADDR address)
4372 {
4373   struct breakpoint *b;
4374
4375   b = create_internal_breakpoint (address, bp_shlib_event);
4376   return b;
4377 }
4378
4379 /* Disable any breakpoints that are on code in shared libraries.  Only
4380    apply to enabled breakpoints, disabled ones can just stay disabled.  */
4381
4382 void
4383 disable_breakpoints_in_shlibs (int silent)
4384 {
4385   struct breakpoint *b;
4386   int disabled_shlib_breaks = 0;
4387
4388   /* See also: insert_breakpoints, under DISABLE_UNSETTABLE_BREAK. */
4389   ALL_BREAKPOINTS (b)
4390   {
4391     if (((b->type == bp_breakpoint) || (b->type == bp_hardware_breakpoint))
4392         && breakpoint_enabled (b) && !b->loc->duplicate
4393 #ifdef PC_SOLIB
4394         && PC_SOLIB (b->loc->address)
4395 #else
4396         && solib_address (b->loc->address)
4397 #endif
4398         )
4399       {
4400         b->enable_state = bp_shlib_disabled;
4401         if (!silent)
4402           {
4403             if (!disabled_shlib_breaks)
4404               {
4405                 target_terminal_ours_for_output ();
4406                 warning (_("Temporarily disabling shared library breakpoints:"));
4407               }
4408             disabled_shlib_breaks = 1;
4409             warning (_("breakpoint #%d "), b->number);
4410           }
4411       }
4412   }
4413 }
4414
4415 /* Disable any breakpoints that are in in an unloaded shared library.  Only
4416    apply to enabled breakpoints, disabled ones can just stay disabled.  */
4417
4418 void
4419 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
4420 {
4421   struct breakpoint *b;
4422   int disabled_shlib_breaks = 0;
4423
4424   /* See also: insert_breakpoints, under DISABLE_UNSETTABLE_BREAK.  */
4425   ALL_BREAKPOINTS (b)
4426   {
4427     if ((b->loc->loc_type == bp_loc_hardware_breakpoint
4428         || b->loc->loc_type == bp_loc_software_breakpoint)
4429         && breakpoint_enabled (b) && !b->loc->duplicate)
4430       {
4431 #ifdef PC_SOLIB
4432         char *so_name = PC_SOLIB (b->loc->address);
4433 #else
4434         char *so_name = solib_address (b->loc->address);
4435 #endif
4436         if (so_name && !strcmp (so_name, solib->so_name))
4437           {
4438             b->enable_state = bp_shlib_disabled;
4439             /* At this point, we cannot rely on remove_breakpoint
4440                succeeding so we must mark the breakpoint as not inserted
4441                to prevent future errors occurring in remove_breakpoints.  */
4442             b->loc->inserted = 0;
4443             if (!disabled_shlib_breaks)
4444               {
4445                 target_terminal_ours_for_output ();
4446                 warning (_("Temporarily disabling breakpoints for unloaded shared library \"%s\""),
4447                           so_name);
4448               }
4449             disabled_shlib_breaks = 1;
4450           }
4451       }
4452   }
4453 }
4454
4455 /* Try to reenable any breakpoints in shared libraries.  */
4456 void
4457 re_enable_breakpoints_in_shlibs (void)
4458 {
4459   struct breakpoint *b, *tmp;
4460
4461   ALL_BREAKPOINTS_SAFE (b, tmp)
4462   {
4463     if (b->enable_state == bp_shlib_disabled)
4464       {
4465         gdb_byte buf[1];
4466         char *lib;
4467         
4468         /* Do not reenable the breakpoint if the shared library is
4469            still not mapped in.  */
4470 #ifdef PC_SOLIB
4471         lib = PC_SOLIB (b->loc->address);
4472 #else
4473         lib = solib_address (b->loc->address);
4474 #endif
4475         if (lib != NULL && target_read_memory (b->loc->address, buf, 1) == 0)
4476           b->enable_state = bp_enabled;
4477       }
4478     else if (b->pending && (b->enable_state == bp_enabled))
4479       {
4480         if (resolve_pending_breakpoint (b) == GDB_RC_OK)
4481           delete_breakpoint (b);
4482       }
4483   }
4484 }
4485
4486 static void
4487 solib_load_unload_1 (char *hookname, int tempflag, char *dll_pathname,
4488                      char *cond_string, enum bptype bp_kind)
4489 {
4490   struct breakpoint *b;
4491   struct symtabs_and_lines sals;
4492   struct cleanup *old_chain;
4493   struct cleanup *canonical_strings_chain = NULL;
4494   char *addr_start = hookname;
4495   char *addr_end = NULL;
4496   char **canonical = (char **) NULL;
4497   int thread = -1;              /* All threads. */
4498
4499   /* Set a breakpoint on the specified hook.  */
4500   sals = decode_line_1 (&hookname, 1, (struct symtab *) NULL, 
4501                         0, &canonical, NULL);
4502   addr_end = hookname;
4503
4504   if (sals.nelts == 0)
4505     {
4506       warning (_("Unable to set a breakpoint on dynamic linker callback.\n"
4507                  "Suggest linking with /opt/langtools/lib/end.o.\n"
4508                  "GDB will be unable to track shl_load/shl_unload calls."));
4509       return;
4510     }
4511   if (sals.nelts != 1)
4512     {
4513       warning (_("Unable to set unique breakpoint on dynamic linker callback.\n"
4514                  "GDB will be unable to track shl_load/shl_unload calls."));
4515       return;
4516     }
4517
4518   /* Make sure that all storage allocated in decode_line_1 gets freed
4519      in case the following errors out.  */
4520   old_chain = make_cleanup (xfree, sals.sals);
4521   if (canonical != (char **) NULL)
4522     {
4523       make_cleanup (xfree, canonical);
4524       canonical_strings_chain = make_cleanup (null_cleanup, 0);
4525       if (canonical[0] != NULL)
4526         make_cleanup (xfree, canonical[0]);
4527     }
4528
4529   resolve_sal_pc (&sals.sals[0]);
4530
4531   /* Remove the canonical strings from the cleanup, they are needed below.  */
4532   if (canonical != (char **) NULL)
4533     discard_cleanups (canonical_strings_chain);
4534
4535   b = set_raw_breakpoint (sals.sals[0], bp_kind);
4536   set_breakpoint_count (breakpoint_count + 1);
4537   b->number = breakpoint_count;
4538   b->cond = NULL;
4539   b->cond_string = (cond_string == NULL) ? 
4540     NULL : savestring (cond_string, strlen (cond_string));
4541   b->thread = thread;
4542
4543   if (canonical != (char **) NULL && canonical[0] != NULL)
4544     b->addr_string = canonical[0];
4545   else if (addr_start)
4546     b->addr_string = savestring (addr_start, addr_end - addr_start);
4547
4548   b->enable_state = bp_enabled;
4549   b->disposition = tempflag ? disp_del : disp_donttouch;
4550
4551   if (dll_pathname == NULL)
4552     b->dll_pathname = NULL;
4553   else
4554     {
4555       b->dll_pathname = (char *) xmalloc (strlen (dll_pathname) + 1);
4556       strcpy (b->dll_pathname, dll_pathname);
4557     }
4558
4559   mention (b);
4560   do_cleanups (old_chain);
4561 }
4562
4563 void
4564 create_solib_load_event_breakpoint (char *hookname, int tempflag,
4565                                     char *dll_pathname, char *cond_string)
4566 {
4567   solib_load_unload_1 (hookname, tempflag, dll_pathname, 
4568                        cond_string, bp_catch_load);
4569 }
4570
4571 void
4572 create_solib_unload_event_breakpoint (char *hookname, int tempflag,
4573                                       char *dll_pathname, char *cond_string)
4574 {
4575   solib_load_unload_1 (hookname, tempflag, dll_pathname, 
4576                        cond_string, bp_catch_unload);
4577 }
4578
4579 static void
4580 create_fork_vfork_event_catchpoint (int tempflag, char *cond_string,
4581                                     enum bptype bp_kind)
4582 {
4583   struct symtab_and_line sal;
4584   struct breakpoint *b;
4585   int thread = -1;              /* All threads. */
4586
4587   init_sal (&sal);
4588   sal.pc = 0;
4589   sal.symtab = NULL;
4590   sal.line = 0;
4591
4592   b = set_raw_breakpoint (sal, bp_kind);
4593   set_breakpoint_count (breakpoint_count + 1);
4594   b->number = breakpoint_count;
4595   b->cond = NULL;
4596   b->cond_string = (cond_string == NULL) ? 
4597     NULL : savestring (cond_string, strlen (cond_string));
4598   b->thread = thread;
4599   b->addr_string = NULL;
4600   b->enable_state = bp_enabled;
4601   b->disposition = tempflag ? disp_del : disp_donttouch;
4602   b->forked_inferior_pid = 0;
4603
4604   mention (b);
4605 }
4606
4607 void
4608 create_fork_event_catchpoint (int tempflag, char *cond_string)
4609 {
4610   create_fork_vfork_event_catchpoint (tempflag, cond_string, bp_catch_fork);
4611 }
4612
4613 void
4614 create_vfork_event_catchpoint (int tempflag, char *cond_string)
4615 {
4616   create_fork_vfork_event_catchpoint (tempflag, cond_string, bp_catch_vfork);
4617 }
4618
4619 void
4620 create_exec_event_catchpoint (int tempflag, char *cond_string)
4621 {
4622   struct symtab_and_line sal;
4623   struct breakpoint *b;
4624   int thread = -1;              /* All threads. */
4625
4626   init_sal (&sal);
4627   sal.pc = 0;
4628   sal.symtab = NULL;
4629   sal.line = 0;
4630
4631   b = set_raw_breakpoint (sal, bp_catch_exec);
4632   set_breakpoint_count (breakpoint_count + 1);
4633   b->number = breakpoint_count;
4634   b->cond = NULL;
4635   b->cond_string = (cond_string == NULL) ?
4636     NULL : savestring (cond_string, strlen (cond_string));
4637   b->thread = thread;
4638   b->addr_string = NULL;
4639   b->enable_state = bp_enabled;
4640   b->disposition = tempflag ? disp_del : disp_donttouch;
4641
4642   mention (b);
4643 }
4644
4645 static int
4646 hw_breakpoint_used_count (void)
4647 {
4648   struct breakpoint *b;
4649   int i = 0;
4650
4651   ALL_BREAKPOINTS (b)
4652   {
4653     if (b->type == bp_hardware_breakpoint && b->enable_state == bp_enabled)
4654       i++;
4655   }
4656
4657   return i;
4658 }
4659
4660 static int
4661 hw_watchpoint_used_count (enum bptype type, int *other_type_used)
4662 {
4663   struct breakpoint *b;
4664   int i = 0;
4665
4666   *other_type_used = 0;
4667   ALL_BREAKPOINTS (b)
4668   {
4669     if (breakpoint_enabled (b))
4670       {
4671         if (b->type == type)
4672           i++;
4673         else if ((b->type == bp_hardware_watchpoint ||
4674                   b->type == bp_read_watchpoint ||
4675                   b->type == bp_access_watchpoint))
4676           *other_type_used = 1;
4677       }
4678   }
4679   return i;
4680 }
4681
4682 /* Call this after hitting the longjmp() breakpoint.  Use this to set
4683    a new breakpoint at the target of the jmp_buf.
4684
4685    FIXME - This ought to be done by setting a temporary breakpoint
4686    that gets deleted automatically... */
4687
4688 void
4689 set_longjmp_resume_breakpoint (CORE_ADDR pc, struct frame_id frame_id)
4690 {
4691   struct breakpoint *b;
4692
4693   ALL_BREAKPOINTS (b)
4694     if (b->type == bp_longjmp_resume)
4695     {
4696       b->loc->requested_address = pc;
4697       b->loc->address = adjust_breakpoint_address (b->loc->requested_address,
4698                                                    b->type);
4699       b->enable_state = bp_enabled;
4700       b->frame_id = frame_id;
4701       check_duplicates (b);
4702       return;
4703     }
4704 }
4705
4706 void
4707 disable_watchpoints_before_interactive_call_start (void)
4708 {
4709   struct breakpoint *b;
4710
4711   ALL_BREAKPOINTS (b)
4712   {
4713     if (((b->type == bp_watchpoint)
4714          || (b->type == bp_hardware_watchpoint)
4715          || (b->type == bp_read_watchpoint)
4716          || (b->type == bp_access_watchpoint)
4717          || ep_is_exception_catchpoint (b))
4718         && breakpoint_enabled (b))
4719       {
4720         b->enable_state = bp_call_disabled;
4721         check_duplicates (b);
4722       }
4723   }
4724 }
4725
4726 void
4727 enable_watchpoints_after_interactive_call_stop (void)
4728 {
4729   struct breakpoint *b;
4730
4731   ALL_BREAKPOINTS (b)
4732   {
4733     if (((b->type == bp_watchpoint)
4734          || (b->type == bp_hardware_watchpoint)
4735          || (b->type == bp_read_watchpoint)
4736          || (b->type == bp_access_watchpoint)
4737          || ep_is_exception_catchpoint (b))
4738         && (b->enable_state == bp_call_disabled))
4739       {
4740         b->enable_state = bp_enabled;
4741         check_duplicates (b);
4742       }
4743   }
4744 }
4745
4746
4747 /* Set a breakpoint that will evaporate an end of command
4748    at address specified by SAL.
4749    Restrict it to frame FRAME if FRAME is nonzero.  */
4750
4751 struct breakpoint *
4752 set_momentary_breakpoint (struct symtab_and_line sal, struct frame_id frame_id,
4753                           enum bptype type)
4754 {
4755   struct breakpoint *b;
4756   b = set_raw_breakpoint (sal, type);
4757   b->enable_state = bp_enabled;
4758   b->disposition = disp_donttouch;
4759   b->frame_id = frame_id;
4760
4761   /* If we're debugging a multi-threaded program, then we
4762      want momentary breakpoints to be active in only a 
4763      single thread of control.  */
4764   if (in_thread_list (inferior_ptid))
4765     b->thread = pid_to_thread_id (inferior_ptid);
4766
4767   return b;
4768 }
4769 \f
4770
4771 /* Tell the user we have just set a breakpoint B.  */
4772
4773 static void
4774 mention (struct breakpoint *b)
4775 {
4776   int say_where = 0;
4777   struct cleanup *old_chain, *ui_out_chain;
4778   struct ui_stream *stb;
4779
4780   stb = ui_out_stream_new (uiout);
4781   old_chain = make_cleanup_ui_out_stream_delete (stb);
4782
4783   /* FIXME: This is misplaced; mention() is called by things (like
4784      hitting a watchpoint) other than breakpoint creation.  It should
4785      be possible to clean this up and at the same time replace the
4786      random calls to breakpoint_changed with this hook, as has already
4787      been done for deprecated_delete_breakpoint_hook and so on.  */
4788   if (deprecated_create_breakpoint_hook)
4789     deprecated_create_breakpoint_hook (b);
4790   breakpoint_create_event (b->number);
4791
4792   if (b->ops != NULL && b->ops->print_mention != NULL)
4793     b->ops->print_mention (b);
4794   else
4795     switch (b->type)
4796       {
4797       case bp_none:
4798         printf_filtered (_("(apparently deleted?) Eventpoint %d: "), b->number);
4799         break;
4800       case bp_watchpoint:
4801         ui_out_text (uiout, "Watchpoint ");
4802         ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
4803         ui_out_field_int (uiout, "number", b->number);
4804         ui_out_text (uiout, ": ");
4805         print_expression (b->exp, stb->stream);
4806         ui_out_field_stream (uiout, "exp", stb);
4807         do_cleanups (ui_out_chain);
4808         break;
4809       case bp_hardware_watchpoint:
4810         ui_out_text (uiout, "Hardware watchpoint ");
4811         ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
4812         ui_out_field_int (uiout, "number", b->number);
4813         ui_out_text (uiout, ": ");
4814         print_expression (b->exp, stb->stream);
4815         ui_out_field_stream (uiout, "exp", stb);
4816         do_cleanups (ui_out_chain);
4817         break;
4818       case bp_read_watchpoint:
4819         ui_out_text (uiout, "Hardware read watchpoint ");
4820         ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
4821         ui_out_field_int (uiout, "number", b->number);
4822         ui_out_text (uiout, ": ");
4823         print_expression (b->exp, stb->stream);
4824         ui_out_field_stream (uiout, "exp", stb);
4825         do_cleanups (ui_out_chain);
4826         break;
4827       case bp_access_watchpoint:
4828         ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
4829         ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
4830         ui_out_field_int (uiout, "number", b->number);
4831         ui_out_text (uiout, ": ");
4832         print_expression (b->exp, stb->stream);
4833         ui_out_field_stream (uiout, "exp", stb);
4834         do_cleanups (ui_out_chain);
4835         break;
4836       case bp_breakpoint:
4837         if (ui_out_is_mi_like_p (uiout))
4838           {
4839             say_where = 0;
4840             break;
4841           }
4842         printf_filtered (_("Breakpoint %d"), b->number);
4843         say_where = 1;
4844         break;
4845       case bp_hardware_breakpoint:
4846         if (ui_out_is_mi_like_p (uiout))
4847           {
4848             say_where = 0;
4849             break;
4850           }
4851         printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
4852         say_where = 1;
4853         break;
4854       case bp_catch_load:
4855       case bp_catch_unload:
4856         printf_filtered (_("Catchpoint %d (%s %s)"),
4857                          b->number,
4858                          (b->type == bp_catch_load) ? "load" : "unload",
4859                          (b->dll_pathname != NULL) ? 
4860                          b->dll_pathname : "<any library>");
4861         break;
4862       case bp_catch_fork:
4863       case bp_catch_vfork:
4864         printf_filtered (_("Catchpoint %d (%s)"),
4865                          b->number,
4866                          (b->type == bp_catch_fork) ? "fork" : "vfork");
4867         break;
4868       case bp_catch_exec:
4869         printf_filtered (_("Catchpoint %d (exec)"),
4870                          b->number);
4871         break;
4872       case bp_catch_catch:
4873       case bp_catch_throw:
4874         printf_filtered (_("Catchpoint %d (%s)"),
4875                          b->number,
4876                          (b->type == bp_catch_catch) ? "catch" : "throw");
4877         break;
4878
4879       case bp_until:
4880       case bp_finish:
4881       case bp_longjmp:
4882       case bp_longjmp_resume:
4883       case bp_step_resume:
4884       case bp_through_sigtramp:
4885       case bp_call_dummy:
4886       case bp_watchpoint_scope:
4887       case bp_shlib_event:
4888       case bp_thread_event:
4889       case bp_overlay_event:
4890         break;
4891       }
4892
4893   if (say_where)
4894     {
4895       /* i18n: cagney/2005-02-11: Below needs to be merged into a
4896          single string.  */
4897       if (b->pending)
4898         {
4899           printf_filtered (_(" (%s) pending."), b->addr_string);
4900         }
4901       else
4902         {
4903           if (addressprint || b->source_file == NULL)
4904             {
4905               printf_filtered (" at ");
4906               deprecated_print_address_numeric (b->loc->address, 1, gdb_stdout);
4907             }
4908           if (b->source_file)
4909             printf_filtered (": file %s, line %d.",
4910                              b->source_file, b->line_number);
4911         }
4912     }
4913   do_cleanups (old_chain);
4914   if (ui_out_is_mi_like_p (uiout))
4915     return;
4916   printf_filtered ("\n");
4917 }
4918 \f
4919
4920 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
4921    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i],
4922    COND[i] and COND_STRING[i] values.
4923
4924    The parameter PENDING_BP points to a pending breakpoint that is
4925    the basis of the breakpoints currently being created.  The pending
4926    breakpoint may contain a separate condition string or commands
4927    that were added after the initial pending breakpoint was created.
4928
4929    NOTE: If the function succeeds, the caller is expected to cleanup
4930    the arrays ADDR_STRING, COND_STRING, COND and SALS (but not the
4931    array contents).  If the function fails (error() is called), the
4932    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4933    COND and SALS arrays and each of those arrays contents. */
4934
4935 static void
4936 create_breakpoints (struct symtabs_and_lines sals, char **addr_string,
4937                     struct expression **cond, char **cond_string,
4938                     enum bptype type, enum bpdisp disposition,
4939                     int thread, int ignore_count, int from_tty,
4940                     struct breakpoint *pending_bp)
4941 {
4942   if (type == bp_hardware_breakpoint)
4943     {
4944       int i = hw_breakpoint_used_count ();
4945       int target_resources_ok = 
4946         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint, 
4947                                             i + sals.nelts, 0);
4948       if (target_resources_ok == 0)
4949         error (_("No hardware breakpoint support in the target."));
4950       else if (target_resources_ok < 0)
4951         error (_("Hardware breakpoints used exceeds limit."));
4952     }
4953
4954   /* Now set all the breakpoints.  */
4955   {
4956     int i;
4957     for (i = 0; i < sals.nelts; i++)
4958       {
4959         struct breakpoint *b;
4960         struct symtab_and_line sal = sals.sals[i];
4961
4962         if (from_tty)
4963           describe_other_breakpoints (sal.pc, sal.section);
4964         
4965         b = set_raw_breakpoint (sal, type);
4966         set_breakpoint_count (breakpoint_count + 1);
4967         b->number = breakpoint_count;
4968         b->cond = cond[i];
4969         b->thread = thread;
4970         if (addr_string[i])
4971           b->addr_string = addr_string[i];
4972         else
4973           /* addr_string has to be used or breakpoint_re_set will delete
4974              me.  */
4975           b->addr_string = xstrprintf ("*0x%s", paddr (b->loc->address));
4976         b->cond_string = cond_string[i];
4977         b->ignore_count = ignore_count;
4978         b->enable_state = bp_enabled;
4979         b->disposition = disposition;
4980         /* If resolving a pending breakpoint, a check must be made to see if
4981            the user has specified a new condition or commands for the 
4982            breakpoint.  A new condition will override any condition that was 
4983            initially specified with the initial breakpoint command.  */
4984         if (pending_bp)
4985           {
4986             char *arg;
4987             if (pending_bp->cond_string)
4988               {
4989                 arg = pending_bp->cond_string;
4990                 b->cond_string = savestring (arg, strlen (arg));
4991                 b->cond = parse_exp_1 (&arg, block_for_pc (b->loc->address), 0);
4992                 if (*arg)
4993                   error (_("Junk at end of pending breakpoint condition expression"));
4994               }
4995             /* If there are commands associated with the breakpoint, they should 
4996                be copied too.  */
4997             if (pending_bp->commands)
4998               b->commands = copy_command_lines (pending_bp->commands);
4999             
5000             /* We have to copy over the ignore_count and thread as well.  */
5001             b->ignore_count = pending_bp->ignore_count;
5002             b->thread = pending_bp->thread;
5003           }
5004         mention (b);
5005       }
5006   }    
5007 }
5008
5009 /* Parse ARG which is assumed to be a SAL specification possibly
5010    followed by conditionals.  On return, SALS contains an array of SAL
5011    addresses found. ADDR_STRING contains a vector of (canonical)
5012    address strings. ARG points to the end of the SAL. */
5013
5014 static void
5015 parse_breakpoint_sals (char **address,
5016                        struct symtabs_and_lines *sals,
5017                        char ***addr_string,
5018                        int *not_found_ptr)
5019 {
5020   char *addr_start = *address;
5021   *addr_string = NULL;
5022   /* If no arg given, or if first arg is 'if ', use the default
5023      breakpoint. */
5024   if ((*address) == NULL
5025       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
5026     {
5027       if (default_breakpoint_valid)
5028         {
5029           struct symtab_and_line sal;
5030           init_sal (&sal);              /* initialize to zeroes */
5031           sals->sals = (struct symtab_and_line *)
5032             xmalloc (sizeof (struct symtab_and_line));
5033           sal.pc = default_breakpoint_address;
5034           sal.line = default_breakpoint_line;
5035           sal.symtab = default_breakpoint_symtab;
5036           sal.section = find_pc_overlay (sal.pc);
5037           sals->sals[0] = sal;
5038           sals->nelts = 1;
5039         }
5040       else
5041         error (_("No default breakpoint address now."));
5042     }
5043   else
5044     {
5045       /* Force almost all breakpoints to be in terms of the
5046          current_source_symtab (which is decode_line_1's default).  This
5047          should produce the results we want almost all of the time while
5048          leaving default_breakpoint_* alone.  
5049          ObjC: However, don't match an Objective-C method name which
5050          may have a '+' or '-' succeeded by a '[' */
5051          
5052       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
5053                         
5054       if (default_breakpoint_valid
5055           && (!cursal.symtab
5056               || ((strchr ("+-", (*address)[0]) != NULL)
5057                   && ((*address)[1] != '['))))
5058         *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
5059                                default_breakpoint_line, addr_string, 
5060                                not_found_ptr);
5061       else
5062         *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
5063                                addr_string, not_found_ptr);
5064     }
5065   /* For any SAL that didn't have a canonical string, fill one in. */
5066   if (sals->nelts > 0 && *addr_string == NULL)
5067     *addr_string = xcalloc (sals->nelts, sizeof (char **));
5068   if (addr_start != (*address))
5069     {
5070       int i;
5071       for (i = 0; i < sals->nelts; i++)
5072         {
5073           /* Add the string if not present. */
5074           if ((*addr_string)[i] == NULL)
5075             (*addr_string)[i] = savestring (addr_start, (*address) - addr_start);
5076         }
5077     }
5078 }
5079
5080
5081 /* Convert each SAL into a real PC.  Verify that the PC can be
5082    inserted as a breakpoint.  If it can't throw an error. */
5083
5084 static void
5085 breakpoint_sals_to_pc (struct symtabs_and_lines *sals,
5086                        char *address)
5087 {    
5088   int i;
5089   for (i = 0; i < sals->nelts; i++)
5090     {
5091       resolve_sal_pc (&sals->sals[i]);
5092
5093       /* It's possible for the PC to be nonzero, but still an illegal
5094          value on some targets.
5095
5096          For example, on HP-UX if you start gdb, and before running the
5097          inferior you try to set a breakpoint on a shared library function
5098          "foo" where the inferior doesn't call "foo" directly but does
5099          pass its address to another function call, then we do find a
5100          minimal symbol for the "foo", but it's address is invalid.
5101          (Appears to be an index into a table that the loader sets up
5102          when the inferior is run.)
5103
5104          Give the target a chance to bless sals.sals[i].pc before we
5105          try to make a breakpoint for it. */
5106 #ifdef DEPRECATED_PC_REQUIRES_RUN_BEFORE_USE
5107       if (DEPRECATED_PC_REQUIRES_RUN_BEFORE_USE (sals->sals[i].pc))
5108         {
5109           if (address == NULL)
5110             error (_("Cannot break without a running program."));
5111           else
5112             error (_("Cannot break on %s without a running program."), 
5113                    address);
5114         }
5115 #endif
5116     }
5117 }
5118
5119 static void
5120 do_captured_parse_breakpoint (struct ui_out *ui, void *data)
5121 {
5122   struct captured_parse_breakpoint_args *args = data;
5123   
5124   parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p, 
5125                          args->not_found_ptr);
5126 }
5127
5128 /* Set a breakpoint according to ARG (function, linenum or *address)
5129    flag: first bit  : 0 non-temporary, 1 temporary.
5130    second bit : 0 normal breakpoint, 1 hardware breakpoint. 
5131
5132    PENDING_BP is non-NULL when this function is being called to resolve
5133    a pending breakpoint.  */
5134
5135 static int
5136 break_command_1 (char *arg, int flag, int from_tty, struct breakpoint *pending_bp)
5137 {
5138   struct gdb_exception e;
5139   int tempflag, hardwareflag;
5140   struct symtabs_and_lines sals;
5141   struct expression **cond = 0;
5142   struct symtab_and_line pending_sal;
5143   char **cond_string = (char **) NULL;
5144   char *copy_arg;
5145   char *err_msg;
5146   char *addr_start = arg;
5147   char **addr_string;
5148   struct cleanup *old_chain;
5149   struct cleanup *breakpoint_chain = NULL;
5150   struct captured_parse_breakpoint_args parse_args;
5151   int i;
5152   int pending = 0;
5153   int thread = -1;
5154   int ignore_count = 0;
5155   int not_found = 0;
5156
5157   hardwareflag = flag & BP_HARDWAREFLAG;
5158   tempflag = flag & BP_TEMPFLAG;
5159
5160   sals.sals = NULL;
5161   sals.nelts = 0;
5162   addr_string = NULL;
5163
5164   parse_args.arg_p = &arg;
5165   parse_args.sals_p = &sals;
5166   parse_args.addr_string_p = &addr_string;
5167   parse_args.not_found_ptr = &not_found;
5168
5169   e = catch_exception (uiout, do_captured_parse_breakpoint, 
5170                        &parse_args, RETURN_MASK_ALL);
5171
5172   /* If caller is interested in rc value from parse, set value.  */
5173   switch (e.reason)
5174     {
5175     case RETURN_QUIT:
5176       exception_print (gdb_stderr, e);
5177       return e.reason;
5178     case RETURN_ERROR:
5179       switch (e.error)
5180         {
5181         case NOT_FOUND_ERROR:
5182           /* If called to resolve pending breakpoint, just return
5183              error code.  */
5184           if (pending_bp)
5185             return e.reason;
5186
5187           exception_print (gdb_stderr, e);
5188
5189           /* If pending breakpoint support is turned off, throw
5190              error.  */
5191
5192           if (pending_break_support == AUTO_BOOLEAN_FALSE)
5193             deprecated_throw_reason (RETURN_ERROR);
5194
5195           /* If pending breakpoint support is auto query and the user
5196              selects no, then simply return the error code.  */
5197           if (pending_break_support == AUTO_BOOLEAN_AUTO && 
5198               !nquery ("Make breakpoint pending on future shared library load? "))
5199             return e.reason;
5200
5201           /* At this point, either the user was queried about setting
5202              a pending breakpoint and selected yes, or pending
5203              breakpoint behavior is on and thus a pending breakpoint
5204              is defaulted on behalf of the user.  */
5205           copy_arg = xstrdup (addr_start);
5206           addr_string = &copy_arg;
5207           sals.nelts = 1;
5208           sals.sals = &pending_sal;
5209           pending_sal.pc = 0;
5210           pending = 1;
5211           break;
5212         default:
5213           exception_print (gdb_stderr, e);
5214           return e.reason;
5215         }
5216     default:
5217       if (!sals.nelts)
5218         return GDB_RC_FAIL;
5219     }
5220
5221   /* Create a chain of things that always need to be cleaned up. */
5222   old_chain = make_cleanup (null_cleanup, 0);
5223
5224   if (!pending)
5225     {
5226       /* Make sure that all storage allocated to SALS gets freed.  */
5227       make_cleanup (xfree, sals.sals);
5228       
5229       /* Cleanup the addr_string array but not its contents. */
5230       make_cleanup (xfree, addr_string);
5231     }
5232
5233   /* Allocate space for all the cond expressions. */
5234   cond = xcalloc (sals.nelts, sizeof (struct expression *));
5235   make_cleanup (xfree, cond);
5236
5237   /* Allocate space for all the cond strings. */
5238   cond_string = xcalloc (sals.nelts, sizeof (char **));
5239   make_cleanup (xfree, cond_string);
5240
5241   /* ----------------------------- SNIP -----------------------------
5242      Anything added to the cleanup chain beyond this point is assumed
5243      to be part of a breakpoint.  If the breakpoint create succeeds
5244      then the memory is not reclaimed. */
5245   breakpoint_chain = make_cleanup (null_cleanup, 0);
5246
5247   /* Mark the contents of the addr_string for cleanup.  These go on
5248      the breakpoint_chain and only occure if the breakpoint create
5249      fails. */
5250   for (i = 0; i < sals.nelts; i++)
5251     {
5252       if (addr_string[i] != NULL)
5253         make_cleanup (xfree, addr_string[i]);
5254     }
5255
5256   /* Resolve all line numbers to PC's and verify that the addresses
5257      are ok for the target.  */
5258   if (!pending)
5259     breakpoint_sals_to_pc (&sals, addr_start);
5260
5261   /* Verify that condition can be parsed, before setting any
5262      breakpoints.  Allocate a separate condition expression for each
5263      breakpoint. */
5264   thread = -1;                  /* No specific thread yet */
5265   if (!pending)
5266     {
5267       for (i = 0; i < sals.nelts; i++)
5268         {
5269           char *tok = arg;
5270           while (tok && *tok)
5271             {
5272               char *end_tok;
5273               int toklen;
5274               char *cond_start = NULL;
5275               char *cond_end = NULL;
5276               while (*tok == ' ' || *tok == '\t')
5277                 tok++;
5278               
5279               end_tok = tok;
5280               
5281               while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5282                 end_tok++;
5283               
5284               toklen = end_tok - tok;
5285               
5286               if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
5287                 {
5288                   tok = cond_start = end_tok + 1;
5289                   cond[i] = parse_exp_1 (&tok, block_for_pc (sals.sals[i].pc), 
5290                                          0);
5291                   make_cleanup (xfree, cond[i]);
5292                   cond_end = tok;
5293                   cond_string[i] = savestring (cond_start, 
5294                                                cond_end - cond_start);
5295                   make_cleanup (xfree, cond_string[i]);
5296                 }
5297               else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
5298                 {
5299                   char *tmptok;
5300                   
5301                   tok = end_tok + 1;
5302                   tmptok = tok;
5303                   thread = strtol (tok, &tok, 0);
5304                   if (tok == tmptok)
5305                     error (_("Junk after thread keyword."));
5306                   if (!valid_thread_id (thread))
5307                     error (_("Unknown thread %d."), thread);
5308                 }
5309               else
5310                 error (_("Junk at end of arguments."));
5311             }
5312         }
5313       create_breakpoints (sals, addr_string, cond, cond_string,
5314                           hardwareflag ? bp_hardware_breakpoint 
5315                           : bp_breakpoint,
5316                           tempflag ? disp_del : disp_donttouch,
5317                           thread, ignore_count, from_tty,
5318                           pending_bp);
5319     }
5320   else
5321     {
5322       struct symtab_and_line sal;
5323       struct breakpoint *b;
5324
5325       sal.symtab = NULL;
5326       sal.pc = 0;
5327
5328       make_cleanup (xfree, copy_arg);
5329
5330       b = set_raw_breakpoint (sal, hardwareflag ? bp_hardware_breakpoint 
5331                               : bp_breakpoint);
5332       set_breakpoint_count (breakpoint_count + 1);
5333       b->number = breakpoint_count;
5334       b->cond = *cond;
5335       b->thread = thread;
5336       b->addr_string = *addr_string;
5337       b->cond_string = *cond_string;
5338       b->ignore_count = ignore_count;
5339       b->pending = 1;
5340       b->disposition = tempflag ? disp_del : disp_donttouch;
5341       b->from_tty = from_tty;
5342       b->flag = flag;
5343       mention (b);
5344     }
5345   
5346   if (sals.nelts > 1)
5347     warning (_("Multiple breakpoints were set.\n"
5348                "Use the \"delete\" command to delete unwanted breakpoints."));
5349   /* That's it. Discard the cleanups for data inserted into the
5350      breakpoint. */
5351   discard_cleanups (breakpoint_chain);
5352   /* But cleanup everything else. */
5353   do_cleanups (old_chain);
5354
5355   return GDB_RC_OK;
5356 }
5357
5358 /* Set a breakpoint of TYPE/DISPOSITION according to ARG (function,
5359    linenum or *address) with COND and IGNORE_COUNT. */
5360
5361 struct captured_breakpoint_args
5362   {
5363     char *address;
5364     char *condition;
5365     int hardwareflag;
5366     int tempflag;
5367     int thread;
5368     int ignore_count;
5369   };
5370
5371 static int
5372 do_captured_breakpoint (struct ui_out *uiout, void *data)
5373 {
5374   struct captured_breakpoint_args *args = data;
5375   struct symtabs_and_lines sals;
5376   struct expression **cond;
5377   struct cleanup *old_chain;
5378   struct cleanup *breakpoint_chain = NULL;
5379   int i;
5380   char **addr_string;
5381   char **cond_string;
5382
5383   char *address_end;
5384
5385   /* Parse the source and lines spec.  Delay check that the expression
5386      didn't contain trailing garbage until after cleanups are in
5387      place. */
5388   sals.sals = NULL;
5389   sals.nelts = 0;
5390   address_end = args->address;
5391   addr_string = NULL;
5392   parse_breakpoint_sals (&address_end, &sals, &addr_string, 0);
5393
5394   if (!sals.nelts)
5395     return GDB_RC_NONE;
5396
5397   /* Create a chain of things at always need to be cleaned up. */
5398   old_chain = make_cleanup (null_cleanup, 0);
5399
5400   /* Always have a addr_string array, even if it is empty. */
5401   make_cleanup (xfree, addr_string);
5402
5403   /* Make sure that all storage allocated to SALS gets freed.  */
5404   make_cleanup (xfree, sals.sals);
5405
5406   /* Allocate space for all the cond expressions. */
5407   cond = xcalloc (sals.nelts, sizeof (struct expression *));
5408   make_cleanup (xfree, cond);
5409
5410   /* Allocate space for all the cond strings. */
5411   cond_string = xcalloc (sals.nelts, sizeof (char **));
5412   make_cleanup (xfree, cond_string);
5413
5414   /* ----------------------------- SNIP -----------------------------
5415      Anything added to the cleanup chain beyond this point is assumed
5416      to be part of a breakpoint.  If the breakpoint create goes
5417      through then that memory is not cleaned up. */
5418   breakpoint_chain = make_cleanup (null_cleanup, 0);
5419
5420   /* Mark the contents of the addr_string for cleanup.  These go on
5421      the breakpoint_chain and only occure if the breakpoint create
5422      fails. */
5423   for (i = 0; i < sals.nelts; i++)
5424     {
5425       if (addr_string[i] != NULL)
5426         make_cleanup (xfree, addr_string[i]);
5427     }
5428
5429   /* Wait until now before checking for garbage at the end of the
5430      address. That way cleanups can take care of freeing any
5431      memory. */
5432   if (*address_end != '\0')
5433     error (_("Garbage %s following breakpoint address"), address_end);
5434
5435   /* Resolve all line numbers to PC's.  */
5436   breakpoint_sals_to_pc (&sals, args->address);
5437
5438   /* Verify that conditions can be parsed, before setting any
5439      breakpoints.  */
5440   for (i = 0; i < sals.nelts; i++)
5441     {
5442       if (args->condition != NULL)
5443         {
5444           char *tok = args->condition;
5445           cond[i] = parse_exp_1 (&tok, block_for_pc (sals.sals[i].pc), 0);
5446           if (*tok != '\0')
5447             error (_("Garbage %s follows condition"), tok);
5448           make_cleanup (xfree, cond[i]);
5449           cond_string[i] = xstrdup (args->condition);
5450         }
5451     }
5452
5453   create_breakpoints (sals, addr_string, cond, cond_string,
5454                       args->hardwareflag ? bp_hardware_breakpoint : bp_breakpoint,
5455                       args->tempflag ? disp_del : disp_donttouch,
5456                       args->thread, args->ignore_count, 0/*from-tty*/, 
5457                       NULL/*pending_bp*/);
5458
5459   /* That's it. Discard the cleanups for data inserted into the
5460      breakpoint. */
5461   discard_cleanups (breakpoint_chain);
5462   /* But cleanup everything else. */
5463   do_cleanups (old_chain);
5464   return GDB_RC_OK;
5465 }
5466
5467 enum gdb_rc
5468 gdb_breakpoint (char *address, char *condition,
5469                 int hardwareflag, int tempflag,
5470                 int thread, int ignore_count,
5471                 char **error_message)
5472 {
5473   struct captured_breakpoint_args args;
5474   args.address = address;
5475   args.condition = condition;
5476   args.hardwareflag = hardwareflag;
5477   args.tempflag = tempflag;
5478   args.thread = thread;
5479   args.ignore_count = ignore_count;
5480   return catch_exceptions_with_msg (uiout, do_captured_breakpoint, &args,
5481                                     error_message, RETURN_MASK_ALL);
5482 }
5483
5484
5485 /* Helper function for break_command_1 and disassemble_command.  */
5486
5487 void
5488 resolve_sal_pc (struct symtab_and_line *sal)
5489 {
5490   CORE_ADDR pc;
5491
5492   if (sal->pc == 0 && sal->symtab != NULL)
5493     {
5494       if (!find_line_pc (sal->symtab, sal->line, &pc))
5495         error (_("No line %d in file \"%s\"."),
5496                sal->line, sal->symtab->filename);
5497       sal->pc = pc;
5498     }
5499
5500   if (sal->section == 0 && sal->symtab != NULL)
5501     {
5502       struct blockvector *bv;
5503       struct block *b;
5504       struct symbol *sym;
5505       int index;
5506
5507       bv = blockvector_for_pc_sect (sal->pc, 0, &index, sal->symtab);
5508       if (bv != NULL)
5509         {
5510           b = BLOCKVECTOR_BLOCK (bv, index);
5511           sym = block_function (b);
5512           if (sym != NULL)
5513             {
5514               fixup_symbol_section (sym, sal->symtab->objfile);
5515               sal->section = SYMBOL_BFD_SECTION (sym);
5516             }
5517           else
5518             {
5519               /* It really is worthwhile to have the section, so we'll just
5520                  have to look harder. This case can be executed if we have 
5521                  line numbers but no functions (as can happen in assembly 
5522                  source).  */
5523
5524               struct minimal_symbol *msym;
5525
5526               msym = lookup_minimal_symbol_by_pc (sal->pc);
5527               if (msym)
5528                 sal->section = SYMBOL_BFD_SECTION (msym);
5529             }
5530         }
5531     }
5532 }
5533
5534 void
5535 break_command (char *arg, int from_tty)
5536 {
5537   break_command_1 (arg, 0, from_tty, NULL);
5538 }
5539
5540 void
5541 tbreak_command (char *arg, int from_tty)
5542 {
5543   break_command_1 (arg, BP_TEMPFLAG, from_tty, NULL);
5544 }
5545
5546 static void
5547 hbreak_command (char *arg, int from_tty)
5548 {
5549   break_command_1 (arg, BP_HARDWAREFLAG, from_tty, NULL);
5550 }
5551
5552 static void
5553 thbreak_command (char *arg, int from_tty)
5554 {
5555   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty, NULL);
5556 }
5557
5558 static void
5559 stop_command (char *arg, int from_tty)
5560 {
5561   printf_filtered (_("Specify the type of breakpoint to set.\n\
5562 Usage: stop in <function | address>\n\
5563        stop at <line>\n"));
5564 }
5565
5566 static void
5567 stopin_command (char *arg, int from_tty)
5568 {
5569   int badInput = 0;
5570
5571   if (arg == (char *) NULL)
5572     badInput = 1;
5573   else if (*arg != '*')
5574     {
5575       char *argptr = arg;
5576       int hasColon = 0;
5577
5578       /* look for a ':'.  If this is a line number specification, then
5579          say it is bad, otherwise, it should be an address or
5580          function/method name */
5581       while (*argptr && !hasColon)
5582         {
5583           hasColon = (*argptr == ':');
5584           argptr++;
5585         }
5586
5587       if (hasColon)
5588         badInput = (*argptr != ':');    /* Not a class::method */
5589       else
5590         badInput = isdigit (*arg);      /* a simple line number */
5591     }
5592
5593   if (badInput)
5594     printf_filtered (_("Usage: stop in <function | address>\n"));
5595   else
5596     break_command_1 (arg, 0, from_tty, NULL);
5597 }
5598
5599 static void
5600 stopat_command (char *arg, int from_tty)
5601 {
5602   int badInput = 0;
5603
5604   if (arg == (char *) NULL || *arg == '*')      /* no line number */
5605     badInput = 1;
5606   else
5607     {
5608       char *argptr = arg;
5609       int hasColon = 0;
5610
5611       /* look for a ':'.  If there is a '::' then get out, otherwise
5612          it is probably a line number. */
5613       while (*argptr && !hasColon)
5614         {
5615           hasColon = (*argptr == ':');
5616           argptr++;
5617         }
5618
5619       if (hasColon)
5620         badInput = (*argptr == ':');    /* we have class::method */
5621       else
5622         badInput = !isdigit (*arg);     /* not a line number */
5623     }
5624
5625   if (badInput)
5626     printf_filtered (_("Usage: stop at <line>\n"));
5627   else
5628     break_command_1 (arg, 0, from_tty, NULL);
5629 }
5630
5631 /* accessflag:  hw_write:  watch write, 
5632                 hw_read:   watch read, 
5633                 hw_access: watch access (read or write) */
5634 static void
5635 watch_command_1 (char *arg, int accessflag, int from_tty)
5636 {
5637   struct breakpoint *b;
5638   struct symtab_and_line sal;
5639   struct expression *exp;
5640   struct block *exp_valid_block;
5641   struct value *val, *mark;
5642   struct frame_info *frame;
5643   struct frame_info *prev_frame = NULL;
5644   char *exp_start = NULL;
5645   char *exp_end = NULL;
5646   char *tok, *end_tok;
5647   int toklen;
5648   char *cond_start = NULL;
5649   char *cond_end = NULL;
5650   struct expression *cond = NULL;
5651   int i, other_type_used, target_resources_ok = 0;
5652   enum bptype bp_type;
5653   int mem_cnt = 0;
5654
5655   init_sal (&sal);              /* initialize to zeroes */
5656
5657   /* Parse arguments.  */
5658   innermost_block = NULL;
5659   exp_start = arg;
5660   exp = parse_exp_1 (&arg, 0, 0);
5661   exp_end = arg;
5662   exp_valid_block = innermost_block;
5663   mark = value_mark ();
5664   val = evaluate_expression (exp);
5665   release_value (val);
5666   if (value_lazy (val))
5667     value_fetch_lazy (val);
5668
5669   tok = arg;
5670   while (*tok == ' ' || *tok == '\t')
5671     tok++;
5672   end_tok = tok;
5673
5674   while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5675     end_tok++;
5676
5677   toklen = end_tok - tok;
5678   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
5679     {
5680       tok = cond_start = end_tok + 1;
5681       cond = parse_exp_1 (&tok, 0, 0);
5682       cond_end = tok;
5683     }
5684   if (*tok)
5685     error (_("Junk at end of command."));
5686
5687   if (accessflag == hw_read)
5688     bp_type = bp_read_watchpoint;
5689   else if (accessflag == hw_access)
5690     bp_type = bp_access_watchpoint;
5691   else
5692     bp_type = bp_hardware_watchpoint;
5693
5694   mem_cnt = can_use_hardware_watchpoint (val);
5695   if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
5696     error (_("Expression cannot be implemented with read/access watchpoint."));
5697   if (mem_cnt != 0)
5698     {
5699       i = hw_watchpoint_used_count (bp_type, &other_type_used);
5700       target_resources_ok = 
5701         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_type, i + mem_cnt, 
5702                                             other_type_used);
5703       if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
5704         error (_("Target does not support this type of hardware watchpoint."));
5705
5706       if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
5707         error (_("Target can only support one kind of HW watchpoint at a time."));
5708     }
5709
5710 #if defined(HPUXHPPA)
5711   /*  On HP-UX if you set a h/w
5712      watchpoint before the "run" command, the inferior dies with a e.g.,
5713      SIGILL once you start it.  I initially believed this was due to a
5714      bad interaction between page protection traps and the initial
5715      startup sequence by the dynamic linker.
5716
5717      However, I tried avoiding that by having HP-UX's implementation of
5718      TARGET_CAN_USE_HW_WATCHPOINT return FALSE if there was no inferior_ptid
5719      yet, which forced slow watches before a "run" or "attach", and it
5720      still fails somewhere in the startup code.
5721
5722      Until I figure out what's happening, I'm disallowing watches altogether
5723      before the "run" or "attach" command.  We'll tell the user they must
5724      set watches after getting the program started. */
5725   if (!target_has_execution)
5726     {
5727       warning (_("can't do that without a running program; try \"break main\"), \"run\" first");
5728       return;
5729     }
5730 #endif /* HPUXHPPA */
5731
5732   /* Change the type of breakpoint to an ordinary watchpoint if a hardware
5733      watchpoint could not be set.  */
5734   if (!mem_cnt || target_resources_ok <= 0)
5735     bp_type = bp_watchpoint;
5736
5737   /* Now set up the breakpoint.  */
5738   b = set_raw_breakpoint (sal, bp_type);
5739   set_breakpoint_count (breakpoint_count + 1);
5740   b->number = breakpoint_count;
5741   b->disposition = disp_donttouch;
5742   b->exp = exp;
5743   b->exp_valid_block = exp_valid_block;
5744   b->exp_string = savestring (exp_start, exp_end - exp_start);
5745   b->val = val;
5746   b->cond = cond;
5747   if (cond_start)
5748     b->cond_string = savestring (cond_start, cond_end - cond_start);
5749   else
5750     b->cond_string = 0;
5751
5752   frame = block_innermost_frame (exp_valid_block);
5753   if (frame)
5754     {
5755       prev_frame = get_prev_frame (frame);
5756       b->watchpoint_frame = get_frame_id (frame);
5757     }
5758   else
5759     {
5760       memset (&b->watchpoint_frame, 0, sizeof (b->watchpoint_frame));
5761     }
5762
5763   /* If the expression is "local", then set up a "watchpoint scope"
5764      breakpoint at the point where we've left the scope of the watchpoint
5765      expression.  */
5766   if (innermost_block)
5767     {
5768       if (prev_frame)
5769         {
5770           struct breakpoint *scope_breakpoint;
5771           scope_breakpoint = create_internal_breakpoint (get_frame_pc (prev_frame),
5772                                                          bp_watchpoint_scope);
5773
5774           scope_breakpoint->enable_state = bp_enabled;
5775
5776           /* Automatically delete the breakpoint when it hits.  */
5777           scope_breakpoint->disposition = disp_del;
5778
5779           /* Only break in the proper frame (help with recursion).  */
5780           scope_breakpoint->frame_id = get_frame_id (prev_frame);
5781
5782           /* Set the address at which we will stop.  */
5783           scope_breakpoint->loc->requested_address
5784             = get_frame_pc (prev_frame);
5785           scope_breakpoint->loc->address
5786             = adjust_breakpoint_address (scope_breakpoint->loc->requested_address,
5787                                          scope_breakpoint->type);
5788
5789           /* The scope breakpoint is related to the watchpoint.  We
5790              will need to act on them together.  */
5791           b->related_breakpoint = scope_breakpoint;
5792         }
5793     }
5794   value_free_to_mark (mark);
5795   mention (b);
5796 }
5797
5798 /* Return count of locations need to be watched and can be handled
5799    in hardware.  If the watchpoint can not be handled
5800    in hardware return zero.  */
5801
5802 static int
5803 can_use_hardware_watchpoint (struct value *v)
5804 {
5805   int found_memory_cnt = 0;
5806   struct value *head = v;
5807
5808   /* Did the user specifically forbid us to use hardware watchpoints? */
5809   if (!can_use_hw_watchpoints)
5810     return 0;
5811
5812   /* Make sure that the value of the expression depends only upon
5813      memory contents, and values computed from them within GDB.  If we
5814      find any register references or function calls, we can't use a
5815      hardware watchpoint.
5816
5817      The idea here is that evaluating an expression generates a series
5818      of values, one holding the value of every subexpression.  (The
5819      expression a*b+c has five subexpressions: a, b, a*b, c, and
5820      a*b+c.)  GDB's values hold almost enough information to establish
5821      the criteria given above --- they identify memory lvalues,
5822      register lvalues, computed values, etcetera.  So we can evaluate
5823      the expression, and then scan the chain of values that leaves
5824      behind to decide whether we can detect any possible change to the
5825      expression's final value using only hardware watchpoints.
5826
5827      However, I don't think that the values returned by inferior
5828      function calls are special in any way.  So this function may not
5829      notice that an expression involving an inferior function call
5830      can't be watched with hardware watchpoints.  FIXME.  */
5831   for (; v; v = value_next (v))
5832     {
5833       if (VALUE_LVAL (v) == lval_memory)
5834         {
5835           if (value_lazy (v))
5836             /* A lazy memory lvalue is one that GDB never needed to fetch;
5837                we either just used its address (e.g., `a' in `a.b') or
5838                we never needed it at all (e.g., `a' in `a,b').  */
5839             ;
5840           else
5841             {
5842               /* Ahh, memory we actually used!  Check if we can cover
5843                  it with hardware watchpoints.  */
5844               struct type *vtype = check_typedef (value_type (v));
5845
5846               /* We only watch structs and arrays if user asked for it
5847                  explicitly, never if they just happen to appear in a
5848                  middle of some value chain.  */
5849               if (v == head
5850                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
5851                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
5852                 {
5853                   CORE_ADDR vaddr = VALUE_ADDRESS (v) + value_offset (v);
5854                   int       len   = TYPE_LENGTH (value_type (v));
5855
5856                   if (!TARGET_REGION_OK_FOR_HW_WATCHPOINT (vaddr, len))
5857                     return 0;
5858                   else
5859                     found_memory_cnt++;
5860                 }
5861             }
5862         }
5863       else if (VALUE_LVAL (v) != not_lval
5864                && deprecated_value_modifiable (v) == 0)
5865         return 0;       /* ??? What does this represent? */
5866       else if (VALUE_LVAL (v) == lval_register)
5867         return 0;       /* cannot watch a register with a HW watchpoint */
5868     }
5869
5870   /* The expression itself looks suitable for using a hardware
5871      watchpoint, but give the target machine a chance to reject it.  */
5872   return found_memory_cnt;
5873 }
5874
5875 void
5876 watch_command_wrapper (char *arg, int from_tty)
5877 {
5878   watch_command (arg, from_tty);
5879 }
5880
5881 static void
5882 watch_command (char *arg, int from_tty)
5883 {
5884   watch_command_1 (arg, hw_write, from_tty);
5885 }
5886
5887 void
5888 rwatch_command_wrapper (char *arg, int from_tty)
5889 {
5890   rwatch_command (arg, from_tty);
5891 }
5892
5893 static void
5894 rwatch_command (char *arg, int from_tty)
5895 {
5896   watch_command_1 (arg, hw_read, from_tty);
5897 }
5898
5899 void
5900 awatch_command_wrapper (char *arg, int from_tty)
5901 {
5902   awatch_command (arg, from_tty);
5903 }
5904
5905 static void
5906 awatch_command (char *arg, int from_tty)
5907 {
5908   watch_command_1 (arg, hw_access, from_tty);
5909 }
5910 \f
5911
5912 /* Helper routines for the until_command routine in infcmd.c.  Here
5913    because it uses the mechanisms of breakpoints.  */
5914
5915 /* This function is called by fetch_inferior_event via the
5916    cmd_continuation pointer, to complete the until command. It takes
5917    care of cleaning up the temporary breakpoints set up by the until
5918    command. */
5919 static void
5920 until_break_command_continuation (struct continuation_arg *arg)
5921 {
5922   struct cleanup *cleanups;
5923
5924   cleanups = (struct cleanup *) arg->data.pointer;
5925   do_exec_cleanups (cleanups);
5926 }
5927
5928 void
5929 until_break_command (char *arg, int from_tty, int anywhere)
5930 {
5931   struct symtabs_and_lines sals;
5932   struct symtab_and_line sal;
5933   struct frame_info *prev_frame = get_prev_frame (deprecated_selected_frame);
5934   struct breakpoint *breakpoint;
5935   struct cleanup *old_chain;
5936   struct continuation_arg *arg1;
5937
5938
5939   clear_proceed_status ();
5940
5941   /* Set a breakpoint where the user wants it and at return from
5942      this function */
5943
5944   if (default_breakpoint_valid)
5945     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
5946                           default_breakpoint_line, (char ***) NULL, NULL);
5947   else
5948     sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 
5949                           0, (char ***) NULL, NULL);
5950
5951   if (sals.nelts != 1)
5952     error (_("Couldn't get information on specified line."));
5953
5954   sal = sals.sals[0];
5955   xfree (sals.sals);    /* malloc'd, so freed */
5956
5957   if (*arg)
5958     error (_("Junk at end of arguments."));
5959
5960   resolve_sal_pc (&sal);
5961
5962   if (anywhere)
5963     /* If the user told us to continue until a specified location,
5964        we don't specify a frame at which we need to stop.  */
5965     breakpoint = set_momentary_breakpoint (sal, null_frame_id, bp_until);
5966   else
5967     /* Otherwise, specify the current frame, because we want to stop only
5968        at the very same frame.  */
5969     breakpoint = set_momentary_breakpoint (sal,
5970                                            get_frame_id (deprecated_selected_frame),
5971                                            bp_until);
5972
5973   if (!target_can_async_p ())
5974     old_chain = make_cleanup_delete_breakpoint (breakpoint);
5975   else
5976     old_chain = make_exec_cleanup_delete_breakpoint (breakpoint);
5977
5978   /* If we are running asynchronously, and the target supports async
5979      execution, we are not waiting for the target to stop, in the call
5980      tp proceed, below. This means that we cannot delete the
5981      brekpoints until the target has actually stopped. The only place
5982      where we get a chance to do that is in fetch_inferior_event, so
5983      we must set things up for that. */
5984
5985   if (target_can_async_p ())
5986     {
5987       /* In this case the arg for the continuation is just the point
5988          in the exec_cleanups chain from where to start doing
5989          cleanups, because all the continuation does is the cleanups in
5990          the exec_cleanup_chain. */
5991       arg1 =
5992         (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
5993       arg1->next         = NULL;
5994       arg1->data.pointer = old_chain;
5995
5996       add_continuation (until_break_command_continuation, arg1);
5997     }
5998
5999   /* Keep within the current frame, or in frames called by the current
6000      one.  */
6001   if (prev_frame)
6002     {
6003       sal = find_pc_line (get_frame_pc (prev_frame), 0);
6004       sal.pc = get_frame_pc (prev_frame);
6005       breakpoint = set_momentary_breakpoint (sal, get_frame_id (prev_frame),
6006                                              bp_until);
6007       if (!target_can_async_p ())
6008         make_cleanup_delete_breakpoint (breakpoint);
6009       else
6010         make_exec_cleanup_delete_breakpoint (breakpoint);
6011     }
6012
6013   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
6014   /* Do the cleanups now, anly if we are not running asynchronously,
6015      of if we are, but the target is still synchronous. */
6016   if (!target_can_async_p ())
6017     do_cleanups (old_chain);
6018 }
6019
6020 static void
6021 ep_skip_leading_whitespace (char **s)
6022 {
6023   if ((s == NULL) || (*s == NULL))
6024     return;
6025   while (isspace (**s))
6026     *s += 1;
6027 }
6028
6029 /* This function examines a string, and attempts to find a token
6030    that might be an event name in the leading characters.  If a
6031    possible match is found, a pointer to the last character of
6032    the token is returned.  Else, NULL is returned. */
6033
6034 static char *
6035 ep_find_event_name_end (char *arg)
6036 {
6037   char *s = arg;
6038   char *event_name_end = NULL;
6039
6040   /* If we could depend upon the presense of strrpbrk, we'd use that... */
6041   if (arg == NULL)
6042     return NULL;
6043
6044   /* We break out of the loop when we find a token delimiter.
6045      Basically, we're looking for alphanumerics and underscores;
6046      anything else delimites the token. */
6047   while (*s != '\0')
6048     {
6049       if (!isalnum (*s) && (*s != '_'))
6050         break;
6051       event_name_end = s;
6052       s++;
6053     }
6054
6055   return event_name_end;
6056 }
6057
6058
6059 /* This function attempts to parse an optional "if <cond>" clause
6060    from the arg string.  If one is not found, it returns NULL.
6061
6062    Else, it returns a pointer to the condition string.  (It does not
6063    attempt to evaluate the string against a particular block.)  And,
6064    it updates arg to point to the first character following the parsed
6065    if clause in the arg string. */
6066
6067 static char *
6068 ep_parse_optional_if_clause (char **arg)
6069 {
6070   char *cond_string;
6071
6072   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
6073     return NULL;
6074
6075   /* Skip the "if" keyword. */
6076   (*arg) += 2;
6077
6078   /* Skip any extra leading whitespace, and record the start of the
6079      condition string. */
6080   ep_skip_leading_whitespace (arg);
6081   cond_string = *arg;
6082
6083   /* Assume that the condition occupies the remainder of the arg string. */
6084   (*arg) += strlen (cond_string);
6085
6086   return cond_string;
6087 }
6088
6089 /* This function attempts to parse an optional filename from the arg
6090    string.  If one is not found, it returns NULL.
6091
6092    Else, it returns a pointer to the parsed filename.  (This function
6093    makes no attempt to verify that a file of that name exists, or is
6094    accessible.)  And, it updates arg to point to the first character
6095    following the parsed filename in the arg string.
6096
6097    Note that clients needing to preserve the returned filename for
6098    future access should copy it to their own buffers. */
6099 static char *
6100 ep_parse_optional_filename (char **arg)
6101 {
6102   static char filename[1024];
6103   char *arg_p = *arg;
6104   int i;
6105   char c;
6106
6107   if ((*arg_p == '\0') || isspace (*arg_p))
6108     return NULL;
6109
6110   for (i = 0;; i++)
6111     {
6112       c = *arg_p;
6113       if (isspace (c))
6114         c = '\0';
6115       filename[i] = c;
6116       if (c == '\0')
6117         break;
6118       arg_p++;
6119     }
6120   *arg = arg_p;
6121
6122   return filename;
6123 }
6124
6125 /* Commands to deal with catching events, such as signals, exceptions,
6126    process start/exit, etc.  */
6127
6128 typedef enum
6129 {
6130   catch_fork, catch_vfork
6131 }
6132 catch_fork_kind;
6133
6134 static void
6135 catch_fork_command_1 (catch_fork_kind fork_kind, char *arg, int tempflag,
6136                       int from_tty)
6137 {
6138   char *cond_string = NULL;
6139
6140   ep_skip_leading_whitespace (&arg);
6141
6142   /* The allowed syntax is:
6143      catch [v]fork
6144      catch [v]fork if <cond>
6145
6146      First, check if there's an if clause. */
6147   cond_string = ep_parse_optional_if_clause (&arg);
6148
6149   if ((*arg != '\0') && !isspace (*arg))
6150     error (_("Junk at end of arguments."));
6151
6152   /* If this target supports it, create a fork or vfork catchpoint
6153      and enable reporting of such events. */
6154   switch (fork_kind)
6155     {
6156     case catch_fork:
6157       create_fork_event_catchpoint (tempflag, cond_string);
6158       break;
6159     case catch_vfork:
6160       create_vfork_event_catchpoint (tempflag, cond_string);
6161       break;
6162     default:
6163       error (_("unsupported or unknown fork kind; cannot catch it"));
6164       break;
6165     }
6166 }
6167
6168 static void
6169 catch_exec_command_1 (char *arg, int tempflag, int from_tty)
6170 {
6171   char *cond_string = NULL;
6172
6173   ep_skip_leading_whitespace (&arg);
6174
6175   /* The allowed syntax is:
6176      catch exec
6177      catch exec if <cond>
6178
6179      First, check if there's an if clause. */
6180   cond_string = ep_parse_optional_if_clause (&arg);
6181
6182   if ((*arg != '\0') && !isspace (*arg))
6183     error (_("Junk at end of arguments."));
6184
6185   /* If this target supports it, create an exec catchpoint
6186      and enable reporting of such events. */
6187   create_exec_event_catchpoint (tempflag, cond_string);
6188 }
6189
6190 static void
6191 catch_load_command_1 (char *arg, int tempflag, int from_tty)
6192 {
6193   char *dll_pathname = NULL;
6194   char *cond_string = NULL;
6195
6196   ep_skip_leading_whitespace (&arg);
6197
6198   /* The allowed syntax is:
6199      catch load
6200      catch load if <cond>
6201      catch load <filename>
6202      catch load <filename> if <cond>
6203
6204      The user is not allowed to specify the <filename> after an
6205      if clause.
6206
6207      We'll ignore the pathological case of a file named "if".
6208
6209      First, check if there's an if clause.  If so, then there
6210      cannot be a filename. */
6211   cond_string = ep_parse_optional_if_clause (&arg);
6212
6213   /* If there was an if clause, then there cannot be a filename.
6214      Else, there might be a filename and an if clause. */
6215   if (cond_string == NULL)
6216     {
6217       dll_pathname = ep_parse_optional_filename (&arg);
6218       ep_skip_leading_whitespace (&arg);
6219       cond_string = ep_parse_optional_if_clause (&arg);
6220     }
6221
6222   if ((*arg != '\0') && !isspace (*arg))
6223     error (_("Junk at end of arguments."));
6224
6225   /* Create a load breakpoint that only triggers when a load of
6226      the specified dll (or any dll, if no pathname was specified)
6227      occurs. */
6228   SOLIB_CREATE_CATCH_LOAD_HOOK (PIDGET (inferior_ptid), tempflag, 
6229                                 dll_pathname, cond_string);
6230 }
6231
6232 static void
6233 catch_unload_command_1 (char *arg, int tempflag, int from_tty)
6234 {
6235   char *dll_pathname = NULL;
6236   char *cond_string = NULL;
6237
6238   ep_skip_leading_whitespace (&arg);
6239
6240   /* The allowed syntax is:
6241      catch unload
6242      catch unload if <cond>
6243      catch unload <filename>
6244      catch unload <filename> if <cond>
6245
6246      The user is not allowed to specify the <filename> after an
6247      if clause.
6248
6249      We'll ignore the pathological case of a file named "if".
6250
6251      First, check if there's an if clause.  If so, then there
6252      cannot be a filename. */
6253   cond_string = ep_parse_optional_if_clause (&arg);
6254
6255   /* If there was an if clause, then there cannot be a filename.
6256      Else, there might be a filename and an if clause. */
6257   if (cond_string == NULL)
6258     {
6259       dll_pathname = ep_parse_optional_filename (&arg);
6260       ep_skip_leading_whitespace (&arg);
6261       cond_string = ep_parse_optional_if_clause (&arg);
6262     }
6263
6264   if ((*arg != '\0') && !isspace (*arg))
6265     error (_("Junk at end of arguments."));
6266
6267   /* Create an unload breakpoint that only triggers when an unload of
6268      the specified dll (or any dll, if no pathname was specified)
6269      occurs. */
6270   SOLIB_CREATE_CATCH_UNLOAD_HOOK (PIDGET (inferior_ptid), tempflag, 
6271                                   dll_pathname, cond_string);
6272 }
6273
6274 /* Commands to deal with catching exceptions.  */
6275
6276 /* Set a breakpoint at the specified callback routine for an
6277    exception event callback */
6278
6279 static void
6280 create_exception_catchpoint (int tempflag, char *cond_string,
6281                              enum exception_event_kind ex_event,
6282                              struct symtab_and_line *sal)
6283 {
6284   struct breakpoint *b;
6285   int thread = -1;              /* All threads. */
6286   enum bptype bptype;
6287
6288   if (!sal)                     /* no exception support? */
6289     return;
6290
6291   switch (ex_event)
6292     {
6293     case EX_EVENT_THROW:
6294       bptype = bp_catch_throw;
6295       break;
6296     case EX_EVENT_CATCH:
6297       bptype = bp_catch_catch;
6298       break;
6299     default:                    /* error condition */
6300       error (_("Internal error -- invalid catchpoint kind"));
6301     }
6302
6303   b = set_raw_breakpoint (*sal, bptype);
6304   set_breakpoint_count (breakpoint_count + 1);
6305   b->number = breakpoint_count;
6306   b->cond = NULL;
6307   b->cond_string = (cond_string == NULL) ? 
6308     NULL : savestring (cond_string, strlen (cond_string));
6309   b->thread = thread;
6310   b->addr_string = NULL;
6311   b->enable_state = bp_enabled;
6312   b->disposition = tempflag ? disp_del : disp_donttouch;
6313   mention (b);
6314 }
6315
6316 static enum print_stop_action
6317 print_exception_catchpoint (struct breakpoint *b)
6318 {
6319   annotate_catchpoint (b->number);
6320
6321   if (strstr (b->addr_string, "throw") != NULL)
6322     printf_filtered (_("\nCatchpoint %d (exception thrown)\n"),
6323                      b->number);
6324   else
6325     printf_filtered (_("\nCatchpoint %d (exception caught)\n"),
6326                      b->number);
6327
6328   return PRINT_SRC_AND_LOC;
6329 }
6330
6331 static void
6332 print_one_exception_catchpoint (struct breakpoint *b, CORE_ADDR *last_addr)
6333 {
6334   if (addressprint)
6335     {
6336       annotate_field (4);
6337       ui_out_field_core_addr (uiout, "addr", b->loc->address);
6338     }
6339   annotate_field (5);
6340   *last_addr = b->loc->address;
6341   if (strstr (b->addr_string, "throw") != NULL)
6342     ui_out_field_string (uiout, "what", "exception throw");
6343   else
6344     ui_out_field_string (uiout, "what", "exception catch");
6345 }
6346
6347 static void
6348 print_mention_exception_catchpoint (struct breakpoint *b)
6349 {
6350   if (strstr (b->addr_string, "throw") != NULL)
6351     printf_filtered (_("Catchpoint %d (throw)"), b->number);
6352   else
6353     printf_filtered (_("Catchpoint %d (catch)"), b->number);
6354 }
6355
6356 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
6357   print_exception_catchpoint,
6358   print_one_exception_catchpoint,
6359   print_mention_exception_catchpoint
6360 };
6361
6362 static int
6363 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
6364                           enum exception_event_kind ex_event, int from_tty)
6365 {
6366   char *trigger_func_name, *nameptr;
6367   struct symtabs_and_lines sals;
6368   struct breakpoint *b;
6369
6370   if (ex_event == EX_EVENT_CATCH)
6371     trigger_func_name = xstrdup ("__cxa_begin_catch");
6372   else
6373     trigger_func_name = xstrdup ("__cxa_throw");
6374
6375   nameptr = trigger_func_name;
6376   sals = decode_line_1 (&nameptr, 1, NULL, 0, NULL, NULL);
6377   if (sals.nelts == 0)
6378     {
6379       xfree (trigger_func_name);
6380       return 0;
6381     }
6382
6383   b = set_raw_breakpoint (sals.sals[0], bp_breakpoint);
6384   set_breakpoint_count (breakpoint_count + 1);
6385   b->number = breakpoint_count;
6386   b->cond = NULL;
6387   b->cond_string = (cond_string == NULL) ? 
6388     NULL : savestring (cond_string, strlen (cond_string));
6389   b->thread = -1;
6390   b->addr_string = trigger_func_name;
6391   b->enable_state = bp_enabled;
6392   b->disposition = tempflag ? disp_del : disp_donttouch;
6393   b->ops = &gnu_v3_exception_catchpoint_ops;
6394
6395   xfree (sals.sals);
6396   mention (b);
6397   return 1;
6398 }
6399
6400 /* Deal with "catch catch" and "catch throw" commands */
6401
6402 static void
6403 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
6404                            int tempflag, int from_tty)
6405 {
6406   char *cond_string = NULL;
6407   struct symtab_and_line *sal = NULL;
6408
6409   ep_skip_leading_whitespace (&arg);
6410
6411   cond_string = ep_parse_optional_if_clause (&arg);
6412
6413   if ((*arg != '\0') && !isspace (*arg))
6414     error (_("Junk at end of arguments."));
6415
6416   if ((ex_event != EX_EVENT_THROW) &&
6417       (ex_event != EX_EVENT_CATCH))
6418     error (_("Unsupported or unknown exception event; cannot catch it"));
6419
6420   if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
6421     return;
6422
6423   /* See if we can find a callback routine */
6424   sal = target_enable_exception_callback (ex_event, 1);
6425
6426   if (sal)
6427     {
6428       /* We have callbacks from the runtime system for exceptions.
6429          Set a breakpoint on the sal found, if no errors */
6430       if (sal != (struct symtab_and_line *) -1)
6431         create_exception_catchpoint (tempflag, cond_string, ex_event, sal);
6432       else
6433         return;         /* something went wrong with setting up callbacks */
6434     }
6435
6436   warning (_("Unsupported with this platform/compiler combination."));
6437 }
6438
6439 /* Cover routine to allow wrapping target_enable_exception_catchpoints
6440    inside a catch_errors */
6441
6442 static int
6443 cover_target_enable_exception_callback (void *arg)
6444 {
6445   args_for_catchpoint_enable *args = arg;
6446   struct symtab_and_line *sal;
6447   sal = target_enable_exception_callback (args->kind, args->enable_p);
6448   if (sal == NULL)
6449     return 0;
6450   else if (sal == (struct symtab_and_line *) -1)
6451     return -1;
6452   else
6453     return 1;                   /*is valid */
6454 }
6455
6456 static void
6457 catch_command_1 (char *arg, int tempflag, int from_tty)
6458 {
6459
6460   /* The first argument may be an event name, such as "start" or "load".
6461      If so, then handle it as such.  If it doesn't match an event name,
6462      then attempt to interpret it as an exception name.  (This latter is
6463      the v4.16-and-earlier GDB meaning of the "catch" command.)
6464
6465      First, try to find the bounds of what might be an event name. */
6466   char *arg1_start = arg;
6467   char *arg1_end;
6468   int arg1_length;
6469
6470   if (arg1_start == NULL)
6471     {
6472       /* Old behaviour was to use pre-v-4.16 syntax */
6473       /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
6474       /* return; */
6475       /* Now, this is not allowed */
6476       error (_("Catch requires an event name."));
6477
6478     }
6479   arg1_end = ep_find_event_name_end (arg1_start);
6480   if (arg1_end == NULL)
6481     error (_("catch requires an event"));
6482   arg1_length = arg1_end + 1 - arg1_start;
6483
6484   /* Try to match what we found against known event names. */
6485   if (strncmp (arg1_start, "signal", arg1_length) == 0)
6486     {
6487       error (_("Catch of signal not yet implemented"));
6488     }
6489   else if (strncmp (arg1_start, "catch", arg1_length) == 0)
6490     {
6491       catch_exception_command_1 (EX_EVENT_CATCH, arg1_end + 1, 
6492                                  tempflag, from_tty);
6493     }
6494   else if (strncmp (arg1_start, "throw", arg1_length) == 0)
6495     {
6496       catch_exception_command_1 (EX_EVENT_THROW, arg1_end + 1, 
6497                                  tempflag, from_tty);
6498     }
6499   else if (strncmp (arg1_start, "thread_start", arg1_length) == 0)
6500     {
6501       error (_("Catch of thread_start not yet implemented"));
6502     }
6503   else if (strncmp (arg1_start, "thread_exit", arg1_length) == 0)
6504     {
6505       error (_("Catch of thread_exit not yet implemented"));
6506     }
6507   else if (strncmp (arg1_start, "thread_join", arg1_length) == 0)
6508     {
6509       error (_("Catch of thread_join not yet implemented"));
6510     }
6511   else if (strncmp (arg1_start, "start", arg1_length) == 0)
6512     {
6513       error (_("Catch of start not yet implemented"));
6514     }
6515   else if (strncmp (arg1_start, "exit", arg1_length) == 0)
6516     {
6517       error (_("Catch of exit not yet implemented"));
6518     }
6519   else if (strncmp (arg1_start, "fork", arg1_length) == 0)
6520     {
6521       catch_fork_command_1 (catch_fork, arg1_end + 1, tempflag, from_tty);
6522     }
6523   else if (strncmp (arg1_start, "vfork", arg1_length) == 0)
6524     {
6525       catch_fork_command_1 (catch_vfork, arg1_end + 1, tempflag, from_tty);
6526     }
6527   else if (strncmp (arg1_start, "exec", arg1_length) == 0)
6528     {
6529       catch_exec_command_1 (arg1_end + 1, tempflag, from_tty);
6530     }
6531   else if (strncmp (arg1_start, "load", arg1_length) == 0)
6532     {
6533       catch_load_command_1 (arg1_end + 1, tempflag, from_tty);
6534     }
6535   else if (strncmp (arg1_start, "unload", arg1_length) == 0)
6536     {
6537       catch_unload_command_1 (arg1_end + 1, tempflag, from_tty);
6538     }
6539   else if (strncmp (arg1_start, "stop", arg1_length) == 0)
6540     {
6541       error (_("Catch of stop not yet implemented"));
6542     }
6543
6544   /* This doesn't appear to be an event name */
6545
6546   else
6547     {
6548       /* Pre-v.4.16 behaviour was to treat the argument
6549          as the name of an exception */
6550       /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
6551       /* Now this is not allowed */
6552       error (_("Unknown event kind specified for catch"));
6553
6554     }
6555 }
6556
6557 /* Used by the gui, could be made a worker for other things. */
6558
6559 struct breakpoint *
6560 set_breakpoint_sal (struct symtab_and_line sal)
6561 {
6562   struct breakpoint *b;
6563   b = set_raw_breakpoint (sal, bp_breakpoint);
6564   set_breakpoint_count (breakpoint_count + 1);
6565   b->number = breakpoint_count;
6566   b->cond = 0;
6567   b->thread = -1;
6568   return b;
6569 }
6570
6571 static void
6572 catch_command (char *arg, int from_tty)
6573 {
6574   catch_command_1 (arg, 0, from_tty);
6575 }
6576 \f
6577
6578 static void
6579 tcatch_command (char *arg, int from_tty)
6580 {
6581   catch_command_1 (arg, 1, from_tty);
6582 }
6583
6584 /* Delete breakpoints by address or line.  */
6585
6586 static void
6587 clear_command (char *arg, int from_tty)
6588 {
6589   struct breakpoint *b, *tmp, *prev, *found;
6590   int default_match;
6591   struct symtabs_and_lines sals;
6592   struct symtab_and_line sal;
6593   int i;
6594
6595   if (arg)
6596     {
6597       sals = decode_line_spec (arg, 1);
6598       default_match = 0;
6599     }
6600   else
6601     {
6602       sals.sals = (struct symtab_and_line *)
6603         xmalloc (sizeof (struct symtab_and_line));
6604       make_cleanup (xfree, sals.sals);
6605       init_sal (&sal);          /* initialize to zeroes */
6606       sal.line = default_breakpoint_line;
6607       sal.symtab = default_breakpoint_symtab;
6608       sal.pc = default_breakpoint_address;
6609       if (sal.symtab == 0)
6610         error (_("No source file specified."));
6611
6612       sals.sals[0] = sal;
6613       sals.nelts = 1;
6614
6615       default_match = 1;
6616     }
6617
6618   /* For each line spec given, delete bps which correspond
6619      to it.  Do it in two passes, solely to preserve the current
6620      behavior that from_tty is forced true if we delete more than
6621      one breakpoint.  */
6622
6623   found = NULL;
6624   for (i = 0; i < sals.nelts; i++)
6625     {
6626       /* If exact pc given, clear bpts at that pc.
6627          If line given (pc == 0), clear all bpts on specified line.
6628          If defaulting, clear all bpts on default line
6629          or at default pc.
6630
6631          defaulting    sal.pc != 0    tests to do
6632
6633          0              1             pc
6634          1              1             pc _and_ line
6635          0              0             line
6636          1              0             <can't happen> */
6637
6638       sal = sals.sals[i];
6639       prev = NULL;
6640
6641       /* Find all matching breakpoints, remove them from the
6642          breakpoint chain, and add them to the 'found' chain.  */
6643       ALL_BREAKPOINTS_SAFE (b, tmp)
6644         {
6645           /* Are we going to delete b? */
6646           if (b->type != bp_none
6647               && b->type != bp_watchpoint
6648               && b->type != bp_hardware_watchpoint
6649               && b->type != bp_read_watchpoint
6650               && b->type != bp_access_watchpoint
6651               /* Not if b is a watchpoint of any sort... */
6652               && (((sal.pc && (b->loc->address == sal.pc)) 
6653                    && (!section_is_overlay (b->loc->section)
6654                        || b->loc->section == sal.section))
6655                   /* Yes, if sal.pc matches b (modulo overlays).  */
6656                   || ((default_match || (0 == sal.pc))
6657                       && b->source_file != NULL
6658                       && sal.symtab != NULL
6659                       && strcmp (b->source_file, sal.symtab->filename) == 0
6660                       && b->line_number == sal.line)))
6661             /* Yes, if sal source file and line matches b.  */
6662             {
6663               /* Remove it from breakpoint_chain...  */
6664               if (b == breakpoint_chain)
6665                 {
6666                   /* b is at the head of the list */
6667                   breakpoint_chain = b->next;
6668                 }
6669               else
6670                 {
6671                   prev->next = b->next;
6672                 }
6673               /* And add it to 'found' chain.  */
6674               b->next = found;
6675               found = b;
6676             }
6677           else
6678             {
6679               /* Keep b, and keep a pointer to it.  */
6680               prev = b;
6681             }
6682         }
6683     }
6684   /* Now go thru the 'found' chain and delete them.  */
6685   if (found == 0)
6686     {
6687       if (arg)
6688         error (_("No breakpoint at %s."), arg);
6689       else
6690         error (_("No breakpoint at this line."));
6691     }
6692
6693   if (found->next)
6694     from_tty = 1;               /* Always report if deleted more than one */
6695   if (from_tty)
6696     {
6697       if (!found->next)
6698         printf_unfiltered (_("Deleted breakpoint "));
6699       else
6700         printf_unfiltered (_("Deleted breakpoints "));
6701     }
6702   breakpoints_changed ();
6703   while (found)
6704     {
6705       if (from_tty)
6706         printf_unfiltered ("%d ", found->number);
6707       tmp = found->next;
6708       delete_breakpoint (found);
6709       found = tmp;
6710     }
6711   if (from_tty)
6712     putchar_unfiltered ('\n');
6713 }
6714 \f
6715 /* Delete breakpoint in BS if they are `delete' breakpoints and
6716    all breakpoints that are marked for deletion, whether hit or not.
6717    This is called after any breakpoint is hit, or after errors.  */
6718
6719 void
6720 breakpoint_auto_delete (bpstat bs)
6721 {
6722   struct breakpoint *b, *temp;
6723
6724   for (; bs; bs = bs->next)
6725     if (bs->breakpoint_at && bs->breakpoint_at->disposition == disp_del
6726         && bs->stop)
6727       delete_breakpoint (bs->breakpoint_at);
6728
6729   ALL_BREAKPOINTS_SAFE (b, temp)
6730   {
6731     if (b->disposition == disp_del_at_next_stop)
6732       delete_breakpoint (b);
6733   }
6734 }
6735
6736 /* Delete a breakpoint and clean up all traces of it in the data
6737    structures. */
6738
6739 void
6740 delete_breakpoint (struct breakpoint *bpt)
6741 {
6742   struct breakpoint *b;
6743   bpstat bs;
6744   struct bp_location *loc;
6745
6746   gdb_assert (bpt != NULL);
6747
6748   /* Has this bp already been deleted?  This can happen because multiple
6749      lists can hold pointers to bp's.  bpstat lists are especial culprits.
6750
6751      One example of this happening is a watchpoint's scope bp.  When the
6752      scope bp triggers, we notice that the watchpoint is out of scope, and
6753      delete it.  We also delete its scope bp.  But the scope bp is marked
6754      "auto-deleting", and is already on a bpstat.  That bpstat is then
6755      checked for auto-deleting bp's, which are deleted.
6756
6757      A real solution to this problem might involve reference counts in bp's,
6758      and/or giving them pointers back to their referencing bpstat's, and
6759      teaching delete_breakpoint to only free a bp's storage when no more
6760      references were extent.  A cheaper bandaid was chosen.  */
6761   if (bpt->type == bp_none)
6762     return;
6763
6764   if (deprecated_delete_breakpoint_hook)
6765     deprecated_delete_breakpoint_hook (bpt);
6766   breakpoint_delete_event (bpt->number);
6767
6768   if (bpt->loc->inserted)
6769     remove_breakpoint (bpt->loc, mark_inserted);
6770
6771   free_valchain (bpt->loc);
6772
6773   if (breakpoint_chain == bpt)
6774     breakpoint_chain = bpt->next;
6775
6776   if (bp_location_chain == bpt->loc)
6777     bp_location_chain = bpt->loc->next;
6778
6779   /* If we have callback-style exception catchpoints, don't go through
6780      the adjustments to the C++ runtime library etc. if the inferior
6781      isn't actually running.  target_enable_exception_callback for a
6782      null target ops vector gives an undesirable error message, so we
6783      check here and avoid it. Since currently (1997-09-17) only HP-UX aCC's
6784      exceptions are supported in this way, it's OK for now.  FIXME */
6785   if (ep_is_exception_catchpoint (bpt) && target_has_execution)
6786     {
6787       /* Format possible error msg */
6788       char *message = xstrprintf ("Error in deleting catchpoint %d:\n",
6789                                   bpt->number);
6790       struct cleanup *cleanups = make_cleanup (xfree, message);
6791       args_for_catchpoint_enable args;
6792       args.kind = bpt->type == bp_catch_catch ? 
6793         EX_EVENT_CATCH : EX_EVENT_THROW;
6794       args.enable_p = 0;
6795       catch_errors (cover_target_enable_exception_callback, &args,
6796                     message, RETURN_MASK_ALL);
6797       do_cleanups (cleanups);
6798     }
6799
6800
6801   ALL_BREAKPOINTS (b)
6802     if (b->next == bpt)
6803     {
6804       b->next = bpt->next;
6805       break;
6806     }
6807
6808   ALL_BP_LOCATIONS (loc)
6809     if (loc->next == bpt->loc)
6810       {
6811         loc->next = bpt->loc->next;
6812         break;
6813       }
6814
6815   check_duplicates (bpt);
6816   /* If this breakpoint was inserted, and there is another breakpoint
6817      at the same address, we need to insert the other breakpoint.  */
6818   if (bpt->loc->inserted
6819       && bpt->type != bp_hardware_watchpoint
6820       && bpt->type != bp_read_watchpoint
6821       && bpt->type != bp_access_watchpoint
6822       && bpt->type != bp_catch_fork
6823       && bpt->type != bp_catch_vfork
6824       && bpt->type != bp_catch_exec)
6825     {
6826       ALL_BREAKPOINTS (b)
6827         if (b->loc->address == bpt->loc->address
6828             && b->loc->section == bpt->loc->section
6829             && !b->loc->duplicate
6830             && b->enable_state != bp_disabled
6831             && b->enable_state != bp_shlib_disabled
6832             && !b->pending
6833             && b->enable_state != bp_call_disabled)
6834         {
6835           int val;
6836
6837           /* We should never reach this point if there is a permanent
6838              breakpoint at the same address as the one being deleted.
6839              If there is a permanent breakpoint somewhere, it should
6840              always be the only one inserted.  */
6841           if (b->enable_state == bp_permanent)
6842             internal_error (__FILE__, __LINE__,
6843                             _("another breakpoint was inserted on top of "
6844                             "a permanent breakpoint"));
6845
6846           memset (&b->loc->target_info, 0, sizeof (b->loc->target_info));
6847           b->loc->target_info.placed_address = b->loc->address;
6848           if (b->type == bp_hardware_breakpoint)
6849             val = target_insert_hw_breakpoint (&b->loc->target_info);
6850           else
6851             val = target_insert_breakpoint (&b->loc->target_info);
6852
6853           /* If there was an error in the insert, print a message, then stop execution.  */
6854           if (val != 0)
6855             {
6856               struct ui_file *tmp_error_stream = mem_fileopen ();
6857               make_cleanup_ui_file_delete (tmp_error_stream);
6858              
6859
6860               if (b->type == bp_hardware_breakpoint)
6861                 {
6862                   fprintf_unfiltered (tmp_error_stream, 
6863                                         "Cannot insert hardware breakpoint %d.\n"
6864                                       "You may have requested too many hardware breakpoints.\n",
6865                                         b->number);
6866                   }
6867                 else
6868                   {
6869                     fprintf_unfiltered (tmp_error_stream, "Cannot insert breakpoint %d.\n", b->number);
6870                     fprintf_filtered (tmp_error_stream, "Error accessing memory address ");
6871                     deprecated_print_address_numeric (b->loc->address, 1, tmp_error_stream);
6872                     fprintf_filtered (tmp_error_stream, ": %s.\n",
6873                                       safe_strerror (val));
6874                   }
6875               
6876               fprintf_unfiltered (tmp_error_stream,"The same program may be running in another process.");
6877               target_terminal_ours_for_output ();
6878               error_stream(tmp_error_stream); 
6879             }
6880           else
6881             b->loc->inserted = 1;
6882         }
6883     }
6884
6885   free_command_lines (&bpt->commands);
6886   if (bpt->cond)
6887     xfree (bpt->cond);
6888   if (bpt->cond_string != NULL)
6889     xfree (bpt->cond_string);
6890   if (bpt->addr_string != NULL)
6891     xfree (bpt->addr_string);
6892   if (bpt->exp != NULL)
6893     xfree (bpt->exp);
6894   if (bpt->exp_string != NULL)
6895     xfree (bpt->exp_string);
6896   if (bpt->val != NULL)
6897     value_free (bpt->val);
6898   if (bpt->source_file != NULL)
6899     xfree (bpt->source_file);
6900   if (bpt->dll_pathname != NULL)
6901     xfree (bpt->dll_pathname);
6902   if (bpt->triggered_dll_pathname != NULL)
6903     xfree (bpt->triggered_dll_pathname);
6904   if (bpt->exec_pathname != NULL)
6905     xfree (bpt->exec_pathname);
6906
6907   /* Be sure no bpstat's are pointing at it after it's been freed.  */
6908   /* FIXME, how can we find all bpstat's?
6909      We just check stop_bpstat for now.  */
6910   for (bs = stop_bpstat; bs; bs = bs->next)
6911     if (bs->breakpoint_at == bpt)
6912       {
6913         bs->breakpoint_at = NULL;
6914         bs->old_val = NULL;
6915         /* bs->commands will be freed later.  */
6916       }
6917   /* On the chance that someone will soon try again to delete this same
6918      bp, we mark it as deleted before freeing its storage. */
6919   bpt->type = bp_none;
6920
6921   xfree (bpt->loc);
6922   xfree (bpt);
6923 }
6924
6925 static void
6926 do_delete_breakpoint_cleanup (void *b)
6927 {
6928   delete_breakpoint (b);
6929 }
6930
6931 struct cleanup *
6932 make_cleanup_delete_breakpoint (struct breakpoint *b)
6933 {
6934   return make_cleanup (do_delete_breakpoint_cleanup, b);
6935 }
6936
6937 struct cleanup *
6938 make_exec_cleanup_delete_breakpoint (struct breakpoint *b)
6939 {
6940   return make_exec_cleanup (do_delete_breakpoint_cleanup, b);
6941 }
6942
6943 void
6944 delete_command (char *arg, int from_tty)
6945 {
6946   struct breakpoint *b, *temp;
6947
6948   dont_repeat ();
6949
6950   if (arg == 0)
6951     {
6952       int breaks_to_delete = 0;
6953
6954       /* Delete all breakpoints if no argument.
6955          Do not delete internal or call-dummy breakpoints, these
6956          have to be deleted with an explicit breakpoint number argument.  */
6957       ALL_BREAKPOINTS (b)
6958       {
6959         if (b->type != bp_call_dummy &&
6960             b->type != bp_shlib_event &&
6961             b->type != bp_thread_event &&
6962             b->type != bp_overlay_event &&
6963             b->number >= 0)
6964           breaks_to_delete = 1;
6965       }
6966
6967       /* Ask user only if there are some breakpoints to delete.  */
6968       if (!from_tty
6969           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
6970         {
6971           ALL_BREAKPOINTS_SAFE (b, temp)
6972           {
6973             if (b->type != bp_call_dummy &&
6974                 b->type != bp_shlib_event &&
6975                 b->type != bp_thread_event &&
6976                 b->type != bp_overlay_event &&
6977                 b->number >= 0)
6978               delete_breakpoint (b);
6979           }
6980         }
6981     }
6982   else
6983     map_breakpoint_numbers (arg, delete_breakpoint);
6984 }
6985
6986 /* Reset a breakpoint given it's struct breakpoint * BINT.
6987    The value we return ends up being the return value from catch_errors.
6988    Unused in this case.  */
6989
6990 static int
6991 breakpoint_re_set_one (void *bint)
6992 {
6993   /* get past catch_errs */
6994   struct breakpoint *b = (struct breakpoint *) bint;
6995   struct value *mark;
6996   int i;
6997   int not_found;
6998   int *not_found_ptr = NULL;
6999   struct symtabs_and_lines sals;
7000   char *s;
7001   enum enable_state save_enable;
7002
7003   switch (b->type)
7004     {
7005     case bp_none:
7006       warning (_("attempted to reset apparently deleted breakpoint #%d?"),
7007                b->number);
7008       return 0;
7009     case bp_breakpoint:
7010     case bp_hardware_breakpoint:
7011     case bp_catch_load:
7012     case bp_catch_unload:
7013       if (b->addr_string == NULL)
7014         {
7015           /* Anything without a string can't be re-set. */
7016           delete_breakpoint (b);
7017           return 0;
7018         }
7019       /* HACK: cagney/2001-11-11: kettenis/2001-11-11: MarkK wrote:
7020
7021          ``And a hack it is, although Apple's Darwin version of GDB
7022          contains an almost identical hack to implement a "future
7023          break" command.  It seems to work in many real world cases,
7024          but it is easy to come up with a test case where the patch
7025          doesn't help at all.''
7026
7027          ``It seems that the way GDB implements breakpoints - in -
7028          shared - libraries was designed for a.out shared library
7029          systems (SunOS 4) where shared libraries were loaded at a
7030          fixed address in memory.  Since ELF shared libraries can (and
7031          will) be loaded at any address in memory, things break.
7032          Fixing this is not trivial.  Therefore, I'm not sure whether
7033          we should add this hack to the branch only.  I cannot
7034          guarantee that things will be fixed on the trunk in the near
7035          future.''
7036
7037          In case we have a problem, disable this breakpoint.  We'll
7038          restore its status if we succeed.  Don't disable a
7039          shlib_disabled breakpoint though.  There's a fair chance we
7040          can't re-set it if the shared library it's in hasn't been
7041          loaded yet.  */
7042
7043       if (b->pending)
7044         break;
7045
7046       save_enable = b->enable_state;
7047       if (b->enable_state != bp_shlib_disabled)
7048         b->enable_state = bp_disabled;
7049       else
7050         /* If resetting a shlib-disabled breakpoint, we don't want to
7051            see an error message if it is not found since we will expect
7052            this to occur until the shared library is finally reloaded.
7053            We accomplish this by giving decode_line_1 a pointer to use
7054            for silent notification that the symbol is not found.  */
7055         not_found_ptr = &not_found;
7056
7057       set_language (b->language);
7058       input_radix = b->input_radix;
7059       s = b->addr_string;
7060       sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
7061                             not_found_ptr);
7062       for (i = 0; i < sals.nelts; i++)
7063         {
7064           resolve_sal_pc (&sals.sals[i]);
7065
7066           /* Reparse conditions, they might contain references to the
7067              old symtab.  */
7068           if (b->cond_string != NULL)
7069             {
7070               s = b->cond_string;
7071               if (b->cond)
7072                 {
7073                   xfree (b->cond);
7074                   /* Avoid re-freeing b->exp if an error during the call
7075                      to parse_exp_1.  */
7076                   b->cond = NULL;
7077                 }
7078               b->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 0);
7079             }
7080
7081           /* We need to re-set the breakpoint if the address changes... */
7082           if (b->loc->address != sals.sals[i].pc
7083           /* ...or new and old breakpoints both have source files, and
7084              the source file name or the line number changes...  */
7085               || (b->source_file != NULL
7086                   && sals.sals[i].symtab != NULL
7087                   && (strcmp (b->source_file, sals.sals[i].symtab->filename) != 0
7088                       || b->line_number != sals.sals[i].line)
7089               )
7090           /* ...or we switch between having a source file and not having
7091              one.  */
7092               || ((b->source_file == NULL) != (sals.sals[i].symtab == NULL))
7093             )
7094             {
7095               if (b->source_file != NULL)
7096                 xfree (b->source_file);
7097               if (sals.sals[i].symtab == NULL)
7098                 b->source_file = NULL;
7099               else
7100                 b->source_file =
7101                   savestring (sals.sals[i].symtab->filename,
7102                               strlen (sals.sals[i].symtab->filename));
7103               b->line_number = sals.sals[i].line;
7104               b->loc->requested_address = sals.sals[i].pc;
7105               b->loc->address
7106                 = adjust_breakpoint_address (b->loc->requested_address,
7107                                              b->type);
7108
7109               /* Used to check for duplicates here, but that can
7110                  cause trouble, as it doesn't check for disabled
7111                  breakpoints. */
7112
7113               mention (b);
7114
7115               /* Might be better to do this just once per breakpoint_re_set,
7116                  rather than once for every breakpoint.  */
7117               breakpoints_changed ();
7118             }
7119           b->loc->section = sals.sals[i].section;
7120           b->enable_state = save_enable;        /* Restore it, this worked. */
7121
7122
7123           /* Now that this is re-enabled, check_duplicates
7124              can be used. */
7125           check_duplicates (b);
7126
7127         }
7128       xfree (sals.sals);
7129       break;
7130
7131     case bp_watchpoint:
7132     case bp_hardware_watchpoint:
7133     case bp_read_watchpoint:
7134     case bp_access_watchpoint:
7135       innermost_block = NULL;
7136       /* The issue arises of what context to evaluate this in.  The
7137          same one as when it was set, but what does that mean when
7138          symbols have been re-read?  We could save the filename and
7139          functionname, but if the context is more local than that, the
7140          best we could do would be something like how many levels deep
7141          and which index at that particular level, but that's going to
7142          be less stable than filenames or function names.  */
7143
7144       /* So for now, just use a global context.  */
7145       if (b->exp)
7146         {
7147           xfree (b->exp);
7148           /* Avoid re-freeing b->exp if an error during the call to
7149              parse_expression.  */
7150           b->exp = NULL;
7151         }
7152       b->exp = parse_expression (b->exp_string);
7153       b->exp_valid_block = innermost_block;
7154       mark = value_mark ();
7155       if (b->val)
7156         {
7157           value_free (b->val);
7158           /* Avoid re-freeing b->val if an error during the call to
7159              evaluate_expression.  */
7160           b->val = NULL;
7161         }
7162       b->val = evaluate_expression (b->exp);
7163       release_value (b->val);
7164       if (value_lazy (b->val) && breakpoint_enabled (b))
7165         value_fetch_lazy (b->val);
7166
7167       if (b->cond_string != NULL)
7168         {
7169           s = b->cond_string;
7170           if (b->cond)
7171             {
7172               xfree (b->cond);
7173               /* Avoid re-freeing b->exp if an error during the call
7174                  to parse_exp_1.  */
7175               b->cond = NULL;
7176             }
7177           b->cond = parse_exp_1 (&s, (struct block *) 0, 0);
7178         }
7179       if (breakpoint_enabled (b))
7180         mention (b);
7181       value_free_to_mark (mark);
7182       break;
7183     case bp_catch_catch:
7184     case bp_catch_throw:
7185       break;
7186       /* We needn't really do anything to reset these, since the mask
7187          that requests them is unaffected by e.g., new libraries being
7188          loaded. */
7189     case bp_catch_fork:
7190     case bp_catch_vfork:
7191     case bp_catch_exec:
7192       break;
7193
7194     default:
7195       printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
7196       /* fall through */
7197       /* Delete longjmp and overlay event breakpoints; they will be
7198          reset later by breakpoint_re_set.  */
7199     case bp_longjmp:
7200     case bp_longjmp_resume:
7201     case bp_overlay_event:
7202       delete_breakpoint (b);
7203       break;
7204
7205       /* This breakpoint is special, it's set up when the inferior
7206          starts and we really don't want to touch it.  */
7207     case bp_shlib_event:
7208
7209       /* Like bp_shlib_event, this breakpoint type is special.
7210          Once it is set up, we do not want to touch it.  */
7211     case bp_thread_event:
7212
7213       /* Keep temporary breakpoints, which can be encountered when we step
7214          over a dlopen call and SOLIB_ADD is resetting the breakpoints.
7215          Otherwise these should have been blown away via the cleanup chain
7216          or by breakpoint_init_inferior when we rerun the executable.  */
7217     case bp_until:
7218     case bp_finish:
7219     case bp_watchpoint_scope:
7220     case bp_call_dummy:
7221     case bp_step_resume:
7222       break;
7223     }
7224
7225   return 0;
7226 }
7227
7228 /* Re-set all breakpoints after symbols have been re-loaded.  */
7229 void
7230 breakpoint_re_set (void)
7231 {
7232   struct breakpoint *b, *temp;
7233   enum language save_language;
7234   int save_input_radix;
7235
7236   save_language = current_language->la_language;
7237   save_input_radix = input_radix;
7238   ALL_BREAKPOINTS_SAFE (b, temp)
7239   {
7240     /* Format possible error msg */
7241     char *message = xstrprintf ("Error in re-setting breakpoint %d:\n",
7242                                 b->number);
7243     struct cleanup *cleanups = make_cleanup (xfree, message);
7244     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
7245     do_cleanups (cleanups);
7246   }
7247   set_language (save_language);
7248   input_radix = save_input_radix;
7249
7250   if (GET_LONGJMP_TARGET_P ())
7251     {
7252       create_longjmp_breakpoint ("longjmp");
7253       create_longjmp_breakpoint ("_longjmp");
7254       create_longjmp_breakpoint ("siglongjmp");
7255       create_longjmp_breakpoint ("_siglongjmp");
7256       create_longjmp_breakpoint (NULL);
7257     }
7258   
7259   create_overlay_event_breakpoint ("_ovly_debug_event");
7260 }
7261 \f
7262 /* Reset the thread number of this breakpoint:
7263
7264    - If the breakpoint is for all threads, leave it as-is.
7265    - Else, reset it to the current thread for inferior_ptid. */
7266 void
7267 breakpoint_re_set_thread (struct breakpoint *b)
7268 {
7269   if (b->thread != -1)
7270     {
7271       if (in_thread_list (inferior_ptid))
7272         b->thread = pid_to_thread_id (inferior_ptid);
7273     }
7274 }
7275
7276 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
7277    If from_tty is nonzero, it prints a message to that effect,
7278    which ends with a period (no newline).  */
7279
7280 void
7281 set_ignore_count (int bptnum, int count, int from_tty)
7282 {
7283   struct breakpoint *b;
7284
7285   if (count < 0)
7286     count = 0;
7287
7288   ALL_BREAKPOINTS (b)
7289     if (b->number == bptnum)
7290     {
7291       b->ignore_count = count;
7292       if (from_tty)
7293         {
7294           if (count == 0)
7295             printf_filtered (_("Will stop next time breakpoint %d is reached."),
7296                              bptnum);
7297           else if (count == 1)
7298             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
7299                              bptnum);
7300           else
7301             printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
7302                              count, bptnum);
7303         }
7304       breakpoints_changed ();
7305       breakpoint_modify_event (b->number);
7306       return;
7307     }
7308
7309   error (_("No breakpoint number %d."), bptnum);
7310 }
7311
7312 /* Clear the ignore counts of all breakpoints.  */
7313 void
7314 breakpoint_clear_ignore_counts (void)
7315 {
7316   struct breakpoint *b;
7317
7318   ALL_BREAKPOINTS (b)
7319     b->ignore_count = 0;
7320 }
7321
7322 /* Command to set ignore-count of breakpoint N to COUNT.  */
7323
7324 static void
7325 ignore_command (char *args, int from_tty)
7326 {
7327   char *p = args;
7328   int num;
7329
7330   if (p == 0)
7331     error_no_arg (_("a breakpoint number"));
7332
7333   num = get_number (&p);
7334   if (num == 0)
7335     error (_("bad breakpoint number: '%s'"), args);
7336   if (*p == 0)
7337     error (_("Second argument (specified ignore-count) is missing."));
7338
7339   set_ignore_count (num,
7340                     longest_to_int (value_as_long (parse_and_eval (p))),
7341                     from_tty);
7342   if (from_tty)
7343     printf_filtered ("\n");
7344 }
7345 \f
7346 /* Call FUNCTION on each of the breakpoints
7347    whose numbers are given in ARGS.  */
7348
7349 static void
7350 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
7351 {
7352   char *p = args;
7353   char *p1;
7354   int num;
7355   struct breakpoint *b, *tmp;
7356   int match;
7357
7358   if (p == 0)
7359     error_no_arg (_("one or more breakpoint numbers"));
7360
7361   while (*p)
7362     {
7363       match = 0;
7364       p1 = p;
7365
7366       num = get_number_or_range (&p1);
7367       if (num == 0)
7368         {
7369           warning (_("bad breakpoint number at or near '%s'"), p);
7370         }
7371       else
7372         {
7373           ALL_BREAKPOINTS_SAFE (b, tmp)
7374             if (b->number == num)
7375               {
7376                 struct breakpoint *related_breakpoint = b->related_breakpoint;
7377                 match = 1;
7378                 function (b);
7379                 if (related_breakpoint)
7380                   function (related_breakpoint);
7381                 break;
7382               }
7383           if (match == 0)
7384             printf_unfiltered (_("No breakpoint number %d.\n"), num);
7385         }
7386       p = p1;
7387     }
7388 }
7389
7390 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
7391    If from_tty is nonzero, it prints a message to that effect,
7392    which ends with a period (no newline).  */
7393
7394 void
7395 disable_breakpoint (struct breakpoint *bpt)
7396 {
7397   /* Never disable a watchpoint scope breakpoint; we want to
7398      hit them when we leave scope so we can delete both the
7399      watchpoint and its scope breakpoint at that time.  */
7400   if (bpt->type == bp_watchpoint_scope)
7401     return;
7402
7403   /* You can't disable permanent breakpoints.  */
7404   if (bpt->enable_state == bp_permanent)
7405     return;
7406
7407   bpt->enable_state = bp_disabled;
7408
7409   check_duplicates (bpt);
7410
7411   if (deprecated_modify_breakpoint_hook)
7412     deprecated_modify_breakpoint_hook (bpt);
7413   breakpoint_modify_event (bpt->number);
7414 }
7415
7416 static void
7417 disable_command (char *args, int from_tty)
7418 {
7419   struct breakpoint *bpt;
7420   if (args == 0)
7421     ALL_BREAKPOINTS (bpt)
7422       switch (bpt->type)
7423       {
7424       case bp_none:
7425         warning (_("attempted to disable apparently deleted breakpoint #%d?"),
7426                  bpt->number);
7427         continue;
7428       case bp_breakpoint:
7429       case bp_catch_load:
7430       case bp_catch_unload:
7431       case bp_catch_fork:
7432       case bp_catch_vfork:
7433       case bp_catch_exec:
7434       case bp_catch_catch:
7435       case bp_catch_throw:
7436       case bp_hardware_breakpoint:
7437       case bp_watchpoint:
7438       case bp_hardware_watchpoint:
7439       case bp_read_watchpoint:
7440       case bp_access_watchpoint:
7441         disable_breakpoint (bpt);
7442       default:
7443         continue;
7444       }
7445   else
7446     map_breakpoint_numbers (args, disable_breakpoint);
7447 }
7448
7449 static void
7450 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
7451 {
7452   int target_resources_ok, other_type_used;
7453   struct value *mark;
7454
7455   if (bpt->type == bp_hardware_breakpoint)
7456     {
7457       int i;
7458       i = hw_breakpoint_used_count ();
7459       target_resources_ok = 
7460         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint, 
7461                                             i + 1, 0);
7462       if (target_resources_ok == 0)
7463         error (_("No hardware breakpoint support in the target."));
7464       else if (target_resources_ok < 0)
7465         error (_("Hardware breakpoints used exceeds limit."));
7466     }
7467
7468   if (bpt->pending)
7469     {
7470       if (bpt->enable_state != bp_enabled)
7471         {
7472           /* When enabling a pending breakpoint, we need to check if the breakpoint
7473              is resolvable since shared libraries could have been loaded
7474              after the breakpoint was disabled.  */
7475           breakpoints_changed ();
7476           if (resolve_pending_breakpoint (bpt) == GDB_RC_OK)
7477             {
7478               delete_breakpoint (bpt);
7479               return;
7480             }
7481           bpt->enable_state = bp_enabled;
7482           bpt->disposition = disposition;
7483         }
7484     }
7485   else  /* Not a pending breakpoint.  */
7486     {
7487       if (bpt->enable_state != bp_permanent)
7488         bpt->enable_state = bp_enabled;
7489       bpt->disposition = disposition;
7490       check_duplicates (bpt);
7491       breakpoints_changed ();
7492       
7493       if (bpt->type == bp_watchpoint || 
7494           bpt->type == bp_hardware_watchpoint ||
7495           bpt->type == bp_read_watchpoint || 
7496           bpt->type == bp_access_watchpoint)
7497         {
7498           struct frame_id saved_frame_id;
7499
7500           saved_frame_id = get_frame_id (get_selected_frame (NULL));
7501           if (bpt->exp_valid_block != NULL)
7502             {
7503               struct frame_info *fr =
7504                 fr = frame_find_by_id (bpt->watchpoint_frame);
7505               if (fr == NULL)
7506                 {
7507                   printf_filtered (_("\
7508 Cannot enable watchpoint %d because the block in which its expression\n\
7509 is valid is not currently in scope.\n"), bpt->number);
7510                   bpt->enable_state = bp_disabled;
7511                   return;
7512                 }
7513               select_frame (fr);
7514             }
7515           
7516           value_free (bpt->val);
7517           mark = value_mark ();
7518           bpt->val = evaluate_expression (bpt->exp);
7519           release_value (bpt->val);
7520           if (value_lazy (bpt->val))
7521             value_fetch_lazy (bpt->val);
7522           
7523           if (bpt->type == bp_hardware_watchpoint ||
7524               bpt->type == bp_read_watchpoint ||
7525               bpt->type == bp_access_watchpoint)
7526             {
7527               int i = hw_watchpoint_used_count (bpt->type, &other_type_used);
7528               int mem_cnt = can_use_hardware_watchpoint (bpt->val);
7529               
7530               /* Hack around 'unused var' error for some targets here */
7531               (void) mem_cnt, i;
7532               target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT (
7533                                                                         bpt->type, i + mem_cnt, other_type_used);
7534               /* we can consider of type is bp_hardware_watchpoint, convert to 
7535                  bp_watchpoint in the following condition */
7536               if (target_resources_ok < 0)
7537                 {
7538                   printf_filtered (_("\
7539 Cannot enable watchpoint %d because target watch resources\n\
7540 have been allocated for other watchpoints.\n"), bpt->number);
7541                   bpt->enable_state = bp_disabled;
7542                   value_free_to_mark (mark);
7543                   return;
7544                 }
7545             }
7546           
7547           select_frame (frame_find_by_id (saved_frame_id));
7548           value_free_to_mark (mark);
7549         }
7550     }
7551
7552   if (deprecated_modify_breakpoint_hook)
7553     deprecated_modify_breakpoint_hook (bpt);
7554   breakpoint_modify_event (bpt->number);
7555 }
7556
7557 void
7558 enable_breakpoint (struct breakpoint *bpt)
7559 {
7560   do_enable_breakpoint (bpt, bpt->disposition);
7561 }
7562
7563 /* The enable command enables the specified breakpoints (or all defined
7564    breakpoints) so they once again become (or continue to be) effective
7565    in stopping the inferior.  */
7566
7567 static void
7568 enable_command (char *args, int from_tty)
7569 {
7570   struct breakpoint *bpt;
7571   if (args == 0)
7572     ALL_BREAKPOINTS (bpt)
7573       switch (bpt->type)
7574       {
7575       case bp_none:
7576         warning (_("attempted to enable apparently deleted breakpoint #%d?"),
7577                  bpt->number);
7578         continue;
7579       case bp_breakpoint:
7580       case bp_catch_load:
7581       case bp_catch_unload:
7582       case bp_catch_fork:
7583       case bp_catch_vfork:
7584       case bp_catch_exec:
7585       case bp_catch_catch:
7586       case bp_catch_throw:
7587       case bp_hardware_breakpoint:
7588       case bp_watchpoint:
7589       case bp_hardware_watchpoint:
7590       case bp_read_watchpoint:
7591       case bp_access_watchpoint:
7592         enable_breakpoint (bpt);
7593       default:
7594         continue;
7595       }
7596   else
7597     map_breakpoint_numbers (args, enable_breakpoint);
7598 }
7599
7600 static void
7601 enable_once_breakpoint (struct breakpoint *bpt)
7602 {
7603   do_enable_breakpoint (bpt, disp_disable);
7604 }
7605
7606 static void
7607 enable_once_command (char *args, int from_tty)
7608 {
7609   map_breakpoint_numbers (args, enable_once_breakpoint);
7610 }
7611
7612 static void
7613 enable_delete_breakpoint (struct breakpoint *bpt)
7614 {
7615   do_enable_breakpoint (bpt, disp_del);
7616 }
7617
7618 static void
7619 enable_delete_command (char *args, int from_tty)
7620 {
7621   map_breakpoint_numbers (args, enable_delete_breakpoint);
7622 }
7623 \f
7624 static void
7625 set_breakpoint_cmd (char *args, int from_tty)
7626 {
7627 }
7628
7629 static void
7630 show_breakpoint_cmd (char *args, int from_tty)
7631 {
7632 }
7633
7634 /* Use default_breakpoint_'s, or nothing if they aren't valid.  */
7635
7636 struct symtabs_and_lines
7637 decode_line_spec_1 (char *string, int funfirstline)
7638 {
7639   struct symtabs_and_lines sals;
7640   if (string == 0)
7641     error (_("Empty line specification."));
7642   if (default_breakpoint_valid)
7643     sals = decode_line_1 (&string, funfirstline,
7644                           default_breakpoint_symtab,
7645                           default_breakpoint_line,
7646                           (char ***) NULL, NULL);
7647   else
7648     sals = decode_line_1 (&string, funfirstline,
7649                           (struct symtab *) NULL, 0, (char ***) NULL, NULL);
7650   if (*string)
7651     error (_("Junk at end of line specification: %s"), string);
7652   return sals;
7653 }
7654
7655 /* Create and insert a raw software breakpoint at PC.  Return an
7656    identifier, which should be used to remove the breakpoint later.
7657    In general, places which call this should be using something on the
7658    breakpoint chain instead; this function should be eliminated
7659    someday.  */
7660
7661 void *
7662 deprecated_insert_raw_breakpoint (CORE_ADDR pc)
7663 {
7664   struct bp_target_info *bp_tgt;
7665
7666   bp_tgt = xmalloc (sizeof (struct bp_target_info));
7667   memset (bp_tgt, 0, sizeof (struct bp_target_info));
7668
7669   bp_tgt->placed_address = pc;
7670   if (target_insert_breakpoint (bp_tgt) != 0)
7671     {
7672       /* Could not insert the breakpoint.  */
7673       xfree (bp_tgt);
7674       return NULL;
7675     }
7676
7677   return bp_tgt;
7678 }
7679
7680 /* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint.  */
7681
7682 int
7683 deprecated_remove_raw_breakpoint (void *bp)
7684 {
7685   struct bp_target_info *bp_tgt = bp;
7686   int ret;
7687
7688   ret = target_remove_breakpoint (bp_tgt);
7689   xfree (bp_tgt);
7690
7691   return ret;
7692 }
7693
7694 /* One (or perhaps two) breakpoints used for software single stepping.  */
7695
7696 static void *single_step_breakpoints[2];
7697
7698 /* Create and insert a breakpoint for software single step.  */
7699
7700 void
7701 insert_single_step_breakpoint (CORE_ADDR next_pc)
7702 {
7703   void **bpt_p;
7704
7705   if (single_step_breakpoints[0] == NULL)
7706     bpt_p = &single_step_breakpoints[0];
7707   else
7708     {
7709       gdb_assert (single_step_breakpoints[1] == NULL);
7710       bpt_p = &single_step_breakpoints[1];
7711     }
7712
7713   /* NOTE drow/2006-04-11: A future improvement to this function would be
7714      to only create the breakpoints once, and actually put them on the
7715      breakpoint chain.  That would let us use set_raw_breakpoint.  We could
7716      adjust the addresses each time they were needed.  Doing this requires
7717      corresponding changes elsewhere where single step breakpoints are
7718      handled, however.  So, for now, we use this.  */
7719
7720   *bpt_p = deprecated_insert_raw_breakpoint (next_pc);
7721   if (*bpt_p == NULL)
7722     error (_("Could not insert single-step breakpoint at 0x%s"),
7723              paddr_nz (next_pc));
7724 }
7725
7726 /* Remove and delete any breakpoints used for software single step.  */
7727
7728 void
7729 remove_single_step_breakpoints (void)
7730 {
7731   gdb_assert (single_step_breakpoints[0] != NULL);
7732
7733   /* See insert_single_step_breakpoint for more about this deprecated
7734      call.  */
7735   deprecated_remove_raw_breakpoint (single_step_breakpoints[0]);
7736   single_step_breakpoints[0] = NULL;
7737
7738   if (single_step_breakpoints[1] != NULL)
7739     {
7740       deprecated_remove_raw_breakpoint (single_step_breakpoints[1]);
7741       single_step_breakpoints[1] = NULL;
7742     }
7743 }
7744
7745 \f
7746 /* This help string is used for the break, hbreak, tbreak and thbreak commands.
7747    It is defined as a macro to prevent duplication.
7748    COMMAND should be a string constant containing the name of the command.  */
7749 #define BREAK_ARGS_HELP(command) \
7750 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
7751 LOCATION may be a line number, function name, or \"*\" and an address.\n\
7752 If a line number is specified, break at start of code for that line.\n\
7753 If a function is specified, break at start of code for that function.\n\
7754 If an address is specified, break at that exact address.\n\
7755 With no LOCATION, uses current execution address of selected stack frame.\n\
7756 This is useful for breaking on return to a stack frame.\n\
7757 \n\
7758 THREADNUM is the number from \"info threads\".\n\
7759 CONDITION is a boolean expression.\n\
7760 \n\
7761 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
7762 \n\
7763 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
7764
7765 void
7766 _initialize_breakpoint (void)
7767 {
7768   static struct cmd_list_element *breakpoint_set_cmdlist;
7769   static struct cmd_list_element *breakpoint_show_cmdlist;
7770   struct cmd_list_element *c;
7771
7772   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
7773
7774   breakpoint_chain = 0;
7775   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
7776      before a breakpoint is set.  */
7777   breakpoint_count = 0;
7778
7779   add_com ("ignore", class_breakpoint, ignore_command, _("\
7780 Set ignore-count of breakpoint number N to COUNT.\n\
7781 Usage is `ignore N COUNT'."));
7782   if (xdb_commands)
7783     add_com_alias ("bc", "ignore", class_breakpoint, 1);
7784
7785   add_com ("commands", class_breakpoint, commands_command, _("\
7786 Set commands to be executed when a breakpoint is hit.\n\
7787 Give breakpoint number as argument after \"commands\".\n\
7788 With no argument, the targeted breakpoint is the last one set.\n\
7789 The commands themselves follow starting on the next line.\n\
7790 Type a line containing \"end\" to indicate the end of them.\n\
7791 Give \"silent\" as the first line to make the breakpoint silent;\n\
7792 then no output is printed when it is hit, except what the commands print."));
7793
7794   add_com ("condition", class_breakpoint, condition_command, _("\
7795 Specify breakpoint number N to break only if COND is true.\n\
7796 Usage is `condition N COND', where N is an integer and COND is an\n\
7797 expression to be evaluated whenever breakpoint N is reached."));
7798
7799   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
7800 Set a temporary breakpoint.\n\
7801 Like \"break\" except the breakpoint is only temporary,\n\
7802 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
7803 by using \"enable delete\" on the breakpoint number.\n\
7804 \n"
7805 BREAK_ARGS_HELP ("tbreak")));
7806   set_cmd_completer (c, location_completer);
7807
7808   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
7809 Set a hardware assisted  breakpoint.\n\
7810 Like \"break\" except the breakpoint requires hardware support,\n\
7811 some target hardware may not have this support.\n\
7812 \n"
7813 BREAK_ARGS_HELP ("hbreak")));
7814   set_cmd_completer (c, location_completer);
7815
7816   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
7817 Set a temporary hardware assisted breakpoint.\n\
7818 Like \"hbreak\" except the breakpoint is only temporary,\n\
7819 so it will be deleted when hit.\n\
7820 \n"
7821 BREAK_ARGS_HELP ("thbreak")));
7822   set_cmd_completer (c, location_completer);
7823
7824   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
7825 Enable some breakpoints.\n\
7826 Give breakpoint numbers (separated by spaces) as arguments.\n\
7827 With no subcommand, breakpoints are enabled until you command otherwise.\n\
7828 This is used to cancel the effect of the \"disable\" command.\n\
7829 With a subcommand you can enable temporarily."),
7830                   &enablelist, "enable ", 1, &cmdlist);
7831   if (xdb_commands)
7832     add_com ("ab", class_breakpoint, enable_command, _("\
7833 Enable some breakpoints.\n\
7834 Give breakpoint numbers (separated by spaces) as arguments.\n\
7835 With no subcommand, breakpoints are enabled until you command otherwise.\n\
7836 This is used to cancel the effect of the \"disable\" command.\n\
7837 With a subcommand you can enable temporarily."));
7838
7839   add_com_alias ("en", "enable", class_breakpoint, 1);
7840
7841   add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
7842 Enable some breakpoints.\n\
7843 Give breakpoint numbers (separated by spaces) as arguments.\n\
7844 This is used to cancel the effect of the \"disable\" command.\n\
7845 May be abbreviated to simply \"enable\".\n"),
7846                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
7847
7848   add_cmd ("once", no_class, enable_once_command, _("\
7849 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
7850 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
7851            &enablebreaklist);
7852
7853   add_cmd ("delete", no_class, enable_delete_command, _("\
7854 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
7855 If a breakpoint is hit while enabled in this fashion, it is deleted."),
7856            &enablebreaklist);
7857
7858   add_cmd ("delete", no_class, enable_delete_command, _("\
7859 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
7860 If a breakpoint is hit while enabled in this fashion, it is deleted."),
7861            &enablelist);
7862
7863   add_cmd ("once", no_class, enable_once_command, _("\
7864 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
7865 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
7866            &enablelist);
7867
7868   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
7869 Disable some breakpoints.\n\
7870 Arguments are breakpoint numbers with spaces in between.\n\
7871 To disable all breakpoints, give no argument.\n\
7872 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
7873                   &disablelist, "disable ", 1, &cmdlist);
7874   add_com_alias ("dis", "disable", class_breakpoint, 1);
7875   add_com_alias ("disa", "disable", class_breakpoint, 1);
7876   if (xdb_commands)
7877     add_com ("sb", class_breakpoint, disable_command, _("\
7878 Disable some breakpoints.\n\
7879 Arguments are breakpoint numbers with spaces in between.\n\
7880 To disable all breakpoints, give no argument.\n\
7881 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
7882
7883   add_cmd ("breakpoints", class_alias, disable_command, _("\
7884 Disable some breakpoints.\n\
7885 Arguments are breakpoint numbers with spaces in between.\n\
7886 To disable all breakpoints, give no argument.\n\
7887 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
7888 This command may be abbreviated \"disable\"."),
7889            &disablelist);
7890
7891   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
7892 Delete some breakpoints or auto-display expressions.\n\
7893 Arguments are breakpoint numbers with spaces in between.\n\
7894 To delete all breakpoints, give no argument.\n\
7895 \n\
7896 Also a prefix command for deletion of other GDB objects.\n\
7897 The \"unset\" command is also an alias for \"delete\"."),
7898                   &deletelist, "delete ", 1, &cmdlist);
7899   add_com_alias ("d", "delete", class_breakpoint, 1);
7900   add_com_alias ("del", "delete", class_breakpoint, 1);
7901   if (xdb_commands)
7902     add_com ("db", class_breakpoint, delete_command, _("\
7903 Delete some breakpoints.\n\
7904 Arguments are breakpoint numbers with spaces in between.\n\
7905 To delete all breakpoints, give no argument.\n"));
7906
7907   add_cmd ("breakpoints", class_alias, delete_command, _("\
7908 Delete some breakpoints or auto-display expressions.\n\
7909 Arguments are breakpoint numbers with spaces in between.\n\
7910 To delete all breakpoints, give no argument.\n\
7911 This command may be abbreviated \"delete\"."),
7912            &deletelist);
7913
7914   add_com ("clear", class_breakpoint, clear_command, _("\
7915 Clear breakpoint at specified line or function.\n\
7916 Argument may be line number, function name, or \"*\" and an address.\n\
7917 If line number is specified, all breakpoints in that line are cleared.\n\
7918 If function is specified, breakpoints at beginning of function are cleared.\n\
7919 If an address is specified, breakpoints at that address are cleared.\n\
7920 \n\
7921 With no argument, clears all breakpoints in the line that the selected frame\n\
7922 is executing in.\n\
7923 \n\
7924 See also the \"delete\" command which clears breakpoints by number."));
7925
7926   c = add_com ("break", class_breakpoint, break_command, _("\
7927 Set breakpoint at specified line or function.\n"
7928 BREAK_ARGS_HELP ("break")));
7929   set_cmd_completer (c, location_completer);
7930
7931   add_com_alias ("b", "break", class_run, 1);
7932   add_com_alias ("br", "break", class_run, 1);
7933   add_com_alias ("bre", "break", class_run, 1);
7934   add_com_alias ("brea", "break", class_run, 1);
7935
7936  if (xdb_commands)
7937     {
7938       add_com_alias ("ba", "break", class_breakpoint, 1);
7939       add_com_alias ("bu", "ubreak", class_breakpoint, 1);
7940     }
7941
7942   if (dbx_commands)
7943     {
7944       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
7945 Break in function/address or break at a line in the current file."),
7946                              &stoplist, "stop ", 1, &cmdlist);
7947       add_cmd ("in", class_breakpoint, stopin_command,
7948                _("Break in function or address."), &stoplist);
7949       add_cmd ("at", class_breakpoint, stopat_command,
7950                _("Break at a line in the current file."), &stoplist);
7951       add_com ("status", class_info, breakpoints_info, _("\
7952 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
7953 The \"Type\" column indicates one of:\n\
7954 \tbreakpoint     - normal breakpoint\n\
7955 \twatchpoint     - watchpoint\n\
7956 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
7957 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
7958 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
7959 address and file/line number respectively.\n\
7960 \n\
7961 Convenience variable \"$_\" and default examine address for \"x\"\n\
7962 are set to the address of the last breakpoint listed.\n\n\
7963 Convenience variable \"$bpnum\" contains the number of the last\n\
7964 breakpoint set."));
7965     }
7966
7967   add_info ("breakpoints", breakpoints_info, _("\
7968 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
7969 The \"Type\" column indicates one of:\n\
7970 \tbreakpoint     - normal breakpoint\n\
7971 \twatchpoint     - watchpoint\n\
7972 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
7973 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
7974 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
7975 address and file/line number respectively.\n\
7976 \n\
7977 Convenience variable \"$_\" and default examine address for \"x\"\n\
7978 are set to the address of the last breakpoint listed.\n\n\
7979 Convenience variable \"$bpnum\" contains the number of the last\n\
7980 breakpoint set."));
7981
7982   if (xdb_commands)
7983     add_com ("lb", class_breakpoint, breakpoints_info, _("\
7984 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
7985 The \"Type\" column indicates one of:\n\
7986 \tbreakpoint     - normal breakpoint\n\
7987 \twatchpoint     - watchpoint\n\
7988 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
7989 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
7990 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
7991 address and file/line number respectively.\n\
7992 \n\
7993 Convenience variable \"$_\" and default examine address for \"x\"\n\
7994 are set to the address of the last breakpoint listed.\n\n\
7995 Convenience variable \"$bpnum\" contains the number of the last\n\
7996 breakpoint set."));
7997
7998   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
7999 Status of all breakpoints, or breakpoint number NUMBER.\n\
8000 The \"Type\" column indicates one of:\n\
8001 \tbreakpoint     - normal breakpoint\n\
8002 \twatchpoint     - watchpoint\n\
8003 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
8004 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
8005 \tuntil          - internal breakpoint used by the \"until\" command\n\
8006 \tfinish         - internal breakpoint used by the \"finish\" command\n\
8007 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8008 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
8009 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
8010 address and file/line number respectively.\n\
8011 \n\
8012 Convenience variable \"$_\" and default examine address for \"x\"\n\
8013 are set to the address of the last breakpoint listed.\n\
8014 \n\
8015 Convenience variable \"$bpnum\" contains the number of the last\n\
8016 breakpoint set."),
8017            &maintenanceinfolist);
8018
8019   add_com ("catch", class_breakpoint, catch_command, _("\
8020 Set catchpoints to catch events.\n\
8021 Raised signals may be caught:\n\
8022 \tcatch signal              - all signals\n\
8023 \tcatch signal <signame>    - a particular signal\n\
8024 Raised exceptions may be caught:\n\
8025 \tcatch throw               - all exceptions, when thrown\n\
8026 \tcatch throw <exceptname>  - a particular exception, when thrown\n\
8027 \tcatch catch               - all exceptions, when caught\n\
8028 \tcatch catch <exceptname>  - a particular exception, when caught\n\
8029 Thread or process events may be caught:\n\
8030 \tcatch thread_start        - any threads, just after creation\n\
8031 \tcatch thread_exit         - any threads, just before expiration\n\
8032 \tcatch thread_join         - any threads, just after joins\n\
8033 Process events may be caught:\n\
8034 \tcatch start               - any processes, just after creation\n\
8035 \tcatch exit                - any processes, just before expiration\n\
8036 \tcatch fork                - calls to fork()\n\
8037 \tcatch vfork               - calls to vfork()\n\
8038 \tcatch exec                - calls to exec()\n\
8039 Dynamically-linked library events may be caught:\n\
8040 \tcatch load                - loads of any library\n\
8041 \tcatch load <libname>      - loads of a particular library\n\
8042 \tcatch unload              - unloads of any library\n\
8043 \tcatch unload <libname>    - unloads of a particular library\n\
8044 The act of your program's execution stopping may also be caught:\n\
8045 \tcatch stop\n\n\
8046 C++ exceptions may be caught:\n\
8047 \tcatch throw               - all exceptions, when thrown\n\
8048 \tcatch catch               - all exceptions, when caught\n\
8049 \n\
8050 Do \"help set follow-fork-mode\" for info on debugging your program\n\
8051 after a fork or vfork is caught.\n\n\
8052 Do \"help breakpoints\" for info on other commands dealing with breakpoints."));
8053
8054   add_com ("tcatch", class_breakpoint, tcatch_command, _("\
8055 Set temporary catchpoints to catch events.\n\
8056 Args like \"catch\" command.\n\
8057 Like \"catch\" except the catchpoint is only temporary,\n\
8058 so it will be deleted when hit.  Equivalent to \"catch\" followed\n\
8059 by using \"enable delete\" on the catchpoint number."));
8060
8061   c = add_com ("watch", class_breakpoint, watch_command, _("\
8062 Set a watchpoint for an expression.\n\
8063 A watchpoint stops execution of your program whenever the value of\n\
8064 an expression changes."));
8065   set_cmd_completer (c, location_completer);
8066
8067   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
8068 Set a read watchpoint for an expression.\n\
8069 A watchpoint stops execution of your program whenever the value of\n\
8070 an expression is read."));
8071   set_cmd_completer (c, location_completer);
8072
8073   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
8074 Set a watchpoint for an expression.\n\
8075 A watchpoint stops execution of your program whenever the value of\n\
8076 an expression is either read or written."));
8077   set_cmd_completer (c, location_completer);
8078
8079   add_info ("watchpoints", breakpoints_info,
8080             _("Synonym for ``info breakpoints''."));
8081
8082
8083   /* XXX: cagney/2005-02-23: This should be a boolean, and should
8084      respond to changes - contrary to the description.  */
8085   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
8086                             &can_use_hw_watchpoints, _("\
8087 Set debugger's willingness to use watchpoint hardware."), _("\
8088 Show debugger's willingness to use watchpoint hardware."), _("\
8089 If zero, gdb will not use hardware for new watchpoints, even if\n\
8090 such is available.  (However, any hardware watchpoints that were\n\
8091 created before setting this to nonzero, will continue to use watchpoint\n\
8092 hardware.)"),
8093                             NULL,
8094                             show_can_use_hw_watchpoints,
8095                             &setlist, &showlist);
8096
8097   can_use_hw_watchpoints = 1;
8098
8099   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
8100 Breakpoint specific settings\n\
8101 Configure various breakpoint-specific variables such as\n\
8102 pending breakpoint behavior"),
8103                   &breakpoint_set_cmdlist, "set breakpoint ",
8104                   0/*allow-unknown*/, &setlist);
8105   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
8106 Breakpoint specific settings\n\
8107 Configure various breakpoint-specific variables such as\n\
8108 pending breakpoint behavior"),
8109                   &breakpoint_show_cmdlist, "show breakpoint ",
8110                   0/*allow-unknown*/, &showlist);
8111
8112   add_setshow_auto_boolean_cmd ("pending", no_class,
8113                                 &pending_break_support, _("\
8114 Set debugger's behavior regarding pending breakpoints."), _("\
8115 Show debugger's behavior regarding pending breakpoints."), _("\
8116 If on, an unrecognized breakpoint location will cause gdb to create a\n\
8117 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
8118 an error.  If auto, an unrecognized breakpoint location results in a\n\
8119 user-query to see if a pending breakpoint should be created."),
8120                                 NULL,
8121                                 show_pending_break_support,
8122                                 &breakpoint_set_cmdlist,
8123                                 &breakpoint_show_cmdlist);
8124
8125   pending_break_support = AUTO_BOOLEAN_AUTO;
8126 }