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