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