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