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