gdb/breakpoint.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Everything about breakpoints, for GDB.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include <ctype.h>
  17. #include "hashtab.h"
  18. #include "symtab.h"
  19. #include "frame.h"
  20. #include "breakpoint.h"
  21. #include "tracepoint.h"
  22. #include "gdbtypes.h"
  23. #include "expression.h"
  24. #include "gdbcore.h"
  25. #include "gdbcmd.h"
  26. #include "value.h"
  27. #include "command.h"
  28. #include "inferior.h"
  29. #include "infrun.h"
  30. #include "gdbthread.h"
  31. #include "target.h"
  32. #include "language.h"
  33. #include "gdb-demangle.h"
  34. #include "filenames.h"
  35. #include "annotate.h"
  36. #include "symfile.h"
  37. #include "objfiles.h"
  38. #include "source.h"
  39. #include "linespec.h"
  40. #include "completer.h"
  41. #include "gdb.h"
  42. #include "ui-out.h"
  43. #include "cli/cli-script.h"
  44. #include "block.h"
  45. #include "solib.h"
  46. #include "solist.h"
  47. #include "observer.h"
  48. #include "memattr.h"
  49. #include "ada-lang.h"
  50. #include "top.h"
  51. #include "valprint.h"
  52. #include "jit.h"
  53. #include "xml-syscall.h"
  54. #include "parser-defs.h"
  55. #include "gdb_regex.h"
  56. #include "probe.h"
  57. #include "cli/cli-utils.h"
  58. #include "continuations.h"
  59. #include "stack.h"
  60. #include "skip.h"
  61. #include "ax-gdb.h"
  62. #include "dummy-frame.h"

  63. #include "format.h"

  64. /* readline include files */
  65. #include "readline/readline.h"
  66. #include "readline/history.h"

  67. /* readline defines this.  */
  68. #undef savestring

  69. #include "mi/mi-common.h"
  70. #include "extension.h"

  71. /* Enums for exception-handling support.  */
  72. enum exception_event_kind
  73. {
  74.   EX_EVENT_THROW,
  75.   EX_EVENT_RETHROW,
  76.   EX_EVENT_CATCH
  77. };

  78. /* Prototypes for local functions.  */

  79. static void enable_delete_command (char *, int);

  80. static void enable_once_command (char *, int);

  81. static void enable_count_command (char *, int);

  82. static void disable_command (char *, int);

  83. static void enable_command (char *, int);

  84. static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
  85.                                                       void *),
  86.                                     void *);

  87. static void ignore_command (char *, int);

  88. static int breakpoint_re_set_one (void *);

  89. static void breakpoint_re_set_default (struct breakpoint *);

  90. static void create_sals_from_address_default (char **,
  91.                                               struct linespec_result *,
  92.                                               enum bptype, char *,
  93.                                               char **);

  94. static void create_breakpoints_sal_default (struct gdbarch *,
  95.                                             struct linespec_result *,
  96.                                             char *, char *, enum bptype,
  97.                                             enum bpdisp, int, int,
  98.                                             int,
  99.                                             const struct breakpoint_ops *,
  100.                                             int, int, int, unsigned);

  101. static void decode_linespec_default (struct breakpoint *, char **,
  102.                                      struct symtabs_and_lines *);

  103. static void clear_command (char *, int);

  104. static void catch_command (char *, int);

  105. static int can_use_hardware_watchpoint (struct value *);

  106. static void break_command_1 (char *, int, int);

  107. static void mention (struct breakpoint *);

  108. static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
  109.                                                                enum bptype,
  110.                                                                const struct breakpoint_ops *);
  111. static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
  112.                                                        const struct symtab_and_line *);

  113. /* This function is used in gdbtk sources and thus can not be made
  114.    static.  */
  115. struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
  116.                                        struct symtab_and_line,
  117.                                        enum bptype,
  118.                                        const struct breakpoint_ops *);

  119. static struct breakpoint *
  120.   momentary_breakpoint_from_master (struct breakpoint *orig,
  121.                                     enum bptype type,
  122.                                     const struct breakpoint_ops *ops,
  123.                                     int loc_enabled);

  124. static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);

  125. static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
  126.                                             CORE_ADDR bpaddr,
  127.                                             enum bptype bptype);

  128. static void describe_other_breakpoints (struct gdbarch *,
  129.                                         struct program_space *, CORE_ADDR,
  130.                                         struct obj_section *, int);

  131. static int watchpoint_locations_match (struct bp_location *loc1,
  132.                                        struct bp_location *loc2);

  133. static int breakpoint_location_address_match (struct bp_location *bl,
  134.                                               struct address_space *aspace,
  135.                                               CORE_ADDR addr);

  136. static void breakpoints_info (char *, int);

  137. static void watchpoints_info (char *, int);

  138. static int breakpoint_1 (char *, int,
  139.                          int (*) (const struct breakpoint *));

  140. static int breakpoint_cond_eval (void *);

  141. static void cleanup_executing_breakpoints (void *);

  142. static void commands_command (char *, int);

  143. static void condition_command (char *, int);

  144. typedef enum
  145.   {
  146.     mark_inserted,
  147.     mark_uninserted
  148.   }
  149. insertion_state_t;

  150. static int remove_breakpoint (struct bp_location *, insertion_state_t);
  151. static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);

  152. static enum print_stop_action print_bp_stop_message (bpstat bs);

  153. static int watchpoint_check (void *);

  154. static void maintenance_info_breakpoints (char *, int);

  155. static int hw_breakpoint_used_count (void);

  156. static int hw_watchpoint_use_count (struct breakpoint *);

  157. static int hw_watchpoint_used_count_others (struct breakpoint *except,
  158.                                             enum bptype type,
  159.                                             int *other_type_used);

  160. static void hbreak_command (char *, int);

  161. static void thbreak_command (char *, int);

  162. static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
  163.                                     int count);

  164. static void stop_command (char *arg, int from_tty);

  165. static void stopin_command (char *arg, int from_tty);

  166. static void stopat_command (char *arg, int from_tty);

  167. static void tcatch_command (char *arg, int from_tty);

  168. static void free_bp_location (struct bp_location *loc);
  169. static void incref_bp_location (struct bp_location *loc);
  170. static void decref_bp_location (struct bp_location **loc);

  171. static struct bp_location *allocate_bp_location (struct breakpoint *bpt);

  172. /* update_global_location_list's modes of operation wrt to whether to
  173.    insert locations now.  */
  174. enum ugll_insert_mode
  175. {
  176.   /* Don't insert any breakpoint locations into the inferior, only
  177.      remove already-inserted locations that no longer should be
  178.      inserted.  Functions that delete a breakpoint or breakpoints
  179.      should specify this mode, so that deleting a breakpoint doesn't
  180.      have the side effect of inserting the locations of other
  181.      breakpoints that are marked not-inserted, but should_be_inserted
  182.      returns true on them.

  183.      This behavior is useful is situations close to tear-down -- e.g.,
  184.      after an exec, while the target still has execution, but
  185.      breakpoint shadows of the previous executable image should *NOT*
  186.      be restored to the new image; or before detaching, where the
  187.      target still has execution and wants to delete breakpoints from
  188.      GDB's lists, and all breakpoints had already been removed from
  189.      the inferior.  */
  190.   UGLL_DONT_INSERT,

  191.   /* May insert breakpoints iff breakpoints_should_be_inserted_now
  192.      claims breakpoints should be inserted now.  */
  193.   UGLL_MAY_INSERT,

  194.   /* Insert locations now, irrespective of
  195.      breakpoints_should_be_inserted_nowE.g., say all threads are
  196.      stopped right now, and the user did "continue".  We need to
  197.      insert breakpoints _before_ resuming the target, but
  198.      UGLL_MAY_INSERT wouldn't insert them, because
  199.      breakpoints_should_be_inserted_now returns false at that point,
  200.      as no thread is running yet.  */
  201.   UGLL_INSERT
  202. };

  203. static void update_global_location_list (enum ugll_insert_mode);

  204. static void update_global_location_list_nothrow (enum ugll_insert_mode);

  205. static int is_hardware_watchpoint (const struct breakpoint *bpt);

  206. static void insert_breakpoint_locations (void);

  207. static int syscall_catchpoint_p (struct breakpoint *b);

  208. static void tracepoints_info (char *, int);

  209. static void delete_trace_command (char *, int);

  210. static void enable_trace_command (char *, int);

  211. static void disable_trace_command (char *, int);

  212. static void trace_pass_command (char *, int);

  213. static void set_tracepoint_count (int num);

  214. static int is_masked_watchpoint (const struct breakpoint *b);

  215. static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);

  216. /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
  217.    otherwise.  */

  218. static int strace_marker_p (struct breakpoint *b);

  219. /* The abstract base class all breakpoint_ops structures inherit
  220.    from.  */
  221. struct breakpoint_ops base_breakpoint_ops;

  222. /* The breakpoint_ops structure to be inherited by all breakpoint_ops
  223.    that are implemented on top of software or hardware breakpoints
  224.    (user breakpoints, internal and momentary breakpoints, etc.).  */
  225. static struct breakpoint_ops bkpt_base_breakpoint_ops;

  226. /* Internal breakpoints class type.  */
  227. static struct breakpoint_ops internal_breakpoint_ops;

  228. /* Momentary breakpoints class type.  */
  229. static struct breakpoint_ops momentary_breakpoint_ops;

  230. /* Momentary breakpoints for bp_longjmp and bp_exception class type.  */
  231. static struct breakpoint_ops longjmp_breakpoint_ops;

  232. /* The breakpoint_ops structure to be used in regular user created
  233.    breakpoints.  */
  234. struct breakpoint_ops bkpt_breakpoint_ops;

  235. /* Breakpoints set on probes.  */
  236. static struct breakpoint_ops bkpt_probe_breakpoint_ops;

  237. /* Dynamic printf class type.  */
  238. struct breakpoint_ops dprintf_breakpoint_ops;

  239. /* The style in which to perform a dynamic printf.  This is a user
  240.    option because different output options have different tradeoffs;
  241.    if GDB does the printing, there is better error handling if there
  242.    is a problem with any of the arguments, but using an inferior
  243.    function lets you have special-purpose printers and sending of
  244.    output to the same place as compiled-in print functions.  */

  245. static const char dprintf_style_gdb[] = "gdb";
  246. static const char dprintf_style_call[] = "call";
  247. static const char dprintf_style_agent[] = "agent";
  248. static const char *const dprintf_style_enums[] = {
  249.   dprintf_style_gdb,
  250.   dprintf_style_call,
  251.   dprintf_style_agent,
  252.   NULL
  253. };
  254. static const char *dprintf_style = dprintf_style_gdb;

  255. /* The function to use for dynamic printf if the preferred style is to
  256.    call into the inferior.  The value is simply a string that is
  257.    copied into the command, so it can be anything that GDB can
  258.    evaluate to a callable address, not necessarily a function name.  */

  259. static char *dprintf_function = "";

  260. /* The channel to use for dynamic printf if the preferred style is to
  261.    call into the inferior; if a nonempty string, it will be passed to
  262.    the call as the first argument, with the format string as the
  263.    second.  As with the dprintf function, this can be anything that
  264.    GDB knows how to evaluate, so in addition to common choices like
  265.    "stderr", this could be an app-specific expression like
  266.    "mystreams[curlogger]".  */

  267. static char *dprintf_channel = "";

  268. /* True if dprintf commands should continue to operate even if GDB
  269.    has disconnected.  */
  270. static int disconnected_dprintf = 1;

  271. /* A reference-counted struct command_line.  This lets multiple
  272.    breakpoints share a single command list.  */
  273. struct counted_command_line
  274. {
  275.   /* The reference count.  */
  276.   int refc;

  277.   /* The command list.  */
  278.   struct command_line *commands;
  279. };

  280. struct command_line *
  281. breakpoint_commands (struct breakpoint *b)
  282. {
  283.   return b->commands ? b->commands->commands : NULL;
  284. }

  285. /* Flag indicating that a command has proceeded the inferior past the
  286.    current breakpoint.  */

  287. static int breakpoint_proceeded;

  288. const char *
  289. bpdisp_text (enum bpdisp disp)
  290. {
  291.   /* NOTE: the following values are a part of MI protocol and
  292.      represent values of 'disp' field returned when inferior stops at
  293.      a breakpoint.  */
  294.   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};

  295.   return bpdisps[(int) disp];
  296. }

  297. /* Prototypes for exported functions.  */
  298. /* If FALSE, gdb will not use hardware support for watchpoints, even
  299.    if such is available.  */
  300. static int can_use_hw_watchpoints;

  301. static void
  302. show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
  303.                              struct cmd_list_element *c,
  304.                              const char *value)
  305. {
  306.   fprintf_filtered (file,
  307.                     _("Debugger's willingness to use "
  308.                       "watchpoint hardware is %s.\n"),
  309.                     value);
  310. }

  311. /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
  312.    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
  313.    for unrecognized breakpoint locations.
  314.    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
  315. static enum auto_boolean pending_break_support;
  316. static void
  317. show_pending_break_support (struct ui_file *file, int from_tty,
  318.                             struct cmd_list_element *c,
  319.                             const char *value)
  320. {
  321.   fprintf_filtered (file,
  322.                     _("Debugger's behavior regarding "
  323.                       "pending breakpoints is %s.\n"),
  324.                     value);
  325. }

  326. /* If 1, gdb will automatically use hardware breakpoints for breakpoints
  327.    set with "break" but falling in read-only memory.
  328.    If 0, gdb will warn about such breakpoints, but won't automatically
  329.    use hardware breakpoints.  */
  330. static int automatic_hardware_breakpoints;
  331. static void
  332. show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
  333.                                      struct cmd_list_element *c,
  334.                                      const char *value)
  335. {
  336.   fprintf_filtered (file,
  337.                     _("Automatic usage of hardware breakpoints is %s.\n"),
  338.                     value);
  339. }

  340. /* If on, GDB keeps breakpoints inserted even if the inferior is
  341.    stopped, and immediately inserts any new breakpoints as soon as
  342.    they're created.  If off (default), GDB keeps breakpoints off of
  343.    the target as long as possible.  That is, it delays inserting
  344.    breakpoints until the next resume, and removes them again when the
  345.    target fully stops.  This is a bit safer in case GDB crashes while
  346.    processing user input.  */
  347. static int always_inserted_mode = 0;

  348. static void
  349. show_always_inserted_mode (struct ui_file *file, int from_tty,
  350.                      struct cmd_list_element *c, const char *value)
  351. {
  352.   fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
  353.                     value);
  354. }

  355. /* See breakpoint.h.  */

  356. int
  357. breakpoints_should_be_inserted_now (void)
  358. {
  359.   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
  360.     {
  361.       /* If breakpoints are global, they should be inserted even if no
  362.          thread under gdb's control is running, or even if there are
  363.          no threads under GDB's control yet.  */
  364.       return 1;
  365.     }
  366.   else if (target_has_execution)
  367.     {
  368.       if (always_inserted_mode)
  369.         {
  370.           /* The user wants breakpoints inserted even if all threads
  371.              are stopped.  */
  372.           return 1;
  373.         }

  374.       if (threads_are_executing ())
  375.         return 1;
  376.     }
  377.   return 0;
  378. }

  379. static const char condition_evaluation_both[] = "host or target";

  380. /* Modes for breakpoint condition evaluation.  */
  381. static const char condition_evaluation_auto[] = "auto";
  382. static const char condition_evaluation_host[] = "host";
  383. static const char condition_evaluation_target[] = "target";
  384. static const char *const condition_evaluation_enums[] = {
  385.   condition_evaluation_auto,
  386.   condition_evaluation_host,
  387.   condition_evaluation_target,
  388.   NULL
  389. };

  390. /* Global that holds the current mode for breakpoint condition evaluation.  */
  391. static const char *condition_evaluation_mode_1 = condition_evaluation_auto;

  392. /* Global that we use to display information to the user (gets its value from
  393.    condition_evaluation_mode_1.  */
  394. static const char *condition_evaluation_mode = condition_evaluation_auto;

  395. /* Translate a condition evaluation mode MODE into either "host"
  396.    or "target".  This is used mostly to translate from "auto" to the
  397.    real setting that is being used.  It returns the translated
  398.    evaluation mode.  */

  399. static const char *
  400. translate_condition_evaluation_mode (const char *mode)
  401. {
  402.   if (mode == condition_evaluation_auto)
  403.     {
  404.       if (target_supports_evaluation_of_breakpoint_conditions ())
  405.         return condition_evaluation_target;
  406.       else
  407.         return condition_evaluation_host;
  408.     }
  409.   else
  410.     return mode;
  411. }

  412. /* Discovers what condition_evaluation_auto translates to.  */

  413. static const char *
  414. breakpoint_condition_evaluation_mode (void)
  415. {
  416.   return translate_condition_evaluation_mode (condition_evaluation_mode);
  417. }

  418. /* Return true if GDB should evaluate breakpoint conditions or false
  419.    otherwise.  */

  420. static int
  421. gdb_evaluates_breakpoint_condition_p (void)
  422. {
  423.   const char *mode = breakpoint_condition_evaluation_mode ();

  424.   return (mode == condition_evaluation_host);
  425. }

  426. void _initialize_breakpoint (void);

  427. /* Are we executing breakpoint commands?  */
  428. static int executing_breakpoint_commands;

  429. /* Are overlay event breakpoints enabled? */
  430. static int overlay_events_enabled;

  431. /* See description in breakpoint.h. */
  432. int target_exact_watchpoints = 0;

  433. /* Walk the following statement or block through all breakpoints.
  434.    ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
  435.    current breakpoint.  */

  436. #define ALL_BREAKPOINTS(Bfor (B = breakpoint_chain; B; B = B->next)

  437. #define ALL_BREAKPOINTS_SAFE(B,TMP)        \
  438.         for (B = breakpoint_chain;        \
  439.              B ? (TMP=B->next, 1): 0;        \
  440.              B = TMP)

  441. /* Similar iterator for the low-level breakpoints.  SAFE variant is
  442.    not provided so update_global_location_list must not be called
  443.    while executing the block of ALL_BP_LOCATIONS.  */

  444. #define ALL_BP_LOCATIONS(B,BP_TMP)                                        \
  445.         for (BP_TMP = bp_location;                                        \
  446.              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP);        \
  447.              BP_TMP++)

  448. /* Iterates through locations with address ADDRESS for the currently selected
  449.    program space.  BP_LOCP_TMP points to each object.  BP_LOCP_START points
  450.    to where the loop should start from.
  451.    If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
  452.    appropriate location to start with.  */

  453. #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS)        \
  454.         for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
  455.              BP_LOCP_TMP = BP_LOCP_START;                                \
  456.              BP_LOCP_START                                                \
  457.              && (BP_LOCP_TMP < bp_location + bp_location_count                \
  458.              && (*BP_LOCP_TMP)->address == ADDRESS);                        \
  459.              BP_LOCP_TMP++)

  460. /* Iterator for tracepoints only.  */

  461. #define ALL_TRACEPOINTS(B)  \
  462.   for (B = breakpoint_chain; B; B = B->next)  \
  463.     if (is_tracepoint (B))

  464. /* Chains of all breakpoints defined.  */

  465. struct breakpoint *breakpoint_chain;

  466. /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */

  467. static struct bp_location **bp_location;

  468. /* Number of elements of BP_LOCATION.  */

  469. static unsigned bp_location_count;

  470. /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
  471.    ADDRESS for the current elements of BP_LOCATION which get a valid
  472.    result from bp_location_has_shadow.  You can use it for roughly
  473.    limiting the subrange of BP_LOCATION to scan for shadow bytes for
  474.    an address you need to read.  */

  475. static CORE_ADDR bp_location_placed_address_before_address_max;

  476. /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
  477.    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
  478.    BP_LOCATION which get a valid result from bp_location_has_shadow.
  479.    You can use it for roughly limiting the subrange of BP_LOCATION to
  480.    scan for shadow bytes for an address you need to read.  */

  481. static CORE_ADDR bp_location_shadow_len_after_address_max;

  482. /* The locations that no longer correspond to any breakpoint, unlinked
  483.    from bp_location array, but for which a hit may still be reported
  484.    by a target.  */
  485. VEC(bp_location_p) *moribund_locations = NULL;

  486. /* Number of last breakpoint made.  */

  487. static int breakpoint_count;

  488. /* The value of `breakpoint_count' before the last command that
  489.    created breakpoints.  If the last (break-like) command created more
  490.    than one breakpoint, then the difference between BREAKPOINT_COUNT
  491.    and PREV_BREAKPOINT_COUNT is more than one.  */
  492. static int prev_breakpoint_count;

  493. /* Number of last tracepoint made.  */

  494. static int tracepoint_count;

  495. static struct cmd_list_element *breakpoint_set_cmdlist;
  496. static struct cmd_list_element *breakpoint_show_cmdlist;
  497. struct cmd_list_element *save_cmdlist;

  498. /* Return whether a breakpoint is an active enabled breakpoint.  */
  499. static int
  500. breakpoint_enabled (struct breakpoint *b)
  501. {
  502.   return (b->enable_state == bp_enabled);
  503. }

  504. /* Set breakpoint count to NUM.  */

  505. static void
  506. set_breakpoint_count (int num)
  507. {
  508.   prev_breakpoint_count = breakpoint_count;
  509.   breakpoint_count = num;
  510.   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
  511. }

  512. /* Used by `start_rbreak_breakpoints' below, to record the current
  513.    breakpoint count before "rbreak" creates any breakpoint.  */
  514. static int rbreak_start_breakpoint_count;

  515. /* Called at the start an "rbreak" command to record the first
  516.    breakpoint made.  */

  517. void
  518. start_rbreak_breakpoints (void)
  519. {
  520.   rbreak_start_breakpoint_count = breakpoint_count;
  521. }

  522. /* Called at the end of an "rbreak" command to record the last
  523.    breakpoint made.  */

  524. void
  525. end_rbreak_breakpoints (void)
  526. {
  527.   prev_breakpoint_count = rbreak_start_breakpoint_count;
  528. }

  529. /* Used in run_command to zero the hit count when a new run starts.  */

  530. void
  531. clear_breakpoint_hit_counts (void)
  532. {
  533.   struct breakpoint *b;

  534.   ALL_BREAKPOINTS (b)
  535.     b->hit_count = 0;
  536. }

  537. /* Allocate a new counted_command_line with reference count of 1.
  538.    The new structure owns COMMANDS.  */

  539. static struct counted_command_line *
  540. alloc_counted_command_line (struct command_line *commands)
  541. {
  542.   struct counted_command_line *result
  543.     = xmalloc (sizeof (struct counted_command_line));

  544.   result->refc = 1;
  545.   result->commands = commands;
  546.   return result;
  547. }

  548. /* Increment reference count.  This does nothing if CMD is NULL.  */

  549. static void
  550. incref_counted_command_line (struct counted_command_line *cmd)
  551. {
  552.   if (cmd)
  553.     ++cmd->refc;
  554. }

  555. /* Decrement reference count.  If the reference count reaches 0,
  556.    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
  557.    nothing if *CMDP is NULL.  */

  558. static void
  559. decref_counted_command_line (struct counted_command_line **cmdp)
  560. {
  561.   if (*cmdp)
  562.     {
  563.       if (--(*cmdp)->refc == 0)
  564.         {
  565.           free_command_lines (&(*cmdp)->commands);
  566.           xfree (*cmdp);
  567.         }
  568.       *cmdp = NULL;
  569.     }
  570. }

  571. /* A cleanup function that calls decref_counted_command_line.  */

  572. static void
  573. do_cleanup_counted_command_line (void *arg)
  574. {
  575.   decref_counted_command_line (arg);
  576. }

  577. /* Create a cleanup that calls decref_counted_command_line on the
  578.    argument.  */

  579. static struct cleanup *
  580. make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
  581. {
  582.   return make_cleanup (do_cleanup_counted_command_line, cmdp);
  583. }


  584. /* Return the breakpoint with the specified number, or NULL
  585.    if the number does not refer to an existing breakpoint.  */

  586. struct breakpoint *
  587. get_breakpoint (int num)
  588. {
  589.   struct breakpoint *b;

  590.   ALL_BREAKPOINTS (b)
  591.     if (b->number == num)
  592.       return b;

  593.   return NULL;
  594. }



  595. /* Mark locations as "conditions have changed" in case the target supports
  596.    evaluating conditions on its side.  */

  597. static void
  598. mark_breakpoint_modified (struct breakpoint *b)
  599. {
  600.   struct bp_location *loc;

  601.   /* This is only meaningful if the target is
  602.      evaluating conditions and if the user has
  603.      opted for condition evaluation on the target's
  604.      side.  */
  605.   if (gdb_evaluates_breakpoint_condition_p ()
  606.       || !target_supports_evaluation_of_breakpoint_conditions ())
  607.     return;

  608.   if (!is_breakpoint (b))
  609.     return;

  610.   for (loc = b->loc; loc; loc = loc->next)
  611.     loc->condition_changed = condition_modified;
  612. }

  613. /* Mark location as "conditions have changed" in case the target supports
  614.    evaluating conditions on its side.  */

  615. static void
  616. mark_breakpoint_location_modified (struct bp_location *loc)
  617. {
  618.   /* This is only meaningful if the target is
  619.      evaluating conditions and if the user has
  620.      opted for condition evaluation on the target's
  621.      side.  */
  622.   if (gdb_evaluates_breakpoint_condition_p ()
  623.       || !target_supports_evaluation_of_breakpoint_conditions ())

  624.     return;

  625.   if (!is_breakpoint (loc->owner))
  626.     return;

  627.   loc->condition_changed = condition_modified;
  628. }

  629. /* Sets the condition-evaluation mode using the static global
  630.    condition_evaluation_mode.  */

  631. static void
  632. set_condition_evaluation_mode (char *args, int from_tty,
  633.                                struct cmd_list_element *c)
  634. {
  635.   const char *old_mode, *new_mode;

  636.   if ((condition_evaluation_mode_1 == condition_evaluation_target)
  637.       && !target_supports_evaluation_of_breakpoint_conditions ())
  638.     {
  639.       condition_evaluation_mode_1 = condition_evaluation_mode;
  640.       warning (_("Target does not support breakpoint condition evaluation.\n"
  641.                  "Using host evaluation mode instead."));
  642.       return;
  643.     }

  644.   new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
  645.   old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);

  646.   /* Flip the switch.  Flip it even if OLD_MODE == NEW_MODE as one of the
  647.      settings was "auto".  */
  648.   condition_evaluation_mode = condition_evaluation_mode_1;

  649.   /* Only update the mode if the user picked a different one.  */
  650.   if (new_mode != old_mode)
  651.     {
  652.       struct bp_location *loc, **loc_tmp;
  653.       /* If the user switched to a different evaluation mode, we
  654.          need to synch the changes with the target as follows:

  655.          "host" -> "target": Send all (valid) conditions to the target.
  656.          "target" -> "host": Remove all the conditions from the target.
  657.       */

  658.       if (new_mode == condition_evaluation_target)
  659.         {
  660.           /* Mark everything modified and synch conditions with the
  661.              target.  */
  662.           ALL_BP_LOCATIONS (loc, loc_tmp)
  663.             mark_breakpoint_location_modified (loc);
  664.           }
  665.       else
  666.         {
  667.           /* Manually mark non-duplicate locations to synch conditions
  668.              with the target.  We do this to remove all the conditions the
  669.              target knows about.  */
  670.           ALL_BP_LOCATIONS (loc, loc_tmp)
  671.             if (is_breakpoint (loc->owner) && loc->inserted)
  672.               loc->needs_update = 1;
  673.         }

  674.       /* Do the update.  */
  675.       update_global_location_list (UGLL_MAY_INSERT);
  676.     }

  677.   return;
  678. }

  679. /* Shows the current mode of breakpoint condition evaluation.  Explicitly shows
  680.    what "auto" is translating to.  */

  681. static void
  682. show_condition_evaluation_mode (struct ui_file *file, int from_tty,
  683.                                 struct cmd_list_element *c, const char *value)
  684. {
  685.   if (condition_evaluation_mode == condition_evaluation_auto)
  686.     fprintf_filtered (file,
  687.                       _("Breakpoint condition evaluation "
  688.                         "mode is %s (currently %s).\n"),
  689.                       value,
  690.                       breakpoint_condition_evaluation_mode ());
  691.   else
  692.     fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
  693.                       value);
  694. }

  695. /* A comparison function for bp_location AP and BP that is used by
  696.    bsearch.  This comparison function only cares about addresses, unlike
  697.    the more general bp_location_compare function.  */

  698. static int
  699. bp_location_compare_addrs (const void *ap, const void *bp)
  700. {
  701.   struct bp_location *a = *(void **) ap;
  702.   struct bp_location *b = *(void **) bp;

  703.   if (a->address == b->address)
  704.     return 0;
  705.   else
  706.     return ((a->address > b->address) - (a->address < b->address));
  707. }

  708. /* Helper function to skip all bp_locations with addresses
  709.    less than ADDRESS.  It returns the first bp_location that
  710.    is greater than or equal to ADDRESS.  If none is found, just
  711.    return NULL.  */

  712. static struct bp_location **
  713. get_first_locp_gte_addr (CORE_ADDR address)
  714. {
  715.   struct bp_location dummy_loc;
  716.   struct bp_location *dummy_locp = &dummy_loc;
  717.   struct bp_location **locp_found = NULL;

  718.   /* Initialize the dummy location's address field.  */
  719.   memset (&dummy_loc, 0, sizeof (struct bp_location));
  720.   dummy_loc.address = address;

  721.   /* Find a close match to the first location at ADDRESS.  */
  722.   locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
  723.                         sizeof (struct bp_location **),
  724.                         bp_location_compare_addrs);

  725.   /* Nothing was found, nothing left to do.  */
  726.   if (locp_found == NULL)
  727.     return NULL;

  728.   /* We may have found a location that is at ADDRESS but is not the first in the
  729.      location's list.  Go backwards (if possible) and locate the first one.  */
  730.   while ((locp_found - 1) >= bp_location
  731.          && (*(locp_found - 1))->address == address)
  732.     locp_found--;

  733.   return locp_found;
  734. }

  735. void
  736. set_breakpoint_condition (struct breakpoint *b, char *exp,
  737.                           int from_tty)
  738. {
  739.   xfree (b->cond_string);
  740.   b->cond_string = NULL;

  741.   if (is_watchpoint (b))
  742.     {
  743.       struct watchpoint *w = (struct watchpoint *) b;

  744.       xfree (w->cond_exp);
  745.       w->cond_exp = NULL;
  746.     }
  747.   else
  748.     {
  749.       struct bp_location *loc;

  750.       for (loc = b->loc; loc; loc = loc->next)
  751.         {
  752.           xfree (loc->cond);
  753.           loc->cond = NULL;

  754.           /* No need to free the condition agent expression
  755.              bytecode (if we have one).  We will handle this
  756.              when we go through update_global_location_list.  */
  757.         }
  758.     }

  759.   if (*exp == 0)
  760.     {
  761.       if (from_tty)
  762.         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
  763.     }
  764.   else
  765.     {
  766.       const char *arg = exp;

  767.       /* I don't know if it matters whether this is the string the user
  768.          typed in or the decompiled expression.  */
  769.       b->cond_string = xstrdup (arg);
  770.       b->condition_not_parsed = 0;

  771.       if (is_watchpoint (b))
  772.         {
  773.           struct watchpoint *w = (struct watchpoint *) b;

  774.           innermost_block = NULL;
  775.           arg = exp;
  776.           w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
  777.           if (*arg)
  778.             error (_("Junk at end of expression"));
  779.           w->cond_exp_valid_block = innermost_block;
  780.         }
  781.       else
  782.         {
  783.           struct bp_location *loc;

  784.           for (loc = b->loc; loc; loc = loc->next)
  785.             {
  786.               arg = exp;
  787.               loc->cond =
  788.                 parse_exp_1 (&arg, loc->address,
  789.                              block_for_pc (loc->address), 0);
  790.               if (*arg)
  791.                 error (_("Junk at end of expression"));
  792.             }
  793.         }
  794.     }
  795.   mark_breakpoint_modified (b);

  796.   observer_notify_breakpoint_modified (b);
  797. }

  798. /* Completion for the "condition" command.  */

  799. static VEC (char_ptr) *
  800. condition_completer (struct cmd_list_element *cmd,
  801.                      const char *text, const char *word)
  802. {
  803.   const char *space;

  804.   text = skip_spaces_const (text);
  805.   space = skip_to_space_const (text);
  806.   if (*space == '\0')
  807.     {
  808.       int len;
  809.       struct breakpoint *b;
  810.       VEC (char_ptr) *result = NULL;

  811.       if (text[0] == '$')
  812.         {
  813.           /* We don't support completion of history indices.  */
  814.           if (isdigit (text[1]))
  815.             return NULL;
  816.           return complete_internalvar (&text[1]);
  817.         }

  818.       /* We're completing the breakpoint number.  */
  819.       len = strlen (text);

  820.       ALL_BREAKPOINTS (b)
  821.         {
  822.           char number[50];

  823.           xsnprintf (number, sizeof (number), "%d", b->number);

  824.           if (strncmp (number, text, len) == 0)
  825.             VEC_safe_push (char_ptr, result, xstrdup (number));
  826.         }

  827.       return result;
  828.     }

  829.   /* We're completing the expression part.  */
  830.   text = skip_spaces_const (space);
  831.   return expression_completer (cmd, text, word);
  832. }

  833. /* condition N EXP -- set break condition of breakpoint N to EXP.  */

  834. static void
  835. condition_command (char *arg, int from_tty)
  836. {
  837.   struct breakpoint *b;
  838.   char *p;
  839.   int bnum;

  840.   if (arg == 0)
  841.     error_no_arg (_("breakpoint number"));

  842.   p = arg;
  843.   bnum = get_number (&p);
  844.   if (bnum == 0)
  845.     error (_("Bad breakpoint argument: '%s'"), arg);

  846.   ALL_BREAKPOINTS (b)
  847.     if (b->number == bnum)
  848.       {
  849.         /* Check if this breakpoint has a "stop" method implemented in an
  850.            extension language.  This method and conditions entered into GDB
  851.            from the CLI are mutually exclusive.  */
  852.         const struct extension_language_defn *extlang
  853.           = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);

  854.         if (extlang != NULL)
  855.           {
  856.             error (_("Only one stop condition allowed.  There is currently"
  857.                      " a %s stop condition defined for this breakpoint."),
  858.                    ext_lang_capitalized_name (extlang));
  859.           }
  860.         set_breakpoint_condition (b, p, from_tty);

  861.         if (is_breakpoint (b))
  862.           update_global_location_list (UGLL_MAY_INSERT);

  863.         return;
  864.       }

  865.   error (_("No breakpoint number %d."), bnum);
  866. }

  867. /* Check that COMMAND do not contain commands that are suitable
  868.    only for tracepoints and not suitable for ordinary breakpoints.
  869.    Throw if any such commands is found.  */

  870. static void
  871. check_no_tracepoint_commands (struct command_line *commands)
  872. {
  873.   struct command_line *c;

  874.   for (c = commands; c; c = c->next)
  875.     {
  876.       int i;

  877.       if (c->control_type == while_stepping_control)
  878.         error (_("The 'while-stepping' command can "
  879.                  "only be used for tracepoints"));

  880.       for (i = 0; i < c->body_count; ++i)
  881.         check_no_tracepoint_commands ((c->body_list)[i]);

  882.       /* Not that command parsing removes leading whitespace and comment
  883.          lines and also empty lines.  So, we only need to check for
  884.          command directly.  */
  885.       if (strstr (c->line, "collect ") == c->line)
  886.         error (_("The 'collect' command can only be used for tracepoints"));

  887.       if (strstr (c->line, "teval ") == c->line)
  888.         error (_("The 'teval' command can only be used for tracepoints"));
  889.     }
  890. }

  891. /* Encapsulate tests for different types of tracepoints.  */

  892. static int
  893. is_tracepoint_type (enum bptype type)
  894. {
  895.   return (type == bp_tracepoint
  896.           || type == bp_fast_tracepoint
  897.           || type == bp_static_tracepoint);
  898. }

  899. int
  900. is_tracepoint (const struct breakpoint *b)
  901. {
  902.   return is_tracepoint_type (b->type);
  903. }

  904. /* A helper function that validates that COMMANDS are valid for a
  905.    breakpoint.  This function will throw an exception if a problem is
  906.    found.  */

  907. static void
  908. validate_commands_for_breakpoint (struct breakpoint *b,
  909.                                   struct command_line *commands)
  910. {
  911.   if (is_tracepoint (b))
  912.     {
  913.       struct tracepoint *t = (struct tracepoint *) b;
  914.       struct command_line *c;
  915.       struct command_line *while_stepping = 0;

  916.       /* Reset the while-stepping step count.  The previous commands
  917.          might have included a while-stepping action, while the new
  918.          ones might not.  */
  919.       t->step_count = 0;

  920.       /* We need to verify that each top-level element of commands is
  921.          valid for tracepoints, that there's at most one
  922.          while-stepping element, and that the while-stepping's body
  923.          has valid tracing commands excluding nested while-stepping.
  924.          We also need to validate the tracepoint action line in the
  925.          context of the tracepoint --- validate_actionline actually
  926.          has side effects, like setting the tracepoint's
  927.          while-stepping STEP_COUNT, in addition to checking if the
  928.          collect/teval actions parse and make sense in the
  929.          tracepoint's context.  */
  930.       for (c = commands; c; c = c->next)
  931.         {
  932.           if (c->control_type == while_stepping_control)
  933.             {
  934.               if (b->type == bp_fast_tracepoint)
  935.                 error (_("The 'while-stepping' command "
  936.                          "cannot be used for fast tracepoint"));
  937.               else if (b->type == bp_static_tracepoint)
  938.                 error (_("The 'while-stepping' command "
  939.                          "cannot be used for static tracepoint"));

  940.               if (while_stepping)
  941.                 error (_("The 'while-stepping' command "
  942.                          "can be used only once"));
  943.               else
  944.                 while_stepping = c;
  945.             }

  946.           validate_actionline (c->line, b);
  947.         }
  948.       if (while_stepping)
  949.         {
  950.           struct command_line *c2;

  951.           gdb_assert (while_stepping->body_count == 1);
  952.           c2 = while_stepping->body_list[0];
  953.           for (; c2; c2 = c2->next)
  954.             {
  955.               if (c2->control_type == while_stepping_control)
  956.                 error (_("The 'while-stepping' command cannot be nested"));
  957.             }
  958.         }
  959.     }
  960.   else
  961.     {
  962.       check_no_tracepoint_commands (commands);
  963.     }
  964. }

  965. /* Return a vector of all the static tracepoints set at ADDR.  The
  966.    caller is responsible for releasing the vector.  */

  967. VEC(breakpoint_p) *
  968. static_tracepoints_here (CORE_ADDR addr)
  969. {
  970.   struct breakpoint *b;
  971.   VEC(breakpoint_p) *found = 0;
  972.   struct bp_location *loc;

  973.   ALL_BREAKPOINTS (b)
  974.     if (b->type == bp_static_tracepoint)
  975.       {
  976.         for (loc = b->loc; loc; loc = loc->next)
  977.           if (loc->address == addr)
  978.             VEC_safe_push(breakpoint_p, found, b);
  979.       }

  980.   return found;
  981. }

  982. /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
  983.    validate that only allowed commands are included.  */

  984. void
  985. breakpoint_set_commands (struct breakpoint *b,
  986.                          struct command_line *commands)
  987. {
  988.   validate_commands_for_breakpoint (b, commands);

  989.   decref_counted_command_line (&b->commands);
  990.   b->commands = alloc_counted_command_line (commands);
  991.   observer_notify_breakpoint_modified (b);
  992. }

  993. /* Set the internal `silent' flag on the breakpoint.  Note that this
  994.    is not the same as the "silent" that may appear in the breakpoint's
  995.    commands.  */

  996. void
  997. breakpoint_set_silent (struct breakpoint *b, int silent)
  998. {
  999.   int old_silent = b->silent;

  1000.   b->silent = silent;
  1001.   if (old_silent != silent)
  1002.     observer_notify_breakpoint_modified (b);
  1003. }

  1004. /* Set the thread for this breakpoint.  If THREAD is -1, make the
  1005.    breakpoint work for any thread.  */

  1006. void
  1007. breakpoint_set_thread (struct breakpoint *b, int thread)
  1008. {
  1009.   int old_thread = b->thread;

  1010.   b->thread = thread;
  1011.   if (old_thread != thread)
  1012.     observer_notify_breakpoint_modified (b);
  1013. }

  1014. /* Set the task for this breakpoint.  If TASK is 0, make the
  1015.    breakpoint work for any task.  */

  1016. void
  1017. breakpoint_set_task (struct breakpoint *b, int task)
  1018. {
  1019.   int old_task = b->task;

  1020.   b->task = task;
  1021.   if (old_task != task)
  1022.     observer_notify_breakpoint_modified (b);
  1023. }

  1024. void
  1025. check_tracepoint_command (char *line, void *closure)
  1026. {
  1027.   struct breakpoint *b = closure;

  1028.   validate_actionline (line, b);
  1029. }

  1030. /* A structure used to pass information through
  1031.    map_breakpoint_numbers.  */

  1032. struct commands_info
  1033. {
  1034.   /* True if the command was typed at a tty.  */
  1035.   int from_tty;

  1036.   /* The breakpoint range spec.  */
  1037.   char *arg;

  1038.   /* Non-NULL if the body of the commands are being read from this
  1039.      already-parsed command.  */
  1040.   struct command_line *control;

  1041.   /* The command lines read from the user, or NULL if they have not
  1042.      yet been read.  */
  1043.   struct counted_command_line *cmd;
  1044. };

  1045. /* A callback for map_breakpoint_numbers that sets the commands for
  1046.    commands_command.  */

  1047. static void
  1048. do_map_commands_command (struct breakpoint *b, void *data)
  1049. {
  1050.   struct commands_info *info = data;

  1051.   if (info->cmd == NULL)
  1052.     {
  1053.       struct command_line *l;

  1054.       if (info->control != NULL)
  1055.         l = copy_command_lines (info->control->body_list[0]);
  1056.       else
  1057.         {
  1058.           struct cleanup *old_chain;
  1059.           char *str;

  1060.           str = xstrprintf (_("Type commands for breakpoint(s) "
  1061.                               "%s, one per line."),
  1062.                             info->arg);

  1063.           old_chain = make_cleanup (xfree, str);

  1064.           l = read_command_lines (str,
  1065.                                   info->from_tty, 1,
  1066.                                   (is_tracepoint (b)
  1067.                                    ? check_tracepoint_command : 0),
  1068.                                   b);

  1069.           do_cleanups (old_chain);
  1070.         }

  1071.       info->cmd = alloc_counted_command_line (l);
  1072.     }

  1073.   /* If a breakpoint was on the list more than once, we don't need to
  1074.      do anything.  */
  1075.   if (b->commands != info->cmd)
  1076.     {
  1077.       validate_commands_for_breakpoint (b, info->cmd->commands);
  1078.       incref_counted_command_line (info->cmd);
  1079.       decref_counted_command_line (&b->commands);
  1080.       b->commands = info->cmd;
  1081.       observer_notify_breakpoint_modified (b);
  1082.     }
  1083. }

  1084. static void
  1085. commands_command_1 (char *arg, int from_tty,
  1086.                     struct command_line *control)
  1087. {
  1088.   struct cleanup *cleanups;
  1089.   struct commands_info info;

  1090.   info.from_tty = from_tty;
  1091.   info.control = control;
  1092.   info.cmd = NULL;
  1093.   /* If we read command lines from the user, then `info' will hold an
  1094.      extra reference to the commands that we must clean up.  */
  1095.   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);

  1096.   if (arg == NULL || !*arg)
  1097.     {
  1098.       if (breakpoint_count - prev_breakpoint_count > 1)
  1099.         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
  1100.                           breakpoint_count);
  1101.       else if (breakpoint_count > 0)
  1102.         arg = xstrprintf ("%d", breakpoint_count);
  1103.       else
  1104.         {
  1105.           /* So that we don't try to free the incoming non-NULL
  1106.              argument in the cleanup below.  Mapping breakpoint
  1107.              numbers will fail in this case.  */
  1108.           arg = NULL;
  1109.         }
  1110.     }
  1111.   else
  1112.     /* The command loop has some static state, so we need to preserve
  1113.        our argument.  */
  1114.     arg = xstrdup (arg);

  1115.   if (arg != NULL)
  1116.     make_cleanup (xfree, arg);

  1117.   info.arg = arg;

  1118.   map_breakpoint_numbers (arg, do_map_commands_command, &info);

  1119.   if (info.cmd == NULL)
  1120.     error (_("No breakpoints specified."));

  1121.   do_cleanups (cleanups);
  1122. }

  1123. static void
  1124. commands_command (char *arg, int from_tty)
  1125. {
  1126.   commands_command_1 (arg, from_tty, NULL);
  1127. }

  1128. /* Like commands_command, but instead of reading the commands from
  1129.    input stream, takes them from an already parsed command structure.

  1130.    This is used by cli-script.c to DTRT with breakpoint commands
  1131.    that are part of if and while bodies.  */
  1132. enum command_control_type
  1133. commands_from_control_command (char *arg, struct command_line *cmd)
  1134. {
  1135.   commands_command_1 (arg, 0, cmd);
  1136.   return simple_control;
  1137. }

  1138. /* Return non-zero if BL->TARGET_INFO contains valid information.  */

  1139. static int
  1140. bp_location_has_shadow (struct bp_location *bl)
  1141. {
  1142.   if (bl->loc_type != bp_loc_software_breakpoint)
  1143.     return 0;
  1144.   if (!bl->inserted)
  1145.     return 0;
  1146.   if (bl->target_info.shadow_len == 0)
  1147.     /* BL isn't valid, or doesn't shadow memory.  */
  1148.     return 0;
  1149.   return 1;
  1150. }

  1151. /* Update BUF, which is LEN bytes read from the target address
  1152.    MEMADDR, by replacing a memory breakpoint with its shadowed
  1153.    contents.

  1154.    If READBUF is not NULL, this buffer must not overlap with the of
  1155.    the breakpoint location's shadow_contents buffer.  Otherwise, a
  1156.    failed assertion internal error will be raised.  */

  1157. static void
  1158. one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
  1159.                             const gdb_byte *writebuf_org,
  1160.                             ULONGEST memaddr, LONGEST len,
  1161.                             struct bp_target_info *target_info,
  1162.                             struct gdbarch *gdbarch)
  1163. {
  1164.   /* Now do full processing of the found relevant range of elements.  */
  1165.   CORE_ADDR bp_addr = 0;
  1166.   int bp_size = 0;
  1167.   int bptoffset = 0;

  1168.   if (!breakpoint_address_match (target_info->placed_address_space, 0,
  1169.                                  current_program_space->aspace, 0))
  1170.     {
  1171.       /* The breakpoint is inserted in a different address space.  */
  1172.       return;
  1173.     }

  1174.   /* Addresses and length of the part of the breakpoint that
  1175.      we need to copy.  */
  1176.   bp_addr = target_info->placed_address;
  1177.   bp_size = target_info->shadow_len;

  1178.   if (bp_addr + bp_size <= memaddr)
  1179.     {
  1180.       /* The breakpoint is entirely before the chunk of memory we are
  1181.          reading.  */
  1182.       return;
  1183.     }

  1184.   if (bp_addr >= memaddr + len)
  1185.     {
  1186.       /* The breakpoint is entirely after the chunk of memory we are
  1187.          reading.  */
  1188.       return;
  1189.     }

  1190.   /* Offset within shadow_contents.  */
  1191.   if (bp_addr < memaddr)
  1192.     {
  1193.       /* Only copy the second part of the breakpoint.  */
  1194.       bp_size -= memaddr - bp_addr;
  1195.       bptoffset = memaddr - bp_addr;
  1196.       bp_addr = memaddr;
  1197.     }

  1198.   if (bp_addr + bp_size > memaddr + len)
  1199.     {
  1200.       /* Only copy the first part of the breakpoint.  */
  1201.       bp_size -= (bp_addr + bp_size) - (memaddr + len);
  1202.     }

  1203.   if (readbuf != NULL)
  1204.     {
  1205.       /* Verify that the readbuf buffer does not overlap with the
  1206.          shadow_contents buffer.  */
  1207.       gdb_assert (target_info->shadow_contents >= readbuf + len
  1208.                   || readbuf >= (target_info->shadow_contents
  1209.                                  + target_info->shadow_len));

  1210.       /* Update the read buffer with this inserted breakpoint's
  1211.          shadow.  */
  1212.       memcpy (readbuf + bp_addr - memaddr,
  1213.               target_info->shadow_contents + bptoffset, bp_size);
  1214.     }
  1215.   else
  1216.     {
  1217.       const unsigned char *bp;
  1218.       CORE_ADDR addr = target_info->reqstd_address;
  1219.       int placed_size;

  1220.       /* Update the shadow with what we want to write to memory.  */
  1221.       memcpy (target_info->shadow_contents + bptoffset,
  1222.               writebuf_org + bp_addr - memaddr, bp_size);

  1223.       /* Determine appropriate breakpoint contents and size for this
  1224.          address.  */
  1225.       bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);

  1226.       /* Update the final write buffer with this inserted
  1227.          breakpoint's INSN.  */
  1228.       memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
  1229.     }
  1230. }

  1231. /* Update BUF, which is LEN bytes read from the target address MEMADDR,
  1232.    by replacing any memory breakpoints with their shadowed contents.

  1233.    If READBUF is not NULL, this buffer must not overlap with any of
  1234.    the breakpoint location's shadow_contents buffers.  Otherwise,
  1235.    a failed assertion internal error will be raised.

  1236.    The range of shadowed area by each bp_location is:
  1237.      bl->address - bp_location_placed_address_before_address_max
  1238.      up to bl->address + bp_location_shadow_len_after_address_max
  1239.    The range we were requested to resolve shadows for is:
  1240.      memaddr ... memaddr + len
  1241.    Thus the safe cutoff boundaries for performance optimization are
  1242.      memaddr + len <= (bl->address
  1243.                        - bp_location_placed_address_before_address_max)
  1244.    and:
  1245.      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */

  1246. void
  1247. breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
  1248.                         const gdb_byte *writebuf_org,
  1249.                         ULONGEST memaddr, LONGEST len)
  1250. {
  1251.   /* Left boundary, right boundary and median element of our binary
  1252.      search.  */
  1253.   unsigned bc_l, bc_r, bc;
  1254.   size_t i;

  1255.   /* Find BC_L which is a leftmost element which may affect BUF
  1256.      content.  It is safe to report lower value but a failure to
  1257.      report higher one.  */

  1258.   bc_l = 0;
  1259.   bc_r = bp_location_count;
  1260.   while (bc_l + 1 < bc_r)
  1261.     {
  1262.       struct bp_location *bl;

  1263.       bc = (bc_l + bc_r) / 2;
  1264.       bl = bp_location[bc];

  1265.       /* Check first BL->ADDRESS will not overflow due to the added
  1266.          constant.  Then advance the left boundary only if we are sure
  1267.          the BC element can in no way affect the BUF content (MEMADDR
  1268.          to MEMADDR + LEN range).

  1269.          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
  1270.          offset so that we cannot miss a breakpoint with its shadow
  1271.          range tail still reaching MEMADDR.  */

  1272.       if ((bl->address + bp_location_shadow_len_after_address_max
  1273.            >= bl->address)
  1274.           && (bl->address + bp_location_shadow_len_after_address_max
  1275.               <= memaddr))
  1276.         bc_l = bc;
  1277.       else
  1278.         bc_r = bc;
  1279.     }

  1280.   /* Due to the binary search above, we need to make sure we pick the
  1281.      first location that's at BC_L's addressE.g., if there are
  1282.      multiple locations at the same address, BC_L may end up pointing
  1283.      at a duplicate location, and miss the "master"/"inserted"
  1284.      location.  Say, given locations L1, L2 and L3 at addresses A and
  1285.      B:

  1286.       L1@A, L2@A, L3@B, ...

  1287.      BC_L could end up pointing at location L2, while the "master"
  1288.      location could be L1.  Since the `loc->inserted' flag is only set
  1289.      on "master" locations, we'd forget to restore the shadow of L1
  1290.      and L2.  */
  1291.   while (bc_l > 0
  1292.          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
  1293.     bc_l--;

  1294.   /* Now do full processing of the found relevant range of elements.  */

  1295.   for (bc = bc_l; bc < bp_location_count; bc++)
  1296.   {
  1297.     struct bp_location *bl = bp_location[bc];
  1298.     CORE_ADDR bp_addr = 0;
  1299.     int bp_size = 0;
  1300.     int bptoffset = 0;

  1301.     /* bp_location array has BL->OWNER always non-NULL.  */
  1302.     if (bl->owner->type == bp_none)
  1303.       warning (_("reading through apparently deleted breakpoint #%d?"),
  1304.                bl->owner->number);

  1305.     /* Performance optimization: any further element can no longer affect BUF
  1306.        content.  */

  1307.     if (bl->address >= bp_location_placed_address_before_address_max
  1308.         && memaddr + len <= (bl->address
  1309.                              - bp_location_placed_address_before_address_max))
  1310.       break;

  1311.     if (!bp_location_has_shadow (bl))
  1312.       continue;

  1313.     one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
  1314.                                 memaddr, len, &bl->target_info, bl->gdbarch);
  1315.   }
  1316. }



  1317. /* Return true if BPT is either a software breakpoint or a hardware
  1318.    breakpoint.  */

  1319. int
  1320. is_breakpoint (const struct breakpoint *bpt)
  1321. {
  1322.   return (bpt->type == bp_breakpoint
  1323.           || bpt->type == bp_hardware_breakpoint
  1324.           || bpt->type == bp_dprintf);
  1325. }

  1326. /* Return true if BPT is of any hardware watchpoint kind.  */

  1327. static int
  1328. is_hardware_watchpoint (const struct breakpoint *bpt)
  1329. {
  1330.   return (bpt->type == bp_hardware_watchpoint
  1331.           || bpt->type == bp_read_watchpoint
  1332.           || bpt->type == bp_access_watchpoint);
  1333. }

  1334. /* Return true if BPT is of any watchpoint kind, hardware or
  1335.    software.  */

  1336. int
  1337. is_watchpoint (const struct breakpoint *bpt)
  1338. {
  1339.   return (is_hardware_watchpoint (bpt)
  1340.           || bpt->type == bp_watchpoint);
  1341. }

  1342. /* Returns true if the current thread and its running state are safe
  1343.    to evaluate or update watchpoint B.  Watchpoints on local
  1344.    expressions need to be evaluated in the context of the thread that
  1345.    was current when the watchpoint was created, and, that thread needs
  1346.    to be stopped to be able to select the correct frame context.
  1347.    Watchpoints on global expressions can be evaluated on any thread,
  1348.    and in any state.  It is presently left to the target allowing
  1349.    memory accesses when threads are running.  */

  1350. static int
  1351. watchpoint_in_thread_scope (struct watchpoint *b)
  1352. {
  1353.   return (b->base.pspace == current_program_space
  1354.           && (ptid_equal (b->watchpoint_thread, null_ptid)
  1355.               || (ptid_equal (inferior_ptid, b->watchpoint_thread)
  1356.                   && !is_executing (inferior_ptid))));
  1357. }

  1358. /* Set watchpoint B to disp_del_at_next_stop, even including its possible
  1359.    associated bp_watchpoint_scope breakpoint.  */

  1360. static void
  1361. watchpoint_del_at_next_stop (struct watchpoint *w)
  1362. {
  1363.   struct breakpoint *b = &w->base;

  1364.   if (b->related_breakpoint != b)
  1365.     {
  1366.       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
  1367.       gdb_assert (b->related_breakpoint->related_breakpoint == b);
  1368.       b->related_breakpoint->disposition = disp_del_at_next_stop;
  1369.       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
  1370.       b->related_breakpoint = b;
  1371.     }
  1372.   b->disposition = disp_del_at_next_stop;
  1373. }

  1374. /* Extract a bitfield value from value VAL using the bit parameters contained in
  1375.    watchpoint W.  */

  1376. static struct value *
  1377. extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
  1378. {
  1379.   struct value *bit_val;

  1380.   if (val == NULL)
  1381.     return NULL;

  1382.   bit_val = allocate_value (value_type (val));

  1383.   unpack_value_bitfield (bit_val,
  1384.                          w->val_bitpos,
  1385.                          w->val_bitsize,
  1386.                          value_contents_for_printing (val),
  1387.                          value_offset (val),
  1388.                          val);

  1389.   return bit_val;
  1390. }

  1391. /* Assuming that B is a watchpoint:
  1392.    - Reparse watchpoint expression, if REPARSE is non-zero
  1393.    - Evaluate expression and store the result in B->val
  1394.    - Evaluate the condition if there is one, and store the result
  1395.      in b->loc->cond.
  1396.    - Update the list of values that must be watched in B->loc.

  1397.    If the watchpoint disposition is disp_del_at_next_stop, then do
  1398.    nothing.  If this is local watchpoint that is out of scope, delete
  1399.    it.

  1400.    Even with `set breakpoint always-inserted on' the watchpoints are
  1401.    removed + inserted on each stop here.  Normal breakpoints must
  1402.    never be removed because they might be missed by a running thread
  1403.    when debugging in non-stop mode.  On the other hand, hardware
  1404.    watchpoints (is_hardware_watchpoint; processed here) are specific
  1405.    to each LWP since they are stored in each LWP's hardware debug
  1406.    registers.  Therefore, such LWP must be stopped first in order to
  1407.    be able to modify its hardware watchpoints.

  1408.    Hardware watchpoints must be reset exactly once after being
  1409.    presented to the user.  It cannot be done sooner, because it would
  1410.    reset the data used to present the watchpoint hit to the user.  And
  1411.    it must not be done later because it could display the same single
  1412.    watchpoint hit during multiple GDB stops.  Note that the latter is
  1413.    relevant only to the hardware watchpoint types bp_read_watchpoint
  1414.    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
  1415.    not user-visible - its hit is suppressed if the memory content has
  1416.    not changed.

  1417.    The following constraints influence the location where we can reset
  1418.    hardware watchpoints:

  1419.    * target_stopped_by_watchpoint and target_stopped_data_address are
  1420.      called several times when GDB stops.

  1421.    [linux]
  1422.    * Multiple hardware watchpoints can be hit at the same time,
  1423.      causing GDB to stop.  GDB only presents one hardware watchpoint
  1424.      hit at a time as the reason for stopping, and all the other hits
  1425.      are presented later, one after the other, each time the user
  1426.      requests the execution to be resumed.  Execution is not resumed
  1427.      for the threads still having pending hit event stored in
  1428.      LWP_INFO->STATUS.  While the watchpoint is already removed from
  1429.      the inferior on the first stop the thread hit event is kept being
  1430.      reported from its cached value by linux_nat_stopped_data_address
  1431.      until the real thread resume happens after the watchpoint gets
  1432.      presented and thus its LWP_INFO->STATUS gets reset.

  1433.    Therefore the hardware watchpoint hit can get safely reset on the
  1434.    watchpoint removal from inferior.  */

  1435. static void
  1436. update_watchpoint (struct watchpoint *b, int reparse)
  1437. {
  1438.   int within_current_scope;
  1439.   struct frame_id saved_frame_id;
  1440.   int frame_saved;

  1441.   /* If this is a local watchpoint, we only want to check if the
  1442.      watchpoint frame is in scope if the current thread is the thread
  1443.      that was used to create the watchpoint.  */
  1444.   if (!watchpoint_in_thread_scope (b))
  1445.     return;

  1446.   if (b->base.disposition == disp_del_at_next_stop)
  1447.     return;

  1448.   frame_saved = 0;

  1449.   /* Determine if the watchpoint is within scope.  */
  1450.   if (b->exp_valid_block == NULL)
  1451.     within_current_scope = 1;
  1452.   else
  1453.     {
  1454.       struct frame_info *fi = get_current_frame ();
  1455.       struct gdbarch *frame_arch = get_frame_arch (fi);
  1456.       CORE_ADDR frame_pc = get_frame_pc (fi);

  1457.       /* If we're in a function epilogue, unwinding may not work
  1458.          properly, so do not attempt to recreate locations at this
  1459.          point.  See similar comments in watchpoint_check.  */
  1460.       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
  1461.         return;

  1462.       /* Save the current frame's ID so we can restore it after
  1463.          evaluating the watchpoint expression on its own frame.  */
  1464.       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
  1465.          took a frame parameter, so that we didn't have to change the
  1466.          selected frame.  */
  1467.       frame_saved = 1;
  1468.       saved_frame_id = get_frame_id (get_selected_frame (NULL));

  1469.       fi = frame_find_by_id (b->watchpoint_frame);
  1470.       within_current_scope = (fi != NULL);
  1471.       if (within_current_scope)
  1472.         select_frame (fi);
  1473.     }

  1474.   /* We don't free locations.  They are stored in the bp_location array
  1475.      and update_global_location_list will eventually delete them and
  1476.      remove breakpoints if needed.  */
  1477.   b->base.loc = NULL;

  1478.   if (within_current_scope && reparse)
  1479.     {
  1480.       const char *s;

  1481.       if (b->exp)
  1482.         {
  1483.           xfree (b->exp);
  1484.           b->exp = NULL;
  1485.         }
  1486.       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
  1487.       b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
  1488.       /* If the meaning of expression itself changed, the old value is
  1489.          no longer relevant.  We don't want to report a watchpoint hit
  1490.          to the user when the old value and the new value may actually
  1491.          be completely different objects.  */
  1492.       value_free (b->val);
  1493.       b->val = NULL;
  1494.       b->val_valid = 0;

  1495.       /* Note that unlike with breakpoints, the watchpoint's condition
  1496.          expression is stored in the breakpoint object, not in the
  1497.          locations (re)created below.  */
  1498.       if (b->base.cond_string != NULL)
  1499.         {
  1500.           if (b->cond_exp != NULL)
  1501.             {
  1502.               xfree (b->cond_exp);
  1503.               b->cond_exp = NULL;
  1504.             }

  1505.           s = b->base.cond_string;
  1506.           b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
  1507.         }
  1508.     }

  1509.   /* If we failed to parse the expression, for example because
  1510.      it refers to a global variable in a not-yet-loaded shared library,
  1511.      don't try to insert watchpoint.  We don't automatically delete
  1512.      such watchpoint, though, since failure to parse expression
  1513.      is different from out-of-scope watchpoint.  */
  1514.   if (!target_has_execution)
  1515.     {
  1516.       /* Without execution, memory can't change.  No use to try and
  1517.          set watchpoint locations.  The watchpoint will be reset when
  1518.          the target gains execution, through breakpoint_re_set.  */
  1519.       if (!can_use_hw_watchpoints)
  1520.         {
  1521.           if (b->base.ops->works_in_software_mode (&b->base))
  1522.             b->base.type = bp_watchpoint;
  1523.           else
  1524.             error (_("Can't set read/access watchpoint when "
  1525.                      "hardware watchpoints are disabled."));
  1526.         }
  1527.     }
  1528.   else if (within_current_scope && b->exp)
  1529.     {
  1530.       int pc = 0;
  1531.       struct value *val_chain, *v, *result, *next;
  1532.       struct program_space *frame_pspace;

  1533.       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);

  1534.       /* Avoid setting b->val if it's already set.  The meaning of
  1535.          b->val is 'the last value' user saw, and we should update
  1536.          it only if we reported that last value to user.  As it
  1537.          happens, the code that reports it updates b->val directly.
  1538.          We don't keep track of the memory value for masked
  1539.          watchpoints.  */
  1540.       if (!b->val_valid && !is_masked_watchpoint (&b->base))
  1541.         {
  1542.           if (b->val_bitsize != 0)
  1543.             {
  1544.               v = extract_bitfield_from_watchpoint_value (b, v);
  1545.               if (v != NULL)
  1546.                 release_value (v);
  1547.             }
  1548.           b->val = v;
  1549.           b->val_valid = 1;
  1550.         }

  1551.       frame_pspace = get_frame_program_space (get_selected_frame (NULL));

  1552.       /* Look at each value on the value chain.  */
  1553.       for (v = val_chain; v; v = value_next (v))
  1554.         {
  1555.           /* If it's a memory location, and GDB actually needed
  1556.              its contents to evaluate the expression, then we
  1557.              must watch it.  If the first value returned is
  1558.              still lazy, that means an error occurred reading it;
  1559.              watch it anyway in case it becomes readable.  */
  1560.           if (VALUE_LVAL (v) == lval_memory
  1561.               && (v == val_chain || ! value_lazy (v)))
  1562.             {
  1563.               struct type *vtype = check_typedef (value_type (v));

  1564.               /* We only watch structs and arrays if user asked
  1565.                  for it explicitly, never if they just happen to
  1566.                  appear in the middle of some value chain.  */
  1567.               if (v == result
  1568.                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
  1569.                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
  1570.                 {
  1571.                   CORE_ADDR addr;
  1572.                   int type;
  1573.                   struct bp_location *loc, **tmp;
  1574.                   int bitpos = 0, bitsize = 0;

  1575.                   if (value_bitsize (v) != 0)
  1576.                     {
  1577.                       /* Extract the bit parameters out from the bitfield
  1578.                          sub-expression.  */
  1579.                       bitpos = value_bitpos (v);
  1580.                       bitsize = value_bitsize (v);
  1581.                     }
  1582.                   else if (v == result && b->val_bitsize != 0)
  1583.                     {
  1584.                      /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
  1585.                         lvalue whose bit parameters are saved in the fields
  1586.                         VAL_BITPOS and VAL_BITSIZE.  */
  1587.                       bitpos = b->val_bitpos;
  1588.                       bitsize = b->val_bitsize;
  1589.                     }

  1590.                   addr = value_address (v);
  1591.                   if (bitsize != 0)
  1592.                     {
  1593.                       /* Skip the bytes that don't contain the bitfield.  */
  1594.                       addr += bitpos / 8;
  1595.                     }

  1596.                   type = hw_write;
  1597.                   if (b->base.type == bp_read_watchpoint)
  1598.                     type = hw_read;
  1599.                   else if (b->base.type == bp_access_watchpoint)
  1600.                     type = hw_access;

  1601.                   loc = allocate_bp_location (&b->base);
  1602.                   for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
  1603.                     ;
  1604.                   *tmp = loc;
  1605.                   loc->gdbarch = get_type_arch (value_type (v));

  1606.                   loc->pspace = frame_pspace;
  1607.                   loc->address = addr;

  1608.                   if (bitsize != 0)
  1609.                     {
  1610.                       /* Just cover the bytes that make up the bitfield.  */
  1611.                       loc->length = ((bitpos % 8) + bitsize + 7) / 8;
  1612.                     }
  1613.                   else
  1614.                     loc->length = TYPE_LENGTH (value_type (v));

  1615.                   loc->watchpoint_type = type;
  1616.                 }
  1617.             }
  1618.         }

  1619.       /* Change the type of breakpoint between hardware assisted or
  1620.          an ordinary watchpoint depending on the hardware support
  1621.          and free hardware slots.  REPARSE is set when the inferior
  1622.          is started.  */
  1623.       if (reparse)
  1624.         {
  1625.           int reg_cnt;
  1626.           enum bp_loc_type loc_type;
  1627.           struct bp_location *bl;

  1628.           reg_cnt = can_use_hardware_watchpoint (val_chain);

  1629.           if (reg_cnt)
  1630.             {
  1631.               int i, target_resources_ok, other_type_used;
  1632.               enum bptype type;

  1633.               /* Use an exact watchpoint when there's only one memory region to be
  1634.                  watched, and only one debug register is needed to watch it.  */
  1635.               b->exact = target_exact_watchpoints && reg_cnt == 1;

  1636.               /* We need to determine how many resources are already
  1637.                  used for all other hardware watchpoints plus this one
  1638.                  to see if we still have enough resources to also fit
  1639.                  this watchpoint in as well.  */

  1640.               /* If this is a software watchpoint, we try to turn it
  1641.                  to a hardware one -- count resources as if B was of
  1642.                  hardware watchpoint type.  */
  1643.               type = b->base.type;
  1644.               if (type == bp_watchpoint)
  1645.                 type = bp_hardware_watchpoint;

  1646.               /* This watchpoint may or may not have been placed on
  1647.                  the list yet at this point (it won't be in the list
  1648.                  if we're trying to create it for the first time,
  1649.                  through watch_command), so always account for it
  1650.                  manually.  */

  1651.               /* Count resources used by all watchpoints except B.  */
  1652.               i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);

  1653.               /* Add in the resources needed for B.  */
  1654.               i += hw_watchpoint_use_count (&b->base);

  1655.               target_resources_ok
  1656.                 = target_can_use_hardware_watchpoint (type, i, other_type_used);
  1657.               if (target_resources_ok <= 0)
  1658.                 {
  1659.                   int sw_mode = b->base.ops->works_in_software_mode (&b->base);

  1660.                   if (target_resources_ok == 0 && !sw_mode)
  1661.                     error (_("Target does not support this type of "
  1662.                              "hardware watchpoint."));
  1663.                   else if (target_resources_ok < 0 && !sw_mode)
  1664.                     error (_("There are not enough available hardware "
  1665.                              "resources for this watchpoint."));

  1666.                   /* Downgrade to software watchpoint.  */
  1667.                   b->base.type = bp_watchpoint;
  1668.                 }
  1669.               else
  1670.                 {
  1671.                   /* If this was a software watchpoint, we've just
  1672.                      found we have enough resources to turn it to a
  1673.                      hardware watchpoint.  Otherwise, this is a
  1674.                      nop.  */
  1675.                   b->base.type = type;
  1676.                 }
  1677.             }
  1678.           else if (!b->base.ops->works_in_software_mode (&b->base))
  1679.             {
  1680.               if (!can_use_hw_watchpoints)
  1681.                 error (_("Can't set read/access watchpoint when "
  1682.                          "hardware watchpoints are disabled."));
  1683.               else
  1684.                 error (_("Expression cannot be implemented with "
  1685.                          "read/access watchpoint."));
  1686.             }
  1687.           else
  1688.             b->base.type = bp_watchpoint;

  1689.           loc_type = (b->base.type == bp_watchpoint? bp_loc_other
  1690.                       : bp_loc_hardware_watchpoint);
  1691.           for (bl = b->base.loc; bl; bl = bl->next)
  1692.             bl->loc_type = loc_type;
  1693.         }

  1694.       for (v = val_chain; v; v = next)
  1695.         {
  1696.           next = value_next (v);
  1697.           if (v != b->val)
  1698.             value_free (v);
  1699.         }

  1700.       /* If a software watchpoint is not watching any memory, then the
  1701.          above left it without any location set up.  But,
  1702.          bpstat_stop_status requires a location to be able to report
  1703.          stops, so make sure there's at least a dummy one.  */
  1704.       if (b->base.type == bp_watchpoint && b->base.loc == NULL)
  1705.         {
  1706.           struct breakpoint *base = &b->base;
  1707.           base->loc = allocate_bp_location (base);
  1708.           base->loc->pspace = frame_pspace;
  1709.           base->loc->address = -1;
  1710.           base->loc->length = -1;
  1711.           base->loc->watchpoint_type = -1;
  1712.         }
  1713.     }
  1714.   else if (!within_current_scope)
  1715.     {
  1716.       printf_filtered (_("\
  1717. Watchpoint %d deleted because the program has left the block\n\
  1718. in which its expression is valid.\n"),
  1719.                        b->base.number);
  1720.       watchpoint_del_at_next_stop (b);
  1721.     }

  1722.   /* Restore the selected frame.  */
  1723.   if (frame_saved)
  1724.     select_frame (frame_find_by_id (saved_frame_id));
  1725. }


  1726. /* Returns 1 iff breakpoint location should be
  1727.    inserted in the inferior.  We don't differentiate the type of BL's owner
  1728.    (breakpoint vs. tracepoint), although insert_location in tracepoint's
  1729.    breakpoint_ops is not defined, because in insert_bp_location,
  1730.    tracepoint's insert_location will not be called.  */
  1731. static int
  1732. should_be_inserted (struct bp_location *bl)
  1733. {
  1734.   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
  1735.     return 0;

  1736.   if (bl->owner->disposition == disp_del_at_next_stop)
  1737.     return 0;

  1738.   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
  1739.     return 0;

  1740.   if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
  1741.     return 0;

  1742.   /* This is set for example, when we're attached to the parent of a
  1743.      vfork, and have detached from the child.  The child is running
  1744.      free, and we expect it to do an exec or exit, at which point the
  1745.      OS makes the parent schedulable again (and the target reports
  1746.      that the vfork is done).  Until the child is done with the shared
  1747.      memory region, do not insert breakpoints in the parent, otherwise
  1748.      the child could still trip on the parent's breakpoints.  Since
  1749.      the parent is blocked anyway, it won't miss any breakpoint.  */
  1750.   if (bl->pspace->breakpoints_not_allowed)
  1751.     return 0;

  1752.   /* Don't insert a breakpoint if we're trying to step past its
  1753.      location.  */
  1754.   if ((bl->loc_type == bp_loc_software_breakpoint
  1755.        || bl->loc_type == bp_loc_hardware_breakpoint)
  1756.       && stepping_past_instruction_at (bl->pspace->aspace,
  1757.                                        bl->address))
  1758.     {
  1759.       if (debug_infrun)
  1760.         {
  1761.           fprintf_unfiltered (gdb_stdlog,
  1762.                               "infrun: skipping breakpoint: "
  1763.                               "stepping past insn at: %s\n",
  1764.                               paddress (bl->gdbarch, bl->address));
  1765.         }
  1766.       return 0;
  1767.     }

  1768.   /* Don't insert watchpoints if we're trying to step past the
  1769.      instruction that triggered one.  */
  1770.   if ((bl->loc_type == bp_loc_hardware_watchpoint)
  1771.       && stepping_past_nonsteppable_watchpoint ())
  1772.     {
  1773.       if (debug_infrun)
  1774.         {
  1775.           fprintf_unfiltered (gdb_stdlog,
  1776.                               "infrun: stepping past non-steppable watchpoint. "
  1777.                               "skipping watchpoint at %s:%d\n",
  1778.                               paddress (bl->gdbarch, bl->address),
  1779.                               bl->length);
  1780.         }
  1781.       return 0;
  1782.     }

  1783.   return 1;
  1784. }

  1785. /* Same as should_be_inserted but does the check assuming
  1786.    that the location is not duplicated.  */

  1787. static int
  1788. unduplicated_should_be_inserted (struct bp_location *bl)
  1789. {
  1790.   int result;
  1791.   const int save_duplicate = bl->duplicate;

  1792.   bl->duplicate = 0;
  1793.   result = should_be_inserted (bl);
  1794.   bl->duplicate = save_duplicate;
  1795.   return result;
  1796. }

  1797. /* Parses a conditional described by an expression COND into an
  1798.    agent expression bytecode suitable for evaluation
  1799.    by the bytecode interpreter.  Return NULL if there was
  1800.    any error during parsing.  */

  1801. static struct agent_expr *
  1802. parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
  1803. {
  1804.   struct agent_expr *aexpr = NULL;
  1805.   volatile struct gdb_exception ex;

  1806.   if (!cond)
  1807.     return NULL;

  1808.   /* We don't want to stop processing, so catch any errors
  1809.      that may show up.  */
  1810.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  1811.     {
  1812.       aexpr = gen_eval_for_expr (scope, cond);
  1813.     }

  1814.   if (ex.reason < 0)
  1815.     {
  1816.       /* If we got here, it means the condition could not be parsed to a valid
  1817.          bytecode expression and thus can't be evaluated on the target's side.
  1818.          It's no use iterating through the conditions.  */
  1819.       return NULL;
  1820.     }

  1821.   /* We have a valid agent expression.  */
  1822.   return aexpr;
  1823. }

  1824. /* Based on location BL, create a list of breakpoint conditions to be
  1825.    passed on to the target.  If we have duplicated locations with different
  1826.    conditions, we will add such conditions to the list.  The idea is that the
  1827.    target will evaluate the list of conditions and will only notify GDB when
  1828.    one of them is true.  */

  1829. static void
  1830. build_target_condition_list (struct bp_location *bl)
  1831. {
  1832.   struct bp_location **locp = NULL, **loc2p;
  1833.   int null_condition_or_parse_error = 0;
  1834.   int modified = bl->needs_update;
  1835.   struct bp_location *loc;

  1836.   /* Release conditions left over from a previous insert.  */
  1837.   VEC_free (agent_expr_p, bl->target_info.conditions);

  1838.   /* This is only meaningful if the target is
  1839.      evaluating conditions and if the user has
  1840.      opted for condition evaluation on the target's
  1841.      side.  */
  1842.   if (gdb_evaluates_breakpoint_condition_p ()
  1843.       || !target_supports_evaluation_of_breakpoint_conditions ())
  1844.     return;

  1845.   /* Do a first pass to check for locations with no assigned
  1846.      conditions or conditions that fail to parse to a valid agent expression
  1847.      bytecode.  If any of these happen, then it's no use to send conditions
  1848.      to the target since this location will always trigger and generate a
  1849.      response back to GDB.  */
  1850.   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
  1851.     {
  1852.       loc = (*loc2p);
  1853.       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
  1854.         {
  1855.           if (modified)
  1856.             {
  1857.               struct agent_expr *aexpr;

  1858.               /* Re-parse the conditions since something changed.  In that
  1859.                  case we already freed the condition bytecodes (see
  1860.                  force_breakpoint_reinsertion).  We just
  1861.                  need to parse the condition to bytecodes again.  */
  1862.               aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
  1863.               loc->cond_bytecode = aexpr;

  1864.               /* Check if we managed to parse the conditional expression
  1865.                  correctly.  If not, we will not send this condition
  1866.                  to the target.  */
  1867.               if (aexpr)
  1868.                 continue;
  1869.             }

  1870.           /* If we have a NULL bytecode expression, it means something
  1871.              went wrong or we have a null condition expression.  */
  1872.           if (!loc->cond_bytecode)
  1873.             {
  1874.               null_condition_or_parse_error = 1;
  1875.               break;
  1876.             }
  1877.         }
  1878.     }

  1879.   /* If any of these happened, it means we will have to evaluate the conditions
  1880.      for the location's address on gdb's side.  It is no use keeping bytecodes
  1881.      for all the other duplicate locations, thus we free all of them here.

  1882.      This is so we have a finer control over which locations' conditions are
  1883.      being evaluated by GDB or the remote stub.  */
  1884.   if (null_condition_or_parse_error)
  1885.     {
  1886.       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
  1887.         {
  1888.           loc = (*loc2p);
  1889.           if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
  1890.             {
  1891.               /* Only go as far as the first NULL bytecode is
  1892.                  located.  */
  1893.               if (!loc->cond_bytecode)
  1894.                 return;

  1895.               free_agent_expr (loc->cond_bytecode);
  1896.               loc->cond_bytecode = NULL;
  1897.             }
  1898.         }
  1899.     }

  1900.   /* No NULL conditions or failed bytecode generation.  Build a condition list
  1901.      for this location's address.  */
  1902.   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
  1903.     {
  1904.       loc = (*loc2p);
  1905.       if (loc->cond
  1906.           && is_breakpoint (loc->owner)
  1907.           && loc->pspace->num == bl->pspace->num
  1908.           && loc->owner->enable_state == bp_enabled
  1909.           && loc->enabled)
  1910.         /* Add the condition to the vector.  This will be used later to send the
  1911.            conditions to the target.  */
  1912.         VEC_safe_push (agent_expr_p, bl->target_info.conditions,
  1913.                        loc->cond_bytecode);
  1914.     }

  1915.   return;
  1916. }

  1917. /* Parses a command described by string CMD into an agent expression
  1918.    bytecode suitable for evaluation by the bytecode interpreter.
  1919.    Return NULL if there was any error during parsing.  */

  1920. static struct agent_expr *
  1921. parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
  1922. {
  1923.   struct cleanup *old_cleanups = 0;
  1924.   struct expression *expr, **argvec;
  1925.   struct agent_expr *aexpr = NULL;
  1926.   volatile struct gdb_exception ex;
  1927.   const char *cmdrest;
  1928.   const char *format_start, *format_end;
  1929.   struct format_piece *fpieces;
  1930.   int nargs;
  1931.   struct gdbarch *gdbarch = get_current_arch ();

  1932.   if (!cmd)
  1933.     return NULL;

  1934.   cmdrest = cmd;

  1935.   if (*cmdrest == ',')
  1936.     ++cmdrest;
  1937.   cmdrest = skip_spaces_const (cmdrest);

  1938.   if (*cmdrest++ != '"')
  1939.     error (_("No format string following the location"));

  1940.   format_start = cmdrest;

  1941.   fpieces = parse_format_string (&cmdrest);

  1942.   old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);

  1943.   format_end = cmdrest;

  1944.   if (*cmdrest++ != '"')
  1945.     error (_("Bad format string, non-terminated '\"'."));

  1946.   cmdrest = skip_spaces_const (cmdrest);

  1947.   if (!(*cmdrest == ',' || *cmdrest == '\0'))
  1948.     error (_("Invalid argument syntax"));

  1949.   if (*cmdrest == ',')
  1950.     cmdrest++;
  1951.   cmdrest = skip_spaces_const (cmdrest);

  1952.   /* For each argument, make an expression.  */

  1953.   argvec = (struct expression **) alloca (strlen (cmd)
  1954.                                          * sizeof (struct expression *));

  1955.   nargs = 0;
  1956.   while (*cmdrest != '\0')
  1957.     {
  1958.       const char *cmd1;

  1959.       cmd1 = cmdrest;
  1960.       expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
  1961.       argvec[nargs++] = expr;
  1962.       cmdrest = cmd1;
  1963.       if (*cmdrest == ',')
  1964.         ++cmdrest;
  1965.     }

  1966.   /* We don't want to stop processing, so catch any errors
  1967.      that may show up.  */
  1968.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  1969.     {
  1970.       aexpr = gen_printf (scope, gdbarch, 0, 0,
  1971.                           format_start, format_end - format_start,
  1972.                           fpieces, nargs, argvec);
  1973.     }

  1974.   do_cleanups (old_cleanups);

  1975.   if (ex.reason < 0)
  1976.     {
  1977.       /* If we got here, it means the command could not be parsed to a valid
  1978.          bytecode expression and thus can't be evaluated on the target's side.
  1979.          It's no use iterating through the other commands.  */
  1980.       return NULL;
  1981.     }

  1982.   /* We have a valid agent expression, return it.  */
  1983.   return aexpr;
  1984. }

  1985. /* Based on location BL, create a list of breakpoint commands to be
  1986.    passed on to the target.  If we have duplicated locations with
  1987.    different commands, we will add any such to the list.  */

  1988. static void
  1989. build_target_command_list (struct bp_location *bl)
  1990. {
  1991.   struct bp_location **locp = NULL, **loc2p;
  1992.   int null_command_or_parse_error = 0;
  1993.   int modified = bl->needs_update;
  1994.   struct bp_location *loc;

  1995.   /* Release commands left over from a previous insert.  */
  1996.   VEC_free (agent_expr_p, bl->target_info.tcommands);

  1997.   if (!target_can_run_breakpoint_commands ())
  1998.     return;

  1999.   /* For now, limit to agent-style dprintf breakpoints.  */
  2000.   if (dprintf_style != dprintf_style_agent)
  2001.     return;

  2002.   /* For now, if we have any duplicate location that isn't a dprintf,
  2003.      don't install the target-side commands, as that would make the
  2004.      breakpoint not be reported to the core, and we'd lose
  2005.      control.  */
  2006.   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
  2007.     {
  2008.       loc = (*loc2p);
  2009.       if (is_breakpoint (loc->owner)
  2010.           && loc->pspace->num == bl->pspace->num
  2011.           && loc->owner->type != bp_dprintf)
  2012.         return;
  2013.     }

  2014.   /* Do a first pass to check for locations with no assigned
  2015.      conditions or conditions that fail to parse to a valid agent expression
  2016.      bytecode.  If any of these happen, then it's no use to send conditions
  2017.      to the target since this location will always trigger and generate a
  2018.      response back to GDB.  */
  2019.   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
  2020.     {
  2021.       loc = (*loc2p);
  2022.       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
  2023.         {
  2024.           if (modified)
  2025.             {
  2026.               struct agent_expr *aexpr;

  2027.               /* Re-parse the commands since something changed.  In that
  2028.                  case we already freed the command bytecodes (see
  2029.                  force_breakpoint_reinsertion).  We just
  2030.                  need to parse the command to bytecodes again.  */
  2031.               aexpr = parse_cmd_to_aexpr (bl->address,
  2032.                                           loc->owner->extra_string);
  2033.               loc->cmd_bytecode = aexpr;

  2034.               if (!aexpr)
  2035.                 continue;
  2036.             }

  2037.           /* If we have a NULL bytecode expression, it means something
  2038.              went wrong or we have a null command expression.  */
  2039.           if (!loc->cmd_bytecode)
  2040.             {
  2041.               null_command_or_parse_error = 1;
  2042.               break;
  2043.             }
  2044.         }
  2045.     }

  2046.   /* If anything failed, then we're not doing target-side commands,
  2047.      and so clean up.  */
  2048.   if (null_command_or_parse_error)
  2049.     {
  2050.       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
  2051.         {
  2052.           loc = (*loc2p);
  2053.           if (is_breakpoint (loc->owner)
  2054.               && loc->pspace->num == bl->pspace->num)
  2055.             {
  2056.               /* Only go as far as the first NULL bytecode is
  2057.                  located.  */
  2058.               if (loc->cmd_bytecode == NULL)
  2059.                 return;

  2060.               free_agent_expr (loc->cmd_bytecode);
  2061.               loc->cmd_bytecode = NULL;
  2062.             }
  2063.         }
  2064.     }

  2065.   /* No NULL commands or failed bytecode generation.  Build a command list
  2066.      for this location's address.  */
  2067.   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
  2068.     {
  2069.       loc = (*loc2p);
  2070.       if (loc->owner->extra_string
  2071.           && is_breakpoint (loc->owner)
  2072.           && loc->pspace->num == bl->pspace->num
  2073.           && loc->owner->enable_state == bp_enabled
  2074.           && loc->enabled)
  2075.         /* Add the command to the vector.  This will be used later
  2076.            to send the commands to the target.  */
  2077.         VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
  2078.                        loc->cmd_bytecode);
  2079.     }

  2080.   bl->target_info.persist = 0;
  2081.   /* Maybe flag this location as persistent.  */
  2082.   if (bl->owner->type == bp_dprintf && disconnected_dprintf)
  2083.     bl->target_info.persist = 1;
  2084. }

  2085. /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
  2086.    location.  Any error messages are printed to TMP_ERROR_STREAM; and
  2087.    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
  2088.    Returns 0 for success, 1 if the bp_location type is not supported or
  2089.    -1 for failure.

  2090.    NOTE drow/2003-09-09: This routine could be broken down to an
  2091.    object-style method for each breakpoint or catchpoint type.  */
  2092. static int
  2093. insert_bp_location (struct bp_location *bl,
  2094.                     struct ui_file *tmp_error_stream,
  2095.                     int *disabled_breaks,
  2096.                     int *hw_breakpoint_error,
  2097.                     int *hw_bp_error_explained_already)
  2098. {
  2099.   enum errors bp_err = GDB_NO_ERROR;
  2100.   const char *bp_err_message = NULL;
  2101.   volatile struct gdb_exception e;

  2102.   if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
  2103.     return 0;

  2104.   /* Note we don't initialize bl->target_info, as that wipes out
  2105.      the breakpoint location's shadow_contents if the breakpoint
  2106.      is still inserted at that location.  This in turn breaks
  2107.      target_read_memory which depends on these buffers when
  2108.      a memory read is requested at the breakpoint location:
  2109.      Once the target_info has been wiped, we fail to see that
  2110.      we have a breakpoint inserted at that address and thus
  2111.      read the breakpoint instead of returning the data saved in
  2112.      the breakpoint location's shadow contents.  */
  2113.   bl->target_info.reqstd_address = bl->address;
  2114.   bl->target_info.placed_address_space = bl->pspace->aspace;
  2115.   bl->target_info.length = bl->length;

  2116.   /* When working with target-side conditions, we must pass all the conditions
  2117.      for the same breakpoint address down to the target since GDB will not
  2118.      insert those locations.  With a list of breakpoint conditions, the target
  2119.      can decide when to stop and notify GDB.  */

  2120.   if (is_breakpoint (bl->owner))
  2121.     {
  2122.       build_target_condition_list (bl);
  2123.       build_target_command_list (bl);
  2124.       /* Reset the modification marker.  */
  2125.       bl->needs_update = 0;
  2126.     }

  2127.   if (bl->loc_type == bp_loc_software_breakpoint
  2128.       || bl->loc_type == bp_loc_hardware_breakpoint)
  2129.     {
  2130.       if (bl->owner->type != bp_hardware_breakpoint)
  2131.         {
  2132.           /* If the explicitly specified breakpoint type
  2133.              is not hardware breakpoint, check the memory map to see
  2134.              if the breakpoint address is in read only memory or not.

  2135.              Two important cases are:
  2136.              - location type is not hardware breakpoint, memory
  2137.              is readonly.  We change the type of the location to
  2138.              hardware breakpoint.
  2139.              - location type is hardware breakpoint, memory is
  2140.              read-write.  This means we've previously made the
  2141.              location hardware one, but then the memory map changed,
  2142.              so we undo.

  2143.              When breakpoints are removed, remove_breakpoints will use
  2144.              location types we've just set here, the only possible
  2145.              problem is that memory map has changed during running
  2146.              program, but it's not going to work anyway with current
  2147.              gdb.  */
  2148.           struct mem_region *mr
  2149.             = lookup_mem_region (bl->target_info.reqstd_address);

  2150.           if (mr)
  2151.             {
  2152.               if (automatic_hardware_breakpoints)
  2153.                 {
  2154.                   enum bp_loc_type new_type;

  2155.                   if (mr->attrib.mode != MEM_RW)
  2156.                     new_type = bp_loc_hardware_breakpoint;
  2157.                   else
  2158.                     new_type = bp_loc_software_breakpoint;

  2159.                   if (new_type != bl->loc_type)
  2160.                     {
  2161.                       static int said = 0;

  2162.                       bl->loc_type = new_type;
  2163.                       if (!said)
  2164.                         {
  2165.                           fprintf_filtered (gdb_stdout,
  2166.                                             _("Note: automatically using "
  2167.                                               "hardware breakpoints for "
  2168.                                               "read-only addresses.\n"));
  2169.                           said = 1;
  2170.                         }
  2171.                     }
  2172.                 }
  2173.               else if (bl->loc_type == bp_loc_software_breakpoint
  2174.                        && mr->attrib.mode != MEM_RW)
  2175.                 {
  2176.                   fprintf_unfiltered (tmp_error_stream,
  2177.                                       _("Cannot insert breakpoint %d.\n"
  2178.                                         "Cannot set software breakpoint "
  2179.                                         "at read-only address %s\n"),
  2180.                                       bl->owner->number,
  2181.                                       paddress (bl->gdbarch, bl->address));
  2182.                   return 1;
  2183.                 }
  2184.             }
  2185.         }

  2186.       /* First check to see if we have to handle an overlay.  */
  2187.       if (overlay_debugging == ovly_off
  2188.           || bl->section == NULL
  2189.           || !(section_is_overlay (bl->section)))
  2190.         {
  2191.           /* No overlay handling: just set the breakpoint.  */
  2192.           TRY_CATCH (e, RETURN_MASK_ALL)
  2193.             {
  2194.               int val;

  2195.               val = bl->owner->ops->insert_location (bl);
  2196.               if (val)
  2197.                 bp_err = GENERIC_ERROR;
  2198.             }
  2199.           if (e.reason < 0)
  2200.             {
  2201.               bp_err = e.error;
  2202.               bp_err_message = e.message;
  2203.             }
  2204.         }
  2205.       else
  2206.         {
  2207.           /* This breakpoint is in an overlay section.
  2208.              Shall we set a breakpoint at the LMA?  */
  2209.           if (!overlay_events_enabled)
  2210.             {
  2211.               /* Yes -- overlay event support is not active,
  2212.                  so we must try to set a breakpoint at the LMA.
  2213.                  This will not work for a hardware breakpoint.  */
  2214.               if (bl->loc_type == bp_loc_hardware_breakpoint)
  2215.                 warning (_("hardware breakpoint %d not supported in overlay!"),
  2216.                          bl->owner->number);
  2217.               else
  2218.                 {
  2219.                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
  2220.                                                              bl->section);
  2221.                   /* Set a software (trap) breakpoint at the LMA.  */
  2222.                   bl->overlay_target_info = bl->target_info;
  2223.                   bl->overlay_target_info.reqstd_address = addr;

  2224.                   /* No overlay handling: just set the breakpoint.  */
  2225.                   TRY_CATCH (e, RETURN_MASK_ALL)
  2226.                     {
  2227.                       int val;

  2228.                       val = target_insert_breakpoint (bl->gdbarch,
  2229.                                                       &bl->overlay_target_info);
  2230.                       if (val)
  2231.                         bp_err = GENERIC_ERROR;
  2232.                     }
  2233.                   if (e.reason < 0)
  2234.                     {
  2235.                       bp_err = e.error;
  2236.                       bp_err_message = e.message;
  2237.                     }

  2238.                   if (bp_err != GDB_NO_ERROR)
  2239.                     fprintf_unfiltered (tmp_error_stream,
  2240.                                         "Overlay breakpoint %d "
  2241.                                         "failed: in ROM?\n",
  2242.                                         bl->owner->number);
  2243.                 }
  2244.             }
  2245.           /* Shall we set a breakpoint at the VMA? */
  2246.           if (section_is_mapped (bl->section))
  2247.             {
  2248.               /* Yes.  This overlay section is mapped into memory.  */
  2249.               TRY_CATCH (e, RETURN_MASK_ALL)
  2250.                 {
  2251.                   int val;

  2252.                   val = bl->owner->ops->insert_location (bl);
  2253.                   if (val)
  2254.                     bp_err = GENERIC_ERROR;
  2255.                 }
  2256.               if (e.reason < 0)
  2257.                 {
  2258.                   bp_err = e.error;
  2259.                   bp_err_message = e.message;
  2260.                 }
  2261.             }
  2262.           else
  2263.             {
  2264.               /* No.  This breakpoint will not be inserted.
  2265.                  No error, but do not mark the bp as 'inserted'.  */
  2266.               return 0;
  2267.             }
  2268.         }

  2269.       if (bp_err != GDB_NO_ERROR)
  2270.         {
  2271.           /* Can't set the breakpoint.  */

  2272.           /* In some cases, we might not be able to insert a
  2273.              breakpoint in a shared library that has already been
  2274.              removed, but we have not yet processed the shlib unload
  2275.              event.  Unfortunately, some targets that implement
  2276.              breakpoint insertion themselves can't tell why the
  2277.              breakpoint insertion failed (e.g., the remote target
  2278.              doesn't define error codes), so we must treat generic
  2279.              errors as memory errors.  */
  2280.           if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
  2281.               && bl->loc_type == bp_loc_software_breakpoint
  2282.               && (solib_name_from_address (bl->pspace, bl->address)
  2283.                   || shared_objfile_contains_address_p (bl->pspace,
  2284.                                                         bl->address)))
  2285.             {
  2286.               /* See also: disable_breakpoints_in_shlibs.  */
  2287.               bl->shlib_disabled = 1;
  2288.               observer_notify_breakpoint_modified (bl->owner);
  2289.               if (!*disabled_breaks)
  2290.                 {
  2291.                   fprintf_unfiltered (tmp_error_stream,
  2292.                                       "Cannot insert breakpoint %d.\n",
  2293.                                       bl->owner->number);
  2294.                   fprintf_unfiltered (tmp_error_stream,
  2295.                                       "Temporarily disabling shared "
  2296.                                       "library breakpoints:\n");
  2297.                 }
  2298.               *disabled_breaks = 1;
  2299.               fprintf_unfiltered (tmp_error_stream,
  2300.                                   "breakpoint #%d\n", bl->owner->number);
  2301.               return 0;
  2302.             }
  2303.           else
  2304.             {
  2305.               if (bl->loc_type == bp_loc_hardware_breakpoint)
  2306.                 {
  2307.                   *hw_breakpoint_error = 1;
  2308.                   *hw_bp_error_explained_already = bp_err_message != NULL;
  2309.                   fprintf_unfiltered (tmp_error_stream,
  2310.                                       "Cannot insert hardware breakpoint %d%s",
  2311.                                       bl->owner->number, bp_err_message ? ":" : ".\n");
  2312.                   if (bp_err_message != NULL)
  2313.                     fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
  2314.                 }
  2315.               else
  2316.                 {
  2317.                   if (bp_err_message == NULL)
  2318.                     {
  2319.                       char *message
  2320.                         = memory_error_message (TARGET_XFER_E_IO,
  2321.                                                 bl->gdbarch, bl->address);
  2322.                       struct cleanup *old_chain = make_cleanup (xfree, message);

  2323.                       fprintf_unfiltered (tmp_error_stream,
  2324.                                           "Cannot insert breakpoint %d.\n"
  2325.                                           "%s\n",
  2326.                                           bl->owner->number, message);
  2327.                       do_cleanups (old_chain);
  2328.                     }
  2329.                   else
  2330.                     {
  2331.                       fprintf_unfiltered (tmp_error_stream,
  2332.                                           "Cannot insert breakpoint %d: %s\n",
  2333.                                           bl->owner->number,
  2334.                                           bp_err_message);
  2335.                     }
  2336.                 }
  2337.               return 1;

  2338.             }
  2339.         }
  2340.       else
  2341.         bl->inserted = 1;

  2342.       return 0;
  2343.     }

  2344.   else if (bl->loc_type == bp_loc_hardware_watchpoint
  2345.            /* NOTE drow/2003-09-08: This state only exists for removing
  2346.               watchpoints.  It's not clear that it's necessary...  */
  2347.            && bl->owner->disposition != disp_del_at_next_stop)
  2348.     {
  2349.       int val;

  2350.       gdb_assert (bl->owner->ops != NULL
  2351.                   && bl->owner->ops->insert_location != NULL);

  2352.       val = bl->owner->ops->insert_location (bl);

  2353.       /* If trying to set a read-watchpoint, and it turns out it's not
  2354.          supported, try emulating one with an access watchpoint.  */
  2355.       if (val == 1 && bl->watchpoint_type == hw_read)
  2356.         {
  2357.           struct bp_location *loc, **loc_temp;

  2358.           /* But don't try to insert it, if there's already another
  2359.              hw_access location that would be considered a duplicate
  2360.              of this one.  */
  2361.           ALL_BP_LOCATIONS (loc, loc_temp)
  2362.             if (loc != bl
  2363.                 && loc->watchpoint_type == hw_access
  2364.                 && watchpoint_locations_match (bl, loc))
  2365.               {
  2366.                 bl->duplicate = 1;
  2367.                 bl->inserted = 1;
  2368.                 bl->target_info = loc->target_info;
  2369.                 bl->watchpoint_type = hw_access;
  2370.                 val = 0;
  2371.                 break;
  2372.               }

  2373.           if (val == 1)
  2374.             {
  2375.               bl->watchpoint_type = hw_access;
  2376.               val = bl->owner->ops->insert_location (bl);

  2377.               if (val)
  2378.                 /* Back to the original value.  */
  2379.                 bl->watchpoint_type = hw_read;
  2380.             }
  2381.         }

  2382.       bl->inserted = (val == 0);
  2383.     }

  2384.   else if (bl->owner->type == bp_catchpoint)
  2385.     {
  2386.       int val;

  2387.       gdb_assert (bl->owner->ops != NULL
  2388.                   && bl->owner->ops->insert_location != NULL);

  2389.       val = bl->owner->ops->insert_location (bl);
  2390.       if (val)
  2391.         {
  2392.           bl->owner->enable_state = bp_disabled;

  2393.           if (val == 1)
  2394.             warning (_("\
  2395. Error inserting catchpoint %d: Your system does not support this type\n\
  2396. of catchpoint."), bl->owner->number);
  2397.           else
  2398.             warning (_("Error inserting catchpoint %d."), bl->owner->number);
  2399.         }

  2400.       bl->inserted = (val == 0);

  2401.       /* We've already printed an error message if there was a problem
  2402.          inserting this catchpoint, and we've disabled the catchpoint,
  2403.          so just return success.  */
  2404.       return 0;
  2405.     }

  2406.   return 0;
  2407. }

  2408. /* This function is called when program space PSPACE is about to be
  2409.    deleted.  It takes care of updating breakpoints to not reference
  2410.    PSPACE anymore.  */

  2411. void
  2412. breakpoint_program_space_exit (struct program_space *pspace)
  2413. {
  2414.   struct breakpoint *b, *b_temp;
  2415.   struct bp_location *loc, **loc_temp;

  2416.   /* Remove any breakpoint that was set through this program space.  */
  2417.   ALL_BREAKPOINTS_SAFE (b, b_temp)
  2418.     {
  2419.       if (b->pspace == pspace)
  2420.         delete_breakpoint (b);
  2421.     }

  2422.   /* Breakpoints set through other program spaces could have locations
  2423.      bound to PSPACE as well.  Remove those.  */
  2424.   ALL_BP_LOCATIONS (loc, loc_temp)
  2425.     {
  2426.       struct bp_location *tmp;

  2427.       if (loc->pspace == pspace)
  2428.         {
  2429.           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
  2430.           if (loc->owner->loc == loc)
  2431.             loc->owner->loc = loc->next;
  2432.           else
  2433.             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
  2434.               if (tmp->next == loc)
  2435.                 {
  2436.                   tmp->next = loc->next;
  2437.                   break;
  2438.                 }
  2439.         }
  2440.     }

  2441.   /* Now update the global location list to permanently delete the
  2442.      removed locations above.  */
  2443.   update_global_location_list (UGLL_DONT_INSERT);
  2444. }

  2445. /* Make sure all breakpoints are inserted in inferior.
  2446.    Throws exception on any error.
  2447.    A breakpoint that is already inserted won't be inserted
  2448.    again, so calling this function twice is safe.  */
  2449. void
  2450. insert_breakpoints (void)
  2451. {
  2452.   struct breakpoint *bpt;

  2453.   ALL_BREAKPOINTS (bpt)
  2454.     if (is_hardware_watchpoint (bpt))
  2455.       {
  2456.         struct watchpoint *w = (struct watchpoint *) bpt;

  2457.         update_watchpoint (w, 0 /* don't reparse.  */);
  2458.       }

  2459.   /* Updating watchpoints creates new locations, so update the global
  2460.      location list.  Explicitly tell ugll to insert locations and
  2461.      ignore breakpoints_always_inserted_mode.  */
  2462.   update_global_location_list (UGLL_INSERT);
  2463. }

  2464. /* Invoke CALLBACK for each of bp_location.  */

  2465. void
  2466. iterate_over_bp_locations (walk_bp_location_callback callback)
  2467. {
  2468.   struct bp_location *loc, **loc_tmp;

  2469.   ALL_BP_LOCATIONS (loc, loc_tmp)
  2470.     {
  2471.       callback (loc, NULL);
  2472.     }
  2473. }

  2474. /* This is used when we need to synch breakpoint conditions between GDB and the
  2475.    target.  It is the case with deleting and disabling of breakpoints when using
  2476.    always-inserted mode.  */

  2477. static void
  2478. update_inserted_breakpoint_locations (void)
  2479. {
  2480.   struct bp_location *bl, **blp_tmp;
  2481.   int error_flag = 0;
  2482.   int val = 0;
  2483.   int disabled_breaks = 0;
  2484.   int hw_breakpoint_error = 0;
  2485.   int hw_bp_details_reported = 0;

  2486.   struct ui_file *tmp_error_stream = mem_fileopen ();
  2487.   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);

  2488.   /* Explicitly mark the warning -- this will only be printed if
  2489.      there was an error.  */
  2490.   fprintf_unfiltered (tmp_error_stream, "Warning:\n");

  2491.   save_current_space_and_thread ();

  2492.   ALL_BP_LOCATIONS (bl, blp_tmp)
  2493.     {
  2494.       /* We only want to update software breakpoints and hardware
  2495.          breakpoints.  */
  2496.       if (!is_breakpoint (bl->owner))
  2497.         continue;

  2498.       /* We only want to update locations that are already inserted
  2499.          and need updating.  This is to avoid unwanted insertion during
  2500.          deletion of breakpoints.  */
  2501.       if (!bl->inserted || (bl->inserted && !bl->needs_update))
  2502.         continue;

  2503.       switch_to_program_space_and_thread (bl->pspace);

  2504.       /* For targets that support global breakpoints, there's no need
  2505.          to select an inferior to insert breakpoint to.  In fact, even
  2506.          if we aren't attached to any process yet, we should still
  2507.          insert breakpoints.  */
  2508.       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
  2509.           && ptid_equal (inferior_ptid, null_ptid))
  2510.         continue;

  2511.       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
  2512.                                     &hw_breakpoint_error, &hw_bp_details_reported);
  2513.       if (val)
  2514.         error_flag = val;
  2515.     }

  2516.   if (error_flag)
  2517.     {
  2518.       target_terminal_ours_for_output ();
  2519.       error_stream (tmp_error_stream);
  2520.     }

  2521.   do_cleanups (cleanups);
  2522. }

  2523. /* Used when starting or continuing the program.  */

  2524. static void
  2525. insert_breakpoint_locations (void)
  2526. {
  2527.   struct breakpoint *bpt;
  2528.   struct bp_location *bl, **blp_tmp;
  2529.   int error_flag = 0;
  2530.   int val = 0;
  2531.   int disabled_breaks = 0;
  2532.   int hw_breakpoint_error = 0;
  2533.   int hw_bp_error_explained_already = 0;

  2534.   struct ui_file *tmp_error_stream = mem_fileopen ();
  2535.   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);

  2536.   /* Explicitly mark the warning -- this will only be printed if
  2537.      there was an error.  */
  2538.   fprintf_unfiltered (tmp_error_stream, "Warning:\n");

  2539.   save_current_space_and_thread ();

  2540.   ALL_BP_LOCATIONS (bl, blp_tmp)
  2541.     {
  2542.       if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
  2543.         continue;

  2544.       /* There is no point inserting thread-specific breakpoints if
  2545.          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
  2546.          has BL->OWNER always non-NULL.  */
  2547.       if (bl->owner->thread != -1
  2548.           && !valid_thread_id (bl->owner->thread))
  2549.         continue;

  2550.       switch_to_program_space_and_thread (bl->pspace);

  2551.       /* For targets that support global breakpoints, there's no need
  2552.          to select an inferior to insert breakpoint to.  In fact, even
  2553.          if we aren't attached to any process yet, we should still
  2554.          insert breakpoints.  */
  2555.       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
  2556.           && ptid_equal (inferior_ptid, null_ptid))
  2557.         continue;

  2558.       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
  2559.                                     &hw_breakpoint_error, &hw_bp_error_explained_already);
  2560.       if (val)
  2561.         error_flag = val;
  2562.     }

  2563.   /* If we failed to insert all locations of a watchpoint, remove
  2564.      them, as half-inserted watchpoint is of limited use.  */
  2565.   ALL_BREAKPOINTS (bpt)
  2566.     {
  2567.       int some_failed = 0;
  2568.       struct bp_location *loc;

  2569.       if (!is_hardware_watchpoint (bpt))
  2570.         continue;

  2571.       if (!breakpoint_enabled (bpt))
  2572.         continue;

  2573.       if (bpt->disposition == disp_del_at_next_stop)
  2574.         continue;

  2575.       for (loc = bpt->loc; loc; loc = loc->next)
  2576.         if (!loc->inserted && should_be_inserted (loc))
  2577.           {
  2578.             some_failed = 1;
  2579.             break;
  2580.           }
  2581.       if (some_failed)
  2582.         {
  2583.           for (loc = bpt->loc; loc; loc = loc->next)
  2584.             if (loc->inserted)
  2585.               remove_breakpoint (loc, mark_uninserted);

  2586.           hw_breakpoint_error = 1;
  2587.           fprintf_unfiltered (tmp_error_stream,
  2588.                               "Could not insert hardware watchpoint %d.\n",
  2589.                               bpt->number);
  2590.           error_flag = -1;
  2591.         }
  2592.     }

  2593.   if (error_flag)
  2594.     {
  2595.       /* If a hardware breakpoint or watchpoint was inserted, add a
  2596.          message about possibly exhausted resources.  */
  2597.       if (hw_breakpoint_error && !hw_bp_error_explained_already)
  2598.         {
  2599.           fprintf_unfiltered (tmp_error_stream,
  2600.                               "Could not insert hardware breakpoints:\n\
  2601. You may have requested too many hardware breakpoints/watchpoints.\n");
  2602.         }
  2603.       target_terminal_ours_for_output ();
  2604.       error_stream (tmp_error_stream);
  2605.     }

  2606.   do_cleanups (cleanups);
  2607. }

  2608. /* Used when the program stops.
  2609.    Returns zero if successful, or non-zero if there was a problem
  2610.    removing a breakpoint location.  */

  2611. int
  2612. remove_breakpoints (void)
  2613. {
  2614.   struct bp_location *bl, **blp_tmp;
  2615.   int val = 0;

  2616.   ALL_BP_LOCATIONS (bl, blp_tmp)
  2617.   {
  2618.     if (bl->inserted && !is_tracepoint (bl->owner))
  2619.       val |= remove_breakpoint (bl, mark_uninserted);
  2620.   }
  2621.   return val;
  2622. }

  2623. /* When a thread exits, remove breakpoints that are related to
  2624.    that thread.  */

  2625. static void
  2626. remove_threaded_breakpoints (struct thread_info *tp, int silent)
  2627. {
  2628.   struct breakpoint *b, *b_tmp;

  2629.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  2630.     {
  2631.       if (b->thread == tp->num && user_breakpoint_p (b))
  2632.         {
  2633.           b->disposition = disp_del_at_next_stop;

  2634.           printf_filtered (_("\
  2635. Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
  2636.                           b->number, tp->num);

  2637.           /* Hide it from the user.  */
  2638.           b->number = 0;
  2639.        }
  2640.     }
  2641. }

  2642. /* Remove breakpoints of process PID.  */

  2643. int
  2644. remove_breakpoints_pid (int pid)
  2645. {
  2646.   struct bp_location *bl, **blp_tmp;
  2647.   int val;
  2648.   struct inferior *inf = find_inferior_pid (pid);

  2649.   ALL_BP_LOCATIONS (bl, blp_tmp)
  2650.   {
  2651.     if (bl->pspace != inf->pspace)
  2652.       continue;

  2653.     if (bl->inserted && !bl->target_info.persist)
  2654.       {
  2655.         val = remove_breakpoint (bl, mark_uninserted);
  2656.         if (val != 0)
  2657.           return val;
  2658.       }
  2659.   }
  2660.   return 0;
  2661. }

  2662. int
  2663. reattach_breakpoints (int pid)
  2664. {
  2665.   struct cleanup *old_chain;
  2666.   struct bp_location *bl, **blp_tmp;
  2667.   int val;
  2668.   struct ui_file *tmp_error_stream;
  2669.   int dummy1 = 0, dummy2 = 0, dummy3 = 0;
  2670.   struct inferior *inf;
  2671.   struct thread_info *tp;

  2672.   tp = any_live_thread_of_process (pid);
  2673.   if (tp == NULL)
  2674.     return 1;

  2675.   inf = find_inferior_pid (pid);
  2676.   old_chain = save_inferior_ptid ();

  2677.   inferior_ptid = tp->ptid;

  2678.   tmp_error_stream = mem_fileopen ();
  2679.   make_cleanup_ui_file_delete (tmp_error_stream);

  2680.   ALL_BP_LOCATIONS (bl, blp_tmp)
  2681.   {
  2682.     if (bl->pspace != inf->pspace)
  2683.       continue;

  2684.     if (bl->inserted)
  2685.       {
  2686.         bl->inserted = 0;
  2687.         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
  2688.         if (val != 0)
  2689.           {
  2690.             do_cleanups (old_chain);
  2691.             return val;
  2692.           }
  2693.       }
  2694.   }
  2695.   do_cleanups (old_chain);
  2696.   return 0;
  2697. }

  2698. static int internal_breakpoint_number = -1;

  2699. /* Set the breakpoint number of B, depending on the value of INTERNAL.
  2700.    If INTERNAL is non-zero, the breakpoint number will be populated
  2701.    from internal_breakpoint_number and that variable decremented.
  2702.    Otherwise the breakpoint number will be populated from
  2703.    breakpoint_count and that value incremented.  Internal breakpoints
  2704.    do not set the internal var bpnum.  */
  2705. static void
  2706. set_breakpoint_number (int internal, struct breakpoint *b)
  2707. {
  2708.   if (internal)
  2709.     b->number = internal_breakpoint_number--;
  2710.   else
  2711.     {
  2712.       set_breakpoint_count (breakpoint_count + 1);
  2713.       b->number = breakpoint_count;
  2714.     }
  2715. }

  2716. static struct breakpoint *
  2717. create_internal_breakpoint (struct gdbarch *gdbarch,
  2718.                             CORE_ADDR address, enum bptype type,
  2719.                             const struct breakpoint_ops *ops)
  2720. {
  2721.   struct symtab_and_line sal;
  2722.   struct breakpoint *b;

  2723.   init_sal (&sal);                /* Initialize to zeroes.  */

  2724.   sal.pc = address;
  2725.   sal.section = find_pc_overlay (sal.pc);
  2726.   sal.pspace = current_program_space;

  2727.   b = set_raw_breakpoint (gdbarch, sal, type, ops);
  2728.   b->number = internal_breakpoint_number--;
  2729.   b->disposition = disp_donttouch;

  2730.   return b;
  2731. }

  2732. static const char *const longjmp_names[] =
  2733.   {
  2734.     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
  2735.   };
  2736. #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)

  2737. /* Per-objfile data private to breakpoint.c.  */
  2738. struct breakpoint_objfile_data
  2739. {
  2740.   /* Minimal symbol for "_ovly_debug_event" (if any).  */
  2741.   struct bound_minimal_symbol overlay_msym;

  2742.   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
  2743.   struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];

  2744.   /* True if we have looked for longjmp probes.  */
  2745.   int longjmp_searched;

  2746.   /* SystemTap probe points for longjmp (if any).  */
  2747.   VEC (probe_p) *longjmp_probes;

  2748.   /* Minimal symbol for "std::terminate()" (if any).  */
  2749.   struct bound_minimal_symbol terminate_msym;

  2750.   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
  2751.   struct bound_minimal_symbol exception_msym;

  2752.   /* True if we have looked for exception probes.  */
  2753.   int exception_searched;

  2754.   /* SystemTap probe points for unwinding (if any).  */
  2755.   VEC (probe_p) *exception_probes;
  2756. };

  2757. static const struct objfile_data *breakpoint_objfile_key;

  2758. /* Minimal symbol not found sentinel.  */
  2759. static struct minimal_symbol msym_not_found;

  2760. /* Returns TRUE if MSYM point to the "not found" sentinel.  */

  2761. static int
  2762. msym_not_found_p (const struct minimal_symbol *msym)
  2763. {
  2764.   return msym == &msym_not_found;
  2765. }

  2766. /* Return per-objfile data needed by breakpoint.c.
  2767.    Allocate the data if necessary.  */

  2768. static struct breakpoint_objfile_data *
  2769. get_breakpoint_objfile_data (struct objfile *objfile)
  2770. {
  2771.   struct breakpoint_objfile_data *bp_objfile_data;

  2772.   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
  2773.   if (bp_objfile_data == NULL)
  2774.     {
  2775.       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
  2776.                                        sizeof (*bp_objfile_data));

  2777.       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
  2778.       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
  2779.     }
  2780.   return bp_objfile_data;
  2781. }

  2782. static void
  2783. free_breakpoint_probes (struct objfile *obj, void *data)
  2784. {
  2785.   struct breakpoint_objfile_data *bp_objfile_data = data;

  2786.   VEC_free (probe_p, bp_objfile_data->longjmp_probes);
  2787.   VEC_free (probe_p, bp_objfile_data->exception_probes);
  2788. }

  2789. static void
  2790. create_overlay_event_breakpoint (void)
  2791. {
  2792.   struct objfile *objfile;
  2793.   const char *const func_name = "_ovly_debug_event";

  2794.   ALL_OBJFILES (objfile)
  2795.     {
  2796.       struct breakpoint *b;
  2797.       struct breakpoint_objfile_data *bp_objfile_data;
  2798.       CORE_ADDR addr;

  2799.       bp_objfile_data = get_breakpoint_objfile_data (objfile);

  2800.       if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
  2801.         continue;

  2802.       if (bp_objfile_data->overlay_msym.minsym == NULL)
  2803.         {
  2804.           struct bound_minimal_symbol m;

  2805.           m = lookup_minimal_symbol_text (func_name, objfile);
  2806.           if (m.minsym == NULL)
  2807.             {
  2808.               /* Avoid future lookups in this objfile.  */
  2809.               bp_objfile_data->overlay_msym.minsym = &msym_not_found;
  2810.               continue;
  2811.             }
  2812.           bp_objfile_data->overlay_msym = m;
  2813.         }

  2814.       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
  2815.       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
  2816.                                       bp_overlay_event,
  2817.                                       &internal_breakpoint_ops);
  2818.       b->addr_string = xstrdup (func_name);

  2819.       if (overlay_debugging == ovly_auto)
  2820.         {
  2821.           b->enable_state = bp_enabled;
  2822.           overlay_events_enabled = 1;
  2823.         }
  2824.       else
  2825.        {
  2826.          b->enable_state = bp_disabled;
  2827.          overlay_events_enabled = 0;
  2828.        }
  2829.     }
  2830.   update_global_location_list (UGLL_MAY_INSERT);
  2831. }

  2832. static void
  2833. create_longjmp_master_breakpoint (void)
  2834. {
  2835.   struct program_space *pspace;
  2836.   struct cleanup *old_chain;

  2837.   old_chain = save_current_program_space ();

  2838.   ALL_PSPACES (pspace)
  2839.   {
  2840.     struct objfile *objfile;

  2841.     set_current_program_space (pspace);

  2842.     ALL_OBJFILES (objfile)
  2843.     {
  2844.       int i;
  2845.       struct gdbarch *gdbarch;
  2846.       struct breakpoint_objfile_data *bp_objfile_data;

  2847.       gdbarch = get_objfile_arch (objfile);

  2848.       bp_objfile_data = get_breakpoint_objfile_data (objfile);

  2849.       if (!bp_objfile_data->longjmp_searched)
  2850.         {
  2851.           VEC (probe_p) *ret;

  2852.           ret = find_probes_in_objfile (objfile, "libc", "longjmp");
  2853.           if (ret != NULL)
  2854.             {
  2855.               /* We are only interested in checking one element.  */
  2856.               struct probe *p = VEC_index (probe_p, ret, 0);

  2857.               if (!can_evaluate_probe_arguments (p))
  2858.                 {
  2859.                   /* We cannot use the probe interface here, because it does
  2860.                      not know how to evaluate arguments.  */
  2861.                   VEC_free (probe_p, ret);
  2862.                   ret = NULL;
  2863.                 }
  2864.             }
  2865.           bp_objfile_data->longjmp_probes = ret;
  2866.           bp_objfile_data->longjmp_searched = 1;
  2867.         }

  2868.       if (bp_objfile_data->longjmp_probes != NULL)
  2869.         {
  2870.           int i;
  2871.           struct probe *probe;
  2872.           struct gdbarch *gdbarch = get_objfile_arch (objfile);

  2873.           for (i = 0;
  2874.                VEC_iterate (probe_p,
  2875.                             bp_objfile_data->longjmp_probes,
  2876.                             i, probe);
  2877.                ++i)
  2878.             {
  2879.               struct breakpoint *b;

  2880.               b = create_internal_breakpoint (gdbarch,
  2881.                                               get_probe_address (probe,
  2882.                                                                  objfile),
  2883.                                               bp_longjmp_master,
  2884.                                               &internal_breakpoint_ops);
  2885.               b->addr_string = xstrdup ("-probe-stap libc:longjmp");
  2886.               b->enable_state = bp_disabled;
  2887.             }

  2888.           continue;
  2889.         }

  2890.       if (!gdbarch_get_longjmp_target_p (gdbarch))
  2891.         continue;

  2892.       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
  2893.         {
  2894.           struct breakpoint *b;
  2895.           const char *func_name;
  2896.           CORE_ADDR addr;

  2897.           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
  2898.             continue;

  2899.           func_name = longjmp_names[i];
  2900.           if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
  2901.             {
  2902.               struct bound_minimal_symbol m;

  2903.               m = lookup_minimal_symbol_text (func_name, objfile);
  2904.               if (m.minsym == NULL)
  2905.                 {
  2906.                   /* Prevent future lookups in this objfile.  */
  2907.                   bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
  2908.                   continue;
  2909.                 }
  2910.               bp_objfile_data->longjmp_msym[i] = m;
  2911.             }

  2912.           addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
  2913.           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
  2914.                                           &internal_breakpoint_ops);
  2915.           b->addr_string = xstrdup (func_name);
  2916.           b->enable_state = bp_disabled;
  2917.         }
  2918.     }
  2919.   }
  2920.   update_global_location_list (UGLL_MAY_INSERT);

  2921.   do_cleanups (old_chain);
  2922. }

  2923. /* Create a master std::terminate breakpoint.  */
  2924. static void
  2925. create_std_terminate_master_breakpoint (void)
  2926. {
  2927.   struct program_space *pspace;
  2928.   struct cleanup *old_chain;
  2929.   const char *const func_name = "std::terminate()";

  2930.   old_chain = save_current_program_space ();

  2931.   ALL_PSPACES (pspace)
  2932.   {
  2933.     struct objfile *objfile;
  2934.     CORE_ADDR addr;

  2935.     set_current_program_space (pspace);

  2936.     ALL_OBJFILES (objfile)
  2937.     {
  2938.       struct breakpoint *b;
  2939.       struct breakpoint_objfile_data *bp_objfile_data;

  2940.       bp_objfile_data = get_breakpoint_objfile_data (objfile);

  2941.       if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
  2942.         continue;

  2943.       if (bp_objfile_data->terminate_msym.minsym == NULL)
  2944.         {
  2945.           struct bound_minimal_symbol m;

  2946.           m = lookup_minimal_symbol (func_name, NULL, objfile);
  2947.           if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
  2948.                                    && MSYMBOL_TYPE (m.minsym) != mst_file_text))
  2949.             {
  2950.               /* Prevent future lookups in this objfile.  */
  2951.               bp_objfile_data->terminate_msym.minsym = &msym_not_found;
  2952.               continue;
  2953.             }
  2954.           bp_objfile_data->terminate_msym = m;
  2955.         }

  2956.       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
  2957.       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
  2958.                                       bp_std_terminate_master,
  2959.                                       &internal_breakpoint_ops);
  2960.       b->addr_string = xstrdup (func_name);
  2961.       b->enable_state = bp_disabled;
  2962.     }
  2963.   }

  2964.   update_global_location_list (UGLL_MAY_INSERT);

  2965.   do_cleanups (old_chain);
  2966. }

  2967. /* Install a master breakpoint on the unwinder's debug hook.  */

  2968. static void
  2969. create_exception_master_breakpoint (void)
  2970. {
  2971.   struct objfile *objfile;
  2972.   const char *const func_name = "_Unwind_DebugHook";

  2973.   ALL_OBJFILES (objfile)
  2974.     {
  2975.       struct breakpoint *b;
  2976.       struct gdbarch *gdbarch;
  2977.       struct breakpoint_objfile_data *bp_objfile_data;
  2978.       CORE_ADDR addr;

  2979.       bp_objfile_data = get_breakpoint_objfile_data (objfile);

  2980.       /* We prefer the SystemTap probe point if it exists.  */
  2981.       if (!bp_objfile_data->exception_searched)
  2982.         {
  2983.           VEC (probe_p) *ret;

  2984.           ret = find_probes_in_objfile (objfile, "libgcc", "unwind");

  2985.           if (ret != NULL)
  2986.             {
  2987.               /* We are only interested in checking one element.  */
  2988.               struct probe *p = VEC_index (probe_p, ret, 0);

  2989.               if (!can_evaluate_probe_arguments (p))
  2990.                 {
  2991.                   /* We cannot use the probe interface here, because it does
  2992.                      not know how to evaluate arguments.  */
  2993.                   VEC_free (probe_p, ret);
  2994.                   ret = NULL;
  2995.                 }
  2996.             }
  2997.           bp_objfile_data->exception_probes = ret;
  2998.           bp_objfile_data->exception_searched = 1;
  2999.         }

  3000.       if (bp_objfile_data->exception_probes != NULL)
  3001.         {
  3002.           struct gdbarch *gdbarch = get_objfile_arch (objfile);
  3003.           int i;
  3004.           struct probe *probe;

  3005.           for (i = 0;
  3006.                VEC_iterate (probe_p,
  3007.                             bp_objfile_data->exception_probes,
  3008.                             i, probe);
  3009.                ++i)
  3010.             {
  3011.               struct breakpoint *b;

  3012.               b = create_internal_breakpoint (gdbarch,
  3013.                                               get_probe_address (probe,
  3014.                                                                  objfile),
  3015.                                               bp_exception_master,
  3016.                                               &internal_breakpoint_ops);
  3017.               b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
  3018.               b->enable_state = bp_disabled;
  3019.             }

  3020.           continue;
  3021.         }

  3022.       /* Otherwise, try the hook function.  */

  3023.       if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
  3024.         continue;

  3025.       gdbarch = get_objfile_arch (objfile);

  3026.       if (bp_objfile_data->exception_msym.minsym == NULL)
  3027.         {
  3028.           struct bound_minimal_symbol debug_hook;

  3029.           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
  3030.           if (debug_hook.minsym == NULL)
  3031.             {
  3032.               bp_objfile_data->exception_msym.minsym = &msym_not_found;
  3033.               continue;
  3034.             }

  3035.           bp_objfile_data->exception_msym = debug_hook;
  3036.         }

  3037.       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
  3038.       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
  3039.                                                  &current_target);
  3040.       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
  3041.                                       &internal_breakpoint_ops);
  3042.       b->addr_string = xstrdup (func_name);
  3043.       b->enable_state = bp_disabled;
  3044.     }

  3045.   update_global_location_list (UGLL_MAY_INSERT);
  3046. }

  3047. void
  3048. update_breakpoints_after_exec (void)
  3049. {
  3050.   struct breakpoint *b, *b_tmp;
  3051.   struct bp_location *bploc, **bplocp_tmp;

  3052.   /* We're about to delete breakpoints from GDB's lists.  If the
  3053.      INSERTED flag is true, GDB will try to lift the breakpoints by
  3054.      writing the breakpoints' "shadow contents" back into memory.  The
  3055.      "shadow contents" are NOT valid after an exec, so GDB should not
  3056.      do that.  Instead, the target is responsible from marking
  3057.      breakpoints out as soon as it detects an exec.  We don't do that
  3058.      here instead, because there may be other attempts to delete
  3059.      breakpoints after detecting an exec and before reaching here.  */
  3060.   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
  3061.     if (bploc->pspace == current_program_space)
  3062.       gdb_assert (!bploc->inserted);

  3063.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  3064.   {
  3065.     if (b->pspace != current_program_space)
  3066.       continue;

  3067.     /* Solib breakpoints must be explicitly reset after an exec().  */
  3068.     if (b->type == bp_shlib_event)
  3069.       {
  3070.         delete_breakpoint (b);
  3071.         continue;
  3072.       }

  3073.     /* JIT breakpoints must be explicitly reset after an exec().  */
  3074.     if (b->type == bp_jit_event)
  3075.       {
  3076.         delete_breakpoint (b);
  3077.         continue;
  3078.       }

  3079.     /* Thread event breakpoints must be set anew after an exec(),
  3080.        as must overlay event and longjmp master breakpoints.  */
  3081.     if (b->type == bp_thread_event || b->type == bp_overlay_event
  3082.         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
  3083.         || b->type == bp_exception_master)
  3084.       {
  3085.         delete_breakpoint (b);
  3086.         continue;
  3087.       }

  3088.     /* Step-resume breakpoints are meaningless after an exec().  */
  3089.     if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
  3090.       {
  3091.         delete_breakpoint (b);
  3092.         continue;
  3093.       }

  3094.     /* Just like single-step breakpoints.  */
  3095.     if (b->type == bp_single_step)
  3096.       {
  3097.         delete_breakpoint (b);
  3098.         continue;
  3099.       }

  3100.     /* Longjmp and longjmp-resume breakpoints are also meaningless
  3101.        after an exec.  */
  3102.     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
  3103.         || b->type == bp_longjmp_call_dummy
  3104.         || b->type == bp_exception || b->type == bp_exception_resume)
  3105.       {
  3106.         delete_breakpoint (b);
  3107.         continue;
  3108.       }

  3109.     if (b->type == bp_catchpoint)
  3110.       {
  3111.         /* For now, none of the bp_catchpoint breakpoints need to
  3112.            do anything at this point.  In the future, if some of
  3113.            the catchpoints need to something, we will need to add
  3114.            a new method, and call this method from here.  */
  3115.         continue;
  3116.       }

  3117.     /* bp_finish is a special case.  The only way we ought to be able
  3118.        to see one of these when an exec() has happened, is if the user
  3119.        caught a vfork, and then said "finish".  Ordinarily a finish just
  3120.        carries them to the call-site of the current callee, by setting
  3121.        a temporary bp there and resuming.  But in this case, the finish
  3122.        will carry them entirely through the vfork & exec.

  3123.        We don't want to allow a bp_finish to remain inserted now.  But
  3124.        we can't safely delete it, 'cause finish_command has a handle to
  3125.        the bp on a bpstat, and will later want to delete it.  There's a
  3126.        chance (and I've seen it happen) that if we delete the bp_finish
  3127.        here, that its storage will get reused by the time finish_command
  3128.        gets 'round to deleting the "use to be a bp_finish" breakpoint.
  3129.        We really must allow finish_command to delete a bp_finish.

  3130.        In the absence of a general solution for the "how do we know
  3131.        it's safe to delete something others may have handles to?"
  3132.        problem, what we'll do here is just uninsert the bp_finish, and
  3133.        let finish_command delete it.

  3134.        (We know the bp_finish is "doomed" in the sense that it's
  3135.        momentary, and will be deleted as soon as finish_command sees
  3136.        the inferior stopped.  So it doesn't matter that the bp's
  3137.        address is probably bogus in the new a.out, unlike e.g., the
  3138.        solib breakpoints.)  */

  3139.     if (b->type == bp_finish)
  3140.       {
  3141.         continue;
  3142.       }

  3143.     /* Without a symbolic address, we have little hope of the
  3144.        pre-exec() address meaning the same thing in the post-exec()
  3145.        a.out.  */
  3146.     if (b->addr_string == NULL)
  3147.       {
  3148.         delete_breakpoint (b);
  3149.         continue;
  3150.       }
  3151.   }
  3152. }

  3153. int
  3154. detach_breakpoints (ptid_t ptid)
  3155. {
  3156.   struct bp_location *bl, **blp_tmp;
  3157.   int val = 0;
  3158.   struct cleanup *old_chain = save_inferior_ptid ();
  3159.   struct inferior *inf = current_inferior ();

  3160.   if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
  3161.     error (_("Cannot detach breakpoints of inferior_ptid"));

  3162.   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
  3163.   inferior_ptid = ptid;
  3164.   ALL_BP_LOCATIONS (bl, blp_tmp)
  3165.   {
  3166.     if (bl->pspace != inf->pspace)
  3167.       continue;

  3168.     /* This function must physically remove breakpoints locations
  3169.        from the specified ptid, without modifying the breakpoint
  3170.        package's state.  Locations of type bp_loc_other are only
  3171.        maintained at GDB side.  So, there is no need to remove
  3172.        these bp_loc_other locations.  Moreover, removing these
  3173.        would modify the breakpoint package's state.  */
  3174.     if (bl->loc_type == bp_loc_other)
  3175.       continue;

  3176.     if (bl->inserted)
  3177.       val |= remove_breakpoint_1 (bl, mark_inserted);
  3178.   }

  3179.   do_cleanups (old_chain);
  3180.   return val;
  3181. }

  3182. /* Remove the breakpoint location BL from the current address space.
  3183.    Note that this is used to detach breakpoints from a child fork.
  3184.    When we get here, the child isn't in the inferior list, and neither
  3185.    do we have objects to represent its address space --- we should
  3186.    *not* look at bl->pspace->aspace here.  */

  3187. static int
  3188. remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
  3189. {
  3190.   int val;

  3191.   /* BL is never in moribund_locations by our callers.  */
  3192.   gdb_assert (bl->owner != NULL);

  3193.   if (bl->permanent)
  3194.     /* Permanent breakpoints cannot be inserted or removed.  */
  3195.     return 0;

  3196.   /* The type of none suggests that owner is actually deleted.
  3197.      This should not ever happen.  */
  3198.   gdb_assert (bl->owner->type != bp_none);

  3199.   if (bl->loc_type == bp_loc_software_breakpoint
  3200.       || bl->loc_type == bp_loc_hardware_breakpoint)
  3201.     {
  3202.       /* "Normal" instruction breakpoint: either the standard
  3203.          trap-instruction bp (bp_breakpoint), or a
  3204.          bp_hardware_breakpoint.  */

  3205.       /* First check to see if we have to handle an overlay.  */
  3206.       if (overlay_debugging == ovly_off
  3207.           || bl->section == NULL
  3208.           || !(section_is_overlay (bl->section)))
  3209.         {
  3210.           /* No overlay handling: just remove the breakpoint.  */

  3211.           /* If we're trying to uninsert a memory breakpoint that we
  3212.              know is set in a dynamic object that is marked
  3213.              shlib_disabled, then either the dynamic object was
  3214.              removed with "remove-symbol-file" or with
  3215.              "nosharedlibrary".  In the former case, we don't know
  3216.              whether another dynamic object might have loaded over the
  3217.              breakpoint's address -- the user might well let us know
  3218.              about it next with add-symbol-file (the whole point of
  3219.              add-symbol-file is letting the user manually maintain a
  3220.              list of dynamically loaded objects).  If we have the
  3221.              breakpoint's shadow memory, that is, this is a software
  3222.              breakpoint managed by GDB, check whether the breakpoint
  3223.              is still inserted in memory, to avoid overwriting wrong
  3224.              code with stale saved shadow contents.  Note that HW
  3225.              breakpoints don't have shadow memory, as they're
  3226.              implemented using a mechanism that is not dependent on
  3227.              being able to modify the target's memory, and as such
  3228.              they should always be removed.  */
  3229.           if (bl->shlib_disabled
  3230.               && bl->target_info.shadow_len != 0
  3231.               && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
  3232.             val = 0;
  3233.           else
  3234.             val = bl->owner->ops->remove_location (bl);
  3235.         }
  3236.       else
  3237.         {
  3238.           /* This breakpoint is in an overlay section.
  3239.              Did we set a breakpoint at the LMA?  */
  3240.           if (!overlay_events_enabled)
  3241.               {
  3242.                 /* Yes -- overlay event support is not active, so we
  3243.                    should have set a breakpoint at the LMA.  Remove it.
  3244.                 */
  3245.                 /* Ignore any failures: if the LMA is in ROM, we will
  3246.                    have already warned when we failed to insert it.  */
  3247.                 if (bl->loc_type == bp_loc_hardware_breakpoint)
  3248.                   target_remove_hw_breakpoint (bl->gdbarch,
  3249.                                                &bl->overlay_target_info);
  3250.                 else
  3251.                   target_remove_breakpoint (bl->gdbarch,
  3252.                                             &bl->overlay_target_info);
  3253.               }
  3254.           /* Did we set a breakpoint at the VMA?
  3255.              If so, we will have marked the breakpoint 'inserted'.  */
  3256.           if (bl->inserted)
  3257.             {
  3258.               /* Yes -- remove it.  Previously we did not bother to
  3259.                  remove the breakpoint if the section had been
  3260.                  unmapped, but let's not rely on that being safe.  We
  3261.                  don't know what the overlay manager might do.  */

  3262.               /* However, we should remove *software* breakpoints only
  3263.                  if the section is still mapped, or else we overwrite
  3264.                  wrong code with the saved shadow contents.  */
  3265.               if (bl->loc_type == bp_loc_hardware_breakpoint
  3266.                   || section_is_mapped (bl->section))
  3267.                 val = bl->owner->ops->remove_location (bl);
  3268.               else
  3269.                 val = 0;
  3270.             }
  3271.           else
  3272.             {
  3273.               /* No -- not inserted, so no need to remove.  No error.  */
  3274.               val = 0;
  3275.             }
  3276.         }

  3277.       /* In some cases, we might not be able to remove a breakpoint in
  3278.          a shared library that has already been removed, but we have
  3279.          not yet processed the shlib unload event.  Similarly for an
  3280.          unloaded add-symbol-file object - the user might not yet have
  3281.          had the chance to remove-symbol-file it.  shlib_disabled will
  3282.          be set if the library/object has already been removed, but
  3283.          the breakpoint hasn't been uninserted yet, e.g., after
  3284.          "nosharedlibrary" or "remove-symbol-file" with breakpoints
  3285.          always-inserted mode.  */
  3286.       if (val
  3287.           && (bl->loc_type == bp_loc_software_breakpoint
  3288.               && (bl->shlib_disabled
  3289.                   || solib_name_from_address (bl->pspace, bl->address)
  3290.                   || shared_objfile_contains_address_p (bl->pspace,
  3291.                                                         bl->address))))
  3292.         val = 0;

  3293.       if (val)
  3294.         return val;
  3295.       bl->inserted = (is == mark_inserted);
  3296.     }
  3297.   else if (bl->loc_type == bp_loc_hardware_watchpoint)
  3298.     {
  3299.       gdb_assert (bl->owner->ops != NULL
  3300.                   && bl->owner->ops->remove_location != NULL);

  3301.       bl->inserted = (is == mark_inserted);
  3302.       bl->owner->ops->remove_location (bl);

  3303.       /* Failure to remove any of the hardware watchpoints comes here.  */
  3304.       if ((is == mark_uninserted) && (bl->inserted))
  3305.         warning (_("Could not remove hardware watchpoint %d."),
  3306.                  bl->owner->number);
  3307.     }
  3308.   else if (bl->owner->type == bp_catchpoint
  3309.            && breakpoint_enabled (bl->owner)
  3310.            && !bl->duplicate)
  3311.     {
  3312.       gdb_assert (bl->owner->ops != NULL
  3313.                   && bl->owner->ops->remove_location != NULL);

  3314.       val = bl->owner->ops->remove_location (bl);
  3315.       if (val)
  3316.         return val;

  3317.       bl->inserted = (is == mark_inserted);
  3318.     }

  3319.   return 0;
  3320. }

  3321. static int
  3322. remove_breakpoint (struct bp_location *bl, insertion_state_t is)
  3323. {
  3324.   int ret;
  3325.   struct cleanup *old_chain;

  3326.   /* BL is never in moribund_locations by our callers.  */
  3327.   gdb_assert (bl->owner != NULL);

  3328.   if (bl->permanent)
  3329.     /* Permanent breakpoints cannot be inserted or removed.  */
  3330.     return 0;

  3331.   /* The type of none suggests that owner is actually deleted.
  3332.      This should not ever happen.  */
  3333.   gdb_assert (bl->owner->type != bp_none);

  3334.   old_chain = save_current_space_and_thread ();

  3335.   switch_to_program_space_and_thread (bl->pspace);

  3336.   ret = remove_breakpoint_1 (bl, is);

  3337.   do_cleanups (old_chain);
  3338.   return ret;
  3339. }

  3340. /* Clear the "inserted" flag in all breakpoints.  */

  3341. void
  3342. mark_breakpoints_out (void)
  3343. {
  3344.   struct bp_location *bl, **blp_tmp;

  3345.   ALL_BP_LOCATIONS (bl, blp_tmp)
  3346.     if (bl->pspace == current_program_space
  3347.         && !bl->permanent)
  3348.       bl->inserted = 0;
  3349. }

  3350. /* Clear the "inserted" flag in all breakpoints and delete any
  3351.    breakpoints which should go away between runs of the program.

  3352.    Plus other such housekeeping that has to be done for breakpoints
  3353.    between runs.

  3354.    Note: this function gets called at the end of a run (by
  3355.    generic_mourn_inferior) and when a run begins (by
  3356.    init_wait_for_inferior).  */



  3357. void
  3358. breakpoint_init_inferior (enum inf_context context)
  3359. {
  3360.   struct breakpoint *b, *b_tmp;
  3361.   struct bp_location *bl, **blp_tmp;
  3362.   int ix;
  3363.   struct program_space *pspace = current_program_space;

  3364.   /* If breakpoint locations are shared across processes, then there's
  3365.      nothing to do.  */
  3366.   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
  3367.     return;

  3368.   mark_breakpoints_out ();

  3369.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  3370.   {
  3371.     if (b->loc && b->loc->pspace != pspace)
  3372.       continue;

  3373.     switch (b->type)
  3374.       {
  3375.       case bp_call_dummy:
  3376.       case bp_longjmp_call_dummy:

  3377.         /* If the call dummy breakpoint is at the entry point it will
  3378.            cause problems when the inferior is rerun, so we better get
  3379.            rid of it.  */

  3380.       case bp_watchpoint_scope:

  3381.         /* Also get rid of scope breakpoints.  */

  3382.       case bp_shlib_event:

  3383.         /* Also remove solib event breakpoints.  Their addresses may
  3384.            have changed since the last time we ran the program.
  3385.            Actually we may now be debugging against different target;
  3386.            and so the solib backend that installed this breakpoint may
  3387.            not be used in by the target.  E.g.,

  3388.            (gdb) file prog-linux
  3389.            (gdb) run               # native linux target
  3390.            ...
  3391.            (gdb) kill
  3392.            (gdb) file prog-win.exe
  3393.            (gdb) tar rem :9999     # remote Windows gdbserver.
  3394.         */

  3395.       case bp_step_resume:

  3396.         /* Also remove step-resume breakpoints.  */

  3397.       case bp_single_step:

  3398.         /* Also remove single-step breakpoints.  */

  3399.         delete_breakpoint (b);
  3400.         break;

  3401.       case bp_watchpoint:
  3402.       case bp_hardware_watchpoint:
  3403.       case bp_read_watchpoint:
  3404.       case bp_access_watchpoint:
  3405.         {
  3406.           struct watchpoint *w = (struct watchpoint *) b;

  3407.           /* Likewise for watchpoints on local expressions.  */
  3408.           if (w->exp_valid_block != NULL)
  3409.             delete_breakpoint (b);
  3410.           else if (context == inf_starting)
  3411.             {
  3412.               /* Reset val field to force reread of starting value in
  3413.                  insert_breakpoints.  */
  3414.               if (w->val)
  3415.                 value_free (w->val);
  3416.               w->val = NULL;
  3417.               w->val_valid = 0;
  3418.           }
  3419.         }
  3420.         break;
  3421.       default:
  3422.         break;
  3423.       }
  3424.   }

  3425.   /* Get rid of the moribund locations.  */
  3426.   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
  3427.     decref_bp_location (&bl);
  3428.   VEC_free (bp_location_p, moribund_locations);
  3429. }

  3430. /* These functions concern about actual breakpoints inserted in the
  3431.    target --- to e.g. check if we need to do decr_pc adjustment or if
  3432.    we need to hop over the bkpt --- so we check for address space
  3433.    match, not program space.  */

  3434. /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
  3435.    exists at PC.  It returns ordinary_breakpoint_here if it's an
  3436.    ordinary breakpoint, or permanent_breakpoint_here if it's a
  3437.    permanent breakpoint.
  3438.    - When continuing from a location with an ordinary breakpoint, we
  3439.      actually single step once before calling insert_breakpoints.
  3440.    - When continuing from a location with a permanent breakpoint, we
  3441.      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
  3442.      the target, to advance the PC past the breakpoint.  */

  3443. enum breakpoint_here
  3444. breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
  3445. {
  3446.   struct bp_location *bl, **blp_tmp;
  3447.   int any_breakpoint_here = 0;

  3448.   ALL_BP_LOCATIONS (bl, blp_tmp)
  3449.     {
  3450.       if (bl->loc_type != bp_loc_software_breakpoint
  3451.           && bl->loc_type != bp_loc_hardware_breakpoint)
  3452.         continue;

  3453.       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
  3454.       if ((breakpoint_enabled (bl->owner)
  3455.            || bl->permanent)
  3456.           && breakpoint_location_address_match (bl, aspace, pc))
  3457.         {
  3458.           if (overlay_debugging
  3459.               && section_is_overlay (bl->section)
  3460.               && !section_is_mapped (bl->section))
  3461.             continue;                /* unmapped overlay -- can't be a match */
  3462.           else if (bl->permanent)
  3463.             return permanent_breakpoint_here;
  3464.           else
  3465.             any_breakpoint_here = 1;
  3466.         }
  3467.     }

  3468.   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
  3469. }

  3470. /* Return true if there's a moribund breakpoint at PC.  */

  3471. int
  3472. moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
  3473. {
  3474.   struct bp_location *loc;
  3475.   int ix;

  3476.   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
  3477.     if (breakpoint_location_address_match (loc, aspace, pc))
  3478.       return 1;

  3479.   return 0;
  3480. }

  3481. /* Returns non-zero iff BL is inserted at PC, in address space
  3482.    ASPACE.  */

  3483. static int
  3484. bp_location_inserted_here_p (struct bp_location *bl,
  3485.                              struct address_space *aspace, CORE_ADDR pc)
  3486. {
  3487.   if (bl->inserted
  3488.       && breakpoint_address_match (bl->pspace->aspace, bl->address,
  3489.                                    aspace, pc))
  3490.     {
  3491.       if (overlay_debugging
  3492.           && section_is_overlay (bl->section)
  3493.           && !section_is_mapped (bl->section))
  3494.         return 0;                /* unmapped overlay -- can't be a match */
  3495.       else
  3496.         return 1;
  3497.     }
  3498.   return 0;
  3499. }

  3500. /* Returns non-zero iff there's a breakpoint inserted at PC.  */

  3501. int
  3502. breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
  3503. {
  3504.   struct bp_location **blp, **blp_tmp = NULL;
  3505.   struct bp_location *bl;

  3506.   ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
  3507.     {
  3508.       struct bp_location *bl = *blp;

  3509.       if (bl->loc_type != bp_loc_software_breakpoint
  3510.           && bl->loc_type != bp_loc_hardware_breakpoint)
  3511.         continue;

  3512.       if (bp_location_inserted_here_p (bl, aspace, pc))
  3513.         return 1;
  3514.     }
  3515.   return 0;
  3516. }

  3517. /* This function returns non-zero iff there is a software breakpoint
  3518.    inserted at PC.  */

  3519. int
  3520. software_breakpoint_inserted_here_p (struct address_space *aspace,
  3521.                                      CORE_ADDR pc)
  3522. {
  3523.   struct bp_location **blp, **blp_tmp = NULL;
  3524.   struct bp_location *bl;

  3525.   ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
  3526.     {
  3527.       struct bp_location *bl = *blp;

  3528.       if (bl->loc_type != bp_loc_software_breakpoint)
  3529.         continue;

  3530.       if (bp_location_inserted_here_p (bl, aspace, pc))
  3531.         return 1;
  3532.     }

  3533.   return 0;
  3534. }

  3535. /* See breakpoint.h.  */

  3536. int
  3537. hardware_breakpoint_inserted_here_p (struct address_space *aspace,
  3538.                                      CORE_ADDR pc)
  3539. {
  3540.   struct bp_location **blp, **blp_tmp = NULL;
  3541.   struct bp_location *bl;

  3542.   ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
  3543.     {
  3544.       struct bp_location *bl = *blp;

  3545.       if (bl->loc_type != bp_loc_hardware_breakpoint)
  3546.         continue;

  3547.       if (bp_location_inserted_here_p (bl, aspace, pc))
  3548.         return 1;
  3549.     }

  3550.   return 0;
  3551. }

  3552. int
  3553. hardware_watchpoint_inserted_in_range (struct address_space *aspace,
  3554.                                        CORE_ADDR addr, ULONGEST len)
  3555. {
  3556.   struct breakpoint *bpt;

  3557.   ALL_BREAKPOINTS (bpt)
  3558.     {
  3559.       struct bp_location *loc;

  3560.       if (bpt->type != bp_hardware_watchpoint
  3561.           && bpt->type != bp_access_watchpoint)
  3562.         continue;

  3563.       if (!breakpoint_enabled (bpt))
  3564.         continue;

  3565.       for (loc = bpt->loc; loc; loc = loc->next)
  3566.         if (loc->pspace->aspace == aspace && loc->inserted)
  3567.           {
  3568.             CORE_ADDR l, h;

  3569.             /* Check for intersection.  */
  3570.             l = max (loc->address, addr);
  3571.             h = min (loc->address + loc->length, addr + len);
  3572.             if (l < h)
  3573.               return 1;
  3574.           }
  3575.     }
  3576.   return 0;
  3577. }


  3578. /* bpstat stuff.  External routines' interfaces are documented
  3579.    in breakpoint.h.  */

  3580. int
  3581. is_catchpoint (struct breakpoint *ep)
  3582. {
  3583.   return (ep->type == bp_catchpoint);
  3584. }

  3585. /* Frees any storage that is part of a bpstat.  Does not walk the
  3586.    'next' chain.  */

  3587. static void
  3588. bpstat_free (bpstat bs)
  3589. {
  3590.   if (bs->old_val != NULL)
  3591.     value_free (bs->old_val);
  3592.   decref_counted_command_line (&bs->commands);
  3593.   decref_bp_location (&bs->bp_location_at);
  3594.   xfree (bs);
  3595. }

  3596. /* Clear a bpstat so that it says we are not at any breakpoint.
  3597.    Also free any storage that is part of a bpstat.  */

  3598. void
  3599. bpstat_clear (bpstat *bsp)
  3600. {
  3601.   bpstat p;
  3602.   bpstat q;

  3603.   if (bsp == 0)
  3604.     return;
  3605.   p = *bsp;
  3606.   while (p != NULL)
  3607.     {
  3608.       q = p->next;
  3609.       bpstat_free (p);
  3610.       p = q;
  3611.     }
  3612.   *bsp = NULL;
  3613. }

  3614. /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
  3615.    is part of the bpstat is copied as well.  */

  3616. bpstat
  3617. bpstat_copy (bpstat bs)
  3618. {
  3619.   bpstat p = NULL;
  3620.   bpstat tmp;
  3621.   bpstat retval = NULL;

  3622.   if (bs == NULL)
  3623.     return bs;

  3624.   for (; bs != NULL; bs = bs->next)
  3625.     {
  3626.       tmp = (bpstat) xmalloc (sizeof (*tmp));
  3627.       memcpy (tmp, bs, sizeof (*tmp));
  3628.       incref_counted_command_line (tmp->commands);
  3629.       incref_bp_location (tmp->bp_location_at);
  3630.       if (bs->old_val != NULL)
  3631.         {
  3632.           tmp->old_val = value_copy (bs->old_val);
  3633.           release_value (tmp->old_val);
  3634.         }

  3635.       if (p == NULL)
  3636.         /* This is the first thing in the chain.  */
  3637.         retval = tmp;
  3638.       else
  3639.         p->next = tmp;
  3640.       p = tmp;
  3641.     }
  3642.   p->next = NULL;
  3643.   return retval;
  3644. }

  3645. /* Find the bpstat associated with this breakpoint.  */

  3646. bpstat
  3647. bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
  3648. {
  3649.   if (bsp == NULL)
  3650.     return NULL;

  3651.   for (; bsp != NULL; bsp = bsp->next)
  3652.     {
  3653.       if (bsp->breakpoint_at == breakpoint)
  3654.         return bsp;
  3655.     }
  3656.   return NULL;
  3657. }

  3658. /* See breakpoint.h.  */

  3659. int
  3660. bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
  3661. {
  3662.   for (; bsp != NULL; bsp = bsp->next)
  3663.     {
  3664.       if (bsp->breakpoint_at == NULL)
  3665.         {
  3666.           /* A moribund location can never explain a signal other than
  3667.              GDB_SIGNAL_TRAP.  */
  3668.           if (sig == GDB_SIGNAL_TRAP)
  3669.             return 1;
  3670.         }
  3671.       else
  3672.         {
  3673.           if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
  3674.                                                         sig))
  3675.             return 1;
  3676.         }
  3677.     }

  3678.   return 0;
  3679. }

  3680. /* Put in *NUM the breakpoint number of the first breakpoint we are
  3681.    stopped at.  *BSP upon return is a bpstat which points to the
  3682.    remaining breakpoints stopped at (but which is not guaranteed to be
  3683.    good for anything but further calls to bpstat_num).

  3684.    Return 0 if passed a bpstat which does not indicate any breakpoints.
  3685.    Return -1 if stopped at a breakpoint that has been deleted since
  3686.    we set it.
  3687.    Return 1 otherwise.  */

  3688. int
  3689. bpstat_num (bpstat *bsp, int *num)
  3690. {
  3691.   struct breakpoint *b;

  3692.   if ((*bsp) == NULL)
  3693.     return 0;                        /* No more breakpoint values */

  3694.   /* We assume we'll never have several bpstats that correspond to a
  3695.      single breakpoint -- otherwise, this function might return the
  3696.      same number more than once and this will look ugly.  */
  3697.   b = (*bsp)->breakpoint_at;
  3698.   *bsp = (*bsp)->next;
  3699.   if (b == NULL)
  3700.     return -1;                        /* breakpoint that's been deleted since */

  3701.   *num = b->number;                /* We have its number */
  3702.   return 1;
  3703. }

  3704. /* See breakpoint.h.  */

  3705. void
  3706. bpstat_clear_actions (void)
  3707. {
  3708.   struct thread_info *tp;
  3709.   bpstat bs;

  3710.   if (ptid_equal (inferior_ptid, null_ptid))
  3711.     return;

  3712.   tp = find_thread_ptid (inferior_ptid);
  3713.   if (tp == NULL)
  3714.     return;

  3715.   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
  3716.     {
  3717.       decref_counted_command_line (&bs->commands);

  3718.       if (bs->old_val != NULL)
  3719.         {
  3720.           value_free (bs->old_val);
  3721.           bs->old_val = NULL;
  3722.         }
  3723.     }
  3724. }

  3725. /* Called when a command is about to proceed the inferior.  */

  3726. static void
  3727. breakpoint_about_to_proceed (void)
  3728. {
  3729.   if (!ptid_equal (inferior_ptid, null_ptid))
  3730.     {
  3731.       struct thread_info *tp = inferior_thread ();

  3732.       /* Allow inferior function calls in breakpoint commands to not
  3733.          interrupt the command list.  When the call finishes
  3734.          successfully, the inferior will be standing at the same
  3735.          breakpoint as if nothing happened.  */
  3736.       if (tp->control.in_infcall)
  3737.         return;
  3738.     }

  3739.   breakpoint_proceeded = 1;
  3740. }

  3741. /* Stub for cleaning up our state if we error-out of a breakpoint
  3742.    command.  */
  3743. static void
  3744. cleanup_executing_breakpoints (void *ignore)
  3745. {
  3746.   executing_breakpoint_commands = 0;
  3747. }

  3748. /* Return non-zero iff CMD as the first line of a command sequence is `silent'
  3749.    or its equivalent.  */

  3750. static int
  3751. command_line_is_silent (struct command_line *cmd)
  3752. {
  3753.   return cmd && (strcmp ("silent", cmd->line) == 0
  3754.                  || (xdb_commands && strcmp ("Q", cmd->line) == 0));
  3755. }

  3756. /* Execute all the commands associated with all the breakpoints at
  3757.    this location.  Any of these commands could cause the process to
  3758.    proceed beyond this point, etc.  We look out for such changes by
  3759.    checking the global "breakpoint_proceeded" after each command.

  3760.    Returns true if a breakpoint command resumed the inferior.  In that
  3761.    case, it is the caller's responsibility to recall it again with the
  3762.    bpstat of the current thread.  */

  3763. static int
  3764. bpstat_do_actions_1 (bpstat *bsp)
  3765. {
  3766.   bpstat bs;
  3767.   struct cleanup *old_chain;
  3768.   int again = 0;

  3769.   /* Avoid endless recursion if a `source' command is contained
  3770.      in bs->commands.  */
  3771.   if (executing_breakpoint_commands)
  3772.     return 0;

  3773.   executing_breakpoint_commands = 1;
  3774.   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);

  3775.   prevent_dont_repeat ();

  3776.   /* This pointer will iterate over the list of bpstat's.  */
  3777.   bs = *bsp;

  3778.   breakpoint_proceeded = 0;
  3779.   for (; bs != NULL; bs = bs->next)
  3780.     {
  3781.       struct counted_command_line *ccmd;
  3782.       struct command_line *cmd;
  3783.       struct cleanup *this_cmd_tree_chain;

  3784.       /* Take ownership of the BSP's command tree, if it has one.

  3785.          The command tree could legitimately contain commands like
  3786.          'step' and 'next', which call clear_proceed_status, which
  3787.          frees stop_bpstat's command tree.  To make sure this doesn't
  3788.          free the tree we're executing out from under us, we need to
  3789.          take ownership of the tree ourselves.  Since a given bpstat's
  3790.          commands are only executed once, we don't need to copy it; we
  3791.          can clear the pointer in the bpstat, and make sure we free
  3792.          the tree when we're done.  */
  3793.       ccmd = bs->commands;
  3794.       bs->commands = NULL;
  3795.       this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
  3796.       cmd = ccmd ? ccmd->commands : NULL;
  3797.       if (command_line_is_silent (cmd))
  3798.         {
  3799.           /* The action has been already done by bpstat_stop_status.  */
  3800.           cmd = cmd->next;
  3801.         }

  3802.       while (cmd != NULL)
  3803.         {
  3804.           execute_control_command (cmd);

  3805.           if (breakpoint_proceeded)
  3806.             break;
  3807.           else
  3808.             cmd = cmd->next;
  3809.         }

  3810.       /* We can free this command tree now.  */
  3811.       do_cleanups (this_cmd_tree_chain);

  3812.       if (breakpoint_proceeded)
  3813.         {
  3814.           if (target_can_async_p ())
  3815.             /* If we are in async mode, then the target might be still
  3816.                running, not stopped at any breakpoint, so nothing for
  3817.                us to do here -- just return to the event loop.  */
  3818.             ;
  3819.           else
  3820.             /* In sync mode, when execute_control_command returns
  3821.                we're already standing on the next breakpoint.
  3822.                Breakpoint commands for that stop were not run, since
  3823.                execute_command does not run breakpoint commands --
  3824.                only command_line_handler does, but that one is not
  3825.                involved in execution of breakpoint commands.  So, we
  3826.                can now execute breakpoint commands.  It should be
  3827.                noted that making execute_command do bpstat actions is
  3828.                not an option -- in this case we'll have recursive
  3829.                invocation of bpstat for each breakpoint with a
  3830.                command, and can easily blow up GDB stack.  Instead, we
  3831.                return true, which will trigger the caller to recall us
  3832.                with the new stop_bpstat.  */
  3833.             again = 1;
  3834.           break;
  3835.         }
  3836.     }
  3837.   do_cleanups (old_chain);
  3838.   return again;
  3839. }

  3840. void
  3841. bpstat_do_actions (void)
  3842. {
  3843.   struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();

  3844.   /* Do any commands attached to breakpoint we are stopped at.  */
  3845.   while (!ptid_equal (inferior_ptid, null_ptid)
  3846.          && target_has_execution
  3847.          && !is_exited (inferior_ptid)
  3848.          && !is_executing (inferior_ptid))
  3849.     /* Since in sync mode, bpstat_do_actions may resume the inferior,
  3850.        and only return when it is stopped at the next breakpoint, we
  3851.        keep doing breakpoint actions until it returns false to
  3852.        indicate the inferior was not resumed.  */
  3853.     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
  3854.       break;

  3855.   discard_cleanups (cleanup_if_error);
  3856. }

  3857. /* Print out the (old or new) value associated with a watchpoint.  */

  3858. static void
  3859. watchpoint_value_print (struct value *val, struct ui_file *stream)
  3860. {
  3861.   if (val == NULL)
  3862.     fprintf_unfiltered (stream, _("<unreadable>"));
  3863.   else
  3864.     {
  3865.       struct value_print_options opts;
  3866.       get_user_print_options (&opts);
  3867.       value_print (val, stream, &opts);
  3868.     }
  3869. }

  3870. /* Generic routine for printing messages indicating why we
  3871.    stopped.  The behavior of this function depends on the value
  3872.    'print_it' in the bpstat structure.  Under some circumstances we
  3873.    may decide not to print anything here and delegate the task to
  3874.    normal_stop().  */

  3875. static enum print_stop_action
  3876. print_bp_stop_message (bpstat bs)
  3877. {
  3878.   switch (bs->print_it)
  3879.     {
  3880.     case print_it_noop:
  3881.       /* Nothing should be printed for this bpstat entry.  */
  3882.       return PRINT_UNKNOWN;
  3883.       break;

  3884.     case print_it_done:
  3885.       /* We still want to print the frame, but we already printed the
  3886.          relevant messages.  */
  3887.       return PRINT_SRC_AND_LOC;
  3888.       break;

  3889.     case print_it_normal:
  3890.       {
  3891.         struct breakpoint *b = bs->breakpoint_at;

  3892.         /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
  3893.            which has since been deleted.  */
  3894.         if (b == NULL)
  3895.           return PRINT_UNKNOWN;

  3896.         /* Normal case.  Call the breakpoint's print_it method.  */
  3897.         return b->ops->print_it (bs);
  3898.       }
  3899.       break;

  3900.     default:
  3901.       internal_error (__FILE__, __LINE__,
  3902.                       _("print_bp_stop_message: unrecognized enum value"));
  3903.       break;
  3904.     }
  3905. }

  3906. /* A helper function that prints a shared library stopped event.  */

  3907. static void
  3908. print_solib_event (int is_catchpoint)
  3909. {
  3910.   int any_deleted
  3911.     = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
  3912.   int any_added
  3913.     = !VEC_empty (so_list_ptr, current_program_space->added_solibs);

  3914.   if (!is_catchpoint)
  3915.     {
  3916.       if (any_added || any_deleted)
  3917.         ui_out_text (current_uiout,
  3918.                      _("Stopped due to shared library event:\n"));
  3919.       else
  3920.         ui_out_text (current_uiout,
  3921.                      _("Stopped due to shared library event (no "
  3922.                        "libraries added or removed)\n"));
  3923.     }

  3924.   if (ui_out_is_mi_like_p (current_uiout))
  3925.     ui_out_field_string (current_uiout, "reason",
  3926.                          async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));

  3927.   if (any_deleted)
  3928.     {
  3929.       struct cleanup *cleanup;
  3930.       char *name;
  3931.       int ix;

  3932.       ui_out_text (current_uiout, _("  Inferior unloaded "));
  3933.       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
  3934.                                                     "removed");
  3935.       for (ix = 0;
  3936.            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
  3937.                         ix, name);
  3938.            ++ix)
  3939.         {
  3940.           if (ix > 0)
  3941.             ui_out_text (current_uiout, "    ");
  3942.           ui_out_field_string (current_uiout, "library", name);
  3943.           ui_out_text (current_uiout, "\n");
  3944.         }

  3945.       do_cleanups (cleanup);
  3946.     }

  3947.   if (any_added)
  3948.     {
  3949.       struct so_list *iter;
  3950.       int ix;
  3951.       struct cleanup *cleanup;

  3952.       ui_out_text (current_uiout, _("  Inferior loaded "));
  3953.       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
  3954.                                                     "added");
  3955.       for (ix = 0;
  3956.            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
  3957.                         ix, iter);
  3958.            ++ix)
  3959.         {
  3960.           if (ix > 0)
  3961.             ui_out_text (current_uiout, "    ");
  3962.           ui_out_field_string (current_uiout, "library", iter->so_name);
  3963.           ui_out_text (current_uiout, "\n");
  3964.         }

  3965.       do_cleanups (cleanup);
  3966.     }
  3967. }

  3968. /* Print a message indicating what happened.  This is called from
  3969.    normal_stop().  The input to this routine is the head of the bpstat
  3970.    list - a list of the eventpoints that caused this stop.  KIND is
  3971.    the target_waitkind for the stopping event.  This
  3972.    routine calls the generic print routine for printing a message
  3973.    about reasons for stopping.  This will print (for example) the
  3974.    "Breakpoint n," part of the output.  The return value of this
  3975.    routine is one of:

  3976.    PRINT_UNKNOWN: Means we printed nothing.
  3977.    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
  3978.    code to print the location.  An example is
  3979.    "Breakpoint 1, " which should be followed by
  3980.    the location.
  3981.    PRINT_SRC_ONLY: Means we printed something, but there is no need
  3982.    to also print the location part of the message.
  3983.    An example is the catch/throw messages, which
  3984.    don't require a location appended to the end.
  3985.    PRINT_NOTHING: We have done some printing and we don't need any
  3986.    further info to be printed.  */

  3987. enum print_stop_action
  3988. bpstat_print (bpstat bs, int kind)
  3989. {
  3990.   int val;

  3991.   /* Maybe another breakpoint in the chain caused us to stop.
  3992.      (Currently all watchpoints go on the bpstat whether hit or not.
  3993.      That probably could (should) be changed, provided care is taken
  3994.      with respect to bpstat_explains_signal).  */
  3995.   for (; bs; bs = bs->next)
  3996.     {
  3997.       val = print_bp_stop_message (bs);
  3998.       if (val == PRINT_SRC_ONLY
  3999.           || val == PRINT_SRC_AND_LOC
  4000.           || val == PRINT_NOTHING)
  4001.         return val;
  4002.     }

  4003.   /* If we had hit a shared library event breakpoint,
  4004.      print_bp_stop_message would print out this message.  If we hit an
  4005.      OS-level shared library event, do the same thing.  */
  4006.   if (kind == TARGET_WAITKIND_LOADED)
  4007.     {
  4008.       print_solib_event (0);
  4009.       return PRINT_NOTHING;
  4010.     }

  4011.   /* We reached the end of the chain, or we got a null BS to start
  4012.      with and nothing was printed.  */
  4013.   return PRINT_UNKNOWN;
  4014. }

  4015. /* Evaluate the expression EXP and return 1 if value is zero.
  4016.    This returns the inverse of the condition because it is called
  4017.    from catch_errors which returns 0 if an exception happened, and if an
  4018.    exception happens we want execution to stop.
  4019.    The argument is a "struct expression *" that has been cast to a
  4020.    "void *" to make it pass through catch_errors.  */

  4021. static int
  4022. breakpoint_cond_eval (void *exp)
  4023. {
  4024.   struct value *mark = value_mark ();
  4025.   int i = !value_true (evaluate_expression ((struct expression *) exp));

  4026.   value_free_to_mark (mark);
  4027.   return i;
  4028. }

  4029. /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */

  4030. static bpstat
  4031. bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
  4032. {
  4033.   bpstat bs;

  4034.   bs = (bpstat) xmalloc (sizeof (*bs));
  4035.   bs->next = NULL;
  4036.   **bs_link_pointer = bs;
  4037.   *bs_link_pointer = &bs->next;
  4038.   bs->breakpoint_at = bl->owner;
  4039.   bs->bp_location_at = bl;
  4040.   incref_bp_location (bl);
  4041.   /* If the condition is false, etc., don't do the commands.  */
  4042.   bs->commands = NULL;
  4043.   bs->old_val = NULL;
  4044.   bs->print_it = print_it_normal;
  4045.   return bs;
  4046. }

  4047. /* The target has stopped with waitstatus WS.  Check if any hardware
  4048.    watchpoints have triggered, according to the target.  */

  4049. int
  4050. watchpoints_triggered (struct target_waitstatus *ws)
  4051. {
  4052.   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
  4053.   CORE_ADDR addr;
  4054.   struct breakpoint *b;

  4055.   if (!stopped_by_watchpoint)
  4056.     {
  4057.       /* We were not stopped by a watchpoint.  Mark all watchpoints
  4058.          as not triggered.  */
  4059.       ALL_BREAKPOINTS (b)
  4060.         if (is_hardware_watchpoint (b))
  4061.           {
  4062.             struct watchpoint *w = (struct watchpoint *) b;

  4063.             w->watchpoint_triggered = watch_triggered_no;
  4064.           }

  4065.       return 0;
  4066.     }

  4067.   if (!target_stopped_data_address (&current_target, &addr))
  4068.     {
  4069.       /* We were stopped by a watchpoint, but we don't know where.
  4070.          Mark all watchpoints as unknown.  */
  4071.       ALL_BREAKPOINTS (b)
  4072.         if (is_hardware_watchpoint (b))
  4073.           {
  4074.             struct watchpoint *w = (struct watchpoint *) b;

  4075.             w->watchpoint_triggered = watch_triggered_unknown;
  4076.           }

  4077.       return 1;
  4078.     }

  4079.   /* The target could report the data address.  Mark watchpoints
  4080.      affected by this data address as triggered, and all others as not
  4081.      triggered.  */

  4082.   ALL_BREAKPOINTS (b)
  4083.     if (is_hardware_watchpoint (b))
  4084.       {
  4085.         struct watchpoint *w = (struct watchpoint *) b;
  4086.         struct bp_location *loc;

  4087.         w->watchpoint_triggered = watch_triggered_no;
  4088.         for (loc = b->loc; loc; loc = loc->next)
  4089.           {
  4090.             if (is_masked_watchpoint (b))
  4091.               {
  4092.                 CORE_ADDR newaddr = addr & w->hw_wp_mask;
  4093.                 CORE_ADDR start = loc->address & w->hw_wp_mask;

  4094.                 if (newaddr == start)
  4095.                   {
  4096.                     w->watchpoint_triggered = watch_triggered_yes;
  4097.                     break;
  4098.                   }
  4099.               }
  4100.             /* Exact match not required.  Within range is sufficient.  */
  4101.             else if (target_watchpoint_addr_within_range (&current_target,
  4102.                                                          addr, loc->address,
  4103.                                                          loc->length))
  4104.               {
  4105.                 w->watchpoint_triggered = watch_triggered_yes;
  4106.                 break;
  4107.               }
  4108.           }
  4109.       }

  4110.   return 1;
  4111. }

  4112. /* Possible return values for watchpoint_check (this can't be an enum
  4113.    because of check_errors).  */
  4114. /* The watchpoint has been deleted.  */
  4115. #define WP_DELETED 1
  4116. /* The value has changed.  */
  4117. #define WP_VALUE_CHANGED 2
  4118. /* The value has not changed.  */
  4119. #define WP_VALUE_NOT_CHANGED 3
  4120. /* Ignore this watchpoint, no matter if the value changed or not.  */
  4121. #define WP_IGNORE 4

  4122. #define BP_TEMPFLAG 1
  4123. #define BP_HARDWAREFLAG 2

  4124. /* Evaluate watchpoint condition expression and check if its value
  4125.    changed.

  4126.    P should be a pointer to struct bpstat, but is defined as a void *
  4127.    in order for this function to be usable with catch_errors.  */

  4128. static int
  4129. watchpoint_check (void *p)
  4130. {
  4131.   bpstat bs = (bpstat) p;
  4132.   struct watchpoint *b;
  4133.   struct frame_info *fr;
  4134.   int within_current_scope;

  4135.   /* BS is built from an existing struct breakpoint.  */
  4136.   gdb_assert (bs->breakpoint_at != NULL);
  4137.   b = (struct watchpoint *) bs->breakpoint_at;

  4138.   /* If this is a local watchpoint, we only want to check if the
  4139.      watchpoint frame is in scope if the current thread is the thread
  4140.      that was used to create the watchpoint.  */
  4141.   if (!watchpoint_in_thread_scope (b))
  4142.     return WP_IGNORE;

  4143.   if (b->exp_valid_block == NULL)
  4144.     within_current_scope = 1;
  4145.   else
  4146.     {
  4147.       struct frame_info *frame = get_current_frame ();
  4148.       struct gdbarch *frame_arch = get_frame_arch (frame);
  4149.       CORE_ADDR frame_pc = get_frame_pc (frame);

  4150.       /* in_function_epilogue_p() returns a non-zero value if we're
  4151.          still in the function but the stack frame has already been
  4152.          invalidated.  Since we can't rely on the values of local
  4153.          variables after the stack has been destroyed, we are treating
  4154.          the watchpoint in that state as `not changed' without further
  4155.          checking.  Don't mark watchpoints as changed if the current
  4156.          frame is in an epilogue - even if they are in some other
  4157.          frame, our view of the stack is likely to be wrong and
  4158.          frame_find_by_id could error out.  */
  4159.       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
  4160.         return WP_IGNORE;

  4161.       fr = frame_find_by_id (b->watchpoint_frame);
  4162.       within_current_scope = (fr != NULL);

  4163.       /* If we've gotten confused in the unwinder, we might have
  4164.          returned a frame that can't describe this variable.  */
  4165.       if (within_current_scope)
  4166.         {
  4167.           struct symbol *function;

  4168.           function = get_frame_function (fr);
  4169.           if (function == NULL
  4170.               || !contained_in (b->exp_valid_block,
  4171.                                 SYMBOL_BLOCK_VALUE (function)))
  4172.             within_current_scope = 0;
  4173.         }

  4174.       if (within_current_scope)
  4175.         /* If we end up stopping, the current frame will get selected
  4176.            in normal_stop.  So this call to select_frame won't affect
  4177.            the user.  */
  4178.         select_frame (fr);
  4179.     }

  4180.   if (within_current_scope)
  4181.     {
  4182.       /* We use value_{,free_to_}mark because it could be a *long*
  4183.          time before we return to the command level and call
  4184.          free_all_values.  We can't call free_all_values because we
  4185.          might be in the middle of evaluating a function call.  */

  4186.       int pc = 0;
  4187.       struct value *mark;
  4188.       struct value *new_val;

  4189.       if (is_masked_watchpoint (&b->base))
  4190.         /* Since we don't know the exact trigger address (from
  4191.            stopped_data_address), just tell the user we've triggered
  4192.            a mask watchpoint.  */
  4193.         return WP_VALUE_CHANGED;

  4194.       mark = value_mark ();
  4195.       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);

  4196.       if (b->val_bitsize != 0)
  4197.         new_val = extract_bitfield_from_watchpoint_value (b, new_val);

  4198.       /* We use value_equal_contents instead of value_equal because
  4199.          the latter coerces an array to a pointer, thus comparing just
  4200.          the address of the array instead of its contents.  This is
  4201.          not what we want.  */
  4202.       if ((b->val != NULL) != (new_val != NULL)
  4203.           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
  4204.         {
  4205.           if (new_val != NULL)
  4206.             {
  4207.               release_value (new_val);
  4208.               value_free_to_mark (mark);
  4209.             }
  4210.           bs->old_val = b->val;
  4211.           b->val = new_val;
  4212.           b->val_valid = 1;
  4213.           return WP_VALUE_CHANGED;
  4214.         }
  4215.       else
  4216.         {
  4217.           /* Nothing changed.  */
  4218.           value_free_to_mark (mark);
  4219.           return WP_VALUE_NOT_CHANGED;
  4220.         }
  4221.     }
  4222.   else
  4223.     {
  4224.       struct ui_out *uiout = current_uiout;

  4225.       /* This seems like the only logical thing to do because
  4226.          if we temporarily ignored the watchpoint, then when
  4227.          we reenter the block in which it is valid it contains
  4228.          garbage (in the case of a function, it may have two
  4229.          garbage values, one before and one after the prologue).
  4230.          So we can't even detect the first assignment to it and
  4231.          watch after that (since the garbage may or may not equal
  4232.          the first value assigned).  */
  4233.       /* We print all the stop information in
  4234.          breakpoint_ops->print_it, but in this case, by the time we
  4235.          call breakpoint_ops->print_it this bp will be deleted
  4236.          already.  So we have no choice but print the information
  4237.          here.  */
  4238.       if (ui_out_is_mi_like_p (uiout))
  4239.         ui_out_field_string
  4240.           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
  4241.       ui_out_text (uiout, "\nWatchpoint ");
  4242.       ui_out_field_int (uiout, "wpnum", b->base.number);
  4243.       ui_out_text (uiout,
  4244.                    " deleted because the program has left the block in\n\
  4245. which its expression is valid.\n");

  4246.       /* Make sure the watchpoint's commands aren't executed.  */
  4247.       decref_counted_command_line (&b->base.commands);
  4248.       watchpoint_del_at_next_stop (b);

  4249.       return WP_DELETED;
  4250.     }
  4251. }

  4252. /* Return true if it looks like target has stopped due to hitting
  4253.    breakpoint location BL.  This function does not check if we should
  4254.    stop, only if BL explains the stop.  */

  4255. static int
  4256. bpstat_check_location (const struct bp_location *bl,
  4257.                        struct address_space *aspace, CORE_ADDR bp_addr,
  4258.                        const struct target_waitstatus *ws)
  4259. {
  4260.   struct breakpoint *b = bl->owner;

  4261.   /* BL is from an existing breakpoint.  */
  4262.   gdb_assert (b != NULL);

  4263.   return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
  4264. }

  4265. /* Determine if the watched values have actually changed, and we
  4266.    should stop.  If not, set BS->stop to 0.  */

  4267. static void
  4268. bpstat_check_watchpoint (bpstat bs)
  4269. {
  4270.   const struct bp_location *bl;
  4271.   struct watchpoint *b;

  4272.   /* BS is built for existing struct breakpoint.  */
  4273.   bl = bs->bp_location_at;
  4274.   gdb_assert (bl != NULL);
  4275.   b = (struct watchpoint *) bs->breakpoint_at;
  4276.   gdb_assert (b != NULL);

  4277.     {
  4278.       int must_check_value = 0;

  4279.       if (b->base.type == bp_watchpoint)
  4280.         /* For a software watchpoint, we must always check the
  4281.            watched value.  */
  4282.         must_check_value = 1;
  4283.       else if (b->watchpoint_triggered == watch_triggered_yes)
  4284.         /* We have a hardware watchpoint (read, write, or access)
  4285.            and the target earlier reported an address watched by
  4286.            this watchpoint.  */
  4287.         must_check_value = 1;
  4288.       else if (b->watchpoint_triggered == watch_triggered_unknown
  4289.                && b->base.type == bp_hardware_watchpoint)
  4290.         /* We were stopped by a hardware watchpoint, but the target could
  4291.            not report the data address.  We must check the watchpoint's
  4292.            value.  Access and read watchpoints are out of luck; without
  4293.            a data address, we can't figure it out.  */
  4294.         must_check_value = 1;

  4295.       if (must_check_value)
  4296.         {
  4297.           char *message
  4298.             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
  4299.                           b->base.number);
  4300.           struct cleanup *cleanups = make_cleanup (xfree, message);
  4301.           int e = catch_errors (watchpoint_check, bs, message,
  4302.                                 RETURN_MASK_ALL);
  4303.           do_cleanups (cleanups);
  4304.           switch (e)
  4305.             {
  4306.             case WP_DELETED:
  4307.               /* We've already printed what needs to be printed.  */
  4308.               bs->print_it = print_it_done;
  4309.               /* Stop.  */
  4310.               break;
  4311.             case WP_IGNORE:
  4312.               bs->print_it = print_it_noop;
  4313.               bs->stop = 0;
  4314.               break;
  4315.             case WP_VALUE_CHANGED:
  4316.               if (b->base.type == bp_read_watchpoint)
  4317.                 {
  4318.                   /* There are two cases to consider here:

  4319.                      1. We're watching the triggered memory for reads.
  4320.                      In that case, trust the target, and always report
  4321.                      the watchpoint hit to the user.  Even though
  4322.                      reads don't cause value changes, the value may
  4323.                      have changed since the last time it was read, and
  4324.                      since we're not trapping writes, we will not see
  4325.                      those, and as such we should ignore our notion of
  4326.                      old value.

  4327.                      2. We're watching the triggered memory for both
  4328.                      reads and writes.  There are two ways this may
  4329.                      happen:

  4330.                      2.1. This is a target that can't break on data
  4331.                      reads only, but can break on accesses (reads or
  4332.                      writes), such as e.g., x86.  We detect this case
  4333.                      at the time we try to insert read watchpoints.

  4334.                      2.2. Otherwise, the target supports read
  4335.                      watchpoints, but, the user set an access or write
  4336.                      watchpoint watching the same memory as this read
  4337.                      watchpoint.

  4338.                      If we're watching memory writes as well as reads,
  4339.                      ignore watchpoint hits when we find that the
  4340.                      value hasn't changed, as reads don't cause
  4341.                      changes.  This still gives false positives when
  4342.                      the program writes the same value to memory as
  4343.                      what there was already in memory (we will confuse
  4344.                      it for a read), but it's much better than
  4345.                      nothing.  */

  4346.                   int other_write_watchpoint = 0;

  4347.                   if (bl->watchpoint_type == hw_read)
  4348.                     {
  4349.                       struct breakpoint *other_b;

  4350.                       ALL_BREAKPOINTS (other_b)
  4351.                         if (other_b->type == bp_hardware_watchpoint
  4352.                             || other_b->type == bp_access_watchpoint)
  4353.                           {
  4354.                             struct watchpoint *other_w =
  4355.                               (struct watchpoint *) other_b;

  4356.                             if (other_w->watchpoint_triggered
  4357.                                 == watch_triggered_yes)
  4358.                               {
  4359.                                 other_write_watchpoint = 1;
  4360.                                 break;
  4361.                               }
  4362.                           }
  4363.                     }

  4364.                   if (other_write_watchpoint
  4365.                       || bl->watchpoint_type == hw_access)
  4366.                     {
  4367.                       /* We're watching the same memory for writes,
  4368.                          and the value changed since the last time we
  4369.                          updated it, so this trap must be for a write.
  4370.                          Ignore it.  */
  4371.                       bs->print_it = print_it_noop;
  4372.                       bs->stop = 0;
  4373.                     }
  4374.                 }
  4375.               break;
  4376.             case WP_VALUE_NOT_CHANGED:
  4377.               if (b->base.type == bp_hardware_watchpoint
  4378.                   || b->base.type == bp_watchpoint)
  4379.                 {
  4380.                   /* Don't stop: write watchpoints shouldn't fire if
  4381.                      the value hasn't changed.  */
  4382.                   bs->print_it = print_it_noop;
  4383.                   bs->stop = 0;
  4384.                 }
  4385.               /* Stop.  */
  4386.               break;
  4387.             default:
  4388.               /* Can't happen.  */
  4389.             case 0:
  4390.               /* Error from catch_errors.  */
  4391.               printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
  4392.               watchpoint_del_at_next_stop (b);
  4393.               /* We've already printed what needs to be printed.  */
  4394.               bs->print_it = print_it_done;
  4395.               break;
  4396.             }
  4397.         }
  4398.       else        /* must_check_value == 0 */
  4399.         {
  4400.           /* This is a case where some watchpoint(s) triggered, but
  4401.              not at the address of this watchpoint, or else no
  4402.              watchpoint triggered after all.  So don't print
  4403.              anything for this watchpoint.  */
  4404.           bs->print_it = print_it_noop;
  4405.           bs->stop = 0;
  4406.         }
  4407.     }
  4408. }

  4409. /* For breakpoints that are currently marked as telling gdb to stop,
  4410.    check conditions (condition proper, frame, thread and ignore count)
  4411.    of breakpoint referred to by BS.  If we should not stop for this
  4412.    breakpoint, set BS->stop to 0.  */

  4413. static void
  4414. bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
  4415. {
  4416.   const struct bp_location *bl;
  4417.   struct breakpoint *b;
  4418.   int value_is_zero = 0;
  4419.   struct expression *cond;

  4420.   gdb_assert (bs->stop);

  4421.   /* BS is built for existing struct breakpoint.  */
  4422.   bl = bs->bp_location_at;
  4423.   gdb_assert (bl != NULL);
  4424.   b = bs->breakpoint_at;
  4425.   gdb_assert (b != NULL);

  4426.   /* Even if the target evaluated the condition on its end and notified GDB, we
  4427.      need to do so again since GDB does not know if we stopped due to a
  4428.      breakpoint or a single step breakpoint.  */

  4429.   if (frame_id_p (b->frame_id)
  4430.       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
  4431.     {
  4432.       bs->stop = 0;
  4433.       return;
  4434.     }

  4435.   /* If this is a thread/task-specific breakpoint, don't waste cpu
  4436.      evaluating the condition if this isn't the specified
  4437.      thread/task.  */
  4438.   if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
  4439.       || (b->task != 0 && b->task != ada_get_task_number (ptid)))

  4440.     {
  4441.       bs->stop = 0;
  4442.       return;
  4443.     }

  4444.   /* Evaluate extension language breakpoints that have a "stop" method
  4445.      implemented.  */
  4446.   bs->stop = breakpoint_ext_lang_cond_says_stop (b);

  4447.   if (is_watchpoint (b))
  4448.     {
  4449.       struct watchpoint *w = (struct watchpoint *) b;

  4450.       cond = w->cond_exp;
  4451.     }
  4452.   else
  4453.     cond = bl->cond;

  4454.   if (cond && b->disposition != disp_del_at_next_stop)
  4455.     {
  4456.       int within_current_scope = 1;
  4457.       struct watchpoint * w;

  4458.       /* We use value_mark and value_free_to_mark because it could
  4459.          be a long time before we return to the command level and
  4460.          call free_all_values.  We can't call free_all_values
  4461.          because we might be in the middle of evaluating a
  4462.          function call.  */
  4463.       struct value *mark = value_mark ();

  4464.       if (is_watchpoint (b))
  4465.         w = (struct watchpoint *) b;
  4466.       else
  4467.         w = NULL;

  4468.       /* Need to select the frame, with all that implies so that
  4469.          the conditions will have the right context.  Because we
  4470.          use the frame, we will not see an inlined function's
  4471.          variables when we arrive at a breakpoint at the start
  4472.          of the inlined function; the current frame will be the
  4473.          call site.  */
  4474.       if (w == NULL || w->cond_exp_valid_block == NULL)
  4475.         select_frame (get_current_frame ());
  4476.       else
  4477.         {
  4478.           struct frame_info *frame;

  4479.           /* For local watchpoint expressions, which particular
  4480.              instance of a local is being watched matters, so we
  4481.              keep track of the frame to evaluate the expression
  4482.              in.  To evaluate the condition however, it doesn't
  4483.              really matter which instantiation of the function
  4484.              where the condition makes sense triggers the
  4485.              watchpoint.  This allows an expression like "watch
  4486.              global if q > 10" set in `func', catch writes to
  4487.              global on all threads that call `func', or catch
  4488.              writes on all recursive calls of `func' by a single
  4489.              thread.  We simply always evaluate the condition in
  4490.              the innermost frame that's executing where it makes
  4491.              sense to evaluate the condition.  It seems
  4492.              intuitive.  */
  4493.           frame = block_innermost_frame (w->cond_exp_valid_block);
  4494.           if (frame != NULL)
  4495.             select_frame (frame);
  4496.           else
  4497.             within_current_scope = 0;
  4498.         }
  4499.       if (within_current_scope)
  4500.         value_is_zero
  4501.           = catch_errors (breakpoint_cond_eval, cond,
  4502.                           "Error in testing breakpoint condition:\n",
  4503.                           RETURN_MASK_ALL);
  4504.       else
  4505.         {
  4506.           warning (_("Watchpoint condition cannot be tested "
  4507.                      "in the current scope"));
  4508.           /* If we failed to set the right context for this
  4509.              watchpoint, unconditionally report it.  */
  4510.           value_is_zero = 0;
  4511.         }
  4512.       /* FIXME-someday, should give breakpoint #.  */
  4513.       value_free_to_mark (mark);
  4514.     }

  4515.   if (cond && value_is_zero)
  4516.     {
  4517.       bs->stop = 0;
  4518.     }
  4519.   else if (b->ignore_count > 0)
  4520.     {
  4521.       b->ignore_count--;
  4522.       bs->stop = 0;
  4523.       /* Increase the hit count even though we don't stop.  */
  4524.       ++(b->hit_count);
  4525.       observer_notify_breakpoint_modified (b);
  4526.     }
  4527. }


  4528. /* Get a bpstat associated with having just stopped at address
  4529.    BP_ADDR in thread PTID.

  4530.    Determine whether we stopped at a breakpoint, etc, or whether we
  4531.    don't understand this stop.  Result is a chain of bpstat's such
  4532.    that:

  4533.    if we don't understand the stop, the result is a null pointer.

  4534.    if we understand why we stopped, the result is not null.

  4535.    Each element of the chain refers to a particular breakpoint or
  4536.    watchpoint at which we have stopped.  (We may have stopped for
  4537.    several reasons concurrently.)

  4538.    Each element of the chain has valid next, breakpoint_at,
  4539.    commands, FIXME??? fields.  */

  4540. bpstat
  4541. bpstat_stop_status (struct address_space *aspace,
  4542.                     CORE_ADDR bp_addr, ptid_t ptid,
  4543.                     const struct target_waitstatus *ws)
  4544. {
  4545.   struct breakpoint *b = NULL;
  4546.   struct bp_location *bl;
  4547.   struct bp_location *loc;
  4548.   /* First item of allocated bpstat's.  */
  4549.   bpstat bs_head = NULL, *bs_link = &bs_head;
  4550.   /* Pointer to the last thing in the chain currently.  */
  4551.   bpstat bs;
  4552.   int ix;
  4553.   int need_remove_insert;
  4554.   int removed_any;

  4555.   /* First, build the bpstat chain with locations that explain a
  4556.      target stop, while being careful to not set the target running,
  4557.      as that may invalidate locations (in particular watchpoint
  4558.      locations are recreated).  Resuming will happen here with
  4559.      breakpoint conditions or watchpoint expressions that include
  4560.      inferior function calls.  */

  4561.   ALL_BREAKPOINTS (b)
  4562.     {
  4563.       if (!breakpoint_enabled (b))
  4564.         continue;

  4565.       for (bl = b->loc; bl != NULL; bl = bl->next)
  4566.         {
  4567.           /* For hardware watchpoints, we look only at the first
  4568.              location.  The watchpoint_check function will work on the
  4569.              entire expression, not the individual locations.  For
  4570.              read watchpoints, the watchpoints_triggered function has
  4571.              checked all locations already.  */
  4572.           if (b->type == bp_hardware_watchpoint && bl != b->loc)
  4573.             break;

  4574.           if (!bl->enabled || bl->shlib_disabled)
  4575.             continue;

  4576.           if (!bpstat_check_location (bl, aspace, bp_addr, ws))
  4577.             continue;

  4578.           /* Come here if it's a watchpoint, or if the break address
  4579.              matches.  */

  4580.           bs = bpstat_alloc (bl, &bs_link);        /* Alloc a bpstat to
  4581.                                                    explain stop.  */

  4582.           /* Assume we stop.  Should we find a watchpoint that is not
  4583.              actually triggered, or if the condition of the breakpoint
  4584.              evaluates as false, we'll reset 'stop' to 0.  */
  4585.           bs->stop = 1;
  4586.           bs->print = 1;

  4587.           /* If this is a scope breakpoint, mark the associated
  4588.              watchpoint as triggered so that we will handle the
  4589.              out-of-scope event.  We'll get to the watchpoint next
  4590.              iteration.  */
  4591.           if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
  4592.             {
  4593.               struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;

  4594.               w->watchpoint_triggered = watch_triggered_yes;
  4595.             }
  4596.         }
  4597.     }

  4598.   /* Check if a moribund breakpoint explains the stop.  */
  4599.   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
  4600.     {
  4601.       if (breakpoint_location_address_match (loc, aspace, bp_addr))
  4602.         {
  4603.           bs = bpstat_alloc (loc, &bs_link);
  4604.           /* For hits of moribund locations, we should just proceed.  */
  4605.           bs->stop = 0;
  4606.           bs->print = 0;
  4607.           bs->print_it = print_it_noop;
  4608.         }
  4609.     }

  4610.   /* A bit of special processing for shlib breakpoints.  We need to
  4611.      process solib loading here, so that the lists of loaded and
  4612.      unloaded libraries are correct before we handle "catch load" and
  4613.      "catch unload".  */
  4614.   for (bs = bs_head; bs != NULL; bs = bs->next)
  4615.     {
  4616.       if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
  4617.         {
  4618.           handle_solib_event ();
  4619.           break;
  4620.         }
  4621.     }

  4622.   /* Now go through the locations that caused the target to stop, and
  4623.      check whether we're interested in reporting this stop to higher
  4624.      layers, or whether we should resume the target transparently.  */

  4625.   removed_any = 0;

  4626.   for (bs = bs_head; bs != NULL; bs = bs->next)
  4627.     {
  4628.       if (!bs->stop)
  4629.         continue;

  4630.       b = bs->breakpoint_at;
  4631.       b->ops->check_status (bs);
  4632.       if (bs->stop)
  4633.         {
  4634.           bpstat_check_breakpoint_conditions (bs, ptid);

  4635.           if (bs->stop)
  4636.             {
  4637.               ++(b->hit_count);
  4638.               observer_notify_breakpoint_modified (b);

  4639.               /* We will stop here.  */
  4640.               if (b->disposition == disp_disable)
  4641.                 {
  4642.                   --(b->enable_count);
  4643.                   if (b->enable_count <= 0)
  4644.                     b->enable_state = bp_disabled;
  4645.                   removed_any = 1;
  4646.                 }
  4647.               if (b->silent)
  4648.                 bs->print = 0;
  4649.               bs->commands = b->commands;
  4650.               incref_counted_command_line (bs->commands);
  4651.               if (command_line_is_silent (bs->commands
  4652.                                           ? bs->commands->commands : NULL))
  4653.                 bs->print = 0;

  4654.               b->ops->after_condition_true (bs);
  4655.             }

  4656.         }

  4657.       /* Print nothing for this entry if we don't stop or don't
  4658.          print.  */
  4659.       if (!bs->stop || !bs->print)
  4660.         bs->print_it = print_it_noop;
  4661.     }

  4662.   /* If we aren't stopping, the value of some hardware watchpoint may
  4663.      not have changed, but the intermediate memory locations we are
  4664.      watching may have.  Don't bother if we're stopping; this will get
  4665.      done later.  */
  4666.   need_remove_insert = 0;
  4667.   if (! bpstat_causes_stop (bs_head))
  4668.     for (bs = bs_head; bs != NULL; bs = bs->next)
  4669.       if (!bs->stop
  4670.           && bs->breakpoint_at
  4671.           && is_hardware_watchpoint (bs->breakpoint_at))
  4672.         {
  4673.           struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;

  4674.           update_watchpoint (w, 0 /* don't reparse.  */);
  4675.           need_remove_insert = 1;
  4676.         }

  4677.   if (need_remove_insert)
  4678.     update_global_location_list (UGLL_MAY_INSERT);
  4679.   else if (removed_any)
  4680.     update_global_location_list (UGLL_DONT_INSERT);

  4681.   return bs_head;
  4682. }

  4683. static void
  4684. handle_jit_event (void)
  4685. {
  4686.   struct frame_info *frame;
  4687.   struct gdbarch *gdbarch;

  4688.   /* Switch terminal for any messages produced by
  4689.      breakpoint_re_set.  */
  4690.   target_terminal_ours_for_output ();

  4691.   frame = get_current_frame ();
  4692.   gdbarch = get_frame_arch (frame);

  4693.   jit_event_handler (gdbarch);

  4694.   target_terminal_inferior ();
  4695. }

  4696. /* Prepare WHAT final decision for infrun.  */

  4697. /* Decide what infrun needs to do with this bpstat.  */

  4698. struct bpstat_what
  4699. bpstat_what (bpstat bs_head)
  4700. {
  4701.   struct bpstat_what retval;
  4702.   int jit_event = 0;
  4703.   bpstat bs;

  4704.   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
  4705.   retval.call_dummy = STOP_NONE;
  4706.   retval.is_longjmp = 0;

  4707.   for (bs = bs_head; bs != NULL; bs = bs->next)
  4708.     {
  4709.       /* Extract this BS's action.  After processing each BS, we check
  4710.          if its action overrides all we've seem so far.  */
  4711.       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
  4712.       enum bptype bptype;

  4713.       if (bs->breakpoint_at == NULL)
  4714.         {
  4715.           /* I suspect this can happen if it was a momentary
  4716.              breakpoint which has since been deleted.  */
  4717.           bptype = bp_none;
  4718.         }
  4719.       else
  4720.         bptype = bs->breakpoint_at->type;

  4721.       switch (bptype)
  4722.         {
  4723.         case bp_none:
  4724.           break;
  4725.         case bp_breakpoint:
  4726.         case bp_hardware_breakpoint:
  4727.         case bp_single_step:
  4728.         case bp_until:
  4729.         case bp_finish:
  4730.         case bp_shlib_event:
  4731.           if (bs->stop)
  4732.             {
  4733.               if (bs->print)
  4734.                 this_action = BPSTAT_WHAT_STOP_NOISY;
  4735.               else
  4736.                 this_action = BPSTAT_WHAT_STOP_SILENT;
  4737.             }
  4738.           else
  4739.             this_action = BPSTAT_WHAT_SINGLE;
  4740.           break;
  4741.         case bp_watchpoint:
  4742.         case bp_hardware_watchpoint:
  4743.         case bp_read_watchpoint:
  4744.         case bp_access_watchpoint:
  4745.           if (bs->stop)
  4746.             {
  4747.               if (bs->print)
  4748.                 this_action = BPSTAT_WHAT_STOP_NOISY;
  4749.               else
  4750.                 this_action = BPSTAT_WHAT_STOP_SILENT;
  4751.             }
  4752.           else
  4753.             {
  4754.               /* There was a watchpoint, but we're not stopping.
  4755.                  This requires no further action.  */
  4756.             }
  4757.           break;
  4758.         case bp_longjmp:
  4759.         case bp_longjmp_call_dummy:
  4760.         case bp_exception:
  4761.           this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
  4762.           retval.is_longjmp = bptype != bp_exception;
  4763.           break;
  4764.         case bp_longjmp_resume:
  4765.         case bp_exception_resume:
  4766.           this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
  4767.           retval.is_longjmp = bptype == bp_longjmp_resume;
  4768.           break;
  4769.         case bp_step_resume:
  4770.           if (bs->stop)
  4771.             this_action = BPSTAT_WHAT_STEP_RESUME;
  4772.           else
  4773.             {
  4774.               /* It is for the wrong frame.  */
  4775.               this_action = BPSTAT_WHAT_SINGLE;
  4776.             }
  4777.           break;
  4778.         case bp_hp_step_resume:
  4779.           if (bs->stop)
  4780.             this_action = BPSTAT_WHAT_HP_STEP_RESUME;
  4781.           else
  4782.             {
  4783.               /* It is for the wrong frame.  */
  4784.               this_action = BPSTAT_WHAT_SINGLE;
  4785.             }
  4786.           break;
  4787.         case bp_watchpoint_scope:
  4788.         case bp_thread_event:
  4789.         case bp_overlay_event:
  4790.         case bp_longjmp_master:
  4791.         case bp_std_terminate_master:
  4792.         case bp_exception_master:
  4793.           this_action = BPSTAT_WHAT_SINGLE;
  4794.           break;
  4795.         case bp_catchpoint:
  4796.           if (bs->stop)
  4797.             {
  4798.               if (bs->print)
  4799.                 this_action = BPSTAT_WHAT_STOP_NOISY;
  4800.               else
  4801.                 this_action = BPSTAT_WHAT_STOP_SILENT;
  4802.             }
  4803.           else
  4804.             {
  4805.               /* There was a catchpoint, but we're not stopping.
  4806.                  This requires no further action.  */
  4807.             }
  4808.           break;
  4809.         case bp_jit_event:
  4810.           jit_event = 1;
  4811.           this_action = BPSTAT_WHAT_SINGLE;
  4812.           break;
  4813.         case bp_call_dummy:
  4814.           /* Make sure the action is stop (silent or noisy),
  4815.              so infrun.c pops the dummy frame.  */
  4816.           retval.call_dummy = STOP_STACK_DUMMY;
  4817.           this_action = BPSTAT_WHAT_STOP_SILENT;
  4818.           break;
  4819.         case bp_std_terminate:
  4820.           /* Make sure the action is stop (silent or noisy),
  4821.              so infrun.c pops the dummy frame.  */
  4822.           retval.call_dummy = STOP_STD_TERMINATE;
  4823.           this_action = BPSTAT_WHAT_STOP_SILENT;
  4824.           break;
  4825.         case bp_tracepoint:
  4826.         case bp_fast_tracepoint:
  4827.         case bp_static_tracepoint:
  4828.           /* Tracepoint hits should not be reported back to GDB, and
  4829.              if one got through somehow, it should have been filtered
  4830.              out already.  */
  4831.           internal_error (__FILE__, __LINE__,
  4832.                           _("bpstat_what: tracepoint encountered"));
  4833.           break;
  4834.         case bp_gnu_ifunc_resolver:
  4835.           /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
  4836.           this_action = BPSTAT_WHAT_SINGLE;
  4837.           break;
  4838.         case bp_gnu_ifunc_resolver_return:
  4839.           /* The breakpoint will be removed, execution will restart from the
  4840.              PC of the former breakpoint.  */
  4841.           this_action = BPSTAT_WHAT_KEEP_CHECKING;
  4842.           break;

  4843.         case bp_dprintf:
  4844.           if (bs->stop)
  4845.             this_action = BPSTAT_WHAT_STOP_SILENT;
  4846.           else
  4847.             this_action = BPSTAT_WHAT_SINGLE;
  4848.           break;

  4849.         default:
  4850.           internal_error (__FILE__, __LINE__,
  4851.                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
  4852.         }

  4853.       retval.main_action = max (retval.main_action, this_action);
  4854.     }

  4855.   /* These operations may affect the bs->breakpoint_at state so they are
  4856.      delayed after MAIN_ACTION is decided above.  */

  4857.   if (jit_event)
  4858.     {
  4859.       if (debug_infrun)
  4860.         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");

  4861.       handle_jit_event ();
  4862.     }

  4863.   for (bs = bs_head; bs != NULL; bs = bs->next)
  4864.     {
  4865.       struct breakpoint *b = bs->breakpoint_at;

  4866.       if (b == NULL)
  4867.         continue;
  4868.       switch (b->type)
  4869.         {
  4870.         case bp_gnu_ifunc_resolver:
  4871.           gnu_ifunc_resolver_stop (b);
  4872.           break;
  4873.         case bp_gnu_ifunc_resolver_return:
  4874.           gnu_ifunc_resolver_return_stop (b);
  4875.           break;
  4876.         }
  4877.     }

  4878.   return retval;
  4879. }

  4880. /* Nonzero if we should step constantly (e.g. watchpoints on machines
  4881.    without hardware support).  This isn't related to a specific bpstat,
  4882.    just to things like whether watchpoints are set.  */

  4883. int
  4884. bpstat_should_step (void)
  4885. {
  4886.   struct breakpoint *b;

  4887.   ALL_BREAKPOINTS (b)
  4888.     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
  4889.       return 1;
  4890.   return 0;
  4891. }

  4892. int
  4893. bpstat_causes_stop (bpstat bs)
  4894. {
  4895.   for (; bs != NULL; bs = bs->next)
  4896.     if (bs->stop)
  4897.       return 1;

  4898.   return 0;
  4899. }



  4900. /* Compute a string of spaces suitable to indent the next line
  4901.    so it starts at the position corresponding to the table column
  4902.    named COL_NAME in the currently active table of UIOUT.  */

  4903. static char *
  4904. wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
  4905. {
  4906.   static char wrap_indent[80];
  4907.   int i, total_width, width, align;
  4908.   char *text;

  4909.   total_width = 0;
  4910.   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
  4911.     {
  4912.       if (strcmp (text, col_name) == 0)
  4913.         {
  4914.           gdb_assert (total_width < sizeof wrap_indent);
  4915.           memset (wrap_indent, ' ', total_width);
  4916.           wrap_indent[total_width] = 0;

  4917.           return wrap_indent;
  4918.         }

  4919.       total_width += width + 1;
  4920.     }

  4921.   return NULL;
  4922. }

  4923. /* Determine if the locations of this breakpoint will have their conditions
  4924.    evaluated by the target, host or a mix of both.  Returns the following:

  4925.     "host": Host evals condition.
  4926.     "host or target": Host or Target evals condition.
  4927.     "target": Target evals condition.
  4928. */

  4929. static const char *
  4930. bp_condition_evaluator (struct breakpoint *b)
  4931. {
  4932.   struct bp_location *bl;
  4933.   char host_evals = 0;
  4934.   char target_evals = 0;

  4935.   if (!b)
  4936.     return NULL;

  4937.   if (!is_breakpoint (b))
  4938.     return NULL;

  4939.   if (gdb_evaluates_breakpoint_condition_p ()
  4940.       || !target_supports_evaluation_of_breakpoint_conditions ())
  4941.     return condition_evaluation_host;

  4942.   for (bl = b->loc; bl; bl = bl->next)
  4943.     {
  4944.       if (bl->cond_bytecode)
  4945.         target_evals++;
  4946.       else
  4947.         host_evals++;
  4948.     }

  4949.   if (host_evals && target_evals)
  4950.     return condition_evaluation_both;
  4951.   else if (target_evals)
  4952.     return condition_evaluation_target;
  4953.   else
  4954.     return condition_evaluation_host;
  4955. }

  4956. /* Determine the breakpoint location's condition evaluator.  This is
  4957.    similar to bp_condition_evaluator, but for locations.  */

  4958. static const char *
  4959. bp_location_condition_evaluator (struct bp_location *bl)
  4960. {
  4961.   if (bl && !is_breakpoint (bl->owner))
  4962.     return NULL;

  4963.   if (gdb_evaluates_breakpoint_condition_p ()
  4964.       || !target_supports_evaluation_of_breakpoint_conditions ())
  4965.     return condition_evaluation_host;

  4966.   if (bl && bl->cond_bytecode)
  4967.     return condition_evaluation_target;
  4968.   else
  4969.     return condition_evaluation_host;
  4970. }

  4971. /* Print the LOC location out of the list of B->LOC locations.  */

  4972. static void
  4973. print_breakpoint_location (struct breakpoint *b,
  4974.                            struct bp_location *loc)
  4975. {
  4976.   struct ui_out *uiout = current_uiout;
  4977.   struct cleanup *old_chain = save_current_program_space ();

  4978.   if (loc != NULL && loc->shlib_disabled)
  4979.     loc = NULL;

  4980.   if (loc != NULL)
  4981.     set_current_program_space (loc->pspace);

  4982.   if (b->display_canonical)
  4983.     ui_out_field_string (uiout, "what", b->addr_string);
  4984.   else if (loc && loc->symtab)
  4985.     {
  4986.       struct symbol *sym
  4987.         = find_pc_sect_function (loc->address, loc->section);
  4988.       if (sym)
  4989.         {
  4990.           ui_out_text (uiout, "in ");
  4991.           ui_out_field_string (uiout, "func",
  4992.                                SYMBOL_PRINT_NAME (sym));
  4993.           ui_out_text (uiout, " ");
  4994.           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
  4995.           ui_out_text (uiout, "at ");
  4996.         }
  4997.       ui_out_field_string (uiout, "file",
  4998.                            symtab_to_filename_for_display (loc->symtab));
  4999.       ui_out_text (uiout, ":");

  5000.       if (ui_out_is_mi_like_p (uiout))
  5001.         ui_out_field_string (uiout, "fullname",
  5002.                              symtab_to_fullname (loc->symtab));

  5003.       ui_out_field_int (uiout, "line", loc->line_number);
  5004.     }
  5005.   else if (loc)
  5006.     {
  5007.       struct ui_file *stb = mem_fileopen ();
  5008.       struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);

  5009.       print_address_symbolic (loc->gdbarch, loc->address, stb,
  5010.                               demangle, "");
  5011.       ui_out_field_stream (uiout, "at", stb);

  5012.       do_cleanups (stb_chain);
  5013.     }
  5014.   else
  5015.     ui_out_field_string (uiout, "pending", b->addr_string);

  5016.   if (loc && is_breakpoint (b)
  5017.       && breakpoint_condition_evaluation_mode () == condition_evaluation_target
  5018.       && bp_condition_evaluator (b) == condition_evaluation_both)
  5019.     {
  5020.       ui_out_text (uiout, " (");
  5021.       ui_out_field_string (uiout, "evaluated-by",
  5022.                            bp_location_condition_evaluator (loc));
  5023.       ui_out_text (uiout, ")");
  5024.     }

  5025.   do_cleanups (old_chain);
  5026. }

  5027. static const char *
  5028. bptype_string (enum bptype type)
  5029. {
  5030.   struct ep_type_description
  5031.     {
  5032.       enum bptype type;
  5033.       char *description;
  5034.     };
  5035.   static struct ep_type_description bptypes[] =
  5036.   {
  5037.     {bp_none, "?deleted?"},
  5038.     {bp_breakpoint, "breakpoint"},
  5039.     {bp_hardware_breakpoint, "hw breakpoint"},
  5040.     {bp_single_step, "sw single-step"},
  5041.     {bp_until, "until"},
  5042.     {bp_finish, "finish"},
  5043.     {bp_watchpoint, "watchpoint"},
  5044.     {bp_hardware_watchpoint, "hw watchpoint"},
  5045.     {bp_read_watchpoint, "read watchpoint"},
  5046.     {bp_access_watchpoint, "acc watchpoint"},
  5047.     {bp_longjmp, "longjmp"},
  5048.     {bp_longjmp_resume, "longjmp resume"},
  5049.     {bp_longjmp_call_dummy, "longjmp for call dummy"},
  5050.     {bp_exception, "exception"},
  5051.     {bp_exception_resume, "exception resume"},
  5052.     {bp_step_resume, "step resume"},
  5053.     {bp_hp_step_resume, "high-priority step resume"},
  5054.     {bp_watchpoint_scope, "watchpoint scope"},
  5055.     {bp_call_dummy, "call dummy"},
  5056.     {bp_std_terminate, "std::terminate"},
  5057.     {bp_shlib_event, "shlib events"},
  5058.     {bp_thread_event, "thread events"},
  5059.     {bp_overlay_event, "overlay events"},
  5060.     {bp_longjmp_master, "longjmp master"},
  5061.     {bp_std_terminate_master, "std::terminate master"},
  5062.     {bp_exception_master, "exception master"},
  5063.     {bp_catchpoint, "catchpoint"},
  5064.     {bp_tracepoint, "tracepoint"},
  5065.     {bp_fast_tracepoint, "fast tracepoint"},
  5066.     {bp_static_tracepoint, "static tracepoint"},
  5067.     {bp_dprintf, "dprintf"},
  5068.     {bp_jit_event, "jit events"},
  5069.     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
  5070.     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
  5071.   };

  5072.   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
  5073.       || ((int) type != bptypes[(int) type].type))
  5074.     internal_error (__FILE__, __LINE__,
  5075.                     _("bptypes table does not describe type #%d."),
  5076.                     (int) type);

  5077.   return bptypes[(int) type].description;
  5078. }

  5079. /* For MI, output a field named 'thread-groups' with a list as the value.
  5080.    For CLI, prefix the list with the string 'inf'. */

  5081. static void
  5082. output_thread_groups (struct ui_out *uiout,
  5083.                       const char *field_name,
  5084.                       VEC(int) *inf_num,
  5085.                       int mi_only)
  5086. {
  5087.   struct cleanup *back_to;
  5088.   int is_mi = ui_out_is_mi_like_p (uiout);
  5089.   int inf;
  5090.   int i;

  5091.   /* For backward compatibility, don't display inferiors in CLI unless
  5092.      there are several.  Always display them for MI. */
  5093.   if (!is_mi && mi_only)
  5094.     return;

  5095.   back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);

  5096.   for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
  5097.     {
  5098.       if (is_mi)
  5099.         {
  5100.           char mi_group[10];

  5101.           xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
  5102.           ui_out_field_string (uiout, NULL, mi_group);
  5103.         }
  5104.       else
  5105.         {
  5106.           if (i == 0)
  5107.             ui_out_text (uiout, " inf ");
  5108.           else
  5109.             ui_out_text (uiout, ", ");

  5110.           ui_out_text (uiout, plongest (inf));
  5111.         }
  5112.     }

  5113.   do_cleanups (back_to);
  5114. }

  5115. /* Print B to gdb_stdout.  */

  5116. static void
  5117. print_one_breakpoint_location (struct breakpoint *b,
  5118.                                struct bp_location *loc,
  5119.                                int loc_number,
  5120.                                struct bp_location **last_loc,
  5121.                                int allflag)
  5122. {
  5123.   struct command_line *l;
  5124.   static char bpenables[] = "nynny";

  5125.   struct ui_out *uiout = current_uiout;
  5126.   int header_of_multiple = 0;
  5127.   int part_of_multiple = (loc != NULL);
  5128.   struct value_print_options opts;

  5129.   get_user_print_options (&opts);

  5130.   gdb_assert (!loc || loc_number != 0);
  5131.   /* See comment in print_one_breakpoint concerning treatment of
  5132.      breakpoints with single disabled location.  */
  5133.   if (loc == NULL
  5134.       && (b->loc != NULL
  5135.           && (b->loc->next != NULL || !b->loc->enabled)))
  5136.     header_of_multiple = 1;
  5137.   if (loc == NULL)
  5138.     loc = b->loc;

  5139.   annotate_record ();

  5140.   /* 1 */
  5141.   annotate_field (0);
  5142.   if (part_of_multiple)
  5143.     {
  5144.       char *formatted;
  5145.       formatted = xstrprintf ("%d.%d", b->number, loc_number);
  5146.       ui_out_field_string (uiout, "number", formatted);
  5147.       xfree (formatted);
  5148.     }
  5149.   else
  5150.     {
  5151.       ui_out_field_int (uiout, "number", b->number);
  5152.     }

  5153.   /* 2 */
  5154.   annotate_field (1);
  5155.   if (part_of_multiple)
  5156.     ui_out_field_skip (uiout, "type");
  5157.   else
  5158.     ui_out_field_string (uiout, "type", bptype_string (b->type));

  5159.   /* 3 */
  5160.   annotate_field (2);
  5161.   if (part_of_multiple)
  5162.     ui_out_field_skip (uiout, "disp");
  5163.   else
  5164.     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));


  5165.   /* 4 */
  5166.   annotate_field (3);
  5167.   if (part_of_multiple)
  5168.     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
  5169.   else
  5170.     ui_out_field_fmt (uiout, "enabled", "%c",
  5171.                       bpenables[(int) b->enable_state]);
  5172.   ui_out_spaces (uiout, 2);


  5173.   /* 5 and 6 */
  5174.   if (b->ops != NULL && b->ops->print_one != NULL)
  5175.     {
  5176.       /* Although the print_one can possibly print all locations,
  5177.          calling it here is not likely to get any nice result.  So,
  5178.          make sure there's just one location.  */
  5179.       gdb_assert (b->loc == NULL || b->loc->next == NULL);
  5180.       b->ops->print_one (b, last_loc);
  5181.     }
  5182.   else
  5183.     switch (b->type)
  5184.       {
  5185.       case bp_none:
  5186.         internal_error (__FILE__, __LINE__,
  5187.                         _("print_one_breakpoint: bp_none encountered\n"));
  5188.         break;

  5189.       case bp_watchpoint:
  5190.       case bp_hardware_watchpoint:
  5191.       case bp_read_watchpoint:
  5192.       case bp_access_watchpoint:
  5193.         {
  5194.           struct watchpoint *w = (struct watchpoint *) b;

  5195.           /* Field 4, the address, is omitted (which makes the columns
  5196.              not line up too nicely with the headers, but the effect
  5197.              is relatively readable).  */
  5198.           if (opts.addressprint)
  5199.             ui_out_field_skip (uiout, "addr");
  5200.           annotate_field (5);
  5201.           ui_out_field_string (uiout, "what", w->exp_string);
  5202.         }
  5203.         break;

  5204.       case bp_breakpoint:
  5205.       case bp_hardware_breakpoint:
  5206.       case bp_single_step:
  5207.       case bp_until:
  5208.       case bp_finish:
  5209.       case bp_longjmp:
  5210.       case bp_longjmp_resume:
  5211.       case bp_longjmp_call_dummy:
  5212.       case bp_exception:
  5213.       case bp_exception_resume:
  5214.       case bp_step_resume:
  5215.       case bp_hp_step_resume:
  5216.       case bp_watchpoint_scope:
  5217.       case bp_call_dummy:
  5218.       case bp_std_terminate:
  5219.       case bp_shlib_event:
  5220.       case bp_thread_event:
  5221.       case bp_overlay_event:
  5222.       case bp_longjmp_master:
  5223.       case bp_std_terminate_master:
  5224.       case bp_exception_master:
  5225.       case bp_tracepoint:
  5226.       case bp_fast_tracepoint:
  5227.       case bp_static_tracepoint:
  5228.       case bp_dprintf:
  5229.       case bp_jit_event:
  5230.       case bp_gnu_ifunc_resolver:
  5231.       case bp_gnu_ifunc_resolver_return:
  5232.         if (opts.addressprint)
  5233.           {
  5234.             annotate_field (4);
  5235.             if (header_of_multiple)
  5236.               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
  5237.             else if (b->loc == NULL || loc->shlib_disabled)
  5238.               ui_out_field_string (uiout, "addr", "<PENDING>");
  5239.             else
  5240.               ui_out_field_core_addr (uiout, "addr",
  5241.                                       loc->gdbarch, loc->address);
  5242.           }
  5243.         annotate_field (5);
  5244.         if (!header_of_multiple)
  5245.           print_breakpoint_location (b, loc);
  5246.         if (b->loc)
  5247.           *last_loc = b->loc;
  5248.         break;
  5249.       }


  5250.   if (loc != NULL && !header_of_multiple)
  5251.     {
  5252.       struct inferior *inf;
  5253.       VEC(int) *inf_num = NULL;
  5254.       int mi_only = 1;

  5255.       ALL_INFERIORS (inf)
  5256.         {
  5257.           if (inf->pspace == loc->pspace)
  5258.             VEC_safe_push (int, inf_num, inf->num);
  5259.         }

  5260.         /* For backward compatibility, don't display inferiors in CLI unless
  5261.            there are several.  Always display for MI. */
  5262.         if (allflag
  5263.             || (!gdbarch_has_global_breakpoints (target_gdbarch ())
  5264.                 && (number_of_program_spaces () > 1
  5265.                     || number_of_inferiors () > 1)
  5266.                 /* LOC is for existing B, it cannot be in
  5267.                    moribund_locations and thus having NULL OWNER.  */
  5268.                 && loc->owner->type != bp_catchpoint))
  5269.         mi_only = 0;
  5270.       output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
  5271.       VEC_free (int, inf_num);
  5272.     }

  5273.   if (!part_of_multiple)
  5274.     {
  5275.       if (b->thread != -1)
  5276.         {
  5277.           /* FIXME: This seems to be redundant and lost here; see the
  5278.              "stop only in" line a little further down.  */
  5279.           ui_out_text (uiout, " thread ");
  5280.           ui_out_field_int (uiout, "thread", b->thread);
  5281.         }
  5282.       else if (b->task != 0)
  5283.         {
  5284.           ui_out_text (uiout, " task ");
  5285.           ui_out_field_int (uiout, "task", b->task);
  5286.         }
  5287.     }

  5288.   ui_out_text (uiout, "\n");

  5289.   if (!part_of_multiple)
  5290.     b->ops->print_one_detail (b, uiout);

  5291.   if (part_of_multiple && frame_id_p (b->frame_id))
  5292.     {
  5293.       annotate_field (6);
  5294.       ui_out_text (uiout, "\tstop only in stack frame at ");
  5295.       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
  5296.          the frame ID.  */
  5297.       ui_out_field_core_addr (uiout, "frame",
  5298.                               b->gdbarch, b->frame_id.stack_addr);
  5299.       ui_out_text (uiout, "\n");
  5300.     }

  5301.   if (!part_of_multiple && b->cond_string)
  5302.     {
  5303.       annotate_field (7);
  5304.       if (is_tracepoint (b))
  5305.         ui_out_text (uiout, "\ttrace only if ");
  5306.       else
  5307.         ui_out_text (uiout, "\tstop only if ");
  5308.       ui_out_field_string (uiout, "cond", b->cond_string);

  5309.       /* Print whether the target is doing the breakpoint's condition
  5310.          evaluation.  If GDB is doing the evaluation, don't print anything.  */
  5311.       if (is_breakpoint (b)
  5312.           && breakpoint_condition_evaluation_mode ()
  5313.           == condition_evaluation_target)
  5314.         {
  5315.           ui_out_text (uiout, " (");
  5316.           ui_out_field_string (uiout, "evaluated-by",
  5317.                                bp_condition_evaluator (b));
  5318.           ui_out_text (uiout, " evals)");
  5319.         }
  5320.       ui_out_text (uiout, "\n");
  5321.     }

  5322.   if (!part_of_multiple && b->thread != -1)
  5323.     {
  5324.       /* FIXME should make an annotation for this.  */
  5325.       ui_out_text (uiout, "\tstop only in thread ");
  5326.       ui_out_field_int (uiout, "thread", b->thread);
  5327.       ui_out_text (uiout, "\n");
  5328.     }

  5329.   if (!part_of_multiple)
  5330.     {
  5331.       if (b->hit_count)
  5332.         {
  5333.           /* FIXME should make an annotation for this.  */
  5334.           if (is_catchpoint (b))
  5335.             ui_out_text (uiout, "\tcatchpoint");
  5336.           else if (is_tracepoint (b))
  5337.             ui_out_text (uiout, "\ttracepoint");
  5338.           else
  5339.             ui_out_text (uiout, "\tbreakpoint");
  5340.           ui_out_text (uiout, " already hit ");
  5341.           ui_out_field_int (uiout, "times", b->hit_count);
  5342.           if (b->hit_count == 1)
  5343.             ui_out_text (uiout, " time\n");
  5344.           else
  5345.             ui_out_text (uiout, " times\n");
  5346.         }
  5347.       else
  5348.         {
  5349.           /* Output the count also if it is zero, but only if this is mi.  */
  5350.           if (ui_out_is_mi_like_p (uiout))
  5351.             ui_out_field_int (uiout, "times", b->hit_count);
  5352.         }
  5353.     }

  5354.   if (!part_of_multiple && b->ignore_count)
  5355.     {
  5356.       annotate_field (8);
  5357.       ui_out_text (uiout, "\tignore next ");
  5358.       ui_out_field_int (uiout, "ignore", b->ignore_count);
  5359.       ui_out_text (uiout, " hits\n");
  5360.     }

  5361.   /* Note that an enable count of 1 corresponds to "enable once"
  5362.      behavior, which is reported by the combination of enablement and
  5363.      disposition, so we don't need to mention it here.  */
  5364.   if (!part_of_multiple && b->enable_count > 1)
  5365.     {
  5366.       annotate_field (8);
  5367.       ui_out_text (uiout, "\tdisable after ");
  5368.       /* Tweak the wording to clarify that ignore and enable counts
  5369.          are distinct, and have additive effect.  */
  5370.       if (b->ignore_count)
  5371.         ui_out_text (uiout, "additional ");
  5372.       else
  5373.         ui_out_text (uiout, "next ");
  5374.       ui_out_field_int (uiout, "enable", b->enable_count);
  5375.       ui_out_text (uiout, " hits\n");
  5376.     }

  5377.   if (!part_of_multiple && is_tracepoint (b))
  5378.     {
  5379.       struct tracepoint *tp = (struct tracepoint *) b;

  5380.       if (tp->traceframe_usage)
  5381.         {
  5382.           ui_out_text (uiout, "\ttrace buffer usage ");
  5383.           ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
  5384.           ui_out_text (uiout, " bytes\n");
  5385.         }
  5386.     }

  5387.   l = b->commands ? b->commands->commands : NULL;
  5388.   if (!part_of_multiple && l)
  5389.     {
  5390.       struct cleanup *script_chain;

  5391.       annotate_field (9);
  5392.       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
  5393.       print_command_lines (uiout, l, 4);
  5394.       do_cleanups (script_chain);
  5395.     }

  5396.   if (is_tracepoint (b))
  5397.     {
  5398.       struct tracepoint *t = (struct tracepoint *) b;

  5399.       if (!part_of_multiple && t->pass_count)
  5400.         {
  5401.           annotate_field (10);
  5402.           ui_out_text (uiout, "\tpass count ");
  5403.           ui_out_field_int (uiout, "pass", t->pass_count);
  5404.           ui_out_text (uiout, " \n");
  5405.         }

  5406.       /* Don't display it when tracepoint or tracepoint location is
  5407.          pending.   */
  5408.       if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
  5409.         {
  5410.           annotate_field (11);

  5411.           if (ui_out_is_mi_like_p (uiout))
  5412.             ui_out_field_string (uiout, "installed",
  5413.                                  loc->inserted ? "y" : "n");
  5414.           else
  5415.             {
  5416.               if (loc->inserted)
  5417.                 ui_out_text (uiout, "\t");
  5418.               else
  5419.                 ui_out_text (uiout, "\tnot ");
  5420.               ui_out_text (uiout, "installed on target\n");
  5421.             }
  5422.         }
  5423.     }

  5424.   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
  5425.     {
  5426.       if (is_watchpoint (b))
  5427.         {
  5428.           struct watchpoint *w = (struct watchpoint *) b;

  5429.           ui_out_field_string (uiout, "original-location", w->exp_string);
  5430.         }
  5431.       else if (b->addr_string)
  5432.         ui_out_field_string (uiout, "original-location", b->addr_string);
  5433.     }
  5434. }

  5435. static void
  5436. print_one_breakpoint (struct breakpoint *b,
  5437.                       struct bp_location **last_loc,
  5438.                       int allflag)
  5439. {
  5440.   struct cleanup *bkpt_chain;
  5441.   struct ui_out *uiout = current_uiout;

  5442.   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");

  5443.   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
  5444.   do_cleanups (bkpt_chain);

  5445.   /* If this breakpoint has custom print function,
  5446.      it's already printed.  Otherwise, print individual
  5447.      locations, if any.  */
  5448.   if (b->ops == NULL || b->ops->print_one == NULL)
  5449.     {
  5450.       /* If breakpoint has a single location that is disabled, we
  5451.          print it as if it had several locations, since otherwise it's
  5452.          hard to represent "breakpoint enabled, location disabled"
  5453.          situation.

  5454.          Note that while hardware watchpoints have several locations
  5455.          internally, that's not a property exposed to user.  */
  5456.       if (b->loc
  5457.           && !is_hardware_watchpoint (b)
  5458.           && (b->loc->next || !b->loc->enabled))
  5459.         {
  5460.           struct bp_location *loc;
  5461.           int n = 1;

  5462.           for (loc = b->loc; loc; loc = loc->next, ++n)
  5463.             {
  5464.               struct cleanup *inner2 =
  5465.                 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  5466.               print_one_breakpoint_location (b, loc, n, last_loc, allflag);
  5467.               do_cleanups (inner2);
  5468.             }
  5469.         }
  5470.     }
  5471. }

  5472. static int
  5473. breakpoint_address_bits (struct breakpoint *b)
  5474. {
  5475.   int print_address_bits = 0;
  5476.   struct bp_location *loc;

  5477.   for (loc = b->loc; loc; loc = loc->next)
  5478.     {
  5479.       int addr_bit;

  5480.       /* Software watchpoints that aren't watching memory don't have
  5481.          an address to print.  */
  5482.       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
  5483.         continue;

  5484.       addr_bit = gdbarch_addr_bit (loc->gdbarch);
  5485.       if (addr_bit > print_address_bits)
  5486.         print_address_bits = addr_bit;
  5487.     }

  5488.   return print_address_bits;
  5489. }

  5490. struct captured_breakpoint_query_args
  5491.   {
  5492.     int bnum;
  5493.   };

  5494. static int
  5495. do_captured_breakpoint_query (struct ui_out *uiout, void *data)
  5496. {
  5497.   struct captured_breakpoint_query_args *args = data;
  5498.   struct breakpoint *b;
  5499.   struct bp_location *dummy_loc = NULL;

  5500.   ALL_BREAKPOINTS (b)
  5501.     {
  5502.       if (args->bnum == b->number)
  5503.         {
  5504.           print_one_breakpoint (b, &dummy_loc, 0);
  5505.           return GDB_RC_OK;
  5506.         }
  5507.     }
  5508.   return GDB_RC_NONE;
  5509. }

  5510. enum gdb_rc
  5511. gdb_breakpoint_query (struct ui_out *uiout, int bnum,
  5512.                       char **error_message)
  5513. {
  5514.   struct captured_breakpoint_query_args args;

  5515.   args.bnum = bnum;
  5516.   /* For the moment we don't trust print_one_breakpoint() to not throw
  5517.      an error.  */
  5518.   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
  5519.                                  error_message, RETURN_MASK_ALL) < 0)
  5520.     return GDB_RC_FAIL;
  5521.   else
  5522.     return GDB_RC_OK;
  5523. }

  5524. /* Return true if this breakpoint was set by the user, false if it is
  5525.    internal or momentary.  */

  5526. int
  5527. user_breakpoint_p (struct breakpoint *b)
  5528. {
  5529.   return b->number > 0;
  5530. }

  5531. /* Print information on user settable breakpoint (watchpoint, etc)
  5532.    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
  5533.    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
  5534.    FILTER is non-NULL, call it on each breakpoint and only include the
  5535.    ones for which it returns non-zero.  Return the total number of
  5536.    breakpoints listed.  */

  5537. static int
  5538. breakpoint_1 (char *args, int allflag,
  5539.               int (*filter) (const struct breakpoint *))
  5540. {
  5541.   struct breakpoint *b;
  5542.   struct bp_location *last_loc = NULL;
  5543.   int nr_printable_breakpoints;
  5544.   struct cleanup *bkpttbl_chain;
  5545.   struct value_print_options opts;
  5546.   int print_address_bits = 0;
  5547.   int print_type_col_width = 14;
  5548.   struct ui_out *uiout = current_uiout;

  5549.   get_user_print_options (&opts);

  5550.   /* Compute the number of rows in the table, as well as the size
  5551.      required for address fields.  */
  5552.   nr_printable_breakpoints = 0;
  5553.   ALL_BREAKPOINTS (b)
  5554.     {
  5555.       /* If we have a filter, only list the breakpoints it accepts.  */
  5556.       if (filter && !filter (b))
  5557.         continue;

  5558.       /* If we have an "args" string, it is a list of breakpoints to
  5559.          accept.  Skip the others.  */
  5560.       if (args != NULL && *args != '\0')
  5561.         {
  5562.           if (allflag && parse_and_eval_long (args) != b->number)
  5563.             continue;
  5564.           if (!allflag && !number_is_in_list (args, b->number))
  5565.             continue;
  5566.         }

  5567.       if (allflag || user_breakpoint_p (b))
  5568.         {
  5569.           int addr_bit, type_len;

  5570.           addr_bit = breakpoint_address_bits (b);
  5571.           if (addr_bit > print_address_bits)
  5572.             print_address_bits = addr_bit;

  5573.           type_len = strlen (bptype_string (b->type));
  5574.           if (type_len > print_type_col_width)
  5575.             print_type_col_width = type_len;

  5576.           nr_printable_breakpoints++;
  5577.         }
  5578.     }

  5579.   if (opts.addressprint)
  5580.     bkpttbl_chain
  5581.       = make_cleanup_ui_out_table_begin_end (uiout, 6,
  5582.                                              nr_printable_breakpoints,
  5583.                                              "BreakpointTable");
  5584.   else
  5585.     bkpttbl_chain
  5586.       = make_cleanup_ui_out_table_begin_end (uiout, 5,
  5587.                                              nr_printable_breakpoints,
  5588.                                              "BreakpointTable");

  5589.   if (nr_printable_breakpoints > 0)
  5590.     annotate_breakpoints_headers ();
  5591.   if (nr_printable_breakpoints > 0)
  5592.     annotate_field (0);
  5593.   ui_out_table_header (uiout, 7, ui_left, "number", "Num");        /* 1 */
  5594.   if (nr_printable_breakpoints > 0)
  5595.     annotate_field (1);
  5596.   ui_out_table_header (uiout, print_type_col_width, ui_left,
  5597.                        "type", "Type");                                /* 2 */
  5598.   if (nr_printable_breakpoints > 0)
  5599.     annotate_field (2);
  5600.   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");        /* 3 */
  5601.   if (nr_printable_breakpoints > 0)
  5602.     annotate_field (3);
  5603.   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");        /* 4 */
  5604.   if (opts.addressprint)
  5605.     {
  5606.       if (nr_printable_breakpoints > 0)
  5607.         annotate_field (4);
  5608.       if (print_address_bits <= 32)
  5609.         ui_out_table_header (uiout, 10, ui_left,
  5610.                              "addr", "Address");                /* 5 */
  5611.       else
  5612.         ui_out_table_header (uiout, 18, ui_left,
  5613.                              "addr", "Address");                /* 5 */
  5614.     }
  5615.   if (nr_printable_breakpoints > 0)
  5616.     annotate_field (5);
  5617.   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");        /* 6 */
  5618.   ui_out_table_body (uiout);
  5619.   if (nr_printable_breakpoints > 0)
  5620.     annotate_breakpoints_table ();

  5621.   ALL_BREAKPOINTS (b)
  5622.     {
  5623.       QUIT;
  5624.       /* If we have a filter, only list the breakpoints it accepts.  */
  5625.       if (filter && !filter (b))
  5626.         continue;

  5627.       /* If we have an "args" string, it is a list of breakpoints to
  5628.          accept.  Skip the others.  */

  5629.       if (args != NULL && *args != '\0')
  5630.         {
  5631.           if (allflag)        /* maintenance info breakpoint */
  5632.             {
  5633.               if (parse_and_eval_long (args) != b->number)
  5634.                 continue;
  5635.             }
  5636.           else                /* all others */
  5637.             {
  5638.               if (!number_is_in_list (args, b->number))
  5639.                 continue;
  5640.             }
  5641.         }
  5642.       /* We only print out user settable breakpoints unless the
  5643.          allflag is set.  */
  5644.       if (allflag || user_breakpoint_p (b))
  5645.         print_one_breakpoint (b, &last_loc, allflag);
  5646.     }

  5647.   do_cleanups (bkpttbl_chain);

  5648.   if (nr_printable_breakpoints == 0)
  5649.     {
  5650.       /* If there's a filter, let the caller decide how to report
  5651.          empty list.  */
  5652.       if (!filter)
  5653.         {
  5654.           if (args == NULL || *args == '\0')
  5655.             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
  5656.           else
  5657.             ui_out_message (uiout, 0,
  5658.                             "No breakpoint or watchpoint matching '%s'.\n",
  5659.                             args);
  5660.         }
  5661.     }
  5662.   else
  5663.     {
  5664.       if (last_loc && !server_command)
  5665.         set_next_address (last_loc->gdbarch, last_loc->address);
  5666.     }

  5667.   /* FIXME?  Should this be moved up so that it is only called when
  5668.      there have been breakpoints? */
  5669.   annotate_breakpoints_table_end ();

  5670.   return nr_printable_breakpoints;
  5671. }

  5672. /* Display the value of default-collect in a way that is generally
  5673.    compatible with the breakpoint list.  */

  5674. static void
  5675. default_collect_info (void)
  5676. {
  5677.   struct ui_out *uiout = current_uiout;

  5678.   /* If it has no value (which is frequently the case), say nothing; a
  5679.      message like "No default-collect." gets in user's face when it's
  5680.      not wanted.  */
  5681.   if (!*default_collect)
  5682.     return;

  5683.   /* The following phrase lines up nicely with per-tracepoint collect
  5684.      actions.  */
  5685.   ui_out_text (uiout, "default collect ");
  5686.   ui_out_field_string (uiout, "default-collect", default_collect);
  5687.   ui_out_text (uiout, " \n");
  5688. }

  5689. static void
  5690. breakpoints_info (char *args, int from_tty)
  5691. {
  5692.   breakpoint_1 (args, 0, NULL);

  5693.   default_collect_info ();
  5694. }

  5695. static void
  5696. watchpoints_info (char *args, int from_tty)
  5697. {
  5698.   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
  5699.   struct ui_out *uiout = current_uiout;

  5700.   if (num_printed == 0)
  5701.     {
  5702.       if (args == NULL || *args == '\0')
  5703.         ui_out_message (uiout, 0, "No watchpoints.\n");
  5704.       else
  5705.         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
  5706.     }
  5707. }

  5708. static void
  5709. maintenance_info_breakpoints (char *args, int from_tty)
  5710. {
  5711.   breakpoint_1 (args, 1, NULL);

  5712.   default_collect_info ();
  5713. }

  5714. static int
  5715. breakpoint_has_pc (struct breakpoint *b,
  5716.                    struct program_space *pspace,
  5717.                    CORE_ADDR pc, struct obj_section *section)
  5718. {
  5719.   struct bp_location *bl = b->loc;

  5720.   for (; bl; bl = bl->next)
  5721.     {
  5722.       if (bl->pspace == pspace
  5723.           && bl->address == pc
  5724.           && (!overlay_debugging || bl->section == section))
  5725.         return 1;
  5726.     }
  5727.   return 0;
  5728. }

  5729. /* Print a message describing any user-breakpoints set at PC.  This
  5730.    concerns with logical breakpoints, so we match program spaces, not
  5731.    address spaces.  */

  5732. static void
  5733. describe_other_breakpoints (struct gdbarch *gdbarch,
  5734.                             struct program_space *pspace, CORE_ADDR pc,
  5735.                             struct obj_section *section, int thread)
  5736. {
  5737.   int others = 0;
  5738.   struct breakpoint *b;

  5739.   ALL_BREAKPOINTS (b)
  5740.     others += (user_breakpoint_p (b)
  5741.                && breakpoint_has_pc (b, pspace, pc, section));
  5742.   if (others > 0)
  5743.     {
  5744.       if (others == 1)
  5745.         printf_filtered (_("Note: breakpoint "));
  5746.       else /* if (others == ???) */
  5747.         printf_filtered (_("Note: breakpoints "));
  5748.       ALL_BREAKPOINTS (b)
  5749.         if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
  5750.           {
  5751.             others--;
  5752.             printf_filtered ("%d", b->number);
  5753.             if (b->thread == -1 && thread != -1)
  5754.               printf_filtered (" (all threads)");
  5755.             else if (b->thread != -1)
  5756.               printf_filtered (" (thread %d)", b->thread);
  5757.             printf_filtered ("%s%s ",
  5758.                              ((b->enable_state == bp_disabled
  5759.                                || b->enable_state == bp_call_disabled)
  5760.                               ? " (disabled)"
  5761.                               : ""),
  5762.                              (others > 1) ? ","
  5763.                              : ((others == 1) ? " and" : ""));
  5764.           }
  5765.       printf_filtered (_("also set at pc "));
  5766.       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
  5767.       printf_filtered (".\n");
  5768.     }
  5769. }


  5770. /* Return true iff it is meaningful to use the address member of
  5771.    BPT.  For some breakpoint types, the address member is irrelevant
  5772.    and it makes no sense to attempt to compare it to other addresses
  5773.    (or use it for any other purpose either).

  5774.    More specifically, each of the following breakpoint types will
  5775.    always have a zero valued address and we don't want to mark
  5776.    breakpoints of any of these types to be a duplicate of an actual
  5777.    breakpoint at address zero:

  5778.       bp_watchpoint
  5779.       bp_catchpoint

  5780. */

  5781. static int
  5782. breakpoint_address_is_meaningful (struct breakpoint *bpt)
  5783. {
  5784.   enum bptype type = bpt->type;

  5785.   return (type != bp_watchpoint && type != bp_catchpoint);
  5786. }

  5787. /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
  5788.    true if LOC1 and LOC2 represent the same watchpoint location.  */

  5789. static int
  5790. watchpoint_locations_match (struct bp_location *loc1,
  5791.                             struct bp_location *loc2)
  5792. {
  5793.   struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
  5794.   struct watchpoint *w2 = (struct watchpoint *) loc2->owner;

  5795.   /* Both of them must exist.  */
  5796.   gdb_assert (w1 != NULL);
  5797.   gdb_assert (w2 != NULL);

  5798.   /* If the target can evaluate the condition expression in hardware,
  5799.      then we we need to insert both watchpoints even if they are at
  5800.      the same place.  Otherwise the watchpoint will only trigger when
  5801.      the condition of whichever watchpoint was inserted evaluates to
  5802.      true, not giving a chance for GDB to check the condition of the
  5803.      other watchpoint.  */
  5804.   if ((w1->cond_exp
  5805.        && target_can_accel_watchpoint_condition (loc1->address,
  5806.                                                  loc1->length,
  5807.                                                  loc1->watchpoint_type,
  5808.                                                  w1->cond_exp))
  5809.       || (w2->cond_exp
  5810.           && target_can_accel_watchpoint_condition (loc2->address,
  5811.                                                     loc2->length,
  5812.                                                     loc2->watchpoint_type,
  5813.                                                     w2->cond_exp)))
  5814.     return 0;

  5815.   /* Note that this checks the owner's type, not the location's.  In
  5816.      case the target does not support read watchpoints, but does
  5817.      support access watchpoints, we'll have bp_read_watchpoint
  5818.      watchpoints with hw_access locations.  Those should be considered
  5819.      duplicates of hw_read locations.  The hw_read locations will
  5820.      become hw_access locations later.  */
  5821.   return (loc1->owner->type == loc2->owner->type
  5822.           && loc1->pspace->aspace == loc2->pspace->aspace
  5823.           && loc1->address == loc2->address
  5824.           && loc1->length == loc2->length);
  5825. }

  5826. /* See breakpoint.h.  */

  5827. int
  5828. breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
  5829.                           struct address_space *aspace2, CORE_ADDR addr2)
  5830. {
  5831.   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
  5832.            || aspace1 == aspace2)
  5833.           && addr1 == addr2);
  5834. }

  5835. /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
  5836.    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
  5837.    matches ASPACE2.  On targets that have global breakpoints, the address
  5838.    space doesn't really matter.  */

  5839. static int
  5840. breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
  5841.                                 int len1, struct address_space *aspace2,
  5842.                                 CORE_ADDR addr2)
  5843. {
  5844.   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
  5845.            || aspace1 == aspace2)
  5846.           && addr2 >= addr1 && addr2 < addr1 + len1);
  5847. }

  5848. /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
  5849.    a ranged breakpoint.  In most targets, a match happens only if ASPACE
  5850.    matches the breakpoint's address space.  On targets that have global
  5851.    breakpoints, the address space doesn't really matter.  */

  5852. static int
  5853. breakpoint_location_address_match (struct bp_location *bl,
  5854.                                    struct address_space *aspace,
  5855.                                    CORE_ADDR addr)
  5856. {
  5857.   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
  5858.                                     aspace, addr)
  5859.           || (bl->length
  5860.               && breakpoint_address_match_range (bl->pspace->aspace,
  5861.                                                  bl->address, bl->length,
  5862.                                                  aspace, addr)));
  5863. }

  5864. /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
  5865.    Then, if LOC1 and LOC2 represent the same tracepoint location, returns
  5866.    true, otherwise returns false.  */

  5867. static int
  5868. tracepoint_locations_match (struct bp_location *loc1,
  5869.                             struct bp_location *loc2)
  5870. {
  5871.   if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
  5872.     /* Since tracepoint locations are never duplicated with others', tracepoint
  5873.        locations at the same address of different tracepoints are regarded as
  5874.        different locations.  */
  5875.     return (loc1->address == loc2->address && loc1->owner == loc2->owner);
  5876.   else
  5877.     return 0;
  5878. }

  5879. /* Assuming LOC1 and LOC2's types' have meaningful target addresses
  5880.    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
  5881.    represent the same location.  */

  5882. static int
  5883. breakpoint_locations_match (struct bp_location *loc1,
  5884.                             struct bp_location *loc2)
  5885. {
  5886.   int hw_point1, hw_point2;

  5887.   /* Both of them must not be in moribund_locations.  */
  5888.   gdb_assert (loc1->owner != NULL);
  5889.   gdb_assert (loc2->owner != NULL);

  5890.   hw_point1 = is_hardware_watchpoint (loc1->owner);
  5891.   hw_point2 = is_hardware_watchpoint (loc2->owner);

  5892.   if (hw_point1 != hw_point2)
  5893.     return 0;
  5894.   else if (hw_point1)
  5895.     return watchpoint_locations_match (loc1, loc2);
  5896.   else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
  5897.     return tracepoint_locations_match (loc1, loc2);
  5898.   else
  5899.     /* We compare bp_location.length in order to cover ranged breakpoints.  */
  5900.     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
  5901.                                      loc2->pspace->aspace, loc2->address)
  5902.             && loc1->length == loc2->length);
  5903. }

  5904. static void
  5905. breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
  5906.                                int bnum, int have_bnum)
  5907. {
  5908.   /* The longest string possibly returned by hex_string_custom
  5909.      is 50 chars.  These must be at least that big for safety.  */
  5910.   char astr1[64];
  5911.   char astr2[64];

  5912.   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
  5913.   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
  5914.   if (have_bnum)
  5915.     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
  5916.              bnum, astr1, astr2);
  5917.   else
  5918.     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
  5919. }

  5920. /* Adjust a breakpoint's address to account for architectural
  5921.    constraints on breakpoint placement.  Return the adjusted address.
  5922.    Note: Very few targets require this kind of adjustment.  For most
  5923.    targets, this function is simply the identity function.  */

  5924. static CORE_ADDR
  5925. adjust_breakpoint_address (struct gdbarch *gdbarch,
  5926.                            CORE_ADDR bpaddr, enum bptype bptype)
  5927. {
  5928.   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
  5929.     {
  5930.       /* Very few targets need any kind of breakpoint adjustment.  */
  5931.       return bpaddr;
  5932.     }
  5933.   else if (bptype == bp_watchpoint
  5934.            || bptype == bp_hardware_watchpoint
  5935.            || bptype == bp_read_watchpoint
  5936.            || bptype == bp_access_watchpoint
  5937.            || bptype == bp_catchpoint)
  5938.     {
  5939.       /* Watchpoints and the various bp_catch_* eventpoints should not
  5940.          have their addresses modified.  */
  5941.       return bpaddr;
  5942.     }
  5943.   else if (bptype == bp_single_step)
  5944.     {
  5945.       /* Single-step breakpoints should not have their addresses
  5946.          modified.  If there's any architectural constrain that
  5947.          applies to this address, then it should have already been
  5948.          taken into account when the breakpoint was created in the
  5949.          first place.  If we didn't do this, stepping through e.g.,
  5950.          Thumb-2 IT blocks would break.  */
  5951.       return bpaddr;
  5952.     }
  5953.   else
  5954.     {
  5955.       CORE_ADDR adjusted_bpaddr;

  5956.       /* Some targets have architectural constraints on the placement
  5957.          of breakpoint instructions.  Obtain the adjusted address.  */
  5958.       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);

  5959.       /* An adjusted breakpoint address can significantly alter
  5960.          a user's expectations.  Print a warning if an adjustment
  5961.          is required.  */
  5962.       if (adjusted_bpaddr != bpaddr)
  5963.         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);

  5964.       return adjusted_bpaddr;
  5965.     }
  5966. }

  5967. void
  5968. init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
  5969.                   struct breakpoint *owner)
  5970. {
  5971.   memset (loc, 0, sizeof (*loc));

  5972.   gdb_assert (ops != NULL);

  5973.   loc->ops = ops;
  5974.   loc->owner = owner;
  5975.   loc->cond = NULL;
  5976.   loc->cond_bytecode = NULL;
  5977.   loc->shlib_disabled = 0;
  5978.   loc->enabled = 1;

  5979.   switch (owner->type)
  5980.     {
  5981.     case bp_breakpoint:
  5982.     case bp_single_step:
  5983.     case bp_until:
  5984.     case bp_finish:
  5985.     case bp_longjmp:
  5986.     case bp_longjmp_resume:
  5987.     case bp_longjmp_call_dummy:
  5988.     case bp_exception:
  5989.     case bp_exception_resume:
  5990.     case bp_step_resume:
  5991.     case bp_hp_step_resume:
  5992.     case bp_watchpoint_scope:
  5993.     case bp_call_dummy:
  5994.     case bp_std_terminate:
  5995.     case bp_shlib_event:
  5996.     case bp_thread_event:
  5997.     case bp_overlay_event:
  5998.     case bp_jit_event:
  5999.     case bp_longjmp_master:
  6000.     case bp_std_terminate_master:
  6001.     case bp_exception_master:
  6002.     case bp_gnu_ifunc_resolver:
  6003.     case bp_gnu_ifunc_resolver_return:
  6004.     case bp_dprintf:
  6005.       loc->loc_type = bp_loc_software_breakpoint;
  6006.       mark_breakpoint_location_modified (loc);
  6007.       break;
  6008.     case bp_hardware_breakpoint:
  6009.       loc->loc_type = bp_loc_hardware_breakpoint;
  6010.       mark_breakpoint_location_modified (loc);
  6011.       break;
  6012.     case bp_hardware_watchpoint:
  6013.     case bp_read_watchpoint:
  6014.     case bp_access_watchpoint:
  6015.       loc->loc_type = bp_loc_hardware_watchpoint;
  6016.       break;
  6017.     case bp_watchpoint:
  6018.     case bp_catchpoint:
  6019.     case bp_tracepoint:
  6020.     case bp_fast_tracepoint:
  6021.     case bp_static_tracepoint:
  6022.       loc->loc_type = bp_loc_other;
  6023.       break;
  6024.     default:
  6025.       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
  6026.     }

  6027.   loc->refc = 1;
  6028. }

  6029. /* Allocate a struct bp_location.  */

  6030. static struct bp_location *
  6031. allocate_bp_location (struct breakpoint *bpt)
  6032. {
  6033.   return bpt->ops->allocate_location (bpt);
  6034. }

  6035. static void
  6036. free_bp_location (struct bp_location *loc)
  6037. {
  6038.   loc->ops->dtor (loc);
  6039.   xfree (loc);
  6040. }

  6041. /* Increment reference count.  */

  6042. static void
  6043. incref_bp_location (struct bp_location *bl)
  6044. {
  6045.   ++bl->refc;
  6046. }

  6047. /* Decrement reference count.  If the reference count reaches 0,
  6048.    destroy the bp_location.  Sets *BLP to NULL.  */

  6049. static void
  6050. decref_bp_location (struct bp_location **blp)
  6051. {
  6052.   gdb_assert ((*blp)->refc > 0);

  6053.   if (--(*blp)->refc == 0)
  6054.     free_bp_location (*blp);
  6055.   *blp = NULL;
  6056. }

  6057. /* Add breakpoint B at the end of the global breakpoint chain.  */

  6058. static void
  6059. add_to_breakpoint_chain (struct breakpoint *b)
  6060. {
  6061.   struct breakpoint *b1;

  6062.   /* Add this breakpoint to the end of the chain so that a list of
  6063.      breakpoints will come out in order of increasing numbers.  */

  6064.   b1 = breakpoint_chain;
  6065.   if (b1 == 0)
  6066.     breakpoint_chain = b;
  6067.   else
  6068.     {
  6069.       while (b1->next)
  6070.         b1 = b1->next;
  6071.       b1->next = b;
  6072.     }
  6073. }

  6074. /* Initializes breakpoint B with type BPTYPE and no locations yet.  */

  6075. static void
  6076. init_raw_breakpoint_without_location (struct breakpoint *b,
  6077.                                       struct gdbarch *gdbarch,
  6078.                                       enum bptype bptype,
  6079.                                       const struct breakpoint_ops *ops)
  6080. {
  6081.   memset (b, 0, sizeof (*b));

  6082.   gdb_assert (ops != NULL);

  6083.   b->ops = ops;
  6084.   b->type = bptype;
  6085.   b->gdbarch = gdbarch;
  6086.   b->language = current_language->la_language;
  6087.   b->input_radix = input_radix;
  6088.   b->thread = -1;
  6089.   b->enable_state = bp_enabled;
  6090.   b->next = 0;
  6091.   b->silent = 0;
  6092.   b->ignore_count = 0;
  6093.   b->commands = NULL;
  6094.   b->frame_id = null_frame_id;
  6095.   b->condition_not_parsed = 0;
  6096.   b->py_bp_object = NULL;
  6097.   b->related_breakpoint = b;
  6098. }

  6099. /* Helper to set_raw_breakpoint below.  Creates a breakpoint
  6100.    that has type BPTYPE and has no locations as yet.  */

  6101. static struct breakpoint *
  6102. set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
  6103.                                      enum bptype bptype,
  6104.                                      const struct breakpoint_ops *ops)
  6105. {
  6106.   struct breakpoint *b = XNEW (struct breakpoint);

  6107.   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
  6108.   add_to_breakpoint_chain (b);
  6109.   return b;
  6110. }

  6111. /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
  6112.    resolutions should be made as the user specified the location explicitly
  6113.    enough.  */

  6114. static void
  6115. set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
  6116. {
  6117.   gdb_assert (loc->owner != NULL);

  6118.   if (loc->owner->type == bp_breakpoint
  6119.       || loc->owner->type == bp_hardware_breakpoint
  6120.       || is_tracepoint (loc->owner))
  6121.     {
  6122.       int is_gnu_ifunc;
  6123.       const char *function_name;
  6124.       CORE_ADDR func_addr;

  6125.       find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
  6126.                                           &func_addr, NULL, &is_gnu_ifunc);

  6127.       if (is_gnu_ifunc && !explicit_loc)
  6128.         {
  6129.           struct breakpoint *b = loc->owner;

  6130.           gdb_assert (loc->pspace == current_program_space);
  6131.           if (gnu_ifunc_resolve_name (function_name,
  6132.                                       &loc->requested_address))
  6133.             {
  6134.               /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
  6135.               loc->address = adjust_breakpoint_address (loc->gdbarch,
  6136.                                                         loc->requested_address,
  6137.                                                         b->type);
  6138.             }
  6139.           else if (b->type == bp_breakpoint && b->loc == loc
  6140.                    && loc->next == NULL && b->related_breakpoint == b)
  6141.             {
  6142.               /* Create only the whole new breakpoint of this type but do not
  6143.                  mess more complicated breakpoints with multiple locations.  */
  6144.               b->type = bp_gnu_ifunc_resolver;
  6145.               /* Remember the resolver's address for use by the return
  6146.                  breakpoint.  */
  6147.               loc->related_address = func_addr;
  6148.             }
  6149.         }

  6150.       if (function_name)
  6151.         loc->function_name = xstrdup (function_name);
  6152.     }
  6153. }

  6154. /* Attempt to determine architecture of location identified by SAL.  */
  6155. struct gdbarch *
  6156. get_sal_arch (struct symtab_and_line sal)
  6157. {
  6158.   if (sal.section)
  6159.     return get_objfile_arch (sal.section->objfile);
  6160.   if (sal.symtab)
  6161.     return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));

  6162.   return NULL;
  6163. }

  6164. /* Low level routine for partially initializing a breakpoint of type
  6165.    BPTYPE.  The newly created breakpoint's address, section, source
  6166.    file name, and line number are provided by SAL.

  6167.    It is expected that the caller will complete the initialization of
  6168.    the newly created breakpoint struct as well as output any status
  6169.    information regarding the creation of a new breakpoint.  */

  6170. static void
  6171. init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
  6172.                      struct symtab_and_line sal, enum bptype bptype,
  6173.                      const struct breakpoint_ops *ops)
  6174. {
  6175.   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);

  6176.   add_location_to_breakpoint (b, &sal);

  6177.   if (bptype != bp_catchpoint)
  6178.     gdb_assert (sal.pspace != NULL);

  6179.   /* Store the program space that was used to set the breakpoint,
  6180.      except for ordinary breakpoints, which are independent of the
  6181.      program space.  */
  6182.   if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
  6183.     b->pspace = sal.pspace;
  6184. }

  6185. /* set_raw_breakpoint is a low level routine for allocating and
  6186.    partially initializing a breakpoint of type BPTYPE.  The newly
  6187.    created breakpoint's address, section, source file name, and line
  6188.    number are provided by SAL.  The newly created and partially
  6189.    initialized breakpoint is added to the breakpoint chain and
  6190.    is also returned as the value of this function.

  6191.    It is expected that the caller will complete the initialization of
  6192.    the newly created breakpoint struct as well as output any status
  6193.    information regarding the creation of a new breakpoint.  In
  6194.    particular, set_raw_breakpoint does NOT set the breakpoint
  6195.    number!  Care should be taken to not allow an error to occur
  6196.    prior to completing the initialization of the breakpoint.  If this
  6197.    should happen, a bogus breakpoint will be left on the chain.  */

  6198. struct breakpoint *
  6199. set_raw_breakpoint (struct gdbarch *gdbarch,
  6200.                     struct symtab_and_line sal, enum bptype bptype,
  6201.                     const struct breakpoint_ops *ops)
  6202. {
  6203.   struct breakpoint *b = XNEW (struct breakpoint);

  6204.   init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
  6205.   add_to_breakpoint_chain (b);
  6206.   return b;
  6207. }


  6208. /* Note that the breakpoint object B describes a permanent breakpoint
  6209.    instruction, hard-wired into the inferior's code.  */
  6210. void
  6211. make_breakpoint_permanent (struct breakpoint *b)
  6212. {
  6213.   struct bp_location *bl;

  6214.   /* By definition, permanent breakpoints are already present in the
  6215.      code.  Mark all locations as inserted.  For now,
  6216.      make_breakpoint_permanent is called in just one place, so it's
  6217.      hard to say if it's reasonable to have permanent breakpoint with
  6218.      multiple locations or not, but it's easy to implement.  */
  6219.   for (bl = b->loc; bl; bl = bl->next)
  6220.     {
  6221.       bl->permanent = 1;
  6222.       bl->inserted = 1;
  6223.     }
  6224. }

  6225. /* Call this routine when stepping and nexting to enable a breakpoint
  6226.    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
  6227.    initiated the operation.  */

  6228. void
  6229. set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
  6230. {
  6231.   struct breakpoint *b, *b_tmp;
  6232.   int thread = tp->num;

  6233.   /* To avoid having to rescan all objfile symbols at every step,
  6234.      we maintain a list of continually-inserted but always disabled
  6235.      longjmp "master" breakpoints.  Here, we simply create momentary
  6236.      clones of those and enable them for the requested thread.  */
  6237.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6238.     if (b->pspace == current_program_space
  6239.         && (b->type == bp_longjmp_master
  6240.             || b->type == bp_exception_master))
  6241.       {
  6242.         enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
  6243.         struct breakpoint *clone;

  6244.         /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
  6245.            after their removal.  */
  6246.         clone = momentary_breakpoint_from_master (b, type,
  6247.                                                   &longjmp_breakpoint_ops, 1);
  6248.         clone->thread = thread;
  6249.       }

  6250.   tp->initiating_frame = frame;
  6251. }

  6252. /* Delete all longjmp breakpoints from THREAD.  */
  6253. void
  6254. delete_longjmp_breakpoint (int thread)
  6255. {
  6256.   struct breakpoint *b, *b_tmp;

  6257.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6258.     if (b->type == bp_longjmp || b->type == bp_exception)
  6259.       {
  6260.         if (b->thread == thread)
  6261.           delete_breakpoint (b);
  6262.       }
  6263. }

  6264. void
  6265. delete_longjmp_breakpoint_at_next_stop (int thread)
  6266. {
  6267.   struct breakpoint *b, *b_tmp;

  6268.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6269.     if (b->type == bp_longjmp || b->type == bp_exception)
  6270.       {
  6271.         if (b->thread == thread)
  6272.           b->disposition = disp_del_at_next_stop;
  6273.       }
  6274. }

  6275. /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
  6276.    INFERIOR_PTID thread.  Chain them all by RELATED_BREAKPOINT and return
  6277.    pointer to any of them.  Return NULL if this system cannot place longjmp
  6278.    breakpoints.  */

  6279. struct breakpoint *
  6280. set_longjmp_breakpoint_for_call_dummy (void)
  6281. {
  6282.   struct breakpoint *b, *retval = NULL;

  6283.   ALL_BREAKPOINTS (b)
  6284.     if (b->pspace == current_program_space && b->type == bp_longjmp_master)
  6285.       {
  6286.         struct breakpoint *new_b;

  6287.         new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
  6288.                                                   &momentary_breakpoint_ops,
  6289.                                                   1);
  6290.         new_b->thread = pid_to_thread_id (inferior_ptid);

  6291.         /* Link NEW_B into the chain of RETVAL breakpoints.  */

  6292.         gdb_assert (new_b->related_breakpoint == new_b);
  6293.         if (retval == NULL)
  6294.           retval = new_b;
  6295.         new_b->related_breakpoint = retval;
  6296.         while (retval->related_breakpoint != new_b->related_breakpoint)
  6297.           retval = retval->related_breakpoint;
  6298.         retval->related_breakpoint = new_b;
  6299.       }

  6300.   return retval;
  6301. }

  6302. /* Verify all existing dummy frames and their associated breakpoints for
  6303.    TP.  Remove those which can no longer be found in the current frame
  6304.    stack.

  6305.    You should call this function only at places where it is safe to currently
  6306.    unwind the whole stack.  Failed stack unwind would discard live dummy
  6307.    frames.  */

  6308. void
  6309. check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
  6310. {
  6311.   struct breakpoint *b, *b_tmp;

  6312.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6313.     if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
  6314.       {
  6315.         struct breakpoint *dummy_b = b->related_breakpoint;

  6316.         while (dummy_b != b && dummy_b->type != bp_call_dummy)
  6317.           dummy_b = dummy_b->related_breakpoint;
  6318.         if (dummy_b->type != bp_call_dummy
  6319.             || frame_find_by_id (dummy_b->frame_id) != NULL)
  6320.           continue;

  6321.         dummy_frame_discard (dummy_b->frame_id, tp->ptid);

  6322.         while (b->related_breakpoint != b)
  6323.           {
  6324.             if (b_tmp == b->related_breakpoint)
  6325.               b_tmp = b->related_breakpoint->next;
  6326.             delete_breakpoint (b->related_breakpoint);
  6327.           }
  6328.         delete_breakpoint (b);
  6329.       }
  6330. }

  6331. void
  6332. enable_overlay_breakpoints (void)
  6333. {
  6334.   struct breakpoint *b;

  6335.   ALL_BREAKPOINTS (b)
  6336.     if (b->type == bp_overlay_event)
  6337.     {
  6338.       b->enable_state = bp_enabled;
  6339.       update_global_location_list (UGLL_MAY_INSERT);
  6340.       overlay_events_enabled = 1;
  6341.     }
  6342. }

  6343. void
  6344. disable_overlay_breakpoints (void)
  6345. {
  6346.   struct breakpoint *b;

  6347.   ALL_BREAKPOINTS (b)
  6348.     if (b->type == bp_overlay_event)
  6349.     {
  6350.       b->enable_state = bp_disabled;
  6351.       update_global_location_list (UGLL_DONT_INSERT);
  6352.       overlay_events_enabled = 0;
  6353.     }
  6354. }

  6355. /* Set an active std::terminate breakpoint for each std::terminate
  6356.    master breakpoint.  */
  6357. void
  6358. set_std_terminate_breakpoint (void)
  6359. {
  6360.   struct breakpoint *b, *b_tmp;

  6361.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6362.     if (b->pspace == current_program_space
  6363.         && b->type == bp_std_terminate_master)
  6364.       {
  6365.         momentary_breakpoint_from_master (b, bp_std_terminate,
  6366.                                           &momentary_breakpoint_ops, 1);
  6367.       }
  6368. }

  6369. /* Delete all the std::terminate breakpoints.  */
  6370. void
  6371. delete_std_terminate_breakpoint (void)
  6372. {
  6373.   struct breakpoint *b, *b_tmp;

  6374.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6375.     if (b->type == bp_std_terminate)
  6376.       delete_breakpoint (b);
  6377. }

  6378. struct breakpoint *
  6379. create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
  6380. {
  6381.   struct breakpoint *b;

  6382.   b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
  6383.                                   &internal_breakpoint_ops);

  6384.   b->enable_state = bp_enabled;
  6385.   /* addr_string has to be used or breakpoint_re_set will delete me.  */
  6386.   b->addr_string
  6387.     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));

  6388.   update_global_location_list_nothrow (UGLL_MAY_INSERT);

  6389.   return b;
  6390. }

  6391. void
  6392. remove_thread_event_breakpoints (void)
  6393. {
  6394.   struct breakpoint *b, *b_tmp;

  6395.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6396.     if (b->type == bp_thread_event
  6397.         && b->loc->pspace == current_program_space)
  6398.       delete_breakpoint (b);
  6399. }

  6400. struct lang_and_radix
  6401.   {
  6402.     enum language lang;
  6403.     int radix;
  6404.   };

  6405. /* Create a breakpoint for JIT code registration and unregistration.  */

  6406. struct breakpoint *
  6407. create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
  6408. {
  6409.   struct breakpoint *b;

  6410.   b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
  6411.                                   &internal_breakpoint_ops);
  6412.   update_global_location_list_nothrow (UGLL_MAY_INSERT);
  6413.   return b;
  6414. }

  6415. /* Remove JIT code registration and unregistration breakpoint(s).  */

  6416. void
  6417. remove_jit_event_breakpoints (void)
  6418. {
  6419.   struct breakpoint *b, *b_tmp;

  6420.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6421.     if (b->type == bp_jit_event
  6422.         && b->loc->pspace == current_program_space)
  6423.       delete_breakpoint (b);
  6424. }

  6425. void
  6426. remove_solib_event_breakpoints (void)
  6427. {
  6428.   struct breakpoint *b, *b_tmp;

  6429.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6430.     if (b->type == bp_shlib_event
  6431.         && b->loc->pspace == current_program_space)
  6432.       delete_breakpoint (b);
  6433. }

  6434. /* See breakpoint.h.  */

  6435. void
  6436. remove_solib_event_breakpoints_at_next_stop (void)
  6437. {
  6438.   struct breakpoint *b, *b_tmp;

  6439.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  6440.     if (b->type == bp_shlib_event
  6441.         && b->loc->pspace == current_program_space)
  6442.       b->disposition = disp_del_at_next_stop;
  6443. }

  6444. /* Helper for create_solib_event_breakpoint /
  6445.    create_and_insert_solib_event_breakpoint.  Allows specifying which
  6446.    INSERT_MODE to pass through to update_global_location_list.  */

  6447. static struct breakpoint *
  6448. create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
  6449.                                  enum ugll_insert_mode insert_mode)
  6450. {
  6451.   struct breakpoint *b;

  6452.   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
  6453.                                   &internal_breakpoint_ops);
  6454.   update_global_location_list_nothrow (insert_mode);
  6455.   return b;
  6456. }

  6457. struct breakpoint *
  6458. create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
  6459. {
  6460.   return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
  6461. }

  6462. /* See breakpoint.h.  */

  6463. struct breakpoint *
  6464. create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
  6465. {
  6466.   struct breakpoint *b;

  6467.   /* Explicitly tell update_global_location_list to insert
  6468.      locations.  */
  6469.   b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
  6470.   if (!b->loc->inserted)
  6471.     {
  6472.       delete_breakpoint (b);
  6473.       return NULL;
  6474.     }
  6475.   return b;
  6476. }

  6477. /* Disable any breakpoints that are on code in shared libraries.  Only
  6478.    apply to enabled breakpoints, disabled ones can just stay disabled.  */

  6479. void
  6480. disable_breakpoints_in_shlibs (void)
  6481. {
  6482.   struct bp_location *loc, **locp_tmp;

  6483.   ALL_BP_LOCATIONS (loc, locp_tmp)
  6484.   {
  6485.     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
  6486.     struct breakpoint *b = loc->owner;

  6487.     /* We apply the check to all breakpoints, including disabled for
  6488.        those with loc->duplicate set.  This is so that when breakpoint
  6489.        becomes enabled, or the duplicate is removed, gdb will try to
  6490.        insert all breakpoints.  If we don't set shlib_disabled here,
  6491.        we'll try to insert those breakpoints and fail.  */
  6492.     if (((b->type == bp_breakpoint)
  6493.          || (b->type == bp_jit_event)
  6494.          || (b->type == bp_hardware_breakpoint)
  6495.          || (is_tracepoint (b)))
  6496.         && loc->pspace == current_program_space
  6497.         && !loc->shlib_disabled
  6498.         && solib_name_from_address (loc->pspace, loc->address)
  6499.         )
  6500.       {
  6501.         loc->shlib_disabled = 1;
  6502.       }
  6503.   }
  6504. }

  6505. /* Disable any breakpoints and tracepoints that are in SOLIB upon
  6506.    notification of unloaded_shlib.  Only apply to enabled breakpoints,
  6507.    disabled ones can just stay disabled.  */

  6508. static void
  6509. disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
  6510. {
  6511.   struct bp_location *loc, **locp_tmp;
  6512.   int disabled_shlib_breaks = 0;

  6513.   /* SunOS a.out shared libraries are always mapped, so do not
  6514.      disable breakpoints; they will only be reported as unloaded
  6515.      through clear_solib when GDB discards its shared library
  6516.      list.  See clear_solib for more information.  */
  6517.   if (exec_bfd != NULL
  6518.       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
  6519.     return;

  6520.   ALL_BP_LOCATIONS (loc, locp_tmp)
  6521.   {
  6522.     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
  6523.     struct breakpoint *b = loc->owner;

  6524.     if (solib->pspace == loc->pspace
  6525.         && !loc->shlib_disabled
  6526.         && (((b->type == bp_breakpoint
  6527.               || b->type == bp_jit_event
  6528.               || b->type == bp_hardware_breakpoint)
  6529.              && (loc->loc_type == bp_loc_hardware_breakpoint
  6530.                  || loc->loc_type == bp_loc_software_breakpoint))
  6531.             || is_tracepoint (b))
  6532.         && solib_contains_address_p (solib, loc->address))
  6533.       {
  6534.         loc->shlib_disabled = 1;
  6535.         /* At this point, we cannot rely on remove_breakpoint
  6536.            succeeding so we must mark the breakpoint as not inserted
  6537.            to prevent future errors occurring in remove_breakpoints.  */
  6538.         loc->inserted = 0;

  6539.         /* This may cause duplicate notifications for the same breakpoint.  */
  6540.         observer_notify_breakpoint_modified (b);

  6541.         if (!disabled_shlib_breaks)
  6542.           {
  6543.             target_terminal_ours_for_output ();
  6544.             warning (_("Temporarily disabling breakpoints "
  6545.                        "for unloaded shared library \"%s\""),
  6546.                      solib->so_name);
  6547.           }
  6548.         disabled_shlib_breaks = 1;
  6549.       }
  6550.   }
  6551. }

  6552. /* Disable any breakpoints and tracepoints in OBJFILE upon
  6553.    notification of free_objfile.  Only apply to enabled breakpoints,
  6554.    disabled ones can just stay disabled.  */

  6555. static void
  6556. disable_breakpoints_in_freed_objfile (struct objfile *objfile)
  6557. {
  6558.   struct breakpoint *b;

  6559.   if (objfile == NULL)
  6560.     return;

  6561.   /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
  6562.      managed by the user with add-symbol-file/remove-symbol-file.
  6563.      Similarly to how breakpoints in shared libraries are handled in
  6564.      response to "nosharedlibrary", mark breakpoints in such modules
  6565.      shlib_disabled so they end up uninserted on the next global
  6566.      location list update.  Shared libraries not loaded by the user
  6567.      aren't handled here -- they're already handled in
  6568.      disable_breakpoints_in_unloaded_shlib, called by solib.c's
  6569.      solib_unloaded observer.  We skip objfiles that are not
  6570.      OBJF_SHARED as those aren't considered dynamic objects (e.g. the
  6571.      main objfile).  */
  6572.   if ((objfile->flags & OBJF_SHARED) == 0
  6573.       || (objfile->flags & OBJF_USERLOADED) == 0)
  6574.     return;

  6575.   ALL_BREAKPOINTS (b)
  6576.     {
  6577.       struct bp_location *loc;
  6578.       int bp_modified = 0;

  6579.       if (!is_breakpoint (b) && !is_tracepoint (b))
  6580.         continue;

  6581.       for (loc = b->loc; loc != NULL; loc = loc->next)
  6582.         {
  6583.           CORE_ADDR loc_addr = loc->address;

  6584.           if (loc->loc_type != bp_loc_hardware_breakpoint
  6585.               && loc->loc_type != bp_loc_software_breakpoint)
  6586.             continue;

  6587.           if (loc->shlib_disabled != 0)
  6588.             continue;

  6589.           if (objfile->pspace != loc->pspace)
  6590.             continue;

  6591.           if (loc->loc_type != bp_loc_hardware_breakpoint
  6592.               && loc->loc_type != bp_loc_software_breakpoint)
  6593.             continue;

  6594.           if (is_addr_in_objfile (loc_addr, objfile))
  6595.             {
  6596.               loc->shlib_disabled = 1;
  6597.               /* At this point, we don't know whether the object was
  6598.                  unmapped from the inferior or not, so leave the
  6599.                  inserted flag alone.  We'll handle failure to
  6600.                  uninsert quietly, in case the object was indeed
  6601.                  unmapped.  */

  6602.               mark_breakpoint_location_modified (loc);

  6603.               bp_modified = 1;
  6604.             }
  6605.         }

  6606.       if (bp_modified)
  6607.         observer_notify_breakpoint_modified (b);
  6608.     }
  6609. }

  6610. /* FORK & VFORK catchpoints.  */

  6611. /* An instance of this type is used to represent a fork or vfork
  6612.    catchpoint.  It includes a "struct breakpoint" as a kind of base
  6613.    class; users downcast to "struct breakpoint *" when needed.  A
  6614.    breakpoint is really of this type iff its ops pointer points to
  6615.    CATCH_FORK_BREAKPOINT_OPS.  */

  6616. struct fork_catchpoint
  6617. {
  6618.   /* The base class.  */
  6619.   struct breakpoint base;

  6620.   /* Process id of a child process whose forking triggered this
  6621.      catchpoint.  This field is only valid immediately after this
  6622.      catchpoint has triggered.  */
  6623.   ptid_t forked_inferior_pid;
  6624. };

  6625. /* Implement the "insert" breakpoint_ops method for fork
  6626.    catchpoints.  */

  6627. static int
  6628. insert_catch_fork (struct bp_location *bl)
  6629. {
  6630.   return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
  6631. }

  6632. /* Implement the "remove" breakpoint_ops method for fork
  6633.    catchpoints.  */

  6634. static int
  6635. remove_catch_fork (struct bp_location *bl)
  6636. {
  6637.   return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
  6638. }

  6639. /* Implement the "breakpoint_hit" breakpoint_ops method for fork
  6640.    catchpoints.  */

  6641. static int
  6642. breakpoint_hit_catch_fork (const struct bp_location *bl,
  6643.                            struct address_space *aspace, CORE_ADDR bp_addr,
  6644.                            const struct target_waitstatus *ws)
  6645. {
  6646.   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;

  6647.   if (ws->kind != TARGET_WAITKIND_FORKED)
  6648.     return 0;

  6649.   c->forked_inferior_pid = ws->value.related_pid;
  6650.   return 1;
  6651. }

  6652. /* Implement the "print_it" breakpoint_ops method for fork
  6653.    catchpoints.  */

  6654. static enum print_stop_action
  6655. print_it_catch_fork (bpstat bs)
  6656. {
  6657.   struct ui_out *uiout = current_uiout;
  6658.   struct breakpoint *b = bs->breakpoint_at;
  6659.   struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;

  6660.   annotate_catchpoint (b->number);
  6661.   if (b->disposition == disp_del)
  6662.     ui_out_text (uiout, "\nTemporary catchpoint ");
  6663.   else
  6664.     ui_out_text (uiout, "\nCatchpoint ");
  6665.   if (ui_out_is_mi_like_p (uiout))
  6666.     {
  6667.       ui_out_field_string (uiout, "reason",
  6668.                            async_reason_lookup (EXEC_ASYNC_FORK));
  6669.       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
  6670.     }
  6671.   ui_out_field_int (uiout, "bkptno", b->number);
  6672.   ui_out_text (uiout, " (forked process ");
  6673.   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
  6674.   ui_out_text (uiout, "), ");
  6675.   return PRINT_SRC_AND_LOC;
  6676. }

  6677. /* Implement the "print_one" breakpoint_ops method for fork
  6678.    catchpoints.  */

  6679. static void
  6680. print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
  6681. {
  6682.   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
  6683.   struct value_print_options opts;
  6684.   struct ui_out *uiout = current_uiout;

  6685.   get_user_print_options (&opts);

  6686.   /* Field 4, the address, is omitted (which makes the columns not
  6687.      line up too nicely with the headers, but the effect is relatively
  6688.      readable).  */
  6689.   if (opts.addressprint)
  6690.     ui_out_field_skip (uiout, "addr");
  6691.   annotate_field (5);
  6692.   ui_out_text (uiout, "fork");
  6693.   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
  6694.     {
  6695.       ui_out_text (uiout, ", process ");
  6696.       ui_out_field_int (uiout, "what",
  6697.                         ptid_get_pid (c->forked_inferior_pid));
  6698.       ui_out_spaces (uiout, 1);
  6699.     }

  6700.   if (ui_out_is_mi_like_p (uiout))
  6701.     ui_out_field_string (uiout, "catch-type", "fork");
  6702. }

  6703. /* Implement the "print_mention" breakpoint_ops method for fork
  6704.    catchpoints.  */

  6705. static void
  6706. print_mention_catch_fork (struct breakpoint *b)
  6707. {
  6708.   printf_filtered (_("Catchpoint %d (fork)"), b->number);
  6709. }

  6710. /* Implement the "print_recreate" breakpoint_ops method for fork
  6711.    catchpoints.  */

  6712. static void
  6713. print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
  6714. {
  6715.   fprintf_unfiltered (fp, "catch fork");
  6716.   print_recreate_thread (b, fp);
  6717. }

  6718. /* The breakpoint_ops structure to be used in fork catchpoints.  */

  6719. static struct breakpoint_ops catch_fork_breakpoint_ops;

  6720. /* Implement the "insert" breakpoint_ops method for vfork
  6721.    catchpoints.  */

  6722. static int
  6723. insert_catch_vfork (struct bp_location *bl)
  6724. {
  6725.   return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
  6726. }

  6727. /* Implement the "remove" breakpoint_ops method for vfork
  6728.    catchpoints.  */

  6729. static int
  6730. remove_catch_vfork (struct bp_location *bl)
  6731. {
  6732.   return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
  6733. }

  6734. /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
  6735.    catchpoints.  */

  6736. static int
  6737. breakpoint_hit_catch_vfork (const struct bp_location *bl,
  6738.                             struct address_space *aspace, CORE_ADDR bp_addr,
  6739.                             const struct target_waitstatus *ws)
  6740. {
  6741.   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;

  6742.   if (ws->kind != TARGET_WAITKIND_VFORKED)
  6743.     return 0;

  6744.   c->forked_inferior_pid = ws->value.related_pid;
  6745.   return 1;
  6746. }

  6747. /* Implement the "print_it" breakpoint_ops method for vfork
  6748.    catchpoints.  */

  6749. static enum print_stop_action
  6750. print_it_catch_vfork (bpstat bs)
  6751. {
  6752.   struct ui_out *uiout = current_uiout;
  6753.   struct breakpoint *b = bs->breakpoint_at;
  6754.   struct fork_catchpoint *c = (struct fork_catchpoint *) b;

  6755.   annotate_catchpoint (b->number);
  6756.   if (b->disposition == disp_del)
  6757.     ui_out_text (uiout, "\nTemporary catchpoint ");
  6758.   else
  6759.     ui_out_text (uiout, "\nCatchpoint ");
  6760.   if (ui_out_is_mi_like_p (uiout))
  6761.     {
  6762.       ui_out_field_string (uiout, "reason",
  6763.                            async_reason_lookup (EXEC_ASYNC_VFORK));
  6764.       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
  6765.     }
  6766.   ui_out_field_int (uiout, "bkptno", b->number);
  6767.   ui_out_text (uiout, " (vforked process ");
  6768.   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
  6769.   ui_out_text (uiout, "), ");
  6770.   return PRINT_SRC_AND_LOC;
  6771. }

  6772. /* Implement the "print_one" breakpoint_ops method for vfork
  6773.    catchpoints.  */

  6774. static void
  6775. print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
  6776. {
  6777.   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
  6778.   struct value_print_options opts;
  6779.   struct ui_out *uiout = current_uiout;

  6780.   get_user_print_options (&opts);
  6781.   /* Field 4, the address, is omitted (which makes the columns not
  6782.      line up too nicely with the headers, but the effect is relatively
  6783.      readable).  */
  6784.   if (opts.addressprint)
  6785.     ui_out_field_skip (uiout, "addr");
  6786.   annotate_field (5);
  6787.   ui_out_text (uiout, "vfork");
  6788.   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
  6789.     {
  6790.       ui_out_text (uiout, ", process ");
  6791.       ui_out_field_int (uiout, "what",
  6792.                         ptid_get_pid (c->forked_inferior_pid));
  6793.       ui_out_spaces (uiout, 1);
  6794.     }

  6795.   if (ui_out_is_mi_like_p (uiout))
  6796.     ui_out_field_string (uiout, "catch-type", "vfork");
  6797. }

  6798. /* Implement the "print_mention" breakpoint_ops method for vfork
  6799.    catchpoints.  */

  6800. static void
  6801. print_mention_catch_vfork (struct breakpoint *b)
  6802. {
  6803.   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
  6804. }

  6805. /* Implement the "print_recreate" breakpoint_ops method for vfork
  6806.    catchpoints.  */

  6807. static void
  6808. print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
  6809. {
  6810.   fprintf_unfiltered (fp, "catch vfork");
  6811.   print_recreate_thread (b, fp);
  6812. }

  6813. /* The breakpoint_ops structure to be used in vfork catchpoints.  */

  6814. static struct breakpoint_ops catch_vfork_breakpoint_ops;

  6815. /* An instance of this type is used to represent an solib catchpoint.
  6816.    It includes a "struct breakpoint" as a kind of base class; users
  6817.    downcast to "struct breakpoint *" when needed.  A breakpoint is
  6818.    really of this type iff its ops pointer points to
  6819.    CATCH_SOLIB_BREAKPOINT_OPS.  */

  6820. struct solib_catchpoint
  6821. {
  6822.   /* The base class.  */
  6823.   struct breakpoint base;

  6824.   /* True for "catch load", false for "catch unload".  */
  6825.   unsigned char is_load;

  6826.   /* Regular expression to match, if any.  COMPILED is only valid when
  6827.      REGEX is non-NULL.  */
  6828.   char *regex;
  6829.   regex_t compiled;
  6830. };

  6831. static void
  6832. dtor_catch_solib (struct breakpoint *b)
  6833. {
  6834.   struct solib_catchpoint *self = (struct solib_catchpoint *) b;

  6835.   if (self->regex)
  6836.     regfree (&self->compiled);
  6837.   xfree (self->regex);

  6838.   base_breakpoint_ops.dtor (b);
  6839. }

  6840. static int
  6841. insert_catch_solib (struct bp_location *ignore)
  6842. {
  6843.   return 0;
  6844. }

  6845. static int
  6846. remove_catch_solib (struct bp_location *ignore)
  6847. {
  6848.   return 0;
  6849. }

  6850. static int
  6851. breakpoint_hit_catch_solib (const struct bp_location *bl,
  6852.                             struct address_space *aspace,
  6853.                             CORE_ADDR bp_addr,
  6854.                             const struct target_waitstatus *ws)
  6855. {
  6856.   struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
  6857.   struct breakpoint *other;

  6858.   if (ws->kind == TARGET_WAITKIND_LOADED)
  6859.     return 1;

  6860.   ALL_BREAKPOINTS (other)
  6861.   {
  6862.     struct bp_location *other_bl;

  6863.     if (other == bl->owner)
  6864.       continue;

  6865.     if (other->type != bp_shlib_event)
  6866.       continue;

  6867.     if (self->base.pspace != NULL && other->pspace != self->base.pspace)
  6868.       continue;

  6869.     for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
  6870.       {
  6871.         if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
  6872.           return 1;
  6873.       }
  6874.   }

  6875.   return 0;
  6876. }

  6877. static void
  6878. check_status_catch_solib (struct bpstats *bs)
  6879. {
  6880.   struct solib_catchpoint *self
  6881.     = (struct solib_catchpoint *) bs->breakpoint_at;
  6882.   int ix;

  6883.   if (self->is_load)
  6884.     {
  6885.       struct so_list *iter;

  6886.       for (ix = 0;
  6887.            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
  6888.                         ix, iter);
  6889.            ++ix)
  6890.         {
  6891.           if (!self->regex
  6892.               || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
  6893.             return;
  6894.         }
  6895.     }
  6896.   else
  6897.     {
  6898.       char *iter;

  6899.       for (ix = 0;
  6900.            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
  6901.                         ix, iter);
  6902.            ++ix)
  6903.         {
  6904.           if (!self->regex
  6905.               || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
  6906.             return;
  6907.         }
  6908.     }

  6909.   bs->stop = 0;
  6910.   bs->print_it = print_it_noop;
  6911. }

  6912. static enum print_stop_action
  6913. print_it_catch_solib (bpstat bs)
  6914. {
  6915.   struct breakpoint *b = bs->breakpoint_at;
  6916.   struct ui_out *uiout = current_uiout;

  6917.   annotate_catchpoint (b->number);
  6918.   if (b->disposition == disp_del)
  6919.     ui_out_text (uiout, "\nTemporary catchpoint ");
  6920.   else
  6921.     ui_out_text (uiout, "\nCatchpoint ");
  6922.   ui_out_field_int (uiout, "bkptno", b->number);
  6923.   ui_out_text (uiout, "\n");
  6924.   if (ui_out_is_mi_like_p (uiout))
  6925.     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
  6926.   print_solib_event (1);
  6927.   return PRINT_SRC_AND_LOC;
  6928. }

  6929. static void
  6930. print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
  6931. {
  6932.   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
  6933.   struct value_print_options opts;
  6934.   struct ui_out *uiout = current_uiout;
  6935.   char *msg;

  6936.   get_user_print_options (&opts);
  6937.   /* Field 4, the address, is omitted (which makes the columns not
  6938.      line up too nicely with the headers, but the effect is relatively
  6939.      readable).  */
  6940.   if (opts.addressprint)
  6941.     {
  6942.       annotate_field (4);
  6943.       ui_out_field_skip (uiout, "addr");
  6944.     }

  6945.   annotate_field (5);
  6946.   if (self->is_load)
  6947.     {
  6948.       if (self->regex)
  6949.         msg = xstrprintf (_("load of library matching %s"), self->regex);
  6950.       else
  6951.         msg = xstrdup (_("load of library"));
  6952.     }
  6953.   else
  6954.     {
  6955.       if (self->regex)
  6956.         msg = xstrprintf (_("unload of library matching %s"), self->regex);
  6957.       else
  6958.         msg = xstrdup (_("unload of library"));
  6959.     }
  6960.   ui_out_field_string (uiout, "what", msg);
  6961.   xfree (msg);

  6962.   if (ui_out_is_mi_like_p (uiout))
  6963.     ui_out_field_string (uiout, "catch-type",
  6964.                          self->is_load ? "load" : "unload");
  6965. }

  6966. static void
  6967. print_mention_catch_solib (struct breakpoint *b)
  6968. {
  6969.   struct solib_catchpoint *self = (struct solib_catchpoint *) b;

  6970.   printf_filtered (_("Catchpoint %d (%s)"), b->number,
  6971.                    self->is_load ? "load" : "unload");
  6972. }

  6973. static void
  6974. print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
  6975. {
  6976.   struct solib_catchpoint *self = (struct solib_catchpoint *) b;

  6977.   fprintf_unfiltered (fp, "%s %s",
  6978.                       b->disposition == disp_del ? "tcatch" : "catch",
  6979.                       self->is_load ? "load" : "unload");
  6980.   if (self->regex)
  6981.     fprintf_unfiltered (fp, " %s", self->regex);
  6982.   fprintf_unfiltered (fp, "\n");
  6983. }

  6984. static struct breakpoint_ops catch_solib_breakpoint_ops;

  6985. /* Shared helper function (MI and CLI) for creating and installing
  6986.    a shared object event catchpoint.  If IS_LOAD is non-zero then
  6987.    the events to be caught are load events, otherwise they are
  6988.    unload events.  If IS_TEMP is non-zero the catchpoint is a
  6989.    temporary one.  If ENABLED is non-zero the catchpoint is
  6990.    created in an enabled state.  */

  6991. void
  6992. add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
  6993. {
  6994.   struct solib_catchpoint *c;
  6995.   struct gdbarch *gdbarch = get_current_arch ();
  6996.   struct cleanup *cleanup;

  6997.   if (!arg)
  6998.     arg = "";
  6999.   arg = skip_spaces (arg);

  7000.   c = XCNEW (struct solib_catchpoint);
  7001.   cleanup = make_cleanup (xfree, c);

  7002.   if (*arg != '\0')
  7003.     {
  7004.       int errcode;

  7005.       errcode = regcomp (&c->compiled, arg, REG_NOSUB);
  7006.       if (errcode != 0)
  7007.         {
  7008.           char *err = get_regcomp_error (errcode, &c->compiled);

  7009.           make_cleanup (xfree, err);
  7010.           error (_("Invalid regexp (%s): %s"), err, arg);
  7011.         }
  7012.       c->regex = xstrdup (arg);
  7013.     }

  7014.   c->is_load = is_load;
  7015.   init_catchpoint (&c->base, gdbarch, is_temp, NULL,
  7016.                    &catch_solib_breakpoint_ops);

  7017.   c->base.enable_state = enabled ? bp_enabled : bp_disabled;

  7018.   discard_cleanups (cleanup);
  7019.   install_breakpoint (0, &c->base, 1);
  7020. }

  7021. /* A helper function that does all the work for "catch load" and
  7022.    "catch unload".  */

  7023. static void
  7024. catch_load_or_unload (char *arg, int from_tty, int is_load,
  7025.                       struct cmd_list_element *command)
  7026. {
  7027.   int tempflag;
  7028.   const int enabled = 1;

  7029.   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;

  7030.   add_solib_catchpoint (arg, is_load, tempflag, enabled);
  7031. }

  7032. static void
  7033. catch_load_command_1 (char *arg, int from_tty,
  7034.                       struct cmd_list_element *command)
  7035. {
  7036.   catch_load_or_unload (arg, from_tty, 1, command);
  7037. }

  7038. static void
  7039. catch_unload_command_1 (char *arg, int from_tty,
  7040.                         struct cmd_list_element *command)
  7041. {
  7042.   catch_load_or_unload (arg, from_tty, 0, command);
  7043. }

  7044. /* An instance of this type is used to represent a syscall catchpoint.
  7045.    It includes a "struct breakpoint" as a kind of base class; users
  7046.    downcast to "struct breakpoint *" when needed.  A breakpoint is
  7047.    really of this type iff its ops pointer points to
  7048.    CATCH_SYSCALL_BREAKPOINT_OPS.  */

  7049. struct syscall_catchpoint
  7050. {
  7051.   /* The base class.  */
  7052.   struct breakpoint base;

  7053.   /* Syscall numbers used for the 'catch syscall' feature.  If no
  7054.      syscall has been specified for filtering, its value is NULL.
  7055.      Otherwise, it holds a list of all syscalls to be caught.  The
  7056.      list elements are allocated with xmalloc.  */
  7057.   VEC(int) *syscalls_to_be_caught;
  7058. };

  7059. /* Implement the "dtor" breakpoint_ops method for syscall
  7060.    catchpoints.  */

  7061. static void
  7062. dtor_catch_syscall (struct breakpoint *b)
  7063. {
  7064.   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;

  7065.   VEC_free (int, c->syscalls_to_be_caught);

  7066.   base_breakpoint_ops.dtor (b);
  7067. }

  7068. static const struct inferior_data *catch_syscall_inferior_data = NULL;

  7069. struct catch_syscall_inferior_data
  7070. {
  7071.   /* We keep a count of the number of times the user has requested a
  7072.      particular syscall to be tracked, and pass this information to the
  7073.      target.  This lets capable targets implement filtering directly.  */

  7074.   /* Number of times that "any" syscall is requested.  */
  7075.   int any_syscall_count;

  7076.   /* Count of each system call.  */
  7077.   VEC(int) *syscalls_counts;

  7078.   /* This counts all syscall catch requests, so we can readily determine
  7079.      if any catching is necessary.  */
  7080.   int total_syscalls_count;
  7081. };

  7082. static struct catch_syscall_inferior_data*
  7083. get_catch_syscall_inferior_data (struct inferior *inf)
  7084. {
  7085.   struct catch_syscall_inferior_data *inf_data;

  7086.   inf_data = inferior_data (inf, catch_syscall_inferior_data);
  7087.   if (inf_data == NULL)
  7088.     {
  7089.       inf_data = XCNEW (struct catch_syscall_inferior_data);
  7090.       set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
  7091.     }

  7092.   return inf_data;
  7093. }

  7094. static void
  7095. catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
  7096. {
  7097.   xfree (arg);
  7098. }


  7099. /* Implement the "insert" breakpoint_ops method for syscall
  7100.    catchpoints.  */

  7101. static int
  7102. insert_catch_syscall (struct bp_location *bl)
  7103. {
  7104.   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
  7105.   struct inferior *inf = current_inferior ();
  7106.   struct catch_syscall_inferior_data *inf_data
  7107.     = get_catch_syscall_inferior_data (inf);

  7108.   ++inf_data->total_syscalls_count;
  7109.   if (!c->syscalls_to_be_caught)
  7110.     ++inf_data->any_syscall_count;
  7111.   else
  7112.     {
  7113.       int i, iter;

  7114.       for (i = 0;
  7115.            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
  7116.            i++)
  7117.         {
  7118.           int elem;

  7119.           if (iter >= VEC_length (int, inf_data->syscalls_counts))
  7120.             {
  7121.               int old_size = VEC_length (int, inf_data->syscalls_counts);
  7122.               uintptr_t vec_addr_offset
  7123.                 = old_size * ((uintptr_t) sizeof (int));
  7124.               uintptr_t vec_addr;
  7125.               VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
  7126.               vec_addr = ((uintptr_t) VEC_address (int,
  7127.                                                   inf_data->syscalls_counts)
  7128.                           + vec_addr_offset);
  7129.               memset ((void *) vec_addr, 0,
  7130.                       (iter + 1 - old_size) * sizeof (int));
  7131.             }
  7132.           elem = VEC_index (int, inf_data->syscalls_counts, iter);
  7133.           VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
  7134.         }
  7135.     }

  7136.   return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
  7137.                                         inf_data->total_syscalls_count != 0,
  7138.                                         inf_data->any_syscall_count,
  7139.                                         VEC_length (int,
  7140.                                                     inf_data->syscalls_counts),
  7141.                                         VEC_address (int,
  7142.                                                      inf_data->syscalls_counts));
  7143. }

  7144. /* Implement the "remove" breakpoint_ops method for syscall
  7145.    catchpoints.  */

  7146. static int
  7147. remove_catch_syscall (struct bp_location *bl)
  7148. {
  7149.   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
  7150.   struct inferior *inf = current_inferior ();
  7151.   struct catch_syscall_inferior_data *inf_data
  7152.     = get_catch_syscall_inferior_data (inf);

  7153.   --inf_data->total_syscalls_count;
  7154.   if (!c->syscalls_to_be_caught)
  7155.     --inf_data->any_syscall_count;
  7156.   else
  7157.     {
  7158.       int i, iter;

  7159.       for (i = 0;
  7160.            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
  7161.            i++)
  7162.         {
  7163.           int elem;
  7164.           if (iter >= VEC_length (int, inf_data->syscalls_counts))
  7165.             /* Shouldn't happen.  */
  7166.             continue;
  7167.           elem = VEC_index (int, inf_data->syscalls_counts, iter);
  7168.           VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
  7169.         }
  7170.     }

  7171.   return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
  7172.                                         inf_data->total_syscalls_count != 0,
  7173.                                         inf_data->any_syscall_count,
  7174.                                         VEC_length (int,
  7175.                                                     inf_data->syscalls_counts),
  7176.                                         VEC_address (int,
  7177.                                                      inf_data->syscalls_counts));
  7178. }

  7179. /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
  7180.    catchpoints.  */

  7181. static int
  7182. breakpoint_hit_catch_syscall (const struct bp_location *bl,
  7183.                               struct address_space *aspace, CORE_ADDR bp_addr,
  7184.                               const struct target_waitstatus *ws)
  7185. {
  7186.   /* We must check if we are catching specific syscalls in this
  7187.      breakpoint.  If we are, then we must guarantee that the called
  7188.      syscall is the same syscall we are catching.  */
  7189.   int syscall_number = 0;
  7190.   const struct syscall_catchpoint *c
  7191.     = (const struct syscall_catchpoint *) bl->owner;

  7192.   if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
  7193.       && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
  7194.     return 0;

  7195.   syscall_number = ws->value.syscall_number;

  7196.   /* Now, checking if the syscall is the same.  */
  7197.   if (c->syscalls_to_be_caught)
  7198.     {
  7199.       int i, iter;

  7200.       for (i = 0;
  7201.            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
  7202.            i++)
  7203.         if (syscall_number == iter)
  7204.           return 1;

  7205.       return 0;
  7206.     }

  7207.   return 1;
  7208. }

  7209. /* Implement the "print_it" breakpoint_ops method for syscall
  7210.    catchpoints.  */

  7211. static enum print_stop_action
  7212. print_it_catch_syscall (bpstat bs)
  7213. {
  7214.   struct ui_out *uiout = current_uiout;
  7215.   struct breakpoint *b = bs->breakpoint_at;
  7216.   /* These are needed because we want to know in which state a
  7217.      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
  7218.      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
  7219.      must print "called syscall" or "returned from syscall".  */
  7220.   ptid_t ptid;
  7221.   struct target_waitstatus last;
  7222.   struct syscall s;
  7223.   struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;

  7224.   get_last_target_status (&ptid, &last);

  7225.   get_syscall_by_number (gdbarch, last.value.syscall_number, &s);

  7226.   annotate_catchpoint (b->number);

  7227.   if (b->disposition == disp_del)
  7228.     ui_out_text (uiout, "\nTemporary catchpoint ");
  7229.   else
  7230.     ui_out_text (uiout, "\nCatchpoint ");
  7231.   if (ui_out_is_mi_like_p (uiout))
  7232.     {
  7233.       ui_out_field_string (uiout, "reason",
  7234.                            async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
  7235.                                                 ? EXEC_ASYNC_SYSCALL_ENTRY
  7236.                                                 : EXEC_ASYNC_SYSCALL_RETURN));
  7237.       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
  7238.     }
  7239.   ui_out_field_int (uiout, "bkptno", b->number);

  7240.   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
  7241.     ui_out_text (uiout, " (call to syscall ");
  7242.   else
  7243.     ui_out_text (uiout, " (returned from syscall ");

  7244.   if (s.name == NULL || ui_out_is_mi_like_p (uiout))
  7245.     ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
  7246.   if (s.name != NULL)
  7247.     ui_out_field_string (uiout, "syscall-name", s.name);

  7248.   ui_out_text (uiout, "), ");

  7249.   return PRINT_SRC_AND_LOC;
  7250. }

  7251. /* Implement the "print_one" breakpoint_ops method for syscall
  7252.    catchpoints.  */

  7253. static void
  7254. print_one_catch_syscall (struct breakpoint *b,
  7255.                          struct bp_location **last_loc)
  7256. {
  7257.   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
  7258.   struct value_print_options opts;
  7259.   struct ui_out *uiout = current_uiout;
  7260.   struct gdbarch *gdbarch = b->loc->gdbarch;

  7261.   get_user_print_options (&opts);
  7262.   /* Field 4, the address, is omitted (which makes the columns not
  7263.      line up too nicely with the headers, but the effect is relatively
  7264.      readable).  */
  7265.   if (opts.addressprint)
  7266.     ui_out_field_skip (uiout, "addr");
  7267.   annotate_field (5);

  7268.   if (c->syscalls_to_be_caught
  7269.       && VEC_length (int, c->syscalls_to_be_caught) > 1)
  7270.     ui_out_text (uiout, "syscalls \"");
  7271.   else
  7272.     ui_out_text (uiout, "syscall \"");

  7273.   if (c->syscalls_to_be_caught)
  7274.     {
  7275.       int i, iter;
  7276.       char *text = xstrprintf ("%s", "");

  7277.       for (i = 0;
  7278.            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
  7279.            i++)
  7280.         {
  7281.           char *x = text;
  7282.           struct syscall s;
  7283.           get_syscall_by_number (gdbarch, iter, &s);

  7284.           if (s.name != NULL)
  7285.             text = xstrprintf ("%s%s, ", text, s.name);
  7286.           else
  7287.             text = xstrprintf ("%s%d, ", text, iter);

  7288.           /* We have to xfree the last 'text' (now stored at 'x')
  7289.              because xstrprintf dynamically allocates new space for it
  7290.              on every call.  */
  7291.           xfree (x);
  7292.         }
  7293.       /* Remove the last comma.  */
  7294.       text[strlen (text) - 2] = '\0';
  7295.       ui_out_field_string (uiout, "what", text);
  7296.     }
  7297.   else
  7298.     ui_out_field_string (uiout, "what", "<any syscall>");
  7299.   ui_out_text (uiout, "\" ");

  7300.   if (ui_out_is_mi_like_p (uiout))
  7301.     ui_out_field_string (uiout, "catch-type", "syscall");
  7302. }

  7303. /* Implement the "print_mention" breakpoint_ops method for syscall
  7304.    catchpoints.  */

  7305. static void
  7306. print_mention_catch_syscall (struct breakpoint *b)
  7307. {
  7308.   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
  7309.   struct gdbarch *gdbarch = b->loc->gdbarch;

  7310.   if (c->syscalls_to_be_caught)
  7311.     {
  7312.       int i, iter;

  7313.       if (VEC_length (int, c->syscalls_to_be_caught) > 1)
  7314.         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
  7315.       else
  7316.         printf_filtered (_("Catchpoint %d (syscall"), b->number);

  7317.       for (i = 0;
  7318.            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
  7319.            i++)
  7320.         {
  7321.           struct syscall s;
  7322.           get_syscall_by_number (gdbarch, iter, &s);

  7323.           if (s.name)
  7324.             printf_filtered (" '%s' [%d]", s.name, s.number);
  7325.           else
  7326.             printf_filtered (" %d", s.number);
  7327.         }
  7328.       printf_filtered (")");
  7329.     }
  7330.   else
  7331.     printf_filtered (_("Catchpoint %d (any syscall)"),
  7332.                      b->number);
  7333. }

  7334. /* Implement the "print_recreate" breakpoint_ops method for syscall
  7335.    catchpoints.  */

  7336. static void
  7337. print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
  7338. {
  7339.   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
  7340.   struct gdbarch *gdbarch = b->loc->gdbarch;

  7341.   fprintf_unfiltered (fp, "catch syscall");

  7342.   if (c->syscalls_to_be_caught)
  7343.     {
  7344.       int i, iter;

  7345.       for (i = 0;
  7346.            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
  7347.            i++)
  7348.         {
  7349.           struct syscall s;

  7350.           get_syscall_by_number (gdbarch, iter, &s);
  7351.           if (s.name)
  7352.             fprintf_unfiltered (fp, " %s", s.name);
  7353.           else
  7354.             fprintf_unfiltered (fp, " %d", s.number);
  7355.         }
  7356.     }
  7357.   print_recreate_thread (b, fp);
  7358. }

  7359. /* The breakpoint_ops structure to be used in syscall catchpoints.  */

  7360. static struct breakpoint_ops catch_syscall_breakpoint_ops;

  7361. /* Returns non-zero if 'b' is a syscall catchpoint.  */

  7362. static int
  7363. syscall_catchpoint_p (struct breakpoint *b)
  7364. {
  7365.   return (b->ops == &catch_syscall_breakpoint_ops);
  7366. }

  7367. /* Initialize a new breakpoint of the bp_catchpoint kind.  If TEMPFLAG
  7368.    is non-zero, then make the breakpoint temporary.  If COND_STRING is
  7369.    not NULL, then store it in the breakpoint.  OPS, if not NULL, is
  7370.    the breakpoint_ops structure associated to the catchpoint.  */

  7371. void
  7372. init_catchpoint (struct breakpoint *b,
  7373.                  struct gdbarch *gdbarch, int tempflag,
  7374.                  char *cond_string,
  7375.                  const struct breakpoint_ops *ops)
  7376. {
  7377.   struct symtab_and_line sal;

  7378.   init_sal (&sal);
  7379.   sal.pspace = current_program_space;

  7380.   init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);

  7381.   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
  7382.   b->disposition = tempflag ? disp_del : disp_donttouch;
  7383. }

  7384. void
  7385. install_breakpoint (int internal, struct breakpoint *b, int update_gll)
  7386. {
  7387.   add_to_breakpoint_chain (b);
  7388.   set_breakpoint_number (internal, b);
  7389.   if (is_tracepoint (b))
  7390.     set_tracepoint_count (breakpoint_count);
  7391.   if (!internal)
  7392.     mention (b);
  7393.   observer_notify_breakpoint_created (b);

  7394.   if (update_gll)
  7395.     update_global_location_list (UGLL_MAY_INSERT);
  7396. }

  7397. static void
  7398. create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
  7399.                                     int tempflag, char *cond_string,
  7400.                                     const struct breakpoint_ops *ops)
  7401. {
  7402.   struct fork_catchpoint *c = XNEW (struct fork_catchpoint);

  7403.   init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);

  7404.   c->forked_inferior_pid = null_ptid;

  7405.   install_breakpoint (0, &c->base, 1);
  7406. }

  7407. /* Exec catchpoints.  */

  7408. /* An instance of this type is used to represent an exec catchpoint.
  7409.    It includes a "struct breakpoint" as a kind of base class; users
  7410.    downcast to "struct breakpoint *" when needed.  A breakpoint is
  7411.    really of this type iff its ops pointer points to
  7412.    CATCH_EXEC_BREAKPOINT_OPS.  */

  7413. struct exec_catchpoint
  7414. {
  7415.   /* The base class.  */
  7416.   struct breakpoint base;

  7417.   /* Filename of a program whose exec triggered this catchpoint.
  7418.      This field is only valid immediately after this catchpoint has
  7419.      triggered.  */
  7420.   char *exec_pathname;
  7421. };

  7422. /* Implement the "dtor" breakpoint_ops method for exec
  7423.    catchpoints.  */

  7424. static void
  7425. dtor_catch_exec (struct breakpoint *b)
  7426. {
  7427.   struct exec_catchpoint *c = (struct exec_catchpoint *) b;

  7428.   xfree (c->exec_pathname);

  7429.   base_breakpoint_ops.dtor (b);
  7430. }

  7431. static int
  7432. insert_catch_exec (struct bp_location *bl)
  7433. {
  7434.   return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
  7435. }

  7436. static int
  7437. remove_catch_exec (struct bp_location *bl)
  7438. {
  7439.   return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
  7440. }

  7441. static int
  7442. breakpoint_hit_catch_exec (const struct bp_location *bl,
  7443.                            struct address_space *aspace, CORE_ADDR bp_addr,
  7444.                            const struct target_waitstatus *ws)
  7445. {
  7446.   struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;

  7447.   if (ws->kind != TARGET_WAITKIND_EXECD)
  7448.     return 0;

  7449.   c->exec_pathname = xstrdup (ws->value.execd_pathname);
  7450.   return 1;
  7451. }

  7452. static enum print_stop_action
  7453. print_it_catch_exec (bpstat bs)
  7454. {
  7455.   struct ui_out *uiout = current_uiout;
  7456.   struct breakpoint *b = bs->breakpoint_at;
  7457.   struct exec_catchpoint *c = (struct exec_catchpoint *) b;

  7458.   annotate_catchpoint (b->number);
  7459.   if (b->disposition == disp_del)
  7460.     ui_out_text (uiout, "\nTemporary catchpoint ");
  7461.   else
  7462.     ui_out_text (uiout, "\nCatchpoint ");
  7463.   if (ui_out_is_mi_like_p (uiout))
  7464.     {
  7465.       ui_out_field_string (uiout, "reason",
  7466.                            async_reason_lookup (EXEC_ASYNC_EXEC));
  7467.       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
  7468.     }
  7469.   ui_out_field_int (uiout, "bkptno", b->number);
  7470.   ui_out_text (uiout, " (exec'd ");
  7471.   ui_out_field_string (uiout, "new-exec", c->exec_pathname);
  7472.   ui_out_text (uiout, "), ");

  7473.   return PRINT_SRC_AND_LOC;
  7474. }

  7475. static void
  7476. print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
  7477. {
  7478.   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
  7479.   struct value_print_options opts;
  7480.   struct ui_out *uiout = current_uiout;

  7481.   get_user_print_options (&opts);

  7482.   /* Field 4, the address, is omitted (which makes the columns
  7483.      not line up too nicely with the headers, but the effect
  7484.      is relatively readable).  */
  7485.   if (opts.addressprint)
  7486.     ui_out_field_skip (uiout, "addr");
  7487.   annotate_field (5);
  7488.   ui_out_text (uiout, "exec");
  7489.   if (c->exec_pathname != NULL)
  7490.     {
  7491.       ui_out_text (uiout, ", program \"");
  7492.       ui_out_field_string (uiout, "what", c->exec_pathname);
  7493.       ui_out_text (uiout, "\" ");
  7494.     }

  7495.   if (ui_out_is_mi_like_p (uiout))
  7496.     ui_out_field_string (uiout, "catch-type", "exec");
  7497. }

  7498. static void
  7499. print_mention_catch_exec (struct breakpoint *b)
  7500. {
  7501.   printf_filtered (_("Catchpoint %d (exec)"), b->number);
  7502. }

  7503. /* Implement the "print_recreate" breakpoint_ops method for exec
  7504.    catchpoints.  */

  7505. static void
  7506. print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
  7507. {
  7508.   fprintf_unfiltered (fp, "catch exec");
  7509.   print_recreate_thread (b, fp);
  7510. }

  7511. static struct breakpoint_ops catch_exec_breakpoint_ops;

  7512. static void
  7513. create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
  7514.                                  const struct breakpoint_ops *ops)
  7515. {
  7516.   struct syscall_catchpoint *c;
  7517.   struct gdbarch *gdbarch = get_current_arch ();

  7518.   c = XNEW (struct syscall_catchpoint);
  7519.   init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
  7520.   c->syscalls_to_be_caught = filter;

  7521.   install_breakpoint (0, &c->base, 1);
  7522. }

  7523. static int
  7524. hw_breakpoint_used_count (void)
  7525. {
  7526.   int i = 0;
  7527.   struct breakpoint *b;
  7528.   struct bp_location *bl;

  7529.   ALL_BREAKPOINTS (b)
  7530.   {
  7531.     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
  7532.       for (bl = b->loc; bl; bl = bl->next)
  7533.         {
  7534.           /* Special types of hardware breakpoints may use more than
  7535.              one register.  */
  7536.           i += b->ops->resources_needed (bl);
  7537.         }
  7538.   }

  7539.   return i;
  7540. }

  7541. /* Returns the resources B would use if it were a hardware
  7542.    watchpoint.  */

  7543. static int
  7544. hw_watchpoint_use_count (struct breakpoint *b)
  7545. {
  7546.   int i = 0;
  7547.   struct bp_location *bl;

  7548.   if (!breakpoint_enabled (b))
  7549.     return 0;

  7550.   for (bl = b->loc; bl; bl = bl->next)
  7551.     {
  7552.       /* Special types of hardware watchpoints may use more than
  7553.          one register.  */
  7554.       i += b->ops->resources_needed (bl);
  7555.     }

  7556.   return i;
  7557. }

  7558. /* Returns the sum the used resources of all hardware watchpoints of
  7559.    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
  7560.    the sum of the used resources of all hardware watchpoints of other
  7561.    types _not_ TYPE.  */

  7562. static int
  7563. hw_watchpoint_used_count_others (struct breakpoint *except,
  7564.                                  enum bptype type, int *other_type_used)
  7565. {
  7566.   int i = 0;
  7567.   struct breakpoint *b;

  7568.   *other_type_used = 0;
  7569.   ALL_BREAKPOINTS (b)
  7570.     {
  7571.       if (b == except)
  7572.         continue;
  7573.       if (!breakpoint_enabled (b))
  7574.         continue;

  7575.       if (b->type == type)
  7576.         i += hw_watchpoint_use_count (b);
  7577.       else if (is_hardware_watchpoint (b))
  7578.         *other_type_used = 1;
  7579.     }

  7580.   return i;
  7581. }

  7582. void
  7583. disable_watchpoints_before_interactive_call_start (void)
  7584. {
  7585.   struct breakpoint *b;

  7586.   ALL_BREAKPOINTS (b)
  7587.   {
  7588.     if (is_watchpoint (b) && breakpoint_enabled (b))
  7589.       {
  7590.         b->enable_state = bp_call_disabled;
  7591.         update_global_location_list (UGLL_DONT_INSERT);
  7592.       }
  7593.   }
  7594. }

  7595. void
  7596. enable_watchpoints_after_interactive_call_stop (void)
  7597. {
  7598.   struct breakpoint *b;

  7599.   ALL_BREAKPOINTS (b)
  7600.   {
  7601.     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
  7602.       {
  7603.         b->enable_state = bp_enabled;
  7604.         update_global_location_list (UGLL_MAY_INSERT);
  7605.       }
  7606.   }
  7607. }

  7608. void
  7609. disable_breakpoints_before_startup (void)
  7610. {
  7611.   current_program_space->executing_startup = 1;
  7612.   update_global_location_list (UGLL_DONT_INSERT);
  7613. }

  7614. void
  7615. enable_breakpoints_after_startup (void)
  7616. {
  7617.   current_program_space->executing_startup = 0;
  7618.   breakpoint_re_set ();
  7619. }

  7620. /* Create a new single-step breakpoint for thread THREAD, with no
  7621.    locations.  */

  7622. static struct breakpoint *
  7623. new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
  7624. {
  7625.   struct breakpoint *b = XNEW (struct breakpoint);

  7626.   init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
  7627.                                         &momentary_breakpoint_ops);

  7628.   b->disposition = disp_donttouch;
  7629.   b->frame_id = null_frame_id;

  7630.   b->thread = thread;
  7631.   gdb_assert (b->thread != 0);

  7632.   add_to_breakpoint_chain (b);

  7633.   return b;
  7634. }

  7635. /* Set a momentary breakpoint of type TYPE at address specified by
  7636.    SAL.  If FRAME_ID is valid, the breakpoint is restricted to that
  7637.    frame.  */

  7638. struct breakpoint *
  7639. set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
  7640.                           struct frame_id frame_id, enum bptype type)
  7641. {
  7642.   struct breakpoint *b;

  7643.   /* If FRAME_ID is valid, it should be a real frame, not an inlined or
  7644.      tail-called one.  */
  7645.   gdb_assert (!frame_id_artificial_p (frame_id));

  7646.   b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
  7647.   b->enable_state = bp_enabled;
  7648.   b->disposition = disp_donttouch;
  7649.   b->frame_id = frame_id;

  7650.   /* If we're debugging a multi-threaded program, then we want
  7651.      momentary breakpoints to be active in only a single thread of
  7652.      control.  */
  7653.   if (in_thread_list (inferior_ptid))
  7654.     b->thread = pid_to_thread_id (inferior_ptid);

  7655.   update_global_location_list_nothrow (UGLL_MAY_INSERT);

  7656.   return b;
  7657. }

  7658. /* Make a momentary breakpoint based on the master breakpoint ORIG.
  7659.    The new breakpoint will have type TYPE, use OPS as its
  7660.    breakpoint_ops, and will set enabled to LOC_ENABLED.  */

  7661. static struct breakpoint *
  7662. momentary_breakpoint_from_master (struct breakpoint *orig,
  7663.                                   enum bptype type,
  7664.                                   const struct breakpoint_ops *ops,
  7665.                                   int loc_enabled)
  7666. {
  7667.   struct breakpoint *copy;

  7668.   copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
  7669.   copy->loc = allocate_bp_location (copy);
  7670.   set_breakpoint_location_function (copy->loc, 1);

  7671.   copy->loc->gdbarch = orig->loc->gdbarch;
  7672.   copy->loc->requested_address = orig->loc->requested_address;
  7673.   copy->loc->address = orig->loc->address;
  7674.   copy->loc->section = orig->loc->section;
  7675.   copy->loc->pspace = orig->loc->pspace;
  7676.   copy->loc->probe = orig->loc->probe;
  7677.   copy->loc->line_number = orig->loc->line_number;
  7678.   copy->loc->symtab = orig->loc->symtab;
  7679.   copy->loc->enabled = loc_enabled;
  7680.   copy->frame_id = orig->frame_id;
  7681.   copy->thread = orig->thread;
  7682.   copy->pspace = orig->pspace;

  7683.   copy->enable_state = bp_enabled;
  7684.   copy->disposition = disp_donttouch;
  7685.   copy->number = internal_breakpoint_number--;

  7686.   update_global_location_list_nothrow (UGLL_DONT_INSERT);
  7687.   return copy;
  7688. }

  7689. /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
  7690.    ORIG is NULL.  */

  7691. struct breakpoint *
  7692. clone_momentary_breakpoint (struct breakpoint *orig)
  7693. {
  7694.   /* If there's nothing to clone, then return nothing.  */
  7695.   if (orig == NULL)
  7696.     return NULL;

  7697.   return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
  7698. }

  7699. struct breakpoint *
  7700. set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
  7701.                                 enum bptype type)
  7702. {
  7703.   struct symtab_and_line sal;

  7704.   sal = find_pc_line (pc, 0);
  7705.   sal.pc = pc;
  7706.   sal.section = find_pc_overlay (pc);
  7707.   sal.explicit_pc = 1;

  7708.   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
  7709. }


  7710. /* Tell the user we have just set a breakpoint B.  */

  7711. static void
  7712. mention (struct breakpoint *b)
  7713. {
  7714.   b->ops->print_mention (b);
  7715.   if (ui_out_is_mi_like_p (current_uiout))
  7716.     return;
  7717.   printf_filtered ("\n");
  7718. }


  7719. static int bp_loc_is_permanent (struct bp_location *loc);

  7720. static struct bp_location *
  7721. add_location_to_breakpoint (struct breakpoint *b,
  7722.                             const struct symtab_and_line *sal)
  7723. {
  7724.   struct bp_location *loc, **tmp;
  7725.   CORE_ADDR adjusted_address;
  7726.   struct gdbarch *loc_gdbarch = get_sal_arch (*sal);

  7727.   if (loc_gdbarch == NULL)
  7728.     loc_gdbarch = b->gdbarch;

  7729.   /* Adjust the breakpoint's address prior to allocating a location.
  7730.      Once we call allocate_bp_location(), that mostly uninitialized
  7731.      location will be placed on the location chain.  Adjustment of the
  7732.      breakpoint may cause target_read_memory() to be called and we do
  7733.      not want its scan of the location chain to find a breakpoint and
  7734.      location that's only been partially initialized.  */
  7735.   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
  7736.                                                 sal->pc, b->type);

  7737.   /* Sort the locations by their ADDRESS.  */
  7738.   loc = allocate_bp_location (b);
  7739.   for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
  7740.        tmp = &((*tmp)->next))
  7741.     ;
  7742.   loc->next = *tmp;
  7743.   *tmp = loc;

  7744.   loc->requested_address = sal->pc;
  7745.   loc->address = adjusted_address;
  7746.   loc->pspace = sal->pspace;
  7747.   loc->probe.probe = sal->probe;
  7748.   loc->probe.objfile = sal->objfile;
  7749.   gdb_assert (loc->pspace != NULL);
  7750.   loc->section = sal->section;
  7751.   loc->gdbarch = loc_gdbarch;
  7752.   loc->line_number = sal->line;
  7753.   loc->symtab = sal->symtab;

  7754.   set_breakpoint_location_function (loc,
  7755.                                     sal->explicit_pc || sal->explicit_line);

  7756.   if (bp_loc_is_permanent (loc))
  7757.     {
  7758.       loc->inserted = 1;
  7759.       loc->permanent = 1;
  7760.     }

  7761.   return loc;
  7762. }


  7763. /* Return 1 if LOC is pointing to a permanent breakpoint,
  7764.    return 0 otherwise.  */

  7765. static int
  7766. bp_loc_is_permanent (struct bp_location *loc)
  7767. {
  7768.   int len;
  7769.   CORE_ADDR addr;
  7770.   const gdb_byte *bpoint;
  7771.   gdb_byte *target_mem;
  7772.   struct cleanup *cleanup;
  7773.   int retval = 0;

  7774.   gdb_assert (loc != NULL);

  7775.   /* bp_call_dummy breakpoint locations are usually memory locations
  7776.      where GDB just wrote a breakpoint instruction, making it look
  7777.      as if there is a permanent breakpoint at that location.  Considering
  7778.      it permanent makes GDB rely on that breakpoint instruction to stop
  7779.      the program, thus removing the need to insert its own breakpoint
  7780.      there.  This is normally expected to work, except that some versions
  7781.      of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
  7782.      0x02 while interrupts disabled, Error state) instead of reporting
  7783.      a SIGTRAP.  QEMU should probably be fixed, but in the interest of
  7784.      compatibility with versions that behave this way, we always consider
  7785.      bp_call_dummy breakpoint locations as non-permanent.  */
  7786.   if (loc->owner->type == bp_call_dummy)
  7787.     return 0;

  7788.   addr = loc->address;
  7789.   bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);

  7790.   /* Software breakpoints unsupported?  */
  7791.   if (bpoint == NULL)
  7792.     return 0;

  7793.   target_mem = alloca (len);

  7794.   /* Enable the automatic memory restoration from breakpoints while
  7795.      we read the memory.  Otherwise we could say about our temporary
  7796.      breakpoints they are permanent.  */
  7797.   cleanup = save_current_space_and_thread ();

  7798.   switch_to_program_space_and_thread (loc->pspace);
  7799.   make_show_memory_breakpoints_cleanup (0);

  7800.   if (target_read_memory (loc->address, target_mem, len) == 0
  7801.       && memcmp (target_mem, bpoint, len) == 0)
  7802.     retval = 1;

  7803.   do_cleanups (cleanup);

  7804.   return retval;
  7805. }

  7806. /* Build a command list for the dprintf corresponding to the current
  7807.    settings of the dprintf style options.  */

  7808. static void
  7809. update_dprintf_command_list (struct breakpoint *b)
  7810. {
  7811.   char *dprintf_args = b->extra_string;
  7812.   char *printf_line = NULL;

  7813.   if (!dprintf_args)
  7814.     return;

  7815.   dprintf_args = skip_spaces (dprintf_args);

  7816.   /* Allow a comma, as it may have terminated a location, but don't
  7817.      insist on it.  */
  7818.   if (*dprintf_args == ',')
  7819.     ++dprintf_args;
  7820.   dprintf_args = skip_spaces (dprintf_args);

  7821.   if (*dprintf_args != '"')
  7822.     error (_("Bad format string, missing '\"'."));

  7823.   if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
  7824.     printf_line = xstrprintf ("printf %s", dprintf_args);
  7825.   else if (strcmp (dprintf_style, dprintf_style_call) == 0)
  7826.     {
  7827.       if (!dprintf_function)
  7828.         error (_("No function supplied for dprintf call"));

  7829.       if (dprintf_channel && strlen (dprintf_channel) > 0)
  7830.         printf_line = xstrprintf ("call (void) %s (%s,%s)",
  7831.                                   dprintf_function,
  7832.                                   dprintf_channel,
  7833.                                   dprintf_args);
  7834.       else
  7835.         printf_line = xstrprintf ("call (void) %s (%s)",
  7836.                                   dprintf_function,
  7837.                                   dprintf_args);
  7838.     }
  7839.   else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
  7840.     {
  7841.       if (target_can_run_breakpoint_commands ())
  7842.         printf_line = xstrprintf ("agent-printf %s", dprintf_args);
  7843.       else
  7844.         {
  7845.           warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
  7846.           printf_line = xstrprintf ("printf %s", dprintf_args);
  7847.         }
  7848.     }
  7849.   else
  7850.     internal_error (__FILE__, __LINE__,
  7851.                     _("Invalid dprintf style."));

  7852.   gdb_assert (printf_line != NULL);
  7853.   /* Manufacture a printf sequence.  */
  7854.   {
  7855.     struct command_line *printf_cmd_line
  7856.       = xmalloc (sizeof (struct command_line));

  7857.     printf_cmd_line = xmalloc (sizeof (struct command_line));
  7858.     printf_cmd_line->control_type = simple_control;
  7859.     printf_cmd_line->body_count = 0;
  7860.     printf_cmd_line->body_list = NULL;
  7861.     printf_cmd_line->next = NULL;
  7862.     printf_cmd_line->line = printf_line;

  7863.     breakpoint_set_commands (b, printf_cmd_line);
  7864.   }
  7865. }

  7866. /* Update all dprintf commands, making their command lists reflect
  7867.    current style settings.  */

  7868. static void
  7869. update_dprintf_commands (char *args, int from_tty,
  7870.                          struct cmd_list_element *c)
  7871. {
  7872.   struct breakpoint *b;

  7873.   ALL_BREAKPOINTS (b)
  7874.     {
  7875.       if (b->type == bp_dprintf)
  7876.         update_dprintf_command_list (b);
  7877.     }
  7878. }

  7879. /* Create a breakpoint with SAL as location.  Use ADDR_STRING
  7880.    as textual description of the location, and COND_STRING
  7881.    as condition expression.  */

  7882. static void
  7883. init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
  7884.                      struct symtabs_and_lines sals, char *addr_string,
  7885.                      char *filter, char *cond_string,
  7886.                      char *extra_string,
  7887.                      enum bptype type, enum bpdisp disposition,
  7888.                      int thread, int task, int ignore_count,
  7889.                      const struct breakpoint_ops *ops, int from_tty,
  7890.                      int enabled, int internal, unsigned flags,
  7891.                      int display_canonical)
  7892. {
  7893.   int i;

  7894.   if (type == bp_hardware_breakpoint)
  7895.     {
  7896.       int target_resources_ok;

  7897.       i = hw_breakpoint_used_count ();
  7898.       target_resources_ok =
  7899.         target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
  7900.                                             i + 1, 0);
  7901.       if (target_resources_ok == 0)
  7902.         error (_("No hardware breakpoint support in the target."));
  7903.       else if (target_resources_ok < 0)
  7904.         error (_("Hardware breakpoints used exceeds limit."));
  7905.     }

  7906.   gdb_assert (sals.nelts > 0);

  7907.   for (i = 0; i < sals.nelts; ++i)
  7908.     {
  7909.       struct symtab_and_line sal = sals.sals[i];
  7910.       struct bp_location *loc;

  7911.       if (from_tty)
  7912.         {
  7913.           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
  7914.           if (!loc_gdbarch)
  7915.             loc_gdbarch = gdbarch;

  7916.           describe_other_breakpoints (loc_gdbarch,
  7917.                                       sal.pspace, sal.pc, sal.section, thread);
  7918.         }

  7919.       if (i == 0)
  7920.         {
  7921.           init_raw_breakpoint (b, gdbarch, sal, type, ops);
  7922.           b->thread = thread;
  7923.           b->task = task;

  7924.           b->cond_string = cond_string;
  7925.           b->extra_string = extra_string;
  7926.           b->ignore_count = ignore_count;
  7927.           b->enable_state = enabled ? bp_enabled : bp_disabled;
  7928.           b->disposition = disposition;

  7929.           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
  7930.             b->loc->inserted = 1;

  7931.           if (type == bp_static_tracepoint)
  7932.             {
  7933.               struct tracepoint *t = (struct tracepoint *) b;
  7934.               struct static_tracepoint_marker marker;

  7935.               if (strace_marker_p (b))
  7936.                 {
  7937.                   /* We already know the marker exists, otherwise, we
  7938.                      wouldn't see a sal for it.  */
  7939.                   char *p = &addr_string[3];
  7940.                   char *endp;
  7941.                   char *marker_str;

  7942.                   p = skip_spaces (p);

  7943.                   endp = skip_to_space (p);

  7944.                   marker_str = savestring (p, endp - p);
  7945.                   t->static_trace_marker_id = marker_str;

  7946.                   printf_filtered (_("Probed static tracepoint "
  7947.                                      "marker \"%s\"\n"),
  7948.                                    t->static_trace_marker_id);
  7949.                 }
  7950.               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
  7951.                 {
  7952.                   t->static_trace_marker_id = xstrdup (marker.str_id);
  7953.                   release_static_tracepoint_marker (&marker);

  7954.                   printf_filtered (_("Probed static tracepoint "
  7955.                                      "marker \"%s\"\n"),
  7956.                                    t->static_trace_marker_id);
  7957.                 }
  7958.               else
  7959.                 warning (_("Couldn't determine the static "
  7960.                            "tracepoint marker to probe"));
  7961.             }

  7962.           loc = b->loc;
  7963.         }
  7964.       else
  7965.         {
  7966.           loc = add_location_to_breakpoint (b, &sal);
  7967.           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
  7968.             loc->inserted = 1;
  7969.         }

  7970.       if (b->cond_string)
  7971.         {
  7972.           const char *arg = b->cond_string;

  7973.           loc->cond = parse_exp_1 (&arg, loc->address,
  7974.                                    block_for_pc (loc->address), 0);
  7975.           if (*arg)
  7976.               error (_("Garbage '%s' follows condition"), arg);
  7977.         }

  7978.       /* Dynamic printf requires and uses additional arguments on the
  7979.          command line, otherwise it's an error.  */
  7980.       if (type == bp_dprintf)
  7981.         {
  7982.           if (b->extra_string)
  7983.             update_dprintf_command_list (b);
  7984.           else
  7985.             error (_("Format string required"));
  7986.         }
  7987.       else if (b->extra_string)
  7988.         error (_("Garbage '%s' at end of command"), b->extra_string);
  7989.     }

  7990.   b->display_canonical = display_canonical;
  7991.   if (addr_string)
  7992.     b->addr_string = addr_string;
  7993.   else
  7994.     /* addr_string has to be used or breakpoint_re_set will delete
  7995.        me.  */
  7996.     b->addr_string
  7997.       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
  7998.   b->filter = filter;
  7999. }

  8000. static void
  8001. create_breakpoint_sal (struct gdbarch *gdbarch,
  8002.                        struct symtabs_and_lines sals, char *addr_string,
  8003.                        char *filter, char *cond_string,
  8004.                        char *extra_string,
  8005.                        enum bptype type, enum bpdisp disposition,
  8006.                        int thread, int task, int ignore_count,
  8007.                        const struct breakpoint_ops *ops, int from_tty,
  8008.                        int enabled, int internal, unsigned flags,
  8009.                        int display_canonical)
  8010. {
  8011.   struct breakpoint *b;
  8012.   struct cleanup *old_chain;

  8013.   if (is_tracepoint_type (type))
  8014.     {
  8015.       struct tracepoint *t;

  8016.       t = XCNEW (struct tracepoint);
  8017.       b = &t->base;
  8018.     }
  8019.   else
  8020.     b = XNEW (struct breakpoint);

  8021.   old_chain = make_cleanup (xfree, b);

  8022.   init_breakpoint_sal (b, gdbarch,
  8023.                        sals, addr_string,
  8024.                        filter, cond_string, extra_string,
  8025.                        type, disposition,
  8026.                        thread, task, ignore_count,
  8027.                        ops, from_tty,
  8028.                        enabled, internal, flags,
  8029.                        display_canonical);
  8030.   discard_cleanups (old_chain);

  8031.   install_breakpoint (internal, b, 0);
  8032. }

  8033. /* Add SALS.nelts breakpoints to the breakpoint table.  For each
  8034.    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
  8035.    value.  COND_STRING, if not NULL, specified the condition to be
  8036.    used for all breakpoints.  Essentially the only case where
  8037.    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
  8038.    function.  In that case, it's still not possible to specify
  8039.    separate conditions for different overloaded functions, so
  8040.    we take just a single condition string.

  8041.    NOTE: If the function succeeds, the caller is expected to cleanup
  8042.    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
  8043.    array contents).  If the function fails (error() is called), the
  8044.    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
  8045.    COND and SALS arrays and each of those arrays contents.  */

  8046. static void
  8047. create_breakpoints_sal (struct gdbarch *gdbarch,
  8048.                         struct linespec_result *canonical,
  8049.                         char *cond_string, char *extra_string,
  8050.                         enum bptype type, enum bpdisp disposition,
  8051.                         int thread, int task, int ignore_count,
  8052.                         const struct breakpoint_ops *ops, int from_tty,
  8053.                         int enabled, int internal, unsigned flags)
  8054. {
  8055.   int i;
  8056.   struct linespec_sals *lsal;

  8057.   if (canonical->pre_expanded)
  8058.     gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);

  8059.   for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
  8060.     {
  8061.       /* Note that 'addr_string' can be NULL in the case of a plain
  8062.          'break', without arguments.  */
  8063.       char *addr_string = (canonical->addr_string
  8064.                            ? xstrdup (canonical->addr_string)
  8065.                            : NULL);
  8066.       char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
  8067.       struct cleanup *inner = make_cleanup (xfree, addr_string);

  8068.       make_cleanup (xfree, filter_string);
  8069.       create_breakpoint_sal (gdbarch, lsal->sals,
  8070.                              addr_string,
  8071.                              filter_string,
  8072.                              cond_string, extra_string,
  8073.                              type, disposition,
  8074.                              thread, task, ignore_count, ops,
  8075.                              from_tty, enabled, internal, flags,
  8076.                              canonical->special_display);
  8077.       discard_cleanups (inner);
  8078.     }
  8079. }

  8080. /* Parse ADDRESS which is assumed to be a SAL specification possibly
  8081.    followed by conditionals.  On return, SALS contains an array of SAL
  8082.    addresses found.  ADDR_STRING contains a vector of (canonical)
  8083.    address strings.  ADDRESS points to the end of the SAL.

  8084.    The array and the line spec strings are allocated on the heap, it is
  8085.    the caller's responsibility to free them.  */

  8086. static void
  8087. parse_breakpoint_sals (char **address,
  8088.                        struct linespec_result *canonical)
  8089. {
  8090.   /* If no arg given, or if first arg is 'if ', use the default
  8091.      breakpoint.  */
  8092.   if ((*address) == NULL
  8093.       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
  8094.     {
  8095.       /* The last displayed codepoint, if it's valid, is our default breakpoint
  8096.          address.  */
  8097.       if (last_displayed_sal_is_valid ())
  8098.         {
  8099.           struct linespec_sals lsal;
  8100.           struct symtab_and_line sal;
  8101.           CORE_ADDR pc;

  8102.           init_sal (&sal);                /* Initialize to zeroes.  */
  8103.           lsal.sals.sals = (struct symtab_and_line *)
  8104.             xmalloc (sizeof (struct symtab_and_line));

  8105.           /* Set sal's pspace, pc, symtab, and line to the values
  8106.              corresponding to the last call to print_frame_info.
  8107.              Be sure to reinitialize LINE with NOTCURRENT == 0
  8108.              as the breakpoint line number is inappropriate otherwise.
  8109.              find_pc_line would adjust PC, re-set it back.  */
  8110.           get_last_displayed_sal (&sal);
  8111.           pc = sal.pc;
  8112.           sal = find_pc_line (pc, 0);

  8113.           /* "break" without arguments is equivalent to "break *PC"
  8114.              where PC is the last displayed codepoint's address.  So
  8115.              make sure to set sal.explicit_pc to prevent GDB from
  8116.              trying to expand the list of sals to include all other
  8117.              instances with the same symtab and line.  */
  8118.           sal.pc = pc;
  8119.           sal.explicit_pc = 1;

  8120.           lsal.sals.sals[0] = sal;
  8121.           lsal.sals.nelts = 1;
  8122.           lsal.canonical = NULL;

  8123.           VEC_safe_push (linespec_sals, canonical->sals, &lsal);
  8124.         }
  8125.       else
  8126.         error (_("No default breakpoint address now."));
  8127.     }
  8128.   else
  8129.     {
  8130.       struct symtab_and_line cursal = get_current_source_symtab_and_line ();

  8131.       /* Force almost all breakpoints to be in terms of the
  8132.          current_source_symtab (which is decode_line_1's default).
  8133.          This should produce the results we want almost all of the
  8134.          time while leaving default_breakpoint_* alone.

  8135.          ObjC: However, don't match an Objective-C method name which
  8136.          may have a '+' or '-' succeeded by a '['.  */
  8137.       if (last_displayed_sal_is_valid ()
  8138.           && (!cursal.symtab
  8139.               || ((strchr ("+-", (*address)[0]) != NULL)
  8140.                   && ((*address)[1] != '['))))
  8141.         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
  8142.                           get_last_displayed_symtab (),
  8143.                           get_last_displayed_line (),
  8144.                           canonical, NULL, NULL);
  8145.       else
  8146.         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
  8147.                           cursal.symtab, cursal.line, canonical, NULL, NULL);
  8148.     }
  8149. }


  8150. /* Convert each SAL into a real PC.  Verify that the PC can be
  8151.    inserted as a breakpoint.  If it can't throw an error.  */

  8152. static void
  8153. breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
  8154. {
  8155.   int i;

  8156.   for (i = 0; i < sals->nelts; i++)
  8157.     resolve_sal_pc (&sals->sals[i]);
  8158. }

  8159. /* Fast tracepoints may have restrictions on valid locations.  For
  8160.    instance, a fast tracepoint using a jump instead of a trap will
  8161.    likely have to overwrite more bytes than a trap would, and so can
  8162.    only be placed where the instruction is longer than the jump, or a
  8163.    multi-instruction sequence does not have a jump into the middle of
  8164.    it, etc.  */

  8165. static void
  8166. check_fast_tracepoint_sals (struct gdbarch *gdbarch,
  8167.                             struct symtabs_and_lines *sals)
  8168. {
  8169.   int i, rslt;
  8170.   struct symtab_and_line *sal;
  8171.   char *msg;
  8172.   struct cleanup *old_chain;

  8173.   for (i = 0; i < sals->nelts; i++)
  8174.     {
  8175.       struct gdbarch *sarch;

  8176.       sal = &sals->sals[i];

  8177.       sarch = get_sal_arch (*sal);
  8178.       /* We fall back to GDBARCH if there is no architecture
  8179.          associated with SAL.  */
  8180.       if (sarch == NULL)
  8181.         sarch = gdbarch;
  8182.       rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
  8183.                                                NULL, &msg);
  8184.       old_chain = make_cleanup (xfree, msg);

  8185.       if (!rslt)
  8186.         error (_("May not have a fast tracepoint at 0x%s%s"),
  8187.                paddress (sarch, sal->pc), (msg ? msg : ""));

  8188.       do_cleanups (old_chain);
  8189.     }
  8190. }

  8191. /* Issue an invalid thread ID error.  */

  8192. static void ATTRIBUTE_NORETURN
  8193. invalid_thread_id_error (int id)
  8194. {
  8195.   error (_("Unknown thread %d."), id);
  8196. }

  8197. /* Given TOK, a string specification of condition and thread, as
  8198.    accepted by the 'break' command, extract the condition
  8199.    string and thread number and set *COND_STRING and *THREAD.
  8200.    PC identifies the context at which the condition should be parsed.
  8201.    If no condition is found, *COND_STRING is set to NULL.
  8202.    If no thread is found, *THREAD is set to -1.  */

  8203. static void
  8204. find_condition_and_thread (const char *tok, CORE_ADDR pc,
  8205.                            char **cond_string, int *thread, int *task,
  8206.                            char **rest)
  8207. {
  8208.   *cond_string = NULL;
  8209.   *thread = -1;
  8210.   *task = 0;
  8211.   *rest = NULL;

  8212.   while (tok && *tok)
  8213.     {
  8214.       const char *end_tok;
  8215.       int toklen;
  8216.       const char *cond_start = NULL;
  8217.       const char *cond_end = NULL;

  8218.       tok = skip_spaces_const (tok);

  8219.       if ((*tok == '"' || *tok == ',') && rest)
  8220.         {
  8221.           *rest = savestring (tok, strlen (tok));
  8222.           return;
  8223.         }

  8224.       end_tok = skip_to_space_const (tok);

  8225.       toklen = end_tok - tok;

  8226.       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
  8227.         {
  8228.           struct expression *expr;

  8229.           tok = cond_start = end_tok + 1;
  8230.           expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
  8231.           xfree (expr);
  8232.           cond_end = tok;
  8233.           *cond_string = savestring (cond_start, cond_end - cond_start);
  8234.         }
  8235.       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
  8236.         {
  8237.           char *tmptok;

  8238.           tok = end_tok + 1;
  8239.           *thread = strtol (tok, &tmptok, 0);
  8240.           if (tok == tmptok)
  8241.             error (_("Junk after thread keyword."));
  8242.           if (!valid_thread_id (*thread))
  8243.             invalid_thread_id_error (*thread);
  8244.           tok = tmptok;
  8245.         }
  8246.       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
  8247.         {
  8248.           char *tmptok;

  8249.           tok = end_tok + 1;
  8250.           *task = strtol (tok, &tmptok, 0);
  8251.           if (tok == tmptok)
  8252.             error (_("Junk after task keyword."));
  8253.           if (!valid_task_id (*task))
  8254.             error (_("Unknown task %d."), *task);
  8255.           tok = tmptok;
  8256.         }
  8257.       else if (rest)
  8258.         {
  8259.           *rest = savestring (tok, strlen (tok));
  8260.           return;
  8261.         }
  8262.       else
  8263.         error (_("Junk at end of arguments."));
  8264.     }
  8265. }

  8266. /* Decode a static tracepoint marker spec.  */

  8267. static struct symtabs_and_lines
  8268. decode_static_tracepoint_spec (char **arg_p)
  8269. {
  8270.   VEC(static_tracepoint_marker_p) *markers = NULL;
  8271.   struct symtabs_and_lines sals;
  8272.   struct cleanup *old_chain;
  8273.   char *p = &(*arg_p)[3];
  8274.   char *endp;
  8275.   char *marker_str;
  8276.   int i;

  8277.   p = skip_spaces (p);

  8278.   endp = skip_to_space (p);

  8279.   marker_str = savestring (p, endp - p);
  8280.   old_chain = make_cleanup (xfree, marker_str);

  8281.   markers = target_static_tracepoint_markers_by_strid (marker_str);
  8282.   if (VEC_empty(static_tracepoint_marker_p, markers))
  8283.     error (_("No known static tracepoint marker named %s"), marker_str);

  8284.   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
  8285.   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);

  8286.   for (i = 0; i < sals.nelts; i++)
  8287.     {
  8288.       struct static_tracepoint_marker *marker;

  8289.       marker = VEC_index (static_tracepoint_marker_p, markers, i);

  8290.       init_sal (&sals.sals[i]);

  8291.       sals.sals[i] = find_pc_line (marker->address, 0);
  8292.       sals.sals[i].pc = marker->address;

  8293.       release_static_tracepoint_marker (marker);
  8294.     }

  8295.   do_cleanups (old_chain);

  8296.   *arg_p = endp;
  8297.   return sals;
  8298. }

  8299. /* Set a breakpoint.  This function is shared between CLI and MI
  8300.    functions for setting a breakpoint.  This function has two major
  8301.    modes of operations, selected by the PARSE_ARG parameter.  If
  8302.    non-zero, the function will parse ARG, extracting location,
  8303.    condition, thread and extra string.  Otherwise, ARG is just the
  8304.    breakpoint's location, with condition, thread, and extra string
  8305.    specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
  8306.    If INTERNAL is non-zero, the breakpoint number will be allocated
  8307.    from the internal breakpoint count.  Returns true if any breakpoint
  8308.    was created; false otherwise.  */

  8309. int
  8310. create_breakpoint (struct gdbarch *gdbarch,
  8311.                    char *arg, char *cond_string,
  8312.                    int thread, char *extra_string,
  8313.                    int parse_arg,
  8314.                    int tempflag, enum bptype type_wanted,
  8315.                    int ignore_count,
  8316.                    enum auto_boolean pending_break_support,
  8317.                    const struct breakpoint_ops *ops,
  8318.                    int from_tty, int enabled, int internal,
  8319.                    unsigned flags)
  8320. {
  8321.   volatile struct gdb_exception e;
  8322.   char *copy_arg = NULL;
  8323.   char *addr_start = arg;
  8324.   struct linespec_result canonical;
  8325.   struct cleanup *old_chain;
  8326.   struct cleanup *bkpt_chain = NULL;
  8327.   int pending = 0;
  8328.   int task = 0;
  8329.   int prev_bkpt_count = breakpoint_count;

  8330.   gdb_assert (ops != NULL);

  8331.   init_linespec_result (&canonical);

  8332.   TRY_CATCH (e, RETURN_MASK_ALL)
  8333.     {
  8334.       ops->create_sals_from_address (&arg, &canonical, type_wanted,
  8335.                                      addr_start, &copy_arg);
  8336.     }

  8337.   /* If caller is interested in rc value from parse, set value.  */
  8338.   switch (e.reason)
  8339.     {
  8340.     case GDB_NO_ERROR:
  8341.       if (VEC_empty (linespec_sals, canonical.sals))
  8342.         return 0;
  8343.       break;
  8344.     case RETURN_ERROR:
  8345.       switch (e.error)
  8346.         {
  8347.         case NOT_FOUND_ERROR:

  8348.           /* If pending breakpoint support is turned off, throw
  8349.              error.  */

  8350.           if (pending_break_support == AUTO_BOOLEAN_FALSE)
  8351.             throw_exception (e);

  8352.           exception_print (gdb_stderr, e);

  8353.           /* If pending breakpoint support is auto query and the user
  8354.              selects no, then simply return the error code.  */
  8355.           if (pending_break_support == AUTO_BOOLEAN_AUTO
  8356.               && !nquery (_("Make %s pending on future shared library load? "),
  8357.                           bptype_string (type_wanted)))
  8358.             return 0;

  8359.           /* At this point, either the user was queried about setting
  8360.              a pending breakpoint and selected yes, or pending
  8361.              breakpoint behavior is on and thus a pending breakpoint
  8362.              is defaulted on behalf of the user.  */
  8363.           {
  8364.             struct linespec_sals lsal;

  8365.             copy_arg = xstrdup (addr_start);
  8366.             lsal.canonical = xstrdup (copy_arg);
  8367.             lsal.sals.nelts = 1;
  8368.             lsal.sals.sals = XNEW (struct symtab_and_line);
  8369.             init_sal (&lsal.sals.sals[0]);
  8370.             pending = 1;
  8371.             VEC_safe_push (linespec_sals, canonical.sals, &lsal);
  8372.           }
  8373.           break;
  8374.         default:
  8375.           throw_exception (e);
  8376.         }
  8377.       break;
  8378.     default:
  8379.       throw_exception (e);
  8380.     }

  8381.   /* Create a chain of things that always need to be cleaned up.  */
  8382.   old_chain = make_cleanup_destroy_linespec_result (&canonical);

  8383.   /* ----------------------------- SNIP -----------------------------
  8384.      Anything added to the cleanup chain beyond this point is assumed
  8385.      to be part of a breakpoint.  If the breakpoint create succeeds
  8386.      then the memory is not reclaimed.  */
  8387.   bkpt_chain = make_cleanup (null_cleanup, 0);

  8388.   /* Resolve all line numbers to PC's and verify that the addresses
  8389.      are ok for the target.  */
  8390.   if (!pending)
  8391.     {
  8392.       int ix;
  8393.       struct linespec_sals *iter;

  8394.       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
  8395.         breakpoint_sals_to_pc (&iter->sals);
  8396.     }

  8397.   /* Fast tracepoints may have additional restrictions on location.  */
  8398.   if (!pending && type_wanted == bp_fast_tracepoint)
  8399.     {
  8400.       int ix;
  8401.       struct linespec_sals *iter;

  8402.       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
  8403.         check_fast_tracepoint_sals (gdbarch, &iter->sals);
  8404.     }

  8405.   /* Verify that condition can be parsed, before setting any
  8406.      breakpoints.  Allocate a separate condition expression for each
  8407.      breakpoint.  */
  8408.   if (!pending)
  8409.     {
  8410.       if (parse_arg)
  8411.         {
  8412.           char *rest;
  8413.           struct linespec_sals *lsal;

  8414.           lsal = VEC_index (linespec_sals, canonical.sals, 0);

  8415.           /* Here we only parse 'arg' to separate condition
  8416.              from thread number, so parsing in context of first
  8417.              sal is OK.  When setting the breakpoint we'll
  8418.              re-parse it in context of each sal.  */

  8419.           find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
  8420.                                      &thread, &task, &rest);
  8421.           if (cond_string)
  8422.             make_cleanup (xfree, cond_string);
  8423.           if (rest)
  8424.             make_cleanup (xfree, rest);
  8425.           if (rest)
  8426.             extra_string = rest;
  8427.         }
  8428.       else
  8429.         {
  8430.           if (*arg != '\0')
  8431.             error (_("Garbage '%s' at end of location"), arg);

  8432.           /* Create a private copy of condition string.  */
  8433.           if (cond_string)
  8434.             {
  8435.               cond_string = xstrdup (cond_string);
  8436.               make_cleanup (xfree, cond_string);
  8437.             }
  8438.           /* Create a private copy of any extra string.  */
  8439.           if (extra_string)
  8440.             {
  8441.               extra_string = xstrdup (extra_string);
  8442.               make_cleanup (xfree, extra_string);
  8443.             }
  8444.         }

  8445.       ops->create_breakpoints_sal (gdbarch, &canonical,
  8446.                                    cond_string, extra_string, type_wanted,
  8447.                                    tempflag ? disp_del : disp_donttouch,
  8448.                                    thread, task, ignore_count, ops,
  8449.                                    from_tty, enabled, internal, flags);
  8450.     }
  8451.   else
  8452.     {
  8453.       struct breakpoint *b;

  8454.       make_cleanup (xfree, copy_arg);

  8455.       if (is_tracepoint_type (type_wanted))
  8456.         {
  8457.           struct tracepoint *t;

  8458.           t = XCNEW (struct tracepoint);
  8459.           b = &t->base;
  8460.         }
  8461.       else
  8462.         b = XNEW (struct breakpoint);

  8463.       init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);

  8464.       b->addr_string = copy_arg;
  8465.       if (parse_arg)
  8466.         b->cond_string = NULL;
  8467.       else
  8468.         {
  8469.           /* Create a private copy of condition string.  */
  8470.           if (cond_string)
  8471.             {
  8472.               cond_string = xstrdup (cond_string);
  8473.               make_cleanup (xfree, cond_string);
  8474.             }
  8475.           b->cond_string = cond_string;
  8476.         }
  8477.       b->extra_string = NULL;
  8478.       b->ignore_count = ignore_count;
  8479.       b->disposition = tempflag ? disp_del : disp_donttouch;
  8480.       b->condition_not_parsed = 1;
  8481.       b->enable_state = enabled ? bp_enabled : bp_disabled;
  8482.       if ((type_wanted != bp_breakpoint
  8483.            && type_wanted != bp_hardware_breakpoint) || thread != -1)
  8484.         b->pspace = current_program_space;

  8485.       install_breakpoint (internal, b, 0);
  8486.     }

  8487.   if (VEC_length (linespec_sals, canonical.sals) > 1)
  8488.     {
  8489.       warning (_("Multiple breakpoints were set.\nUse the "
  8490.                  "\"delete\" command to delete unwanted breakpoints."));
  8491.       prev_breakpoint_count = prev_bkpt_count;
  8492.     }

  8493.   /* That's it.  Discard the cleanups for data inserted into the
  8494.      breakpoint.  */
  8495.   discard_cleanups (bkpt_chain);
  8496.   /* But cleanup everything else.  */
  8497.   do_cleanups (old_chain);

  8498.   /* error call may happen here - have BKPT_CHAIN already discarded.  */
  8499.   update_global_location_list (UGLL_MAY_INSERT);

  8500.   return 1;
  8501. }

  8502. /* Set a breakpoint.
  8503.    ARG is a string describing breakpoint address,
  8504.    condition, and thread.
  8505.    FLAG specifies if a breakpoint is hardware on,
  8506.    and if breakpoint is temporary, using BP_HARDWARE_FLAG
  8507.    and BP_TEMPFLAG.  */

  8508. static void
  8509. break_command_1 (char *arg, int flag, int from_tty)
  8510. {
  8511.   int tempflag = flag & BP_TEMPFLAG;
  8512.   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
  8513.                              ? bp_hardware_breakpoint
  8514.                              : bp_breakpoint);
  8515.   struct breakpoint_ops *ops;
  8516.   const char *arg_cp = arg;

  8517.   /* Matching breakpoints on probes.  */
  8518.   if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
  8519.     ops = &bkpt_probe_breakpoint_ops;
  8520.   else
  8521.     ops = &bkpt_breakpoint_ops;

  8522.   create_breakpoint (get_current_arch (),
  8523.                      arg,
  8524.                      NULL, 0, NULL, 1 /* parse arg */,
  8525.                      tempflag, type_wanted,
  8526.                      0 /* Ignore count */,
  8527.                      pending_break_support,
  8528.                      ops,
  8529.                      from_tty,
  8530.                      1 /* enabled */,
  8531.                      0 /* internal */,
  8532.                      0);
  8533. }

  8534. /* Helper function for break_command_1 and disassemble_command.  */

  8535. void
  8536. resolve_sal_pc (struct symtab_and_line *sal)
  8537. {
  8538.   CORE_ADDR pc;

  8539.   if (sal->pc == 0 && sal->symtab != NULL)
  8540.     {
  8541.       if (!find_line_pc (sal->symtab, sal->line, &pc))
  8542.         error (_("No line %d in file \"%s\"."),
  8543.                sal->line, symtab_to_filename_for_display (sal->symtab));
  8544.       sal->pc = pc;

  8545.       /* If this SAL corresponds to a breakpoint inserted using a line
  8546.          number, then skip the function prologue if necessary.  */
  8547.       if (sal->explicit_line)
  8548.         skip_prologue_sal (sal);
  8549.     }

  8550.   if (sal->section == 0 && sal->symtab != NULL)
  8551.     {
  8552.       const struct blockvector *bv;
  8553.       const struct block *b;
  8554.       struct symbol *sym;

  8555.       bv = blockvector_for_pc_sect (sal->pc, 0, &b,
  8556.                                     SYMTAB_COMPUNIT (sal->symtab));
  8557.       if (bv != NULL)
  8558.         {
  8559.           sym = block_linkage_function (b);
  8560.           if (sym != NULL)
  8561.             {
  8562.               fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
  8563.               sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
  8564.                                                  sym);
  8565.             }
  8566.           else
  8567.             {
  8568.               /* It really is worthwhile to have the section, so we'll
  8569.                  just have to look harder. This case can be executed
  8570.                  if we have line numbers but no functions (as can
  8571.                  happen in assembly source).  */

  8572.               struct bound_minimal_symbol msym;
  8573.               struct cleanup *old_chain = save_current_space_and_thread ();

  8574.               switch_to_program_space_and_thread (sal->pspace);

  8575.               msym = lookup_minimal_symbol_by_pc (sal->pc);
  8576.               if (msym.minsym)
  8577.                 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);

  8578.               do_cleanups (old_chain);
  8579.             }
  8580.         }
  8581.     }
  8582. }

  8583. void
  8584. break_command (char *arg, int from_tty)
  8585. {
  8586.   break_command_1 (arg, 0, from_tty);
  8587. }

  8588. void
  8589. tbreak_command (char *arg, int from_tty)
  8590. {
  8591.   break_command_1 (arg, BP_TEMPFLAG, from_tty);
  8592. }

  8593. static void
  8594. hbreak_command (char *arg, int from_tty)
  8595. {
  8596.   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
  8597. }

  8598. static void
  8599. thbreak_command (char *arg, int from_tty)
  8600. {
  8601.   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
  8602. }

  8603. static void
  8604. stop_command (char *arg, int from_tty)
  8605. {
  8606.   printf_filtered (_("Specify the type of breakpoint to set.\n\
  8607. Usage: stop in <function | address>\n\
  8608.        stop at <line>\n"));
  8609. }

  8610. static void
  8611. stopin_command (char *arg, int from_tty)
  8612. {
  8613.   int badInput = 0;

  8614.   if (arg == (char *) NULL)
  8615.     badInput = 1;
  8616.   else if (*arg != '*')
  8617.     {
  8618.       char *argptr = arg;
  8619.       int hasColon = 0;

  8620.       /* Look for a ':'.  If this is a line number specification, then
  8621.          say it is bad, otherwise, it should be an address or
  8622.          function/method name.  */
  8623.       while (*argptr && !hasColon)
  8624.         {
  8625.           hasColon = (*argptr == ':');
  8626.           argptr++;
  8627.         }

  8628.       if (hasColon)
  8629.         badInput = (*argptr != ':');        /* Not a class::method */
  8630.       else
  8631.         badInput = isdigit (*arg);        /* a simple line number */
  8632.     }

  8633.   if (badInput)
  8634.     printf_filtered (_("Usage: stop in <function | address>\n"));
  8635.   else
  8636.     break_command_1 (arg, 0, from_tty);
  8637. }

  8638. static void
  8639. stopat_command (char *arg, int from_tty)
  8640. {
  8641.   int badInput = 0;

  8642.   if (arg == (char *) NULL || *arg == '*')        /* no line number */
  8643.     badInput = 1;
  8644.   else
  8645.     {
  8646.       char *argptr = arg;
  8647.       int hasColon = 0;

  8648.       /* Look for a ':'.  If there is a '::' then get out, otherwise
  8649.          it is probably a line number.  */
  8650.       while (*argptr && !hasColon)
  8651.         {
  8652.           hasColon = (*argptr == ':');
  8653.           argptr++;
  8654.         }

  8655.       if (hasColon)
  8656.         badInput = (*argptr == ':');        /* we have class::method */
  8657.       else
  8658.         badInput = !isdigit (*arg);        /* not a line number */
  8659.     }

  8660.   if (badInput)
  8661.     printf_filtered (_("Usage: stop at <line>\n"));
  8662.   else
  8663.     break_command_1 (arg, 0, from_tty);
  8664. }

  8665. /* The dynamic printf command is mostly like a regular breakpoint, but
  8666.    with a prewired command list consisting of a single output command,
  8667.    built from extra arguments supplied on the dprintf command
  8668.    line.  */

  8669. static void
  8670. dprintf_command (char *arg, int from_tty)
  8671. {
  8672.   create_breakpoint (get_current_arch (),
  8673.                      arg,
  8674.                      NULL, 0, NULL, 1 /* parse arg */,
  8675.                      0, bp_dprintf,
  8676.                      0 /* Ignore count */,
  8677.                      pending_break_support,
  8678.                      &dprintf_breakpoint_ops,
  8679.                      from_tty,
  8680.                      1 /* enabled */,
  8681.                      0 /* internal */,
  8682.                      0);
  8683. }

  8684. static void
  8685. agent_printf_command (char *arg, int from_tty)
  8686. {
  8687.   error (_("May only run agent-printf on the target"));
  8688. }

  8689. /* Implement the "breakpoint_hit" breakpoint_ops method for
  8690.    ranged breakpoints.  */

  8691. static int
  8692. breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
  8693.                                   struct address_space *aspace,
  8694.                                   CORE_ADDR bp_addr,
  8695.                                   const struct target_waitstatus *ws)
  8696. {
  8697.   if (ws->kind != TARGET_WAITKIND_STOPPED
  8698.       || ws->value.sig != GDB_SIGNAL_TRAP)
  8699.     return 0;

  8700.   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
  8701.                                          bl->length, aspace, bp_addr);
  8702. }

  8703. /* Implement the "resources_needed" breakpoint_ops method for
  8704.    ranged breakpoints.  */

  8705. static int
  8706. resources_needed_ranged_breakpoint (const struct bp_location *bl)
  8707. {
  8708.   return target_ranged_break_num_registers ();
  8709. }

  8710. /* Implement the "print_it" breakpoint_ops method for
  8711.    ranged breakpoints.  */

  8712. static enum print_stop_action
  8713. print_it_ranged_breakpoint (bpstat bs)
  8714. {
  8715.   struct breakpoint *b = bs->breakpoint_at;
  8716.   struct bp_location *bl = b->loc;
  8717.   struct ui_out *uiout = current_uiout;

  8718.   gdb_assert (b->type == bp_hardware_breakpoint);

  8719.   /* Ranged breakpoints have only one location.  */
  8720.   gdb_assert (bl && bl->next == NULL);

  8721.   annotate_breakpoint (b->number);
  8722.   if (b->disposition == disp_del)
  8723.     ui_out_text (uiout, "\nTemporary ranged breakpoint ");
  8724.   else
  8725.     ui_out_text (uiout, "\nRanged breakpoint ");
  8726.   if (ui_out_is_mi_like_p (uiout))
  8727.     {
  8728.       ui_out_field_string (uiout, "reason",
  8729.                       async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
  8730.       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
  8731.     }
  8732.   ui_out_field_int (uiout, "bkptno", b->number);
  8733.   ui_out_text (uiout, ", ");

  8734.   return PRINT_SRC_AND_LOC;
  8735. }

  8736. /* Implement the "print_one" breakpoint_ops method for
  8737.    ranged breakpoints.  */

  8738. static void
  8739. print_one_ranged_breakpoint (struct breakpoint *b,
  8740.                              struct bp_location **last_loc)
  8741. {
  8742.   struct bp_location *bl = b->loc;
  8743.   struct value_print_options opts;
  8744.   struct ui_out *uiout = current_uiout;

  8745.   /* Ranged breakpoints have only one location.  */
  8746.   gdb_assert (bl && bl->next == NULL);

  8747.   get_user_print_options (&opts);

  8748.   if (opts.addressprint)
  8749.     /* We don't print the address range here, it will be printed later
  8750.        by print_one_detail_ranged_breakpoint.  */
  8751.     ui_out_field_skip (uiout, "addr");
  8752.   annotate_field (5);
  8753.   print_breakpoint_location (b, bl);
  8754.   *last_loc = bl;
  8755. }

  8756. /* Implement the "print_one_detail" breakpoint_ops method for
  8757.    ranged breakpoints.  */

  8758. static void
  8759. print_one_detail_ranged_breakpoint (const struct breakpoint *b,
  8760.                                     struct ui_out *uiout)
  8761. {
  8762.   CORE_ADDR address_start, address_end;
  8763.   struct bp_location *bl = b->loc;
  8764.   struct ui_file *stb = mem_fileopen ();
  8765.   struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);

  8766.   gdb_assert (bl);

  8767.   address_start = bl->address;
  8768.   address_end = address_start + bl->length - 1;

  8769.   ui_out_text (uiout, "\taddress range: ");
  8770.   fprintf_unfiltered (stb, "[%s, %s]",
  8771.                       print_core_address (bl->gdbarch, address_start),
  8772.                       print_core_address (bl->gdbarch, address_end));
  8773.   ui_out_field_stream (uiout, "addr", stb);
  8774.   ui_out_text (uiout, "\n");

  8775.   do_cleanups (cleanup);
  8776. }

  8777. /* Implement the "print_mention" breakpoint_ops method for
  8778.    ranged breakpoints.  */

  8779. static void
  8780. print_mention_ranged_breakpoint (struct breakpoint *b)
  8781. {
  8782.   struct bp_location *bl = b->loc;
  8783.   struct ui_out *uiout = current_uiout;

  8784.   gdb_assert (bl);
  8785.   gdb_assert (b->type == bp_hardware_breakpoint);

  8786.   if (ui_out_is_mi_like_p (uiout))
  8787.     return;

  8788.   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
  8789.                    b->number, paddress (bl->gdbarch, bl->address),
  8790.                    paddress (bl->gdbarch, bl->address + bl->length - 1));
  8791. }

  8792. /* Implement the "print_recreate" breakpoint_ops method for
  8793.    ranged breakpoints.  */

  8794. static void
  8795. print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
  8796. {
  8797.   fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
  8798.                       b->addr_string_range_end);
  8799.   print_recreate_thread (b, fp);
  8800. }

  8801. /* The breakpoint_ops structure to be used in ranged breakpoints.  */

  8802. static struct breakpoint_ops ranged_breakpoint_ops;

  8803. /* Find the address where the end of the breakpoint range should be
  8804.    placed, given the SAL of the end of the range.  This is so that if
  8805.    the user provides a line number, the end of the range is set to the
  8806.    last instruction of the given line.  */

  8807. static CORE_ADDR
  8808. find_breakpoint_range_end (struct symtab_and_line sal)
  8809. {
  8810.   CORE_ADDR end;

  8811.   /* If the user provided a PC value, use it.  Otherwise,
  8812.      find the address of the end of the given location.  */
  8813.   if (sal.explicit_pc)
  8814.     end = sal.pc;
  8815.   else
  8816.     {
  8817.       int ret;
  8818.       CORE_ADDR start;

  8819.       ret = find_line_pc_range (sal, &start, &end);
  8820.       if (!ret)
  8821.         error (_("Could not find location of the end of the range."));

  8822.       /* find_line_pc_range returns the start of the next line.  */
  8823.       end--;
  8824.     }

  8825.   return end;
  8826. }

  8827. /* Implement the "break-range" CLI command.  */

  8828. static void
  8829. break_range_command (char *arg, int from_tty)
  8830. {
  8831.   char *arg_start, *addr_string_start, *addr_string_end;
  8832.   struct linespec_result canonical_start, canonical_end;
  8833.   int bp_count, can_use_bp, length;
  8834.   CORE_ADDR end;
  8835.   struct breakpoint *b;
  8836.   struct symtab_and_line sal_start, sal_end;
  8837.   struct cleanup *cleanup_bkpt;
  8838.   struct linespec_sals *lsal_start, *lsal_end;

  8839.   /* We don't support software ranged breakpoints.  */
  8840.   if (target_ranged_break_num_registers () < 0)
  8841.     error (_("This target does not support hardware ranged breakpoints."));

  8842.   bp_count = hw_breakpoint_used_count ();
  8843.   bp_count += target_ranged_break_num_registers ();
  8844.   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
  8845.                                                    bp_count, 0);
  8846.   if (can_use_bp < 0)
  8847.     error (_("Hardware breakpoints used exceeds limit."));

  8848.   arg = skip_spaces (arg);
  8849.   if (arg == NULL || arg[0] == '\0')
  8850.     error(_("No address range specified."));

  8851.   init_linespec_result (&canonical_start);

  8852.   arg_start = arg;
  8853.   parse_breakpoint_sals (&arg, &canonical_start);

  8854.   cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);

  8855.   if (arg[0] != ',')
  8856.     error (_("Too few arguments."));
  8857.   else if (VEC_empty (linespec_sals, canonical_start.sals))
  8858.     error (_("Could not find location of the beginning of the range."));

  8859.   lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);

  8860.   if (VEC_length (linespec_sals, canonical_start.sals) > 1
  8861.       || lsal_start->sals.nelts != 1)
  8862.     error (_("Cannot create a ranged breakpoint with multiple locations."));

  8863.   sal_start = lsal_start->sals.sals[0];
  8864.   addr_string_start = savestring (arg_start, arg - arg_start);
  8865.   make_cleanup (xfree, addr_string_start);

  8866.   arg++;        /* Skip the comma.  */
  8867.   arg = skip_spaces (arg);

  8868.   /* Parse the end location.  */

  8869.   init_linespec_result (&canonical_end);
  8870.   arg_start = arg;

  8871.   /* We call decode_line_full directly here instead of using
  8872.      parse_breakpoint_sals because we need to specify the start location's
  8873.      symtab and line as the default symtab and line for the end of the
  8874.      range.  This makes it possible to have ranges like "foo.c:27, +14",
  8875.      where +14 means 14 lines from the start location.  */
  8876.   decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
  8877.                     sal_start.symtab, sal_start.line,
  8878.                     &canonical_end, NULL, NULL);

  8879.   make_cleanup_destroy_linespec_result (&canonical_end);

  8880.   if (VEC_empty (linespec_sals, canonical_end.sals))
  8881.     error (_("Could not find location of the end of the range."));

  8882.   lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
  8883.   if (VEC_length (linespec_sals, canonical_end.sals) > 1
  8884.       || lsal_end->sals.nelts != 1)
  8885.     error (_("Cannot create a ranged breakpoint with multiple locations."));

  8886.   sal_end = lsal_end->sals.sals[0];
  8887.   addr_string_end = savestring (arg_start, arg - arg_start);
  8888.   make_cleanup (xfree, addr_string_end);

  8889.   end = find_breakpoint_range_end (sal_end);
  8890.   if (sal_start.pc > end)
  8891.     error (_("Invalid address range, end precedes start."));

  8892.   length = end - sal_start.pc + 1;
  8893.   if (length < 0)
  8894.     /* Length overflowed.  */
  8895.     error (_("Address range too large."));
  8896.   else if (length == 1)
  8897.     {
  8898.       /* This range is simple enough to be handled by
  8899.          the `hbreak' command.  */
  8900.       hbreak_command (addr_string_start, 1);

  8901.       do_cleanups (cleanup_bkpt);

  8902.       return;
  8903.     }

  8904.   /* Now set up the breakpoint.  */
  8905.   b = set_raw_breakpoint (get_current_arch (), sal_start,
  8906.                           bp_hardware_breakpoint, &ranged_breakpoint_ops);
  8907.   set_breakpoint_count (breakpoint_count + 1);
  8908.   b->number = breakpoint_count;
  8909.   b->disposition = disp_donttouch;
  8910.   b->addr_string = xstrdup (addr_string_start);
  8911.   b->addr_string_range_end = xstrdup (addr_string_end);
  8912.   b->loc->length = length;

  8913.   do_cleanups (cleanup_bkpt);

  8914.   mention (b);
  8915.   observer_notify_breakpoint_created (b);
  8916.   update_global_location_list (UGLL_MAY_INSERT);
  8917. }

  8918. /*  Return non-zero if EXP is verified as constant.  Returned zero
  8919.     means EXP is variable.  Also the constant detection may fail for
  8920.     some constant expressions and in such case still falsely return
  8921.     zero.  */

  8922. static int
  8923. watchpoint_exp_is_const (const struct expression *exp)
  8924. {
  8925.   int i = exp->nelts;

  8926.   while (i > 0)
  8927.     {
  8928.       int oplenp, argsp;

  8929.       /* We are only interested in the descriptor of each element.  */
  8930.       operator_length (exp, i, &oplenp, &argsp);
  8931.       i -= oplenp;

  8932.       switch (exp->elts[i].opcode)
  8933.         {
  8934.         case BINOP_ADD:
  8935.         case BINOP_SUB:
  8936.         case BINOP_MUL:
  8937.         case BINOP_DIV:
  8938.         case BINOP_REM:
  8939.         case BINOP_MOD:
  8940.         case BINOP_LSH:
  8941.         case BINOP_RSH:
  8942.         case BINOP_LOGICAL_AND:
  8943.         case BINOP_LOGICAL_OR:
  8944.         case BINOP_BITWISE_AND:
  8945.         case BINOP_BITWISE_IOR:
  8946.         case BINOP_BITWISE_XOR:
  8947.         case BINOP_EQUAL:
  8948.         case BINOP_NOTEQUAL:
  8949.         case BINOP_LESS:
  8950.         case BINOP_GTR:
  8951.         case BINOP_LEQ:
  8952.         case BINOP_GEQ:
  8953.         case BINOP_REPEAT:
  8954.         case BINOP_COMMA:
  8955.         case BINOP_EXP:
  8956.         case BINOP_MIN:
  8957.         case BINOP_MAX:
  8958.         case BINOP_INTDIV:
  8959.         case BINOP_CONCAT:
  8960.         case TERNOP_COND:
  8961.         case TERNOP_SLICE:

  8962.         case OP_LONG:
  8963.         case OP_DOUBLE:
  8964.         case OP_DECFLOAT:
  8965.         case OP_LAST:
  8966.         case OP_COMPLEX:
  8967.         case OP_STRING:
  8968.         case OP_ARRAY:
  8969.         case OP_TYPE:
  8970.         case OP_TYPEOF:
  8971.         case OP_DECLTYPE:
  8972.         case OP_TYPEID:
  8973.         case OP_NAME:
  8974.         case OP_OBJC_NSSTRING:

  8975.         case UNOP_NEG:
  8976.         case UNOP_LOGICAL_NOT:
  8977.         case UNOP_COMPLEMENT:
  8978.         case UNOP_ADDR:
  8979.         case UNOP_HIGH:
  8980.         case UNOP_CAST:

  8981.         case UNOP_CAST_TYPE:
  8982.         case UNOP_REINTERPRET_CAST:
  8983.         case UNOP_DYNAMIC_CAST:
  8984.           /* Unary, binary and ternary operators: We have to check
  8985.              their operands.  If they are constant, then so is the
  8986.              result of that operation.  For instance, if A and B are
  8987.              determined to be constants, then so is "A + B".

  8988.              UNOP_IND is one exception to the rule above, because the
  8989.              value of *ADDR is not necessarily a constant, even when
  8990.              ADDR is.  */
  8991.           break;

  8992.         case OP_VAR_VALUE:
  8993.           /* Check whether the associated symbol is a constant.

  8994.              We use SYMBOL_CLASS rather than TYPE_CONST because it's
  8995.              possible that a buggy compiler could mark a variable as
  8996.              constant even when it is not, and TYPE_CONST would return
  8997.              true in this case, while SYMBOL_CLASS wouldn't.

  8998.              We also have to check for function symbols because they
  8999.              are always constant.  */
  9000.           {
  9001.             struct symbol *s = exp->elts[i + 2].symbol;

  9002.             if (SYMBOL_CLASS (s) != LOC_BLOCK
  9003.                 && SYMBOL_CLASS (s) != LOC_CONST
  9004.                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
  9005.               return 0;
  9006.             break;
  9007.           }

  9008.         /* The default action is to return 0 because we are using
  9009.            the optimistic approach here: If we don't know something,
  9010.            then it is not a constant.  */
  9011.         default:
  9012.           return 0;
  9013.         }
  9014.     }

  9015.   return 1;
  9016. }

  9017. /* Implement the "dtor" breakpoint_ops method for watchpoints.  */

  9018. static void
  9019. dtor_watchpoint (struct breakpoint *self)
  9020. {
  9021.   struct watchpoint *w = (struct watchpoint *) self;

  9022.   xfree (w->cond_exp);
  9023.   xfree (w->exp);
  9024.   xfree (w->exp_string);
  9025.   xfree (w->exp_string_reparse);
  9026.   value_free (w->val);

  9027.   base_breakpoint_ops.dtor (self);
  9028. }

  9029. /* Implement the "re_set" breakpoint_ops method for watchpoints.  */

  9030. static void
  9031. re_set_watchpoint (struct breakpoint *b)
  9032. {
  9033.   struct watchpoint *w = (struct watchpoint *) b;

  9034.   /* Watchpoint can be either on expression using entirely global
  9035.      variables, or it can be on local variables.

  9036.      Watchpoints of the first kind are never auto-deleted, and even
  9037.      persist across program restarts.  Since they can use variables
  9038.      from shared libraries, we need to reparse expression as libraries
  9039.      are loaded and unloaded.

  9040.      Watchpoints on local variables can also change meaning as result
  9041.      of solib event.  For example, if a watchpoint uses both a local
  9042.      and a global variables in expression, it's a local watchpoint,
  9043.      but unloading of a shared library will make the expression
  9044.      invalid.  This is not a very common use case, but we still
  9045.      re-evaluate expression, to avoid surprises to the user.

  9046.      Note that for local watchpoints, we re-evaluate it only if
  9047.      watchpoints frame id is still valid.  If it's not, it means the
  9048.      watchpoint is out of scope and will be deleted soon.  In fact,
  9049.      I'm not sure we'll ever be called in this case.

  9050.      If a local watchpoint's frame id is still valid, then
  9051.      w->exp_valid_block is likewise valid, and we can safely use it.

  9052.      Don't do anything about disabled watchpoints, since they will be
  9053.      reevaluated again when enabled.  */
  9054.   update_watchpoint (w, 1 /* reparse */);
  9055. }

  9056. /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */

  9057. static int
  9058. insert_watchpoint (struct bp_location *bl)
  9059. {
  9060.   struct watchpoint *w = (struct watchpoint *) bl->owner;
  9061.   int length = w->exact ? 1 : bl->length;

  9062.   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
  9063.                                    w->cond_exp);
  9064. }

  9065. /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */

  9066. static int
  9067. remove_watchpoint (struct bp_location *bl)
  9068. {
  9069.   struct watchpoint *w = (struct watchpoint *) bl->owner;
  9070.   int length = w->exact ? 1 : bl->length;

  9071.   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
  9072.                                    w->cond_exp);
  9073. }

  9074. static int
  9075. breakpoint_hit_watchpoint (const struct bp_location *bl,
  9076.                            struct address_space *aspace, CORE_ADDR bp_addr,
  9077.                            const struct target_waitstatus *ws)
  9078. {
  9079.   struct breakpoint *b = bl->owner;
  9080.   struct watchpoint *w = (struct watchpoint *) b;

  9081.   /* Continuable hardware watchpoints are treated as non-existent if the
  9082.      reason we stopped wasn't a hardware watchpoint (we didn't stop on
  9083.      some data address).  Otherwise gdb won't stop on a break instruction
  9084.      in the code (not from a breakpoint) when a hardware watchpoint has
  9085.      been defined.  Also skip watchpoints which we know did not trigger
  9086.      (did not match the data address).  */
  9087.   if (is_hardware_watchpoint (b)
  9088.       && w->watchpoint_triggered == watch_triggered_no)
  9089.     return 0;

  9090.   return 1;
  9091. }

  9092. static void
  9093. check_status_watchpoint (bpstat bs)
  9094. {
  9095.   gdb_assert (is_watchpoint (bs->breakpoint_at));

  9096.   bpstat_check_watchpoint (bs);
  9097. }

  9098. /* Implement the "resources_needed" breakpoint_ops method for
  9099.    hardware watchpoints.  */

  9100. static int
  9101. resources_needed_watchpoint (const struct bp_location *bl)
  9102. {
  9103.   struct watchpoint *w = (struct watchpoint *) bl->owner;
  9104.   int length = w->exact? 1 : bl->length;

  9105.   return target_region_ok_for_hw_watchpoint (bl->address, length);
  9106. }

  9107. /* Implement the "works_in_software_mode" breakpoint_ops method for
  9108.    hardware watchpoints.  */

  9109. static int
  9110. works_in_software_mode_watchpoint (const struct breakpoint *b)
  9111. {
  9112.   /* Read and access watchpoints only work with hardware support.  */
  9113.   return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
  9114. }

  9115. static enum print_stop_action
  9116. print_it_watchpoint (bpstat bs)
  9117. {
  9118.   struct cleanup *old_chain;
  9119.   struct breakpoint *b;
  9120.   struct ui_file *stb;
  9121.   enum print_stop_action result;
  9122.   struct watchpoint *w;
  9123.   struct ui_out *uiout = current_uiout;

  9124.   gdb_assert (bs->bp_location_at != NULL);

  9125.   b = bs->breakpoint_at;
  9126.   w = (struct watchpoint *) b;

  9127.   stb = mem_fileopen ();
  9128.   old_chain = make_cleanup_ui_file_delete (stb);

  9129.   switch (b->type)
  9130.     {
  9131.     case bp_watchpoint:
  9132.     case bp_hardware_watchpoint:
  9133.       annotate_watchpoint (b->number);
  9134.       if (ui_out_is_mi_like_p (uiout))
  9135.         ui_out_field_string
  9136.           (uiout, "reason",
  9137.            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
  9138.       mention (b);
  9139.       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
  9140.       ui_out_text (uiout, "\nOld value = ");
  9141.       watchpoint_value_print (bs->old_val, stb);
  9142.       ui_out_field_stream (uiout, "old", stb);
  9143.       ui_out_text (uiout, "\nNew value = ");
  9144.       watchpoint_value_print (w->val, stb);
  9145.       ui_out_field_stream (uiout, "new", stb);
  9146.       ui_out_text (uiout, "\n");
  9147.       /* More than one watchpoint may have been triggered.  */
  9148.       result = PRINT_UNKNOWN;
  9149.       break;

  9150.     case bp_read_watchpoint:
  9151.       if (ui_out_is_mi_like_p (uiout))
  9152.         ui_out_field_string
  9153.           (uiout, "reason",
  9154.            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
  9155.       mention (b);
  9156.       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
  9157.       ui_out_text (uiout, "\nValue = ");
  9158.       watchpoint_value_print (w->val, stb);
  9159.       ui_out_field_stream (uiout, "value", stb);
  9160.       ui_out_text (uiout, "\n");
  9161.       result = PRINT_UNKNOWN;
  9162.       break;

  9163.     case bp_access_watchpoint:
  9164.       if (bs->old_val != NULL)
  9165.         {
  9166.           annotate_watchpoint (b->number);
  9167.           if (ui_out_is_mi_like_p (uiout))
  9168.             ui_out_field_string
  9169.               (uiout, "reason",
  9170.                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
  9171.           mention (b);
  9172.           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
  9173.           ui_out_text (uiout, "\nOld value = ");
  9174.           watchpoint_value_print (bs->old_val, stb);
  9175.           ui_out_field_stream (uiout, "old", stb);
  9176.           ui_out_text (uiout, "\nNew value = ");
  9177.         }
  9178.       else
  9179.         {
  9180.           mention (b);
  9181.           if (ui_out_is_mi_like_p (uiout))
  9182.             ui_out_field_string
  9183.               (uiout, "reason",
  9184.                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
  9185.           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
  9186.           ui_out_text (uiout, "\nValue = ");
  9187.         }
  9188.       watchpoint_value_print (w->val, stb);
  9189.       ui_out_field_stream (uiout, "new", stb);
  9190.       ui_out_text (uiout, "\n");
  9191.       result = PRINT_UNKNOWN;
  9192.       break;
  9193.     default:
  9194.       result = PRINT_UNKNOWN;
  9195.     }

  9196.   do_cleanups (old_chain);
  9197.   return result;
  9198. }

  9199. /* Implement the "print_mention" breakpoint_ops method for hardware
  9200.    watchpoints.  */

  9201. static void
  9202. print_mention_watchpoint (struct breakpoint *b)
  9203. {
  9204.   struct cleanup *ui_out_chain;
  9205.   struct watchpoint *w = (struct watchpoint *) b;
  9206.   struct ui_out *uiout = current_uiout;

  9207.   switch (b->type)
  9208.     {
  9209.     case bp_watchpoint:
  9210.       ui_out_text (uiout, "Watchpoint ");
  9211.       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
  9212.       break;
  9213.     case bp_hardware_watchpoint:
  9214.       ui_out_text (uiout, "Hardware watchpoint ");
  9215.       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
  9216.       break;
  9217.     case bp_read_watchpoint:
  9218.       ui_out_text (uiout, "Hardware read watchpoint ");
  9219.       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
  9220.       break;
  9221.     case bp_access_watchpoint:
  9222.       ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
  9223.       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
  9224.       break;
  9225.     default:
  9226.       internal_error (__FILE__, __LINE__,
  9227.                       _("Invalid hardware watchpoint type."));
  9228.     }

  9229.   ui_out_field_int (uiout, "number", b->number);
  9230.   ui_out_text (uiout, ": ");
  9231.   ui_out_field_string (uiout, "exp", w->exp_string);
  9232.   do_cleanups (ui_out_chain);
  9233. }

  9234. /* Implement the "print_recreate" breakpoint_ops method for
  9235.    watchpoints.  */

  9236. static void
  9237. print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
  9238. {
  9239.   struct watchpoint *w = (struct watchpoint *) b;

  9240.   switch (b->type)
  9241.     {
  9242.     case bp_watchpoint:
  9243.     case bp_hardware_watchpoint:
  9244.       fprintf_unfiltered (fp, "watch");
  9245.       break;
  9246.     case bp_read_watchpoint:
  9247.       fprintf_unfiltered (fp, "rwatch");
  9248.       break;
  9249.     case bp_access_watchpoint:
  9250.       fprintf_unfiltered (fp, "awatch");
  9251.       break;
  9252.     default:
  9253.       internal_error (__FILE__, __LINE__,
  9254.                       _("Invalid watchpoint type."));
  9255.     }

  9256.   fprintf_unfiltered (fp, " %s", w->exp_string);
  9257.   print_recreate_thread (b, fp);
  9258. }

  9259. /* Implement the "explains_signal" breakpoint_ops method for
  9260.    watchpoints.  */

  9261. static int
  9262. explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
  9263. {
  9264.   /* A software watchpoint cannot cause a signal other than
  9265.      GDB_SIGNAL_TRAP.  */
  9266.   if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
  9267.     return 0;

  9268.   return 1;
  9269. }

  9270. /* The breakpoint_ops structure to be used in hardware watchpoints.  */

  9271. static struct breakpoint_ops watchpoint_breakpoint_ops;

  9272. /* Implement the "insert" breakpoint_ops method for
  9273.    masked hardware watchpoints.  */

  9274. static int
  9275. insert_masked_watchpoint (struct bp_location *bl)
  9276. {
  9277.   struct watchpoint *w = (struct watchpoint *) bl->owner;

  9278.   return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
  9279.                                         bl->watchpoint_type);
  9280. }

  9281. /* Implement the "remove" breakpoint_ops method for
  9282.    masked hardware watchpoints.  */

  9283. static int
  9284. remove_masked_watchpoint (struct bp_location *bl)
  9285. {
  9286.   struct watchpoint *w = (struct watchpoint *) bl->owner;

  9287.   return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
  9288.                                         bl->watchpoint_type);
  9289. }

  9290. /* Implement the "resources_needed" breakpoint_ops method for
  9291.    masked hardware watchpoints.  */

  9292. static int
  9293. resources_needed_masked_watchpoint (const struct bp_location *bl)
  9294. {
  9295.   struct watchpoint *w = (struct watchpoint *) bl->owner;

  9296.   return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
  9297. }

  9298. /* Implement the "works_in_software_mode" breakpoint_ops method for
  9299.    masked hardware watchpoints.  */

  9300. static int
  9301. works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
  9302. {
  9303.   return 0;
  9304. }

  9305. /* Implement the "print_it" breakpoint_ops method for
  9306.    masked hardware watchpoints.  */

  9307. static enum print_stop_action
  9308. print_it_masked_watchpoint (bpstat bs)
  9309. {
  9310.   struct breakpoint *b = bs->breakpoint_at;
  9311.   struct ui_out *uiout = current_uiout;

  9312.   /* Masked watchpoints have only one location.  */
  9313.   gdb_assert (b->loc && b->loc->next == NULL);

  9314.   switch (b->type)
  9315.     {
  9316.     case bp_hardware_watchpoint:
  9317.       annotate_watchpoint (b->number);
  9318.       if (ui_out_is_mi_like_p (uiout))
  9319.         ui_out_field_string
  9320.           (uiout, "reason",
  9321.            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
  9322.       break;

  9323.     case bp_read_watchpoint:
  9324.       if (ui_out_is_mi_like_p (uiout))
  9325.         ui_out_field_string
  9326.           (uiout, "reason",
  9327.            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
  9328.       break;

  9329.     case bp_access_watchpoint:
  9330.       if (ui_out_is_mi_like_p (uiout))
  9331.         ui_out_field_string
  9332.           (uiout, "reason",
  9333.            async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
  9334.       break;
  9335.     default:
  9336.       internal_error (__FILE__, __LINE__,
  9337.                       _("Invalid hardware watchpoint type."));
  9338.     }

  9339.   mention (b);
  9340.   ui_out_text (uiout, _("\n\
  9341. Check the underlying instruction at PC for the memory\n\
  9342. address and value which triggered this watchpoint.\n"));
  9343.   ui_out_text (uiout, "\n");

  9344.   /* More than one watchpoint may have been triggered.  */
  9345.   return PRINT_UNKNOWN;
  9346. }

  9347. /* Implement the "print_one_detail" breakpoint_ops method for
  9348.    masked hardware watchpoints.  */

  9349. static void
  9350. print_one_detail_masked_watchpoint (const struct breakpoint *b,
  9351.                                     struct ui_out *uiout)
  9352. {
  9353.   struct watchpoint *w = (struct watchpoint *) b;

  9354.   /* Masked watchpoints have only one location.  */
  9355.   gdb_assert (b->loc && b->loc->next == NULL);

  9356.   ui_out_text (uiout, "\tmask ");
  9357.   ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
  9358.   ui_out_text (uiout, "\n");
  9359. }

  9360. /* Implement the "print_mention" breakpoint_ops method for
  9361.    masked hardware watchpoints.  */

  9362. static void
  9363. print_mention_masked_watchpoint (struct breakpoint *b)
  9364. {
  9365.   struct watchpoint *w = (struct watchpoint *) b;
  9366.   struct ui_out *uiout = current_uiout;
  9367.   struct cleanup *ui_out_chain;

  9368.   switch (b->type)
  9369.     {
  9370.     case bp_hardware_watchpoint:
  9371.       ui_out_text (uiout, "Masked hardware watchpoint ");
  9372.       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
  9373.       break;
  9374.     case bp_read_watchpoint:
  9375.       ui_out_text (uiout, "Masked hardware read watchpoint ");
  9376.       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
  9377.       break;
  9378.     case bp_access_watchpoint:
  9379.       ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
  9380.       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
  9381.       break;
  9382.     default:
  9383.       internal_error (__FILE__, __LINE__,
  9384.                       _("Invalid hardware watchpoint type."));
  9385.     }

  9386.   ui_out_field_int (uiout, "number", b->number);
  9387.   ui_out_text (uiout, ": ");
  9388.   ui_out_field_string (uiout, "exp", w->exp_string);
  9389.   do_cleanups (ui_out_chain);
  9390. }

  9391. /* Implement the "print_recreate" breakpoint_ops method for
  9392.    masked hardware watchpoints.  */

  9393. static void
  9394. print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
  9395. {
  9396.   struct watchpoint *w = (struct watchpoint *) b;
  9397.   char tmp[40];

  9398.   switch (b->type)
  9399.     {
  9400.     case bp_hardware_watchpoint:
  9401.       fprintf_unfiltered (fp, "watch");
  9402.       break;
  9403.     case bp_read_watchpoint:
  9404.       fprintf_unfiltered (fp, "rwatch");
  9405.       break;
  9406.     case bp_access_watchpoint:
  9407.       fprintf_unfiltered (fp, "awatch");
  9408.       break;
  9409.     default:
  9410.       internal_error (__FILE__, __LINE__,
  9411.                       _("Invalid hardware watchpoint type."));
  9412.     }

  9413.   sprintf_vma (tmp, w->hw_wp_mask);
  9414.   fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
  9415.   print_recreate_thread (b, fp);
  9416. }

  9417. /* The breakpoint_ops structure to be used in masked hardware watchpoints.  */

  9418. static struct breakpoint_ops masked_watchpoint_breakpoint_ops;

  9419. /* Tell whether the given watchpoint is a masked hardware watchpoint.  */

  9420. static int
  9421. is_masked_watchpoint (const struct breakpoint *b)
  9422. {
  9423.   return b->ops == &masked_watchpoint_breakpoint_ops;
  9424. }

  9425. /* accessflag:  hw_write:  watch write,
  9426.                 hw_read:   watch read,
  9427.                 hw_access: watch access (read or write) */
  9428. static void
  9429. watch_command_1 (const char *arg, int accessflag, int from_tty,
  9430.                  int just_location, int internal)
  9431. {
  9432.   volatile struct gdb_exception e;
  9433.   struct breakpoint *b, *scope_breakpoint = NULL;
  9434.   struct expression *exp;
  9435.   const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
  9436.   struct value *val, *mark, *result;
  9437.   int saved_bitpos = 0, saved_bitsize = 0;
  9438.   struct frame_info *frame;
  9439.   const char *exp_start = NULL;
  9440.   const char *exp_end = NULL;
  9441.   const char *tok, *end_tok;
  9442.   int toklen = -1;
  9443.   const char *cond_start = NULL;
  9444.   const char *cond_end = NULL;
  9445.   enum bptype bp_type;
  9446.   int thread = -1;
  9447.   int pc = 0;
  9448.   /* Flag to indicate whether we are going to use masks for
  9449.      the hardware watchpoint.  */
  9450.   int use_mask = 0;
  9451.   CORE_ADDR mask = 0;
  9452.   struct watchpoint *w;
  9453.   char *expression;
  9454.   struct cleanup *back_to;

  9455.   /* Make sure that we actually have parameters to parse.  */
  9456.   if (arg != NULL && arg[0] != '\0')
  9457.     {
  9458.       const char *value_start;

  9459.       exp_end = arg + strlen (arg);

  9460.       /* Look for "parameter value" pairs at the end
  9461.          of the arguments string.  */
  9462.       for (tok = exp_end - 1; tok > arg; tok--)
  9463.         {
  9464.           /* Skip whitespace at the end of the argument list.  */
  9465.           while (tok > arg && (*tok == ' ' || *tok == '\t'))
  9466.             tok--;

  9467.           /* Find the beginning of the last token.
  9468.              This is the value of the parameter.  */
  9469.           while (tok > arg && (*tok != ' ' && *tok != '\t'))
  9470.             tok--;
  9471.           value_start = tok + 1;

  9472.           /* Skip whitespace.  */
  9473.           while (tok > arg && (*tok == ' ' || *tok == '\t'))
  9474.             tok--;

  9475.           end_tok = tok;

  9476.           /* Find the beginning of the second to last token.
  9477.              This is the parameter itself.  */
  9478.           while (tok > arg && (*tok != ' ' && *tok != '\t'))
  9479.             tok--;
  9480.           tok++;
  9481.           toklen = end_tok - tok + 1;

  9482.           if (toklen == 6 && !strncmp (tok, "thread", 6))
  9483.             {
  9484.               /* At this point we've found a "thread" token, which means
  9485.                  the user is trying to set a watchpoint that triggers
  9486.                  only in a specific thread.  */
  9487.               char *endp;

  9488.               if (thread != -1)
  9489.                 error(_("You can specify only one thread."));

  9490.               /* Extract the thread ID from the next token.  */
  9491.               thread = strtol (value_start, &endp, 0);

  9492.               /* Check if the user provided a valid numeric value for the
  9493.                  thread ID.  */
  9494.               if (*endp != ' ' && *endp != '\t' && *endp != '\0')
  9495.                 error (_("Invalid thread ID specification %s."), value_start);

  9496.               /* Check if the thread actually exists.  */
  9497.               if (!valid_thread_id (thread))
  9498.                 invalid_thread_id_error (thread);
  9499.             }
  9500.           else if (toklen == 4 && !strncmp (tok, "mask", 4))
  9501.             {
  9502.               /* We've found a "mask" token, which means the user wants to
  9503.                  create a hardware watchpoint that is going to have the mask
  9504.                  facility.  */
  9505.               struct value *mask_value, *mark;

  9506.               if (use_mask)
  9507.                 error(_("You can specify only one mask."));

  9508.               use_mask = just_location = 1;

  9509.               mark = value_mark ();
  9510.               mask_value = parse_to_comma_and_eval (&value_start);
  9511.               mask = value_as_address (mask_value);
  9512.               value_free_to_mark (mark);
  9513.             }
  9514.           else
  9515.             /* We didn't recognize what we found.  We should stop here.  */
  9516.             break;

  9517.           /* Truncate the string and get rid of the "parameter value" pair before
  9518.              the arguments string is parsed by the parse_exp_1 function.  */
  9519.           exp_end = tok;
  9520.         }
  9521.     }
  9522.   else
  9523.     exp_end = arg;

  9524.   /* Parse the rest of the arguments.  From here on out, everything
  9525.      is in terms of a newly allocated string instead of the original
  9526.      ARG.  */
  9527.   innermost_block = NULL;
  9528.   expression = savestring (arg, exp_end - arg);
  9529.   back_to = make_cleanup (xfree, expression);
  9530.   exp_start = arg = expression;
  9531.   exp = parse_exp_1 (&arg, 0, 0, 0);
  9532.   exp_end = arg;
  9533.   /* Remove trailing whitespace from the expression before saving it.
  9534.      This makes the eventual display of the expression string a bit
  9535.      prettier.  */
  9536.   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
  9537.     --exp_end;

  9538.   /* Checking if the expression is not constant.  */
  9539.   if (watchpoint_exp_is_const (exp))
  9540.     {
  9541.       int len;

  9542.       len = exp_end - exp_start;
  9543.       while (len > 0 && isspace (exp_start[len - 1]))
  9544.         len--;
  9545.       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
  9546.     }

  9547.   exp_valid_block = innermost_block;
  9548.   mark = value_mark ();
  9549.   fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);

  9550.   if (val != NULL && just_location)
  9551.     {
  9552.       saved_bitpos = value_bitpos (val);
  9553.       saved_bitsize = value_bitsize (val);
  9554.     }

  9555.   if (just_location)
  9556.     {
  9557.       int ret;

  9558.       exp_valid_block = NULL;
  9559.       val = value_addr (result);
  9560.       release_value (val);
  9561.       value_free_to_mark (mark);

  9562.       if (use_mask)
  9563.         {
  9564.           ret = target_masked_watch_num_registers (value_as_address (val),
  9565.                                                    mask);
  9566.           if (ret == -1)
  9567.             error (_("This target does not support masked watchpoints."));
  9568.           else if (ret == -2)
  9569.             error (_("Invalid mask or memory region."));
  9570.         }
  9571.     }
  9572.   else if (val != NULL)
  9573.     release_value (val);

  9574.   tok = skip_spaces_const (arg);
  9575.   end_tok = skip_to_space_const (tok);

  9576.   toklen = end_tok - tok;
  9577.   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
  9578.     {
  9579.       struct expression *cond;

  9580.       innermost_block = NULL;
  9581.       tok = cond_start = end_tok + 1;
  9582.       cond = parse_exp_1 (&tok, 0, 0, 0);

  9583.       /* The watchpoint expression may not be local, but the condition
  9584.          may still be.  E.g.: `watch global if local > 0'.  */
  9585.       cond_exp_valid_block = innermost_block;

  9586.       xfree (cond);
  9587.       cond_end = tok;
  9588.     }
  9589.   if (*tok)
  9590.     error (_("Junk at end of command."));

  9591.   frame = block_innermost_frame (exp_valid_block);

  9592.   /* If the expression is "local", then set up a "watchpoint scope"
  9593.      breakpoint at the point where we've left the scope of the watchpoint
  9594.      expression.  Create the scope breakpoint before the watchpoint, so
  9595.      that we will encounter it first in bpstat_stop_status.  */
  9596.   if (exp_valid_block && frame)
  9597.     {
  9598.       if (frame_id_p (frame_unwind_caller_id (frame)))
  9599.         {
  9600.            scope_breakpoint
  9601.             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
  9602.                                           frame_unwind_caller_pc (frame),
  9603.                                           bp_watchpoint_scope,
  9604.                                           &momentary_breakpoint_ops);

  9605.           scope_breakpoint->enable_state = bp_enabled;

  9606.           /* Automatically delete the breakpoint when it hits.  */
  9607.           scope_breakpoint->disposition = disp_del;

  9608.           /* Only break in the proper frame (help with recursion).  */
  9609.           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);

  9610.           /* Set the address at which we will stop.  */
  9611.           scope_breakpoint->loc->gdbarch
  9612.             = frame_unwind_caller_arch (frame);
  9613.           scope_breakpoint->loc->requested_address
  9614.             = frame_unwind_caller_pc (frame);
  9615.           scope_breakpoint->loc->address
  9616.             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
  9617.                                          scope_breakpoint->loc->requested_address,
  9618.                                          scope_breakpoint->type);
  9619.         }
  9620.     }

  9621.   /* Now set up the breakpoint.  We create all watchpoints as hardware
  9622.      watchpoints here even if hardware watchpoints are turned off, a call
  9623.      to update_watchpoint later in this function will cause the type to
  9624.      drop back to bp_watchpoint (software watchpoint) if required.  */

  9625.   if (accessflag == hw_read)
  9626.     bp_type = bp_read_watchpoint;
  9627.   else if (accessflag == hw_access)
  9628.     bp_type = bp_access_watchpoint;
  9629.   else
  9630.     bp_type = bp_hardware_watchpoint;

  9631.   w = XCNEW (struct watchpoint);
  9632.   b = &w->base;
  9633.   if (use_mask)
  9634.     init_raw_breakpoint_without_location (b, NULL, bp_type,
  9635.                                           &masked_watchpoint_breakpoint_ops);
  9636.   else
  9637.     init_raw_breakpoint_without_location (b, NULL, bp_type,
  9638.                                           &watchpoint_breakpoint_ops);
  9639.   b->thread = thread;
  9640.   b->disposition = disp_donttouch;
  9641.   b->pspace = current_program_space;
  9642.   w->exp = exp;
  9643.   w->exp_valid_block = exp_valid_block;
  9644.   w->cond_exp_valid_block = cond_exp_valid_block;
  9645.   if (just_location)
  9646.     {
  9647.       struct type *t = value_type (val);
  9648.       CORE_ADDR addr = value_as_address (val);
  9649.       char *name;

  9650.       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
  9651.       name = type_to_string (t);

  9652.       w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
  9653.                                           core_addr_to_string (addr));
  9654.       xfree (name);

  9655.       w->exp_string = xstrprintf ("-location %.*s",
  9656.                                   (int) (exp_end - exp_start), exp_start);

  9657.       /* The above expression is in C.  */
  9658.       b->language = language_c;
  9659.     }
  9660.   else
  9661.     w->exp_string = savestring (exp_start, exp_end - exp_start);

  9662.   if (use_mask)
  9663.     {
  9664.       w->hw_wp_mask = mask;
  9665.     }
  9666.   else
  9667.     {
  9668.       w->val = val;
  9669.       w->val_bitpos = saved_bitpos;
  9670.       w->val_bitsize = saved_bitsize;
  9671.       w->val_valid = 1;
  9672.     }

  9673.   if (cond_start)
  9674.     b->cond_string = savestring (cond_start, cond_end - cond_start);
  9675.   else
  9676.     b->cond_string = 0;

  9677.   if (frame)
  9678.     {
  9679.       w->watchpoint_frame = get_frame_id (frame);
  9680.       w->watchpoint_thread = inferior_ptid;
  9681.     }
  9682.   else
  9683.     {
  9684.       w->watchpoint_frame = null_frame_id;
  9685.       w->watchpoint_thread = null_ptid;
  9686.     }

  9687.   if (scope_breakpoint != NULL)
  9688.     {
  9689.       /* The scope breakpoint is related to the watchpoint.  We will
  9690.          need to act on them together.  */
  9691.       b->related_breakpoint = scope_breakpoint;
  9692.       scope_breakpoint->related_breakpoint = b;
  9693.     }

  9694.   if (!just_location)
  9695.     value_free_to_mark (mark);

  9696.   TRY_CATCH (e, RETURN_MASK_ALL)
  9697.     {
  9698.       /* Finally update the new watchpoint.  This creates the locations
  9699.          that should be inserted.  */
  9700.       update_watchpoint (w, 1);
  9701.     }
  9702.   if (e.reason < 0)
  9703.     {
  9704.       delete_breakpoint (b);
  9705.       throw_exception (e);
  9706.     }

  9707.   install_breakpoint (internal, b, 1);
  9708.   do_cleanups (back_to);
  9709. }

  9710. /* Return count of debug registers needed to watch the given expression.
  9711.    If the watchpoint cannot be handled in hardware return zero.  */

  9712. static int
  9713. can_use_hardware_watchpoint (struct value *v)
  9714. {
  9715.   int found_memory_cnt = 0;
  9716.   struct value *head = v;

  9717.   /* Did the user specifically forbid us to use hardware watchpoints? */
  9718.   if (!can_use_hw_watchpoints)
  9719.     return 0;

  9720.   /* Make sure that the value of the expression depends only upon
  9721.      memory contents, and values computed from them within GDB.  If we
  9722.      find any register references or function calls, we can't use a
  9723.      hardware watchpoint.

  9724.      The idea here is that evaluating an expression generates a series
  9725.      of values, one holding the value of every subexpression.  (The
  9726.      expression a*b+c has five subexpressions: a, b, a*b, c, and
  9727.      a*b+c.)  GDB's values hold almost enough information to establish
  9728.      the criteria given above --- they identify memory lvalues,
  9729.      register lvalues, computed values, etcetera.  So we can evaluate
  9730.      the expression, and then scan the chain of values that leaves
  9731.      behind to decide whether we can detect any possible change to the
  9732.      expression's final value using only hardware watchpoints.

  9733.      However, I don't think that the values returned by inferior
  9734.      function calls are special in any way.  So this function may not
  9735.      notice that an expression involving an inferior function call
  9736.      can't be watched with hardware watchpoints.  FIXME.  */
  9737.   for (; v; v = value_next (v))
  9738.     {
  9739.       if (VALUE_LVAL (v) == lval_memory)
  9740.         {
  9741.           if (v != head && value_lazy (v))
  9742.             /* A lazy memory lvalue in the chain is one that GDB never
  9743.                needed to fetch; we either just used its address (e.g.,
  9744.                `a' in `a.b') or we never needed it at all (e.g., `a'
  9745.                in `a,b').  This doesn't apply to HEAD; if that is
  9746.                lazy then it was not readable, but watch it anyway.  */
  9747.             ;
  9748.           else
  9749.             {
  9750.               /* Ahh, memory we actually used!  Check if we can cover
  9751.                  it with hardware watchpoints.  */
  9752.               struct type *vtype = check_typedef (value_type (v));

  9753.               /* We only watch structs and arrays if user asked for it
  9754.                  explicitly, never if they just happen to appear in a
  9755.                  middle of some value chain.  */
  9756.               if (v == head
  9757.                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
  9758.                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
  9759.                 {
  9760.                   CORE_ADDR vaddr = value_address (v);
  9761.                   int len;
  9762.                   int num_regs;

  9763.                   len = (target_exact_watchpoints
  9764.                          && is_scalar_type_recursive (vtype))?
  9765.                     1 : TYPE_LENGTH (value_type (v));

  9766.                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
  9767.                   if (!num_regs)
  9768.                     return 0;
  9769.                   else
  9770.                     found_memory_cnt += num_regs;
  9771.                 }
  9772.             }
  9773.         }
  9774.       else if (VALUE_LVAL (v) != not_lval
  9775.                && deprecated_value_modifiable (v) == 0)
  9776.         return 0;        /* These are values from the history (e.g., $1).  */
  9777.       else if (VALUE_LVAL (v) == lval_register)
  9778.         return 0;        /* Cannot watch a register with a HW watchpoint.  */
  9779.     }

  9780.   /* The expression itself looks suitable for using a hardware
  9781.      watchpoint, but give the target machine a chance to reject it.  */
  9782.   return found_memory_cnt;
  9783. }

  9784. void
  9785. watch_command_wrapper (char *arg, int from_tty, int internal)
  9786. {
  9787.   watch_command_1 (arg, hw_write, from_tty, 0, internal);
  9788. }

  9789. /* A helper function that looks for the "-location" argument and then
  9790.    calls watch_command_1.  */

  9791. static void
  9792. watch_maybe_just_location (char *arg, int accessflag, int from_tty)
  9793. {
  9794.   int just_location = 0;

  9795.   if (arg
  9796.       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
  9797.           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
  9798.     {
  9799.       arg = skip_spaces (arg);
  9800.       just_location = 1;
  9801.     }

  9802.   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
  9803. }

  9804. static void
  9805. watch_command (char *arg, int from_tty)
  9806. {
  9807.   watch_maybe_just_location (arg, hw_write, from_tty);
  9808. }

  9809. void
  9810. rwatch_command_wrapper (char *arg, int from_tty, int internal)
  9811. {
  9812.   watch_command_1 (arg, hw_read, from_tty, 0, internal);
  9813. }

  9814. static void
  9815. rwatch_command (char *arg, int from_tty)
  9816. {
  9817.   watch_maybe_just_location (arg, hw_read, from_tty);
  9818. }

  9819. void
  9820. awatch_command_wrapper (char *arg, int from_tty, int internal)
  9821. {
  9822.   watch_command_1 (arg, hw_access, from_tty, 0, internal);
  9823. }

  9824. static void
  9825. awatch_command (char *arg, int from_tty)
  9826. {
  9827.   watch_maybe_just_location (arg, hw_access, from_tty);
  9828. }


  9829. /* Helper routines for the until_command routine in infcmd.c.  Here
  9830.    because it uses the mechanisms of breakpoints.  */

  9831. struct until_break_command_continuation_args
  9832. {
  9833.   struct breakpoint *breakpoint;
  9834.   struct breakpoint *breakpoint2;
  9835.   int thread_num;
  9836. };

  9837. /* This function is called by fetch_inferior_event via the
  9838.    cmd_continuation pointer, to complete the until command.  It takes
  9839.    care of cleaning up the temporary breakpoints set up by the until
  9840.    command.  */
  9841. static void
  9842. until_break_command_continuation (void *arg, int err)
  9843. {
  9844.   struct until_break_command_continuation_args *a = arg;

  9845.   delete_breakpoint (a->breakpoint);
  9846.   if (a->breakpoint2)
  9847.     delete_breakpoint (a->breakpoint2);
  9848.   delete_longjmp_breakpoint (a->thread_num);
  9849. }

  9850. void
  9851. until_break_command (char *arg, int from_tty, int anywhere)
  9852. {
  9853.   struct symtabs_and_lines sals;
  9854.   struct symtab_and_line sal;
  9855.   struct frame_info *frame;
  9856.   struct gdbarch *frame_gdbarch;
  9857.   struct frame_id stack_frame_id;
  9858.   struct frame_id caller_frame_id;
  9859.   struct breakpoint *breakpoint;
  9860.   struct breakpoint *breakpoint2 = NULL;
  9861.   struct cleanup *old_chain;
  9862.   int thread;
  9863.   struct thread_info *tp;

  9864.   clear_proceed_status (0);

  9865.   /* Set a breakpoint where the user wants it and at return from
  9866.      this function.  */

  9867.   if (last_displayed_sal_is_valid ())
  9868.     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
  9869.                           get_last_displayed_symtab (),
  9870.                           get_last_displayed_line ());
  9871.   else
  9872.     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
  9873.                           (struct symtab *) NULL, 0);

  9874.   if (sals.nelts != 1)
  9875.     error (_("Couldn't get information on specified line."));

  9876.   sal = sals.sals[0];
  9877.   xfree (sals.sals);        /* malloc'd, so freed.  */

  9878.   if (*arg)
  9879.     error (_("Junk at end of arguments."));

  9880.   resolve_sal_pc (&sal);

  9881.   tp = inferior_thread ();
  9882.   thread = tp->num;

  9883.   old_chain = make_cleanup (null_cleanup, NULL);

  9884.   /* Note linespec handling above invalidates the frame chain.
  9885.      Installing a breakpoint also invalidates the frame chain (as it
  9886.      may need to switch threads), so do any frame handling before
  9887.      that.  */

  9888.   frame = get_selected_frame (NULL);
  9889.   frame_gdbarch = get_frame_arch (frame);
  9890.   stack_frame_id = get_stack_frame_id (frame);
  9891.   caller_frame_id = frame_unwind_caller_id (frame);

  9892.   /* Keep within the current frame, or in frames called by the current
  9893.      one.  */

  9894.   if (frame_id_p (caller_frame_id))
  9895.     {
  9896.       struct symtab_and_line sal2;

  9897.       sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
  9898.       sal2.pc = frame_unwind_caller_pc (frame);
  9899.       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
  9900.                                               sal2,
  9901.                                               caller_frame_id,
  9902.                                               bp_until);
  9903.       make_cleanup_delete_breakpoint (breakpoint2);

  9904.       set_longjmp_breakpoint (tp, caller_frame_id);
  9905.       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
  9906.     }

  9907.   /* set_momentary_breakpoint could invalidate FRAME.  */
  9908.   frame = NULL;

  9909.   if (anywhere)
  9910.     /* If the user told us to continue until a specified location,
  9911.        we don't specify a frame at which we need to stop.  */
  9912.     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
  9913.                                            null_frame_id, bp_until);
  9914.   else
  9915.     /* Otherwise, specify the selected frame, because we want to stop
  9916.        only at the very same frame.  */
  9917.     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
  9918.                                            stack_frame_id, bp_until);
  9919.   make_cleanup_delete_breakpoint (breakpoint);

  9920.   proceed (-1, GDB_SIGNAL_DEFAULT, 0);

  9921.   /* If we are running asynchronously, and proceed call above has
  9922.      actually managed to start the target, arrange for breakpoints to
  9923.      be deleted when the target stops.  Otherwise, we're already
  9924.      stopped and delete breakpoints via cleanup chain.  */

  9925.   if (target_can_async_p () && is_running (inferior_ptid))
  9926.     {
  9927.       struct until_break_command_continuation_args *args;
  9928.       args = xmalloc (sizeof (*args));

  9929.       args->breakpoint = breakpoint;
  9930.       args->breakpoint2 = breakpoint2;
  9931.       args->thread_num = thread;

  9932.       discard_cleanups (old_chain);
  9933.       add_continuation (inferior_thread (),
  9934.                         until_break_command_continuation, args,
  9935.                         xfree);
  9936.     }
  9937.   else
  9938.     do_cleanups (old_chain);
  9939. }

  9940. /* This function attempts to parse an optional "if <cond>" clause
  9941.    from the arg string.  If one is not found, it returns NULL.

  9942.    Else, it returns a pointer to the condition string.  (It does not
  9943.    attempt to evaluate the string against a particular block.)  And,
  9944.    it updates arg to point to the first character following the parsed
  9945.    if clause in the arg string.  */

  9946. char *
  9947. ep_parse_optional_if_clause (char **arg)
  9948. {
  9949.   char *cond_string;

  9950.   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
  9951.     return NULL;

  9952.   /* Skip the "if" keyword.  */
  9953.   (*arg) += 2;

  9954.   /* Skip any extra leading whitespace, and record the start of the
  9955.      condition string.  */
  9956.   *arg = skip_spaces (*arg);
  9957.   cond_string = *arg;

  9958.   /* Assume that the condition occupies the remainder of the arg
  9959.      string.  */
  9960.   (*arg) += strlen (cond_string);

  9961.   return cond_string;
  9962. }

  9963. /* Commands to deal with catching events, such as signals, exceptions,
  9964.    process start/exit, etc.  */

  9965. typedef enum
  9966. {
  9967.   catch_fork_temporary, catch_vfork_temporary,
  9968.   catch_fork_permanent, catch_vfork_permanent
  9969. }
  9970. catch_fork_kind;

  9971. static void
  9972. catch_fork_command_1 (char *arg, int from_tty,
  9973.                       struct cmd_list_element *command)
  9974. {
  9975.   struct gdbarch *gdbarch = get_current_arch ();
  9976.   char *cond_string = NULL;
  9977.   catch_fork_kind fork_kind;
  9978.   int tempflag;

  9979.   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
  9980.   tempflag = (fork_kind == catch_fork_temporary
  9981.               || fork_kind == catch_vfork_temporary);

  9982.   if (!arg)
  9983.     arg = "";
  9984.   arg = skip_spaces (arg);

  9985.   /* The allowed syntax is:
  9986.      catch [v]fork
  9987.      catch [v]fork if <cond>

  9988.      First, check if there's an if clause.  */
  9989.   cond_string = ep_parse_optional_if_clause (&arg);

  9990.   if ((*arg != '\0') && !isspace (*arg))
  9991.     error (_("Junk at end of arguments."));

  9992.   /* If this target supports it, create a fork or vfork catchpoint
  9993.      and enable reporting of such events.  */
  9994.   switch (fork_kind)
  9995.     {
  9996.     case catch_fork_temporary:
  9997.     case catch_fork_permanent:
  9998.       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
  9999.                                           &catch_fork_breakpoint_ops);
  10000.       break;
  10001.     case catch_vfork_temporary:
  10002.     case catch_vfork_permanent:
  10003.       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
  10004.                                           &catch_vfork_breakpoint_ops);
  10005.       break;
  10006.     default:
  10007.       error (_("unsupported or unknown fork kind; cannot catch it"));
  10008.       break;
  10009.     }
  10010. }

  10011. static void
  10012. catch_exec_command_1 (char *arg, int from_tty,
  10013.                       struct cmd_list_element *command)
  10014. {
  10015.   struct exec_catchpoint *c;
  10016.   struct gdbarch *gdbarch = get_current_arch ();
  10017.   int tempflag;
  10018.   char *cond_string = NULL;

  10019.   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;

  10020.   if (!arg)
  10021.     arg = "";
  10022.   arg = skip_spaces (arg);

  10023.   /* The allowed syntax is:
  10024.      catch exec
  10025.      catch exec if <cond>

  10026.      First, check if there's an if clause.  */
  10027.   cond_string = ep_parse_optional_if_clause (&arg);

  10028.   if ((*arg != '\0') && !isspace (*arg))
  10029.     error (_("Junk at end of arguments."));

  10030.   c = XNEW (struct exec_catchpoint);
  10031.   init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
  10032.                    &catch_exec_breakpoint_ops);
  10033.   c->exec_pathname = NULL;

  10034.   install_breakpoint (0, &c->base, 1);
  10035. }

  10036. void
  10037. init_ada_exception_breakpoint (struct breakpoint *b,
  10038.                                struct gdbarch *gdbarch,
  10039.                                struct symtab_and_line sal,
  10040.                                char *addr_string,
  10041.                                const struct breakpoint_ops *ops,
  10042.                                int tempflag,
  10043.                                int enabled,
  10044.                                int from_tty)
  10045. {
  10046.   if (from_tty)
  10047.     {
  10048.       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
  10049.       if (!loc_gdbarch)
  10050.         loc_gdbarch = gdbarch;

  10051.       describe_other_breakpoints (loc_gdbarch,
  10052.                                   sal.pspace, sal.pc, sal.section, -1);
  10053.       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
  10054.          version for exception catchpoints, because two catchpoints
  10055.          used for different exception names will use the same address.
  10056.          In this case, a "breakpoint ... also set at..." warning is
  10057.          unproductive.  Besides, the warning phrasing is also a bit
  10058.          inappropriate, we should use the word catchpoint, and tell
  10059.          the user what type of catchpoint it is.  The above is good
  10060.          enough for now, though.  */
  10061.     }

  10062.   init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);

  10063.   b->enable_state = enabled ? bp_enabled : bp_disabled;
  10064.   b->disposition = tempflag ? disp_del : disp_donttouch;
  10065.   b->addr_string = addr_string;
  10066.   b->language = language_ada;
  10067. }

  10068. /* Splits the argument using space as delimiter.  Returns an xmalloc'd
  10069.    filter list, or NULL if no filtering is required.  */
  10070. static VEC(int) *
  10071. catch_syscall_split_args (char *arg)
  10072. {
  10073.   VEC(int) *result = NULL;
  10074.   struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
  10075.   struct gdbarch *gdbarch = target_gdbarch ();

  10076.   while (*arg != '\0')
  10077.     {
  10078.       int i, syscall_number;
  10079.       char *endptr;
  10080.       char cur_name[128];
  10081.       struct syscall s;

  10082.       /* Skip whitespace.  */
  10083.       arg = skip_spaces (arg);

  10084.       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
  10085.         cur_name[i] = arg[i];
  10086.       cur_name[i] = '\0';
  10087.       arg += i;

  10088.       /* Check if the user provided a syscall name or a number.  */
  10089.       syscall_number = (int) strtol (cur_name, &endptr, 0);
  10090.       if (*endptr == '\0')
  10091.         get_syscall_by_number (gdbarch, syscall_number, &s);
  10092.       else
  10093.         {
  10094.           /* We have a name.  Let's check if it's valid and convert it
  10095.              to a number.  */
  10096.           get_syscall_by_name (gdbarch, cur_name, &s);

  10097.           if (s.number == UNKNOWN_SYSCALL)
  10098.             /* Here we have to issue an error instead of a warning,
  10099.                because GDB cannot do anything useful if there's no
  10100.                syscall number to be caught.  */
  10101.             error (_("Unknown syscall name '%s'."), cur_name);
  10102.         }

  10103.       /* Ok, it's valid.  */
  10104.       VEC_safe_push (int, result, s.number);
  10105.     }

  10106.   discard_cleanups (cleanup);
  10107.   return result;
  10108. }

  10109. /* Implement the "catch syscall" command.  */

  10110. static void
  10111. catch_syscall_command_1 (char *arg, int from_tty,
  10112.                          struct cmd_list_element *command)
  10113. {
  10114.   int tempflag;
  10115.   VEC(int) *filter;
  10116.   struct syscall s;
  10117.   struct gdbarch *gdbarch = get_current_arch ();

  10118.   /* Checking if the feature if supported.  */
  10119.   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
  10120.     error (_("The feature 'catch syscall' is not supported on \
  10121. this architecture yet."));

  10122.   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;

  10123.   arg = skip_spaces (arg);

  10124.   /* We need to do this first "dummy" translation in order
  10125.      to get the syscall XML file loaded or, most important,
  10126.      to display a warning to the user if there's no XML file
  10127.      for his/her architecture.  */
  10128.   get_syscall_by_number (gdbarch, 0, &s);

  10129.   /* The allowed syntax is:
  10130.      catch syscall
  10131.      catch syscall <name | number> [<name | number> ... <name | number>]

  10132.      Let's check if there's a syscall name.  */

  10133.   if (arg != NULL)
  10134.     filter = catch_syscall_split_args (arg);
  10135.   else
  10136.     filter = NULL;

  10137.   create_syscall_event_catchpoint (tempflag, filter,
  10138.                                    &catch_syscall_breakpoint_ops);
  10139. }

  10140. static void
  10141. catch_command (char *arg, int from_tty)
  10142. {
  10143.   error (_("Catch requires an event name."));
  10144. }


  10145. static void
  10146. tcatch_command (char *arg, int from_tty)
  10147. {
  10148.   error (_("Catch requires an event name."));
  10149. }

  10150. /* A qsort comparison function that sorts breakpoints in order.  */

  10151. static int
  10152. compare_breakpoints (const void *a, const void *b)
  10153. {
  10154.   const breakpoint_p *ba = a;
  10155.   uintptr_t ua = (uintptr_t) *ba;
  10156.   const breakpoint_p *bb = b;
  10157.   uintptr_t ub = (uintptr_t) *bb;

  10158.   if ((*ba)->number < (*bb)->number)
  10159.     return -1;
  10160.   else if ((*ba)->number > (*bb)->number)
  10161.     return 1;

  10162.   /* Now sort by address, in case we see, e..g, two breakpoints with
  10163.      the number 0.  */
  10164.   if (ua < ub)
  10165.     return -1;
  10166.   return ua > ub ? 1 : 0;
  10167. }

  10168. /* Delete breakpoints by address or line.  */

  10169. static void
  10170. clear_command (char *arg, int from_tty)
  10171. {
  10172.   struct breakpoint *b, *prev;
  10173.   VEC(breakpoint_p) *found = 0;
  10174.   int ix;
  10175.   int default_match;
  10176.   struct symtabs_and_lines sals;
  10177.   struct symtab_and_line sal;
  10178.   int i;
  10179.   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);

  10180.   if (arg)
  10181.     {
  10182.       sals = decode_line_with_current_source (arg,
  10183.                                               (DECODE_LINE_FUNFIRSTLINE
  10184.                                                | DECODE_LINE_LIST_MODE));
  10185.       make_cleanup (xfree, sals.sals);
  10186.       default_match = 0;
  10187.     }
  10188.   else
  10189.     {
  10190.       sals.sals = (struct symtab_and_line *)
  10191.         xmalloc (sizeof (struct symtab_and_line));
  10192.       make_cleanup (xfree, sals.sals);
  10193.       init_sal (&sal);                /* Initialize to zeroes.  */

  10194.       /* Set sal's line, symtab, pc, and pspace to the values
  10195.          corresponding to the last call to print_frame_info.  If the
  10196.          codepoint is not valid, this will set all the fields to 0.  */
  10197.       get_last_displayed_sal (&sal);
  10198.       if (sal.symtab == 0)
  10199.         error (_("No source file specified."));

  10200.       sals.sals[0] = sal;
  10201.       sals.nelts = 1;

  10202.       default_match = 1;
  10203.     }

  10204.   /* We don't call resolve_sal_pc here.  That's not as bad as it
  10205.      seems, because all existing breakpoints typically have both
  10206.      file/line and pc set.  So, if clear is given file/line, we can
  10207.      match this to existing breakpoint without obtaining pc at all.

  10208.      We only support clearing given the address explicitly
  10209.      present in breakpoint table.  Say, we've set breakpoint
  10210.      at file:line.  There were several PC values for that file:line,
  10211.      due to optimization, all in one block.

  10212.      We've picked one PC value.  If "clear" is issued with another
  10213.      PC corresponding to the same file:line, the breakpoint won't
  10214.      be cleared.  We probably can still clear the breakpoint, but
  10215.      since the other PC value is never presented to user, user
  10216.      can only find it by guessing, and it does not seem important
  10217.      to support that.  */

  10218.   /* For each line spec given, delete bps which correspond to it.  Do
  10219.      it in two passes, solely to preserve the current behavior that
  10220.      from_tty is forced true if we delete more than one
  10221.      breakpoint.  */

  10222.   found = NULL;
  10223.   make_cleanup (VEC_cleanup (breakpoint_p), &found);
  10224.   for (i = 0; i < sals.nelts; i++)
  10225.     {
  10226.       const char *sal_fullname;

  10227.       /* If exact pc given, clear bpts at that pc.
  10228.          If line given (pc == 0), clear all bpts on specified line.
  10229.          If defaulting, clear all bpts on default line
  10230.          or at default pc.

  10231.          defaulting    sal.pc != 0    tests to do

  10232.          0              1             pc
  10233.          1              1             pc _and_ line
  10234.          0              0             line
  10235.          1              0             <can't happen> */

  10236.       sal = sals.sals[i];
  10237.       sal_fullname = (sal.symtab == NULL
  10238.                       ? NULL : symtab_to_fullname (sal.symtab));

  10239.       /* Find all matching breakpoints and add them to 'found'.  */
  10240.       ALL_BREAKPOINTS (b)
  10241.         {
  10242.           int match = 0;
  10243.           /* Are we going to delete b?  */
  10244.           if (b->type != bp_none && !is_watchpoint (b))
  10245.             {
  10246.               struct bp_location *loc = b->loc;
  10247.               for (; loc; loc = loc->next)
  10248.                 {
  10249.                   /* If the user specified file:line, don't allow a PC
  10250.                      match.  This matches historical gdb behavior.  */
  10251.                   int pc_match = (!sal.explicit_line
  10252.                                   && sal.pc
  10253.                                   && (loc->pspace == sal.pspace)
  10254.                                   && (loc->address == sal.pc)
  10255.                                   && (!section_is_overlay (loc->section)
  10256.                                       || loc->section == sal.section));
  10257.                   int line_match = 0;

  10258.                   if ((default_match || sal.explicit_line)
  10259.                       && loc->symtab != NULL
  10260.                       && sal_fullname != NULL
  10261.                       && sal.pspace == loc->pspace
  10262.                       && loc->line_number == sal.line
  10263.                       && filename_cmp (symtab_to_fullname (loc->symtab),
  10264.                                        sal_fullname) == 0)
  10265.                     line_match = 1;

  10266.                   if (pc_match || line_match)
  10267.                     {
  10268.                       match = 1;
  10269.                       break;
  10270.                     }
  10271.                 }
  10272.             }

  10273.           if (match)
  10274.             VEC_safe_push(breakpoint_p, found, b);
  10275.         }
  10276.     }

  10277.   /* Now go thru the 'found' chain and delete them.  */
  10278.   if (VEC_empty(breakpoint_p, found))
  10279.     {
  10280.       if (arg)
  10281.         error (_("No breakpoint at %s."), arg);
  10282.       else
  10283.         error (_("No breakpoint at this line."));
  10284.     }

  10285.   /* Remove duplicates from the vec.  */
  10286.   qsort (VEC_address (breakpoint_p, found),
  10287.          VEC_length (breakpoint_p, found),
  10288.          sizeof (breakpoint_p),
  10289.          compare_breakpoints);
  10290.   prev = VEC_index (breakpoint_p, found, 0);
  10291.   for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
  10292.     {
  10293.       if (b == prev)
  10294.         {
  10295.           VEC_ordered_remove (breakpoint_p, found, ix);
  10296.           --ix;
  10297.         }
  10298.     }

  10299.   if (VEC_length(breakpoint_p, found) > 1)
  10300.     from_tty = 1;        /* Always report if deleted more than one.  */
  10301.   if (from_tty)
  10302.     {
  10303.       if (VEC_length(breakpoint_p, found) == 1)
  10304.         printf_unfiltered (_("Deleted breakpoint "));
  10305.       else
  10306.         printf_unfiltered (_("Deleted breakpoints "));
  10307.     }

  10308.   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
  10309.     {
  10310.       if (from_tty)
  10311.         printf_unfiltered ("%d ", b->number);
  10312.       delete_breakpoint (b);
  10313.     }
  10314.   if (from_tty)
  10315.     putchar_unfiltered ('\n');

  10316.   do_cleanups (cleanups);
  10317. }

  10318. /* Delete breakpoint in BS if they are `delete' breakpoints and
  10319.    all breakpoints that are marked for deletion, whether hit or not.
  10320.    This is called after any breakpoint is hit, or after errors.  */

  10321. void
  10322. breakpoint_auto_delete (bpstat bs)
  10323. {
  10324.   struct breakpoint *b, *b_tmp;

  10325.   for (; bs; bs = bs->next)
  10326.     if (bs->breakpoint_at
  10327.         && bs->breakpoint_at->disposition == disp_del
  10328.         && bs->stop)
  10329.       delete_breakpoint (bs->breakpoint_at);

  10330.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  10331.   {
  10332.     if (b->disposition == disp_del_at_next_stop)
  10333.       delete_breakpoint (b);
  10334.   }
  10335. }

  10336. /* A comparison function for bp_location AP and BP being interfaced to
  10337.    qsort.  Sort elements primarily by their ADDRESS (no matter what
  10338.    does breakpoint_address_is_meaningful say for its OWNER),
  10339.    secondarily by ordering first permanent elements and
  10340.    terciarily just ensuring the array is sorted stable way despite
  10341.    qsort being an unstable algorithm.  */

  10342. static int
  10343. bp_location_compare (const void *ap, const void *bp)
  10344. {
  10345.   struct bp_location *a = *(void **) ap;
  10346.   struct bp_location *b = *(void **) bp;

  10347.   if (a->address != b->address)
  10348.     return (a->address > b->address) - (a->address < b->address);

  10349.   /* Sort locations at the same address by their pspace number, keeping
  10350.      locations of the same inferior (in a multi-inferior environment)
  10351.      grouped.  */

  10352.   if (a->pspace->num != b->pspace->num)
  10353.     return ((a->pspace->num > b->pspace->num)
  10354.             - (a->pspace->num < b->pspace->num));

  10355.   /* Sort permanent breakpoints first.  */
  10356.   if (a->permanent != b->permanent)
  10357.     return (a->permanent < b->permanent) - (a->permanent > b->permanent);

  10358.   /* Make the internal GDB representation stable across GDB runs
  10359.      where A and B memory inside GDB can differ.  Breakpoint locations of
  10360.      the same type at the same address can be sorted in arbitrary order.  */

  10361.   if (a->owner->number != b->owner->number)
  10362.     return ((a->owner->number > b->owner->number)
  10363.             - (a->owner->number < b->owner->number));

  10364.   return (a > b) - (a < b);
  10365. }

  10366. /* Set bp_location_placed_address_before_address_max and
  10367.    bp_location_shadow_len_after_address_max according to the current
  10368.    content of the bp_location array.  */

  10369. static void
  10370. bp_location_target_extensions_update (void)
  10371. {
  10372.   struct bp_location *bl, **blp_tmp;

  10373.   bp_location_placed_address_before_address_max = 0;
  10374.   bp_location_shadow_len_after_address_max = 0;

  10375.   ALL_BP_LOCATIONS (bl, blp_tmp)
  10376.     {
  10377.       CORE_ADDR start, end, addr;

  10378.       if (!bp_location_has_shadow (bl))
  10379.         continue;

  10380.       start = bl->target_info.placed_address;
  10381.       end = start + bl->target_info.shadow_len;

  10382.       gdb_assert (bl->address >= start);
  10383.       addr = bl->address - start;
  10384.       if (addr > bp_location_placed_address_before_address_max)
  10385.         bp_location_placed_address_before_address_max = addr;

  10386.       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */

  10387.       gdb_assert (bl->address < end);
  10388.       addr = end - bl->address;
  10389.       if (addr > bp_location_shadow_len_after_address_max)
  10390.         bp_location_shadow_len_after_address_max = addr;
  10391.     }
  10392. }

  10393. /* Download tracepoint locations if they haven't been.  */

  10394. static void
  10395. download_tracepoint_locations (void)
  10396. {
  10397.   struct breakpoint *b;
  10398.   struct cleanup *old_chain;

  10399.   if (!target_can_download_tracepoint ())
  10400.     return;

  10401.   old_chain = save_current_space_and_thread ();

  10402.   ALL_TRACEPOINTS (b)
  10403.     {
  10404.       struct bp_location *bl;
  10405.       struct tracepoint *t;
  10406.       int bp_location_downloaded = 0;

  10407.       if ((b->type == bp_fast_tracepoint
  10408.            ? !may_insert_fast_tracepoints
  10409.            : !may_insert_tracepoints))
  10410.         continue;

  10411.       for (bl = b->loc; bl; bl = bl->next)
  10412.         {
  10413.           /* In tracepoint, locations are _never_ duplicated, so
  10414.              should_be_inserted is equivalent to
  10415.              unduplicated_should_be_inserted.  */
  10416.           if (!should_be_inserted (bl) || bl->inserted)
  10417.             continue;

  10418.           switch_to_program_space_and_thread (bl->pspace);

  10419.           target_download_tracepoint (bl);

  10420.           bl->inserted = 1;
  10421.           bp_location_downloaded = 1;
  10422.         }
  10423.       t = (struct tracepoint *) b;
  10424.       t->number_on_target = b->number;
  10425.       if (bp_location_downloaded)
  10426.         observer_notify_breakpoint_modified (b);
  10427.     }

  10428.   do_cleanups (old_chain);
  10429. }

  10430. /* Swap the insertion/duplication state between two locations.  */

  10431. static void
  10432. swap_insertion (struct bp_location *left, struct bp_location *right)
  10433. {
  10434.   const int left_inserted = left->inserted;
  10435.   const int left_duplicate = left->duplicate;
  10436.   const int left_needs_update = left->needs_update;
  10437.   const struct bp_target_info left_target_info = left->target_info;

  10438.   /* Locations of tracepoints can never be duplicated.  */
  10439.   if (is_tracepoint (left->owner))
  10440.     gdb_assert (!left->duplicate);
  10441.   if (is_tracepoint (right->owner))
  10442.     gdb_assert (!right->duplicate);

  10443.   left->inserted = right->inserted;
  10444.   left->duplicate = right->duplicate;
  10445.   left->needs_update = right->needs_update;
  10446.   left->target_info = right->target_info;
  10447.   right->inserted = left_inserted;
  10448.   right->duplicate = left_duplicate;
  10449.   right->needs_update = left_needs_update;
  10450.   right->target_info = left_target_info;
  10451. }

  10452. /* Force the re-insertion of the locations at ADDRESS.  This is called
  10453.    once a new/deleted/modified duplicate location is found and we are evaluating
  10454.    conditions on the target's side.  Such conditions need to be updated on
  10455.    the target.  */

  10456. static void
  10457. force_breakpoint_reinsertion (struct bp_location *bl)
  10458. {
  10459.   struct bp_location **locp = NULL, **loc2p;
  10460.   struct bp_location *loc;
  10461.   CORE_ADDR address = 0;
  10462.   int pspace_num;

  10463.   address = bl->address;
  10464.   pspace_num = bl->pspace->num;

  10465.   /* This is only meaningful if the target is
  10466.      evaluating conditions and if the user has
  10467.      opted for condition evaluation on the target's
  10468.      side.  */
  10469.   if (gdb_evaluates_breakpoint_condition_p ()
  10470.       || !target_supports_evaluation_of_breakpoint_conditions ())
  10471.     return;

  10472.   /* Flag all breakpoint locations with this address and
  10473.      the same program space as the location
  10474.      as "its condition has changed".  We need to
  10475.      update the conditions on the target's side.  */
  10476.   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
  10477.     {
  10478.       loc = *loc2p;

  10479.       if (!is_breakpoint (loc->owner)
  10480.           || pspace_num != loc->pspace->num)
  10481.         continue;

  10482.       /* Flag the location appropriately.  We use a different state to
  10483.          let everyone know that we already updated the set of locations
  10484.          with addr bl->address and program space bl->pspace.  This is so
  10485.          we don't have to keep calling these functions just to mark locations
  10486.          that have already been marked.  */
  10487.       loc->condition_changed = condition_updated;

  10488.       /* Free the agent expression bytecode as well.  We will compute
  10489.          it later on.  */
  10490.       if (loc->cond_bytecode)
  10491.         {
  10492.           free_agent_expr (loc->cond_bytecode);
  10493.           loc->cond_bytecode = NULL;
  10494.         }
  10495.     }
  10496. }
  10497. /* Called whether new breakpoints are created, or existing breakpoints
  10498.    deleted, to update the global location list and recompute which
  10499.    locations are duplicate of which.

  10500.    The INSERT_MODE flag determines whether locations may not, may, or
  10501.    shall be inserted now.  See 'enum ugll_insert_mode' for more
  10502.    info.  */

  10503. static void
  10504. update_global_location_list (enum ugll_insert_mode insert_mode)
  10505. {
  10506.   struct breakpoint *b;
  10507.   struct bp_location **locp, *loc;
  10508.   struct cleanup *cleanups;
  10509.   /* Last breakpoint location address that was marked for update.  */
  10510.   CORE_ADDR last_addr = 0;
  10511.   /* Last breakpoint location program space that was marked for update.  */
  10512.   int last_pspace_num = -1;

  10513.   /* Used in the duplicates detection below.  When iterating over all
  10514.      bp_locations, points to the first bp_location of a given address.
  10515.      Breakpoints and watchpoints of different types are never
  10516.      duplicates of each other.  Keep one pointer for each type of
  10517.      breakpoint/watchpoint, so we only need to loop over all locations
  10518.      once.  */
  10519.   struct bp_location *bp_loc_first;  /* breakpoint */
  10520.   struct bp_location *wp_loc_first;  /* hardware watchpoint */
  10521.   struct bp_location *awp_loc_first; /* access watchpoint */
  10522.   struct bp_location *rwp_loc_first; /* read watchpoint */

  10523.   /* Saved former bp_location array which we compare against the newly
  10524.      built bp_location from the current state of ALL_BREAKPOINTS.  */
  10525.   struct bp_location **old_location, **old_locp;
  10526.   unsigned old_location_count;

  10527.   old_location = bp_location;
  10528.   old_location_count = bp_location_count;
  10529.   bp_location = NULL;
  10530.   bp_location_count = 0;
  10531.   cleanups = make_cleanup (xfree, old_location);

  10532.   ALL_BREAKPOINTS (b)
  10533.     for (loc = b->loc; loc; loc = loc->next)
  10534.       bp_location_count++;

  10535.   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
  10536.   locp = bp_location;
  10537.   ALL_BREAKPOINTS (b)
  10538.     for (loc = b->loc; loc; loc = loc->next)
  10539.       *locp++ = loc;
  10540.   qsort (bp_location, bp_location_count, sizeof (*bp_location),
  10541.          bp_location_compare);

  10542.   bp_location_target_extensions_update ();

  10543.   /* Identify bp_location instances that are no longer present in the
  10544.      new list, and therefore should be freed.  Note that it's not
  10545.      necessary that those locations should be removed from inferior --
  10546.      if there's another location at the same address (previously
  10547.      marked as duplicate), we don't need to remove/insert the
  10548.      location.

  10549.      LOCP is kept in sync with OLD_LOCP, each pointing to the current
  10550.      and former bp_location array state respectively.  */

  10551.   locp = bp_location;
  10552.   for (old_locp = old_location; old_locp < old_location + old_location_count;
  10553.        old_locp++)
  10554.     {
  10555.       struct bp_location *old_loc = *old_locp;
  10556.       struct bp_location **loc2p;

  10557.       /* Tells if 'old_loc' is found among the new locations.  If
  10558.          not, we have to free it.  */
  10559.       int found_object = 0;
  10560.       /* Tells if the location should remain inserted in the target.  */
  10561.       int keep_in_target = 0;
  10562.       int removed = 0;

  10563.       /* Skip LOCP entries which will definitely never be needed.
  10564.          Stop either at or being the one matching OLD_LOC.  */
  10565.       while (locp < bp_location + bp_location_count
  10566.              && (*locp)->address < old_loc->address)
  10567.         locp++;

  10568.       for (loc2p = locp;
  10569.            (loc2p < bp_location + bp_location_count
  10570.             && (*loc2p)->address == old_loc->address);
  10571.            loc2p++)
  10572.         {
  10573.           /* Check if this is a new/duplicated location or a duplicated
  10574.              location that had its condition modified.  If so, we want to send
  10575.              its condition to the target if evaluation of conditions is taking
  10576.              place there.  */
  10577.           if ((*loc2p)->condition_changed == condition_modified
  10578.               && (last_addr != old_loc->address
  10579.                   || last_pspace_num != old_loc->pspace->num))
  10580.             {
  10581.               force_breakpoint_reinsertion (*loc2p);
  10582.               last_pspace_num = old_loc->pspace->num;
  10583.             }

  10584.           if (*loc2p == old_loc)
  10585.             found_object = 1;
  10586.         }

  10587.       /* We have already handled this address, update it so that we don't
  10588.          have to go through updates again.  */
  10589.       last_addr = old_loc->address;

  10590.       /* Target-side condition evaluation: Handle deleted locations.  */
  10591.       if (!found_object)
  10592.         force_breakpoint_reinsertion (old_loc);

  10593.       /* If this location is no longer present, and inserted, look if
  10594.          there's maybe a new location at the same address.  If so,
  10595.          mark that one inserted, and don't remove this one.  This is
  10596.          needed so that we don't have a time window where a breakpoint
  10597.          at certain location is not inserted.  */

  10598.       if (old_loc->inserted)
  10599.         {
  10600.           /* If the location is inserted now, we might have to remove
  10601.              it.  */

  10602.           if (found_object && should_be_inserted (old_loc))
  10603.             {
  10604.               /* The location is still present in the location list,
  10605.                  and still should be inserted.  Don't do anything.  */
  10606.               keep_in_target = 1;
  10607.             }
  10608.           else
  10609.             {
  10610.               /* This location still exists, but it won't be kept in the
  10611.                  target since it may have been disabled.  We proceed to
  10612.                  remove its target-side condition.  */

  10613.               /* The location is either no longer present, or got
  10614.                  disabled.  See if there's another location at the
  10615.                  same address, in which case we don't need to remove
  10616.                  this one from the target.  */

  10617.               /* OLD_LOC comes from existing struct breakpoint.  */
  10618.               if (breakpoint_address_is_meaningful (old_loc->owner))
  10619.                 {
  10620.                   for (loc2p = locp;
  10621.                        (loc2p < bp_location + bp_location_count
  10622.                         && (*loc2p)->address == old_loc->address);
  10623.                        loc2p++)
  10624.                     {
  10625.                       struct bp_location *loc2 = *loc2p;

  10626.                       if (breakpoint_locations_match (loc2, old_loc))
  10627.                         {
  10628.                           /* Read watchpoint locations are switched to
  10629.                              access watchpoints, if the former are not
  10630.                              supported, but the latter are.  */
  10631.                           if (is_hardware_watchpoint (old_loc->owner))
  10632.                             {
  10633.                               gdb_assert (is_hardware_watchpoint (loc2->owner));
  10634.                               loc2->watchpoint_type = old_loc->watchpoint_type;
  10635.                             }

  10636.                           /* loc2 is a duplicated location. We need to check
  10637.                              if it should be inserted in case it will be
  10638.                              unduplicated.  */
  10639.                           if (loc2 != old_loc
  10640.                               && unduplicated_should_be_inserted (loc2))
  10641.                             {
  10642.                               swap_insertion (old_loc, loc2);
  10643.                               keep_in_target = 1;
  10644.                               break;
  10645.                             }
  10646.                         }
  10647.                     }
  10648.                 }
  10649.             }

  10650.           if (!keep_in_target)
  10651.             {
  10652.               if (remove_breakpoint (old_loc, mark_uninserted))
  10653.                 {
  10654.                   /* This is just about all we can do.  We could keep
  10655.                      this location on the global list, and try to
  10656.                      remove it next time, but there's no particular
  10657.                      reason why we will succeed next time.

  10658.                      Note that at this point, old_loc->owner is still
  10659.                      valid, as delete_breakpoint frees the breakpoint
  10660.                      only after calling us.  */
  10661.                   printf_filtered (_("warning: Error removing "
  10662.                                      "breakpoint %d\n"),
  10663.                                    old_loc->owner->number);
  10664.                 }
  10665.               removed = 1;
  10666.             }
  10667.         }

  10668.       if (!found_object)
  10669.         {
  10670.           if (removed && non_stop
  10671.               && breakpoint_address_is_meaningful (old_loc->owner)
  10672.               && !is_hardware_watchpoint (old_loc->owner))
  10673.             {
  10674.               /* This location was removed from the target.  In
  10675.                  non-stop mode, a race condition is possible where
  10676.                  we've removed a breakpoint, but stop events for that
  10677.                  breakpoint are already queued and will arrive later.
  10678.                  We apply an heuristic to be able to distinguish such
  10679.                  SIGTRAPs from other random SIGTRAPs: we keep this
  10680.                  breakpoint location for a bit, and will retire it
  10681.                  after we see some number of events.  The theory here
  10682.                  is that reporting of events should, "on the average",
  10683.                  be fair, so after a while we'll see events from all
  10684.                  threads that have anything of interest, and no longer
  10685.                  need to keep this breakpoint location around.  We
  10686.                  don't hold locations forever so to reduce chances of
  10687.                  mistaking a non-breakpoint SIGTRAP for a breakpoint
  10688.                  SIGTRAP.

  10689.                  The heuristic failing can be disastrous on
  10690.                  decr_pc_after_break targets.

  10691.                  On decr_pc_after_break targets, like e.g., x86-linux,
  10692.                  if we fail to recognize a late breakpoint SIGTRAP,
  10693.                  because events_till_retirement has reached 0 too
  10694.                  soon, we'll fail to do the PC adjustment, and report
  10695.                  a random SIGTRAP to the user.  When the user resumes
  10696.                  the inferior, it will most likely immediately crash
  10697.                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
  10698.                  corrupted, because of being resumed e.g., in the
  10699.                  middle of a multi-byte instruction, or skipped a
  10700.                  one-byte instruction.  This was actually seen happen
  10701.                  on native x86-linux, and should be less rare on
  10702.                  targets that do not support new thread events, like
  10703.                  remote, due to the heuristic depending on
  10704.                  thread_count.

  10705.                  Mistaking a random SIGTRAP for a breakpoint trap
  10706.                  causes similar symptoms (PC adjustment applied when
  10707.                  it shouldn't), but then again, playing with SIGTRAPs
  10708.                  behind the debugger's back is asking for trouble.

  10709.                  Since hardware watchpoint traps are always
  10710.                  distinguishable from other traps, so we don't need to
  10711.                  apply keep hardware watchpoint moribund locations
  10712.                  around.  We simply always ignore hardware watchpoint
  10713.                  traps we can no longer explain.  */

  10714.               old_loc->events_till_retirement = 3 * (thread_count () + 1);
  10715.               old_loc->owner = NULL;

  10716.               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
  10717.             }
  10718.           else
  10719.             {
  10720.               old_loc->owner = NULL;
  10721.               decref_bp_location (&old_loc);
  10722.             }
  10723.         }
  10724.     }

  10725.   /* Rescan breakpoints at the same address and section, marking the
  10726.      first one as "first" and any others as "duplicates".  This is so
  10727.      that the bpt instruction is only inserted once.  If we have a
  10728.      permanent breakpoint at the same place as BPT, make that one the
  10729.      official one, and the rest as duplicates.  Permanent breakpoints
  10730.      are sorted first for the same address.

  10731.      Do the same for hardware watchpoints, but also considering the
  10732.      watchpoint's type (regular/access/read) and length.  */

  10733.   bp_loc_first = NULL;
  10734.   wp_loc_first = NULL;
  10735.   awp_loc_first = NULL;
  10736.   rwp_loc_first = NULL;
  10737.   ALL_BP_LOCATIONS (loc, locp)
  10738.     {
  10739.       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
  10740.          non-NULL.  */
  10741.       struct bp_location **loc_first_p;
  10742.       b = loc->owner;

  10743.       if (!unduplicated_should_be_inserted (loc)
  10744.           || !breakpoint_address_is_meaningful (b)
  10745.           /* Don't detect duplicate for tracepoint locations because they are
  10746.            never duplicated.  See the comments in field `duplicate' of
  10747.            `struct bp_location'.  */
  10748.           || is_tracepoint (b))
  10749.         {
  10750.           /* Clear the condition modification flag.  */
  10751.           loc->condition_changed = condition_unchanged;
  10752.           continue;
  10753.         }

  10754.       /* Permanent breakpoint should always be inserted.  */
  10755.       if (loc->permanent && ! loc->inserted)
  10756.         internal_error (__FILE__, __LINE__,
  10757.                         _("allegedly permanent breakpoint is not "
  10758.                         "actually inserted"));

  10759.       if (b->type == bp_hardware_watchpoint)
  10760.         loc_first_p = &wp_loc_first;
  10761.       else if (b->type == bp_read_watchpoint)
  10762.         loc_first_p = &rwp_loc_first;
  10763.       else if (b->type == bp_access_watchpoint)
  10764.         loc_first_p = &awp_loc_first;
  10765.       else
  10766.         loc_first_p = &bp_loc_first;

  10767.       if (*loc_first_p == NULL
  10768.           || (overlay_debugging && loc->section != (*loc_first_p)->section)
  10769.           || !breakpoint_locations_match (loc, *loc_first_p))
  10770.         {
  10771.           *loc_first_p = loc;
  10772.           loc->duplicate = 0;

  10773.           if (is_breakpoint (loc->owner) && loc->condition_changed)
  10774.             {
  10775.               loc->needs_update = 1;
  10776.               /* Clear the condition modification flag.  */
  10777.               loc->condition_changed = condition_unchanged;
  10778.             }
  10779.           continue;
  10780.         }


  10781.       /* This and the above ensure the invariant that the first location
  10782.          is not duplicated, and is the inserted one.
  10783.          All following are marked as duplicated, and are not inserted.  */
  10784.       if (loc->inserted)
  10785.         swap_insertion (loc, *loc_first_p);
  10786.       loc->duplicate = 1;

  10787.       /* Clear the condition modification flag.  */
  10788.       loc->condition_changed = condition_unchanged;

  10789.       if (loc->inserted && !loc->permanent
  10790.           && (*loc_first_p)->permanent)
  10791.         internal_error (__FILE__, __LINE__,
  10792.                         _("another breakpoint was inserted on top of "
  10793.                         "a permanent breakpoint"));
  10794.     }

  10795.   if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
  10796.     {
  10797.       if (insert_mode != UGLL_DONT_INSERT)
  10798.         insert_breakpoint_locations ();
  10799.       else
  10800.         {
  10801.           /* Even though the caller told us to not insert new
  10802.              locations, we may still need to update conditions on the
  10803.              target's side of breakpoints that were already inserted
  10804.              if the target is evaluating breakpoint conditions.  We
  10805.              only update conditions for locations that are marked
  10806.              "needs_update".  */
  10807.           update_inserted_breakpoint_locations ();
  10808.         }
  10809.     }

  10810.   if (insert_mode != UGLL_DONT_INSERT)
  10811.     download_tracepoint_locations ();

  10812.   do_cleanups (cleanups);
  10813. }

  10814. void
  10815. breakpoint_retire_moribund (void)
  10816. {
  10817.   struct bp_location *loc;
  10818.   int ix;

  10819.   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
  10820.     if (--(loc->events_till_retirement) == 0)
  10821.       {
  10822.         decref_bp_location (&loc);
  10823.         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
  10824.         --ix;
  10825.       }
  10826. }

  10827. static void
  10828. update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
  10829. {
  10830.   volatile struct gdb_exception e;

  10831.   TRY_CATCH (e, RETURN_MASK_ERROR)
  10832.     update_global_location_list (insert_mode);
  10833. }

  10834. /* Clear BKP from a BPS.  */

  10835. static void
  10836. bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
  10837. {
  10838.   bpstat bs;

  10839.   for (bs = bps; bs; bs = bs->next)
  10840.     if (bs->breakpoint_at == bpt)
  10841.       {
  10842.         bs->breakpoint_at = NULL;
  10843.         bs->old_val = NULL;
  10844.         /* bs->commands will be freed later.  */
  10845.       }
  10846. }

  10847. /* Callback for iterate_over_threads.  */
  10848. static int
  10849. bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
  10850. {
  10851.   struct breakpoint *bpt = data;

  10852.   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
  10853.   return 0;
  10854. }

  10855. /* Helper for breakpoint and tracepoint breakpoint_ops->mention
  10856.    callbacks.  */

  10857. static void
  10858. say_where (struct breakpoint *b)
  10859. {
  10860.   struct value_print_options opts;

  10861.   get_user_print_options (&opts);

  10862.   /* i18n: cagney/2005-02-11: Below needs to be merged into a
  10863.      single string.  */
  10864.   if (b->loc == NULL)
  10865.     {
  10866.       printf_filtered (_(" (%s) pending."), b->addr_string);
  10867.     }
  10868.   else
  10869.     {
  10870.       if (opts.addressprint || b->loc->symtab == NULL)
  10871.         {
  10872.           printf_filtered (" at ");
  10873.           fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
  10874.                           gdb_stdout);
  10875.         }
  10876.       if (b->loc->symtab != NULL)
  10877.         {
  10878.           /* If there is a single location, we can print the location
  10879.              more nicely.  */
  10880.           if (b->loc->next == NULL)
  10881.             printf_filtered (": file %s, line %d.",
  10882.                              symtab_to_filename_for_display (b->loc->symtab),
  10883.                              b->loc->line_number);
  10884.           else
  10885.             /* This is not ideal, but each location may have a
  10886.                different file name, and this at least reflects the
  10887.                real situation somewhat.  */
  10888.             printf_filtered (": %s.", b->addr_string);
  10889.         }

  10890.       if (b->loc->next)
  10891.         {
  10892.           struct bp_location *loc = b->loc;
  10893.           int n = 0;
  10894.           for (; loc; loc = loc->next)
  10895.             ++n;
  10896.           printf_filtered (" (%d locations)", n);
  10897.         }
  10898.     }
  10899. }

  10900. /* Default bp_location_ops methods.  */

  10901. static void
  10902. bp_location_dtor (struct bp_location *self)
  10903. {
  10904.   xfree (self->cond);
  10905.   if (self->cond_bytecode)
  10906.     free_agent_expr (self->cond_bytecode);
  10907.   xfree (self->function_name);

  10908.   VEC_free (agent_expr_p, self->target_info.conditions);
  10909.   VEC_free (agent_expr_p, self->target_info.tcommands);
  10910. }

  10911. static const struct bp_location_ops bp_location_ops =
  10912. {
  10913.   bp_location_dtor
  10914. };

  10915. /* Default breakpoint_ops methods all breakpoint_ops ultimately
  10916.    inherit from.  */

  10917. static void
  10918. base_breakpoint_dtor (struct breakpoint *self)
  10919. {
  10920.   decref_counted_command_line (&self->commands);
  10921.   xfree (self->cond_string);
  10922.   xfree (self->extra_string);
  10923.   xfree (self->addr_string);
  10924.   xfree (self->filter);
  10925.   xfree (self->addr_string_range_end);
  10926. }

  10927. static struct bp_location *
  10928. base_breakpoint_allocate_location (struct breakpoint *self)
  10929. {
  10930.   struct bp_location *loc;

  10931.   loc = XNEW (struct bp_location);
  10932.   init_bp_location (loc, &bp_location_ops, self);
  10933.   return loc;
  10934. }

  10935. static void
  10936. base_breakpoint_re_set (struct breakpoint *b)
  10937. {
  10938.   /* Nothing to re-set. */
  10939. }

  10940. #define internal_error_pure_virtual_called() \
  10941.   gdb_assert_not_reached ("pure virtual function called")

  10942. static int
  10943. base_breakpoint_insert_location (struct bp_location *bl)
  10944. {
  10945.   internal_error_pure_virtual_called ();
  10946. }

  10947. static int
  10948. base_breakpoint_remove_location (struct bp_location *bl)
  10949. {
  10950.   internal_error_pure_virtual_called ();
  10951. }

  10952. static int
  10953. base_breakpoint_breakpoint_hit (const struct bp_location *bl,
  10954.                                 struct address_space *aspace,
  10955.                                 CORE_ADDR bp_addr,
  10956.                                 const struct target_waitstatus *ws)
  10957. {
  10958.   internal_error_pure_virtual_called ();
  10959. }

  10960. static void
  10961. base_breakpoint_check_status (bpstat bs)
  10962. {
  10963.   /* Always stop.   */
  10964. }

  10965. /* A "works_in_software_mode" breakpoint_ops method that just internal
  10966.    errors.  */

  10967. static int
  10968. base_breakpoint_works_in_software_mode (const struct breakpoint *b)
  10969. {
  10970.   internal_error_pure_virtual_called ();
  10971. }

  10972. /* A "resources_needed" breakpoint_ops method that just internal
  10973.    errors.  */

  10974. static int
  10975. base_breakpoint_resources_needed (const struct bp_location *bl)
  10976. {
  10977.   internal_error_pure_virtual_called ();
  10978. }

  10979. static enum print_stop_action
  10980. base_breakpoint_print_it (bpstat bs)
  10981. {
  10982.   internal_error_pure_virtual_called ();
  10983. }

  10984. static void
  10985. base_breakpoint_print_one_detail (const struct breakpoint *self,
  10986.                                   struct ui_out *uiout)
  10987. {
  10988.   /* nothing */
  10989. }

  10990. static void
  10991. base_breakpoint_print_mention (struct breakpoint *b)
  10992. {
  10993.   internal_error_pure_virtual_called ();
  10994. }

  10995. static void
  10996. base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
  10997. {
  10998.   internal_error_pure_virtual_called ();
  10999. }

  11000. static void
  11001. base_breakpoint_create_sals_from_address (char **arg,
  11002.                                           struct linespec_result *canonical,
  11003.                                           enum bptype type_wanted,
  11004.                                           char *addr_start,
  11005.                                           char **copy_arg)
  11006. {
  11007.   internal_error_pure_virtual_called ();
  11008. }

  11009. static void
  11010. base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
  11011.                                         struct linespec_result *c,
  11012.                                         char *cond_string,
  11013.                                         char *extra_string,
  11014.                                         enum bptype type_wanted,
  11015.                                         enum bpdisp disposition,
  11016.                                         int thread,
  11017.                                         int task, int ignore_count,
  11018.                                         const struct breakpoint_ops *o,
  11019.                                         int from_tty, int enabled,
  11020.                                         int internal, unsigned flags)
  11021. {
  11022.   internal_error_pure_virtual_called ();
  11023. }

  11024. static void
  11025. base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
  11026.                                  struct symtabs_and_lines *sals)
  11027. {
  11028.   internal_error_pure_virtual_called ();
  11029. }

  11030. /* The default 'explains_signal' method.  */

  11031. static int
  11032. base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
  11033. {
  11034.   return 1;
  11035. }

  11036. /* The default "after_condition_true" method.  */

  11037. static void
  11038. base_breakpoint_after_condition_true (struct bpstats *bs)
  11039. {
  11040.   /* Nothing to do.   */
  11041. }

  11042. struct breakpoint_ops base_breakpoint_ops =
  11043. {
  11044.   base_breakpoint_dtor,
  11045.   base_breakpoint_allocate_location,
  11046.   base_breakpoint_re_set,
  11047.   base_breakpoint_insert_location,
  11048.   base_breakpoint_remove_location,
  11049.   base_breakpoint_breakpoint_hit,
  11050.   base_breakpoint_check_status,
  11051.   base_breakpoint_resources_needed,
  11052.   base_breakpoint_works_in_software_mode,
  11053.   base_breakpoint_print_it,
  11054.   NULL,
  11055.   base_breakpoint_print_one_detail,
  11056.   base_breakpoint_print_mention,
  11057.   base_breakpoint_print_recreate,
  11058.   base_breakpoint_create_sals_from_address,
  11059.   base_breakpoint_create_breakpoints_sal,
  11060.   base_breakpoint_decode_linespec,
  11061.   base_breakpoint_explains_signal,
  11062.   base_breakpoint_after_condition_true,
  11063. };

  11064. /* Default breakpoint_ops methods.  */

  11065. static void
  11066. bkpt_re_set (struct breakpoint *b)
  11067. {
  11068.   /* FIXME: is this still reachable?  */
  11069.   if (b->addr_string == NULL)
  11070.     {
  11071.       /* Anything without a string can't be re-set.  */
  11072.       delete_breakpoint (b);
  11073.       return;
  11074.     }

  11075.   breakpoint_re_set_default (b);
  11076. }

  11077. static int
  11078. bkpt_insert_location (struct bp_location *bl)
  11079. {
  11080.   if (bl->loc_type == bp_loc_hardware_breakpoint)
  11081.     return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
  11082.   else
  11083.     return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
  11084. }

  11085. static int
  11086. bkpt_remove_location (struct bp_location *bl)
  11087. {
  11088.   if (bl->loc_type == bp_loc_hardware_breakpoint)
  11089.     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
  11090.   else
  11091.     return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
  11092. }

  11093. static int
  11094. bkpt_breakpoint_hit (const struct bp_location *bl,
  11095.                      struct address_space *aspace, CORE_ADDR bp_addr,
  11096.                      const struct target_waitstatus *ws)
  11097. {
  11098.   if (ws->kind != TARGET_WAITKIND_STOPPED
  11099.       || ws->value.sig != GDB_SIGNAL_TRAP)
  11100.     return 0;

  11101.   if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
  11102.                                  aspace, bp_addr))
  11103.     return 0;

  11104.   if (overlay_debugging                /* unmapped overlay section */
  11105.       && section_is_overlay (bl->section)
  11106.       && !section_is_mapped (bl->section))
  11107.     return 0;

  11108.   return 1;
  11109. }

  11110. static int
  11111. dprintf_breakpoint_hit (const struct bp_location *bl,
  11112.                         struct address_space *aspace, CORE_ADDR bp_addr,
  11113.                         const struct target_waitstatus *ws)
  11114. {
  11115.   if (dprintf_style == dprintf_style_agent
  11116.       && target_can_run_breakpoint_commands ())
  11117.     {
  11118.       /* An agent-style dprintf never causes a stop.  If we see a trap
  11119.          for this address it must be for a breakpoint that happens to
  11120.          be set at the same address.  */
  11121.       return 0;
  11122.     }

  11123.   return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
  11124. }

  11125. static int
  11126. bkpt_resources_needed (const struct bp_location *bl)
  11127. {
  11128.   gdb_assert (bl->owner->type == bp_hardware_breakpoint);

  11129.   return 1;
  11130. }

  11131. static enum print_stop_action
  11132. bkpt_print_it (bpstat bs)
  11133. {
  11134.   struct breakpoint *b;
  11135.   const struct bp_location *bl;
  11136.   int bp_temp;
  11137.   struct ui_out *uiout = current_uiout;

  11138.   gdb_assert (bs->bp_location_at != NULL);

  11139.   bl = bs->bp_location_at;
  11140.   b = bs->breakpoint_at;

  11141.   bp_temp = b->disposition == disp_del;
  11142.   if (bl->address != bl->requested_address)
  11143.     breakpoint_adjustment_warning (bl->requested_address,
  11144.                                    bl->address,
  11145.                                    b->number, 1);
  11146.   annotate_breakpoint (b->number);
  11147.   if (bp_temp)
  11148.     ui_out_text (uiout, "\nTemporary breakpoint ");
  11149.   else
  11150.     ui_out_text (uiout, "\nBreakpoint ");
  11151.   if (ui_out_is_mi_like_p (uiout))
  11152.     {
  11153.       ui_out_field_string (uiout, "reason",
  11154.                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
  11155.       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
  11156.     }
  11157.   ui_out_field_int (uiout, "bkptno", b->number);
  11158.   ui_out_text (uiout, ", ");

  11159.   return PRINT_SRC_AND_LOC;
  11160. }

  11161. static void
  11162. bkpt_print_mention (struct breakpoint *b)
  11163. {
  11164.   if (ui_out_is_mi_like_p (current_uiout))
  11165.     return;

  11166.   switch (b->type)
  11167.     {
  11168.     case bp_breakpoint:
  11169.     case bp_gnu_ifunc_resolver:
  11170.       if (b->disposition == disp_del)
  11171.         printf_filtered (_("Temporary breakpoint"));
  11172.       else
  11173.         printf_filtered (_("Breakpoint"));
  11174.       printf_filtered (_(" %d"), b->number);
  11175.       if (b->type == bp_gnu_ifunc_resolver)
  11176.         printf_filtered (_(" at gnu-indirect-function resolver"));
  11177.       break;
  11178.     case bp_hardware_breakpoint:
  11179.       printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
  11180.       break;
  11181.     case bp_dprintf:
  11182.       printf_filtered (_("Dprintf %d"), b->number);
  11183.       break;
  11184.     }

  11185.   say_where (b);
  11186. }

  11187. static void
  11188. bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
  11189. {
  11190.   if (tp->type == bp_breakpoint && tp->disposition == disp_del)
  11191.     fprintf_unfiltered (fp, "tbreak");
  11192.   else if (tp->type == bp_breakpoint)
  11193.     fprintf_unfiltered (fp, "break");
  11194.   else if (tp->type == bp_hardware_breakpoint
  11195.            && tp->disposition == disp_del)
  11196.     fprintf_unfiltered (fp, "thbreak");
  11197.   else if (tp->type == bp_hardware_breakpoint)
  11198.     fprintf_unfiltered (fp, "hbreak");
  11199.   else
  11200.     internal_error (__FILE__, __LINE__,
  11201.                     _("unhandled breakpoint type %d"), (int) tp->type);

  11202.   fprintf_unfiltered (fp, " %s", tp->addr_string);
  11203.   print_recreate_thread (tp, fp);
  11204. }

  11205. static void
  11206. bkpt_create_sals_from_address (char **arg,
  11207.                                struct linespec_result *canonical,
  11208.                                enum bptype type_wanted,
  11209.                                char *addr_start, char **copy_arg)
  11210. {
  11211.   create_sals_from_address_default (arg, canonical, type_wanted,
  11212.                                     addr_start, copy_arg);
  11213. }

  11214. static void
  11215. bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
  11216.                              struct linespec_result *canonical,
  11217.                              char *cond_string,
  11218.                              char *extra_string,
  11219.                              enum bptype type_wanted,
  11220.                              enum bpdisp disposition,
  11221.                              int thread,
  11222.                              int task, int ignore_count,
  11223.                              const struct breakpoint_ops *ops,
  11224.                              int from_tty, int enabled,
  11225.                              int internal, unsigned flags)
  11226. {
  11227.   create_breakpoints_sal_default (gdbarch, canonical,
  11228.                                   cond_string, extra_string,
  11229.                                   type_wanted,
  11230.                                   disposition, thread, task,
  11231.                                   ignore_count, ops, from_tty,
  11232.                                   enabled, internal, flags);
  11233. }

  11234. static void
  11235. bkpt_decode_linespec (struct breakpoint *b, char **s,
  11236.                       struct symtabs_and_lines *sals)
  11237. {
  11238.   decode_linespec_default (b, s, sals);
  11239. }

  11240. /* Virtual table for internal breakpoints.  */

  11241. static void
  11242. internal_bkpt_re_set (struct breakpoint *b)
  11243. {
  11244.   switch (b->type)
  11245.     {
  11246.       /* Delete overlay event and longjmp master breakpoints; they
  11247.          will be reset later by breakpoint_re_set.  */
  11248.     case bp_overlay_event:
  11249.     case bp_longjmp_master:
  11250.     case bp_std_terminate_master:
  11251.     case bp_exception_master:
  11252.       delete_breakpoint (b);
  11253.       break;

  11254.       /* This breakpoint is special, it's set up when the inferior
  11255.          starts and we really don't want to touch it.  */
  11256.     case bp_shlib_event:

  11257.       /* Like bp_shlib_event, this breakpoint type is special.  Once
  11258.          it is set up, we do not want to touch it.  */
  11259.     case bp_thread_event:
  11260.       break;
  11261.     }
  11262. }

  11263. static void
  11264. internal_bkpt_check_status (bpstat bs)
  11265. {
  11266.   if (bs->breakpoint_at->type == bp_shlib_event)
  11267.     {
  11268.       /* If requested, stop when the dynamic linker notifies GDB of
  11269.          events.  This allows the user to get control and place
  11270.          breakpoints in initializer routines for dynamically loaded
  11271.          objects (among other things).  */
  11272.       bs->stop = stop_on_solib_events;
  11273.       bs->print = stop_on_solib_events;
  11274.     }
  11275.   else
  11276.     bs->stop = 0;
  11277. }

  11278. static enum print_stop_action
  11279. internal_bkpt_print_it (bpstat bs)
  11280. {
  11281.   struct breakpoint *b;

  11282.   b = bs->breakpoint_at;

  11283.   switch (b->type)
  11284.     {
  11285.     case bp_shlib_event:
  11286.       /* Did we stop because the user set the stop_on_solib_events
  11287.          variable?  (If so, we report this as a generic, "Stopped due
  11288.          to shlib event" message.) */
  11289.       print_solib_event (0);
  11290.       break;

  11291.     case bp_thread_event:
  11292.       /* Not sure how we will get here.
  11293.          GDB should not stop for these breakpoints.  */
  11294.       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
  11295.       break;

  11296.     case bp_overlay_event:
  11297.       /* By analogy with the thread event, GDB should not stop for these.  */
  11298.       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
  11299.       break;

  11300.     case bp_longjmp_master:
  11301.       /* These should never be enabled.  */
  11302.       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
  11303.       break;

  11304.     case bp_std_terminate_master:
  11305.       /* These should never be enabled.  */
  11306.       printf_filtered (_("std::terminate Master Breakpoint: "
  11307.                          "gdb should not stop!\n"));
  11308.       break;

  11309.     case bp_exception_master:
  11310.       /* These should never be enabled.  */
  11311.       printf_filtered (_("Exception Master Breakpoint: "
  11312.                          "gdb should not stop!\n"));
  11313.       break;
  11314.     }

  11315.   return PRINT_NOTHING;
  11316. }

  11317. static void
  11318. internal_bkpt_print_mention (struct breakpoint *b)
  11319. {
  11320.   /* Nothing to mention.  These breakpoints are internal.  */
  11321. }

  11322. /* Virtual table for momentary breakpoints  */

  11323. static void
  11324. momentary_bkpt_re_set (struct breakpoint *b)
  11325. {
  11326.   /* Keep temporary breakpoints, which can be encountered when we step
  11327.      over a dlopen call and solib_add is resetting the breakpoints.
  11328.      Otherwise these should have been blown away via the cleanup chain
  11329.      or by breakpoint_init_inferior when we rerun the executable.  */
  11330. }

  11331. static void
  11332. momentary_bkpt_check_status (bpstat bs)
  11333. {
  11334.   /* Nothing.  The point of these breakpoints is causing a stop.  */
  11335. }

  11336. static enum print_stop_action
  11337. momentary_bkpt_print_it (bpstat bs)
  11338. {
  11339.   struct ui_out *uiout = current_uiout;

  11340.   if (ui_out_is_mi_like_p (uiout))
  11341.     {
  11342.       struct breakpoint *b = bs->breakpoint_at;

  11343.       switch (b->type)
  11344.         {
  11345.         case bp_finish:
  11346.           ui_out_field_string
  11347.             (uiout, "reason",
  11348.              async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
  11349.           break;

  11350.         case bp_until:
  11351.           ui_out_field_string
  11352.             (uiout, "reason",
  11353.              async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
  11354.           break;
  11355.         }
  11356.     }

  11357.   return PRINT_UNKNOWN;
  11358. }

  11359. static void
  11360. momentary_bkpt_print_mention (struct breakpoint *b)
  11361. {
  11362.   /* Nothing to mention.  These breakpoints are internal.  */
  11363. }

  11364. /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.

  11365.    It gets cleared already on the removal of the first one of such placed
  11366.    breakpoints.  This is OK as they get all removed altogether.  */

  11367. static void
  11368. longjmp_bkpt_dtor (struct breakpoint *self)
  11369. {
  11370.   struct thread_info *tp = find_thread_id (self->thread);

  11371.   if (tp)
  11372.     tp->initiating_frame = null_frame_id;

  11373.   momentary_breakpoint_ops.dtor (self);
  11374. }

  11375. /* Specific methods for probe breakpoints.  */

  11376. static int
  11377. bkpt_probe_insert_location (struct bp_location *bl)
  11378. {
  11379.   int v = bkpt_insert_location (bl);

  11380.   if (v == 0)
  11381.     {
  11382.       /* The insertion was successful, now let's set the probe's semaphore
  11383.          if needed.  */
  11384.       if (bl->probe.probe->pops->set_semaphore != NULL)
  11385.         bl->probe.probe->pops->set_semaphore (bl->probe.probe,
  11386.                                               bl->probe.objfile,
  11387.                                               bl->gdbarch);
  11388.     }

  11389.   return v;
  11390. }

  11391. static int
  11392. bkpt_probe_remove_location (struct bp_location *bl)
  11393. {
  11394.   /* Let's clear the semaphore before removing the location.  */
  11395.   if (bl->probe.probe->pops->clear_semaphore != NULL)
  11396.     bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
  11397.                                             bl->probe.objfile,
  11398.                                             bl->gdbarch);

  11399.   return bkpt_remove_location (bl);
  11400. }

  11401. static void
  11402. bkpt_probe_create_sals_from_address (char **arg,
  11403.                                      struct linespec_result *canonical,
  11404.                                      enum bptype type_wanted,
  11405.                                      char *addr_start, char **copy_arg)
  11406. {
  11407.   struct linespec_sals lsal;

  11408.   lsal.sals = parse_probes (arg, canonical);

  11409.   *copy_arg = xstrdup (canonical->addr_string);
  11410.   lsal.canonical = xstrdup (*copy_arg);

  11411.   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
  11412. }

  11413. static void
  11414. bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
  11415.                             struct symtabs_and_lines *sals)
  11416. {
  11417.   *sals = parse_probes (s, NULL);
  11418.   if (!sals->sals)
  11419.     error (_("probe not found"));
  11420. }

  11421. /* The breakpoint_ops structure to be used in tracepoints.  */

  11422. static void
  11423. tracepoint_re_set (struct breakpoint *b)
  11424. {
  11425.   breakpoint_re_set_default (b);
  11426. }

  11427. static int
  11428. tracepoint_breakpoint_hit (const struct bp_location *bl,
  11429.                            struct address_space *aspace, CORE_ADDR bp_addr,
  11430.                            const struct target_waitstatus *ws)
  11431. {
  11432.   /* By definition, the inferior does not report stops at
  11433.      tracepoints.  */
  11434.   return 0;
  11435. }

  11436. static void
  11437. tracepoint_print_one_detail (const struct breakpoint *self,
  11438.                              struct ui_out *uiout)
  11439. {
  11440.   struct tracepoint *tp = (struct tracepoint *) self;
  11441.   if (tp->static_trace_marker_id)
  11442.     {
  11443.       gdb_assert (self->type == bp_static_tracepoint);

  11444.       ui_out_text (uiout, "\tmarker id is ");
  11445.       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
  11446.                            tp->static_trace_marker_id);
  11447.       ui_out_text (uiout, "\n");
  11448.     }
  11449. }

  11450. static void
  11451. tracepoint_print_mention (struct breakpoint *b)
  11452. {
  11453.   if (ui_out_is_mi_like_p (current_uiout))
  11454.     return;

  11455.   switch (b->type)
  11456.     {
  11457.     case bp_tracepoint:
  11458.       printf_filtered (_("Tracepoint"));
  11459.       printf_filtered (_(" %d"), b->number);
  11460.       break;
  11461.     case bp_fast_tracepoint:
  11462.       printf_filtered (_("Fast tracepoint"));
  11463.       printf_filtered (_(" %d"), b->number);
  11464.       break;
  11465.     case bp_static_tracepoint:
  11466.       printf_filtered (_("Static tracepoint"));
  11467.       printf_filtered (_(" %d"), b->number);
  11468.       break;
  11469.     default:
  11470.       internal_error (__FILE__, __LINE__,
  11471.                       _("unhandled tracepoint type %d"), (int) b->type);
  11472.     }

  11473.   say_where (b);
  11474. }

  11475. static void
  11476. tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
  11477. {
  11478.   struct tracepoint *tp = (struct tracepoint *) self;

  11479.   if (self->type == bp_fast_tracepoint)
  11480.     fprintf_unfiltered (fp, "ftrace");
  11481.   if (self->type == bp_static_tracepoint)
  11482.     fprintf_unfiltered (fp, "strace");
  11483.   else if (self->type == bp_tracepoint)
  11484.     fprintf_unfiltered (fp, "trace");
  11485.   else
  11486.     internal_error (__FILE__, __LINE__,
  11487.                     _("unhandled tracepoint type %d"), (int) self->type);

  11488.   fprintf_unfiltered (fp, " %s", self->addr_string);
  11489.   print_recreate_thread (self, fp);

  11490.   if (tp->pass_count)
  11491.     fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
  11492. }

  11493. static void
  11494. tracepoint_create_sals_from_address (char **arg,
  11495.                                      struct linespec_result *canonical,
  11496.                                      enum bptype type_wanted,
  11497.                                      char *addr_start, char **copy_arg)
  11498. {
  11499.   create_sals_from_address_default (arg, canonical, type_wanted,
  11500.                                     addr_start, copy_arg);
  11501. }

  11502. static void
  11503. tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
  11504.                                    struct linespec_result *canonical,
  11505.                                    char *cond_string,
  11506.                                    char *extra_string,
  11507.                                    enum bptype type_wanted,
  11508.                                    enum bpdisp disposition,
  11509.                                    int thread,
  11510.                                    int task, int ignore_count,
  11511.                                    const struct breakpoint_ops *ops,
  11512.                                    int from_tty, int enabled,
  11513.                                    int internal, unsigned flags)
  11514. {
  11515.   create_breakpoints_sal_default (gdbarch, canonical,
  11516.                                   cond_string, extra_string,
  11517.                                   type_wanted,
  11518.                                   disposition, thread, task,
  11519.                                   ignore_count, ops, from_tty,
  11520.                                   enabled, internal, flags);
  11521. }

  11522. static void
  11523. tracepoint_decode_linespec (struct breakpoint *b, char **s,
  11524.                             struct symtabs_and_lines *sals)
  11525. {
  11526.   decode_linespec_default (b, s, sals);
  11527. }

  11528. struct breakpoint_ops tracepoint_breakpoint_ops;

  11529. /* The breakpoint_ops structure to be use on tracepoints placed in a
  11530.    static probe.  */

  11531. static void
  11532. tracepoint_probe_create_sals_from_address (char **arg,
  11533.                                            struct linespec_result *canonical,
  11534.                                            enum bptype type_wanted,
  11535.                                            char *addr_start, char **copy_arg)
  11536. {
  11537.   /* We use the same method for breakpoint on probes.  */
  11538.   bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
  11539.                                        addr_start, copy_arg);
  11540. }

  11541. static void
  11542. tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
  11543.                                   struct symtabs_and_lines *sals)
  11544. {
  11545.   /* We use the same method for breakpoint on probes.  */
  11546.   bkpt_probe_decode_linespec (b, s, sals);
  11547. }

  11548. static struct breakpoint_ops tracepoint_probe_breakpoint_ops;

  11549. /* Dprintf breakpoint_ops methods.  */

  11550. static void
  11551. dprintf_re_set (struct breakpoint *b)
  11552. {
  11553.   breakpoint_re_set_default (b);

  11554.   /* This breakpoint could have been pending, and be resolved now, and
  11555.      if so, we should now have the extra string.  If we don't, the
  11556.      dprintf was malformed when created, but we couldn't tell because
  11557.      we can't extract the extra string until the location is
  11558.      resolved.  */
  11559.   if (b->loc != NULL && b->extra_string == NULL)
  11560.     error (_("Format string required"));

  11561.   /* 1 - connect to target 1, that can run breakpoint commands.
  11562.      2 - create a dprintf, which resolves fine.
  11563.      3 - disconnect from target 1
  11564.      4 - connect to target 2, that can NOT run breakpoint commands.

  11565.      After steps #3/#4, you'll want the dprintf command list to
  11566.      be updated, because target 1 and 2 may well return different
  11567.      answers for target_can_run_breakpoint_commands().
  11568.      Given absence of finer grained resetting, we get to do
  11569.      it all the time.  */
  11570.   if (b->extra_string != NULL)
  11571.     update_dprintf_command_list (b);
  11572. }

  11573. /* Implement the "print_recreate" breakpoint_ops method for dprintf.  */

  11574. static void
  11575. dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
  11576. {
  11577.   fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
  11578.                       tp->extra_string);
  11579.   print_recreate_thread (tp, fp);
  11580. }

  11581. /* Implement the "after_condition_true" breakpoint_ops method for
  11582.    dprintf.

  11583.    dprintf's are implemented with regular commands in their command
  11584.    list, but we run the commands here instead of before presenting the
  11585.    stop to the user, as dprintf's don't actually cause a stop.  This
  11586.    also makes it so that the commands of multiple dprintfs at the same
  11587.    address are all handled.  */

  11588. static void
  11589. dprintf_after_condition_true (struct bpstats *bs)
  11590. {
  11591.   struct cleanup *old_chain;
  11592.   struct bpstats tmp_bs = { NULL };
  11593.   struct bpstats *tmp_bs_p = &tmp_bs;

  11594.   /* dprintf's never cause a stop.  This wasn't set in the
  11595.      check_status hook instead because that would make the dprintf's
  11596.      condition not be evaluated.  */
  11597.   bs->stop = 0;

  11598.   /* Run the command list here.  Take ownership of it instead of
  11599.      copying.  We never want these commands to run later in
  11600.      bpstat_do_actions, if a breakpoint that causes a stop happens to
  11601.      be set at same address as this dprintf, or even if running the
  11602.      commands here throws.  */
  11603.   tmp_bs.commands = bs->commands;
  11604.   bs->commands = NULL;
  11605.   old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);

  11606.   bpstat_do_actions_1 (&tmp_bs_p);

  11607.   /* 'tmp_bs.commands' will usually be NULL by now, but
  11608.      bpstat_do_actions_1 may return early without processing the whole
  11609.      list.  */
  11610.   do_cleanups (old_chain);
  11611. }

  11612. /* The breakpoint_ops structure to be used on static tracepoints with
  11613.    markers (`-m').  */

  11614. static void
  11615. strace_marker_create_sals_from_address (char **arg,
  11616.                                         struct linespec_result *canonical,
  11617.                                         enum bptype type_wanted,
  11618.                                         char *addr_start, char **copy_arg)
  11619. {
  11620.   struct linespec_sals lsal;

  11621.   lsal.sals = decode_static_tracepoint_spec (arg);

  11622.   *copy_arg = savestring (addr_start, *arg - addr_start);

  11623.   canonical->addr_string = xstrdup (*copy_arg);
  11624.   lsal.canonical = xstrdup (*copy_arg);
  11625.   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
  11626. }

  11627. static void
  11628. strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
  11629.                                       struct linespec_result *canonical,
  11630.                                       char *cond_string,
  11631.                                       char *extra_string,
  11632.                                       enum bptype type_wanted,
  11633.                                       enum bpdisp disposition,
  11634.                                       int thread,
  11635.                                       int task, int ignore_count,
  11636.                                       const struct breakpoint_ops *ops,
  11637.                                       int from_tty, int enabled,
  11638.                                       int internal, unsigned flags)
  11639. {
  11640.   int i;
  11641.   struct linespec_sals *lsal = VEC_index (linespec_sals,
  11642.                                           canonical->sals, 0);

  11643.   /* If the user is creating a static tracepoint by marker id
  11644.      (strace -m MARKER_ID), then store the sals index, so that
  11645.      breakpoint_re_set can try to match up which of the newly
  11646.      found markers corresponds to this one, and, don't try to
  11647.      expand multiple locations for each sal, given than SALS
  11648.      already should contain all sals for MARKER_ID.  */

  11649.   for (i = 0; i < lsal->sals.nelts; ++i)
  11650.     {
  11651.       struct symtabs_and_lines expanded;
  11652.       struct tracepoint *tp;
  11653.       struct cleanup *old_chain;
  11654.       char *addr_string;

  11655.       expanded.nelts = 1;
  11656.       expanded.sals = &lsal->sals.sals[i];

  11657.       addr_string = xstrdup (canonical->addr_string);
  11658.       old_chain = make_cleanup (xfree, addr_string);

  11659.       tp = XCNEW (struct tracepoint);
  11660.       init_breakpoint_sal (&tp->base, gdbarch, expanded,
  11661.                            addr_string, NULL,
  11662.                            cond_string, extra_string,
  11663.                            type_wanted, disposition,
  11664.                            thread, task, ignore_count, ops,
  11665.                            from_tty, enabled, internal, flags,
  11666.                            canonical->special_display);
  11667.       /* Given that its possible to have multiple markers with
  11668.          the same string id, if the user is creating a static
  11669.          tracepoint by marker id ("strace -m MARKER_ID"), then
  11670.          store the sals index, so that breakpoint_re_set can
  11671.          try to match up which of the newly found markers
  11672.          corresponds to this one  */
  11673.       tp->static_trace_marker_id_idx = i;

  11674.       install_breakpoint (internal, &tp->base, 0);

  11675.       discard_cleanups (old_chain);
  11676.     }
  11677. }

  11678. static void
  11679. strace_marker_decode_linespec (struct breakpoint *b, char **s,
  11680.                                struct symtabs_and_lines *sals)
  11681. {
  11682.   struct tracepoint *tp = (struct tracepoint *) b;

  11683.   *sals = decode_static_tracepoint_spec (s);
  11684.   if (sals->nelts > tp->static_trace_marker_id_idx)
  11685.     {
  11686.       sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
  11687.       sals->nelts = 1;
  11688.     }
  11689.   else
  11690.     error (_("marker %s not found"), tp->static_trace_marker_id);
  11691. }

  11692. static struct breakpoint_ops strace_marker_breakpoint_ops;

  11693. static int
  11694. strace_marker_p (struct breakpoint *b)
  11695. {
  11696.   return b->ops == &strace_marker_breakpoint_ops;
  11697. }

  11698. /* Delete a breakpoint and clean up all traces of it in the data
  11699.    structures.  */

  11700. void
  11701. delete_breakpoint (struct breakpoint *bpt)
  11702. {
  11703.   struct breakpoint *b;

  11704.   gdb_assert (bpt != NULL);

  11705.   /* Has this bp already been deleted?  This can happen because
  11706.      multiple lists can hold pointers to bp'sbpstat lists are
  11707.      especial culprits.

  11708.      One example of this happening is a watchpoint's scope bp.  When
  11709.      the scope bp triggers, we notice that the watchpoint is out of
  11710.      scope, and delete it.  We also delete its scope bp.  But the
  11711.      scope bp is marked "auto-deleting", and is already on a bpstat.
  11712.      That bpstat is then checked for auto-deleting bp's, which are
  11713.      deleted.

  11714.      A real solution to this problem might involve reference counts in
  11715.      bp's, and/or giving them pointers back to their referencing
  11716.      bpstat's, and teaching delete_breakpoint to only free a bp's
  11717.      storage when no more references were extent.  A cheaper bandaid
  11718.      was chosen.  */
  11719.   if (bpt->type == bp_none)
  11720.     return;

  11721.   /* At least avoid this stale reference until the reference counting
  11722.      of breakpoints gets resolved.  */
  11723.   if (bpt->related_breakpoint != bpt)
  11724.     {
  11725.       struct breakpoint *related;
  11726.       struct watchpoint *w;

  11727.       if (bpt->type == bp_watchpoint_scope)
  11728.         w = (struct watchpoint *) bpt->related_breakpoint;
  11729.       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
  11730.         w = (struct watchpoint *) bpt;
  11731.       else
  11732.         w = NULL;
  11733.       if (w != NULL)
  11734.         watchpoint_del_at_next_stop (w);

  11735.       /* Unlink bpt from the bpt->related_breakpoint ring.  */
  11736.       for (related = bpt; related->related_breakpoint != bpt;
  11737.            related = related->related_breakpoint);
  11738.       related->related_breakpoint = bpt->related_breakpoint;
  11739.       bpt->related_breakpoint = bpt;
  11740.     }

  11741.   /* watch_command_1 creates a watchpoint but only sets its number if
  11742.      update_watchpoint succeeds in creating its bp_locations.  If there's
  11743.      a problem in that process, we'll be asked to delete the half-created
  11744.      watchpoint.  In that case, don't announce the deletion.  */
  11745.   if (bpt->number)
  11746.     observer_notify_breakpoint_deleted (bpt);

  11747.   if (breakpoint_chain == bpt)
  11748.     breakpoint_chain = bpt->next;

  11749.   ALL_BREAKPOINTS (b)
  11750.     if (b->next == bpt)
  11751.     {
  11752.       b->next = bpt->next;
  11753.       break;
  11754.     }

  11755.   /* Be sure no bpstat's are pointing at the breakpoint after it's
  11756.      been freed.  */
  11757.   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
  11758.      in all threads for now.  Note that we cannot just remove bpstats
  11759.      pointing at bpt from the stop_bpstat list entirely, as breakpoint
  11760.      commands are associated with the bpstat; if we remove it here,
  11761.      then the later call to bpstat_do_actions (&stop_bpstat); in
  11762.      event-top.c won't do anything, and temporary breakpoints with
  11763.      commands won't work.  */

  11764.   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);

  11765.   /* Now that breakpoint is removed from breakpoint list, update the
  11766.      global location list.  This will remove locations that used to
  11767.      belong to this breakpoint.  Do this before freeing the breakpoint
  11768.      itself, since remove_breakpoint looks at location's owner.  It
  11769.      might be better design to have location completely
  11770.      self-contained, but it's not the case now.  */
  11771.   update_global_location_list (UGLL_DONT_INSERT);

  11772.   bpt->ops->dtor (bpt);
  11773.   /* On the chance that someone will soon try again to delete this
  11774.      same bp, we mark it as deleted before freeing its storage.  */
  11775.   bpt->type = bp_none;
  11776.   xfree (bpt);
  11777. }

  11778. static void
  11779. do_delete_breakpoint_cleanup (void *b)
  11780. {
  11781.   delete_breakpoint (b);
  11782. }

  11783. struct cleanup *
  11784. make_cleanup_delete_breakpoint (struct breakpoint *b)
  11785. {
  11786.   return make_cleanup (do_delete_breakpoint_cleanup, b);
  11787. }

  11788. /* Iterator function to call a user-provided callback function once
  11789.    for each of B and its related breakpoints.  */

  11790. static void
  11791. iterate_over_related_breakpoints (struct breakpoint *b,
  11792.                                   void (*function) (struct breakpoint *,
  11793.                                                     void *),
  11794.                                   void *data)
  11795. {
  11796.   struct breakpoint *related;

  11797.   related = b;
  11798.   do
  11799.     {
  11800.       struct breakpoint *next;

  11801.       /* FUNCTION may delete RELATED.  */
  11802.       next = related->related_breakpoint;

  11803.       if (next == related)
  11804.         {
  11805.           /* RELATED is the last ring entry.  */
  11806.           function (related, data);

  11807.           /* FUNCTION may have deleted it, so we'd never reach back to
  11808.              B.  There's nothing left to do anyway, so just break
  11809.              out.  */
  11810.           break;
  11811.         }
  11812.       else
  11813.         function (related, data);

  11814.       related = next;
  11815.     }
  11816.   while (related != b);
  11817. }

  11818. static void
  11819. do_delete_breakpoint (struct breakpoint *b, void *ignore)
  11820. {
  11821.   delete_breakpoint (b);
  11822. }

  11823. /* A callback for map_breakpoint_numbers that calls
  11824.    delete_breakpoint.  */

  11825. static void
  11826. do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
  11827. {
  11828.   iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
  11829. }

  11830. void
  11831. delete_command (char *arg, int from_tty)
  11832. {
  11833.   struct breakpoint *b, *b_tmp;

  11834.   dont_repeat ();

  11835.   if (arg == 0)
  11836.     {
  11837.       int breaks_to_delete = 0;

  11838.       /* Delete all breakpoints if no argument.  Do not delete
  11839.          internal breakpoints, these have to be deleted with an
  11840.          explicit breakpoint number argument.  */
  11841.       ALL_BREAKPOINTS (b)
  11842.         if (user_breakpoint_p (b))
  11843.           {
  11844.             breaks_to_delete = 1;
  11845.             break;
  11846.           }

  11847.       /* Ask user only if there are some breakpoints to delete.  */
  11848.       if (!from_tty
  11849.           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
  11850.         {
  11851.           ALL_BREAKPOINTS_SAFE (b, b_tmp)
  11852.             if (user_breakpoint_p (b))
  11853.               delete_breakpoint (b);
  11854.         }
  11855.     }
  11856.   else
  11857.     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
  11858. }

  11859. static int
  11860. all_locations_are_pending (struct bp_location *loc)
  11861. {
  11862.   for (; loc; loc = loc->next)
  11863.     if (!loc->shlib_disabled
  11864.         && !loc->pspace->executing_startup)
  11865.       return 0;
  11866.   return 1;
  11867. }

  11868. /* Subroutine of update_breakpoint_locations to simplify it.
  11869.    Return non-zero if multiple fns in list LOC have the same name.
  11870.    Null names are ignored.  */

  11871. static int
  11872. ambiguous_names_p (struct bp_location *loc)
  11873. {
  11874.   struct bp_location *l;
  11875.   htab_t htab = htab_create_alloc (13, htab_hash_string,
  11876.                                    (int (*) (const void *,
  11877.                                              const void *)) streq,
  11878.                                    NULL, xcalloc, xfree);

  11879.   for (l = loc; l != NULL; l = l->next)
  11880.     {
  11881.       const char **slot;
  11882.       const char *name = l->function_name;

  11883.       /* Allow for some names to be NULL, ignore them.  */
  11884.       if (name == NULL)
  11885.         continue;

  11886.       slot = (const char **) htab_find_slot (htab, (const void *) name,
  11887.                                              INSERT);
  11888.       /* NOTE: We can assume slot != NULL here because xcalloc never
  11889.          returns NULL.  */
  11890.       if (*slot != NULL)
  11891.         {
  11892.           htab_delete (htab);
  11893.           return 1;
  11894.         }
  11895.       *slot = name;
  11896.     }

  11897.   htab_delete (htab);
  11898.   return 0;
  11899. }

  11900. /* When symbols change, it probably means the sources changed as well,
  11901.    and it might mean the static tracepoint markers are no longer at
  11902.    the same address or line numbers they used to be at last we
  11903.    checked.  Losing your static tracepoints whenever you rebuild is
  11904.    undesirable.  This function tries to resync/rematch gdb static
  11905.    tracepoints with the markers on the target, for static tracepoints
  11906.    that have not been set by marker id.  Static tracepoint that have
  11907.    been set by marker id are reset by marker id in breakpoint_re_set.
  11908.    The heuristic is:

  11909.    1) For a tracepoint set at a specific address, look for a marker at
  11910.    the old PC.  If one is found there, assume to be the same marker.
  11911.    If the name / string id of the marker found is different from the
  11912.    previous known name, assume that means the user renamed the marker
  11913.    in the sources, and output a warning.

  11914.    2) For a tracepoint set at a given line number, look for a marker
  11915.    at the new address of the old line number.  If one is found there,
  11916.    assume to be the same marker.  If the name / string id of the
  11917.    marker found is different from the previous known name, assume that
  11918.    means the user renamed the marker in the sources, and output a
  11919.    warning.

  11920.    3) If a marker is no longer found at the same address or line, it
  11921.    may mean the marker no longer exists.  But it may also just mean
  11922.    the code changed a bit.  Maybe the user added a few lines of code
  11923.    that made the marker move up or down (in line number terms).  Ask
  11924.    the target for info about the marker with the string id as we knew
  11925.    it.  If found, update line number and address in the matching
  11926.    static tracepoint.  This will get confused if there's more than one
  11927.    marker with the same ID (possible in UST, although unadvised
  11928.    precisely because it confuses tools).  */

  11929. static struct symtab_and_line
  11930. update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
  11931. {
  11932.   struct tracepoint *tp = (struct tracepoint *) b;
  11933.   struct static_tracepoint_marker marker;
  11934.   CORE_ADDR pc;

  11935.   pc = sal.pc;
  11936.   if (sal.line)
  11937.     find_line_pc (sal.symtab, sal.line, &pc);

  11938.   if (target_static_tracepoint_marker_at (pc, &marker))
  11939.     {
  11940.       if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
  11941.         warning (_("static tracepoint %d changed probed marker from %s to %s"),
  11942.                  b->number,
  11943.                  tp->static_trace_marker_id, marker.str_id);

  11944.       xfree (tp->static_trace_marker_id);
  11945.       tp->static_trace_marker_id = xstrdup (marker.str_id);
  11946.       release_static_tracepoint_marker (&marker);

  11947.       return sal;
  11948.     }

  11949.   /* Old marker wasn't found on target at lineno.  Try looking it up
  11950.      by string ID.  */
  11951.   if (!sal.explicit_pc
  11952.       && sal.line != 0
  11953.       && sal.symtab != NULL
  11954.       && tp->static_trace_marker_id != NULL)
  11955.     {
  11956.       VEC(static_tracepoint_marker_p) *markers;

  11957.       markers
  11958.         = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);

  11959.       if (!VEC_empty(static_tracepoint_marker_p, markers))
  11960.         {
  11961.           struct symtab_and_line sal2;
  11962.           struct symbol *sym;
  11963.           struct static_tracepoint_marker *tpmarker;
  11964.           struct ui_out *uiout = current_uiout;

  11965.           tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);

  11966.           xfree (tp->static_trace_marker_id);
  11967.           tp->static_trace_marker_id = xstrdup (tpmarker->str_id);

  11968.           warning (_("marker for static tracepoint %d (%s) not "
  11969.                      "found at previous line number"),
  11970.                    b->number, tp->static_trace_marker_id);

  11971.           init_sal (&sal2);

  11972.           sal2.pc = tpmarker->address;

  11973.           sal2 = find_pc_line (tpmarker->address, 0);
  11974.           sym = find_pc_sect_function (tpmarker->address, NULL);
  11975.           ui_out_text (uiout, "Now in ");
  11976.           if (sym)
  11977.             {
  11978.               ui_out_field_string (uiout, "func",
  11979.                                    SYMBOL_PRINT_NAME (sym));
  11980.               ui_out_text (uiout, " at ");
  11981.             }
  11982.           ui_out_field_string (uiout, "file",
  11983.                                symtab_to_filename_for_display (sal2.symtab));
  11984.           ui_out_text (uiout, ":");

  11985.           if (ui_out_is_mi_like_p (uiout))
  11986.             {
  11987.               const char *fullname = symtab_to_fullname (sal2.symtab);

  11988.               ui_out_field_string (uiout, "fullname", fullname);
  11989.             }

  11990.           ui_out_field_int (uiout, "line", sal2.line);
  11991.           ui_out_text (uiout, "\n");

  11992.           b->loc->line_number = sal2.line;
  11993.           b->loc->symtab = sym != NULL ? sal2.symtab : NULL;

  11994.           xfree (b->addr_string);
  11995.           b->addr_string = xstrprintf ("%s:%d",
  11996.                                    symtab_to_filename_for_display (sal2.symtab),
  11997.                                        b->loc->line_number);

  11998.           /* Might be nice to check if function changed, and warn if
  11999.              so.  */

  12000.           release_static_tracepoint_marker (tpmarker);
  12001.         }
  12002.     }
  12003.   return sal;
  12004. }

  12005. /* Returns 1 iff locations A and B are sufficiently same that
  12006.    we don't need to report breakpoint as changed.  */

  12007. static int
  12008. locations_are_equal (struct bp_location *a, struct bp_location *b)
  12009. {
  12010.   while (a && b)
  12011.     {
  12012.       if (a->address != b->address)
  12013.         return 0;

  12014.       if (a->shlib_disabled != b->shlib_disabled)
  12015.         return 0;

  12016.       if (a->enabled != b->enabled)
  12017.         return 0;

  12018.       a = a->next;
  12019.       b = b->next;
  12020.     }

  12021.   if ((a == NULL) != (b == NULL))
  12022.     return 0;

  12023.   return 1;
  12024. }

  12025. /* Create new breakpoint locations for B (a hardware or software breakpoint)
  12026.    based on SALS and SALS_END.  If SALS_END.NELTS is not zero, then B is
  12027.    a ranged breakpoint.  */

  12028. void
  12029. update_breakpoint_locations (struct breakpoint *b,
  12030.                              struct symtabs_and_lines sals,
  12031.                              struct symtabs_and_lines sals_end)
  12032. {
  12033.   int i;
  12034.   struct bp_location *existing_locations = b->loc;

  12035.   if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
  12036.     {
  12037.       /* Ranged breakpoints have only one start location and one end
  12038.          location.  */
  12039.       b->enable_state = bp_disabled;
  12040.       update_global_location_list (UGLL_MAY_INSERT);
  12041.       printf_unfiltered (_("Could not reset ranged breakpoint %d: "
  12042.                            "multiple locations found\n"),
  12043.                          b->number);
  12044.       return;
  12045.     }

  12046.   /* If there's no new locations, and all existing locations are
  12047.      pending, don't do anything.  This optimizes the common case where
  12048.      all locations are in the same shared library, that was unloaded.
  12049.      We'd like to retain the location, so that when the library is
  12050.      loaded again, we don't loose the enabled/disabled status of the
  12051.      individual locations.  */
  12052.   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
  12053.     return;

  12054.   b->loc = NULL;

  12055.   for (i = 0; i < sals.nelts; ++i)
  12056.     {
  12057.       struct bp_location *new_loc;

  12058.       switch_to_program_space_and_thread (sals.sals[i].pspace);

  12059.       new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));

  12060.       /* Reparse conditions, they might contain references to the
  12061.          old symtab.  */
  12062.       if (b->cond_string != NULL)
  12063.         {
  12064.           const char *s;
  12065.           volatile struct gdb_exception e;

  12066.           s = b->cond_string;
  12067.           TRY_CATCH (e, RETURN_MASK_ERROR)
  12068.             {
  12069.               new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
  12070.                                            block_for_pc (sals.sals[i].pc),
  12071.                                            0);
  12072.             }
  12073.           if (e.reason < 0)
  12074.             {
  12075.               warning (_("failed to reevaluate condition "
  12076.                          "for breakpoint %d: %s"),
  12077.                        b->number, e.message);
  12078.               new_loc->enabled = 0;
  12079.             }
  12080.         }

  12081.       if (sals_end.nelts)
  12082.         {
  12083.           CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);

  12084.           new_loc->length = end - sals.sals[0].pc + 1;
  12085.         }
  12086.     }

  12087.   /* If possible, carry over 'disable' status from existing
  12088.      breakpoints.  */
  12089.   {
  12090.     struct bp_location *e = existing_locations;
  12091.     /* If there are multiple breakpoints with the same function name,
  12092.        e.g. for inline functions, comparing function names won't work.
  12093.        Instead compare pc addresses; this is just a heuristic as things
  12094.        may have moved, but in practice it gives the correct answer
  12095.        often enough until a better solution is found.  */
  12096.     int have_ambiguous_names = ambiguous_names_p (b->loc);

  12097.     for (; e; e = e->next)
  12098.       {
  12099.         if (!e->enabled && e->function_name)
  12100.           {
  12101.             struct bp_location *l = b->loc;
  12102.             if (have_ambiguous_names)
  12103.               {
  12104.                 for (; l; l = l->next)
  12105.                   if (breakpoint_locations_match (e, l))
  12106.                     {
  12107.                       l->enabled = 0;
  12108.                       break;
  12109.                     }
  12110.               }
  12111.             else
  12112.               {
  12113.                 for (; l; l = l->next)
  12114.                   if (l->function_name
  12115.                       && strcmp (e->function_name, l->function_name) == 0)
  12116.                     {
  12117.                       l->enabled = 0;
  12118.                       break;
  12119.                     }
  12120.               }
  12121.           }
  12122.       }
  12123.   }

  12124.   if (!locations_are_equal (existing_locations, b->loc))
  12125.     observer_notify_breakpoint_modified (b);

  12126.   update_global_location_list (UGLL_MAY_INSERT);
  12127. }

  12128. /* Find the SaL locations corresponding to the given ADDR_STRING.
  12129.    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */

  12130. static struct symtabs_and_lines
  12131. addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
  12132. {
  12133.   char *s;
  12134.   struct symtabs_and_lines sals = {0};
  12135.   volatile struct gdb_exception e;

  12136.   gdb_assert (b->ops != NULL);
  12137.   s = addr_string;

  12138.   TRY_CATCH (e, RETURN_MASK_ERROR)
  12139.     {
  12140.       b->ops->decode_linespec (b, &s, &sals);
  12141.     }
  12142.   if (e.reason < 0)
  12143.     {
  12144.       int not_found_and_ok = 0;
  12145.       /* For pending breakpoints, it's expected that parsing will
  12146.          fail until the right shared library is loaded.  User has
  12147.          already told to create pending breakpoints and don't need
  12148.          extra messages.  If breakpoint is in bp_shlib_disabled
  12149.          state, then user already saw the message about that
  12150.          breakpoint being disabled, and don't want to see more
  12151.          errors.  */
  12152.       if (e.error == NOT_FOUND_ERROR
  12153.           && (b->condition_not_parsed
  12154.               || (b->loc && b->loc->shlib_disabled)
  12155.               || (b->loc && b->loc->pspace->executing_startup)
  12156.               || b->enable_state == bp_disabled))
  12157.         not_found_and_ok = 1;

  12158.       if (!not_found_and_ok)
  12159.         {
  12160.           /* We surely don't want to warn about the same breakpoint
  12161.              10 timesOne solution, implemented here, is disable
  12162.              the breakpoint on error.  Another solution would be to
  12163.              have separate 'warning emitted' flag.  Since this
  12164.              happens only when a binary has changed, I don't know
  12165.              which approach is better.  */
  12166.           b->enable_state = bp_disabled;
  12167.           throw_exception (e);
  12168.         }
  12169.     }

  12170.   if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
  12171.     {
  12172.       int i;

  12173.       for (i = 0; i < sals.nelts; ++i)
  12174.         resolve_sal_pc (&sals.sals[i]);
  12175.       if (b->condition_not_parsed && s && s[0])
  12176.         {
  12177.           char *cond_string, *extra_string;
  12178.           int thread, task;

  12179.           find_condition_and_thread (s, sals.sals[0].pc,
  12180.                                      &cond_string, &thread, &task,
  12181.                                      &extra_string);
  12182.           if (cond_string)
  12183.             b->cond_string = cond_string;
  12184.           b->thread = thread;
  12185.           b->task = task;
  12186.           if (extra_string)
  12187.             b->extra_string = extra_string;
  12188.           b->condition_not_parsed = 0;
  12189.         }

  12190.       if (b->type == bp_static_tracepoint && !strace_marker_p (b))
  12191.         sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);

  12192.       *found = 1;
  12193.     }
  12194.   else
  12195.     *found = 0;

  12196.   return sals;
  12197. }

  12198. /* The default re_set method, for typical hardware or software
  12199.    breakpoints.  Reevaluate the breakpoint and recreate its
  12200.    locations.  */

  12201. static void
  12202. breakpoint_re_set_default (struct breakpoint *b)
  12203. {
  12204.   int found;
  12205.   struct symtabs_and_lines sals, sals_end;
  12206.   struct symtabs_and_lines expanded = {0};
  12207.   struct symtabs_and_lines expanded_end = {0};

  12208.   sals = addr_string_to_sals (b, b->addr_string, &found);
  12209.   if (found)
  12210.     {
  12211.       make_cleanup (xfree, sals.sals);
  12212.       expanded = sals;
  12213.     }

  12214.   if (b->addr_string_range_end)
  12215.     {
  12216.       sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
  12217.       if (found)
  12218.         {
  12219.           make_cleanup (xfree, sals_end.sals);
  12220.           expanded_end = sals_end;
  12221.         }
  12222.     }

  12223.   update_breakpoint_locations (b, expanded, expanded_end);
  12224. }

  12225. /* Default method for creating SALs from an address string.  It basically
  12226.    calls parse_breakpoint_sals.  Return 1 for success, zero for failure.  */

  12227. static void
  12228. create_sals_from_address_default (char **arg,
  12229.                                   struct linespec_result *canonical,
  12230.                                   enum bptype type_wanted,
  12231.                                   char *addr_start, char **copy_arg)
  12232. {
  12233.   parse_breakpoint_sals (arg, canonical);
  12234. }

  12235. /* Call create_breakpoints_sal for the given arguments.  This is the default
  12236.    function for the `create_breakpoints_sal' method of
  12237.    breakpoint_ops.  */

  12238. static void
  12239. create_breakpoints_sal_default (struct gdbarch *gdbarch,
  12240.                                 struct linespec_result *canonical,
  12241.                                 char *cond_string,
  12242.                                 char *extra_string,
  12243.                                 enum bptype type_wanted,
  12244.                                 enum bpdisp disposition,
  12245.                                 int thread,
  12246.                                 int task, int ignore_count,
  12247.                                 const struct breakpoint_ops *ops,
  12248.                                 int from_tty, int enabled,
  12249.                                 int internal, unsigned flags)
  12250. {
  12251.   create_breakpoints_sal (gdbarch, canonical, cond_string,
  12252.                           extra_string,
  12253.                           type_wanted, disposition,
  12254.                           thread, task, ignore_count, ops, from_tty,
  12255.                           enabled, internal, flags);
  12256. }

  12257. /* Decode the line represented by S by calling decode_line_full.  This is the
  12258.    default function for the `decode_linespec' method of breakpoint_ops.  */

  12259. static void
  12260. decode_linespec_default (struct breakpoint *b, char **s,
  12261.                          struct symtabs_and_lines *sals)
  12262. {
  12263.   struct linespec_result canonical;

  12264.   init_linespec_result (&canonical);
  12265.   decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
  12266.                     (struct symtab *) NULL, 0,
  12267.                     &canonical, multiple_symbols_all,
  12268.                     b->filter);

  12269.   /* We should get 0 or 1 resulting SALs.  */
  12270.   gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);

  12271.   if (VEC_length (linespec_sals, canonical.sals) > 0)
  12272.     {
  12273.       struct linespec_sals *lsal;

  12274.       lsal = VEC_index (linespec_sals, canonical.sals, 0);
  12275.       *sals = lsal->sals;
  12276.       /* Arrange it so the destructor does not free the
  12277.          contents.  */
  12278.       lsal->sals.sals = NULL;
  12279.     }

  12280.   destroy_linespec_result (&canonical);
  12281. }

  12282. /* Prepare the global context for a re-set of breakpoint B.  */

  12283. static struct cleanup *
  12284. prepare_re_set_context (struct breakpoint *b)
  12285. {
  12286.   struct cleanup *cleanups;

  12287.   input_radix = b->input_radix;
  12288.   cleanups = save_current_space_and_thread ();
  12289.   if (b->pspace != NULL)
  12290.     switch_to_program_space_and_thread (b->pspace);
  12291.   set_language (b->language);

  12292.   return cleanups;
  12293. }

  12294. /* Reset a breakpoint given it's struct breakpoint * BINT.
  12295.    The value we return ends up being the return value from catch_errors.
  12296.    Unused in this case.  */

  12297. static int
  12298. breakpoint_re_set_one (void *bint)
  12299. {
  12300.   /* Get past catch_errs.  */
  12301.   struct breakpoint *b = (struct breakpoint *) bint;
  12302.   struct cleanup *cleanups;

  12303.   cleanups = prepare_re_set_context (b);
  12304.   b->ops->re_set (b);
  12305.   do_cleanups (cleanups);
  12306.   return 0;
  12307. }

  12308. /* Re-set all breakpoints after symbols have been re-loaded.  */
  12309. void
  12310. breakpoint_re_set (void)
  12311. {
  12312.   struct breakpoint *b, *b_tmp;
  12313.   enum language save_language;
  12314.   int save_input_radix;
  12315.   struct cleanup *old_chain;

  12316.   save_language = current_language->la_language;
  12317.   save_input_radix = input_radix;
  12318.   old_chain = save_current_program_space ();

  12319.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  12320.   {
  12321.     /* Format possible error msg.  */
  12322.     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
  12323.                                 b->number);
  12324.     struct cleanup *cleanups = make_cleanup (xfree, message);
  12325.     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
  12326.     do_cleanups (cleanups);
  12327.   }
  12328.   set_language (save_language);
  12329.   input_radix = save_input_radix;

  12330.   jit_breakpoint_re_set ();

  12331.   do_cleanups (old_chain);

  12332.   create_overlay_event_breakpoint ();
  12333.   create_longjmp_master_breakpoint ();
  12334.   create_std_terminate_master_breakpoint ();
  12335.   create_exception_master_breakpoint ();
  12336. }

  12337. /* Reset the thread number of this breakpoint:

  12338.    - If the breakpoint is for all threads, leave it as-is.
  12339.    - Else, reset it to the current thread for inferior_ptid.  */
  12340. void
  12341. breakpoint_re_set_thread (struct breakpoint *b)
  12342. {
  12343.   if (b->thread != -1)
  12344.     {
  12345.       if (in_thread_list (inferior_ptid))
  12346.         b->thread = pid_to_thread_id (inferior_ptid);

  12347.       /* We're being called after following a fork.  The new fork is
  12348.          selected as current, and unless this was a vfork will have a
  12349.          different program space from the original thread.  Reset that
  12350.          as well.  */
  12351.       b->loc->pspace = current_program_space;
  12352.     }
  12353. }

  12354. /* Set ignore-count of breakpoint number BPTNUM to COUNT.
  12355.    If from_tty is nonzero, it prints a message to that effect,
  12356.    which ends with a period (no newline).  */

  12357. void
  12358. set_ignore_count (int bptnum, int count, int from_tty)
  12359. {
  12360.   struct breakpoint *b;

  12361.   if (count < 0)
  12362.     count = 0;

  12363.   ALL_BREAKPOINTS (b)
  12364.     if (b->number == bptnum)
  12365.     {
  12366.       if (is_tracepoint (b))
  12367.         {
  12368.           if (from_tty && count != 0)
  12369.             printf_filtered (_("Ignore count ignored for tracepoint %d."),
  12370.                              bptnum);
  12371.           return;
  12372.         }

  12373.       b->ignore_count = count;
  12374.       if (from_tty)
  12375.         {
  12376.           if (count == 0)
  12377.             printf_filtered (_("Will stop next time "
  12378.                                "breakpoint %d is reached."),
  12379.                              bptnum);
  12380.           else if (count == 1)
  12381.             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
  12382.                              bptnum);
  12383.           else
  12384.             printf_filtered (_("Will ignore next %d "
  12385.                                "crossings of breakpoint %d."),
  12386.                              count, bptnum);
  12387.         }
  12388.       observer_notify_breakpoint_modified (b);
  12389.       return;
  12390.     }

  12391.   error (_("No breakpoint number %d."), bptnum);
  12392. }

  12393. /* Command to set ignore-count of breakpoint N to COUNT.  */

  12394. static void
  12395. ignore_command (char *args, int from_tty)
  12396. {
  12397.   char *p = args;
  12398.   int num;

  12399.   if (p == 0)
  12400.     error_no_arg (_("a breakpoint number"));

  12401.   num = get_number (&p);
  12402.   if (num == 0)
  12403.     error (_("bad breakpoint number: '%s'"), args);
  12404.   if (*p == 0)
  12405.     error (_("Second argument (specified ignore-count) is missing."));

  12406.   set_ignore_count (num,
  12407.                     longest_to_int (value_as_long (parse_and_eval (p))),
  12408.                     from_tty);
  12409.   if (from_tty)
  12410.     printf_filtered ("\n");
  12411. }

  12412. /* Call FUNCTION on each of the breakpoints
  12413.    whose numbers are given in ARGS.  */

  12414. static void
  12415. map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
  12416.                                                       void *),
  12417.                         void *data)
  12418. {
  12419.   int num;
  12420.   struct breakpoint *b, *tmp;
  12421.   int match;
  12422.   struct get_number_or_range_state state;

  12423.   if (args == 0)
  12424.     error_no_arg (_("one or more breakpoint numbers"));

  12425.   init_number_or_range (&state, args);

  12426.   while (!state.finished)
  12427.     {
  12428.       const char *p = state.string;

  12429.       match = 0;

  12430.       num = get_number_or_range (&state);
  12431.       if (num == 0)
  12432.         {
  12433.           warning (_("bad breakpoint number at or near '%s'"), p);
  12434.         }
  12435.       else
  12436.         {
  12437.           ALL_BREAKPOINTS_SAFE (b, tmp)
  12438.             if (b->number == num)
  12439.               {
  12440.                 match = 1;
  12441.                 function (b, data);
  12442.                 break;
  12443.               }
  12444.           if (match == 0)
  12445.             printf_unfiltered (_("No breakpoint number %d.\n"), num);
  12446.         }
  12447.     }
  12448. }

  12449. static struct bp_location *
  12450. find_location_by_number (char *number)
  12451. {
  12452.   char *dot = strchr (number, '.');
  12453.   char *p1;
  12454.   int bp_num;
  12455.   int loc_num;
  12456.   struct breakpoint *b;
  12457.   struct bp_location *loc;

  12458.   *dot = '\0';

  12459.   p1 = number;
  12460.   bp_num = get_number (&p1);
  12461.   if (bp_num == 0)
  12462.     error (_("Bad breakpoint number '%s'"), number);

  12463.   ALL_BREAKPOINTS (b)
  12464.     if (b->number == bp_num)
  12465.       {
  12466.         break;
  12467.       }

  12468.   if (!b || b->number != bp_num)
  12469.     error (_("Bad breakpoint number '%s'"), number);

  12470.   p1 = dot+1;
  12471.   loc_num = get_number (&p1);
  12472.   if (loc_num == 0)
  12473.     error (_("Bad breakpoint location number '%s'"), number);

  12474.   --loc_num;
  12475.   loc = b->loc;
  12476.   for (;loc_num && loc; --loc_num, loc = loc->next)
  12477.     ;
  12478.   if (!loc)
  12479.     error (_("Bad breakpoint location number '%s'"), dot+1);

  12480.   return loc;
  12481. }


  12482. /* Set ignore-count of breakpoint number BPTNUM to COUNT.
  12483.    If from_tty is nonzero, it prints a message to that effect,
  12484.    which ends with a period (no newline).  */

  12485. void
  12486. disable_breakpoint (struct breakpoint *bpt)
  12487. {
  12488.   /* Never disable a watchpoint scope breakpoint; we want to
  12489.      hit them when we leave scope so we can delete both the
  12490.      watchpoint and its scope breakpoint at that time.  */
  12491.   if (bpt->type == bp_watchpoint_scope)
  12492.     return;

  12493.   bpt->enable_state = bp_disabled;

  12494.   /* Mark breakpoint locations modified.  */
  12495.   mark_breakpoint_modified (bpt);

  12496.   if (target_supports_enable_disable_tracepoint ()
  12497.       && current_trace_status ()->running && is_tracepoint (bpt))
  12498.     {
  12499.       struct bp_location *location;

  12500.       for (location = bpt->loc; location; location = location->next)
  12501.         target_disable_tracepoint (location);
  12502.     }

  12503.   update_global_location_list (UGLL_DONT_INSERT);

  12504.   observer_notify_breakpoint_modified (bpt);
  12505. }

  12506. /* A callback for iterate_over_related_breakpoints.  */

  12507. static void
  12508. do_disable_breakpoint (struct breakpoint *b, void *ignore)
  12509. {
  12510.   disable_breakpoint (b);
  12511. }

  12512. /* A callback for map_breakpoint_numbers that calls
  12513.    disable_breakpoint.  */

  12514. static void
  12515. do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
  12516. {
  12517.   iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
  12518. }

  12519. static void
  12520. disable_command (char *args, int from_tty)
  12521. {
  12522.   if (args == 0)
  12523.     {
  12524.       struct breakpoint *bpt;

  12525.       ALL_BREAKPOINTS (bpt)
  12526.         if (user_breakpoint_p (bpt))
  12527.           disable_breakpoint (bpt);
  12528.     }
  12529.   else
  12530.     {
  12531.       char *num = extract_arg (&args);

  12532.       while (num)
  12533.         {
  12534.           if (strchr (num, '.'))
  12535.             {
  12536.               struct bp_location *loc = find_location_by_number (num);

  12537.               if (loc)
  12538.                 {
  12539.                   if (loc->enabled)
  12540.                     {
  12541.                       loc->enabled = 0;
  12542.                       mark_breakpoint_location_modified (loc);
  12543.                     }
  12544.                   if (target_supports_enable_disable_tracepoint ()
  12545.                       && current_trace_status ()->running && loc->owner
  12546.                       && is_tracepoint (loc->owner))
  12547.                     target_disable_tracepoint (loc);
  12548.                 }
  12549.               update_global_location_list (UGLL_DONT_INSERT);
  12550.             }
  12551.           else
  12552.             map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
  12553.           num = extract_arg (&args);
  12554.         }
  12555.     }
  12556. }

  12557. static void
  12558. enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
  12559.                         int count)
  12560. {
  12561.   int target_resources_ok;

  12562.   if (bpt->type == bp_hardware_breakpoint)
  12563.     {
  12564.       int i;
  12565.       i = hw_breakpoint_used_count ();
  12566.       target_resources_ok =
  12567.         target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
  12568.                                             i + 1, 0);
  12569.       if (target_resources_ok == 0)
  12570.         error (_("No hardware breakpoint support in the target."));
  12571.       else if (target_resources_ok < 0)
  12572.         error (_("Hardware breakpoints used exceeds limit."));
  12573.     }

  12574.   if (is_watchpoint (bpt))
  12575.     {
  12576.       /* Initialize it just to avoid a GCC false warning.  */
  12577.       enum enable_state orig_enable_state = 0;
  12578.       volatile struct gdb_exception e;

  12579.       TRY_CATCH (e, RETURN_MASK_ALL)
  12580.         {
  12581.           struct watchpoint *w = (struct watchpoint *) bpt;

  12582.           orig_enable_state = bpt->enable_state;
  12583.           bpt->enable_state = bp_enabled;
  12584.           update_watchpoint (w, 1 /* reparse */);
  12585.         }
  12586.       if (e.reason < 0)
  12587.         {
  12588.           bpt->enable_state = orig_enable_state;
  12589.           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
  12590.                              bpt->number);
  12591.           return;
  12592.         }
  12593.     }

  12594.   bpt->enable_state = bp_enabled;

  12595.   /* Mark breakpoint locations modified.  */
  12596.   mark_breakpoint_modified (bpt);

  12597.   if (target_supports_enable_disable_tracepoint ()
  12598.       && current_trace_status ()->running && is_tracepoint (bpt))
  12599.     {
  12600.       struct bp_location *location;

  12601.       for (location = bpt->loc; location; location = location->next)
  12602.         target_enable_tracepoint (location);
  12603.     }

  12604.   bpt->disposition = disposition;
  12605.   bpt->enable_count = count;
  12606.   update_global_location_list (UGLL_MAY_INSERT);

  12607.   observer_notify_breakpoint_modified (bpt);
  12608. }


  12609. void
  12610. enable_breakpoint (struct breakpoint *bpt)
  12611. {
  12612.   enable_breakpoint_disp (bpt, bpt->disposition, 0);
  12613. }

  12614. static void
  12615. do_enable_breakpoint (struct breakpoint *bpt, void *arg)
  12616. {
  12617.   enable_breakpoint (bpt);
  12618. }

  12619. /* A callback for map_breakpoint_numbers that calls
  12620.    enable_breakpoint.  */

  12621. static void
  12622. do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
  12623. {
  12624.   iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
  12625. }

  12626. /* The enable command enables the specified breakpoints (or all defined
  12627.    breakpoints) so they once again become (or continue to be) effective
  12628.    in stopping the inferior.  */

  12629. static void
  12630. enable_command (char *args, int from_tty)
  12631. {
  12632.   if (args == 0)
  12633.     {
  12634.       struct breakpoint *bpt;

  12635.       ALL_BREAKPOINTS (bpt)
  12636.         if (user_breakpoint_p (bpt))
  12637.           enable_breakpoint (bpt);
  12638.     }
  12639.   else
  12640.     {
  12641.       char *num = extract_arg (&args);

  12642.       while (num)
  12643.         {
  12644.           if (strchr (num, '.'))
  12645.             {
  12646.               struct bp_location *loc = find_location_by_number (num);

  12647.               if (loc)
  12648.                 {
  12649.                   if (!loc->enabled)
  12650.                     {
  12651.                       loc->enabled = 1;
  12652.                       mark_breakpoint_location_modified (loc);
  12653.                     }
  12654.                   if (target_supports_enable_disable_tracepoint ()
  12655.                       && current_trace_status ()->running && loc->owner
  12656.                       && is_tracepoint (loc->owner))
  12657.                     target_enable_tracepoint (loc);
  12658.                 }
  12659.               update_global_location_list (UGLL_MAY_INSERT);
  12660.             }
  12661.           else
  12662.             map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
  12663.           num = extract_arg (&args);
  12664.         }
  12665.     }
  12666. }

  12667. /* This struct packages up disposition data for application to multiple
  12668.    breakpoints.  */

  12669. struct disp_data
  12670. {
  12671.   enum bpdisp disp;
  12672.   int count;
  12673. };

  12674. static void
  12675. do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
  12676. {
  12677.   struct disp_data disp_data = *(struct disp_data *) arg;

  12678.   enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
  12679. }

  12680. static void
  12681. do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
  12682. {
  12683.   struct disp_data disp = { disp_disable, 1 };

  12684.   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
  12685. }

  12686. static void
  12687. enable_once_command (char *args, int from_tty)
  12688. {
  12689.   map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
  12690. }

  12691. static void
  12692. do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
  12693. {
  12694.   struct disp_data disp = { disp_disable, *(int *) countptr };

  12695.   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
  12696. }

  12697. static void
  12698. enable_count_command (char *args, int from_tty)
  12699. {
  12700.   int count = get_number (&args);

  12701.   map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
  12702. }

  12703. static void
  12704. do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
  12705. {
  12706.   struct disp_data disp = { disp_del, 1 };

  12707.   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
  12708. }

  12709. static void
  12710. enable_delete_command (char *args, int from_tty)
  12711. {
  12712.   map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
  12713. }

  12714. static void
  12715. set_breakpoint_cmd (char *args, int from_tty)
  12716. {
  12717. }

  12718. static void
  12719. show_breakpoint_cmd (char *args, int from_tty)
  12720. {
  12721. }

  12722. /* Invalidate last known value of any hardware watchpoint if
  12723.    the memory which that value represents has been written to by
  12724.    GDB itself.  */

  12725. static void
  12726. invalidate_bp_value_on_memory_change (struct inferior *inferior,
  12727.                                       CORE_ADDR addr, ssize_t len,
  12728.                                       const bfd_byte *data)
  12729. {
  12730.   struct breakpoint *bp;

  12731.   ALL_BREAKPOINTS (bp)
  12732.     if (bp->enable_state == bp_enabled
  12733.         && bp->type == bp_hardware_watchpoint)
  12734.       {
  12735.         struct watchpoint *wp = (struct watchpoint *) bp;

  12736.         if (wp->val_valid && wp->val)
  12737.           {
  12738.             struct bp_location *loc;

  12739.             for (loc = bp->loc; loc != NULL; loc = loc->next)
  12740.               if (loc->loc_type == bp_loc_hardware_watchpoint
  12741.                   && loc->address + loc->length > addr
  12742.                   && addr + len > loc->address)
  12743.                 {
  12744.                   value_free (wp->val);
  12745.                   wp->val = NULL;
  12746.                   wp->val_valid = 0;
  12747.                 }
  12748.           }
  12749.       }
  12750. }

  12751. /* Create and insert a breakpoint for software single step.  */

  12752. void
  12753. insert_single_step_breakpoint (struct gdbarch *gdbarch,
  12754.                                struct address_space *aspace,
  12755.                                CORE_ADDR next_pc)
  12756. {
  12757.   struct thread_info *tp = inferior_thread ();
  12758.   struct symtab_and_line sal;
  12759.   CORE_ADDR pc = next_pc;

  12760.   if (tp->control.single_step_breakpoints == NULL)
  12761.     {
  12762.       tp->control.single_step_breakpoints
  12763.         = new_single_step_breakpoint (tp->num, gdbarch);
  12764.     }

  12765.   sal = find_pc_line (pc, 0);
  12766.   sal.pc = pc;
  12767.   sal.section = find_pc_overlay (pc);
  12768.   sal.explicit_pc = 1;
  12769.   add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);

  12770.   update_global_location_list (UGLL_INSERT);
  12771. }

  12772. /* See breakpoint.h.  */

  12773. int
  12774. breakpoint_has_location_inserted_here (struct breakpoint *bp,
  12775.                                        struct address_space *aspace,
  12776.                                        CORE_ADDR pc)
  12777. {
  12778.   struct bp_location *loc;

  12779.   for (loc = bp->loc; loc != NULL; loc = loc->next)
  12780.     if (loc->inserted
  12781.         && breakpoint_location_address_match (loc, aspace, pc))
  12782.       return 1;

  12783.   return 0;
  12784. }

  12785. /* Check whether a software single-step breakpoint is inserted at
  12786.    PC.  */

  12787. int
  12788. single_step_breakpoint_inserted_here_p (struct address_space *aspace,
  12789.                                         CORE_ADDR pc)
  12790. {
  12791.   struct breakpoint *bpt;

  12792.   ALL_BREAKPOINTS (bpt)
  12793.     {
  12794.       if (bpt->type == bp_single_step
  12795.           && breakpoint_has_location_inserted_here (bpt, aspace, pc))
  12796.         return 1;
  12797.     }
  12798.   return 0;
  12799. }

  12800. /* Returns 0 if 'bp' is NOT a syscall catchpoint,
  12801.    non-zero otherwise.  */
  12802. static int
  12803. is_syscall_catchpoint_enabled (struct breakpoint *bp)
  12804. {
  12805.   if (syscall_catchpoint_p (bp)
  12806.       && bp->enable_state != bp_disabled
  12807.       && bp->enable_state != bp_call_disabled)
  12808.     return 1;
  12809.   else
  12810.     return 0;
  12811. }

  12812. int
  12813. catch_syscall_enabled (void)
  12814. {
  12815.   struct catch_syscall_inferior_data *inf_data
  12816.     = get_catch_syscall_inferior_data (current_inferior ());

  12817.   return inf_data->total_syscalls_count != 0;
  12818. }

  12819. int
  12820. catching_syscall_number (int syscall_number)
  12821. {
  12822.   struct breakpoint *bp;

  12823.   ALL_BREAKPOINTS (bp)
  12824.     if (is_syscall_catchpoint_enabled (bp))
  12825.       {
  12826.         struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;

  12827.         if (c->syscalls_to_be_caught)
  12828.           {
  12829.             int i, iter;
  12830.             for (i = 0;
  12831.                  VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
  12832.                  i++)
  12833.               if (syscall_number == iter)
  12834.                 return 1;
  12835.           }
  12836.         else
  12837.           return 1;
  12838.       }

  12839.   return 0;
  12840. }

  12841. /* Complete syscall names.  Used by "catch syscall".  */
  12842. static VEC (char_ptr) *
  12843. catch_syscall_completer (struct cmd_list_element *cmd,
  12844.                          const char *text, const char *word)
  12845. {
  12846.   const char **list = get_syscall_names (get_current_arch ());
  12847.   VEC (char_ptr) *retlist
  12848.     = (list == NULL) ? NULL : complete_on_enum (list, word, word);

  12849.   xfree (list);
  12850.   return retlist;
  12851. }

  12852. /* Tracepoint-specific operations.  */

  12853. /* Set tracepoint count to NUM.  */
  12854. static void
  12855. set_tracepoint_count (int num)
  12856. {
  12857.   tracepoint_count = num;
  12858.   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
  12859. }

  12860. static void
  12861. trace_command (char *arg, int from_tty)
  12862. {
  12863.   struct breakpoint_ops *ops;
  12864.   const char *arg_cp = arg;

  12865.   if (arg && probe_linespec_to_ops (&arg_cp))
  12866.     ops = &tracepoint_probe_breakpoint_ops;
  12867.   else
  12868.     ops = &tracepoint_breakpoint_ops;

  12869.   create_breakpoint (get_current_arch (),
  12870.                      arg,
  12871.                      NULL, 0, NULL, 1 /* parse arg */,
  12872.                      0 /* tempflag */,
  12873.                      bp_tracepoint /* type_wanted */,
  12874.                      0 /* Ignore count */,
  12875.                      pending_break_support,
  12876.                      ops,
  12877.                      from_tty,
  12878.                      1 /* enabled */,
  12879.                      0 /* internal */, 0);
  12880. }

  12881. static void
  12882. ftrace_command (char *arg, int from_tty)
  12883. {
  12884.   create_breakpoint (get_current_arch (),
  12885.                      arg,
  12886.                      NULL, 0, NULL, 1 /* parse arg */,
  12887.                      0 /* tempflag */,
  12888.                      bp_fast_tracepoint /* type_wanted */,
  12889.                      0 /* Ignore count */,
  12890.                      pending_break_support,
  12891.                      &tracepoint_breakpoint_ops,
  12892.                      from_tty,
  12893.                      1 /* enabled */,
  12894.                      0 /* internal */, 0);
  12895. }

  12896. /* strace command implementation.  Creates a static tracepoint.  */

  12897. static void
  12898. strace_command (char *arg, int from_tty)
  12899. {
  12900.   struct breakpoint_ops *ops;

  12901.   /* Decide if we are dealing with a static tracepoint marker (`-m'),
  12902.      or with a normal static tracepoint.  */
  12903.   if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
  12904.     ops = &strace_marker_breakpoint_ops;
  12905.   else
  12906.     ops = &tracepoint_breakpoint_ops;

  12907.   create_breakpoint (get_current_arch (),
  12908.                      arg,
  12909.                      NULL, 0, NULL, 1 /* parse arg */,
  12910.                      0 /* tempflag */,
  12911.                      bp_static_tracepoint /* type_wanted */,
  12912.                      0 /* Ignore count */,
  12913.                      pending_break_support,
  12914.                      ops,
  12915.                      from_tty,
  12916.                      1 /* enabled */,
  12917.                      0 /* internal */, 0);
  12918. }

  12919. /* Set up a fake reader function that gets command lines from a linked
  12920.    list that was acquired during tracepoint uploading.  */

  12921. static struct uploaded_tp *this_utp;
  12922. static int next_cmd;

  12923. static char *
  12924. read_uploaded_action (void)
  12925. {
  12926.   char *rslt;

  12927.   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);

  12928.   next_cmd++;

  12929.   return rslt;
  12930. }

  12931. /* Given information about a tracepoint as recorded on a target (which
  12932.    can be either a live system or a trace file), attempt to create an
  12933.    equivalent GDB tracepoint.  This is not a reliable process, since
  12934.    the target does not necessarily have all the information used when
  12935.    the tracepoint was originally defined.  */

  12936. struct tracepoint *
  12937. create_tracepoint_from_upload (struct uploaded_tp *utp)
  12938. {
  12939.   char *addr_str, small_buf[100];
  12940.   struct tracepoint *tp;

  12941.   if (utp->at_string)
  12942.     addr_str = utp->at_string;
  12943.   else
  12944.     {
  12945.       /* In the absence of a source location, fall back to raw
  12946.          address.  Since there is no way to confirm that the address
  12947.          means the same thing as when the trace was started, warn the
  12948.          user.  */
  12949.       warning (_("Uploaded tracepoint %d has no "
  12950.                  "source location, using raw address"),
  12951.                utp->number);
  12952.       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
  12953.       addr_str = small_buf;
  12954.     }

  12955.   /* There's not much we can do with a sequence of bytecodes.  */
  12956.   if (utp->cond && !utp->cond_string)
  12957.     warning (_("Uploaded tracepoint %d condition "
  12958.                "has no source form, ignoring it"),
  12959.              utp->number);

  12960.   if (!create_breakpoint (get_current_arch (),
  12961.                           addr_str,
  12962.                           utp->cond_string, -1, NULL,
  12963.                           0 /* parse cond/thread */,
  12964.                           0 /* tempflag */,
  12965.                           utp->type /* type_wanted */,
  12966.                           0 /* Ignore count */,
  12967.                           pending_break_support,
  12968.                           &tracepoint_breakpoint_ops,
  12969.                           0 /* from_tty */,
  12970.                           utp->enabled /* enabled */,
  12971.                           0 /* internal */,
  12972.                           CREATE_BREAKPOINT_FLAGS_INSERTED))
  12973.     return NULL;

  12974.   /* Get the tracepoint we just created.  */
  12975.   tp = get_tracepoint (tracepoint_count);
  12976.   gdb_assert (tp != NULL);

  12977.   if (utp->pass > 0)
  12978.     {
  12979.       xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
  12980.                  tp->base.number);

  12981.       trace_pass_command (small_buf, 0);
  12982.     }

  12983.   /* If we have uploaded versions of the original commands, set up a
  12984.      special-purpose "reader" function and call the usual command line
  12985.      reader, then pass the result to the breakpoint command-setting
  12986.      function.  */
  12987.   if (!VEC_empty (char_ptr, utp->cmd_strings))
  12988.     {
  12989.       struct command_line *cmd_list;

  12990.       this_utp = utp;
  12991.       next_cmd = 0;

  12992.       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);

  12993.       breakpoint_set_commands (&tp->base, cmd_list);
  12994.     }
  12995.   else if (!VEC_empty (char_ptr, utp->actions)
  12996.            || !VEC_empty (char_ptr, utp->step_actions))
  12997.     warning (_("Uploaded tracepoint %d actions "
  12998.                "have no source form, ignoring them"),
  12999.              utp->number);

  13000.   /* Copy any status information that might be available.  */
  13001.   tp->base.hit_count = utp->hit_count;
  13002.   tp->traceframe_usage = utp->traceframe_usage;

  13003.   return tp;
  13004. }

  13005. /* Print information on tracepoint number TPNUM_EXP, or all if
  13006.    omitted.  */

  13007. static void
  13008. tracepoints_info (char *args, int from_tty)
  13009. {
  13010.   struct ui_out *uiout = current_uiout;
  13011.   int num_printed;

  13012.   num_printed = breakpoint_1 (args, 0, is_tracepoint);

  13013.   if (num_printed == 0)
  13014.     {
  13015.       if (args == NULL || *args == '\0')
  13016.         ui_out_message (uiout, 0, "No tracepoints.\n");
  13017.       else
  13018.         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
  13019.     }

  13020.   default_collect_info ();
  13021. }

  13022. /* The 'enable trace' command enables tracepoints.
  13023.    Not supported by all targets.  */
  13024. static void
  13025. enable_trace_command (char *args, int from_tty)
  13026. {
  13027.   enable_command (args, from_tty);
  13028. }

  13029. /* The 'disable trace' command disables tracepoints.
  13030.    Not supported by all targets.  */
  13031. static void
  13032. disable_trace_command (char *args, int from_tty)
  13033. {
  13034.   disable_command (args, from_tty);
  13035. }

  13036. /* Remove a tracepoint (or all if no argument).  */
  13037. static void
  13038. delete_trace_command (char *arg, int from_tty)
  13039. {
  13040.   struct breakpoint *b, *b_tmp;

  13041.   dont_repeat ();

  13042.   if (arg == 0)
  13043.     {
  13044.       int breaks_to_delete = 0;

  13045.       /* Delete all breakpoints if no argument.
  13046.          Do not delete internal or call-dummy breakpoints, these
  13047.          have to be deleted with an explicit breakpoint number
  13048.          argument.  */
  13049.       ALL_TRACEPOINTS (b)
  13050.         if (is_tracepoint (b) && user_breakpoint_p (b))
  13051.           {
  13052.             breaks_to_delete = 1;
  13053.             break;
  13054.           }

  13055.       /* Ask user only if there are some breakpoints to delete.  */
  13056.       if (!from_tty
  13057.           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
  13058.         {
  13059.           ALL_BREAKPOINTS_SAFE (b, b_tmp)
  13060.             if (is_tracepoint (b) && user_breakpoint_p (b))
  13061.               delete_breakpoint (b);
  13062.         }
  13063.     }
  13064.   else
  13065.     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
  13066. }

  13067. /* Helper function for trace_pass_command.  */

  13068. static void
  13069. trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
  13070. {
  13071.   tp->pass_count = count;
  13072.   observer_notify_breakpoint_modified (&tp->base);
  13073.   if (from_tty)
  13074.     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
  13075.                      tp->base.number, count);
  13076. }

  13077. /* Set passcount for tracepoint.

  13078.    First command argument is passcount, second is tracepoint number.
  13079.    If tracepoint number omitted, apply to most recently defined.
  13080.    Also accepts special argument "all".  */

  13081. static void
  13082. trace_pass_command (char *args, int from_tty)
  13083. {
  13084.   struct tracepoint *t1;
  13085.   unsigned int count;

  13086.   if (args == 0 || *args == 0)
  13087.     error (_("passcount command requires an "
  13088.              "argument (count + optional TP num)"));

  13089.   count = strtoul (args, &args, 10);        /* Count comes first, then TP num.  */

  13090.   args = skip_spaces (args);
  13091.   if (*args && strncasecmp (args, "all", 3) == 0)
  13092.     {
  13093.       struct breakpoint *b;

  13094.       args += 3;                        /* Skip special argument "all".  */
  13095.       if (*args)
  13096.         error (_("Junk at end of arguments."));

  13097.       ALL_TRACEPOINTS (b)
  13098.       {
  13099.         t1 = (struct tracepoint *) b;
  13100.         trace_pass_set_count (t1, count, from_tty);
  13101.       }
  13102.     }
  13103.   else if (*args == '\0')
  13104.     {
  13105.       t1 = get_tracepoint_by_number (&args, NULL);
  13106.       if (t1)
  13107.         trace_pass_set_count (t1, count, from_tty);
  13108.     }
  13109.   else
  13110.     {
  13111.       struct get_number_or_range_state state;

  13112.       init_number_or_range (&state, args);
  13113.       while (!state.finished)
  13114.         {
  13115.           t1 = get_tracepoint_by_number (&args, &state);
  13116.           if (t1)
  13117.             trace_pass_set_count (t1, count, from_tty);
  13118.         }
  13119.     }
  13120. }

  13121. struct tracepoint *
  13122. get_tracepoint (int num)
  13123. {
  13124.   struct breakpoint *t;

  13125.   ALL_TRACEPOINTS (t)
  13126.     if (t->number == num)
  13127.       return (struct tracepoint *) t;

  13128.   return NULL;
  13129. }

  13130. /* Find the tracepoint with the given target-side number (which may be
  13131.    different from the tracepoint number after disconnecting and
  13132.    reconnecting).  */

  13133. struct tracepoint *
  13134. get_tracepoint_by_number_on_target (int num)
  13135. {
  13136.   struct breakpoint *b;

  13137.   ALL_TRACEPOINTS (b)
  13138.     {
  13139.       struct tracepoint *t = (struct tracepoint *) b;

  13140.       if (t->number_on_target == num)
  13141.         return t;
  13142.     }

  13143.   return NULL;
  13144. }

  13145. /* Utility: parse a tracepoint number and look it up in the list.
  13146.    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
  13147.    If the argument is missing, the most recent tracepoint
  13148.    (tracepoint_count) is returned.  */

  13149. struct tracepoint *
  13150. get_tracepoint_by_number (char **arg,
  13151.                           struct get_number_or_range_state *state)
  13152. {
  13153.   struct breakpoint *t;
  13154.   int tpnum;
  13155.   char *instring = arg == NULL ? NULL : *arg;

  13156.   if (state)
  13157.     {
  13158.       gdb_assert (!state->finished);
  13159.       tpnum = get_number_or_range (state);
  13160.     }
  13161.   else if (arg == NULL || *arg == NULL || ! **arg)
  13162.     tpnum = tracepoint_count;
  13163.   else
  13164.     tpnum = get_number (arg);

  13165.   if (tpnum <= 0)
  13166.     {
  13167.       if (instring && *instring)
  13168.         printf_filtered (_("bad tracepoint number at or near '%s'\n"),
  13169.                          instring);
  13170.       else
  13171.         printf_filtered (_("No previous tracepoint\n"));
  13172.       return NULL;
  13173.     }

  13174.   ALL_TRACEPOINTS (t)
  13175.     if (t->number == tpnum)
  13176.     {
  13177.       return (struct tracepoint *) t;
  13178.     }

  13179.   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
  13180.   return NULL;
  13181. }

  13182. void
  13183. print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
  13184. {
  13185.   if (b->thread != -1)
  13186.     fprintf_unfiltered (fp, " thread %d", b->thread);

  13187.   if (b->task != 0)
  13188.     fprintf_unfiltered (fp, " task %d", b->task);

  13189.   fprintf_unfiltered (fp, "\n");
  13190. }

  13191. /* Save information on user settable breakpoints (watchpoints, etc) to
  13192.    a new script file named FILENAME.  If FILTER is non-NULL, call it
  13193.    on each breakpoint and only include the ones for which it returns
  13194.    non-zero.  */

  13195. static void
  13196. save_breakpoints (char *filename, int from_tty,
  13197.                   int (*filter) (const struct breakpoint *))
  13198. {
  13199.   struct breakpoint *tp;
  13200.   int any = 0;
  13201.   struct cleanup *cleanup;
  13202.   struct ui_file *fp;
  13203.   int extra_trace_bits = 0;

  13204.   if (filename == 0 || *filename == 0)
  13205.     error (_("Argument required (file name in which to save)"));

  13206.   /* See if we have anything to save.  */
  13207.   ALL_BREAKPOINTS (tp)
  13208.   {
  13209.     /* Skip internal and momentary breakpoints.  */
  13210.     if (!user_breakpoint_p (tp))
  13211.       continue;

  13212.     /* If we have a filter, only save the breakpoints it accepts.  */
  13213.     if (filter && !filter (tp))
  13214.       continue;

  13215.     any = 1;

  13216.     if (is_tracepoint (tp))
  13217.       {
  13218.         extra_trace_bits = 1;

  13219.         /* We can stop searching.  */
  13220.         break;
  13221.       }
  13222.   }

  13223.   if (!any)
  13224.     {
  13225.       warning (_("Nothing to save."));
  13226.       return;
  13227.     }

  13228.   filename = tilde_expand (filename);
  13229.   cleanup = make_cleanup (xfree, filename);
  13230.   fp = gdb_fopen (filename, "w");
  13231.   if (!fp)
  13232.     error (_("Unable to open file '%s' for saving (%s)"),
  13233.            filename, safe_strerror (errno));
  13234.   make_cleanup_ui_file_delete (fp);

  13235.   if (extra_trace_bits)
  13236.     save_trace_state_variables (fp);

  13237.   ALL_BREAKPOINTS (tp)
  13238.   {
  13239.     /* Skip internal and momentary breakpoints.  */
  13240.     if (!user_breakpoint_p (tp))
  13241.       continue;

  13242.     /* If we have a filter, only save the breakpoints it accepts.  */
  13243.     if (filter && !filter (tp))
  13244.       continue;

  13245.     tp->ops->print_recreate (tp, fp);

  13246.     /* Note, we can't rely on tp->number for anything, as we can't
  13247.        assume the recreated breakpoint numbers will match.  Use $bpnum
  13248.        instead.  */

  13249.     if (tp->cond_string)
  13250.       fprintf_unfiltered (fp, condition $bpnum %s\n", tp->cond_string);

  13251.     if (tp->ignore_count)
  13252.       fprintf_unfiltered (fp, ignore $bpnum %d\n", tp->ignore_count);

  13253.     if (tp->type != bp_dprintf && tp->commands)
  13254.       {
  13255.         volatile struct gdb_exception ex;

  13256.         fprintf_unfiltered (fp, "  commands\n");

  13257.         ui_out_redirect (current_uiout, fp);
  13258.         TRY_CATCH (ex, RETURN_MASK_ALL)
  13259.           {
  13260.             print_command_lines (current_uiout, tp->commands->commands, 2);
  13261.           }
  13262.         ui_out_redirect (current_uiout, NULL);

  13263.         if (ex.reason < 0)
  13264.           throw_exception (ex);

  13265.         fprintf_unfiltered (fp, end\n");
  13266.       }

  13267.     if (tp->enable_state == bp_disabled)
  13268.       fprintf_unfiltered (fp, "disable $bpnum\n");

  13269.     /* If this is a multi-location breakpoint, check if the locations
  13270.        should be individually disabled.  Watchpoint locations are
  13271.        special, and not user visible.  */
  13272.     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
  13273.       {
  13274.         struct bp_location *loc;
  13275.         int n = 1;

  13276.         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
  13277.           if (!loc->enabled)
  13278.             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
  13279.       }
  13280.   }

  13281.   if (extra_trace_bits && *default_collect)
  13282.     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);

  13283.   if (from_tty)
  13284.     printf_filtered (_("Saved to file '%s'.\n"), filename);
  13285.   do_cleanups (cleanup);
  13286. }

  13287. /* The `save breakpoints' command.  */

  13288. static void
  13289. save_breakpoints_command (char *args, int from_tty)
  13290. {
  13291.   save_breakpoints (args, from_tty, NULL);
  13292. }

  13293. /* The `save tracepoints' command.  */

  13294. static void
  13295. save_tracepoints_command (char *args, int from_tty)
  13296. {
  13297.   save_breakpoints (args, from_tty, is_tracepoint);
  13298. }

  13299. /* Create a vector of all tracepoints.  */

  13300. VEC(breakpoint_p) *
  13301. all_tracepoints (void)
  13302. {
  13303.   VEC(breakpoint_p) *tp_vec = 0;
  13304.   struct breakpoint *tp;

  13305.   ALL_TRACEPOINTS (tp)
  13306.   {
  13307.     VEC_safe_push (breakpoint_p, tp_vec, tp);
  13308.   }

  13309.   return tp_vec;
  13310. }


  13311. /* This help string is used for the break, hbreak, tbreak and thbreak
  13312.    commands.  It is defined as a macro to prevent duplication.
  13313.    COMMAND should be a string constant containing the name of the
  13314.    command.  */
  13315. #define BREAK_ARGS_HELP(command) \
  13316. command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
  13317. PROBE_MODIFIER shall be present if the command is to be placed in a\n\
  13318. probe point.  Accepted values are `-probe' (for a generic, automatically\n\
  13319. guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
  13320. LOCATION may be a line number, function name, or \"*\" and an address.\n\
  13321. If a line number is specified, break at start of code for that line.\n\
  13322. If a function is specified, break at start of code for that function.\n\
  13323. If an address is specified, break at that exact address.\n\
  13324. With no LOCATION, uses current execution address of the selected\n\
  13325. stack frame.  This is useful for breaking on return to a stack frame.\n\
  13326. \n\
  13327. THREADNUM is the number from \"info threads\".\n\
  13328. CONDITION is a boolean expression.\n\
  13329. \n\
  13330. Multiple breakpoints at one place are permitted, and useful if their\n\
  13331. conditions are different.\n\
  13332. \n\
  13333. Do \"help breakpoints\" for info on other commands dealing with breakpoints."

  13334. /* List of subcommands for "catch".  */
  13335. static struct cmd_list_element *catch_cmdlist;

  13336. /* List of subcommands for "tcatch".  */
  13337. static struct cmd_list_element *tcatch_cmdlist;

  13338. void
  13339. add_catch_command (char *name, char *docstring,
  13340.                    cmd_sfunc_ftype *sfunc,
  13341.                    completer_ftype *completer,
  13342.                    void *user_data_catch,
  13343.                    void *user_data_tcatch)
  13344. {
  13345.   struct cmd_list_element *command;

  13346.   command = add_cmd (name, class_breakpoint, NULL, docstring,
  13347.                      &catch_cmdlist);
  13348.   set_cmd_sfunc (command, sfunc);
  13349.   set_cmd_context (command, user_data_catch);
  13350.   set_cmd_completer (command, completer);

  13351.   command = add_cmd (name, class_breakpoint, NULL, docstring,
  13352.                      &tcatch_cmdlist);
  13353.   set_cmd_sfunc (command, sfunc);
  13354.   set_cmd_context (command, user_data_tcatch);
  13355.   set_cmd_completer (command, completer);
  13356. }

  13357. static void
  13358. clear_syscall_counts (struct inferior *inf)
  13359. {
  13360.   struct catch_syscall_inferior_data *inf_data
  13361.     = get_catch_syscall_inferior_data (inf);

  13362.   inf_data->total_syscalls_count = 0;
  13363.   inf_data->any_syscall_count = 0;
  13364.   VEC_free (int, inf_data->syscalls_counts);
  13365. }

  13366. static void
  13367. save_command (char *arg, int from_tty)
  13368. {
  13369.   printf_unfiltered (_("\"save\" must be followed by "
  13370.                        "the name of a save subcommand.\n"));
  13371.   help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
  13372. }

  13373. struct breakpoint *
  13374. iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
  13375.                           void *data)
  13376. {
  13377.   struct breakpoint *b, *b_tmp;

  13378.   ALL_BREAKPOINTS_SAFE (b, b_tmp)
  13379.     {
  13380.       if ((*callback) (b, data))
  13381.         return b;
  13382.     }

  13383.   return NULL;
  13384. }

  13385. /* Zero if any of the breakpoint's locations could be a location where
  13386.    functions have been inlined, nonzero otherwise.  */

  13387. static int
  13388. is_non_inline_function (struct breakpoint *b)
  13389. {
  13390.   /* The shared library event breakpoint is set on the address of a
  13391.      non-inline function.  */
  13392.   if (b->type == bp_shlib_event)
  13393.     return 1;

  13394.   return 0;
  13395. }

  13396. /* Nonzero if the specified PC cannot be a location where functions
  13397.    have been inlined.  */

  13398. int
  13399. pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
  13400.                            const struct target_waitstatus *ws)
  13401. {
  13402.   struct breakpoint *b;
  13403.   struct bp_location *bl;

  13404.   ALL_BREAKPOINTS (b)
  13405.     {
  13406.       if (!is_non_inline_function (b))
  13407.         continue;

  13408.       for (bl = b->loc; bl != NULL; bl = bl->next)
  13409.         {
  13410.           if (!bl->shlib_disabled
  13411.               && bpstat_check_location (bl, aspace, pc, ws))
  13412.             return 1;
  13413.         }
  13414.     }

  13415.   return 0;
  13416. }

  13417. /* Remove any references to OBJFILE which is going to be freed.  */

  13418. void
  13419. breakpoint_free_objfile (struct objfile *objfile)
  13420. {
  13421.   struct bp_location **locp, *loc;

  13422.   ALL_BP_LOCATIONS (loc, locp)
  13423.     if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
  13424.       loc->symtab = NULL;
  13425. }

  13426. void
  13427. initialize_breakpoint_ops (void)
  13428. {
  13429.   static int initialized = 0;

  13430.   struct breakpoint_ops *ops;

  13431.   if (initialized)
  13432.     return;
  13433.   initialized = 1;

  13434.   /* The breakpoint_ops structure to be inherit by all kinds of
  13435.      breakpoints (real breakpoints, i.e., user "break" breakpoints,
  13436.      internal and momentary breakpoints, etc.).  */
  13437.   ops = &bkpt_base_breakpoint_ops;
  13438.   *ops = base_breakpoint_ops;
  13439.   ops->re_set = bkpt_re_set;
  13440.   ops->insert_location = bkpt_insert_location;
  13441.   ops->remove_location = bkpt_remove_location;
  13442.   ops->breakpoint_hit = bkpt_breakpoint_hit;
  13443.   ops->create_sals_from_address = bkpt_create_sals_from_address;
  13444.   ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
  13445.   ops->decode_linespec = bkpt_decode_linespec;

  13446.   /* The breakpoint_ops structure to be used in regular breakpoints.  */
  13447.   ops = &bkpt_breakpoint_ops;
  13448.   *ops = bkpt_base_breakpoint_ops;
  13449.   ops->re_set = bkpt_re_set;
  13450.   ops->resources_needed = bkpt_resources_needed;
  13451.   ops->print_it = bkpt_print_it;
  13452.   ops->print_mention = bkpt_print_mention;
  13453.   ops->print_recreate = bkpt_print_recreate;

  13454.   /* Ranged breakpoints.  */
  13455.   ops = &ranged_breakpoint_ops;
  13456.   *ops = bkpt_breakpoint_ops;
  13457.   ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
  13458.   ops->resources_needed = resources_needed_ranged_breakpoint;
  13459.   ops->print_it = print_it_ranged_breakpoint;
  13460.   ops->print_one = print_one_ranged_breakpoint;
  13461.   ops->print_one_detail = print_one_detail_ranged_breakpoint;
  13462.   ops->print_mention = print_mention_ranged_breakpoint;
  13463.   ops->print_recreate = print_recreate_ranged_breakpoint;

  13464.   /* Internal breakpoints.  */
  13465.   ops = &internal_breakpoint_ops;
  13466.   *ops = bkpt_base_breakpoint_ops;
  13467.   ops->re_set = internal_bkpt_re_set;
  13468.   ops->check_status = internal_bkpt_check_status;
  13469.   ops->print_it = internal_bkpt_print_it;
  13470.   ops->print_mention = internal_bkpt_print_mention;

  13471.   /* Momentary breakpoints.  */
  13472.   ops = &momentary_breakpoint_ops;
  13473.   *ops = bkpt_base_breakpoint_ops;
  13474.   ops->re_set = momentary_bkpt_re_set;
  13475.   ops->check_status = momentary_bkpt_check_status;
  13476.   ops->print_it = momentary_bkpt_print_it;
  13477.   ops->print_mention = momentary_bkpt_print_mention;

  13478.   /* Momentary breakpoints for bp_longjmp and bp_exception.  */
  13479.   ops = &longjmp_breakpoint_ops;
  13480.   *ops = momentary_breakpoint_ops;
  13481.   ops->dtor = longjmp_bkpt_dtor;

  13482.   /* Probe breakpoints.  */
  13483.   ops = &bkpt_probe_breakpoint_ops;
  13484.   *ops = bkpt_breakpoint_ops;
  13485.   ops->insert_location = bkpt_probe_insert_location;
  13486.   ops->remove_location = bkpt_probe_remove_location;
  13487.   ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
  13488.   ops->decode_linespec = bkpt_probe_decode_linespec;

  13489.   /* Watchpoints.  */
  13490.   ops = &watchpoint_breakpoint_ops;
  13491.   *ops = base_breakpoint_ops;
  13492.   ops->dtor = dtor_watchpoint;
  13493.   ops->re_set = re_set_watchpoint;
  13494.   ops->insert_location = insert_watchpoint;
  13495.   ops->remove_location = remove_watchpoint;
  13496.   ops->breakpoint_hit = breakpoint_hit_watchpoint;
  13497.   ops->check_status = check_status_watchpoint;
  13498.   ops->resources_needed = resources_needed_watchpoint;
  13499.   ops->works_in_software_mode = works_in_software_mode_watchpoint;
  13500.   ops->print_it = print_it_watchpoint;
  13501.   ops->print_mention = print_mention_watchpoint;
  13502.   ops->print_recreate = print_recreate_watchpoint;
  13503.   ops->explains_signal = explains_signal_watchpoint;

  13504.   /* Masked watchpoints.  */
  13505.   ops = &masked_watchpoint_breakpoint_ops;
  13506.   *ops = watchpoint_breakpoint_ops;
  13507.   ops->insert_location = insert_masked_watchpoint;
  13508.   ops->remove_location = remove_masked_watchpoint;
  13509.   ops->resources_needed = resources_needed_masked_watchpoint;
  13510.   ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
  13511.   ops->print_it = print_it_masked_watchpoint;
  13512.   ops->print_one_detail = print_one_detail_masked_watchpoint;
  13513.   ops->print_mention = print_mention_masked_watchpoint;
  13514.   ops->print_recreate = print_recreate_masked_watchpoint;

  13515.   /* Tracepoints.  */
  13516.   ops = &tracepoint_breakpoint_ops;
  13517.   *ops = base_breakpoint_ops;
  13518.   ops->re_set = tracepoint_re_set;
  13519.   ops->breakpoint_hit = tracepoint_breakpoint_hit;
  13520.   ops->print_one_detail = tracepoint_print_one_detail;
  13521.   ops->print_mention = tracepoint_print_mention;
  13522.   ops->print_recreate = tracepoint_print_recreate;
  13523.   ops->create_sals_from_address = tracepoint_create_sals_from_address;
  13524.   ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
  13525.   ops->decode_linespec = tracepoint_decode_linespec;

  13526.   /* Probe tracepoints.  */
  13527.   ops = &tracepoint_probe_breakpoint_ops;
  13528.   *ops = tracepoint_breakpoint_ops;
  13529.   ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
  13530.   ops->decode_linespec = tracepoint_probe_decode_linespec;

  13531.   /* Static tracepoints with marker (`-m').  */
  13532.   ops = &strace_marker_breakpoint_ops;
  13533.   *ops = tracepoint_breakpoint_ops;
  13534.   ops->create_sals_from_address = strace_marker_create_sals_from_address;
  13535.   ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
  13536.   ops->decode_linespec = strace_marker_decode_linespec;

  13537.   /* Fork catchpoints.  */
  13538.   ops = &catch_fork_breakpoint_ops;
  13539.   *ops = base_breakpoint_ops;
  13540.   ops->insert_location = insert_catch_fork;
  13541.   ops->remove_location = remove_catch_fork;
  13542.   ops->breakpoint_hit = breakpoint_hit_catch_fork;
  13543.   ops->print_it = print_it_catch_fork;
  13544.   ops->print_one = print_one_catch_fork;
  13545.   ops->print_mention = print_mention_catch_fork;
  13546.   ops->print_recreate = print_recreate_catch_fork;

  13547.   /* Vfork catchpoints.  */
  13548.   ops = &catch_vfork_breakpoint_ops;
  13549.   *ops = base_breakpoint_ops;
  13550.   ops->insert_location = insert_catch_vfork;
  13551.   ops->remove_location = remove_catch_vfork;
  13552.   ops->breakpoint_hit = breakpoint_hit_catch_vfork;
  13553.   ops->print_it = print_it_catch_vfork;
  13554.   ops->print_one = print_one_catch_vfork;
  13555.   ops->print_mention = print_mention_catch_vfork;
  13556.   ops->print_recreate = print_recreate_catch_vfork;

  13557.   /* Exec catchpoints.  */
  13558.   ops = &catch_exec_breakpoint_ops;
  13559.   *ops = base_breakpoint_ops;
  13560.   ops->dtor = dtor_catch_exec;
  13561.   ops->insert_location = insert_catch_exec;
  13562.   ops->remove_location = remove_catch_exec;
  13563.   ops->breakpoint_hit = breakpoint_hit_catch_exec;
  13564.   ops->print_it = print_it_catch_exec;
  13565.   ops->print_one = print_one_catch_exec;
  13566.   ops->print_mention = print_mention_catch_exec;
  13567.   ops->print_recreate = print_recreate_catch_exec;

  13568.   /* Syscall catchpoints.  */
  13569.   ops = &catch_syscall_breakpoint_ops;
  13570.   *ops = base_breakpoint_ops;
  13571.   ops->dtor = dtor_catch_syscall;
  13572.   ops->insert_location = insert_catch_syscall;
  13573.   ops->remove_location = remove_catch_syscall;
  13574.   ops->breakpoint_hit = breakpoint_hit_catch_syscall;
  13575.   ops->print_it = print_it_catch_syscall;
  13576.   ops->print_one = print_one_catch_syscall;
  13577.   ops->print_mention = print_mention_catch_syscall;
  13578.   ops->print_recreate = print_recreate_catch_syscall;

  13579.   /* Solib-related catchpoints.  */
  13580.   ops = &catch_solib_breakpoint_ops;
  13581.   *ops = base_breakpoint_ops;
  13582.   ops->dtor = dtor_catch_solib;
  13583.   ops->insert_location = insert_catch_solib;
  13584.   ops->remove_location = remove_catch_solib;
  13585.   ops->breakpoint_hit = breakpoint_hit_catch_solib;
  13586.   ops->check_status = check_status_catch_solib;
  13587.   ops->print_it = print_it_catch_solib;
  13588.   ops->print_one = print_one_catch_solib;
  13589.   ops->print_mention = print_mention_catch_solib;
  13590.   ops->print_recreate = print_recreate_catch_solib;

  13591.   ops = &dprintf_breakpoint_ops;
  13592.   *ops = bkpt_base_breakpoint_ops;
  13593.   ops->re_set = dprintf_re_set;
  13594.   ops->resources_needed = bkpt_resources_needed;
  13595.   ops->print_it = bkpt_print_it;
  13596.   ops->print_mention = bkpt_print_mention;
  13597.   ops->print_recreate = dprintf_print_recreate;
  13598.   ops->after_condition_true = dprintf_after_condition_true;
  13599.   ops->breakpoint_hit = dprintf_breakpoint_hit;
  13600. }

  13601. /* Chain containing all defined "enable breakpoint" subcommands.  */

  13602. static struct cmd_list_element *enablebreaklist = NULL;

  13603. void
  13604. _initialize_breakpoint (void)
  13605. {
  13606.   struct cmd_list_element *c;

  13607.   initialize_breakpoint_ops ();

  13608.   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
  13609.   observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
  13610.   observer_attach_inferior_exit (clear_syscall_counts);
  13611.   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);

  13612.   breakpoint_objfile_key
  13613.     = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);

  13614.   catch_syscall_inferior_data
  13615.     = register_inferior_data_with_cleanup (NULL,
  13616.                                            catch_syscall_inferior_data_cleanup);

  13617.   breakpoint_chain = 0;
  13618.   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
  13619.      before a breakpoint is set.  */
  13620.   breakpoint_count = 0;

  13621.   tracepoint_count = 0;

  13622.   add_com ("ignore", class_breakpoint, ignore_command, _("\
  13623. Set ignore-count of breakpoint number N to COUNT.\n\
  13624. Usage is `ignore N COUNT'."));
  13625.   if (xdb_commands)
  13626.     add_com_alias ("bc", "ignore", class_breakpoint, 1);

  13627.   add_com ("commands", class_breakpoint, commands_command, _("\
  13628. Set commands to be executed when a breakpoint is hit.\n\
  13629. Give breakpoint number as argument after \"commands\".\n\
  13630. With no argument, the targeted breakpoint is the last one set.\n\
  13631. The commands themselves follow starting on the next line.\n\
  13632. Type a line containing \"end\" to indicate the end of them.\n\
  13633. Give \"silent\" as the first line to make the breakpoint silent;\n\
  13634. then no output is printed when it is hit, except what the commands print."));

  13635.   c = add_com ("condition", class_breakpoint, condition_command, _("\
  13636. Specify breakpoint number N to break only if COND is true.\n\
  13637. Usage is `condition N COND', where N is an integer and COND is an\n\
  13638. expression to be evaluated whenever breakpoint N is reached."));
  13639.   set_cmd_completer (c, condition_completer);

  13640.   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
  13641. Set a temporary breakpoint.\n\
  13642. Like \"break\" except the breakpoint is only temporary,\n\
  13643. so it will be deleted when hit.  Equivalent to \"break\" followed\n\
  13644. by using \"enable delete\" on the breakpoint number.\n\
  13645. \n"
  13646. BREAK_ARGS_HELP ("tbreak")));
  13647.   set_cmd_completer (c, location_completer);

  13648.   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
  13649. Set a hardware assisted breakpoint.\n\
  13650. Like \"break\" except the breakpoint requires hardware support,\n\
  13651. some target hardware may not have this support.\n\
  13652. \n"
  13653. BREAK_ARGS_HELP ("hbreak")));
  13654.   set_cmd_completer (c, location_completer);

  13655.   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
  13656. Set a temporary hardware assisted breakpoint.\n\
  13657. Like \"hbreak\" except the breakpoint is only temporary,\n\
  13658. so it will be deleted when hit.\n\
  13659. \n"
  13660. BREAK_ARGS_HELP ("thbreak")));
  13661.   set_cmd_completer (c, location_completer);

  13662.   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
  13663. Enable some breakpoints.\n\
  13664. Give breakpoint numbers (separated by spaces) as arguments.\n\
  13665. With no subcommand, breakpoints are enabled until you command otherwise.\n\
  13666. This is used to cancel the effect of the \"disable\" command.\n\
  13667. With a subcommand you can enable temporarily."),
  13668.                   &enablelist, "enable ", 1, &cmdlist);
  13669.   if (xdb_commands)
  13670.     add_com ("ab", class_breakpoint, enable_command, _("\
  13671. Enable some breakpoints.\n\
  13672. Give breakpoint numbers (separated by spaces) as arguments.\n\
  13673. With no subcommand, breakpoints are enabled until you command otherwise.\n\
  13674. This is used to cancel the effect of the \"disable\" command.\n\
  13675. With a subcommand you can enable temporarily."));

  13676.   add_com_alias ("en", "enable", class_breakpoint, 1);

  13677.   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
  13678. Enable some breakpoints.\n\
  13679. Give breakpoint numbers (separated by spaces) as arguments.\n\
  13680. This is used to cancel the effect of the \"disable\" command.\n\
  13681. May be abbreviated to simply \"enable\".\n"),
  13682.                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);

  13683.   add_cmd ("once", no_class, enable_once_command, _("\
  13684. Enable breakpoints for one hit.  Give breakpoint numbers.\n\
  13685. If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
  13686.            &enablebreaklist);

  13687.   add_cmd ("delete", no_class, enable_delete_command, _("\
  13688. Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
  13689. If a breakpoint is hit while enabled in this fashion, it is deleted."),
  13690.            &enablebreaklist);

  13691.   add_cmd ("count", no_class, enable_count_command, _("\
  13692. Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
  13693. If a breakpoint is hit while enabled in this fashion,\n\
  13694. the count is decremented; when it reaches zero, the breakpoint is disabled."),
  13695.            &enablebreaklist);

  13696.   add_cmd ("delete", no_class, enable_delete_command, _("\
  13697. Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
  13698. If a breakpoint is hit while enabled in this fashion, it is deleted."),
  13699.            &enablelist);

  13700.   add_cmd ("once", no_class, enable_once_command, _("\
  13701. Enable breakpoints for one hit.  Give breakpoint numbers.\n\
  13702. If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
  13703.            &enablelist);

  13704.   add_cmd ("count", no_class, enable_count_command, _("\
  13705. Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
  13706. If a breakpoint is hit while enabled in this fashion,\n\
  13707. the count is decremented; when it reaches zero, the breakpoint is disabled."),
  13708.            &enablelist);

  13709.   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
  13710. Disable some breakpoints.\n\
  13711. Arguments are breakpoint numbers with spaces in between.\n\
  13712. To disable all breakpoints, give no argument.\n\
  13713. A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
  13714.                   &disablelist, "disable ", 1, &cmdlist);
  13715.   add_com_alias ("dis", "disable", class_breakpoint, 1);
  13716.   add_com_alias ("disa", "disable", class_breakpoint, 1);
  13717.   if (xdb_commands)
  13718.     add_com ("sb", class_breakpoint, disable_command, _("\
  13719. Disable some breakpoints.\n\
  13720. Arguments are breakpoint numbers with spaces in between.\n\
  13721. To disable all breakpoints, give no argument.\n\
  13722. A disabled breakpoint is not forgotten, but has no effect until re-enabled."));

  13723.   add_cmd ("breakpoints", class_alias, disable_command, _("\
  13724. Disable some breakpoints.\n\
  13725. Arguments are breakpoint numbers with spaces in between.\n\
  13726. To disable all breakpoints, give no argument.\n\
  13727. A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
  13728. This command may be abbreviated \"disable\"."),
  13729.            &disablelist);

  13730.   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
  13731. Delete some breakpoints or auto-display expressions.\n\
  13732. Arguments are breakpoint numbers with spaces in between.\n\
  13733. To delete all breakpoints, give no argument.\n\
  13734. \n\
  13735. Also a prefix command for deletion of other GDB objects.\n\
  13736. The \"unset\" command is also an alias for \"delete\"."),
  13737.                   &deletelist, "delete ", 1, &cmdlist);
  13738.   add_com_alias ("d", "delete", class_breakpoint, 1);
  13739.   add_com_alias ("del", "delete", class_breakpoint, 1);
  13740.   if (xdb_commands)
  13741.     add_com ("db", class_breakpoint, delete_command, _("\
  13742. Delete some breakpoints.\n\
  13743. Arguments are breakpoint numbers with spaces in between.\n\
  13744. To delete all breakpoints, give no argument.\n"));

  13745.   add_cmd ("breakpoints", class_alias, delete_command, _("\
  13746. Delete some breakpoints or auto-display expressions.\n\
  13747. Arguments are breakpoint numbers with spaces in between.\n\
  13748. To delete all breakpoints, give no argument.\n\
  13749. This command may be abbreviated \"delete\"."),
  13750.            &deletelist);

  13751.   add_com ("clear", class_breakpoint, clear_command, _("\
  13752. Clear breakpoint at specified line or function.\n\
  13753. Argument may be line number, function name, or \"*\" and an address.\n\
  13754. If line number is specified, all breakpoints in that line are cleared.\n\
  13755. If function is specified, breakpoints at beginning of function are cleared.\n\
  13756. If an address is specified, breakpoints at that address are cleared.\n\
  13757. \n\
  13758. With no argument, clears all breakpoints in the line that the selected frame\n\
  13759. is executing in.\n\
  13760. \n\
  13761. See also the \"delete\" command which clears breakpoints by number."));
  13762.   add_com_alias ("cl", "clear", class_breakpoint, 1);

  13763.   c = add_com ("break", class_breakpoint, break_command, _("\
  13764. Set breakpoint at specified line or function.\n"
  13765. BREAK_ARGS_HELP ("break")));
  13766.   set_cmd_completer (c, location_completer);

  13767.   add_com_alias ("b", "break", class_run, 1);
  13768.   add_com_alias ("br", "break", class_run, 1);
  13769.   add_com_alias ("bre", "break", class_run, 1);
  13770.   add_com_alias ("brea", "break", class_run, 1);

  13771.   if (xdb_commands)
  13772.    add_com_alias ("ba", "break", class_breakpoint, 1);

  13773.   if (dbx_commands)
  13774.     {
  13775.       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
  13776. Break in function/address or break at a line in the current file."),
  13777.                              &stoplist, "stop ", 1, &cmdlist);
  13778.       add_cmd ("in", class_breakpoint, stopin_command,
  13779.                _("Break in function or address."), &stoplist);
  13780.       add_cmd ("at", class_breakpoint, stopat_command,
  13781.                _("Break at a line in the current file."), &stoplist);
  13782.       add_com ("status", class_info, breakpoints_info, _("\
  13783. Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
  13784. The \"Type\" column indicates one of:\n\
  13785. \tbreakpoint     - normal breakpoint\n\
  13786. \twatchpoint     - watchpoint\n\
  13787. The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
  13788. the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
  13789. breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
  13790. address and file/line number respectively.\n\
  13791. \n\
  13792. Convenience variable \"$_\" and default examine address for \"x\"\n\
  13793. are set to the address of the last breakpoint listed unless the command\n\
  13794. is prefixed with \"server \".\n\n\
  13795. Convenience variable \"$bpnum\" contains the number of the last\n\
  13796. breakpoint set."));
  13797.     }

  13798.   add_info ("breakpoints", breakpoints_info, _("\
  13799. Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
  13800. The \"Type\" column indicates one of:\n\
  13801. \tbreakpoint     - normal breakpoint\n\
  13802. \twatchpoint     - watchpoint\n\
  13803. The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
  13804. the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
  13805. breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
  13806. address and file/line number respectively.\n\
  13807. \n\
  13808. Convenience variable \"$_\" and default examine address for \"x\"\n\
  13809. are set to the address of the last breakpoint listed unless the command\n\
  13810. is prefixed with \"server \".\n\n\
  13811. Convenience variable \"$bpnum\" contains the number of the last\n\
  13812. breakpoint set."));

  13813.   add_info_alias ("b", "breakpoints", 1);

  13814.   if (xdb_commands)
  13815.     add_com ("lb", class_breakpoint, breakpoints_info, _("\
  13816. Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
  13817. The \"Type\" column indicates one of:\n\
  13818. \tbreakpoint     - normal breakpoint\n\
  13819. \twatchpoint     - watchpoint\n\
  13820. The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
  13821. the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
  13822. breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
  13823. address and file/line number respectively.\n\
  13824. \n\
  13825. Convenience variable \"$_\" and default examine address for \"x\"\n\
  13826. are set to the address of the last breakpoint listed unless the command\n\
  13827. is prefixed with \"server \".\n\n\
  13828. Convenience variable \"$bpnum\" contains the number of the last\n\
  13829. breakpoint set."));

  13830.   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
  13831. Status of all breakpoints, or breakpoint number NUMBER.\n\
  13832. The \"Type\" column indicates one of:\n\
  13833. \tbreakpoint     - normal breakpoint\n\
  13834. \twatchpoint     - watchpoint\n\
  13835. \tlongjmp        - internal breakpoint used to step through longjmp()\n\
  13836. \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
  13837. \tuntil          - internal breakpoint used by the \"until\" command\n\
  13838. \tfinish         - internal breakpoint used by the \"finish\" command\n\
  13839. The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
  13840. the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
  13841. breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
  13842. address and file/line number respectively.\n\
  13843. \n\
  13844. Convenience variable \"$_\" and default examine address for \"x\"\n\
  13845. are set to the address of the last breakpoint listed unless the command\n\
  13846. is prefixed with \"server \".\n\n\
  13847. Convenience variable \"$bpnum\" contains the number of the last\n\
  13848. breakpoint set."),
  13849.            &maintenanceinfolist);

  13850.   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
  13851. Set catchpoints to catch events."),
  13852.                   &catch_cmdlist, "catch ",
  13853.                   0/*allow-unknown*/, &cmdlist);

  13854.   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
  13855. Set temporary catchpoints to catch events."),
  13856.                   &tcatch_cmdlist, "tcatch ",
  13857.                   0/*allow-unknown*/, &cmdlist);

  13858.   add_catch_command ("fork", _("Catch calls to fork."),
  13859.                      catch_fork_command_1,
  13860.                      NULL,
  13861.                      (void *) (uintptr_t) catch_fork_permanent,
  13862.                      (void *) (uintptr_t) catch_fork_temporary);
  13863.   add_catch_command ("vfork", _("Catch calls to vfork."),
  13864.                      catch_fork_command_1,
  13865.                      NULL,
  13866.                      (void *) (uintptr_t) catch_vfork_permanent,
  13867.                      (void *) (uintptr_t) catch_vfork_temporary);
  13868.   add_catch_command ("exec", _("Catch calls to exec."),
  13869.                      catch_exec_command_1,
  13870.                      NULL,
  13871.                      CATCH_PERMANENT,
  13872.                      CATCH_TEMPORARY);
  13873.   add_catch_command ("load", _("Catch loads of shared libraries.\n\
  13874. Usage: catch load [REGEX]\n\
  13875. If REGEX is given, only stop for libraries matching the regular expression."),
  13876.                      catch_load_command_1,
  13877.                      NULL,
  13878.                      CATCH_PERMANENT,
  13879.                      CATCH_TEMPORARY);
  13880.   add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
  13881. Usage: catch unload [REGEX]\n\
  13882. If REGEX is given, only stop for libraries matching the regular expression."),
  13883.                      catch_unload_command_1,
  13884.                      NULL,
  13885.                      CATCH_PERMANENT,
  13886.                      CATCH_TEMPORARY);
  13887.   add_catch_command ("syscall", _("\
  13888. Catch system calls by their names and/or numbers.\n\
  13889. Arguments say which system calls to catch.  If no arguments\n\
  13890. are given, every system call will be caught.\n\
  13891. Arguments, if given, should be one or more system call names\n\
  13892. (if your system supports that), or system call numbers."),
  13893.                      catch_syscall_command_1,
  13894.                      catch_syscall_completer,
  13895.                      CATCH_PERMANENT,
  13896.                      CATCH_TEMPORARY);

  13897.   c = add_com ("watch", class_breakpoint, watch_command, _("\
  13898. Set a watchpoint for an expression.\n\
  13899. Usage: watch [-l|-location] EXPRESSION\n\
  13900. A watchpoint stops execution of your program whenever the value of\n\
  13901. an expression changes.\n\
  13902. If -l or -location is given, this evaluates EXPRESSION and watches\n\
  13903. the memory to which it refers."));
  13904.   set_cmd_completer (c, expression_completer);

  13905.   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
  13906. Set a read watchpoint for an expression.\n\
  13907. Usage: rwatch [-l|-location] EXPRESSION\n\
  13908. A watchpoint stops execution of your program whenever the value of\n\
  13909. an expression is read.\n\
  13910. If -l or -location is given, this evaluates EXPRESSION and watches\n\
  13911. the memory to which it refers."));
  13912.   set_cmd_completer (c, expression_completer);

  13913.   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
  13914. Set a watchpoint for an expression.\n\
  13915. Usage: awatch [-l|-location] EXPRESSION\n\
  13916. A watchpoint stops execution of your program whenever the value of\n\
  13917. an expression is either read or written.\n\
  13918. If -l or -location is given, this evaluates EXPRESSION and watches\n\
  13919. the memory to which it refers."));
  13920.   set_cmd_completer (c, expression_completer);

  13921.   add_info ("watchpoints", watchpoints_info, _("\
  13922. Status of specified watchpoints (all watchpoints if no argument)."));

  13923.   /* XXX: cagney/2005-02-23: This should be a boolean, and should
  13924.      respond to changes - contrary to the description.  */
  13925.   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
  13926.                             &can_use_hw_watchpoints, _("\
  13927. Set debugger's willingness to use watchpoint hardware."), _("\
  13928. Show debugger's willingness to use watchpoint hardware."), _("\
  13929. If zero, gdb will not use hardware for new watchpoints, even if\n\
  13930. such is available.  (However, any hardware watchpoints that were\n\
  13931. created before setting this to nonzero, will continue to use watchpoint\n\
  13932. hardware.)"),
  13933.                             NULL,
  13934.                             show_can_use_hw_watchpoints,
  13935.                             &setlist, &showlist);

  13936.   can_use_hw_watchpoints = 1;

  13937.   /* Tracepoint manipulation commands.  */

  13938.   c = add_com ("trace", class_breakpoint, trace_command, _("\
  13939. Set a tracepoint at specified line or function.\n\
  13940. \n"
  13941. BREAK_ARGS_HELP ("trace") "\n\
  13942. Do \"help tracepoints\" for info on other tracepoint commands."));
  13943.   set_cmd_completer (c, location_completer);

  13944.   add_com_alias ("tp", "trace", class_alias, 0);
  13945.   add_com_alias ("tr", "trace", class_alias, 1);
  13946.   add_com_alias ("tra", "trace", class_alias, 1);
  13947.   add_com_alias ("trac", "trace", class_alias, 1);

  13948.   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
  13949. Set a fast tracepoint at specified line or function.\n\
  13950. \n"
  13951. BREAK_ARGS_HELP ("ftrace") "\n\
  13952. Do \"help tracepoints\" for info on other tracepoint commands."));
  13953.   set_cmd_completer (c, location_completer);

  13954.   c = add_com ("strace", class_breakpoint, strace_command, _("\
  13955. Set a static tracepoint at specified line, function or marker.\n\
  13956. \n\
  13957. strace [LOCATION] [if CONDITION]\n\
  13958. LOCATION may be a line number, function name, \"*\" and an address,\n\
  13959. or -m MARKER_ID.\n\
  13960. If a line number is specified, probe the marker at start of code\n\
  13961. for that line.  If a function is specified, probe the marker at start\n\
  13962. of code for that function.  If an address is specified, probe the marker\n\
  13963. at that exact address.  If a marker id is specified, probe the marker\n\
  13964. with that name.  With no LOCATION, uses current execution address of\n\
  13965. the selected stack frame.\n\
  13966. Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
  13967. This collects arbitrary user data passed in the probe point call to the\n\
  13968. tracing library.  You can inspect it when analyzing the trace buffer,\n\
  13969. by printing the $_sdata variable like any other convenience variable.\n\
  13970. \n\
  13971. CONDITION is a boolean expression.\n\
  13972. \n\
  13973. Multiple tracepoints at one place are permitted, and useful if their\n\
  13974. conditions are different.\n\
  13975. \n\
  13976. Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
  13977. Do \"help tracepoints\" for info on other tracepoint commands."));
  13978.   set_cmd_completer (c, location_completer);

  13979.   add_info ("tracepoints", tracepoints_info, _("\
  13980. Status of specified tracepoints (all tracepoints if no argument).\n\
  13981. Convenience variable \"$tpnum\" contains the number of the\n\
  13982. last tracepoint set."));

  13983.   add_info_alias ("tp", "tracepoints", 1);

  13984.   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
  13985. Delete specified tracepoints.\n\
  13986. Arguments are tracepoint numbers, separated by spaces.\n\
  13987. No argument means delete all tracepoints."),
  13988.            &deletelist);
  13989.   add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);

  13990.   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
  13991. Disable specified tracepoints.\n\
  13992. Arguments are tracepoint numbers, separated by spaces.\n\
  13993. No argument means disable all tracepoints."),
  13994.            &disablelist);
  13995.   deprecate_cmd (c, "disable");

  13996.   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
  13997. Enable specified tracepoints.\n\
  13998. Arguments are tracepoint numbers, separated by spaces.\n\
  13999. No argument means enable all tracepoints."),
  14000.            &enablelist);
  14001.   deprecate_cmd (c, "enable");

  14002.   add_com ("passcount", class_trace, trace_pass_command, _("\
  14003. Set the passcount for a tracepoint.\n\
  14004. The trace will end when the tracepoint has been passed 'count' times.\n\
  14005. Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
  14006. if TPNUM is omitted, passcount refers to the last tracepoint defined."));

  14007.   add_prefix_cmd ("save", class_breakpoint, save_command,
  14008.                   _("Save breakpoint definitions as a script."),
  14009.                   &save_cmdlist, "save ",
  14010.                   0/*allow-unknown*/, &cmdlist);

  14011.   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
  14012. Save current breakpoint definitions as a script.\n\
  14013. This includes all types of breakpoints (breakpoints, watchpoints,\n\
  14014. catchpoints, tracepoints).  Use the 'source' command in another debug\n\
  14015. session to restore them."),
  14016.                &save_cmdlist);
  14017.   set_cmd_completer (c, filename_completer);

  14018.   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
  14019. Save current tracepoint definitions as a script.\n\
  14020. Use the 'source' command in another debug session to restore them."),
  14021.                &save_cmdlist);
  14022.   set_cmd_completer (c, filename_completer);

  14023.   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
  14024.   deprecate_cmd (c, "save tracepoints");

  14025.   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
  14026. Breakpoint specific settings\n\
  14027. Configure various breakpoint-specific variables such as\n\
  14028. pending breakpoint behavior"),
  14029.                   &breakpoint_set_cmdlist, "set breakpoint ",
  14030.                   0/*allow-unknown*/, &setlist);
  14031.   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
  14032. Breakpoint specific settings\n\
  14033. Configure various breakpoint-specific variables such as\n\
  14034. pending breakpoint behavior"),
  14035.                   &breakpoint_show_cmdlist, "show breakpoint ",
  14036.                   0/*allow-unknown*/, &showlist);

  14037.   add_setshow_auto_boolean_cmd ("pending", no_class,
  14038.                                 &pending_break_support, _("\
  14039. Set debugger's behavior regarding pending breakpoints."), _("\
  14040. Show debugger's behavior regarding pending breakpoints."), _("\
  14041. If on, an unrecognized breakpoint location will cause gdb to create a\n\
  14042. pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
  14043. an error.  If auto, an unrecognized breakpoint location results in a\n\
  14044. user-query to see if a pending breakpoint should be created."),
  14045.                                 NULL,
  14046.                                 show_pending_break_support,
  14047.                                 &breakpoint_set_cmdlist,
  14048.                                 &breakpoint_show_cmdlist);

  14049.   pending_break_support = AUTO_BOOLEAN_AUTO;

  14050.   add_setshow_boolean_cmd ("auto-hw", no_class,
  14051.                            &automatic_hardware_breakpoints, _("\
  14052. Set automatic usage of hardware breakpoints."), _("\
  14053. Show automatic usage of hardware breakpoints."), _("\
  14054. If set, the debugger will automatically use hardware breakpoints for\n\
  14055. breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
  14056. a warning will be emitted for such breakpoints."),
  14057.                            NULL,
  14058.                            show_automatic_hardware_breakpoints,
  14059.                            &breakpoint_set_cmdlist,
  14060.                            &breakpoint_show_cmdlist);

  14061.   add_setshow_boolean_cmd ("always-inserted", class_support,
  14062.                            &always_inserted_mode, _("\
  14063. Set mode for inserting breakpoints."), _("\
  14064. Show mode for inserting breakpoints."), _("\
  14065. When this mode is on, breakpoints are inserted immediately as soon as\n\
  14066. they're created, kept inserted even when execution stops, and removed\n\
  14067. only when the user deletes them.  When this mode is off (the default),\n\
  14068. breakpoints are inserted only when execution continues, and removed\n\
  14069. when execution stops."),
  14070.                                 NULL,
  14071.                                 &show_always_inserted_mode,
  14072.                                 &breakpoint_set_cmdlist,
  14073.                                 &breakpoint_show_cmdlist);

  14074.   add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
  14075.                         condition_evaluation_enums,
  14076.                         &condition_evaluation_mode_1, _("\
  14077. Set mode of breakpoint condition evaluation."), _("\
  14078. Show mode of breakpoint condition evaluation."), _("\
  14079. When this is set to \"host\", breakpoint conditions will be\n\
  14080. evaluated on the host's side by GDB.  When it is set to \"target\",\n\
  14081. breakpoint conditions will be downloaded to the target (if the target\n\
  14082. supports such feature) and conditions will be evaluated on the target's side.\n\
  14083. If this is set to \"auto\" (default), this will be automatically set to\n\
  14084. \"target\" if it supports condition evaluation, otherwise it will\n\
  14085. be set to \"gdb\""),
  14086.                            &set_condition_evaluation_mode,
  14087.                            &show_condition_evaluation_mode,
  14088.                            &breakpoint_set_cmdlist,
  14089.                            &breakpoint_show_cmdlist);

  14090.   add_com ("break-range", class_breakpoint, break_range_command, _("\
  14091. Set a breakpoint for an address range.\n\
  14092. break-range START-LOCATION, END-LOCATION\n\
  14093. where START-LOCATION and END-LOCATION can be one of the following:\n\
  14094.   LINENUM, for that line in the current file,\n\
  14095.   FILE:LINENUM, for that line in that file,\n\
  14096.   +OFFSET, for that number of lines after the current line\n\
  14097.            or the start of the range\n\
  14098.   FUNCTION, for the first line in that function,\n\
  14099.   FILE:FUNCTION, to distinguish among like-named static functions.\n\
  14100.   *ADDRESS, for the instruction at that address.\n\
  14101. \n\
  14102. The breakpoint will stop execution of the inferior whenever it executes\n\
  14103. an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
  14104. range (including START-LOCATION and END-LOCATION)."));

  14105.   c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
  14106. Set a dynamic printf at specified line or function.\n\
  14107. dprintf location,format string,arg1,arg2,...\n\
  14108. location may be a line number, function name, or \"*\" and an address.\n\
  14109. If a line number is specified, break at start of code for that line.\n\
  14110. If a function is specified, break at start of code for that function."));
  14111.   set_cmd_completer (c, location_completer);

  14112.   add_setshow_enum_cmd ("dprintf-style", class_support,
  14113.                         dprintf_style_enums, &dprintf_style, _("\
  14114. Set the style of usage for dynamic printf."), _("\
  14115. Show the style of usage for dynamic printf."), _("\
  14116. This setting chooses how GDB will do a dynamic printf.\n\
  14117. If the value is \"gdb\", then the printing is done by GDB to its own\n\
  14118. console, as with the \"printf\" command.\n\
  14119. If the value is \"call\", the print is done by calling a function in your\n\
  14120. program; by default printf(), but you can choose a different function or\n\
  14121. output stream by setting dprintf-function and dprintf-channel."),
  14122.                         update_dprintf_commands, NULL,
  14123.                         &setlist, &showlist);

  14124.   dprintf_function = xstrdup ("printf");
  14125.   add_setshow_string_cmd ("dprintf-function", class_support,
  14126.                           &dprintf_function, _("\
  14127. Set the function to use for dynamic printf"), _("\
  14128. Show the function to use for dynamic printf"), NULL,
  14129.                           update_dprintf_commands, NULL,
  14130.                           &setlist, &showlist);

  14131.   dprintf_channel = xstrdup ("");
  14132.   add_setshow_string_cmd ("dprintf-channel", class_support,
  14133.                           &dprintf_channel, _("\
  14134. Set the channel to use for dynamic printf"), _("\
  14135. Show the channel to use for dynamic printf"), NULL,
  14136.                           update_dprintf_commands, NULL,
  14137.                           &setlist, &showlist);

  14138.   add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
  14139.                            &disconnected_dprintf, _("\
  14140. Set whether dprintf continues after GDB disconnects."), _("\
  14141. Show whether dprintf continues after GDB disconnects."), _("\
  14142. Use this to let dprintf commands continue to hit and produce output\n\
  14143. even if GDB disconnects or detaches from the target."),
  14144.                            NULL,
  14145.                            NULL,
  14146.                            &setlist, &showlist);

  14147.   add_com ("agent-printf", class_vars, agent_printf_command, _("\
  14148. agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
  14149. (target agent only) This is useful for formatted output in user-defined commands."));

  14150.   automatic_hardware_breakpoints = 1;

  14151.   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
  14152.   observer_attach_thread_exit (remove_threaded_breakpoints);
  14153. }