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