xref: /dragonfly/contrib/gdb-7/gdb/breakpoint.c (revision fb151170)
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    2008, 2009, 2010, 2011 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 "arch-utils.h"
24 #include <ctype.h>
25 #include "hashtab.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "breakpoint.h"
29 #include "tracepoint.h"
30 #include "gdbtypes.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "value.h"
35 #include "command.h"
36 #include "inferior.h"
37 #include "gdbthread.h"
38 #include "target.h"
39 #include "language.h"
40 #include "gdb_string.h"
41 #include "demangle.h"
42 #include "filenames.h"
43 #include "annotate.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "source.h"
47 #include "linespec.h"
48 #include "completer.h"
49 #include "gdb.h"
50 #include "ui-out.h"
51 #include "cli/cli-script.h"
52 #include "gdb_assert.h"
53 #include "block.h"
54 #include "solib.h"
55 #include "solist.h"
56 #include "observer.h"
57 #include "exceptions.h"
58 #include "memattr.h"
59 #include "ada-lang.h"
60 #include "top.h"
61 #include "wrapper.h"
62 #include "valprint.h"
63 #include "jit.h"
64 #include "xml-syscall.h"
65 #include "parser-defs.h"
66 #include "cli/cli-utils.h"
67 
68 /* readline include files */
69 #include "readline/readline.h"
70 #include "readline/history.h"
71 
72 /* readline defines this.  */
73 #undef savestring
74 
75 #include "mi/mi-common.h"
76 #include "python/python.h"
77 
78 /* Arguments to pass as context to some catch command handlers.  */
79 #define CATCH_PERMANENT ((void *) (uintptr_t) 0)
80 #define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
81 
82 /* Prototypes for local functions.  */
83 
84 static void enable_delete_command (char *, int);
85 
86 static void enable_once_command (char *, int);
87 
88 static void disable_command (char *, int);
89 
90 static void enable_command (char *, int);
91 
92 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
93 						      void *),
94 				    void *);
95 
96 static void ignore_command (char *, int);
97 
98 static int breakpoint_re_set_one (void *);
99 
100 static void clear_command (char *, int);
101 
102 static void catch_command (char *, int);
103 
104 static int can_use_hardware_watchpoint (struct value *, int);
105 
106 static void break_command_1 (char *, int, int);
107 
108 static void mention (struct breakpoint *);
109 
110 /* This function is used in gdbtk sources and thus can not be made
111    static.  */
112 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
113 					      struct symtab_and_line,
114 					      enum bptype);
115 
116 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
117 
118 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
119 					    CORE_ADDR bpaddr,
120                                             enum bptype bptype);
121 
122 static void describe_other_breakpoints (struct gdbarch *,
123 					struct program_space *, CORE_ADDR,
124 					struct obj_section *, int);
125 
126 static int breakpoint_address_match (struct address_space *aspace1,
127 				     CORE_ADDR addr1,
128 				     struct address_space *aspace2,
129 				     CORE_ADDR addr2);
130 
131 static int watchpoint_locations_match (struct bp_location *loc1,
132 				       struct bp_location *loc2);
133 
134 static int breakpoint_location_address_match (struct bp_location *bl,
135 					      struct address_space *aspace,
136 					      CORE_ADDR addr);
137 
138 static void breakpoints_info (char *, int);
139 
140 static void watchpoints_info (char *, int);
141 
142 static int breakpoint_1 (char *, int,
143 			 int (*) (const struct breakpoint *));
144 
145 static int breakpoint_cond_eval (void *);
146 
147 static void cleanup_executing_breakpoints (void *);
148 
149 static void commands_command (char *, int);
150 
151 static void condition_command (char *, int);
152 
153 typedef enum
154   {
155     mark_inserted,
156     mark_uninserted
157   }
158 insertion_state_t;
159 
160 static int remove_breakpoint (struct bp_location *, insertion_state_t);
161 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
162 
163 static enum print_stop_action print_it_typical (bpstat);
164 
165 static enum print_stop_action print_bp_stop_message (bpstat bs);
166 
167 static int watchpoint_check (void *);
168 
169 static void maintenance_info_breakpoints (char *, int);
170 
171 static int hw_breakpoint_used_count (void);
172 
173 static int hw_watchpoint_used_count (enum bptype, int *);
174 
175 static void hbreak_command (char *, int);
176 
177 static void thbreak_command (char *, int);
178 
179 static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
180 
181 static void stop_command (char *arg, int from_tty);
182 
183 static void stopin_command (char *arg, int from_tty);
184 
185 static void stopat_command (char *arg, int from_tty);
186 
187 static char *ep_parse_optional_if_clause (char **arg);
188 
189 static void catch_exception_command_1 (enum exception_event_kind ex_event,
190 				       char *arg, int tempflag, int from_tty);
191 
192 static void tcatch_command (char *arg, int from_tty);
193 
194 static void detach_single_step_breakpoints (void);
195 
196 static int single_step_breakpoint_inserted_here_p (struct address_space *,
197 						   CORE_ADDR pc);
198 
199 static void free_bp_location (struct bp_location *loc);
200 static void incref_bp_location (struct bp_location *loc);
201 static void decref_bp_location (struct bp_location **loc);
202 
203 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
204 
205 static void update_global_location_list (int);
206 
207 static void update_global_location_list_nothrow (int);
208 
209 static int is_hardware_watchpoint (const struct breakpoint *bpt);
210 
211 static int is_watchpoint (const struct breakpoint *bpt);
212 
213 static void insert_breakpoint_locations (void);
214 
215 static int syscall_catchpoint_p (struct breakpoint *b);
216 
217 static void tracepoints_info (char *, int);
218 
219 static void delete_trace_command (char *, int);
220 
221 static void enable_trace_command (char *, int);
222 
223 static void disable_trace_command (char *, int);
224 
225 static void trace_pass_command (char *, int);
226 
227 /* Assuming we're creating a static tracepoint, does S look like a
228    static tracepoint marker spec ("-m MARKER_ID")?  */
229 #define is_marker_spec(s)						\
230   (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
231 
232 /* A reference-counted struct command_line.  This lets multiple
233    breakpoints share a single command list.  */
234 struct counted_command_line
235 {
236   /* The reference count.  */
237   int refc;
238 
239   /* The command list.  */
240   struct command_line *commands;
241 };
242 
243 struct command_line *
244 breakpoint_commands (struct breakpoint *b)
245 {
246   return b->commands ? b->commands->commands : NULL;
247 }
248 
249 /* Flag indicating that a command has proceeded the inferior past the
250    current breakpoint.  */
251 
252 static int breakpoint_proceeded;
253 
254 const char *
255 bpdisp_text (enum bpdisp disp)
256 {
257   /* NOTE: the following values are a part of MI protocol and
258      represent values of 'disp' field returned when inferior stops at
259      a breakpoint.  */
260   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
261 
262   return bpdisps[(int) disp];
263 }
264 
265 /* Prototypes for exported functions.  */
266 /* If FALSE, gdb will not use hardware support for watchpoints, even
267    if such is available.  */
268 static int can_use_hw_watchpoints;
269 
270 static void
271 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
272 			     struct cmd_list_element *c,
273 			     const char *value)
274 {
275   fprintf_filtered (file,
276 		    _("Debugger's willingness to use "
277 		      "watchpoint hardware is %s.\n"),
278 		    value);
279 }
280 
281 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
282    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
283    for unrecognized breakpoint locations.
284    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
285 static enum auto_boolean pending_break_support;
286 static void
287 show_pending_break_support (struct ui_file *file, int from_tty,
288 			    struct cmd_list_element *c,
289 			    const char *value)
290 {
291   fprintf_filtered (file,
292 		    _("Debugger's behavior regarding "
293 		      "pending breakpoints is %s.\n"),
294 		    value);
295 }
296 
297 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
298    set with "break" but falling in read-only memory.
299    If 0, gdb will warn about such breakpoints, but won't automatically
300    use hardware breakpoints.  */
301 static int automatic_hardware_breakpoints;
302 static void
303 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
304 				     struct cmd_list_element *c,
305 				     const char *value)
306 {
307   fprintf_filtered (file,
308 		    _("Automatic usage of hardware breakpoints is %s.\n"),
309 		    value);
310 }
311 
312 /* If on, gdb will keep breakpoints inserted even as inferior is
313    stopped, and immediately insert any new breakpoints.  If off, gdb
314    will insert breakpoints into inferior only when resuming it, and
315    will remove breakpoints upon stop.  If auto, GDB will behave as ON
316    if in non-stop mode, and as OFF if all-stop mode.*/
317 
318 static const char always_inserted_auto[] = "auto";
319 static const char always_inserted_on[] = "on";
320 static const char always_inserted_off[] = "off";
321 static const char *always_inserted_enums[] = {
322   always_inserted_auto,
323   always_inserted_off,
324   always_inserted_on,
325   NULL
326 };
327 static const char *always_inserted_mode = always_inserted_auto;
328 static void
329 show_always_inserted_mode (struct ui_file *file, int from_tty,
330 		     struct cmd_list_element *c, const char *value)
331 {
332   if (always_inserted_mode == always_inserted_auto)
333     fprintf_filtered (file,
334 		      _("Always inserted breakpoint "
335 			"mode is %s (currently %s).\n"),
336 		      value,
337 		      breakpoints_always_inserted_mode () ? "on" : "off");
338   else
339     fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
340 		      value);
341 }
342 
343 int
344 breakpoints_always_inserted_mode (void)
345 {
346   return (always_inserted_mode == always_inserted_on
347 	  || (always_inserted_mode == always_inserted_auto && non_stop));
348 }
349 
350 void _initialize_breakpoint (void);
351 
352 /* Are we executing breakpoint commands?  */
353 static int executing_breakpoint_commands;
354 
355 /* Are overlay event breakpoints enabled? */
356 static int overlay_events_enabled;
357 
358 /* See description in breakpoint.h. */
359 int target_exact_watchpoints = 0;
360 
361 /* Walk the following statement or block through all breakpoints.
362    ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
363    current breakpoint.  */
364 
365 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
366 
367 #define ALL_BREAKPOINTS_SAFE(B,TMP)	\
368 	for (B = breakpoint_chain;	\
369 	     B ? (TMP=B->next, 1): 0;	\
370 	     B = TMP)
371 
372 /* Similar iterator for the low-level breakpoints.  SAFE variant is
373    not provided so update_global_location_list must not be called
374    while executing the block of ALL_BP_LOCATIONS.  */
375 
376 #define ALL_BP_LOCATIONS(B,BP_TMP)					\
377 	for (BP_TMP = bp_location;					\
378 	     BP_TMP < bp_location + bp_location_count && (B = *BP_TMP);	\
379 	     BP_TMP++)
380 
381 /* Iterator for tracepoints only.  */
382 
383 #define ALL_TRACEPOINTS(B)  \
384   for (B = breakpoint_chain; B; B = B->next)  \
385     if (is_tracepoint (B))
386 
387 /* Chains of all breakpoints defined.  */
388 
389 struct breakpoint *breakpoint_chain;
390 
391 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
392 
393 static struct bp_location **bp_location;
394 
395 /* Number of elements of BP_LOCATION.  */
396 
397 static unsigned bp_location_count;
398 
399 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
400    ADDRESS for the current elements of BP_LOCATION which get a valid
401    result from bp_location_has_shadow.  You can use it for roughly
402    limiting the subrange of BP_LOCATION to scan for shadow bytes for
403    an address you need to read.  */
404 
405 static CORE_ADDR bp_location_placed_address_before_address_max;
406 
407 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
408    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
409    BP_LOCATION which get a valid result from bp_location_has_shadow.
410    You can use it for roughly limiting the subrange of BP_LOCATION to
411    scan for shadow bytes for an address you need to read.  */
412 
413 static CORE_ADDR bp_location_shadow_len_after_address_max;
414 
415 /* The locations that no longer correspond to any breakpoint, unlinked
416    from bp_location array, but for which a hit may still be reported
417    by a target.  */
418 VEC(bp_location_p) *moribund_locations = NULL;
419 
420 /* Number of last breakpoint made.  */
421 
422 static int breakpoint_count;
423 
424 /* The value of `breakpoint_count' before the last command that
425    created breakpoints.  If the last (break-like) command created more
426    than one breakpoint, then the difference between BREAKPOINT_COUNT
427    and PREV_BREAKPOINT_COUNT is more than one.  */
428 static int prev_breakpoint_count;
429 
430 /* Number of last tracepoint made.  */
431 
432 static int tracepoint_count;
433 
434 static struct cmd_list_element *breakpoint_set_cmdlist;
435 static struct cmd_list_element *breakpoint_show_cmdlist;
436 struct cmd_list_element *save_cmdlist;
437 
438 /* Return whether a breakpoint is an active enabled breakpoint.  */
439 static int
440 breakpoint_enabled (struct breakpoint *b)
441 {
442   return (b->enable_state == bp_enabled);
443 }
444 
445 /* Set breakpoint count to NUM.  */
446 
447 static void
448 set_breakpoint_count (int num)
449 {
450   prev_breakpoint_count = breakpoint_count;
451   breakpoint_count = num;
452   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
453 }
454 
455 /* Used by `start_rbreak_breakpoints' below, to record the current
456    breakpoint count before "rbreak" creates any breakpoint.  */
457 static int rbreak_start_breakpoint_count;
458 
459 /* Called at the start an "rbreak" command to record the first
460    breakpoint made.  */
461 
462 void
463 start_rbreak_breakpoints (void)
464 {
465   rbreak_start_breakpoint_count = breakpoint_count;
466 }
467 
468 /* Called at the end of an "rbreak" command to record the last
469    breakpoint made.  */
470 
471 void
472 end_rbreak_breakpoints (void)
473 {
474   prev_breakpoint_count = rbreak_start_breakpoint_count;
475 }
476 
477 /* Used in run_command to zero the hit count when a new run starts.  */
478 
479 void
480 clear_breakpoint_hit_counts (void)
481 {
482   struct breakpoint *b;
483 
484   ALL_BREAKPOINTS (b)
485     b->hit_count = 0;
486 }
487 
488 /* Allocate a new counted_command_line with reference count of 1.
489    The new structure owns COMMANDS.  */
490 
491 static struct counted_command_line *
492 alloc_counted_command_line (struct command_line *commands)
493 {
494   struct counted_command_line *result
495     = xmalloc (sizeof (struct counted_command_line));
496 
497   result->refc = 1;
498   result->commands = commands;
499   return result;
500 }
501 
502 /* Increment reference count.  This does nothing if CMD is NULL.  */
503 
504 static void
505 incref_counted_command_line (struct counted_command_line *cmd)
506 {
507   if (cmd)
508     ++cmd->refc;
509 }
510 
511 /* Decrement reference count.  If the reference count reaches 0,
512    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
513    nothing if *CMDP is NULL.  */
514 
515 static void
516 decref_counted_command_line (struct counted_command_line **cmdp)
517 {
518   if (*cmdp)
519     {
520       if (--(*cmdp)->refc == 0)
521 	{
522 	  free_command_lines (&(*cmdp)->commands);
523 	  xfree (*cmdp);
524 	}
525       *cmdp = NULL;
526     }
527 }
528 
529 /* A cleanup function that calls decref_counted_command_line.  */
530 
531 static void
532 do_cleanup_counted_command_line (void *arg)
533 {
534   decref_counted_command_line (arg);
535 }
536 
537 /* Create a cleanup that calls decref_counted_command_line on the
538    argument.  */
539 
540 static struct cleanup *
541 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
542 {
543   return make_cleanup (do_cleanup_counted_command_line, cmdp);
544 }
545 
546 /* Default address, symtab and line to put a breakpoint at
547    for "break" command with no arg.
548    If default_breakpoint_valid is zero, the other three are
549    not valid, and "break" with no arg is an error.
550 
551    This set by print_stack_frame, which calls set_default_breakpoint.  */
552 
553 int default_breakpoint_valid;
554 CORE_ADDR default_breakpoint_address;
555 struct symtab *default_breakpoint_symtab;
556 int default_breakpoint_line;
557 struct program_space *default_breakpoint_pspace;
558 
559 
560 /* Return the breakpoint with the specified number, or NULL
561    if the number does not refer to an existing breakpoint.  */
562 
563 struct breakpoint *
564 get_breakpoint (int num)
565 {
566   struct breakpoint *b;
567 
568   ALL_BREAKPOINTS (b)
569     if (b->number == num)
570       return b;
571 
572   return NULL;
573 }
574 
575 
576 
577 void
578 set_breakpoint_condition (struct breakpoint *b, char *exp,
579 			  int from_tty)
580 {
581   struct bp_location *loc = b->loc;
582 
583   for (; loc; loc = loc->next)
584     {
585       xfree (loc->cond);
586       loc->cond = NULL;
587     }
588   xfree (b->cond_string);
589   b->cond_string = NULL;
590   xfree (b->cond_exp);
591   b->cond_exp = NULL;
592 
593   if (*exp == 0)
594     {
595       if (from_tty)
596 	printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
597     }
598   else
599     {
600       char *arg = exp;
601 
602       /* I don't know if it matters whether this is the string the user
603 	 typed in or the decompiled expression.  */
604       b->cond_string = xstrdup (arg);
605       b->condition_not_parsed = 0;
606 
607       if (is_watchpoint (b))
608 	{
609 	  innermost_block = NULL;
610 	  arg = exp;
611 	  b->cond_exp = parse_exp_1 (&arg, 0, 0);
612 	  if (*arg)
613 	    error (_("Junk at end of expression"));
614 	  b->cond_exp_valid_block = innermost_block;
615 	}
616       else
617 	{
618 	  for (loc = b->loc; loc; loc = loc->next)
619 	    {
620 	      arg = exp;
621 	      loc->cond =
622 		parse_exp_1 (&arg, block_for_pc (loc->address), 0);
623 	      if (*arg)
624 		error (_("Junk at end of expression"));
625 	    }
626 	}
627     }
628   breakpoints_changed ();
629   observer_notify_breakpoint_modified (b->number);
630 }
631 
632 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
633 
634 static void
635 condition_command (char *arg, int from_tty)
636 {
637   struct breakpoint *b;
638   char *p;
639   int bnum;
640 
641   if (arg == 0)
642     error_no_arg (_("breakpoint number"));
643 
644   p = arg;
645   bnum = get_number (&p);
646   if (bnum == 0)
647     error (_("Bad breakpoint argument: '%s'"), arg);
648 
649   ALL_BREAKPOINTS (b)
650     if (b->number == bnum)
651       {
652 	/* Check if this breakpoint has a Python object assigned to
653 	   it, and if it has a definition of the "stop"
654 	   method.  This method and conditions entered into GDB from
655 	   the CLI are mutually exclusive.  */
656 	if (b->py_bp_object
657 	    && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
658 	  error (_("Cannot set a condition where a Python 'stop' "
659 		   "method has been defined in the breakpoint."));
660 	set_breakpoint_condition (b, p, from_tty);
661 	return;
662       }
663 
664   error (_("No breakpoint number %d."), bnum);
665 }
666 
667 /* Check that COMMAND do not contain commands that are suitable
668    only for tracepoints and not suitable for ordinary breakpoints.
669    Throw if any such commands is found.  */
670 
671 static void
672 check_no_tracepoint_commands (struct command_line *commands)
673 {
674   struct command_line *c;
675 
676   for (c = commands; c; c = c->next)
677     {
678       int i;
679 
680       if (c->control_type == while_stepping_control)
681 	error (_("The 'while-stepping' command can "
682 		 "only be used for tracepoints"));
683 
684       for (i = 0; i < c->body_count; ++i)
685 	check_no_tracepoint_commands ((c->body_list)[i]);
686 
687       /* Not that command parsing removes leading whitespace and comment
688 	 lines and also empty lines.  So, we only need to check for
689 	 command directly.  */
690       if (strstr (c->line, "collect ") == c->line)
691 	error (_("The 'collect' command can only be used for tracepoints"));
692 
693       if (strstr (c->line, "teval ") == c->line)
694 	error (_("The 'teval' command can only be used for tracepoints"));
695     }
696 }
697 
698 /* Encapsulate tests for different types of tracepoints.  */
699 
700 int
701 is_tracepoint (const struct breakpoint *b)
702 {
703   return (b->type == bp_tracepoint
704 	  || b->type == bp_fast_tracepoint
705 	  || b->type == bp_static_tracepoint);
706 }
707 
708 /* A helper function that validsates that COMMANDS are valid for a
709    breakpoint.  This function will throw an exception if a problem is
710    found.  */
711 
712 static void
713 validate_commands_for_breakpoint (struct breakpoint *b,
714 				  struct command_line *commands)
715 {
716   if (is_tracepoint (b))
717     {
718       /* We need to verify that each top-level element of commands is
719 	 valid for tracepoints, that there's at most one
720 	 while-stepping element, and that while-stepping's body has
721 	 valid tracing commands excluding nested while-stepping.  */
722       struct command_line *c;
723       struct command_line *while_stepping = 0;
724       for (c = commands; c; c = c->next)
725 	{
726 	  if (c->control_type == while_stepping_control)
727 	    {
728 	      if (b->type == bp_fast_tracepoint)
729 		error (_("The 'while-stepping' command "
730 			 "cannot be used for fast tracepoint"));
731 	      else if (b->type == bp_static_tracepoint)
732 		error (_("The 'while-stepping' command "
733 			 "cannot be used for static tracepoint"));
734 
735 	      if (while_stepping)
736 		error (_("The 'while-stepping' command "
737 			 "can be used only once"));
738 	      else
739 		while_stepping = c;
740 	    }
741 	}
742       if (while_stepping)
743 	{
744 	  struct command_line *c2;
745 
746 	  gdb_assert (while_stepping->body_count == 1);
747 	  c2 = while_stepping->body_list[0];
748 	  for (; c2; c2 = c2->next)
749 	    {
750 	      if (c2->control_type == while_stepping_control)
751 		error (_("The 'while-stepping' command cannot be nested"));
752 	    }
753 	}
754     }
755   else
756     {
757       check_no_tracepoint_commands (commands);
758     }
759 }
760 
761 /* Return a vector of all the static tracepoints set at ADDR.  The
762    caller is responsible for releasing the vector.  */
763 
764 VEC(breakpoint_p) *
765 static_tracepoints_here (CORE_ADDR addr)
766 {
767   struct breakpoint *b;
768   VEC(breakpoint_p) *found = 0;
769   struct bp_location *loc;
770 
771   ALL_BREAKPOINTS (b)
772     if (b->type == bp_static_tracepoint)
773       {
774 	for (loc = b->loc; loc; loc = loc->next)
775 	  if (loc->address == addr)
776 	    VEC_safe_push(breakpoint_p, found, b);
777       }
778 
779   return found;
780 }
781 
782 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
783    validate that only allowed commands are included.  */
784 
785 void
786 breakpoint_set_commands (struct breakpoint *b,
787 			 struct command_line *commands)
788 {
789   validate_commands_for_breakpoint (b, commands);
790 
791   decref_counted_command_line (&b->commands);
792   b->commands = alloc_counted_command_line (commands);
793   breakpoints_changed ();
794   observer_notify_breakpoint_modified (b->number);
795 }
796 
797 /* Set the internal `silent' flag on the breakpoint.  Note that this
798    is not the same as the "silent" that may appear in the breakpoint's
799    commands.  */
800 
801 void
802 breakpoint_set_silent (struct breakpoint *b, int silent)
803 {
804   int old_silent = b->silent;
805 
806   b->silent = silent;
807   if (old_silent != silent)
808     observer_notify_breakpoint_modified (b->number);
809 }
810 
811 /* Set the thread for this breakpoint.  If THREAD is -1, make the
812    breakpoint work for any thread.  */
813 
814 void
815 breakpoint_set_thread (struct breakpoint *b, int thread)
816 {
817   int old_thread = b->thread;
818 
819   b->thread = thread;
820   if (old_thread != thread)
821     observer_notify_breakpoint_modified (b->number);
822 }
823 
824 /* Set the task for this breakpoint.  If TASK is 0, make the
825    breakpoint work for any task.  */
826 
827 void
828 breakpoint_set_task (struct breakpoint *b, int task)
829 {
830   int old_task = b->task;
831 
832   b->task = task;
833   if (old_task != task)
834     observer_notify_breakpoint_modified (b->number);
835 }
836 
837 void
838 check_tracepoint_command (char *line, void *closure)
839 {
840   struct breakpoint *b = closure;
841 
842   validate_actionline (&line, b);
843 }
844 
845 /* A structure used to pass information through
846    map_breakpoint_numbers.  */
847 
848 struct commands_info
849 {
850   /* True if the command was typed at a tty.  */
851   int from_tty;
852 
853   /* The breakpoint range spec.  */
854   char *arg;
855 
856   /* Non-NULL if the body of the commands are being read from this
857      already-parsed command.  */
858   struct command_line *control;
859 
860   /* The command lines read from the user, or NULL if they have not
861      yet been read.  */
862   struct counted_command_line *cmd;
863 };
864 
865 /* A callback for map_breakpoint_numbers that sets the commands for
866    commands_command.  */
867 
868 static void
869 do_map_commands_command (struct breakpoint *b, void *data)
870 {
871   struct commands_info *info = data;
872 
873   if (info->cmd == NULL)
874     {
875       struct command_line *l;
876 
877       if (info->control != NULL)
878 	l = copy_command_lines (info->control->body_list[0]);
879       else
880 	{
881 	  struct cleanup *old_chain;
882 	  char *str;
883 
884 	  str = xstrprintf (_("Type commands for breakpoint(s) "
885 			      "%s, one per line."),
886 			    info->arg);
887 
888 	  old_chain = make_cleanup (xfree, str);
889 
890 	  l = read_command_lines (str,
891 				  info->from_tty, 1,
892 				  (is_tracepoint (b)
893 				   ? check_tracepoint_command : 0),
894 				  b);
895 
896 	  do_cleanups (old_chain);
897 	}
898 
899       info->cmd = alloc_counted_command_line (l);
900     }
901 
902   /* If a breakpoint was on the list more than once, we don't need to
903      do anything.  */
904   if (b->commands != info->cmd)
905     {
906       validate_commands_for_breakpoint (b, info->cmd->commands);
907       incref_counted_command_line (info->cmd);
908       decref_counted_command_line (&b->commands);
909       b->commands = info->cmd;
910       breakpoints_changed ();
911       observer_notify_breakpoint_modified (b->number);
912     }
913 }
914 
915 static void
916 commands_command_1 (char *arg, int from_tty,
917 		    struct command_line *control)
918 {
919   struct cleanup *cleanups;
920   struct commands_info info;
921 
922   info.from_tty = from_tty;
923   info.control = control;
924   info.cmd = NULL;
925   /* If we read command lines from the user, then `info' will hold an
926      extra reference to the commands that we must clean up.  */
927   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
928 
929   if (arg == NULL || !*arg)
930     {
931       if (breakpoint_count - prev_breakpoint_count > 1)
932 	arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
933 			  breakpoint_count);
934       else if (breakpoint_count > 0)
935 	arg = xstrprintf ("%d", breakpoint_count);
936       else
937 	{
938 	  /* So that we don't try to free the incoming non-NULL
939 	     argument in the cleanup below.  Mapping breakpoint
940 	     numbers will fail in this case.  */
941 	  arg = NULL;
942 	}
943     }
944   else
945     /* The command loop has some static state, so we need to preserve
946        our argument.  */
947     arg = xstrdup (arg);
948 
949   if (arg != NULL)
950     make_cleanup (xfree, arg);
951 
952   info.arg = arg;
953 
954   map_breakpoint_numbers (arg, do_map_commands_command, &info);
955 
956   if (info.cmd == NULL)
957     error (_("No breakpoints specified."));
958 
959   do_cleanups (cleanups);
960 }
961 
962 static void
963 commands_command (char *arg, int from_tty)
964 {
965   commands_command_1 (arg, from_tty, NULL);
966 }
967 
968 /* Like commands_command, but instead of reading the commands from
969    input stream, takes them from an already parsed command structure.
970 
971    This is used by cli-script.c to DTRT with breakpoint commands
972    that are part of if and while bodies.  */
973 enum command_control_type
974 commands_from_control_command (char *arg, struct command_line *cmd)
975 {
976   commands_command_1 (arg, 0, cmd);
977   return simple_control;
978 }
979 
980 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
981 
982 static int
983 bp_location_has_shadow (struct bp_location *bl)
984 {
985   if (bl->loc_type != bp_loc_software_breakpoint)
986     return 0;
987   if (!bl->inserted)
988     return 0;
989   if (bl->target_info.shadow_len == 0)
990     /* bp isn't valid, or doesn't shadow memory.  */
991     return 0;
992   return 1;
993 }
994 
995 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
996    by replacing any memory breakpoints with their shadowed contents.
997 
998    The range of shadowed area by each bp_location is:
999      bl->address - bp_location_placed_address_before_address_max
1000      up to bl->address + bp_location_shadow_len_after_address_max
1001    The range we were requested to resolve shadows for is:
1002      memaddr ... memaddr + len
1003    Thus the safe cutoff boundaries for performance optimization are
1004      memaddr + len <= (bl->address
1005 		       - bp_location_placed_address_before_address_max)
1006    and:
1007      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
1008 
1009 void
1010 breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
1011 {
1012   /* Left boundary, right boundary and median element of our binary
1013      search.  */
1014   unsigned bc_l, bc_r, bc;
1015 
1016   /* Find BC_L which is a leftmost element which may affect BUF
1017      content.  It is safe to report lower value but a failure to
1018      report higher one.  */
1019 
1020   bc_l = 0;
1021   bc_r = bp_location_count;
1022   while (bc_l + 1 < bc_r)
1023     {
1024       struct bp_location *bl;
1025 
1026       bc = (bc_l + bc_r) / 2;
1027       bl = bp_location[bc];
1028 
1029       /* Check first BL->ADDRESS will not overflow due to the added
1030 	 constant.  Then advance the left boundary only if we are sure
1031 	 the BC element can in no way affect the BUF content (MEMADDR
1032 	 to MEMADDR + LEN range).
1033 
1034 	 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1035 	 offset so that we cannot miss a breakpoint with its shadow
1036 	 range tail still reaching MEMADDR.  */
1037 
1038       if ((bl->address + bp_location_shadow_len_after_address_max
1039 	   >= bl->address)
1040 	  && (bl->address + bp_location_shadow_len_after_address_max
1041 	      <= memaddr))
1042 	bc_l = bc;
1043       else
1044 	bc_r = bc;
1045     }
1046 
1047   /* Due to the binary search above, we need to make sure we pick the
1048      first location that's at BC_L's address.  E.g., if there are
1049      multiple locations at the same address, BC_L may end up pointing
1050      at a duplicate location, and miss the "master"/"inserted"
1051      location.  Say, given locations L1, L2 and L3 at addresses A and
1052      B:
1053 
1054       L1@A, L2@A, L3@B, ...
1055 
1056      BC_L could end up pointing at location L2, while the "master"
1057      location could be L1.  Since the `loc->inserted' flag is only set
1058      on "master" locations, we'd forget to restore the shadow of L1
1059      and L2.  */
1060   while (bc_l > 0
1061 	 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1062     bc_l--;
1063 
1064   /* Now do full processing of the found relevant range of elements.  */
1065 
1066   for (bc = bc_l; bc < bp_location_count; bc++)
1067   {
1068     struct bp_location *bl = bp_location[bc];
1069     CORE_ADDR bp_addr = 0;
1070     int bp_size = 0;
1071     int bptoffset = 0;
1072 
1073     /* bp_location array has BL->OWNER always non-NULL.  */
1074     if (bl->owner->type == bp_none)
1075       warning (_("reading through apparently deleted breakpoint #%d?"),
1076 	       bl->owner->number);
1077 
1078     /* Performance optimization: any futher element can no longer affect BUF
1079        content.  */
1080 
1081     if (bl->address >= bp_location_placed_address_before_address_max
1082         && memaddr + len <= (bl->address
1083 			     - bp_location_placed_address_before_address_max))
1084       break;
1085 
1086     if (!bp_location_has_shadow (bl))
1087       continue;
1088     if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1089 				   current_program_space->aspace, 0))
1090       continue;
1091 
1092     /* Addresses and length of the part of the breakpoint that
1093        we need to copy.  */
1094     bp_addr = bl->target_info.placed_address;
1095     bp_size = bl->target_info.shadow_len;
1096 
1097     if (bp_addr + bp_size <= memaddr)
1098       /* The breakpoint is entirely before the chunk of memory we
1099          are reading.  */
1100       continue;
1101 
1102     if (bp_addr >= memaddr + len)
1103       /* The breakpoint is entirely after the chunk of memory we are
1104          reading.  */
1105       continue;
1106 
1107     /* Offset within shadow_contents.  */
1108     if (bp_addr < memaddr)
1109       {
1110 	/* Only copy the second part of the breakpoint.  */
1111 	bp_size -= memaddr - bp_addr;
1112 	bptoffset = memaddr - bp_addr;
1113 	bp_addr = memaddr;
1114       }
1115 
1116     if (bp_addr + bp_size > memaddr + len)
1117       {
1118 	/* Only copy the first part of the breakpoint.  */
1119 	bp_size -= (bp_addr + bp_size) - (memaddr + len);
1120       }
1121 
1122     memcpy (buf + bp_addr - memaddr,
1123 	    bl->target_info.shadow_contents + bptoffset, bp_size);
1124   }
1125 }
1126 
1127 
1128 /* Return true if BPT is of any hardware watchpoint kind.  */
1129 
1130 static int
1131 is_hardware_watchpoint (const struct breakpoint *bpt)
1132 {
1133   return (bpt->type == bp_hardware_watchpoint
1134 	  || bpt->type == bp_read_watchpoint
1135 	  || bpt->type == bp_access_watchpoint);
1136 }
1137 
1138 /* Return true if BPT is of any watchpoint kind, hardware or
1139    software.  */
1140 
1141 static int
1142 is_watchpoint (const struct breakpoint *bpt)
1143 {
1144   return (is_hardware_watchpoint (bpt)
1145 	  || bpt->type == bp_watchpoint);
1146 }
1147 
1148 /* Assuming that B is a watchpoint: returns true if the current thread
1149    and its running state are safe to evaluate or update watchpoint B.
1150    Watchpoints on local expressions need to be evaluated in the
1151    context of the thread that was current when the watchpoint was
1152    created, and, that thread needs to be stopped to be able to select
1153    the correct frame context.  Watchpoints on global expressions can
1154    be evaluated on any thread, and in any state.  It is presently left
1155    to the target allowing memory accesses when threads are
1156    running.  */
1157 
1158 static int
1159 watchpoint_in_thread_scope (struct breakpoint *b)
1160 {
1161   return (ptid_equal (b->watchpoint_thread, null_ptid)
1162 	  || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1163 	      && !is_executing (inferior_ptid)));
1164 }
1165 
1166 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1167    associated bp_watchpoint_scope breakpoint.  */
1168 
1169 static void
1170 watchpoint_del_at_next_stop (struct breakpoint *b)
1171 {
1172   gdb_assert (is_watchpoint (b));
1173 
1174   if (b->related_breakpoint != b)
1175     {
1176       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1177       gdb_assert (b->related_breakpoint->related_breakpoint == b);
1178       b->related_breakpoint->disposition = disp_del_at_next_stop;
1179       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1180       b->related_breakpoint = b;
1181     }
1182   b->disposition = disp_del_at_next_stop;
1183 }
1184 
1185 /* Assuming that B is a watchpoint:
1186    - Reparse watchpoint expression, if REPARSE is non-zero
1187    - Evaluate expression and store the result in B->val
1188    - Evaluate the condition if there is one, and store the result
1189      in b->loc->cond.
1190    - Update the list of values that must be watched in B->loc.
1191 
1192    If the watchpoint disposition is disp_del_at_next_stop, then do
1193    nothing.  If this is local watchpoint that is out of scope, delete
1194    it.
1195 
1196    Even with `set breakpoint always-inserted on' the watchpoints are
1197    removed + inserted on each stop here.  Normal breakpoints must
1198    never be removed because they might be missed by a running thread
1199    when debugging in non-stop mode.  On the other hand, hardware
1200    watchpoints (is_hardware_watchpoint; processed here) are specific
1201    to each LWP since they are stored in each LWP's hardware debug
1202    registers.  Therefore, such LWP must be stopped first in order to
1203    be able to modify its hardware watchpoints.
1204 
1205    Hardware watchpoints must be reset exactly once after being
1206    presented to the user.  It cannot be done sooner, because it would
1207    reset the data used to present the watchpoint hit to the user.  And
1208    it must not be done later because it could display the same single
1209    watchpoint hit during multiple GDB stops.  Note that the latter is
1210    relevant only to the hardware watchpoint types bp_read_watchpoint
1211    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1212    not user-visible - its hit is suppressed if the memory content has
1213    not changed.
1214 
1215    The following constraints influence the location where we can reset
1216    hardware watchpoints:
1217 
1218    * target_stopped_by_watchpoint and target_stopped_data_address are
1219      called several times when GDB stops.
1220 
1221    [linux]
1222    * Multiple hardware watchpoints can be hit at the same time,
1223      causing GDB to stop.  GDB only presents one hardware watchpoint
1224      hit at a time as the reason for stopping, and all the other hits
1225      are presented later, one after the other, each time the user
1226      requests the execution to be resumed.  Execution is not resumed
1227      for the threads still having pending hit event stored in
1228      LWP_INFO->STATUS.  While the watchpoint is already removed from
1229      the inferior on the first stop the thread hit event is kept being
1230      reported from its cached value by linux_nat_stopped_data_address
1231      until the real thread resume happens after the watchpoint gets
1232      presented and thus its LWP_INFO->STATUS gets reset.
1233 
1234    Therefore the hardware watchpoint hit can get safely reset on the
1235    watchpoint removal from inferior.  */
1236 
1237 static void
1238 update_watchpoint (struct breakpoint *b, int reparse)
1239 {
1240   int within_current_scope;
1241   struct frame_id saved_frame_id;
1242   int frame_saved;
1243 
1244   gdb_assert (is_watchpoint (b));
1245 
1246   /* If this is a local watchpoint, we only want to check if the
1247      watchpoint frame is in scope if the current thread is the thread
1248      that was used to create the watchpoint.  */
1249   if (!watchpoint_in_thread_scope (b))
1250     return;
1251 
1252   if (b->disposition == disp_del_at_next_stop)
1253     return;
1254 
1255   frame_saved = 0;
1256 
1257   /* Determine if the watchpoint is within scope.  */
1258   if (b->exp_valid_block == NULL)
1259     within_current_scope = 1;
1260   else
1261     {
1262       struct frame_info *fi = get_current_frame ();
1263       struct gdbarch *frame_arch = get_frame_arch (fi);
1264       CORE_ADDR frame_pc = get_frame_pc (fi);
1265 
1266       /* If we're in a function epilogue, unwinding may not work
1267 	 properly, so do not attempt to recreate locations at this
1268 	 point.  See similar comments in watchpoint_check.  */
1269       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1270 	return;
1271 
1272       /* Save the current frame's ID so we can restore it after
1273          evaluating the watchpoint expression on its own frame.  */
1274       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1275          took a frame parameter, so that we didn't have to change the
1276          selected frame.  */
1277       frame_saved = 1;
1278       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1279 
1280       fi = frame_find_by_id (b->watchpoint_frame);
1281       within_current_scope = (fi != NULL);
1282       if (within_current_scope)
1283 	select_frame (fi);
1284     }
1285 
1286   /* We don't free locations.  They are stored in the bp_location array
1287      and update_global_location_list will eventually delete them and
1288      remove breakpoints if needed.  */
1289   b->loc = NULL;
1290 
1291   if (within_current_scope && reparse)
1292     {
1293       char *s;
1294 
1295       if (b->exp)
1296 	{
1297 	  xfree (b->exp);
1298 	  b->exp = NULL;
1299 	}
1300       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1301       b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1302       /* If the meaning of expression itself changed, the old value is
1303 	 no longer relevant.  We don't want to report a watchpoint hit
1304 	 to the user when the old value and the new value may actually
1305 	 be completely different objects.  */
1306       value_free (b->val);
1307       b->val = NULL;
1308       b->val_valid = 0;
1309 
1310       /* Note that unlike with breakpoints, the watchpoint's condition
1311 	 expression is stored in the breakpoint object, not in the
1312 	 locations (re)created below.  */
1313       if (b->cond_string != NULL)
1314 	{
1315 	  if (b->cond_exp != NULL)
1316 	    {
1317 	      xfree (b->cond_exp);
1318 	      b->cond_exp = NULL;
1319 	    }
1320 
1321 	  s = b->cond_string;
1322 	  b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1323 	}
1324     }
1325 
1326   /* If we failed to parse the expression, for example because
1327      it refers to a global variable in a not-yet-loaded shared library,
1328      don't try to insert watchpoint.  We don't automatically delete
1329      such watchpoint, though, since failure to parse expression
1330      is different from out-of-scope watchpoint.  */
1331   if ( !target_has_execution)
1332     {
1333       /* Without execution, memory can't change.  No use to try and
1334 	 set watchpoint locations.  The watchpoint will be reset when
1335 	 the target gains execution, through breakpoint_re_set.  */
1336     }
1337   else if (within_current_scope && b->exp)
1338     {
1339       int pc = 0;
1340       struct value *val_chain, *v, *result, *next;
1341       struct program_space *frame_pspace;
1342 
1343       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1344 
1345       /* Avoid setting b->val if it's already set.  The meaning of
1346 	 b->val is 'the last value' user saw, and we should update
1347 	 it only if we reported that last value to user.  As it
1348 	 happens, the code that reports it updates b->val directly.  */
1349       if (!b->val_valid)
1350 	{
1351 	  b->val = v;
1352 	  b->val_valid = 1;
1353 	}
1354 
1355       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1356 
1357       /* Look at each value on the value chain.  */
1358       for (v = val_chain; v; v = value_next (v))
1359 	{
1360 	  /* If it's a memory location, and GDB actually needed
1361 	     its contents to evaluate the expression, then we
1362 	     must watch it.  If the first value returned is
1363 	     still lazy, that means an error occurred reading it;
1364 	     watch it anyway in case it becomes readable.  */
1365 	  if (VALUE_LVAL (v) == lval_memory
1366 	      && (v == val_chain || ! value_lazy (v)))
1367 	    {
1368 	      struct type *vtype = check_typedef (value_type (v));
1369 
1370 	      /* We only watch structs and arrays if user asked
1371 		 for it explicitly, never if they just happen to
1372 		 appear in the middle of some value chain.  */
1373 	      if (v == result
1374 		  || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1375 		      && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1376 		{
1377 		  CORE_ADDR addr;
1378 		  int len, type;
1379 		  struct bp_location *loc, **tmp;
1380 
1381 		  addr = value_address (v);
1382 		  len = TYPE_LENGTH (value_type (v));
1383 		  type = hw_write;
1384 		  if (b->type == bp_read_watchpoint)
1385 		    type = hw_read;
1386 		  else if (b->type == bp_access_watchpoint)
1387 		    type = hw_access;
1388 
1389 		  loc = allocate_bp_location (b);
1390 		  for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1391 		    ;
1392 		  *tmp = loc;
1393 		  loc->gdbarch = get_type_arch (value_type (v));
1394 
1395 		  loc->pspace = frame_pspace;
1396 		  loc->address = addr;
1397 		  loc->length = len;
1398 		  loc->watchpoint_type = type;
1399 		}
1400 	    }
1401 	}
1402 
1403       /* Change the type of breakpoint between hardware assisted or
1404 	 an ordinary watchpoint depending on the hardware support
1405 	 and free hardware slots.  REPARSE is set when the inferior
1406 	 is started.  */
1407       if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1408 	  && reparse)
1409 	{
1410 	  int reg_cnt;
1411 	  enum bp_loc_type loc_type;
1412 	  struct bp_location *bl;
1413 
1414 	  reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1415 
1416 	  if (reg_cnt)
1417 	    {
1418 	      int i, target_resources_ok, other_type_used;
1419 	      enum enable_state orig_enable_state;
1420 
1421 	      /* We need to determine how many resources are already
1422 		 used for all other hardware watchpoints plus this one
1423 		 to see if we still have enough resources to also fit
1424 		 this watchpoint in as well.  To guarantee the
1425 		 hw_watchpoint_used_count call below counts this
1426 		 watchpoint, make sure that it is marked as a hardware
1427 		 watchpoint.  */
1428 	      b->type = bp_hardware_watchpoint;
1429 
1430 	      /* hw_watchpoint_used_count ignores disabled watchpoints,
1431 		 and b might be disabled if we're being called from
1432 		 do_enable_breakpoint.  */
1433 	      orig_enable_state = b->enable_state;
1434 	      b->enable_state = bp_enabled;
1435 
1436 	      i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1437 					    &other_type_used);
1438 
1439 	      b->enable_state = orig_enable_state;
1440 
1441 	      target_resources_ok = target_can_use_hardware_watchpoint
1442 		    (bp_hardware_watchpoint, i, other_type_used);
1443 	      if (target_resources_ok <= 0)
1444 		b->type = bp_watchpoint;
1445 	    }
1446 	  else
1447 	    b->type = bp_watchpoint;
1448 
1449 	  loc_type = (b->type == bp_watchpoint? bp_loc_other
1450 		      : bp_loc_hardware_watchpoint);
1451 	  for (bl = b->loc; bl; bl = bl->next)
1452 	    bl->loc_type = loc_type;
1453 	}
1454 
1455       for (v = val_chain; v; v = next)
1456 	{
1457 	  next = value_next (v);
1458 	  if (v != b->val)
1459 	    value_free (v);
1460 	}
1461 
1462       /* If a software watchpoint is not watching any memory, then the
1463 	 above left it without any location set up.  But,
1464 	 bpstat_stop_status requires a location to be able to report
1465 	 stops, so make sure there's at least a dummy one.  */
1466       if (b->type == bp_watchpoint && b->loc == NULL)
1467 	{
1468 	  b->loc = allocate_bp_location (b);
1469 	  b->loc->pspace = frame_pspace;
1470 	  b->loc->address = -1;
1471 	  b->loc->length = -1;
1472 	  b->loc->watchpoint_type = -1;
1473 	}
1474     }
1475   else if (!within_current_scope)
1476     {
1477       printf_filtered (_("\
1478 Watchpoint %d deleted because the program has left the block\n\
1479 in which its expression is valid.\n"),
1480 		       b->number);
1481       watchpoint_del_at_next_stop (b);
1482     }
1483 
1484   /* Restore the selected frame.  */
1485   if (frame_saved)
1486     select_frame (frame_find_by_id (saved_frame_id));
1487 }
1488 
1489 
1490 /* Returns 1 iff breakpoint location should be
1491    inserted in the inferior.  */
1492 static int
1493 should_be_inserted (struct bp_location *bl)
1494 {
1495   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1496     return 0;
1497 
1498   if (bl->owner->disposition == disp_del_at_next_stop)
1499     return 0;
1500 
1501   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1502     return 0;
1503 
1504   /* This is set for example, when we're attached to the parent of a
1505      vfork, and have detached from the child.  The child is running
1506      free, and we expect it to do an exec or exit, at which point the
1507      OS makes the parent schedulable again (and the target reports
1508      that the vfork is done).  Until the child is done with the shared
1509      memory region, do not insert breakpoints in the parent, otherwise
1510      the child could still trip on the parent's breakpoints.  Since
1511      the parent is blocked anyway, it won't miss any breakpoint.  */
1512   if (bl->pspace->breakpoints_not_allowed)
1513     return 0;
1514 
1515   /* Tracepoints are inserted by the target at a time of its choosing,
1516      not by us.  */
1517   if (is_tracepoint (bl->owner))
1518     return 0;
1519 
1520   return 1;
1521 }
1522 
1523 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
1524    location.  Any error messages are printed to TMP_ERROR_STREAM; and
1525    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
1526 
1527    NOTE drow/2003-09-09: This routine could be broken down to an
1528    object-style method for each breakpoint or catchpoint type.  */
1529 static int
1530 insert_bp_location (struct bp_location *bl,
1531 		    struct ui_file *tmp_error_stream,
1532 		    int *disabled_breaks,
1533 		    int *hw_breakpoint_error)
1534 {
1535   int val = 0;
1536 
1537   if (!should_be_inserted (bl) || bl->inserted)
1538     return 0;
1539 
1540   /* Initialize the target-specific information.  */
1541   memset (&bl->target_info, 0, sizeof (bl->target_info));
1542   bl->target_info.placed_address = bl->address;
1543   bl->target_info.placed_address_space = bl->pspace->aspace;
1544   bl->target_info.length = bl->length;
1545 
1546   if (bl->loc_type == bp_loc_software_breakpoint
1547       || bl->loc_type == bp_loc_hardware_breakpoint)
1548     {
1549       if (bl->owner->type != bp_hardware_breakpoint)
1550 	{
1551 	  /* If the explicitly specified breakpoint type
1552 	     is not hardware breakpoint, check the memory map to see
1553 	     if the breakpoint address is in read only memory or not.
1554 
1555 	     Two important cases are:
1556 	     - location type is not hardware breakpoint, memory
1557 	     is readonly.  We change the type of the location to
1558 	     hardware breakpoint.
1559 	     - location type is hardware breakpoint, memory is
1560 	     read-write.  This means we've previously made the
1561 	     location hardware one, but then the memory map changed,
1562 	     so we undo.
1563 
1564 	     When breakpoints are removed, remove_breakpoints will use
1565 	     location types we've just set here, the only possible
1566 	     problem is that memory map has changed during running
1567 	     program, but it's not going to work anyway with current
1568 	     gdb.  */
1569 	  struct mem_region *mr
1570 	    = lookup_mem_region (bl->target_info.placed_address);
1571 
1572 	  if (mr)
1573 	    {
1574 	      if (automatic_hardware_breakpoints)
1575 		{
1576 		  enum bp_loc_type new_type;
1577 
1578 		  if (mr->attrib.mode != MEM_RW)
1579 		    new_type = bp_loc_hardware_breakpoint;
1580 		  else
1581 		    new_type = bp_loc_software_breakpoint;
1582 
1583 		  if (new_type != bl->loc_type)
1584 		    {
1585 		      static int said = 0;
1586 
1587 		      bl->loc_type = new_type;
1588 		      if (!said)
1589 			{
1590 			  fprintf_filtered (gdb_stdout,
1591 					    _("Note: automatically using "
1592 					      "hardware breakpoints for "
1593 					      "read-only addresses.\n"));
1594 			  said = 1;
1595 			}
1596 		    }
1597 		}
1598 	      else if (bl->loc_type == bp_loc_software_breakpoint
1599 		       && mr->attrib.mode != MEM_RW)
1600 		warning (_("cannot set software breakpoint "
1601 			   "at readonly address %s"),
1602 			 paddress (bl->gdbarch, bl->address));
1603 	    }
1604 	}
1605 
1606       /* First check to see if we have to handle an overlay.  */
1607       if (overlay_debugging == ovly_off
1608 	  || bl->section == NULL
1609 	  || !(section_is_overlay (bl->section)))
1610 	{
1611 	  /* No overlay handling: just set the breakpoint.  */
1612 
1613 	  if (bl->loc_type == bp_loc_hardware_breakpoint)
1614 	    val = target_insert_hw_breakpoint (bl->gdbarch,
1615 					       &bl->target_info);
1616 	  else
1617 	    val = target_insert_breakpoint (bl->gdbarch,
1618 					    &bl->target_info);
1619 	}
1620       else
1621 	{
1622 	  /* This breakpoint is in an overlay section.
1623 	     Shall we set a breakpoint at the LMA?  */
1624 	  if (!overlay_events_enabled)
1625 	    {
1626 	      /* Yes -- overlay event support is not active,
1627 		 so we must try to set a breakpoint at the LMA.
1628 		 This will not work for a hardware breakpoint.  */
1629 	      if (bl->loc_type == bp_loc_hardware_breakpoint)
1630 		warning (_("hardware breakpoint %d not supported in overlay!"),
1631 			 bl->owner->number);
1632 	      else
1633 		{
1634 		  CORE_ADDR addr = overlay_unmapped_address (bl->address,
1635 							     bl->section);
1636 		  /* Set a software (trap) breakpoint at the LMA.  */
1637 		  bl->overlay_target_info = bl->target_info;
1638 		  bl->overlay_target_info.placed_address = addr;
1639 		  val = target_insert_breakpoint (bl->gdbarch,
1640 						  &bl->overlay_target_info);
1641 		  if (val != 0)
1642 		    fprintf_unfiltered (tmp_error_stream,
1643 					"Overlay breakpoint %d "
1644 					"failed: in ROM?\n",
1645 					bl->owner->number);
1646 		}
1647 	    }
1648 	  /* Shall we set a breakpoint at the VMA? */
1649 	  if (section_is_mapped (bl->section))
1650 	    {
1651 	      /* Yes.  This overlay section is mapped into memory.  */
1652 	      if (bl->loc_type == bp_loc_hardware_breakpoint)
1653 		val = target_insert_hw_breakpoint (bl->gdbarch,
1654 						   &bl->target_info);
1655 	      else
1656 		val = target_insert_breakpoint (bl->gdbarch,
1657 						&bl->target_info);
1658 	    }
1659 	  else
1660 	    {
1661 	      /* No.  This breakpoint will not be inserted.
1662 		 No error, but do not mark the bp as 'inserted'.  */
1663 	      return 0;
1664 	    }
1665 	}
1666 
1667       if (val)
1668 	{
1669 	  /* Can't set the breakpoint.  */
1670 	  if (solib_name_from_address (bl->pspace, bl->address))
1671 	    {
1672 	      /* See also: disable_breakpoints_in_shlibs.  */
1673 	      val = 0;
1674 	      bl->shlib_disabled = 1;
1675 	      if (!*disabled_breaks)
1676 		{
1677 		  fprintf_unfiltered (tmp_error_stream,
1678 				      "Cannot insert breakpoint %d.\n",
1679 				      bl->owner->number);
1680 		  fprintf_unfiltered (tmp_error_stream,
1681 				      "Temporarily disabling shared "
1682 				      "library breakpoints:\n");
1683 		}
1684 	      *disabled_breaks = 1;
1685 	      fprintf_unfiltered (tmp_error_stream,
1686 				  "breakpoint #%d\n", bl->owner->number);
1687 	    }
1688 	  else
1689 	    {
1690 	      if (bl->loc_type == bp_loc_hardware_breakpoint)
1691 		{
1692 		  *hw_breakpoint_error = 1;
1693 		  fprintf_unfiltered (tmp_error_stream,
1694 				      "Cannot insert hardware "
1695 				      "breakpoint %d.\n",
1696 				      bl->owner->number);
1697 		}
1698 	      else
1699 		{
1700 		  fprintf_unfiltered (tmp_error_stream,
1701 				      "Cannot insert breakpoint %d.\n",
1702 				      bl->owner->number);
1703 		  fprintf_filtered (tmp_error_stream,
1704 				    "Error accessing memory address ");
1705 		  fputs_filtered (paddress (bl->gdbarch, bl->address),
1706 				  tmp_error_stream);
1707 		  fprintf_filtered (tmp_error_stream, ": %s.\n",
1708 				    safe_strerror (val));
1709 		}
1710 
1711 	    }
1712 	}
1713       else
1714 	bl->inserted = 1;
1715 
1716       return val;
1717     }
1718 
1719   else if (bl->loc_type == bp_loc_hardware_watchpoint
1720 	   /* NOTE drow/2003-09-08: This state only exists for removing
1721 	      watchpoints.  It's not clear that it's necessary...  */
1722 	   && bl->owner->disposition != disp_del_at_next_stop)
1723     {
1724       gdb_assert (bl->owner->ops != NULL
1725 		  && bl->owner->ops->insert_location != NULL);
1726 
1727       val = bl->owner->ops->insert_location (bl);
1728 
1729       /* If trying to set a read-watchpoint, and it turns out it's not
1730 	 supported, try emulating one with an access watchpoint.  */
1731       if (val == 1 && bl->watchpoint_type == hw_read)
1732 	{
1733 	  struct bp_location *loc, **loc_temp;
1734 
1735 	  /* But don't try to insert it, if there's already another
1736 	     hw_access location that would be considered a duplicate
1737 	     of this one.  */
1738 	  ALL_BP_LOCATIONS (loc, loc_temp)
1739 	    if (loc != bl
1740 		&& loc->watchpoint_type == hw_access
1741 		&& watchpoint_locations_match (bl, loc))
1742 	      {
1743 		bl->duplicate = 1;
1744 		bl->inserted = 1;
1745 		bl->target_info = loc->target_info;
1746 		bl->watchpoint_type = hw_access;
1747 		val = 0;
1748 		break;
1749 	      }
1750 
1751 	  if (val == 1)
1752 	    {
1753 	      bl->watchpoint_type = hw_access;
1754 	      val = bl->owner->ops->insert_location (bl);
1755 
1756 	      if (val)
1757 		/* Back to the original value.  */
1758 		bl->watchpoint_type = hw_read;
1759 	    }
1760 	}
1761 
1762       bl->inserted = (val == 0);
1763     }
1764 
1765   else if (bl->owner->type == bp_catchpoint)
1766     {
1767       gdb_assert (bl->owner->ops != NULL
1768 		  && bl->owner->ops->insert_location != NULL);
1769 
1770       val = bl->owner->ops->insert_location (bl);
1771       if (val)
1772 	{
1773 	  bl->owner->enable_state = bp_disabled;
1774 
1775 	  if (val == 1)
1776 	    warning (_("\
1777 Error inserting catchpoint %d: Your system does not support this type\n\
1778 of catchpoint."), bl->owner->number);
1779 	  else
1780 	    warning (_("Error inserting catchpoint %d."), bl->owner->number);
1781 	}
1782 
1783       bl->inserted = (val == 0);
1784 
1785       /* We've already printed an error message if there was a problem
1786 	 inserting this catchpoint, and we've disabled the catchpoint,
1787 	 so just return success.  */
1788       return 0;
1789     }
1790 
1791   return 0;
1792 }
1793 
1794 /* This function is called when program space PSPACE is about to be
1795    deleted.  It takes care of updating breakpoints to not reference
1796    PSPACE anymore.  */
1797 
1798 void
1799 breakpoint_program_space_exit (struct program_space *pspace)
1800 {
1801   struct breakpoint *b, *b_temp;
1802   struct bp_location *loc, **loc_temp;
1803 
1804   /* Remove any breakpoint that was set through this program space.  */
1805   ALL_BREAKPOINTS_SAFE (b, b_temp)
1806     {
1807       if (b->pspace == pspace)
1808 	delete_breakpoint (b);
1809     }
1810 
1811   /* Breakpoints set through other program spaces could have locations
1812      bound to PSPACE as well.  Remove those.  */
1813   ALL_BP_LOCATIONS (loc, loc_temp)
1814     {
1815       struct bp_location *tmp;
1816 
1817       if (loc->pspace == pspace)
1818 	{
1819 	  /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
1820 	  if (loc->owner->loc == loc)
1821 	    loc->owner->loc = loc->next;
1822 	  else
1823 	    for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1824 	      if (tmp->next == loc)
1825 		{
1826 		  tmp->next = loc->next;
1827 		  break;
1828 		}
1829 	}
1830     }
1831 
1832   /* Now update the global location list to permanently delete the
1833      removed locations above.  */
1834   update_global_location_list (0);
1835 }
1836 
1837 /* Make sure all breakpoints are inserted in inferior.
1838    Throws exception on any error.
1839    A breakpoint that is already inserted won't be inserted
1840    again, so calling this function twice is safe.  */
1841 void
1842 insert_breakpoints (void)
1843 {
1844   struct breakpoint *bpt;
1845 
1846   ALL_BREAKPOINTS (bpt)
1847     if (is_hardware_watchpoint (bpt))
1848       update_watchpoint (bpt, 0 /* don't reparse.  */);
1849 
1850   update_global_location_list (1);
1851 
1852   /* update_global_location_list does not insert breakpoints when
1853      always_inserted_mode is not enabled.  Explicitly insert them
1854      now.  */
1855   if (!breakpoints_always_inserted_mode ())
1856     insert_breakpoint_locations ();
1857 }
1858 
1859 /* insert_breakpoints is used when starting or continuing the program.
1860    remove_breakpoints is used when the program stops.
1861    Both return zero if successful,
1862    or an `errno' value if could not write the inferior.  */
1863 
1864 static void
1865 insert_breakpoint_locations (void)
1866 {
1867   struct breakpoint *bpt;
1868   struct bp_location *bl, **blp_tmp;
1869   int error = 0;
1870   int val = 0;
1871   int disabled_breaks = 0;
1872   int hw_breakpoint_error = 0;
1873 
1874   struct ui_file *tmp_error_stream = mem_fileopen ();
1875   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
1876 
1877   /* Explicitly mark the warning -- this will only be printed if
1878      there was an error.  */
1879   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1880 
1881   save_current_space_and_thread ();
1882 
1883   ALL_BP_LOCATIONS (bl, blp_tmp)
1884     {
1885       if (!should_be_inserted (bl) || bl->inserted)
1886 	continue;
1887 
1888       /* There is no point inserting thread-specific breakpoints if
1889 	 the thread no longer exists.  ALL_BP_LOCATIONS bp_location
1890 	 has BL->OWNER always non-NULL.  */
1891       if (bl->owner->thread != -1
1892 	  && !valid_thread_id (bl->owner->thread))
1893 	continue;
1894 
1895       switch_to_program_space_and_thread (bl->pspace);
1896 
1897       /* For targets that support global breakpoints, there's no need
1898 	 to select an inferior to insert breakpoint to.  In fact, even
1899 	 if we aren't attached to any process yet, we should still
1900 	 insert breakpoints.  */
1901       if (!gdbarch_has_global_breakpoints (target_gdbarch)
1902 	  && ptid_equal (inferior_ptid, null_ptid))
1903 	continue;
1904 
1905       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1906 				    &hw_breakpoint_error);
1907       if (val)
1908 	error = val;
1909     }
1910 
1911   /* If we failed to insert all locations of a watchpoint, remove
1912      them, as half-inserted watchpoint is of limited use.  */
1913   ALL_BREAKPOINTS (bpt)
1914     {
1915       int some_failed = 0;
1916       struct bp_location *loc;
1917 
1918       if (!is_hardware_watchpoint (bpt))
1919 	continue;
1920 
1921       if (!breakpoint_enabled (bpt))
1922 	continue;
1923 
1924       if (bpt->disposition == disp_del_at_next_stop)
1925 	continue;
1926 
1927       for (loc = bpt->loc; loc; loc = loc->next)
1928 	if (!loc->inserted && should_be_inserted (loc))
1929 	  {
1930 	    some_failed = 1;
1931 	    break;
1932 	  }
1933       if (some_failed)
1934 	{
1935 	  for (loc = bpt->loc; loc; loc = loc->next)
1936 	    if (loc->inserted)
1937 	      remove_breakpoint (loc, mark_uninserted);
1938 
1939 	  hw_breakpoint_error = 1;
1940 	  fprintf_unfiltered (tmp_error_stream,
1941 			      "Could not insert hardware watchpoint %d.\n",
1942 			      bpt->number);
1943 	  error = -1;
1944 	}
1945     }
1946 
1947   if (error)
1948     {
1949       /* If a hardware breakpoint or watchpoint was inserted, add a
1950          message about possibly exhausted resources.  */
1951       if (hw_breakpoint_error)
1952 	{
1953 	  fprintf_unfiltered (tmp_error_stream,
1954 			      "Could not insert hardware breakpoints:\n\
1955 You may have requested too many hardware breakpoints/watchpoints.\n");
1956 	}
1957       target_terminal_ours_for_output ();
1958       error_stream (tmp_error_stream);
1959     }
1960 
1961   do_cleanups (cleanups);
1962 }
1963 
1964 int
1965 remove_breakpoints (void)
1966 {
1967   struct bp_location *bl, **blp_tmp;
1968   int val = 0;
1969 
1970   ALL_BP_LOCATIONS (bl, blp_tmp)
1971   {
1972     if (bl->inserted)
1973       val |= remove_breakpoint (bl, mark_uninserted);
1974   }
1975   return val;
1976 }
1977 
1978 /* Remove breakpoints of process PID.  */
1979 
1980 int
1981 remove_breakpoints_pid (int pid)
1982 {
1983   struct bp_location *bl, **blp_tmp;
1984   int val;
1985   struct inferior *inf = find_inferior_pid (pid);
1986 
1987   ALL_BP_LOCATIONS (bl, blp_tmp)
1988   {
1989     if (bl->pspace != inf->pspace)
1990       continue;
1991 
1992     if (bl->inserted)
1993       {
1994 	val = remove_breakpoint (bl, mark_uninserted);
1995 	if (val != 0)
1996 	  return val;
1997       }
1998   }
1999   return 0;
2000 }
2001 
2002 int
2003 remove_hw_watchpoints (void)
2004 {
2005   struct bp_location *bl, **blp_tmp;
2006   int val = 0;
2007 
2008   ALL_BP_LOCATIONS (bl, blp_tmp)
2009   {
2010     if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
2011       val |= remove_breakpoint (bl, mark_uninserted);
2012   }
2013   return val;
2014 }
2015 
2016 int
2017 reattach_breakpoints (int pid)
2018 {
2019   struct cleanup *old_chain;
2020   struct bp_location *bl, **blp_tmp;
2021   int val;
2022   struct ui_file *tmp_error_stream;
2023   int dummy1 = 0, dummy2 = 0;
2024   struct inferior *inf;
2025   struct thread_info *tp;
2026 
2027   tp = any_live_thread_of_process (pid);
2028   if (tp == NULL)
2029     return 1;
2030 
2031   inf = find_inferior_pid (pid);
2032   old_chain = save_inferior_ptid ();
2033 
2034   inferior_ptid = tp->ptid;
2035 
2036   tmp_error_stream = mem_fileopen ();
2037   make_cleanup_ui_file_delete (tmp_error_stream);
2038 
2039   ALL_BP_LOCATIONS (bl, blp_tmp)
2040   {
2041     if (bl->pspace != inf->pspace)
2042       continue;
2043 
2044     if (bl->inserted)
2045       {
2046 	bl->inserted = 0;
2047 	val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2048 	if (val != 0)
2049 	  {
2050 	    do_cleanups (old_chain);
2051 	    return val;
2052 	  }
2053       }
2054   }
2055   do_cleanups (old_chain);
2056   return 0;
2057 }
2058 
2059 static int internal_breakpoint_number = -1;
2060 
2061 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2062    If INTERNAL is non-zero, the breakpoint number will be populated
2063    from internal_breakpoint_number and that variable decremented.
2064    Otherwis the breakpoint number will be populated from
2065    breakpoint_count and that value incremented.  Internal breakpoints
2066    do not set the internal var bpnum.  */
2067 static void
2068 set_breakpoint_number (int internal, struct breakpoint *b)
2069 {
2070   if (internal)
2071     b->number = internal_breakpoint_number--;
2072   else
2073     {
2074       set_breakpoint_count (breakpoint_count + 1);
2075       b->number = breakpoint_count;
2076     }
2077 }
2078 
2079 static struct breakpoint *
2080 create_internal_breakpoint (struct gdbarch *gdbarch,
2081 			    CORE_ADDR address, enum bptype type)
2082 {
2083   struct symtab_and_line sal;
2084   struct breakpoint *b;
2085 
2086   init_sal (&sal);		/* Initialize to zeroes.  */
2087 
2088   sal.pc = address;
2089   sal.section = find_pc_overlay (sal.pc);
2090   sal.pspace = current_program_space;
2091 
2092   b = set_raw_breakpoint (gdbarch, sal, type);
2093   b->number = internal_breakpoint_number--;
2094   b->disposition = disp_donttouch;
2095 
2096   return b;
2097 }
2098 
2099 static const char *const longjmp_names[] =
2100   {
2101     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2102   };
2103 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2104 
2105 /* Per-objfile data private to breakpoint.c.  */
2106 struct breakpoint_objfile_data
2107 {
2108   /* Minimal symbol for "_ovly_debug_event" (if any).  */
2109   struct minimal_symbol *overlay_msym;
2110 
2111   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
2112   struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2113 
2114   /* Minimal symbol for "std::terminate()" (if any).  */
2115   struct minimal_symbol *terminate_msym;
2116 
2117   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
2118   struct minimal_symbol *exception_msym;
2119 };
2120 
2121 static const struct objfile_data *breakpoint_objfile_key;
2122 
2123 /* Minimal symbol not found sentinel.  */
2124 static struct minimal_symbol msym_not_found;
2125 
2126 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
2127 
2128 static int
2129 msym_not_found_p (const struct minimal_symbol *msym)
2130 {
2131   return msym == &msym_not_found;
2132 }
2133 
2134 /* Return per-objfile data needed by breakpoint.c.
2135    Allocate the data if necessary.  */
2136 
2137 static struct breakpoint_objfile_data *
2138 get_breakpoint_objfile_data (struct objfile *objfile)
2139 {
2140   struct breakpoint_objfile_data *bp_objfile_data;
2141 
2142   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2143   if (bp_objfile_data == NULL)
2144     {
2145       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2146 				       sizeof (*bp_objfile_data));
2147 
2148       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2149       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2150     }
2151   return bp_objfile_data;
2152 }
2153 
2154 static void
2155 create_overlay_event_breakpoint (void)
2156 {
2157   struct objfile *objfile;
2158   const char *const func_name = "_ovly_debug_event";
2159 
2160   ALL_OBJFILES (objfile)
2161     {
2162       struct breakpoint *b;
2163       struct breakpoint_objfile_data *bp_objfile_data;
2164       CORE_ADDR addr;
2165 
2166       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2167 
2168       if (msym_not_found_p (bp_objfile_data->overlay_msym))
2169 	continue;
2170 
2171       if (bp_objfile_data->overlay_msym == NULL)
2172 	{
2173 	  struct minimal_symbol *m;
2174 
2175 	  m = lookup_minimal_symbol_text (func_name, objfile);
2176 	  if (m == NULL)
2177 	    {
2178 	      /* Avoid future lookups in this objfile.  */
2179 	      bp_objfile_data->overlay_msym = &msym_not_found;
2180 	      continue;
2181 	    }
2182 	  bp_objfile_data->overlay_msym = m;
2183 	}
2184 
2185       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2186       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2187                                       bp_overlay_event);
2188       b->addr_string = xstrdup (func_name);
2189 
2190       if (overlay_debugging == ovly_auto)
2191         {
2192           b->enable_state = bp_enabled;
2193           overlay_events_enabled = 1;
2194         }
2195       else
2196        {
2197          b->enable_state = bp_disabled;
2198          overlay_events_enabled = 0;
2199        }
2200     }
2201   update_global_location_list (1);
2202 }
2203 
2204 static void
2205 create_longjmp_master_breakpoint (void)
2206 {
2207   struct program_space *pspace;
2208   struct cleanup *old_chain;
2209 
2210   old_chain = save_current_program_space ();
2211 
2212   ALL_PSPACES (pspace)
2213   {
2214     struct objfile *objfile;
2215 
2216     set_current_program_space (pspace);
2217 
2218     ALL_OBJFILES (objfile)
2219     {
2220       int i;
2221       struct gdbarch *gdbarch;
2222       struct breakpoint_objfile_data *bp_objfile_data;
2223 
2224       gdbarch = get_objfile_arch (objfile);
2225       if (!gdbarch_get_longjmp_target_p (gdbarch))
2226 	continue;
2227 
2228       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2229 
2230       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2231 	{
2232 	  struct breakpoint *b;
2233 	  const char *func_name;
2234 	  CORE_ADDR addr;
2235 
2236 	  if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2237 	    continue;
2238 
2239 	  func_name = longjmp_names[i];
2240 	  if (bp_objfile_data->longjmp_msym[i] == NULL)
2241 	    {
2242 	      struct minimal_symbol *m;
2243 
2244 	      m = lookup_minimal_symbol_text (func_name, objfile);
2245 	      if (m == NULL)
2246 		{
2247 		  /* Prevent future lookups in this objfile.  */
2248 		  bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2249 		  continue;
2250 		}
2251 	      bp_objfile_data->longjmp_msym[i] = m;
2252 	    }
2253 
2254 	  addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2255 	  b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
2256 	  b->addr_string = xstrdup (func_name);
2257 	  b->enable_state = bp_disabled;
2258 	}
2259     }
2260   }
2261   update_global_location_list (1);
2262 
2263   do_cleanups (old_chain);
2264 }
2265 
2266 /* Create a master std::terminate breakpoint.  */
2267 static void
2268 create_std_terminate_master_breakpoint (void)
2269 {
2270   struct program_space *pspace;
2271   struct cleanup *old_chain;
2272   const char *const func_name = "std::terminate()";
2273 
2274   old_chain = save_current_program_space ();
2275 
2276   ALL_PSPACES (pspace)
2277   {
2278     struct objfile *objfile;
2279     CORE_ADDR addr;
2280 
2281     set_current_program_space (pspace);
2282 
2283     ALL_OBJFILES (objfile)
2284     {
2285       struct breakpoint *b;
2286       struct breakpoint_objfile_data *bp_objfile_data;
2287 
2288       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2289 
2290       if (msym_not_found_p (bp_objfile_data->terminate_msym))
2291 	continue;
2292 
2293       if (bp_objfile_data->terminate_msym == NULL)
2294 	{
2295 	  struct minimal_symbol *m;
2296 
2297 	  m = lookup_minimal_symbol (func_name, NULL, objfile);
2298 	  if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2299 			    && MSYMBOL_TYPE (m) != mst_file_text))
2300 	    {
2301 	      /* Prevent future lookups in this objfile.  */
2302 	      bp_objfile_data->terminate_msym = &msym_not_found;
2303 	      continue;
2304 	    }
2305 	  bp_objfile_data->terminate_msym = m;
2306 	}
2307 
2308       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2309       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2310                                       bp_std_terminate_master);
2311       b->addr_string = xstrdup (func_name);
2312       b->enable_state = bp_disabled;
2313     }
2314   }
2315 
2316   update_global_location_list (1);
2317 
2318   do_cleanups (old_chain);
2319 }
2320 
2321 /* Install a master breakpoint on the unwinder's debug hook.  */
2322 
2323 void
2324 create_exception_master_breakpoint (void)
2325 {
2326   struct objfile *objfile;
2327   const char *const func_name = "_Unwind_DebugHook";
2328 
2329   ALL_OBJFILES (objfile)
2330     {
2331       struct breakpoint *b;
2332       struct gdbarch *gdbarch;
2333       struct breakpoint_objfile_data *bp_objfile_data;
2334       CORE_ADDR addr;
2335 
2336       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2337 
2338       if (msym_not_found_p (bp_objfile_data->exception_msym))
2339 	continue;
2340 
2341       gdbarch = get_objfile_arch (objfile);
2342 
2343       if (bp_objfile_data->exception_msym == NULL)
2344 	{
2345 	  struct minimal_symbol *debug_hook;
2346 
2347 	  debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2348 	  if (debug_hook == NULL)
2349 	    {
2350 	      bp_objfile_data->exception_msym = &msym_not_found;
2351 	      continue;
2352 	    }
2353 
2354 	  bp_objfile_data->exception_msym = debug_hook;
2355 	}
2356 
2357       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2358       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2359 						 &current_target);
2360       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2361       b->addr_string = xstrdup (func_name);
2362       b->enable_state = bp_disabled;
2363     }
2364 
2365   update_global_location_list (1);
2366 }
2367 
2368 void
2369 update_breakpoints_after_exec (void)
2370 {
2371   struct breakpoint *b, *b_tmp;
2372   struct bp_location *bploc, **bplocp_tmp;
2373 
2374   /* We're about to delete breakpoints from GDB's lists.  If the
2375      INSERTED flag is true, GDB will try to lift the breakpoints by
2376      writing the breakpoints' "shadow contents" back into memory.  The
2377      "shadow contents" are NOT valid after an exec, so GDB should not
2378      do that.  Instead, the target is responsible from marking
2379      breakpoints out as soon as it detects an exec.  We don't do that
2380      here instead, because there may be other attempts to delete
2381      breakpoints after detecting an exec and before reaching here.  */
2382   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
2383     if (bploc->pspace == current_program_space)
2384       gdb_assert (!bploc->inserted);
2385 
2386   ALL_BREAKPOINTS_SAFE (b, b_tmp)
2387   {
2388     if (b->pspace != current_program_space)
2389       continue;
2390 
2391     /* Solib breakpoints must be explicitly reset after an exec().  */
2392     if (b->type == bp_shlib_event)
2393       {
2394 	delete_breakpoint (b);
2395 	continue;
2396       }
2397 
2398     /* JIT breakpoints must be explicitly reset after an exec().  */
2399     if (b->type == bp_jit_event)
2400       {
2401 	delete_breakpoint (b);
2402 	continue;
2403       }
2404 
2405     /* Thread event breakpoints must be set anew after an exec(),
2406        as must overlay event and longjmp master breakpoints.  */
2407     if (b->type == bp_thread_event || b->type == bp_overlay_event
2408 	|| b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2409 	|| b->type == bp_exception_master)
2410       {
2411 	delete_breakpoint (b);
2412 	continue;
2413       }
2414 
2415     /* Step-resume breakpoints are meaningless after an exec().  */
2416     if (b->type == bp_step_resume)
2417       {
2418 	delete_breakpoint (b);
2419 	continue;
2420       }
2421 
2422     /* Longjmp and longjmp-resume breakpoints are also meaningless
2423        after an exec.  */
2424     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2425 	|| b->type == bp_exception || b->type == bp_exception_resume)
2426       {
2427 	delete_breakpoint (b);
2428 	continue;
2429       }
2430 
2431     if (b->type == bp_catchpoint)
2432       {
2433         /* For now, none of the bp_catchpoint breakpoints need to
2434            do anything at this point.  In the future, if some of
2435            the catchpoints need to something, we will need to add
2436            a new method, and call this method from here.  */
2437         continue;
2438       }
2439 
2440     /* bp_finish is a special case.  The only way we ought to be able
2441        to see one of these when an exec() has happened, is if the user
2442        caught a vfork, and then said "finish".  Ordinarily a finish just
2443        carries them to the call-site of the current callee, by setting
2444        a temporary bp there and resuming.  But in this case, the finish
2445        will carry them entirely through the vfork & exec.
2446 
2447        We don't want to allow a bp_finish to remain inserted now.  But
2448        we can't safely delete it, 'cause finish_command has a handle to
2449        the bp on a bpstat, and will later want to delete it.  There's a
2450        chance (and I've seen it happen) that if we delete the bp_finish
2451        here, that its storage will get reused by the time finish_command
2452        gets 'round to deleting the "use to be a bp_finish" breakpoint.
2453        We really must allow finish_command to delete a bp_finish.
2454 
2455        In the absense of a general solution for the "how do we know
2456        it's safe to delete something others may have handles to?"
2457        problem, what we'll do here is just uninsert the bp_finish, and
2458        let finish_command delete it.
2459 
2460        (We know the bp_finish is "doomed" in the sense that it's
2461        momentary, and will be deleted as soon as finish_command sees
2462        the inferior stopped.  So it doesn't matter that the bp's
2463        address is probably bogus in the new a.out, unlike e.g., the
2464        solib breakpoints.)  */
2465 
2466     if (b->type == bp_finish)
2467       {
2468 	continue;
2469       }
2470 
2471     /* Without a symbolic address, we have little hope of the
2472        pre-exec() address meaning the same thing in the post-exec()
2473        a.out.  */
2474     if (b->addr_string == NULL)
2475       {
2476 	delete_breakpoint (b);
2477 	continue;
2478       }
2479   }
2480   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
2481   create_overlay_event_breakpoint ();
2482   create_longjmp_master_breakpoint ();
2483   create_std_terminate_master_breakpoint ();
2484   create_exception_master_breakpoint ();
2485 }
2486 
2487 int
2488 detach_breakpoints (int pid)
2489 {
2490   struct bp_location *bl, **blp_tmp;
2491   int val = 0;
2492   struct cleanup *old_chain = save_inferior_ptid ();
2493   struct inferior *inf = current_inferior ();
2494 
2495   if (pid == PIDGET (inferior_ptid))
2496     error (_("Cannot detach breakpoints of inferior_ptid"));
2497 
2498   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
2499   inferior_ptid = pid_to_ptid (pid);
2500   ALL_BP_LOCATIONS (bl, blp_tmp)
2501   {
2502     if (bl->pspace != inf->pspace)
2503       continue;
2504 
2505     if (bl->inserted)
2506       val |= remove_breakpoint_1 (bl, mark_inserted);
2507   }
2508 
2509   /* Detach single-step breakpoints as well.  */
2510   detach_single_step_breakpoints ();
2511 
2512   do_cleanups (old_chain);
2513   return val;
2514 }
2515 
2516 /* Remove the breakpoint location BL from the current address space.
2517    Note that this is used to detach breakpoints from a child fork.
2518    When we get here, the child isn't in the inferior list, and neither
2519    do we have objects to represent its address space --- we should
2520    *not* look at bl->pspace->aspace here.  */
2521 
2522 static int
2523 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
2524 {
2525   int val;
2526 
2527   /* BL is never in moribund_locations by our callers.  */
2528   gdb_assert (bl->owner != NULL);
2529 
2530   if (bl->owner->enable_state == bp_permanent)
2531     /* Permanent breakpoints cannot be inserted or removed.  */
2532     return 0;
2533 
2534   /* The type of none suggests that owner is actually deleted.
2535      This should not ever happen.  */
2536   gdb_assert (bl->owner->type != bp_none);
2537 
2538   if (bl->loc_type == bp_loc_software_breakpoint
2539       || bl->loc_type == bp_loc_hardware_breakpoint)
2540     {
2541       /* "Normal" instruction breakpoint: either the standard
2542 	 trap-instruction bp (bp_breakpoint), or a
2543 	 bp_hardware_breakpoint.  */
2544 
2545       /* First check to see if we have to handle an overlay.  */
2546       if (overlay_debugging == ovly_off
2547 	  || bl->section == NULL
2548 	  || !(section_is_overlay (bl->section)))
2549 	{
2550 	  /* No overlay handling: just remove the breakpoint.  */
2551 
2552 	  if (bl->loc_type == bp_loc_hardware_breakpoint)
2553 	    val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
2554 	  else
2555 	    val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
2556 	}
2557       else
2558 	{
2559 	  /* This breakpoint is in an overlay section.
2560 	     Did we set a breakpoint at the LMA?  */
2561 	  if (!overlay_events_enabled)
2562 	      {
2563 		/* Yes -- overlay event support is not active, so we
2564 		   should have set a breakpoint at the LMA.  Remove it.
2565 		*/
2566 		/* Ignore any failures: if the LMA is in ROM, we will
2567 		   have already warned when we failed to insert it.  */
2568 		if (bl->loc_type == bp_loc_hardware_breakpoint)
2569 		  target_remove_hw_breakpoint (bl->gdbarch,
2570 					       &bl->overlay_target_info);
2571 		else
2572 		  target_remove_breakpoint (bl->gdbarch,
2573 					    &bl->overlay_target_info);
2574 	      }
2575 	  /* Did we set a breakpoint at the VMA?
2576 	     If so, we will have marked the breakpoint 'inserted'.  */
2577 	  if (bl->inserted)
2578 	    {
2579 	      /* Yes -- remove it.  Previously we did not bother to
2580 		 remove the breakpoint if the section had been
2581 		 unmapped, but let's not rely on that being safe.  We
2582 		 don't know what the overlay manager might do.  */
2583 	      if (bl->loc_type == bp_loc_hardware_breakpoint)
2584 		val = target_remove_hw_breakpoint (bl->gdbarch,
2585 						   &bl->target_info);
2586 
2587 	      /* However, we should remove *software* breakpoints only
2588 		 if the section is still mapped, or else we overwrite
2589 		 wrong code with the saved shadow contents.  */
2590 	      else if (section_is_mapped (bl->section))
2591 		val = target_remove_breakpoint (bl->gdbarch,
2592 						&bl->target_info);
2593 	      else
2594 		val = 0;
2595 	    }
2596 	  else
2597 	    {
2598 	      /* No -- not inserted, so no need to remove.  No error.  */
2599 	      val = 0;
2600 	    }
2601 	}
2602 
2603       /* In some cases, we might not be able to remove a breakpoint
2604 	 in a shared library that has already been removed, but we
2605 	 have not yet processed the shlib unload event.  */
2606       if (val && solib_name_from_address (bl->pspace, bl->address))
2607 	val = 0;
2608 
2609       if (val)
2610 	return val;
2611       bl->inserted = (is == mark_inserted);
2612     }
2613   else if (bl->loc_type == bp_loc_hardware_watchpoint)
2614     {
2615       gdb_assert (bl->owner->ops != NULL
2616 		  && bl->owner->ops->remove_location != NULL);
2617 
2618       bl->inserted = (is == mark_inserted);
2619       bl->owner->ops->remove_location (bl);
2620 
2621       /* Failure to remove any of the hardware watchpoints comes here.  */
2622       if ((is == mark_uninserted) && (bl->inserted))
2623 	warning (_("Could not remove hardware watchpoint %d."),
2624 		 bl->owner->number);
2625     }
2626   else if (bl->owner->type == bp_catchpoint
2627            && breakpoint_enabled (bl->owner)
2628            && !bl->duplicate)
2629     {
2630       gdb_assert (bl->owner->ops != NULL
2631 		  && bl->owner->ops->remove_location != NULL);
2632 
2633       val = bl->owner->ops->remove_location (bl);
2634       if (val)
2635 	return val;
2636 
2637       bl->inserted = (is == mark_inserted);
2638     }
2639 
2640   return 0;
2641 }
2642 
2643 static int
2644 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
2645 {
2646   int ret;
2647   struct cleanup *old_chain;
2648 
2649   /* BL is never in moribund_locations by our callers.  */
2650   gdb_assert (bl->owner != NULL);
2651 
2652   if (bl->owner->enable_state == bp_permanent)
2653     /* Permanent breakpoints cannot be inserted or removed.  */
2654     return 0;
2655 
2656   /* The type of none suggests that owner is actually deleted.
2657      This should not ever happen.  */
2658   gdb_assert (bl->owner->type != bp_none);
2659 
2660   old_chain = save_current_space_and_thread ();
2661 
2662   switch_to_program_space_and_thread (bl->pspace);
2663 
2664   ret = remove_breakpoint_1 (bl, is);
2665 
2666   do_cleanups (old_chain);
2667   return ret;
2668 }
2669 
2670 /* Clear the "inserted" flag in all breakpoints.  */
2671 
2672 void
2673 mark_breakpoints_out (void)
2674 {
2675   struct bp_location *bl, **blp_tmp;
2676 
2677   ALL_BP_LOCATIONS (bl, blp_tmp)
2678     if (bl->pspace == current_program_space)
2679       bl->inserted = 0;
2680 }
2681 
2682 /* Clear the "inserted" flag in all breakpoints and delete any
2683    breakpoints which should go away between runs of the program.
2684 
2685    Plus other such housekeeping that has to be done for breakpoints
2686    between runs.
2687 
2688    Note: this function gets called at the end of a run (by
2689    generic_mourn_inferior) and when a run begins (by
2690    init_wait_for_inferior).  */
2691 
2692 
2693 
2694 void
2695 breakpoint_init_inferior (enum inf_context context)
2696 {
2697   struct breakpoint *b, *b_tmp;
2698   struct bp_location *bl, **blp_tmp;
2699   int ix;
2700   struct program_space *pspace = current_program_space;
2701 
2702   /* If breakpoint locations are shared across processes, then there's
2703      nothing to do.  */
2704   if (gdbarch_has_global_breakpoints (target_gdbarch))
2705     return;
2706 
2707   ALL_BP_LOCATIONS (bl, blp_tmp)
2708   {
2709     /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
2710     if (bl->pspace == pspace
2711 	&& bl->owner->enable_state != bp_permanent)
2712       bl->inserted = 0;
2713   }
2714 
2715   ALL_BREAKPOINTS_SAFE (b, b_tmp)
2716   {
2717     if (b->loc && b->loc->pspace != pspace)
2718       continue;
2719 
2720     switch (b->type)
2721       {
2722       case bp_call_dummy:
2723 
2724 	/* If the call dummy breakpoint is at the entry point it will
2725 	   cause problems when the inferior is rerun, so we better get
2726 	   rid of it.  */
2727 
2728       case bp_watchpoint_scope:
2729 
2730 	/* Also get rid of scope breakpoints.  */
2731 
2732       case bp_shlib_event:
2733 
2734 	/* Also remove solib event breakpoints.  Their addresses may
2735 	   have changed since the last time we ran the program.
2736 	   Actually we may now be debugging against different target;
2737 	   and so the solib backend that installed this breakpoint may
2738 	   not be used in by the target.  E.g.,
2739 
2740 	   (gdb) file prog-linux
2741 	   (gdb) run               # native linux target
2742 	   ...
2743 	   (gdb) kill
2744 	   (gdb) file prog-win.exe
2745 	   (gdb) tar rem :9999     # remote Windows gdbserver.
2746 	*/
2747 
2748 	delete_breakpoint (b);
2749 	break;
2750 
2751       case bp_watchpoint:
2752       case bp_hardware_watchpoint:
2753       case bp_read_watchpoint:
2754       case bp_access_watchpoint:
2755 
2756 	/* Likewise for watchpoints on local expressions.  */
2757 	if (b->exp_valid_block != NULL)
2758 	  delete_breakpoint (b);
2759 	else if (context == inf_starting)
2760 	  {
2761 	    /* Reset val field to force reread of starting value in
2762 	       insert_breakpoints.  */
2763 	    if (b->val)
2764 	      value_free (b->val);
2765 	    b->val = NULL;
2766 	    b->val_valid = 0;
2767 	  }
2768 	break;
2769       default:
2770 	break;
2771       }
2772   }
2773 
2774   /* Get rid of the moribund locations.  */
2775   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2776     decref_bp_location (&bl);
2777   VEC_free (bp_location_p, moribund_locations);
2778 }
2779 
2780 /* These functions concern about actual breakpoints inserted in the
2781    target --- to e.g. check if we need to do decr_pc adjustment or if
2782    we need to hop over the bkpt --- so we check for address space
2783    match, not program space.  */
2784 
2785 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2786    exists at PC.  It returns ordinary_breakpoint_here if it's an
2787    ordinary breakpoint, or permanent_breakpoint_here if it's a
2788    permanent breakpoint.
2789    - When continuing from a location with an ordinary breakpoint, we
2790      actually single step once before calling insert_breakpoints.
2791    - When continuing from a localion with a permanent breakpoint, we
2792      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2793      the target, to advance the PC past the breakpoint.  */
2794 
2795 enum breakpoint_here
2796 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2797 {
2798   struct bp_location *bl, **blp_tmp;
2799   int any_breakpoint_here = 0;
2800 
2801   ALL_BP_LOCATIONS (bl, blp_tmp)
2802     {
2803       if (bl->loc_type != bp_loc_software_breakpoint
2804 	  && bl->loc_type != bp_loc_hardware_breakpoint)
2805 	continue;
2806 
2807       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
2808       if ((breakpoint_enabled (bl->owner)
2809 	   || bl->owner->enable_state == bp_permanent)
2810 	  && breakpoint_location_address_match (bl, aspace, pc))
2811 	{
2812 	  if (overlay_debugging
2813 	      && section_is_overlay (bl->section)
2814 	      && !section_is_mapped (bl->section))
2815 	    continue;		/* unmapped overlay -- can't be a match */
2816 	  else if (bl->owner->enable_state == bp_permanent)
2817 	    return permanent_breakpoint_here;
2818 	  else
2819 	    any_breakpoint_here = 1;
2820 	}
2821     }
2822 
2823   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
2824 }
2825 
2826 /* Return true if there's a moribund breakpoint at PC.  */
2827 
2828 int
2829 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2830 {
2831   struct bp_location *loc;
2832   int ix;
2833 
2834   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
2835     if (breakpoint_location_address_match (loc, aspace, pc))
2836       return 1;
2837 
2838   return 0;
2839 }
2840 
2841 /* Returns non-zero if there's a breakpoint inserted at PC, which is
2842    inserted using regular breakpoint_chain / bp_location array
2843    mechanism.  This does not check for single-step breakpoints, which
2844    are inserted and removed using direct target manipulation.  */
2845 
2846 int
2847 regular_breakpoint_inserted_here_p (struct address_space *aspace,
2848 				    CORE_ADDR pc)
2849 {
2850   struct bp_location *bl, **blp_tmp;
2851 
2852   ALL_BP_LOCATIONS (bl, blp_tmp)
2853     {
2854       if (bl->loc_type != bp_loc_software_breakpoint
2855 	  && bl->loc_type != bp_loc_hardware_breakpoint)
2856 	continue;
2857 
2858       if (bl->inserted
2859 	  && breakpoint_location_address_match (bl, aspace, pc))
2860 	{
2861 	  if (overlay_debugging
2862 	      && section_is_overlay (bl->section)
2863 	      && !section_is_mapped (bl->section))
2864 	    continue;		/* unmapped overlay -- can't be a match */
2865 	  else
2866 	    return 1;
2867 	}
2868     }
2869   return 0;
2870 }
2871 
2872 /* Returns non-zero iff there's either regular breakpoint
2873    or a single step breakpoint inserted at PC.  */
2874 
2875 int
2876 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
2877 {
2878   if (regular_breakpoint_inserted_here_p (aspace, pc))
2879     return 1;
2880 
2881   if (single_step_breakpoint_inserted_here_p (aspace, pc))
2882     return 1;
2883 
2884   return 0;
2885 }
2886 
2887 /* This function returns non-zero iff there is a software breakpoint
2888    inserted at PC.  */
2889 
2890 int
2891 software_breakpoint_inserted_here_p (struct address_space *aspace,
2892 				     CORE_ADDR pc)
2893 {
2894   struct bp_location *bl, **blp_tmp;
2895 
2896   ALL_BP_LOCATIONS (bl, blp_tmp)
2897     {
2898       if (bl->loc_type != bp_loc_software_breakpoint)
2899 	continue;
2900 
2901       if (bl->inserted
2902 	  && breakpoint_address_match (bl->pspace->aspace, bl->address,
2903 				       aspace, pc))
2904 	{
2905 	  if (overlay_debugging
2906 	      && section_is_overlay (bl->section)
2907 	      && !section_is_mapped (bl->section))
2908 	    continue;		/* unmapped overlay -- can't be a match */
2909 	  else
2910 	    return 1;
2911 	}
2912     }
2913 
2914   /* Also check for software single-step breakpoints.  */
2915   if (single_step_breakpoint_inserted_here_p (aspace, pc))
2916     return 1;
2917 
2918   return 0;
2919 }
2920 
2921 int
2922 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2923 				       CORE_ADDR addr, ULONGEST len)
2924 {
2925   struct breakpoint *bpt;
2926 
2927   ALL_BREAKPOINTS (bpt)
2928     {
2929       struct bp_location *loc;
2930 
2931       if (bpt->type != bp_hardware_watchpoint
2932 	  && bpt->type != bp_access_watchpoint)
2933 	continue;
2934 
2935       if (!breakpoint_enabled (bpt))
2936 	continue;
2937 
2938       for (loc = bpt->loc; loc; loc = loc->next)
2939 	if (loc->pspace->aspace == aspace && loc->inserted)
2940 	  {
2941 	    CORE_ADDR l, h;
2942 
2943 	    /* Check for intersection.  */
2944 	    l = max (loc->address, addr);
2945 	    h = min (loc->address + loc->length, addr + len);
2946 	    if (l < h)
2947 	      return 1;
2948 	  }
2949     }
2950   return 0;
2951 }
2952 
2953 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2954    PC is valid for process/thread PTID.  */
2955 
2956 int
2957 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2958 			 ptid_t ptid)
2959 {
2960   struct bp_location *bl, **blp_tmp;
2961   /* The thread and task IDs associated to PTID, computed lazily.  */
2962   int thread = -1;
2963   int task = 0;
2964 
2965   ALL_BP_LOCATIONS (bl, blp_tmp)
2966     {
2967       if (bl->loc_type != bp_loc_software_breakpoint
2968 	  && bl->loc_type != bp_loc_hardware_breakpoint)
2969 	continue;
2970 
2971       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
2972       if (!breakpoint_enabled (bl->owner)
2973 	  && bl->owner->enable_state != bp_permanent)
2974 	continue;
2975 
2976       if (!breakpoint_location_address_match (bl, aspace, pc))
2977 	continue;
2978 
2979       if (bl->owner->thread != -1)
2980 	{
2981 	  /* This is a thread-specific breakpoint.  Check that ptid
2982 	     matches that thread.  If thread hasn't been computed yet,
2983 	     it is now time to do so.  */
2984 	  if (thread == -1)
2985 	    thread = pid_to_thread_id (ptid);
2986 	  if (bl->owner->thread != thread)
2987 	    continue;
2988 	}
2989 
2990       if (bl->owner->task != 0)
2991         {
2992 	  /* This is a task-specific breakpoint.  Check that ptid
2993 	     matches that task.  If task hasn't been computed yet,
2994 	     it is now time to do so.  */
2995 	  if (task == 0)
2996 	    task = ada_get_task_number (ptid);
2997 	  if (bl->owner->task != task)
2998 	    continue;
2999         }
3000 
3001       if (overlay_debugging
3002 	  && section_is_overlay (bl->section)
3003 	  && !section_is_mapped (bl->section))
3004 	continue;	    /* unmapped overlay -- can't be a match */
3005 
3006       return 1;
3007     }
3008 
3009   return 0;
3010 }
3011 
3012 
3013 /* bpstat stuff.  External routines' interfaces are documented
3014    in breakpoint.h.  */
3015 
3016 int
3017 ep_is_catchpoint (struct breakpoint *ep)
3018 {
3019   return (ep->type == bp_catchpoint);
3020 }
3021 
3022 /* Frees any storage that is part of a bpstat.  Does not walk the
3023    'next' chain.  */
3024 
3025 static void
3026 bpstat_free (bpstat bs)
3027 {
3028   if (bs->old_val != NULL)
3029     value_free (bs->old_val);
3030   decref_counted_command_line (&bs->commands);
3031   decref_bp_location (&bs->bp_location_at);
3032   xfree (bs);
3033 }
3034 
3035 /* Clear a bpstat so that it says we are not at any breakpoint.
3036    Also free any storage that is part of a bpstat.  */
3037 
3038 void
3039 bpstat_clear (bpstat *bsp)
3040 {
3041   bpstat p;
3042   bpstat q;
3043 
3044   if (bsp == 0)
3045     return;
3046   p = *bsp;
3047   while (p != NULL)
3048     {
3049       q = p->next;
3050       bpstat_free (p);
3051       p = q;
3052     }
3053   *bsp = NULL;
3054 }
3055 
3056 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
3057    is part of the bpstat is copied as well.  */
3058 
3059 bpstat
3060 bpstat_copy (bpstat bs)
3061 {
3062   bpstat p = NULL;
3063   bpstat tmp;
3064   bpstat retval = NULL;
3065 
3066   if (bs == NULL)
3067     return bs;
3068 
3069   for (; bs != NULL; bs = bs->next)
3070     {
3071       tmp = (bpstat) xmalloc (sizeof (*tmp));
3072       memcpy (tmp, bs, sizeof (*tmp));
3073       incref_counted_command_line (tmp->commands);
3074       incref_bp_location (tmp->bp_location_at);
3075       if (bs->old_val != NULL)
3076 	{
3077 	  tmp->old_val = value_copy (bs->old_val);
3078 	  release_value (tmp->old_val);
3079 	}
3080 
3081       if (p == NULL)
3082 	/* This is the first thing in the chain.  */
3083 	retval = tmp;
3084       else
3085 	p->next = tmp;
3086       p = tmp;
3087     }
3088   p->next = NULL;
3089   return retval;
3090 }
3091 
3092 /* Find the bpstat associated with this breakpoint.  */
3093 
3094 bpstat
3095 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3096 {
3097   if (bsp == NULL)
3098     return NULL;
3099 
3100   for (; bsp != NULL; bsp = bsp->next)
3101     {
3102       if (bsp->breakpoint_at == breakpoint)
3103 	return bsp;
3104     }
3105   return NULL;
3106 }
3107 
3108 /* Put in *NUM the breakpoint number of the first breakpoint we are
3109    stopped at.  *BSP upon return is a bpstat which points to the
3110    remaining breakpoints stopped at (but which is not guaranteed to be
3111    good for anything but further calls to bpstat_num).
3112 
3113    Return 0 if passed a bpstat which does not indicate any breakpoints.
3114    Return -1 if stopped at a breakpoint that has been deleted since
3115    we set it.
3116    Return 1 otherwise.  */
3117 
3118 int
3119 bpstat_num (bpstat *bsp, int *num)
3120 {
3121   struct breakpoint *b;
3122 
3123   if ((*bsp) == NULL)
3124     return 0;			/* No more breakpoint values */
3125 
3126   /* We assume we'll never have several bpstats that correspond to a
3127      single breakpoint -- otherwise, this function might return the
3128      same number more than once and this will look ugly.  */
3129   b = (*bsp)->breakpoint_at;
3130   *bsp = (*bsp)->next;
3131   if (b == NULL)
3132     return -1;			/* breakpoint that's been deleted since */
3133 
3134   *num = b->number;		/* We have its number */
3135   return 1;
3136 }
3137 
3138 /* Modify BS so that the actions will not be performed.  */
3139 
3140 void
3141 bpstat_clear_actions (bpstat bs)
3142 {
3143   for (; bs != NULL; bs = bs->next)
3144     {
3145       decref_counted_command_line (&bs->commands);
3146       bs->commands_left = NULL;
3147       if (bs->old_val != NULL)
3148 	{
3149 	  value_free (bs->old_val);
3150 	  bs->old_val = NULL;
3151 	}
3152     }
3153 }
3154 
3155 /* Called when a command is about to proceed the inferior.  */
3156 
3157 static void
3158 breakpoint_about_to_proceed (void)
3159 {
3160   if (!ptid_equal (inferior_ptid, null_ptid))
3161     {
3162       struct thread_info *tp = inferior_thread ();
3163 
3164       /* Allow inferior function calls in breakpoint commands to not
3165 	 interrupt the command list.  When the call finishes
3166 	 successfully, the inferior will be standing at the same
3167 	 breakpoint as if nothing happened.  */
3168       if (tp->control.in_infcall)
3169 	return;
3170     }
3171 
3172   breakpoint_proceeded = 1;
3173 }
3174 
3175 /* Stub for cleaning up our state if we error-out of a breakpoint
3176    command.  */
3177 static void
3178 cleanup_executing_breakpoints (void *ignore)
3179 {
3180   executing_breakpoint_commands = 0;
3181 }
3182 
3183 /* Execute all the commands associated with all the breakpoints at
3184    this location.  Any of these commands could cause the process to
3185    proceed beyond this point, etc.  We look out for such changes by
3186    checking the global "breakpoint_proceeded" after each command.
3187 
3188    Returns true if a breakpoint command resumed the inferior.  In that
3189    case, it is the caller's responsibility to recall it again with the
3190    bpstat of the current thread.  */
3191 
3192 static int
3193 bpstat_do_actions_1 (bpstat *bsp)
3194 {
3195   bpstat bs;
3196   struct cleanup *old_chain;
3197   int again = 0;
3198 
3199   /* Avoid endless recursion if a `source' command is contained
3200      in bs->commands.  */
3201   if (executing_breakpoint_commands)
3202     return 0;
3203 
3204   executing_breakpoint_commands = 1;
3205   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3206 
3207   /* This pointer will iterate over the list of bpstat's.  */
3208   bs = *bsp;
3209 
3210   breakpoint_proceeded = 0;
3211   for (; bs != NULL; bs = bs->next)
3212     {
3213       struct counted_command_line *ccmd;
3214       struct command_line *cmd;
3215       struct cleanup *this_cmd_tree_chain;
3216 
3217       /* Take ownership of the BSP's command tree, if it has one.
3218 
3219          The command tree could legitimately contain commands like
3220          'step' and 'next', which call clear_proceed_status, which
3221          frees stop_bpstat's command tree.  To make sure this doesn't
3222          free the tree we're executing out from under us, we need to
3223          take ownership of the tree ourselves.  Since a given bpstat's
3224          commands are only executed once, we don't need to copy it; we
3225          can clear the pointer in the bpstat, and make sure we free
3226          the tree when we're done.  */
3227       ccmd = bs->commands;
3228       bs->commands = NULL;
3229       this_cmd_tree_chain
3230 	= make_cleanup_decref_counted_command_line (&ccmd);
3231       cmd = bs->commands_left;
3232       bs->commands_left = NULL;
3233 
3234       while (cmd != NULL)
3235 	{
3236 	  execute_control_command (cmd);
3237 
3238 	  if (breakpoint_proceeded)
3239 	    break;
3240 	  else
3241 	    cmd = cmd->next;
3242 	}
3243 
3244       /* We can free this command tree now.  */
3245       do_cleanups (this_cmd_tree_chain);
3246 
3247       if (breakpoint_proceeded)
3248 	{
3249 	  if (target_can_async_p ())
3250 	    /* If we are in async mode, then the target might be still
3251 	       running, not stopped at any breakpoint, so nothing for
3252 	       us to do here -- just return to the event loop.  */
3253 	    ;
3254 	  else
3255 	    /* In sync mode, when execute_control_command returns
3256 	       we're already standing on the next breakpoint.
3257 	       Breakpoint commands for that stop were not run, since
3258 	       execute_command does not run breakpoint commands --
3259 	       only command_line_handler does, but that one is not
3260 	       involved in execution of breakpoint commands.  So, we
3261 	       can now execute breakpoint commands.  It should be
3262 	       noted that making execute_command do bpstat actions is
3263 	       not an option -- in this case we'll have recursive
3264 	       invocation of bpstat for each breakpoint with a
3265 	       command, and can easily blow up GDB stack.  Instead, we
3266 	       return true, which will trigger the caller to recall us
3267 	       with the new stop_bpstat.  */
3268 	    again = 1;
3269 	  break;
3270 	}
3271     }
3272   do_cleanups (old_chain);
3273   return again;
3274 }
3275 
3276 void
3277 bpstat_do_actions (void)
3278 {
3279   /* Do any commands attached to breakpoint we are stopped at.  */
3280   while (!ptid_equal (inferior_ptid, null_ptid)
3281 	 && target_has_execution
3282 	 && !is_exited (inferior_ptid)
3283 	 && !is_executing (inferior_ptid))
3284     /* Since in sync mode, bpstat_do_actions may resume the inferior,
3285        and only return when it is stopped at the next breakpoint, we
3286        keep doing breakpoint actions until it returns false to
3287        indicate the inferior was not resumed.  */
3288     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
3289       break;
3290 }
3291 
3292 /* Print out the (old or new) value associated with a watchpoint.  */
3293 
3294 static void
3295 watchpoint_value_print (struct value *val, struct ui_file *stream)
3296 {
3297   if (val == NULL)
3298     fprintf_unfiltered (stream, _("<unreadable>"));
3299   else
3300     {
3301       struct value_print_options opts;
3302       get_user_print_options (&opts);
3303       value_print (val, stream, &opts);
3304     }
3305 }
3306 
3307 /* This is the normal print function for a bpstat.  In the future,
3308    much of this logic could (should?) be moved to bpstat_stop_status,
3309    by having it set different print_it values.
3310 
3311    Current scheme: When we stop, bpstat_print() is called.  It loops
3312    through the bpstat list of things causing this stop, calling the
3313    print_bp_stop_message function on each one.  The behavior of the
3314    print_bp_stop_message function depends on the print_it field of
3315    bpstat.  If such field so indicates, call this function here.
3316 
3317    Return values from this routine (ultimately used by bpstat_print()
3318    and normal_stop() to decide what to do):
3319    PRINT_NOTHING: Means we already printed all we needed to print,
3320    don't print anything else.
3321    PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3322    that something to be followed by a location.
3323    PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3324    that something to be followed by a location.
3325    PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3326    analysis.  */
3327 
3328 static enum print_stop_action
3329 print_it_typical (bpstat bs)
3330 {
3331   struct cleanup *old_chain;
3332   struct breakpoint *b;
3333   const struct bp_location *bl;
3334   struct ui_stream *stb;
3335   int bp_temp = 0;
3336   enum print_stop_action result;
3337 
3338   gdb_assert (bs->bp_location_at != NULL);
3339 
3340   bl = bs->bp_location_at;
3341   b = bs->breakpoint_at;
3342 
3343   stb = ui_out_stream_new (uiout);
3344   old_chain = make_cleanup_ui_out_stream_delete (stb);
3345 
3346   switch (b->type)
3347     {
3348     case bp_breakpoint:
3349     case bp_hardware_breakpoint:
3350       bp_temp = b->disposition == disp_del;
3351       if (bl->address != bl->requested_address)
3352 	breakpoint_adjustment_warning (bl->requested_address,
3353 	                               bl->address,
3354 				       b->number, 1);
3355       annotate_breakpoint (b->number);
3356       if (bp_temp)
3357 	ui_out_text (uiout, "\nTemporary breakpoint ");
3358       else
3359 	ui_out_text (uiout, "\nBreakpoint ");
3360       if (ui_out_is_mi_like_p (uiout))
3361 	{
3362 	  ui_out_field_string (uiout, "reason",
3363 			  async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3364 	  ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3365 	}
3366       ui_out_field_int (uiout, "bkptno", b->number);
3367       ui_out_text (uiout, ", ");
3368       result = PRINT_SRC_AND_LOC;
3369       break;
3370 
3371     case bp_shlib_event:
3372       /* Did we stop because the user set the stop_on_solib_events
3373 	 variable?  (If so, we report this as a generic, "Stopped due
3374 	 to shlib event" message.) */
3375       printf_filtered (_("Stopped due to shared library event\n"));
3376       result = PRINT_NOTHING;
3377       break;
3378 
3379     case bp_thread_event:
3380       /* Not sure how we will get here.
3381 	 GDB should not stop for these breakpoints.  */
3382       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
3383       result = PRINT_NOTHING;
3384       break;
3385 
3386     case bp_overlay_event:
3387       /* By analogy with the thread event, GDB should not stop for these.  */
3388       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
3389       result = PRINT_NOTHING;
3390       break;
3391 
3392     case bp_longjmp_master:
3393       /* These should never be enabled.  */
3394       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3395       result = PRINT_NOTHING;
3396       break;
3397 
3398     case bp_std_terminate_master:
3399       /* These should never be enabled.  */
3400       printf_filtered (_("std::terminate Master Breakpoint: "
3401 			 "gdb should not stop!\n"));
3402       result = PRINT_NOTHING;
3403       break;
3404 
3405     case bp_exception_master:
3406       /* These should never be enabled.  */
3407       printf_filtered (_("Exception Master Breakpoint: "
3408 			 "gdb should not stop!\n"));
3409       result = PRINT_NOTHING;
3410       break;
3411 
3412     case bp_watchpoint:
3413     case bp_hardware_watchpoint:
3414       annotate_watchpoint (b->number);
3415       if (ui_out_is_mi_like_p (uiout))
3416 	ui_out_field_string
3417 	  (uiout, "reason",
3418 	   async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3419       mention (b);
3420       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3421       ui_out_text (uiout, "\nOld value = ");
3422       watchpoint_value_print (bs->old_val, stb->stream);
3423       ui_out_field_stream (uiout, "old", stb);
3424       ui_out_text (uiout, "\nNew value = ");
3425       watchpoint_value_print (b->val, stb->stream);
3426       ui_out_field_stream (uiout, "new", stb);
3427       ui_out_text (uiout, "\n");
3428       /* More than one watchpoint may have been triggered.  */
3429       result = PRINT_UNKNOWN;
3430       break;
3431 
3432     case bp_read_watchpoint:
3433       if (ui_out_is_mi_like_p (uiout))
3434 	ui_out_field_string
3435 	  (uiout, "reason",
3436 	   async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
3437       mention (b);
3438       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3439       ui_out_text (uiout, "\nValue = ");
3440       watchpoint_value_print (b->val, stb->stream);
3441       ui_out_field_stream (uiout, "value", stb);
3442       ui_out_text (uiout, "\n");
3443       result = PRINT_UNKNOWN;
3444       break;
3445 
3446     case bp_access_watchpoint:
3447       if (bs->old_val != NULL)
3448 	{
3449 	  annotate_watchpoint (b->number);
3450 	  if (ui_out_is_mi_like_p (uiout))
3451 	    ui_out_field_string
3452 	      (uiout, "reason",
3453 	       async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
3454 	  mention (b);
3455 	  make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3456 	  ui_out_text (uiout, "\nOld value = ");
3457 	  watchpoint_value_print (bs->old_val, stb->stream);
3458 	  ui_out_field_stream (uiout, "old", stb);
3459 	  ui_out_text (uiout, "\nNew value = ");
3460 	}
3461       else
3462 	{
3463 	  mention (b);
3464 	  if (ui_out_is_mi_like_p (uiout))
3465 	    ui_out_field_string
3466 	      (uiout, "reason",
3467 	       async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
3468 	  make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3469 	  ui_out_text (uiout, "\nValue = ");
3470 	}
3471       watchpoint_value_print (b->val, stb->stream);
3472       ui_out_field_stream (uiout, "new", stb);
3473       ui_out_text (uiout, "\n");
3474       result = PRINT_UNKNOWN;
3475       break;
3476 
3477     /* Fall through, we don't deal with these types of breakpoints
3478        here.  */
3479 
3480     case bp_finish:
3481       if (ui_out_is_mi_like_p (uiout))
3482 	ui_out_field_string
3483 	  (uiout, "reason",
3484 	   async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
3485       result = PRINT_UNKNOWN;
3486       break;
3487 
3488     case bp_until:
3489       if (ui_out_is_mi_like_p (uiout))
3490 	ui_out_field_string
3491 	  (uiout, "reason",
3492 	   async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
3493       result = PRINT_UNKNOWN;
3494       break;
3495 
3496     case bp_none:
3497     case bp_longjmp:
3498     case bp_longjmp_resume:
3499     case bp_exception:
3500     case bp_exception_resume:
3501     case bp_step_resume:
3502     case bp_watchpoint_scope:
3503     case bp_call_dummy:
3504     case bp_std_terminate:
3505     case bp_tracepoint:
3506     case bp_fast_tracepoint:
3507     case bp_jit_event:
3508     case bp_gnu_ifunc_resolver:
3509     case bp_gnu_ifunc_resolver_return:
3510     default:
3511       result = PRINT_UNKNOWN;
3512       break;
3513     }
3514 
3515   do_cleanups (old_chain);
3516   return result;
3517 }
3518 
3519 /* Generic routine for printing messages indicating why we
3520    stopped.  The behavior of this function depends on the value
3521    'print_it' in the bpstat structure.  Under some circumstances we
3522    may decide not to print anything here and delegate the task to
3523    normal_stop().  */
3524 
3525 static enum print_stop_action
3526 print_bp_stop_message (bpstat bs)
3527 {
3528   switch (bs->print_it)
3529     {
3530     case print_it_noop:
3531       /* Nothing should be printed for this bpstat entry.  */
3532       return PRINT_UNKNOWN;
3533       break;
3534 
3535     case print_it_done:
3536       /* We still want to print the frame, but we already printed the
3537          relevant messages.  */
3538       return PRINT_SRC_AND_LOC;
3539       break;
3540 
3541     case print_it_normal:
3542       {
3543 	struct breakpoint *b = bs->breakpoint_at;
3544 
3545 	/* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3546 	   which has since been deleted.  */
3547 	if (b == NULL)
3548 	  return PRINT_UNKNOWN;
3549 
3550 	/* Normal case.  Call the breakpoint's print_it method, or
3551 	   print_it_typical.  */
3552 	if (b->ops != NULL && b->ops->print_it != NULL)
3553 	  return b->ops->print_it (b);
3554 	else
3555 	  return print_it_typical (bs);
3556       }
3557 	break;
3558 
3559     default:
3560       internal_error (__FILE__, __LINE__,
3561 		      _("print_bp_stop_message: unrecognized enum value"));
3562       break;
3563     }
3564 }
3565 
3566 /* Print a message indicating what happened.  This is called from
3567    normal_stop().  The input to this routine is the head of the bpstat
3568    list - a list of the eventpoints that caused this stop.  This
3569    routine calls the generic print routine for printing a message
3570    about reasons for stopping.  This will print (for example) the
3571    "Breakpoint n," part of the output.  The return value of this
3572    routine is one of:
3573 
3574    PRINT_UNKNOWN: Means we printed nothing.
3575    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
3576    code to print the location.  An example is
3577    "Breakpoint 1, " which should be followed by
3578    the location.
3579    PRINT_SRC_ONLY: Means we printed something, but there is no need
3580    to also print the location part of the message.
3581    An example is the catch/throw messages, which
3582    don't require a location appended to the end.
3583    PRINT_NOTHING: We have done some printing and we don't need any
3584    further info to be printed.  */
3585 
3586 enum print_stop_action
3587 bpstat_print (bpstat bs)
3588 {
3589   int val;
3590 
3591   /* Maybe another breakpoint in the chain caused us to stop.
3592      (Currently all watchpoints go on the bpstat whether hit or not.
3593      That probably could (should) be changed, provided care is taken
3594      with respect to bpstat_explains_signal).  */
3595   for (; bs; bs = bs->next)
3596     {
3597       val = print_bp_stop_message (bs);
3598       if (val == PRINT_SRC_ONLY
3599 	  || val == PRINT_SRC_AND_LOC
3600 	  || val == PRINT_NOTHING)
3601 	return val;
3602     }
3603 
3604   /* We reached the end of the chain, or we got a null BS to start
3605      with and nothing was printed.  */
3606   return PRINT_UNKNOWN;
3607 }
3608 
3609 /* Evaluate the expression EXP and return 1 if value is zero.  This is
3610    used inside a catch_errors to evaluate the breakpoint condition.
3611    The argument is a "struct expression *" that has been cast to a
3612    "char *" to make it pass through catch_errors.  */
3613 
3614 static int
3615 breakpoint_cond_eval (void *exp)
3616 {
3617   struct value *mark = value_mark ();
3618   int i = !value_true (evaluate_expression ((struct expression *) exp));
3619 
3620   value_free_to_mark (mark);
3621   return i;
3622 }
3623 
3624 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
3625 
3626 static bpstat
3627 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
3628 {
3629   bpstat bs;
3630 
3631   bs = (bpstat) xmalloc (sizeof (*bs));
3632   bs->next = NULL;
3633   **bs_link_pointer = bs;
3634   *bs_link_pointer = &bs->next;
3635   bs->breakpoint_at = bl->owner;
3636   bs->bp_location_at = bl;
3637   incref_bp_location (bl);
3638   /* If the condition is false, etc., don't do the commands.  */
3639   bs->commands = NULL;
3640   bs->commands_left = NULL;
3641   bs->old_val = NULL;
3642   bs->print_it = print_it_normal;
3643   return bs;
3644 }
3645 
3646 /* The target has stopped with waitstatus WS.  Check if any hardware
3647    watchpoints have triggered, according to the target.  */
3648 
3649 int
3650 watchpoints_triggered (struct target_waitstatus *ws)
3651 {
3652   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
3653   CORE_ADDR addr;
3654   struct breakpoint *b;
3655 
3656   if (!stopped_by_watchpoint)
3657     {
3658       /* We were not stopped by a watchpoint.  Mark all watchpoints
3659 	 as not triggered.  */
3660       ALL_BREAKPOINTS (b)
3661 	if (is_hardware_watchpoint (b))
3662 	  b->watchpoint_triggered = watch_triggered_no;
3663 
3664       return 0;
3665     }
3666 
3667   if (!target_stopped_data_address (&current_target, &addr))
3668     {
3669       /* We were stopped by a watchpoint, but we don't know where.
3670 	 Mark all watchpoints as unknown.  */
3671       ALL_BREAKPOINTS (b)
3672 	if (is_hardware_watchpoint (b))
3673 	  b->watchpoint_triggered = watch_triggered_unknown;
3674 
3675       return stopped_by_watchpoint;
3676     }
3677 
3678   /* The target could report the data address.  Mark watchpoints
3679      affected by this data address as triggered, and all others as not
3680      triggered.  */
3681 
3682   ALL_BREAKPOINTS (b)
3683     if (is_hardware_watchpoint (b))
3684       {
3685 	struct bp_location *loc;
3686 
3687 	b->watchpoint_triggered = watch_triggered_no;
3688 	for (loc = b->loc; loc; loc = loc->next)
3689 	  /* Exact match not required.  Within range is
3690 	     sufficient.  */
3691 	  if (target_watchpoint_addr_within_range (&current_target,
3692 						   addr, loc->address,
3693 						   loc->length))
3694 	    {
3695 	      b->watchpoint_triggered = watch_triggered_yes;
3696 	      break;
3697 	    }
3698       }
3699 
3700   return 1;
3701 }
3702 
3703 /* Possible return values for watchpoint_check (this can't be an enum
3704    because of check_errors).  */
3705 /* The watchpoint has been deleted.  */
3706 #define WP_DELETED 1
3707 /* The value has changed.  */
3708 #define WP_VALUE_CHANGED 2
3709 /* The value has not changed.  */
3710 #define WP_VALUE_NOT_CHANGED 3
3711 /* Ignore this watchpoint, no matter if the value changed or not.  */
3712 #define WP_IGNORE 4
3713 
3714 #define BP_TEMPFLAG 1
3715 #define BP_HARDWAREFLAG 2
3716 
3717 /* Evaluate watchpoint condition expression and check if its value
3718    changed.
3719 
3720    P should be a pointer to struct bpstat, but is defined as a void *
3721    in order for this function to be usable with catch_errors.  */
3722 
3723 static int
3724 watchpoint_check (void *p)
3725 {
3726   bpstat bs = (bpstat) p;
3727   struct breakpoint *b;
3728   struct frame_info *fr;
3729   int within_current_scope;
3730 
3731   /* BS is built from an existing struct breakpoint.  */
3732   gdb_assert (bs->breakpoint_at != NULL);
3733   b = bs->breakpoint_at;
3734 
3735   gdb_assert (is_watchpoint (b));
3736 
3737   /* If this is a local watchpoint, we only want to check if the
3738      watchpoint frame is in scope if the current thread is the thread
3739      that was used to create the watchpoint.  */
3740   if (!watchpoint_in_thread_scope (b))
3741     return WP_IGNORE;
3742 
3743   if (b->exp_valid_block == NULL)
3744     within_current_scope = 1;
3745   else
3746     {
3747       struct frame_info *frame = get_current_frame ();
3748       struct gdbarch *frame_arch = get_frame_arch (frame);
3749       CORE_ADDR frame_pc = get_frame_pc (frame);
3750 
3751       /* in_function_epilogue_p() returns a non-zero value if we're
3752 	 still in the function but the stack frame has already been
3753 	 invalidated.  Since we can't rely on the values of local
3754 	 variables after the stack has been destroyed, we are treating
3755 	 the watchpoint in that state as `not changed' without further
3756 	 checking.  Don't mark watchpoints as changed if the current
3757 	 frame is in an epilogue - even if they are in some other
3758 	 frame, our view of the stack is likely to be wrong and
3759 	 frame_find_by_id could error out.  */
3760       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
3761 	return WP_IGNORE;
3762 
3763       fr = frame_find_by_id (b->watchpoint_frame);
3764       within_current_scope = (fr != NULL);
3765 
3766       /* If we've gotten confused in the unwinder, we might have
3767 	 returned a frame that can't describe this variable.  */
3768       if (within_current_scope)
3769 	{
3770 	  struct symbol *function;
3771 
3772 	  function = get_frame_function (fr);
3773 	  if (function == NULL
3774 	      || !contained_in (b->exp_valid_block,
3775 				SYMBOL_BLOCK_VALUE (function)))
3776 	    within_current_scope = 0;
3777 	}
3778 
3779       if (within_current_scope)
3780 	/* If we end up stopping, the current frame will get selected
3781 	   in normal_stop.  So this call to select_frame won't affect
3782 	   the user.  */
3783 	select_frame (fr);
3784     }
3785 
3786   if (within_current_scope)
3787     {
3788       /* We use value_{,free_to_}mark because it could be a *long*
3789          time before we return to the command level and call
3790          free_all_values.  We can't call free_all_values because we
3791          might be in the middle of evaluating a function call.  */
3792 
3793       int pc = 0;
3794       struct value *mark = value_mark ();
3795       struct value *new_val;
3796 
3797       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
3798 
3799       /* We use value_equal_contents instead of value_equal because
3800 	 the latter coerces an array to a pointer, thus comparing just
3801 	 the address of the array instead of its contents.  This is
3802 	 not what we want.  */
3803       if ((b->val != NULL) != (new_val != NULL)
3804 	  || (b->val != NULL && !value_equal_contents (b->val, new_val)))
3805 	{
3806 	  if (new_val != NULL)
3807 	    {
3808 	      release_value (new_val);
3809 	      value_free_to_mark (mark);
3810 	    }
3811 	  bs->old_val = b->val;
3812 	  b->val = new_val;
3813 	  b->val_valid = 1;
3814 	  return WP_VALUE_CHANGED;
3815 	}
3816       else
3817 	{
3818 	  /* Nothing changed.  */
3819 	  value_free_to_mark (mark);
3820 	  return WP_VALUE_NOT_CHANGED;
3821 	}
3822     }
3823   else
3824     {
3825       /* This seems like the only logical thing to do because
3826          if we temporarily ignored the watchpoint, then when
3827          we reenter the block in which it is valid it contains
3828          garbage (in the case of a function, it may have two
3829          garbage values, one before and one after the prologue).
3830          So we can't even detect the first assignment to it and
3831          watch after that (since the garbage may or may not equal
3832          the first value assigned).  */
3833       /* We print all the stop information in print_it_typical(), but
3834 	 in this case, by the time we call print_it_typical() this bp
3835 	 will be deleted already.  So we have no choice but print the
3836 	 information here.  */
3837       if (ui_out_is_mi_like_p (uiout))
3838 	ui_out_field_string
3839 	  (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
3840       ui_out_text (uiout, "\nWatchpoint ");
3841       ui_out_field_int (uiout, "wpnum", b->number);
3842       ui_out_text (uiout,
3843 		   " deleted because the program has left the block in\n\
3844 which its expression is valid.\n");
3845 
3846       watchpoint_del_at_next_stop (b);
3847 
3848       return WP_DELETED;
3849     }
3850 }
3851 
3852 /* Return true if it looks like target has stopped due to hitting
3853    breakpoint location BL.  This function does not check if we
3854    should stop, only if BL explains the stop.   */
3855 static int
3856 bpstat_check_location (const struct bp_location *bl,
3857 		       struct address_space *aspace, CORE_ADDR bp_addr)
3858 {
3859   struct breakpoint *b = bl->owner;
3860 
3861   /* BL is from existing struct breakpoint.  */
3862   gdb_assert (b != NULL);
3863 
3864   if (b->ops && b->ops->breakpoint_hit)
3865     return b->ops->breakpoint_hit (bl, aspace, bp_addr);
3866 
3867   /* By definition, the inferior does not report stops at
3868      tracepoints.  */
3869   if (is_tracepoint (b))
3870     return 0;
3871 
3872   if (!is_watchpoint (b)
3873       && b->type != bp_hardware_breakpoint
3874       && b->type != bp_catchpoint)	/* a non-watchpoint bp */
3875     {
3876       if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3877 				     aspace, bp_addr))
3878 	return 0;
3879       if (overlay_debugging		/* unmapped overlay section */
3880 	  && section_is_overlay (bl->section)
3881 	  && !section_is_mapped (bl->section))
3882 	return 0;
3883     }
3884 
3885   /* Continuable hardware watchpoints are treated as non-existent if the
3886      reason we stopped wasn't a hardware watchpoint (we didn't stop on
3887      some data address).  Otherwise gdb won't stop on a break instruction
3888      in the code (not from a breakpoint) when a hardware watchpoint has
3889      been defined.  Also skip watchpoints which we know did not trigger
3890      (did not match the data address).  */
3891 
3892   if (is_hardware_watchpoint (b)
3893       && b->watchpoint_triggered == watch_triggered_no)
3894     return 0;
3895 
3896   if (b->type == bp_hardware_breakpoint)
3897     {
3898       if (bl->address != bp_addr)
3899 	return 0;
3900       if (overlay_debugging		/* unmapped overlay section */
3901 	  && section_is_overlay (bl->section)
3902 	  && !section_is_mapped (bl->section))
3903 	return 0;
3904     }
3905 
3906   return 1;
3907 }
3908 
3909 /* If BS refers to a watchpoint, determine if the watched values
3910    has actually changed, and we should stop.  If not, set BS->stop
3911    to 0.  */
3912 static void
3913 bpstat_check_watchpoint (bpstat bs)
3914 {
3915   const struct bp_location *bl;
3916   struct breakpoint *b;
3917 
3918   /* BS is built for existing struct breakpoint.  */
3919   bl = bs->bp_location_at;
3920   gdb_assert (bl != NULL);
3921   b = bs->breakpoint_at;
3922   gdb_assert (b != NULL);
3923 
3924   if (is_watchpoint (b))
3925     {
3926       int must_check_value = 0;
3927 
3928       if (b->type == bp_watchpoint)
3929 	/* For a software watchpoint, we must always check the
3930 	   watched value.  */
3931 	must_check_value = 1;
3932       else if (b->watchpoint_triggered == watch_triggered_yes)
3933 	/* We have a hardware watchpoint (read, write, or access)
3934 	   and the target earlier reported an address watched by
3935 	   this watchpoint.  */
3936 	must_check_value = 1;
3937       else if (b->watchpoint_triggered == watch_triggered_unknown
3938 	       && b->type == bp_hardware_watchpoint)
3939 	/* We were stopped by a hardware watchpoint, but the target could
3940 	   not report the data address.  We must check the watchpoint's
3941 	   value.  Access and read watchpoints are out of luck; without
3942 	   a data address, we can't figure it out.  */
3943 	must_check_value = 1;
3944 
3945       if (must_check_value)
3946 	{
3947 	  char *message
3948 	    = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3949 			  b->number);
3950 	  struct cleanup *cleanups = make_cleanup (xfree, message);
3951 	  int e = catch_errors (watchpoint_check, bs, message,
3952 				RETURN_MASK_ALL);
3953 	  do_cleanups (cleanups);
3954 	  switch (e)
3955 	    {
3956 	    case WP_DELETED:
3957 	      /* We've already printed what needs to be printed.  */
3958 	      bs->print_it = print_it_done;
3959 	      /* Stop.  */
3960 	      break;
3961 	    case WP_IGNORE:
3962 	      bs->print_it = print_it_noop;
3963 	      bs->stop = 0;
3964 	      break;
3965 	    case WP_VALUE_CHANGED:
3966 	      if (b->type == bp_read_watchpoint)
3967 		{
3968 		  /* There are two cases to consider here:
3969 
3970 		     1. We're watching the triggered memory for reads.
3971 		     In that case, trust the target, and always report
3972 		     the watchpoint hit to the user.  Even though
3973 		     reads don't cause value changes, the value may
3974 		     have changed since the last time it was read, and
3975 		     since we're not trapping writes, we will not see
3976 		     those, and as such we should ignore our notion of
3977 		     old value.
3978 
3979 		     2. We're watching the triggered memory for both
3980 		     reads and writes.  There are two ways this may
3981 		     happen:
3982 
3983 		     2.1. This is a target that can't break on data
3984 		     reads only, but can break on accesses (reads or
3985 		     writes), such as e.g., x86.  We detect this case
3986 		     at the time we try to insert read watchpoints.
3987 
3988 		     2.2. Otherwise, the target supports read
3989 		     watchpoints, but, the user set an access or write
3990 		     watchpoint watching the same memory as this read
3991 		     watchpoint.
3992 
3993 		     If we're watching memory writes as well as reads,
3994 		     ignore watchpoint hits when we find that the
3995 		     value hasn't changed, as reads don't cause
3996 		     changes.  This still gives false positives when
3997 		     the program writes the same value to memory as
3998 		     what there was already in memory (we will confuse
3999 		     it for a read), but it's much better than
4000 		     nothing.  */
4001 
4002 		  int other_write_watchpoint = 0;
4003 
4004 		  if (bl->watchpoint_type == hw_read)
4005 		    {
4006 		      struct breakpoint *other_b;
4007 
4008 		      ALL_BREAKPOINTS (other_b)
4009 			if ((other_b->type == bp_hardware_watchpoint
4010 			     || other_b->type == bp_access_watchpoint)
4011 			    && (other_b->watchpoint_triggered
4012 				== watch_triggered_yes))
4013 			  {
4014 			    other_write_watchpoint = 1;
4015 			    break;
4016 			  }
4017 		    }
4018 
4019 		  if (other_write_watchpoint
4020 		      || bl->watchpoint_type == hw_access)
4021 		    {
4022 		      /* We're watching the same memory for writes,
4023 			 and the value changed since the last time we
4024 			 updated it, so this trap must be for a write.
4025 			 Ignore it.  */
4026 		      bs->print_it = print_it_noop;
4027 		      bs->stop = 0;
4028 		    }
4029 		}
4030 	      break;
4031 	    case WP_VALUE_NOT_CHANGED:
4032 	      if (b->type == bp_hardware_watchpoint
4033 		  || b->type == bp_watchpoint)
4034 		{
4035 		  /* Don't stop: write watchpoints shouldn't fire if
4036 		     the value hasn't changed.  */
4037 		  bs->print_it = print_it_noop;
4038 		  bs->stop = 0;
4039 		}
4040 	      /* Stop.  */
4041 	      break;
4042 	    default:
4043 	      /* Can't happen.  */
4044 	    case 0:
4045 	      /* Error from catch_errors.  */
4046 	      printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
4047 	      watchpoint_del_at_next_stop (b);
4048 	      /* We've already printed what needs to be printed.  */
4049 	      bs->print_it = print_it_done;
4050 	      break;
4051 	    }
4052 	}
4053       else	/* must_check_value == 0 */
4054 	{
4055 	  /* This is a case where some watchpoint(s) triggered, but
4056 	     not at the address of this watchpoint, or else no
4057 	     watchpoint triggered after all.  So don't print
4058 	     anything for this watchpoint.  */
4059 	  bs->print_it = print_it_noop;
4060 	  bs->stop = 0;
4061 	}
4062     }
4063 }
4064 
4065 
4066 /* Check conditions (condition proper, frame, thread and ignore count)
4067    of breakpoint referred to by BS.  If we should not stop for this
4068    breakpoint, set BS->stop to 0.  */
4069 
4070 static void
4071 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4072 {
4073   int thread_id = pid_to_thread_id (ptid);
4074   const struct bp_location *bl;
4075   struct breakpoint *b;
4076 
4077   /* BS is built for existing struct breakpoint.  */
4078   bl = bs->bp_location_at;
4079   gdb_assert (bl != NULL);
4080   b = bs->breakpoint_at;
4081   gdb_assert (b != NULL);
4082 
4083   if (frame_id_p (b->frame_id)
4084       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
4085     bs->stop = 0;
4086   else if (bs->stop)
4087     {
4088       int value_is_zero = 0;
4089       struct expression *cond;
4090 
4091       /* Evaluate Python breakpoints that have a "stop"
4092 	 method implemented.  */
4093       if (b->py_bp_object)
4094 	bs->stop = gdbpy_should_stop (b->py_bp_object);
4095 
4096       if (is_watchpoint (b))
4097 	cond = b->cond_exp;
4098       else
4099 	cond = bl->cond;
4100 
4101       if (cond && b->disposition != disp_del_at_next_stop)
4102 	{
4103 	  int within_current_scope = 1;
4104 
4105 	  /* We use value_mark and value_free_to_mark because it could
4106 	     be a long time before we return to the command level and
4107 	     call free_all_values.  We can't call free_all_values
4108 	     because we might be in the middle of evaluating a
4109 	     function call.  */
4110 	  struct value *mark = value_mark ();
4111 
4112 	  /* Need to select the frame, with all that implies so that
4113 	     the conditions will have the right context.  Because we
4114 	     use the frame, we will not see an inlined function's
4115 	     variables when we arrive at a breakpoint at the start
4116 	     of the inlined function; the current frame will be the
4117 	     call site.  */
4118 	  if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4119 	    select_frame (get_current_frame ());
4120 	  else
4121 	    {
4122 	      struct frame_info *frame;
4123 
4124 	      /* For local watchpoint expressions, which particular
4125 		 instance of a local is being watched matters, so we
4126 		 keep track of the frame to evaluate the expression
4127 		 in.  To evaluate the condition however, it doesn't
4128 		 really matter which instantiation of the function
4129 		 where the condition makes sense triggers the
4130 		 watchpoint.  This allows an expression like "watch
4131 		 global if q > 10" set in `func', catch writes to
4132 		 global on all threads that call `func', or catch
4133 		 writes on all recursive calls of `func' by a single
4134 		 thread.  We simply always evaluate the condition in
4135 		 the innermost frame that's executing where it makes
4136 		 sense to evaluate the condition.  It seems
4137 		 intuitive.  */
4138 	      frame = block_innermost_frame (b->cond_exp_valid_block);
4139 	      if (frame != NULL)
4140 		select_frame (frame);
4141 	      else
4142 		within_current_scope = 0;
4143 	    }
4144 	  if (within_current_scope)
4145 	    value_is_zero
4146 	      = catch_errors (breakpoint_cond_eval, cond,
4147 			      "Error in testing breakpoint condition:\n",
4148 			      RETURN_MASK_ALL);
4149 	  else
4150 	    {
4151 	      warning (_("Watchpoint condition cannot be tested "
4152 			 "in the current scope"));
4153 	      /* If we failed to set the right context for this
4154 		 watchpoint, unconditionally report it.  */
4155 	      value_is_zero = 0;
4156 	    }
4157 	  /* FIXME-someday, should give breakpoint #.  */
4158 	  value_free_to_mark (mark);
4159 	}
4160 
4161       if (cond && value_is_zero)
4162 	{
4163 	  bs->stop = 0;
4164 	}
4165       else if (b->thread != -1 && b->thread != thread_id)
4166 	{
4167 	  bs->stop = 0;
4168 	}
4169       else if (b->ignore_count > 0)
4170 	{
4171 	  b->ignore_count--;
4172 	  annotate_ignore_count_change ();
4173 	  bs->stop = 0;
4174 	  /* Increase the hit count even though we don't stop.  */
4175 	  ++(b->hit_count);
4176 	}
4177     }
4178 }
4179 
4180 
4181 /* Get a bpstat associated with having just stopped at address
4182    BP_ADDR in thread PTID.
4183 
4184    Determine whether we stopped at a breakpoint, etc, or whether we
4185    don't understand this stop.  Result is a chain of bpstat's such
4186    that:
4187 
4188    if we don't understand the stop, the result is a null pointer.
4189 
4190    if we understand why we stopped, the result is not null.
4191 
4192    Each element of the chain refers to a particular breakpoint or
4193    watchpoint at which we have stopped.  (We may have stopped for
4194    several reasons concurrently.)
4195 
4196    Each element of the chain has valid next, breakpoint_at,
4197    commands, FIXME??? fields.  */
4198 
4199 bpstat
4200 bpstat_stop_status (struct address_space *aspace,
4201 		    CORE_ADDR bp_addr, ptid_t ptid)
4202 {
4203   struct breakpoint *b = NULL;
4204   struct bp_location *bl;
4205   struct bp_location *loc;
4206   /* First item of allocated bpstat's.  */
4207   bpstat bs_head = NULL, *bs_link = &bs_head;
4208   /* Pointer to the last thing in the chain currently.  */
4209   bpstat bs;
4210   int ix;
4211   int need_remove_insert;
4212   int removed_any;
4213 
4214   /* First, build the bpstat chain with locations that explain a
4215      target stop, while being careful to not set the target running,
4216      as that may invalidate locations (in particular watchpoint
4217      locations are recreated).  Resuming will happen here with
4218      breakpoint conditions or watchpoint expressions that include
4219      inferior function calls.  */
4220 
4221   ALL_BREAKPOINTS (b)
4222     {
4223       if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4224 	continue;
4225 
4226       for (bl = b->loc; bl != NULL; bl = bl->next)
4227 	{
4228 	  /* For hardware watchpoints, we look only at the first
4229 	     location.  The watchpoint_check function will work on the
4230 	     entire expression, not the individual locations.  For
4231 	     read watchpoints, the watchpoints_triggered function has
4232 	     checked all locations already.  */
4233 	  if (b->type == bp_hardware_watchpoint && bl != b->loc)
4234 	    break;
4235 
4236 	  if (bl->shlib_disabled)
4237 	    continue;
4238 
4239 	  if (!bpstat_check_location (bl, aspace, bp_addr))
4240 	    continue;
4241 
4242 	  /* Come here if it's a watchpoint, or if the break address
4243 	     matches.  */
4244 
4245 	  bs = bpstat_alloc (bl, &bs_link);	/* Alloc a bpstat to
4246 						   explain stop.  */
4247 
4248 	  /* Assume we stop.  Should we find a watchpoint that is not
4249 	     actually triggered, or if the condition of the breakpoint
4250 	     evaluates as false, we'll reset 'stop' to 0.  */
4251 	  bs->stop = 1;
4252 	  bs->print = 1;
4253 
4254 	  /* If this is a scope breakpoint, mark the associated
4255 	     watchpoint as triggered so that we will handle the
4256 	     out-of-scope event.  We'll get to the watchpoint next
4257 	     iteration.  */
4258 	  if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
4259 	    b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4260 	}
4261     }
4262 
4263   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4264     {
4265       if (breakpoint_location_address_match (loc, aspace, bp_addr))
4266 	{
4267 	  bs = bpstat_alloc (loc, &bs_link);
4268 	  /* For hits of moribund locations, we should just proceed.  */
4269 	  bs->stop = 0;
4270 	  bs->print = 0;
4271 	  bs->print_it = print_it_noop;
4272 	}
4273     }
4274 
4275   /* Now go through the locations that caused the target to stop, and
4276      check whether we're interested in reporting this stop to higher
4277      layers, or whether we should resume the target transparently.  */
4278 
4279   removed_any = 0;
4280 
4281   for (bs = bs_head; bs != NULL; bs = bs->next)
4282     {
4283       if (!bs->stop)
4284 	continue;
4285 
4286       bpstat_check_watchpoint (bs);
4287       if (!bs->stop)
4288 	continue;
4289 
4290       b = bs->breakpoint_at;
4291 
4292 	  if (b->type == bp_thread_event || b->type == bp_overlay_event
4293 	      || b->type == bp_longjmp_master
4294 	      || b->type == bp_std_terminate_master
4295 	      || b->type == bp_exception_master)
4296 	    /* We do not stop for these.  */
4297 	    bs->stop = 0;
4298 	  else
4299 	    bpstat_check_breakpoint_conditions (bs, ptid);
4300 
4301 	  if (bs->stop)
4302 	    {
4303 	      ++(b->hit_count);
4304 
4305 	      /* We will stop here.  */
4306 	      if (b->disposition == disp_disable)
4307 		{
4308 		  if (b->enable_state != bp_permanent)
4309 		    b->enable_state = bp_disabled;
4310 		  removed_any = 1;
4311 		}
4312 	      if (b->silent)
4313 		bs->print = 0;
4314 	      bs->commands = b->commands;
4315 	      incref_counted_command_line (bs->commands);
4316 	      bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4317 	      if (bs->commands_left
4318 		  && (strcmp ("silent", bs->commands_left->line) == 0
4319 		      || (xdb_commands
4320 			  && strcmp ("Q",
4321 				     bs->commands_left->line) == 0)))
4322 		{
4323 		  bs->commands_left = bs->commands_left->next;
4324 		  bs->print = 0;
4325 		}
4326 	    }
4327 
4328 	  /* Print nothing for this entry if we dont stop or dont print.  */
4329 	  if (bs->stop == 0 || bs->print == 0)
4330 	    bs->print_it = print_it_noop;
4331     }
4332 
4333   /* If we aren't stopping, the value of some hardware watchpoint may
4334      not have changed, but the intermediate memory locations we are
4335      watching may have.  Don't bother if we're stopping; this will get
4336      done later.  */
4337   need_remove_insert = 0;
4338   if (! bpstat_causes_stop (bs_head))
4339     for (bs = bs_head; bs != NULL; bs = bs->next)
4340       if (!bs->stop
4341 	  && bs->breakpoint_at
4342 	  && is_hardware_watchpoint (bs->breakpoint_at))
4343 	{
4344 	  update_watchpoint (bs->breakpoint_at, 0 /* don't reparse.  */);
4345 	  need_remove_insert = 1;
4346 	}
4347 
4348   if (need_remove_insert)
4349     update_global_location_list (1);
4350   else if (removed_any)
4351     update_global_location_list (0);
4352 
4353   return bs_head;
4354 }
4355 
4356 static void
4357 handle_jit_event (void)
4358 {
4359   struct frame_info *frame;
4360   struct gdbarch *gdbarch;
4361 
4362   /* Switch terminal for any messages produced by
4363      breakpoint_re_set.  */
4364   target_terminal_ours_for_output ();
4365 
4366   frame = get_current_frame ();
4367   gdbarch = get_frame_arch (frame);
4368 
4369   jit_event_handler (gdbarch);
4370 
4371   target_terminal_inferior ();
4372 }
4373 
4374 /* Prepare WHAT final decision for infrun.  */
4375 
4376 /* Decide what infrun needs to do with this bpstat.  */
4377 
4378 struct bpstat_what
4379 bpstat_what (bpstat bs_head)
4380 {
4381   struct bpstat_what retval;
4382   /* We need to defer calling `solib_add', as adding new symbols
4383      resets breakpoints, which in turn deletes breakpoint locations,
4384      and hence may clear unprocessed entries in the BS chain.  */
4385   int shlib_event = 0;
4386   int jit_event = 0;
4387   bpstat bs;
4388 
4389   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
4390   retval.call_dummy = STOP_NONE;
4391   retval.is_longjmp = 0;
4392 
4393   for (bs = bs_head; bs != NULL; bs = bs->next)
4394     {
4395       /* Extract this BS's action.  After processing each BS, we check
4396 	 if its action overrides all we've seem so far.  */
4397       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4398       enum bptype bptype;
4399 
4400       if (bs->breakpoint_at == NULL)
4401 	{
4402 	  /* I suspect this can happen if it was a momentary
4403 	     breakpoint which has since been deleted.  */
4404 	  bptype = bp_none;
4405 	}
4406       else if (bs->breakpoint_at == NULL)
4407 	bptype = bp_none;
4408       else
4409 	bptype = bs->breakpoint_at->type;
4410 
4411       switch (bptype)
4412 	{
4413 	case bp_none:
4414 	  break;
4415 	case bp_breakpoint:
4416 	case bp_hardware_breakpoint:
4417 	case bp_until:
4418 	case bp_finish:
4419 	  if (bs->stop)
4420 	    {
4421 	      if (bs->print)
4422 		this_action = BPSTAT_WHAT_STOP_NOISY;
4423 	      else
4424 		this_action = BPSTAT_WHAT_STOP_SILENT;
4425 	    }
4426 	  else
4427 	    this_action = BPSTAT_WHAT_SINGLE;
4428 	  break;
4429 	case bp_watchpoint:
4430 	case bp_hardware_watchpoint:
4431 	case bp_read_watchpoint:
4432 	case bp_access_watchpoint:
4433 	  if (bs->stop)
4434 	    {
4435 	      if (bs->print)
4436 		this_action = BPSTAT_WHAT_STOP_NOISY;
4437 	      else
4438 		this_action = BPSTAT_WHAT_STOP_SILENT;
4439 	    }
4440 	  else
4441 	    {
4442 	      /* There was a watchpoint, but we're not stopping.
4443 		 This requires no further action.  */
4444 	    }
4445 	  break;
4446 	case bp_longjmp:
4447 	case bp_exception:
4448 	  this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
4449 	  retval.is_longjmp = bptype == bp_longjmp;
4450 	  break;
4451 	case bp_longjmp_resume:
4452 	case bp_exception_resume:
4453 	  this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
4454 	  retval.is_longjmp = bptype == bp_longjmp_resume;
4455 	  break;
4456 	case bp_step_resume:
4457 	  if (bs->stop)
4458 	    this_action = BPSTAT_WHAT_STEP_RESUME;
4459 	  else
4460 	    {
4461 	      /* It is for the wrong frame.  */
4462 	      this_action = BPSTAT_WHAT_SINGLE;
4463 	    }
4464 	  break;
4465 	case bp_watchpoint_scope:
4466 	case bp_thread_event:
4467 	case bp_overlay_event:
4468 	case bp_longjmp_master:
4469 	case bp_std_terminate_master:
4470 	case bp_exception_master:
4471 	  this_action = BPSTAT_WHAT_SINGLE;
4472 	  break;
4473 	case bp_catchpoint:
4474 	  if (bs->stop)
4475 	    {
4476 	      if (bs->print)
4477 		this_action = BPSTAT_WHAT_STOP_NOISY;
4478 	      else
4479 		this_action = BPSTAT_WHAT_STOP_SILENT;
4480 	    }
4481 	  else
4482 	    {
4483 	      /* There was a catchpoint, but we're not stopping.
4484 		 This requires no further action.  */
4485 	    }
4486 	  break;
4487 	case bp_shlib_event:
4488 	  shlib_event = 1;
4489 
4490 	  /* If requested, stop when the dynamic linker notifies GDB
4491 	     of events.  This allows the user to get control and place
4492 	     breakpoints in initializer routines for dynamically
4493 	     loaded objects (among other things).  */
4494 	  if (stop_on_solib_events)
4495 	    this_action = BPSTAT_WHAT_STOP_NOISY;
4496 	  else
4497 	    this_action = BPSTAT_WHAT_SINGLE;
4498 	  break;
4499 	case bp_jit_event:
4500 	  jit_event = 1;
4501 	  this_action = BPSTAT_WHAT_SINGLE;
4502 	  break;
4503 	case bp_call_dummy:
4504 	  /* Make sure the action is stop (silent or noisy),
4505 	     so infrun.c pops the dummy frame.  */
4506 	  retval.call_dummy = STOP_STACK_DUMMY;
4507 	  this_action = BPSTAT_WHAT_STOP_SILENT;
4508 	  break;
4509 	case bp_std_terminate:
4510 	  /* Make sure the action is stop (silent or noisy),
4511 	     so infrun.c pops the dummy frame.  */
4512 	  retval.call_dummy = STOP_STD_TERMINATE;
4513 	  this_action = BPSTAT_WHAT_STOP_SILENT;
4514 	  break;
4515 	case bp_tracepoint:
4516 	case bp_fast_tracepoint:
4517 	case bp_static_tracepoint:
4518 	  /* Tracepoint hits should not be reported back to GDB, and
4519 	     if one got through somehow, it should have been filtered
4520 	     out already.  */
4521 	  internal_error (__FILE__, __LINE__,
4522 			  _("bpstat_what: tracepoint encountered"));
4523 	  break;
4524 	case bp_gnu_ifunc_resolver:
4525 	  /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
4526 	  this_action = BPSTAT_WHAT_SINGLE;
4527 	  break;
4528 	case bp_gnu_ifunc_resolver_return:
4529 	  /* The breakpoint will be removed, execution will restart from the
4530 	     PC of the former breakpoint.  */
4531 	  this_action = BPSTAT_WHAT_KEEP_CHECKING;
4532 	  break;
4533 	default:
4534 	  internal_error (__FILE__, __LINE__,
4535 			  _("bpstat_what: unhandled bptype %d"), (int) bptype);
4536 	}
4537 
4538       retval.main_action = max (retval.main_action, this_action);
4539     }
4540 
4541   /* These operations may affect the bs->breakpoint_at state so they are
4542      delayed after MAIN_ACTION is decided above.  */
4543 
4544   if (shlib_event)
4545     {
4546       if (debug_infrun)
4547 	fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4548 
4549       /* Check for any newly added shared libraries if we're supposed
4550 	 to be adding them automatically.  */
4551 
4552       /* Switch terminal for any messages produced by
4553 	 breakpoint_re_set.  */
4554       target_terminal_ours_for_output ();
4555 
4556 #ifdef SOLIB_ADD
4557       SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4558 #else
4559       solib_add (NULL, 0, &current_target, auto_solib_add);
4560 #endif
4561 
4562       target_terminal_inferior ();
4563     }
4564 
4565   if (jit_event)
4566     {
4567       if (debug_infrun)
4568 	fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4569 
4570       handle_jit_event ();
4571     }
4572 
4573   for (bs = bs_head; bs != NULL; bs = bs->next)
4574     {
4575       struct breakpoint *b = bs->breakpoint_at;
4576 
4577       if (b == NULL)
4578 	continue;
4579       switch (b->type)
4580 	{
4581 	case bp_gnu_ifunc_resolver:
4582 	  gnu_ifunc_resolver_stop (b);
4583 	  break;
4584 	case bp_gnu_ifunc_resolver_return:
4585 	  gnu_ifunc_resolver_return_stop (b);
4586 	  break;
4587 	}
4588     }
4589 
4590   return retval;
4591 }
4592 
4593 /* Nonzero if we should step constantly (e.g. watchpoints on machines
4594    without hardware support).  This isn't related to a specific bpstat,
4595    just to things like whether watchpoints are set.  */
4596 
4597 int
4598 bpstat_should_step (void)
4599 {
4600   struct breakpoint *b;
4601 
4602   ALL_BREAKPOINTS (b)
4603     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4604       return 1;
4605   return 0;
4606 }
4607 
4608 int
4609 bpstat_causes_stop (bpstat bs)
4610 {
4611   for (; bs != NULL; bs = bs->next)
4612     if (bs->stop)
4613       return 1;
4614 
4615   return 0;
4616 }
4617 
4618 
4619 
4620 /* Compute a string of spaces suitable to indent the next line
4621    so it starts at the position corresponding to the table column
4622    named COL_NAME in the currently active table of UIOUT.  */
4623 
4624 static char *
4625 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4626 {
4627   static char wrap_indent[80];
4628   int i, total_width, width, align;
4629   char *text;
4630 
4631   total_width = 0;
4632   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4633     {
4634       if (strcmp (text, col_name) == 0)
4635 	{
4636 	  gdb_assert (total_width < sizeof wrap_indent);
4637 	  memset (wrap_indent, ' ', total_width);
4638 	  wrap_indent[total_width] = 0;
4639 
4640 	  return wrap_indent;
4641 	}
4642 
4643       total_width += width + 1;
4644     }
4645 
4646   return NULL;
4647 }
4648 
4649 /* Print the LOC location out of the list of B->LOC locations.  */
4650 
4651 static void
4652 print_breakpoint_location (struct breakpoint *b,
4653 			   struct bp_location *loc)
4654 {
4655   struct cleanup *old_chain = save_current_program_space ();
4656 
4657   if (loc != NULL && loc->shlib_disabled)
4658     loc = NULL;
4659 
4660   if (loc != NULL)
4661     set_current_program_space (loc->pspace);
4662 
4663   if (b->display_canonical)
4664     ui_out_field_string (uiout, "what", b->addr_string);
4665   else if (b->source_file && loc)
4666     {
4667       struct symbol *sym
4668 	= find_pc_sect_function (loc->address, loc->section);
4669       if (sym)
4670 	{
4671 	  ui_out_text (uiout, "in ");
4672 	  ui_out_field_string (uiout, "func",
4673 			       SYMBOL_PRINT_NAME (sym));
4674 	  ui_out_text (uiout, " ");
4675 	  ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4676 	  ui_out_text (uiout, "at ");
4677 	}
4678       ui_out_field_string (uiout, "file", b->source_file);
4679       ui_out_text (uiout, ":");
4680 
4681       if (ui_out_is_mi_like_p (uiout))
4682 	{
4683 	  struct symtab_and_line sal = find_pc_line (loc->address, 0);
4684 	  char *fullname = symtab_to_fullname (sal.symtab);
4685 
4686 	  if (fullname)
4687 	    ui_out_field_string (uiout, "fullname", fullname);
4688 	}
4689 
4690       ui_out_field_int (uiout, "line", b->line_number);
4691     }
4692   else if (loc)
4693     {
4694       struct ui_stream *stb = ui_out_stream_new (uiout);
4695       struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4696 
4697       print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4698 			      demangle, "");
4699       ui_out_field_stream (uiout, "at", stb);
4700 
4701       do_cleanups (stb_chain);
4702     }
4703   else
4704     ui_out_field_string (uiout, "pending", b->addr_string);
4705 
4706   do_cleanups (old_chain);
4707 }
4708 
4709 static const char *
4710 bptype_string (enum bptype type)
4711 {
4712   struct ep_type_description
4713     {
4714       enum bptype type;
4715       char *description;
4716     };
4717   static struct ep_type_description bptypes[] =
4718   {
4719     {bp_none, "?deleted?"},
4720     {bp_breakpoint, "breakpoint"},
4721     {bp_hardware_breakpoint, "hw breakpoint"},
4722     {bp_until, "until"},
4723     {bp_finish, "finish"},
4724     {bp_watchpoint, "watchpoint"},
4725     {bp_hardware_watchpoint, "hw watchpoint"},
4726     {bp_read_watchpoint, "read watchpoint"},
4727     {bp_access_watchpoint, "acc watchpoint"},
4728     {bp_longjmp, "longjmp"},
4729     {bp_longjmp_resume, "longjmp resume"},
4730     {bp_exception, "exception"},
4731     {bp_exception_resume, "exception resume"},
4732     {bp_step_resume, "step resume"},
4733     {bp_watchpoint_scope, "watchpoint scope"},
4734     {bp_call_dummy, "call dummy"},
4735     {bp_std_terminate, "std::terminate"},
4736     {bp_shlib_event, "shlib events"},
4737     {bp_thread_event, "thread events"},
4738     {bp_overlay_event, "overlay events"},
4739     {bp_longjmp_master, "longjmp master"},
4740     {bp_std_terminate_master, "std::terminate master"},
4741     {bp_exception_master, "exception master"},
4742     {bp_catchpoint, "catchpoint"},
4743     {bp_tracepoint, "tracepoint"},
4744     {bp_fast_tracepoint, "fast tracepoint"},
4745     {bp_static_tracepoint, "static tracepoint"},
4746     {bp_jit_event, "jit events"},
4747     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4748     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
4749   };
4750 
4751   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4752       || ((int) type != bptypes[(int) type].type))
4753     internal_error (__FILE__, __LINE__,
4754 		    _("bptypes table does not describe type #%d."),
4755 		    (int) type);
4756 
4757   return bptypes[(int) type].description;
4758 }
4759 
4760 /* Print B to gdb_stdout.  */
4761 
4762 static void
4763 print_one_breakpoint_location (struct breakpoint *b,
4764 			       struct bp_location *loc,
4765 			       int loc_number,
4766 			       struct bp_location **last_loc,
4767 			       int allflag)
4768 {
4769   struct command_line *l;
4770   static char bpenables[] = "nynny";
4771   struct cleanup *bkpt_chain;
4772 
4773   int header_of_multiple = 0;
4774   int part_of_multiple = (loc != NULL);
4775   struct value_print_options opts;
4776 
4777   get_user_print_options (&opts);
4778 
4779   gdb_assert (!loc || loc_number != 0);
4780   /* See comment in print_one_breakpoint concerning treatment of
4781      breakpoints with single disabled location.  */
4782   if (loc == NULL
4783       && (b->loc != NULL
4784 	  && (b->loc->next != NULL || !b->loc->enabled)))
4785     header_of_multiple = 1;
4786   if (loc == NULL)
4787     loc = b->loc;
4788 
4789   annotate_record ();
4790   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4791 
4792   /* 1 */
4793   annotate_field (0);
4794   if (part_of_multiple)
4795     {
4796       char *formatted;
4797       formatted = xstrprintf ("%d.%d", b->number, loc_number);
4798       ui_out_field_string (uiout, "number", formatted);
4799       xfree (formatted);
4800     }
4801   else
4802     {
4803       ui_out_field_int (uiout, "number", b->number);
4804     }
4805 
4806   /* 2 */
4807   annotate_field (1);
4808   if (part_of_multiple)
4809     ui_out_field_skip (uiout, "type");
4810   else
4811     ui_out_field_string (uiout, "type", bptype_string (b->type));
4812 
4813   /* 3 */
4814   annotate_field (2);
4815   if (part_of_multiple)
4816     ui_out_field_skip (uiout, "disp");
4817   else
4818     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
4819 
4820 
4821   /* 4 */
4822   annotate_field (3);
4823   if (part_of_multiple)
4824     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
4825   else
4826     ui_out_field_fmt (uiout, "enabled", "%c",
4827 		      bpenables[(int) b->enable_state]);
4828   ui_out_spaces (uiout, 2);
4829 
4830 
4831   /* 5 and 6 */
4832   if (b->ops != NULL && b->ops->print_one != NULL)
4833     {
4834       /* Although the print_one can possibly print all locations,
4835 	 calling it here is not likely to get any nice result.  So,
4836 	 make sure there's just one location.  */
4837       gdb_assert (b->loc == NULL || b->loc->next == NULL);
4838       b->ops->print_one (b, last_loc);
4839     }
4840   else
4841     switch (b->type)
4842       {
4843       case bp_none:
4844 	internal_error (__FILE__, __LINE__,
4845 			_("print_one_breakpoint: bp_none encountered\n"));
4846 	break;
4847 
4848       case bp_watchpoint:
4849       case bp_hardware_watchpoint:
4850       case bp_read_watchpoint:
4851       case bp_access_watchpoint:
4852 	/* Field 4, the address, is omitted (which makes the columns
4853 	   not line up too nicely with the headers, but the effect
4854 	   is relatively readable).  */
4855 	if (opts.addressprint)
4856 	  ui_out_field_skip (uiout, "addr");
4857 	annotate_field (5);
4858 	ui_out_field_string (uiout, "what", b->exp_string);
4859 	break;
4860 
4861       case bp_breakpoint:
4862       case bp_hardware_breakpoint:
4863       case bp_until:
4864       case bp_finish:
4865       case bp_longjmp:
4866       case bp_longjmp_resume:
4867       case bp_exception:
4868       case bp_exception_resume:
4869       case bp_step_resume:
4870       case bp_watchpoint_scope:
4871       case bp_call_dummy:
4872       case bp_std_terminate:
4873       case bp_shlib_event:
4874       case bp_thread_event:
4875       case bp_overlay_event:
4876       case bp_longjmp_master:
4877       case bp_std_terminate_master:
4878       case bp_exception_master:
4879       case bp_tracepoint:
4880       case bp_fast_tracepoint:
4881       case bp_static_tracepoint:
4882       case bp_jit_event:
4883       case bp_gnu_ifunc_resolver:
4884       case bp_gnu_ifunc_resolver_return:
4885 	if (opts.addressprint)
4886 	  {
4887 	    annotate_field (4);
4888 	    if (header_of_multiple)
4889 	      ui_out_field_string (uiout, "addr", "<MULTIPLE>");
4890 	    else if (b->loc == NULL || loc->shlib_disabled)
4891 	      ui_out_field_string (uiout, "addr", "<PENDING>");
4892 	    else
4893 	      ui_out_field_core_addr (uiout, "addr",
4894 				      loc->gdbarch, loc->address);
4895 	  }
4896 	annotate_field (5);
4897 	if (!header_of_multiple)
4898 	  print_breakpoint_location (b, loc);
4899 	if (b->loc)
4900 	  *last_loc = b->loc;
4901 	break;
4902       }
4903 
4904 
4905   /* For backward compatibility, don't display inferiors unless there
4906      are several.  */
4907   if (loc != NULL
4908       && !header_of_multiple
4909       && (allflag
4910 	  || (!gdbarch_has_global_breakpoints (target_gdbarch)
4911 	      && (number_of_program_spaces () > 1
4912 		  || number_of_inferiors () > 1)
4913 	      /* LOC is for existing B, it cannot be in
4914 		 moribund_locations and thus having NULL OWNER.  */
4915 	      && loc->owner->type != bp_catchpoint)))
4916     {
4917       struct inferior *inf;
4918       int first = 1;
4919 
4920       for (inf = inferior_list; inf != NULL; inf = inf->next)
4921 	{
4922 	  if (inf->pspace == loc->pspace)
4923 	    {
4924 	      if (first)
4925 		{
4926 		  first = 0;
4927 		  ui_out_text (uiout, " inf ");
4928 		}
4929 	      else
4930 		ui_out_text (uiout, ", ");
4931 	      ui_out_text (uiout, plongest (inf->num));
4932 	    }
4933 	}
4934     }
4935 
4936   if (!part_of_multiple)
4937     {
4938       if (b->thread != -1)
4939 	{
4940 	  /* FIXME: This seems to be redundant and lost here; see the
4941 	     "stop only in" line a little further down.  */
4942 	  ui_out_text (uiout, " thread ");
4943 	  ui_out_field_int (uiout, "thread", b->thread);
4944 	}
4945       else if (b->task != 0)
4946 	{
4947 	  ui_out_text (uiout, " task ");
4948 	  ui_out_field_int (uiout, "task", b->task);
4949 	}
4950     }
4951 
4952   ui_out_text (uiout, "\n");
4953 
4954   if (!part_of_multiple && b->ops && b->ops->print_one_detail)
4955     b->ops->print_one_detail (b, uiout);
4956 
4957   if (!part_of_multiple && b->static_trace_marker_id)
4958     {
4959       gdb_assert (b->type == bp_static_tracepoint);
4960 
4961       ui_out_text (uiout, "\tmarker id is ");
4962       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4963 			   b->static_trace_marker_id);
4964       ui_out_text (uiout, "\n");
4965     }
4966 
4967   if (part_of_multiple && frame_id_p (b->frame_id))
4968     {
4969       annotate_field (6);
4970       ui_out_text (uiout, "\tstop only in stack frame at ");
4971       /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4972          the frame ID.  */
4973       ui_out_field_core_addr (uiout, "frame",
4974 			      b->gdbarch, b->frame_id.stack_addr);
4975       ui_out_text (uiout, "\n");
4976     }
4977 
4978   if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
4979     {
4980       /* We do not print the condition for Ada exception catchpoints
4981          because the condition is an internal implementation detail
4982          that we do not want to expose to the user.  */
4983       annotate_field (7);
4984       if (is_tracepoint (b))
4985 	ui_out_text (uiout, "\ttrace only if ");
4986       else
4987 	ui_out_text (uiout, "\tstop only if ");
4988       ui_out_field_string (uiout, "cond", b->cond_string);
4989       ui_out_text (uiout, "\n");
4990     }
4991 
4992   if (!part_of_multiple && b->thread != -1)
4993     {
4994       /* FIXME should make an annotation for this.  */
4995       ui_out_text (uiout, "\tstop only in thread ");
4996       ui_out_field_int (uiout, "thread", b->thread);
4997       ui_out_text (uiout, "\n");
4998     }
4999 
5000   if (!part_of_multiple && b->hit_count)
5001     {
5002       /* FIXME should make an annotation for this.  */
5003       if (ep_is_catchpoint (b))
5004 	ui_out_text (uiout, "\tcatchpoint");
5005       else
5006 	ui_out_text (uiout, "\tbreakpoint");
5007       ui_out_text (uiout, " already hit ");
5008       ui_out_field_int (uiout, "times", b->hit_count);
5009       if (b->hit_count == 1)
5010 	ui_out_text (uiout, " time\n");
5011       else
5012 	ui_out_text (uiout, " times\n");
5013     }
5014 
5015   /* Output the count also if it is zero, but only if this is mi.
5016      FIXME: Should have a better test for this.  */
5017   if (ui_out_is_mi_like_p (uiout))
5018     if (!part_of_multiple && b->hit_count == 0)
5019       ui_out_field_int (uiout, "times", b->hit_count);
5020 
5021   if (!part_of_multiple && b->ignore_count)
5022     {
5023       annotate_field (8);
5024       ui_out_text (uiout, "\tignore next ");
5025       ui_out_field_int (uiout, "ignore", b->ignore_count);
5026       ui_out_text (uiout, " hits\n");
5027     }
5028 
5029   l = b->commands ? b->commands->commands : NULL;
5030   if (!part_of_multiple && l)
5031     {
5032       struct cleanup *script_chain;
5033 
5034       annotate_field (9);
5035       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
5036       print_command_lines (uiout, l, 4);
5037       do_cleanups (script_chain);
5038     }
5039 
5040   if (!part_of_multiple && b->pass_count)
5041     {
5042       annotate_field (10);
5043       ui_out_text (uiout, "\tpass count ");
5044       ui_out_field_int (uiout, "pass", b->pass_count);
5045       ui_out_text (uiout, " \n");
5046     }
5047 
5048   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5049     {
5050       if (b->addr_string)
5051 	ui_out_field_string (uiout, "original-location", b->addr_string);
5052       else if (b->exp_string)
5053 	ui_out_field_string (uiout, "original-location", b->exp_string);
5054     }
5055 
5056   do_cleanups (bkpt_chain);
5057 }
5058 
5059 static void
5060 print_one_breakpoint (struct breakpoint *b,
5061 		      struct bp_location **last_loc,
5062 		      int allflag)
5063 {
5064   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
5065 
5066   /* If this breakpoint has custom print function,
5067      it's already printed.  Otherwise, print individual
5068      locations, if any.  */
5069   if (b->ops == NULL || b->ops->print_one == NULL)
5070     {
5071       /* If breakpoint has a single location that is disabled, we
5072 	 print it as if it had several locations, since otherwise it's
5073 	 hard to represent "breakpoint enabled, location disabled"
5074 	 situation.
5075 
5076 	 Note that while hardware watchpoints have several locations
5077 	 internally, that's not a property exposed to user.  */
5078       if (b->loc
5079 	  && !is_hardware_watchpoint (b)
5080 	  && (b->loc->next || !b->loc->enabled)
5081 	  && !ui_out_is_mi_like_p (uiout))
5082 	{
5083 	  struct bp_location *loc;
5084 	  int n = 1;
5085 	  for (loc = b->loc; loc; loc = loc->next, ++n)
5086 	    print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5087 	}
5088     }
5089 }
5090 
5091 static int
5092 breakpoint_address_bits (struct breakpoint *b)
5093 {
5094   int print_address_bits = 0;
5095   struct bp_location *loc;
5096 
5097   for (loc = b->loc; loc; loc = loc->next)
5098     {
5099       int addr_bit;
5100 
5101       /* Software watchpoints that aren't watching memory don't have
5102 	 an address to print.  */
5103       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5104 	continue;
5105 
5106       addr_bit = gdbarch_addr_bit (loc->gdbarch);
5107       if (addr_bit > print_address_bits)
5108 	print_address_bits = addr_bit;
5109     }
5110 
5111   return print_address_bits;
5112 }
5113 
5114 struct captured_breakpoint_query_args
5115   {
5116     int bnum;
5117   };
5118 
5119 static int
5120 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5121 {
5122   struct captured_breakpoint_query_args *args = data;
5123   struct breakpoint *b;
5124   struct bp_location *dummy_loc = NULL;
5125 
5126   ALL_BREAKPOINTS (b)
5127     {
5128       if (args->bnum == b->number)
5129 	{
5130 	  print_one_breakpoint (b, &dummy_loc, 0);
5131 	  return GDB_RC_OK;
5132 	}
5133     }
5134   return GDB_RC_NONE;
5135 }
5136 
5137 enum gdb_rc
5138 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5139 		      char **error_message)
5140 {
5141   struct captured_breakpoint_query_args args;
5142 
5143   args.bnum = bnum;
5144   /* For the moment we don't trust print_one_breakpoint() to not throw
5145      an error.  */
5146   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5147 				 error_message, RETURN_MASK_ALL) < 0)
5148     return GDB_RC_FAIL;
5149   else
5150     return GDB_RC_OK;
5151 }
5152 
5153 /* Return non-zero if B is user settable (breakpoints, watchpoints,
5154    catchpoints, et.al.).  */
5155 
5156 static int
5157 user_settable_breakpoint (const struct breakpoint *b)
5158 {
5159   return (b->type == bp_breakpoint
5160 	  || b->type == bp_catchpoint
5161 	  || b->type == bp_hardware_breakpoint
5162 	  || is_tracepoint (b)
5163 	  || is_watchpoint (b)
5164 	  || b->type == bp_gnu_ifunc_resolver);
5165 }
5166 
5167 /* Return true if this breakpoint was set by the user, false if it is
5168    internal or momentary.  */
5169 
5170 int
5171 user_breakpoint_p (struct breakpoint *b)
5172 {
5173   return user_settable_breakpoint (b) && b->number > 0;
5174 }
5175 
5176 /* Print information on user settable breakpoint (watchpoint, etc)
5177    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
5178    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
5179    FILTER is non-NULL, call it on each breakpoint and only include the
5180    ones for which it returns non-zero.  Return the total number of
5181    breakpoints listed.  */
5182 
5183 static int
5184 breakpoint_1 (char *args, int allflag,
5185 	      int (*filter) (const struct breakpoint *))
5186 {
5187   struct breakpoint *b;
5188   struct bp_location *last_loc = NULL;
5189   int nr_printable_breakpoints;
5190   struct cleanup *bkpttbl_chain;
5191   struct value_print_options opts;
5192   int print_address_bits = 0;
5193   int print_type_col_width = 14;
5194 
5195   get_user_print_options (&opts);
5196 
5197   /* Compute the number of rows in the table, as well as the size
5198      required for address fields.  */
5199   nr_printable_breakpoints = 0;
5200   ALL_BREAKPOINTS (b)
5201     {
5202       /* If we have a filter, only list the breakpoints it accepts.  */
5203       if (filter && !filter (b))
5204 	continue;
5205 
5206       /* If we have an "args" string, it is a list of breakpoints to
5207 	 accept.  Skip the others.  */
5208       if (args != NULL && *args != '\0')
5209 	{
5210 	  if (allflag && parse_and_eval_long (args) != b->number)
5211 	    continue;
5212 	  if (!allflag && !number_is_in_list (args, b->number))
5213 	    continue;
5214 	}
5215 
5216       if (allflag || user_breakpoint_p (b))
5217 	{
5218 	  int addr_bit, type_len;
5219 
5220 	  addr_bit = breakpoint_address_bits (b);
5221 	  if (addr_bit > print_address_bits)
5222 	    print_address_bits = addr_bit;
5223 
5224 	  type_len = strlen (bptype_string (b->type));
5225 	  if (type_len > print_type_col_width)
5226 	    print_type_col_width = type_len;
5227 
5228 	  nr_printable_breakpoints++;
5229 	}
5230     }
5231 
5232   if (opts.addressprint)
5233     bkpttbl_chain
5234       = make_cleanup_ui_out_table_begin_end (uiout, 6,
5235 					     nr_printable_breakpoints,
5236                                              "BreakpointTable");
5237   else
5238     bkpttbl_chain
5239       = make_cleanup_ui_out_table_begin_end (uiout, 5,
5240 					     nr_printable_breakpoints,
5241                                              "BreakpointTable");
5242 
5243   if (nr_printable_breakpoints > 0)
5244     annotate_breakpoints_headers ();
5245   if (nr_printable_breakpoints > 0)
5246     annotate_field (0);
5247   ui_out_table_header (uiout, 7, ui_left, "number", "Num");	/* 1 */
5248   if (nr_printable_breakpoints > 0)
5249     annotate_field (1);
5250   ui_out_table_header (uiout, print_type_col_width, ui_left,
5251 		       "type", "Type");				/* 2 */
5252   if (nr_printable_breakpoints > 0)
5253     annotate_field (2);
5254   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");	/* 3 */
5255   if (nr_printable_breakpoints > 0)
5256     annotate_field (3);
5257   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");	/* 4 */
5258   if (opts.addressprint)
5259     {
5260       if (nr_printable_breakpoints > 0)
5261 	annotate_field (4);
5262       if (print_address_bits <= 32)
5263 	ui_out_table_header (uiout, 10, ui_left,
5264 			     "addr", "Address");		/* 5 */
5265       else
5266 	ui_out_table_header (uiout, 18, ui_left,
5267 			     "addr", "Address");		/* 5 */
5268     }
5269   if (nr_printable_breakpoints > 0)
5270     annotate_field (5);
5271   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");	/* 6 */
5272   ui_out_table_body (uiout);
5273   if (nr_printable_breakpoints > 0)
5274     annotate_breakpoints_table ();
5275 
5276   ALL_BREAKPOINTS (b)
5277     {
5278       QUIT;
5279       /* If we have a filter, only list the breakpoints it accepts.  */
5280       if (filter && !filter (b))
5281 	continue;
5282 
5283       /* If we have an "args" string, it is a list of breakpoints to
5284 	 accept.  Skip the others.  */
5285 
5286       if (args != NULL && *args != '\0')
5287 	{
5288 	  if (allflag)	/* maintenance info breakpoint */
5289 	    {
5290 	      if (parse_and_eval_long (args) != b->number)
5291 		continue;
5292 	    }
5293 	  else		/* all others */
5294 	    {
5295 	      if (!number_is_in_list (args, b->number))
5296 		continue;
5297 	    }
5298 	}
5299       /* We only print out user settable breakpoints unless the
5300 	 allflag is set.  */
5301       if (allflag || user_breakpoint_p (b))
5302 	print_one_breakpoint (b, &last_loc, allflag);
5303     }
5304 
5305   do_cleanups (bkpttbl_chain);
5306 
5307   if (nr_printable_breakpoints == 0)
5308     {
5309       /* If there's a filter, let the caller decide how to report
5310 	 empty list.  */
5311       if (!filter)
5312 	{
5313 	  if (args == NULL || *args == '\0')
5314 	    ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5315 	  else
5316 	    ui_out_message (uiout, 0,
5317 			    "No breakpoint or watchpoint matching '%s'.\n",
5318 			    args);
5319 	}
5320     }
5321   else
5322     {
5323       if (last_loc && !server_command)
5324 	set_next_address (last_loc->gdbarch, last_loc->address);
5325     }
5326 
5327   /* FIXME?  Should this be moved up so that it is only called when
5328      there have been breakpoints? */
5329   annotate_breakpoints_table_end ();
5330 
5331   return nr_printable_breakpoints;
5332 }
5333 
5334 /* Display the value of default-collect in a way that is generally
5335    compatible with the breakpoint list.  */
5336 
5337 static void
5338 default_collect_info (void)
5339 {
5340   /* If it has no value (which is frequently the case), say nothing; a
5341      message like "No default-collect." gets in user's face when it's
5342      not wanted.  */
5343   if (!*default_collect)
5344     return;
5345 
5346   /* The following phrase lines up nicely with per-tracepoint collect
5347      actions.  */
5348   ui_out_text (uiout, "default collect ");
5349   ui_out_field_string (uiout, "default-collect", default_collect);
5350   ui_out_text (uiout, " \n");
5351 }
5352 
5353 static void
5354 breakpoints_info (char *args, int from_tty)
5355 {
5356   breakpoint_1 (args, 0, NULL);
5357 
5358   default_collect_info ();
5359 }
5360 
5361 static void
5362 watchpoints_info (char *args, int from_tty)
5363 {
5364   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
5365 
5366   if (num_printed == 0)
5367     {
5368       if (args == NULL || *args == '\0')
5369 	ui_out_message (uiout, 0, "No watchpoints.\n");
5370       else
5371 	ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
5372     }
5373 }
5374 
5375 static void
5376 maintenance_info_breakpoints (char *args, int from_tty)
5377 {
5378   breakpoint_1 (args, 1, NULL);
5379 
5380   default_collect_info ();
5381 }
5382 
5383 static int
5384 breakpoint_has_pc (struct breakpoint *b,
5385 		   struct program_space *pspace,
5386 		   CORE_ADDR pc, struct obj_section *section)
5387 {
5388   struct bp_location *bl = b->loc;
5389 
5390   for (; bl; bl = bl->next)
5391     {
5392       if (bl->pspace == pspace
5393 	  && bl->address == pc
5394 	  && (!overlay_debugging || bl->section == section))
5395 	return 1;
5396     }
5397   return 0;
5398 }
5399 
5400 /* Print a message describing any breakpoints set at PC.  This
5401    concerns with logical breakpoints, so we match program spaces, not
5402    address spaces.  */
5403 
5404 static void
5405 describe_other_breakpoints (struct gdbarch *gdbarch,
5406 			    struct program_space *pspace, CORE_ADDR pc,
5407 			    struct obj_section *section, int thread)
5408 {
5409   int others = 0;
5410   struct breakpoint *b;
5411 
5412   ALL_BREAKPOINTS (b)
5413     others += breakpoint_has_pc (b, pspace, pc, section);
5414   if (others > 0)
5415     {
5416       if (others == 1)
5417 	printf_filtered (_("Note: breakpoint "));
5418       else /* if (others == ???) */
5419 	printf_filtered (_("Note: breakpoints "));
5420       ALL_BREAKPOINTS (b)
5421 	if (breakpoint_has_pc (b, pspace, pc, section))
5422 	  {
5423 	    others--;
5424 	    printf_filtered ("%d", b->number);
5425 	    if (b->thread == -1 && thread != -1)
5426 	      printf_filtered (" (all threads)");
5427 	    else if (b->thread != -1)
5428 	      printf_filtered (" (thread %d)", b->thread);
5429 	    printf_filtered ("%s%s ",
5430 			     ((b->enable_state == bp_disabled
5431 			       || b->enable_state == bp_call_disabled
5432 			       || b->enable_state == bp_startup_disabled)
5433 			      ? " (disabled)"
5434 			      : b->enable_state == bp_permanent
5435 			      ? " (permanent)"
5436 			      : ""),
5437 			     (others > 1) ? ","
5438 			     : ((others == 1) ? " and" : ""));
5439 	  }
5440       printf_filtered (_("also set at pc "));
5441       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
5442       printf_filtered (".\n");
5443     }
5444 }
5445 
5446 /* Set the default place to put a breakpoint
5447    for the `break' command with no arguments.  */
5448 
5449 void
5450 set_default_breakpoint (int valid, struct program_space *pspace,
5451 			CORE_ADDR addr, struct symtab *symtab,
5452 			int line)
5453 {
5454   default_breakpoint_valid = valid;
5455   default_breakpoint_pspace = pspace;
5456   default_breakpoint_address = addr;
5457   default_breakpoint_symtab = symtab;
5458   default_breakpoint_line = line;
5459 }
5460 
5461 /* Return true iff it is meaningful to use the address member of
5462    BPT.  For some breakpoint types, the address member is irrelevant
5463    and it makes no sense to attempt to compare it to other addresses
5464    (or use it for any other purpose either).
5465 
5466    More specifically, each of the following breakpoint types will
5467    always have a zero valued address and we don't want to mark
5468    breakpoints of any of these types to be a duplicate of an actual
5469    breakpoint at address zero:
5470 
5471       bp_watchpoint
5472       bp_catchpoint
5473 
5474 */
5475 
5476 static int
5477 breakpoint_address_is_meaningful (struct breakpoint *bpt)
5478 {
5479   enum bptype type = bpt->type;
5480 
5481   return (type != bp_watchpoint && type != bp_catchpoint);
5482 }
5483 
5484 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5485    true if LOC1 and LOC2 represent the same watchpoint location.  */
5486 
5487 static int
5488 watchpoint_locations_match (struct bp_location *loc1,
5489 			    struct bp_location *loc2)
5490 {
5491   /* Both of them must not be in moribund_locations.  */
5492   gdb_assert (loc1->owner != NULL);
5493   gdb_assert (loc2->owner != NULL);
5494 
5495   /* If the target can evaluate the condition expression in hardware,
5496      then we we need to insert both watchpoints even if they are at
5497      the same place.  Otherwise the watchpoint will only trigger when
5498      the condition of whichever watchpoint was inserted evaluates to
5499      true, not giving a chance for GDB to check the condition of the
5500      other watchpoint.  */
5501   if ((loc1->owner->cond_exp
5502        && target_can_accel_watchpoint_condition (loc1->address,
5503 						 loc1->length,
5504 						 loc1->watchpoint_type,
5505 						 loc1->owner->cond_exp))
5506       || (loc2->owner->cond_exp
5507 	  && target_can_accel_watchpoint_condition (loc2->address,
5508 						    loc2->length,
5509 						    loc2->watchpoint_type,
5510 						    loc2->owner->cond_exp)))
5511     return 0;
5512 
5513   /* Note that this checks the owner's type, not the location's.  In
5514      case the target does not support read watchpoints, but does
5515      support access watchpoints, we'll have bp_read_watchpoint
5516      watchpoints with hw_access locations.  Those should be considered
5517      duplicates of hw_read locations.  The hw_read locations will
5518      become hw_access locations later.  */
5519   return (loc1->owner->type == loc2->owner->type
5520 	  && loc1->pspace->aspace == loc2->pspace->aspace
5521 	  && loc1->address == loc2->address
5522 	  && loc1->length == loc2->length);
5523 }
5524 
5525 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5526    same breakpoint location.  In most targets, this can only be true
5527    if ASPACE1 matches ASPACE2.  On targets that have global
5528    breakpoints, the address space doesn't really matter.  */
5529 
5530 static int
5531 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5532 			  struct address_space *aspace2, CORE_ADDR addr2)
5533 {
5534   return ((gdbarch_has_global_breakpoints (target_gdbarch)
5535 	   || aspace1 == aspace2)
5536 	  && addr1 == addr2);
5537 }
5538 
5539 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5540    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
5541    matches ASPACE2.  On targets that have global breakpoints, the address
5542    space doesn't really matter.  */
5543 
5544 static int
5545 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5546 				int len1, struct address_space *aspace2,
5547 				CORE_ADDR addr2)
5548 {
5549   return ((gdbarch_has_global_breakpoints (target_gdbarch)
5550 	   || aspace1 == aspace2)
5551 	  && addr2 >= addr1 && addr2 < addr1 + len1);
5552 }
5553 
5554 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
5555    a ranged breakpoint.  In most targets, a match happens only if ASPACE
5556    matches the breakpoint's address space.  On targets that have global
5557    breakpoints, the address space doesn't really matter.  */
5558 
5559 static int
5560 breakpoint_location_address_match (struct bp_location *bl,
5561 				   struct address_space *aspace,
5562 				   CORE_ADDR addr)
5563 {
5564   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5565 				    aspace, addr)
5566 	  || (bl->length
5567 	      && breakpoint_address_match_range (bl->pspace->aspace,
5568 						 bl->address, bl->length,
5569 						 aspace, addr)));
5570 }
5571 
5572 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
5573    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5574    represent the same location.  */
5575 
5576 static int
5577 breakpoint_locations_match (struct bp_location *loc1,
5578 			    struct bp_location *loc2)
5579 {
5580   int hw_point1, hw_point2;
5581 
5582   /* Both of them must not be in moribund_locations.  */
5583   gdb_assert (loc1->owner != NULL);
5584   gdb_assert (loc2->owner != NULL);
5585 
5586   hw_point1 = is_hardware_watchpoint (loc1->owner);
5587   hw_point2 = is_hardware_watchpoint (loc2->owner);
5588 
5589   if (hw_point1 != hw_point2)
5590     return 0;
5591   else if (hw_point1)
5592     return watchpoint_locations_match (loc1, loc2);
5593   else
5594     /* We compare bp_location.length in order to cover ranged breakpoints.  */
5595     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5596 				     loc2->pspace->aspace, loc2->address)
5597 	    && loc1->length == loc2->length);
5598 }
5599 
5600 static void
5601 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5602                                int bnum, int have_bnum)
5603 {
5604   /* The longest string possibly returned by hex_string_custom
5605      is 50 chars.  These must be at least that big for safety.  */
5606   char astr1[64];
5607   char astr2[64];
5608 
5609   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5610   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
5611   if (have_bnum)
5612     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
5613              bnum, astr1, astr2);
5614   else
5615     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
5616 }
5617 
5618 /* Adjust a breakpoint's address to account for architectural
5619    constraints on breakpoint placement.  Return the adjusted address.
5620    Note: Very few targets require this kind of adjustment.  For most
5621    targets, this function is simply the identity function.  */
5622 
5623 static CORE_ADDR
5624 adjust_breakpoint_address (struct gdbarch *gdbarch,
5625 			   CORE_ADDR bpaddr, enum bptype bptype)
5626 {
5627   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
5628     {
5629       /* Very few targets need any kind of breakpoint adjustment.  */
5630       return bpaddr;
5631     }
5632   else if (bptype == bp_watchpoint
5633            || bptype == bp_hardware_watchpoint
5634            || bptype == bp_read_watchpoint
5635            || bptype == bp_access_watchpoint
5636            || bptype == bp_catchpoint)
5637     {
5638       /* Watchpoints and the various bp_catch_* eventpoints should not
5639          have their addresses modified.  */
5640       return bpaddr;
5641     }
5642   else
5643     {
5644       CORE_ADDR adjusted_bpaddr;
5645 
5646       /* Some targets have architectural constraints on the placement
5647          of breakpoint instructions.  Obtain the adjusted address.  */
5648       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
5649 
5650       /* An adjusted breakpoint address can significantly alter
5651          a user's expectations.  Print a warning if an adjustment
5652 	 is required.  */
5653       if (adjusted_bpaddr != bpaddr)
5654 	breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5655 
5656       return adjusted_bpaddr;
5657     }
5658 }
5659 
5660 /* Allocate a struct bp_location.  */
5661 
5662 static struct bp_location *
5663 allocate_bp_location (struct breakpoint *bpt)
5664 {
5665   struct bp_location *loc;
5666 
5667   loc = xmalloc (sizeof (struct bp_location));
5668   memset (loc, 0, sizeof (*loc));
5669 
5670   loc->owner = bpt;
5671   loc->cond = NULL;
5672   loc->shlib_disabled = 0;
5673   loc->enabled = 1;
5674 
5675   switch (bpt->type)
5676     {
5677     case bp_breakpoint:
5678     case bp_until:
5679     case bp_finish:
5680     case bp_longjmp:
5681     case bp_longjmp_resume:
5682     case bp_exception:
5683     case bp_exception_resume:
5684     case bp_step_resume:
5685     case bp_watchpoint_scope:
5686     case bp_call_dummy:
5687     case bp_std_terminate:
5688     case bp_shlib_event:
5689     case bp_thread_event:
5690     case bp_overlay_event:
5691     case bp_jit_event:
5692     case bp_longjmp_master:
5693     case bp_std_terminate_master:
5694     case bp_exception_master:
5695     case bp_gnu_ifunc_resolver:
5696     case bp_gnu_ifunc_resolver_return:
5697       loc->loc_type = bp_loc_software_breakpoint;
5698       break;
5699     case bp_hardware_breakpoint:
5700       loc->loc_type = bp_loc_hardware_breakpoint;
5701       break;
5702     case bp_hardware_watchpoint:
5703     case bp_read_watchpoint:
5704     case bp_access_watchpoint:
5705       loc->loc_type = bp_loc_hardware_watchpoint;
5706       break;
5707     case bp_watchpoint:
5708     case bp_catchpoint:
5709     case bp_tracepoint:
5710     case bp_fast_tracepoint:
5711     case bp_static_tracepoint:
5712       loc->loc_type = bp_loc_other;
5713       break;
5714     default:
5715       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
5716     }
5717 
5718   loc->refc = 1;
5719   return loc;
5720 }
5721 
5722 static void
5723 free_bp_location (struct bp_location *loc)
5724 {
5725   if (loc->cond)
5726     xfree (loc->cond);
5727 
5728   if (loc->function_name)
5729     xfree (loc->function_name);
5730 
5731   xfree (loc);
5732 }
5733 
5734 /* Increment reference count.  */
5735 
5736 static void
5737 incref_bp_location (struct bp_location *bl)
5738 {
5739   ++bl->refc;
5740 }
5741 
5742 /* Decrement reference count.  If the reference count reaches 0,
5743    destroy the bp_location.  Sets *BLP to NULL.  */
5744 
5745 static void
5746 decref_bp_location (struct bp_location **blp)
5747 {
5748   gdb_assert ((*blp)->refc > 0);
5749 
5750   if (--(*blp)->refc == 0)
5751     free_bp_location (*blp);
5752   *blp = NULL;
5753 }
5754 
5755 /* Helper to set_raw_breakpoint below.  Creates a breakpoint that has
5756    type BPTYPE and has no locations as yet.  */
5757 /* This function is used in gdbtk sources and thus can not be made
5758    static.  */
5759 
5760 static struct breakpoint *
5761 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5762 				     enum bptype bptype)
5763 {
5764   struct breakpoint *b, *b1;
5765 
5766   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5767   memset (b, 0, sizeof (*b));
5768 
5769   b->type = bptype;
5770   b->gdbarch = gdbarch;
5771   b->language = current_language->la_language;
5772   b->input_radix = input_radix;
5773   b->thread = -1;
5774   b->enable_state = bp_enabled;
5775   b->next = 0;
5776   b->silent = 0;
5777   b->ignore_count = 0;
5778   b->commands = NULL;
5779   b->frame_id = null_frame_id;
5780   b->forked_inferior_pid = null_ptid;
5781   b->exec_pathname = NULL;
5782   b->syscalls_to_be_caught = NULL;
5783   b->ops = NULL;
5784   b->condition_not_parsed = 0;
5785   b->py_bp_object = NULL;
5786   b->related_breakpoint = b;
5787 
5788   /* Add this breakpoint to the end of the chain so that a list of
5789      breakpoints will come out in order of increasing numbers.  */
5790 
5791   b1 = breakpoint_chain;
5792   if (b1 == 0)
5793     breakpoint_chain = b;
5794   else
5795     {
5796       while (b1->next)
5797 	b1 = b1->next;
5798       b1->next = b;
5799     }
5800   return b;
5801 }
5802 
5803 /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
5804    resolutions should be made as the user specified the location explicitly
5805    enough.  */
5806 
5807 static void
5808 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
5809 {
5810   gdb_assert (loc->owner != NULL);
5811 
5812   if (loc->owner->type == bp_breakpoint
5813       || loc->owner->type == bp_hardware_breakpoint
5814       || is_tracepoint (loc->owner))
5815     {
5816       int is_gnu_ifunc;
5817 
5818       find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5819 					  NULL, NULL, &is_gnu_ifunc);
5820 
5821       if (is_gnu_ifunc && !explicit_loc)
5822 	{
5823 	  struct breakpoint *b = loc->owner;
5824 
5825 	  gdb_assert (loc->pspace == current_program_space);
5826 	  if (gnu_ifunc_resolve_name (loc->function_name,
5827 				      &loc->requested_address))
5828 	    {
5829 	      /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
5830 	      loc->address = adjust_breakpoint_address (loc->gdbarch,
5831 							loc->requested_address,
5832 							b->type);
5833 	    }
5834 	  else if (b->type == bp_breakpoint && b->loc == loc
5835 	           && loc->next == NULL && b->related_breakpoint == b)
5836 	    {
5837 	      /* Create only the whole new breakpoint of this type but do not
5838 		 mess more complicated breakpoints with multiple locations.  */
5839 	      b->type = bp_gnu_ifunc_resolver;
5840 	    }
5841 	}
5842 
5843       if (loc->function_name)
5844 	loc->function_name = xstrdup (loc->function_name);
5845     }
5846 }
5847 
5848 /* Attempt to determine architecture of location identified by SAL.  */
5849 static struct gdbarch *
5850 get_sal_arch (struct symtab_and_line sal)
5851 {
5852   if (sal.section)
5853     return get_objfile_arch (sal.section->objfile);
5854   if (sal.symtab)
5855     return get_objfile_arch (sal.symtab->objfile);
5856 
5857   return NULL;
5858 }
5859 
5860 /* set_raw_breakpoint is a low level routine for allocating and
5861    partially initializing a breakpoint of type BPTYPE.  The newly
5862    created breakpoint's address, section, source file name, and line
5863    number are provided by SAL.  The newly created and partially
5864    initialized breakpoint is added to the breakpoint chain and
5865    is also returned as the value of this function.
5866 
5867    It is expected that the caller will complete the initialization of
5868    the newly created breakpoint struct as well as output any status
5869    information regarding the creation of a new breakpoint.  In
5870    particular, set_raw_breakpoint does NOT set the breakpoint
5871    number!  Care should be taken to not allow an error to occur
5872    prior to completing the initialization of the breakpoint.  If this
5873    should happen, a bogus breakpoint will be left on the chain.  */
5874 
5875 struct breakpoint *
5876 set_raw_breakpoint (struct gdbarch *gdbarch,
5877 		    struct symtab_and_line sal, enum bptype bptype)
5878 {
5879   struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch,
5880 							      bptype);
5881   CORE_ADDR adjusted_address;
5882   struct gdbarch *loc_gdbarch;
5883 
5884   loc_gdbarch = get_sal_arch (sal);
5885   if (!loc_gdbarch)
5886     loc_gdbarch = b->gdbarch;
5887 
5888   if (bptype != bp_catchpoint)
5889     gdb_assert (sal.pspace != NULL);
5890 
5891   /* Adjust the breakpoint's address prior to allocating a location.
5892      Once we call allocate_bp_location(), that mostly uninitialized
5893      location will be placed on the location chain.  Adjustment of the
5894      breakpoint may cause target_read_memory() to be called and we do
5895      not want its scan of the location chain to find a breakpoint and
5896      location that's only been partially initialized.  */
5897   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5898 						sal.pc, b->type);
5899 
5900   b->loc = allocate_bp_location (b);
5901   b->loc->gdbarch = loc_gdbarch;
5902   b->loc->requested_address = sal.pc;
5903   b->loc->address = adjusted_address;
5904   b->loc->pspace = sal.pspace;
5905 
5906   /* Store the program space that was used to set the breakpoint, for
5907      breakpoint resetting.  */
5908   b->pspace = sal.pspace;
5909 
5910   if (sal.symtab == NULL)
5911     b->source_file = NULL;
5912   else
5913     b->source_file = xstrdup (sal.symtab->filename);
5914   b->loc->section = sal.section;
5915   b->line_number = sal.line;
5916 
5917   set_breakpoint_location_function (b->loc,
5918 				    sal.explicit_pc || sal.explicit_line);
5919 
5920   breakpoints_changed ();
5921 
5922   return b;
5923 }
5924 
5925 
5926 /* Note that the breakpoint object B describes a permanent breakpoint
5927    instruction, hard-wired into the inferior's code.  */
5928 void
5929 make_breakpoint_permanent (struct breakpoint *b)
5930 {
5931   struct bp_location *bl;
5932 
5933   b->enable_state = bp_permanent;
5934 
5935   /* By definition, permanent breakpoints are already present in the
5936      code.  Mark all locations as inserted.  For now,
5937      make_breakpoint_permanent is called in just one place, so it's
5938      hard to say if it's reasonable to have permanent breakpoint with
5939      multiple locations or not, but it's easy to implmement.  */
5940   for (bl = b->loc; bl; bl = bl->next)
5941     bl->inserted = 1;
5942 }
5943 
5944 /* Call this routine when stepping and nexting to enable a breakpoint
5945    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
5946    initiated the operation.  */
5947 
5948 void
5949 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
5950 {
5951   struct breakpoint *b, *b_tmp;
5952   int thread = tp->num;
5953 
5954   /* To avoid having to rescan all objfile symbols at every step,
5955      we maintain a list of continually-inserted but always disabled
5956      longjmp "master" breakpoints.  Here, we simply create momentary
5957      clones of those and enable them for the requested thread.  */
5958   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5959     if (b->pspace == current_program_space
5960 	&& (b->type == bp_longjmp_master
5961 	    || b->type == bp_exception_master))
5962       {
5963 	struct breakpoint *clone = clone_momentary_breakpoint (b);
5964 
5965 	clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5966 	clone->thread = thread;
5967       }
5968 
5969   tp->initiating_frame = frame;
5970 }
5971 
5972 /* Delete all longjmp breakpoints from THREAD.  */
5973 void
5974 delete_longjmp_breakpoint (int thread)
5975 {
5976   struct breakpoint *b, *b_tmp;
5977 
5978   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5979     if (b->type == bp_longjmp || b->type == bp_exception)
5980       {
5981 	if (b->thread == thread)
5982 	  delete_breakpoint (b);
5983       }
5984 }
5985 
5986 void
5987 enable_overlay_breakpoints (void)
5988 {
5989   struct breakpoint *b;
5990 
5991   ALL_BREAKPOINTS (b)
5992     if (b->type == bp_overlay_event)
5993     {
5994       b->enable_state = bp_enabled;
5995       update_global_location_list (1);
5996       overlay_events_enabled = 1;
5997     }
5998 }
5999 
6000 void
6001 disable_overlay_breakpoints (void)
6002 {
6003   struct breakpoint *b;
6004 
6005   ALL_BREAKPOINTS (b)
6006     if (b->type == bp_overlay_event)
6007     {
6008       b->enable_state = bp_disabled;
6009       update_global_location_list (0);
6010       overlay_events_enabled = 0;
6011     }
6012 }
6013 
6014 /* Set an active std::terminate breakpoint for each std::terminate
6015    master breakpoint.  */
6016 void
6017 set_std_terminate_breakpoint (void)
6018 {
6019   struct breakpoint *b, *b_tmp;
6020 
6021   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6022     if (b->pspace == current_program_space
6023 	&& b->type == bp_std_terminate_master)
6024       {
6025 	struct breakpoint *clone = clone_momentary_breakpoint (b);
6026 	clone->type = bp_std_terminate;
6027       }
6028 }
6029 
6030 /* Delete all the std::terminate breakpoints.  */
6031 void
6032 delete_std_terminate_breakpoint (void)
6033 {
6034   struct breakpoint *b, *b_tmp;
6035 
6036   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6037     if (b->type == bp_std_terminate)
6038       delete_breakpoint (b);
6039 }
6040 
6041 struct breakpoint *
6042 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6043 {
6044   struct breakpoint *b;
6045 
6046   b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
6047 
6048   b->enable_state = bp_enabled;
6049   /* addr_string has to be used or breakpoint_re_set will delete me.  */
6050   b->addr_string
6051     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
6052 
6053   update_global_location_list_nothrow (1);
6054 
6055   return b;
6056 }
6057 
6058 void
6059 remove_thread_event_breakpoints (void)
6060 {
6061   struct breakpoint *b, *b_tmp;
6062 
6063   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6064     if (b->type == bp_thread_event
6065 	&& b->loc->pspace == current_program_space)
6066       delete_breakpoint (b);
6067 }
6068 
6069 struct captured_parse_breakpoint_args
6070   {
6071     char **arg_p;
6072     struct symtabs_and_lines *sals_p;
6073     struct linespec_result *canonical_p;
6074   };
6075 
6076 struct lang_and_radix
6077   {
6078     enum language lang;
6079     int radix;
6080   };
6081 
6082 /* Create a breakpoint for JIT code registration and unregistration.  */
6083 
6084 struct breakpoint *
6085 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6086 {
6087   struct breakpoint *b;
6088 
6089   b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
6090   update_global_location_list_nothrow (1);
6091   return b;
6092 }
6093 
6094 /* Remove JIT code registration and unregistration breakpoint(s).  */
6095 
6096 void
6097 remove_jit_event_breakpoints (void)
6098 {
6099   struct breakpoint *b, *b_tmp;
6100 
6101   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6102     if (b->type == bp_jit_event
6103 	&& b->loc->pspace == current_program_space)
6104       delete_breakpoint (b);
6105 }
6106 
6107 void
6108 remove_solib_event_breakpoints (void)
6109 {
6110   struct breakpoint *b, *b_tmp;
6111 
6112   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6113     if (b->type == bp_shlib_event
6114 	&& b->loc->pspace == current_program_space)
6115       delete_breakpoint (b);
6116 }
6117 
6118 struct breakpoint *
6119 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6120 {
6121   struct breakpoint *b;
6122 
6123   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
6124   update_global_location_list_nothrow (1);
6125   return b;
6126 }
6127 
6128 /* Disable any breakpoints that are on code in shared libraries.  Only
6129    apply to enabled breakpoints, disabled ones can just stay disabled.  */
6130 
6131 void
6132 disable_breakpoints_in_shlibs (void)
6133 {
6134   struct bp_location *loc, **locp_tmp;
6135 
6136   ALL_BP_LOCATIONS (loc, locp_tmp)
6137   {
6138     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
6139     struct breakpoint *b = loc->owner;
6140 
6141     /* We apply the check to all breakpoints, including disabled for
6142        those with loc->duplicate set.  This is so that when breakpoint
6143        becomes enabled, or the duplicate is removed, gdb will try to
6144        insert all breakpoints.  If we don't set shlib_disabled here,
6145        we'll try to insert those breakpoints and fail.  */
6146     if (((b->type == bp_breakpoint)
6147 	 || (b->type == bp_jit_event)
6148 	 || (b->type == bp_hardware_breakpoint)
6149 	 || (is_tracepoint (b)))
6150 	&& loc->pspace == current_program_space
6151 	&& !loc->shlib_disabled
6152 #ifdef PC_SOLIB
6153 	&& PC_SOLIB (loc->address)
6154 #else
6155 	&& solib_name_from_address (loc->pspace, loc->address)
6156 #endif
6157 	)
6158       {
6159 	loc->shlib_disabled = 1;
6160       }
6161   }
6162 }
6163 
6164 /* Disable any breakpoints that are in in an unloaded shared library.
6165    Only apply to enabled breakpoints, disabled ones can just stay
6166    disabled.  */
6167 
6168 static void
6169 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6170 {
6171   struct bp_location *loc, **locp_tmp;
6172   int disabled_shlib_breaks = 0;
6173 
6174   /* SunOS a.out shared libraries are always mapped, so do not
6175      disable breakpoints; they will only be reported as unloaded
6176      through clear_solib when GDB discards its shared library
6177      list.  See clear_solib for more information.  */
6178   if (exec_bfd != NULL
6179       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6180     return;
6181 
6182   ALL_BP_LOCATIONS (loc, locp_tmp)
6183   {
6184     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
6185     struct breakpoint *b = loc->owner;
6186 
6187     if ((loc->loc_type == bp_loc_hardware_breakpoint
6188 	 || loc->loc_type == bp_loc_software_breakpoint)
6189 	&& solib->pspace == loc->pspace
6190 	&& !loc->shlib_disabled
6191 	&& (b->type == bp_breakpoint
6192 	    || b->type == bp_jit_event
6193 	    || b->type == bp_hardware_breakpoint)
6194 	&& solib_contains_address_p (solib, loc->address))
6195       {
6196 	loc->shlib_disabled = 1;
6197 	/* At this point, we cannot rely on remove_breakpoint
6198 	   succeeding so we must mark the breakpoint as not inserted
6199 	   to prevent future errors occurring in remove_breakpoints.  */
6200 	loc->inserted = 0;
6201 	if (!disabled_shlib_breaks)
6202 	  {
6203 	    target_terminal_ours_for_output ();
6204 	    warning (_("Temporarily disabling breakpoints "
6205 		       "for unloaded shared library \"%s\""),
6206 		     solib->so_name);
6207 	  }
6208 	disabled_shlib_breaks = 1;
6209       }
6210   }
6211 }
6212 
6213 /* FORK & VFORK catchpoints.  */
6214 
6215 /* Implement the "insert" breakpoint_ops method for fork
6216    catchpoints.  */
6217 
6218 static int
6219 insert_catch_fork (struct bp_location *bl)
6220 {
6221   return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
6222 }
6223 
6224 /* Implement the "remove" breakpoint_ops method for fork
6225    catchpoints.  */
6226 
6227 static int
6228 remove_catch_fork (struct bp_location *bl)
6229 {
6230   return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6231 }
6232 
6233 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
6234    catchpoints.  */
6235 
6236 static int
6237 breakpoint_hit_catch_fork (const struct bp_location *bl,
6238 			   struct address_space *aspace, CORE_ADDR bp_addr)
6239 {
6240   return inferior_has_forked (inferior_ptid, &bl->owner->forked_inferior_pid);
6241 }
6242 
6243 /* Implement the "print_it" breakpoint_ops method for fork
6244    catchpoints.  */
6245 
6246 static enum print_stop_action
6247 print_it_catch_fork (struct breakpoint *b)
6248 {
6249   annotate_catchpoint (b->number);
6250   printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6251 		   b->number, ptid_get_pid (b->forked_inferior_pid));
6252   return PRINT_SRC_AND_LOC;
6253 }
6254 
6255 /* Implement the "print_one" breakpoint_ops method for fork
6256    catchpoints.  */
6257 
6258 static void
6259 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
6260 {
6261   struct value_print_options opts;
6262 
6263   get_user_print_options (&opts);
6264 
6265   /* Field 4, the address, is omitted (which makes the columns not
6266      line up too nicely with the headers, but the effect is relatively
6267      readable).  */
6268   if (opts.addressprint)
6269     ui_out_field_skip (uiout, "addr");
6270   annotate_field (5);
6271   ui_out_text (uiout, "fork");
6272   if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6273     {
6274       ui_out_text (uiout, ", process ");
6275       ui_out_field_int (uiout, "what",
6276                         ptid_get_pid (b->forked_inferior_pid));
6277       ui_out_spaces (uiout, 1);
6278     }
6279 }
6280 
6281 /* Implement the "print_mention" breakpoint_ops method for fork
6282    catchpoints.  */
6283 
6284 static void
6285 print_mention_catch_fork (struct breakpoint *b)
6286 {
6287   printf_filtered (_("Catchpoint %d (fork)"), b->number);
6288 }
6289 
6290 /* Implement the "print_recreate" breakpoint_ops method for fork
6291    catchpoints.  */
6292 
6293 static void
6294 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6295 {
6296   fprintf_unfiltered (fp, "catch fork");
6297 }
6298 
6299 /* The breakpoint_ops structure to be used in fork catchpoints.  */
6300 
6301 static struct breakpoint_ops catch_fork_breakpoint_ops =
6302 {
6303   insert_catch_fork,
6304   remove_catch_fork,
6305   breakpoint_hit_catch_fork,
6306   NULL, /* resources_needed */
6307   print_it_catch_fork,
6308   print_one_catch_fork,
6309   NULL, /* print_one_detail */
6310   print_mention_catch_fork,
6311   print_recreate_catch_fork
6312 };
6313 
6314 /* Implement the "insert" breakpoint_ops method for vfork
6315    catchpoints.  */
6316 
6317 static int
6318 insert_catch_vfork (struct bp_location *bl)
6319 {
6320   return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
6321 }
6322 
6323 /* Implement the "remove" breakpoint_ops method for vfork
6324    catchpoints.  */
6325 
6326 static int
6327 remove_catch_vfork (struct bp_location *bl)
6328 {
6329   return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6330 }
6331 
6332 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6333    catchpoints.  */
6334 
6335 static int
6336 breakpoint_hit_catch_vfork (const struct bp_location *bl,
6337 			    struct address_space *aspace, CORE_ADDR bp_addr)
6338 {
6339   return inferior_has_vforked (inferior_ptid, &bl->owner->forked_inferior_pid);
6340 }
6341 
6342 /* Implement the "print_it" breakpoint_ops method for vfork
6343    catchpoints.  */
6344 
6345 static enum print_stop_action
6346 print_it_catch_vfork (struct breakpoint *b)
6347 {
6348   annotate_catchpoint (b->number);
6349   printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6350 		   b->number, ptid_get_pid (b->forked_inferior_pid));
6351   return PRINT_SRC_AND_LOC;
6352 }
6353 
6354 /* Implement the "print_one" breakpoint_ops method for vfork
6355    catchpoints.  */
6356 
6357 static void
6358 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
6359 {
6360   struct value_print_options opts;
6361 
6362   get_user_print_options (&opts);
6363   /* Field 4, the address, is omitted (which makes the columns not
6364      line up too nicely with the headers, but the effect is relatively
6365      readable).  */
6366   if (opts.addressprint)
6367     ui_out_field_skip (uiout, "addr");
6368   annotate_field (5);
6369   ui_out_text (uiout, "vfork");
6370   if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6371     {
6372       ui_out_text (uiout, ", process ");
6373       ui_out_field_int (uiout, "what",
6374                         ptid_get_pid (b->forked_inferior_pid));
6375       ui_out_spaces (uiout, 1);
6376     }
6377 }
6378 
6379 /* Implement the "print_mention" breakpoint_ops method for vfork
6380    catchpoints.  */
6381 
6382 static void
6383 print_mention_catch_vfork (struct breakpoint *b)
6384 {
6385   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6386 }
6387 
6388 /* Implement the "print_recreate" breakpoint_ops method for vfork
6389    catchpoints.  */
6390 
6391 static void
6392 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6393 {
6394   fprintf_unfiltered (fp, "catch vfork");
6395 }
6396 
6397 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
6398 
6399 static struct breakpoint_ops catch_vfork_breakpoint_ops =
6400 {
6401   insert_catch_vfork,
6402   remove_catch_vfork,
6403   breakpoint_hit_catch_vfork,
6404   NULL, /* resources_needed */
6405   print_it_catch_vfork,
6406   print_one_catch_vfork,
6407   NULL, /* print_one_detail */
6408   print_mention_catch_vfork,
6409   print_recreate_catch_vfork
6410 };
6411 
6412 /* Implement the "insert" breakpoint_ops method for syscall
6413    catchpoints.  */
6414 
6415 static int
6416 insert_catch_syscall (struct bp_location *bl)
6417 {
6418   struct inferior *inf = current_inferior ();
6419 
6420   ++inf->total_syscalls_count;
6421   if (!bl->owner->syscalls_to_be_caught)
6422     ++inf->any_syscall_count;
6423   else
6424     {
6425       int i, iter;
6426 
6427       for (i = 0;
6428            VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
6429            i++)
6430 	{
6431           int elem;
6432 
6433 	  if (iter >= VEC_length (int, inf->syscalls_counts))
6434 	    {
6435               int old_size = VEC_length (int, inf->syscalls_counts);
6436               uintptr_t vec_addr_offset
6437 		= old_size * ((uintptr_t) sizeof (int));
6438               uintptr_t vec_addr;
6439               VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6440               vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6441 		vec_addr_offset;
6442               memset ((void *) vec_addr, 0,
6443                       (iter + 1 - old_size) * sizeof (int));
6444 	    }
6445           elem = VEC_index (int, inf->syscalls_counts, iter);
6446           VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6447 	}
6448     }
6449 
6450   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6451 					inf->total_syscalls_count != 0,
6452 					inf->any_syscall_count,
6453 					VEC_length (int, inf->syscalls_counts),
6454 					VEC_address (int, inf->syscalls_counts));
6455 }
6456 
6457 /* Implement the "remove" breakpoint_ops method for syscall
6458    catchpoints.  */
6459 
6460 static int
6461 remove_catch_syscall (struct bp_location *bl)
6462 {
6463   struct inferior *inf = current_inferior ();
6464 
6465   --inf->total_syscalls_count;
6466   if (!bl->owner->syscalls_to_be_caught)
6467     --inf->any_syscall_count;
6468   else
6469     {
6470       int i, iter;
6471 
6472       for (i = 0;
6473            VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
6474            i++)
6475 	{
6476           int elem;
6477 	  if (iter >= VEC_length (int, inf->syscalls_counts))
6478 	    /* Shouldn't happen.  */
6479 	    continue;
6480           elem = VEC_index (int, inf->syscalls_counts, iter);
6481           VEC_replace (int, inf->syscalls_counts, iter, --elem);
6482         }
6483     }
6484 
6485   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6486 					inf->total_syscalls_count != 0,
6487 					inf->any_syscall_count,
6488 					VEC_length (int, inf->syscalls_counts),
6489 					VEC_address (int,
6490 						     inf->syscalls_counts));
6491 }
6492 
6493 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6494    catchpoints.  */
6495 
6496 static int
6497 breakpoint_hit_catch_syscall (const struct bp_location *bl,
6498 			      struct address_space *aspace, CORE_ADDR bp_addr)
6499 {
6500   /* We must check if we are catching specific syscalls in this
6501      breakpoint.  If we are, then we must guarantee that the called
6502      syscall is the same syscall we are catching.  */
6503   int syscall_number = 0;
6504   const struct breakpoint *b = bl->owner;
6505 
6506   if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6507     return 0;
6508 
6509   /* Now, checking if the syscall is the same.  */
6510   if (b->syscalls_to_be_caught)
6511     {
6512       int i, iter;
6513 
6514       for (i = 0;
6515            VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6516            i++)
6517 	if (syscall_number == iter)
6518 	  break;
6519       /* Not the same.  */
6520       if (!iter)
6521 	return 0;
6522     }
6523 
6524   return 1;
6525 }
6526 
6527 /* Implement the "print_it" breakpoint_ops method for syscall
6528    catchpoints.  */
6529 
6530 static enum print_stop_action
6531 print_it_catch_syscall (struct breakpoint *b)
6532 {
6533   /* These are needed because we want to know in which state a
6534      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6535      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6536      must print "called syscall" or "returned from syscall".  */
6537   ptid_t ptid;
6538   struct target_waitstatus last;
6539   struct syscall s;
6540   struct cleanup *old_chain;
6541   char *syscall_id;
6542 
6543   get_last_target_status (&ptid, &last);
6544 
6545   get_syscall_by_number (last.value.syscall_number, &s);
6546 
6547   annotate_catchpoint (b->number);
6548 
6549   if (s.name == NULL)
6550     syscall_id = xstrprintf ("%d", last.value.syscall_number);
6551   else
6552     syscall_id = xstrprintf ("'%s'", s.name);
6553 
6554   old_chain = make_cleanup (xfree, syscall_id);
6555 
6556   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6557     printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6558                      b->number, syscall_id);
6559   else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6560     printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6561                      b->number, syscall_id);
6562 
6563   do_cleanups (old_chain);
6564 
6565   return PRINT_SRC_AND_LOC;
6566 }
6567 
6568 /* Implement the "print_one" breakpoint_ops method for syscall
6569    catchpoints.  */
6570 
6571 static void
6572 print_one_catch_syscall (struct breakpoint *b,
6573 			 struct bp_location **last_loc)
6574 {
6575   struct value_print_options opts;
6576 
6577   get_user_print_options (&opts);
6578   /* Field 4, the address, is omitted (which makes the columns not
6579      line up too nicely with the headers, but the effect is relatively
6580      readable).  */
6581   if (opts.addressprint)
6582     ui_out_field_skip (uiout, "addr");
6583   annotate_field (5);
6584 
6585   if (b->syscalls_to_be_caught
6586       && VEC_length (int, b->syscalls_to_be_caught) > 1)
6587     ui_out_text (uiout, "syscalls \"");
6588   else
6589     ui_out_text (uiout, "syscall \"");
6590 
6591   if (b->syscalls_to_be_caught)
6592     {
6593       int i, iter;
6594       char *text = xstrprintf ("%s", "");
6595 
6596       for (i = 0;
6597            VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6598            i++)
6599         {
6600           char *x = text;
6601           struct syscall s;
6602           get_syscall_by_number (iter, &s);
6603 
6604           if (s.name != NULL)
6605             text = xstrprintf ("%s%s, ", text, s.name);
6606           else
6607             text = xstrprintf ("%s%d, ", text, iter);
6608 
6609           /* We have to xfree the last 'text' (now stored at 'x')
6610              because xstrprintf dinamically allocates new space for it
6611              on every call.  */
6612 	  xfree (x);
6613         }
6614       /* Remove the last comma.  */
6615       text[strlen (text) - 2] = '\0';
6616       ui_out_field_string (uiout, "what", text);
6617     }
6618   else
6619     ui_out_field_string (uiout, "what", "<any syscall>");
6620   ui_out_text (uiout, "\" ");
6621 }
6622 
6623 /* Implement the "print_mention" breakpoint_ops method for syscall
6624    catchpoints.  */
6625 
6626 static void
6627 print_mention_catch_syscall (struct breakpoint *b)
6628 {
6629   if (b->syscalls_to_be_caught)
6630     {
6631       int i, iter;
6632 
6633       if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6634         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6635       else
6636         printf_filtered (_("Catchpoint %d (syscall"), b->number);
6637 
6638       for (i = 0;
6639            VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6640            i++)
6641         {
6642           struct syscall s;
6643           get_syscall_by_number (iter, &s);
6644 
6645           if (s.name)
6646             printf_filtered (" '%s' [%d]", s.name, s.number);
6647           else
6648             printf_filtered (" %d", s.number);
6649         }
6650       printf_filtered (")");
6651     }
6652   else
6653     printf_filtered (_("Catchpoint %d (any syscall)"),
6654                      b->number);
6655 }
6656 
6657 /* Implement the "print_recreate" breakpoint_ops method for syscall
6658    catchpoints.  */
6659 
6660 static void
6661 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6662 {
6663   fprintf_unfiltered (fp, "catch syscall");
6664 
6665   if (b->syscalls_to_be_caught)
6666     {
6667       int i, iter;
6668 
6669       for (i = 0;
6670            VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6671            i++)
6672         {
6673           struct syscall s;
6674 
6675           get_syscall_by_number (iter, &s);
6676           if (s.name)
6677             fprintf_unfiltered (fp, " %s", s.name);
6678           else
6679             fprintf_unfiltered (fp, " %d", s.number);
6680         }
6681     }
6682 }
6683 
6684 /* The breakpoint_ops structure to be used in syscall catchpoints.  */
6685 
6686 static struct breakpoint_ops catch_syscall_breakpoint_ops =
6687 {
6688   insert_catch_syscall,
6689   remove_catch_syscall,
6690   breakpoint_hit_catch_syscall,
6691   NULL, /* resources_needed */
6692   print_it_catch_syscall,
6693   print_one_catch_syscall,
6694   NULL, /* print_one_detail */
6695   print_mention_catch_syscall,
6696   print_recreate_catch_syscall
6697 };
6698 
6699 /* Returns non-zero if 'b' is a syscall catchpoint.  */
6700 
6701 static int
6702 syscall_catchpoint_p (struct breakpoint *b)
6703 {
6704   return (b->ops == &catch_syscall_breakpoint_ops);
6705 }
6706 
6707 /* Create a new breakpoint of the bp_catchpoint kind and return it,
6708    but does NOT mention it nor update the global location list.
6709    This is useful if you need to fill more fields in the
6710    struct breakpoint before calling mention.
6711 
6712    If TEMPFLAG is non-zero, then make the breakpoint temporary.
6713    If COND_STRING is not NULL, then store it in the breakpoint.
6714    OPS, if not NULL, is the breakpoint_ops structure associated
6715    to the catchpoint.  */
6716 
6717 static struct breakpoint *
6718 create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6719 				   char *cond_string,
6720 				   struct breakpoint_ops *ops)
6721 {
6722   struct symtab_and_line sal;
6723   struct breakpoint *b;
6724 
6725   init_sal (&sal);
6726   sal.pspace = current_program_space;
6727 
6728   b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
6729   set_breakpoint_count (breakpoint_count + 1);
6730   b->number = breakpoint_count;
6731 
6732   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
6733   b->thread = -1;
6734   b->addr_string = NULL;
6735   b->enable_state = bp_enabled;
6736   b->disposition = tempflag ? disp_del : disp_donttouch;
6737   b->ops = ops;
6738 
6739   return b;
6740 }
6741 
6742 /* Create a new breakpoint of the bp_catchpoint kind and return it.
6743 
6744    If TEMPFLAG is non-zero, then make the breakpoint temporary.
6745    If COND_STRING is not NULL, then store it in the breakpoint.
6746    OPS, if not NULL, is the breakpoint_ops structure associated
6747    to the catchpoint.  */
6748 
6749 static struct breakpoint *
6750 create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6751 		   char *cond_string, struct breakpoint_ops *ops)
6752 {
6753   struct breakpoint *b =
6754     create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6755 
6756   mention (b);
6757   update_global_location_list (1);
6758 
6759   return b;
6760 }
6761 
6762 static void
6763 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6764 				    int tempflag, char *cond_string,
6765                                     struct breakpoint_ops *ops)
6766 {
6767   struct breakpoint *b
6768     = create_catchpoint (gdbarch, tempflag, cond_string, ops);
6769 
6770   /* FIXME: We should put this information in a breakpoint private data
6771      area.  */
6772   b->forked_inferior_pid = null_ptid;
6773 }
6774 
6775 /* Exec catchpoints.  */
6776 
6777 static int
6778 insert_catch_exec (struct bp_location *bl)
6779 {
6780   return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
6781 }
6782 
6783 static int
6784 remove_catch_exec (struct bp_location *bl)
6785 {
6786   return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6787 }
6788 
6789 static int
6790 breakpoint_hit_catch_exec (const struct bp_location *bl,
6791 			   struct address_space *aspace, CORE_ADDR bp_addr)
6792 {
6793   return inferior_has_execd (inferior_ptid, &bl->owner->exec_pathname);
6794 }
6795 
6796 static enum print_stop_action
6797 print_it_catch_exec (struct breakpoint *b)
6798 {
6799   annotate_catchpoint (b->number);
6800   printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6801 		   b->exec_pathname);
6802   return PRINT_SRC_AND_LOC;
6803 }
6804 
6805 static void
6806 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
6807 {
6808   struct value_print_options opts;
6809 
6810   get_user_print_options (&opts);
6811 
6812   /* Field 4, the address, is omitted (which makes the columns
6813      not line up too nicely with the headers, but the effect
6814      is relatively readable).  */
6815   if (opts.addressprint)
6816     ui_out_field_skip (uiout, "addr");
6817   annotate_field (5);
6818   ui_out_text (uiout, "exec");
6819   if (b->exec_pathname != NULL)
6820     {
6821       ui_out_text (uiout, ", program \"");
6822       ui_out_field_string (uiout, "what", b->exec_pathname);
6823       ui_out_text (uiout, "\" ");
6824     }
6825 }
6826 
6827 static void
6828 print_mention_catch_exec (struct breakpoint *b)
6829 {
6830   printf_filtered (_("Catchpoint %d (exec)"), b->number);
6831 }
6832 
6833 /* Implement the "print_recreate" breakpoint_ops method for exec
6834    catchpoints.  */
6835 
6836 static void
6837 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6838 {
6839   fprintf_unfiltered (fp, "catch exec");
6840 }
6841 
6842 static struct breakpoint_ops catch_exec_breakpoint_ops =
6843 {
6844   insert_catch_exec,
6845   remove_catch_exec,
6846   breakpoint_hit_catch_exec,
6847   NULL, /* resources_needed */
6848   print_it_catch_exec,
6849   print_one_catch_exec,
6850   NULL, /* print_one_detail */
6851   print_mention_catch_exec,
6852   print_recreate_catch_exec
6853 };
6854 
6855 static void
6856 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6857                                  struct breakpoint_ops *ops)
6858 {
6859   struct gdbarch *gdbarch = get_current_arch ();
6860   struct breakpoint *b =
6861     create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6862 
6863   b->syscalls_to_be_caught = filter;
6864 
6865   /* Now, we have to mention the breakpoint and update the global
6866      location list.  */
6867   mention (b);
6868   update_global_location_list (1);
6869 }
6870 
6871 static int
6872 hw_breakpoint_used_count (void)
6873 {
6874   int i = 0;
6875   struct breakpoint *b;
6876   struct bp_location *bl;
6877 
6878   ALL_BREAKPOINTS (b)
6879   {
6880     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
6881       for (bl = b->loc; bl; bl = bl->next)
6882 	{
6883 	  /* Special types of hardware breakpoints may use more than
6884 	     one register.  */
6885 	  if (b->ops && b->ops->resources_needed)
6886 	    i += b->ops->resources_needed (bl);
6887 	  else
6888 	    i++;
6889 	}
6890   }
6891 
6892   return i;
6893 }
6894 
6895 static int
6896 hw_watchpoint_used_count (enum bptype type, int *other_type_used)
6897 {
6898   int i = 0;
6899   struct breakpoint *b;
6900   struct bp_location *bl;
6901 
6902   *other_type_used = 0;
6903   ALL_BREAKPOINTS (b)
6904     {
6905       if (!breakpoint_enabled (b))
6906 	continue;
6907 
6908 	if (b->type == type)
6909 	  for (bl = b->loc; bl; bl = bl->next)
6910 	    {
6911 	      /* Special types of hardware watchpoints may use more than
6912 		 one register.  */
6913 	      if (b->ops && b->ops->resources_needed)
6914 		i += b->ops->resources_needed (bl);
6915 	      else
6916 		i++;
6917 	    }
6918 	else if (is_hardware_watchpoint (b))
6919 	  *other_type_used = 1;
6920     }
6921 
6922   return i;
6923 }
6924 
6925 void
6926 disable_watchpoints_before_interactive_call_start (void)
6927 {
6928   struct breakpoint *b;
6929 
6930   ALL_BREAKPOINTS (b)
6931   {
6932     if (is_watchpoint (b) && breakpoint_enabled (b))
6933       {
6934 	b->enable_state = bp_call_disabled;
6935 	update_global_location_list (0);
6936       }
6937   }
6938 }
6939 
6940 void
6941 enable_watchpoints_after_interactive_call_stop (void)
6942 {
6943   struct breakpoint *b;
6944 
6945   ALL_BREAKPOINTS (b)
6946   {
6947     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
6948       {
6949 	b->enable_state = bp_enabled;
6950 	update_global_location_list (1);
6951       }
6952   }
6953 }
6954 
6955 void
6956 disable_breakpoints_before_startup (void)
6957 {
6958   struct breakpoint *b;
6959   int found = 0;
6960 
6961   ALL_BREAKPOINTS (b)
6962     {
6963       if (b->pspace != current_program_space)
6964 	continue;
6965 
6966       if ((b->type == bp_breakpoint
6967 	   || b->type == bp_hardware_breakpoint)
6968 	  && breakpoint_enabled (b))
6969 	{
6970 	  b->enable_state = bp_startup_disabled;
6971 	  found = 1;
6972 	}
6973     }
6974 
6975   if (found)
6976     update_global_location_list (0);
6977 
6978   current_program_space->executing_startup = 1;
6979 }
6980 
6981 void
6982 enable_breakpoints_after_startup (void)
6983 {
6984   struct breakpoint *b;
6985   int found = 0;
6986 
6987   current_program_space->executing_startup = 0;
6988 
6989   ALL_BREAKPOINTS (b)
6990     {
6991       if (b->pspace != current_program_space)
6992 	continue;
6993 
6994       if ((b->type == bp_breakpoint
6995 	   || b->type == bp_hardware_breakpoint)
6996 	  && b->enable_state == bp_startup_disabled)
6997 	{
6998 	  b->enable_state = bp_enabled;
6999 	  found = 1;
7000 	}
7001     }
7002 
7003   if (found)
7004     breakpoint_re_set ();
7005 }
7006 
7007 
7008 /* Set a breakpoint that will evaporate an end of command
7009    at address specified by SAL.
7010    Restrict it to frame FRAME if FRAME is nonzero.  */
7011 
7012 struct breakpoint *
7013 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7014 			  struct frame_id frame_id, enum bptype type)
7015 {
7016   struct breakpoint *b;
7017 
7018   /* If FRAME_ID is valid, it should be a real frame, not an inlined
7019      one.  */
7020   gdb_assert (!frame_id_inlined_p (frame_id));
7021 
7022   b = set_raw_breakpoint (gdbarch, sal, type);
7023   b->enable_state = bp_enabled;
7024   b->disposition = disp_donttouch;
7025   b->frame_id = frame_id;
7026 
7027   /* If we're debugging a multi-threaded program, then we want
7028      momentary breakpoints to be active in only a single thread of
7029      control.  */
7030   if (in_thread_list (inferior_ptid))
7031     b->thread = pid_to_thread_id (inferior_ptid);
7032 
7033   update_global_location_list_nothrow (1);
7034 
7035   return b;
7036 }
7037 
7038 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
7039    ORIG is NULL.  */
7040 
7041 struct breakpoint *
7042 clone_momentary_breakpoint (struct breakpoint *orig)
7043 {
7044   struct breakpoint *copy;
7045 
7046   /* If there's nothing to clone, then return nothing.  */
7047   if (orig == NULL)
7048     return NULL;
7049 
7050   copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
7051   copy->loc = allocate_bp_location (copy);
7052   set_breakpoint_location_function (copy->loc, 1);
7053 
7054   copy->loc->gdbarch = orig->loc->gdbarch;
7055   copy->loc->requested_address = orig->loc->requested_address;
7056   copy->loc->address = orig->loc->address;
7057   copy->loc->section = orig->loc->section;
7058   copy->loc->pspace = orig->loc->pspace;
7059 
7060   if (orig->source_file == NULL)
7061     copy->source_file = NULL;
7062   else
7063     copy->source_file = xstrdup (orig->source_file);
7064 
7065   copy->line_number = orig->line_number;
7066   copy->frame_id = orig->frame_id;
7067   copy->thread = orig->thread;
7068   copy->pspace = orig->pspace;
7069 
7070   copy->enable_state = bp_enabled;
7071   copy->disposition = disp_donttouch;
7072   copy->number = internal_breakpoint_number--;
7073 
7074   update_global_location_list_nothrow (0);
7075   return copy;
7076 }
7077 
7078 struct breakpoint *
7079 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7080 				enum bptype type)
7081 {
7082   struct symtab_and_line sal;
7083 
7084   sal = find_pc_line (pc, 0);
7085   sal.pc = pc;
7086   sal.section = find_pc_overlay (pc);
7087   sal.explicit_pc = 1;
7088 
7089   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
7090 }
7091 
7092 
7093 /* Tell the user we have just set a breakpoint B.  */
7094 
7095 static void
7096 mention (struct breakpoint *b)
7097 {
7098   int say_where = 0;
7099   struct cleanup *ui_out_chain;
7100   struct value_print_options opts;
7101 
7102   get_user_print_options (&opts);
7103 
7104   /* FIXME: This is misplaced; mention() is called by things (like
7105      hitting a watchpoint) other than breakpoint creation.  It should
7106      be possible to clean this up and at the same time replace the
7107      random calls to breakpoint_changed with this hook.  */
7108   observer_notify_breakpoint_created (b->number);
7109 
7110   if (b->ops != NULL && b->ops->print_mention != NULL)
7111     b->ops->print_mention (b);
7112   else
7113     switch (b->type)
7114       {
7115       case bp_none:
7116 	printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
7117 			 b->number);
7118 	break;
7119       case bp_watchpoint:
7120 	ui_out_text (uiout, "Watchpoint ");
7121 	ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7122 	ui_out_field_int (uiout, "number", b->number);
7123 	ui_out_text (uiout, ": ");
7124 	ui_out_field_string (uiout, "exp", b->exp_string);
7125 	do_cleanups (ui_out_chain);
7126 	break;
7127       case bp_hardware_watchpoint:
7128 	ui_out_text (uiout, "Hardware watchpoint ");
7129 	ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7130 	ui_out_field_int (uiout, "number", b->number);
7131 	ui_out_text (uiout, ": ");
7132 	ui_out_field_string (uiout, "exp", b->exp_string);
7133 	do_cleanups (ui_out_chain);
7134 	break;
7135       case bp_read_watchpoint:
7136 	ui_out_text (uiout, "Hardware read watchpoint ");
7137 	ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
7138 	ui_out_field_int (uiout, "number", b->number);
7139 	ui_out_text (uiout, ": ");
7140 	ui_out_field_string (uiout, "exp", b->exp_string);
7141 	do_cleanups (ui_out_chain);
7142 	break;
7143       case bp_access_watchpoint:
7144 	ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
7145 	ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
7146 	ui_out_field_int (uiout, "number", b->number);
7147 	ui_out_text (uiout, ": ");
7148 	ui_out_field_string (uiout, "exp", b->exp_string);
7149 	do_cleanups (ui_out_chain);
7150 	break;
7151       case bp_breakpoint:
7152       case bp_gnu_ifunc_resolver:
7153 	if (ui_out_is_mi_like_p (uiout))
7154 	  {
7155 	    say_where = 0;
7156 	    break;
7157 	  }
7158 	if (b->disposition == disp_del)
7159 	  printf_filtered (_("Temporary breakpoint"));
7160 	else
7161 	  printf_filtered (_("Breakpoint"));
7162 	printf_filtered (_(" %d"), b->number);
7163 	if (b->type == bp_gnu_ifunc_resolver)
7164 	  printf_filtered (_(" at gnu-indirect-function resolver"));
7165 	say_where = 1;
7166 	break;
7167       case bp_hardware_breakpoint:
7168 	if (ui_out_is_mi_like_p (uiout))
7169 	  {
7170 	    say_where = 0;
7171 	    break;
7172 	  }
7173 	printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
7174 	say_where = 1;
7175 	break;
7176       case bp_tracepoint:
7177 	if (ui_out_is_mi_like_p (uiout))
7178 	  {
7179 	    say_where = 0;
7180 	    break;
7181 	  }
7182 	printf_filtered (_("Tracepoint"));
7183 	printf_filtered (_(" %d"), b->number);
7184 	say_where = 1;
7185 	break;
7186       case bp_fast_tracepoint:
7187 	if (ui_out_is_mi_like_p (uiout))
7188 	  {
7189 	    say_where = 0;
7190 	    break;
7191 	  }
7192 	printf_filtered (_("Fast tracepoint"));
7193 	printf_filtered (_(" %d"), b->number);
7194 	say_where = 1;
7195 	break;
7196       case bp_static_tracepoint:
7197 	if (ui_out_is_mi_like_p (uiout))
7198 	  {
7199 	    say_where = 0;
7200 	    break;
7201 	  }
7202 	printf_filtered (_("Static tracepoint"));
7203 	printf_filtered (_(" %d"), b->number);
7204 	say_where = 1;
7205 	break;
7206 
7207       case bp_until:
7208       case bp_finish:
7209       case bp_longjmp:
7210       case bp_longjmp_resume:
7211       case bp_exception:
7212       case bp_exception_resume:
7213       case bp_step_resume:
7214       case bp_call_dummy:
7215       case bp_std_terminate:
7216       case bp_watchpoint_scope:
7217       case bp_shlib_event:
7218       case bp_thread_event:
7219       case bp_overlay_event:
7220       case bp_jit_event:
7221       case bp_longjmp_master:
7222       case bp_std_terminate_master:
7223       case bp_exception_master:
7224       case bp_gnu_ifunc_resolver_return:
7225 	break;
7226       }
7227 
7228   if (say_where)
7229     {
7230       /* i18n: cagney/2005-02-11: Below needs to be merged into a
7231 	 single string.  */
7232       if (b->loc == NULL)
7233 	{
7234 	  printf_filtered (_(" (%s) pending."), b->addr_string);
7235 	}
7236       else
7237 	{
7238 	  if (opts.addressprint || b->source_file == NULL)
7239 	    {
7240 	      printf_filtered (" at ");
7241 	      fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7242 			      gdb_stdout);
7243 	    }
7244 	  if (b->source_file)
7245 	    printf_filtered (": file %s, line %d.",
7246 			     b->source_file, b->line_number);
7247 
7248 	  if (b->loc->next)
7249 	    {
7250 	      struct bp_location *loc = b->loc;
7251 	      int n = 0;
7252 	      for (; loc; loc = loc->next)
7253 		++n;
7254 	      printf_filtered (" (%d locations)", n);
7255 	    }
7256 
7257 	}
7258     }
7259   if (ui_out_is_mi_like_p (uiout))
7260     return;
7261   printf_filtered ("\n");
7262 }
7263 
7264 
7265 static struct bp_location *
7266 add_location_to_breakpoint (struct breakpoint *b,
7267 			    const struct symtab_and_line *sal)
7268 {
7269   struct bp_location *loc, **tmp;
7270 
7271   loc = allocate_bp_location (b);
7272   for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7273     ;
7274   *tmp = loc;
7275   loc->gdbarch = get_sal_arch (*sal);
7276   if (!loc->gdbarch)
7277     loc->gdbarch = b->gdbarch;
7278   loc->requested_address = sal->pc;
7279   loc->address = adjust_breakpoint_address (loc->gdbarch,
7280 					    loc->requested_address, b->type);
7281   loc->pspace = sal->pspace;
7282   gdb_assert (loc->pspace != NULL);
7283   loc->section = sal->section;
7284 
7285   set_breakpoint_location_function (loc,
7286 				    sal->explicit_pc || sal->explicit_line);
7287   return loc;
7288 }
7289 
7290 
7291 /* Return 1 if LOC is pointing to a permanent breakpoint,
7292    return 0 otherwise.  */
7293 
7294 static int
7295 bp_loc_is_permanent (struct bp_location *loc)
7296 {
7297   int len;
7298   CORE_ADDR addr;
7299   const gdb_byte *brk;
7300   gdb_byte *target_mem;
7301   struct cleanup *cleanup;
7302   int retval = 0;
7303 
7304   gdb_assert (loc != NULL);
7305 
7306   addr = loc->address;
7307   brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
7308 
7309   /* Software breakpoints unsupported?  */
7310   if (brk == NULL)
7311     return 0;
7312 
7313   target_mem = alloca (len);
7314 
7315   /* Enable the automatic memory restoration from breakpoints while
7316      we read the memory.  Otherwise we could say about our temporary
7317      breakpoints they are permanent.  */
7318   cleanup = save_current_space_and_thread ();
7319 
7320   switch_to_program_space_and_thread (loc->pspace);
7321   make_show_memory_breakpoints_cleanup (0);
7322 
7323   if (target_read_memory (loc->address, target_mem, len) == 0
7324       && memcmp (target_mem, brk, len) == 0)
7325     retval = 1;
7326 
7327   do_cleanups (cleanup);
7328 
7329   return retval;
7330 }
7331 
7332 
7333 
7334 /* Create a breakpoint with SAL as location.  Use ADDR_STRING
7335    as textual description of the location, and COND_STRING
7336    as condition expression.  */
7337 
7338 static void
7339 create_breakpoint_sal (struct gdbarch *gdbarch,
7340 		       struct symtabs_and_lines sals, char *addr_string,
7341 		       char *cond_string,
7342 		       enum bptype type, enum bpdisp disposition,
7343 		       int thread, int task, int ignore_count,
7344 		       struct breakpoint_ops *ops, int from_tty,
7345 		       int enabled, int internal, int display_canonical)
7346 {
7347   struct breakpoint *b = NULL;
7348   int i;
7349 
7350   if (type == bp_hardware_breakpoint)
7351     {
7352       int i = hw_breakpoint_used_count ();
7353       int target_resources_ok =
7354 	target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
7355 					    i + 1, 0);
7356       if (target_resources_ok == 0)
7357 	error (_("No hardware breakpoint support in the target."));
7358       else if (target_resources_ok < 0)
7359 	error (_("Hardware breakpoints used exceeds limit."));
7360     }
7361 
7362   gdb_assert (sals.nelts > 0);
7363 
7364   for (i = 0; i < sals.nelts; ++i)
7365     {
7366       struct symtab_and_line sal = sals.sals[i];
7367       struct bp_location *loc;
7368 
7369       if (from_tty)
7370 	{
7371 	  struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7372 	  if (!loc_gdbarch)
7373 	    loc_gdbarch = gdbarch;
7374 
7375 	  describe_other_breakpoints (loc_gdbarch,
7376 				      sal.pspace, sal.pc, sal.section, thread);
7377 	}
7378 
7379       if (i == 0)
7380 	{
7381 	  b = set_raw_breakpoint (gdbarch, sal, type);
7382 	  set_breakpoint_number (internal, b);
7383 	  b->thread = thread;
7384 	  b->task = task;
7385 
7386 	  b->cond_string = cond_string;
7387 	  b->ignore_count = ignore_count;
7388 	  b->enable_state = enabled ? bp_enabled : bp_disabled;
7389 	  b->disposition = disposition;
7390 	  b->pspace = sals.sals[0].pspace;
7391 
7392 	  if (type == bp_static_tracepoint)
7393 	    {
7394 	      struct static_tracepoint_marker marker;
7395 
7396 	      if (is_marker_spec (addr_string))
7397 		{
7398 		  /* We already know the marker exists, otherwise, we
7399 		     wouldn't see a sal for it.  */
7400 		  char *p = &addr_string[3];
7401 		  char *endp;
7402 		  char *marker_str;
7403 		  int i;
7404 
7405 		  p = skip_spaces (p);
7406 
7407 		  endp = skip_to_space (p);
7408 
7409 		  marker_str = savestring (p, endp - p);
7410 		  b->static_trace_marker_id = marker_str;
7411 
7412 		  printf_filtered (_("Probed static tracepoint "
7413 				     "marker \"%s\"\n"),
7414 				   b->static_trace_marker_id);
7415 		}
7416 	      else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7417 		{
7418 		  b->static_trace_marker_id = xstrdup (marker.str_id);
7419 		  release_static_tracepoint_marker (&marker);
7420 
7421 		  printf_filtered (_("Probed static tracepoint "
7422 				     "marker \"%s\"\n"),
7423 				   b->static_trace_marker_id);
7424 		}
7425 	      else
7426 		warning (_("Couldn't determine the static "
7427 			   "tracepoint marker to probe"));
7428 	    }
7429 
7430 	  if (enabled && b->pspace->executing_startup
7431 	      && (b->type == bp_breakpoint
7432 		  || b->type == bp_hardware_breakpoint))
7433 	    b->enable_state = bp_startup_disabled;
7434 
7435 	  loc = b->loc;
7436 	}
7437       else
7438 	{
7439 	  loc = add_location_to_breakpoint (b, &sal);
7440 	}
7441 
7442       if (bp_loc_is_permanent (loc))
7443 	make_breakpoint_permanent (b);
7444 
7445       if (b->cond_string)
7446 	{
7447 	  char *arg = b->cond_string;
7448 	  loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
7449 	  if (*arg)
7450               error (_("Garbage %s follows condition"), arg);
7451 	}
7452     }
7453 
7454   b->display_canonical = display_canonical;
7455   if (addr_string)
7456     b->addr_string = addr_string;
7457   else
7458     /* addr_string has to be used or breakpoint_re_set will delete
7459        me.  */
7460     b->addr_string
7461       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7462 
7463   b->ops = ops;
7464   if (internal)
7465     /* Do not mention breakpoints with a negative number, but do
7466        notify observers.  */
7467     observer_notify_breakpoint_created (b->number);
7468   else
7469     mention (b);
7470 }
7471 
7472 /* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7473    elements to fill the void space.  */
7474 static void
7475 remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
7476 {
7477   int i = index_to_remove+1;
7478   int last_index = sal->nelts-1;
7479 
7480   for (;i <= last_index; ++i)
7481     sal->sals[i-1] = sal->sals[i];
7482 
7483   --(sal->nelts);
7484 }
7485 
7486 /* If appropriate, obtains all sals that correspond to the same file
7487    and line as SAL, in all program spaces.  Users debugging with IDEs,
7488    will want to set a breakpoint at foo.c:line, and not really care
7489    about program spaces.  This is done only if SAL does not have
7490    explicit PC and has line and file information.  If we got just a
7491    single expanded sal, return the original.
7492 
7493    Otherwise, if SAL.explicit_line is not set, filter out all sals for
7494    which the name of enclosing function is different from SAL.  This
7495    makes sure that if we have breakpoint originally set in template
7496    instantiation, say foo<int>(), we won't expand SAL to locations at
7497    the same line in all existing instantiations of 'foo'.  */
7498 
7499 static struct symtabs_and_lines
7500 expand_line_sal_maybe (struct symtab_and_line sal)
7501 {
7502   struct symtabs_and_lines expanded;
7503   CORE_ADDR original_pc = sal.pc;
7504   char *original_function = NULL;
7505   int found;
7506   int i;
7507   struct cleanup *old_chain;
7508 
7509   /* If we have explicit pc, don't expand.
7510      If we have no line number, we can't expand.  */
7511   if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7512     {
7513       expanded.nelts = 1;
7514       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7515       expanded.sals[0] = sal;
7516       return expanded;
7517     }
7518 
7519   sal.pc = 0;
7520 
7521   old_chain = save_current_space_and_thread ();
7522 
7523   switch_to_program_space_and_thread (sal.pspace);
7524 
7525   find_pc_partial_function (original_pc, &original_function, NULL, NULL);
7526 
7527   /* Note that expand_line_sal visits *all* program spaces.  */
7528   expanded = expand_line_sal (sal);
7529 
7530   if (expanded.nelts == 1)
7531     {
7532       /* We had one sal, we got one sal.  Return that sal, adjusting it
7533          past the function prologue if necessary.  */
7534       xfree (expanded.sals);
7535       expanded.nelts = 1;
7536       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7537       sal.pc = original_pc;
7538       expanded.sals[0] = sal;
7539       skip_prologue_sal (&expanded.sals[0]);
7540       do_cleanups (old_chain);
7541       return expanded;
7542     }
7543 
7544   if (!sal.explicit_line)
7545     {
7546       CORE_ADDR func_addr, func_end;
7547       for (i = 0; i < expanded.nelts; ++i)
7548 	{
7549 	  CORE_ADDR pc = expanded.sals[i].pc;
7550 	  char *this_function;
7551 
7552 	  /* We need to switch threads as well since we're about to
7553 	     read memory.  */
7554 	  switch_to_program_space_and_thread (expanded.sals[i].pspace);
7555 
7556 	  if (find_pc_partial_function (pc, &this_function,
7557 					&func_addr, &func_end))
7558 	    {
7559 	      if (this_function
7560 		  && strcmp (this_function, original_function) != 0)
7561 		{
7562 		  remove_sal (&expanded, i);
7563 		  --i;
7564 		}
7565 	    }
7566 	}
7567     }
7568 
7569   /* Skip the function prologue if necessary.  */
7570   for (i = 0; i < expanded.nelts; ++i)
7571     skip_prologue_sal (&expanded.sals[i]);
7572 
7573   do_cleanups (old_chain);
7574 
7575   if (expanded.nelts <= 1)
7576     {
7577       /* This is un ugly workaround.  If we get zero expanded sals
7578          then something is really wrong.  Fix that by returning the
7579          original sal.  */
7580 
7581       xfree (expanded.sals);
7582       expanded.nelts = 1;
7583       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7584       sal.pc = original_pc;
7585       expanded.sals[0] = sal;
7586       return expanded;
7587     }
7588 
7589   if (original_pc)
7590     {
7591       found = 0;
7592       for (i = 0; i < expanded.nelts; ++i)
7593 	if (expanded.sals[i].pc == original_pc)
7594 	  {
7595 	    found = 1;
7596 	    break;
7597 	  }
7598       gdb_assert (found);
7599     }
7600 
7601   return expanded;
7602 }
7603 
7604 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
7605    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7606    value.  COND_STRING, if not NULL, specified the condition to be
7607    used for all breakpoints.  Essentially the only case where
7608    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7609    function.  In that case, it's still not possible to specify
7610    separate conditions for different overloaded functions, so
7611    we take just a single condition string.
7612 
7613    NOTE: If the function succeeds, the caller is expected to cleanup
7614    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
7615    array contents).  If the function fails (error() is called), the
7616    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
7617    COND and SALS arrays and each of those arrays contents.  */
7618 
7619 static void
7620 create_breakpoints_sal (struct gdbarch *gdbarch,
7621 			struct symtabs_and_lines sals,
7622 			struct linespec_result *canonical,
7623 			char *cond_string,
7624 			enum bptype type, enum bpdisp disposition,
7625 			int thread, int task, int ignore_count,
7626 			struct breakpoint_ops *ops, int from_tty,
7627 			int enabled, int internal)
7628 {
7629   int i;
7630 
7631   for (i = 0; i < sals.nelts; ++i)
7632     {
7633       struct symtabs_and_lines expanded =
7634 	expand_line_sal_maybe (sals.sals[i]);
7635 
7636       create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
7637 			     cond_string, type, disposition,
7638 			     thread, task, ignore_count, ops,
7639 			     from_tty, enabled, internal,
7640 			     canonical->special_display);
7641     }
7642 }
7643 
7644 /* Parse ADDRESS which is assumed to be a SAL specification possibly
7645    followed by conditionals.  On return, SALS contains an array of SAL
7646    addresses found.  ADDR_STRING contains a vector of (canonical)
7647    address strings.  ADDRESS points to the end of the SAL.
7648 
7649    The array and the line spec strings are allocated on the heap, it is
7650    the caller's responsibility to free them.  */
7651 
7652 static void
7653 parse_breakpoint_sals (char **address,
7654 		       struct symtabs_and_lines *sals,
7655 		       struct linespec_result *canonical)
7656 {
7657   char *addr_start = *address;
7658 
7659   /* If no arg given, or if first arg is 'if ', use the default
7660      breakpoint.  */
7661   if ((*address) == NULL
7662       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
7663     {
7664       if (default_breakpoint_valid)
7665 	{
7666 	  struct symtab_and_line sal;
7667 
7668 	  init_sal (&sal);		/* Initialize to zeroes.  */
7669 	  sals->sals = (struct symtab_and_line *)
7670 	    xmalloc (sizeof (struct symtab_and_line));
7671 	  sal.pc = default_breakpoint_address;
7672 	  sal.line = default_breakpoint_line;
7673 	  sal.symtab = default_breakpoint_symtab;
7674 	  sal.pspace = default_breakpoint_pspace;
7675 	  sal.section = find_pc_overlay (sal.pc);
7676 
7677 	  /* "break" without arguments is equivalent to "break *PC"
7678 	     where PC is the default_breakpoint_address.  So make sure
7679 	     to set sal.explicit_pc to prevent GDB from trying to
7680 	     expand the list of sals to include all other instances
7681 	     with the same symtab and line.  */
7682 	  sal.explicit_pc = 1;
7683 
7684 	  sals->sals[0] = sal;
7685 	  sals->nelts = 1;
7686 	}
7687       else
7688 	error (_("No default breakpoint address now."));
7689     }
7690   else
7691     {
7692       /* Force almost all breakpoints to be in terms of the
7693          current_source_symtab (which is decode_line_1's default).
7694          This should produce the results we want almost all of the
7695          time while leaving default_breakpoint_* alone.
7696 
7697          ObjC: However, don't match an Objective-C method name which
7698          may have a '+' or '-' succeeded by a '[' */
7699 
7700       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
7701 
7702       if (default_breakpoint_valid
7703 	  && (!cursal.symtab
7704  	      || ((strchr ("+-", (*address)[0]) != NULL)
7705  		  && ((*address)[1] != '['))))
7706 	*sals = decode_line_1 (address, 1, default_breakpoint_symtab,
7707 			       default_breakpoint_line, canonical);
7708       else
7709 	*sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7710 		               canonical);
7711     }
7712   /* For any SAL that didn't have a canonical string, fill one in.  */
7713   if (sals->nelts > 0 && canonical->canonical == NULL)
7714     canonical->canonical = xcalloc (sals->nelts, sizeof (char **));
7715   if (addr_start != (*address))
7716     {
7717       int i;
7718 
7719       for (i = 0; i < sals->nelts; i++)
7720 	{
7721 	  /* Add the string if not present.  */
7722 	  if (canonical->canonical[i] == NULL)
7723 	    canonical->canonical[i] = savestring (addr_start,
7724 						  (*address) - addr_start);
7725 	}
7726     }
7727 }
7728 
7729 
7730 /* Convert each SAL into a real PC.  Verify that the PC can be
7731    inserted as a breakpoint.  If it can't throw an error.  */
7732 
7733 static void
7734 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
7735 {
7736   int i;
7737 
7738   for (i = 0; i < sals->nelts; i++)
7739     resolve_sal_pc (&sals->sals[i]);
7740 }
7741 
7742 /* Fast tracepoints may have restrictions on valid locations.  For
7743    instance, a fast tracepoint using a jump instead of a trap will
7744    likely have to overwrite more bytes than a trap would, and so can
7745    only be placed where the instruction is longer than the jump, or a
7746    multi-instruction sequence does not have a jump into the middle of
7747    it, etc.  */
7748 
7749 static void
7750 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7751 			    struct symtabs_and_lines *sals)
7752 {
7753   int i, rslt;
7754   struct symtab_and_line *sal;
7755   char *msg;
7756   struct cleanup *old_chain;
7757 
7758   for (i = 0; i < sals->nelts; i++)
7759     {
7760       sal = &sals->sals[i];
7761 
7762       rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7763 					       NULL, &msg);
7764       old_chain = make_cleanup (xfree, msg);
7765 
7766       if (!rslt)
7767 	error (_("May not have a fast tracepoint at 0x%s%s"),
7768 	       paddress (gdbarch, sal->pc), (msg ? msg : ""));
7769 
7770       do_cleanups (old_chain);
7771     }
7772 }
7773 
7774 static void
7775 do_captured_parse_breakpoint (struct ui_out *ui, void *data)
7776 {
7777   struct captured_parse_breakpoint_args *args = data;
7778 
7779   parse_breakpoint_sals (args->arg_p, args->sals_p, args->canonical_p);
7780 }
7781 
7782 /* Given TOK, a string specification of condition and thread, as
7783    accepted by the 'break' command, extract the condition
7784    string and thread number and set *COND_STRING and *THREAD.
7785    PC identifies the context at which the condition should be parsed.
7786    If no condition is found, *COND_STRING is set to NULL.
7787    If no thread is found, *THREAD is set to -1.  */
7788 static void
7789 find_condition_and_thread (char *tok, CORE_ADDR pc,
7790 			   char **cond_string, int *thread, int *task)
7791 {
7792   *cond_string = NULL;
7793   *thread = -1;
7794   while (tok && *tok)
7795     {
7796       char *end_tok;
7797       int toklen;
7798       char *cond_start = NULL;
7799       char *cond_end = NULL;
7800 
7801       tok = skip_spaces (tok);
7802 
7803       end_tok = skip_to_space (tok);
7804 
7805       toklen = end_tok - tok;
7806 
7807       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7808 	{
7809 	  struct expression *expr;
7810 
7811 	  tok = cond_start = end_tok + 1;
7812 	  expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7813 	  xfree (expr);
7814 	  cond_end = tok;
7815 	  *cond_string = savestring (cond_start,
7816 				     cond_end - cond_start);
7817 	}
7818       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7819 	{
7820 	  char *tmptok;
7821 
7822 	  tok = end_tok + 1;
7823 	  tmptok = tok;
7824 	  *thread = strtol (tok, &tok, 0);
7825 	  if (tok == tmptok)
7826 	    error (_("Junk after thread keyword."));
7827 	  if (!valid_thread_id (*thread))
7828 	    error (_("Unknown thread %d."), *thread);
7829 	}
7830       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7831 	{
7832 	  char *tmptok;
7833 
7834 	  tok = end_tok + 1;
7835 	  tmptok = tok;
7836 	  *task = strtol (tok, &tok, 0);
7837 	  if (tok == tmptok)
7838 	    error (_("Junk after task keyword."));
7839 	  if (!valid_task_id (*task))
7840 	    error (_("Unknown task %d."), *task);
7841 	}
7842       else
7843 	error (_("Junk at end of arguments."));
7844     }
7845 }
7846 
7847 /* Decode a static tracepoint marker spec.  */
7848 
7849 static struct symtabs_and_lines
7850 decode_static_tracepoint_spec (char **arg_p)
7851 {
7852   VEC(static_tracepoint_marker_p) *markers = NULL;
7853   struct symtabs_and_lines sals;
7854   struct symtab_and_line sal;
7855   struct symbol *sym;
7856   struct cleanup *old_chain;
7857   char *p = &(*arg_p)[3];
7858   char *endp;
7859   char *marker_str;
7860   int i;
7861 
7862   p = skip_spaces (p);
7863 
7864   endp = skip_to_space (p);
7865 
7866   marker_str = savestring (p, endp - p);
7867   old_chain = make_cleanup (xfree, marker_str);
7868 
7869   markers = target_static_tracepoint_markers_by_strid (marker_str);
7870   if (VEC_empty(static_tracepoint_marker_p, markers))
7871     error (_("No known static tracepoint marker named %s"), marker_str);
7872 
7873   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7874   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7875 
7876   for (i = 0; i < sals.nelts; i++)
7877     {
7878       struct static_tracepoint_marker *marker;
7879 
7880       marker = VEC_index (static_tracepoint_marker_p, markers, i);
7881 
7882       init_sal (&sals.sals[i]);
7883 
7884       sals.sals[i] = find_pc_line (marker->address, 0);
7885       sals.sals[i].pc = marker->address;
7886 
7887       release_static_tracepoint_marker (marker);
7888     }
7889 
7890   do_cleanups (old_chain);
7891 
7892   *arg_p = endp;
7893   return sals;
7894 }
7895 
7896 /* Set a breakpoint.  This function is shared between CLI and MI
7897    functions for setting a breakpoint.  This function has two major
7898    modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7899    parameter.  If non-zero, the function will parse arg, extracting
7900    breakpoint location, address and thread.  Otherwise, ARG is just
7901    the location of breakpoint, with condition and thread specified by
7902    the COND_STRING and THREAD parameters.  If INTERNAL is non-zero,
7903    the breakpoint number will be allocated from the internal
7904    breakpoint count.  Returns true if any breakpoint was created;
7905    false otherwise.  */
7906 
7907 int
7908 create_breakpoint (struct gdbarch *gdbarch,
7909 		   char *arg, char *cond_string, int thread,
7910 		   int parse_condition_and_thread,
7911 		   int tempflag, enum bptype type_wanted,
7912 		   int ignore_count,
7913 		   enum auto_boolean pending_break_support,
7914 		   struct breakpoint_ops *ops,
7915 		   int from_tty, int enabled, int internal)
7916 {
7917   struct gdb_exception e;
7918   struct symtabs_and_lines sals;
7919   struct symtab_and_line pending_sal;
7920   char *copy_arg;
7921   char *addr_start = arg;
7922   struct linespec_result canonical;
7923   struct cleanup *old_chain;
7924   struct cleanup *bkpt_chain = NULL;
7925   struct captured_parse_breakpoint_args parse_args;
7926   int i;
7927   int pending = 0;
7928   int task = 0;
7929   int prev_bkpt_count = breakpoint_count;
7930 
7931   sals.sals = NULL;
7932   sals.nelts = 0;
7933   init_linespec_result (&canonical);
7934 
7935   parse_args.arg_p = &arg;
7936   parse_args.sals_p = &sals;
7937   parse_args.canonical_p = &canonical;
7938 
7939   if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7940     {
7941       int i;
7942 
7943       sals = decode_static_tracepoint_spec (&arg);
7944 
7945       copy_arg = savestring (addr_start, arg - addr_start);
7946       canonical.canonical = xcalloc (sals.nelts, sizeof (char **));
7947       for (i = 0; i < sals.nelts; i++)
7948 	canonical.canonical[i] = xstrdup (copy_arg);
7949       goto done;
7950     }
7951 
7952   e = catch_exception (uiout, do_captured_parse_breakpoint,
7953 		       &parse_args, RETURN_MASK_ALL);
7954 
7955   /* If caller is interested in rc value from parse, set value.  */
7956   switch (e.reason)
7957     {
7958     case RETURN_QUIT:
7959       throw_exception (e);
7960     case RETURN_ERROR:
7961       switch (e.error)
7962 	{
7963 	case NOT_FOUND_ERROR:
7964 
7965 	  /* If pending breakpoint support is turned off, throw
7966 	     error.  */
7967 
7968 	  if (pending_break_support == AUTO_BOOLEAN_FALSE)
7969 	    throw_exception (e);
7970 
7971 	  exception_print (gdb_stderr, e);
7972 
7973           /* If pending breakpoint support is auto query and the user
7974 	     selects no, then simply return the error code.  */
7975 	  if (pending_break_support == AUTO_BOOLEAN_AUTO
7976 	      && !nquery (_("Make breakpoint pending on "
7977 			    "future shared library load? ")))
7978 	    return 0;
7979 
7980 	  /* At this point, either the user was queried about setting
7981 	     a pending breakpoint and selected yes, or pending
7982 	     breakpoint behavior is on and thus a pending breakpoint
7983 	     is defaulted on behalf of the user.  */
7984 	  copy_arg = xstrdup (addr_start);
7985 	  canonical.canonical = &copy_arg;
7986 	  sals.nelts = 1;
7987 	  sals.sals = &pending_sal;
7988 	  pending_sal.pc = 0;
7989 	  pending = 1;
7990 	  break;
7991 	default:
7992 	  throw_exception (e);
7993 	}
7994       break;
7995     default:
7996       if (!sals.nelts)
7997 	return 0;
7998     }
7999 
8000   done:
8001 
8002   /* Create a chain of things that always need to be cleaned up.  */
8003   old_chain = make_cleanup (null_cleanup, 0);
8004 
8005   if (!pending)
8006     {
8007       /* Make sure that all storage allocated to SALS gets freed.  */
8008       make_cleanup (xfree, sals.sals);
8009 
8010       /* Cleanup the canonical array but not its contents.  */
8011       make_cleanup (xfree, canonical.canonical);
8012     }
8013 
8014   /* ----------------------------- SNIP -----------------------------
8015      Anything added to the cleanup chain beyond this point is assumed
8016      to be part of a breakpoint.  If the breakpoint create succeeds
8017      then the memory is not reclaimed.  */
8018   bkpt_chain = make_cleanup (null_cleanup, 0);
8019 
8020   /* Mark the contents of the canonical for cleanup.  These go on
8021      the bkpt_chain and only occur if the breakpoint create fails.  */
8022   for (i = 0; i < sals.nelts; i++)
8023     {
8024       if (canonical.canonical[i] != NULL)
8025 	make_cleanup (xfree, canonical.canonical[i]);
8026     }
8027 
8028   /* Resolve all line numbers to PC's and verify that the addresses
8029      are ok for the target.  */
8030   if (!pending)
8031     breakpoint_sals_to_pc (&sals);
8032 
8033   /* Fast tracepoints may have additional restrictions on location.  */
8034   if (type_wanted == bp_fast_tracepoint)
8035     check_fast_tracepoint_sals (gdbarch, &sals);
8036 
8037   /* Verify that condition can be parsed, before setting any
8038      breakpoints.  Allocate a separate condition expression for each
8039      breakpoint.  */
8040   if (!pending)
8041     {
8042       if (parse_condition_and_thread)
8043         {
8044             /* Here we only parse 'arg' to separate condition
8045                from thread number, so parsing in context of first
8046                sal is OK.  When setting the breakpoint we'll
8047                re-parse it in context of each sal.  */
8048             cond_string = NULL;
8049             thread = -1;
8050             find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
8051                                        &thread, &task);
8052             if (cond_string)
8053                 make_cleanup (xfree, cond_string);
8054         }
8055       else
8056         {
8057             /* Create a private copy of condition string.  */
8058             if (cond_string)
8059             {
8060                 cond_string = xstrdup (cond_string);
8061                 make_cleanup (xfree, cond_string);
8062             }
8063         }
8064 
8065       /* If the user is creating a static tracepoint by marker id
8066 	 (strace -m MARKER_ID), then store the sals index, so that
8067 	 breakpoint_re_set can try to match up which of the newly
8068 	 found markers corresponds to this one, and, don't try to
8069 	 expand multiple locations for each sal, given than SALS
8070 	 already should contain all sals for MARKER_ID.  */
8071       if (type_wanted == bp_static_tracepoint
8072 	  && is_marker_spec (canonical.canonical[0]))
8073 	{
8074 	  int i;
8075 
8076 	  for (i = 0; i < sals.nelts; ++i)
8077 	    {
8078 	      struct symtabs_and_lines expanded;
8079 	      struct breakpoint *tp;
8080 	      struct cleanup *old_chain;
8081 
8082 	      expanded.nelts = 1;
8083 	      expanded.sals = xmalloc (sizeof (struct symtab_and_line));
8084 	      expanded.sals[0] = sals.sals[i];
8085 	      old_chain = make_cleanup (xfree, expanded.sals);
8086 
8087 	      create_breakpoint_sal (gdbarch, expanded, canonical.canonical[i],
8088 				     cond_string, type_wanted,
8089 				     tempflag ? disp_del : disp_donttouch,
8090 				     thread, task, ignore_count, ops,
8091 				     from_tty, enabled, internal,
8092 				     canonical.special_display);
8093 
8094 	      do_cleanups (old_chain);
8095 
8096 	      /* Get the tracepoint we just created.  */
8097 	      if (internal)
8098 		tp = get_breakpoint (internal_breakpoint_number);
8099 	      else
8100 		tp = get_breakpoint (breakpoint_count);
8101 	      gdb_assert (tp != NULL);
8102 
8103 	      /* Given that its possible to have multiple markers with
8104 		 the same string id, if the user is creating a static
8105 		 tracepoint by marker id ("strace -m MARKER_ID"), then
8106 		 store the sals index, so that breakpoint_re_set can
8107 		 try to match up which of the newly found markers
8108 		 corresponds to this one  */
8109 	      tp->static_trace_marker_id_idx = i;
8110 	    }
8111 	}
8112       else
8113 	create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
8114 				type_wanted,
8115 				tempflag ? disp_del : disp_donttouch,
8116 				thread, task, ignore_count, ops, from_tty,
8117 				enabled, internal);
8118     }
8119   else
8120     {
8121       struct breakpoint *b;
8122 
8123       make_cleanup (xfree, copy_arg);
8124 
8125       b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
8126       set_breakpoint_number (internal, b);
8127       b->thread = -1;
8128       b->addr_string = canonical.canonical[0];
8129       b->cond_string = NULL;
8130       b->ignore_count = ignore_count;
8131       b->disposition = tempflag ? disp_del : disp_donttouch;
8132       b->condition_not_parsed = 1;
8133       b->ops = ops;
8134       b->enable_state = enabled ? bp_enabled : bp_disabled;
8135       b->pspace = current_program_space;
8136       b->py_bp_object = NULL;
8137 
8138       if (enabled && b->pspace->executing_startup
8139 	  && (b->type == bp_breakpoint
8140 	      || b->type == bp_hardware_breakpoint))
8141 	b->enable_state = bp_startup_disabled;
8142 
8143       if (internal)
8144         /* Do not mention breakpoints with a negative number,
8145 	   but do notify observers.  */
8146         observer_notify_breakpoint_created (b->number);
8147       else
8148         mention (b);
8149     }
8150 
8151   if (sals.nelts > 1)
8152     {
8153       warning (_("Multiple breakpoints were set.\nUse the "
8154 		 "\"delete\" command to delete unwanted breakpoints."));
8155       prev_breakpoint_count = prev_bkpt_count;
8156     }
8157 
8158   /* That's it.  Discard the cleanups for data inserted into the
8159      breakpoint.  */
8160   discard_cleanups (bkpt_chain);
8161   /* But cleanup everything else.  */
8162   do_cleanups (old_chain);
8163 
8164   /* error call may happen here - have BKPT_CHAIN already discarded.  */
8165   update_global_location_list (1);
8166 
8167   return 1;
8168 }
8169 
8170 /* Set a breakpoint.
8171    ARG is a string describing breakpoint address,
8172    condition, and thread.
8173    FLAG specifies if a breakpoint is hardware on,
8174    and if breakpoint is temporary, using BP_HARDWARE_FLAG
8175    and BP_TEMPFLAG.  */
8176 
8177 static void
8178 break_command_1 (char *arg, int flag, int from_tty)
8179 {
8180   int tempflag = flag & BP_TEMPFLAG;
8181   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8182 			     ? bp_hardware_breakpoint
8183 			     : bp_breakpoint);
8184 
8185   create_breakpoint (get_current_arch (),
8186 		     arg,
8187 		     NULL, 0, 1 /* parse arg */,
8188 		     tempflag, type_wanted,
8189 		     0 /* Ignore count */,
8190 		     pending_break_support,
8191 		     NULL /* breakpoint_ops */,
8192 		     from_tty,
8193 		     1 /* enabled */,
8194 		     0 /* internal */);
8195 }
8196 
8197 
8198 /* Helper function for break_command_1 and disassemble_command.  */
8199 
8200 void
8201 resolve_sal_pc (struct symtab_and_line *sal)
8202 {
8203   CORE_ADDR pc;
8204 
8205   if (sal->pc == 0 && sal->symtab != NULL)
8206     {
8207       if (!find_line_pc (sal->symtab, sal->line, &pc))
8208 	error (_("No line %d in file \"%s\"."),
8209 	       sal->line, sal->symtab->filename);
8210       sal->pc = pc;
8211 
8212       /* If this SAL corresponds to a breakpoint inserted using a line
8213          number, then skip the function prologue if necessary.  */
8214       if (sal->explicit_line)
8215 	skip_prologue_sal (sal);
8216     }
8217 
8218   if (sal->section == 0 && sal->symtab != NULL)
8219     {
8220       struct blockvector *bv;
8221       struct block *b;
8222       struct symbol *sym;
8223 
8224       bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
8225       if (bv != NULL)
8226 	{
8227 	  sym = block_linkage_function (b);
8228 	  if (sym != NULL)
8229 	    {
8230 	      fixup_symbol_section (sym, sal->symtab->objfile);
8231 	      sal->section = SYMBOL_OBJ_SECTION (sym);
8232 	    }
8233 	  else
8234 	    {
8235 	      /* It really is worthwhile to have the section, so we'll
8236 	         just have to look harder. This case can be executed
8237 	         if we have line numbers but no functions (as can
8238 	         happen in assembly source).  */
8239 
8240 	      struct minimal_symbol *msym;
8241 	      struct cleanup *old_chain = save_current_space_and_thread ();
8242 
8243 	      switch_to_program_space_and_thread (sal->pspace);
8244 
8245 	      msym = lookup_minimal_symbol_by_pc (sal->pc);
8246 	      if (msym)
8247 		sal->section = SYMBOL_OBJ_SECTION (msym);
8248 
8249 	      do_cleanups (old_chain);
8250 	    }
8251 	}
8252     }
8253 }
8254 
8255 void
8256 break_command (char *arg, int from_tty)
8257 {
8258   break_command_1 (arg, 0, from_tty);
8259 }
8260 
8261 void
8262 tbreak_command (char *arg, int from_tty)
8263 {
8264   break_command_1 (arg, BP_TEMPFLAG, from_tty);
8265 }
8266 
8267 static void
8268 hbreak_command (char *arg, int from_tty)
8269 {
8270   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
8271 }
8272 
8273 static void
8274 thbreak_command (char *arg, int from_tty)
8275 {
8276   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
8277 }
8278 
8279 static void
8280 stop_command (char *arg, int from_tty)
8281 {
8282   printf_filtered (_("Specify the type of breakpoint to set.\n\
8283 Usage: stop in <function | address>\n\
8284        stop at <line>\n"));
8285 }
8286 
8287 static void
8288 stopin_command (char *arg, int from_tty)
8289 {
8290   int badInput = 0;
8291 
8292   if (arg == (char *) NULL)
8293     badInput = 1;
8294   else if (*arg != '*')
8295     {
8296       char *argptr = arg;
8297       int hasColon = 0;
8298 
8299       /* Look for a ':'.  If this is a line number specification, then
8300          say it is bad, otherwise, it should be an address or
8301          function/method name.  */
8302       while (*argptr && !hasColon)
8303 	{
8304 	  hasColon = (*argptr == ':');
8305 	  argptr++;
8306 	}
8307 
8308       if (hasColon)
8309 	badInput = (*argptr != ':');	/* Not a class::method */
8310       else
8311 	badInput = isdigit (*arg);	/* a simple line number */
8312     }
8313 
8314   if (badInput)
8315     printf_filtered (_("Usage: stop in <function | address>\n"));
8316   else
8317     break_command_1 (arg, 0, from_tty);
8318 }
8319 
8320 static void
8321 stopat_command (char *arg, int from_tty)
8322 {
8323   int badInput = 0;
8324 
8325   if (arg == (char *) NULL || *arg == '*')	/* no line number */
8326     badInput = 1;
8327   else
8328     {
8329       char *argptr = arg;
8330       int hasColon = 0;
8331 
8332       /* Look for a ':'.  If there is a '::' then get out, otherwise
8333          it is probably a line number.  */
8334       while (*argptr && !hasColon)
8335 	{
8336 	  hasColon = (*argptr == ':');
8337 	  argptr++;
8338 	}
8339 
8340       if (hasColon)
8341 	badInput = (*argptr == ':');	/* we have class::method */
8342       else
8343 	badInput = !isdigit (*arg);	/* not a line number */
8344     }
8345 
8346   if (badInput)
8347     printf_filtered (_("Usage: stop at <line>\n"));
8348   else
8349     break_command_1 (arg, 0, from_tty);
8350 }
8351 
8352 /* Implement the "breakpoint_hit" breakpoint_ops method for
8353    ranged breakpoints.  */
8354 
8355 static int
8356 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8357 				  struct address_space *aspace,
8358 				  CORE_ADDR bp_addr)
8359 {
8360   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8361 					 bl->length, aspace, bp_addr);
8362 }
8363 
8364 /* Implement the "resources_needed" breakpoint_ops method for
8365    ranged breakpoints.  */
8366 
8367 static int
8368 resources_needed_ranged_breakpoint (const struct bp_location *bl)
8369 {
8370   return target_ranged_break_num_registers ();
8371 }
8372 
8373 /* Implement the "print_it" breakpoint_ops method for
8374    ranged breakpoints.  */
8375 
8376 static enum print_stop_action
8377 print_it_ranged_breakpoint (struct breakpoint *b)
8378 {
8379   struct bp_location *bl = b->loc;
8380 
8381   gdb_assert (b->type == bp_hardware_breakpoint);
8382 
8383   /* Ranged breakpoints have only one location.  */
8384   gdb_assert (bl && bl->next == NULL);
8385 
8386   annotate_breakpoint (b->number);
8387   if (b->disposition == disp_del)
8388     ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8389   else
8390     ui_out_text (uiout, "\nRanged breakpoint ");
8391   if (ui_out_is_mi_like_p (uiout))
8392     {
8393       ui_out_field_string (uiout, "reason",
8394 		      async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8395       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8396     }
8397   ui_out_field_int (uiout, "bkptno", b->number);
8398   ui_out_text (uiout, ", ");
8399 
8400   return PRINT_SRC_AND_LOC;
8401 }
8402 
8403 /* Implement the "print_one" breakpoint_ops method for
8404    ranged breakpoints.  */
8405 
8406 static void
8407 print_one_ranged_breakpoint (struct breakpoint *b,
8408 			     struct bp_location **last_loc)
8409 {
8410   struct bp_location *bl = b->loc;
8411   struct value_print_options opts;
8412 
8413   /* Ranged breakpoints have only one location.  */
8414   gdb_assert (bl && bl->next == NULL);
8415 
8416   get_user_print_options (&opts);
8417 
8418   if (opts.addressprint)
8419     /* We don't print the address range here, it will be printed later
8420        by print_one_detail_ranged_breakpoint.  */
8421     ui_out_field_skip (uiout, "addr");
8422   annotate_field (5);
8423   print_breakpoint_location (b, bl);
8424   *last_loc = bl;
8425 }
8426 
8427 /* Implement the "print_one_detail" breakpoint_ops method for
8428    ranged breakpoints.  */
8429 
8430 static void
8431 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8432 				    struct ui_out *uiout)
8433 {
8434   CORE_ADDR address_start, address_end;
8435   struct bp_location *bl = b->loc;
8436   struct ui_stream *stb = ui_out_stream_new (uiout);
8437   struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8438 
8439   gdb_assert (bl);
8440 
8441   address_start = bl->address;
8442   address_end = address_start + bl->length - 1;
8443 
8444   ui_out_text (uiout, "\taddress range: ");
8445   fprintf_unfiltered (stb->stream, "[%s, %s]",
8446 		      print_core_address (bl->gdbarch, address_start),
8447 		      print_core_address (bl->gdbarch, address_end));
8448   ui_out_field_stream (uiout, "addr", stb);
8449   ui_out_text (uiout, "\n");
8450 
8451   do_cleanups (cleanup);
8452 }
8453 
8454 /* Implement the "print_mention" breakpoint_ops method for
8455    ranged breakpoints.  */
8456 
8457 static void
8458 print_mention_ranged_breakpoint (struct breakpoint *b)
8459 {
8460   struct bp_location *bl = b->loc;
8461 
8462   gdb_assert (bl);
8463   gdb_assert (b->type == bp_hardware_breakpoint);
8464 
8465   if (ui_out_is_mi_like_p (uiout))
8466     return;
8467 
8468   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8469 		   b->number, paddress (bl->gdbarch, bl->address),
8470 		   paddress (bl->gdbarch, bl->address + bl->length - 1));
8471 }
8472 
8473 /* Implement the "print_recreate" breakpoint_ops method for
8474    ranged breakpoints.  */
8475 
8476 static void
8477 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8478 {
8479   fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8480 		      b->addr_string_range_end);
8481 }
8482 
8483 /* The breakpoint_ops structure to be used in ranged breakpoints.  */
8484 
8485 static struct breakpoint_ops ranged_breakpoint_ops =
8486 {
8487   NULL, /* insert */
8488   NULL, /* remove */
8489   breakpoint_hit_ranged_breakpoint,
8490   resources_needed_ranged_breakpoint,
8491   print_it_ranged_breakpoint,
8492   print_one_ranged_breakpoint,
8493   print_one_detail_ranged_breakpoint,
8494   print_mention_ranged_breakpoint,
8495   print_recreate_ranged_breakpoint
8496 };
8497 
8498 /* Find the address where the end of the breakpoint range should be
8499    placed, given the SAL of the end of the range.  This is so that if
8500    the user provides a line number, the end of the range is set to the
8501    last instruction of the given line.  */
8502 
8503 static CORE_ADDR
8504 find_breakpoint_range_end (struct symtab_and_line sal)
8505 {
8506   CORE_ADDR end;
8507 
8508   /* If the user provided a PC value, use it.  Otherwise,
8509      find the address of the end of the given location.  */
8510   if (sal.explicit_pc)
8511     end = sal.pc;
8512   else
8513     {
8514       int ret;
8515       CORE_ADDR start;
8516 
8517       ret = find_line_pc_range (sal, &start, &end);
8518       if (!ret)
8519 	error (_("Could not find location of the end of the range."));
8520 
8521       /* find_line_pc_range returns the start of the next line.  */
8522       end--;
8523     }
8524 
8525   return end;
8526 }
8527 
8528 /* Implement the "break-range" CLI command.  */
8529 
8530 static void
8531 break_range_command (char *arg, int from_tty)
8532 {
8533   char *arg_start, *addr_string_start, *addr_string_end;
8534   struct linespec_result canonical_start, canonical_end;
8535   int bp_count, can_use_bp, length;
8536   CORE_ADDR end;
8537   struct breakpoint *b;
8538   struct symtab_and_line sal_start, sal_end;
8539   struct symtabs_and_lines sals_start, sals_end;
8540   struct cleanup *cleanup_bkpt;
8541 
8542   /* We don't support software ranged breakpoints.  */
8543   if (target_ranged_break_num_registers () < 0)
8544     error (_("This target does not support hardware ranged breakpoints."));
8545 
8546   bp_count = hw_breakpoint_used_count ();
8547   bp_count += target_ranged_break_num_registers ();
8548   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8549 						   bp_count, 0);
8550   if (can_use_bp < 0)
8551     error (_("Hardware breakpoints used exceeds limit."));
8552 
8553   if (arg == NULL || arg[0] == '\0')
8554     error(_("No address range specified."));
8555 
8556   sals_start.sals = NULL;
8557   sals_start.nelts = 0;
8558   init_linespec_result (&canonical_start);
8559 
8560   while (*arg == ' ' || *arg == '\t')
8561     arg++;
8562 
8563   parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
8564 
8565   sal_start = sals_start.sals[0];
8566   addr_string_start = canonical_start.canonical[0];
8567   cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8568   xfree (sals_start.sals);
8569   xfree (canonical_start.canonical);
8570 
8571   if (arg[0] != ',')
8572     error (_("Too few arguments."));
8573   else if (sals_start.nelts == 0)
8574     error (_("Could not find location of the beginning of the range."));
8575   else if (sals_start.nelts != 1)
8576     error (_("Cannot create a ranged breakpoint with multiple locations."));
8577 
8578   resolve_sal_pc (&sal_start);
8579 
8580   arg++;	/* Skip the comma.  */
8581   while (*arg == ' ' || *arg == '\t')
8582     arg++;
8583 
8584   /* Parse the end location.  */
8585 
8586   sals_end.sals = NULL;
8587   sals_end.nelts = 0;
8588   init_linespec_result (&canonical_end);
8589   arg_start = arg;
8590 
8591   /* We call decode_line_1 directly here instead of using
8592      parse_breakpoint_sals because we need to specify the start location's
8593      symtab and line as the default symtab and line for the end of the
8594      range.  This makes it possible to have ranges like "foo.c:27, +14",
8595      where +14 means 14 lines from the start location.  */
8596   sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
8597 			    &canonical_end);
8598 
8599   /* canonical_end can be NULL if it was of the form "*0xdeadbeef".  */
8600   if (canonical_end.canonical == NULL)
8601     canonical_end.canonical = xcalloc (1, sizeof (char **));
8602   /* Add the string if not present.  */
8603   if (arg_start != arg && canonical_end.canonical[0] == NULL)
8604     canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8605 
8606   sal_end = sals_end.sals[0];
8607   addr_string_end = canonical_end.canonical[0];
8608   make_cleanup (xfree, addr_string_end);
8609   xfree (sals_end.sals);
8610   xfree (canonical_end.canonical);
8611 
8612   if (sals_end.nelts == 0)
8613     error (_("Could not find location of the end of the range."));
8614   else if (sals_end.nelts != 1)
8615     error (_("Cannot create a ranged breakpoint with multiple locations."));
8616 
8617   resolve_sal_pc (&sal_end);
8618 
8619   end = find_breakpoint_range_end (sal_end);
8620   if (sal_start.pc > end)
8621     error (_("Invalid address range, end preceeds start."));
8622 
8623   length = end - sal_start.pc + 1;
8624   if (length < 0)
8625     /* Length overflowed.  */
8626     error (_("Address range too large."));
8627   else if (length == 1)
8628     {
8629       /* This range is simple enough to be handled by
8630 	 the `hbreak' command.  */
8631       hbreak_command (addr_string_start, 1);
8632 
8633       do_cleanups (cleanup_bkpt);
8634 
8635       return;
8636     }
8637 
8638   /* Now set up the breakpoint.  */
8639   b = set_raw_breakpoint (get_current_arch (), sal_start,
8640 			  bp_hardware_breakpoint);
8641   set_breakpoint_count (breakpoint_count + 1);
8642   b->number = breakpoint_count;
8643   b->disposition = disp_donttouch;
8644   b->addr_string = addr_string_start;
8645   b->addr_string_range_end = addr_string_end;
8646   b->ops = &ranged_breakpoint_ops;
8647   b->loc->length = length;
8648 
8649   discard_cleanups (cleanup_bkpt);
8650 
8651   mention (b);
8652   update_global_location_list (1);
8653 }
8654 
8655 /*  Return non-zero if EXP is verified as constant.  Returned zero
8656     means EXP is variable.  Also the constant detection may fail for
8657     some constant expressions and in such case still falsely return
8658     zero.  */
8659 static int
8660 watchpoint_exp_is_const (const struct expression *exp)
8661 {
8662   int i = exp->nelts;
8663 
8664   while (i > 0)
8665     {
8666       int oplenp, argsp;
8667 
8668       /* We are only interested in the descriptor of each element.  */
8669       operator_length (exp, i, &oplenp, &argsp);
8670       i -= oplenp;
8671 
8672       switch (exp->elts[i].opcode)
8673 	{
8674 	case BINOP_ADD:
8675 	case BINOP_SUB:
8676 	case BINOP_MUL:
8677 	case BINOP_DIV:
8678 	case BINOP_REM:
8679 	case BINOP_MOD:
8680 	case BINOP_LSH:
8681 	case BINOP_RSH:
8682 	case BINOP_LOGICAL_AND:
8683 	case BINOP_LOGICAL_OR:
8684 	case BINOP_BITWISE_AND:
8685 	case BINOP_BITWISE_IOR:
8686 	case BINOP_BITWISE_XOR:
8687 	case BINOP_EQUAL:
8688 	case BINOP_NOTEQUAL:
8689 	case BINOP_LESS:
8690 	case BINOP_GTR:
8691 	case BINOP_LEQ:
8692 	case BINOP_GEQ:
8693 	case BINOP_REPEAT:
8694 	case BINOP_COMMA:
8695 	case BINOP_EXP:
8696 	case BINOP_MIN:
8697 	case BINOP_MAX:
8698 	case BINOP_INTDIV:
8699 	case BINOP_CONCAT:
8700 	case BINOP_IN:
8701 	case BINOP_RANGE:
8702 	case TERNOP_COND:
8703 	case TERNOP_SLICE:
8704 	case TERNOP_SLICE_COUNT:
8705 
8706 	case OP_LONG:
8707 	case OP_DOUBLE:
8708 	case OP_DECFLOAT:
8709 	case OP_LAST:
8710 	case OP_COMPLEX:
8711 	case OP_STRING:
8712 	case OP_BITSTRING:
8713 	case OP_ARRAY:
8714 	case OP_TYPE:
8715 	case OP_NAME:
8716 	case OP_OBJC_NSSTRING:
8717 
8718 	case UNOP_NEG:
8719 	case UNOP_LOGICAL_NOT:
8720 	case UNOP_COMPLEMENT:
8721 	case UNOP_ADDR:
8722 	case UNOP_HIGH:
8723 	  /* Unary, binary and ternary operators: We have to check
8724 	     their operands.  If they are constant, then so is the
8725 	     result of that operation.  For instance, if A and B are
8726 	     determined to be constants, then so is "A + B".
8727 
8728 	     UNOP_IND is one exception to the rule above, because the
8729 	     value of *ADDR is not necessarily a constant, even when
8730 	     ADDR is.  */
8731 	  break;
8732 
8733 	case OP_VAR_VALUE:
8734 	  /* Check whether the associated symbol is a constant.
8735 
8736 	     We use SYMBOL_CLASS rather than TYPE_CONST because it's
8737 	     possible that a buggy compiler could mark a variable as
8738 	     constant even when it is not, and TYPE_CONST would return
8739 	     true in this case, while SYMBOL_CLASS wouldn't.
8740 
8741 	     We also have to check for function symbols because they
8742 	     are always constant.  */
8743 	  {
8744 	    struct symbol *s = exp->elts[i + 2].symbol;
8745 
8746 	    if (SYMBOL_CLASS (s) != LOC_BLOCK
8747 		&& SYMBOL_CLASS (s) != LOC_CONST
8748 		&& SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8749 	      return 0;
8750 	    break;
8751 	  }
8752 
8753 	/* The default action is to return 0 because we are using
8754 	   the optimistic approach here: If we don't know something,
8755 	   then it is not a constant.  */
8756 	default:
8757 	  return 0;
8758 	}
8759     }
8760 
8761   return 1;
8762 }
8763 
8764 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
8765 
8766 static int
8767 insert_watchpoint (struct bp_location *bl)
8768 {
8769   int length = bl->owner->exact? 1 : bl->length;
8770 
8771   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8772 				   bl->owner->cond_exp);
8773 }
8774 
8775 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
8776 
8777 static int
8778 remove_watchpoint (struct bp_location *bl)
8779 {
8780   int length = bl->owner->exact? 1 : bl->length;
8781 
8782   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8783 				   bl->owner->cond_exp);
8784 }
8785 
8786 /* Implement the "resources_needed" breakpoint_ops method for
8787    hardware watchpoints.  */
8788 
8789 static int
8790 resources_needed_watchpoint (const struct bp_location *bl)
8791 {
8792   int length = bl->owner->exact? 1 : bl->length;
8793 
8794   return target_region_ok_for_hw_watchpoint (bl->address, length);
8795 }
8796 
8797 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
8798 
8799 static struct breakpoint_ops watchpoint_breakpoint_ops =
8800 {
8801   insert_watchpoint,
8802   remove_watchpoint,
8803   NULL, /* breakpoint_hit */
8804   resources_needed_watchpoint,
8805   NULL, /* print_it */
8806   NULL, /* print_one */
8807   NULL, /* print_one_detail */
8808   NULL, /* print_mention */
8809   NULL  /* print_recreate */
8810 };
8811 
8812 /* accessflag:  hw_write:  watch write,
8813                 hw_read:   watch read,
8814 		hw_access: watch access (read or write) */
8815 static void
8816 watch_command_1 (char *arg, int accessflag, int from_tty,
8817 		 int just_location, int internal)
8818 {
8819   struct breakpoint *b, *scope_breakpoint = NULL;
8820   struct expression *exp;
8821   struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
8822   struct value *val, *mark, *result;
8823   struct frame_info *frame;
8824   char *exp_start = NULL;
8825   char *exp_end = NULL;
8826   char *tok, *id_tok_start, *end_tok;
8827   int toklen;
8828   char *cond_start = NULL;
8829   char *cond_end = NULL;
8830   int i, other_type_used, target_resources_ok = 0;
8831   enum bptype bp_type;
8832   int reg_cnt = 0;
8833   int thread = -1;
8834   int pc = 0;
8835 
8836   /* Make sure that we actually have parameters to parse.  */
8837   if (arg != NULL && arg[0] != '\0')
8838     {
8839       toklen = strlen (arg); /* Size of argument list.  */
8840 
8841       /* Points tok to the end of the argument list.  */
8842       tok = arg + toklen - 1;
8843 
8844       /* Go backwards in the parameters list.  Skip the last
8845          parameter.  If we're expecting a 'thread <thread_num>'
8846          parameter, this should be the thread identifier.  */
8847       while (tok > arg && (*tok == ' ' || *tok == '\t'))
8848         tok--;
8849       while (tok > arg && (*tok != ' ' && *tok != '\t'))
8850         tok--;
8851 
8852       /* Points end_tok to the beginning of the last token.  */
8853       id_tok_start = tok + 1;
8854 
8855       /* Go backwards in the parameters list.  Skip one more
8856          parameter.  If we're expecting a 'thread <thread_num>'
8857          parameter, we should reach a "thread" token.  */
8858       while (tok > arg && (*tok == ' ' || *tok == '\t'))
8859         tok--;
8860 
8861       end_tok = tok;
8862 
8863       while (tok > arg && (*tok != ' ' && *tok != '\t'))
8864         tok--;
8865 
8866       /* Move the pointer forward to skip the whitespace and
8867          calculate the length of the token.  */
8868       tok++;
8869       toklen = end_tok - tok;
8870 
8871       if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8872         {
8873           /* At this point we've found a "thread" token, which means
8874              the user is trying to set a watchpoint that triggers
8875              only in a specific thread.  */
8876           char *endp;
8877 
8878           /* Extract the thread ID from the next token.  */
8879           thread = strtol (id_tok_start, &endp, 0);
8880 
8881           /* Check if the user provided a valid numeric value for the
8882              thread ID.  */
8883           if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8884             error (_("Invalid thread ID specification %s."), id_tok_start);
8885 
8886           /* Check if the thread actually exists.  */
8887           if (!valid_thread_id (thread))
8888             error (_("Unknown thread %d."), thread);
8889 
8890           /* Truncate the string and get rid of the thread <thread_num>
8891              parameter before the parameter list is parsed by the
8892              evaluate_expression() function.  */
8893           *tok = '\0';
8894         }
8895     }
8896 
8897   /* Parse the rest of the arguments.  */
8898   innermost_block = NULL;
8899   exp_start = arg;
8900   exp = parse_exp_1 (&arg, 0, 0);
8901   exp_end = arg;
8902   /* Remove trailing whitespace from the expression before saving it.
8903      This makes the eventual display of the expression string a bit
8904      prettier.  */
8905   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8906     --exp_end;
8907 
8908   /* Checking if the expression is not constant.  */
8909   if (watchpoint_exp_is_const (exp))
8910     {
8911       int len;
8912 
8913       len = exp_end - exp_start;
8914       while (len > 0 && isspace (exp_start[len - 1]))
8915 	len--;
8916       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8917     }
8918 
8919   exp_valid_block = innermost_block;
8920   mark = value_mark ();
8921   fetch_subexp_value (exp, &pc, &val, &result, NULL);
8922 
8923   if (just_location)
8924     {
8925       exp_valid_block = NULL;
8926       val = value_addr (result);
8927       release_value (val);
8928       value_free_to_mark (mark);
8929     }
8930   else if (val != NULL)
8931     release_value (val);
8932 
8933   tok = skip_spaces (arg);
8934   end_tok = skip_to_space (tok);
8935 
8936   toklen = end_tok - tok;
8937   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8938     {
8939       struct expression *cond;
8940 
8941       innermost_block = NULL;
8942       tok = cond_start = end_tok + 1;
8943       cond = parse_exp_1 (&tok, 0, 0);
8944 
8945       /* The watchpoint expression may not be local, but the condition
8946 	 may still be.  E.g.: `watch global if local > 0'.  */
8947       cond_exp_valid_block = innermost_block;
8948 
8949       xfree (cond);
8950       cond_end = tok;
8951     }
8952   if (*tok)
8953     error (_("Junk at end of command."));
8954 
8955   if (accessflag == hw_read)
8956     bp_type = bp_read_watchpoint;
8957   else if (accessflag == hw_access)
8958     bp_type = bp_access_watchpoint;
8959   else
8960     bp_type = bp_hardware_watchpoint;
8961 
8962   reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8963   if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8964     error (_("Expression cannot be implemented with read/access watchpoint."));
8965   if (reg_cnt != 0)
8966     {
8967       i = hw_watchpoint_used_count (bp_type, &other_type_used);
8968       target_resources_ok =
8969 	target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
8970 					    other_type_used);
8971       if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8972 	error (_("Target does not support this type of hardware watchpoint."));
8973 
8974       if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8975 	error (_("Target can only support one kind "
8976 		 "of HW watchpoint at a time."));
8977     }
8978 
8979   /* Change the type of breakpoint to an ordinary watchpoint if a
8980      hardware watchpoint could not be set.  */
8981   if (!reg_cnt || target_resources_ok <= 0)
8982     bp_type = bp_watchpoint;
8983 
8984   frame = block_innermost_frame (exp_valid_block);
8985 
8986   /* If the expression is "local", then set up a "watchpoint scope"
8987      breakpoint at the point where we've left the scope of the watchpoint
8988      expression.  Create the scope breakpoint before the watchpoint, so
8989      that we will encounter it first in bpstat_stop_status.  */
8990   if (exp_valid_block && frame)
8991     {
8992       if (frame_id_p (frame_unwind_caller_id (frame)))
8993 	{
8994  	  scope_breakpoint
8995 	    = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8996 					  frame_unwind_caller_pc (frame),
8997 					  bp_watchpoint_scope);
8998 
8999 	  scope_breakpoint->enable_state = bp_enabled;
9000 
9001 	  /* Automatically delete the breakpoint when it hits.  */
9002 	  scope_breakpoint->disposition = disp_del;
9003 
9004 	  /* Only break in the proper frame (help with recursion).  */
9005 	  scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
9006 
9007 	  /* Set the address at which we will stop.  */
9008 	  scope_breakpoint->loc->gdbarch
9009 	    = frame_unwind_caller_arch (frame);
9010 	  scope_breakpoint->loc->requested_address
9011 	    = frame_unwind_caller_pc (frame);
9012 	  scope_breakpoint->loc->address
9013 	    = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9014 					 scope_breakpoint->loc->requested_address,
9015 					 scope_breakpoint->type);
9016 	}
9017     }
9018 
9019   /* Now set up the breakpoint.  */
9020   b = set_raw_breakpoint_without_location (NULL, bp_type);
9021   set_breakpoint_number (internal, b);
9022   b->thread = thread;
9023   b->disposition = disp_donttouch;
9024   b->exp = exp;
9025   b->exp_valid_block = exp_valid_block;
9026   b->cond_exp_valid_block = cond_exp_valid_block;
9027   if (just_location)
9028     {
9029       struct type *t = value_type (val);
9030       CORE_ADDR addr = value_as_address (val);
9031       char *name;
9032 
9033       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9034       name = type_to_string (t);
9035 
9036       b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
9037 					  core_addr_to_string (addr));
9038       xfree (name);
9039 
9040       b->exp_string = xstrprintf ("-location %.*s",
9041 				  (int) (exp_end - exp_start), exp_start);
9042 
9043       /* The above expression is in C.  */
9044       b->language = language_c;
9045     }
9046   else
9047     b->exp_string = savestring (exp_start, exp_end - exp_start);
9048   b->val = val;
9049   b->val_valid = 1;
9050   b->ops = &watchpoint_breakpoint_ops;
9051 
9052   /* Use an exact watchpoint when there's only one memory region to be
9053      watched, and only one debug register is needed to watch it.  */
9054   b->exact = target_exact_watchpoints && reg_cnt == 1;
9055 
9056   if (cond_start)
9057     b->cond_string = savestring (cond_start, cond_end - cond_start);
9058   else
9059     b->cond_string = 0;
9060 
9061   if (frame)
9062     {
9063       b->watchpoint_frame = get_frame_id (frame);
9064       b->watchpoint_thread = inferior_ptid;
9065     }
9066   else
9067     {
9068       b->watchpoint_frame = null_frame_id;
9069       b->watchpoint_thread = null_ptid;
9070     }
9071 
9072   if (scope_breakpoint != NULL)
9073     {
9074       /* The scope breakpoint is related to the watchpoint.  We will
9075 	 need to act on them together.  */
9076       b->related_breakpoint = scope_breakpoint;
9077       scope_breakpoint->related_breakpoint = b;
9078     }
9079 
9080   if (!just_location)
9081     value_free_to_mark (mark);
9082 
9083   /* Finally update the new watchpoint.  This creates the locations
9084      that should be inserted.  */
9085   update_watchpoint (b, 1);
9086   if (internal)
9087     /* Do not mention breakpoints with a negative number, but do
9088        notify observers.  */
9089     observer_notify_breakpoint_created (b->number);
9090   else
9091     mention (b);
9092   update_global_location_list (1);
9093 }
9094 
9095 /* Return count of debug registers needed to watch the given expression.
9096    If EXACT_WATCHPOINTS is 1, then consider that only the address of
9097    the start of the watched region will be monitored (i.e., all accesses
9098    will be aligned).  This uses less debug registers on some targets.
9099 
9100    If the watchpoint cannot be handled in hardware return zero.  */
9101 
9102 static int
9103 can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
9104 {
9105   int found_memory_cnt = 0;
9106   struct value *head = v;
9107 
9108   /* Did the user specifically forbid us to use hardware watchpoints? */
9109   if (!can_use_hw_watchpoints)
9110     return 0;
9111 
9112   /* Make sure that the value of the expression depends only upon
9113      memory contents, and values computed from them within GDB.  If we
9114      find any register references or function calls, we can't use a
9115      hardware watchpoint.
9116 
9117      The idea here is that evaluating an expression generates a series
9118      of values, one holding the value of every subexpression.  (The
9119      expression a*b+c has five subexpressions: a, b, a*b, c, and
9120      a*b+c.)  GDB's values hold almost enough information to establish
9121      the criteria given above --- they identify memory lvalues,
9122      register lvalues, computed values, etcetera.  So we can evaluate
9123      the expression, and then scan the chain of values that leaves
9124      behind to decide whether we can detect any possible change to the
9125      expression's final value using only hardware watchpoints.
9126 
9127      However, I don't think that the values returned by inferior
9128      function calls are special in any way.  So this function may not
9129      notice that an expression involving an inferior function call
9130      can't be watched with hardware watchpoints.  FIXME.  */
9131   for (; v; v = value_next (v))
9132     {
9133       if (VALUE_LVAL (v) == lval_memory)
9134 	{
9135 	  if (v != head && value_lazy (v))
9136 	    /* A lazy memory lvalue in the chain is one that GDB never
9137 	       needed to fetch; we either just used its address (e.g.,
9138 	       `a' in `a.b') or we never needed it at all (e.g., `a'
9139 	       in `a,b').  This doesn't apply to HEAD; if that is
9140 	       lazy then it was not readable, but watch it anyway.  */
9141 	    ;
9142 	  else
9143 	    {
9144 	      /* Ahh, memory we actually used!  Check if we can cover
9145                  it with hardware watchpoints.  */
9146 	      struct type *vtype = check_typedef (value_type (v));
9147 
9148 	      /* We only watch structs and arrays if user asked for it
9149 		 explicitly, never if they just happen to appear in a
9150 		 middle of some value chain.  */
9151 	      if (v == head
9152 		  || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9153 		      && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9154 		{
9155 		  CORE_ADDR vaddr = value_address (v);
9156 		  int len;
9157 		  int num_regs;
9158 
9159 		  len = (exact_watchpoints
9160 			 && is_scalar_type_recursive (vtype))?
9161 		    1 : TYPE_LENGTH (value_type (v));
9162 
9163 		  num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9164 		  if (!num_regs)
9165 		    return 0;
9166 		  else
9167 		    found_memory_cnt += num_regs;
9168 		}
9169 	    }
9170 	}
9171       else if (VALUE_LVAL (v) != not_lval
9172 	       && deprecated_value_modifiable (v) == 0)
9173 	return 0;	/* These are values from the history (e.g., $1).  */
9174       else if (VALUE_LVAL (v) == lval_register)
9175 	return 0;	/* Cannot watch a register with a HW watchpoint.  */
9176     }
9177 
9178   /* The expression itself looks suitable for using a hardware
9179      watchpoint, but give the target machine a chance to reject it.  */
9180   return found_memory_cnt;
9181 }
9182 
9183 void
9184 watch_command_wrapper (char *arg, int from_tty, int internal)
9185 {
9186   watch_command_1 (arg, hw_write, from_tty, 0, internal);
9187 }
9188 
9189 /* A helper function that looks for an argument at the start of a
9190    string.  The argument must also either be at the end of the string,
9191    or be followed by whitespace.  Returns 1 if it finds the argument,
9192    0 otherwise.  If the argument is found, it updates *STR.  */
9193 
9194 static int
9195 check_for_argument (char **str, char *arg, int arg_len)
9196 {
9197   if (strncmp (*str, arg, arg_len) == 0
9198       && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9199     {
9200       *str += arg_len;
9201       return 1;
9202     }
9203   return 0;
9204 }
9205 
9206 /* A helper function that looks for the "-location" argument and then
9207    calls watch_command_1.  */
9208 
9209 static void
9210 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9211 {
9212   int just_location = 0;
9213 
9214   if (arg
9215       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9216 	  || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9217     {
9218       arg = skip_spaces (arg);
9219       just_location = 1;
9220     }
9221 
9222   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
9223 }
9224 
9225 static void
9226 watch_command (char *arg, int from_tty)
9227 {
9228   watch_maybe_just_location (arg, hw_write, from_tty);
9229 }
9230 
9231 void
9232 rwatch_command_wrapper (char *arg, int from_tty, int internal)
9233 {
9234   watch_command_1 (arg, hw_read, from_tty, 0, internal);
9235 }
9236 
9237 static void
9238 rwatch_command (char *arg, int from_tty)
9239 {
9240   watch_maybe_just_location (arg, hw_read, from_tty);
9241 }
9242 
9243 void
9244 awatch_command_wrapper (char *arg, int from_tty, int internal)
9245 {
9246   watch_command_1 (arg, hw_access, from_tty, 0, internal);
9247 }
9248 
9249 static void
9250 awatch_command (char *arg, int from_tty)
9251 {
9252   watch_maybe_just_location (arg, hw_access, from_tty);
9253 }
9254 
9255 
9256 /* Helper routines for the until_command routine in infcmd.c.  Here
9257    because it uses the mechanisms of breakpoints.  */
9258 
9259 struct until_break_command_continuation_args
9260 {
9261   struct breakpoint *breakpoint;
9262   struct breakpoint *breakpoint2;
9263   int thread_num;
9264 };
9265 
9266 /* This function is called by fetch_inferior_event via the
9267    cmd_continuation pointer, to complete the until command.  It takes
9268    care of cleaning up the temporary breakpoints set up by the until
9269    command.  */
9270 static void
9271 until_break_command_continuation (void *arg)
9272 {
9273   struct until_break_command_continuation_args *a = arg;
9274 
9275   delete_breakpoint (a->breakpoint);
9276   if (a->breakpoint2)
9277     delete_breakpoint (a->breakpoint2);
9278   delete_longjmp_breakpoint (a->thread_num);
9279 }
9280 
9281 void
9282 until_break_command (char *arg, int from_tty, int anywhere)
9283 {
9284   struct symtabs_and_lines sals;
9285   struct symtab_and_line sal;
9286   struct frame_info *frame = get_selected_frame (NULL);
9287   struct breakpoint *breakpoint;
9288   struct breakpoint *breakpoint2 = NULL;
9289   struct cleanup *old_chain;
9290   int thread;
9291   struct thread_info *tp;
9292 
9293   clear_proceed_status ();
9294 
9295   /* Set a breakpoint where the user wants it and at return from
9296      this function.  */
9297 
9298   if (default_breakpoint_valid)
9299     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
9300 			  default_breakpoint_line, NULL);
9301   else
9302     sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
9303 
9304   if (sals.nelts != 1)
9305     error (_("Couldn't get information on specified line."));
9306 
9307   sal = sals.sals[0];
9308   xfree (sals.sals);	/* malloc'd, so freed.  */
9309 
9310   if (*arg)
9311     error (_("Junk at end of arguments."));
9312 
9313   resolve_sal_pc (&sal);
9314 
9315   if (anywhere)
9316     /* If the user told us to continue until a specified location,
9317        we don't specify a frame at which we need to stop.  */
9318     breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9319 					   null_frame_id, bp_until);
9320   else
9321     /* Otherwise, specify the selected frame, because we want to stop
9322        only at the very same frame.  */
9323     breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9324 					   get_stack_frame_id (frame),
9325 					   bp_until);
9326 
9327   old_chain = make_cleanup_delete_breakpoint (breakpoint);
9328 
9329   tp = inferior_thread ();
9330   thread = tp->num;
9331 
9332   /* Keep within the current frame, or in frames called by the current
9333      one.  */
9334 
9335   if (frame_id_p (frame_unwind_caller_id (frame)))
9336     {
9337       sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9338       sal.pc = frame_unwind_caller_pc (frame);
9339       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9340 					      sal,
9341 					      frame_unwind_caller_id (frame),
9342 					      bp_until);
9343       make_cleanup_delete_breakpoint (breakpoint2);
9344 
9345       set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9346       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
9347     }
9348 
9349   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
9350 
9351   /* If we are running asynchronously, and proceed call above has
9352      actually managed to start the target, arrange for breakpoints to
9353      be deleted when the target stops.  Otherwise, we're already
9354      stopped and delete breakpoints via cleanup chain.  */
9355 
9356   if (target_can_async_p () && is_running (inferior_ptid))
9357     {
9358       struct until_break_command_continuation_args *args;
9359       args = xmalloc (sizeof (*args));
9360 
9361       args->breakpoint = breakpoint;
9362       args->breakpoint2 = breakpoint2;
9363       args->thread_num = thread;
9364 
9365       discard_cleanups (old_chain);
9366       add_continuation (inferior_thread (),
9367 			until_break_command_continuation, args,
9368 			xfree);
9369     }
9370   else
9371     do_cleanups (old_chain);
9372 }
9373 
9374 /* This function attempts to parse an optional "if <cond>" clause
9375    from the arg string.  If one is not found, it returns NULL.
9376 
9377    Else, it returns a pointer to the condition string.  (It does not
9378    attempt to evaluate the string against a particular block.)  And,
9379    it updates arg to point to the first character following the parsed
9380    if clause in the arg string.  */
9381 
9382 static char *
9383 ep_parse_optional_if_clause (char **arg)
9384 {
9385   char *cond_string;
9386 
9387   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
9388     return NULL;
9389 
9390   /* Skip the "if" keyword.  */
9391   (*arg) += 2;
9392 
9393   /* Skip any extra leading whitespace, and record the start of the
9394      condition string.  */
9395   *arg = skip_spaces (*arg);
9396   cond_string = *arg;
9397 
9398   /* Assume that the condition occupies the remainder of the arg
9399      string.  */
9400   (*arg) += strlen (cond_string);
9401 
9402   return cond_string;
9403 }
9404 
9405 /* Commands to deal with catching events, such as signals, exceptions,
9406    process start/exit, etc.  */
9407 
9408 typedef enum
9409 {
9410   catch_fork_temporary, catch_vfork_temporary,
9411   catch_fork_permanent, catch_vfork_permanent
9412 }
9413 catch_fork_kind;
9414 
9415 static void
9416 catch_fork_command_1 (char *arg, int from_tty,
9417 		      struct cmd_list_element *command)
9418 {
9419   struct gdbarch *gdbarch = get_current_arch ();
9420   char *cond_string = NULL;
9421   catch_fork_kind fork_kind;
9422   int tempflag;
9423 
9424   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9425   tempflag = (fork_kind == catch_fork_temporary
9426 	      || fork_kind == catch_vfork_temporary);
9427 
9428   if (!arg)
9429     arg = "";
9430   arg = skip_spaces (arg);
9431 
9432   /* The allowed syntax is:
9433      catch [v]fork
9434      catch [v]fork if <cond>
9435 
9436      First, check if there's an if clause.  */
9437   cond_string = ep_parse_optional_if_clause (&arg);
9438 
9439   if ((*arg != '\0') && !isspace (*arg))
9440     error (_("Junk at end of arguments."));
9441 
9442   /* If this target supports it, create a fork or vfork catchpoint
9443      and enable reporting of such events.  */
9444   switch (fork_kind)
9445     {
9446     case catch_fork_temporary:
9447     case catch_fork_permanent:
9448       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
9449                                           &catch_fork_breakpoint_ops);
9450       break;
9451     case catch_vfork_temporary:
9452     case catch_vfork_permanent:
9453       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
9454                                           &catch_vfork_breakpoint_ops);
9455       break;
9456     default:
9457       error (_("unsupported or unknown fork kind; cannot catch it"));
9458       break;
9459     }
9460 }
9461 
9462 static void
9463 catch_exec_command_1 (char *arg, int from_tty,
9464 		      struct cmd_list_element *command)
9465 {
9466   struct gdbarch *gdbarch = get_current_arch ();
9467   int tempflag;
9468   char *cond_string = NULL;
9469 
9470   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9471 
9472   if (!arg)
9473     arg = "";
9474   arg = skip_spaces (arg);
9475 
9476   /* The allowed syntax is:
9477      catch exec
9478      catch exec if <cond>
9479 
9480      First, check if there's an if clause.  */
9481   cond_string = ep_parse_optional_if_clause (&arg);
9482 
9483   if ((*arg != '\0') && !isspace (*arg))
9484     error (_("Junk at end of arguments."));
9485 
9486   /* If this target supports it, create an exec catchpoint
9487      and enable reporting of such events.  */
9488   create_catchpoint (gdbarch, tempflag, cond_string,
9489 		     &catch_exec_breakpoint_ops);
9490 }
9491 
9492 static enum print_stop_action
9493 print_exception_catchpoint (struct breakpoint *b)
9494 {
9495   int bp_temp, bp_throw;
9496 
9497   annotate_catchpoint (b->number);
9498 
9499   bp_throw = strstr (b->addr_string, "throw") != NULL;
9500   if (b->loc->address != b->loc->requested_address)
9501     breakpoint_adjustment_warning (b->loc->requested_address,
9502 	                           b->loc->address,
9503 				   b->number, 1);
9504   bp_temp = b->disposition == disp_del;
9505   ui_out_text (uiout,
9506 	       bp_temp ? "Temporary catchpoint "
9507 		       : "Catchpoint ");
9508   if (!ui_out_is_mi_like_p (uiout))
9509     ui_out_field_int (uiout, "bkptno", b->number);
9510   ui_out_text (uiout,
9511 	       bp_throw ? " (exception thrown), "
9512 		        : " (exception caught), ");
9513   if (ui_out_is_mi_like_p (uiout))
9514     {
9515       ui_out_field_string (uiout, "reason",
9516 			   async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9517       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9518       ui_out_field_int (uiout, "bkptno", b->number);
9519     }
9520   return PRINT_SRC_AND_LOC;
9521 }
9522 
9523 static void
9524 print_one_exception_catchpoint (struct breakpoint *b,
9525 				struct bp_location **last_loc)
9526 {
9527   struct value_print_options opts;
9528 
9529   get_user_print_options (&opts);
9530   if (opts.addressprint)
9531     {
9532       annotate_field (4);
9533       if (b->loc == NULL || b->loc->shlib_disabled)
9534 	ui_out_field_string (uiout, "addr", "<PENDING>");
9535       else
9536 	ui_out_field_core_addr (uiout, "addr",
9537 				b->loc->gdbarch, b->loc->address);
9538     }
9539   annotate_field (5);
9540   if (b->loc)
9541     *last_loc = b->loc;
9542   if (strstr (b->addr_string, "throw") != NULL)
9543     ui_out_field_string (uiout, "what", "exception throw");
9544   else
9545     ui_out_field_string (uiout, "what", "exception catch");
9546 }
9547 
9548 static void
9549 print_mention_exception_catchpoint (struct breakpoint *b)
9550 {
9551   int bp_temp;
9552   int bp_throw;
9553 
9554   bp_temp = b->disposition == disp_del;
9555   bp_throw = strstr (b->addr_string, "throw") != NULL;
9556   ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9557 			      : _("Catchpoint "));
9558   ui_out_field_int (uiout, "bkptno", b->number);
9559   ui_out_text (uiout, bp_throw ? _(" (throw)")
9560 			       : _(" (catch)"));
9561 }
9562 
9563 /* Implement the "print_recreate" breakpoint_ops method for throw and
9564    catch catchpoints.  */
9565 
9566 static void
9567 print_recreate_exception_catchpoint (struct breakpoint *b,
9568 				     struct ui_file *fp)
9569 {
9570   int bp_temp;
9571   int bp_throw;
9572 
9573   bp_temp = b->disposition == disp_del;
9574   bp_throw = strstr (b->addr_string, "throw") != NULL;
9575   fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9576   fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9577 }
9578 
9579 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
9580   NULL, /* insert */
9581   NULL, /* remove */
9582   NULL, /* breakpoint_hit */
9583   NULL, /* resources_needed */
9584   print_exception_catchpoint,
9585   print_one_exception_catchpoint,
9586   NULL, /* print_one_detail */
9587   print_mention_exception_catchpoint,
9588   print_recreate_exception_catchpoint
9589 };
9590 
9591 static int
9592 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9593 			  enum exception_event_kind ex_event, int from_tty)
9594 {
9595   char *trigger_func_name;
9596 
9597   if (ex_event == EX_EVENT_CATCH)
9598     trigger_func_name = "__cxa_begin_catch";
9599   else
9600     trigger_func_name = "__cxa_throw";
9601 
9602   create_breakpoint (get_current_arch (),
9603 		     trigger_func_name, cond_string, -1,
9604 		     0 /* condition and thread are valid.  */,
9605 		     tempflag, bp_breakpoint,
9606 		     0,
9607 		     AUTO_BOOLEAN_TRUE /* pending */,
9608 		     &gnu_v3_exception_catchpoint_ops, from_tty,
9609 		     1 /* enabled */,
9610 		     0 /* internal */);
9611 
9612   return 1;
9613 }
9614 
9615 /* Deal with "catch catch" and "catch throw" commands.  */
9616 
9617 static void
9618 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9619 			   int tempflag, int from_tty)
9620 {
9621   char *cond_string = NULL;
9622 
9623   if (!arg)
9624     arg = "";
9625   arg = skip_spaces (arg);
9626 
9627   cond_string = ep_parse_optional_if_clause (&arg);
9628 
9629   if ((*arg != '\0') && !isspace (*arg))
9630     error (_("Junk at end of arguments."));
9631 
9632   if (ex_event != EX_EVENT_THROW
9633       && ex_event != EX_EVENT_CATCH)
9634     error (_("Unsupported or unknown exception event; cannot catch it"));
9635 
9636   if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9637     return;
9638 
9639   warning (_("Unsupported with this platform/compiler combination."));
9640 }
9641 
9642 /* Implementation of "catch catch" command.  */
9643 
9644 static void
9645 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9646 {
9647   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9648 
9649   catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9650 }
9651 
9652 /* Implementation of "catch throw" command.  */
9653 
9654 static void
9655 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9656 {
9657   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9658 
9659   catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9660 }
9661 
9662 /* Create a breakpoint struct for Ada exception catchpoints.  */
9663 
9664 static void
9665 create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9666 				 struct symtab_and_line sal,
9667                                  char *addr_string,
9668                                  char *exp_string,
9669                                  char *cond_string,
9670                                  struct expression *cond,
9671                                  struct breakpoint_ops *ops,
9672                                  int tempflag,
9673                                  int from_tty)
9674 {
9675   struct breakpoint *b;
9676 
9677   if (from_tty)
9678     {
9679       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9680       if (!loc_gdbarch)
9681 	loc_gdbarch = gdbarch;
9682 
9683       describe_other_breakpoints (loc_gdbarch,
9684 				  sal.pspace, sal.pc, sal.section, -1);
9685       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9686          version for exception catchpoints, because two catchpoints
9687          used for different exception names will use the same address.
9688          In this case, a "breakpoint ... also set at..." warning is
9689          unproductive.  Besides, the warning phrasing is also a bit
9690          inapropriate, we should use the word catchpoint, and tell
9691          the user what type of catchpoint it is.  The above is good
9692          enough for now, though.  */
9693     }
9694 
9695   b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
9696   set_breakpoint_count (breakpoint_count + 1);
9697 
9698   b->enable_state = bp_enabled;
9699   b->disposition = tempflag ? disp_del : disp_donttouch;
9700   b->number = breakpoint_count;
9701   b->ignore_count = 0;
9702   b->loc->cond = cond;
9703   b->addr_string = addr_string;
9704   b->language = language_ada;
9705   b->cond_string = cond_string;
9706   b->exp_string = exp_string;
9707   b->thread = -1;
9708   b->ops = ops;
9709 
9710   mention (b);
9711   update_global_location_list (1);
9712 }
9713 
9714 /* Implement the "catch exception" command.  */
9715 
9716 static void
9717 catch_ada_exception_command (char *arg, int from_tty,
9718 			     struct cmd_list_element *command)
9719 {
9720   struct gdbarch *gdbarch = get_current_arch ();
9721   int tempflag;
9722   struct symtab_and_line sal;
9723   char *addr_string = NULL;
9724   char *exp_string = NULL;
9725   char *cond_string = NULL;
9726   struct expression *cond = NULL;
9727   struct breakpoint_ops *ops = NULL;
9728 
9729   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9730 
9731   if (!arg)
9732     arg = "";
9733   sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9734                                        &cond_string, &cond, &ops);
9735   create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
9736                                    cond_string, cond, ops, tempflag,
9737                                    from_tty);
9738 }
9739 
9740 /* Cleanup function for a syscall filter list.  */
9741 static void
9742 clean_up_filters (void *arg)
9743 {
9744   VEC(int) *iter = *(VEC(int) **) arg;
9745   VEC_free (int, iter);
9746 }
9747 
9748 /* Splits the argument using space as delimiter.  Returns an xmalloc'd
9749    filter list, or NULL if no filtering is required.  */
9750 static VEC(int) *
9751 catch_syscall_split_args (char *arg)
9752 {
9753   VEC(int) *result = NULL;
9754   struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9755 
9756   while (*arg != '\0')
9757     {
9758       int i, syscall_number;
9759       char *endptr;
9760       char cur_name[128];
9761       struct syscall s;
9762 
9763       /* Skip whitespace.  */
9764       while (isspace (*arg))
9765 	arg++;
9766 
9767       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9768 	cur_name[i] = arg[i];
9769       cur_name[i] = '\0';
9770       arg += i;
9771 
9772       /* Check if the user provided a syscall name or a number.  */
9773       syscall_number = (int) strtol (cur_name, &endptr, 0);
9774       if (*endptr == '\0')
9775 	get_syscall_by_number (syscall_number, &s);
9776       else
9777 	{
9778 	  /* We have a name.  Let's check if it's valid and convert it
9779 	     to a number.  */
9780 	  get_syscall_by_name (cur_name, &s);
9781 
9782 	  if (s.number == UNKNOWN_SYSCALL)
9783 	    /* Here we have to issue an error instead of a warning,
9784 	       because GDB cannot do anything useful if there's no
9785 	       syscall number to be caught.  */
9786 	    error (_("Unknown syscall name '%s'."), cur_name);
9787 	}
9788 
9789       /* Ok, it's valid.  */
9790       VEC_safe_push (int, result, s.number);
9791     }
9792 
9793   discard_cleanups (cleanup);
9794   return result;
9795 }
9796 
9797 /* Implement the "catch syscall" command.  */
9798 
9799 static void
9800 catch_syscall_command_1 (char *arg, int from_tty,
9801 			 struct cmd_list_element *command)
9802 {
9803   int tempflag;
9804   VEC(int) *filter;
9805   struct syscall s;
9806   struct gdbarch *gdbarch = get_current_arch ();
9807 
9808   /* Checking if the feature if supported.  */
9809   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9810     error (_("The feature 'catch syscall' is not supported on \
9811 this architecture yet."));
9812 
9813   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9814 
9815   arg = skip_spaces (arg);
9816 
9817   /* We need to do this first "dummy" translation in order
9818      to get the syscall XML file loaded or, most important,
9819      to display a warning to the user if there's no XML file
9820      for his/her architecture.  */
9821   get_syscall_by_number (0, &s);
9822 
9823   /* The allowed syntax is:
9824      catch syscall
9825      catch syscall <name | number> [<name | number> ... <name | number>]
9826 
9827      Let's check if there's a syscall name.  */
9828 
9829   if (arg != NULL)
9830     filter = catch_syscall_split_args (arg);
9831   else
9832     filter = NULL;
9833 
9834   create_syscall_event_catchpoint (tempflag, filter,
9835 				   &catch_syscall_breakpoint_ops);
9836 }
9837 
9838 /* Implement the "catch assert" command.  */
9839 
9840 static void
9841 catch_assert_command (char *arg, int from_tty,
9842 		      struct cmd_list_element *command)
9843 {
9844   struct gdbarch *gdbarch = get_current_arch ();
9845   int tempflag;
9846   struct symtab_and_line sal;
9847   char *addr_string = NULL;
9848   struct breakpoint_ops *ops = NULL;
9849 
9850   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9851 
9852   if (!arg)
9853     arg = "";
9854   sal = ada_decode_assert_location (arg, &addr_string, &ops);
9855   create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9856 				   ops, tempflag, from_tty);
9857 }
9858 
9859 static void
9860 catch_command (char *arg, int from_tty)
9861 {
9862   error (_("Catch requires an event name."));
9863 }
9864 
9865 
9866 static void
9867 tcatch_command (char *arg, int from_tty)
9868 {
9869   error (_("Catch requires an event name."));
9870 }
9871 
9872 /* Delete breakpoints by address or line.  */
9873 
9874 static void
9875 clear_command (char *arg, int from_tty)
9876 {
9877   struct breakpoint *b;
9878   VEC(breakpoint_p) *found = 0;
9879   int ix;
9880   int default_match;
9881   struct symtabs_and_lines sals;
9882   struct symtab_and_line sal;
9883   int i;
9884 
9885   if (arg)
9886     {
9887       sals = decode_line_spec (arg, 1);
9888       default_match = 0;
9889     }
9890   else
9891     {
9892       sals.sals = (struct symtab_and_line *)
9893 	xmalloc (sizeof (struct symtab_and_line));
9894       make_cleanup (xfree, sals.sals);
9895       init_sal (&sal);		/* Initialize to zeroes.  */
9896       sal.line = default_breakpoint_line;
9897       sal.symtab = default_breakpoint_symtab;
9898       sal.pc = default_breakpoint_address;
9899       sal.pspace = default_breakpoint_pspace;
9900       if (sal.symtab == 0)
9901 	error (_("No source file specified."));
9902 
9903       sals.sals[0] = sal;
9904       sals.nelts = 1;
9905 
9906       default_match = 1;
9907     }
9908 
9909   /* We don't call resolve_sal_pc here.  That's not as bad as it
9910      seems, because all existing breakpoints typically have both
9911      file/line and pc set.  So, if clear is given file/line, we can
9912      match this to existing breakpoint without obtaining pc at all.
9913 
9914      We only support clearing given the address explicitly
9915      present in breakpoint table.  Say, we've set breakpoint
9916      at file:line.  There were several PC values for that file:line,
9917      due to optimization, all in one block.
9918 
9919      We've picked one PC value.  If "clear" is issued with another
9920      PC corresponding to the same file:line, the breakpoint won't
9921      be cleared.  We probably can still clear the breakpoint, but
9922      since the other PC value is never presented to user, user
9923      can only find it by guessing, and it does not seem important
9924      to support that.  */
9925 
9926   /* For each line spec given, delete bps which correspond to it.  Do
9927      it in two passes, solely to preserve the current behavior that
9928      from_tty is forced true if we delete more than one
9929      breakpoint.  */
9930 
9931   found = NULL;
9932   for (i = 0; i < sals.nelts; i++)
9933     {
9934       /* If exact pc given, clear bpts at that pc.
9935          If line given (pc == 0), clear all bpts on specified line.
9936          If defaulting, clear all bpts on default line
9937          or at default pc.
9938 
9939          defaulting    sal.pc != 0    tests to do
9940 
9941          0              1             pc
9942          1              1             pc _and_ line
9943          0              0             line
9944          1              0             <can't happen> */
9945 
9946       sal = sals.sals[i];
9947 
9948       /* Find all matching breakpoints and add them to 'found'.  */
9949       ALL_BREAKPOINTS (b)
9950 	{
9951 	  int match = 0;
9952 	  /* Are we going to delete b?  */
9953 	  if (b->type != bp_none && !is_watchpoint (b))
9954 	    {
9955 	      struct bp_location *loc = b->loc;
9956 	      for (; loc; loc = loc->next)
9957 		{
9958 		  int pc_match = sal.pc
9959 		    && (loc->pspace == sal.pspace)
9960 		    && (loc->address == sal.pc)
9961 		    && (!section_is_overlay (loc->section)
9962 			|| loc->section == sal.section);
9963 		  int line_match = ((default_match || (0 == sal.pc))
9964 				    && b->source_file != NULL
9965 				    && sal.symtab != NULL
9966 				    && sal.pspace == loc->pspace
9967 				    && filename_cmp (b->source_file,
9968 						     sal.symtab->filename) == 0
9969 				    && b->line_number == sal.line);
9970 		  if (pc_match || line_match)
9971 		    {
9972 		      match = 1;
9973 		      break;
9974 		    }
9975 		}
9976 	    }
9977 
9978 	  if (match)
9979 	    VEC_safe_push(breakpoint_p, found, b);
9980 	}
9981     }
9982   /* Now go thru the 'found' chain and delete them.  */
9983   if (VEC_empty(breakpoint_p, found))
9984     {
9985       if (arg)
9986 	error (_("No breakpoint at %s."), arg);
9987       else
9988 	error (_("No breakpoint at this line."));
9989     }
9990 
9991   if (VEC_length(breakpoint_p, found) > 1)
9992     from_tty = 1;	/* Always report if deleted more than one.  */
9993   if (from_tty)
9994     {
9995       if (VEC_length(breakpoint_p, found) == 1)
9996 	printf_unfiltered (_("Deleted breakpoint "));
9997       else
9998 	printf_unfiltered (_("Deleted breakpoints "));
9999     }
10000   breakpoints_changed ();
10001 
10002   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
10003     {
10004       if (from_tty)
10005 	printf_unfiltered ("%d ", b->number);
10006       delete_breakpoint (b);
10007     }
10008   if (from_tty)
10009     putchar_unfiltered ('\n');
10010 }
10011 
10012 /* Delete breakpoint in BS if they are `delete' breakpoints and
10013    all breakpoints that are marked for deletion, whether hit or not.
10014    This is called after any breakpoint is hit, or after errors.  */
10015 
10016 void
10017 breakpoint_auto_delete (bpstat bs)
10018 {
10019   struct breakpoint *b, *b_tmp;
10020 
10021   for (; bs; bs = bs->next)
10022     if (bs->breakpoint_at
10023 	&& bs->breakpoint_at->disposition == disp_del
10024 	&& bs->stop)
10025       delete_breakpoint (bs->breakpoint_at);
10026 
10027   ALL_BREAKPOINTS_SAFE (b, b_tmp)
10028   {
10029     if (b->disposition == disp_del_at_next_stop)
10030       delete_breakpoint (b);
10031   }
10032 }
10033 
10034 /* A comparison function for bp_location AP and BP being interfaced to
10035    qsort.  Sort elements primarily by their ADDRESS (no matter what
10036    does breakpoint_address_is_meaningful say for its OWNER),
10037    secondarily by ordering first bp_permanent OWNERed elements and
10038    terciarily just ensuring the array is sorted stable way despite
10039    qsort being an instable algorithm.  */
10040 
10041 static int
10042 bp_location_compare (const void *ap, const void *bp)
10043 {
10044   struct bp_location *a = *(void **) ap;
10045   struct bp_location *b = *(void **) bp;
10046   /* A and B come from existing breakpoints having non-NULL OWNER.  */
10047   int a_perm = a->owner->enable_state == bp_permanent;
10048   int b_perm = b->owner->enable_state == bp_permanent;
10049 
10050   if (a->address != b->address)
10051     return (a->address > b->address) - (a->address < b->address);
10052 
10053   /* Sort permanent breakpoints first.  */
10054   if (a_perm != b_perm)
10055     return (a_perm < b_perm) - (a_perm > b_perm);
10056 
10057   /* Make the user-visible order stable across GDB runs.  Locations of
10058      the same breakpoint can be sorted in arbitrary order.  */
10059 
10060   if (a->owner->number != b->owner->number)
10061     return (a->owner->number > b->owner->number)
10062            - (a->owner->number < b->owner->number);
10063 
10064   return (a > b) - (a < b);
10065 }
10066 
10067 /* Set bp_location_placed_address_before_address_max and
10068    bp_location_shadow_len_after_address_max according to the current
10069    content of the bp_location array.  */
10070 
10071 static void
10072 bp_location_target_extensions_update (void)
10073 {
10074   struct bp_location *bl, **blp_tmp;
10075 
10076   bp_location_placed_address_before_address_max = 0;
10077   bp_location_shadow_len_after_address_max = 0;
10078 
10079   ALL_BP_LOCATIONS (bl, blp_tmp)
10080     {
10081       CORE_ADDR start, end, addr;
10082 
10083       if (!bp_location_has_shadow (bl))
10084 	continue;
10085 
10086       start = bl->target_info.placed_address;
10087       end = start + bl->target_info.shadow_len;
10088 
10089       gdb_assert (bl->address >= start);
10090       addr = bl->address - start;
10091       if (addr > bp_location_placed_address_before_address_max)
10092 	bp_location_placed_address_before_address_max = addr;
10093 
10094       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
10095 
10096       gdb_assert (bl->address < end);
10097       addr = end - bl->address;
10098       if (addr > bp_location_shadow_len_after_address_max)
10099 	bp_location_shadow_len_after_address_max = addr;
10100     }
10101 }
10102 
10103 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
10104    into the inferior, only remove already-inserted locations that no
10105    longer should be inserted.  Functions that delete a breakpoint or
10106    breakpoints should pass false, so that deleting a breakpoint
10107    doesn't have the side effect of inserting the locations of other
10108    breakpoints that are marked not-inserted, but should_be_inserted
10109    returns true on them.
10110 
10111    This behaviour is useful is situations close to tear-down -- e.g.,
10112    after an exec, while the target still has execution, but breakpoint
10113    shadows of the previous executable image should *NOT* be restored
10114    to the new image; or before detaching, where the target still has
10115    execution and wants to delete breakpoints from GDB's lists, and all
10116    breakpoints had already been removed from the inferior.  */
10117 
10118 static void
10119 update_global_location_list (int should_insert)
10120 {
10121   struct breakpoint *b;
10122   struct bp_location **locp, *loc;
10123   struct cleanup *cleanups;
10124 
10125   /* Used in the duplicates detection below.  When iterating over all
10126      bp_locations, points to the first bp_location of a given address.
10127      Breakpoints and watchpoints of different types are never
10128      duplicates of each other.  Keep one pointer for each type of
10129      breakpoint/watchpoint, so we only need to loop over all locations
10130      once.  */
10131   struct bp_location *bp_loc_first;  /* breakpoint */
10132   struct bp_location *wp_loc_first;  /* hardware watchpoint */
10133   struct bp_location *awp_loc_first; /* access watchpoint */
10134   struct bp_location *rwp_loc_first; /* read watchpoint */
10135 
10136   /* Saved former bp_location array which we compare against the newly
10137      built bp_location from the current state of ALL_BREAKPOINTS.  */
10138   struct bp_location **old_location, **old_locp;
10139   unsigned old_location_count;
10140 
10141   old_location = bp_location;
10142   old_location_count = bp_location_count;
10143   bp_location = NULL;
10144   bp_location_count = 0;
10145   cleanups = make_cleanup (xfree, old_location);
10146 
10147   ALL_BREAKPOINTS (b)
10148     for (loc = b->loc; loc; loc = loc->next)
10149       bp_location_count++;
10150 
10151   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10152   locp = bp_location;
10153   ALL_BREAKPOINTS (b)
10154     for (loc = b->loc; loc; loc = loc->next)
10155       *locp++ = loc;
10156   qsort (bp_location, bp_location_count, sizeof (*bp_location),
10157 	 bp_location_compare);
10158 
10159   bp_location_target_extensions_update ();
10160 
10161   /* Identify bp_location instances that are no longer present in the
10162      new list, and therefore should be freed.  Note that it's not
10163      necessary that those locations should be removed from inferior --
10164      if there's another location at the same address (previously
10165      marked as duplicate), we don't need to remove/insert the
10166      location.
10167 
10168      LOCP is kept in sync with OLD_LOCP, each pointing to the current
10169      and former bp_location array state respectively.  */
10170 
10171   locp = bp_location;
10172   for (old_locp = old_location; old_locp < old_location + old_location_count;
10173        old_locp++)
10174     {
10175       struct bp_location *old_loc = *old_locp;
10176       struct bp_location **loc2p;
10177 
10178       /* Tells if 'old_loc' is found amoung the new locations.  If
10179 	 not, we have to free it.  */
10180       int found_object = 0;
10181       /* Tells if the location should remain inserted in the target.  */
10182       int keep_in_target = 0;
10183       int removed = 0;
10184 
10185       /* Skip LOCP entries which will definitely never be needed.
10186 	 Stop either at or being the one matching OLD_LOC.  */
10187       while (locp < bp_location + bp_location_count
10188 	     && (*locp)->address < old_loc->address)
10189 	locp++;
10190 
10191       for (loc2p = locp;
10192 	   (loc2p < bp_location + bp_location_count
10193 	    && (*loc2p)->address == old_loc->address);
10194 	   loc2p++)
10195 	{
10196 	  if (*loc2p == old_loc)
10197 	    {
10198 	      found_object = 1;
10199 	      break;
10200 	    }
10201 	}
10202 
10203       /* If this location is no longer present, and inserted, look if
10204 	 there's maybe a new location at the same address.  If so,
10205 	 mark that one inserted, and don't remove this one.  This is
10206 	 needed so that we don't have a time window where a breakpoint
10207 	 at certain location is not inserted.  */
10208 
10209       if (old_loc->inserted)
10210 	{
10211 	  /* If the location is inserted now, we might have to remove
10212 	     it.  */
10213 
10214 	  if (found_object && should_be_inserted (old_loc))
10215 	    {
10216 	      /* The location is still present in the location list,
10217 		 and still should be inserted.  Don't do anything.  */
10218 	      keep_in_target = 1;
10219 	    }
10220 	  else
10221 	    {
10222 	      /* The location is either no longer present, or got
10223 		 disabled.  See if there's another location at the
10224 		 same address, in which case we don't need to remove
10225 		 this one from the target.  */
10226 
10227 	      /* OLD_LOC comes from existing struct breakpoint.  */
10228 	      if (breakpoint_address_is_meaningful (old_loc->owner))
10229 		{
10230 		  for (loc2p = locp;
10231 		       (loc2p < bp_location + bp_location_count
10232 			&& (*loc2p)->address == old_loc->address);
10233 		       loc2p++)
10234 		    {
10235 		      struct bp_location *loc2 = *loc2p;
10236 
10237 		      if (breakpoint_locations_match (loc2, old_loc))
10238 			{
10239 			  /* For the sake of should_be_inserted.
10240 			     Duplicates check below will fix up this
10241 			     later.  */
10242 			  loc2->duplicate = 0;
10243 
10244 			  /* Read watchpoint locations are switched to
10245 			     access watchpoints, if the former are not
10246 			     supported, but the latter are.  */
10247 			  if (is_hardware_watchpoint (old_loc->owner))
10248 			    {
10249 			      gdb_assert (is_hardware_watchpoint (loc2->owner));
10250 			      loc2->watchpoint_type = old_loc->watchpoint_type;
10251 			    }
10252 
10253 			  if (loc2 != old_loc && should_be_inserted (loc2))
10254 			    {
10255 			      loc2->inserted = 1;
10256 			      loc2->target_info = old_loc->target_info;
10257 			      keep_in_target = 1;
10258 			      break;
10259 			    }
10260 			}
10261 		    }
10262 		}
10263 	    }
10264 
10265 	  if (!keep_in_target)
10266 	    {
10267 	      if (remove_breakpoint (old_loc, mark_uninserted))
10268 		{
10269 		  /* This is just about all we can do.  We could keep
10270 		     this location on the global list, and try to
10271 		     remove it next time, but there's no particular
10272 		     reason why we will succeed next time.
10273 
10274 		     Note that at this point, old_loc->owner is still
10275 		     valid, as delete_breakpoint frees the breakpoint
10276 		     only after calling us.  */
10277 		  printf_filtered (_("warning: Error removing "
10278 				     "breakpoint %d\n"),
10279 				   old_loc->owner->number);
10280 		}
10281 	      removed = 1;
10282 	    }
10283 	}
10284 
10285       if (!found_object)
10286 	{
10287 	  if (removed && non_stop
10288 	      && breakpoint_address_is_meaningful (old_loc->owner)
10289 	      && !is_hardware_watchpoint (old_loc->owner))
10290 	    {
10291 	      /* This location was removed from the target.  In
10292 		 non-stop mode, a race condition is possible where
10293 		 we've removed a breakpoint, but stop events for that
10294 		 breakpoint are already queued and will arrive later.
10295 		 We apply an heuristic to be able to distinguish such
10296 		 SIGTRAPs from other random SIGTRAPs: we keep this
10297 		 breakpoint location for a bit, and will retire it
10298 		 after we see some number of events.  The theory here
10299 		 is that reporting of events should, "on the average",
10300 		 be fair, so after a while we'll see events from all
10301 		 threads that have anything of interest, and no longer
10302 		 need to keep this breakpoint location around.  We
10303 		 don't hold locations forever so to reduce chances of
10304 		 mistaking a non-breakpoint SIGTRAP for a breakpoint
10305 		 SIGTRAP.
10306 
10307 		 The heuristic failing can be disastrous on
10308 		 decr_pc_after_break targets.
10309 
10310 		 On decr_pc_after_break targets, like e.g., x86-linux,
10311 		 if we fail to recognize a late breakpoint SIGTRAP,
10312 		 because events_till_retirement has reached 0 too
10313 		 soon, we'll fail to do the PC adjustment, and report
10314 		 a random SIGTRAP to the user.  When the user resumes
10315 		 the inferior, it will most likely immediately crash
10316 		 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
10317 		 corrupted, because of being resumed e.g., in the
10318 		 middle of a multi-byte instruction, or skipped a
10319 		 one-byte instruction.  This was actually seen happen
10320 		 on native x86-linux, and should be less rare on
10321 		 targets that do not support new thread events, like
10322 		 remote, due to the heuristic depending on
10323 		 thread_count.
10324 
10325 		 Mistaking a random SIGTRAP for a breakpoint trap
10326 		 causes similar symptoms (PC adjustment applied when
10327 		 it shouldn't), but then again, playing with SIGTRAPs
10328 		 behind the debugger's back is asking for trouble.
10329 
10330 		 Since hardware watchpoint traps are always
10331 		 distinguishable from other traps, so we don't need to
10332 		 apply keep hardware watchpoint moribund locations
10333 		 around.  We simply always ignore hardware watchpoint
10334 		 traps we can no longer explain.  */
10335 
10336 	      old_loc->events_till_retirement = 3 * (thread_count () + 1);
10337 	      old_loc->owner = NULL;
10338 
10339 	      VEC_safe_push (bp_location_p, moribund_locations, old_loc);
10340 	    }
10341 	  else
10342 	    {
10343 	      old_loc->owner = NULL;
10344 	      decref_bp_location (&old_loc);
10345 	    }
10346 	}
10347     }
10348 
10349   /* Rescan breakpoints at the same address and section, marking the
10350      first one as "first" and any others as "duplicates".  This is so
10351      that the bpt instruction is only inserted once.  If we have a
10352      permanent breakpoint at the same place as BPT, make that one the
10353      official one, and the rest as duplicates.  Permanent breakpoints
10354      are sorted first for the same address.
10355 
10356      Do the same for hardware watchpoints, but also considering the
10357      watchpoint's type (regular/access/read) and length.  */
10358 
10359   bp_loc_first = NULL;
10360   wp_loc_first = NULL;
10361   awp_loc_first = NULL;
10362   rwp_loc_first = NULL;
10363   ALL_BP_LOCATIONS (loc, locp)
10364     {
10365       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10366 	 non-NULL.  */
10367       struct breakpoint *b = loc->owner;
10368       struct bp_location **loc_first_p;
10369 
10370       if (b->enable_state == bp_disabled
10371 	  || b->enable_state == bp_call_disabled
10372 	  || b->enable_state == bp_startup_disabled
10373 	  || !loc->enabled
10374 	  || loc->shlib_disabled
10375 	  || !breakpoint_address_is_meaningful (b)
10376 	  || is_tracepoint (b))
10377 	continue;
10378 
10379       /* Permanent breakpoint should always be inserted.  */
10380       if (b->enable_state == bp_permanent && ! loc->inserted)
10381 	internal_error (__FILE__, __LINE__,
10382 			_("allegedly permanent breakpoint is not "
10383 			"actually inserted"));
10384 
10385       if (b->type == bp_hardware_watchpoint)
10386 	loc_first_p = &wp_loc_first;
10387       else if (b->type == bp_read_watchpoint)
10388 	loc_first_p = &rwp_loc_first;
10389       else if (b->type == bp_access_watchpoint)
10390 	loc_first_p = &awp_loc_first;
10391       else
10392 	loc_first_p = &bp_loc_first;
10393 
10394       if (*loc_first_p == NULL
10395 	  || (overlay_debugging && loc->section != (*loc_first_p)->section)
10396 	  || !breakpoint_locations_match (loc, *loc_first_p))
10397 	{
10398 	  *loc_first_p = loc;
10399 	  loc->duplicate = 0;
10400 	  continue;
10401 	}
10402 
10403       loc->duplicate = 1;
10404 
10405       if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10406 	  && b->enable_state != bp_permanent)
10407 	internal_error (__FILE__, __LINE__,
10408 			_("another breakpoint was inserted on top of "
10409 			"a permanent breakpoint"));
10410     }
10411 
10412   if (breakpoints_always_inserted_mode () && should_insert
10413       && (have_live_inferiors ()
10414 	  || (gdbarch_has_global_breakpoints (target_gdbarch))))
10415     insert_breakpoint_locations ();
10416 
10417   do_cleanups (cleanups);
10418 }
10419 
10420 void
10421 breakpoint_retire_moribund (void)
10422 {
10423   struct bp_location *loc;
10424   int ix;
10425 
10426   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10427     if (--(loc->events_till_retirement) == 0)
10428       {
10429 	decref_bp_location (&loc);
10430 	VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10431 	--ix;
10432       }
10433 }
10434 
10435 static void
10436 update_global_location_list_nothrow (int inserting)
10437 {
10438   struct gdb_exception e;
10439 
10440   TRY_CATCH (e, RETURN_MASK_ERROR)
10441     update_global_location_list (inserting);
10442 }
10443 
10444 /* Clear BKP from a BPS.  */
10445 
10446 static void
10447 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10448 {
10449   bpstat bs;
10450 
10451   for (bs = bps; bs; bs = bs->next)
10452     if (bs->breakpoint_at == bpt)
10453       {
10454 	bs->breakpoint_at = NULL;
10455 	bs->old_val = NULL;
10456 	/* bs->commands will be freed later.  */
10457       }
10458 }
10459 
10460 /* Callback for iterate_over_threads.  */
10461 static int
10462 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10463 {
10464   struct breakpoint *bpt = data;
10465 
10466   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10467   return 0;
10468 }
10469 
10470 /* Delete a breakpoint and clean up all traces of it in the data
10471    structures.  */
10472 
10473 void
10474 delete_breakpoint (struct breakpoint *bpt)
10475 {
10476   struct breakpoint *b;
10477 
10478   gdb_assert (bpt != NULL);
10479 
10480   /* Has this bp already been deleted?  This can happen because
10481      multiple lists can hold pointers to bp's.  bpstat lists are
10482      especial culprits.
10483 
10484      One example of this happening is a watchpoint's scope bp.  When
10485      the scope bp triggers, we notice that the watchpoint is out of
10486      scope, and delete it.  We also delete its scope bp.  But the
10487      scope bp is marked "auto-deleting", and is already on a bpstat.
10488      That bpstat is then checked for auto-deleting bp's, which are
10489      deleted.
10490 
10491      A real solution to this problem might involve reference counts in
10492      bp's, and/or giving them pointers back to their referencing
10493      bpstat's, and teaching delete_breakpoint to only free a bp's
10494      storage when no more references were extent.  A cheaper bandaid
10495      was chosen.  */
10496   if (bpt->type == bp_none)
10497     return;
10498 
10499   /* At least avoid this stale reference until the reference counting
10500      of breakpoints gets resolved.  */
10501   if (bpt->related_breakpoint != bpt)
10502     {
10503       struct breakpoint *related;
10504 
10505       if (bpt->type == bp_watchpoint_scope)
10506 	watchpoint_del_at_next_stop (bpt->related_breakpoint);
10507       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
10508 	watchpoint_del_at_next_stop (bpt);
10509 
10510       /* Unlink bpt from the bpt->related_breakpoint ring.  */
10511       for (related = bpt; related->related_breakpoint != bpt;
10512 	   related = related->related_breakpoint);
10513       related->related_breakpoint = bpt->related_breakpoint;
10514       bpt->related_breakpoint = bpt;
10515     }
10516 
10517   observer_notify_breakpoint_deleted (bpt->number);
10518 
10519   if (breakpoint_chain == bpt)
10520     breakpoint_chain = bpt->next;
10521 
10522   ALL_BREAKPOINTS (b)
10523     if (b->next == bpt)
10524     {
10525       b->next = bpt->next;
10526       break;
10527     }
10528 
10529   decref_counted_command_line (&bpt->commands);
10530   xfree (bpt->cond_string);
10531   xfree (bpt->cond_exp);
10532   xfree (bpt->addr_string);
10533   xfree (bpt->addr_string_range_end);
10534   xfree (bpt->exp);
10535   xfree (bpt->exp_string);
10536   xfree (bpt->exp_string_reparse);
10537   value_free (bpt->val);
10538   xfree (bpt->source_file);
10539   xfree (bpt->exec_pathname);
10540   clean_up_filters (&bpt->syscalls_to_be_caught);
10541 
10542 
10543   /* Be sure no bpstat's are pointing at the breakpoint after it's
10544      been freed.  */
10545   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
10546      in all threeds for now.  Note that we cannot just remove bpstats
10547      pointing at bpt from the stop_bpstat list entirely, as breakpoint
10548      commands are associated with the bpstat; if we remove it here,
10549      then the later call to bpstat_do_actions (&stop_bpstat); in
10550      event-top.c won't do anything, and temporary breakpoints with
10551      commands won't work.  */
10552 
10553   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10554 
10555   /* Now that breakpoint is removed from breakpoint list, update the
10556      global location list.  This will remove locations that used to
10557      belong to this breakpoint.  Do this before freeing the breakpoint
10558      itself, since remove_breakpoint looks at location's owner.  It
10559      might be better design to have location completely
10560      self-contained, but it's not the case now.  */
10561   update_global_location_list (0);
10562 
10563 
10564   /* On the chance that someone will soon try again to delete this
10565      same bp, we mark it as deleted before freeing its storage.  */
10566   bpt->type = bp_none;
10567 
10568   xfree (bpt);
10569 }
10570 
10571 static void
10572 do_delete_breakpoint_cleanup (void *b)
10573 {
10574   delete_breakpoint (b);
10575 }
10576 
10577 struct cleanup *
10578 make_cleanup_delete_breakpoint (struct breakpoint *b)
10579 {
10580   return make_cleanup (do_delete_breakpoint_cleanup, b);
10581 }
10582 
10583 /* A callback for map_breakpoint_numbers that calls
10584    delete_breakpoint.  */
10585 
10586 static void
10587 do_delete_breakpoint (struct breakpoint *b, void *ignore)
10588 {
10589   delete_breakpoint (b);
10590 }
10591 
10592 void
10593 delete_command (char *arg, int from_tty)
10594 {
10595   struct breakpoint *b, *b_tmp;
10596 
10597   dont_repeat ();
10598 
10599   if (arg == 0)
10600     {
10601       int breaks_to_delete = 0;
10602 
10603       /* Delete all breakpoints if no argument.
10604          Do not delete internal or call-dummy breakpoints, these have
10605          to be deleted with an explicit breakpoint number argument.  */
10606       ALL_BREAKPOINTS (b)
10607       {
10608 	if (b->type != bp_call_dummy
10609 	    && b->type != bp_std_terminate
10610 	    && b->type != bp_shlib_event
10611 	    && b->type != bp_jit_event
10612 	    && b->type != bp_thread_event
10613 	    && b->type != bp_overlay_event
10614 	    && b->type != bp_longjmp_master
10615 	    && b->type != bp_std_terminate_master
10616 	    && b->type != bp_exception_master
10617 	    && b->number >= 0)
10618 	  {
10619 	    breaks_to_delete = 1;
10620 	    break;
10621 	  }
10622       }
10623 
10624       /* Ask user only if there are some breakpoints to delete.  */
10625       if (!from_tty
10626 	  || (breaks_to_delete && query (_("Delete all breakpoints? "))))
10627 	{
10628 	  ALL_BREAKPOINTS_SAFE (b, b_tmp)
10629 	  {
10630 	    if (b->type != bp_call_dummy
10631 		&& b->type != bp_std_terminate
10632 		&& b->type != bp_shlib_event
10633 		&& b->type != bp_thread_event
10634 		&& b->type != bp_jit_event
10635 		&& b->type != bp_overlay_event
10636 		&& b->type != bp_longjmp_master
10637 		&& b->type != bp_std_terminate_master
10638 		&& b->type != bp_exception_master
10639 		&& b->number >= 0)
10640 	      delete_breakpoint (b);
10641 	  }
10642 	}
10643     }
10644   else
10645     map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
10646 }
10647 
10648 static int
10649 all_locations_are_pending (struct bp_location *loc)
10650 {
10651   for (; loc; loc = loc->next)
10652     if (!loc->shlib_disabled)
10653       return 0;
10654   return 1;
10655 }
10656 
10657 /* Subroutine of update_breakpoint_locations to simplify it.
10658    Return non-zero if multiple fns in list LOC have the same name.
10659    Null names are ignored.  */
10660 
10661 static int
10662 ambiguous_names_p (struct bp_location *loc)
10663 {
10664   struct bp_location *l;
10665   htab_t htab = htab_create_alloc (13, htab_hash_string,
10666 				   (int (*) (const void *,
10667 					     const void *)) streq,
10668 				   NULL, xcalloc, xfree);
10669 
10670   for (l = loc; l != NULL; l = l->next)
10671     {
10672       const char **slot;
10673       const char *name = l->function_name;
10674 
10675       /* Allow for some names to be NULL, ignore them.  */
10676       if (name == NULL)
10677 	continue;
10678 
10679       slot = (const char **) htab_find_slot (htab, (const void *) name,
10680 					     INSERT);
10681       /* NOTE: We can assume slot != NULL here because xcalloc never
10682 	 returns NULL.  */
10683       if (*slot != NULL)
10684 	{
10685 	  htab_delete (htab);
10686 	  return 1;
10687 	}
10688       *slot = name;
10689     }
10690 
10691   htab_delete (htab);
10692   return 0;
10693 }
10694 
10695 /* When symbols change, it probably means the sources changed as well,
10696    and it might mean the static tracepoint markers are no longer at
10697    the same address or line numbers they used to be at last we
10698    checked.  Losing your static tracepoints whenever you rebuild is
10699    undesirable.  This function tries to resync/rematch gdb static
10700    tracepoints with the markers on the target, for static tracepoints
10701    that have not been set by marker id.  Static tracepoint that have
10702    been set by marker id are reset by marker id in breakpoint_re_set.
10703    The heuristic is:
10704 
10705    1) For a tracepoint set at a specific address, look for a marker at
10706    the old PC.  If one is found there, assume to be the same marker.
10707    If the name / string id of the marker found is different from the
10708    previous known name, assume that means the user renamed the marker
10709    in the sources, and output a warning.
10710 
10711    2) For a tracepoint set at a given line number, look for a marker
10712    at the new address of the old line number.  If one is found there,
10713    assume to be the same marker.  If the name / string id of the
10714    marker found is different from the previous known name, assume that
10715    means the user renamed the marker in the sources, and output a
10716    warning.
10717 
10718    3) If a marker is no longer found at the same address or line, it
10719    may mean the marker no longer exists.  But it may also just mean
10720    the code changed a bit.  Maybe the user added a few lines of code
10721    that made the marker move up or down (in line number terms).  Ask
10722    the target for info about the marker with the string id as we knew
10723    it.  If found, update line number and address in the matching
10724    static tracepoint.  This will get confused if there's more than one
10725    marker with the same ID (possible in UST, although unadvised
10726    precisely because it confuses tools).  */
10727 
10728 static struct symtab_and_line
10729 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10730 {
10731   struct static_tracepoint_marker marker;
10732   CORE_ADDR pc;
10733   int i;
10734 
10735   pc = sal.pc;
10736   if (sal.line)
10737     find_line_pc (sal.symtab, sal.line, &pc);
10738 
10739   if (target_static_tracepoint_marker_at (pc, &marker))
10740     {
10741       if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10742 	warning (_("static tracepoint %d changed probed marker from %s to %s"),
10743 		 b->number,
10744 		 b->static_trace_marker_id, marker.str_id);
10745 
10746       xfree (b->static_trace_marker_id);
10747       b->static_trace_marker_id = xstrdup (marker.str_id);
10748       release_static_tracepoint_marker (&marker);
10749 
10750       return sal;
10751     }
10752 
10753   /* Old marker wasn't found on target at lineno.  Try looking it up
10754      by string ID.  */
10755   if (!sal.explicit_pc
10756       && sal.line != 0
10757       && sal.symtab != NULL
10758       && b->static_trace_marker_id != NULL)
10759     {
10760       VEC(static_tracepoint_marker_p) *markers;
10761 
10762       markers
10763 	= target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10764 
10765       if (!VEC_empty(static_tracepoint_marker_p, markers))
10766 	{
10767 	  struct symtab_and_line sal;
10768 	  struct symbol *sym;
10769 	  struct static_tracepoint_marker *marker;
10770 
10771 	  marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10772 
10773 	  xfree (b->static_trace_marker_id);
10774 	  b->static_trace_marker_id = xstrdup (marker->str_id);
10775 
10776 	  warning (_("marker for static tracepoint %d (%s) not "
10777 		     "found at previous line number"),
10778 		   b->number, b->static_trace_marker_id);
10779 
10780 	  init_sal (&sal);
10781 
10782 	  sal.pc = marker->address;
10783 
10784 	  sal = find_pc_line (marker->address, 0);
10785 	  sym = find_pc_sect_function (marker->address, NULL);
10786 	  ui_out_text (uiout, "Now in ");
10787 	  if (sym)
10788 	    {
10789 	      ui_out_field_string (uiout, "func",
10790 				   SYMBOL_PRINT_NAME (sym));
10791 	      ui_out_text (uiout, " at ");
10792 	    }
10793 	  ui_out_field_string (uiout, "file", sal.symtab->filename);
10794 	  ui_out_text (uiout, ":");
10795 
10796 	  if (ui_out_is_mi_like_p (uiout))
10797 	    {
10798 	      char *fullname = symtab_to_fullname (sal.symtab);
10799 
10800 	      if (fullname)
10801 		ui_out_field_string (uiout, "fullname", fullname);
10802 	    }
10803 
10804 	  ui_out_field_int (uiout, "line", sal.line);
10805 	  ui_out_text (uiout, "\n");
10806 
10807 	  b->line_number = sal.line;
10808 
10809 	  xfree (b->source_file);
10810 	  if (sym)
10811 	    b->source_file = xstrdup (sal.symtab->filename);
10812 	  else
10813 	    b->source_file = NULL;
10814 
10815 	  xfree (b->addr_string);
10816 	  b->addr_string = xstrprintf ("%s:%d",
10817 				       sal.symtab->filename, b->line_number);
10818 
10819 	  /* Might be nice to check if function changed, and warn if
10820 	     so.  */
10821 
10822 	  release_static_tracepoint_marker (marker);
10823 	}
10824     }
10825   return sal;
10826 }
10827 
10828 /* Create new breakpoint locations for B (a hardware or software breakpoint)
10829    based on SALS and SALS_END.  If SALS_END.NELTS is not zero, then B is
10830    a ranged breakpoint.  */
10831 
10832 void
10833 update_breakpoint_locations (struct breakpoint *b,
10834 			     struct symtabs_and_lines sals,
10835 			     struct symtabs_and_lines sals_end)
10836 {
10837   int i;
10838   struct bp_location *existing_locations = b->loc;
10839 
10840   /* Ranged breakpoints have only one start location and one end location.  */
10841   gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
10842 
10843   /* If there's no new locations, and all existing locations are
10844      pending, don't do anything.  This optimizes the common case where
10845      all locations are in the same shared library, that was unloaded.
10846      We'd like to retain the location, so that when the library is
10847      loaded again, we don't loose the enabled/disabled status of the
10848      individual locations.  */
10849   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
10850     return;
10851 
10852   b->loc = NULL;
10853 
10854   for (i = 0; i < sals.nelts; ++i)
10855     {
10856       struct bp_location *new_loc =
10857 	add_location_to_breakpoint (b, &(sals.sals[i]));
10858 
10859       /* Reparse conditions, they might contain references to the
10860 	 old symtab.  */
10861       if (b->cond_string != NULL)
10862 	{
10863 	  char *s;
10864 	  struct gdb_exception e;
10865 
10866 	  s = b->cond_string;
10867 	  TRY_CATCH (e, RETURN_MASK_ERROR)
10868 	    {
10869 	      new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10870 					   0);
10871 	    }
10872 	  if (e.reason < 0)
10873 	    {
10874 	      warning (_("failed to reevaluate condition "
10875 			 "for breakpoint %d: %s"),
10876 		       b->number, e.message);
10877 	      new_loc->enabled = 0;
10878 	    }
10879 	}
10880 
10881       if (b->source_file != NULL)
10882 	xfree (b->source_file);
10883       if (sals.sals[i].symtab == NULL)
10884 	b->source_file = NULL;
10885       else
10886 	b->source_file = xstrdup (sals.sals[i].symtab->filename);
10887 
10888       if (b->line_number == 0)
10889 	b->line_number = sals.sals[i].line;
10890 
10891       if (sals_end.nelts)
10892 	{
10893 	  CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
10894 
10895 	  new_loc->length = end - sals.sals[0].pc + 1;
10896 	}
10897     }
10898 
10899   /* Update locations of permanent breakpoints.  */
10900   if (b->enable_state == bp_permanent)
10901     make_breakpoint_permanent (b);
10902 
10903   /* If possible, carry over 'disable' status from existing
10904      breakpoints.  */
10905   {
10906     struct bp_location *e = existing_locations;
10907     /* If there are multiple breakpoints with the same function name,
10908        e.g. for inline functions, comparing function names won't work.
10909        Instead compare pc addresses; this is just a heuristic as things
10910        may have moved, but in practice it gives the correct answer
10911        often enough until a better solution is found.  */
10912     int have_ambiguous_names = ambiguous_names_p (b->loc);
10913 
10914     for (; e; e = e->next)
10915       {
10916 	if (!e->enabled && e->function_name)
10917 	  {
10918 	    struct bp_location *l = b->loc;
10919 	    if (have_ambiguous_names)
10920 	      {
10921 		for (; l; l = l->next)
10922 		  if (breakpoint_locations_match (e, l))
10923 		    {
10924 		      l->enabled = 0;
10925 		      break;
10926 		    }
10927 	      }
10928 	    else
10929 	      {
10930 		for (; l; l = l->next)
10931 		  if (l->function_name
10932 		      && strcmp (e->function_name, l->function_name) == 0)
10933 		    {
10934 		      l->enabled = 0;
10935 		      break;
10936 		    }
10937 	      }
10938 	  }
10939       }
10940   }
10941 
10942   update_global_location_list (1);
10943 }
10944 
10945 /* Find the SaL locations corresponding to the given ADDR_STRING.
10946    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
10947 
10948 static struct symtabs_and_lines
10949 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
10950 {
10951   char *s;
10952   int marker_spec;
10953   struct symtabs_and_lines sals = {0};
10954   struct gdb_exception e;
10955 
10956   s = addr_string;
10957   marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10958 
10959   TRY_CATCH (e, RETURN_MASK_ERROR)
10960     {
10961       if (marker_spec)
10962 	{
10963 	  sals = decode_static_tracepoint_spec (&s);
10964 	  if (sals.nelts > b->static_trace_marker_id_idx)
10965 	    {
10966 	      sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10967 	      sals.nelts = 1;
10968 	    }
10969 	  else
10970 	    error (_("marker %s not found"), b->static_trace_marker_id);
10971 	}
10972       else
10973 	sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
10974     }
10975   if (e.reason < 0)
10976     {
10977       int not_found_and_ok = 0;
10978       /* For pending breakpoints, it's expected that parsing will
10979 	 fail until the right shared library is loaded.  User has
10980 	 already told to create pending breakpoints and don't need
10981 	 extra messages.  If breakpoint is in bp_shlib_disabled
10982 	 state, then user already saw the message about that
10983 	 breakpoint being disabled, and don't want to see more
10984 	 errors.  */
10985       if (e.error == NOT_FOUND_ERROR
10986 	  && (b->condition_not_parsed
10987 	      || (b->loc && b->loc->shlib_disabled)
10988 	      || b->enable_state == bp_disabled))
10989 	not_found_and_ok = 1;
10990 
10991       if (!not_found_and_ok)
10992 	{
10993 	  /* We surely don't want to warn about the same breakpoint
10994 	     10 times.  One solution, implemented here, is disable
10995 	     the breakpoint on error.  Another solution would be to
10996 	     have separate 'warning emitted' flag.  Since this
10997 	     happens only when a binary has changed, I don't know
10998 	     which approach is better.  */
10999 	  b->enable_state = bp_disabled;
11000 	  throw_exception (e);
11001 	}
11002     }
11003 
11004   if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
11005     {
11006       gdb_assert (sals.nelts == 1);
11007 
11008       resolve_sal_pc (&sals.sals[0]);
11009       if (b->condition_not_parsed && s && s[0])
11010 	{
11011 	  char *cond_string = 0;
11012 	  int thread = -1;
11013 	  int task = 0;
11014 
11015 	  find_condition_and_thread (s, sals.sals[0].pc,
11016 				     &cond_string, &thread, &task);
11017 	  if (cond_string)
11018 	    b->cond_string = cond_string;
11019 	  b->thread = thread;
11020 	  b->task = task;
11021 	  b->condition_not_parsed = 0;
11022 	}
11023 
11024       if (b->type == bp_static_tracepoint && !marker_spec)
11025 	sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
11026 
11027       *found = 1;
11028     }
11029   else
11030     *found = 0;
11031 
11032   return sals;
11033 }
11034 
11035 /* Reevaluate a hardware or software breakpoint and recreate its locations.
11036    This is necessary after symbols are read (e.g., an executable or DSO
11037    was loaded, or the inferior just started).  */
11038 
11039 static void
11040 re_set_breakpoint (struct breakpoint *b)
11041 {
11042   int found;
11043   struct symtabs_and_lines sals, sals_end;
11044   struct symtabs_and_lines expanded = {0};
11045   struct symtabs_and_lines expanded_end = {0};
11046   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11047 
11048   input_radix = b->input_radix;
11049   save_current_space_and_thread ();
11050   switch_to_program_space_and_thread (b->pspace);
11051   set_language (b->language);
11052 
11053   sals = addr_string_to_sals (b, b->addr_string, &found);
11054   if (found)
11055     {
11056       make_cleanup (xfree, sals.sals);
11057       expanded = expand_line_sal_maybe (sals.sals[0]);
11058     }
11059 
11060   if (b->addr_string_range_end)
11061     {
11062       sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11063       if (found)
11064 	{
11065 	  make_cleanup (xfree, sals_end.sals);
11066 	  expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11067 	}
11068     }
11069 
11070   update_breakpoint_locations (b, expanded, expanded_end);
11071   do_cleanups (cleanups);
11072 }
11073 
11074 /* Reset a breakpoint given it's struct breakpoint * BINT.
11075    The value we return ends up being the return value from catch_errors.
11076    Unused in this case.  */
11077 
11078 static int
11079 breakpoint_re_set_one (void *bint)
11080 {
11081   /* Get past catch_errs.  */
11082   struct breakpoint *b = (struct breakpoint *) bint;
11083 
11084   switch (b->type)
11085     {
11086     case bp_none:
11087       warning (_("attempted to reset apparently deleted breakpoint #%d?"),
11088 	       b->number);
11089       return 0;
11090     case bp_breakpoint:
11091     case bp_hardware_breakpoint:
11092     case bp_tracepoint:
11093     case bp_fast_tracepoint:
11094     case bp_static_tracepoint:
11095     case bp_gnu_ifunc_resolver:
11096       /* Do not attempt to re-set breakpoints disabled during startup.  */
11097       if (b->enable_state == bp_startup_disabled)
11098 	return 0;
11099 
11100       if (b->addr_string == NULL)
11101 	{
11102 	  /* Anything without a string can't be re-set.  */
11103 	  delete_breakpoint (b);
11104 	  return 0;
11105 	}
11106 
11107       re_set_breakpoint (b);
11108       break;
11109 
11110     case bp_watchpoint:
11111     case bp_hardware_watchpoint:
11112     case bp_read_watchpoint:
11113     case bp_access_watchpoint:
11114       /* Watchpoint can be either on expression using entirely global
11115 	 variables, or it can be on local variables.
11116 
11117 	 Watchpoints of the first kind are never auto-deleted, and
11118 	 even persist across program restarts. Since they can use
11119 	 variables from shared libraries, we need to reparse
11120 	 expression as libraries are loaded and unloaded.
11121 
11122 	 Watchpoints on local variables can also change meaning as
11123 	 result of solib event.  For example, if a watchpoint uses
11124 	 both a local and a global variables in expression, it's a
11125 	 local watchpoint, but unloading of a shared library will make
11126 	 the expression invalid.  This is not a very common use case,
11127 	 but we still re-evaluate expression, to avoid surprises to
11128 	 the user.
11129 
11130 	 Note that for local watchpoints, we re-evaluate it only if
11131 	 watchpoints frame id is still valid.  If it's not, it means
11132 	 the watchpoint is out of scope and will be deleted soon.  In
11133 	 fact, I'm not sure we'll ever be called in this case.
11134 
11135 	 If a local watchpoint's frame id is still valid, then
11136 	 b->exp_valid_block is likewise valid, and we can safely use it.
11137 
11138 	 Don't do anything about disabled watchpoints, since they will
11139 	 be reevaluated again when enabled.  */
11140       update_watchpoint (b, 1 /* reparse */);
11141       break;
11142       /* We needn't really do anything to reset these, since the mask
11143          that requests them is unaffected by e.g., new libraries being
11144          loaded.  */
11145     case bp_catchpoint:
11146       break;
11147 
11148     default:
11149       printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
11150       /* fall through */
11151       /* Delete overlay event and longjmp master breakpoints; they will be
11152 	 reset later by breakpoint_re_set.  */
11153     case bp_overlay_event:
11154     case bp_longjmp_master:
11155     case bp_std_terminate_master:
11156     case bp_exception_master:
11157       delete_breakpoint (b);
11158       break;
11159 
11160       /* This breakpoint is special, it's set up when the inferior
11161          starts and we really don't want to touch it.  */
11162     case bp_shlib_event:
11163 
11164       /* Like bp_shlib_event, this breakpoint type is special.
11165 	 Once it is set up, we do not want to touch it.  */
11166     case bp_thread_event:
11167 
11168       /* Keep temporary breakpoints, which can be encountered when we
11169          step over a dlopen call and SOLIB_ADD is resetting the
11170          breakpoints.  Otherwise these should have been blown away via
11171          the cleanup chain or by breakpoint_init_inferior when we
11172          rerun the executable.  */
11173     case bp_until:
11174     case bp_finish:
11175     case bp_watchpoint_scope:
11176     case bp_call_dummy:
11177     case bp_std_terminate:
11178     case bp_step_resume:
11179     case bp_longjmp:
11180     case bp_longjmp_resume:
11181     case bp_exception:
11182     case bp_exception_resume:
11183     case bp_jit_event:
11184     case bp_gnu_ifunc_resolver_return:
11185       break;
11186     }
11187 
11188   return 0;
11189 }
11190 
11191 /* Re-set all breakpoints after symbols have been re-loaded.  */
11192 void
11193 breakpoint_re_set (void)
11194 {
11195   struct breakpoint *b, *b_tmp;
11196   enum language save_language;
11197   int save_input_radix;
11198   struct cleanup *old_chain;
11199 
11200   save_language = current_language->la_language;
11201   save_input_radix = input_radix;
11202   old_chain = save_current_program_space ();
11203 
11204   ALL_BREAKPOINTS_SAFE (b, b_tmp)
11205   {
11206     /* Format possible error msg.  */
11207     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
11208 				b->number);
11209     struct cleanup *cleanups = make_cleanup (xfree, message);
11210     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
11211     do_cleanups (cleanups);
11212   }
11213   set_language (save_language);
11214   input_radix = save_input_radix;
11215 
11216   jit_breakpoint_re_set ();
11217 
11218   do_cleanups (old_chain);
11219 
11220   create_overlay_event_breakpoint ();
11221   create_longjmp_master_breakpoint ();
11222   create_std_terminate_master_breakpoint ();
11223   create_exception_master_breakpoint ();
11224 }
11225 
11226 /* Reset the thread number of this breakpoint:
11227 
11228    - If the breakpoint is for all threads, leave it as-is.
11229    - Else, reset it to the current thread for inferior_ptid.  */
11230 void
11231 breakpoint_re_set_thread (struct breakpoint *b)
11232 {
11233   if (b->thread != -1)
11234     {
11235       if (in_thread_list (inferior_ptid))
11236 	b->thread = pid_to_thread_id (inferior_ptid);
11237 
11238       /* We're being called after following a fork.  The new fork is
11239 	 selected as current, and unless this was a vfork will have a
11240 	 different program space from the original thread.  Reset that
11241 	 as well.  */
11242       b->loc->pspace = current_program_space;
11243     }
11244 }
11245 
11246 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
11247    If from_tty is nonzero, it prints a message to that effect,
11248    which ends with a period (no newline).  */
11249 
11250 void
11251 set_ignore_count (int bptnum, int count, int from_tty)
11252 {
11253   struct breakpoint *b;
11254 
11255   if (count < 0)
11256     count = 0;
11257 
11258   ALL_BREAKPOINTS (b)
11259     if (b->number == bptnum)
11260     {
11261       if (is_tracepoint (b))
11262 	{
11263 	  if (from_tty && count != 0)
11264 	    printf_filtered (_("Ignore count ignored for tracepoint %d."),
11265 			     bptnum);
11266 	  return;
11267 	}
11268 
11269       b->ignore_count = count;
11270       if (from_tty)
11271 	{
11272 	  if (count == 0)
11273 	    printf_filtered (_("Will stop next time "
11274 			       "breakpoint %d is reached."),
11275 			     bptnum);
11276 	  else if (count == 1)
11277 	    printf_filtered (_("Will ignore next crossing of breakpoint %d."),
11278 			     bptnum);
11279 	  else
11280 	    printf_filtered (_("Will ignore next %d "
11281 			       "crossings of breakpoint %d."),
11282 			     count, bptnum);
11283 	}
11284       breakpoints_changed ();
11285       observer_notify_breakpoint_modified (b->number);
11286       return;
11287     }
11288 
11289   error (_("No breakpoint number %d."), bptnum);
11290 }
11291 
11292 /* Command to set ignore-count of breakpoint N to COUNT.  */
11293 
11294 static void
11295 ignore_command (char *args, int from_tty)
11296 {
11297   char *p = args;
11298   int num;
11299 
11300   if (p == 0)
11301     error_no_arg (_("a breakpoint number"));
11302 
11303   num = get_number (&p);
11304   if (num == 0)
11305     error (_("bad breakpoint number: '%s'"), args);
11306   if (*p == 0)
11307     error (_("Second argument (specified ignore-count) is missing."));
11308 
11309   set_ignore_count (num,
11310 		    longest_to_int (value_as_long (parse_and_eval (p))),
11311 		    from_tty);
11312   if (from_tty)
11313     printf_filtered ("\n");
11314 }
11315 
11316 /* Call FUNCTION on each of the breakpoints
11317    whose numbers are given in ARGS.  */
11318 
11319 static void
11320 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
11321 						      void *),
11322 			void *data)
11323 {
11324   int num;
11325   struct breakpoint *b, *tmp;
11326   int match;
11327   struct get_number_or_range_state state;
11328 
11329   if (args == 0)
11330     error_no_arg (_("one or more breakpoint numbers"));
11331 
11332   init_number_or_range (&state, args);
11333 
11334   while (!state.finished)
11335     {
11336       char *p = state.string;
11337 
11338       match = 0;
11339 
11340       num = get_number_or_range (&state);
11341       if (num == 0)
11342 	{
11343 	  warning (_("bad breakpoint number at or near '%s'"), p);
11344 	}
11345       else
11346 	{
11347 	  ALL_BREAKPOINTS_SAFE (b, tmp)
11348 	    if (b->number == num)
11349 	      {
11350 		struct breakpoint *related_breakpoint;
11351 
11352 		match = 1;
11353 		related_breakpoint = b;
11354 		do
11355 		  {
11356 		    struct breakpoint *next_related_b;
11357 
11358 		    /* FUNCTION can be also delete_breakpoint.  */
11359 		    next_related_b = related_breakpoint->related_breakpoint;
11360 		    function (related_breakpoint, data);
11361 
11362 		    /* For delete_breakpoint of the last entry of the ring we
11363 		       were traversing we would never get back to B.  */
11364 		    if (next_related_b == related_breakpoint)
11365 		      break;
11366 		    related_breakpoint = next_related_b;
11367 		  }
11368 		while (related_breakpoint != b);
11369 		break;
11370 	      }
11371 	  if (match == 0)
11372 	    printf_unfiltered (_("No breakpoint number %d.\n"), num);
11373 	}
11374     }
11375 }
11376 
11377 static struct bp_location *
11378 find_location_by_number (char *number)
11379 {
11380   char *dot = strchr (number, '.');
11381   char *p1;
11382   int bp_num;
11383   int loc_num;
11384   struct breakpoint *b;
11385   struct bp_location *loc;
11386 
11387   *dot = '\0';
11388 
11389   p1 = number;
11390   bp_num = get_number (&p1);
11391   if (bp_num == 0)
11392     error (_("Bad breakpoint number '%s'"), number);
11393 
11394   ALL_BREAKPOINTS (b)
11395     if (b->number == bp_num)
11396       {
11397 	break;
11398       }
11399 
11400   if (!b || b->number != bp_num)
11401     error (_("Bad breakpoint number '%s'"), number);
11402 
11403   p1 = dot+1;
11404   loc_num = get_number (&p1);
11405   if (loc_num == 0)
11406     error (_("Bad breakpoint location number '%s'"), number);
11407 
11408   --loc_num;
11409   loc = b->loc;
11410   for (;loc_num && loc; --loc_num, loc = loc->next)
11411     ;
11412   if (!loc)
11413     error (_("Bad breakpoint location number '%s'"), dot+1);
11414 
11415   return loc;
11416 }
11417 
11418 
11419 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
11420    If from_tty is nonzero, it prints a message to that effect,
11421    which ends with a period (no newline).  */
11422 
11423 void
11424 disable_breakpoint (struct breakpoint *bpt)
11425 {
11426   /* Never disable a watchpoint scope breakpoint; we want to
11427      hit them when we leave scope so we can delete both the
11428      watchpoint and its scope breakpoint at that time.  */
11429   if (bpt->type == bp_watchpoint_scope)
11430     return;
11431 
11432   /* You can't disable permanent breakpoints.  */
11433   if (bpt->enable_state == bp_permanent)
11434     return;
11435 
11436   bpt->enable_state = bp_disabled;
11437 
11438   update_global_location_list (0);
11439 
11440   observer_notify_breakpoint_modified (bpt->number);
11441 }
11442 
11443 /* A callback for map_breakpoint_numbers that calls
11444    disable_breakpoint.  */
11445 
11446 static void
11447 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
11448 {
11449   disable_breakpoint (b);
11450 }
11451 
11452 static void
11453 disable_command (char *args, int from_tty)
11454 {
11455   struct breakpoint *bpt;
11456 
11457   if (args == 0)
11458     ALL_BREAKPOINTS (bpt)
11459       switch (bpt->type)
11460       {
11461       case bp_none:
11462 	warning (_("attempted to disable apparently deleted breakpoint #%d?"),
11463 		 bpt->number);
11464 	break;
11465       case bp_breakpoint:
11466       case bp_tracepoint:
11467       case bp_fast_tracepoint:
11468       case bp_static_tracepoint:
11469       case bp_catchpoint:
11470       case bp_hardware_breakpoint:
11471       case bp_watchpoint:
11472       case bp_hardware_watchpoint:
11473       case bp_read_watchpoint:
11474       case bp_access_watchpoint:
11475 	disable_breakpoint (bpt);
11476 	break;
11477       default:
11478 	break;
11479       }
11480   else if (strchr (args, '.'))
11481     {
11482       struct bp_location *loc = find_location_by_number (args);
11483       if (loc)
11484 	loc->enabled = 0;
11485       update_global_location_list (0);
11486     }
11487   else
11488     map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
11489 }
11490 
11491 static void
11492 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
11493 {
11494   int target_resources_ok;
11495 
11496   if (bpt->type == bp_hardware_breakpoint)
11497     {
11498       int i;
11499       i = hw_breakpoint_used_count ();
11500       target_resources_ok =
11501 	target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
11502 					    i + 1, 0);
11503       if (target_resources_ok == 0)
11504 	error (_("No hardware breakpoint support in the target."));
11505       else if (target_resources_ok < 0)
11506 	error (_("Hardware breakpoints used exceeds limit."));
11507     }
11508 
11509   if (is_watchpoint (bpt))
11510     {
11511       struct gdb_exception e;
11512 
11513       TRY_CATCH (e, RETURN_MASK_ALL)
11514 	{
11515 	  update_watchpoint (bpt, 1 /* reparse */);
11516 	}
11517       if (e.reason < 0)
11518 	{
11519 	  exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
11520 			     bpt->number);
11521 	  return;
11522 	}
11523     }
11524 
11525   if (bpt->enable_state != bp_permanent)
11526     bpt->enable_state = bp_enabled;
11527   bpt->disposition = disposition;
11528   update_global_location_list (1);
11529   breakpoints_changed ();
11530 
11531   observer_notify_breakpoint_modified (bpt->number);
11532 }
11533 
11534 
11535 void
11536 enable_breakpoint (struct breakpoint *bpt)
11537 {
11538   do_enable_breakpoint (bpt, bpt->disposition);
11539 }
11540 
11541 /* A callback for map_breakpoint_numbers that calls
11542    enable_breakpoint.  */
11543 
11544 static void
11545 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11546 {
11547   enable_breakpoint (b);
11548 }
11549 
11550 /* The enable command enables the specified breakpoints (or all defined
11551    breakpoints) so they once again become (or continue to be) effective
11552    in stopping the inferior.  */
11553 
11554 static void
11555 enable_command (char *args, int from_tty)
11556 {
11557   struct breakpoint *bpt;
11558 
11559   if (args == 0)
11560     ALL_BREAKPOINTS (bpt)
11561       switch (bpt->type)
11562       {
11563       case bp_none:
11564 	warning (_("attempted to enable apparently deleted breakpoint #%d?"),
11565 		 bpt->number);
11566 	break;
11567       case bp_breakpoint:
11568       case bp_tracepoint:
11569       case bp_fast_tracepoint:
11570       case bp_static_tracepoint:
11571       case bp_catchpoint:
11572       case bp_hardware_breakpoint:
11573       case bp_watchpoint:
11574       case bp_hardware_watchpoint:
11575       case bp_read_watchpoint:
11576       case bp_access_watchpoint:
11577 	enable_breakpoint (bpt);
11578 	break;
11579       default:
11580 	break;
11581       }
11582   else if (strchr (args, '.'))
11583     {
11584       struct bp_location *loc = find_location_by_number (args);
11585       if (loc)
11586 	loc->enabled = 1;
11587       update_global_location_list (1);
11588     }
11589   else
11590     map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
11591 }
11592 
11593 static void
11594 enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
11595 {
11596   do_enable_breakpoint (bpt, disp_disable);
11597 }
11598 
11599 static void
11600 enable_once_command (char *args, int from_tty)
11601 {
11602   map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
11603 }
11604 
11605 static void
11606 enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
11607 {
11608   do_enable_breakpoint (bpt, disp_del);
11609 }
11610 
11611 static void
11612 enable_delete_command (char *args, int from_tty)
11613 {
11614   map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
11615 }
11616 
11617 static void
11618 set_breakpoint_cmd (char *args, int from_tty)
11619 {
11620 }
11621 
11622 static void
11623 show_breakpoint_cmd (char *args, int from_tty)
11624 {
11625 }
11626 
11627 /* Invalidate last known value of any hardware watchpoint if
11628    the memory which that value represents has been written to by
11629    GDB itself.  */
11630 
11631 static void
11632 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11633 				      const bfd_byte *data)
11634 {
11635   struct breakpoint *bp;
11636 
11637   ALL_BREAKPOINTS (bp)
11638     if (bp->enable_state == bp_enabled
11639 	&& bp->type == bp_hardware_watchpoint
11640 	&& bp->val_valid && bp->val)
11641       {
11642 	struct bp_location *loc;
11643 
11644 	for (loc = bp->loc; loc != NULL; loc = loc->next)
11645 	  if (loc->loc_type == bp_loc_hardware_watchpoint
11646 	      && loc->address + loc->length > addr
11647 	      && addr + len > loc->address)
11648 	    {
11649 	      value_free (bp->val);
11650 	      bp->val = NULL;
11651 	      bp->val_valid = 0;
11652 	    }
11653       }
11654 }
11655 
11656 /* Use default_breakpoint_'s, or nothing if they aren't valid.  */
11657 
11658 struct symtabs_and_lines
11659 decode_line_spec_1 (char *string, int funfirstline)
11660 {
11661   struct symtabs_and_lines sals;
11662 
11663   if (string == 0)
11664     error (_("Empty line specification."));
11665   if (default_breakpoint_valid)
11666     sals = decode_line_1 (&string, funfirstline,
11667 			  default_breakpoint_symtab,
11668 			  default_breakpoint_line,
11669 			  NULL);
11670   else
11671     sals = decode_line_1 (&string, funfirstline,
11672 			  (struct symtab *) NULL, 0, NULL);
11673   if (*string)
11674     error (_("Junk at end of line specification: %s"), string);
11675   return sals;
11676 }
11677 
11678 /* Create and insert a raw software breakpoint at PC.  Return an
11679    identifier, which should be used to remove the breakpoint later.
11680    In general, places which call this should be using something on the
11681    breakpoint chain instead; this function should be eliminated
11682    someday.  */
11683 
11684 void *
11685 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11686 				  struct address_space *aspace, CORE_ADDR pc)
11687 {
11688   struct bp_target_info *bp_tgt;
11689 
11690   bp_tgt = XZALLOC (struct bp_target_info);
11691 
11692   bp_tgt->placed_address_space = aspace;
11693   bp_tgt->placed_address = pc;
11694 
11695   if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
11696     {
11697       /* Could not insert the breakpoint.  */
11698       xfree (bp_tgt);
11699       return NULL;
11700     }
11701 
11702   return bp_tgt;
11703 }
11704 
11705 /* Remove a breakpoint BP inserted by
11706    deprecated_insert_raw_breakpoint.  */
11707 
11708 int
11709 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
11710 {
11711   struct bp_target_info *bp_tgt = bp;
11712   int ret;
11713 
11714   ret = target_remove_breakpoint (gdbarch, bp_tgt);
11715   xfree (bp_tgt);
11716 
11717   return ret;
11718 }
11719 
11720 /* One (or perhaps two) breakpoints used for software single
11721    stepping.  */
11722 
11723 static void *single_step_breakpoints[2];
11724 static struct gdbarch *single_step_gdbarch[2];
11725 
11726 /* Create and insert a breakpoint for software single step.  */
11727 
11728 void
11729 insert_single_step_breakpoint (struct gdbarch *gdbarch,
11730 			       struct address_space *aspace,
11731 			       CORE_ADDR next_pc)
11732 {
11733   void **bpt_p;
11734 
11735   if (single_step_breakpoints[0] == NULL)
11736     {
11737       bpt_p = &single_step_breakpoints[0];
11738       single_step_gdbarch[0] = gdbarch;
11739     }
11740   else
11741     {
11742       gdb_assert (single_step_breakpoints[1] == NULL);
11743       bpt_p = &single_step_breakpoints[1];
11744       single_step_gdbarch[1] = gdbarch;
11745     }
11746 
11747   /* NOTE drow/2006-04-11: A future improvement to this function would
11748      be to only create the breakpoints once, and actually put them on
11749      the breakpoint chain.  That would let us use set_raw_breakpoint.
11750      We could adjust the addresses each time they were needed.  Doing
11751      this requires corresponding changes elsewhere where single step
11752      breakpoints are handled, however.  So, for now, we use this.  */
11753 
11754   *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
11755   if (*bpt_p == NULL)
11756     error (_("Could not insert single-step breakpoint at %s"),
11757 	     paddress (gdbarch, next_pc));
11758 }
11759 
11760 /* Check if the breakpoints used for software single stepping
11761    were inserted or not.  */
11762 
11763 int
11764 single_step_breakpoints_inserted (void)
11765 {
11766   return (single_step_breakpoints[0] != NULL
11767           || single_step_breakpoints[1] != NULL);
11768 }
11769 
11770 /* Remove and delete any breakpoints used for software single step.  */
11771 
11772 void
11773 remove_single_step_breakpoints (void)
11774 {
11775   gdb_assert (single_step_breakpoints[0] != NULL);
11776 
11777   /* See insert_single_step_breakpoint for more about this deprecated
11778      call.  */
11779   deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11780 				    single_step_breakpoints[0]);
11781   single_step_gdbarch[0] = NULL;
11782   single_step_breakpoints[0] = NULL;
11783 
11784   if (single_step_breakpoints[1] != NULL)
11785     {
11786       deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11787 					single_step_breakpoints[1]);
11788       single_step_gdbarch[1] = NULL;
11789       single_step_breakpoints[1] = NULL;
11790     }
11791 }
11792 
11793 /* Delete software single step breakpoints without removing them from
11794    the inferior.  This is intended to be used if the inferior's address
11795    space where they were inserted is already gone, e.g. after exit or
11796    exec.  */
11797 
11798 void
11799 cancel_single_step_breakpoints (void)
11800 {
11801   int i;
11802 
11803   for (i = 0; i < 2; i++)
11804     if (single_step_breakpoints[i])
11805       {
11806 	xfree (single_step_breakpoints[i]);
11807 	single_step_breakpoints[i] = NULL;
11808 	single_step_gdbarch[i] = NULL;
11809       }
11810 }
11811 
11812 /* Detach software single-step breakpoints from INFERIOR_PTID without
11813    removing them.  */
11814 
11815 static void
11816 detach_single_step_breakpoints (void)
11817 {
11818   int i;
11819 
11820   for (i = 0; i < 2; i++)
11821     if (single_step_breakpoints[i])
11822       target_remove_breakpoint (single_step_gdbarch[i],
11823 				single_step_breakpoints[i]);
11824 }
11825 
11826 /* Check whether a software single-step breakpoint is inserted at
11827    PC.  */
11828 
11829 static int
11830 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11831 					CORE_ADDR pc)
11832 {
11833   int i;
11834 
11835   for (i = 0; i < 2; i++)
11836     {
11837       struct bp_target_info *bp_tgt = single_step_breakpoints[i];
11838       if (bp_tgt
11839 	  && breakpoint_address_match (bp_tgt->placed_address_space,
11840 				       bp_tgt->placed_address,
11841 				       aspace, pc))
11842 	return 1;
11843     }
11844 
11845   return 0;
11846 }
11847 
11848 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
11849    non-zero otherwise.  */
11850 static int
11851 is_syscall_catchpoint_enabled (struct breakpoint *bp)
11852 {
11853   if (syscall_catchpoint_p (bp)
11854       && bp->enable_state != bp_disabled
11855       && bp->enable_state != bp_call_disabled)
11856     return 1;
11857   else
11858     return 0;
11859 }
11860 
11861 int
11862 catch_syscall_enabled (void)
11863 {
11864   struct inferior *inf = current_inferior ();
11865 
11866   return inf->total_syscalls_count != 0;
11867 }
11868 
11869 int
11870 catching_syscall_number (int syscall_number)
11871 {
11872   struct breakpoint *bp;
11873 
11874   ALL_BREAKPOINTS (bp)
11875     if (is_syscall_catchpoint_enabled (bp))
11876       {
11877 	if (bp->syscalls_to_be_caught)
11878 	  {
11879             int i, iter;
11880             for (i = 0;
11881                  VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11882                  i++)
11883 	      if (syscall_number == iter)
11884 		return 1;
11885 	  }
11886 	else
11887 	  return 1;
11888       }
11889 
11890   return 0;
11891 }
11892 
11893 /* Complete syscall names.  Used by "catch syscall".  */
11894 static char **
11895 catch_syscall_completer (struct cmd_list_element *cmd,
11896                          char *text, char *word)
11897 {
11898   const char **list = get_syscall_names ();
11899   char **retlist
11900     = (list == NULL) ? NULL : complete_on_enum (list, text, word);
11901 
11902   xfree (list);
11903   return retlist;
11904 }
11905 
11906 /* Tracepoint-specific operations.  */
11907 
11908 /* Set tracepoint count to NUM.  */
11909 static void
11910 set_tracepoint_count (int num)
11911 {
11912   tracepoint_count = num;
11913   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
11914 }
11915 
11916 void
11917 trace_command (char *arg, int from_tty)
11918 {
11919   if (create_breakpoint (get_current_arch (),
11920 			 arg,
11921 			 NULL, 0, 1 /* parse arg */,
11922 			 0 /* tempflag */,
11923 			 bp_tracepoint /* type_wanted */,
11924 			 0 /* Ignore count */,
11925 			 pending_break_support,
11926 			 NULL,
11927 			 from_tty,
11928 			 1 /* enabled */,
11929 			 0 /* internal */))
11930     set_tracepoint_count (breakpoint_count);
11931 }
11932 
11933 void
11934 ftrace_command (char *arg, int from_tty)
11935 {
11936   if (create_breakpoint (get_current_arch (),
11937 			 arg,
11938 			 NULL, 0, 1 /* parse arg */,
11939 			 0 /* tempflag */,
11940 			 bp_fast_tracepoint /* type_wanted */,
11941 			 0 /* Ignore count */,
11942 			 pending_break_support,
11943 			 NULL,
11944 			 from_tty,
11945 			 1 /* enabled */,
11946 			 0 /* internal */))
11947     set_tracepoint_count (breakpoint_count);
11948 }
11949 
11950 /* strace command implementation.  Creates a static tracepoint.  */
11951 
11952 void
11953 strace_command (char *arg, int from_tty)
11954 {
11955   if (create_breakpoint (get_current_arch (),
11956 			 arg,
11957 			 NULL, 0, 1 /* parse arg */,
11958 			 0 /* tempflag */,
11959 			 bp_static_tracepoint /* type_wanted */,
11960 			 0 /* Ignore count */,
11961 			 pending_break_support,
11962 			 NULL,
11963 			 from_tty,
11964 			 1 /* enabled */,
11965 			 0 /* internal */))
11966     set_tracepoint_count (breakpoint_count);
11967 }
11968 
11969 /* Set up a fake reader function that gets command lines from a linked
11970    list that was acquired during tracepoint uploading.  */
11971 
11972 static struct uploaded_tp *this_utp;
11973 static int next_cmd;
11974 
11975 static char *
11976 read_uploaded_action (void)
11977 {
11978   char *rslt;
11979 
11980   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
11981 
11982   next_cmd++;
11983 
11984   return rslt;
11985 }
11986 
11987 /* Given information about a tracepoint as recorded on a target (which
11988    can be either a live system or a trace file), attempt to create an
11989    equivalent GDB tracepoint.  This is not a reliable process, since
11990    the target does not necessarily have all the information used when
11991    the tracepoint was originally defined.  */
11992 
11993 struct breakpoint *
11994 create_tracepoint_from_upload (struct uploaded_tp *utp)
11995 {
11996   char *addr_str, small_buf[100];
11997   struct breakpoint *tp;
11998 
11999   if (utp->at_string)
12000     addr_str = utp->at_string;
12001   else
12002     {
12003       /* In the absence of a source location, fall back to raw
12004 	 address.  Since there is no way to confirm that the address
12005 	 means the same thing as when the trace was started, warn the
12006 	 user.  */
12007       warning (_("Uploaded tracepoint %d has no "
12008 		 "source location, using raw address"),
12009 	       utp->number);
12010       sprintf (small_buf, "*%s", hex_string (utp->addr));
12011       addr_str = small_buf;
12012     }
12013 
12014   /* There's not much we can do with a sequence of bytecodes.  */
12015   if (utp->cond && !utp->cond_string)
12016     warning (_("Uploaded tracepoint %d condition "
12017 	       "has no source form, ignoring it"),
12018 	     utp->number);
12019 
12020   if (!create_breakpoint (get_current_arch (),
12021 			  addr_str,
12022 			  utp->cond_string, -1, 0 /* parse cond/thread */,
12023 			  0 /* tempflag */,
12024 			  utp->type /* type_wanted */,
12025 			  0 /* Ignore count */,
12026 			  pending_break_support,
12027 			  NULL,
12028 			  0 /* from_tty */,
12029 			  utp->enabled /* enabled */,
12030 			  0 /* internal */))
12031     return NULL;
12032 
12033   set_tracepoint_count (breakpoint_count);
12034 
12035   /* Get the tracepoint we just created.  */
12036   tp = get_tracepoint (tracepoint_count);
12037   gdb_assert (tp != NULL);
12038 
12039   if (utp->pass > 0)
12040     {
12041       sprintf (small_buf, "%d %d", utp->pass, tp->number);
12042 
12043       trace_pass_command (small_buf, 0);
12044     }
12045 
12046   /* If we have uploaded versions of the original commands, set up a
12047      special-purpose "reader" function and call the usual command line
12048      reader, then pass the result to the breakpoint command-setting
12049      function.  */
12050   if (!VEC_empty (char_ptr, utp->cmd_strings))
12051     {
12052       struct command_line *cmd_list;
12053 
12054       this_utp = utp;
12055       next_cmd = 0;
12056 
12057       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12058 
12059       breakpoint_set_commands (tp, cmd_list);
12060     }
12061   else if (!VEC_empty (char_ptr, utp->actions)
12062 	   || !VEC_empty (char_ptr, utp->step_actions))
12063     warning (_("Uploaded tracepoint %d actions "
12064 	       "have no source form, ignoring them"),
12065 	     utp->number);
12066 
12067   return tp;
12068   }
12069 
12070 /* Print information on tracepoint number TPNUM_EXP, or all if
12071    omitted.  */
12072 
12073 static void
12074 tracepoints_info (char *args, int from_tty)
12075 {
12076   int num_printed;
12077 
12078   num_printed = breakpoint_1 (args, 0, is_tracepoint);
12079 
12080   if (num_printed == 0)
12081     {
12082       if (args == NULL || *args == '\0')
12083 	ui_out_message (uiout, 0, "No tracepoints.\n");
12084       else
12085 	ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
12086     }
12087 
12088   default_collect_info ();
12089 }
12090 
12091 /* The 'enable trace' command enables tracepoints.
12092    Not supported by all targets.  */
12093 static void
12094 enable_trace_command (char *args, int from_tty)
12095 {
12096   enable_command (args, from_tty);
12097 }
12098 
12099 /* The 'disable trace' command disables tracepoints.
12100    Not supported by all targets.  */
12101 static void
12102 disable_trace_command (char *args, int from_tty)
12103 {
12104   disable_command (args, from_tty);
12105 }
12106 
12107 /* Remove a tracepoint (or all if no argument).  */
12108 static void
12109 delete_trace_command (char *arg, int from_tty)
12110 {
12111   struct breakpoint *b, *b_tmp;
12112 
12113   dont_repeat ();
12114 
12115   if (arg == 0)
12116     {
12117       int breaks_to_delete = 0;
12118 
12119       /* Delete all breakpoints if no argument.
12120          Do not delete internal or call-dummy breakpoints, these
12121          have to be deleted with an explicit breakpoint number
12122 	 argument.  */
12123       ALL_TRACEPOINTS (b)
12124       {
12125 	if (b->number >= 0)
12126 	  {
12127 	    breaks_to_delete = 1;
12128 	    break;
12129 	  }
12130       }
12131 
12132       /* Ask user only if there are some breakpoints to delete.  */
12133       if (!from_tty
12134 	  || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12135 	{
12136 	  ALL_BREAKPOINTS_SAFE (b, b_tmp)
12137 	  {
12138 	    if (is_tracepoint (b)
12139 		&& b->number >= 0)
12140 	      delete_breakpoint (b);
12141 	  }
12142 	}
12143     }
12144   else
12145     map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
12146 }
12147 
12148 /* Helper function for trace_pass_command.  */
12149 
12150 static void
12151 trace_pass_set_count (struct breakpoint *bp, int count, int from_tty)
12152 {
12153   bp->pass_count = count;
12154   observer_notify_tracepoint_modified (bp->number);
12155   if (from_tty)
12156     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
12157 		     bp->number, count);
12158 }
12159 
12160 /* Set passcount for tracepoint.
12161 
12162    First command argument is passcount, second is tracepoint number.
12163    If tracepoint number omitted, apply to most recently defined.
12164    Also accepts special argument "all".  */
12165 
12166 static void
12167 trace_pass_command (char *args, int from_tty)
12168 {
12169   struct breakpoint *t1;
12170   unsigned int count;
12171 
12172   if (args == 0 || *args == 0)
12173     error (_("passcount command requires an "
12174 	     "argument (count + optional TP num)"));
12175 
12176   count = strtoul (args, &args, 10);	/* Count comes first, then TP num.  */
12177 
12178   while (*args && isspace ((int) *args))
12179     args++;
12180 
12181   if (*args && strncasecmp (args, "all", 3) == 0)
12182     {
12183       args += 3;			/* Skip special argument "all".  */
12184       if (*args)
12185 	error (_("Junk at end of arguments."));
12186 
12187       ALL_TRACEPOINTS (t1)
12188       {
12189 	trace_pass_set_count (t1, count, from_tty);
12190       }
12191     }
12192   else if (*args == '\0')
12193     {
12194       t1 = get_tracepoint_by_number (&args, NULL, 1);
12195       if (t1)
12196 	trace_pass_set_count (t1, count, from_tty);
12197     }
12198   else
12199     {
12200       struct get_number_or_range_state state;
12201 
12202       init_number_or_range (&state, args);
12203       while (!state.finished)
12204 	{
12205 	  t1 = get_tracepoint_by_number (&args, &state, 1);
12206 	  if (t1)
12207 	    trace_pass_set_count (t1, count, from_tty);
12208 	}
12209     }
12210 }
12211 
12212 struct breakpoint *
12213 get_tracepoint (int num)
12214 {
12215   struct breakpoint *t;
12216 
12217   ALL_TRACEPOINTS (t)
12218     if (t->number == num)
12219       return t;
12220 
12221   return NULL;
12222 }
12223 
12224 /* Find the tracepoint with the given target-side number (which may be
12225    different from the tracepoint number after disconnecting and
12226    reconnecting).  */
12227 
12228 struct breakpoint *
12229 get_tracepoint_by_number_on_target (int num)
12230 {
12231   struct breakpoint *t;
12232 
12233   ALL_TRACEPOINTS (t)
12234     if (t->number_on_target == num)
12235       return t;
12236 
12237   return NULL;
12238 }
12239 
12240 /* Utility: parse a tracepoint number and look it up in the list.
12241    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12242    If OPTIONAL_P is true, then if the argument is missing, the most
12243    recent tracepoint (tracepoint_count) is returned.  */
12244 struct breakpoint *
12245 get_tracepoint_by_number (char **arg,
12246 			  struct get_number_or_range_state *state,
12247 			  int optional_p)
12248 {
12249   extern int tracepoint_count;
12250   struct breakpoint *t;
12251   int tpnum;
12252   char *instring = arg == NULL ? NULL : *arg;
12253 
12254   if (state)
12255     {
12256       gdb_assert (!state->finished);
12257       tpnum = get_number_or_range (state);
12258     }
12259   else if (arg == NULL || *arg == NULL || ! **arg)
12260     {
12261       if (optional_p)
12262 	tpnum = tracepoint_count;
12263       else
12264 	error_no_arg (_("tracepoint number"));
12265     }
12266   else
12267     tpnum = get_number (arg);
12268 
12269   if (tpnum <= 0)
12270     {
12271       if (instring && *instring)
12272 	printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12273 			 instring);
12274       else
12275 	printf_filtered (_("Tracepoint argument missing "
12276 			   "and no previous tracepoint\n"));
12277       return NULL;
12278     }
12279 
12280   ALL_TRACEPOINTS (t)
12281     if (t->number == tpnum)
12282     {
12283       return t;
12284     }
12285 
12286   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
12287   return NULL;
12288 }
12289 
12290 /* Save information on user settable breakpoints (watchpoints, etc) to
12291    a new script file named FILENAME.  If FILTER is non-NULL, call it
12292    on each breakpoint and only include the ones for which it returns
12293    non-zero.  */
12294 
12295 static void
12296 save_breakpoints (char *filename, int from_tty,
12297 		  int (*filter) (const struct breakpoint *))
12298 {
12299   struct breakpoint *tp;
12300   int any = 0;
12301   char *pathname;
12302   struct cleanup *cleanup;
12303   struct ui_file *fp;
12304   int extra_trace_bits = 0;
12305 
12306   if (filename == 0 || *filename == 0)
12307     error (_("Argument required (file name in which to save)"));
12308 
12309   /* See if we have anything to save.  */
12310   ALL_BREAKPOINTS (tp)
12311   {
12312     /* Skip internal and momentary breakpoints.  */
12313     if (!user_breakpoint_p (tp))
12314       continue;
12315 
12316     /* If we have a filter, only save the breakpoints it accepts.  */
12317     if (filter && !filter (tp))
12318       continue;
12319 
12320     any = 1;
12321 
12322     if (is_tracepoint (tp))
12323       {
12324 	extra_trace_bits = 1;
12325 
12326 	/* We can stop searching.  */
12327 	break;
12328       }
12329   }
12330 
12331   if (!any)
12332     {
12333       warning (_("Nothing to save."));
12334       return;
12335     }
12336 
12337   pathname = tilde_expand (filename);
12338   cleanup = make_cleanup (xfree, pathname);
12339   fp = gdb_fopen (pathname, "w");
12340   if (!fp)
12341     error (_("Unable to open file '%s' for saving (%s)"),
12342 	   filename, safe_strerror (errno));
12343   make_cleanup_ui_file_delete (fp);
12344 
12345   if (extra_trace_bits)
12346     save_trace_state_variables (fp);
12347 
12348   ALL_BREAKPOINTS (tp)
12349   {
12350     /* Skip internal and momentary breakpoints.  */
12351     if (!user_breakpoint_p (tp))
12352       continue;
12353 
12354     /* If we have a filter, only save the breakpoints it accepts.  */
12355     if (filter && !filter (tp))
12356       continue;
12357 
12358     if (tp->ops != NULL && tp->ops->print_recreate != NULL)
12359       (tp->ops->print_recreate) (tp, fp);
12360     else
12361       {
12362 	if (tp->type == bp_fast_tracepoint)
12363 	  fprintf_unfiltered (fp, "ftrace");
12364 	if (tp->type == bp_static_tracepoint)
12365 	  fprintf_unfiltered (fp, "strace");
12366 	else if (tp->type == bp_tracepoint)
12367 	  fprintf_unfiltered (fp, "trace");
12368 	else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12369 	  fprintf_unfiltered (fp, "tbreak");
12370 	else if (tp->type == bp_breakpoint)
12371 	  fprintf_unfiltered (fp, "break");
12372 	else if (tp->type == bp_hardware_breakpoint
12373 		 && tp->disposition == disp_del)
12374 	  fprintf_unfiltered (fp, "thbreak");
12375 	else if (tp->type == bp_hardware_breakpoint)
12376 	  fprintf_unfiltered (fp, "hbreak");
12377 	else if (tp->type == bp_watchpoint)
12378 	  fprintf_unfiltered (fp, "watch");
12379 	else if (tp->type == bp_hardware_watchpoint)
12380 	  fprintf_unfiltered (fp, "watch");
12381 	else if (tp->type == bp_read_watchpoint)
12382 	  fprintf_unfiltered (fp, "rwatch");
12383 	else if (tp->type == bp_access_watchpoint)
12384 	  fprintf_unfiltered (fp, "awatch");
12385 	else
12386 	  internal_error (__FILE__, __LINE__,
12387 			  _("unhandled breakpoint type %d"), (int) tp->type);
12388 
12389 	if (tp->exp_string)
12390 	  fprintf_unfiltered (fp, " %s", tp->exp_string);
12391 	else if (tp->addr_string)
12392 	  fprintf_unfiltered (fp, " %s", tp->addr_string);
12393 	else
12394 	  {
12395 	    char tmp[40];
12396 
12397 	    sprintf_vma (tmp, tp->loc->address);
12398 	    fprintf_unfiltered (fp, " *0x%s", tmp);
12399 	  }
12400       }
12401 
12402     if (tp->thread != -1)
12403       fprintf_unfiltered (fp, " thread %d", tp->thread);
12404 
12405     if (tp->task != 0)
12406       fprintf_unfiltered (fp, " task %d", tp->task);
12407 
12408     fprintf_unfiltered (fp, "\n");
12409 
12410     /* Note, we can't rely on tp->number for anything, as we can't
12411        assume the recreated breakpoint numbers will match.  Use $bpnum
12412        instead.  */
12413 
12414     if (tp->cond_string)
12415       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
12416 
12417     if (tp->ignore_count)
12418       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
12419 
12420     if (tp->pass_count)
12421       fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
12422 
12423     if (tp->commands)
12424       {
12425 	volatile struct gdb_exception ex;
12426 
12427 	fprintf_unfiltered (fp, "  commands\n");
12428 
12429 	ui_out_redirect (uiout, fp);
12430 	TRY_CATCH (ex, RETURN_MASK_ALL)
12431 	  {
12432 	    print_command_lines (uiout, tp->commands->commands, 2);
12433 	  }
12434 	ui_out_redirect (uiout, NULL);
12435 
12436 	if (ex.reason < 0)
12437 	  throw_exception (ex);
12438 
12439 	fprintf_unfiltered (fp, "  end\n");
12440       }
12441 
12442     if (tp->enable_state == bp_disabled)
12443       fprintf_unfiltered (fp, "disable\n");
12444 
12445     /* If this is a multi-location breakpoint, check if the locations
12446        should be individually disabled.  Watchpoint locations are
12447        special, and not user visible.  */
12448     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
12449       {
12450 	struct bp_location *loc;
12451 	int n = 1;
12452 
12453 	for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
12454 	  if (!loc->enabled)
12455 	    fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
12456       }
12457   }
12458 
12459   if (extra_trace_bits && *default_collect)
12460     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
12461 
12462   do_cleanups (cleanup);
12463   if (from_tty)
12464     printf_filtered (_("Saved to file '%s'.\n"), filename);
12465 }
12466 
12467 /* The `save breakpoints' command.  */
12468 
12469 static void
12470 save_breakpoints_command (char *args, int from_tty)
12471 {
12472   save_breakpoints (args, from_tty, NULL);
12473 }
12474 
12475 /* The `save tracepoints' command.  */
12476 
12477 static void
12478 save_tracepoints_command (char *args, int from_tty)
12479 {
12480   save_breakpoints (args, from_tty, is_tracepoint);
12481 }
12482 
12483 /* Create a vector of all tracepoints.  */
12484 
12485 VEC(breakpoint_p) *
12486 all_tracepoints (void)
12487 {
12488   VEC(breakpoint_p) *tp_vec = 0;
12489   struct breakpoint *tp;
12490 
12491   ALL_TRACEPOINTS (tp)
12492   {
12493     VEC_safe_push (breakpoint_p, tp_vec, tp);
12494   }
12495 
12496   return tp_vec;
12497 }
12498 
12499 
12500 /* This help string is used for the break, hbreak, tbreak and thbreak
12501    commands.  It is defined as a macro to prevent duplication.
12502    COMMAND should be a string constant containing the name of the
12503    command.  */
12504 #define BREAK_ARGS_HELP(command) \
12505 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
12506 LOCATION may be a line number, function name, or \"*\" and an address.\n\
12507 If a line number is specified, break at start of code for that line.\n\
12508 If a function is specified, break at start of code for that function.\n\
12509 If an address is specified, break at that exact address.\n\
12510 With no LOCATION, uses current execution address of the selected\n\
12511 stack frame.  This is useful for breaking on return to a stack frame.\n\
12512 \n\
12513 THREADNUM is the number from \"info threads\".\n\
12514 CONDITION is a boolean expression.\n\
12515 \n\
12516 Multiple breakpoints at one place are permitted, and useful if their\n\
12517 conditions are different.\n\
12518 \n\
12519 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
12520 
12521 /* List of subcommands for "catch".  */
12522 static struct cmd_list_element *catch_cmdlist;
12523 
12524 /* List of subcommands for "tcatch".  */
12525 static struct cmd_list_element *tcatch_cmdlist;
12526 
12527 /* Like add_cmd, but add the command to both the "catch" and "tcatch"
12528    lists, and pass some additional user data to the command function.  */
12529 static void
12530 add_catch_command (char *name, char *docstring,
12531 		   void (*sfunc) (char *args, int from_tty,
12532 				  struct cmd_list_element *command),
12533                    char **(*completer) (struct cmd_list_element *cmd,
12534                                          char *text, char *word),
12535 		   void *user_data_catch,
12536 		   void *user_data_tcatch)
12537 {
12538   struct cmd_list_element *command;
12539 
12540   command = add_cmd (name, class_breakpoint, NULL, docstring,
12541 		     &catch_cmdlist);
12542   set_cmd_sfunc (command, sfunc);
12543   set_cmd_context (command, user_data_catch);
12544   set_cmd_completer (command, completer);
12545 
12546   command = add_cmd (name, class_breakpoint, NULL, docstring,
12547 		     &tcatch_cmdlist);
12548   set_cmd_sfunc (command, sfunc);
12549   set_cmd_context (command, user_data_tcatch);
12550   set_cmd_completer (command, completer);
12551 }
12552 
12553 static void
12554 clear_syscall_counts (struct inferior *inf)
12555 {
12556   inf->total_syscalls_count = 0;
12557   inf->any_syscall_count = 0;
12558   VEC_free (int, inf->syscalls_counts);
12559 }
12560 
12561 static void
12562 save_command (char *arg, int from_tty)
12563 {
12564   printf_unfiltered (_("\"save\" must be followed by "
12565 		       "the name of a save subcommand.\n"));
12566   help_list (save_cmdlist, "save ", -1, gdb_stdout);
12567 }
12568 
12569 struct breakpoint *
12570 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12571 			  void *data)
12572 {
12573   struct breakpoint *b, *b_tmp;
12574 
12575   ALL_BREAKPOINTS_SAFE (b, b_tmp)
12576     {
12577       if ((*callback) (b, data))
12578 	return b;
12579     }
12580 
12581   return NULL;
12582 }
12583 
12584 void
12585 _initialize_breakpoint (void)
12586 {
12587   struct cmd_list_element *c;
12588 
12589   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
12590   observer_attach_inferior_exit (clear_syscall_counts);
12591   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
12592 
12593   breakpoint_objfile_key = register_objfile_data ();
12594 
12595   breakpoint_chain = 0;
12596   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
12597      before a breakpoint is set.  */
12598   breakpoint_count = 0;
12599 
12600   tracepoint_count = 0;
12601 
12602   add_com ("ignore", class_breakpoint, ignore_command, _("\
12603 Set ignore-count of breakpoint number N to COUNT.\n\
12604 Usage is `ignore N COUNT'."));
12605   if (xdb_commands)
12606     add_com_alias ("bc", "ignore", class_breakpoint, 1);
12607 
12608   add_com ("commands", class_breakpoint, commands_command, _("\
12609 Set commands to be executed when a breakpoint is hit.\n\
12610 Give breakpoint number as argument after \"commands\".\n\
12611 With no argument, the targeted breakpoint is the last one set.\n\
12612 The commands themselves follow starting on the next line.\n\
12613 Type a line containing \"end\" to indicate the end of them.\n\
12614 Give \"silent\" as the first line to make the breakpoint silent;\n\
12615 then no output is printed when it is hit, except what the commands print."));
12616 
12617   add_com ("condition", class_breakpoint, condition_command, _("\
12618 Specify breakpoint number N to break only if COND is true.\n\
12619 Usage is `condition N COND', where N is an integer and COND is an\n\
12620 expression to be evaluated whenever breakpoint N is reached."));
12621 
12622   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
12623 Set a temporary breakpoint.\n\
12624 Like \"break\" except the breakpoint is only temporary,\n\
12625 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
12626 by using \"enable delete\" on the breakpoint number.\n\
12627 \n"
12628 BREAK_ARGS_HELP ("tbreak")));
12629   set_cmd_completer (c, location_completer);
12630 
12631   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
12632 Set a hardware assisted breakpoint.\n\
12633 Like \"break\" except the breakpoint requires hardware support,\n\
12634 some target hardware may not have this support.\n\
12635 \n"
12636 BREAK_ARGS_HELP ("hbreak")));
12637   set_cmd_completer (c, location_completer);
12638 
12639   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
12640 Set a temporary hardware assisted breakpoint.\n\
12641 Like \"hbreak\" except the breakpoint is only temporary,\n\
12642 so it will be deleted when hit.\n\
12643 \n"
12644 BREAK_ARGS_HELP ("thbreak")));
12645   set_cmd_completer (c, location_completer);
12646 
12647   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12648 Enable some breakpoints.\n\
12649 Give breakpoint numbers (separated by spaces) as arguments.\n\
12650 With no subcommand, breakpoints are enabled until you command otherwise.\n\
12651 This is used to cancel the effect of the \"disable\" command.\n\
12652 With a subcommand you can enable temporarily."),
12653 		  &enablelist, "enable ", 1, &cmdlist);
12654   if (xdb_commands)
12655     add_com ("ab", class_breakpoint, enable_command, _("\
12656 Enable some breakpoints.\n\
12657 Give breakpoint numbers (separated by spaces) as arguments.\n\
12658 With no subcommand, breakpoints are enabled until you command otherwise.\n\
12659 This is used to cancel the effect of the \"disable\" command.\n\
12660 With a subcommand you can enable temporarily."));
12661 
12662   add_com_alias ("en", "enable", class_breakpoint, 1);
12663 
12664   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
12665 Enable some breakpoints.\n\
12666 Give breakpoint numbers (separated by spaces) as arguments.\n\
12667 This is used to cancel the effect of the \"disable\" command.\n\
12668 May be abbreviated to simply \"enable\".\n"),
12669 		   &enablebreaklist, "enable breakpoints ", 1, &enablelist);
12670 
12671   add_cmd ("once", no_class, enable_once_command, _("\
12672 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
12673 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
12674 	   &enablebreaklist);
12675 
12676   add_cmd ("delete", no_class, enable_delete_command, _("\
12677 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
12678 If a breakpoint is hit while enabled in this fashion, it is deleted."),
12679 	   &enablebreaklist);
12680 
12681   add_cmd ("delete", no_class, enable_delete_command, _("\
12682 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
12683 If a breakpoint is hit while enabled in this fashion, it is deleted."),
12684 	   &enablelist);
12685 
12686   add_cmd ("once", no_class, enable_once_command, _("\
12687 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
12688 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
12689 	   &enablelist);
12690 
12691   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12692 Disable some breakpoints.\n\
12693 Arguments are breakpoint numbers with spaces in between.\n\
12694 To disable all breakpoints, give no argument.\n\
12695 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
12696 		  &disablelist, "disable ", 1, &cmdlist);
12697   add_com_alias ("dis", "disable", class_breakpoint, 1);
12698   add_com_alias ("disa", "disable", class_breakpoint, 1);
12699   if (xdb_commands)
12700     add_com ("sb", class_breakpoint, disable_command, _("\
12701 Disable some breakpoints.\n\
12702 Arguments are breakpoint numbers with spaces in between.\n\
12703 To disable all breakpoints, give no argument.\n\
12704 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
12705 
12706   add_cmd ("breakpoints", class_alias, disable_command, _("\
12707 Disable some breakpoints.\n\
12708 Arguments are breakpoint numbers with spaces in between.\n\
12709 To disable all breakpoints, give no argument.\n\
12710 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
12711 This command may be abbreviated \"disable\"."),
12712 	   &disablelist);
12713 
12714   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12715 Delete some breakpoints or auto-display expressions.\n\
12716 Arguments are breakpoint numbers with spaces in between.\n\
12717 To delete all breakpoints, give no argument.\n\
12718 \n\
12719 Also a prefix command for deletion of other GDB objects.\n\
12720 The \"unset\" command is also an alias for \"delete\"."),
12721 		  &deletelist, "delete ", 1, &cmdlist);
12722   add_com_alias ("d", "delete", class_breakpoint, 1);
12723   add_com_alias ("del", "delete", class_breakpoint, 1);
12724   if (xdb_commands)
12725     add_com ("db", class_breakpoint, delete_command, _("\
12726 Delete some breakpoints.\n\
12727 Arguments are breakpoint numbers with spaces in between.\n\
12728 To delete all breakpoints, give no argument.\n"));
12729 
12730   add_cmd ("breakpoints", class_alias, delete_command, _("\
12731 Delete some breakpoints or auto-display expressions.\n\
12732 Arguments are breakpoint numbers with spaces in between.\n\
12733 To delete all breakpoints, give no argument.\n\
12734 This command may be abbreviated \"delete\"."),
12735 	   &deletelist);
12736 
12737   add_com ("clear", class_breakpoint, clear_command, _("\
12738 Clear breakpoint at specified line or function.\n\
12739 Argument may be line number, function name, or \"*\" and an address.\n\
12740 If line number is specified, all breakpoints in that line are cleared.\n\
12741 If function is specified, breakpoints at beginning of function are cleared.\n\
12742 If an address is specified, breakpoints at that address are cleared.\n\
12743 \n\
12744 With no argument, clears all breakpoints in the line that the selected frame\n\
12745 is executing in.\n\
12746 \n\
12747 See also the \"delete\" command which clears breakpoints by number."));
12748   add_com_alias ("cl", "clear", class_breakpoint, 1);
12749 
12750   c = add_com ("break", class_breakpoint, break_command, _("\
12751 Set breakpoint at specified line or function.\n"
12752 BREAK_ARGS_HELP ("break")));
12753   set_cmd_completer (c, location_completer);
12754 
12755   add_com_alias ("b", "break", class_run, 1);
12756   add_com_alias ("br", "break", class_run, 1);
12757   add_com_alias ("bre", "break", class_run, 1);
12758   add_com_alias ("brea", "break", class_run, 1);
12759 
12760   if (xdb_commands)
12761    add_com_alias ("ba", "break", class_breakpoint, 1);
12762 
12763   if (dbx_commands)
12764     {
12765       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12766 Break in function/address or break at a line in the current file."),
12767 			     &stoplist, "stop ", 1, &cmdlist);
12768       add_cmd ("in", class_breakpoint, stopin_command,
12769 	       _("Break in function or address."), &stoplist);
12770       add_cmd ("at", class_breakpoint, stopat_command,
12771 	       _("Break at a line in the current file."), &stoplist);
12772       add_com ("status", class_info, breakpoints_info, _("\
12773 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
12774 The \"Type\" column indicates one of:\n\
12775 \tbreakpoint     - normal breakpoint\n\
12776 \twatchpoint     - watchpoint\n\
12777 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12778 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
12779 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
12780 address and file/line number respectively.\n\
12781 \n\
12782 Convenience variable \"$_\" and default examine address for \"x\"\n\
12783 are set to the address of the last breakpoint listed unless the command\n\
12784 is prefixed with \"server \".\n\n\
12785 Convenience variable \"$bpnum\" contains the number of the last\n\
12786 breakpoint set."));
12787     }
12788 
12789   add_info ("breakpoints", breakpoints_info, _("\
12790 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
12791 The \"Type\" column indicates one of:\n\
12792 \tbreakpoint     - normal breakpoint\n\
12793 \twatchpoint     - watchpoint\n\
12794 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12795 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
12796 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
12797 address and file/line number respectively.\n\
12798 \n\
12799 Convenience variable \"$_\" and default examine address for \"x\"\n\
12800 are set to the address of the last breakpoint listed unless the command\n\
12801 is prefixed with \"server \".\n\n\
12802 Convenience variable \"$bpnum\" contains the number of the last\n\
12803 breakpoint set."));
12804 
12805   add_info_alias ("b", "breakpoints", 1);
12806 
12807   if (xdb_commands)
12808     add_com ("lb", class_breakpoint, breakpoints_info, _("\
12809 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
12810 The \"Type\" column indicates one of:\n\
12811 \tbreakpoint     - normal breakpoint\n\
12812 \twatchpoint     - watchpoint\n\
12813 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12814 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
12815 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
12816 address and file/line number respectively.\n\
12817 \n\
12818 Convenience variable \"$_\" and default examine address for \"x\"\n\
12819 are set to the address of the last breakpoint listed unless the command\n\
12820 is prefixed with \"server \".\n\n\
12821 Convenience variable \"$bpnum\" contains the number of the last\n\
12822 breakpoint set."));
12823 
12824   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12825 Status of all breakpoints, or breakpoint number NUMBER.\n\
12826 The \"Type\" column indicates one of:\n\
12827 \tbreakpoint     - normal breakpoint\n\
12828 \twatchpoint     - watchpoint\n\
12829 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
12830 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12831 \tuntil          - internal breakpoint used by the \"until\" command\n\
12832 \tfinish         - internal breakpoint used by the \"finish\" command\n\
12833 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12834 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
12835 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
12836 address and file/line number respectively.\n\
12837 \n\
12838 Convenience variable \"$_\" and default examine address for \"x\"\n\
12839 are set to the address of the last breakpoint listed unless the command\n\
12840 is prefixed with \"server \".\n\n\
12841 Convenience variable \"$bpnum\" contains the number of the last\n\
12842 breakpoint set."),
12843 	   &maintenanceinfolist);
12844 
12845   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12846 Set catchpoints to catch events."),
12847 		  &catch_cmdlist, "catch ",
12848 		  0/*allow-unknown*/, &cmdlist);
12849 
12850   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12851 Set temporary catchpoints to catch events."),
12852 		  &tcatch_cmdlist, "tcatch ",
12853 		  0/*allow-unknown*/, &cmdlist);
12854 
12855   /* Add catch and tcatch sub-commands.  */
12856   add_catch_command ("catch", _("\
12857 Catch an exception, when caught.\n\
12858 With an argument, catch only exceptions with the given name."),
12859 		     catch_catch_command,
12860                      NULL,
12861 		     CATCH_PERMANENT,
12862 		     CATCH_TEMPORARY);
12863   add_catch_command ("throw", _("\
12864 Catch an exception, when thrown.\n\
12865 With an argument, catch only exceptions with the given name."),
12866 		     catch_throw_command,
12867                      NULL,
12868 		     CATCH_PERMANENT,
12869 		     CATCH_TEMPORARY);
12870   add_catch_command ("fork", _("Catch calls to fork."),
12871 		     catch_fork_command_1,
12872                      NULL,
12873 		     (void *) (uintptr_t) catch_fork_permanent,
12874 		     (void *) (uintptr_t) catch_fork_temporary);
12875   add_catch_command ("vfork", _("Catch calls to vfork."),
12876 		     catch_fork_command_1,
12877                      NULL,
12878 		     (void *) (uintptr_t) catch_vfork_permanent,
12879 		     (void *) (uintptr_t) catch_vfork_temporary);
12880   add_catch_command ("exec", _("Catch calls to exec."),
12881 		     catch_exec_command_1,
12882                      NULL,
12883 		     CATCH_PERMANENT,
12884 		     CATCH_TEMPORARY);
12885   add_catch_command ("syscall", _("\
12886 Catch system calls by their names and/or numbers.\n\
12887 Arguments say which system calls to catch.  If no arguments\n\
12888 are given, every system call will be caught.\n\
12889 Arguments, if given, should be one or more system call names\n\
12890 (if your system supports that), or system call numbers."),
12891 		     catch_syscall_command_1,
12892 		     catch_syscall_completer,
12893 		     CATCH_PERMANENT,
12894 		     CATCH_TEMPORARY);
12895   add_catch_command ("exception", _("\
12896 Catch Ada exceptions, when raised.\n\
12897 With an argument, catch only exceptions with the given name."),
12898 		     catch_ada_exception_command,
12899                      NULL,
12900 		     CATCH_PERMANENT,
12901 		     CATCH_TEMPORARY);
12902   add_catch_command ("assert", _("\
12903 Catch failed Ada assertions, when raised.\n\
12904 With an argument, catch only exceptions with the given name."),
12905 		     catch_assert_command,
12906                      NULL,
12907 		     CATCH_PERMANENT,
12908 		     CATCH_TEMPORARY);
12909 
12910   c = add_com ("watch", class_breakpoint, watch_command, _("\
12911 Set a watchpoint for an expression.\n\
12912 Usage: watch [-l|-location] EXPRESSION\n\
12913 A watchpoint stops execution of your program whenever the value of\n\
12914 an expression changes.\n\
12915 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12916 the memory to which it refers."));
12917   set_cmd_completer (c, expression_completer);
12918 
12919   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12920 Set a read watchpoint for an expression.\n\
12921 Usage: rwatch [-l|-location] EXPRESSION\n\
12922 A watchpoint stops execution of your program whenever the value of\n\
12923 an expression is read.\n\
12924 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12925 the memory to which it refers."));
12926   set_cmd_completer (c, expression_completer);
12927 
12928   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12929 Set a watchpoint for an expression.\n\
12930 Usage: awatch [-l|-location] EXPRESSION\n\
12931 A watchpoint stops execution of your program whenever the value of\n\
12932 an expression is either read or written.\n\
12933 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12934 the memory to which it refers."));
12935   set_cmd_completer (c, expression_completer);
12936 
12937   add_info ("watchpoints", watchpoints_info, _("\
12938 Status of specified watchpoints (all watchpoints if no argument)."));
12939 
12940   /* XXX: cagney/2005-02-23: This should be a boolean, and should
12941      respond to changes - contrary to the description.  */
12942   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12943 			    &can_use_hw_watchpoints, _("\
12944 Set debugger's willingness to use watchpoint hardware."), _("\
12945 Show debugger's willingness to use watchpoint hardware."), _("\
12946 If zero, gdb will not use hardware for new watchpoints, even if\n\
12947 such is available.  (However, any hardware watchpoints that were\n\
12948 created before setting this to nonzero, will continue to use watchpoint\n\
12949 hardware.)"),
12950 			    NULL,
12951 			    show_can_use_hw_watchpoints,
12952 			    &setlist, &showlist);
12953 
12954   can_use_hw_watchpoints = 1;
12955 
12956   /* Tracepoint manipulation commands.  */
12957 
12958   c = add_com ("trace", class_breakpoint, trace_command, _("\
12959 Set a tracepoint at specified line or function.\n\
12960 \n"
12961 BREAK_ARGS_HELP ("trace") "\n\
12962 Do \"help tracepoints\" for info on other tracepoint commands."));
12963   set_cmd_completer (c, location_completer);
12964 
12965   add_com_alias ("tp", "trace", class_alias, 0);
12966   add_com_alias ("tr", "trace", class_alias, 1);
12967   add_com_alias ("tra", "trace", class_alias, 1);
12968   add_com_alias ("trac", "trace", class_alias, 1);
12969 
12970   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12971 Set a fast tracepoint at specified line or function.\n\
12972 \n"
12973 BREAK_ARGS_HELP ("ftrace") "\n\
12974 Do \"help tracepoints\" for info on other tracepoint commands."));
12975   set_cmd_completer (c, location_completer);
12976 
12977   c = add_com ("strace", class_breakpoint, strace_command, _("\
12978 Set a static tracepoint at specified line, function or marker.\n\
12979 \n\
12980 strace [LOCATION] [if CONDITION]\n\
12981 LOCATION may be a line number, function name, \"*\" and an address,\n\
12982 or -m MARKER_ID.\n\
12983 If a line number is specified, probe the marker at start of code\n\
12984 for that line.  If a function is specified, probe the marker at start\n\
12985 of code for that function.  If an address is specified, probe the marker\n\
12986 at that exact address.  If a marker id is specified, probe the marker\n\
12987 with that name.  With no LOCATION, uses current execution address of\n\
12988 the selected stack frame.\n\
12989 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12990 This collects arbitrary user data passed in the probe point call to the\n\
12991 tracing library.  You can inspect it when analyzing the trace buffer,\n\
12992 by printing the $_sdata variable like any other convenience variable.\n\
12993 \n\
12994 CONDITION is a boolean expression.\n\
12995 \n\
12996 Multiple tracepoints at one place are permitted, and useful if their\n\
12997 conditions are different.\n\
12998 \n\
12999 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13000 Do \"help tracepoints\" for info on other tracepoint commands."));
13001   set_cmd_completer (c, location_completer);
13002 
13003   add_info ("tracepoints", tracepoints_info, _("\
13004 Status of specified tracepoints (all tracepoints if no argument).\n\
13005 Convenience variable \"$tpnum\" contains the number of the\n\
13006 last tracepoint set."));
13007 
13008   add_info_alias ("tp", "tracepoints", 1);
13009 
13010   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13011 Delete specified tracepoints.\n\
13012 Arguments are tracepoint numbers, separated by spaces.\n\
13013 No argument means delete all tracepoints."),
13014 	   &deletelist);
13015 
13016   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13017 Disable specified tracepoints.\n\
13018 Arguments are tracepoint numbers, separated by spaces.\n\
13019 No argument means disable all tracepoints."),
13020 	   &disablelist);
13021   deprecate_cmd (c, "disable");
13022 
13023   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13024 Enable specified tracepoints.\n\
13025 Arguments are tracepoint numbers, separated by spaces.\n\
13026 No argument means enable all tracepoints."),
13027 	   &enablelist);
13028   deprecate_cmd (c, "enable");
13029 
13030   add_com ("passcount", class_trace, trace_pass_command, _("\
13031 Set the passcount for a tracepoint.\n\
13032 The trace will end when the tracepoint has been passed 'count' times.\n\
13033 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13034 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13035 
13036   add_prefix_cmd ("save", class_breakpoint, save_command,
13037 		  _("Save breakpoint definitions as a script."),
13038 		  &save_cmdlist, "save ",
13039 		  0/*allow-unknown*/, &cmdlist);
13040 
13041   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13042 Save current breakpoint definitions as a script.\n\
13043 This includes all types of breakpoints (breakpoints, watchpoints,\n\
13044 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
13045 session to restore them."),
13046 	       &save_cmdlist);
13047   set_cmd_completer (c, filename_completer);
13048 
13049   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
13050 Save current tracepoint definitions as a script.\n\
13051 Use the 'source' command in another debug session to restore them."),
13052 	       &save_cmdlist);
13053   set_cmd_completer (c, filename_completer);
13054 
13055   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13056   deprecate_cmd (c, "save tracepoints");
13057 
13058   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
13059 Breakpoint specific settings\n\
13060 Configure various breakpoint-specific variables such as\n\
13061 pending breakpoint behavior"),
13062 		  &breakpoint_set_cmdlist, "set breakpoint ",
13063 		  0/*allow-unknown*/, &setlist);
13064   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
13065 Breakpoint specific settings\n\
13066 Configure various breakpoint-specific variables such as\n\
13067 pending breakpoint behavior"),
13068 		  &breakpoint_show_cmdlist, "show breakpoint ",
13069 		  0/*allow-unknown*/, &showlist);
13070 
13071   add_setshow_auto_boolean_cmd ("pending", no_class,
13072 				&pending_break_support, _("\
13073 Set debugger's behavior regarding pending breakpoints."), _("\
13074 Show debugger's behavior regarding pending breakpoints."), _("\
13075 If on, an unrecognized breakpoint location will cause gdb to create a\n\
13076 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
13077 an error.  If auto, an unrecognized breakpoint location results in a\n\
13078 user-query to see if a pending breakpoint should be created."),
13079 				NULL,
13080 				show_pending_break_support,
13081 				&breakpoint_set_cmdlist,
13082 				&breakpoint_show_cmdlist);
13083 
13084   pending_break_support = AUTO_BOOLEAN_AUTO;
13085 
13086   add_setshow_boolean_cmd ("auto-hw", no_class,
13087 			   &automatic_hardware_breakpoints, _("\
13088 Set automatic usage of hardware breakpoints."), _("\
13089 Show automatic usage of hardware breakpoints."), _("\
13090 If set, the debugger will automatically use hardware breakpoints for\n\
13091 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
13092 a warning will be emitted for such breakpoints."),
13093 			   NULL,
13094 			   show_automatic_hardware_breakpoints,
13095 			   &breakpoint_set_cmdlist,
13096 			   &breakpoint_show_cmdlist);
13097 
13098   add_setshow_enum_cmd ("always-inserted", class_support,
13099 			always_inserted_enums, &always_inserted_mode, _("\
13100 Set mode for inserting breakpoints."), _("\
13101 Show mode for inserting breakpoints."), _("\
13102 When this mode is off, breakpoints are inserted in inferior when it is\n\
13103 resumed, and removed when execution stops.  When this mode is on,\n\
13104 breakpoints are inserted immediately and removed only when the user\n\
13105 deletes the breakpoint.  When this mode is auto (which is the default),\n\
13106 the behaviour depends on the non-stop setting (see help set non-stop).\n\
13107 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13108 behaves as if always-inserted mode is on; if gdb is controlling the\n\
13109 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
13110 			   NULL,
13111 			   &show_always_inserted_mode,
13112 			   &breakpoint_set_cmdlist,
13113 			   &breakpoint_show_cmdlist);
13114 
13115   add_com ("break-range", class_breakpoint, break_range_command, _("\
13116 Set a breakpoint for an address range.\n\
13117 break-range START-LOCATION, END-LOCATION\n\
13118 where START-LOCATION and END-LOCATION can be one of the following:\n\
13119   LINENUM, for that line in the current file,\n\
13120   FILE:LINENUM, for that line in that file,\n\
13121   +OFFSET, for that number of lines after the current line\n\
13122            or the start of the range\n\
13123   FUNCTION, for the first line in that function,\n\
13124   FILE:FUNCTION, to distinguish among like-named static functions.\n\
13125   *ADDRESS, for the instruction at that address.\n\
13126 \n\
13127 The breakpoint will stop execution of the inferior whenever it executes\n\
13128 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13129 range (including START-LOCATION and END-LOCATION)."));
13130 
13131   automatic_hardware_breakpoints = 1;
13132 
13133   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
13134 }
13135