OSDN Git Service

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