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