OSDN Git Service

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