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