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