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