]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
gas: restore Dwarf info generation after macro diagnostic adjustments
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
4a94e368 3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
d55e5aa6 21#include "arch-utils.h"
4de283e4
TT
22#include <ctype.h>
23#include "hashtab.h"
24#include "symtab.h"
25#include "frame.h"
c906108c 26#include "breakpoint.h"
4de283e4
TT
27#include "tracepoint.h"
28#include "gdbtypes.h"
c906108c 29#include "expression.h"
d55e5aa6 30#include "gdbcore.h"
4de283e4
TT
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
c906108c 34#include "inferior.h"
45741a9c 35#include "infrun.h"
4de283e4
TT
36#include "gdbthread.h"
37#include "target.h"
c906108c 38#include "language.h"
4de283e4
TT
39#include "gdb-demangle.h"
40#include "filenames.h"
41#include "annotate.h"
42#include "symfile.h"
d55e5aa6 43#include "objfiles.h"
4de283e4
TT
44#include "source.h"
45#include "linespec.h"
46#include "completer.h"
47#include "ui-out.h"
48#include "cli/cli-script.h"
49#include "block.h"
50#include "solib.h"
51#include "solist.h"
d55e5aa6 52#include "observable.h"
4de283e4
TT
53#include "memattr.h"
54#include "ada-lang.h"
55#include "top.h"
56#include "valprint.h"
57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
d322d6d6 59#include "gdbsupport/gdb_regex.h"
55aa24fb 60#include "probe.h"
4de283e4 61#include "cli/cli-utils.h"
d55e5aa6 62#include "stack.h"
4de283e4
TT
63#include "ax-gdb.h"
64#include "dummy-frame.h"
65#include "interps.h"
268a13a5 66#include "gdbsupport/format.h"
cfc31633 67#include "thread-fsm.h"
5d5658a1 68#include "tid-parse.h"
4de283e4 69#include "cli/cli-style.h"
0f8e2034 70#include "cli/cli-decode.h"
d3ce09f5 71
1042e4c0 72/* readline include files */
073bbbb0 73#include "readline/tilde.h"
1042e4c0
SS
74
75/* readline defines this. */
76#undef savestring
77
034dad6f 78#include "mi/mi-common.h"
6dddc817 79#include "extension.h"
325fac50 80#include <algorithm>
5ed8105e 81#include "progspace-and-thread.h"
268a13a5
TT
82#include "gdbsupport/array-view.h"
83#include "gdbsupport/gdb_optional.h"
104c1213 84
4a64f543 85/* Prototypes for local functions. */
c906108c 86
896b6bda 87static void map_breakpoint_numbers (const char *,
48649e1b 88 gdb::function_view<void (breakpoint *)>);
c906108c 89
f00aae0f 90static void
264f9890
PA
91 create_sals_from_location_spec_default (location_spec *locspec,
92 linespec_result *canonical);
983af33b 93
eee031e2
TT
94static void create_breakpoints_sal (struct gdbarch *,
95 struct linespec_result *,
96 gdb::unique_xmalloc_ptr<char>,
97 gdb::unique_xmalloc_ptr<char>,
98 enum bptype,
99 enum bpdisp, int, int,
100 int,
eee031e2 101 int, int, int, unsigned);
983af33b 102
a6535de1
TT
103static int can_use_hardware_watchpoint
104 (const std::vector<value_ref_ptr> &vals);
c906108c 105
b713485d 106static void mention (const breakpoint *);
c906108c 107
7a3e3265 108static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
c906108c 109
752a2291
PA
110static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
111
06edf0c0
PA
112static struct breakpoint *
113 momentary_breakpoint_from_master (struct breakpoint *orig,
114 enum bptype type,
7ab97995 115 int loc_enabled, int thread);
06edf0c0 116
d8de7963 117static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
76897487 118
a6d9a66e
UW
119static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
120 CORE_ADDR bpaddr,
60270718
AB
121 enum bptype bptype,
122 struct program_space *pspace);
76897487 123
d8de7963
AB
124static bool watchpoint_locations_match (const struct bp_location *loc1,
125 const struct bp_location *loc2);
85d721b8 126
d8de7963
AB
127static bool breakpoint_locations_match (const struct bp_location *loc1,
128 const struct bp_location *loc2,
129 bool sw_hw_bps_match = false);
7f32a4d5 130
d8de7963
AB
131static bool breakpoint_location_address_match (struct bp_location *bl,
132 const struct address_space *aspace,
133 CORE_ADDR addr);
f1310107 134
d8de7963
AB
135static bool breakpoint_location_address_range_overlap (struct bp_location *,
136 const address_space *,
137 CORE_ADDR, int);
d35ae833 138
834c0d03 139static int remove_breakpoint (struct bp_location *);
b2b6a7da 140static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 141
313f3b21 142static enum print_stop_action print_bp_stop_message (bpstat *bs);
c906108c 143
a14ed312 144static int hw_breakpoint_used_count (void);
c906108c 145
a1398e0c
PA
146static int hw_watchpoint_use_count (struct breakpoint *);
147
148static int hw_watchpoint_used_count_others (struct breakpoint *except,
149 enum bptype type,
150 int *other_type_used);
c906108c 151
816338b5
SS
152static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
153 int count);
c906108c 154
f431efe5 155static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 156
264f9890 157static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
5d926615 158 (struct breakpoint *b,
264f9890 159 location_spec *locspec,
5d926615
TT
160 struct program_space *search_pspace);
161
6e14e441
PA
162static bool bl_address_is_meaningful (bp_location *loc);
163
d8a77e4c
AB
164static int find_loc_num_by_location (const bp_location *loc);
165
44702360
PA
166/* update_global_location_list's modes of operation wrt to whether to
167 insert locations now. */
168enum ugll_insert_mode
169{
170 /* Don't insert any breakpoint locations into the inferior, only
171 remove already-inserted locations that no longer should be
172 inserted. Functions that delete a breakpoint or breakpoints
173 should specify this mode, so that deleting a breakpoint doesn't
174 have the side effect of inserting the locations of other
175 breakpoints that are marked not-inserted, but should_be_inserted
176 returns true on them.
177
178 This behavior is useful is situations close to tear-down -- e.g.,
179 after an exec, while the target still has execution, but
180 breakpoint shadows of the previous executable image should *NOT*
181 be restored to the new image; or before detaching, where the
182 target still has execution and wants to delete breakpoints from
183 GDB's lists, and all breakpoints had already been removed from
184 the inferior. */
185 UGLL_DONT_INSERT,
186
a25a5a45
PA
187 /* May insert breakpoints iff breakpoints_should_be_inserted_now
188 claims breakpoints should be inserted now. */
04086b45
PA
189 UGLL_MAY_INSERT,
190
a25a5a45
PA
191 /* Insert locations now, irrespective of
192 breakpoints_should_be_inserted_now. E.g., say all threads are
193 stopped right now, and the user did "continue". We need to
194 insert breakpoints _before_ resuming the target, but
195 UGLL_MAY_INSERT wouldn't insert them, because
196 breakpoints_should_be_inserted_now returns false at that point,
197 as no thread is running yet. */
04086b45 198 UGLL_INSERT
44702360
PA
199};
200
201static void update_global_location_list (enum ugll_insert_mode);
a5606eee 202
44702360 203static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 204
74960c60 205static void insert_breakpoint_locations (void);
a5606eee 206
0b39b52e 207static void trace_pass_command (const char *, int);
1042e4c0 208
558a9d82
YQ
209static void set_tracepoint_count (int num);
210
f2478a7e 211static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 212
d8de7963
AB
213/* Return true if B refers to a static tracepoint set by marker ("-m"),
214 zero otherwise. */
983af33b 215
d8de7963 216static bool strace_marker_p (struct breakpoint *b);
0fb4aa4b 217
264f9890
PA
218static void bkpt_probe_create_sals_from_location_spec
219 (location_spec *locspec,
03ada39e 220 struct linespec_result *canonical);
264f9890
PA
221static void tracepoint_probe_create_sals_from_location_spec
222 (location_spec *locspec,
03ada39e 223 struct linespec_result *canonical);
8613a476 224
74421c0b 225const struct breakpoint_ops code_breakpoint_ops =
8613a476 226{
264f9890 227 create_sals_from_location_spec_default,
eee031e2 228 create_breakpoints_sal,
8613a476
TT
229};
230
55aa24fb 231/* Breakpoints set on probes. */
a678887d 232static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
8613a476 233{
264f9890 234 bkpt_probe_create_sals_from_location_spec,
eee031e2 235 create_breakpoints_sal,
8613a476 236};
55aa24fb 237
bac7c5cf 238/* Tracepoints set on probes. */
a678887d 239static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
8613a476 240{
264f9890 241 tracepoint_probe_create_sals_from_location_spec,
eee031e2 242 create_breakpoints_sal,
8613a476 243};
bac7c5cf 244
1c2cbcf1
PA
245/* Implementation of abstract dtors. These must exist to satisfy the
246 linker. */
247
248breakpoint::~breakpoint ()
249{
250}
251
74421c0b 252code_breakpoint::~code_breakpoint ()
1c2cbcf1
PA
253{
254}
255
256catchpoint::~catchpoint ()
257{
258}
259
098f1272 260/* The structure to be used in regular breakpoints. */
74421c0b 261struct ordinary_breakpoint : public code_breakpoint
098f1272 262{
74421c0b 263 using code_breakpoint::code_breakpoint;
73063f51 264
7dd8e7ae 265 int resources_needed (const struct bp_location *) override;
7bd86313 266 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 267 void print_mention () const override;
4d1ae558 268 void print_recreate (struct ui_file *fp) const override;
098f1272
TT
269};
270
9a71ed14
PA
271/* Internal breakpoints. These typically have a lifetime the same as
272 the program, and they end up installed on the breakpoint chain with
273 a negative breakpoint number. They're visible in "maint info
274 breakpoints", but not "info breakpoints". */
74421c0b 275struct internal_breakpoint : public code_breakpoint
098f1272 276{
752a2291
PA
277 internal_breakpoint (struct gdbarch *gdbarch,
278 enum bptype type, CORE_ADDR address)
74421c0b 279 : code_breakpoint (gdbarch, type)
752a2291
PA
280 {
281 symtab_and_line sal;
282 sal.pc = address;
283 sal.section = find_pc_overlay (sal.pc);
284 sal.pspace = current_program_space;
285 add_location (sal);
286
287 pspace = current_program_space;
288 disposition = disp_donttouch;
289 }
73063f51 290
c359fff5
TT
291 void re_set () override;
292 void check_status (struct bpstat *bs) override;
7bd86313 293 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 294 void print_mention () const override;
098f1272
TT
295};
296
9a71ed14
PA
297/* Momentary breakpoints. These typically have a lifetime of some run
298 control command only, are always thread-specific, and have 0 for
299 breakpoint number. I.e., there can be many momentary breakpoints
300 on the breakpoint chain and they all same the same number (zero).
301 They're visible in "maint info breakpoints", but not "info
302 breakpoints". */
74421c0b 303struct momentary_breakpoint : public code_breakpoint
098f1272 304{
7ab97995
PA
305 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
306 program_space *pspace_,
307 const struct frame_id &frame_id_,
308 int thread_)
74421c0b 309 : code_breakpoint (gdbarch_, bptype)
7ab97995
PA
310 {
311 /* If FRAME_ID is valid, it should be a real frame, not an inlined
312 or tail-called one. */
313 gdb_assert (!frame_id_artificial_p (frame_id));
314
315 /* Momentary breakpoints are always thread-specific. */
316 gdb_assert (thread_ > 0);
317
318 pspace = pspace_;
319 enable_state = bp_enabled;
320 disposition = disp_donttouch;
321 frame_id = frame_id_;
322 thread = thread_;
323 }
73063f51 324
1fd30a47
TT
325 void re_set () override;
326 void check_status (struct bpstat *bs) override;
7bd86313 327 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 328 void print_mention () const override;
098f1272
TT
329};
330
331/* DPrintf breakpoints. */
5ad71d67 332struct dprintf_breakpoint : public ordinary_breakpoint
098f1272 333{
73063f51
TT
334 using ordinary_breakpoint::ordinary_breakpoint;
335
f293a0b5
TT
336 void re_set () override;
337 int breakpoint_hit (const struct bp_location *bl,
338 const address_space *aspace,
339 CORE_ADDR bp_addr,
340 const target_waitstatus &ws) override;
4d1ae558 341 void print_recreate (struct ui_file *fp) const override;
f293a0b5 342 void after_condition_true (struct bpstat *bs) override;
098f1272
TT
343};
344
ec45bb67
TT
345/* Ranged breakpoints. */
346struct ranged_breakpoint : public ordinary_breakpoint
347{
b925bf21
PA
348 explicit ranged_breakpoint (struct gdbarch *gdbarch,
349 const symtab_and_line &sal_start,
350 int length,
264f9890
PA
351 location_spec_up start_locspec,
352 location_spec_up end_locspec)
73063f51
TT
353 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
354 {
b925bf21
PA
355 bp_location *bl = add_location (sal_start);
356 bl->length = length;
357
358 disposition = disp_donttouch;
359
264f9890
PA
360 locspec = std::move (start_locspec);
361 locspec_range_end = std::move (end_locspec);
73063f51
TT
362 }
363
ec45bb67
TT
364 int breakpoint_hit (const struct bp_location *bl,
365 const address_space *aspace,
366 CORE_ADDR bp_addr,
367 const target_waitstatus &ws) override;
368 int resources_needed (const struct bp_location *) override;
7bd86313 369 enum print_stop_action print_it (const bpstat *bs) const override;
a67bcaba 370 bool print_one (bp_location **) const override;
ec45bb67 371 void print_one_detail (struct ui_out *) const override;
b713485d 372 void print_mention () const override;
4d1ae558 373 void print_recreate (struct ui_file *fp) const override;
ec45bb67
TT
374};
375
6d7a8c56
TT
376/* Static tracepoints with marker (`-m'). */
377struct static_marker_tracepoint : public tracepoint
378{
73063f51
TT
379 using tracepoint::tracepoint;
380
264f9890
PA
381 std::vector<symtab_and_line> decode_location_spec
382 (struct location_spec *locspec,
6d7a8c56
TT
383 struct program_space *search_pspace) override;
384};
385
d3ce09f5
SS
386/* The style in which to perform a dynamic printf. This is a user
387 option because different output options have different tradeoffs;
388 if GDB does the printing, there is better error handling if there
389 is a problem with any of the arguments, but using an inferior
390 function lets you have special-purpose printers and sending of
391 output to the same place as compiled-in print functions. */
392
393static const char dprintf_style_gdb[] = "gdb";
394static const char dprintf_style_call[] = "call";
395static const char dprintf_style_agent[] = "agent";
396static const char *const dprintf_style_enums[] = {
397 dprintf_style_gdb,
398 dprintf_style_call,
399 dprintf_style_agent,
400 NULL
401};
402static const char *dprintf_style = dprintf_style_gdb;
403
404/* The function to use for dynamic printf if the preferred style is to
405 call into the inferior. The value is simply a string that is
406 copied into the command, so it can be anything that GDB can
407 evaluate to a callable address, not necessarily a function name. */
408
e0700ba4 409static std::string dprintf_function = "printf";
d3ce09f5
SS
410
411/* The channel to use for dynamic printf if the preferred style is to
412 call into the inferior; if a nonempty string, it will be passed to
413 the call as the first argument, with the format string as the
414 second. As with the dprintf function, this can be anything that
415 GDB knows how to evaluate, so in addition to common choices like
416 "stderr", this could be an app-specific expression like
417 "mystreams[curlogger]". */
418
e0700ba4 419static std::string dprintf_channel;
d3ce09f5
SS
420
421/* True if dprintf commands should continue to operate even if GDB
422 has disconnected. */
491144b5 423static bool disconnected_dprintf = true;
d3ce09f5 424
5cea2a26
PA
425struct command_line *
426breakpoint_commands (struct breakpoint *b)
427{
d1b0a7bf 428 return b->commands ? b->commands.get () : NULL;
5cea2a26 429}
3daf8fe5 430
f3b1572e
PA
431/* Flag indicating that a command has proceeded the inferior past the
432 current breakpoint. */
433
491144b5 434static bool breakpoint_proceeded;
f3b1572e 435
956a9fb9 436const char *
2cec12e5
AR
437bpdisp_text (enum bpdisp disp)
438{
4a64f543
MS
439 /* NOTE: the following values are a part of MI protocol and
440 represent values of 'disp' field returned when inferior stops at
441 a breakpoint. */
bc043ef3 442 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 443
2cec12e5
AR
444 return bpdisps[(int) disp];
445}
c906108c 446
4a64f543 447/* Prototypes for exported functions. */
c906108c 448/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 449 if such is available. */
c906108c
SS
450static int can_use_hw_watchpoints;
451
920d2a44
AC
452static void
453show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c,
455 const char *value)
456{
6cb06a8c
TT
457 gdb_printf (file,
458 _("Debugger's willingness to use "
459 "watchpoint hardware is %s.\n"),
460 value);
920d2a44
AC
461}
462
fa8d40ab
JJ
463/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
464 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 465 for unrecognized breakpoint locations.
fa8d40ab
JJ
466 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
467static enum auto_boolean pending_break_support;
920d2a44
AC
468static void
469show_pending_break_support (struct ui_file *file, int from_tty,
470 struct cmd_list_element *c,
471 const char *value)
472{
6cb06a8c
TT
473 gdb_printf (file,
474 _("Debugger's behavior regarding "
475 "pending breakpoints is %s.\n"),
476 value);
920d2a44 477}
fa8d40ab 478
491144b5 479/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 480 set with "break" but falling in read-only memory.
491144b5 481 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 482 use hardware breakpoints. */
491144b5 483static bool automatic_hardware_breakpoints;
765dc015
VP
484static void
485show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
486 struct cmd_list_element *c,
487 const char *value)
488{
6cb06a8c
TT
489 gdb_printf (file,
490 _("Automatic usage of hardware breakpoints is %s.\n"),
491 value);
765dc015
VP
492}
493
a25a5a45
PA
494/* If on, GDB keeps breakpoints inserted even if the inferior is
495 stopped, and immediately inserts any new breakpoints as soon as
496 they're created. If off (default), GDB keeps breakpoints off of
497 the target as long as possible. That is, it delays inserting
498 breakpoints until the next resume, and removes them again when the
499 target fully stops. This is a bit safer in case GDB crashes while
500 processing user input. */
491144b5 501static bool always_inserted_mode = false;
72d0e2c5 502
33e5cbd6 503static void
74960c60 504show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 505 struct cmd_list_element *c, const char *value)
74960c60 506{
6cb06a8c
TT
507 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
508 value);
74960c60
VP
509}
510
b57bacec
PA
511/* See breakpoint.h. */
512
33e5cbd6 513int
a25a5a45 514breakpoints_should_be_inserted_now (void)
33e5cbd6 515{
a25a5a45
PA
516 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
517 {
518 /* If breakpoints are global, they should be inserted even if no
519 thread under gdb's control is running, or even if there are
520 no threads under GDB's control yet. */
521 return 1;
522 }
5b6d1e4f 523 else
a25a5a45 524 {
a25a5a45
PA
525 if (always_inserted_mode)
526 {
527 /* The user wants breakpoints inserted even if all threads
528 are stopped. */
529 return 1;
530 }
531
5b6d1e4f
PA
532 for (inferior *inf : all_inferiors ())
533 if (inf->has_execution ()
534 && threads_are_executing (inf->process_target ()))
535 return 1;
372316f1
PA
536
537 /* Don't remove breakpoints yet if, even though all threads are
538 stopped, we still have events to process. */
08036331 539 for (thread_info *tp : all_non_exited_threads ())
1edb66d8 540 if (tp->resumed () && tp->has_pending_waitstatus ())
372316f1 541 return 1;
a25a5a45
PA
542 }
543 return 0;
33e5cbd6 544}
765dc015 545
b775012e
LM
546static const char condition_evaluation_both[] = "host or target";
547
548/* Modes for breakpoint condition evaluation. */
549static const char condition_evaluation_auto[] = "auto";
550static const char condition_evaluation_host[] = "host";
551static const char condition_evaluation_target[] = "target";
552static const char *const condition_evaluation_enums[] = {
553 condition_evaluation_auto,
554 condition_evaluation_host,
555 condition_evaluation_target,
556 NULL
557};
558
559/* Global that holds the current mode for breakpoint condition evaluation. */
560static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
561
562/* Global that we use to display information to the user (gets its value from
563 condition_evaluation_mode_1. */
564static const char *condition_evaluation_mode = condition_evaluation_auto;
565
566/* Translate a condition evaluation mode MODE into either "host"
567 or "target". This is used mostly to translate from "auto" to the
568 real setting that is being used. It returns the translated
569 evaluation mode. */
570
571static const char *
572translate_condition_evaluation_mode (const char *mode)
573{
574 if (mode == condition_evaluation_auto)
575 {
576 if (target_supports_evaluation_of_breakpoint_conditions ())
577 return condition_evaluation_target;
578 else
579 return condition_evaluation_host;
580 }
581 else
582 return mode;
583}
584
585/* Discovers what condition_evaluation_auto translates to. */
586
587static const char *
588breakpoint_condition_evaluation_mode (void)
589{
590 return translate_condition_evaluation_mode (condition_evaluation_mode);
591}
592
593/* Return true if GDB should evaluate breakpoint conditions or false
594 otherwise. */
595
d8de7963 596static bool
b775012e
LM
597gdb_evaluates_breakpoint_condition_p (void)
598{
599 const char *mode = breakpoint_condition_evaluation_mode ();
600
601 return (mode == condition_evaluation_host);
602}
603
c906108c
SS
604/* Are we executing breakpoint commands? */
605static int executing_breakpoint_commands;
606
c02f5703
MS
607/* Are overlay event breakpoints enabled? */
608static int overlay_events_enabled;
609
e09342b5 610/* See description in breakpoint.h. */
491144b5 611bool target_exact_watchpoints = false;
e09342b5 612
c906108c 613/* Walk the following statement or block through all breakpoints.
e5dd4106 614 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 615 current breakpoint. */
c906108c 616
5c44784c
JM
617#define ALL_BREAKPOINTS_SAFE(B,TMP) \
618 for (B = breakpoint_chain; \
619 B ? (TMP=B->next, 1): 0; \
620 B = TMP)
c906108c 621
7cc221ef 622/* Chains of all breakpoints defined. */
c906108c 623
81e6b8eb 624static struct breakpoint *breakpoint_chain;
c906108c 625
240edef6 626/* See breakpoint.h. */
43892fdf 627
240edef6 628breakpoint_range
43892fdf
SM
629all_breakpoints ()
630{
631 return breakpoint_range (breakpoint_chain);
632}
633
240edef6 634/* See breakpoint.h. */
1428b37a 635
240edef6 636breakpoint_safe_range
1428b37a
SM
637all_breakpoints_safe ()
638{
639 return breakpoint_safe_range (all_breakpoints ());
640}
641
f6d17b2b
SM
642/* See breakpoint.h. */
643
644tracepoint_range
645all_tracepoints ()
646{
647 return tracepoint_range (breakpoint_chain);
648}
649
39ef2f62 650/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 651
5d51cd5d 652static std::vector<bp_location *> bp_locations;
876fa593 653
055c879f
SM
654/* See breakpoint.h. */
655
656const std::vector<bp_location *> &
48d7020b
SM
657all_bp_locations ()
658{
659 return bp_locations;
660}
661
e0d9a270
SM
662/* Range to iterate over breakpoint locations at a given address. */
663
664struct bp_locations_at_addr_range
665{
666 using iterator = std::vector<bp_location *>::iterator;
667
668 bp_locations_at_addr_range (CORE_ADDR addr)
669 {
670 struct compare
671 {
672 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
673 { return loc->address < addr_; }
674
675 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
676 { return addr_ < loc->address; }
677 };
678
679 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
680 addr, compare ());
681
682 m_begin = it_pair.first;
683 m_end = it_pair.second;
684 }
685
686 iterator begin () const
687 { return m_begin; }
688
689 iterator end () const
690 { return m_end; }
691
692private:
693 iterator m_begin;
694 iterator m_end;
695};
696
697/* Return a range to iterate over all breakpoint locations exactly at address
698 ADDR.
699
700 If it's needed to iterate multiple times on the same range, it's possible
701 to save the range in a local variable and use it multiple times:
702
703 auto range = all_bp_locations_at_addr (addr);
704
705 for (bp_location *loc : range)
706 // use loc
707
708 for (bp_location *loc : range)
709 // use loc
710
711 This saves a bit of time, as it avoids re-doing the binary searches to find
712 the range's boundaries. Just remember not to change the bp_locations vector
713 in the mean time, as it could make the range's iterators stale. */
714
715static bp_locations_at_addr_range
716all_bp_locations_at_addr (CORE_ADDR addr)
717{
718 return bp_locations_at_addr_range (addr);
719}
720
4a64f543 721/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 722 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 723 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 724 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 725 an address you need to read. */
876fa593 726
f5336ca5 727static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 728
4a64f543
MS
729/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
730 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
731 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
732 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 733 scan for shadow bytes for an address you need to read. */
876fa593 734
f5336ca5 735static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 736
4a64f543 737/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
738 from the bp_locations array, but for which a hit may still be
739 reported by a target. */
1123588c 740static std::vector<bp_location *> moribund_locations;
20874c92 741
c906108c
SS
742/* Number of last breakpoint made. */
743
95a42b64
TT
744static int breakpoint_count;
745
86b17b60
PA
746/* The value of `breakpoint_count' before the last command that
747 created breakpoints. If the last (break-like) command created more
748 than one breakpoint, then the difference between BREAKPOINT_COUNT
749 and PREV_BREAKPOINT_COUNT is more than one. */
750static int prev_breakpoint_count;
c906108c 751
1042e4c0
SS
752/* Number of last tracepoint made. */
753
95a42b64 754static int tracepoint_count;
1042e4c0 755
6149aea9
PA
756static struct cmd_list_element *breakpoint_set_cmdlist;
757static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 758struct cmd_list_element *save_cmdlist;
6149aea9 759
468d015d 760/* Return whether a breakpoint is an active enabled breakpoint. */
d8de7963 761static bool
468d015d
JJ
762breakpoint_enabled (struct breakpoint *b)
763{
0d381245 764 return (b->enable_state == bp_enabled);
468d015d
JJ
765}
766
c906108c
SS
767/* Set breakpoint count to NUM. */
768
95a42b64 769static void
fba45db2 770set_breakpoint_count (int num)
c906108c 771{
86b17b60 772 prev_breakpoint_count = breakpoint_count;
c906108c 773 breakpoint_count = num;
4fa62494 774 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
775}
776
86b17b60
PA
777/* Used by `start_rbreak_breakpoints' below, to record the current
778 breakpoint count before "rbreak" creates any breakpoint. */
779static int rbreak_start_breakpoint_count;
780
95a42b64
TT
781/* Called at the start an "rbreak" command to record the first
782 breakpoint made. */
86b17b60 783
c80049d3 784scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 785{
86b17b60 786 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
787}
788
789/* Called at the end of an "rbreak" command to record the last
790 breakpoint made. */
86b17b60 791
c80049d3 792scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 793{
86b17b60 794 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
795}
796
4a64f543 797/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
798
799void
fba45db2 800clear_breakpoint_hit_counts (void)
c906108c 801{
43892fdf 802 for (breakpoint *b : all_breakpoints ())
c906108c
SS
803 b->hit_count = 0;
804}
805
c906108c 806\f
48cb2d85
VP
807/* Return the breakpoint with the specified number, or NULL
808 if the number does not refer to an existing breakpoint. */
809
810struct breakpoint *
811get_breakpoint (int num)
812{
43892fdf 813 for (breakpoint *b : all_breakpoints ())
48cb2d85
VP
814 if (b->number == num)
815 return b;
816
43892fdf 817 return nullptr;
48cb2d85 818}
5c44784c 819
78805ff8
PW
820/* Return TRUE if NUM refer to an existing breakpoint that has
821 multiple code locations. */
822
823static bool
824has_multiple_locations (int num)
825{
826 for (breakpoint *b : all_breakpoints ())
827 if (b->number == num)
828 return b->loc != nullptr && b->loc->next != nullptr;
829
830 return false;
831}
832
c906108c 833\f
adc36818 834
b775012e
LM
835/* Mark locations as "conditions have changed" in case the target supports
836 evaluating conditions on its side. */
837
838static void
839mark_breakpoint_modified (struct breakpoint *b)
840{
b775012e
LM
841 /* This is only meaningful if the target is
842 evaluating conditions and if the user has
843 opted for condition evaluation on the target's
844 side. */
845 if (gdb_evaluates_breakpoint_condition_p ()
846 || !target_supports_evaluation_of_breakpoint_conditions ())
847 return;
848
849 if (!is_breakpoint (b))
850 return;
851
40cb8ca5 852 for (bp_location *loc : b->locations ())
b775012e
LM
853 loc->condition_changed = condition_modified;
854}
855
856/* Mark location as "conditions have changed" in case the target supports
857 evaluating conditions on its side. */
858
859static void
860mark_breakpoint_location_modified (struct bp_location *loc)
861{
862 /* This is only meaningful if the target is
863 evaluating conditions and if the user has
864 opted for condition evaluation on the target's
865 side. */
866 if (gdb_evaluates_breakpoint_condition_p ()
867 || !target_supports_evaluation_of_breakpoint_conditions ())
868
869 return;
870
871 if (!is_breakpoint (loc->owner))
872 return;
873
874 loc->condition_changed = condition_modified;
875}
876
877/* Sets the condition-evaluation mode using the static global
878 condition_evaluation_mode. */
879
880static void
eb4c3f4a 881set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
882 struct cmd_list_element *c)
883{
b775012e
LM
884 const char *old_mode, *new_mode;
885
886 if ((condition_evaluation_mode_1 == condition_evaluation_target)
887 && !target_supports_evaluation_of_breakpoint_conditions ())
888 {
889 condition_evaluation_mode_1 = condition_evaluation_mode;
890 warning (_("Target does not support breakpoint condition evaluation.\n"
891 "Using host evaluation mode instead."));
892 return;
893 }
894
895 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
896 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
897
abf1152a
JK
898 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
899 settings was "auto". */
900 condition_evaluation_mode = condition_evaluation_mode_1;
901
b775012e
LM
902 /* Only update the mode if the user picked a different one. */
903 if (new_mode != old_mode)
904 {
b775012e
LM
905 /* If the user switched to a different evaluation mode, we
906 need to synch the changes with the target as follows:
907
908 "host" -> "target": Send all (valid) conditions to the target.
909 "target" -> "host": Remove all the conditions from the target.
910 */
911
b775012e
LM
912 if (new_mode == condition_evaluation_target)
913 {
914 /* Mark everything modified and synch conditions with the
915 target. */
48d7020b 916 for (bp_location *loc : all_bp_locations ())
b775012e 917 mark_breakpoint_location_modified (loc);
24b21115 918 }
b775012e
LM
919 else
920 {
921 /* Manually mark non-duplicate locations to synch conditions
922 with the target. We do this to remove all the conditions the
923 target knows about. */
48d7020b 924 for (bp_location *loc : all_bp_locations ())
b775012e
LM
925 if (is_breakpoint (loc->owner) && loc->inserted)
926 loc->needs_update = 1;
927 }
928
929 /* Do the update. */
44702360 930 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
931 }
932
933 return;
934}
935
936/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
937 what "auto" is translating to. */
938
939static void
940show_condition_evaluation_mode (struct ui_file *file, int from_tty,
941 struct cmd_list_element *c, const char *value)
942{
943 if (condition_evaluation_mode == condition_evaluation_auto)
6cb06a8c
TT
944 gdb_printf (file,
945 _("Breakpoint condition evaluation "
946 "mode is %s (currently %s).\n"),
947 value,
948 breakpoint_condition_evaluation_mode ());
b775012e 949 else
6cb06a8c
TT
950 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
951 value);
b775012e
LM
952}
953
b5fa468f
TBA
954/* Parse COND_STRING in the context of LOC and set as the condition
955 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
956 the number of LOC within its owner. In case of parsing error, mark
957 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
958
959static void
960set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
961 int bp_num, int loc_num)
962{
963 bool has_junk = false;
964 try
965 {
966 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
967 block_for_pc (loc->address), 0);
968 if (*cond_string != 0)
969 has_junk = true;
970 else
971 {
972 loc->cond = std::move (new_exp);
973 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
974 gdb_printf (_("Breakpoint %d's condition is now valid at "
975 "location %d, enabling.\n"),
976 bp_num, loc_num);
b5fa468f
TBA
977
978 loc->disabled_by_cond = false;
979 }
980 }
981 catch (const gdb_exception_error &e)
982 {
983 if (loc->enabled)
984 {
985 /* Warn if a user-enabled location is now becoming disabled-by-cond.
986 BP_NUM is 0 if the breakpoint is being defined for the first
987 time using the "break ... if ..." command, and non-zero if
988 already defined. */
989 if (bp_num != 0)
990 warning (_("failed to validate condition at location %d.%d, "
991 "disabling:\n %s"), bp_num, loc_num, e.what ());
992 else
993 warning (_("failed to validate condition at location %d, "
994 "disabling:\n %s"), loc_num, e.what ());
995 }
996
997 loc->disabled_by_cond = true;
998 }
999
1000 if (has_junk)
1001 error (_("Garbage '%s' follows condition"), cond_string);
1002}
1003
adc36818 1004void
7a26bd4d 1005set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 1006 int from_tty, bool force)
adc36818 1007{
4c55e970 1008 if (*exp == 0)
3a5c3e22 1009 {
6f781ee3 1010 b->cond_string.reset ();
3a5c3e22 1011
4c55e970 1012 if (is_watchpoint (b))
98ed24fb 1013 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
1014 else
1015 {
b5fa468f 1016 int loc_num = 1;
40cb8ca5 1017 for (bp_location *loc : b->locations ())
4c55e970
TBA
1018 {
1019 loc->cond.reset ();
b5fa468f 1020 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
1021 gdb_printf (_("Breakpoint %d's condition is now valid at "
1022 "location %d, enabling.\n"),
1023 b->number, loc_num);
b5fa468f
TBA
1024 loc->disabled_by_cond = false;
1025 loc_num++;
4c55e970
TBA
1026
1027 /* No need to free the condition agent expression
1028 bytecode (if we have one). We will handle this
1029 when we go through update_global_location_list. */
1030 }
1031 }
1e620590 1032
adc36818 1033 if (from_tty)
6cb06a8c 1034 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
adc36818
PM
1035 }
1036 else
1037 {
adc36818
PM
1038 if (is_watchpoint (b))
1039 {
699bd4cf 1040 innermost_block_tracker tracker;
78319c15 1041 const char *arg = exp;
4c55e970 1042 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 1043 if (*arg != 0)
adc36818 1044 error (_("Junk at end of expression"));
98ed24fb 1045 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
4c55e970 1046 w->cond_exp = std::move (new_exp);
699bd4cf 1047 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
1048 }
1049 else
1050 {
4c55e970
TBA
1051 /* Parse and set condition expressions. We make two passes.
1052 In the first, we parse the condition string to see if it
b5fa468f
TBA
1053 is valid in at least one location. If so, the condition
1054 would be accepted. So we go ahead and set the locations'
1055 conditions. In case no valid case is found, we throw
4c55e970
TBA
1056 the error and the condition string will be rejected.
1057 This two-pass approach is taken to avoid setting the
1058 state of locations in case of a reject. */
40cb8ca5 1059 for (bp_location *loc : b->locations ())
4c55e970 1060 {
b5fa468f
TBA
1061 try
1062 {
1063 const char *arg = exp;
1064 parse_exp_1 (&arg, loc->address,
1065 block_for_pc (loc->address), 0);
1066 if (*arg != 0)
1067 error (_("Junk at end of expression"));
1068 break;
1069 }
1070 catch (const gdb_exception_error &e)
1071 {
1072 /* Condition string is invalid. If this happens to
733d554a
TBA
1073 be the last loc, abandon (if not forced) or continue
1074 (if forced). */
1075 if (loc->next == nullptr && !force)
b5fa468f
TBA
1076 throw;
1077 }
4c55e970
TBA
1078 }
1079
b5fa468f 1080 /* If we reach here, the condition is valid at some locations. */
dda83cd7 1081 int loc_num = 1;
40cb8ca5
SM
1082 for (bp_location *loc : b->locations ())
1083 {
1084 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1085 loc_num++;
1086 }
adc36818 1087 }
1e620590
TBA
1088
1089 /* We know that the new condition parsed successfully. The
1090 condition string of the breakpoint can be safely updated. */
6f781ee3 1091 b->cond_string = make_unique_xstrdup (exp);
1e620590 1092 b->condition_not_parsed = 0;
adc36818 1093 }
b775012e
LM
1094 mark_breakpoint_modified (b);
1095
76727919 1096 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
1097}
1098
79aabb73
TBA
1099/* See breakpoint.h. */
1100
1101void
1102set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1103 bool force)
1104{
43892fdf 1105 for (breakpoint *b : all_breakpoints ())
79aabb73
TBA
1106 if (b->number == bpnum)
1107 {
1108 /* Check if this breakpoint has a "stop" method implemented in an
1109 extension language. This method and conditions entered into GDB
1110 from the CLI are mutually exclusive. */
1111 const struct extension_language_defn *extlang
1112 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1113
1114 if (extlang != NULL)
1115 {
1116 error (_("Only one stop condition allowed. There is currently"
1117 " a %s stop condition defined for this breakpoint."),
1118 ext_lang_capitalized_name (extlang));
1119 }
1120 set_breakpoint_condition (b, exp, from_tty, force);
1121
1122 if (is_breakpoint (b))
1123 update_global_location_list (UGLL_MAY_INSERT);
1124
1125 return;
1126 }
1127
1128 error (_("No breakpoint number %d."), bpnum);
1129}
1130
b1d4d8d1
TBA
1131/* The options for the "condition" command. */
1132
1133struct condition_command_opts
1134{
1135 /* For "-force". */
1136 bool force_condition = false;
1137};
1138
1139static const gdb::option::option_def condition_command_option_defs[] = {
1140
1141 gdb::option::flag_option_def<condition_command_opts> {
1142 "force",
1143 [] (condition_command_opts *opts) { return &opts->force_condition; },
1144 N_("Set the condition even if it is invalid for all current locations."),
1145 },
1146
1147};
1148
1149/* Create an option_def_group for the "condition" options, with
1150 CC_OPTS as context. */
1151
1152static inline gdb::option::option_def_group
1153make_condition_command_options_def_group (condition_command_opts *cc_opts)
1154{
1155 return {{condition_command_option_defs}, cc_opts};
1156}
1157
d55637df
TT
1158/* Completion for the "condition" command. */
1159
eb3ff9a5 1160static void
6f937416 1161condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1162 completion_tracker &tracker,
b1d4d8d1 1163 const char *text, const char * /*word*/)
d55637df 1164{
b1d4d8d1
TBA
1165 bool has_no_arguments = (*text == '\0');
1166 condition_command_opts cc_opts;
1167 const auto group = make_condition_command_options_def_group (&cc_opts);
1168 if (gdb::option::complete_options
1169 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1170 return;
d55637df 1171
f1735a53 1172 text = skip_spaces (text);
b1d4d8d1 1173 const char *space = skip_to_space (text);
d55637df
TT
1174 if (*space == '\0')
1175 {
1176 int len;
d55637df
TT
1177
1178 if (text[0] == '$')
1179 {
b1d4d8d1 1180 tracker.advance_custom_word_point_by (1);
d55637df 1181 /* We don't support completion of history indices. */
eb3ff9a5
PA
1182 if (!isdigit (text[1]))
1183 complete_internalvar (tracker, &text[1]);
1184 return;
d55637df
TT
1185 }
1186
b1d4d8d1
TBA
1187 /* Suggest the "-force" flag if no arguments are given. If
1188 arguments were passed, they either already include the flag,
1189 or we are beyond the point of suggesting it because it's
1190 positionally the first argument. */
1191 if (has_no_arguments)
1192 gdb::option::complete_on_all_options (tracker, group);
1193
d55637df
TT
1194 /* We're completing the breakpoint number. */
1195 len = strlen (text);
1196
43892fdf 1197 for (breakpoint *b : all_breakpoints ())
58ce7251
SDJ
1198 {
1199 char number[50];
1200
1201 xsnprintf (number, sizeof (number), "%d", b->number);
1202
1203 if (strncmp (number, text, len) == 0)
b02f78f9 1204 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1205 }
d55637df 1206
eb3ff9a5 1207 return;
d55637df
TT
1208 }
1209
b1d4d8d1
TBA
1210 /* We're completing the expression part. Skip the breakpoint num. */
1211 const char *exp_start = skip_spaces (space);
1212 tracker.advance_custom_word_point_by (exp_start - text);
1213 text = exp_start;
1214 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1215 expression_completer (cmd, tracker, text, word);
d55637df
TT
1216}
1217
c906108c
SS
1218/* condition N EXP -- set break condition of breakpoint N to EXP. */
1219
1220static void
0b39b52e 1221condition_command (const char *arg, int from_tty)
c906108c 1222{
0b39b52e 1223 const char *p;
52f0bd74 1224 int bnum;
c906108c
SS
1225
1226 if (arg == 0)
e2e0b3e5 1227 error_no_arg (_("breakpoint number"));
c906108c
SS
1228
1229 p = arg;
733d554a
TBA
1230
1231 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1232 condition_command_opts cc_opts;
1233 const auto group = make_condition_command_options_def_group (&cc_opts);
1234 gdb::option::process_options
1235 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1236
c906108c 1237 bnum = get_number (&p);
5c44784c 1238 if (bnum == 0)
8a3fe4f8 1239 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1240
79aabb73 1241 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1242}
1243
a7bdde9e
VP
1244/* Check that COMMAND do not contain commands that are suitable
1245 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1246 Throw if any such commands is found. */
1247
a7bdde9e
VP
1248static void
1249check_no_tracepoint_commands (struct command_line *commands)
1250{
1251 struct command_line *c;
cc59ec59 1252
a7bdde9e
VP
1253 for (c = commands; c; c = c->next)
1254 {
a7bdde9e 1255 if (c->control_type == while_stepping_control)
3e43a32a
MS
1256 error (_("The 'while-stepping' command can "
1257 "only be used for tracepoints"));
a7bdde9e 1258
12973681
TT
1259 check_no_tracepoint_commands (c->body_list_0.get ());
1260 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1261
1262 /* Not that command parsing removes leading whitespace and comment
4a64f543 1263 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1264 command directly. */
1265 if (strstr (c->line, "collect ") == c->line)
1266 error (_("The 'collect' command can only be used for tracepoints"));
1267
51661e93
VP
1268 if (strstr (c->line, "teval ") == c->line)
1269 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1270 }
1271}
1272
098f1272 1273struct longjmp_breakpoint : public momentary_breakpoint
3b0871f4 1274{
73063f51
TT
1275 using momentary_breakpoint::momentary_breakpoint;
1276
c1fc2657 1277 ~longjmp_breakpoint () override;
3b0871f4
SM
1278};
1279
d77f58be
SS
1280/* Encapsulate tests for different types of tracepoints. */
1281
3b0871f4
SM
1282static bool
1283is_tracepoint_type (bptype type)
d9b3f62e
PA
1284{
1285 return (type == bp_tracepoint
1286 || type == bp_fast_tracepoint
7b572efb
TT
1287 || type == bp_static_tracepoint
1288 || type == bp_static_marker_tracepoint);
d9b3f62e
PA
1289}
1290
f2478a7e
SM
1291/* See breakpoint.h. */
1292
1293bool
d77f58be 1294is_tracepoint (const struct breakpoint *b)
a7bdde9e 1295{
d9b3f62e 1296 return is_tracepoint_type (b->type);
a7bdde9e 1297}
d9b3f62e 1298
a5e364af
SM
1299/* Factory function to create an appropriate instance of breakpoint given
1300 TYPE. */
1301
3b003a61 1302template<typename... Arg>
74421c0b 1303static std::unique_ptr<code_breakpoint>
3b003a61
PA
1304new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1305 Arg&&... args)
a5e364af 1306{
74421c0b 1307 code_breakpoint *b;
a5e364af 1308
098f1272
TT
1309 switch (type)
1310 {
1311 case bp_breakpoint:
1312 case bp_hardware_breakpoint:
3b003a61
PA
1313 b = new ordinary_breakpoint (gdbarch, type,
1314 std::forward<Arg> (args)...);
098f1272
TT
1315 break;
1316
1317 case bp_fast_tracepoint:
1318 case bp_static_tracepoint:
1319 case bp_tracepoint:
3b003a61
PA
1320 b = new tracepoint (gdbarch, type,
1321 std::forward<Arg> (args)...);
098f1272
TT
1322 break;
1323
6d7a8c56 1324 case bp_static_marker_tracepoint:
3b003a61
PA
1325 b = new static_marker_tracepoint (gdbarch, type,
1326 std::forward<Arg> (args)...);
6d7a8c56
TT
1327 break;
1328
098f1272 1329 case bp_dprintf:
3b003a61
PA
1330 b = new dprintf_breakpoint (gdbarch, type,
1331 std::forward<Arg> (args)...);
098f1272
TT
1332 break;
1333
098f1272
TT
1334 default:
1335 gdb_assert_not_reached ("invalid type");
1336 }
a5e364af 1337
74421c0b 1338 return std::unique_ptr<code_breakpoint> (b);
a5e364af
SM
1339}
1340
e5dd4106 1341/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1342 breakpoint. This function will throw an exception if a problem is
1343 found. */
48cb2d85 1344
95a42b64
TT
1345static void
1346validate_commands_for_breakpoint (struct breakpoint *b,
1347 struct command_line *commands)
48cb2d85 1348{
d77f58be 1349 if (is_tracepoint (b))
a7bdde9e 1350 {
c9a6ce02 1351 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1352 struct command_line *c;
1353 struct command_line *while_stepping = 0;
c9a6ce02
PA
1354
1355 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1356 might have included a while-stepping action, while the new
1357 ones might not. */
c9a6ce02
PA
1358 t->step_count = 0;
1359
1360 /* We need to verify that each top-level element of commands is
1361 valid for tracepoints, that there's at most one
1362 while-stepping element, and that the while-stepping's body
1363 has valid tracing commands excluding nested while-stepping.
1364 We also need to validate the tracepoint action line in the
1365 context of the tracepoint --- validate_actionline actually
1366 has side effects, like setting the tracepoint's
1367 while-stepping STEP_COUNT, in addition to checking if the
1368 collect/teval actions parse and make sense in the
1369 tracepoint's context. */
a7bdde9e
VP
1370 for (c = commands; c; c = c->next)
1371 {
a7bdde9e
VP
1372 if (c->control_type == while_stepping_control)
1373 {
1374 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1375 error (_("The 'while-stepping' command "
1376 "cannot be used for fast tracepoint"));
7b572efb
TT
1377 else if (b->type == bp_static_tracepoint
1378 || b->type == bp_static_marker_tracepoint)
3e43a32a
MS
1379 error (_("The 'while-stepping' command "
1380 "cannot be used for static tracepoint"));
a7bdde9e
VP
1381
1382 if (while_stepping)
3e43a32a
MS
1383 error (_("The 'while-stepping' command "
1384 "can be used only once"));
a7bdde9e
VP
1385 else
1386 while_stepping = c;
1387 }
c9a6ce02
PA
1388
1389 validate_actionline (c->line, b);
a7bdde9e
VP
1390 }
1391 if (while_stepping)
1392 {
1393 struct command_line *c2;
1394
12973681
TT
1395 gdb_assert (while_stepping->body_list_1 == nullptr);
1396 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1397 for (; c2; c2 = c2->next)
1398 {
a7bdde9e
VP
1399 if (c2->control_type == while_stepping_control)
1400 error (_("The 'while-stepping' command cannot be nested"));
1401 }
1402 }
1403 }
1404 else
1405 {
1406 check_no_tracepoint_commands (commands);
1407 }
95a42b64
TT
1408}
1409
0fb4aa4b
PA
1410/* Return a vector of all the static tracepoints set at ADDR. The
1411 caller is responsible for releasing the vector. */
1412
f51e0e20 1413std::vector<breakpoint *>
0fb4aa4b
PA
1414static_tracepoints_here (CORE_ADDR addr)
1415{
f51e0e20 1416 std::vector<breakpoint *> found;
0fb4aa4b 1417
43892fdf 1418 for (breakpoint *b : all_breakpoints ())
7b572efb
TT
1419 if (b->type == bp_static_tracepoint
1420 || b->type == bp_static_marker_tracepoint)
0fb4aa4b 1421 {
40cb8ca5 1422 for (bp_location *loc : b->locations ())
0fb4aa4b 1423 if (loc->address == addr)
f51e0e20 1424 found.push_back (b);
0fb4aa4b
PA
1425 }
1426
1427 return found;
1428}
1429
95a42b64 1430/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1431 validate that only allowed commands are included. */
95a42b64
TT
1432
1433void
4a64f543 1434breakpoint_set_commands (struct breakpoint *b,
12973681 1435 counted_command_line &&commands)
95a42b64 1436{
93921405 1437 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1438
d1b0a7bf 1439 b->commands = std::move (commands);
76727919 1440 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1441}
1442
45a43567
TT
1443/* Set the internal `silent' flag on the breakpoint. Note that this
1444 is not the same as the "silent" that may appear in the breakpoint's
1445 commands. */
1446
1447void
1448breakpoint_set_silent (struct breakpoint *b, int silent)
1449{
1450 int old_silent = b->silent;
1451
1452 b->silent = silent;
1453 if (old_silent != silent)
76727919 1454 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1455}
1456
1457/* Set the thread for this breakpoint. If THREAD is -1, make the
1458 breakpoint work for any thread. */
1459
1460void
1461breakpoint_set_thread (struct breakpoint *b, int thread)
1462{
1463 int old_thread = b->thread;
1464
1465 b->thread = thread;
1466 if (old_thread != thread)
76727919 1467 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1468}
1469
1470/* Set the task for this breakpoint. If TASK is 0, make the
1471 breakpoint work for any task. */
1472
1473void
1474breakpoint_set_task (struct breakpoint *b, int task)
1475{
1476 int old_task = b->task;
1477
1478 b->task = task;
1479 if (old_task != task)
76727919 1480 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1481}
1482
95a42b64 1483static void
896b6bda 1484commands_command_1 (const char *arg, int from_tty,
4a64f543 1485 struct command_line *control)
95a42b64 1486{
d1b0a7bf 1487 counted_command_line cmd;
999700cd
PW
1488 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1489 NULL after the call to read_command_lines if the user provides an empty
1490 list of command by just typing "end". */
1491 bool cmd_read = false;
95a42b64 1492
896b6bda
PA
1493 std::string new_arg;
1494
95a42b64
TT
1495 if (arg == NULL || !*arg)
1496 {
da1df1db 1497 /* Argument not explicitly given. Synthesize it. */
86b17b60 1498 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1499 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1500 breakpoint_count);
95a42b64 1501 else if (breakpoint_count > 0)
896b6bda 1502 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1503 }
da1df1db
TBA
1504 else
1505 {
1506 /* Create a copy of ARG. This is needed because the "commands"
1507 command may be coming from a script. In that case, the read
1508 line buffer is going to be overwritten in the lambda of
1509 'map_breakpoint_numbers' below when reading the next line
1510 before we are are done parsing the breakpoint numbers. */
1511 new_arg = arg;
1512 }
1513 arg = new_arg.c_str ();
48649e1b
TT
1514
1515 map_breakpoint_numbers
1516 (arg, [&] (breakpoint *b)
1517 {
999700cd 1518 if (!cmd_read)
48649e1b 1519 {
999700cd 1520 gdb_assert (cmd == NULL);
48649e1b 1521 if (control != NULL)
12973681 1522 cmd = control->body_list_0;
48649e1b
TT
1523 else
1524 {
81b1e71c
TT
1525 std::string str
1526 = string_printf (_("Type commands for breakpoint(s) "
1527 "%s, one per line."),
1528 arg);
48649e1b 1529
60b3cef2
TT
1530 auto do_validate = [=] (const char *line)
1531 {
1532 validate_actionline (line, b);
1533 };
1534 gdb::function_view<void (const char *)> validator;
1535 if (is_tracepoint (b))
1536 validator = do_validate;
1537
1538 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1539 }
999700cd 1540 cmd_read = true;
48649e1b
TT
1541 }
1542
1543 /* If a breakpoint was on the list more than once, we don't need to
1544 do anything. */
1545 if (b->commands != cmd)
1546 {
d1b0a7bf 1547 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1548 b->commands = cmd;
76727919 1549 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1550 }
1551 });
95a42b64
TT
1552}
1553
1554static void
0b39b52e 1555commands_command (const char *arg, int from_tty)
95a42b64
TT
1556{
1557 commands_command_1 (arg, from_tty, NULL);
c906108c 1558}
40c03ae8
EZ
1559
1560/* Like commands_command, but instead of reading the commands from
1561 input stream, takes them from an already parsed command structure.
1562
1563 This is used by cli-script.c to DTRT with breakpoint commands
1564 that are part of if and while bodies. */
1565enum command_control_type
896b6bda 1566commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1567{
95a42b64
TT
1568 commands_command_1 (arg, 0, cmd);
1569 return simple_control;
40c03ae8 1570}
876fa593 1571
d8de7963 1572/* Return true if BL->TARGET_INFO contains valid information. */
876fa593 1573
d8de7963 1574static bool
876fa593
JK
1575bp_location_has_shadow (struct bp_location *bl)
1576{
1577 if (bl->loc_type != bp_loc_software_breakpoint)
d8de7963 1578 return false;
876fa593 1579 if (!bl->inserted)
d8de7963 1580 return false;
876fa593 1581 if (bl->target_info.shadow_len == 0)
e5dd4106 1582 /* BL isn't valid, or doesn't shadow memory. */
d8de7963
AB
1583 return false;
1584 return true;
876fa593
JK
1585}
1586
9d497a19
PA
1587/* Update BUF, which is LEN bytes read from the target address
1588 MEMADDR, by replacing a memory breakpoint with its shadowed
1589 contents.
1590
1591 If READBUF is not NULL, this buffer must not overlap with the of
1592 the breakpoint location's shadow_contents buffer. Otherwise, a
1593 failed assertion internal error will be raised. */
1594
1595static void
1596one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1597 const gdb_byte *writebuf_org,
1598 ULONGEST memaddr, LONGEST len,
1599 struct bp_target_info *target_info,
1600 struct gdbarch *gdbarch)
1601{
1602 /* Now do full processing of the found relevant range of elements. */
1603 CORE_ADDR bp_addr = 0;
1604 int bp_size = 0;
1605 int bptoffset = 0;
1606
1607 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1608 current_program_space->aspace, 0))
1609 {
1610 /* The breakpoint is inserted in a different address space. */
1611 return;
1612 }
1613
1614 /* Addresses and length of the part of the breakpoint that
1615 we need to copy. */
1616 bp_addr = target_info->placed_address;
1617 bp_size = target_info->shadow_len;
1618
1619 if (bp_addr + bp_size <= memaddr)
1620 {
1621 /* The breakpoint is entirely before the chunk of memory we are
1622 reading. */
1623 return;
1624 }
1625
1626 if (bp_addr >= memaddr + len)
1627 {
1628 /* The breakpoint is entirely after the chunk of memory we are
1629 reading. */
1630 return;
1631 }
1632
1633 /* Offset within shadow_contents. */
1634 if (bp_addr < memaddr)
1635 {
1636 /* Only copy the second part of the breakpoint. */
1637 bp_size -= memaddr - bp_addr;
1638 bptoffset = memaddr - bp_addr;
1639 bp_addr = memaddr;
1640 }
1641
1642 if (bp_addr + bp_size > memaddr + len)
1643 {
1644 /* Only copy the first part of the breakpoint. */
1645 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1646 }
1647
1648 if (readbuf != NULL)
1649 {
1650 /* Verify that the readbuf buffer does not overlap with the
1651 shadow_contents buffer. */
1652 gdb_assert (target_info->shadow_contents >= readbuf + len
1653 || readbuf >= (target_info->shadow_contents
1654 + target_info->shadow_len));
1655
1656 /* Update the read buffer with this inserted breakpoint's
1657 shadow. */
1658 memcpy (readbuf + bp_addr - memaddr,
1659 target_info->shadow_contents + bptoffset, bp_size);
1660 }
1661 else
1662 {
1663 const unsigned char *bp;
0d5ed153
MR
1664 CORE_ADDR addr = target_info->reqstd_address;
1665 int placed_size;
9d497a19
PA
1666
1667 /* Update the shadow with what we want to write to memory. */
1668 memcpy (target_info->shadow_contents + bptoffset,
1669 writebuf_org + bp_addr - memaddr, bp_size);
1670
1671 /* Determine appropriate breakpoint contents and size for this
1672 address. */
0d5ed153 1673 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1674
1675 /* Update the final write buffer with this inserted
1676 breakpoint's INSN. */
1677 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1678 }
1679}
1680
8defab1a 1681/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1682 by replacing any memory breakpoints with their shadowed contents.
1683
35c63cd8
JB
1684 If READBUF is not NULL, this buffer must not overlap with any of
1685 the breakpoint location's shadow_contents buffers. Otherwise,
1686 a failed assertion internal error will be raised.
1687
876fa593 1688 The range of shadowed area by each bp_location is:
f5336ca5
PA
1689 bl->address - bp_locations_placed_address_before_address_max
1690 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1691 The range we were requested to resolve shadows for is:
1692 memaddr ... memaddr + len
1693 Thus the safe cutoff boundaries for performance optimization are
35df4500 1694 memaddr + len <= (bl->address
f5336ca5 1695 - bp_locations_placed_address_before_address_max)
876fa593 1696 and:
f5336ca5 1697 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1698
8defab1a 1699void
f0ba3972
PA
1700breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1701 const gdb_byte *writebuf_org,
1702 ULONGEST memaddr, LONGEST len)
c906108c 1703{
4a64f543
MS
1704 /* Left boundary, right boundary and median element of our binary
1705 search. */
876fa593
JK
1706 unsigned bc_l, bc_r, bc;
1707
4a64f543
MS
1708 /* Find BC_L which is a leftmost element which may affect BUF
1709 content. It is safe to report lower value but a failure to
1710 report higher one. */
876fa593
JK
1711
1712 bc_l = 0;
5d51cd5d 1713 bc_r = bp_locations.size ();
876fa593
JK
1714 while (bc_l + 1 < bc_r)
1715 {
35df4500 1716 struct bp_location *bl;
876fa593
JK
1717
1718 bc = (bc_l + bc_r) / 2;
f5336ca5 1719 bl = bp_locations[bc];
876fa593 1720
4a64f543
MS
1721 /* Check first BL->ADDRESS will not overflow due to the added
1722 constant. Then advance the left boundary only if we are sure
1723 the BC element can in no way affect the BUF content (MEMADDR
1724 to MEMADDR + LEN range).
876fa593 1725
f5336ca5 1726 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1727 offset so that we cannot miss a breakpoint with its shadow
1728 range tail still reaching MEMADDR. */
c5aa993b 1729
f5336ca5 1730 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1731 >= bl->address)
f5336ca5 1732 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1733 <= memaddr))
876fa593
JK
1734 bc_l = bc;
1735 else
1736 bc_r = bc;
1737 }
1738
128070bb
PA
1739 /* Due to the binary search above, we need to make sure we pick the
1740 first location that's at BC_L's address. E.g., if there are
1741 multiple locations at the same address, BC_L may end up pointing
1742 at a duplicate location, and miss the "master"/"inserted"
1743 location. Say, given locations L1, L2 and L3 at addresses A and
1744 B:
1745
1746 L1@A, L2@A, L3@B, ...
1747
1748 BC_L could end up pointing at location L2, while the "master"
1749 location could be L1. Since the `loc->inserted' flag is only set
1750 on "master" locations, we'd forget to restore the shadow of L1
1751 and L2. */
1752 while (bc_l > 0
f5336ca5 1753 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1754 bc_l--;
1755
876fa593
JK
1756 /* Now do full processing of the found relevant range of elements. */
1757
5d51cd5d 1758 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1759 {
1760 struct bp_location *bl = bp_locations[bc];
876fa593 1761
01add95b
SM
1762 /* bp_location array has BL->OWNER always non-NULL. */
1763 if (bl->owner->type == bp_none)
1764 warning (_("reading through apparently deleted breakpoint #%d?"),
1765 bl->owner->number);
ffce0d52 1766
01add95b
SM
1767 /* Performance optimization: any further element can no longer affect BUF
1768 content. */
876fa593 1769
01add95b
SM
1770 if (bl->address >= bp_locations_placed_address_before_address_max
1771 && (memaddr + len
1772 <= (bl->address
1773 - bp_locations_placed_address_before_address_max)))
1774 break;
876fa593 1775
01add95b
SM
1776 if (!bp_location_has_shadow (bl))
1777 continue;
6c95b8df 1778
01add95b
SM
1779 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1780 memaddr, len, &bl->target_info, bl->gdbarch);
1781 }
c906108c 1782}
9d497a19 1783
f2478a7e 1784/* See breakpoint.h. */
b775012e 1785
f2478a7e 1786bool
b775012e
LM
1787is_breakpoint (const struct breakpoint *bpt)
1788{
1789 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1790 || bpt->type == bp_hardware_breakpoint
1791 || bpt->type == bp_dprintf);
b775012e
LM
1792}
1793
60e1c644
PA
1794/* Return true if BPT is of any hardware watchpoint kind. */
1795
f2478a7e 1796static bool
d77f58be 1797is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1798{
1799 return (bpt->type == bp_hardware_watchpoint
1800 || bpt->type == bp_read_watchpoint
1801 || bpt->type == bp_access_watchpoint);
1802}
7270d8f2 1803
f2478a7e 1804/* See breakpoint.h. */
60e1c644 1805
f2478a7e 1806bool
d77f58be 1807is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1808{
1809 return (is_hardware_watchpoint (bpt)
1810 || bpt->type == bp_watchpoint);
1811}
1812
3a5c3e22
PA
1813/* Returns true if the current thread and its running state are safe
1814 to evaluate or update watchpoint B. Watchpoints on local
1815 expressions need to be evaluated in the context of the thread that
1816 was current when the watchpoint was created, and, that thread needs
1817 to be stopped to be able to select the correct frame context.
1818 Watchpoints on global expressions can be evaluated on any thread,
1819 and in any state. It is presently left to the target allowing
1820 memory accesses when threads are running. */
f6bc2008 1821
d8de7963 1822static bool
3a5c3e22 1823watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1824{
c1fc2657 1825 return (b->pspace == current_program_space
d7e15655
TT
1826 && (b->watchpoint_thread == null_ptid
1827 || (inferior_ptid == b->watchpoint_thread
611841bb 1828 && !inferior_thread ()->executing ())));
f6bc2008
PA
1829}
1830
d0fb5eae
JK
1831/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1832 associated bp_watchpoint_scope breakpoint. */
1833
1834static void
3a5c3e22 1835watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1836{
c1fc2657 1837 if (w->related_breakpoint != w)
d0fb5eae 1838 {
c1fc2657
SM
1839 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1840 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1841 w->related_breakpoint->disposition = disp_del_at_next_stop;
1842 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1843 w->related_breakpoint = w;
d0fb5eae 1844 }
c1fc2657 1845 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1846}
1847
bb9d5f81
PP
1848/* Extract a bitfield value from value VAL using the bit parameters contained in
1849 watchpoint W. */
1850
1851static struct value *
1852extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1853{
1854 struct value *bit_val;
1855
1856 if (val == NULL)
1857 return NULL;
1858
1859 bit_val = allocate_value (value_type (val));
1860
1861 unpack_value_bitfield (bit_val,
1862 w->val_bitpos,
1863 w->val_bitsize,
50888e42 1864 value_contents_for_printing (val).data (),
bb9d5f81
PP
1865 value_offset (val),
1866 val);
1867
1868 return bit_val;
1869}
1870
6e14e441
PA
1871/* Allocate a dummy location and add it to B. This is required
1872 because bpstat_stop_status requires a location to be able to report
1873 stops. */
c6d81124
PA
1874
1875static void
6e14e441
PA
1876add_dummy_location (struct breakpoint *b,
1877 struct program_space *pspace)
c6d81124 1878{
6e14e441 1879 gdb_assert (b->loc == NULL);
c6d81124 1880
6e14e441 1881 b->loc = new bp_location (b, bp_loc_other);
c6d81124 1882 b->loc->pspace = pspace;
c6d81124
PA
1883}
1884
567e1b4e 1885/* Assuming that B is a watchpoint:
d8de7963 1886 - Reparse watchpoint expression, if REPARSE is true
a5606eee 1887 - Evaluate expression and store the result in B->val
567e1b4e
JB
1888 - Evaluate the condition if there is one, and store the result
1889 in b->loc->cond.
a5606eee
VP
1890 - Update the list of values that must be watched in B->loc.
1891
4a64f543
MS
1892 If the watchpoint disposition is disp_del_at_next_stop, then do
1893 nothing. If this is local watchpoint that is out of scope, delete
1894 it.
1895
1896 Even with `set breakpoint always-inserted on' the watchpoints are
1897 removed + inserted on each stop here. Normal breakpoints must
1898 never be removed because they might be missed by a running thread
1899 when debugging in non-stop mode. On the other hand, hardware
1900 watchpoints (is_hardware_watchpoint; processed here) are specific
1901 to each LWP since they are stored in each LWP's hardware debug
1902 registers. Therefore, such LWP must be stopped first in order to
1903 be able to modify its hardware watchpoints.
1904
1905 Hardware watchpoints must be reset exactly once after being
1906 presented to the user. It cannot be done sooner, because it would
1907 reset the data used to present the watchpoint hit to the user. And
1908 it must not be done later because it could display the same single
1909 watchpoint hit during multiple GDB stops. Note that the latter is
1910 relevant only to the hardware watchpoint types bp_read_watchpoint
1911 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1912 not user-visible - its hit is suppressed if the memory content has
1913 not changed.
1914
1915 The following constraints influence the location where we can reset
1916 hardware watchpoints:
1917
1918 * target_stopped_by_watchpoint and target_stopped_data_address are
1919 called several times when GDB stops.
1920
1921 [linux]
1922 * Multiple hardware watchpoints can be hit at the same time,
1923 causing GDB to stop. GDB only presents one hardware watchpoint
1924 hit at a time as the reason for stopping, and all the other hits
1925 are presented later, one after the other, each time the user
1926 requests the execution to be resumed. Execution is not resumed
1927 for the threads still having pending hit event stored in
1928 LWP_INFO->STATUS. While the watchpoint is already removed from
1929 the inferior on the first stop the thread hit event is kept being
1930 reported from its cached value by linux_nat_stopped_data_address
1931 until the real thread resume happens after the watchpoint gets
1932 presented and thus its LWP_INFO->STATUS gets reset.
1933
1934 Therefore the hardware watchpoint hit can get safely reset on the
1935 watchpoint removal from inferior. */
a79d3c27 1936
b40ce68a 1937static void
d8de7963 1938update_watchpoint (struct watchpoint *b, bool reparse)
7270d8f2 1939{
705b6305 1940 bool within_current_scope;
a5606eee 1941
f6bc2008
PA
1942 /* If this is a local watchpoint, we only want to check if the
1943 watchpoint frame is in scope if the current thread is the thread
1944 that was used to create the watchpoint. */
1945 if (!watchpoint_in_thread_scope (b))
1946 return;
1947
c1fc2657 1948 if (b->disposition == disp_del_at_next_stop)
a5606eee 1949 return;
705b6305
AB
1950
1951 gdb::optional<scoped_restore_selected_frame> restore_frame;
a5606eee
VP
1952
1953 /* Determine if the watchpoint is within scope. */
1954 if (b->exp_valid_block == NULL)
705b6305 1955 within_current_scope = true;
a5606eee
VP
1956 else
1957 {
bd2b40ac 1958 frame_info_ptr fi = get_current_frame ();
b5db5dfc
UW
1959 struct gdbarch *frame_arch = get_frame_arch (fi);
1960 CORE_ADDR frame_pc = get_frame_pc (fi);
1961
c9cf6e20
MG
1962 /* If we're at a point where the stack has been destroyed
1963 (e.g. in a function epilogue), unwinding may not work
1964 properly. Do not attempt to recreate locations at this
b5db5dfc 1965 point. See similar comments in watchpoint_check. */
c9cf6e20 1966 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1967 return;
66076460
DJ
1968
1969 /* Save the current frame's ID so we can restore it after
dda83cd7 1970 evaluating the watchpoint expression on its own frame. */
66076460 1971 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1972 took a frame parameter, so that we didn't have to change the
1973 selected frame. */
705b6305 1974 restore_frame.emplace ();
66076460 1975
a5606eee
VP
1976 fi = frame_find_by_id (b->watchpoint_frame);
1977 within_current_scope = (fi != NULL);
1978 if (within_current_scope)
1979 select_frame (fi);
1980 }
1981
b5db5dfc
UW
1982 /* We don't free locations. They are stored in the bp_location array
1983 and update_global_location_list will eventually delete them and
1984 remove breakpoints if needed. */
c1fc2657 1985 b->loc = NULL;
b5db5dfc 1986
a5606eee
VP
1987 if (within_current_scope && reparse)
1988 {
bbc13ae3 1989 const char *s;
d63d0675 1990
4d01a485 1991 b->exp.reset ();
a4c50be3
TT
1992 s = (b->exp_string_reparse
1993 ? b->exp_string_reparse.get ()
1994 : b->exp_string.get ());
1bb9788d 1995 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1996 /* If the meaning of expression itself changed, the old value is
1997 no longer relevant. We don't want to report a watchpoint hit
1998 to the user when the old value and the new value may actually
1999 be completely different objects. */
fa4727a6 2000 b->val = NULL;
4c1d86d9 2001 b->val_valid = false;
60e1c644
PA
2002
2003 /* Note that unlike with breakpoints, the watchpoint's condition
2004 expression is stored in the breakpoint object, not in the
2005 locations (re)created below. */
c1fc2657 2006 if (b->cond_string != NULL)
60e1c644 2007 {
4d01a485 2008 b->cond_exp.reset ();
60e1c644 2009
6f781ee3 2010 s = b->cond_string.get ();
1bb9788d 2011 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 2012 }
a5606eee 2013 }
a5606eee
VP
2014
2015 /* If we failed to parse the expression, for example because
2016 it refers to a global variable in a not-yet-loaded shared library,
2017 don't try to insert watchpoint. We don't automatically delete
2018 such watchpoint, though, since failure to parse expression
2019 is different from out-of-scope watchpoint. */
55f6301a 2020 if (!target_has_execution ())
2d134ed3
PA
2021 {
2022 /* Without execution, memory can't change. No use to try and
2023 set watchpoint locations. The watchpoint will be reset when
2024 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
2025 if (!can_use_hw_watchpoints)
2026 {
c47614fe 2027 if (b->works_in_software_mode ())
c1fc2657 2028 b->type = bp_watchpoint;
e8369a73 2029 else
638aa5a1
AB
2030 error (_("Can't set read/access watchpoint when "
2031 "hardware watchpoints are disabled."));
e8369a73 2032 }
2d134ed3
PA
2033 }
2034 else if (within_current_scope && b->exp)
a5606eee 2035 {
a6535de1 2036 std::vector<value_ref_ptr> val_chain;
8d49165d 2037 struct value *v, *result;
2d134ed3 2038 struct program_space *frame_pspace;
a5606eee 2039
1eaebe02 2040 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 2041 &val_chain, false);
a5606eee 2042
a5606eee
VP
2043 /* Avoid setting b->val if it's already set. The meaning of
2044 b->val is 'the last value' user saw, and we should update
2045 it only if we reported that last value to user. As it
9c06b0b4
TJB
2046 happens, the code that reports it updates b->val directly.
2047 We don't keep track of the memory value for masked
2048 watchpoints. */
c1fc2657 2049 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 2050 {
bb9d5f81 2051 if (b->val_bitsize != 0)
850645cf
TT
2052 v = extract_bitfield_from_watchpoint_value (b, v);
2053 b->val = release_value (v);
4c1d86d9 2054 b->val_valid = true;
fa4727a6 2055 }
a5606eee 2056
2d134ed3
PA
2057 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2058
a5606eee 2059 /* Look at each value on the value chain. */
a6535de1
TT
2060 gdb_assert (!val_chain.empty ());
2061 for (const value_ref_ptr &iter : val_chain)
a5606eee 2062 {
a6535de1
TT
2063 v = iter.get ();
2064
a5606eee
VP
2065 /* If it's a memory location, and GDB actually needed
2066 its contents to evaluate the expression, then we
fa4727a6
DJ
2067 must watch it. If the first value returned is
2068 still lazy, that means an error occurred reading it;
2069 watch it anyway in case it becomes readable. */
a5606eee 2070 if (VALUE_LVAL (v) == lval_memory
a6535de1 2071 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
2072 {
2073 struct type *vtype = check_typedef (value_type (v));
7270d8f2 2074
a5606eee
VP
2075 /* We only watch structs and arrays if user asked
2076 for it explicitly, never if they just happen to
2077 appear in the middle of some value chain. */
fa4727a6 2078 if (v == result
78134374
SM
2079 || (vtype->code () != TYPE_CODE_STRUCT
2080 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
2081 {
2082 CORE_ADDR addr;
f486487f 2083 enum target_hw_bp_type type;
a5606eee 2084 struct bp_location *loc, **tmp;
bb9d5f81
PP
2085 int bitpos = 0, bitsize = 0;
2086
2087 if (value_bitsize (v) != 0)
2088 {
2089 /* Extract the bit parameters out from the bitfield
2090 sub-expression. */
2091 bitpos = value_bitpos (v);
2092 bitsize = value_bitsize (v);
2093 }
2094 else if (v == result && b->val_bitsize != 0)
2095 {
2096 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2097 lvalue whose bit parameters are saved in the fields
2098 VAL_BITPOS and VAL_BITSIZE. */
2099 bitpos = b->val_bitpos;
2100 bitsize = b->val_bitsize;
2101 }
a5606eee 2102
42ae5230 2103 addr = value_address (v);
bb9d5f81
PP
2104 if (bitsize != 0)
2105 {
2106 /* Skip the bytes that don't contain the bitfield. */
2107 addr += bitpos / 8;
2108 }
2109
a5606eee 2110 type = hw_write;
c1fc2657 2111 if (b->type == bp_read_watchpoint)
a5606eee 2112 type = hw_read;
c1fc2657 2113 else if (b->type == bp_access_watchpoint)
a5606eee 2114 type = hw_access;
3a5c3e22 2115
ee89d0a4 2116 loc = b->allocate_location ();
c1fc2657 2117 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2118 ;
2119 *tmp = loc;
8ee511af 2120 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
2121
2122 loc->pspace = frame_pspace;
f17d9474 2123 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
2124
2125 if (bitsize != 0)
2126 {
2127 /* Just cover the bytes that make up the bitfield. */
2128 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2129 }
2130 else
df86565b 2131 loc->length = value_type (v)->length ();
bb9d5f81 2132
a5606eee
VP
2133 loc->watchpoint_type = type;
2134 }
2135 }
9fa40276
TJB
2136 }
2137
2138 /* Change the type of breakpoint between hardware assisted or
bc45f536
CL
2139 an ordinary watchpoint depending on the hardware support and
2140 free hardware slots. Recheck the number of free hardware slots
2141 as the value chain may have changed. */
9fa40276 2142 {
e09342b5 2143 int reg_cnt;
9fa40276 2144 enum bp_loc_type loc_type;
a5606eee 2145
a9634178 2146 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2147
2148 if (reg_cnt)
9fa40276
TJB
2149 {
2150 int i, target_resources_ok, other_type_used;
a1398e0c 2151 enum bptype type;
9fa40276 2152
a9634178
TJB
2153 /* Use an exact watchpoint when there's only one memory region to be
2154 watched, and only one debug register is needed to watch it. */
2155 b->exact = target_exact_watchpoints && reg_cnt == 1;
2156
9fa40276 2157 /* We need to determine how many resources are already
e09342b5
TJB
2158 used for all other hardware watchpoints plus this one
2159 to see if we still have enough resources to also fit
a1398e0c
PA
2160 this watchpoint in as well. */
2161
2162 /* If this is a software watchpoint, we try to turn it
2163 to a hardware one -- count resources as if B was of
2164 hardware watchpoint type. */
c1fc2657 2165 type = b->type;
a1398e0c
PA
2166 if (type == bp_watchpoint)
2167 type = bp_hardware_watchpoint;
2168
2169 /* This watchpoint may or may not have been placed on
2170 the list yet at this point (it won't be in the list
2171 if we're trying to create it for the first time,
2172 through watch_command), so always account for it
2173 manually. */
2174
2175 /* Count resources used by all watchpoints except B. */
c1fc2657 2176 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2177
2178 /* Add in the resources needed for B. */
c1fc2657 2179 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2180
2181 target_resources_ok
2182 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2183 if (target_resources_ok <= 0)
a9634178 2184 {
5a61e176 2185 bool sw_mode = b->works_in_software_mode ();
9c06b0b4
TJB
2186
2187 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2188 error (_("Target does not support this type of "
2189 "hardware watchpoint."));
9c06b0b4
TJB
2190 else if (target_resources_ok < 0 && !sw_mode)
2191 error (_("There are not enough available hardware "
2192 "resources for this watchpoint."));
a1398e0c
PA
2193
2194 /* Downgrade to software watchpoint. */
c1fc2657 2195 b->type = bp_watchpoint;
a1398e0c
PA
2196 }
2197 else
2198 {
2199 /* If this was a software watchpoint, we've just
2200 found we have enough resources to turn it to a
2201 hardware watchpoint. Otherwise, this is a
2202 nop. */
c1fc2657 2203 b->type = type;
a9634178 2204 }
9fa40276 2205 }
c47614fe 2206 else if (!b->works_in_software_mode ())
638aa5a1
AB
2207 {
2208 if (!can_use_hw_watchpoints)
2209 error (_("Can't set read/access watchpoint when "
2210 "hardware watchpoints are disabled."));
2211 else
2212 error (_("Expression cannot be implemented with "
2213 "read/access watchpoint."));
2214 }
9fa40276 2215 else
c1fc2657 2216 b->type = bp_watchpoint;
9fa40276 2217
6e14e441 2218 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
9fa40276 2219 : bp_loc_hardware_watchpoint);
40cb8ca5 2220 for (bp_location *bl : b->locations ())
9fa40276
TJB
2221 bl->loc_type = loc_type;
2222 }
2223
c7437ca6
PA
2224 /* If a software watchpoint is not watching any memory, then the
2225 above left it without any location set up. But,
2226 bpstat_stop_status requires a location to be able to report
2227 stops, so make sure there's at least a dummy one. */
c1fc2657 2228 if (b->type == bp_watchpoint && b->loc == NULL)
6e14e441 2229 add_dummy_location (b, frame_pspace);
a5606eee
VP
2230 }
2231 else if (!within_current_scope)
7270d8f2 2232 {
6cb06a8c 2233 gdb_printf (_("\
ac74f770
MS
2234Watchpoint %d deleted because the program has left the block\n\
2235in which its expression is valid.\n"),
6cb06a8c 2236 b->number);
d0fb5eae 2237 watchpoint_del_at_next_stop (b);
7270d8f2 2238 }
7270d8f2
OF
2239}
2240
d8de7963 2241/* Returns true iff breakpoint location should be
1e4d1764
YQ
2242 inserted in the inferior. We don't differentiate the type of BL's owner
2243 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2244 breakpoint_ops is not defined, because in insert_bp_location,
2245 tracepoint's insert_location will not be called. */
d8de7963
AB
2246
2247static bool
35df4500 2248should_be_inserted (struct bp_location *bl)
74960c60 2249{
35df4500 2250 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
d8de7963 2251 return false;
74960c60 2252
35df4500 2253 if (bl->owner->disposition == disp_del_at_next_stop)
d8de7963 2254 return false;
74960c60 2255
b5fa468f
TBA
2256 if (!bl->enabled || bl->disabled_by_cond
2257 || bl->shlib_disabled || bl->duplicate)
d8de7963 2258 return false;
74960c60 2259
f8eba3c6 2260 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
d8de7963 2261 return false;
f8eba3c6 2262
56710373
PA
2263 /* This is set for example, when we're attached to the parent of a
2264 vfork, and have detached from the child. The child is running
2265 free, and we expect it to do an exec or exit, at which point the
2266 OS makes the parent schedulable again (and the target reports
2267 that the vfork is done). Until the child is done with the shared
2268 memory region, do not insert breakpoints in the parent, otherwise
2269 the child could still trip on the parent's breakpoints. Since
2270 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2271 if (bl->pspace->breakpoints_not_allowed)
d8de7963 2272 return false;
56710373 2273
31e77af2 2274 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2275 location, except if the breakpoint is a single-step breakpoint,
2276 and the breakpoint's thread is the thread which is stepping past
2277 a breakpoint. */
31e77af2
PA
2278 if ((bl->loc_type == bp_loc_software_breakpoint
2279 || bl->loc_type == bp_loc_hardware_breakpoint)
2280 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2281 bl->address)
2282 /* The single-step breakpoint may be inserted at the location
2283 we're trying to step if the instruction branches to itself.
2284 However, the instruction won't be executed at all and it may
2285 break the semantics of the instruction, for example, the
2286 instruction is a conditional branch or updates some flags.
2287 We can't fix it unless GDB is able to emulate the instruction
2288 or switch to displaced stepping. */
2289 && !(bl->owner->type == bp_single_step
2290 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2291 {
1eb8556f
SM
2292 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2293 paddress (bl->gdbarch, bl->address));
d8de7963 2294 return false;
e558d7c1 2295 }
31e77af2 2296
963f9c80
PA
2297 /* Don't insert watchpoints if we're trying to step past the
2298 instruction that triggered one. */
2299 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2300 && stepping_past_nonsteppable_watchpoint ())
2301 {
1eb8556f 2302 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2303 "skipping watchpoint at %s:%d",
1eb8556f 2304 paddress (bl->gdbarch, bl->address), bl->length);
d8de7963 2305 return false;
963f9c80
PA
2306 }
2307
d8de7963 2308 return true;
74960c60
VP
2309}
2310
934709f0
PW
2311/* Same as should_be_inserted but does the check assuming
2312 that the location is not duplicated. */
2313
aaa141a0 2314static bool
934709f0
PW
2315unduplicated_should_be_inserted (struct bp_location *bl)
2316{
aaa141a0
AB
2317 scoped_restore restore_bl_duplicate
2318 = make_scoped_restore (&bl->duplicate, 0);
934709f0 2319
aaa141a0 2320 return should_be_inserted (bl);
934709f0
PW
2321}
2322
b775012e
LM
2323/* Parses a conditional described by an expression COND into an
2324 agent expression bytecode suitable for evaluation
2325 by the bytecode interpreter. Return NULL if there was
2326 any error during parsing. */
2327
833177a4 2328static agent_expr_up
b775012e
LM
2329parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2330{
833177a4 2331 if (cond == NULL)
b775012e
LM
2332 return NULL;
2333
833177a4
PA
2334 agent_expr_up aexpr;
2335
b775012e
LM
2336 /* We don't want to stop processing, so catch any errors
2337 that may show up. */
a70b8144 2338 try
b775012e 2339 {
036e657b 2340 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2341 }
2342
230d2906 2343 catch (const gdb_exception_error &ex)
b775012e
LM
2344 {
2345 /* If we got here, it means the condition could not be parsed to a valid
2346 bytecode expression and thus can't be evaluated on the target's side.
2347 It's no use iterating through the conditions. */
b775012e
LM
2348 }
2349
2350 /* We have a valid agent expression. */
2351 return aexpr;
2352}
2353
2354/* Based on location BL, create a list of breakpoint conditions to be
2355 passed on to the target. If we have duplicated locations with different
2356 conditions, we will add such conditions to the list. The idea is that the
2357 target will evaluate the list of conditions and will only notify GDB when
2358 one of them is true. */
2359
2360static void
2361build_target_condition_list (struct bp_location *bl)
2362{
d8de7963 2363 bool null_condition_or_parse_error = false;
b775012e 2364 int modified = bl->needs_update;
b775012e 2365
8b4f3082 2366 /* Release conditions left over from a previous insert. */
3cde5c42 2367 bl->target_info.conditions.clear ();
8b4f3082 2368
b775012e
LM
2369 /* This is only meaningful if the target is
2370 evaluating conditions and if the user has
2371 opted for condition evaluation on the target's
2372 side. */
2373 if (gdb_evaluates_breakpoint_condition_p ()
2374 || !target_supports_evaluation_of_breakpoint_conditions ())
2375 return;
2376
e0d9a270
SM
2377 auto loc_range = all_bp_locations_at_addr (bl->address);
2378
b775012e 2379 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2380 conditions or conditions that fail to parse to a valid agent
2381 expression bytecode. If any of these happen, then it's no use to
2382 send conditions to the target since this location will always
2383 trigger and generate a response back to GDB. Note we consider
2384 all locations at the same address irrespective of type, i.e.,
2385 even if the locations aren't considered duplicates (e.g.,
2386 software breakpoint and hardware breakpoint at the same
2387 address). */
e0d9a270 2388 for (bp_location *loc : loc_range)
b775012e 2389 {
b775012e
LM
2390 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2391 {
2392 if (modified)
2393 {
b775012e
LM
2394 /* Re-parse the conditions since something changed. In that
2395 case we already freed the condition bytecodes (see
2396 force_breakpoint_reinsertion). We just
2397 need to parse the condition to bytecodes again. */
833177a4
PA
2398 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2399 loc->cond.get ());
b775012e
LM
2400 }
2401
2402 /* If we have a NULL bytecode expression, it means something
2403 went wrong or we have a null condition expression. */
2404 if (!loc->cond_bytecode)
2405 {
d8de7963 2406 null_condition_or_parse_error = true;
b775012e
LM
2407 break;
2408 }
2409 }
2410 }
2411
2412 /* If any of these happened, it means we will have to evaluate the conditions
2413 for the location's address on gdb's side. It is no use keeping bytecodes
2414 for all the other duplicate locations, thus we free all of them here.
2415
2416 This is so we have a finer control over which locations' conditions are
2417 being evaluated by GDB or the remote stub. */
2418 if (null_condition_or_parse_error)
2419 {
e0d9a270 2420 for (bp_location *loc : loc_range)
b775012e 2421 {
b775012e
LM
2422 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2423 {
2424 /* Only go as far as the first NULL bytecode is
2425 located. */
2426 if (!loc->cond_bytecode)
2427 return;
2428
833177a4 2429 loc->cond_bytecode.reset ();
b775012e
LM
2430 }
2431 }
2432 }
2433
7f32a4d5
PA
2434 /* No NULL conditions or failed bytecode generation. Build a
2435 condition list for this location's address. If we have software
2436 and hardware locations at the same address, they aren't
2437 considered duplicates, but we still marge all the conditions
2438 anyway, as it's simpler, and doesn't really make a practical
2439 difference. */
e0d9a270
SM
2440 for (bp_location *loc : loc_range)
2441 if (loc->cond
2442 && is_breakpoint (loc->owner)
2443 && loc->pspace->num == bl->pspace->num
2444 && loc->owner->enable_state == bp_enabled
2445 && loc->enabled
2446 && !loc->disabled_by_cond)
2447 {
2448 /* Add the condition to the vector. This will be used later
2449 to send the conditions to the target. */
2450 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2451 }
b775012e
LM
2452
2453 return;
2454}
2455
d3ce09f5
SS
2456/* Parses a command described by string CMD into an agent expression
2457 bytecode suitable for evaluation by the bytecode interpreter.
2458 Return NULL if there was any error during parsing. */
2459
833177a4 2460static agent_expr_up
d3ce09f5
SS
2461parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2462{
bbc13ae3
KS
2463 const char *cmdrest;
2464 const char *format_start, *format_end;
d3ce09f5
SS
2465 struct gdbarch *gdbarch = get_current_arch ();
2466
833177a4 2467 if (cmd == NULL)
d3ce09f5
SS
2468 return NULL;
2469
2470 cmdrest = cmd;
2471
2472 if (*cmdrest == ',')
2473 ++cmdrest;
f1735a53 2474 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2475
2476 if (*cmdrest++ != '"')
2477 error (_("No format string following the location"));
2478
2479 format_start = cmdrest;
2480
8e481c3b 2481 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2482
2483 format_end = cmdrest;
2484
2485 if (*cmdrest++ != '"')
2486 error (_("Bad format string, non-terminated '\"'."));
2487
f1735a53 2488 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2489
2490 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2491 error (_("Invalid argument syntax"));
2492
2493 if (*cmdrest == ',')
2494 cmdrest++;
f1735a53 2495 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2496
2497 /* For each argument, make an expression. */
2498
8e481c3b 2499 std::vector<struct expression *> argvec;
d3ce09f5
SS
2500 while (*cmdrest != '\0')
2501 {
bbc13ae3 2502 const char *cmd1;
d3ce09f5
SS
2503
2504 cmd1 = cmdrest;
4d01a485 2505 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2506 argvec.push_back (expr.release ());
d3ce09f5
SS
2507 cmdrest = cmd1;
2508 if (*cmdrest == ',')
2509 ++cmdrest;
2510 }
2511
833177a4
PA
2512 agent_expr_up aexpr;
2513
d3ce09f5
SS
2514 /* We don't want to stop processing, so catch any errors
2515 that may show up. */
a70b8144 2516 try
d3ce09f5 2517 {
036e657b
JB
2518 aexpr = gen_printf (scope, gdbarch, 0, 0,
2519 format_start, format_end - format_start,
8e481c3b 2520 argvec.size (), argvec.data ());
d3ce09f5 2521 }
230d2906 2522 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2523 {
2524 /* If we got here, it means the command could not be parsed to a valid
2525 bytecode expression and thus can't be evaluated on the target's side.
2526 It's no use iterating through the other commands. */
d3ce09f5 2527 }
492d29ea 2528
d3ce09f5
SS
2529 /* We have a valid agent expression, return it. */
2530 return aexpr;
2531}
2532
2533/* Based on location BL, create a list of breakpoint commands to be
2534 passed on to the target. If we have duplicated locations with
2535 different commands, we will add any such to the list. */
2536
2537static void
2538build_target_command_list (struct bp_location *bl)
2539{
d8de7963 2540 bool null_command_or_parse_error = false;
d3ce09f5 2541 int modified = bl->needs_update;
d3ce09f5 2542
3cde5c42
PA
2543 /* Clear commands left over from a previous insert. */
2544 bl->target_info.tcommands.clear ();
8b4f3082 2545
41fac0cf 2546 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2547 return;
2548
41fac0cf
PA
2549 /* For now, limit to agent-style dprintf breakpoints. */
2550 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2551 return;
2552
e0d9a270
SM
2553 auto loc_range = all_bp_locations_at_addr (bl->address);
2554
7f32a4d5
PA
2555 /* For now, if we have any location at the same address that isn't a
2556 dprintf, don't install the target-side commands, as that would
2557 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2558 control. */
e0d9a270
SM
2559 for (bp_location *loc : loc_range)
2560 if (is_breakpoint (loc->owner)
2561 && loc->pspace->num == bl->pspace->num
2562 && loc->owner->type != bp_dprintf)
2563 return;
41fac0cf 2564
d3ce09f5
SS
2565 /* Do a first pass to check for locations with no assigned
2566 conditions or conditions that fail to parse to a valid agent expression
2567 bytecode. If any of these happen, then it's no use to send conditions
2568 to the target since this location will always trigger and generate a
2569 response back to GDB. */
e0d9a270 2570 for (bp_location *loc : loc_range)
d3ce09f5 2571 {
d3ce09f5
SS
2572 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2573 {
2574 if (modified)
2575 {
d3ce09f5
SS
2576 /* Re-parse the commands since something changed. In that
2577 case we already freed the command bytecodes (see
2578 force_breakpoint_reinsertion). We just
2579 need to parse the command to bytecodes again. */
833177a4
PA
2580 loc->cmd_bytecode
2581 = parse_cmd_to_aexpr (bl->address,
6f781ee3 2582 loc->owner->extra_string.get ());
d3ce09f5
SS
2583 }
2584
2585 /* If we have a NULL bytecode expression, it means something
2586 went wrong or we have a null command expression. */
2587 if (!loc->cmd_bytecode)
2588 {
d8de7963 2589 null_command_or_parse_error = true;
d3ce09f5
SS
2590 break;
2591 }
2592 }
2593 }
2594
2595 /* If anything failed, then we're not doing target-side commands,
2596 and so clean up. */
2597 if (null_command_or_parse_error)
2598 {
e0d9a270
SM
2599 for (bp_location *loc : loc_range)
2600 if (is_breakpoint (loc->owner)
2601 && loc->pspace->num == bl->pspace->num)
2602 {
2603 /* Only go as far as the first NULL bytecode is
2604 located. */
2605 if (loc->cmd_bytecode == NULL)
2606 return;
d3ce09f5 2607
e0d9a270
SM
2608 loc->cmd_bytecode.reset ();
2609 }
d3ce09f5
SS
2610 }
2611
7f32a4d5
PA
2612 /* No NULL commands or failed bytecode generation. Build a command
2613 list for all duplicate locations at this location's address.
2614 Note that here we must care for whether the breakpoint location
2615 types are considered duplicates, otherwise, say, if we have a
2616 software and hardware location at the same address, the target
2617 could end up running the commands twice. For the moment, we only
2618 support targets-side commands with dprintf, but it doesn't hurt
2619 to be pedantically correct in case that changes. */
e0d9a270
SM
2620 for (bp_location *loc : loc_range)
2621 if (breakpoint_locations_match (bl, loc)
2622 && loc->owner->extra_string
2623 && is_breakpoint (loc->owner)
2624 && loc->pspace->num == bl->pspace->num
2625 && loc->owner->enable_state == bp_enabled
2626 && loc->enabled
2627 && !loc->disabled_by_cond)
2628 {
2629 /* Add the command to the vector. This will be used later
2630 to send the commands to the target. */
2631 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2632 }
d3ce09f5
SS
2633
2634 bl->target_info.persist = 0;
2635 /* Maybe flag this location as persistent. */
2636 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2637 bl->target_info.persist = 1;
2638}
2639
833b7ab5
YQ
2640/* Return the kind of breakpoint on address *ADDR. Get the kind
2641 of breakpoint according to ADDR except single-step breakpoint.
2642 Get the kind of single-step breakpoint according to the current
2643 registers state. */
cd6c3b4f
YQ
2644
2645static int
b2ff9ed3 2646breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
cd6c3b4f 2647{
833b7ab5
YQ
2648 if (bl->owner->type == bp_single_step)
2649 {
2650 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2651 struct regcache *regcache;
2652
00431a78 2653 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2654
2655 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2656 regcache, addr);
2657 }
2658 else
2659 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2660}
2661
47e2c30a
TV
2662/* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2663 E is either the currently handled exception, or a copy, or a sliced copy,
2664 so we can't rethrow that one, but we can use it to inspect the properties
2665 of the currently handled exception. */
2666
2667static void
2668rethrow_on_target_close_error (const gdb_exception &e)
2669{
2670 if (e.reason == 0)
2671 return;
2672 /* Can't set the breakpoint. */
2673
2674 if (e.error != TARGET_CLOSE_ERROR)
2675 return;
2676
2677 /* If the target has closed then it will have deleted any breakpoints
2678 inserted within the target inferior, as a result any further attempts
2679 to interact with the breakpoint objects is not possible. Just rethrow
2680 the error. Don't use e to rethrow, to prevent object slicing of the
2681 exception. */
2682 throw;
2683}
b2829fcf 2684
35df4500
TJB
2685/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2686 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2687 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2688 Returns 0 for success, 1 if the bp_location type is not supported or
2689 -1 for failure.
879bfdc2 2690
4a64f543
MS
2691 NOTE drow/2003-09-09: This routine could be broken down to an
2692 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2693static int
35df4500 2694insert_bp_location (struct bp_location *bl,
26bb91f3 2695 struct ui_file *tmp_error_stream,
3fbb6ffa 2696 int *disabled_breaks,
dd61ec5c
MW
2697 int *hw_breakpoint_error,
2698 int *hw_bp_error_explained_already)
879bfdc2 2699{
cc06b668 2700 gdb_exception bp_excpt;
879bfdc2 2701
b775012e 2702 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2703 return 0;
2704
35c63cd8
JB
2705 /* Note we don't initialize bl->target_info, as that wipes out
2706 the breakpoint location's shadow_contents if the breakpoint
2707 is still inserted at that location. This in turn breaks
2708 target_read_memory which depends on these buffers when
2709 a memory read is requested at the breakpoint location:
2710 Once the target_info has been wiped, we fail to see that
2711 we have a breakpoint inserted at that address and thus
2712 read the breakpoint instead of returning the data saved in
2713 the breakpoint location's shadow contents. */
0d5ed153 2714 bl->target_info.reqstd_address = bl->address;
35df4500 2715 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2716 bl->target_info.length = bl->length;
8181d85f 2717
b775012e
LM
2718 /* When working with target-side conditions, we must pass all the conditions
2719 for the same breakpoint address down to the target since GDB will not
2720 insert those locations. With a list of breakpoint conditions, the target
2721 can decide when to stop and notify GDB. */
2722
2723 if (is_breakpoint (bl->owner))
2724 {
2725 build_target_condition_list (bl);
d3ce09f5
SS
2726 build_target_command_list (bl);
2727 /* Reset the modification marker. */
b775012e
LM
2728 bl->needs_update = 0;
2729 }
2730
7f32a4d5
PA
2731 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2732 set at a read-only address, then a breakpoint location will have
2733 been changed to hardware breakpoint before we get here. If it is
2734 "off" however, error out before actually trying to insert the
2735 breakpoint, with a nicer error message. */
35df4500 2736 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2737 && !automatic_hardware_breakpoints)
879bfdc2 2738 {
7f32a4d5 2739 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2740
7f32a4d5
PA
2741 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2742 {
6cb06a8c
TT
2743 gdb_printf (tmp_error_stream,
2744 _("Cannot insert breakpoint %d.\n"
2745 "Cannot set software breakpoint "
2746 "at read-only address %s\n"),
2747 bl->owner->number,
2748 paddress (bl->gdbarch, bl->address));
7f32a4d5 2749 return 1;
765dc015 2750 }
7f32a4d5
PA
2751 }
2752
2753 if (bl->loc_type == bp_loc_software_breakpoint
2754 || bl->loc_type == bp_loc_hardware_breakpoint)
2755 {
879bfdc2
DJ
2756 /* First check to see if we have to handle an overlay. */
2757 if (overlay_debugging == ovly_off
35df4500
TJB
2758 || bl->section == NULL
2759 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2760 {
2761 /* No overlay handling: just set the breakpoint. */
a70b8144 2762 try
dd61ec5c 2763 {
0000e5cc
PA
2764 int val;
2765
c47614fe 2766 val = bl->owner->insert_location (bl);
0000e5cc 2767 if (val)
688fca4f 2768 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2769 }
94aeb44b 2770 catch (gdb_exception &e)
dd61ec5c 2771 {
47e2c30a 2772 rethrow_on_target_close_error (e);
94aeb44b 2773 bp_excpt = std::move (e);
dd61ec5c 2774 }
879bfdc2
DJ
2775 }
2776 else
2777 {
4a64f543 2778 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2779 Shall we set a breakpoint at the LMA? */
2780 if (!overlay_events_enabled)
2781 {
2782 /* Yes -- overlay event support is not active,
2783 so we must try to set a breakpoint at the LMA.
2784 This will not work for a hardware breakpoint. */
35df4500 2785 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2786 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2787 bl->owner->number);
879bfdc2
DJ
2788 else
2789 {
35df4500
TJB
2790 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2791 bl->section);
879bfdc2 2792 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2793 bl->overlay_target_info = bl->target_info;
0d5ed153 2794 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2795
2796 /* No overlay handling: just set the breakpoint. */
a70b8144 2797 try
0000e5cc
PA
2798 {
2799 int val;
2800
579c6ad9 2801 bl->overlay_target_info.kind
cd6c3b4f
YQ
2802 = breakpoint_kind (bl, &addr);
2803 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2804 val = target_insert_breakpoint (bl->gdbarch,
2805 &bl->overlay_target_info);
2806 if (val)
688fca4f
PA
2807 bp_excpt
2808 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2809 }
94aeb44b 2810 catch (gdb_exception &e)
0000e5cc 2811 {
47e2c30a 2812 rethrow_on_target_close_error (e);
94aeb44b 2813 bp_excpt = std::move (e);
0000e5cc
PA
2814 }
2815
688fca4f 2816 if (bp_excpt.reason != 0)
6cb06a8c
TT
2817 gdb_printf (tmp_error_stream,
2818 "Overlay breakpoint %d "
2819 "failed: in ROM?\n",
2820 bl->owner->number);
879bfdc2
DJ
2821 }
2822 }
2823 /* Shall we set a breakpoint at the VMA? */
35df4500 2824 if (section_is_mapped (bl->section))
879bfdc2
DJ
2825 {
2826 /* Yes. This overlay section is mapped into memory. */
a70b8144 2827 try
dda83cd7 2828 {
0000e5cc
PA
2829 int val;
2830
c47614fe 2831 val = bl->owner->insert_location (bl);
0000e5cc 2832 if (val)
688fca4f 2833 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2834 }
94aeb44b 2835 catch (gdb_exception &e)
dda83cd7 2836 {
47e2c30a 2837 rethrow_on_target_close_error (e);
94aeb44b 2838 bp_excpt = std::move (e);
dda83cd7 2839 }
879bfdc2
DJ
2840 }
2841 else
2842 {
2843 /* No. This breakpoint will not be inserted.
2844 No error, but do not mark the bp as 'inserted'. */
2845 return 0;
2846 }
2847 }
2848
688fca4f 2849 if (bp_excpt.reason != 0)
879bfdc2
DJ
2850 {
2851 /* Can't set the breakpoint. */
80dc83fd
AB
2852 gdb_assert (bl->owner != nullptr);
2853
0000e5cc
PA
2854 /* In some cases, we might not be able to insert a
2855 breakpoint in a shared library that has already been
2856 removed, but we have not yet processed the shlib unload
2857 event. Unfortunately, some targets that implement
076855f9
PA
2858 breakpoint insertion themselves can't tell why the
2859 breakpoint insertion failed (e.g., the remote target
2860 doesn't define error codes), so we must treat generic
2861 errors as memory errors. */
688fca4f
PA
2862 if (bp_excpt.reason == RETURN_ERROR
2863 && (bp_excpt.error == GENERIC_ERROR
2864 || bp_excpt.error == MEMORY_ERROR)
076855f9 2865 && bl->loc_type == bp_loc_software_breakpoint
08351840 2866 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2867 || shared_objfile_contains_address_p (bl->pspace,
2868 bl->address)))
879bfdc2 2869 {
4a64f543 2870 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2871 bl->shlib_disabled = 1;
76727919 2872 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2873 if (!*disabled_breaks)
2874 {
6cb06a8c
TT
2875 gdb_printf (tmp_error_stream,
2876 "Cannot insert breakpoint %d.\n",
2877 bl->owner->number);
2878 gdb_printf (tmp_error_stream,
2879 "Temporarily disabling shared "
2880 "library breakpoints:\n");
3fbb6ffa
TJB
2881 }
2882 *disabled_breaks = 1;
6cb06a8c
TT
2883 gdb_printf (tmp_error_stream,
2884 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2885 return 0;
879bfdc2
DJ
2886 }
2887 else
879bfdc2 2888 {
35df4500 2889 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2890 {
0000e5cc 2891 *hw_breakpoint_error = 1;
688fca4f 2892 *hw_bp_error_explained_already = bp_excpt.message != NULL;
6cb06a8c
TT
2893 gdb_printf (tmp_error_stream,
2894 "Cannot insert hardware breakpoint %d%s",
2895 bl->owner->number,
2896 bp_excpt.message ? ":" : ".\n");
dda83cd7 2897 if (bp_excpt.message != NULL)
6cb06a8c
TT
2898 gdb_printf (tmp_error_stream, "%s.\n",
2899 bp_excpt.what ());
879bfdc2
DJ
2900 }
2901 else
2902 {
688fca4f 2903 if (bp_excpt.message == NULL)
0000e5cc 2904 {
1ccbe998 2905 std::string message
0000e5cc
PA
2906 = memory_error_message (TARGET_XFER_E_IO,
2907 bl->gdbarch, bl->address);
0000e5cc 2908
6cb06a8c
TT
2909 gdb_printf (tmp_error_stream,
2910 "Cannot insert breakpoint %d.\n"
2911 "%s\n",
2912 bl->owner->number, message.c_str ());
0000e5cc
PA
2913 }
2914 else
2915 {
6cb06a8c
TT
2916 gdb_printf (tmp_error_stream,
2917 "Cannot insert breakpoint %d: %s\n",
2918 bl->owner->number,
2919 bp_excpt.what ());
0000e5cc 2920 }
879bfdc2 2921 }
0000e5cc 2922 return 1;
879bfdc2
DJ
2923
2924 }
2925 }
2926 else
35df4500 2927 bl->inserted = 1;
879bfdc2 2928
0000e5cc 2929 return 0;
879bfdc2
DJ
2930 }
2931
35df4500 2932 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2933 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2934 watchpoints. It's not clear that it's necessary... */
35df4500 2935 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2936 {
0000e5cc
PA
2937 int val;
2938
c47614fe 2939 val = bl->owner->insert_location (bl);
85d721b8
PA
2940
2941 /* If trying to set a read-watchpoint, and it turns out it's not
2942 supported, try emulating one with an access watchpoint. */
35df4500 2943 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2944 {
85d721b8
PA
2945 /* But don't try to insert it, if there's already another
2946 hw_access location that would be considered a duplicate
2947 of this one. */
48d7020b 2948 for (bp_location *loc : all_bp_locations ())
35df4500 2949 if (loc != bl
85d721b8 2950 && loc->watchpoint_type == hw_access
35df4500 2951 && watchpoint_locations_match (bl, loc))
85d721b8 2952 {
35df4500
TJB
2953 bl->duplicate = 1;
2954 bl->inserted = 1;
2955 bl->target_info = loc->target_info;
2956 bl->watchpoint_type = hw_access;
85d721b8
PA
2957 val = 0;
2958 break;
2959 }
2960
2961 if (val == 1)
2962 {
77b06cd7 2963 bl->watchpoint_type = hw_access;
c47614fe 2964 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2965
2966 if (val)
2967 /* Back to the original value. */
2968 bl->watchpoint_type = hw_read;
85d721b8
PA
2969 }
2970 }
2971
35df4500 2972 bl->inserted = (val == 0);
879bfdc2
DJ
2973 }
2974
35df4500 2975 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2976 {
0000e5cc
PA
2977 int val;
2978
c47614fe 2979 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2980 if (val)
2981 {
2982 bl->owner->enable_state = bp_disabled;
2983
2984 if (val == 1)
2985 warning (_("\
2986Error inserting catchpoint %d: Your system does not support this type\n\
2987of catchpoint."), bl->owner->number);
2988 else
2989 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2990 }
2991
2992 bl->inserted = (val == 0);
1640b821
DJ
2993
2994 /* We've already printed an error message if there was a problem
2995 inserting this catchpoint, and we've disabled the catchpoint,
2996 so just return success. */
2997 return 0;
879bfdc2
DJ
2998 }
2999
3000 return 0;
3001}
3002
6c95b8df
PA
3003/* This function is called when program space PSPACE is about to be
3004 deleted. It takes care of updating breakpoints to not reference
3005 PSPACE anymore. */
3006
3007void
3008breakpoint_program_space_exit (struct program_space *pspace)
3009{
6c95b8df 3010 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
3011 for (breakpoint *b : all_breakpoints_safe ())
3012 if (b->pspace == pspace)
3013 delete_breakpoint (b);
6c95b8df
PA
3014
3015 /* Breakpoints set through other program spaces could have locations
3016 bound to PSPACE as well. Remove those. */
48d7020b 3017 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
3018 {
3019 struct bp_location *tmp;
3020
3021 if (loc->pspace == pspace)
3022 {
2bdf28a0 3023 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3024 if (loc->owner->loc == loc)
3025 loc->owner->loc = loc->next;
3026 else
3027 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3028 if (tmp->next == loc)
3029 {
3030 tmp->next = loc->next;
3031 break;
3032 }
3033 }
3034 }
3035
3036 /* Now update the global location list to permanently delete the
3037 removed locations above. */
44702360 3038 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3039}
3040
74960c60
VP
3041/* Make sure all breakpoints are inserted in inferior.
3042 Throws exception on any error.
3043 A breakpoint that is already inserted won't be inserted
3044 again, so calling this function twice is safe. */
3045void
3046insert_breakpoints (void)
3047{
43892fdf 3048 for (breakpoint *bpt : all_breakpoints ())
74960c60 3049 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3050 {
3051 struct watchpoint *w = (struct watchpoint *) bpt;
3052
d8de7963 3053 update_watchpoint (w, false /* don't reparse. */);
3a5c3e22 3054 }
74960c60 3055
04086b45
PA
3056 /* Updating watchpoints creates new locations, so update the global
3057 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3058 ignore breakpoints_always_inserted_mode. Also,
3059 update_global_location_list tries to "upgrade" software
3060 breakpoints to hardware breakpoints to handle "set breakpoint
3061 auto-hw", so we need to call it even if we don't have new
3062 locations. */
04086b45 3063 update_global_location_list (UGLL_INSERT);
74960c60
VP
3064}
3065
b775012e
LM
3066/* This is used when we need to synch breakpoint conditions between GDB and the
3067 target. It is the case with deleting and disabling of breakpoints when using
3068 always-inserted mode. */
3069
3070static void
3071update_inserted_breakpoint_locations (void)
3072{
b775012e
LM
3073 int error_flag = 0;
3074 int val = 0;
3075 int disabled_breaks = 0;
3076 int hw_breakpoint_error = 0;
dd61ec5c 3077 int hw_bp_details_reported = 0;
b775012e 3078
d7e74731 3079 string_file tmp_error_stream;
b775012e
LM
3080
3081 /* Explicitly mark the warning -- this will only be printed if
3082 there was an error. */
d7e74731 3083 tmp_error_stream.puts ("Warning:\n");
b775012e 3084
5ed8105e 3085 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3086
48d7020b 3087 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3088 {
3089 /* We only want to update software breakpoints and hardware
3090 breakpoints. */
3091 if (!is_breakpoint (bl->owner))
3092 continue;
3093
3094 /* We only want to update locations that are already inserted
3095 and need updating. This is to avoid unwanted insertion during
3096 deletion of breakpoints. */
4daf1902 3097 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3098 continue;
3099
3100 switch_to_program_space_and_thread (bl->pspace);
3101
3102 /* For targets that support global breakpoints, there's no need
3103 to select an inferior to insert breakpoint to. In fact, even
3104 if we aren't attached to any process yet, we should still
3105 insert breakpoints. */
f5656ead 3106 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3107 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3108 continue;
3109
d7e74731 3110 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3111 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3112 if (val)
3113 error_flag = val;
3114 }
3115
3116 if (error_flag)
3117 {
223ffa71 3118 target_terminal::ours_for_output ();
b775012e
LM
3119 error_stream (tmp_error_stream);
3120 }
b775012e
LM
3121}
3122
c30eee59 3123/* Used when starting or continuing the program. */
c906108c 3124
74960c60
VP
3125static void
3126insert_breakpoint_locations (void)
c906108c 3127{
eacd795a 3128 int error_flag = 0;
c906108c 3129 int val = 0;
3fbb6ffa 3130 int disabled_breaks = 0;
81d0cc19 3131 int hw_breakpoint_error = 0;
dd61ec5c 3132 int hw_bp_error_explained_already = 0;
c906108c 3133
d7e74731
PA
3134 string_file tmp_error_stream;
3135
81d0cc19
GS
3136 /* Explicitly mark the warning -- this will only be printed if
3137 there was an error. */
d7e74731 3138 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3139
5ed8105e 3140 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3141
48d7020b 3142 for (bp_location *bl : all_bp_locations ())
879bfdc2 3143 {
b775012e 3144 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3145 continue;
3146
4a64f543
MS
3147 /* There is no point inserting thread-specific breakpoints if
3148 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3149 has BL->OWNER always non-NULL. */
35df4500 3150 if (bl->owner->thread != -1
5d5658a1 3151 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3152 continue;
3153
35df4500 3154 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3155
3156 /* For targets that support global breakpoints, there's no need
3157 to select an inferior to insert breakpoint to. In fact, even
3158 if we aren't attached to any process yet, we should still
3159 insert breakpoints. */
f5656ead 3160 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3161 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3162 continue;
3163
d7e74731 3164 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3165 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3166 if (val)
eacd795a 3167 error_flag = val;
879bfdc2 3168 }
c906108c 3169
4a64f543
MS
3170 /* If we failed to insert all locations of a watchpoint, remove
3171 them, as half-inserted watchpoint is of limited use. */
43892fdf 3172 for (breakpoint *bpt : all_breakpoints ())
a5606eee 3173 {
d8de7963 3174 bool some_failed = false;
a5606eee
VP
3175
3176 if (!is_hardware_watchpoint (bpt))
3177 continue;
3178
d6b74ac4 3179 if (!breakpoint_enabled (bpt))
a5606eee 3180 continue;
74960c60
VP
3181
3182 if (bpt->disposition == disp_del_at_next_stop)
3183 continue;
a5606eee 3184
40cb8ca5 3185 for (bp_location *loc : bpt->locations ())
56710373 3186 if (!loc->inserted && should_be_inserted (loc))
a5606eee 3187 {
d8de7963 3188 some_failed = true;
a5606eee
VP
3189 break;
3190 }
40cb8ca5 3191
a5606eee
VP
3192 if (some_failed)
3193 {
40cb8ca5 3194 for (bp_location *loc : bpt->locations ())
a5606eee 3195 if (loc->inserted)
834c0d03 3196 remove_breakpoint (loc);
a5606eee
VP
3197
3198 hw_breakpoint_error = 1;
d7e74731
PA
3199 tmp_error_stream.printf ("Could not insert "
3200 "hardware watchpoint %d.\n",
3201 bpt->number);
eacd795a 3202 error_flag = -1;
a5606eee
VP
3203 }
3204 }
3205
eacd795a 3206 if (error_flag)
81d0cc19
GS
3207 {
3208 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3209 message about possibly exhausted resources. */
dd61ec5c 3210 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3211 {
d7e74731 3212 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3213You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3214 }
223ffa71 3215 target_terminal::ours_for_output ();
81d0cc19
GS
3216 error_stream (tmp_error_stream);
3217 }
c906108c
SS
3218}
3219
c30eee59
TJB
3220/* Used when the program stops.
3221 Returns zero if successful, or non-zero if there was a problem
3222 removing a breakpoint location. */
3223
c906108c 3224int
fba45db2 3225remove_breakpoints (void)
c906108c 3226{
3a1bae8e 3227 int val = 0;
c906108c 3228
48d7020b 3229 for (bp_location *bl : all_bp_locations ())
1e4d1764 3230 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3231 val |= remove_breakpoint (bl);
01add95b 3232
3a1bae8e 3233 return val;
c906108c
SS
3234}
3235
49fa26b0
PA
3236/* When a thread exits, remove breakpoints that are related to
3237 that thread. */
3238
3239static void
3240remove_threaded_breakpoints (struct thread_info *tp, int silent)
3241{
1428b37a 3242 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3243 {
5d5658a1 3244 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3245 {
3246 b->disposition = disp_del_at_next_stop;
3247
6cb06a8c 3248 gdb_printf (_("\
43792cf0 3249Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
6cb06a8c 3250 b->number, print_thread_id (tp));
49fa26b0
PA
3251
3252 /* Hide it from the user. */
3253 b->number = 0;
3254 }
3255 }
3256}
3257
f3869b1a 3258/* See breakpoint.h. */
6c95b8df 3259
f3869b1a 3260void
00431a78 3261remove_breakpoints_inf (inferior *inf)
6c95b8df 3262{
6c95b8df 3263 int val;
6c95b8df 3264
48d7020b 3265 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3266 {
3267 if (bl->pspace != inf->pspace)
3268 continue;
6c95b8df 3269
01add95b
SM
3270 if (bl->inserted && !bl->target_info.persist)
3271 {
3272 val = remove_breakpoint (bl);
3273 if (val != 0)
3274 return;
3275 }
3276 }
6c95b8df
PA
3277}
3278
e58b0e63
PA
3279static int internal_breakpoint_number = -1;
3280
84f4c1fe
PM
3281/* Set the breakpoint number of B, depending on the value of INTERNAL.
3282 If INTERNAL is non-zero, the breakpoint number will be populated
3283 from internal_breakpoint_number and that variable decremented.
e5dd4106 3284 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3285 breakpoint_count and that value incremented. Internal breakpoints
3286 do not set the internal var bpnum. */
3287static void
3288set_breakpoint_number (int internal, struct breakpoint *b)
3289{
3290 if (internal)
3291 b->number = internal_breakpoint_number--;
3292 else
3293 {
3294 set_breakpoint_count (breakpoint_count + 1);
3295 b->number = breakpoint_count;
3296 }
3297}
3298
829b6b37
TV
3299/* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3300
e62c965a 3301static struct breakpoint *
a6d9a66e 3302create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3303 CORE_ADDR address, enum bptype type)
e62c965a 3304{
752a2291
PA
3305 std::unique_ptr<internal_breakpoint> b
3306 (new internal_breakpoint (gdbarch, type, address));
e62c965a 3307
e62c965a 3308 b->number = internal_breakpoint_number--;
e62c965a 3309
752a2291 3310 return add_to_breakpoint_chain (std::move (b));
e62c965a
PP
3311}
3312
829b6b37
TV
3313/* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3314 GDBARCH. */
3315
3316static struct breakpoint *
3317create_internal_breakpoint (struct gdbarch *gdbarch,
3318 struct bound_minimal_symbol &msym, enum bptype type)
3319{
3320 CORE_ADDR address;
3321
3322 address = msym.value_address ();
3323
3324 address = gdbarch_convert_from_func_ptr_addr
3325 (gdbarch, address, current_inferior ()->top_target ());
3326
3327 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3328 related to addresses in $pc. We're getting the address from the
3329 minimal symbol table. */
3330
3331 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3332 with elsewhere? Needs testing on vax. */
3333
3334 if (gdbarch_skip_entrypoint_p (gdbarch))
3335 address = gdbarch_skip_entrypoint (gdbarch, address);
3336
3337 return create_internal_breakpoint (gdbarch, address, type);
3338}
3339
17450429
PP
3340static const char *const longjmp_names[] =
3341 {
3342 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3343 };
3344#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3345
3346/* Per-objfile data private to breakpoint.c. */
3347struct breakpoint_objfile_data
3348{
3349 /* Minimal symbol for "_ovly_debug_event" (if any). */
f6b3ad54 3350 struct bound_minimal_symbol overlay_msym;
17450429
PP
3351
3352 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
f6b3ad54 3353 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3354
28106bc2 3355 /* True if we have looked for longjmp probes. */
43dce439 3356 int longjmp_searched = 0;
28106bc2 3357
45461e0d
SM
3358 /* SystemTap probe points for longjmp (if any). These are non-owning
3359 references. */
3360 std::vector<probe *> longjmp_probes;
28106bc2 3361
17450429 3362 /* Minimal symbol for "std::terminate()" (if any). */
f6b3ad54 3363 struct bound_minimal_symbol terminate_msym;
17450429
PP
3364
3365 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
f6b3ad54 3366 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3367
3368 /* True if we have looked for exception probes. */
43dce439 3369 int exception_searched = 0;
28106bc2 3370
45461e0d
SM
3371 /* SystemTap probe points for unwinding (if any). These are non-owning
3372 references. */
3373 std::vector<probe *> exception_probes;
17450429
PP
3374};
3375
08b8a139 3376static const registry<objfile>::key<breakpoint_objfile_data>
51d3063a 3377 breakpoint_objfile_key;
17450429
PP
3378
3379/* Minimal symbol not found sentinel. */
3380static struct minimal_symbol msym_not_found;
3381
3382/* Returns TRUE if MSYM point to the "not found" sentinel. */
3383
d8de7963 3384static bool
17450429
PP
3385msym_not_found_p (const struct minimal_symbol *msym)
3386{
3387 return msym == &msym_not_found;
3388}
3389
3390/* Return per-objfile data needed by breakpoint.c.
3391 Allocate the data if necessary. */
3392
3393static struct breakpoint_objfile_data *
3394get_breakpoint_objfile_data (struct objfile *objfile)
3395{
3396 struct breakpoint_objfile_data *bp_objfile_data;
3397
51d3063a 3398 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3399 if (bp_objfile_data == NULL)
51d3063a 3400 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3401 return bp_objfile_data;
3402}
3403
e62c965a 3404static void
af02033e 3405create_overlay_event_breakpoint (void)
e62c965a 3406{
af02033e 3407 const char *const func_name = "_ovly_debug_event";
e62c965a 3408
2030c079 3409 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3410 {
3411 struct breakpoint *b;
17450429
PP
3412 struct breakpoint_objfile_data *bp_objfile_data;
3413 CORE_ADDR addr;
69de3c6a 3414
17450429
PP
3415 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3416
3b7344d5 3417 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3418 continue;
3419
3b7344d5 3420 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3421 {
3b7344d5 3422 struct bound_minimal_symbol m;
17450429
PP
3423
3424 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3425 if (m.minsym == NULL)
17450429
PP
3426 {
3427 /* Avoid future lookups in this objfile. */
3b7344d5 3428 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3429 continue;
3430 }
3431 bp_objfile_data->overlay_msym = m;
3432 }
e62c965a 3433
4aeddc50 3434 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3435 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3436 bp_overlay_event);
40d97ee2 3437 b->locspec = new_explicit_location_spec_function (func_name);
e62c965a 3438
69de3c6a 3439 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3440 {
3441 b->enable_state = bp_enabled;
3442 overlay_events_enabled = 1;
3443 }
69de3c6a
PP
3444 else
3445 {
dda83cd7
SM
3446 b->enable_state = bp_disabled;
3447 overlay_events_enabled = 0;
69de3c6a 3448 }
e62c965a 3449 }
e62c965a
PP
3450}
3451
2a7f6487
TV
3452/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3453 true if a breakpoint was installed. */
3454
3455static bool
3456create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3457{
2a7f6487
TV
3458 struct gdbarch *gdbarch = objfile->arch ();
3459 struct breakpoint_objfile_data *bp_objfile_data
3460 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3461
2a7f6487 3462 if (!bp_objfile_data->longjmp_searched)
94c93c35 3463 {
2a7f6487
TV
3464 std::vector<probe *> ret
3465 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3466
2a7f6487 3467 if (!ret.empty ())
94c93c35 3468 {
2a7f6487
TV
3469 /* We are only interested in checking one element. */
3470 probe *p = ret[0];
0fd8e87f 3471
2a7f6487
TV
3472 if (!p->can_evaluate_arguments ())
3473 {
3474 /* We cannot use the probe interface here,
3475 because it does not know how to evaluate
3476 arguments. */
3477 ret.clear ();
3478 }
3479 }
3480 bp_objfile_data->longjmp_probes = ret;
3481 bp_objfile_data->longjmp_searched = 1;
3482 }
0fd8e87f 3483
2a7f6487
TV
3484 if (bp_objfile_data->longjmp_probes.empty ())
3485 return false;
17450429 3486
2a7f6487
TV
3487 for (probe *p : bp_objfile_data->longjmp_probes)
3488 {
3489 struct breakpoint *b;
25f9533e 3490
2a7f6487
TV
3491 b = create_internal_breakpoint (gdbarch,
3492 p->get_relocated_address (objfile),
9efa3c7f 3493 bp_longjmp_master);
264f9890 3494 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
2a7f6487
TV
3495 b->enable_state = bp_disabled;
3496 }
aed57c53 3497
2a7f6487
TV
3498 return true;
3499}
25f9533e 3500
2a7f6487
TV
3501/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3502 Return true if at least one breakpoint was installed. */
3503
3504static bool
3505create_longjmp_master_breakpoint_names (objfile *objfile)
3506{
3507 struct gdbarch *gdbarch = objfile->arch ();
3508 if (!gdbarch_get_longjmp_target_p (gdbarch))
3509 return false;
3510
3511 struct breakpoint_objfile_data *bp_objfile_data
3512 = get_breakpoint_objfile_data (objfile);
3513 unsigned int installed_bp = 0;
3514
3515 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3516 {
3517 struct breakpoint *b;
3518 const char *func_name;
3519 CORE_ADDR addr;
2a7f6487
TV
3520
3521 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3522 continue;
28106bc2 3523
2a7f6487
TV
3524 func_name = longjmp_names[i];
3525 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3526 {
3527 struct bound_minimal_symbol m;
3528
3529 m = lookup_minimal_symbol_text (func_name, objfile);
3530 if (m.minsym == NULL)
3531 {
3532 /* Prevent future lookups in this objfile. */
3533 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3534 continue;
3535 }
2a7f6487
TV
3536 bp_objfile_data->longjmp_msym[i] = m;
3537 }
28106bc2 3538
4aeddc50 3539 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3540 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
40d97ee2 3541 b->locspec = new_explicit_location_spec_function (func_name);
2a7f6487
TV
3542 b->enable_state = bp_disabled;
3543 installed_bp++;
3544 }
28106bc2 3545
2a7f6487
TV
3546 return installed_bp > 0;
3547}
0fd8e87f 3548
2a7f6487 3549/* Create a master longjmp breakpoint. */
17450429 3550
2a7f6487
TV
3551static void
3552create_longjmp_master_breakpoint (void)
3553{
3554 scoped_restore_current_program_space restore_pspace;
aed57c53 3555
2a7f6487
TV
3556 for (struct program_space *pspace : program_spaces)
3557 {
3558 set_current_program_space (pspace);
17450429 3559
2a7f6487
TV
3560 for (objfile *obj : current_program_space->objfiles ())
3561 {
3562 /* Skip separate debug object, it's handled in the loop below. */
3563 if (obj->separate_debug_objfile_backlink != nullptr)
3564 continue;
3565
3566 /* Try a probe kind breakpoint on main objfile. */
3567 if (create_longjmp_master_breakpoint_probe (obj))
3568 continue;
3569
3570 /* Try longjmp_names kind breakpoints on main and separate_debug
3571 objfiles. */
3572 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3573 if (create_longjmp_master_breakpoint_names (debug_objfile))
3574 break;
94c93c35
TT
3575 }
3576 }
0fd8e87f
UW
3577}
3578
af02033e 3579/* Create a master std::terminate breakpoint. */
aa7d318d 3580static void
af02033e 3581create_std_terminate_master_breakpoint (void)
aa7d318d 3582{
af02033e 3583 const char *const func_name = "std::terminate()";
aa7d318d 3584
5ed8105e 3585 scoped_restore_current_program_space restore_pspace;
aa7d318d 3586
94c93c35
TT
3587 for (struct program_space *pspace : program_spaces)
3588 {
94c93c35 3589 set_current_program_space (pspace);
17450429 3590
94c93c35
TT
3591 for (objfile *objfile : current_program_space->objfiles ())
3592 {
3593 struct breakpoint *b;
3594 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3595
94c93c35 3596 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3597
94c93c35
TT
3598 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3599 continue;
17450429 3600
94c93c35
TT
3601 if (bp_objfile_data->terminate_msym.minsym == NULL)
3602 {
3603 struct bound_minimal_symbol m;
17450429 3604
94c93c35 3605 m = lookup_minimal_symbol (func_name, NULL, objfile);
60f62e2b
SM
3606 if (m.minsym == NULL || (m.minsym->type () != mst_text
3607 && m.minsym->type () != mst_file_text))
94c93c35
TT
3608 {
3609 /* Prevent future lookups in this objfile. */
3610 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3611 continue;
3612 }
3613 bp_objfile_data->terminate_msym = m;
3614 }
aa7d318d 3615
829b6b37
TV
3616 b = create_internal_breakpoint (objfile->arch (),
3617 bp_objfile_data->terminate_msym,
9efa3c7f 3618 bp_std_terminate_master);
40d97ee2 3619 b->locspec = new_explicit_location_spec_function (func_name);
94c93c35
TT
3620 b->enable_state = bp_disabled;
3621 }
3622 }
aa7d318d
TT
3623}
3624
1940319c
TV
3625/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3626 probe. Return true if a breakpoint was installed. */
186c406b 3627
1940319c
TV
3628static bool
3629create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3630{
1940319c
TV
3631 struct breakpoint *b;
3632 struct gdbarch *gdbarch;
3633 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3634
1940319c 3635 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3636
1940319c
TV
3637 /* We prefer the SystemTap probe point if it exists. */
3638 if (!bp_objfile_data->exception_searched)
3639 {
3640 std::vector<probe *> ret
3641 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3642
1940319c 3643 if (!ret.empty ())
28106bc2 3644 {
1940319c
TV
3645 /* We are only interested in checking one element. */
3646 probe *p = ret[0];
25f9533e 3647
1940319c 3648 if (!p->can_evaluate_arguments ())
25f9533e 3649 {
1940319c
TV
3650 /* We cannot use the probe interface here, because it does
3651 not know how to evaluate arguments. */
3652 ret.clear ();
25f9533e 3653 }
28106bc2 3654 }
1940319c
TV
3655 bp_objfile_data->exception_probes = ret;
3656 bp_objfile_data->exception_searched = 1;
3657 }
28106bc2 3658
1940319c
TV
3659 if (bp_objfile_data->exception_probes.empty ())
3660 return false;
45461e0d 3661
1940319c 3662 gdbarch = objfile->arch ();
28106bc2 3663
1940319c
TV
3664 for (probe *p : bp_objfile_data->exception_probes)
3665 {
3666 b = create_internal_breakpoint (gdbarch,
3667 p->get_relocated_address (objfile),
9efa3c7f 3668 bp_exception_master);
264f9890 3669 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
1940319c
TV
3670 b->enable_state = bp_disabled;
3671 }
28106bc2 3672
1940319c
TV
3673 return true;
3674}
28106bc2 3675
1940319c
TV
3676/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3677 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3678
1940319c
TV
3679static bool
3680create_exception_master_breakpoint_hook (objfile *objfile)
3681{
3682 const char *const func_name = "_Unwind_DebugHook";
3683 struct breakpoint *b;
3684 struct gdbarch *gdbarch;
3685 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3686
1940319c 3687 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3688
1940319c
TV
3689 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3690 return false;
17450429 3691
1940319c
TV
3692 gdbarch = objfile->arch ();
3693
3694 if (bp_objfile_data->exception_msym.minsym == NULL)
3695 {
3696 struct bound_minimal_symbol debug_hook;
3697
3698 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3699 if (debug_hook.minsym == NULL)
3700 {
3701 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3702 return false;
186c406b 3703 }
17450429 3704
1940319c
TV
3705 bp_objfile_data->exception_msym = debug_hook;
3706 }
3707
829b6b37
TV
3708 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3709 bp_exception_master);
40d97ee2 3710 b->locspec = new_explicit_location_spec_function (func_name);
1940319c
TV
3711 b->enable_state = bp_disabled;
3712
3713 return true;
3714}
3715
3716/* Install a master breakpoint on the unwinder's debug hook. */
3717
3718static void
3719create_exception_master_breakpoint (void)
3720{
3721 for (objfile *obj : current_program_space->objfiles ())
3722 {
3723 /* Skip separate debug object. */
3724 if (obj->separate_debug_objfile_backlink)
3725 continue;
3726
3727 /* Try a probe kind breakpoint. */
3728 if (create_exception_master_breakpoint_probe (obj))
3729 continue;
3730
7c6944ab
TV
3731 /* Iterate over main and separate debug objects and try an
3732 _Unwind_DebugHook kind breakpoint. */
3733 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3734 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3735 break;
186c406b 3736 }
186c406b
TT
3737}
3738
9ef9e6a6
KS
3739/* Does B have a location spec? */
3740
d8de7963 3741static bool
264f9890 3742breakpoint_location_spec_empty_p (const struct breakpoint *b)
9ef9e6a6 3743{
238dc9af 3744 return (b->locspec != nullptr && b->locspec->empty_p ());
9ef9e6a6
KS
3745}
3746
c906108c 3747void
fba45db2 3748update_breakpoints_after_exec (void)
c906108c 3749{
25b22b0a
PA
3750 /* We're about to delete breakpoints from GDB's lists. If the
3751 INSERTED flag is true, GDB will try to lift the breakpoints by
3752 writing the breakpoints' "shadow contents" back into memory. The
3753 "shadow contents" are NOT valid after an exec, so GDB should not
3754 do that. Instead, the target is responsible from marking
3755 breakpoints out as soon as it detects an exec. We don't do that
3756 here instead, because there may be other attempts to delete
3757 breakpoints after detecting an exec and before reaching here. */
48d7020b 3758 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3759 if (bploc->pspace == current_program_space)
3760 gdb_assert (!bploc->inserted);
c906108c 3761
1428b37a 3762 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3763 {
3764 if (b->pspace != current_program_space)
c5aa993b 3765 continue;
c906108c 3766
01add95b
SM
3767 /* Solib breakpoints must be explicitly reset after an exec(). */
3768 if (b->type == bp_shlib_event)
3769 {
3770 delete_breakpoint (b);
3771 continue;
3772 }
4efc6507 3773
01add95b
SM
3774 /* JIT breakpoints must be explicitly reset after an exec(). */
3775 if (b->type == bp_jit_event)
3776 {
3777 delete_breakpoint (b);
3778 continue;
3779 }
c4093a6a 3780
01add95b
SM
3781 /* Thread event breakpoints must be set anew after an exec(),
3782 as must overlay event and longjmp master breakpoints. */
3783 if (b->type == bp_thread_event || b->type == bp_overlay_event
3784 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3785 || b->type == bp_exception_master)
3786 {
3787 delete_breakpoint (b);
3788 continue;
3789 }
c5aa993b 3790
01add95b
SM
3791 /* Step-resume breakpoints are meaningless after an exec(). */
3792 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3793 {
3794 delete_breakpoint (b);
3795 continue;
3796 }
7c16b83e 3797
01add95b
SM
3798 /* Just like single-step breakpoints. */
3799 if (b->type == bp_single_step)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
611c83ae 3804
01add95b
SM
3805 /* Longjmp and longjmp-resume breakpoints are also meaningless
3806 after an exec. */
3807 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3808 || b->type == bp_longjmp_call_dummy
3809 || b->type == bp_exception || b->type == bp_exception_resume)
3810 {
3811 delete_breakpoint (b);
3812 continue;
3813 }
ce78b96d 3814
01add95b
SM
3815 if (b->type == bp_catchpoint)
3816 {
3817 /* For now, none of the bp_catchpoint breakpoints need to
3818 do anything at this point. In the future, if some of
3819 the catchpoints need to something, we will need to add
3820 a new method, and call this method from here. */
3821 continue;
3822 }
c5aa993b 3823
01add95b
SM
3824 /* bp_finish is a special case. The only way we ought to be able
3825 to see one of these when an exec() has happened, is if the user
3826 caught a vfork, and then said "finish". Ordinarily a finish just
3827 carries them to the call-site of the current callee, by setting
3828 a temporary bp there and resuming. But in this case, the finish
3829 will carry them entirely through the vfork & exec.
3830
3831 We don't want to allow a bp_finish to remain inserted now. But
3832 we can't safely delete it, 'cause finish_command has a handle to
3833 the bp on a bpstat, and will later want to delete it. There's a
3834 chance (and I've seen it happen) that if we delete the bp_finish
3835 here, that its storage will get reused by the time finish_command
3836 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3837 We really must allow finish_command to delete a bp_finish.
3838
3839 In the absence of a general solution for the "how do we know
3840 it's safe to delete something others may have handles to?"
3841 problem, what we'll do here is just uninsert the bp_finish, and
3842 let finish_command delete it.
3843
3844 (We know the bp_finish is "doomed" in the sense that it's
3845 momentary, and will be deleted as soon as finish_command sees
3846 the inferior stopped. So it doesn't matter that the bp's
3847 address is probably bogus in the new a.out, unlike e.g., the
3848 solib breakpoints.) */
3849
3850 if (b->type == bp_finish)
3851 {
3852 continue;
3853 }
3854
3855 /* Without a symbolic address, we have little hope of the
3856 pre-exec() address meaning the same thing in the post-exec()
3857 a.out. */
264f9890 3858 if (breakpoint_location_spec_empty_p (b))
01add95b
SM
3859 {
3860 delete_breakpoint (b);
3861 continue;
3862 }
3863 }
c906108c
SS
3864}
3865
3866int
d80ee84f 3867detach_breakpoints (ptid_t ptid)
c906108c 3868{
3a1bae8e 3869 int val = 0;
2989a365 3870 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3871 struct inferior *inf = current_inferior ();
c5aa993b 3872
e99b03dc 3873 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3874 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3875
6c95b8df 3876 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3877 inferior_ptid = ptid;
48d7020b 3878 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3879 {
3880 if (bl->pspace != inf->pspace)
3881 continue;
3882
3883 /* This function must physically remove breakpoints locations
3884 from the specified ptid, without modifying the breakpoint
6e14e441
PA
3885 package's state. Locations of type bp_loc_other and
3886 bp_loc_software_watchpoint are only maintained at GDB side,
3887 so there is no need to remove them. Moreover, removing these
01add95b 3888 would modify the breakpoint package's state. */
6e14e441
PA
3889 if (bl->loc_type == bp_loc_other
3890 || bl->loc_type == bp_loc_software_watchpoint)
01add95b
SM
3891 continue;
3892
3893 if (bl->inserted)
3894 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3895 }
d03285ec 3896
3a1bae8e 3897 return val;
c906108c
SS
3898}
3899
35df4500 3900/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3901 Note that this is used to detach breakpoints from a child fork.
3902 When we get here, the child isn't in the inferior list, and neither
3903 do we have objects to represent its address space --- we should
35df4500 3904 *not* look at bl->pspace->aspace here. */
6c95b8df 3905
c906108c 3906static int
b2b6a7da 3907remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3908{
3909 int val;
c5aa993b 3910
35df4500
TJB
3911 /* BL is never in moribund_locations by our callers. */
3912 gdb_assert (bl->owner != NULL);
2bdf28a0 3913
74960c60
VP
3914 /* The type of none suggests that owner is actually deleted.
3915 This should not ever happen. */
35df4500 3916 gdb_assert (bl->owner->type != bp_none);
0bde7532 3917
35df4500
TJB
3918 if (bl->loc_type == bp_loc_software_breakpoint
3919 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3920 {
c02f5703
MS
3921 /* "Normal" instruction breakpoint: either the standard
3922 trap-instruction bp (bp_breakpoint), or a
3923 bp_hardware_breakpoint. */
3924
3925 /* First check to see if we have to handle an overlay. */
3926 if (overlay_debugging == ovly_off
35df4500
TJB
3927 || bl->section == NULL
3928 || !(section_is_overlay (bl->section)))
c02f5703
MS
3929 {
3930 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3931
3932 /* If we're trying to uninsert a memory breakpoint that we
3933 know is set in a dynamic object that is marked
3934 shlib_disabled, then either the dynamic object was
3935 removed with "remove-symbol-file" or with
3936 "nosharedlibrary". In the former case, we don't know
3937 whether another dynamic object might have loaded over the
3938 breakpoint's address -- the user might well let us know
3939 about it next with add-symbol-file (the whole point of
d03de421 3940 add-symbol-file is letting the user manually maintain a
08351840
PA
3941 list of dynamically loaded objects). If we have the
3942 breakpoint's shadow memory, that is, this is a software
3943 breakpoint managed by GDB, check whether the breakpoint
3944 is still inserted in memory, to avoid overwriting wrong
3945 code with stale saved shadow contents. Note that HW
3946 breakpoints don't have shadow memory, as they're
3947 implemented using a mechanism that is not dependent on
3948 being able to modify the target's memory, and as such
3949 they should always be removed. */
3950 if (bl->shlib_disabled
3951 && bl->target_info.shadow_len != 0
3952 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3953 val = 0;
3954 else
c47614fe 3955 val = bl->owner->remove_location (bl, reason);
c02f5703 3956 }
c906108c
SS
3957 else
3958 {
4a64f543 3959 /* This breakpoint is in an overlay section.
c02f5703
MS
3960 Did we set a breakpoint at the LMA? */
3961 if (!overlay_events_enabled)
3962 {
3963 /* Yes -- overlay event support is not active, so we
3964 should have set a breakpoint at the LMA. Remove it.
3965 */
c02f5703
MS
3966 /* Ignore any failures: if the LMA is in ROM, we will
3967 have already warned when we failed to insert it. */
35df4500
TJB
3968 if (bl->loc_type == bp_loc_hardware_breakpoint)
3969 target_remove_hw_breakpoint (bl->gdbarch,
3970 &bl->overlay_target_info);
c02f5703 3971 else
35df4500 3972 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3973 &bl->overlay_target_info,
3974 reason);
c02f5703
MS
3975 }
3976 /* Did we set a breakpoint at the VMA?
3977 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3978 if (bl->inserted)
c906108c 3979 {
c02f5703
MS
3980 /* Yes -- remove it. Previously we did not bother to
3981 remove the breakpoint if the section had been
3982 unmapped, but let's not rely on that being safe. We
3983 don't know what the overlay manager might do. */
aa67235e
UW
3984
3985 /* However, we should remove *software* breakpoints only
3986 if the section is still mapped, or else we overwrite
3987 wrong code with the saved shadow contents. */
348d480f
PA
3988 if (bl->loc_type == bp_loc_hardware_breakpoint
3989 || section_is_mapped (bl->section))
c47614fe 3990 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
3991 else
3992 val = 0;
c906108c 3993 }
c02f5703
MS
3994 else
3995 {
3996 /* No -- not inserted, so no need to remove. No error. */
3997 val = 0;
3998 }
c906108c 3999 }
879d1e6b 4000
08351840
PA
4001 /* In some cases, we might not be able to remove a breakpoint in
4002 a shared library that has already been removed, but we have
4003 not yet processed the shlib unload event. Similarly for an
4004 unloaded add-symbol-file object - the user might not yet have
4005 had the chance to remove-symbol-file it. shlib_disabled will
4006 be set if the library/object has already been removed, but
4007 the breakpoint hasn't been uninserted yet, e.g., after
4008 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4009 always-inserted mode. */
076855f9 4010 if (val
08351840
PA
4011 && (bl->loc_type == bp_loc_software_breakpoint
4012 && (bl->shlib_disabled
4013 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4014 || shared_objfile_contains_address_p (bl->pspace,
4015 bl->address))))
879d1e6b
UW
4016 val = 0;
4017
c906108c
SS
4018 if (val)
4019 return val;
b2b6a7da 4020 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4021 }
35df4500 4022 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4023 {
b2b6a7da 4024 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 4025 bl->owner->remove_location (bl, reason);
2e70b7b9 4026
c906108c 4027 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4028 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4029 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4030 bl->owner->number);
c906108c 4031 }
35df4500 4032 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
4033 && breakpoint_enabled (bl->owner)
4034 && !bl->duplicate)
ce78b96d 4035 {
c47614fe 4036 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
4037 if (val)
4038 return val;
77b06cd7 4039
b2b6a7da 4040 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4041 }
c906108c
SS
4042
4043 return 0;
4044}
4045
6c95b8df 4046static int
834c0d03 4047remove_breakpoint (struct bp_location *bl)
6c95b8df 4048{
35df4500
TJB
4049 /* BL is never in moribund_locations by our callers. */
4050 gdb_assert (bl->owner != NULL);
2bdf28a0 4051
6c95b8df
PA
4052 /* The type of none suggests that owner is actually deleted.
4053 This should not ever happen. */
35df4500 4054 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4055
5ed8105e 4056 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4057
35df4500 4058 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4059
5ed8105e 4060 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4061}
4062
c906108c
SS
4063/* Clear the "inserted" flag in all breakpoints. */
4064
25b22b0a 4065void
fba45db2 4066mark_breakpoints_out (void)
c906108c 4067{
48d7020b 4068 for (bp_location *bl : all_bp_locations ())
66c4b3e8 4069 if (bl->pspace == current_program_space)
35df4500 4070 bl->inserted = 0;
c906108c
SS
4071}
4072
53a5351d
JM
4073/* Clear the "inserted" flag in all breakpoints and delete any
4074 breakpoints which should go away between runs of the program.
c906108c
SS
4075
4076 Plus other such housekeeping that has to be done for breakpoints
4077 between runs.
4078
53a5351d
JM
4079 Note: this function gets called at the end of a run (by
4080 generic_mourn_inferior) and when a run begins (by
4a64f543 4081 init_wait_for_inferior). */
c906108c
SS
4082
4083
4084
4085void
fba45db2 4086breakpoint_init_inferior (enum inf_context context)
c906108c 4087{
6c95b8df 4088 struct program_space *pspace = current_program_space;
c906108c 4089
50c71eaf
PA
4090 /* If breakpoint locations are shared across processes, then there's
4091 nothing to do. */
f5656ead 4092 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4093 return;
4094
1a853c52 4095 mark_breakpoints_out ();
075f6582 4096
1428b37a 4097 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
4098 {
4099 if (b->loc && b->loc->pspace != pspace)
4100 continue;
6c95b8df 4101
01add95b
SM
4102 switch (b->type)
4103 {
4104 case bp_call_dummy:
4105 case bp_longjmp_call_dummy:
c906108c 4106
01add95b
SM
4107 /* If the call dummy breakpoint is at the entry point it will
4108 cause problems when the inferior is rerun, so we better get
4109 rid of it. */
ab92d69b 4110
01add95b 4111 case bp_watchpoint_scope:
ab92d69b 4112
01add95b 4113 /* Also get rid of scope breakpoints. */
ab92d69b 4114
01add95b 4115 case bp_shlib_event:
ab92d69b 4116
01add95b
SM
4117 /* Also remove solib event breakpoints. Their addresses may
4118 have changed since the last time we ran the program.
4119 Actually we may now be debugging against different target;
4120 and so the solib backend that installed this breakpoint may
4121 not be used in by the target. E.g.,
ab92d69b 4122
01add95b
SM
4123 (gdb) file prog-linux
4124 (gdb) run # native linux target
4125 ...
4126 (gdb) kill
4127 (gdb) file prog-win.exe
4128 (gdb) tar rem :9999 # remote Windows gdbserver.
4129 */
c906108c 4130
01add95b 4131 case bp_step_resume:
f59f708a 4132
01add95b 4133 /* Also remove step-resume breakpoints. */
f59f708a 4134
01add95b 4135 case bp_single_step:
7c16b83e 4136
01add95b 4137 /* Also remove single-step breakpoints. */
7c16b83e 4138
01add95b
SM
4139 delete_breakpoint (b);
4140 break;
c906108c 4141
01add95b
SM
4142 case bp_watchpoint:
4143 case bp_hardware_watchpoint:
4144 case bp_read_watchpoint:
4145 case bp_access_watchpoint:
4146 {
4147 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4148
01add95b
SM
4149 /* Likewise for watchpoints on local expressions. */
4150 if (w->exp_valid_block != NULL)
4151 delete_breakpoint (b);
4152 else
4153 {
4154 /* Get rid of existing locations, which are no longer
4155 valid. New ones will be created in
4156 update_watchpoint, when the inferior is restarted.
4157 The next update_global_location_list call will
4158 garbage collect them. */
4159 b->loc = NULL;
4160
4161 if (context == inf_starting)
4162 {
4163 /* Reset val field to force reread of starting value in
4164 insert_breakpoints. */
4165 w->val.reset (nullptr);
4166 w->val_valid = false;
4167 }
4168 }
4169 }
4170 break;
4171 default:
4172 break;
3a5c3e22 4173 }
01add95b 4174 }
1c5cfe86
PA
4175
4176 /* Get rid of the moribund locations. */
1123588c 4177 for (bp_location *bl : moribund_locations)
35df4500 4178 decref_bp_location (&bl);
1123588c 4179 moribund_locations.clear ();
c906108c
SS
4180}
4181
6c95b8df
PA
4182/* These functions concern about actual breakpoints inserted in the
4183 target --- to e.g. check if we need to do decr_pc adjustment or if
4184 we need to hop over the bkpt --- so we check for address space
4185 match, not program space. */
4186
c2c6d25f
JM
4187/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4188 exists at PC. It returns ordinary_breakpoint_here if it's an
4189 ordinary breakpoint, or permanent_breakpoint_here if it's a
4190 permanent breakpoint.
4191 - When continuing from a location with an ordinary breakpoint, we
4192 actually single step once before calling insert_breakpoints.
e5dd4106 4193 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4194 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4195 the target, to advance the PC past the breakpoint. */
c906108c 4196
c2c6d25f 4197enum breakpoint_here
accd0bcd 4198breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4199{
d8de7963 4200 bool any_breakpoint_here = false;
c906108c 4201
48d7020b 4202 for (bp_location *bl : all_bp_locations ())
075f6582 4203 {
35df4500
TJB
4204 if (bl->loc_type != bp_loc_software_breakpoint
4205 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4206 continue;
4207
f1310107 4208 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4209 if ((breakpoint_enabled (bl->owner)
1a853c52 4210 || bl->permanent)
f1310107 4211 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4212 {
4213 if (overlay_debugging
35df4500
TJB
4214 && section_is_overlay (bl->section)
4215 && !section_is_mapped (bl->section))
075f6582 4216 continue; /* unmapped overlay -- can't be a match */
1a853c52 4217 else if (bl->permanent)
075f6582
DJ
4218 return permanent_breakpoint_here;
4219 else
d8de7963 4220 any_breakpoint_here = true;
075f6582
DJ
4221 }
4222 }
c906108c 4223
f486487f 4224 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4225}
4226
d35ae833
PA
4227/* See breakpoint.h. */
4228
4229int
accd0bcd 4230breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4231 CORE_ADDR addr, ULONGEST len)
4232{
48d7020b 4233 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4234 {
4235 if (bl->loc_type != bp_loc_software_breakpoint
4236 && bl->loc_type != bp_loc_hardware_breakpoint)
4237 continue;
4238
4239 if ((breakpoint_enabled (bl->owner)
4240 || bl->permanent)
4241 && breakpoint_location_address_range_overlap (bl, aspace,
4242 addr, len))
4243 {
4244 if (overlay_debugging
4245 && section_is_overlay (bl->section)
4246 && !section_is_mapped (bl->section))
4247 {
4248 /* Unmapped overlay -- can't be a match. */
4249 continue;
4250 }
4251
4252 return 1;
4253 }
4254 }
4255
4256 return 0;
4257}
4258
1c5cfe86
PA
4259/* Return true if there's a moribund breakpoint at PC. */
4260
4261int
accd0bcd 4262moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4263{
1123588c 4264 for (bp_location *loc : moribund_locations)
f1310107 4265 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4266 return 1;
4267
4268 return 0;
4269}
c2c6d25f 4270
d8de7963 4271/* Returns true iff BL is inserted at PC, in address space ASPACE. */
f7ce857f 4272
d8de7963 4273static bool
b2ff9ed3 4274bp_location_inserted_here_p (const struct bp_location *bl,
accd0bcd 4275 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4276{
4277 if (bl->inserted
4278 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4279 aspace, pc))
4280 {
d8de7963
AB
4281 /* An unmapped overlay can't be a match. */
4282 return !(overlay_debugging
4283 && section_is_overlay (bl->section)
4284 && !section_is_mapped (bl->section));
f7ce857f 4285 }
d8de7963 4286 return false;
f7ce857f
PA
4287}
4288
a1fd2fa5 4289/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4290
4291int
accd0bcd 4292breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4293{
e0d9a270 4294 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4295 {
35df4500
TJB
4296 if (bl->loc_type != bp_loc_software_breakpoint
4297 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4298 continue;
4299
f7ce857f
PA
4300 if (bp_location_inserted_here_p (bl, aspace, pc))
4301 return 1;
c5aa993b 4302 }
c36b740a
VP
4303 return 0;
4304}
4305
a1fd2fa5
PA
4306/* This function returns non-zero iff there is a software breakpoint
4307 inserted at PC. */
c36b740a
VP
4308
4309int
accd0bcd 4310software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4311 CORE_ADDR pc)
4fa8626c 4312{
e0d9a270 4313 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4314 {
35df4500 4315 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4316 continue;
4317
f7ce857f
PA
4318 if (bp_location_inserted_here_p (bl, aspace, pc))
4319 return 1;
4fa8626c
DJ
4320 }
4321
4322 return 0;
9c02b525
PA
4323}
4324
4325/* See breakpoint.h. */
4326
4327int
accd0bcd 4328hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4329 CORE_ADDR pc)
4330{
e0d9a270 4331 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4332 {
9c02b525
PA
4333 if (bl->loc_type != bp_loc_hardware_breakpoint)
4334 continue;
4335
4336 if (bp_location_inserted_here_p (bl, aspace, pc))
4337 return 1;
4338 }
4339
4340 return 0;
4fa8626c
DJ
4341}
4342
9093389c 4343int
accd0bcd 4344hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4345 CORE_ADDR addr, ULONGEST len)
4346{
43892fdf 4347 for (breakpoint *bpt : all_breakpoints ())
9093389c 4348 {
9093389c
PA
4349 if (bpt->type != bp_hardware_watchpoint
4350 && bpt->type != bp_access_watchpoint)
4351 continue;
4352
4353 if (!breakpoint_enabled (bpt))
4354 continue;
4355
40cb8ca5 4356 for (bp_location *loc : bpt->locations ())
9093389c
PA
4357 if (loc->pspace->aspace == aspace && loc->inserted)
4358 {
4359 CORE_ADDR l, h;
4360
4361 /* Check for intersection. */
768adc05
PA
4362 l = std::max<CORE_ADDR> (loc->address, addr);
4363 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4364 if (l < h)
4365 return 1;
4366 }
4367 }
4368 return 0;
4369}
c5aa993b 4370
f2478a7e 4371/* See breakpoint.h. */
c906108c 4372
f2478a7e
SM
4373bool
4374is_catchpoint (struct breakpoint *b)
c906108c 4375{
f2478a7e 4376 return (b->type == bp_catchpoint);
c906108c
SS
4377}
4378
c906108c
SS
4379/* Clear a bpstat so that it says we are not at any breakpoint.
4380 Also free any storage that is part of a bpstat. */
4381
4382void
313f3b21 4383bpstat_clear (bpstat **bsp)
c906108c 4384{
313f3b21
SM
4385 bpstat *p;
4386 bpstat *q;
c906108c
SS
4387
4388 if (bsp == 0)
4389 return;
4390 p = *bsp;
4391 while (p != NULL)
4392 {
4393 q = p->next;
04afa70c 4394 delete p;
c906108c
SS
4395 p = q;
4396 }
4397 *bsp = NULL;
4398}
4399
313f3b21 4400bpstat::bpstat (const bpstat &other)
04afa70c
TT
4401 : next (NULL),
4402 bp_location_at (other.bp_location_at),
4403 breakpoint_at (other.breakpoint_at),
4404 commands (other.commands),
04afa70c
TT
4405 print (other.print),
4406 stop (other.stop),
4407 print_it (other.print_it)
4408{
850645cf
TT
4409 if (other.old_val != NULL)
4410 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4411}
4412
c906108c
SS
4413/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4414 is part of the bpstat is copied as well. */
4415
313f3b21
SM
4416bpstat *
4417bpstat_copy (bpstat *bs)
c906108c 4418{
313f3b21
SM
4419 bpstat *p = nullptr;
4420 bpstat *tmp;
4421 bpstat *retval = nullptr;
c906108c
SS
4422
4423 if (bs == NULL)
4424 return bs;
4425
4426 for (; bs != NULL; bs = bs->next)
4427 {
313f3b21 4428 tmp = new bpstat (*bs);
31cc81e9 4429
c906108c
SS
4430 if (p == NULL)
4431 /* This is the first thing in the chain. */
4432 retval = tmp;
4433 else
4434 p->next = tmp;
4435 p = tmp;
4436 }
4437 p->next = NULL;
4438 return retval;
4439}
4440
4a64f543 4441/* Find the bpstat associated with this breakpoint. */
c906108c 4442
313f3b21
SM
4443bpstat *
4444bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4445{
c5aa993b
JM
4446 if (bsp == NULL)
4447 return NULL;
c906108c 4448
c5aa993b
JM
4449 for (; bsp != NULL; bsp = bsp->next)
4450 {
f431efe5 4451 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4452 return bsp;
4453 }
c906108c
SS
4454 return NULL;
4455}
4456
ab04a2af
TT
4457/* See breakpoint.h. */
4458
4c462cb0 4459bool
313f3b21 4460bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4461{
ab04a2af
TT
4462 for (; bsp != NULL; bsp = bsp->next)
4463 {
427cd150
TT
4464 if (bsp->breakpoint_at == NULL)
4465 {
4466 /* A moribund location can never explain a signal other than
4467 GDB_SIGNAL_TRAP. */
4468 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4469 return true;
427cd150
TT
4470 }
4471 else
47591c29 4472 {
c47614fe 4473 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4474 return true;
47591c29 4475 }
ab04a2af
TT
4476 }
4477
4c462cb0 4478 return false;
ab04a2af
TT
4479}
4480
78805ff8 4481/* See breakpoint.h. */
c906108c
SS
4482
4483int
313f3b21 4484bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4485{
4486 struct breakpoint *b;
4487
4488 if ((*bsp) == NULL)
4489 return 0; /* No more breakpoint values */
8671a17b 4490
4a64f543
MS
4491 /* We assume we'll never have several bpstats that correspond to a
4492 single breakpoint -- otherwise, this function might return the
4493 same number more than once and this will look ugly. */
f431efe5 4494 b = (*bsp)->breakpoint_at;
8671a17b
PA
4495 *bsp = (*bsp)->next;
4496 if (b == NULL)
4497 return -1; /* breakpoint that's been deleted since */
4498
4499 *num = b->number; /* We have its number */
4500 return 1;
c906108c
SS
4501}
4502
78805ff8
PW
4503/* See breakpoint.h */
4504
4505int
4506bpstat_locno (const bpstat *bs)
4507{
4508 const struct breakpoint *b = bs->breakpoint_at;
4509 const struct bp_location *bl = bs->bp_location_at.get ();
4510
4511 int locno = 0;
4512
28a072f4 4513 if (b != nullptr && b->loc != nullptr && b->loc->next != nullptr)
78805ff8
PW
4514 {
4515 const bp_location *bl_i;
4516
4517 for (bl_i = b->loc;
4518 bl_i != bl && bl_i->next != nullptr;
4519 bl_i = bl_i->next)
4520 locno++;
4521
4522 if (bl_i == bl)
4523 locno++;
4524 else
4525 {
4526 warning (_("location number not found for breakpoint %d address %s."),
4527 b->number, paddress (bl->gdbarch, bl->address));
4528 locno = 0;
4529 }
4530 }
4531
4532 return locno;
4533}
4534
4535/* See breakpoint.h. */
4536
4537void
4538print_num_locno (const bpstat *bs, struct ui_out *uiout)
4539{
4540 struct breakpoint *b = bs->breakpoint_at;
4541
4542 if (b == nullptr)
4543 uiout->text (_("deleted breakpoint"));
4544 else
4545 {
4546 uiout->field_signed ("bkptno", b->number);
4547
4548 int locno = bpstat_locno (bs);
4549 if (locno != 0)
4550 uiout->message (".%pF", signed_field ("locno", locno));
4551 }
4552}
4553
e93ca019 4554/* See breakpoint.h. */
c906108c
SS
4555
4556void
e93ca019 4557bpstat_clear_actions (void)
c906108c 4558{
313f3b21 4559 bpstat *bs;
e93ca019 4560
00431a78 4561 if (inferior_ptid == null_ptid)
e93ca019
JK
4562 return;
4563
00431a78 4564 thread_info *tp = inferior_thread ();
e93ca019 4565 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4566 {
d1b0a7bf 4567 bs->commands = NULL;
850645cf 4568 bs->old_val.reset (nullptr);
c906108c
SS
4569 }
4570}
4571
f3b1572e
PA
4572/* Called when a command is about to proceed the inferior. */
4573
4574static void
4575breakpoint_about_to_proceed (void)
4576{
d7e15655 4577 if (inferior_ptid != null_ptid)
f3b1572e
PA
4578 {
4579 struct thread_info *tp = inferior_thread ();
4580
4581 /* Allow inferior function calls in breakpoint commands to not
4582 interrupt the command list. When the call finishes
4583 successfully, the inferior will be standing at the same
4584 breakpoint as if nothing happened. */
16c381f0 4585 if (tp->control.in_infcall)
f3b1572e
PA
4586 return;
4587 }
4588
4589 breakpoint_proceeded = 1;
4590}
4591
d8de7963 4592/* Return true iff CMD as the first line of a command sequence is `silent'
abf85f46
JK
4593 or its equivalent. */
4594
d8de7963 4595static bool
abf85f46
JK
4596command_line_is_silent (struct command_line *cmd)
4597{
4f45d445 4598 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4599}
4600
83f35083
PW
4601/* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4602 A locno 0 is changed to 1 to e.g. let the user do
4603 (gdb) disable $_hit_bpnum.$_hit_locno
4604 for a single location breakpoint. */
4605
78805ff8 4606static void
83f35083 4607set_hit_convenience_vars (int bpnum, int locno)
78805ff8 4608{
83f35083
PW
4609 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4610 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4611 (locno > 0 ? locno : 1));
78805ff8
PW
4612}
4613
4a64f543
MS
4614/* Execute all the commands associated with all the breakpoints at
4615 this location. Any of these commands could cause the process to
4616 proceed beyond this point, etc. We look out for such changes by
4617 checking the global "breakpoint_proceeded" after each command.
c906108c 4618
347bddb7
PA
4619 Returns true if a breakpoint command resumed the inferior. In that
4620 case, it is the caller's responsibility to recall it again with the
4621 bpstat of the current thread. */
4622
d8de7963 4623static bool
313f3b21 4624bpstat_do_actions_1 (bpstat **bsp)
c906108c 4625{
313f3b21 4626 bpstat *bs;
d8de7963 4627 bool again = false;
c906108c
SS
4628
4629 /* Avoid endless recursion if a `source' command is contained
4630 in bs->commands. */
4631 if (executing_breakpoint_commands)
d8de7963 4632 return false;
c906108c 4633
81b1e71c
TT
4634 scoped_restore save_executing
4635 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4636
1ac32117 4637 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4638
4a64f543 4639 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4640 bs = *bsp;
4641
78805ff8 4642 /* The $_hit_* convenience variables are set before running the
83f35083
PW
4643 commands of BS. In case we have several bs, after the loop,
4644 we set again the variables to the first printed bpnum and locno.
4645 For multiple breakpoints, this ensures the variables are set to the
4646 breakpoint printed for the user. */
4647 int printed_hit_bpnum = -1;
4648 int printed_hit_locno = -1;
78805ff8 4649
c906108c
SS
4650 breakpoint_proceeded = 0;
4651 for (; bs != NULL; bs = bs->next)
4652 {
d1b0a7bf 4653 struct command_line *cmd = NULL;
6c50ab1c 4654
83f35083
PW
4655 /* Set the _hit_* convenience variables before running BS's commands. */
4656 {
4657 const struct breakpoint *b = bs->breakpoint_at;
4658 if (b != nullptr)
4659 {
4660 int locno = bpstat_locno (bs);
4661
4662 set_hit_convenience_vars (b->number, locno);
4663 if (printed_hit_locno == -1 && bs->print)
4664 {
4665 printed_hit_bpnum = b->number;
4666 printed_hit_locno = locno;
4667 }
4668 }
4669 }
78805ff8 4670
6c50ab1c
JB
4671 /* Take ownership of the BSP's command tree, if it has one.
4672
dda83cd7
SM
4673 The command tree could legitimately contain commands like
4674 'step' and 'next', which call clear_proceed_status, which
83f35083 4675 frees the bpstat BS and its command tree. To make sure this doesn't
dda83cd7
SM
4676 free the tree we're executing out from under us, we need to
4677 take ownership of the tree ourselves. Since a given bpstat's
4678 commands are only executed once, we don't need to copy it; we
4679 can clear the pointer in the bpstat, and make sure we free
4680 the tree when we're done. */
d1b0a7bf 4681 counted_command_line ccmd = bs->commands;
9add0f1b 4682 bs->commands = NULL;
d1b0a7bf
TT
4683 if (ccmd != NULL)
4684 cmd = ccmd.get ();
abf85f46
JK
4685 if (command_line_is_silent (cmd))
4686 {
4687 /* The action has been already done by bpstat_stop_status. */
4688 cmd = cmd->next;
4689 }
6c50ab1c 4690
c906108c
SS
4691 while (cmd != NULL)
4692 {
4693 execute_control_command (cmd);
83f35083
PW
4694 /* After execute_control_command, if breakpoint_proceeded is true,
4695 BS has been freed and cannot be accessed anymore. */
c906108c
SS
4696
4697 if (breakpoint_proceeded)
4698 break;
4699 else
4700 cmd = cmd->next;
4701 }
6c50ab1c 4702
c906108c 4703 if (breakpoint_proceeded)
32c1e744 4704 {
cb814510 4705 if (current_ui->async)
347bddb7
PA
4706 /* If we are in async mode, then the target might be still
4707 running, not stopped at any breakpoint, so nothing for
4708 us to do here -- just return to the event loop. */
4709 ;
32c1e744
VP
4710 else
4711 /* In sync mode, when execute_control_command returns
4712 we're already standing on the next breakpoint.
347bddb7
PA
4713 Breakpoint commands for that stop were not run, since
4714 execute_command does not run breakpoint commands --
4715 only command_line_handler does, but that one is not
4716 involved in execution of breakpoint commands. So, we
4717 can now execute breakpoint commands. It should be
4718 noted that making execute_command do bpstat actions is
4719 not an option -- in this case we'll have recursive
4720 invocation of bpstat for each breakpoint with a
4721 command, and can easily blow up GDB stack. Instead, we
4722 return true, which will trigger the caller to recall us
4723 with the new stop_bpstat. */
d8de7963 4724 again = true;
347bddb7 4725 break;
32c1e744 4726 }
c906108c 4727 }
78805ff8
PW
4728
4729 /* Now that we have executed the commands of all bs, set the _hit_*
83f35083
PW
4730 convenience variables to the printed values. */
4731 if (printed_hit_locno != -1)
4732 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
78805ff8 4733
347bddb7
PA
4734 return again;
4735}
4736
00431a78
PA
4737/* Helper for bpstat_do_actions. Get the current thread, if there's
4738 one, is alive and has execution. Return NULL otherwise. */
4739
4740static thread_info *
4741get_bpstat_thread ()
4742{
55f6301a 4743 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4744 return NULL;
4745
4746 thread_info *tp = inferior_thread ();
611841bb 4747 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4748 return NULL;
4749 return tp;
4750}
4751
347bddb7
PA
4752void
4753bpstat_do_actions (void)
4754{
694c6bf5 4755 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4756 thread_info *tp;
353d1d73 4757
347bddb7 4758 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4759 while ((tp = get_bpstat_thread ()) != NULL)
4760 {
4761 /* Since in sync mode, bpstat_do_actions may resume the
4762 inferior, and only return when it is stopped at the next
4763 breakpoint, we keep doing breakpoint actions until it returns
4764 false to indicate the inferior was not resumed. */
4765 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4766 break;
4767 }
353d1d73 4768
694c6bf5 4769 cleanup_if_error.release ();
c906108c
SS
4770}
4771
fa4727a6
DJ
4772/* Print out the (old or new) value associated with a watchpoint. */
4773
4774static void
4775watchpoint_value_print (struct value *val, struct ui_file *stream)
4776{
4777 if (val == NULL)
7f6aba03 4778 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4779 else
79a45b7d
TT
4780 {
4781 struct value_print_options opts;
4782 get_user_print_options (&opts);
4783 value_print (val, stream, &opts);
4784 }
fa4727a6
DJ
4785}
4786
f303dbd6
PA
4787/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4788 debugging multiple threads. */
4789
4790void
4791maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4792{
112e8700 4793 if (uiout->is_mi_like_p ())
f303dbd6
PA
4794 return;
4795
112e8700 4796 uiout->text ("\n");
f303dbd6
PA
4797
4798 if (show_thread_that_caused_stop ())
4799 {
f303dbd6
PA
4800 struct thread_info *thr = inferior_thread ();
4801
112e8700 4802 uiout->text ("Thread ");
33eca680 4803 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 4804
25558938 4805 const char *name = thread_name (thr);
f303dbd6
PA
4806 if (name != NULL)
4807 {
112e8700 4808 uiout->text (" \"");
33eca680 4809 uiout->field_string ("name", name);
112e8700 4810 uiout->text ("\"");
f303dbd6
PA
4811 }
4812
112e8700 4813 uiout->text (" hit ");
f303dbd6
PA
4814 }
4815}
4816
e514a9d6 4817/* Generic routine for printing messages indicating why we
4a64f543 4818 stopped. The behavior of this function depends on the value
e514a9d6
JM
4819 'print_it' in the bpstat structure. Under some circumstances we
4820 may decide not to print anything here and delegate the task to
4a64f543 4821 normal_stop(). */
e514a9d6
JM
4822
4823static enum print_stop_action
313f3b21 4824print_bp_stop_message (bpstat *bs)
e514a9d6
JM
4825{
4826 switch (bs->print_it)
4827 {
4828 case print_it_noop:
4a64f543 4829 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4830 return PRINT_UNKNOWN;
4831 break;
4832
4833 case print_it_done:
4834 /* We still want to print the frame, but we already printed the
dda83cd7 4835 relevant messages. */
e514a9d6
JM
4836 return PRINT_SRC_AND_LOC;
4837 break;
4838
4839 case print_it_normal:
4f8d1dc6 4840 {
f431efe5
PA
4841 struct breakpoint *b = bs->breakpoint_at;
4842
1a6a67de
TJB
4843 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4844 which has since been deleted. */
4845 if (b == NULL)
4846 return PRINT_UNKNOWN;
4847
348d480f 4848 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 4849 return b->print_it (bs);
4f8d1dc6 4850 }
348d480f 4851 break;
3086aeae 4852
e514a9d6 4853 default:
f34652de 4854 internal_error (_("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4855 break;
c906108c 4856 }
c906108c
SS
4857}
4858
2f9ee862 4859/* See breakpoint.h. */
edcc5120 4860
2f9ee862 4861void
4e9e993a 4862print_solib_event (bool is_catchpoint)
edcc5120 4863{
6fb16ce6 4864 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4865 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4866
4867 if (!is_catchpoint)
4868 {
4869 if (any_added || any_deleted)
112e8700 4870 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4871 else
112e8700
SM
4872 current_uiout->text (_("Stopped due to shared library event (no "
4873 "libraries added or removed)\n"));
edcc5120
TT
4874 }
4875
112e8700
SM
4876 if (current_uiout->is_mi_like_p ())
4877 current_uiout->field_string ("reason",
4878 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4879
4880 if (any_deleted)
4881 {
112e8700 4882 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4883 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4884 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4885 {
6fb16ce6
SM
4886 const std::string &name = current_program_space->deleted_solibs[ix];
4887
edcc5120 4888 if (ix > 0)
112e8700
SM
4889 current_uiout->text (" ");
4890 current_uiout->field_string ("library", name);
4891 current_uiout->text ("\n");
edcc5120 4892 }
edcc5120
TT
4893 }
4894
4895 if (any_added)
4896 {
112e8700 4897 current_uiout->text (_(" Inferior loaded "));
10f489e5 4898 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4899 bool first = true;
52941706 4900 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4901 {
bcb430e4 4902 if (!first)
112e8700 4903 current_uiout->text (" ");
bcb430e4 4904 first = false;
112e8700
SM
4905 current_uiout->field_string ("library", iter->so_name);
4906 current_uiout->text ("\n");
edcc5120 4907 }
edcc5120
TT
4908 }
4909}
4910
e514a9d6
JM
4911/* Print a message indicating what happened. This is called from
4912 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4913 list - a list of the eventpoints that caused this stop. KIND is
4914 the target_waitkind for the stopping event. This
e514a9d6
JM
4915 routine calls the generic print routine for printing a message
4916 about reasons for stopping. This will print (for example) the
4917 "Breakpoint n," part of the output. The return value of this
4918 routine is one of:
c906108c 4919
4a64f543 4920 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4921 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4922 code to print the location. An example is
c5aa993b
JM
4923 "Breakpoint 1, " which should be followed by
4924 the location.
917317f4 4925 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4926 to also print the location part of the message.
4927 An example is the catch/throw messages, which
4a64f543 4928 don't require a location appended to the end.
917317f4 4929 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4930 further info to be printed. */
c906108c 4931
917317f4 4932enum print_stop_action
403c71fd 4933bpstat_print (bpstat *bs, target_waitkind kind)
c906108c 4934{
f486487f 4935 enum print_stop_action val;
c5aa993b 4936
c906108c 4937 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4938 (Currently all watchpoints go on the bpstat whether hit or not.
4939 That probably could (should) be changed, provided care is taken
c906108c 4940 with respect to bpstat_explains_signal). */
e514a9d6
JM
4941 for (; bs; bs = bs->next)
4942 {
4943 val = print_bp_stop_message (bs);
4944 if (val == PRINT_SRC_ONLY
78805ff8 4945 || val == PRINT_SRC_AND_LOC
e514a9d6
JM
4946 || val == PRINT_NOTHING)
4947 return val;
4948 }
c906108c 4949
36dfb11c
TT
4950 /* If we had hit a shared library event breakpoint,
4951 print_bp_stop_message would print out this message. If we hit an
4952 OS-level shared library event, do the same thing. */
4953 if (kind == TARGET_WAITKIND_LOADED)
4954 {
4e9e993a 4955 print_solib_event (false);
36dfb11c
TT
4956 return PRINT_NOTHING;
4957 }
4958
e514a9d6 4959 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4960 with and nothing was printed. */
917317f4 4961 return PRINT_UNKNOWN;
c906108c
SS
4962}
4963
bf469271 4964/* Evaluate the boolean expression EXP and return the result. */
c906108c 4965
bf469271
PA
4966static bool
4967breakpoint_cond_eval (expression *exp)
c906108c 4968{
65558ca5
TT
4969 scoped_value_mark mark;
4970 return value_true (evaluate_expression (exp));
c906108c
SS
4971}
4972
5760d0ab 4973/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4974
313f3b21 4975bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 4976 : next (NULL),
b6433ede 4977 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4978 breakpoint_at (bl->owner),
4979 commands (NULL),
04afa70c
TT
4980 print (0),
4981 stop (0),
4982 print_it (print_it_normal)
c906108c 4983{
04afa70c
TT
4984 **bs_link_pointer = this;
4985 *bs_link_pointer = &next;
4986}
4987
313f3b21 4988bpstat::bpstat ()
04afa70c 4989 : next (NULL),
04afa70c
TT
4990 breakpoint_at (NULL),
4991 commands (NULL),
04afa70c
TT
4992 print (0),
4993 stop (0),
4994 print_it (print_it_normal)
4995{
c906108c
SS
4996}
4997\f
d983da9c
DJ
4998/* The target has stopped with waitstatus WS. Check if any hardware
4999 watchpoints have triggered, according to the target. */
5000
5001int
c272a98c 5002watchpoints_triggered (const target_waitstatus &ws)
d983da9c 5003{
57810aa7 5004 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 5005 CORE_ADDR addr;
d983da9c
DJ
5006
5007 if (!stopped_by_watchpoint)
5008 {
5009 /* We were not stopped by a watchpoint. Mark all watchpoints
5010 as not triggered. */
43892fdf 5011 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5012 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5013 {
5014 struct watchpoint *w = (struct watchpoint *) b;
5015
5016 w->watchpoint_triggered = watch_triggered_no;
5017 }
d983da9c
DJ
5018
5019 return 0;
5020 }
5021
328d42d8 5022 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
5023 {
5024 /* We were stopped by a watchpoint, but we don't know where.
5025 Mark all watchpoints as unknown. */
43892fdf 5026 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5027 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5028 {
5029 struct watchpoint *w = (struct watchpoint *) b;
5030
5031 w->watchpoint_triggered = watch_triggered_unknown;
5032 }
d983da9c 5033
3c4797ba 5034 return 1;
d983da9c
DJ
5035 }
5036
5037 /* The target could report the data address. Mark watchpoints
5038 affected by this data address as triggered, and all others as not
5039 triggered. */
5040
43892fdf 5041 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5042 if (is_hardware_watchpoint (b))
d983da9c 5043 {
3a5c3e22 5044 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 5045
3a5c3e22 5046 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 5047 for (bp_location *loc : b->locations ())
9c06b0b4 5048 {
3a5c3e22 5049 if (is_masked_watchpoint (b))
9c06b0b4 5050 {
3a5c3e22
PA
5051 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5052 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5053
5054 if (newaddr == start)
5055 {
3a5c3e22 5056 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5057 break;
5058 }
5059 }
5060 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
5061 else if (target_watchpoint_addr_within_range
5062 (current_inferior ()->top_target (), addr, loc->address,
5063 loc->length))
9c06b0b4 5064 {
3a5c3e22 5065 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5066 break;
5067 }
5068 }
d983da9c
DJ
5069 }
5070
5071 return 1;
5072}
5073
bf469271
PA
5074/* Possible return values for watchpoint_check. */
5075enum wp_check_result
5076 {
5077 /* The watchpoint has been deleted. */
5078 WP_DELETED = 1,
5079
5080 /* The value has changed. */
5081 WP_VALUE_CHANGED = 2,
5082
5083 /* The value has not changed. */
5084 WP_VALUE_NOT_CHANGED = 3,
5085
5086 /* Ignore this watchpoint, no matter if the value changed or not. */
5087 WP_IGNORE = 4,
5088 };
c906108c
SS
5089
5090#define BP_TEMPFLAG 1
5091#define BP_HARDWAREFLAG 2
5092
4a64f543 5093/* Evaluate watchpoint condition expression and check if its value
bf469271 5094 changed. */
553e4c11 5095
bf469271 5096static wp_check_result
313f3b21 5097watchpoint_check (bpstat *bs)
c906108c 5098{
3a5c3e22 5099 struct watchpoint *b;
bd2b40ac 5100 frame_info_ptr fr;
d8de7963 5101 bool within_current_scope;
c906108c 5102
f431efe5 5103 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5104 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5105 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5106
f6bc2008
PA
5107 /* If this is a local watchpoint, we only want to check if the
5108 watchpoint frame is in scope if the current thread is the thread
5109 that was used to create the watchpoint. */
5110 if (!watchpoint_in_thread_scope (b))
60e1c644 5111 return WP_IGNORE;
f6bc2008 5112
c906108c 5113 if (b->exp_valid_block == NULL)
d8de7963 5114 within_current_scope = true;
c906108c
SS
5115 else
5116 {
bd2b40ac 5117 frame_info_ptr frame = get_current_frame ();
edb3359d
DJ
5118 struct gdbarch *frame_arch = get_frame_arch (frame);
5119 CORE_ADDR frame_pc = get_frame_pc (frame);
5120
c9cf6e20 5121 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5122 still in the function but the stack frame has already been
5123 invalidated. Since we can't rely on the values of local
5124 variables after the stack has been destroyed, we are treating
5125 the watchpoint in that state as `not changed' without further
5126 checking. Don't mark watchpoints as changed if the current
5127 frame is in an epilogue - even if they are in some other
5128 frame, our view of the stack is likely to be wrong and
5129 frame_find_by_id could error out. */
c9cf6e20 5130 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5131 return WP_IGNORE;
a0f49112 5132
101dcfbe 5133 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5134 within_current_scope = (fr != NULL);
69fbadd5
DJ
5135
5136 /* If we've gotten confused in the unwinder, we might have
5137 returned a frame that can't describe this variable. */
edb3359d
DJ
5138 if (within_current_scope)
5139 {
5140 struct symbol *function;
5141
5142 function = get_frame_function (fr);
5143 if (function == NULL
4aeddc50 5144 || !contained_in (b->exp_valid_block, function->value_block ()))
d8de7963 5145 within_current_scope = false;
edb3359d 5146 }
69fbadd5 5147
edb3359d 5148 if (within_current_scope)
c906108c
SS
5149 /* If we end up stopping, the current frame will get selected
5150 in normal_stop. So this call to select_frame won't affect
5151 the user. */
0f7d239c 5152 select_frame (fr);
c906108c 5153 }
c5aa993b 5154
c906108c
SS
5155 if (within_current_scope)
5156 {
4a64f543 5157 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5158 time before we return to the command level and call
5159 free_all_values. We can't call free_all_values because we
5160 might be in the middle of evaluating a function call. */
c906108c 5161
9c06b0b4 5162 struct value *mark;
fa4727a6
DJ
5163 struct value *new_val;
5164
c1fc2657 5165 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5166 /* Since we don't know the exact trigger address (from
5167 stopped_data_address), just tell the user we've triggered
5168 a mask watchpoint. */
5169 return WP_VALUE_CHANGED;
5170
5171 mark = value_mark ();
1eaebe02 5172 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5173 NULL, NULL, false);
218d2fc6 5174
bb9d5f81
PP
5175 if (b->val_bitsize != 0)
5176 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5177
4a64f543
MS
5178 /* We use value_equal_contents instead of value_equal because
5179 the latter coerces an array to a pointer, thus comparing just
5180 the address of the array instead of its contents. This is
5181 not what we want. */
fa4727a6 5182 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5183 || (b->val != NULL && !value_equal_contents (b->val.get (),
5184 new_val)))
c906108c 5185 {
c906108c 5186 bs->old_val = b->val;
850645cf 5187 b->val = release_value (new_val);
4c1d86d9 5188 b->val_valid = true;
850645cf
TT
5189 if (new_val != NULL)
5190 value_free_to_mark (mark);
c906108c
SS
5191 return WP_VALUE_CHANGED;
5192 }
5193 else
5194 {
60e1c644 5195 /* Nothing changed. */
c906108c 5196 value_free_to_mark (mark);
c906108c
SS
5197 return WP_VALUE_NOT_CHANGED;
5198 }
5199 }
5200 else
5201 {
5202 /* This seems like the only logical thing to do because
dda83cd7
SM
5203 if we temporarily ignored the watchpoint, then when
5204 we reenter the block in which it is valid it contains
5205 garbage (in the case of a function, it may have two
5206 garbage values, one before and one after the prologue).
5207 So we can't even detect the first assignment to it and
5208 watch after that (since the garbage may or may not equal
5209 the first value assigned). */
348d480f 5210 /* We print all the stop information in
c47614fe
TT
5211 breakpointprint_it, but in this case, by the time we
5212 call breakpoint->print_it this bp will be deleted
348d480f
PA
5213 already. So we have no choice but print the information
5214 here. */
468afe6c 5215
0e454242 5216 SWITCH_THRU_ALL_UIS ()
dda83cd7 5217 {
468afe6c
PA
5218 struct ui_out *uiout = current_uiout;
5219
112e8700
SM
5220 if (uiout->is_mi_like_p ())
5221 uiout->field_string
5222 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5223 uiout->message ("\nWatchpoint %pF deleted because the program has "
5224 "left the block in\n"
5225 "which its expression is valid.\n",
5226 signed_field ("wpnum", b->number));
468afe6c 5227 }
4ce44c66 5228
cdac0397 5229 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5230 b->commands = NULL;
d0fb5eae 5231 watchpoint_del_at_next_stop (b);
c906108c
SS
5232
5233 return WP_DELETED;
5234 }
5235}
5236
18a18393 5237/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5238 breakpoint location BL. This function does not check if we should
5239 stop, only if BL explains the stop. */
5240
d8de7963 5241static bool
6c95b8df 5242bpstat_check_location (const struct bp_location *bl,
accd0bcd 5243 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5244 const target_waitstatus &ws)
18a18393
VP
5245{
5246 struct breakpoint *b = bl->owner;
5247
348d480f 5248 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5249 gdb_assert (b != NULL);
5250
c47614fe 5251 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5252}
5253
3a5c3e22
PA
5254/* Determine if the watched values have actually changed, and we
5255 should stop. If not, set BS->stop to 0. */
5256
18a18393 5257static void
313f3b21 5258bpstat_check_watchpoint (bpstat *bs)
18a18393 5259{
2bdf28a0 5260 const struct bp_location *bl;
3a5c3e22 5261 struct watchpoint *b;
2bdf28a0
JK
5262
5263 /* BS is built for existing struct breakpoint. */
b6433ede 5264 bl = bs->bp_location_at.get ();
2bdf28a0 5265 gdb_assert (bl != NULL);
3a5c3e22 5266 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5267 gdb_assert (b != NULL);
18a18393 5268
18a18393 5269 {
d8de7963
AB
5270 bool must_check_value = false;
5271
c1fc2657 5272 if (b->type == bp_watchpoint)
18a18393
VP
5273 /* For a software watchpoint, we must always check the
5274 watched value. */
d8de7963 5275 must_check_value = true;
18a18393
VP
5276 else if (b->watchpoint_triggered == watch_triggered_yes)
5277 /* We have a hardware watchpoint (read, write, or access)
5278 and the target earlier reported an address watched by
5279 this watchpoint. */
d8de7963 5280 must_check_value = true;
18a18393 5281 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5282 && b->type == bp_hardware_watchpoint)
18a18393
VP
5283 /* We were stopped by a hardware watchpoint, but the target could
5284 not report the data address. We must check the watchpoint's
5285 value. Access and read watchpoints are out of luck; without
5286 a data address, we can't figure it out. */
d8de7963 5287 must_check_value = true;
3a5c3e22 5288
18a18393
VP
5289 if (must_check_value)
5290 {
bf469271
PA
5291 wp_check_result e;
5292
a70b8144 5293 try
bf469271
PA
5294 {
5295 e = watchpoint_check (bs);
5296 }
230d2906 5297 catch (const gdb_exception &ex)
bf469271
PA
5298 {
5299 exception_fprintf (gdb_stderr, ex,
5300 "Error evaluating expression "
5301 "for watchpoint %d\n",
5302 b->number);
5303
5304 SWITCH_THRU_ALL_UIS ()
5305 {
6cb06a8c
TT
5306 gdb_printf (_("Watchpoint %d deleted.\n"),
5307 b->number);
bf469271
PA
5308 }
5309 watchpoint_del_at_next_stop (b);
5310 e = WP_DELETED;
5311 }
bf469271 5312
18a18393
VP
5313 switch (e)
5314 {
5315 case WP_DELETED:
5316 /* We've already printed what needs to be printed. */
5317 bs->print_it = print_it_done;
5318 /* Stop. */
5319 break;
60e1c644
PA
5320 case WP_IGNORE:
5321 bs->print_it = print_it_noop;
5322 bs->stop = 0;
5323 break;
18a18393 5324 case WP_VALUE_CHANGED:
c1fc2657 5325 if (b->type == bp_read_watchpoint)
18a18393 5326 {
85d721b8
PA
5327 /* There are two cases to consider here:
5328
4a64f543 5329 1. We're watching the triggered memory for reads.
85d721b8
PA
5330 In that case, trust the target, and always report
5331 the watchpoint hit to the user. Even though
5332 reads don't cause value changes, the value may
5333 have changed since the last time it was read, and
5334 since we're not trapping writes, we will not see
5335 those, and as such we should ignore our notion of
5336 old value.
5337
4a64f543 5338 2. We're watching the triggered memory for both
85d721b8
PA
5339 reads and writes. There are two ways this may
5340 happen:
5341
4a64f543 5342 2.1. This is a target that can't break on data
85d721b8
PA
5343 reads only, but can break on accesses (reads or
5344 writes), such as e.g., x86. We detect this case
5345 at the time we try to insert read watchpoints.
5346
4a64f543 5347 2.2. Otherwise, the target supports read
85d721b8
PA
5348 watchpoints, but, the user set an access or write
5349 watchpoint watching the same memory as this read
5350 watchpoint.
5351
5352 If we're watching memory writes as well as reads,
5353 ignore watchpoint hits when we find that the
5354 value hasn't changed, as reads don't cause
5355 changes. This still gives false positives when
5356 the program writes the same value to memory as
5357 what there was already in memory (we will confuse
5358 it for a read), but it's much better than
5359 nothing. */
5360
5361 int other_write_watchpoint = 0;
5362
5363 if (bl->watchpoint_type == hw_read)
5364 {
43892fdf 5365 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5366 if (other_b->type == bp_hardware_watchpoint
5367 || other_b->type == bp_access_watchpoint)
85d721b8 5368 {
3a5c3e22
PA
5369 struct watchpoint *other_w =
5370 (struct watchpoint *) other_b;
5371
5372 if (other_w->watchpoint_triggered
5373 == watch_triggered_yes)
5374 {
5375 other_write_watchpoint = 1;
5376 break;
5377 }
85d721b8
PA
5378 }
5379 }
5380
5381 if (other_write_watchpoint
5382 || bl->watchpoint_type == hw_access)
5383 {
5384 /* We're watching the same memory for writes,
5385 and the value changed since the last time we
5386 updated it, so this trap must be for a write.
5387 Ignore it. */
5388 bs->print_it = print_it_noop;
5389 bs->stop = 0;
5390 }
18a18393
VP
5391 }
5392 break;
5393 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5394 if (b->type == bp_hardware_watchpoint
5395 || b->type == bp_watchpoint)
18a18393
VP
5396 {
5397 /* Don't stop: write watchpoints shouldn't fire if
5398 the value hasn't changed. */
5399 bs->print_it = print_it_noop;
5400 bs->stop = 0;
5401 }
5402 /* Stop. */
5403 break;
5404 default:
5405 /* Can't happen. */
18a18393
VP
5406 break;
5407 }
5408 }
d8de7963 5409 else /* !must_check_value */
18a18393
VP
5410 {
5411 /* This is a case where some watchpoint(s) triggered, but
5412 not at the address of this watchpoint, or else no
5413 watchpoint triggered after all. So don't print
5414 anything for this watchpoint. */
5415 bs->print_it = print_it_noop;
5416 bs->stop = 0;
5417 }
5418 }
5419}
5420
7d4df6a4
DE
5421/* For breakpoints that are currently marked as telling gdb to stop,
5422 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5423 of breakpoint referred to by BS. If we should not stop for this
5424 breakpoint, set BS->stop to 0. */
f431efe5 5425
18a18393 5426static void
313f3b21 5427bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5428{
d8a77e4c
AB
5429 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5430
2bdf28a0
JK
5431 const struct bp_location *bl;
5432 struct breakpoint *b;
bf469271
PA
5433 /* Assume stop. */
5434 bool condition_result = true;
7d4df6a4
DE
5435 struct expression *cond;
5436
5437 gdb_assert (bs->stop);
2bdf28a0
JK
5438
5439 /* BS is built for existing struct breakpoint. */
b6433ede 5440 bl = bs->bp_location_at.get ();
2bdf28a0 5441 gdb_assert (bl != NULL);
f431efe5 5442 b = bs->breakpoint_at;
2bdf28a0 5443 gdb_assert (b != NULL);
18a18393 5444
d8a77e4c
AB
5445 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5446 thread->ptid.to_string ().c_str (),
5447 b->number, find_loc_num_by_location (bl));
5448
b775012e
LM
5449 /* Even if the target evaluated the condition on its end and notified GDB, we
5450 need to do so again since GDB does not know if we stopped due to a
5451 breakpoint or a single step breakpoint. */
5452
18a18393 5453 if (frame_id_p (b->frame_id)
a0cbd650 5454 && b->frame_id != get_stack_frame_id (get_current_frame ()))
18a18393 5455 {
d8a77e4c
AB
5456 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5457 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5458 b->frame_id.to_string ().c_str ());
7d4df6a4
DE
5459 bs->stop = 0;
5460 return;
5461 }
60e1c644 5462
12ab52e9
PA
5463 /* If this is a thread/task-specific breakpoint, don't waste cpu
5464 evaluating the condition if this isn't the specified
5465 thread/task. */
00431a78
PA
5466 if ((b->thread != -1 && b->thread != thread->global_num)
5467 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b 5468 {
d8a77e4c 5469 infrun_debug_printf ("incorrect thread or task, not stopping");
6c1b0f7b
DE
5470 bs->stop = 0;
5471 return;
5472 }
5473
6dddc817
DE
5474 /* Evaluate extension language breakpoints that have a "stop" method
5475 implemented. */
5476 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5477
7d4df6a4
DE
5478 if (is_watchpoint (b))
5479 {
5480 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5481
4d01a485 5482 cond = w->cond_exp.get ();
7d4df6a4
DE
5483 }
5484 else
4d01a485 5485 cond = bl->cond.get ();
60e1c644 5486
80e0c6dc 5487 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
7d4df6a4 5488 {
d8de7963 5489 bool within_current_scope = true;
7d4df6a4 5490 struct watchpoint * w;
60e1c644 5491
65558ca5
TT
5492 /* We use scoped_value_mark because it could be a long time
5493 before we return to the command level and call
5494 free_all_values. We can't call free_all_values because we
5495 might be in the middle of evaluating a function call. */
5496 scoped_value_mark mark;
7d4df6a4
DE
5497
5498 if (is_watchpoint (b))
5499 w = (struct watchpoint *) b;
5500 else
5501 w = NULL;
5502
5503 /* Need to select the frame, with all that implies so that
5504 the conditions will have the right context. Because we
5505 use the frame, we will not see an inlined function's
5506 variables when we arrive at a breakpoint at the start
5507 of the inlined function; the current frame will be the
5508 call site. */
5509 if (w == NULL || w->cond_exp_valid_block == NULL)
5510 select_frame (get_current_frame ());
5511 else
18a18393 5512 {
bd2b40ac 5513 frame_info_ptr frame;
7d4df6a4
DE
5514
5515 /* For local watchpoint expressions, which particular
5516 instance of a local is being watched matters, so we
5517 keep track of the frame to evaluate the expression
5518 in. To evaluate the condition however, it doesn't
5519 really matter which instantiation of the function
5520 where the condition makes sense triggers the
5521 watchpoint. This allows an expression like "watch
5522 global if q > 10" set in `func', catch writes to
5523 global on all threads that call `func', or catch
5524 writes on all recursive calls of `func' by a single
5525 thread. We simply always evaluate the condition in
5526 the innermost frame that's executing where it makes
5527 sense to evaluate the condition. It seems
5528 intuitive. */
5529 frame = block_innermost_frame (w->cond_exp_valid_block);
5530 if (frame != NULL)
5531 select_frame (frame);
5532 else
d8de7963 5533 within_current_scope = false;
18a18393 5534 }
7d4df6a4 5535 if (within_current_scope)
bf469271 5536 {
a70b8144 5537 try
bf469271
PA
5538 {
5539 condition_result = breakpoint_cond_eval (cond);
5540 }
230d2906 5541 catch (const gdb_exception &ex)
bf469271
PA
5542 {
5543 exception_fprintf (gdb_stderr, ex,
5544 "Error in testing breakpoint condition:\n");
5545 }
bf469271 5546 }
7d4df6a4 5547 else
18a18393 5548 {
7d4df6a4
DE
5549 warning (_("Watchpoint condition cannot be tested "
5550 "in the current scope"));
5551 /* If we failed to set the right context for this
5552 watchpoint, unconditionally report it. */
18a18393 5553 }
7d4df6a4 5554 /* FIXME-someday, should give breakpoint #. */
18a18393 5555 }
7d4df6a4 5556
80e0c6dc 5557 if (cond != nullptr && !condition_result)
7d4df6a4 5558 {
d8a77e4c 5559 infrun_debug_printf ("condition_result = false, not stopping");
7d4df6a4 5560 bs->stop = 0;
d8a77e4c 5561 return;
7d4df6a4 5562 }
7d4df6a4
DE
5563 else if (b->ignore_count > 0)
5564 {
d8a77e4c
AB
5565 infrun_debug_printf ("ignore count %d, not stopping",
5566 b->ignore_count);
7d4df6a4
DE
5567 b->ignore_count--;
5568 bs->stop = 0;
5569 /* Increase the hit count even though we don't stop. */
5570 ++(b->hit_count);
76727919 5571 gdb::observers::breakpoint_modified.notify (b);
d8a77e4c
AB
5572 return;
5573 }
5574
5575 if (bs->stop)
5576 infrun_debug_printf ("stopping at this breakpoint");
5577 else
5578 infrun_debug_printf ("not stopping at this breakpoint");
18a18393
VP
5579}
5580
1cf4d951
PA
5581/* Returns true if we need to track moribund locations of LOC's type
5582 on the current target. */
5583
d8de7963 5584static bool
b2ff9ed3 5585need_moribund_for_location_type (const struct bp_location *loc)
1cf4d951
PA
5586{
5587 return ((loc->loc_type == bp_loc_software_breakpoint
5588 && !target_supports_stopped_by_sw_breakpoint ())
5589 || (loc->loc_type == bp_loc_hardware_breakpoint
5590 && !target_supports_stopped_by_hw_breakpoint ()));
5591}
5592
ddfe970e 5593/* See breakpoint.h. */
c906108c 5594
313f3b21 5595bpstat *
ddfe970e 5596build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5597 const target_waitstatus &ws)
c906108c 5598{
313f3b21 5599 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5600
43892fdf 5601 for (breakpoint *b : all_breakpoints ())
429374b8 5602 {
1a853c52 5603 if (!breakpoint_enabled (b))
429374b8 5604 continue;
a5606eee 5605
40cb8ca5 5606 for (bp_location *bl : b->locations ())
429374b8 5607 {
4a64f543
MS
5608 /* For hardware watchpoints, we look only at the first
5609 location. The watchpoint_check function will work on the
5610 entire expression, not the individual locations. For
5611 read watchpoints, the watchpoints_triggered function has
5612 checked all locations already. */
429374b8
JK
5613 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5614 break;
18a18393 5615
b5fa468f 5616 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5617 continue;
c5aa993b 5618
09ac7c10 5619 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5620 continue;
c5aa993b 5621
4a64f543
MS
5622 /* Come here if it's a watchpoint, or if the break address
5623 matches. */
c5aa993b 5624
313f3b21 5625 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5626 explain stop. */
c5aa993b 5627
f431efe5
PA
5628 /* Assume we stop. Should we find a watchpoint that is not
5629 actually triggered, or if the condition of the breakpoint
5630 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5631 bs->stop = 1;
5632 bs->print = 1;
d983da9c 5633
f431efe5
PA
5634 /* If this is a scope breakpoint, mark the associated
5635 watchpoint as triggered so that we will handle the
5636 out-of-scope event. We'll get to the watchpoint next
5637 iteration. */
d0fb5eae 5638 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5639 {
5640 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5641
5642 w->watchpoint_triggered = watch_triggered_yes;
5643 }
f431efe5
PA
5644 }
5645 }
5646
7c16b83e 5647 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5648 if (!target_supports_stopped_by_sw_breakpoint ()
5649 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5650 {
1123588c 5651 for (bp_location *loc : moribund_locations)
f431efe5 5652 {
1cf4d951
PA
5653 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5654 && need_moribund_for_location_type (loc))
5655 {
313f3b21 5656 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951
PA
5657 /* For hits of moribund locations, we should just proceed. */
5658 bs->stop = 0;
5659 bs->print = 0;
5660 bs->print_it = print_it_noop;
5661 }
f431efe5
PA
5662 }
5663 }
5664
ddfe970e
KS
5665 return bs_head;
5666}
5667
5668/* See breakpoint.h. */
5669
313f3b21 5670bpstat *
ddfe970e 5671bpstat_stop_status (const address_space *aspace,
00431a78 5672 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5673 const target_waitstatus &ws,
313f3b21 5674 bpstat *stop_chain)
ddfe970e
KS
5675{
5676 struct breakpoint *b = NULL;
5677 /* First item of allocated bpstat's. */
313f3b21
SM
5678 bpstat *bs_head = stop_chain;
5679 bpstat *bs;
ddfe970e
KS
5680 int need_remove_insert;
5681 int removed_any;
5682
5683 /* First, build the bpstat chain with locations that explain a
5684 target stop, while being careful to not set the target running,
5685 as that may invalidate locations (in particular watchpoint
5686 locations are recreated). Resuming will happen here with
5687 breakpoint conditions or watchpoint expressions that include
5688 inferior function calls. */
5689 if (bs_head == NULL)
5690 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5691
edcc5120
TT
5692 /* A bit of special processing for shlib breakpoints. We need to
5693 process solib loading here, so that the lists of loaded and
5694 unloaded libraries are correct before we handle "catch load" and
5695 "catch unload". */
5696 for (bs = bs_head; bs != NULL; bs = bs->next)
5697 {
5d268276 5698 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5699 {
5700 handle_solib_event ();
5701 break;
5702 }
5703 }
5704
f431efe5
PA
5705 /* Now go through the locations that caused the target to stop, and
5706 check whether we're interested in reporting this stop to higher
5707 layers, or whether we should resume the target transparently. */
5708
5709 removed_any = 0;
5710
5760d0ab 5711 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5712 {
5713 if (!bs->stop)
5714 continue;
5715
f431efe5 5716 b = bs->breakpoint_at;
c47614fe 5717 b->check_status (bs);
348d480f 5718 if (bs->stop)
28010a5d 5719 {
00431a78 5720 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5721
429374b8
JK
5722 if (bs->stop)
5723 {
5724 ++(b->hit_count);
c906108c 5725
4a64f543 5726 /* We will stop here. */
429374b8
JK
5727 if (b->disposition == disp_disable)
5728 {
816338b5 5729 --(b->enable_count);
1a853c52 5730 if (b->enable_count <= 0)
429374b8 5731 b->enable_state = bp_disabled;
f431efe5 5732 removed_any = 1;
429374b8 5733 }
bd7ccaa9 5734 gdb::observers::breakpoint_modified.notify (b);
429374b8
JK
5735 if (b->silent)
5736 bs->print = 0;
5737 bs->commands = b->commands;
abf85f46 5738 if (command_line_is_silent (bs->commands
d1b0a7bf 5739 ? bs->commands.get () : NULL))
abf85f46 5740 bs->print = 0;
9d6e6e84 5741
c47614fe 5742 b->after_condition_true (bs);
429374b8
JK
5743 }
5744
348d480f 5745 }
a9b3a50f
PA
5746
5747 /* Print nothing for this entry if we don't stop or don't
5748 print. */
5749 if (!bs->stop || !bs->print)
5750 bs->print_it = print_it_noop;
429374b8 5751 }
876fa593 5752
d983da9c
DJ
5753 /* If we aren't stopping, the value of some hardware watchpoint may
5754 not have changed, but the intermediate memory locations we are
5755 watching may have. Don't bother if we're stopping; this will get
5756 done later. */
d832cb68 5757 need_remove_insert = 0;
5760d0ab
JK
5758 if (! bpstat_causes_stop (bs_head))
5759 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5760 if (!bs->stop
f431efe5
PA
5761 && bs->breakpoint_at
5762 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5763 {
3a5c3e22
PA
5764 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5765
d8de7963 5766 update_watchpoint (w, false /* don't reparse. */);
d832cb68 5767 need_remove_insert = 1;
d983da9c
DJ
5768 }
5769
d832cb68 5770 if (need_remove_insert)
44702360 5771 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5772 else if (removed_any)
44702360 5773 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5774
5760d0ab 5775 return bs_head;
c906108c 5776}
628fe4e4 5777
d37e0847
PA
5778/* See breakpoint.h. */
5779
5780bpstat *
5781bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5782 thread_info *thread, const target_waitstatus &ws)
5783{
5784 gdb_assert (!target_stopped_by_watchpoint ());
5785
5786 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5787 previous stop to avoid confusing bpstat_stop_status. */
5788 watchpoints_triggered (ws);
5789
5790 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5791}
5792
628fe4e4 5793static void
61c26be8 5794handle_jit_event (CORE_ADDR address)
628fe4e4 5795{
628fe4e4
JK
5796 struct gdbarch *gdbarch;
5797
1eb8556f 5798 infrun_debug_printf ("handling bp_jit_event");
243a9253 5799
628fe4e4
JK
5800 /* Switch terminal for any messages produced by
5801 breakpoint_re_set. */
223ffa71 5802 target_terminal::ours_for_output ();
628fe4e4 5803
61c26be8
MS
5804 gdbarch = get_frame_arch (get_current_frame ());
5805 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5806 thus it is expected that its objectfile can be found through
5807 minimal symbol lookup. If it doesn't work (and assert fails), it
5808 most likely means that `jit_breakpoint_re_set` was changes and this
5809 function needs to be updated too. */
5810 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5811 gdb_assert (jit_bp_sym.objfile != nullptr);
508ccf9b
TV
5812 objfile *objfile = jit_bp_sym.objfile;
5813 if (objfile->separate_debug_objfile_backlink)
5814 objfile = objfile->separate_debug_objfile_backlink;
5815 jit_event_handler (gdbarch, objfile);
628fe4e4 5816
223ffa71 5817 target_terminal::inferior ();
628fe4e4
JK
5818}
5819
5820/* Prepare WHAT final decision for infrun. */
5821
5822/* Decide what infrun needs to do with this bpstat. */
5823
c906108c 5824struct bpstat_what
313f3b21 5825bpstat_what (bpstat *bs_head)
c906108c 5826{
c906108c 5827 struct bpstat_what retval;
313f3b21 5828 bpstat *bs;
c906108c 5829
628fe4e4 5830 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5831 retval.call_dummy = STOP_NONE;
e2d0f980 5832 retval.is_longjmp = false;
628fe4e4 5833
0e30163f 5834 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5835 {
628fe4e4
JK
5836 /* Extract this BS's action. After processing each BS, we check
5837 if its action overrides all we've seem so far. */
5838 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5839 enum bptype bptype;
5840
c906108c 5841 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5842 {
5843 /* I suspect this can happen if it was a momentary
5844 breakpoint which has since been deleted. */
5845 bptype = bp_none;
5846 }
20874c92 5847 else
f431efe5 5848 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5849
5850 switch (bptype)
c906108c
SS
5851 {
5852 case bp_none:
628fe4e4 5853 break;
c906108c
SS
5854 case bp_breakpoint:
5855 case bp_hardware_breakpoint:
7c16b83e 5856 case bp_single_step:
c906108c
SS
5857 case bp_until:
5858 case bp_finish:
a9b3a50f 5859 case bp_shlib_event:
c906108c
SS
5860 if (bs->stop)
5861 {
5862 if (bs->print)
628fe4e4 5863 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5864 else
628fe4e4 5865 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5866 }
5867 else
628fe4e4 5868 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5869 break;
5870 case bp_watchpoint:
5871 case bp_hardware_watchpoint:
5872 case bp_read_watchpoint:
5873 case bp_access_watchpoint:
5874 if (bs->stop)
5875 {
5876 if (bs->print)
628fe4e4 5877 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5878 else
628fe4e4 5879 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5880 }
5881 else
628fe4e4
JK
5882 {
5883 /* There was a watchpoint, but we're not stopping.
5884 This requires no further action. */
5885 }
c906108c
SS
5886 break;
5887 case bp_longjmp:
e2e4d78b 5888 case bp_longjmp_call_dummy:
186c406b 5889 case bp_exception:
0a39bb32
PA
5890 if (bs->stop)
5891 {
5892 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5893 retval.is_longjmp = bptype != bp_exception;
5894 }
5895 else
5896 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5897 break;
5898 case bp_longjmp_resume:
186c406b 5899 case bp_exception_resume:
0a39bb32
PA
5900 if (bs->stop)
5901 {
5902 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5903 retval.is_longjmp = bptype == bp_longjmp_resume;
5904 }
5905 else
5906 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5907 break;
5908 case bp_step_resume:
5909 if (bs->stop)
628fe4e4
JK
5910 this_action = BPSTAT_WHAT_STEP_RESUME;
5911 else
c906108c 5912 {
628fe4e4
JK
5913 /* It is for the wrong frame. */
5914 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5915 }
c906108c 5916 break;
2c03e5be
PA
5917 case bp_hp_step_resume:
5918 if (bs->stop)
5919 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5920 else
5921 {
5922 /* It is for the wrong frame. */
5923 this_action = BPSTAT_WHAT_SINGLE;
5924 }
5925 break;
c906108c 5926 case bp_watchpoint_scope:
c4093a6a 5927 case bp_thread_event:
1900040c 5928 case bp_overlay_event:
0fd8e87f 5929 case bp_longjmp_master:
aa7d318d 5930 case bp_std_terminate_master:
186c406b 5931 case bp_exception_master:
628fe4e4 5932 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5933 break;
ce78b96d 5934 case bp_catchpoint:
c5aa993b
JM
5935 if (bs->stop)
5936 {
5937 if (bs->print)
628fe4e4 5938 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5939 else
628fe4e4 5940 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5941 }
5942 else
628fe4e4 5943 {
cb1e4e32
PA
5944 /* Some catchpoints are implemented with breakpoints.
5945 For those, we need to step over the breakpoint. */
6e14e441
PA
5946 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5947 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
cb1e4e32 5948 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5949 }
5950 break;
628fe4e4 5951 case bp_jit_event:
628fe4e4 5952 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5953 break;
c906108c 5954 case bp_call_dummy:
53a5351d
JM
5955 /* Make sure the action is stop (silent or noisy),
5956 so infrun.c pops the dummy frame. */
aa7d318d 5957 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5958 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5959 break;
5960 case bp_std_terminate:
5961 /* Make sure the action is stop (silent or noisy),
5962 so infrun.c pops the dummy frame. */
aa7d318d 5963 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5964 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5965 break;
1042e4c0 5966 case bp_tracepoint:
7a697b8d 5967 case bp_fast_tracepoint:
0fb4aa4b 5968 case bp_static_tracepoint:
7b572efb 5969 case bp_static_marker_tracepoint:
1042e4c0
SS
5970 /* Tracepoint hits should not be reported back to GDB, and
5971 if one got through somehow, it should have been filtered
5972 out already. */
f34652de 5973 internal_error (_("bpstat_what: tracepoint encountered"));
0e30163f
JK
5974 break;
5975 case bp_gnu_ifunc_resolver:
5976 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5977 this_action = BPSTAT_WHAT_SINGLE;
5978 break;
5979 case bp_gnu_ifunc_resolver_return:
5980 /* The breakpoint will be removed, execution will restart from the
5981 PC of the former breakpoint. */
5982 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5983 break;
e7e0cddf
SS
5984
5985 case bp_dprintf:
a11cfd87
HZ
5986 if (bs->stop)
5987 this_action = BPSTAT_WHAT_STOP_SILENT;
5988 else
5989 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5990 break;
5991
628fe4e4 5992 default:
f34652de 5993 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5994 }
628fe4e4 5995
325fac50 5996 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5997 }
628fe4e4 5998
243a9253
PA
5999 return retval;
6000}
628fe4e4 6001
243a9253 6002void
313f3b21 6003bpstat_run_callbacks (bpstat *bs_head)
243a9253 6004{
313f3b21 6005 bpstat *bs;
628fe4e4 6006
0e30163f
JK
6007 for (bs = bs_head; bs != NULL; bs = bs->next)
6008 {
6009 struct breakpoint *b = bs->breakpoint_at;
6010
6011 if (b == NULL)
6012 continue;
6013 switch (b->type)
6014 {
243a9253 6015 case bp_jit_event:
61c26be8 6016 handle_jit_event (bs->bp_location_at->address);
243a9253 6017 break;
0e30163f 6018 case bp_gnu_ifunc_resolver:
74421c0b 6019 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
0e30163f
JK
6020 break;
6021 case bp_gnu_ifunc_resolver_return:
74421c0b 6022 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
0e30163f
JK
6023 break;
6024 }
6025 }
c906108c
SS
6026}
6027
4c462cb0 6028/* See breakpoint.h. */
c906108c 6029
4c462cb0
SM
6030bool
6031bpstat_should_step ()
c906108c 6032{
43892fdf 6033 for (breakpoint *b : all_breakpoints ())
717a8278 6034 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 6035 return true;
43892fdf 6036
4c462cb0 6037 return false;
c906108c
SS
6038}
6039
4c462cb0
SM
6040/* See breakpoint.h. */
6041
6042bool
313f3b21 6043bpstat_causes_stop (bpstat *bs)
67822962
PA
6044{
6045 for (; bs != NULL; bs = bs->next)
6046 if (bs->stop)
4c462cb0 6047 return true;
67822962 6048
4c462cb0 6049 return false;
67822962
PA
6050}
6051
c906108c 6052\f
c5aa993b 6053
6c92c339 6054/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
6055 so it starts at the position corresponding to the table column
6056 named COL_NAME in the currently active table of UIOUT. */
6057
6c92c339 6058static int
170b53b2
UW
6059wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6060{
170b53b2 6061 int i, total_width, width, align;
c5209615 6062 const char *text;
170b53b2
UW
6063
6064 total_width = 0;
112e8700 6065 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6066 {
6067 if (strcmp (text, col_name) == 0)
6c92c339 6068 return total_width;
170b53b2
UW
6069
6070 total_width += width + 1;
6071 }
6072
6c92c339 6073 return 0;
170b53b2
UW
6074}
6075
b775012e
LM
6076/* Determine if the locations of this breakpoint will have their conditions
6077 evaluated by the target, host or a mix of both. Returns the following:
6078
6079 "host": Host evals condition.
6080 "host or target": Host or Target evals condition.
6081 "target": Target evals condition.
6082*/
6083
6084static const char *
a67bcaba 6085bp_condition_evaluator (const breakpoint *b)
b775012e 6086{
b775012e
LM
6087 char host_evals = 0;
6088 char target_evals = 0;
6089
6090 if (!b)
6091 return NULL;
6092
6093 if (!is_breakpoint (b))
6094 return NULL;
6095
6096 if (gdb_evaluates_breakpoint_condition_p ()
6097 || !target_supports_evaluation_of_breakpoint_conditions ())
6098 return condition_evaluation_host;
6099
40cb8ca5 6100 for (bp_location *bl : b->locations ())
b775012e
LM
6101 {
6102 if (bl->cond_bytecode)
6103 target_evals++;
6104 else
6105 host_evals++;
6106 }
6107
6108 if (host_evals && target_evals)
6109 return condition_evaluation_both;
6110 else if (target_evals)
6111 return condition_evaluation_target;
6112 else
6113 return condition_evaluation_host;
6114}
6115
6116/* Determine the breakpoint location's condition evaluator. This is
6117 similar to bp_condition_evaluator, but for locations. */
6118
6119static const char *
b2ff9ed3 6120bp_location_condition_evaluator (const struct bp_location *bl)
b775012e
LM
6121{
6122 if (bl && !is_breakpoint (bl->owner))
6123 return NULL;
6124
6125 if (gdb_evaluates_breakpoint_condition_p ()
6126 || !target_supports_evaluation_of_breakpoint_conditions ())
6127 return condition_evaluation_host;
6128
6129 if (bl && bl->cond_bytecode)
6130 return condition_evaluation_target;
6131 else
6132 return condition_evaluation_host;
6133}
6134
859825b8
JK
6135/* Print the LOC location out of the list of B->LOC locations. */
6136
170b53b2 6137static void
a67bcaba 6138print_breakpoint_location (const breakpoint *b,
170b53b2 6139 struct bp_location *loc)
0d381245 6140{
79a45e25 6141 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6142
6143 scoped_restore_current_program_space restore_pspace;
6c95b8df 6144
859825b8
JK
6145 if (loc != NULL && loc->shlib_disabled)
6146 loc = NULL;
6147
6c95b8df
PA
6148 if (loc != NULL)
6149 set_current_program_space (loc->pspace);
6150
56435ebe 6151 if (b->display_canonical)
709438c7 6152 uiout->field_string ("what", b->locspec->to_string ());
2f202fde 6153 else if (loc && loc->symtab)
0d381245 6154 {
4a27f119
KS
6155 const struct symbol *sym = loc->symbol;
6156
0d381245
VP
6157 if (sym)
6158 {
112e8700 6159 uiout->text ("in ");
987012b8 6160 uiout->field_string ("func", sym->print_name (),
e43b10e1 6161 function_name_style.style ());
112e8700
SM
6162 uiout->text (" ");
6163 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6164 uiout->text ("at ");
0d381245 6165 }
112e8700 6166 uiout->field_string ("file",
cbe56571 6167 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6168 file_name_style.style ());
112e8700 6169 uiout->text (":");
05cba821 6170
112e8700
SM
6171 if (uiout->is_mi_like_p ())
6172 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6173
381befee 6174 uiout->field_signed ("line", loc->line_number);
0d381245 6175 }
859825b8 6176 else if (loc)
0d381245 6177 {
d7e74731 6178 string_file stb;
170b53b2 6179
d7e74731 6180 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6181 demangle, "");
112e8700 6182 uiout->field_stream ("at", stb);
0d381245 6183 }
859825b8 6184 else
f00aae0f 6185 {
709438c7 6186 uiout->field_string ("pending", b->locspec->to_string ());
f00aae0f
KS
6187 /* If extra_string is available, it could be holding a condition
6188 or dprintf arguments. In either case, make sure it is printed,
6189 too, but only for non-MI streams. */
112e8700 6190 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6191 {
6192 if (b->type == bp_dprintf)
112e8700 6193 uiout->text (",");
f00aae0f 6194 else
112e8700 6195 uiout->text (" ");
6f781ee3 6196 uiout->text (b->extra_string.get ());
f00aae0f
KS
6197 }
6198 }
6c95b8df 6199
b775012e
LM
6200 if (loc && is_breakpoint (b)
6201 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6202 && bp_condition_evaluator (b) == condition_evaluation_both)
6203 {
112e8700
SM
6204 uiout->text (" (");
6205 uiout->field_string ("evaluated-by",
b775012e 6206 bp_location_condition_evaluator (loc));
112e8700 6207 uiout->text (")");
b775012e 6208 }
0d381245
VP
6209}
6210
269b11a2
PA
6211static const char *
6212bptype_string (enum bptype type)
c906108c 6213{
c4093a6a
JM
6214 struct ep_type_description
6215 {
6216 enum bptype type;
a121b7c1 6217 const char *description;
c4093a6a
JM
6218 };
6219 static struct ep_type_description bptypes[] =
c906108c 6220 {
c5aa993b
JM
6221 {bp_none, "?deleted?"},
6222 {bp_breakpoint, "breakpoint"},
c906108c 6223 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6224 {bp_single_step, "sw single-step"},
c5aa993b
JM
6225 {bp_until, "until"},
6226 {bp_finish, "finish"},
6227 {bp_watchpoint, "watchpoint"},
c906108c 6228 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6229 {bp_read_watchpoint, "read watchpoint"},
6230 {bp_access_watchpoint, "acc watchpoint"},
6231 {bp_longjmp, "longjmp"},
6232 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6233 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6234 {bp_exception, "exception"},
6235 {bp_exception_resume, "exception resume"},
c5aa993b 6236 {bp_step_resume, "step resume"},
2c03e5be 6237 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6238 {bp_watchpoint_scope, "watchpoint scope"},
6239 {bp_call_dummy, "call dummy"},
aa7d318d 6240 {bp_std_terminate, "std::terminate"},
c5aa993b 6241 {bp_shlib_event, "shlib events"},
c4093a6a 6242 {bp_thread_event, "thread events"},
1900040c 6243 {bp_overlay_event, "overlay events"},
0fd8e87f 6244 {bp_longjmp_master, "longjmp master"},
aa7d318d 6245 {bp_std_terminate_master, "std::terminate master"},
186c406b 6246 {bp_exception_master, "exception master"},
ce78b96d 6247 {bp_catchpoint, "catchpoint"},
1042e4c0 6248 {bp_tracepoint, "tracepoint"},
7a697b8d 6249 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6250 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6251 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6252 {bp_dprintf, "dprintf"},
4efc6507 6253 {bp_jit_event, "jit events"},
0e30163f
JK
6254 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6255 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6256 };
269b11a2
PA
6257
6258 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6259 || ((int) type != bptypes[(int) type].type))
f34652de 6260 internal_error (_("bptypes table does not describe type #%d."),
269b11a2
PA
6261 (int) type);
6262
6263 return bptypes[(int) type].description;
6264}
6265
998580f1
MK
6266/* For MI, output a field named 'thread-groups' with a list as the value.
6267 For CLI, prefix the list with the string 'inf'. */
6268
6269static void
6270output_thread_groups (struct ui_out *uiout,
6271 const char *field_name,
5c632425 6272 const std::vector<int> &inf_nums,
998580f1
MK
6273 int mi_only)
6274{
112e8700 6275 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6276
6277 /* For backward compatibility, don't display inferiors in CLI unless
6278 there are several. Always display them for MI. */
6279 if (!is_mi && mi_only)
6280 return;
6281
10f489e5 6282 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6283
5c632425 6284 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6285 {
6286 if (is_mi)
6287 {
6288 char mi_group[10];
6289
5c632425 6290 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6291 uiout->field_string (NULL, mi_group);
998580f1
MK
6292 }
6293 else
6294 {
6295 if (i == 0)
112e8700 6296 uiout->text (" inf ");
998580f1 6297 else
112e8700 6298 uiout->text (", ");
998580f1 6299
5c632425 6300 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6301 }
6302 }
998580f1
MK
6303}
6304
9db0d853
SM
6305/* See breakpoint.h. */
6306
6307bool fix_breakpoint_script_output_globally = false;
6308
a38118e5
PA
6309/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6310 instead of going via breakpoint_ops::print_one. This makes "maint
6311 info breakpoints" show the software breakpoint locations of
6312 catchpoints, which are considered internal implementation
c01e038b
TT
6313 detail. Returns true if RAW_LOC is false and if the breakpoint's
6314 print_one method did something; false otherwise. */
269b11a2 6315
c01e038b 6316static bool
269b11a2
PA
6317print_one_breakpoint_location (struct breakpoint *b,
6318 struct bp_location *loc,
6319 int loc_number,
6320 struct bp_location **last_loc,
a38118e5 6321 int allflag, bool raw_loc)
269b11a2
PA
6322{
6323 struct command_line *l;
c2c6d25f 6324 static char bpenables[] = "nynny";
c906108c 6325
79a45e25 6326 struct ui_out *uiout = current_uiout;
d8de7963
AB
6327 bool header_of_multiple = false;
6328 bool part_of_multiple = (loc != NULL);
79a45b7d
TT
6329 struct value_print_options opts;
6330
6331 get_user_print_options (&opts);
0d381245
VP
6332
6333 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6334 /* See comment in print_one_breakpoint concerning treatment of
6335 breakpoints with single disabled location. */
0d381245
VP
6336 if (loc == NULL
6337 && (b->loc != NULL
b5fa468f
TBA
6338 && (b->loc->next != NULL
6339 || !b->loc->enabled || b->loc->disabled_by_cond)))
d8de7963 6340 header_of_multiple = true;
0d381245
VP
6341 if (loc == NULL)
6342 loc = b->loc;
6343
c4093a6a
JM
6344 annotate_record ();
6345
6346 /* 1 */
6347 annotate_field (0);
0d381245 6348 if (part_of_multiple)
528e1572 6349 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6350 else
381befee 6351 uiout->field_signed ("number", b->number);
c4093a6a
JM
6352
6353 /* 2 */
6354 annotate_field (1);
0d381245 6355 if (part_of_multiple)
112e8700 6356 uiout->field_skip ("type");
269b11a2 6357 else
112e8700 6358 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6359
6360 /* 3 */
6361 annotate_field (2);
0d381245 6362 if (part_of_multiple)
112e8700 6363 uiout->field_skip ("disp");
0d381245 6364 else
112e8700 6365 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6366
c4093a6a
JM
6367 /* 4 */
6368 annotate_field (3);
0d381245 6369 if (part_of_multiple)
fbcda577
PA
6370 {
6371 /* For locations that are disabled because of an invalid
6372 condition, display "N*" on the CLI, where "*" refers to a
6373 footnote below the table. For MI, simply display a "N"
6374 without a footnote. On the CLI, for enabled locations whose
6375 breakpoint is disabled, display "y-". */
6376 auto get_enable_state = [uiout, loc] () -> const char *
6377 {
6378 if (uiout->is_mi_like_p ())
6379 {
6380 if (loc->disabled_by_cond)
6381 return "N";
6382 else if (!loc->enabled)
6383 return "n";
6384 else
6385 return "y";
6386 }
6387 else
6388 {
6389 if (loc->disabled_by_cond)
6390 return "N*";
6391 else if (!loc->enabled)
6392 return "n";
6393 else if (!breakpoint_enabled (loc->owner))
6394 return "y-";
6395 else
6396 return "y";
6397 }
6398 };
6399 uiout->field_string ("enabled", get_enable_state ());
6400 }
0d381245 6401 else
112e8700 6402 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6403
c4093a6a 6404 /* 5 and 6 */
c01e038b 6405 bool result = false;
c47614fe 6406 if (!raw_loc && b->print_one (last_loc))
c01e038b 6407 result = true;
3086aeae 6408 else
a38118e5
PA
6409 {
6410 if (is_watchpoint (b))
3a5c3e22
PA
6411 {
6412 struct watchpoint *w = (struct watchpoint *) b;
6413
6414 /* Field 4, the address, is omitted (which makes the columns
6415 not line up too nicely with the headers, but the effect
6416 is relatively readable). */
6417 if (opts.addressprint)
112e8700 6418 uiout->field_skip ("addr");
3a5c3e22 6419 annotate_field (5);
a4c50be3 6420 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6421 }
f06f1252
TT
6422 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6423 || is_ada_exception_catchpoint (b))
a38118e5
PA
6424 {
6425 if (opts.addressprint)
6426 {
6427 annotate_field (4);
6428 if (header_of_multiple)
7f6aba03
TT
6429 uiout->field_string ("addr", "<MULTIPLE>",
6430 metadata_style.style ());
a38118e5 6431 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6432 uiout->field_string ("addr", "<PENDING>",
6433 metadata_style.style ());
a38118e5
PA
6434 else
6435 uiout->field_core_addr ("addr",
6436 loc->gdbarch, loc->address);
6437 }
6438 annotate_field (5);
6439 if (!header_of_multiple)
6440 print_breakpoint_location (b, loc);
6441 if (b->loc)
6442 *last_loc = b->loc;
6443 }
6444 }
6c95b8df 6445
998580f1 6446 if (loc != NULL && !header_of_multiple)
6c95b8df 6447 {
5c632425 6448 std::vector<int> inf_nums;
998580f1 6449 int mi_only = 1;
6c95b8df 6450
08036331 6451 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6452 {
6453 if (inf->pspace == loc->pspace)
5c632425 6454 inf_nums.push_back (inf->num);
6c95b8df 6455 }
998580f1 6456
dda83cd7 6457 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6458 there are several. Always display for MI. */
6459 if (allflag
6460 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6461 && (program_spaces.size () > 1
998580f1
MK
6462 || number_of_inferiors () > 1)
6463 /* LOC is for existing B, it cannot be in
6464 moribund_locations and thus having NULL OWNER. */
6465 && loc->owner->type != bp_catchpoint))
6466 mi_only = 0;
5c632425 6467 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6468 }
6469
4a306c9a 6470 if (!part_of_multiple)
c4093a6a 6471 {
4a306c9a
JB
6472 if (b->thread != -1)
6473 {
6474 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6475 "stop only in" line a little further down. */
112e8700 6476 uiout->text (" thread ");
381befee 6477 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6478 }
6479 else if (b->task != 0)
6480 {
112e8700 6481 uiout->text (" task ");
381befee 6482 uiout->field_signed ("task", b->task);
4a306c9a 6483 }
c4093a6a 6484 }
f1310107 6485
112e8700 6486 uiout->text ("\n");
f1310107 6487
348d480f 6488 if (!part_of_multiple)
c47614fe 6489 b->print_one_detail (uiout);
f1310107 6490
0d381245 6491 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6492 {
6493 annotate_field (6);
112e8700 6494 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6495 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6496 the frame ID. */
112e8700 6497 uiout->field_core_addr ("frame",
5af949e3 6498 b->gdbarch, b->frame_id.stack_addr);
112e8700 6499 uiout->text ("\n");
c4093a6a
JM
6500 }
6501
28010a5d 6502 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6503 {
6504 annotate_field (7);
d77f58be 6505 if (is_tracepoint (b))
112e8700 6506 uiout->text ("\ttrace only if ");
1042e4c0 6507 else
112e8700 6508 uiout->text ("\tstop only if ");
6f781ee3 6509 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6510
6511 /* Print whether the target is doing the breakpoint's condition
6512 evaluation. If GDB is doing the evaluation, don't print anything. */
6513 if (is_breakpoint (b)
6514 && breakpoint_condition_evaluation_mode ()
6515 == condition_evaluation_target)
6516 {
6a831f06
PA
6517 uiout->message (" (%pF evals)",
6518 string_field ("evaluated-by",
6519 bp_condition_evaluator (b)));
b775012e 6520 }
112e8700 6521 uiout->text ("\n");
0101ce28
JJ
6522 }
6523
0d381245 6524 if (!part_of_multiple && b->thread != -1)
c4093a6a 6525 {
4a64f543 6526 /* FIXME should make an annotation for this. */
112e8700
SM
6527 uiout->text ("\tstop only in thread ");
6528 if (uiout->is_mi_like_p ())
381befee 6529 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6530 else
6531 {
6532 struct thread_info *thr = find_thread_global_id (b->thread);
6533
112e8700 6534 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6535 }
112e8700 6536 uiout->text ("\n");
c4093a6a
JM
6537 }
6538
556ec64d
YQ
6539 if (!part_of_multiple)
6540 {
6541 if (b->hit_count)
31f56a27
YQ
6542 {
6543 /* FIXME should make an annotation for this. */
6544 if (is_catchpoint (b))
112e8700 6545 uiout->text ("\tcatchpoint");
31f56a27 6546 else if (is_tracepoint (b))
112e8700 6547 uiout->text ("\ttracepoint");
31f56a27 6548 else
112e8700
SM
6549 uiout->text ("\tbreakpoint");
6550 uiout->text (" already hit ");
381befee 6551 uiout->field_signed ("times", b->hit_count);
31f56a27 6552 if (b->hit_count == 1)
112e8700 6553 uiout->text (" time\n");
31f56a27 6554 else
112e8700 6555 uiout->text (" times\n");
31f56a27 6556 }
556ec64d
YQ
6557 else
6558 {
31f56a27 6559 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6560 if (uiout->is_mi_like_p ())
381befee 6561 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6562 }
6563 }
8b93c638 6564
0d381245 6565 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6566 {
6567 annotate_field (8);
6a831f06
PA
6568 uiout->message ("\tignore next %pF hits\n",
6569 signed_field ("ignore", b->ignore_count));
c4093a6a 6570 }
059fb39f 6571
816338b5
SS
6572 /* Note that an enable count of 1 corresponds to "enable once"
6573 behavior, which is reported by the combination of enablement and
6574 disposition, so we don't need to mention it here. */
6575 if (!part_of_multiple && b->enable_count > 1)
6576 {
6577 annotate_field (8);
112e8700 6578 uiout->text ("\tdisable after ");
816338b5
SS
6579 /* Tweak the wording to clarify that ignore and enable counts
6580 are distinct, and have additive effect. */
6581 if (b->ignore_count)
112e8700 6582 uiout->text ("additional ");
816338b5 6583 else
112e8700 6584 uiout->text ("next ");
381befee 6585 uiout->field_signed ("enable", b->enable_count);
112e8700 6586 uiout->text (" hits\n");
816338b5
SS
6587 }
6588
f196051f
SS
6589 if (!part_of_multiple && is_tracepoint (b))
6590 {
6591 struct tracepoint *tp = (struct tracepoint *) b;
6592
6593 if (tp->traceframe_usage)
6594 {
112e8700 6595 uiout->text ("\ttrace buffer usage ");
381befee 6596 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6597 uiout->text (" bytes\n");
f196051f
SS
6598 }
6599 }
d3ce09f5 6600
d1b0a7bf 6601 l = b->commands ? b->commands.get () : NULL;
059fb39f 6602 if (!part_of_multiple && l)
c4093a6a
JM
6603 {
6604 annotate_field (9);
9db0d853
SM
6605
6606 bool use_fixed_output =
6607 (uiout->test_flags (fix_breakpoint_script_output)
6608 || fix_breakpoint_script_output_globally);
6609
6610 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6611 gdb::optional<ui_out_emit_list> list_emitter;
6612
6613 if (use_fixed_output)
6614 list_emitter.emplace (uiout, "script");
6615 else
6616 tuple_emitter.emplace (uiout, "script");
6617
8b93c638 6618 print_command_lines (uiout, l, 4);
c4093a6a 6619 }
d24317b4 6620
d9b3f62e 6621 if (is_tracepoint (b))
1042e4c0 6622 {
d9b3f62e
PA
6623 struct tracepoint *t = (struct tracepoint *) b;
6624
6625 if (!part_of_multiple && t->pass_count)
6626 {
6627 annotate_field (10);
112e8700 6628 uiout->text ("\tpass count ");
381befee 6629 uiout->field_signed ("pass", t->pass_count);
112e8700 6630 uiout->text (" \n");
d9b3f62e 6631 }
f2a8bc8a
YQ
6632
6633 /* Don't display it when tracepoint or tracepoint location is
6634 pending. */
6635 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6636 {
6637 annotate_field (11);
6638
112e8700
SM
6639 if (uiout->is_mi_like_p ())
6640 uiout->field_string ("installed",
f2a8bc8a
YQ
6641 loc->inserted ? "y" : "n");
6642 else
6643 {
6644 if (loc->inserted)
112e8700 6645 uiout->text ("\t");
f2a8bc8a 6646 else
112e8700
SM
6647 uiout->text ("\tnot ");
6648 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6649 }
6650 }
1042e4c0
SS
6651 }
6652
112e8700 6653 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6654 {
3a5c3e22
PA
6655 if (is_watchpoint (b))
6656 {
6657 struct watchpoint *w = (struct watchpoint *) b;
6658
a4c50be3 6659 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6660 }
264f9890
PA
6661 else if (b->locspec != nullptr)
6662 {
709438c7 6663 const char *str = b->locspec->to_string ();
264f9890
PA
6664 if (str != nullptr)
6665 uiout->field_string ("original-location", str);
6666 }
d24317b4 6667 }
c01e038b
TT
6668
6669 return result;
c4093a6a 6670}
c5aa993b 6671
13674803
SM
6672/* See breakpoint.h. */
6673
6674bool fix_multi_location_breakpoint_output_globally = false;
6675
0d381245
VP
6676static void
6677print_one_breakpoint (struct breakpoint *b,
4a64f543 6678 struct bp_location **last_loc,
6c95b8df 6679 int allflag)
0d381245 6680{
79a45e25 6681 struct ui_out *uiout = current_uiout;
13674803
SM
6682 bool use_fixed_output
6683 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6684 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6685
b4be1b06 6686 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
c01e038b
TT
6687 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6688 allflag, false);
8d3788bd 6689
b4be1b06
SM
6690 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6691 are outside. */
6692 if (!use_fixed_output)
6693 bkpt_tuple_emitter.reset ();
0d381245
VP
6694
6695 /* If this breakpoint has custom print function,
6696 it's already printed. Otherwise, print individual
6697 locations, if any. */
c01e038b 6698 if (!printed || allflag)
0d381245 6699 {
4a64f543
MS
6700 /* If breakpoint has a single location that is disabled, we
6701 print it as if it had several locations, since otherwise it's
6702 hard to represent "breakpoint enabled, location disabled"
6703 situation.
6704
6705 Note that while hardware watchpoints have several locations
a38118e5
PA
6706 internally, that's not a property exposed to users.
6707
6708 Likewise, while catchpoints may be implemented with
6709 breakpoints (e.g., catch throw), that's not a property
6710 exposed to users. We do however display the internal
6711 breakpoint locations with "maint info breakpoints". */
6712 if (!is_hardware_watchpoint (b)
f06f1252
TT
6713 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6714 || is_ada_exception_catchpoint (b))
a38118e5 6715 && (allflag
b5fa468f
TBA
6716 || (b->loc && (b->loc->next
6717 || !b->loc->enabled
6718 || b->loc->disabled_by_cond))))
0d381245 6719 {
b4be1b06
SM
6720 gdb::optional<ui_out_emit_list> locations_list;
6721
6722 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6723 MI record. For later versions, place breakpoint locations in a
6724 list. */
6725 if (uiout->is_mi_like_p () && use_fixed_output)
6726 locations_list.emplace (uiout, "locations");
8d3788bd 6727
b4be1b06 6728 int n = 1;
40cb8ca5 6729 for (bp_location *loc : b->locations ())
8d3788bd 6730 {
b4be1b06 6731 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6732 print_one_breakpoint_location (b, loc, n, last_loc,
6733 allflag, allflag);
40cb8ca5 6734 n++;
8d3788bd 6735 }
0d381245
VP
6736 }
6737 }
6738}
6739
a6d9a66e
UW
6740static int
6741breakpoint_address_bits (struct breakpoint *b)
6742{
6743 int print_address_bits = 0;
a6d9a66e 6744
40cb8ca5 6745 for (bp_location *loc : b->locations ())
a6d9a66e 6746 {
6e14e441
PA
6747 if (!bl_address_is_meaningful (loc))
6748 continue;
c7437ca6 6749
6e14e441 6750 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6751 if (addr_bit > print_address_bits)
6752 print_address_bits = addr_bit;
6753 }
6754
6755 return print_address_bits;
6756}
0d381245 6757
65630365 6758/* See breakpoint.h. */
c5aa993b 6759
65630365
PA
6760void
6761print_breakpoint (breakpoint *b)
c4093a6a 6762{
a6d9a66e 6763 struct bp_location *dummy_loc = NULL;
65630365 6764 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6765}
c5aa993b 6766
09d682a4
TT
6767/* Return true if this breakpoint was set by the user, false if it is
6768 internal or momentary. */
6769
6770int
6771user_breakpoint_p (struct breakpoint *b)
6772{
46c6471b 6773 return b->number > 0;
09d682a4
TT
6774}
6775
93daf339
TT
6776/* See breakpoint.h. */
6777
6778int
6779pending_breakpoint_p (struct breakpoint *b)
6780{
6781 return b->loc == NULL;
6782}
6783
5c458ae8
SM
6784/* Print information on breakpoints (including watchpoints and tracepoints).
6785
6786 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6787 understood by number_or_range_parser. Only breakpoints included in this
6788 list are then printed.
6789
6790 If SHOW_INTERNAL is true, print internal breakpoints.
6791
6792 If FILTER is non-NULL, call it on each breakpoint and only include the
6793 ones for which it returns true.
6794
6795 Return the total number of breakpoints listed. */
c906108c 6796
d77f58be 6797static int
5c458ae8 6798breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6799 bool (*filter) (const struct breakpoint *))
c4093a6a 6800{
a6d9a66e 6801 struct bp_location *last_loc = NULL;
7f3b0473 6802 int nr_printable_breakpoints;
79a45b7d 6803 struct value_print_options opts;
a6d9a66e 6804 int print_address_bits = 0;
269b11a2 6805 int print_type_col_width = 14;
79a45e25 6806 struct ui_out *uiout = current_uiout;
b5fa468f 6807 bool has_disabled_by_cond_location = false;
269b11a2 6808
79a45b7d
TT
6809 get_user_print_options (&opts);
6810
4a64f543
MS
6811 /* Compute the number of rows in the table, as well as the size
6812 required for address fields. */
7f3b0473 6813 nr_printable_breakpoints = 0;
43892fdf 6814 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6815 {
6816 /* If we have a filter, only list the breakpoints it accepts. */
6817 if (filter && !filter (b))
6818 continue;
6819
5c458ae8 6820 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6821 accept. Skip the others. */
5c458ae8 6822 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6823 {
5c458ae8 6824 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6825 continue;
5c458ae8 6826 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6827 continue;
6828 }
269b11a2 6829
5c458ae8 6830 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6831 {
6832 int addr_bit, type_len;
a6d9a66e 6833
e5a67952
MS
6834 addr_bit = breakpoint_address_bits (b);
6835 if (addr_bit > print_address_bits)
6836 print_address_bits = addr_bit;
269b11a2 6837
e5a67952
MS
6838 type_len = strlen (bptype_string (b->type));
6839 if (type_len > print_type_col_width)
6840 print_type_col_width = type_len;
6841
6842 nr_printable_breakpoints++;
6843 }
6844 }
7f3b0473 6845
4a2b031d
TT
6846 {
6847 ui_out_emit_table table_emitter (uiout,
6848 opts.addressprint ? 6 : 5,
6849 nr_printable_breakpoints,
6850 "BreakpointTable");
6851
6852 if (nr_printable_breakpoints > 0)
6853 annotate_breakpoints_headers ();
6854 if (nr_printable_breakpoints > 0)
6855 annotate_field (0);
6856 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6857 if (nr_printable_breakpoints > 0)
6858 annotate_field (1);
6859 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6860 if (nr_printable_breakpoints > 0)
6861 annotate_field (2);
6862 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6863 if (nr_printable_breakpoints > 0)
6864 annotate_field (3);
6865 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6866 if (opts.addressprint)
6867 {
6868 if (nr_printable_breakpoints > 0)
6869 annotate_field (4);
6870 if (print_address_bits <= 32)
6871 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6872 else
6873 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6874 }
6875 if (nr_printable_breakpoints > 0)
6876 annotate_field (5);
6877 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6878 uiout->table_body ();
6879 if (nr_printable_breakpoints > 0)
6880 annotate_breakpoints_table ();
6881
43892fdf 6882 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6883 {
6884 QUIT;
6885 /* If we have a filter, only list the breakpoints it accepts. */
6886 if (filter && !filter (b))
6887 continue;
e5a67952 6888
5c458ae8 6889 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6890 accept. Skip the others. */
e5a67952 6891
5c458ae8 6892 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6893 {
5c458ae8 6894 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6895 {
5c458ae8 6896 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6897 continue;
6898 }
6899 else /* all others */
6900 {
5c458ae8 6901 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6902 continue;
6903 }
6904 }
6905 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6906 show_internal is set. */
6907 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6908 {
6909 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6910 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6911 if (loc->disabled_by_cond)
6912 has_disabled_by_cond_location = true;
6913 }
4a2b031d
TT
6914 }
6915 }
698384cd 6916
7f3b0473 6917 if (nr_printable_breakpoints == 0)
c906108c 6918 {
4a64f543
MS
6919 /* If there's a filter, let the caller decide how to report
6920 empty list. */
d77f58be
SS
6921 if (!filter)
6922 {
5c458ae8 6923 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6924 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6925 else
112e8700 6926 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6927 bp_num_list);
d77f58be 6928 }
c906108c
SS
6929 }
6930 else
c4093a6a 6931 {
a6d9a66e
UW
6932 if (last_loc && !server_command)
6933 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6934
85c88e2a 6935 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6936 uiout->message (_("(*): Breakpoint condition is invalid at this "
6937 "location.\n"));
c4093a6a 6938 }
c906108c 6939
4a64f543 6940 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6941 there have been breakpoints? */
c906108c 6942 annotate_breakpoints_table_end ();
d77f58be
SS
6943
6944 return nr_printable_breakpoints;
c906108c
SS
6945}
6946
ad443146
SS
6947/* Display the value of default-collect in a way that is generally
6948 compatible with the breakpoint list. */
6949
6950static void
6951default_collect_info (void)
6952{
79a45e25
PA
6953 struct ui_out *uiout = current_uiout;
6954
ad443146
SS
6955 /* If it has no value (which is frequently the case), say nothing; a
6956 message like "No default-collect." gets in user's face when it's
6957 not wanted. */
e0700ba4 6958 if (default_collect.empty ())
ad443146
SS
6959 return;
6960
6961 /* The following phrase lines up nicely with per-tracepoint collect
6962 actions. */
112e8700
SM
6963 uiout->text ("default collect ");
6964 uiout->field_string ("default-collect", default_collect);
6965 uiout->text (" \n");
ad443146
SS
6966}
6967
c906108c 6968static void
0b39b52e 6969info_breakpoints_command (const char *args, int from_tty)
c906108c 6970{
5c458ae8 6971 breakpoint_1 (args, false, NULL);
ad443146
SS
6972
6973 default_collect_info ();
d77f58be
SS
6974}
6975
6976static void
1d12d88f 6977info_watchpoints_command (const char *args, int from_tty)
d77f58be 6978{
5c458ae8 6979 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6980 struct ui_out *uiout = current_uiout;
d77f58be
SS
6981
6982 if (num_printed == 0)
6983 {
e5a67952 6984 if (args == NULL || *args == '\0')
112e8700 6985 uiout->message ("No watchpoints.\n");
d77f58be 6986 else
112e8700 6987 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6988 }
c906108c
SS
6989}
6990
7a292a7a 6991static void
4495129a 6992maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6993{
5c458ae8 6994 breakpoint_1 (args, true, NULL);
ad443146
SS
6995
6996 default_collect_info ();
c906108c
SS
6997}
6998
d8de7963 6999static bool
714835d5 7000breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7001 struct program_space *pspace,
714835d5 7002 CORE_ADDR pc, struct obj_section *section)
0d381245 7003{
40cb8ca5 7004 for (bp_location *bl : b->locations ())
0d381245 7005 {
6c95b8df
PA
7006 if (bl->pspace == pspace
7007 && bl->address == pc
0d381245 7008 && (!overlay_debugging || bl->section == section))
d8de7963 7009 return true;
0d381245 7010 }
d8de7963 7011 return false;
0d381245
VP
7012}
7013
bd21b6c9 7014/* See breakpoint.h. */
c906108c 7015
bd21b6c9 7016void
6c95b8df
PA
7017describe_other_breakpoints (struct gdbarch *gdbarch,
7018 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7019 struct obj_section *section, int thread)
c906108c 7020{
52f0bd74 7021 int others = 0;
c906108c 7022
43892fdf 7023 for (breakpoint *b : all_breakpoints ())
672f9b60 7024 others += (user_breakpoint_p (b)
dda83cd7 7025 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 7026
c906108c
SS
7027 if (others > 0)
7028 {
a3f17187 7029 if (others == 1)
6cb06a8c 7030 gdb_printf (_("Note: breakpoint "));
a3f17187 7031 else /* if (others == ???) */
6cb06a8c 7032 gdb_printf (_("Note: breakpoints "));
43892fdf 7033 for (breakpoint *b : all_breakpoints ())
672f9b60 7034 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7035 {
7036 others--;
6cb06a8c 7037 gdb_printf ("%d", b->number);
0d381245 7038 if (b->thread == -1 && thread != -1)
6cb06a8c 7039 gdb_printf (" (all threads)");
0d381245 7040 else if (b->thread != -1)
6cb06a8c
TT
7041 gdb_printf (" (thread %d)", b->thread);
7042 gdb_printf ("%s%s ",
7043 ((b->enable_state == bp_disabled
7044 || b->enable_state == bp_call_disabled)
7045 ? " (disabled)"
7046 : ""),
7047 (others > 1) ? ","
7048 : ((others == 1) ? " and" : ""));
0d381245 7049 }
6a831f06
PA
7050 current_uiout->message (_("also set at pc %ps.\n"),
7051 styled_string (address_style.style (),
7052 paddress (gdbarch, pc)));
c906108c
SS
7053 }
7054}
7055\f
c906108c 7056
cb1e4e32
PA
7057/* Return true iff it is meaningful to use the address member of LOC.
7058 For some breakpoint types, the locations' address members are
7059 irrelevant and it makes no sense to attempt to compare them to
7060 other addresses (or use them for any other purpose either).
2d134ed3 7061
cb1e4e32
PA
7062 More specifically, software watchpoints and catchpoints that are
7063 not backed by breakpoints always have a zero valued location
7064 address and we don't want to mark breakpoints of any of these types
7065 to be a duplicate of an actual breakpoint location at address
7066 zero. */
e4f237da 7067
cb1e4e32
PA
7068static bool
7069bl_address_is_meaningful (bp_location *loc)
e4f237da 7070{
cb1e4e32 7071 return loc->loc_type != bp_loc_other;
2d134ed3
PA
7072}
7073
7074/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7075 true if LOC1 and LOC2 represent the same watchpoint location. */
7076
d8de7963 7077static bool
b2ff9ed3
AB
7078watchpoint_locations_match (const struct bp_location *loc1,
7079 const struct bp_location *loc2)
2d134ed3 7080{
3a5c3e22
PA
7081 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7082 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7083
7084 /* Both of them must exist. */
7085 gdb_assert (w1 != NULL);
7086 gdb_assert (w2 != NULL);
2bdf28a0 7087
4a64f543
MS
7088 /* If the target can evaluate the condition expression in hardware,
7089 then we we need to insert both watchpoints even if they are at
7090 the same place. Otherwise the watchpoint will only trigger when
7091 the condition of whichever watchpoint was inserted evaluates to
7092 true, not giving a chance for GDB to check the condition of the
7093 other watchpoint. */
3a5c3e22 7094 if ((w1->cond_exp
4a64f543
MS
7095 && target_can_accel_watchpoint_condition (loc1->address,
7096 loc1->length,
0cf6dd15 7097 loc1->watchpoint_type,
4d01a485 7098 w1->cond_exp.get ()))
3a5c3e22 7099 || (w2->cond_exp
4a64f543
MS
7100 && target_can_accel_watchpoint_condition (loc2->address,
7101 loc2->length,
0cf6dd15 7102 loc2->watchpoint_type,
4d01a485 7103 w2->cond_exp.get ())))
d8de7963 7104 return false;
0cf6dd15 7105
85d721b8
PA
7106 /* Note that this checks the owner's type, not the location's. In
7107 case the target does not support read watchpoints, but does
7108 support access watchpoints, we'll have bp_read_watchpoint
7109 watchpoints with hw_access locations. Those should be considered
7110 duplicates of hw_read locations. The hw_read locations will
7111 become hw_access locations later. */
2d134ed3
PA
7112 return (loc1->owner->type == loc2->owner->type
7113 && loc1->pspace->aspace == loc2->pspace->aspace
7114 && loc1->address == loc2->address
7115 && loc1->length == loc2->length);
e4f237da
KB
7116}
7117
31e77af2 7118/* See breakpoint.h. */
6c95b8df 7119
31e77af2 7120int
accd0bcd
YQ
7121breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7122 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 7123{
f5656ead 7124 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7125 || aspace1 == aspace2)
7126 && addr1 == addr2);
7127}
7128
f1310107
TJB
7129/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7130 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7131 matches ASPACE2. On targets that have global breakpoints, the address
7132 space doesn't really matter. */
7133
d8de7963 7134static bool
accd0bcd
YQ
7135breakpoint_address_match_range (const address_space *aspace1,
7136 CORE_ADDR addr1,
7137 int len1, const address_space *aspace2,
f1310107
TJB
7138 CORE_ADDR addr2)
7139{
f5656ead 7140 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7141 || aspace1 == aspace2)
7142 && addr2 >= addr1 && addr2 < addr1 + len1);
7143}
7144
7145/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7146 a ranged breakpoint. In most targets, a match happens only if ASPACE
7147 matches the breakpoint's address space. On targets that have global
7148 breakpoints, the address space doesn't really matter. */
7149
d8de7963 7150static bool
f1310107 7151breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 7152 const address_space *aspace,
f1310107
TJB
7153 CORE_ADDR addr)
7154{
7155 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7156 aspace, addr)
7157 || (bl->length
7158 && breakpoint_address_match_range (bl->pspace->aspace,
7159 bl->address, bl->length,
7160 aspace, addr)));
7161}
7162
d35ae833
PA
7163/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7164 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7165 match happens only if ASPACE matches the breakpoint's address
7166 space. On targets that have global breakpoints, the address space
7167 doesn't really matter. */
7168
d8de7963 7169static bool
d35ae833 7170breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 7171 const address_space *aspace,
d35ae833
PA
7172 CORE_ADDR addr, int len)
7173{
7174 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7175 || bl->pspace->aspace == aspace)
7176 {
7177 int bl_len = bl->length != 0 ? bl->length : 1;
7178
7179 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7180 return 1;
7181 }
7182 return 0;
7183}
7184
1e4d1764
YQ
7185/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7186 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7187 true, otherwise returns false. */
7188
d8de7963 7189static bool
b2ff9ed3
AB
7190tracepoint_locations_match (const struct bp_location *loc1,
7191 const struct bp_location *loc2)
1e4d1764
YQ
7192{
7193 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7194 /* Since tracepoint locations are never duplicated with others', tracepoint
7195 locations at the same address of different tracepoints are regarded as
7196 different locations. */
7197 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7198 else
d8de7963 7199 return false;
1e4d1764
YQ
7200}
7201
2d134ed3 7202/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7203 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7204 the same location. If SW_HW_BPS_MATCH is true, then software
7205 breakpoint locations and hardware breakpoint locations match,
7206 otherwise they don't. */
2d134ed3 7207
d8de7963 7208static bool
b2ff9ed3
AB
7209breakpoint_locations_match (const struct bp_location *loc1,
7210 const struct bp_location *loc2,
7f32a4d5 7211 bool sw_hw_bps_match)
2d134ed3 7212{
2bdf28a0
JK
7213 int hw_point1, hw_point2;
7214
7215 /* Both of them must not be in moribund_locations. */
7216 gdb_assert (loc1->owner != NULL);
7217 gdb_assert (loc2->owner != NULL);
7218
7219 hw_point1 = is_hardware_watchpoint (loc1->owner);
7220 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7221
7222 if (hw_point1 != hw_point2)
d8de7963 7223 return false;
2d134ed3
PA
7224 else if (hw_point1)
7225 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7226 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7227 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7228 else
7f32a4d5
PA
7229 /* We compare bp_location.length in order to cover ranged
7230 breakpoints. Keep this in sync with
7231 bp_location_is_less_than. */
f1310107
TJB
7232 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7233 loc2->pspace->aspace, loc2->address)
7f32a4d5 7234 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7235 && loc1->length == loc2->length);
2d134ed3
PA
7236}
7237
76897487
KB
7238static void
7239breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
d8de7963 7240 int bnum, bool have_bnum)
76897487 7241{
f63fbe86
MS
7242 /* The longest string possibly returned by hex_string_custom
7243 is 50 chars. These must be at least that big for safety. */
7244 char astr1[64];
7245 char astr2[64];
76897487 7246
bb599908
PH
7247 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7248 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7249 if (have_bnum)
8a3fe4f8 7250 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7251 bnum, astr1, astr2);
76897487 7252 else
8a3fe4f8 7253 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7254}
7255
4a64f543
MS
7256/* Adjust a breakpoint's address to account for architectural
7257 constraints on breakpoint placement. Return the adjusted address.
7258 Note: Very few targets require this kind of adjustment. For most
7259 targets, this function is simply the identity function. */
76897487
KB
7260
7261static CORE_ADDR
a6d9a66e 7262adjust_breakpoint_address (struct gdbarch *gdbarch,
60270718
AB
7263 CORE_ADDR bpaddr, enum bptype bptype,
7264 struct program_space *pspace)
76897487 7265{
60270718
AB
7266 gdb_assert (pspace != nullptr);
7267
a0de8c21
YQ
7268 if (bptype == bp_watchpoint
7269 || bptype == bp_hardware_watchpoint
7270 || bptype == bp_read_watchpoint
7271 || bptype == bp_access_watchpoint
7272 || bptype == bp_catchpoint)
88f7da05
KB
7273 {
7274 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7275 have their addresses modified. */
88f7da05
KB
7276 return bpaddr;
7277 }
7c16b83e
PA
7278 else if (bptype == bp_single_step)
7279 {
7280 /* Single-step breakpoints should not have their addresses
7281 modified. If there's any architectural constrain that
7282 applies to this address, then it should have already been
7283 taken into account when the breakpoint was created in the
7284 first place. If we didn't do this, stepping through e.g.,
7285 Thumb-2 IT blocks would break. */
7286 return bpaddr;
7287 }
76897487
KB
7288 else
7289 {
a0de8c21
YQ
7290 CORE_ADDR adjusted_bpaddr = bpaddr;
7291
60270718
AB
7292 /* Some targets have architectural constraints on the placement
7293 of breakpoint instructions. Obtain the adjusted address. */
a0de8c21
YQ
7294 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7295 {
60270718
AB
7296 /* Targets that implement this adjustment function will likely
7297 inspect either the symbol table, target memory at BPADDR, or
7298 even state registers, so ensure a suitable thread (and its
7299 associated program space) are currently selected. */
7300 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7301 switch_to_program_space_and_thread (pspace);
7302 adjusted_bpaddr
7303 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
a0de8c21 7304 }
76897487 7305
a0de8c21 7306 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7307
7308 /* An adjusted breakpoint address can significantly alter
dda83cd7 7309 a user's expectations. Print a warning if an adjustment
76897487
KB
7310 is required. */
7311 if (adjusted_bpaddr != bpaddr)
d8de7963 7312 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
76897487
KB
7313
7314 return adjusted_bpaddr;
7315 }
7316}
7317
cb1e4e32
PA
7318static bp_loc_type
7319bp_location_from_bp_type (bptype type)
7cc221ef 7320{
cb1e4e32 7321 switch (type)
e049a4b5
DJ
7322 {
7323 case bp_breakpoint:
7c16b83e 7324 case bp_single_step:
e049a4b5
DJ
7325 case bp_until:
7326 case bp_finish:
7327 case bp_longjmp:
7328 case bp_longjmp_resume:
e2e4d78b 7329 case bp_longjmp_call_dummy:
186c406b
TT
7330 case bp_exception:
7331 case bp_exception_resume:
e049a4b5 7332 case bp_step_resume:
2c03e5be 7333 case bp_hp_step_resume:
e049a4b5
DJ
7334 case bp_watchpoint_scope:
7335 case bp_call_dummy:
aa7d318d 7336 case bp_std_terminate:
e049a4b5
DJ
7337 case bp_shlib_event:
7338 case bp_thread_event:
7339 case bp_overlay_event:
4efc6507 7340 case bp_jit_event:
0fd8e87f 7341 case bp_longjmp_master:
aa7d318d 7342 case bp_std_terminate_master:
186c406b 7343 case bp_exception_master:
0e30163f
JK
7344 case bp_gnu_ifunc_resolver:
7345 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7346 case bp_dprintf:
cb1e4e32 7347 return bp_loc_software_breakpoint;
e049a4b5 7348 case bp_hardware_breakpoint:
cb1e4e32 7349 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7350 case bp_hardware_watchpoint:
7351 case bp_read_watchpoint:
7352 case bp_access_watchpoint:
cb1e4e32 7353 return bp_loc_hardware_watchpoint;
e049a4b5 7354 case bp_watchpoint:
6e14e441 7355 return bp_loc_software_watchpoint;
ce78b96d 7356 case bp_catchpoint:
15c3d785
PA
7357 case bp_tracepoint:
7358 case bp_fast_tracepoint:
0fb4aa4b 7359 case bp_static_tracepoint:
7b572efb 7360 case bp_static_marker_tracepoint:
cb1e4e32 7361 return bp_loc_other;
e049a4b5 7362 default:
f34652de 7363 internal_error (_("unknown breakpoint type"));
e049a4b5 7364 }
cb1e4e32
PA
7365}
7366
7367bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7368{
7369 this->owner = owner;
7370 this->cond_bytecode = NULL;
7371 this->shlib_disabled = 0;
7372 this->enabled = 1;
b5fa468f 7373 this->disabled_by_cond = false;
cb1e4e32
PA
7374
7375 this->loc_type = type;
e049a4b5 7376
cb1e4e32
PA
7377 if (this->loc_type == bp_loc_software_breakpoint
7378 || this->loc_type == bp_loc_hardware_breakpoint)
7379 mark_breakpoint_location_modified (this);
7380
b6433ede 7381 incref ();
cb1e4e32
PA
7382}
7383
7384bp_location::bp_location (breakpoint *owner)
7385 : bp_location::bp_location (owner,
7386 bp_location_from_bp_type (owner->type))
7387{
28010a5d
PA
7388}
7389
f431efe5
PA
7390/* Decrement reference count. If the reference count reaches 0,
7391 destroy the bp_location. Sets *BLP to NULL. */
7392
7393static void
7394decref_bp_location (struct bp_location **blp)
7395{
b6433ede 7396 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7397 *blp = NULL;
7398}
7399
346774a9 7400/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7401
b270e6f9
TT
7402static breakpoint *
7403add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7404{
346774a9 7405 struct breakpoint *b1;
b270e6f9 7406 struct breakpoint *result = b.get ();
c906108c 7407
346774a9
PA
7408 /* Add this breakpoint to the end of the chain so that a list of
7409 breakpoints will come out in order of increasing numbers. */
7410
7411 b1 = breakpoint_chain;
7412 if (b1 == 0)
b270e6f9 7413 breakpoint_chain = b.release ();
346774a9
PA
7414 else
7415 {
7416 while (b1->next)
7417 b1 = b1->next;
b270e6f9 7418 b1->next = b.release ();
346774a9 7419 }
b270e6f9
TT
7420
7421 return result;
346774a9
PA
7422}
7423
0ba852ab 7424/* Initialize loc->function_name. */
0e30163f 7425
0d381245 7426static void
0ba852ab 7427set_breakpoint_location_function (struct bp_location *loc)
0d381245 7428{
2bdf28a0
JK
7429 gdb_assert (loc->owner != NULL);
7430
0d381245 7431 if (loc->owner->type == bp_breakpoint
1042e4c0 7432 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7433 || is_tracepoint (loc->owner))
0d381245 7434 {
2c02bd72 7435 const char *function_name;
0e30163f 7436
3467ec66 7437 if (loc->msymbol != NULL
60f62e2b
SM
7438 && (loc->msymbol->type () == mst_text_gnu_ifunc
7439 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7440 {
7441 struct breakpoint *b = loc->owner;
7442
c9d95fa3 7443 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7444
7445 if (b->type == bp_breakpoint && b->loc == loc
7446 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7447 {
7448 /* Create only the whole new breakpoint of this type but do not
7449 mess more complicated breakpoints with multiple locations. */
7450 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7451 /* Remember the resolver's address for use by the return
dda83cd7 7452 breakpoint. */
3467ec66 7453 loc->related_address = loc->address;
0e30163f
JK
7454 }
7455 }
3467ec66
PA
7456 else
7457 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7458
2c02bd72 7459 if (function_name)
23d6ee64 7460 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7461 }
7462}
7463
a6d9a66e 7464/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7465struct gdbarch *
a6d9a66e
UW
7466get_sal_arch (struct symtab_and_line sal)
7467{
7468 if (sal.section)
08feed99 7469 return sal.section->objfile->arch ();
a6d9a66e 7470 if (sal.symtab)
3c86fae3 7471 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7472
7473 return NULL;
7474}
7475
53a5351d 7476/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7477 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7478 initiated the operation. */
c906108c
SS
7479
7480void
186c406b 7481set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7482{
5d5658a1 7483 int thread = tp->global_num;
0fd8e87f
UW
7484
7485 /* To avoid having to rescan all objfile symbols at every step,
7486 we maintain a list of continually-inserted but always disabled
7487 longjmp "master" breakpoints. Here, we simply create momentary
7488 clones of those and enable them for the requested thread. */
1428b37a 7489 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7490 if (b->pspace == current_program_space
186c406b
TT
7491 && (b->type == bp_longjmp_master
7492 || b->type == bp_exception_master))
0fd8e87f 7493 {
06edf0c0 7494 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
e2e4d78b
JK
7495 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7496 after their removal. */
7ab97995 7497 momentary_breakpoint_from_master (b, type, 1, thread);
0fd8e87f 7498 }
186c406b
TT
7499
7500 tp->initiating_frame = frame;
c906108c
SS
7501}
7502
611c83ae 7503/* Delete all longjmp breakpoints from THREAD. */
c906108c 7504void
611c83ae 7505delete_longjmp_breakpoint (int thread)
c906108c 7506{
1428b37a 7507 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7508 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7509 {
7510 if (b->thread == thread)
7511 delete_breakpoint (b);
7512 }
c906108c
SS
7513}
7514
f59f708a
PA
7515void
7516delete_longjmp_breakpoint_at_next_stop (int thread)
7517{
1428b37a 7518 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7519 if (b->type == bp_longjmp || b->type == bp_exception)
7520 {
7521 if (b->thread == thread)
7522 b->disposition = disp_del_at_next_stop;
7523 }
7524}
7525
e2e4d78b
JK
7526/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7527 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7528 pointer to any of them. Return NULL if this system cannot place longjmp
7529 breakpoints. */
7530
7531struct breakpoint *
7532set_longjmp_breakpoint_for_call_dummy (void)
7533{
43892fdf 7534 breakpoint *retval = nullptr;
e2e4d78b 7535
43892fdf 7536 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7537 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7538 {
7ab97995
PA
7539 int thread = inferior_thread ()->global_num;
7540 breakpoint *new_b
7541 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7542 1, thread);
e2e4d78b
JK
7543
7544 /* Link NEW_B into the chain of RETVAL breakpoints. */
7545
7546 gdb_assert (new_b->related_breakpoint == new_b);
7547 if (retval == NULL)
7548 retval = new_b;
7549 new_b->related_breakpoint = retval;
7550 while (retval->related_breakpoint != new_b->related_breakpoint)
7551 retval = retval->related_breakpoint;
7552 retval->related_breakpoint = new_b;
7553 }
7554
7555 return retval;
7556}
7557
7558/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7559 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7560 stack.
7561
b4b3e2de
AB
7562 If the unwind fails then there is not sufficient information to discard
7563 dummy frames. In this case, elide the clean up and the dummy frames will
7564 be cleaned up next time this function is called from a location where
7565 unwinding is possible. */
e2e4d78b
JK
7566
7567void
b67a2c6f 7568check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7569{
7570 struct breakpoint *b, *b_tmp;
7571
7572 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7573 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7574 {
7575 struct breakpoint *dummy_b = b->related_breakpoint;
7576
b4b3e2de
AB
7577 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7578 chained off b->related_breakpoint. */
e2e4d78b
JK
7579 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7580 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7581
7582 /* If there was no bp_call_dummy breakpoint then there's nothing
7583 more to do. Or, if the dummy frame associated with the
7584 bp_call_dummy is still on the stack then we need to leave this
7585 bp_call_dummy in place. */
e2e4d78b
JK
7586 if (dummy_b->type != bp_call_dummy
7587 || frame_find_by_id (dummy_b->frame_id) != NULL)
7588 continue;
b4b3e2de
AB
7589
7590 /* We didn't find the dummy frame on the stack, this could be
7591 because we have longjmp'd to a stack frame that is previous to
7592 the dummy frame, or it could be because the stack unwind is
7593 broken at some point between the longjmp frame and the dummy
7594 frame.
7595
7596 Next we figure out why the stack unwind stopped. If it looks
7597 like the unwind is complete then we assume the dummy frame has
7598 been jumped over, however, if the unwind stopped for an
7599 unexpected reason then we assume the stack unwind is currently
7600 broken, and that we will (eventually) return to the dummy
7601 frame.
7602
7603 It might be tempting to consider using frame_id_inner here, but
7604 that is not safe. There is no guarantee that the stack frames
7605 we are looking at here are even on the same stack as the
7606 original dummy frame, hence frame_id_inner can't be used. See
7607 the comments on frame_id_inner for more details. */
7608 bool unwind_finished_unexpectedly = false;
bd2b40ac 7609 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
b4b3e2de 7610 {
bd2b40ac 7611 frame_info_ptr prev = get_prev_frame (fi);
b4b3e2de
AB
7612 if (prev == nullptr)
7613 {
7614 /* FI is the last stack frame. Why did this frame not
7615 unwind further? */
7616 auto stop_reason = get_frame_unwind_stop_reason (fi);
7617 if (stop_reason != UNWIND_NO_REASON
7618 && stop_reason != UNWIND_OUTERMOST)
7619 unwind_finished_unexpectedly = true;
7620 }
7621 fi = prev;
7622 }
7623 if (unwind_finished_unexpectedly)
7624 continue;
7625
00431a78 7626 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7627
7628 while (b->related_breakpoint != b)
7629 {
7630 if (b_tmp == b->related_breakpoint)
7631 b_tmp = b->related_breakpoint->next;
7632 delete_breakpoint (b->related_breakpoint);
7633 }
7634 delete_breakpoint (b);
7635 }
7636}
7637
1900040c
MS
7638void
7639enable_overlay_breakpoints (void)
7640{
43892fdf 7641 for (breakpoint *b : all_breakpoints ())
1900040c 7642 if (b->type == bp_overlay_event)
01add95b
SM
7643 {
7644 b->enable_state = bp_enabled;
7645 update_global_location_list (UGLL_MAY_INSERT);
7646 overlay_events_enabled = 1;
7647 }
1900040c
MS
7648}
7649
7650void
7651disable_overlay_breakpoints (void)
7652{
43892fdf 7653 for (breakpoint *b : all_breakpoints ())
1900040c 7654 if (b->type == bp_overlay_event)
01add95b
SM
7655 {
7656 b->enable_state = bp_disabled;
7657 update_global_location_list (UGLL_DONT_INSERT);
7658 overlay_events_enabled = 0;
7659 }
1900040c
MS
7660}
7661
aa7d318d
TT
7662/* Set an active std::terminate breakpoint for each std::terminate
7663 master breakpoint. */
7664void
7665set_std_terminate_breakpoint (void)
7666{
1428b37a 7667 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7668 if (b->pspace == current_program_space
7669 && b->type == bp_std_terminate_master)
7670 {
7ab97995
PA
7671 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7672 inferior_thread ()->global_num);
aa7d318d
TT
7673 }
7674}
7675
7676/* Delete all the std::terminate breakpoints. */
7677void
7678delete_std_terminate_breakpoint (void)
7679{
1428b37a 7680 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7681 if (b->type == bp_std_terminate)
7682 delete_breakpoint (b);
7683}
7684
c4093a6a 7685struct breakpoint *
a6d9a66e 7686create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7687{
7688 struct breakpoint *b;
c4093a6a 7689
9efa3c7f 7690 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 7691
b5de0fa7 7692 b->enable_state = bp_enabled;
264f9890
PA
7693 /* locspec has to be used or breakpoint_re_set will delete me. */
7694 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
c4093a6a 7695
44702360 7696 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7697
c4093a6a
JM
7698 return b;
7699}
7700
0101ce28
JJ
7701struct lang_and_radix
7702 {
7703 enum language lang;
7704 int radix;
7705 };
7706
4efc6507
DE
7707/* Create a breakpoint for JIT code registration and unregistration. */
7708
7709struct breakpoint *
7710create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7711{
9efa3c7f 7712 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 7713}
0101ce28 7714
03673fc7
PP
7715/* Remove JIT code registration and unregistration breakpoint(s). */
7716
7717void
7718remove_jit_event_breakpoints (void)
7719{
1428b37a 7720 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7721 if (b->type == bp_jit_event
7722 && b->loc->pspace == current_program_space)
7723 delete_breakpoint (b);
7724}
7725
cae688ec
JJ
7726void
7727remove_solib_event_breakpoints (void)
7728{
1428b37a 7729 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7730 if (b->type == bp_shlib_event
7731 && b->loc->pspace == current_program_space)
cae688ec
JJ
7732 delete_breakpoint (b);
7733}
7734
f37f681c
PA
7735/* See breakpoint.h. */
7736
7737void
7738remove_solib_event_breakpoints_at_next_stop (void)
7739{
1428b37a 7740 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7741 if (b->type == bp_shlib_event
7742 && b->loc->pspace == current_program_space)
7743 b->disposition = disp_del_at_next_stop;
7744}
7745
04086b45
PA
7746/* Helper for create_solib_event_breakpoint /
7747 create_and_insert_solib_event_breakpoint. Allows specifying which
7748 INSERT_MODE to pass through to update_global_location_list. */
7749
7750static struct breakpoint *
7751create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7752 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7753{
7754 struct breakpoint *b;
7755
9efa3c7f 7756 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 7757 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7758 return b;
7759}
7760
04086b45
PA
7761struct breakpoint *
7762create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7763{
7764 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7765}
7766
f37f681c
PA
7767/* See breakpoint.h. */
7768
7769struct breakpoint *
7770create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7771{
7772 struct breakpoint *b;
7773
04086b45
PA
7774 /* Explicitly tell update_global_location_list to insert
7775 locations. */
7776 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7777 if (!b->loc->inserted)
7778 {
7779 delete_breakpoint (b);
7780 return NULL;
7781 }
7782 return b;
7783}
7784
cae688ec
JJ
7785/* Disable any breakpoints that are on code in shared libraries. Only
7786 apply to enabled breakpoints, disabled ones can just stay disabled. */
7787
7788void
cb851954 7789disable_breakpoints_in_shlibs (void)
cae688ec 7790{
48d7020b 7791 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7792 {
7793 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7794 struct breakpoint *b = loc->owner;
7795
7796 /* We apply the check to all breakpoints, including disabled for
7797 those with loc->duplicate set. This is so that when breakpoint
7798 becomes enabled, or the duplicate is removed, gdb will try to
7799 insert all breakpoints. If we don't set shlib_disabled here,
7800 we'll try to insert those breakpoints and fail. */
7801 if (((b->type == bp_breakpoint)
7802 || (b->type == bp_jit_event)
7803 || (b->type == bp_hardware_breakpoint)
7804 || (is_tracepoint (b)))
7805 && loc->pspace == current_program_space
7806 && !loc->shlib_disabled
7807 && solib_name_from_address (loc->pspace, loc->address)
7808 )
7809 {
7810 loc->shlib_disabled = 1;
7811 }
7812 }
cae688ec
JJ
7813}
7814
63644780
NB
7815/* Disable any breakpoints and tracepoints that are in SOLIB upon
7816 notification of unloaded_shlib. Only apply to enabled breakpoints,
7817 disabled ones can just stay disabled. */
84acb35a 7818
75149521 7819static void
84acb35a
JJ
7820disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7821{
d8de7963 7822 bool disabled_shlib_breaks = false;
84acb35a 7823
48d7020b 7824 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7825 {
7826 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7827 struct breakpoint *b = loc->owner;
7828
7829 if (solib->pspace == loc->pspace
7830 && !loc->shlib_disabled
7831 && (((b->type == bp_breakpoint
7832 || b->type == bp_jit_event
7833 || b->type == bp_hardware_breakpoint)
7834 && (loc->loc_type == bp_loc_hardware_breakpoint
7835 || loc->loc_type == bp_loc_software_breakpoint))
7836 || is_tracepoint (b))
7837 && solib_contains_address_p (solib, loc->address))
7838 {
7839 loc->shlib_disabled = 1;
7840 /* At this point, we cannot rely on remove_breakpoint
7841 succeeding so we must mark the breakpoint as not inserted
7842 to prevent future errors occurring in remove_breakpoints. */
7843 loc->inserted = 0;
7844
7845 /* This may cause duplicate notifications for the same breakpoint. */
7846 gdb::observers::breakpoint_modified.notify (b);
7847
7848 if (!disabled_shlib_breaks)
7849 {
7850 target_terminal::ours_for_output ();
7851 warning (_("Temporarily disabling breakpoints "
7852 "for unloaded shared library \"%s\""),
7853 solib->so_name);
7854 }
d8de7963 7855 disabled_shlib_breaks = true;
01add95b
SM
7856 }
7857 }
84acb35a
JJ
7858}
7859
63644780
NB
7860/* Disable any breakpoints and tracepoints in OBJFILE upon
7861 notification of free_objfile. Only apply to enabled breakpoints,
7862 disabled ones can just stay disabled. */
7863
7864static void
7865disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7866{
63644780
NB
7867 if (objfile == NULL)
7868 return;
7869
d03de421
PA
7870 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7871 managed by the user with add-symbol-file/remove-symbol-file.
7872 Similarly to how breakpoints in shared libraries are handled in
7873 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7874 shlib_disabled so they end up uninserted on the next global
7875 location list update. Shared libraries not loaded by the user
7876 aren't handled here -- they're already handled in
7877 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7878 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7879 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7880 main objfile). */
7881 if ((objfile->flags & OBJF_SHARED) == 0
7882 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7883 return;
7884
43892fdf 7885 for (breakpoint *b : all_breakpoints ())
63644780 7886 {
d8de7963 7887 bool bp_modified = false;
63644780
NB
7888
7889 if (!is_breakpoint (b) && !is_tracepoint (b))
7890 continue;
7891
40cb8ca5 7892 for (bp_location *loc : b->locations ())
63644780
NB
7893 {
7894 CORE_ADDR loc_addr = loc->address;
7895
7896 if (loc->loc_type != bp_loc_hardware_breakpoint
7897 && loc->loc_type != bp_loc_software_breakpoint)
7898 continue;
7899
7900 if (loc->shlib_disabled != 0)
7901 continue;
7902
7903 if (objfile->pspace != loc->pspace)
7904 continue;
7905
7906 if (loc->loc_type != bp_loc_hardware_breakpoint
7907 && loc->loc_type != bp_loc_software_breakpoint)
7908 continue;
7909
7910 if (is_addr_in_objfile (loc_addr, objfile))
7911 {
7912 loc->shlib_disabled = 1;
08351840
PA
7913 /* At this point, we don't know whether the object was
7914 unmapped from the inferior or not, so leave the
7915 inserted flag alone. We'll handle failure to
7916 uninsert quietly, in case the object was indeed
7917 unmapped. */
63644780
NB
7918
7919 mark_breakpoint_location_modified (loc);
7920
d8de7963 7921 bp_modified = true;
63644780
NB
7922 }
7923 }
7924
7925 if (bp_modified)
76727919 7926 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7927 }
7928}
7929
b650a282 7930/* See breakpoint.h. */
ce78b96d 7931
92bb0228
PA
7932breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7933 bool temp, const char *cond_string_)
7934 : type (bptype),
7935 disposition (temp ? disp_del : disp_donttouch),
7936 gdbarch (gdbarch_),
7937 language (current_language->la_language),
7938 input_radix (::input_radix),
7939 cond_string (cond_string_ != nullptr
7940 ? make_unique_xstrdup (cond_string_)
7941 : nullptr),
7942 related_breakpoint (this)
7943{
7944}
7945
7946/* See breakpoint.h. */
7947
fed1c982 7948catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
92bb0228
PA
7949 const char *cond_string)
7950 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
c906108c 7951{
acd0955b 7952 add_dummy_location (this, current_program_space);
c5aa993b 7953
acd0955b 7954 pspace = current_program_space;
346774a9
PA
7955}
7956
28010a5d 7957void
b270e6f9 7958install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 7959{
b270e6f9 7960 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 7961 set_breakpoint_number (internal, b);
558a9d82
YQ
7962 if (is_tracepoint (b))
7963 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
7964 if (!internal)
7965 mention (b);
76727919 7966 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
7967
7968 if (update_gll)
44702360 7969 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
7970}
7971
c906108c 7972static int
fba45db2 7973hw_breakpoint_used_count (void)
c906108c 7974{
c906108c
SS
7975 int i = 0;
7976
43892fdf 7977 for (breakpoint *b : all_breakpoints ())
d6b74ac4 7978 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 7979 for (bp_location *bl : b->locations ())
f1310107
TJB
7980 {
7981 /* Special types of hardware breakpoints may use more than
7982 one register. */
c47614fe 7983 i += b->resources_needed (bl);
f1310107 7984 }
c906108c
SS
7985
7986 return i;
7987}
7988
a1398e0c
PA
7989/* Returns the resources B would use if it were a hardware
7990 watchpoint. */
7991
c906108c 7992static int
a1398e0c 7993hw_watchpoint_use_count (struct breakpoint *b)
c906108c 7994{
c906108c
SS
7995 int i = 0;
7996
a1398e0c
PA
7997 if (!breakpoint_enabled (b))
7998 return 0;
7999
40cb8ca5 8000 for (bp_location *bl : b->locations ())
a1398e0c
PA
8001 {
8002 /* Special types of hardware watchpoints may use more than
8003 one register. */
c47614fe 8004 i += b->resources_needed (bl);
a1398e0c
PA
8005 }
8006
8007 return i;
8008}
8009
8010/* Returns the sum the used resources of all hardware watchpoints of
8011 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8012 the sum of the used resources of all hardware watchpoints of other
8013 types _not_ TYPE. */
8014
8015static int
8016hw_watchpoint_used_count_others (struct breakpoint *except,
8017 enum bptype type, int *other_type_used)
8018{
8019 int i = 0;
a1398e0c 8020
c906108c 8021 *other_type_used = 0;
43892fdf 8022 for (breakpoint *b : all_breakpoints ())
e09342b5 8023 {
a1398e0c
PA
8024 if (b == except)
8025 continue;
e09342b5
TJB
8026 if (!breakpoint_enabled (b))
8027 continue;
8028
a1398e0c
PA
8029 if (b->type == type)
8030 i += hw_watchpoint_use_count (b);
8031 else if (is_hardware_watchpoint (b))
8032 *other_type_used = 1;
e09342b5
TJB
8033 }
8034
c906108c
SS
8035 return i;
8036}
8037
c906108c 8038void
fba45db2 8039disable_watchpoints_before_interactive_call_start (void)
c906108c 8040{
43892fdf 8041 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8042 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8043 {
b5de0fa7 8044 b->enable_state = bp_call_disabled;
44702360 8045 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8046 }
c906108c
SS
8047}
8048
8049void
fba45db2 8050enable_watchpoints_after_interactive_call_stop (void)
c906108c 8051{
43892fdf 8052 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8053 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8054 {
b5de0fa7 8055 b->enable_state = bp_enabled;
44702360 8056 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8057 }
c906108c
SS
8058}
8059
8bea4e01
UW
8060void
8061disable_breakpoints_before_startup (void)
8062{
6c95b8df 8063 current_program_space->executing_startup = 1;
44702360 8064 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8065}
8066
8067void
8068enable_breakpoints_after_startup (void)
8069{
6c95b8df 8070 current_program_space->executing_startup = 0;
f8eba3c6 8071 breakpoint_re_set ();
8bea4e01
UW
8072}
8073
7a3e3265
PA
8074/* Allocate a new momentary breakpoint. */
8075
7ab97995 8076template<typename... Arg>
7a3e3265 8077static momentary_breakpoint *
7ab97995
PA
8078new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8079 Arg&&... args)
7a3e3265
PA
8080{
8081 if (type == bp_longjmp || type == bp_exception)
7ab97995
PA
8082 return new longjmp_breakpoint (gdbarch, type,
8083 std::forward<Arg> (args)...);
7a3e3265 8084 else
7ab97995
PA
8085 return new momentary_breakpoint (gdbarch, type,
8086 std::forward<Arg> (args)...);
7a3e3265
PA
8087}
8088
7c16b83e
PA
8089/* Set a momentary breakpoint of type TYPE at address specified by
8090 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8091 frame. */
c906108c 8092
454dafbd 8093breakpoint_up
a6d9a66e
UW
8094set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8095 struct frame_id frame_id, enum bptype type)
c906108c 8096{
193facb3
JK
8097 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8098 tail-called one. */
8099 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8100
7a3e3265 8101 std::unique_ptr<momentary_breakpoint> b
7ab97995
PA
8102 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8103 inferior_thread ()->global_num));
7a3e3265
PA
8104
8105 b->add_location (sal);
c906108c 8106
7a3e3265
PA
8107 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8108
44702360 8109 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8110
7a3e3265 8111 return bp;
c906108c 8112}
611c83ae 8113
06edf0c0 8114/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8115 The new breakpoint will have type TYPE, use OPS as its
8116 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8117
06edf0c0
PA
8118static struct breakpoint *
8119momentary_breakpoint_from_master (struct breakpoint *orig,
8120 enum bptype type,
7ab97995
PA
8121 int loc_enabled,
8122 int thread)
e58b0e63 8123{
7a3e3265 8124 std::unique_ptr<breakpoint> copy
7ab97995
PA
8125 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8126 orig->frame_id, thread));
ee89d0a4 8127 copy->loc = copy->allocate_location ();
0ba852ab 8128 set_breakpoint_location_function (copy->loc);
e58b0e63 8129
a6d9a66e 8130 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8131 copy->loc->requested_address = orig->loc->requested_address;
8132 copy->loc->address = orig->loc->address;
8133 copy->loc->section = orig->loc->section;
6c95b8df 8134 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8135 copy->loc->probe = orig->loc->probe;
f8eba3c6 8136 copy->loc->line_number = orig->loc->line_number;
2f202fde 8137 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8138 copy->loc->enabled = loc_enabled;
e58b0e63 8139
7a3e3265 8140 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
44702360 8141 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7a3e3265 8142 return b;
e58b0e63
PA
8143}
8144
06edf0c0
PA
8145/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8146 ORIG is NULL. */
8147
8148struct breakpoint *
8149clone_momentary_breakpoint (struct breakpoint *orig)
8150{
8151 /* If there's nothing to clone, then return nothing. */
8152 if (orig == NULL)
8153 return NULL;
8154
7ab97995
PA
8155 return momentary_breakpoint_from_master (orig, orig->type, 0,
8156 orig->thread);
06edf0c0
PA
8157}
8158
454dafbd 8159breakpoint_up
a6d9a66e
UW
8160set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8161 enum bptype type)
611c83ae
PA
8162{
8163 struct symtab_and_line sal;
8164
8165 sal = find_pc_line (pc, 0);
8166 sal.pc = pc;
8167 sal.section = find_pc_overlay (pc);
8168 sal.explicit_pc = 1;
8169
a6d9a66e 8170 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8171}
c906108c 8172\f
c5aa993b 8173
c906108c
SS
8174/* Tell the user we have just set a breakpoint B. */
8175
8176static void
b713485d 8177mention (const breakpoint *b)
c906108c 8178{
c47614fe 8179 b->print_mention ();
2d33446d 8180 current_uiout->text ("\n");
c906108c 8181}
c906108c 8182\f
c5aa993b 8183
5133a315 8184static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8185
7f32a4d5
PA
8186/* Handle "set breakpoint auto-hw on".
8187
8188 If the explicitly specified breakpoint type is not hardware
8189 breakpoint, check the memory map to see whether the breakpoint
8190 address is in read-only memory.
8191
8192 - location type is not hardware breakpoint, memory is read-only.
8193 We change the type of the location to hardware breakpoint.
8194
8195 - location type is hardware breakpoint, memory is read-write. This
8196 means we've previously made the location hardware one, but then the
8197 memory map changed, so we undo.
8198*/
8199
8200static void
8201handle_automatic_hardware_breakpoints (bp_location *bl)
8202{
8203 if (automatic_hardware_breakpoints
8204 && bl->owner->type != bp_hardware_breakpoint
8205 && (bl->loc_type == bp_loc_software_breakpoint
8206 || bl->loc_type == bp_loc_hardware_breakpoint))
8207 {
8208 /* When breakpoints are removed, remove_breakpoints will use
8209 location types we've just set here, the only possible problem
8210 is that memory map has changed during running program, but
8211 it's not going to work anyway with current gdb. */
8212 mem_region *mr = lookup_mem_region (bl->address);
8213
8214 if (mr != nullptr)
8215 {
8216 enum bp_loc_type new_type;
8217
8218 if (mr->attrib.mode != MEM_RW)
8219 new_type = bp_loc_hardware_breakpoint;
8220 else
8221 new_type = bp_loc_software_breakpoint;
8222
8223 if (new_type != bl->loc_type)
8224 {
8225 static bool said = false;
8226
8227 bl->loc_type = new_type;
8228 if (!said)
8229 {
6cb06a8c
TT
8230 gdb_printf (_("Note: automatically using "
8231 "hardware breakpoints for "
8232 "read-only addresses.\n"));
7f32a4d5
PA
8233 said = true;
8234 }
8235 }
8236 }
8237 }
8238}
8239
06615628 8240bp_location *
74421c0b 8241code_breakpoint::add_location (const symtab_and_line &sal)
0d381245 8242{
06615628 8243 struct bp_location *new_loc, **tmp;
3742cc8b 8244 CORE_ADDR adjusted_address;
06615628 8245 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8246
8247 if (loc_gdbarch == NULL)
06615628 8248 loc_gdbarch = gdbarch;
3742cc8b
YQ
8249
8250 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8251 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8252 location will be placed on the location chain. Adjustment of the
8253 breakpoint may cause target_read_memory() to be called and we do
8254 not want its scan of the location chain to find a breakpoint and
8255 location that's only been partially initialized. */
8256 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
60270718
AB
8257 sal.pc, type,
8258 sal.pspace);
0d381245 8259
d30113d4 8260 /* Sort the locations by their ADDRESS. */
06615628
PA
8261 new_loc = allocate_location ();
8262 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
d30113d4 8263 tmp = &((*tmp)->next))
0d381245 8264 ;
06615628
PA
8265 new_loc->next = *tmp;
8266 *tmp = new_loc;
8267
8268 new_loc->requested_address = sal.pc;
8269 new_loc->address = adjusted_address;
8270 new_loc->pspace = sal.pspace;
8271 new_loc->probe.prob = sal.prob;
8272 new_loc->probe.objfile = sal.objfile;
8273 gdb_assert (new_loc->pspace != NULL);
8274 new_loc->section = sal.section;
8275 new_loc->gdbarch = loc_gdbarch;
8276 new_loc->line_number = sal.line;
8277 new_loc->symtab = sal.symtab;
8278 new_loc->symbol = sal.symbol;
8279 new_loc->msymbol = sal.msymbol;
8280 new_loc->objfile = sal.objfile;
8281
8282 set_breakpoint_location_function (new_loc);
1a853c52 8283
6ae88661
LM
8284 /* While by definition, permanent breakpoints are already present in the
8285 code, we don't mark the location as inserted. Normally one would expect
8286 that GDB could rely on that breakpoint instruction to stop the program,
8287 thus removing the need to insert its own breakpoint, except that executing
8288 the breakpoint instruction can kill the target instead of reporting a
8289 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8290 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8291 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8292 breakpoint be inserted normally results in QEMU knowing about the GDB
8293 breakpoint, and thus trap before the breakpoint instruction is executed.
8294 (If GDB later needs to continue execution past the permanent breakpoint,
8295 it manually increments the PC, thus avoiding executing the breakpoint
8296 instruction.) */
06615628
PA
8297 if (bp_loc_is_permanent (new_loc))
8298 new_loc->permanent = 1;
1a853c52 8299
06615628 8300 return new_loc;
0d381245 8301}
514f746b
AR
8302\f
8303
5133a315
LM
8304/* Return true if LOC is pointing to a permanent breakpoint,
8305 return false otherwise. */
1cf4d951 8306
5133a315 8307static bool
1cf4d951
PA
8308bp_loc_is_permanent (struct bp_location *loc)
8309{
514f746b
AR
8310 gdb_assert (loc != NULL);
8311
cb1e4e32
PA
8312 /* If we have a non-breakpoint-backed catchpoint or a software
8313 watchpoint, just return 0. We should not attempt to read from
8314 the addresses the locations of these breakpoint types point to.
5133a315 8315 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8316 memory. */
cb1e4e32 8317 if (!bl_address_is_meaningful (loc))
5133a315 8318 return false;
244558af 8319
5ed8105e 8320 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8321 switch_to_program_space_and_thread (loc->pspace);
5133a315 8322 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8323}
8324
e7e0cddf
SS
8325/* Build a command list for the dprintf corresponding to the current
8326 settings of the dprintf style options. */
8327
8328static void
8329update_dprintf_command_list (struct breakpoint *b)
8330{
6f781ee3 8331 const char *dprintf_args = b->extra_string.get ();
8579fd13 8332 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf
SS
8333
8334 if (!dprintf_args)
8335 return;
8336
8337 dprintf_args = skip_spaces (dprintf_args);
8338
8339 /* Allow a comma, as it may have terminated a location, but don't
8340 insist on it. */
8341 if (*dprintf_args == ',')
8342 ++dprintf_args;
8343 dprintf_args = skip_spaces (dprintf_args);
8344
8345 if (*dprintf_args != '"')
8346 error (_("Bad format string, missing '\"'."));
8347
d3ce09f5 8348 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8349 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8350 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8351 {
e0700ba4 8352 if (dprintf_function.empty ())
e7e0cddf
SS
8353 error (_("No function supplied for dprintf call"));
8354
e0700ba4 8355 if (!dprintf_channel.empty ())
e7e0cddf 8356 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8357 dprintf_function.c_str (),
8358 dprintf_channel.c_str (),
e7e0cddf
SS
8359 dprintf_args);
8360 else
8361 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8362 dprintf_function.c_str (),
e7e0cddf
SS
8363 dprintf_args);
8364 }
d3ce09f5
SS
8365 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8366 {
8367 if (target_can_run_breakpoint_commands ())
8368 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8369 else
8370 {
8371 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8372 printf_line = xstrprintf ("printf %s", dprintf_args);
8373 }
8374 }
e7e0cddf 8375 else
f34652de 8376 internal_error (_("Invalid dprintf style."));
e7e0cddf 8377
f28045c2 8378 gdb_assert (printf_line != NULL);
e7e0cddf 8379
12973681
TT
8380 /* Manufacture a printf sequence. */
8381 struct command_line *printf_cmd_line
8579fd13 8382 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8383 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8384 command_lines_deleter ()));
e7e0cddf
SS
8385}
8386
8387/* Update all dprintf commands, making their command lists reflect
8388 current style settings. */
8389
8390static void
eb4c3f4a 8391update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8392 struct cmd_list_element *c)
8393{
43892fdf
SM
8394 for (breakpoint *b : all_breakpoints ())
8395 if (b->type == bp_dprintf)
e7e0cddf 8396 update_dprintf_command_list (b);
e7e0cddf 8397}
c3f6f71d 8398
74421c0b 8399code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
3b003a61
PA
8400 enum bptype type_,
8401 gdb::array_view<const symtab_and_line> sals,
264f9890 8402 location_spec_up &&locspec_,
3b003a61
PA
8403 gdb::unique_xmalloc_ptr<char> filter_,
8404 gdb::unique_xmalloc_ptr<char> cond_string_,
8405 gdb::unique_xmalloc_ptr<char> extra_string_,
8406 enum bpdisp disposition_,
8407 int thread_, int task_, int ignore_count_,
8408 int from_tty,
8409 int enabled_, unsigned flags,
8410 int display_canonical_)
8411 : breakpoint (gdbarch_, type_)
018d34a4 8412{
0d381245 8413 int i;
018d34a4
VP
8414
8415 if (type == bp_hardware_breakpoint)
8416 {
fbbd034e
AS
8417 int target_resources_ok;
8418
8419 i = hw_breakpoint_used_count ();
8420 target_resources_ok =
8421 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8422 i + 1, 0);
8423 if (target_resources_ok == 0)
8424 error (_("No hardware breakpoint support in the target."));
8425 else if (target_resources_ok < 0)
8426 error (_("Hardware breakpoints used exceeds limit."));
8427 }
8428
6c5b2ebe 8429 gdb_assert (!sals.empty ());
6c95b8df 8430
3b003a61
PA
8431 thread = thread_;
8432 task = task_;
0d381245 8433
3b003a61
PA
8434 cond_string = std::move (cond_string_);
8435 extra_string = std::move (extra_string_);
8436 ignore_count = ignore_count_;
8437 enable_state = enabled_ ? bp_enabled : bp_disabled;
8438 disposition = disposition_;
5af949e3 8439
3b003a61
PA
8440 if (type == bp_static_tracepoint
8441 || type == bp_static_marker_tracepoint)
8442 {
98ed24fb 8443 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
3b003a61 8444 struct static_tracepoint_marker marker;
0d381245 8445
3b003a61 8446 if (strace_marker_p (this))
0d381245 8447 {
3b003a61
PA
8448 /* We already know the marker exists, otherwise, we wouldn't
8449 see a sal for it. */
709438c7 8450 const char *p = &locspec_->to_string ()[3];
3b003a61 8451 const char *endp;
0fb4aa4b 8452
3b003a61 8453 p = skip_spaces (p);
0fb4aa4b 8454
3b003a61 8455 endp = skip_to_space (p);
0fb4aa4b 8456
3b003a61 8457 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8458
3b003a61
PA
8459 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8460 t->static_trace_marker_id.c_str ());
8461 }
8462 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8463 {
8464 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8465
3b003a61
PA
8466 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8467 t->static_trace_marker_id.c_str ());
0d381245
VP
8468 }
8469 else
3b003a61
PA
8470 warning (_("Couldn't determine the static tracepoint marker to probe"));
8471 }
8472
8473 for (const auto &sal : sals)
8474 {
8475 if (from_tty)
018d34a4 8476 {
3b003a61
PA
8477 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8478 if (loc_gdbarch == nullptr)
8479 loc_gdbarch = gdbarch;
8480
8481 describe_other_breakpoints (loc_gdbarch,
8482 sal.pspace, sal.pc, sal.section, thread);
0d381245
VP
8483 }
8484
3b003a61
PA
8485 bp_location *new_loc = add_location (sal);
8486 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8487 new_loc->inserted = 1;
8488
b5fa468f
TBA
8489 /* Do not set breakpoint locations conditions yet. As locations
8490 are inserted, they get sorted based on their addresses. Let
8491 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8492
8493 /* Dynamic printf requires and uses additional arguments on the
8494 command line, otherwise it's an error. */
8495 if (type == bp_dprintf)
8496 {
3b003a61
PA
8497 if (extra_string != nullptr)
8498 update_dprintf_command_list (this);
e7e0cddf
SS
8499 else
8500 error (_("Format string required"));
8501 }
3b003a61
PA
8502 else if (extra_string != nullptr)
8503 error (_("Garbage '%s' at end of command"), extra_string.get ());
855a6e68 8504 }
018d34a4 8505
b5fa468f
TBA
8506 /* The order of the locations is now stable. Set the location
8507 condition using the location's number. */
8508 int loc_num = 1;
3b003a61 8509 for (bp_location *bl : locations ())
b5fa468f 8510 {
3b003a61
PA
8511 if (cond_string != nullptr)
8512 set_breakpoint_location_condition (cond_string.get (), bl,
8513 number, loc_num);
b5fa468f
TBA
8514
8515 ++loc_num;
8516 }
8517
3b003a61 8518 display_canonical = display_canonical_;
264f9890
PA
8519 if (locspec_ != nullptr)
8520 locspec = std::move (locspec_);
018d34a4 8521 else
264f9890 8522 locspec = new_address_location_spec (this->loc->address, NULL, 0);
3b003a61 8523 filter = std::move (filter_);
d9b3f62e 8524}
018d34a4 8525
d9b3f62e
PA
8526static void
8527create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8528 gdb::array_view<const symtab_and_line> sals,
264f9890 8529 location_spec_up &&locspec,
e1e01040
PA
8530 gdb::unique_xmalloc_ptr<char> filter,
8531 gdb::unique_xmalloc_ptr<char> cond_string,
8532 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8533 enum bptype type, enum bpdisp disposition,
8534 int thread, int task, int ignore_count,
ef4848c7 8535 int from_tty,
44f238bb
PA
8536 int enabled, int internal, unsigned flags,
8537 int display_canonical)
d9b3f62e 8538{
74421c0b 8539 std::unique_ptr<code_breakpoint> b
3b003a61
PA
8540 = new_breakpoint_from_type (gdbarch,
8541 type,
8542 sals,
264f9890 8543 std::move (locspec),
3b003a61
PA
8544 std::move (filter),
8545 std::move (cond_string),
8546 std::move (extra_string),
8547 disposition,
8548 thread, task, ignore_count,
8549 from_tty,
8550 enabled, flags,
8551 display_canonical);
d9b3f62e 8552
b270e6f9 8553 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8554}
8555
8556/* Add SALS.nelts breakpoints to the breakpoint table. For each
8557 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8558 value. COND_STRING, if not NULL, specified the condition to be
8559 used for all breakpoints. Essentially the only case where
8560 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8561 function. In that case, it's still not possible to specify
8562 separate conditions for different overloaded functions, so
8563 we take just a single condition string.
8564
c3f6f71d 8565 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8566 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8567 array contents). If the function fails (error() is called), the
8568 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8569 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8570
8571static void
8cdf0e15 8572create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8573 struct linespec_result *canonical,
e1e01040
PA
8574 gdb::unique_xmalloc_ptr<char> cond_string,
8575 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8576 enum bptype type, enum bpdisp disposition,
8577 int thread, int task, int ignore_count,
ef4848c7 8578 int from_tty,
44f238bb 8579 int enabled, int internal, unsigned flags)
c906108c 8580{
f8eba3c6 8581 if (canonical->pre_expanded)
6c5b2ebe 8582 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8583
6c5b2ebe 8584 for (const auto &lsal : canonical->lsals)
c3f6f71d 8585 {
f00aae0f 8586 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8587 'break', without arguments. */
264f9890 8588 location_spec_up locspec
5c1ddcb6
PA
8589 = (canonical->locspec != nullptr
8590 ? canonical->locspec->clone ()
8591 : nullptr);
e1e01040 8592 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8593 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8594
6c5b2ebe 8595 create_breakpoint_sal (gdbarch, lsal.sals,
264f9890 8596 std::move (locspec),
e1e01040
PA
8597 std::move (filter_string),
8598 std::move (cond_string),
8599 std::move (extra_string),
e7e0cddf 8600 type, disposition,
ef4848c7 8601 thread, task, ignore_count,
44f238bb 8602 from_tty, enabled, internal, flags,
56435ebe 8603 canonical->special_display);
c3f6f71d 8604 }
c3f6f71d 8605}
c906108c 8606
264f9890 8607/* Parse LOCSPEC which is assumed to be a SAL specification possibly
c3f6f71d 8608 followed by conditionals. On return, SALS contains an array of SAL
264f9890
PA
8609 addresses found. LOCSPEC points to the end of the SAL (for
8610 linespec locspecs).
9998af43
TJB
8611
8612 The array and the line spec strings are allocated on the heap, it is
8613 the caller's responsibility to free them. */
c906108c 8614
b9362cc7 8615static void
264f9890 8616parse_breakpoint_sals (location_spec *locspec,
58438ac1 8617 struct linespec_result *canonical)
c3f6f71d 8618{
f00aae0f
KS
8619 struct symtab_and_line cursal;
8620
7464aeaa 8621 if (locspec->type () == LINESPEC_LOCATION_SPEC)
f00aae0f 8622 {
40d97ee2 8623 const char *spec = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 8624
a20714ff 8625 if (spec == NULL)
f00aae0f
KS
8626 {
8627 /* The last displayed codepoint, if it's valid, is our default
8628 breakpoint address. */
8629 if (last_displayed_sal_is_valid ())
8630 {
f00aae0f
KS
8631 /* Set sal's pspace, pc, symtab, and line to the values
8632 corresponding to the last call to print_frame_info.
8633 Be sure to reinitialize LINE with NOTCURRENT == 0
8634 as the breakpoint line number is inappropriate otherwise.
8635 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8636 symtab_and_line sal = get_last_displayed_sal ();
8637 CORE_ADDR pc = sal.pc;
8638
f00aae0f
KS
8639 sal = find_pc_line (pc, 0);
8640
8641 /* "break" without arguments is equivalent to "break *PC"
8642 where PC is the last displayed codepoint's address. So
8643 make sure to set sal.explicit_pc to prevent GDB from
8644 trying to expand the list of sals to include all other
8645 instances with the same symtab and line. */
8646 sal.pc = pc;
8647 sal.explicit_pc = 1;
8648
6c5b2ebe
PA
8649 struct linespec_sals lsal;
8650 lsal.sals = {sal};
f00aae0f
KS
8651 lsal.canonical = NULL;
8652
6c5b2ebe 8653 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8654 return;
8655 }
8656 else
8657 error (_("No default breakpoint address now."));
c906108c 8658 }
c906108c 8659 }
f00aae0f
KS
8660
8661 /* Force almost all breakpoints to be in terms of the
8662 current_source_symtab (which is decode_line_1's default).
8663 This should produce the results we want almost all of the
8664 time while leaving default_breakpoint_* alone.
8665
8666 ObjC: However, don't match an Objective-C method name which
8667 may have a '+' or '-' succeeded by a '['. */
8668 cursal = get_current_source_symtab_and_line ();
8669 if (last_displayed_sal_is_valid ())
c906108c 8670 {
a20714ff 8671 const char *spec = NULL;
cc80f267 8672
7464aeaa 8673 if (locspec->type () == LINESPEC_LOCATION_SPEC)
40d97ee2 8674 spec = as_linespec_location_spec (locspec)->spec_string;
cc80f267 8675
f00aae0f 8676 if (!cursal.symtab
a20714ff
PA
8677 || (spec != NULL
8678 && strchr ("+-", spec[0]) != NULL
8679 && spec[1] != '['))
f00aae0f 8680 {
264f9890 8681 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
8682 get_last_displayed_symtab (),
8683 get_last_displayed_line (),
8684 canonical, NULL, NULL);
8685 return;
8686 }
c906108c 8687 }
f00aae0f 8688
264f9890 8689 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 8690 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 8691}
c906108c 8692
c906108c 8693
c3f6f71d 8694/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8695 inserted as a breakpoint. If it can't throw an error. */
c906108c 8696
b9362cc7 8697static void
6c5b2ebe 8698breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 8699{
6c5b2ebe
PA
8700 for (auto &sal : sals)
8701 resolve_sal_pc (&sal);
c3f6f71d
JM
8702}
8703
7a697b8d
SS
8704/* Fast tracepoints may have restrictions on valid locations. For
8705 instance, a fast tracepoint using a jump instead of a trap will
8706 likely have to overwrite more bytes than a trap would, and so can
8707 only be placed where the instruction is longer than the jump, or a
8708 multi-instruction sequence does not have a jump into the middle of
8709 it, etc. */
8710
8711static void
8712check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 8713 gdb::array_view<const symtab_and_line> sals)
7a697b8d 8714{
6c5b2ebe 8715 for (const auto &sal : sals)
7a697b8d 8716 {
f8eba3c6
TT
8717 struct gdbarch *sarch;
8718
6c5b2ebe 8719 sarch = get_sal_arch (sal);
f8eba3c6
TT
8720 /* We fall back to GDBARCH if there is no architecture
8721 associated with SAL. */
8722 if (sarch == NULL)
8723 sarch = gdbarch;
281d762b
TT
8724 std::string msg;
8725 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 8726 error (_("May not have a fast tracepoint at %s%s"),
281d762b 8727 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
8728 }
8729}
8730
018d34a4
VP
8731/* Given TOK, a string specification of condition and thread, as
8732 accepted by the 'break' command, extract the condition
8733 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8734 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8735 If no condition is found, *COND_STRING is set to NULL.
8736 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8737
8738static void
bbc13ae3 8739find_condition_and_thread (const char *tok, CORE_ADDR pc,
6f781ee3
TT
8740 gdb::unique_xmalloc_ptr<char> *cond_string,
8741 int *thread, int *task,
8742 gdb::unique_xmalloc_ptr<char> *rest)
018d34a4 8743{
6f781ee3 8744 cond_string->reset ();
018d34a4 8745 *thread = -1;
ed1d1739 8746 *task = 0;
6f781ee3 8747 rest->reset ();
733d554a 8748 bool force = false;
ed1d1739 8749
018d34a4
VP
8750 while (tok && *tok)
8751 {
bbc13ae3 8752 const char *end_tok;
018d34a4 8753 int toklen;
bbc13ae3
KS
8754 const char *cond_start = NULL;
8755 const char *cond_end = NULL;
cc59ec59 8756
f1735a53 8757 tok = skip_spaces (tok);
e7e0cddf
SS
8758
8759 if ((*tok == '"' || *tok == ',') && rest)
8760 {
6f781ee3 8761 rest->reset (savestring (tok, strlen (tok)));
e7e0cddf
SS
8762 return;
8763 }
8764
f1735a53 8765 end_tok = skip_to_space (tok);
d634f2de 8766
018d34a4 8767 toklen = end_tok - tok;
d634f2de 8768
018d34a4
VP
8769 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8770 {
8771 tok = cond_start = end_tok + 1;
733d554a
TBA
8772 try
8773 {
8774 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8775 }
8776 catch (const gdb_exception_error &)
8777 {
8778 if (!force)
8779 throw;
8780 else
8781 tok = tok + strlen (tok);
8782 }
018d34a4 8783 cond_end = tok;
6f781ee3 8784 cond_string->reset (savestring (cond_start, cond_end - cond_start));
018d34a4 8785 }
733d554a
TBA
8786 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8787 {
5759831a 8788 tok = tok + toklen;
733d554a
TBA
8789 force = true;
8790 }
018d34a4
VP
8791 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8792 {
5d5658a1
PA
8793 const char *tmptok;
8794 struct thread_info *thr;
d634f2de 8795
018d34a4 8796 tok = end_tok + 1;
5d5658a1 8797 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
8798 if (tok == tmptok)
8799 error (_("Junk after thread keyword."));
5d5658a1 8800 *thread = thr->global_num;
bbc13ae3 8801 tok = tmptok;
018d34a4 8802 }
4a306c9a
JB
8803 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8804 {
8805 char *tmptok;
8806
8807 tok = end_tok + 1;
bbc13ae3 8808 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
8809 if (tok == tmptok)
8810 error (_("Junk after task keyword."));
8811 if (!valid_task_id (*task))
b6199126 8812 error (_("Unknown task %d."), *task);
bbc13ae3 8813 tok = tmptok;
4a306c9a 8814 }
e7e0cddf
SS
8815 else if (rest)
8816 {
6f781ee3 8817 rest->reset (savestring (tok, strlen (tok)));
ccab2054 8818 return;
e7e0cddf 8819 }
018d34a4
VP
8820 else
8821 error (_("Junk at end of arguments."));
8822 }
8823}
8824
b5fa468f
TBA
8825/* Call 'find_condition_and_thread' for each sal in SALS until a parse
8826 succeeds. The parsed values are written to COND_STRING, THREAD,
8827 TASK, and REST. See the comment of 'find_condition_and_thread'
8828 for the description of these parameters and INPUT. */
8829
8830static void
8831find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
6f781ee3
TT
8832 const char *input,
8833 gdb::unique_xmalloc_ptr<char> *cond_string,
8834 int *thread, int *task,
8835 gdb::unique_xmalloc_ptr<char> *rest)
b5fa468f
TBA
8836{
8837 int num_failures = 0;
8838 for (auto &sal : sals)
8839 {
6f781ee3 8840 gdb::unique_xmalloc_ptr<char> cond;
b5fa468f
TBA
8841 int thread_id = 0;
8842 int task_id = 0;
6f781ee3 8843 gdb::unique_xmalloc_ptr<char> remaining;
b5fa468f
TBA
8844
8845 /* Here we want to parse 'arg' to separate condition from thread
8846 number. But because parsing happens in a context and the
8847 contexts of sals might be different, try each until there is
8848 success. Finding one successful parse is sufficient for our
8849 goal. When setting the breakpoint we'll re-parse the
8850 condition in the context of each sal. */
8851 try
8852 {
8853 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8854 &task_id, &remaining);
6f781ee3 8855 *cond_string = std::move (cond);
b5fa468f
TBA
8856 *thread = thread_id;
8857 *task = task_id;
6f781ee3 8858 *rest = std::move (remaining);
b5fa468f
TBA
8859 break;
8860 }
8861 catch (const gdb_exception_error &e)
8862 {
8863 num_failures++;
8864 /* If no sal remains, do not continue. */
8865 if (num_failures == sals.size ())
8866 throw;
8867 }
8868 }
8869}
8870
0fb4aa4b
PA
8871/* Decode a static tracepoint marker spec. */
8872
6c5b2ebe 8873static std::vector<symtab_and_line>
f00aae0f 8874decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 8875{
f00aae0f
KS
8876 const char *p = &(*arg_p)[3];
8877 const char *endp;
0fb4aa4b 8878
f1735a53 8879 p = skip_spaces (p);
0fb4aa4b 8880
f1735a53 8881 endp = skip_to_space (p);
0fb4aa4b 8882
81b1e71c 8883 std::string marker_str (p, endp - p);
0fb4aa4b 8884
5d9310c4
SM
8885 std::vector<static_tracepoint_marker> markers
8886 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8887 if (markers.empty ())
81b1e71c
TT
8888 error (_("No known static tracepoint marker named %s"),
8889 marker_str.c_str ());
0fb4aa4b 8890
6c5b2ebe 8891 std::vector<symtab_and_line> sals;
5d9310c4 8892 sals.reserve (markers.size ());
0fb4aa4b 8893
5d9310c4 8894 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 8895 {
5d9310c4
SM
8896 symtab_and_line sal = find_pc_line (marker.address, 0);
8897 sal.pc = marker.address;
6c5b2ebe 8898 sals.push_back (sal);
5d9310c4 8899 }
0fb4aa4b 8900
0fb4aa4b
PA
8901 *arg_p = endp;
8902 return sals;
8903}
8904
bac7c5cf
GB
8905/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8906 according to IS_TRACEPOINT. */
8907
8908static const struct breakpoint_ops *
264f9890
PA
8909breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8910 bool is_tracepoint)
bac7c5cf
GB
8911{
8912 if (is_tracepoint)
8913 {
264f9890 8914 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8915 return &tracepoint_probe_breakpoint_ops;
8916 else
74421c0b 8917 return &code_breakpoint_ops;
bac7c5cf
GB
8918 }
8919 else
8920 {
264f9890 8921 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8922 return &bkpt_probe_breakpoint_ops;
8923 else
74421c0b 8924 return &code_breakpoint_ops;
bac7c5cf
GB
8925 }
8926}
8927
8928/* See breakpoint.h. */
8929
8930const struct breakpoint_ops *
264f9890
PA
8931breakpoint_ops_for_location_spec (const location_spec *locspec,
8932 bool is_tracepoint)
bac7c5cf 8933{
264f9890
PA
8934 if (locspec != nullptr)
8935 return (breakpoint_ops_for_location_spec_type
7464aeaa 8936 (locspec->type (), is_tracepoint));
74421c0b 8937 return &code_breakpoint_ops;
bac7c5cf
GB
8938}
8939
f00aae0f 8940/* See breakpoint.h. */
0101ce28 8941
8cdf0e15
VP
8942int
8943create_breakpoint (struct gdbarch *gdbarch,
264f9890 8944 location_spec *locspec,
e1e01040
PA
8945 const char *cond_string,
8946 int thread, const char *extra_string,
10a636cc 8947 bool force_condition, int parse_extra,
0fb4aa4b 8948 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8949 int ignore_count,
8950 enum auto_boolean pending_break_support,
c0a91b2b 8951 const struct breakpoint_ops *ops,
44f238bb
PA
8952 int from_tty, int enabled, int internal,
8953 unsigned flags)
c3f6f71d 8954{
7efd8fc2 8955 struct linespec_result canonical;
d8de7963 8956 bool pending = false;
4a306c9a 8957 int task = 0;
86b17b60 8958 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8959
348d480f
PA
8960 gdb_assert (ops != NULL);
8961
f00aae0f
KS
8962 /* If extra_string isn't useful, set it to NULL. */
8963 if (extra_string != NULL && *extra_string == '\0')
8964 extra_string = NULL;
8965
a70b8144 8966 try
b78a6381 8967 {
264f9890 8968 ops->create_sals_from_location_spec (locspec, &canonical);
b78a6381 8969 }
230d2906 8970 catch (const gdb_exception_error &e)
0101ce28 8971 {
492d29ea
PA
8972 /* If caller is interested in rc value from parse, set
8973 value. */
8974 if (e.error == NOT_FOUND_ERROR)
0101ce28 8975 {
05ff989b
AC
8976 /* If pending breakpoint support is turned off, throw
8977 error. */
fa8d40ab
JJ
8978
8979 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 8980 throw;
723a2275
VP
8981
8982 exception_print (gdb_stderr, e);
fa8d40ab 8983
dda83cd7 8984 /* If pending breakpoint support is auto query and the user
05ff989b 8985 selects no, then simply return the error code. */
059fb39f 8986 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
8987 && !nquery (_("Make %s pending on future shared library load? "),
8988 bptype_string (type_wanted)))
fd9b8c24 8989 return 0;
fa8d40ab 8990
05ff989b
AC
8991 /* At this point, either the user was queried about setting
8992 a pending breakpoint and selected yes, or pending
8993 breakpoint behavior is on and thus a pending breakpoint
8994 is defaulted on behalf of the user. */
d8de7963 8995 pending = true;
0101ce28 8996 }
492d29ea 8997 else
eedc3f4f 8998 throw;
0101ce28 8999 }
492d29ea 9000
6c5b2ebe 9001 if (!pending && canonical.lsals.empty ())
492d29ea 9002 return 0;
c3f6f71d 9003
c3f6f71d
JM
9004 /* Resolve all line numbers to PC's and verify that the addresses
9005 are ok for the target. */
0101ce28 9006 if (!pending)
f8eba3c6 9007 {
6c5b2ebe
PA
9008 for (auto &lsal : canonical.lsals)
9009 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9010 }
c3f6f71d 9011
7a697b8d 9012 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9013 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9014 {
6c5b2ebe
PA
9015 for (const auto &lsal : canonical.lsals)
9016 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9017 }
7a697b8d 9018
c3f6f71d
JM
9019 /* Verify that condition can be parsed, before setting any
9020 breakpoints. Allocate a separate condition expression for each
4a64f543 9021 breakpoint. */
0101ce28 9022 if (!pending)
c3f6f71d 9023 {
e1e01040
PA
9024 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9025 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9026
f00aae0f 9027 if (parse_extra)
dda83cd7 9028 {
6f781ee3
TT
9029 gdb::unique_xmalloc_ptr<char> rest;
9030 gdb::unique_xmalloc_ptr<char> cond;
52d361e1 9031
6c5b2ebe 9032 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9033
b5fa468f
TBA
9034 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9035 &cond, &thread, &task, &rest);
6f781ee3
TT
9036 cond_string_copy = std::move (cond);
9037 extra_string_copy = std::move (rest);
dda83cd7 9038 }
2f069f6f 9039 else
dda83cd7 9040 {
f00aae0f
KS
9041 if (type_wanted != bp_dprintf
9042 && extra_string != NULL && *extra_string != '\0')
9043 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9044
10a636cc
TBA
9045 /* Check the validity of the condition. We should error out
9046 if the condition is invalid at all of the locations and
9047 if it is not forced. In the PARSE_EXTRA case above, this
9048 check is done when parsing the EXTRA_STRING. */
9049 if (cond_string != nullptr && !force_condition)
9050 {
9051 int num_failures = 0;
9052 const linespec_sals &lsal = canonical.lsals[0];
9053 for (const auto &sal : lsal.sals)
9054 {
9055 const char *cond = cond_string;
9056 try
9057 {
9058 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9059 /* One success is sufficient to keep going. */
9060 break;
9061 }
9062 catch (const gdb_exception_error &)
9063 {
9064 num_failures++;
9065 /* If this is the last sal, error out. */
9066 if (num_failures == lsal.sals.size ())
9067 throw;
9068 }
9069 }
9070 }
9071
0878d0fa
YQ
9072 /* Create a private copy of condition string. */
9073 if (cond_string)
e1e01040 9074 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9075 /* Create a private copy of any extra string. */
9076 if (extra_string)
e1e01040 9077 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9078 }
0fb4aa4b 9079
52d361e1 9080 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9081 std::move (cond_string_copy),
9082 std::move (extra_string_copy),
9083 type_wanted,
d9b3f62e 9084 tempflag ? disp_del : disp_donttouch,
ef4848c7 9085 thread, task, ignore_count,
44f238bb 9086 from_tty, enabled, internal, flags);
c906108c 9087 }
0101ce28
JJ
9088 else
9089 {
73063f51
TT
9090 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9091 type_wanted);
5c1ddcb6 9092 b->locspec = locspec->clone ();
bfccc43c 9093
f00aae0f
KS
9094 if (parse_extra)
9095 b->cond_string = NULL;
e12c7713
MK
9096 else
9097 {
9098 /* Create a private copy of condition string. */
6f781ee3
TT
9099 b->cond_string.reset (cond_string != NULL
9100 ? xstrdup (cond_string)
9101 : NULL);
15630549 9102 b->thread = thread;
e12c7713 9103 }
f00aae0f
KS
9104
9105 /* Create a private copy of any extra string. */
6f781ee3
TT
9106 b->extra_string.reset (extra_string != NULL
9107 ? xstrdup (extra_string)
9108 : NULL);
0101ce28 9109 b->ignore_count = ignore_count;
0101ce28 9110 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9111 b->condition_not_parsed = 1;
41447f92 9112 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9113 if ((type_wanted != bp_breakpoint
dda83cd7 9114 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9115 b->pspace = current_program_space;
8bea4e01 9116
b270e6f9 9117 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9118 }
9119
6c5b2ebe 9120 if (canonical.lsals.size () > 1)
95a42b64 9121 {
3e43a32a
MS
9122 warning (_("Multiple breakpoints were set.\nUse the "
9123 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9124 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9125 }
9126
44702360 9127 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9128
9129 return 1;
c3f6f71d 9130}
c906108c 9131
348d480f 9132/* Set a breakpoint.
72b2ff0e
VP
9133 ARG is a string describing breakpoint address,
9134 condition, and thread.
9135 FLAG specifies if a breakpoint is hardware on,
9136 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9137 and BP_TEMPFLAG. */
348d480f 9138
98deb0da 9139static void
f2fc3015 9140break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9141{
72b2ff0e 9142 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9143 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9144 ? bp_hardware_breakpoint
9145 : bp_breakpoint);
f00aae0f 9146
264f9890
PA
9147 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9148 const struct breakpoint_ops *ops
9149 = breakpoint_ops_for_location_spec (locspec.get (),
9150 false /* is_tracepoint */);
c3f6f71d 9151
8cdf0e15 9152 create_breakpoint (get_current_arch (),
264f9890 9153 locspec.get (),
10a636cc 9154 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9155 tempflag, type_wanted,
8cdf0e15
VP
9156 0 /* Ignore count */,
9157 pending_break_support,
55aa24fb 9158 ops,
8cdf0e15 9159 from_tty,
84f4c1fe 9160 1 /* enabled */,
44f238bb
PA
9161 0 /* internal */,
9162 0);
c906108c
SS
9163}
9164
c906108c
SS
9165/* Helper function for break_command_1 and disassemble_command. */
9166
9167void
fba45db2 9168resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9169{
9170 CORE_ADDR pc;
9171
9172 if (sal->pc == 0 && sal->symtab != NULL)
9173 {
9174 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9175 error (_("No line %d in file \"%s\"."),
05cba821 9176 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9177 sal->pc = pc;
6a048695 9178
4a64f543 9179 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9180 number, then skip the function prologue if necessary. */
6a048695 9181 if (sal->explicit_line)
059acae7 9182 skip_prologue_sal (sal);
c906108c
SS
9183 }
9184
9185 if (sal->section == 0 && sal->symtab != NULL)
9186 {
346d1dfe 9187 const struct blockvector *bv;
3977b71f 9188 const struct block *b;
c5aa993b 9189 struct symbol *sym;
c906108c 9190
43f3e411 9191 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9192 sal->symtab->compunit ());
c906108c
SS
9193 if (bv != NULL)
9194 {
7f0df278 9195 sym = block_linkage_function (b);
c906108c
SS
9196 if (sym != NULL)
9197 {
3c86fae3
SM
9198 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9199 sal->section
9200 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9201 }
9202 else
9203 {
4a64f543 9204 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9205 just have to look harder. This case can be executed
9206 if we have line numbers but no functions (as can
9207 happen in assembly source). */
c906108c 9208
5ed8105e 9209 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9210 switch_to_program_space_and_thread (sal->pspace);
c906108c 9211
5ed8105e 9212 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9213 if (msym.minsym)
1db66e34 9214 sal->section = msym.obj_section ();
c906108c
SS
9215 }
9216 }
9217 }
9218}
9219
9220void
0b39b52e 9221break_command (const char *arg, int from_tty)
c906108c 9222{
db107f19 9223 break_command_1 (arg, 0, from_tty);
c906108c
SS
9224}
9225
c906108c 9226void
0b39b52e 9227tbreak_command (const char *arg, int from_tty)
c906108c 9228{
db107f19 9229 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9230}
9231
c906108c 9232static void
0b39b52e 9233hbreak_command (const char *arg, int from_tty)
c906108c 9234{
db107f19 9235 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9236}
9237
9238static void
0b39b52e 9239thbreak_command (const char *arg, int from_tty)
c906108c 9240{
db107f19 9241 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9242}
9243
e7e0cddf
SS
9244/* The dynamic printf command is mostly like a regular breakpoint, but
9245 with a prewired command list consisting of a single output command,
9246 built from extra arguments supplied on the dprintf command
9247 line. */
9248
da821c7b 9249static void
0b39b52e 9250dprintf_command (const char *arg, int from_tty)
e7e0cddf 9251{
264f9890 9252 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f
KS
9253
9254 /* If non-NULL, ARG should have been advanced past the location;
9255 the next character must be ','. */
9256 if (arg != NULL)
9257 {
9258 if (arg[0] != ',' || arg[1] == '\0')
9259 error (_("Format string required"));
9260 else
9261 {
9262 /* Skip the comma. */
9263 ++arg;
9264 }
9265 }
9266
e7e0cddf 9267 create_breakpoint (get_current_arch (),
264f9890 9268 locspec.get (),
10a636cc 9269 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9270 0, bp_dprintf,
9271 0 /* Ignore count */,
9272 pending_break_support,
74421c0b 9273 &code_breakpoint_ops,
e7e0cddf
SS
9274 from_tty,
9275 1 /* enabled */,
9276 0 /* internal */,
9277 0);
9278}
9279
d3ce09f5 9280static void
0b39b52e 9281agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9282{
9283 error (_("May only run agent-printf on the target"));
9284}
9285
ec45bb67 9286/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9287
ec45bb67
TT
9288int
9289ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9290 const address_space *aspace,
9291 CORE_ADDR bp_addr,
9292 const target_waitstatus &ws)
f1310107 9293{
c272a98c
SM
9294 if (ws.kind () != TARGET_WAITKIND_STOPPED
9295 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9296 return 0;
9297
f1310107
TJB
9298 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9299 bl->length, aspace, bp_addr);
9300}
9301
ec45bb67 9302/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9303
ec45bb67
TT
9304int
9305ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9306{
9307 return target_ranged_break_num_registers ();
9308}
9309
ec45bb67 9310/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9311
ec45bb67 9312enum print_stop_action
7bd86313 9313ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9314{
ec45bb67 9315 struct bp_location *bl = loc;
79a45e25 9316 struct ui_out *uiout = current_uiout;
f1310107 9317
ec45bb67 9318 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9319
9320 /* Ranged breakpoints have only one location. */
9321 gdb_assert (bl && bl->next == NULL);
9322
ec45bb67 9323 annotate_breakpoint (number);
f303dbd6
PA
9324
9325 maybe_print_thread_hit_breakpoint (uiout);
9326
ec45bb67 9327 if (disposition == disp_del)
112e8700 9328 uiout->text ("Temporary ranged breakpoint ");
f1310107 9329 else
112e8700
SM
9330 uiout->text ("Ranged breakpoint ");
9331 if (uiout->is_mi_like_p ())
f1310107 9332 {
112e8700 9333 uiout->field_string ("reason",
f1310107 9334 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9335 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9336 }
78805ff8 9337 print_num_locno (bs, uiout);
112e8700 9338 uiout->text (", ");
f1310107
TJB
9339
9340 return PRINT_SRC_AND_LOC;
9341}
9342
ec45bb67 9343/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9344
ec45bb67 9345bool
a67bcaba 9346ranged_breakpoint::print_one (bp_location **last_loc) const
f1310107 9347{
ec45bb67 9348 struct bp_location *bl = loc;
f1310107 9349 struct value_print_options opts;
79a45e25 9350 struct ui_out *uiout = current_uiout;
f1310107
TJB
9351
9352 /* Ranged breakpoints have only one location. */
9353 gdb_assert (bl && bl->next == NULL);
9354
9355 get_user_print_options (&opts);
9356
9357 if (opts.addressprint)
9358 /* We don't print the address range here, it will be printed later
f7968895 9359 by ranged_breakpoint::print_one_detail. */
112e8700 9360 uiout->field_skip ("addr");
f1310107 9361 annotate_field (5);
ec45bb67 9362 print_breakpoint_location (this, bl);
f1310107 9363 *last_loc = bl;
c01e038b
TT
9364
9365 return true;
f1310107
TJB
9366}
9367
ec45bb67 9368/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9369
ec45bb67
TT
9370void
9371ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9372{
9373 CORE_ADDR address_start, address_end;
ec45bb67 9374 struct bp_location *bl = loc;
d7e74731 9375 string_file stb;
f1310107
TJB
9376
9377 gdb_assert (bl);
9378
9379 address_start = bl->address;
9380 address_end = address_start + bl->length - 1;
9381
112e8700 9382 uiout->text ("\taddress range: ");
d7e74731
PA
9383 stb.printf ("[%s, %s]",
9384 print_core_address (bl->gdbarch, address_start),
9385 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9386 uiout->field_stream ("addr", stb);
9387 uiout->text ("\n");
f1310107
TJB
9388}
9389
ec45bb67 9390/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9391
ec45bb67 9392void
b713485d 9393ranged_breakpoint::print_mention () const
f1310107 9394{
ec45bb67 9395 struct bp_location *bl = loc;
79a45e25 9396 struct ui_out *uiout = current_uiout;
f1310107
TJB
9397
9398 gdb_assert (bl);
ec45bb67 9399 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9400
2d33446d 9401 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
ec45bb67 9402 number, paddress (bl->gdbarch, bl->address),
2d33446d 9403 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9404}
9405
ec45bb67 9406/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9407
ec45bb67 9408void
4d1ae558 9409ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9410{
6cb06a8c 9411 gdb_printf (fp, "break-range %s, %s",
709438c7
PA
9412 locspec->to_string (),
9413 locspec_range_end->to_string ());
04d0163c 9414 print_recreate_thread (fp);
f1310107
TJB
9415}
9416
f1310107
TJB
9417/* Find the address where the end of the breakpoint range should be
9418 placed, given the SAL of the end of the range. This is so that if
9419 the user provides a line number, the end of the range is set to the
9420 last instruction of the given line. */
9421
9422static CORE_ADDR
9423find_breakpoint_range_end (struct symtab_and_line sal)
9424{
9425 CORE_ADDR end;
9426
9427 /* If the user provided a PC value, use it. Otherwise,
9428 find the address of the end of the given location. */
9429 if (sal.explicit_pc)
9430 end = sal.pc;
9431 else
9432 {
9433 int ret;
9434 CORE_ADDR start;
9435
9436 ret = find_line_pc_range (sal, &start, &end);
9437 if (!ret)
9438 error (_("Could not find location of the end of the range."));
9439
9440 /* find_line_pc_range returns the start of the next line. */
9441 end--;
9442 }
9443
9444 return end;
9445}
9446
9447/* Implement the "break-range" CLI command. */
9448
9449static void
0b39b52e 9450break_range_command (const char *arg, int from_tty)
f1310107 9451{
f2fc3015 9452 const char *arg_start;
f1310107
TJB
9453 struct linespec_result canonical_start, canonical_end;
9454 int bp_count, can_use_bp, length;
9455 CORE_ADDR end;
f1310107
TJB
9456
9457 /* We don't support software ranged breakpoints. */
9458 if (target_ranged_break_num_registers () < 0)
9459 error (_("This target does not support hardware ranged breakpoints."));
9460
9461 bp_count = hw_breakpoint_used_count ();
9462 bp_count += target_ranged_break_num_registers ();
9463 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9464 bp_count, 0);
9465 if (can_use_bp < 0)
9466 error (_("Hardware breakpoints used exceeds limit."));
9467
f8eba3c6 9468 arg = skip_spaces (arg);
f1310107
TJB
9469 if (arg == NULL || arg[0] == '\0')
9470 error(_("No address range specified."));
9471
f8eba3c6 9472 arg_start = arg;
264f9890
PA
9473 location_spec_up start_locspec
9474 = string_to_location_spec (&arg, current_language);
9475 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
f1310107
TJB
9476
9477 if (arg[0] != ',')
9478 error (_("Too few arguments."));
6c5b2ebe 9479 else if (canonical_start.lsals.empty ())
f1310107 9480 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9481
6c5b2ebe 9482 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9483
6c5b2ebe
PA
9484 if (canonical_start.lsals.size () > 1
9485 || lsal_start.sals.size () != 1)
f1310107
TJB
9486 error (_("Cannot create a ranged breakpoint with multiple locations."));
9487
6c5b2ebe 9488 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9489 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9490
9491 arg++; /* Skip the comma. */
f8eba3c6 9492 arg = skip_spaces (arg);
f1310107 9493
264f9890 9494 /* Parse the end location specification. */
f1310107 9495
f1310107
TJB
9496 arg_start = arg;
9497
f8eba3c6 9498 /* We call decode_line_full directly here instead of using
264f9890
PA
9499 parse_breakpoint_sals because we need to specify the start
9500 location spec's symtab and line as the default symtab and line
9501 for the end of the range. This makes it possible to have ranges
9502 like "foo.c:27, +14", where +14 means 14 lines from the start
9503 location spec. */
9504 location_spec_up end_locspec
9505 = string_to_location_spec (&arg, current_language);
9506 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9507 sal_start.symtab, sal_start.line,
9508 &canonical_end, NULL, NULL);
9509
6c5b2ebe 9510 if (canonical_end.lsals.empty ())
f1310107 9511 error (_("Could not find location of the end of the range."));
f8eba3c6 9512
6c5b2ebe
PA
9513 const linespec_sals &lsal_end = canonical_end.lsals[0];
9514 if (canonical_end.lsals.size () > 1
9515 || lsal_end.sals.size () != 1)
f1310107
TJB
9516 error (_("Cannot create a ranged breakpoint with multiple locations."));
9517
6c5b2ebe 9518 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9519
9520 end = find_breakpoint_range_end (sal_end);
9521 if (sal_start.pc > end)
177b42fe 9522 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9523
9524 length = end - sal_start.pc + 1;
9525 if (length < 0)
9526 /* Length overflowed. */
9527 error (_("Address range too large."));
9528 else if (length == 1)
9529 {
9530 /* This range is simple enough to be handled by
9531 the `hbreak' command. */
81b1e71c 9532 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9533
9534 return;
9535 }
9536
b925bf21
PA
9537 /* Now set up the breakpoint and install it. */
9538
9539 std::unique_ptr<breakpoint> br
9540 (new ranged_breakpoint (get_current_arch (),
9541 sal_start, length,
264f9890
PA
9542 std::move (start_locspec),
9543 std::move (end_locspec)));
f1310107 9544
27a62b43 9545 install_breakpoint (false, std::move (br), true);
f1310107
TJB
9546}
9547
4a64f543
MS
9548/* Return non-zero if EXP is verified as constant. Returned zero
9549 means EXP is variable. Also the constant detection may fail for
9550 some constant expressions and in such case still falsely return
9551 zero. */
2e6e3d9c 9552
dab72643 9553static bool
65d79d4b
SDJ
9554watchpoint_exp_is_const (const struct expression *exp)
9555{
1eaebe02 9556 return exp->op->constant_p ();
65d79d4b
SDJ
9557}
9558
3a292923 9559/* Implement the "re_set" method for watchpoints. */
348d480f 9560
3a292923
TT
9561void
9562watchpoint::re_set ()
348d480f
PA
9563{
9564 /* Watchpoint can be either on expression using entirely global
9565 variables, or it can be on local variables.
9566
9567 Watchpoints of the first kind are never auto-deleted, and even
9568 persist across program restarts. Since they can use variables
9569 from shared libraries, we need to reparse expression as libraries
9570 are loaded and unloaded.
9571
9572 Watchpoints on local variables can also change meaning as result
9573 of solib event. For example, if a watchpoint uses both a local
9574 and a global variables in expression, it's a local watchpoint,
9575 but unloading of a shared library will make the expression
9576 invalid. This is not a very common use case, but we still
9577 re-evaluate expression, to avoid surprises to the user.
9578
9579 Note that for local watchpoints, we re-evaluate it only if
9580 watchpoints frame id is still valid. If it's not, it means the
9581 watchpoint is out of scope and will be deleted soon. In fact,
9582 I'm not sure we'll ever be called in this case.
9583
9584 If a local watchpoint's frame id is still valid, then
3a292923 9585 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9586
3a5c3e22
PA
9587 Don't do anything about disabled watchpoints, since they will be
9588 reevaluated again when enabled. */
d8de7963 9589 update_watchpoint (this, true /* reparse */);
348d480f
PA
9590}
9591
3a292923 9592/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9593
3a292923
TT
9594int
9595watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9596{
3a292923 9597 int length = exact ? 1 : bl->length;
e09342b5
TJB
9598
9599 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9600 cond_exp.get ());
77b06cd7
TJB
9601}
9602
3a292923 9603/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9604
3a292923
TT
9605int
9606watchpoint::remove_location (struct bp_location *bl,
9607 enum remove_bp_reason reason)
77b06cd7 9608{
3a292923 9609 int length = exact ? 1 : bl->length;
e09342b5
TJB
9610
9611 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9612 cond_exp.get ());
e09342b5
TJB
9613}
9614
3a292923
TT
9615int
9616watchpoint::breakpoint_hit (const struct bp_location *bl,
9617 const address_space *aspace, CORE_ADDR bp_addr,
9618 const target_waitstatus &ws)
e09342b5 9619{
348d480f 9620 struct breakpoint *b = bl->owner;
77b06cd7 9621
348d480f
PA
9622 /* Continuable hardware watchpoints are treated as non-existent if the
9623 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9624 some data address). Otherwise gdb won't stop on a break instruction
9625 in the code (not from a breakpoint) when a hardware watchpoint has
9626 been defined. Also skip watchpoints which we know did not trigger
9627 (did not match the data address). */
9628 if (is_hardware_watchpoint (b)
3a292923 9629 && watchpoint_triggered == watch_triggered_no)
348d480f 9630 return 0;
9c06b0b4 9631
348d480f 9632 return 1;
9c06b0b4
TJB
9633}
9634
3a292923
TT
9635void
9636watchpoint::check_status (bpstat *bs)
9c06b0b4 9637{
348d480f 9638 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9639
348d480f 9640 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9641}
9642
3a292923
TT
9643/* Implement the "resources_needed" method for hardware
9644 watchpoints. */
9c06b0b4 9645
3a292923
TT
9646int
9647watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9648{
3a292923 9649 int length = exact? 1 : bl->length;
348d480f
PA
9650
9651 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9652}
9653
3a292923
TT
9654/* Implement the "works_in_software_mode" method for hardware
9655 watchpoints. */
9c06b0b4 9656
5a61e176 9657bool
3a292923 9658watchpoint::works_in_software_mode () const
9c06b0b4 9659{
efa80663 9660 /* Read and access watchpoints only work with hardware support. */
3a292923 9661 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9662}
9663
3a292923 9664enum print_stop_action
7bd86313 9665watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9666{
348d480f 9667 struct breakpoint *b;
348d480f 9668 enum print_stop_action result;
79a45e25 9669 struct ui_out *uiout = current_uiout;
348d480f
PA
9670
9671 gdb_assert (bs->bp_location_at != NULL);
9672
348d480f
PA
9673 b = bs->breakpoint_at;
9674
f303dbd6
PA
9675 annotate_watchpoint (b->number);
9676 maybe_print_thread_hit_breakpoint (uiout);
9677
d7e74731
PA
9678 string_file stb;
9679
76f9c9cf 9680 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
9681 switch (b->type)
9682 {
348d480f 9683 case bp_watchpoint:
9c06b0b4 9684 case bp_hardware_watchpoint:
112e8700
SM
9685 if (uiout->is_mi_like_p ())
9686 uiout->field_string
9687 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 9688 mention (b);
76f9c9cf 9689 tuple_emitter.emplace (uiout, "value");
112e8700 9690 uiout->text ("\nOld value = ");
850645cf 9691 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9692 uiout->field_stream ("old", stb);
9693 uiout->text ("\nNew value = ");
3a292923 9694 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9695 uiout->field_stream ("new", stb);
9696 uiout->text ("\n");
348d480f
PA
9697 /* More than one watchpoint may have been triggered. */
9698 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9699 break;
9700
9701 case bp_read_watchpoint:
112e8700
SM
9702 if (uiout->is_mi_like_p ())
9703 uiout->field_string
9704 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 9705 mention (b);
76f9c9cf 9706 tuple_emitter.emplace (uiout, "value");
112e8700 9707 uiout->text ("\nValue = ");
3a292923 9708 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9709 uiout->field_stream ("value", stb);
9710 uiout->text ("\n");
348d480f 9711 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9712 break;
9713
9714 case bp_access_watchpoint:
348d480f
PA
9715 if (bs->old_val != NULL)
9716 {
112e8700
SM
9717 if (uiout->is_mi_like_p ())
9718 uiout->field_string
9719 ("reason",
348d480f
PA
9720 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9721 mention (b);
76f9c9cf 9722 tuple_emitter.emplace (uiout, "value");
112e8700 9723 uiout->text ("\nOld value = ");
850645cf 9724 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9725 uiout->field_stream ("old", stb);
9726 uiout->text ("\nNew value = ");
348d480f
PA
9727 }
9728 else
9729 {
9730 mention (b);
112e8700
SM
9731 if (uiout->is_mi_like_p ())
9732 uiout->field_string
9733 ("reason",
348d480f 9734 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 9735 tuple_emitter.emplace (uiout, "value");
112e8700 9736 uiout->text ("\nValue = ");
348d480f 9737 }
3a292923 9738 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9739 uiout->field_stream ("new", stb);
9740 uiout->text ("\n");
348d480f 9741 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9742 break;
9743 default:
348d480f 9744 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9745 }
9746
348d480f
PA
9747 return result;
9748}
9749
3a292923 9750/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 9751
3a292923 9752void
b713485d 9753watchpoint::print_mention () const
348d480f 9754{
79a45e25 9755 struct ui_out *uiout = current_uiout;
46b9c129 9756 const char *tuple_name;
348d480f 9757
3a292923 9758 switch (type)
348d480f
PA
9759 {
9760 case bp_watchpoint:
112e8700 9761 uiout->text ("Watchpoint ");
46b9c129 9762 tuple_name = "wpt";
348d480f
PA
9763 break;
9764 case bp_hardware_watchpoint:
112e8700 9765 uiout->text ("Hardware watchpoint ");
46b9c129 9766 tuple_name = "wpt";
348d480f
PA
9767 break;
9768 case bp_read_watchpoint:
112e8700 9769 uiout->text ("Hardware read watchpoint ");
46b9c129 9770 tuple_name = "hw-rwpt";
348d480f
PA
9771 break;
9772 case bp_access_watchpoint:
112e8700 9773 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 9774 tuple_name = "hw-awpt";
348d480f
PA
9775 break;
9776 default:
f34652de 9777 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9778 }
9779
46b9c129 9780 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9781 uiout->field_signed ("number", number);
112e8700 9782 uiout->text (": ");
3a292923 9783 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
9784}
9785
3a292923 9786/* Implement the "print_recreate" method for watchpoints. */
348d480f 9787
3a292923 9788void
4d1ae558 9789watchpoint::print_recreate (struct ui_file *fp) const
348d480f 9790{
3a292923 9791 switch (type)
348d480f
PA
9792 {
9793 case bp_watchpoint:
9794 case bp_hardware_watchpoint:
6cb06a8c 9795 gdb_printf (fp, "watch");
348d480f
PA
9796 break;
9797 case bp_read_watchpoint:
6cb06a8c 9798 gdb_printf (fp, "rwatch");
348d480f
PA
9799 break;
9800 case bp_access_watchpoint:
6cb06a8c 9801 gdb_printf (fp, "awatch");
348d480f
PA
9802 break;
9803 default:
f34652de 9804 internal_error (_("Invalid watchpoint type."));
348d480f
PA
9805 }
9806
3a292923 9807 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 9808 print_recreate_thread (fp);
348d480f
PA
9809}
9810
3a292923 9811/* Implement the "explains_signal" method for watchpoints. */
427cd150 9812
a6860f3a 9813bool
3a292923 9814watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
9815{
9816 /* A software watchpoint cannot cause a signal other than
9817 GDB_SIGNAL_TRAP. */
3a292923 9818 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 9819 return false;
427cd150 9820
a6860f3a 9821 return true;
427cd150
TT
9822}
9823
3a292923
TT
9824struct masked_watchpoint : public watchpoint
9825{
73063f51
TT
9826 using watchpoint::watchpoint;
9827
3a292923
TT
9828 int insert_location (struct bp_location *) override;
9829 int remove_location (struct bp_location *,
9830 enum remove_bp_reason reason) override;
9831 int resources_needed (const struct bp_location *) override;
5a61e176 9832 bool works_in_software_mode () const override;
7bd86313 9833 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 9834 void print_one_detail (struct ui_out *) const override;
b713485d 9835 void print_mention () const override;
4d1ae558 9836 void print_recreate (struct ui_file *fp) const override;
3a292923 9837};
348d480f 9838
3a292923 9839/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 9840
3a292923
TT
9841int
9842masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 9843{
3a292923 9844 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
9845 bl->watchpoint_type);
9846}
9847
3a292923 9848/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 9849
3a292923
TT
9850int
9851masked_watchpoint::remove_location (struct bp_location *bl,
9852 enum remove_bp_reason reason)
348d480f 9853{
3a292923 9854 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 9855 bl->watchpoint_type);
348d480f
PA
9856}
9857
3a292923
TT
9858/* Implement the "resources_needed" method for masked hardware
9859 watchpoints. */
348d480f 9860
3a292923
TT
9861int
9862masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 9863{
3a292923 9864 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
9865}
9866
3a292923
TT
9867/* Implement the "works_in_software_mode" method for masked hardware
9868 watchpoints. */
348d480f 9869
5a61e176 9870bool
3a292923 9871masked_watchpoint::works_in_software_mode () const
348d480f 9872{
5a61e176 9873 return false;
348d480f
PA
9874}
9875
3a292923
TT
9876/* Implement the "print_it" method for masked hardware
9877 watchpoints. */
348d480f 9878
3a292923 9879enum print_stop_action
7bd86313 9880masked_watchpoint::print_it (const bpstat *bs) const
348d480f
PA
9881{
9882 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9883 struct ui_out *uiout = current_uiout;
348d480f
PA
9884
9885 /* Masked watchpoints have only one location. */
9886 gdb_assert (b->loc && b->loc->next == NULL);
9887
f303dbd6
PA
9888 annotate_watchpoint (b->number);
9889 maybe_print_thread_hit_breakpoint (uiout);
9890
348d480f
PA
9891 switch (b->type)
9892 {
9893 case bp_hardware_watchpoint:
112e8700
SM
9894 if (uiout->is_mi_like_p ())
9895 uiout->field_string
9896 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9897 break;
9898
9899 case bp_read_watchpoint:
112e8700
SM
9900 if (uiout->is_mi_like_p ())
9901 uiout->field_string
9902 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9903 break;
9904
9905 case bp_access_watchpoint:
112e8700
SM
9906 if (uiout->is_mi_like_p ())
9907 uiout->field_string
9908 ("reason",
348d480f
PA
9909 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9910 break;
9911 default:
f34652de 9912 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9913 }
9914
9915 mention (b);
112e8700 9916 uiout->text (_("\n\
9c06b0b4
TJB
9917Check the underlying instruction at PC for the memory\n\
9918address and value which triggered this watchpoint.\n"));
112e8700 9919 uiout->text ("\n");
9c06b0b4
TJB
9920
9921 /* More than one watchpoint may have been triggered. */
9922 return PRINT_UNKNOWN;
9923}
9924
3a292923
TT
9925/* Implement the "print_one_detail" method for masked hardware
9926 watchpoints. */
9c06b0b4 9927
3a292923
TT
9928void
9929masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
9930{
9931 /* Masked watchpoints have only one location. */
3a292923 9932 gdb_assert (loc && loc->next == NULL);
9c06b0b4 9933
112e8700 9934 uiout->text ("\tmask ");
3a292923 9935 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
112e8700 9936 uiout->text ("\n");
9c06b0b4
TJB
9937}
9938
3a292923
TT
9939/* Implement the "print_mention" method for masked hardware
9940 watchpoints. */
9c06b0b4 9941
3a292923 9942void
b713485d 9943masked_watchpoint::print_mention () const
9c06b0b4 9944{
79a45e25 9945 struct ui_out *uiout = current_uiout;
46b9c129 9946 const char *tuple_name;
9c06b0b4 9947
3a292923 9948 switch (type)
9c06b0b4
TJB
9949 {
9950 case bp_hardware_watchpoint:
112e8700 9951 uiout->text ("Masked hardware watchpoint ");
46b9c129 9952 tuple_name = "wpt";
9c06b0b4
TJB
9953 break;
9954 case bp_read_watchpoint:
112e8700 9955 uiout->text ("Masked hardware read watchpoint ");
46b9c129 9956 tuple_name = "hw-rwpt";
9c06b0b4
TJB
9957 break;
9958 case bp_access_watchpoint:
112e8700 9959 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 9960 tuple_name = "hw-awpt";
9c06b0b4
TJB
9961 break;
9962 default:
f34652de 9963 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
9964 }
9965
46b9c129 9966 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9967 uiout->field_signed ("number", number);
112e8700 9968 uiout->text (": ");
3a292923 9969 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
9970}
9971
3a292923
TT
9972/* Implement the "print_recreate" method for masked hardware
9973 watchpoints. */
9c06b0b4 9974
3a292923 9975void
4d1ae558 9976masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 9977{
3a292923 9978 switch (type)
9c06b0b4
TJB
9979 {
9980 case bp_hardware_watchpoint:
6cb06a8c 9981 gdb_printf (fp, "watch");
9c06b0b4
TJB
9982 break;
9983 case bp_read_watchpoint:
6cb06a8c 9984 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
9985 break;
9986 case bp_access_watchpoint:
6cb06a8c 9987 gdb_printf (fp, "awatch");
9c06b0b4
TJB
9988 break;
9989 default:
f34652de 9990 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
9991 }
9992
3a292923
TT
9993 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9994 phex (hw_wp_mask, sizeof (CORE_ADDR)));
04d0163c 9995 print_recreate_thread (fp);
9c06b0b4
TJB
9996}
9997
9c06b0b4
TJB
9998/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9999
f2478a7e 10000static bool
9c06b0b4
TJB
10001is_masked_watchpoint (const struct breakpoint *b)
10002{
3a292923 10003 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
10004}
10005
53a5351d 10006/* accessflag: hw_write: watch write,
dda83cd7 10007 hw_read: watch read,
53a5351d 10008 hw_access: watch access (read or write) */
c906108c 10009static void
bbc13ae3 10010watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10011 bool just_location, bool internal)
c906108c 10012{
c1fc2657 10013 struct breakpoint *scope_breakpoint = NULL;
270140bd 10014 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10015 struct value *result;
bb9d5f81 10016 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10017 const char *exp_start = NULL;
10018 const char *exp_end = NULL;
10019 const char *tok, *end_tok;
9c06b0b4 10020 int toklen = -1;
bbc13ae3
KS
10021 const char *cond_start = NULL;
10022 const char *cond_end = NULL;
c906108c 10023 enum bptype bp_type;
37e4754d 10024 int thread = -1;
9c06b0b4
TJB
10025 /* Flag to indicate whether we are going to use masks for
10026 the hardware watchpoint. */
2e362716 10027 bool use_mask = false;
9c06b0b4 10028 CORE_ADDR mask = 0;
8a18382f 10029 int task = 0;
c906108c 10030
37e4754d
LM
10031 /* Make sure that we actually have parameters to parse. */
10032 if (arg != NULL && arg[0] != '\0')
10033 {
bbc13ae3
KS
10034 const char *value_start;
10035
10036 exp_end = arg + strlen (arg);
37e4754d 10037
9c06b0b4
TJB
10038 /* Look for "parameter value" pairs at the end
10039 of the arguments string. */
bbc13ae3 10040 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10041 {
10042 /* Skip whitespace at the end of the argument list. */
10043 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10044 tok--;
10045
10046 /* Find the beginning of the last token.
10047 This is the value of the parameter. */
10048 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10049 tok--;
10050 value_start = tok + 1;
10051
10052 /* Skip whitespace. */
10053 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10054 tok--;
10055
10056 end_tok = tok;
10057
10058 /* Find the beginning of the second to last token.
10059 This is the parameter itself. */
10060 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10061 tok--;
10062 tok++;
10063 toklen = end_tok - tok + 1;
10064
61012eef 10065 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10066 {
5d5658a1 10067 struct thread_info *thr;
9c06b0b4
TJB
10068 /* At this point we've found a "thread" token, which means
10069 the user is trying to set a watchpoint that triggers
10070 only in a specific thread. */
5d5658a1 10071 const char *endp;
37e4754d 10072
9c06b0b4
TJB
10073 if (thread != -1)
10074 error(_("You can specify only one thread."));
37e4754d 10075
9c06b0b4 10076 /* Extract the thread ID from the next token. */
5d5658a1 10077 thr = parse_thread_id (value_start, &endp);
37e4754d 10078
5d5658a1 10079 /* Check if the user provided a valid thread ID. */
9c06b0b4 10080 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10081 invalid_thread_id_error (value_start);
9c06b0b4 10082
5d5658a1 10083 thread = thr->global_num;
9c06b0b4 10084 }
8a18382f
TT
10085 else if (toklen == 4 && startswith (tok, "task"))
10086 {
10087 char *tmp;
10088
10089 task = strtol (value_start, &tmp, 0);
10090 if (tmp == value_start)
10091 error (_("Junk after task keyword."));
10092 if (!valid_task_id (task))
10093 error (_("Unknown task %d."), task);
10094 }
61012eef 10095 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10096 {
10097 /* We've found a "mask" token, which means the user wants to
10098 create a hardware watchpoint that is going to have the mask
10099 facility. */
65558ca5 10100 struct value *mask_value;
37e4754d 10101
9c06b0b4
TJB
10102 if (use_mask)
10103 error(_("You can specify only one mask."));
37e4754d 10104
2e362716 10105 use_mask = just_location = true;
37e4754d 10106
65558ca5 10107 scoped_value_mark mark;
9c06b0b4
TJB
10108 mask_value = parse_to_comma_and_eval (&value_start);
10109 mask = value_as_address (mask_value);
9c06b0b4
TJB
10110 }
10111 else
10112 /* We didn't recognize what we found. We should stop here. */
10113 break;
37e4754d 10114
9c06b0b4
TJB
10115 /* Truncate the string and get rid of the "parameter value" pair before
10116 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10117 exp_end = tok;
9c06b0b4 10118 }
37e4754d 10119 }
bbc13ae3
KS
10120 else
10121 exp_end = arg;
37e4754d 10122
bbc13ae3
KS
10123 /* Parse the rest of the arguments. From here on out, everything
10124 is in terms of a newly allocated string instead of the original
10125 ARG. */
81b1e71c
TT
10126 std::string expression (arg, exp_end - arg);
10127 exp_start = arg = expression.c_str ();
699bd4cf
TT
10128 innermost_block_tracker tracker;
10129 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10130 exp_end = arg;
fa8a61dc
TT
10131 /* Remove trailing whitespace from the expression before saving it.
10132 This makes the eventual display of the expression string a bit
10133 prettier. */
10134 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10135 --exp_end;
10136
65d79d4b 10137 /* Checking if the expression is not constant. */
4d01a485 10138 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10139 {
10140 int len;
10141
10142 len = exp_end - exp_start;
10143 while (len > 0 && isspace (exp_start[len - 1]))
10144 len--;
10145 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10146 }
10147
699bd4cf 10148 exp_valid_block = tracker.block ();
b926417a 10149 struct value *mark = value_mark ();
850645cf 10150 struct value *val_as_value = nullptr;
1eaebe02
TT
10151 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10152 just_location);
06a64a0b 10153
850645cf 10154 if (val_as_value != NULL && just_location)
bb9d5f81 10155 {
850645cf
TT
10156 saved_bitpos = value_bitpos (val_as_value);
10157 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10158 }
10159
850645cf 10160 value_ref_ptr val;
06a64a0b
TT
10161 if (just_location)
10162 {
9c06b0b4
TJB
10163 int ret;
10164
06a64a0b 10165 exp_valid_block = NULL;
850645cf 10166 val = release_value (value_addr (result));
06a64a0b 10167 value_free_to_mark (mark);
9c06b0b4
TJB
10168
10169 if (use_mask)
10170 {
850645cf 10171 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10172 mask);
10173 if (ret == -1)
10174 error (_("This target does not support masked watchpoints."));
10175 else if (ret == -2)
10176 error (_("Invalid mask or memory region."));
10177 }
06a64a0b 10178 }
850645cf
TT
10179 else if (val_as_value != NULL)
10180 val = release_value (val_as_value);
c906108c 10181
f1735a53
TT
10182 tok = skip_spaces (arg);
10183 end_tok = skip_to_space (tok);
c906108c
SS
10184
10185 toklen = end_tok - tok;
10186 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10187 {
10188 tok = cond_start = end_tok + 1;
699bd4cf
TT
10189 innermost_block_tracker if_tracker;
10190 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10191
10192 /* The watchpoint expression may not be local, but the condition
10193 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10194 cond_exp_valid_block = if_tracker.block ();
60e1c644 10195
c906108c
SS
10196 cond_end = tok;
10197 }
10198 if (*tok)
8a3fe4f8 10199 error (_("Junk at end of command."));
c906108c 10200
bd2b40ac 10201 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
441d7c93
PA
10202
10203 /* Save this because create_internal_breakpoint below invalidates
10204 'wp_frame'. */
10205 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10206
10207 /* If the expression is "local", then set up a "watchpoint scope"
10208 breakpoint at the point where we've left the scope of the watchpoint
10209 expression. Create the scope breakpoint before the watchpoint, so
10210 that we will encounter it first in bpstat_stop_status. */
441d7c93 10211 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10212 {
441d7c93
PA
10213 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10214
10215 if (frame_id_p (caller_frame_id))
edb3359d 10216 {
441d7c93
PA
10217 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10218 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10219
24b21115 10220 scope_breakpoint
441d7c93 10221 = create_internal_breakpoint (caller_arch, caller_pc,
9efa3c7f 10222 bp_watchpoint_scope);
d983da9c 10223
441d7c93
PA
10224 /* create_internal_breakpoint could invalidate WP_FRAME. */
10225 wp_frame = NULL;
10226
edb3359d 10227 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10228
edb3359d
DJ
10229 /* Automatically delete the breakpoint when it hits. */
10230 scope_breakpoint->disposition = disp_del;
d983da9c 10231
edb3359d 10232 /* Only break in the proper frame (help with recursion). */
441d7c93 10233 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10234
edb3359d 10235 /* Set the address at which we will stop. */
441d7c93
PA
10236 scope_breakpoint->loc->gdbarch = caller_arch;
10237 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10238 scope_breakpoint->loc->address
a6d9a66e
UW
10239 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10240 scope_breakpoint->loc->requested_address,
60270718
AB
10241 scope_breakpoint->type,
10242 current_program_space);
edb3359d 10243 }
d983da9c
DJ
10244 }
10245
e8369a73
AB
10246 /* Now set up the breakpoint. We create all watchpoints as hardware
10247 watchpoints here even if hardware watchpoints are turned off, a call
10248 to update_watchpoint later in this function will cause the type to
10249 drop back to bp_watchpoint (software watchpoint) if required. */
10250
10251 if (accessflag == hw_read)
10252 bp_type = bp_read_watchpoint;
10253 else if (accessflag == hw_access)
10254 bp_type = bp_access_watchpoint;
10255 else
10256 bp_type = bp_hardware_watchpoint;
3a5c3e22 10257
3a292923 10258 std::unique_ptr<watchpoint> w;
348d480f 10259 if (use_mask)
73063f51 10260 w.reset (new masked_watchpoint (nullptr, bp_type));
348d480f 10261 else
73063f51 10262 w.reset (new watchpoint (nullptr, bp_type));
3a292923 10263
c1fc2657 10264 w->thread = thread;
8a18382f 10265 w->task = task;
c1fc2657
SM
10266 w->disposition = disp_donttouch;
10267 w->pspace = current_program_space;
b22e99fd 10268 w->exp = std::move (exp);
3a5c3e22
PA
10269 w->exp_valid_block = exp_valid_block;
10270 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10271 if (just_location)
10272 {
850645cf
TT
10273 struct type *t = value_type (val.get ());
10274 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10275
43cc5389 10276 w->exp_string_reparse
a4c50be3 10277 = current_language->watch_location_expression (t, addr);
06a64a0b 10278
8579fd13
AB
10279 w->exp_string = xstrprintf ("-location %.*s",
10280 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10281 }
10282 else
a4c50be3 10283 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10284
10285 if (use_mask)
10286 {
3a5c3e22 10287 w->hw_wp_mask = mask;
9c06b0b4
TJB
10288 }
10289 else
10290 {
3a5c3e22 10291 w->val = val;
bb9d5f81
PP
10292 w->val_bitpos = saved_bitpos;
10293 w->val_bitsize = saved_bitsize;
4c1d86d9 10294 w->val_valid = true;
9c06b0b4 10295 }
77b06cd7 10296
c906108c 10297 if (cond_start)
6f781ee3 10298 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10299 else
c1fc2657 10300 w->cond_string = 0;
c5aa993b 10301
441d7c93 10302 if (frame_id_p (watchpoint_frame))
f6bc2008 10303 {
441d7c93 10304 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10305 w->watchpoint_thread = inferior_ptid;
f6bc2008 10306 }
c906108c 10307 else
f6bc2008 10308 {
3a5c3e22
PA
10309 w->watchpoint_frame = null_frame_id;
10310 w->watchpoint_thread = null_ptid;
f6bc2008 10311 }
c906108c 10312
d983da9c 10313 if (scope_breakpoint != NULL)
c906108c 10314 {
d983da9c
DJ
10315 /* The scope breakpoint is related to the watchpoint. We will
10316 need to act on them together. */
c1fc2657 10317 w->related_breakpoint = scope_breakpoint;
b270e6f9 10318 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10319 }
d983da9c 10320
06a64a0b
TT
10321 if (!just_location)
10322 value_free_to_mark (mark);
2d134ed3 10323
b270e6f9
TT
10324 /* Finally update the new watchpoint. This creates the locations
10325 that should be inserted. */
d8de7963 10326 update_watchpoint (w.get (), true /* reparse */);
a9634178 10327
b270e6f9 10328 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10329}
10330
e09342b5 10331/* Return count of debug registers needed to watch the given expression.
e09342b5 10332 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10333
c906108c 10334static int
a6535de1 10335can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10336{
10337 int found_memory_cnt = 0;
10338
10339 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10340 if (!can_use_hw_watchpoints)
c906108c 10341 return 0;
c5aa993b 10342
a6535de1
TT
10343 gdb_assert (!vals.empty ());
10344 struct value *head = vals[0].get ();
10345
5c44784c
JM
10346 /* Make sure that the value of the expression depends only upon
10347 memory contents, and values computed from them within GDB. If we
10348 find any register references or function calls, we can't use a
10349 hardware watchpoint.
10350
10351 The idea here is that evaluating an expression generates a series
10352 of values, one holding the value of every subexpression. (The
10353 expression a*b+c has five subexpressions: a, b, a*b, c, and
10354 a*b+c.) GDB's values hold almost enough information to establish
10355 the criteria given above --- they identify memory lvalues,
10356 register lvalues, computed values, etcetera. So we can evaluate
10357 the expression, and then scan the chain of values that leaves
10358 behind to decide whether we can detect any possible change to the
10359 expression's final value using only hardware watchpoints.
10360
10361 However, I don't think that the values returned by inferior
10362 function calls are special in any way. So this function may not
10363 notice that an expression involving an inferior function call
10364 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10365 for (const value_ref_ptr &iter : vals)
c906108c 10366 {
a6535de1
TT
10367 struct value *v = iter.get ();
10368
5c44784c 10369 if (VALUE_LVAL (v) == lval_memory)
c906108c 10370 {
8464be76
DJ
10371 if (v != head && value_lazy (v))
10372 /* A lazy memory lvalue in the chain is one that GDB never
10373 needed to fetch; we either just used its address (e.g.,
10374 `a' in `a.b') or we never needed it at all (e.g., `a'
10375 in `a,b'). This doesn't apply to HEAD; if that is
10376 lazy then it was not readable, but watch it anyway. */
5c44784c 10377 ;
53a5351d 10378 else
5c44784c
JM
10379 {
10380 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10381 it with hardware watchpoints. */
df407dfe 10382 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10383
10384 /* We only watch structs and arrays if user asked for it
10385 explicitly, never if they just happen to appear in a
10386 middle of some value chain. */
10387 if (v == head
78134374
SM
10388 || (vtype->code () != TYPE_CODE_STRUCT
10389 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10390 {
42ae5230 10391 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10392 int len;
10393 int num_regs;
10394
a9634178 10395 len = (target_exact_watchpoints
e09342b5 10396 && is_scalar_type_recursive (vtype))?
df86565b 10397 1 : value_type (v)->length ();
2e70b7b9 10398
e09342b5
TJB
10399 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10400 if (!num_regs)
2e70b7b9
MS
10401 return 0;
10402 else
e09342b5 10403 found_memory_cnt += num_regs;
2e70b7b9 10404 }
5c44784c 10405 }
c5aa993b 10406 }
5086187c
AC
10407 else if (VALUE_LVAL (v) != not_lval
10408 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10409 return 0; /* These are values from the history (e.g., $1). */
5086187c 10410 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10411 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10412 }
10413
10414 /* The expression itself looks suitable for using a hardware
10415 watchpoint, but give the target machine a chance to reject it. */
10416 return found_memory_cnt;
10417}
10418
8b93c638 10419void
2e362716 10420watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10421{
84f4c1fe 10422 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10423}
10424
a15a5258
AB
10425/* Options for the watch, awatch, and rwatch commands. */
10426
10427struct watch_options
10428{
10429 /* For -location. */
10430 bool location = false;
10431};
10432
10433/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10434
10435 Historically GDB always accepted both '-location' and '-l' flags for
10436 these commands (both flags being synonyms). When converting to the
10437 newer option scheme only '-location' is added here. That's fine (for
10438 backward compatibility) as any non-ambiguous prefix of a flag will be
10439 accepted, so '-l', '-loc', are now all accepted.
10440
10441 What this means is that, if in the future, we add any new flag here
10442 that starts with '-l' then this will break backward compatibility, so
10443 please, don't do that! */
10444
10445static const gdb::option::option_def watch_option_defs[] = {
10446 gdb::option::flag_option_def<watch_options> {
10447 "location",
10448 [] (watch_options *opt) { return &opt->location; },
10449 N_("\
10450This evaluates EXPRESSION and watches the memory to which is refers.\n\
10451-l can be used as a short form of -location."),
10452 },
10453};
10454
10455/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10456 commands. */
10457
10458static gdb::option::option_def_group
10459make_watch_options_def_group (watch_options *opts)
10460{
10461 return {{watch_option_defs}, opts};
10462}
10463
06a64a0b
TT
10464/* A helper function that looks for the "-location" argument and then
10465 calls watch_command_1. */
10466
10467static void
0b39b52e 10468watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10469{
a15a5258
AB
10470 watch_options opts;
10471 auto grp = make_watch_options_def_group (&opts);
10472 gdb::option::process_options
10473 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10474 if (arg != nullptr && *arg == '\0')
10475 arg = nullptr;
10476
10477 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10478}
06a64a0b 10479
a15a5258
AB
10480/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10481static void
10482watch_command_completer (struct cmd_list_element *ignore,
10483 completion_tracker &tracker,
10484 const char *text, const char * /*word*/)
10485{
10486 const auto group = make_watch_options_def_group (nullptr);
10487 if (gdb::option::complete_options
10488 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10489 return;
06a64a0b 10490
a15a5258
AB
10491 const char *word = advance_to_expression_complete_word_point (tracker, text);
10492 expression_completer (ignore, tracker, text, word);
8b93c638 10493}
8926118c 10494
c5aa993b 10495static void
0b39b52e 10496watch_command (const char *arg, int from_tty)
c906108c 10497{
06a64a0b 10498 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10499}
10500
8b93c638 10501void
2e362716 10502rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10503{
84f4c1fe 10504 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10505}
8926118c 10506
c5aa993b 10507static void
0b39b52e 10508rwatch_command (const char *arg, int from_tty)
c906108c 10509{
06a64a0b 10510 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10511}
10512
8b93c638 10513void
2e362716 10514awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10515{
84f4c1fe 10516 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10517}
8926118c 10518
c5aa993b 10519static void
0b39b52e 10520awatch_command (const char *arg, int from_tty)
c906108c 10521{
06a64a0b 10522 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10523}
c906108c 10524\f
c5aa993b 10525
cfc31633
PA
10526/* Data for the FSM that manages the until(location)/advance commands
10527 in infcmd.c. Here because it uses the mechanisms of
10528 breakpoints. */
c906108c 10529
46e3ed7f 10530struct until_break_fsm : public thread_fsm
bfec99b2 10531{
46e3ed7f 10532 /* The thread that was current when the command was executed. */
cfc31633
PA
10533 int thread;
10534
df631783
PA
10535 /* The breakpoint set at the return address in the caller frame,
10536 plus breakpoints at all the destination locations. */
10537 std::vector<breakpoint_up> breakpoints;
cfc31633 10538
46e3ed7f 10539 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 10540 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
10541 : thread_fsm (cmd_interp),
10542 thread (thread),
df631783 10543 breakpoints (std::move (breakpoints))
46e3ed7f
TT
10544 {
10545 }
cfc31633 10546
46e3ed7f
TT
10547 void clean_up (struct thread_info *thread) override;
10548 bool should_stop (struct thread_info *thread) override;
10549 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10550};
10551
cfc31633
PA
10552/* Implementation of the 'should_stop' FSM method for the
10553 until(location)/advance commands. */
10554
46e3ed7f
TT
10555bool
10556until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10557{
df631783
PA
10558 for (const breakpoint_up &bp : breakpoints)
10559 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10560 bp.get ()) != NULL)
10561 {
10562 set_finished ();
10563 break;
10564 }
cfc31633 10565
46e3ed7f 10566 return true;
cfc31633
PA
10567}
10568
10569/* Implementation of the 'clean_up' FSM method for the
10570 until(location)/advance commands. */
10571
46e3ed7f
TT
10572void
10573until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10574{
cfc31633 10575 /* Clean up our temporary breakpoints. */
df631783 10576 breakpoints.clear ();
46e3ed7f 10577 delete_longjmp_breakpoint (thread);
cfc31633
PA
10578}
10579
10580/* Implementation of the 'async_reply_reason' FSM method for the
10581 until(location)/advance commands. */
10582
46e3ed7f
TT
10583enum async_reply_reason
10584until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10585{
10586 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10587}
10588
c906108c 10589void
f2fc3015 10590until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10591{
bd2b40ac 10592 frame_info_ptr frame;
8556afb4
PA
10593 struct gdbarch *frame_gdbarch;
10594 struct frame_id stack_frame_id;
10595 struct frame_id caller_frame_id;
186c406b
TT
10596 int thread;
10597 struct thread_info *tp;
c906108c 10598
70509625 10599 clear_proceed_status (0);
c906108c
SS
10600
10601 /* Set a breakpoint where the user wants it and at return from
4a64f543 10602 this function. */
c5aa993b 10603
264f9890 10604 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f 10605
6c5b2ebe
PA
10606 std::vector<symtab_and_line> sals
10607 = (last_displayed_sal_is_valid ()
264f9890 10608 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
6c5b2ebe
PA
10609 get_last_displayed_symtab (),
10610 get_last_displayed_line ())
264f9890 10611 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 10612 NULL, NULL, 0));
c5aa993b 10613
df631783 10614 if (sals.empty ())
8a3fe4f8 10615 error (_("Couldn't get information on specified line."));
c5aa993b 10616
c906108c 10617 if (*arg)
8a3fe4f8 10618 error (_("Junk at end of arguments."));
c5aa993b 10619
186c406b 10620 tp = inferior_thread ();
5d5658a1 10621 thread = tp->global_num;
186c406b 10622
8556afb4
PA
10623 /* Note linespec handling above invalidates the frame chain.
10624 Installing a breakpoint also invalidates the frame chain (as it
10625 may need to switch threads), so do any frame handling before
10626 that. */
10627
10628 frame = get_selected_frame (NULL);
10629 frame_gdbarch = get_frame_arch (frame);
10630 stack_frame_id = get_stack_frame_id (frame);
10631 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10632
ae66c1fc
EZ
10633 /* Keep within the current frame, or in frames called by the current
10634 one. */
edb3359d 10635
df631783 10636 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
10637
10638 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10639
883bc8d1 10640 if (frame_id_p (caller_frame_id))
c906108c 10641 {
883bc8d1 10642 struct symtab_and_line sal2;
cfc31633 10643 struct gdbarch *caller_gdbarch;
883bc8d1
PA
10644
10645 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10646 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 10647 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
10648
10649 breakpoint_up caller_breakpoint
10650 = set_momentary_breakpoint (caller_gdbarch, sal2,
10651 caller_frame_id, bp_until);
10652 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 10653
0f443d1b 10654 set_longjmp_breakpoint (tp, stack_frame_id);
5419bdae 10655 lj_deleter.emplace (thread);
c906108c 10656 }
c5aa993b 10657
c70a6932
JK
10658 /* set_momentary_breakpoint could invalidate FRAME. */
10659 frame = NULL;
10660
df631783
PA
10661 /* If the user told us to continue until a specified location, we
10662 don't specify a frame at which we need to stop. Otherwise,
10663 specify the selected frame, because we want to stop only at the
10664 very same frame. */
10665 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10666
10667 for (symtab_and_line &sal : sals)
10668 {
10669 resolve_sal_pc (&sal);
10670
10671 breakpoint_up location_breakpoint
10672 = set_momentary_breakpoint (frame_gdbarch, sal,
10673 stop_frame_id, bp_until);
10674 breakpoints.emplace_back (std::move (location_breakpoint));
10675 }
883bc8d1 10676
573269a8
LS
10677 tp->set_thread_fsm
10678 (std::unique_ptr<thread_fsm>
10679 (new until_break_fsm (command_interp (), tp->global_num,
10680 std::move (breakpoints))));
f107f563 10681
5419bdae
TT
10682 if (lj_deleter)
10683 lj_deleter->release ();
f107f563 10684
cfc31633 10685 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 10686}
ae66c1fc 10687
c906108c
SS
10688\f
10689
81b1e71c 10690/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
10691
10692static int
81b1e71c 10693compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 10694{
81b1e71c
TT
10695 uintptr_t ua = (uintptr_t) a;
10696 uintptr_t ub = (uintptr_t) b;
8a2c437b 10697
81b1e71c 10698 if (a->number < b->number)
8a2c437b 10699 return -1;
81b1e71c 10700 else if (a->number > b->number)
8a2c437b
TT
10701 return 1;
10702
10703 /* Now sort by address, in case we see, e..g, two breakpoints with
10704 the number 0. */
10705 if (ua < ub)
10706 return -1;
94b0e70d 10707 return ua > ub ? 1 : 0;
8a2c437b
TT
10708}
10709
80f8a6eb 10710/* Delete breakpoints by address or line. */
c906108c
SS
10711
10712static void
0b39b52e 10713clear_command (const char *arg, int from_tty)
c906108c 10714{
c906108c 10715 int default_match;
c906108c 10716
6c5b2ebe
PA
10717 std::vector<symtab_and_line> decoded_sals;
10718 symtab_and_line last_sal;
10719 gdb::array_view<symtab_and_line> sals;
c906108c
SS
10720 if (arg)
10721 {
6c5b2ebe
PA
10722 decoded_sals
10723 = decode_line_with_current_source (arg,
10724 (DECODE_LINE_FUNFIRSTLINE
10725 | DECODE_LINE_LIST_MODE));
c906108c 10726 default_match = 0;
6c5b2ebe 10727 sals = decoded_sals;
c906108c
SS
10728 }
10729 else
10730 {
1bfeeb0f
JL
10731 /* Set sal's line, symtab, pc, and pspace to the values
10732 corresponding to the last call to print_frame_info. If the
10733 codepoint is not valid, this will set all the fields to 0. */
51abb421 10734 last_sal = get_last_displayed_sal ();
6c5b2ebe 10735 if (last_sal.symtab == 0)
8a3fe4f8 10736 error (_("No source file specified."));
c906108c 10737
c906108c 10738 default_match = 1;
6c5b2ebe 10739 sals = last_sal;
c906108c
SS
10740 }
10741
4a64f543
MS
10742 /* We don't call resolve_sal_pc here. That's not as bad as it
10743 seems, because all existing breakpoints typically have both
10744 file/line and pc set. So, if clear is given file/line, we can
10745 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10746
10747 We only support clearing given the address explicitly
10748 present in breakpoint table. Say, we've set breakpoint
4a64f543 10749 at file:line. There were several PC values for that file:line,
ed0616c6 10750 due to optimization, all in one block.
4a64f543
MS
10751
10752 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10753 PC corresponding to the same file:line, the breakpoint won't
10754 be cleared. We probably can still clear the breakpoint, but
10755 since the other PC value is never presented to user, user
10756 can only find it by guessing, and it does not seem important
10757 to support that. */
10758
4a64f543
MS
10759 /* For each line spec given, delete bps which correspond to it. Do
10760 it in two passes, solely to preserve the current behavior that
10761 from_tty is forced true if we delete more than one
10762 breakpoint. */
c906108c 10763
81b1e71c 10764 std::vector<struct breakpoint *> found;
6c5b2ebe 10765 for (const auto &sal : sals)
c906108c 10766 {
05cba821
JK
10767 const char *sal_fullname;
10768
c906108c 10769 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
10770 If line given (pc == 0), clear all bpts on specified line.
10771 If defaulting, clear all bpts on default line
10772 or at default pc.
c5aa993b 10773
dda83cd7 10774 defaulting sal.pc != 0 tests to do
c5aa993b 10775
dda83cd7
SM
10776 0 1 pc
10777 1 1 pc _and_ line
10778 0 0 line
10779 1 0 <can't happen> */
c906108c 10780
05cba821
JK
10781 sal_fullname = (sal.symtab == NULL
10782 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 10783
4a64f543 10784 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 10785 for (breakpoint *b : all_breakpoints ())
c5aa993b 10786 {
0d381245 10787 int match = 0;
4a64f543 10788 /* Are we going to delete b? */
a5c69b1e
EL
10789 if (b->type != bp_none && !is_watchpoint (b)
10790 && user_breakpoint_p (b))
0d381245 10791 {
40cb8ca5 10792 for (bp_location *loc : b->locations ())
0d381245 10793 {
f8eba3c6
TT
10794 /* If the user specified file:line, don't allow a PC
10795 match. This matches historical gdb behavior. */
10796 int pc_match = (!sal.explicit_line
10797 && sal.pc
10798 && (loc->pspace == sal.pspace)
10799 && (loc->address == sal.pc)
10800 && (!section_is_overlay (loc->section)
10801 || loc->section == sal.section));
4aac40c8
TT
10802 int line_match = 0;
10803
10804 if ((default_match || sal.explicit_line)
2f202fde 10805 && loc->symtab != NULL
05cba821 10806 && sal_fullname != NULL
4aac40c8 10807 && sal.pspace == loc->pspace
05cba821
JK
10808 && loc->line_number == sal.line
10809 && filename_cmp (symtab_to_fullname (loc->symtab),
10810 sal_fullname) == 0)
10811 line_match = 1;
4aac40c8 10812
0d381245
VP
10813 if (pc_match || line_match)
10814 {
10815 match = 1;
10816 break;
10817 }
10818 }
10819 }
10820
10821 if (match)
81b1e71c 10822 found.push_back (b);
c906108c 10823 }
80f8a6eb 10824 }
8a2c437b 10825
80f8a6eb 10826 /* Now go thru the 'found' chain and delete them. */
81b1e71c 10827 if (found.empty ())
80f8a6eb
MS
10828 {
10829 if (arg)
8a3fe4f8 10830 error (_("No breakpoint at %s."), arg);
80f8a6eb 10831 else
8a3fe4f8 10832 error (_("No breakpoint at this line."));
80f8a6eb 10833 }
c906108c 10834
8a2c437b 10835 /* Remove duplicates from the vec. */
81b1e71c 10836 std::sort (found.begin (), found.end (),
b926417a 10837 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10838 {
b926417a 10839 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
10840 });
10841 found.erase (std::unique (found.begin (), found.end (),
b926417a 10842 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10843 {
b926417a 10844 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
10845 }),
10846 found.end ());
8a2c437b 10847
81b1e71c 10848 if (found.size () > 1)
4a64f543 10849 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10850 if (from_tty)
a3f17187 10851 {
81b1e71c 10852 if (found.size () == 1)
6cb06a8c 10853 gdb_printf (_("Deleted breakpoint "));
a3f17187 10854 else
6cb06a8c 10855 gdb_printf (_("Deleted breakpoints "));
a3f17187 10856 }
d6e956e5 10857
81b1e71c 10858 for (breakpoint *iter : found)
80f8a6eb 10859 {
c5aa993b 10860 if (from_tty)
6cb06a8c 10861 gdb_printf ("%d ", iter->number);
81b1e71c 10862 delete_breakpoint (iter);
c906108c 10863 }
80f8a6eb 10864 if (from_tty)
a11ac3b3 10865 gdb_putc ('\n');
c906108c
SS
10866}
10867\f
10868/* Delete breakpoint in BS if they are `delete' breakpoints and
10869 all breakpoints that are marked for deletion, whether hit or not.
10870 This is called after any breakpoint is hit, or after errors. */
10871
10872void
313f3b21 10873breakpoint_auto_delete (bpstat *bs)
c906108c 10874{
c906108c 10875 for (; bs; bs = bs->next)
f431efe5
PA
10876 if (bs->breakpoint_at
10877 && bs->breakpoint_at->disposition == disp_del
c906108c 10878 && bs->stop)
f431efe5 10879 delete_breakpoint (bs->breakpoint_at);
c906108c 10880
1428b37a 10881 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 10882 if (b->disposition == disp_del_at_next_stop)
c5aa993b 10883 delete_breakpoint (b);
c906108c
SS
10884}
10885
4a64f543 10886/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 10887 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
10888 bl_address_is_meaningful says), secondarily by ordering first
10889 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 10890 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
10891
10892static int
39ef2f62 10893bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 10894{
876fa593 10895 if (a->address != b->address)
39ef2f62 10896 return a->address < b->address;
876fa593 10897
dea2aa5f
LM
10898 /* Sort locations at the same address by their pspace number, keeping
10899 locations of the same inferior (in a multi-inferior environment)
10900 grouped. */
10901
10902 if (a->pspace->num != b->pspace->num)
39ef2f62 10903 return a->pspace->num < b->pspace->num;
dea2aa5f 10904
876fa593 10905 /* Sort permanent breakpoints first. */
1a853c52 10906 if (a->permanent != b->permanent)
39ef2f62 10907 return a->permanent > b->permanent;
876fa593 10908
7f32a4d5
PA
10909 /* Sort by type in order to make duplicate determination easier.
10910 See update_global_location_list. This is kept in sync with
10911 breakpoint_locations_match. */
10912 if (a->loc_type < b->loc_type)
10913 return true;
10914
10915 /* Likewise, for range-breakpoints, sort by length. */
10916 if (a->loc_type == bp_loc_hardware_breakpoint
10917 && b->loc_type == bp_loc_hardware_breakpoint
10918 && a->length < b->length)
10919 return true;
10920
c56a97f9
JK
10921 /* Make the internal GDB representation stable across GDB runs
10922 where A and B memory inside GDB can differ. Breakpoint locations of
10923 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
10924
10925 if (a->owner->number != b->owner->number)
39ef2f62 10926 return a->owner->number < b->owner->number;
876fa593 10927
39ef2f62 10928 return a < b;
876fa593
JK
10929}
10930
f5336ca5
PA
10931/* Set bp_locations_placed_address_before_address_max and
10932 bp_locations_shadow_len_after_address_max according to the current
10933 content of the bp_locations array. */
f7545552
TT
10934
10935static void
f5336ca5 10936bp_locations_target_extensions_update (void)
f7545552 10937{
f5336ca5
PA
10938 bp_locations_placed_address_before_address_max = 0;
10939 bp_locations_shadow_len_after_address_max = 0;
876fa593 10940
48d7020b 10941 for (bp_location *bl : all_bp_locations ())
876fa593
JK
10942 {
10943 CORE_ADDR start, end, addr;
10944
10945 if (!bp_location_has_shadow (bl))
10946 continue;
10947
10948 start = bl->target_info.placed_address;
10949 end = start + bl->target_info.shadow_len;
10950
10951 gdb_assert (bl->address >= start);
10952 addr = bl->address - start;
f5336ca5
PA
10953 if (addr > bp_locations_placed_address_before_address_max)
10954 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
10955
10956 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10957
10958 gdb_assert (bl->address < end);
10959 addr = end - bl->address;
f5336ca5
PA
10960 if (addr > bp_locations_shadow_len_after_address_max)
10961 bp_locations_shadow_len_after_address_max = addr;
876fa593 10962 }
f7545552
TT
10963}
10964
1e4d1764
YQ
10965/* Download tracepoint locations if they haven't been. */
10966
10967static void
10968download_tracepoint_locations (void)
10969{
dd2e65cc 10970 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 10971
5ed8105e 10972 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 10973
f6d17b2b 10974 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
10975 {
10976 struct tracepoint *t;
d8de7963 10977 bool bp_location_downloaded = false;
1e4d1764 10978
7ed2c994 10979 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
10980 ? !may_insert_fast_tracepoints
10981 : !may_insert_tracepoints))
10982 continue;
10983
dd2e65cc
YQ
10984 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10985 {
10986 if (target_can_download_tracepoint ())
10987 can_download_tracepoint = TRIBOOL_TRUE;
10988 else
10989 can_download_tracepoint = TRIBOOL_FALSE;
10990 }
10991
10992 if (can_download_tracepoint == TRIBOOL_FALSE)
10993 break;
10994
40cb8ca5 10995 for (bp_location *bl : b->locations ())
7ed2c994
YQ
10996 {
10997 /* In tracepoint, locations are _never_ duplicated, so
10998 should_be_inserted is equivalent to
10999 unduplicated_should_be_inserted. */
11000 if (!should_be_inserted (bl) || bl->inserted)
11001 continue;
1e4d1764 11002
7ed2c994 11003 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11004
7ed2c994 11005 target_download_tracepoint (bl);
1e4d1764 11006
7ed2c994 11007 bl->inserted = 1;
d8de7963 11008 bp_location_downloaded = true;
7ed2c994
YQ
11009 }
11010 t = (struct tracepoint *) b;
11011 t->number_on_target = b->number;
f2a8bc8a 11012 if (bp_location_downloaded)
76727919 11013 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11014 }
1e4d1764
YQ
11015}
11016
934709f0
PW
11017/* Swap the insertion/duplication state between two locations. */
11018
11019static void
11020swap_insertion (struct bp_location *left, struct bp_location *right)
11021{
11022 const int left_inserted = left->inserted;
11023 const int left_duplicate = left->duplicate;
b775012e 11024 const int left_needs_update = left->needs_update;
934709f0
PW
11025 const struct bp_target_info left_target_info = left->target_info;
11026
1e4d1764
YQ
11027 /* Locations of tracepoints can never be duplicated. */
11028 if (is_tracepoint (left->owner))
11029 gdb_assert (!left->duplicate);
11030 if (is_tracepoint (right->owner))
11031 gdb_assert (!right->duplicate);
11032
934709f0
PW
11033 left->inserted = right->inserted;
11034 left->duplicate = right->duplicate;
b775012e 11035 left->needs_update = right->needs_update;
934709f0
PW
11036 left->target_info = right->target_info;
11037 right->inserted = left_inserted;
11038 right->duplicate = left_duplicate;
b775012e 11039 right->needs_update = left_needs_update;
934709f0
PW
11040 right->target_info = left_target_info;
11041}
11042
b775012e
LM
11043/* Force the re-insertion of the locations at ADDRESS. This is called
11044 once a new/deleted/modified duplicate location is found and we are evaluating
11045 conditions on the target's side. Such conditions need to be updated on
11046 the target. */
11047
11048static void
11049force_breakpoint_reinsertion (struct bp_location *bl)
11050{
b775012e
LM
11051 CORE_ADDR address = 0;
11052 int pspace_num;
11053
11054 address = bl->address;
11055 pspace_num = bl->pspace->num;
11056
11057 /* This is only meaningful if the target is
11058 evaluating conditions and if the user has
11059 opted for condition evaluation on the target's
11060 side. */
11061 if (gdb_evaluates_breakpoint_condition_p ()
11062 || !target_supports_evaluation_of_breakpoint_conditions ())
11063 return;
11064
11065 /* Flag all breakpoint locations with this address and
11066 the same program space as the location
11067 as "its condition has changed". We need to
11068 update the conditions on the target's side. */
e0d9a270 11069 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11070 {
b775012e
LM
11071 if (!is_breakpoint (loc->owner)
11072 || pspace_num != loc->pspace->num)
11073 continue;
11074
11075 /* Flag the location appropriately. We use a different state to
11076 let everyone know that we already updated the set of locations
11077 with addr bl->address and program space bl->pspace. This is so
11078 we don't have to keep calling these functions just to mark locations
11079 that have already been marked. */
11080 loc->condition_changed = condition_updated;
11081
11082 /* Free the agent expression bytecode as well. We will compute
11083 it later on. */
833177a4 11084 loc->cond_bytecode.reset ();
b775012e
LM
11085 }
11086}
7f32a4d5 11087
44702360
PA
11088/* Called whether new breakpoints are created, or existing breakpoints
11089 deleted, to update the global location list and recompute which
11090 locations are duplicate of which.
b775012e 11091
04086b45
PA
11092 The INSERT_MODE flag determines whether locations may not, may, or
11093 shall be inserted now. See 'enum ugll_insert_mode' for more
11094 info. */
b60e7edf 11095
0d381245 11096static void
44702360 11097update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11098{
b775012e
LM
11099 /* Last breakpoint location address that was marked for update. */
11100 CORE_ADDR last_addr = 0;
11101 /* Last breakpoint location program space that was marked for update. */
11102 int last_pspace_num = -1;
f7545552 11103
2d134ed3
PA
11104 /* Used in the duplicates detection below. When iterating over all
11105 bp_locations, points to the first bp_location of a given address.
11106 Breakpoints and watchpoints of different types are never
11107 duplicates of each other. Keep one pointer for each type of
11108 breakpoint/watchpoint, so we only need to loop over all locations
11109 once. */
11110 struct bp_location *bp_loc_first; /* breakpoint */
11111 struct bp_location *wp_loc_first; /* hardware watchpoint */
11112 struct bp_location *awp_loc_first; /* access watchpoint */
11113 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11114
f5336ca5
PA
11115 /* Saved former bp_locations array which we compare against the newly
11116 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11117 std::vector<bp_location *> old_locations = std::move (bp_locations);
11118 bp_locations.clear ();
876fa593 11119
43892fdf 11120 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11121 for (bp_location *loc : b->locations ())
5d51cd5d 11122 bp_locations.push_back (loc);
7f32a4d5
PA
11123
11124 /* See if we need to "upgrade" a software breakpoint to a hardware
11125 breakpoint. Do this before deciding whether locations are
11126 duplicates. Also do this before sorting because sorting order
11127 depends on location type. */
5d51cd5d
SM
11128 for (bp_location *loc : bp_locations)
11129 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11130 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11131
5d51cd5d 11132 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11133 bp_location_is_less_than);
876fa593 11134
f5336ca5 11135 bp_locations_target_extensions_update ();
74960c60 11136
4a64f543
MS
11137 /* Identify bp_location instances that are no longer present in the
11138 new list, and therefore should be freed. Note that it's not
11139 necessary that those locations should be removed from inferior --
11140 if there's another location at the same address (previously
11141 marked as duplicate), we don't need to remove/insert the
11142 location.
876fa593 11143
4a64f543
MS
11144 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11145 and former bp_location array state respectively. */
876fa593 11146
5d51cd5d
SM
11147 size_t loc_i = 0;
11148 for (bp_location *old_loc : old_locations)
74960c60 11149 {
e5dd4106 11150 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11151 not, we have to free it. */
d8de7963 11152 bool found_object = false;
20874c92 11153 /* Tells if the location should remain inserted in the target. */
d8de7963
AB
11154 bool keep_in_target = false;
11155 bool removed = false;
876fa593 11156
4a64f543
MS
11157 /* Skip LOCP entries which will definitely never be needed.
11158 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11159 while (loc_i < bp_locations.size ()
11160 && bp_locations[loc_i]->address < old_loc->address)
11161 loc_i++;
c7d46a38 11162
5d51cd5d
SM
11163 for (size_t loc2_i = loc_i;
11164 (loc2_i < bp_locations.size ()
11165 && bp_locations[loc2_i]->address == old_loc->address);
11166 loc2_i++)
c7d46a38 11167 {
b775012e
LM
11168 /* Check if this is a new/duplicated location or a duplicated
11169 location that had its condition modified. If so, we want to send
11170 its condition to the target if evaluation of conditions is taking
11171 place there. */
5d51cd5d 11172 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11173 && (last_addr != old_loc->address
11174 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11175 {
5d51cd5d 11176 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11177 last_pspace_num = old_loc->pspace->num;
c7d46a38 11178 }
b775012e 11179
5d51cd5d 11180 if (bp_locations[loc2_i] == old_loc)
d8de7963 11181 found_object = true;
c7d46a38 11182 }
74960c60 11183
b775012e
LM
11184 /* We have already handled this address, update it so that we don't
11185 have to go through updates again. */
11186 last_addr = old_loc->address;
11187
11188 /* Target-side condition evaluation: Handle deleted locations. */
11189 if (!found_object)
11190 force_breakpoint_reinsertion (old_loc);
11191
4a64f543
MS
11192 /* If this location is no longer present, and inserted, look if
11193 there's maybe a new location at the same address. If so,
11194 mark that one inserted, and don't remove this one. This is
11195 needed so that we don't have a time window where a breakpoint
11196 at certain location is not inserted. */
74960c60 11197
876fa593 11198 if (old_loc->inserted)
0d381245 11199 {
4a64f543
MS
11200 /* If the location is inserted now, we might have to remove
11201 it. */
74960c60 11202
876fa593 11203 if (found_object && should_be_inserted (old_loc))
74960c60 11204 {
4a64f543
MS
11205 /* The location is still present in the location list,
11206 and still should be inserted. Don't do anything. */
d8de7963 11207 keep_in_target = true;
74960c60
VP
11208 }
11209 else
11210 {
b775012e
LM
11211 /* This location still exists, but it won't be kept in the
11212 target since it may have been disabled. We proceed to
11213 remove its target-side condition. */
11214
4a64f543
MS
11215 /* The location is either no longer present, or got
11216 disabled. See if there's another location at the
11217 same address, in which case we don't need to remove
11218 this one from the target. */
876fa593 11219
2bdf28a0 11220 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11221 if (bl_address_is_meaningful (old_loc))
876fa593 11222 {
5d51cd5d
SM
11223 for (size_t loc2_i = loc_i;
11224 (loc2_i < bp_locations.size ()
11225 && bp_locations[loc2_i]->address == old_loc->address);
11226 loc2_i++)
876fa593 11227 {
5d51cd5d 11228 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11229
7f32a4d5
PA
11230 if (loc2 == old_loc)
11231 continue;
11232
2d134ed3 11233 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11234 {
85d721b8
PA
11235 /* Read watchpoint locations are switched to
11236 access watchpoints, if the former are not
11237 supported, but the latter are. */
11238 if (is_hardware_watchpoint (old_loc->owner))
11239 {
11240 gdb_assert (is_hardware_watchpoint (loc2->owner));
11241 loc2->watchpoint_type = old_loc->watchpoint_type;
11242 }
11243
934709f0
PW
11244 /* loc2 is a duplicated location. We need to check
11245 if it should be inserted in case it will be
11246 unduplicated. */
7f32a4d5 11247 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11248 {
934709f0 11249 swap_insertion (old_loc, loc2);
d8de7963 11250 keep_in_target = true;
c7d46a38
PA
11251 break;
11252 }
876fa593
JK
11253 }
11254 }
11255 }
74960c60
VP
11256 }
11257
20874c92
VP
11258 if (!keep_in_target)
11259 {
834c0d03 11260 if (remove_breakpoint (old_loc))
20874c92 11261 {
4a64f543
MS
11262 /* This is just about all we can do. We could keep
11263 this location on the global list, and try to
11264 remove it next time, but there's no particular
11265 reason why we will succeed next time.
20874c92 11266
4a64f543
MS
11267 Note that at this point, old_loc->owner is still
11268 valid, as delete_breakpoint frees the breakpoint
11269 only after calling us. */
6cb06a8c
TT
11270 gdb_printf (_("warning: Error removing "
11271 "breakpoint %d\n"),
11272 old_loc->owner->number);
20874c92 11273 }
d8de7963 11274 removed = true;
20874c92 11275 }
0d381245 11276 }
74960c60
VP
11277
11278 if (!found_object)
1c5cfe86 11279 {
fbea99ea 11280 if (removed && target_is_non_stop_p ()
1cf4d951 11281 && need_moribund_for_location_type (old_loc))
20874c92 11282 {
db82e815
PA
11283 /* This location was removed from the target. In
11284 non-stop mode, a race condition is possible where
11285 we've removed a breakpoint, but stop events for that
11286 breakpoint are already queued and will arrive later.
11287 We apply an heuristic to be able to distinguish such
11288 SIGTRAPs from other random SIGTRAPs: we keep this
11289 breakpoint location for a bit, and will retire it
11290 after we see some number of events. The theory here
11291 is that reporting of events should, "on the average",
11292 be fair, so after a while we'll see events from all
11293 threads that have anything of interest, and no longer
11294 need to keep this breakpoint location around. We
11295 don't hold locations forever so to reduce chances of
11296 mistaking a non-breakpoint SIGTRAP for a breakpoint
11297 SIGTRAP.
11298
11299 The heuristic failing can be disastrous on
11300 decr_pc_after_break targets.
11301
11302 On decr_pc_after_break targets, like e.g., x86-linux,
11303 if we fail to recognize a late breakpoint SIGTRAP,
11304 because events_till_retirement has reached 0 too
11305 soon, we'll fail to do the PC adjustment, and report
11306 a random SIGTRAP to the user. When the user resumes
11307 the inferior, it will most likely immediately crash
2dec564e 11308 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11309 corrupted, because of being resumed e.g., in the
11310 middle of a multi-byte instruction, or skipped a
11311 one-byte instruction. This was actually seen happen
11312 on native x86-linux, and should be less rare on
11313 targets that do not support new thread events, like
11314 remote, due to the heuristic depending on
11315 thread_count.
11316
11317 Mistaking a random SIGTRAP for a breakpoint trap
11318 causes similar symptoms (PC adjustment applied when
11319 it shouldn't), but then again, playing with SIGTRAPs
11320 behind the debugger's back is asking for trouble.
11321
11322 Since hardware watchpoint traps are always
11323 distinguishable from other traps, so we don't need to
11324 apply keep hardware watchpoint moribund locations
11325 around. We simply always ignore hardware watchpoint
11326 traps we can no longer explain. */
11327
5b6d1e4f
PA
11328 process_stratum_target *proc_target = nullptr;
11329 for (inferior *inf : all_inferiors ())
11330 if (inf->pspace == old_loc->pspace)
11331 {
11332 proc_target = inf->process_target ();
11333 break;
11334 }
11335 if (proc_target != nullptr)
11336 old_loc->events_till_retirement
11337 = 3 * (thread_count (proc_target) + 1);
11338 else
11339 old_loc->events_till_retirement = 1;
876fa593 11340 old_loc->owner = NULL;
20874c92 11341
1123588c 11342 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11343 }
11344 else
f431efe5
PA
11345 {
11346 old_loc->owner = NULL;
11347 decref_bp_location (&old_loc);
11348 }
20874c92 11349 }
74960c60 11350 }
1c5cfe86 11351
348d480f
PA
11352 /* Rescan breakpoints at the same address and section, marking the
11353 first one as "first" and any others as "duplicates". This is so
11354 that the bpt instruction is only inserted once. If we have a
11355 permanent breakpoint at the same place as BPT, make that one the
11356 official one, and the rest as duplicates. Permanent breakpoints
11357 are sorted first for the same address.
11358
11359 Do the same for hardware watchpoints, but also considering the
11360 watchpoint's type (regular/access/read) and length. */
11361
11362 bp_loc_first = NULL;
11363 wp_loc_first = NULL;
11364 awp_loc_first = NULL;
11365 rwp_loc_first = NULL;
40cb8ca5 11366
48d7020b 11367 for (bp_location *loc : all_bp_locations ())
348d480f
PA
11368 {
11369 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11370 non-NULL. */
348d480f 11371 struct bp_location **loc_first_p;
43892fdf 11372 breakpoint *b = loc->owner;
348d480f 11373
6f380991 11374 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11375 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11376 /* Don't detect duplicate for tracepoint locations because they are
11377 never duplicated. See the comments in field `duplicate' of
11378 `struct bp_location'. */
348d480f 11379 || is_tracepoint (b))
b775012e
LM
11380 {
11381 /* Clear the condition modification flag. */
11382 loc->condition_changed = condition_unchanged;
11383 continue;
11384 }
348d480f 11385
348d480f
PA
11386 if (b->type == bp_hardware_watchpoint)
11387 loc_first_p = &wp_loc_first;
11388 else if (b->type == bp_read_watchpoint)
11389 loc_first_p = &rwp_loc_first;
11390 else if (b->type == bp_access_watchpoint)
11391 loc_first_p = &awp_loc_first;
11392 else
11393 loc_first_p = &bp_loc_first;
11394
11395 if (*loc_first_p == NULL
11396 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11397 || !breakpoint_locations_match (loc, *loc_first_p))
11398 {
11399 *loc_first_p = loc;
11400 loc->duplicate = 0;
b775012e
LM
11401
11402 if (is_breakpoint (loc->owner) && loc->condition_changed)
11403 {
11404 loc->needs_update = 1;
11405 /* Clear the condition modification flag. */
11406 loc->condition_changed = condition_unchanged;
11407 }
348d480f
PA
11408 continue;
11409 }
11410
934709f0
PW
11411
11412 /* This and the above ensure the invariant that the first location
11413 is not duplicated, and is the inserted one.
11414 All following are marked as duplicated, and are not inserted. */
11415 if (loc->inserted)
11416 swap_insertion (loc, *loc_first_p);
348d480f
PA
11417 loc->duplicate = 1;
11418
b775012e
LM
11419 /* Clear the condition modification flag. */
11420 loc->condition_changed = condition_unchanged;
348d480f
PA
11421 }
11422
a25a5a45 11423 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11424 {
04086b45 11425 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11426 insert_breakpoint_locations ();
11427 else
11428 {
44702360
PA
11429 /* Even though the caller told us to not insert new
11430 locations, we may still need to update conditions on the
11431 target's side of breakpoints that were already inserted
11432 if the target is evaluating breakpoint conditions. We
b775012e
LM
11433 only update conditions for locations that are marked
11434 "needs_update". */
11435 update_inserted_breakpoint_locations ();
11436 }
11437 }
348d480f 11438
04086b45 11439 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 11440 download_tracepoint_locations ();
348d480f
PA
11441}
11442
11443void
11444breakpoint_retire_moribund (void)
11445{
1123588c
TT
11446 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11447 {
11448 struct bp_location *loc = moribund_locations[ix];
11449 if (--(loc->events_till_retirement) == 0)
11450 {
11451 decref_bp_location (&loc);
11452 unordered_remove (moribund_locations, ix);
11453 --ix;
11454 }
11455 }
348d480f
PA
11456}
11457
11458static void
44702360 11459update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 11460{
348d480f 11461
a70b8144 11462 try
492d29ea
PA
11463 {
11464 update_global_location_list (insert_mode);
11465 }
230d2906 11466 catch (const gdb_exception_error &e)
492d29ea
PA
11467 {
11468 }
348d480f
PA
11469}
11470
11471/* Clear BKP from a BPS. */
11472
11473static void
313f3b21 11474bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
348d480f 11475{
313f3b21 11476 bpstat *bs;
348d480f
PA
11477
11478 for (bs = bps; bs; bs = bs->next)
11479 if (bs->breakpoint_at == bpt)
11480 {
11481 bs->breakpoint_at = NULL;
11482 bs->old_val = NULL;
11483 /* bs->commands will be freed later. */
11484 }
11485}
11486
11487/* Callback for iterate_over_threads. */
11488static int
11489bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11490{
9a3c8263 11491 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
11492
11493 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11494 return 0;
11495}
11496
c47614fe 11497/* Helper for breakpoint and tracepoint breakpoint->mention
348d480f
PA
11498 callbacks. */
11499
11500static void
b713485d 11501say_where (const breakpoint *b)
348d480f
PA
11502{
11503 struct value_print_options opts;
11504
11505 get_user_print_options (&opts);
11506
11507 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11508 single string. */
11509 if (b->loc == NULL)
11510 {
f00aae0f
KS
11511 /* For pending locations, the output differs slightly based
11512 on b->extra_string. If this is non-NULL, it contains either
11513 a condition or dprintf arguments. */
11514 if (b->extra_string == NULL)
11515 {
709438c7 11516 gdb_printf (_(" (%s) pending."), b->locspec->to_string ());
f00aae0f
KS
11517 }
11518 else if (b->type == bp_dprintf)
11519 {
6cb06a8c 11520 gdb_printf (_(" (%s,%s) pending."),
709438c7 11521 b->locspec->to_string (),
6cb06a8c 11522 b->extra_string.get ());
f00aae0f
KS
11523 }
11524 else
11525 {
6cb06a8c 11526 gdb_printf (_(" (%s %s) pending."),
709438c7 11527 b->locspec->to_string (),
6cb06a8c 11528 b->extra_string.get ());
f00aae0f 11529 }
348d480f
PA
11530 }
11531 else
11532 {
2f202fde 11533 if (opts.addressprint || b->loc->symtab == NULL)
6cb06a8c
TT
11534 gdb_printf (" at %ps",
11535 styled_string (address_style.style (),
11536 paddress (b->loc->gdbarch,
11537 b->loc->address)));
2f202fde 11538 if (b->loc->symtab != NULL)
f8eba3c6
TT
11539 {
11540 /* If there is a single location, we can print the location
11541 more nicely. */
11542 if (b->loc->next == NULL)
0bb296cb 11543 {
6a831f06
PA
11544 const char *filename
11545 = symtab_to_filename_for_display (b->loc->symtab);
6cb06a8c
TT
11546 gdb_printf (": file %ps, line %d.",
11547 styled_string (file_name_style.style (),
11548 filename),
11549 b->loc->line_number);
0bb296cb 11550 }
f8eba3c6
TT
11551 else
11552 /* This is not ideal, but each location may have a
11553 different file name, and this at least reflects the
11554 real situation somewhat. */
709438c7 11555 gdb_printf (": %s.", b->locspec->to_string ());
f8eba3c6 11556 }
348d480f
PA
11557
11558 if (b->loc->next)
11559 {
11560 struct bp_location *loc = b->loc;
11561 int n = 0;
11562 for (; loc; loc = loc->next)
11563 ++n;
6cb06a8c 11564 gdb_printf (" (%d locations)", n);
348d480f
PA
11565 }
11566 }
11567}
11568
40cb8ca5
SM
11569/* See breakpoint.h. */
11570
a67bcaba 11571bp_location_range breakpoint::locations () const
40cb8ca5 11572{
9be25986 11573 return bp_location_range (this->loc);
40cb8ca5
SM
11574}
11575
4c6a92b1
TT
11576struct bp_location *
11577breakpoint::allocate_location ()
11578{
11579 return new bp_location (this);
11580}
11581
2060206e
PA
11582#define internal_error_pure_virtual_called() \
11583 gdb_assert_not_reached ("pure virtual function called")
11584
4c6a92b1
TT
11585int
11586breakpoint::insert_location (struct bp_location *bl)
11587{
11588 internal_error_pure_virtual_called ();
11589}
11590
4c6a92b1
TT
11591int
11592breakpoint::remove_location (struct bp_location *bl,
11593 enum remove_bp_reason reason)
11594{
11595 internal_error_pure_virtual_called ();
11596}
11597
4c6a92b1
TT
11598int
11599breakpoint::breakpoint_hit (const struct bp_location *bl,
11600 const address_space *aspace,
11601 CORE_ADDR bp_addr,
11602 const target_waitstatus &ws)
11603{
11604 internal_error_pure_virtual_called ();
11605}
11606
4c6a92b1
TT
11607int
11608breakpoint::resources_needed (const struct bp_location *bl)
11609{
11610 internal_error_pure_virtual_called ();
11611}
11612
4c6a92b1 11613enum print_stop_action
7bd86313 11614breakpoint::print_it (const bpstat *bs) const
4c6a92b1
TT
11615{
11616 internal_error_pure_virtual_called ();
11617}
11618
4c6a92b1 11619void
b713485d 11620breakpoint::print_mention () const
4c6a92b1
TT
11621{
11622 internal_error_pure_virtual_called ();
11623}
11624
4c6a92b1 11625void
4d1ae558 11626breakpoint::print_recreate (struct ui_file *fp) const
4c6a92b1
TT
11627{
11628 internal_error_pure_virtual_called ();
11629}
11630
2060206e
PA
11631/* Default breakpoint_ops methods. */
11632
2b5ab5b8 11633void
74421c0b 11634code_breakpoint::re_set ()
348d480f 11635{
06edf0c0 11636 /* FIXME: is this still reachable? */
264f9890 11637 if (breakpoint_location_spec_empty_p (this))
06edf0c0 11638 {
f00aae0f 11639 /* Anything without a location can't be re-set. */
2b5ab5b8 11640 delete_breakpoint (this);
06edf0c0 11641 return;
348d480f 11642 }
06edf0c0 11643
b3d5660a 11644 re_set_default ();
348d480f
PA
11645}
11646
2b5ab5b8 11647int
74421c0b 11648code_breakpoint::insert_location (struct bp_location *bl)
348d480f 11649{
cd6c3b4f
YQ
11650 CORE_ADDR addr = bl->target_info.reqstd_address;
11651
579c6ad9 11652 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
11653 bl->target_info.placed_address = addr;
11654
5d926615 11655 int result;
348d480f 11656 if (bl->loc_type == bp_loc_hardware_breakpoint)
5d926615 11657 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 11658 else
5d926615
TT
11659 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11660
11661 if (result == 0 && bl->probe.prob != nullptr)
11662 {
11663 /* The insertion was successful, now let's set the probe's semaphore
11664 if needed. */
11665 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11666 }
11667
11668 return result;
348d480f
PA
11669}
11670
2b5ab5b8 11671int
74421c0b 11672code_breakpoint::remove_location (struct bp_location *bl,
2b5ab5b8 11673 enum remove_bp_reason reason)
348d480f 11674{
5d926615
TT
11675 if (bl->probe.prob != nullptr)
11676 {
11677 /* Let's clear the semaphore before removing the location. */
11678 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11679 }
11680
348d480f
PA
11681 if (bl->loc_type == bp_loc_hardware_breakpoint)
11682 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11683 else
73971819 11684 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
11685}
11686
2b5ab5b8 11687int
74421c0b 11688code_breakpoint::breakpoint_hit (const struct bp_location *bl,
2b5ab5b8
TT
11689 const address_space *aspace,
11690 CORE_ADDR bp_addr,
11691 const target_waitstatus &ws)
348d480f 11692{
c272a98c
SM
11693 if (ws.kind () != TARGET_WAITKIND_STOPPED
11694 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
11695 return 0;
11696
348d480f
PA
11697 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11698 aspace, bp_addr))
11699 return 0;
11700
11701 if (overlay_debugging /* unmapped overlay section */
11702 && section_is_overlay (bl->section)
11703 && !section_is_mapped (bl->section))
11704 return 0;
11705
11706 return 1;
11707}
11708
f293a0b5
TT
11709int
11710dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11711 const address_space *aspace,
11712 CORE_ADDR bp_addr,
11713 const target_waitstatus &ws)
cd1608cc
PA
11714{
11715 if (dprintf_style == dprintf_style_agent
11716 && target_can_run_breakpoint_commands ())
11717 {
11718 /* An agent-style dprintf never causes a stop. If we see a trap
11719 for this address it must be for a breakpoint that happens to
11720 be set at the same address. */
11721 return 0;
11722 }
11723
f293a0b5 11724 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
cd1608cc
PA
11725}
11726
7dd8e7ae
TT
11727int
11728ordinary_breakpoint::resources_needed (const struct bp_location *bl)
348d480f 11729{
7dd8e7ae 11730 gdb_assert (type == bp_hardware_breakpoint);
348d480f
PA
11731
11732 return 1;
11733}
11734
7dd8e7ae 11735enum print_stop_action
7bd86313 11736ordinary_breakpoint::print_it (const bpstat *bs) const
348d480f 11737{
348d480f 11738 const struct bp_location *bl;
001c8c33 11739 int bp_temp;
79a45e25 11740 struct ui_out *uiout = current_uiout;
348d480f 11741
b6433ede 11742 bl = bs->bp_location_at.get ();
348d480f 11743
7dd8e7ae 11744 bp_temp = disposition == disp_del;
001c8c33
PA
11745 if (bl->address != bl->requested_address)
11746 breakpoint_adjustment_warning (bl->requested_address,
11747 bl->address,
d8de7963 11748 number, true);
7dd8e7ae 11749 annotate_breakpoint (number);
f303dbd6
PA
11750 maybe_print_thread_hit_breakpoint (uiout);
11751
112e8700 11752 if (uiout->is_mi_like_p ())
348d480f 11753 {
112e8700 11754 uiout->field_string ("reason",
001c8c33 11755 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7dd8e7ae 11756 uiout->field_string ("disp", bpdisp_text (disposition));
06edf0c0 11757 }
78805ff8 11758
6a831f06 11759 if (bp_temp)
78805ff8 11760 uiout->text ("Temporary breakpoint ");
6a831f06 11761 else
78805ff8
PW
11762 uiout->text ("Breakpoint ");
11763 print_num_locno (bs, uiout);
11764 uiout->text (", ");
06edf0c0 11765
001c8c33 11766 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11767}
11768
7dd8e7ae 11769void
b713485d 11770ordinary_breakpoint::print_mention () const
06edf0c0 11771{
112e8700 11772 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
11773 return;
11774
7dd8e7ae 11775 switch (type)
06edf0c0
PA
11776 {
11777 case bp_breakpoint:
11778 case bp_gnu_ifunc_resolver:
7dd8e7ae 11779 if (disposition == disp_del)
6cb06a8c 11780 gdb_printf (_("Temporary breakpoint"));
06edf0c0 11781 else
6cb06a8c 11782 gdb_printf (_("Breakpoint"));
7dd8e7ae
TT
11783 gdb_printf (_(" %d"), number);
11784 if (type == bp_gnu_ifunc_resolver)
6cb06a8c 11785 gdb_printf (_(" at gnu-indirect-function resolver"));
06edf0c0
PA
11786 break;
11787 case bp_hardware_breakpoint:
7dd8e7ae 11788 gdb_printf (_("Hardware assisted breakpoint %d"), number);
06edf0c0 11789 break;
e7e0cddf 11790 case bp_dprintf:
7dd8e7ae 11791 gdb_printf (_("Dprintf %d"), number);
e7e0cddf 11792 break;
06edf0c0
PA
11793 }
11794
7dd8e7ae 11795 say_where (this);
06edf0c0
PA
11796}
11797
7dd8e7ae 11798void
4d1ae558 11799ordinary_breakpoint::print_recreate (struct ui_file *fp) const
06edf0c0 11800{
7dd8e7ae 11801 if (type == bp_breakpoint && disposition == disp_del)
6cb06a8c 11802 gdb_printf (fp, "tbreak");
7dd8e7ae 11803 else if (type == bp_breakpoint)
6cb06a8c 11804 gdb_printf (fp, "break");
7dd8e7ae
TT
11805 else if (type == bp_hardware_breakpoint
11806 && disposition == disp_del)
6cb06a8c 11807 gdb_printf (fp, "thbreak");
7dd8e7ae 11808 else if (type == bp_hardware_breakpoint)
6cb06a8c 11809 gdb_printf (fp, "hbreak");
06edf0c0 11810 else
f34652de 11811 internal_error (_("unhandled breakpoint type %d"), (int) type);
06edf0c0 11812
709438c7 11813 gdb_printf (fp, " %s", locspec->to_string ());
f00aae0f
KS
11814
11815 /* Print out extra_string if this breakpoint is pending. It might
11816 contain, for example, conditions that were set by the user. */
7dd8e7ae
TT
11817 if (loc == NULL && extra_string != NULL)
11818 gdb_printf (fp, " %s", extra_string.get ());
f00aae0f 11819
04d0163c 11820 print_recreate_thread (fp);
06edf0c0
PA
11821}
11822
2b5ab5b8 11823std::vector<symtab_and_line>
264f9890
PA
11824code_breakpoint::decode_location_spec (location_spec *locspec,
11825 program_space *search_pspace)
983af33b 11826{
7464aeaa 11827 if (locspec->type () == PROBE_LOCATION_SPEC)
264f9890 11828 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
5d926615 11829
8f5bc641
TT
11830 struct linespec_result canonical;
11831
11832 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
11833 NULL, 0, &canonical, multiple_symbols_all,
11834 filter.get ());
11835
11836 /* We should get 0 or 1 resulting SALs. */
11837 gdb_assert (canonical.lsals.size () < 2);
11838
11839 if (!canonical.lsals.empty ())
11840 {
11841 const linespec_sals &lsal = canonical.lsals[0];
11842 return std::move (lsal.sals);
11843 }
11844 return {};
983af33b
SDJ
11845}
11846
06edf0c0
PA
11847/* Virtual table for internal breakpoints. */
11848
c359fff5
TT
11849void
11850internal_breakpoint::re_set ()
06edf0c0 11851{
c359fff5 11852 switch (type)
06edf0c0
PA
11853 {
11854 /* Delete overlay event and longjmp master breakpoints; they
11855 will be reset later by breakpoint_re_set. */
11856 case bp_overlay_event:
11857 case bp_longjmp_master:
11858 case bp_std_terminate_master:
11859 case bp_exception_master:
c359fff5 11860 delete_breakpoint (this);
06edf0c0
PA
11861 break;
11862
11863 /* This breakpoint is special, it's set up when the inferior
dda83cd7 11864 starts and we really don't want to touch it. */
06edf0c0
PA
11865 case bp_shlib_event:
11866
11867 /* Like bp_shlib_event, this breakpoint type is special. Once
11868 it is set up, we do not want to touch it. */
11869 case bp_thread_event:
11870 break;
11871 }
11872}
11873
c359fff5
TT
11874void
11875internal_breakpoint::check_status (bpstat *bs)
06edf0c0 11876{
c359fff5 11877 if (type == bp_shlib_event)
a9b3a50f
PA
11878 {
11879 /* If requested, stop when the dynamic linker notifies GDB of
11880 events. This allows the user to get control and place
11881 breakpoints in initializer routines for dynamically loaded
11882 objects (among other things). */
11883 bs->stop = stop_on_solib_events;
11884 bs->print = stop_on_solib_events;
11885 }
11886 else
11887 bs->stop = 0;
06edf0c0
PA
11888}
11889
c359fff5 11890enum print_stop_action
7bd86313 11891internal_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11892{
c359fff5 11893 switch (type)
06edf0c0 11894 {
348d480f
PA
11895 case bp_shlib_event:
11896 /* Did we stop because the user set the stop_on_solib_events
11897 variable? (If so, we report this as a generic, "Stopped due
11898 to shlib event" message.) */
4e9e993a 11899 print_solib_event (false);
348d480f
PA
11900 break;
11901
11902 case bp_thread_event:
11903 /* Not sure how we will get here.
11904 GDB should not stop for these breakpoints. */
6cb06a8c 11905 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11906 break;
11907
11908 case bp_overlay_event:
11909 /* By analogy with the thread event, GDB should not stop for these. */
6cb06a8c 11910 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11911 break;
11912
11913 case bp_longjmp_master:
11914 /* These should never be enabled. */
6cb06a8c 11915 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11916 break;
11917
11918 case bp_std_terminate_master:
11919 /* These should never be enabled. */
6cb06a8c
TT
11920 gdb_printf (_("std::terminate Master Breakpoint: "
11921 "gdb should not stop!\n"));
348d480f
PA
11922 break;
11923
11924 case bp_exception_master:
11925 /* These should never be enabled. */
6cb06a8c
TT
11926 gdb_printf (_("Exception Master Breakpoint: "
11927 "gdb should not stop!\n"));
06edf0c0
PA
11928 break;
11929 }
11930
001c8c33 11931 return PRINT_NOTHING;
06edf0c0
PA
11932}
11933
c359fff5 11934void
b713485d 11935internal_breakpoint::print_mention () const
06edf0c0
PA
11936{
11937 /* Nothing to mention. These breakpoints are internal. */
11938}
11939
06edf0c0
PA
11940/* Virtual table for momentary breakpoints */
11941
1fd30a47
TT
11942void
11943momentary_breakpoint::re_set ()
06edf0c0
PA
11944{
11945 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 11946 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
11947 Otherwise these should have been blown away via the cleanup chain
11948 or by breakpoint_init_inferior when we rerun the executable. */
11949}
11950
1fd30a47
TT
11951void
11952momentary_breakpoint::check_status (bpstat *bs)
06edf0c0
PA
11953{
11954 /* Nothing. The point of these breakpoints is causing a stop. */
11955}
11956
1fd30a47 11957enum print_stop_action
7bd86313 11958momentary_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11959{
001c8c33 11960 return PRINT_UNKNOWN;
348d480f
PA
11961}
11962
1fd30a47 11963void
b713485d 11964momentary_breakpoint::print_mention () const
348d480f 11965{
06edf0c0 11966 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11967}
11968
e2e4d78b
JK
11969/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11970
11971 It gets cleared already on the removal of the first one of such placed
11972 breakpoints. This is OK as they get all removed altogether. */
11973
c1fc2657 11974longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 11975{
c1fc2657 11976 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 11977
c1fc2657 11978 if (tp != NULL)
e2e4d78b 11979 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
11980}
11981
55aa24fb 11982static void
264f9890
PA
11983bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
11984 struct linespec_result *canonical)
03ada39e 11985
55aa24fb
SDJ
11986{
11987 struct linespec_sals lsal;
11988
264f9890 11989 lsal.sals = parse_probes (locspec, NULL, canonical);
709438c7 11990 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 11991 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
11992}
11993
6c5b2ebe 11994static std::vector<symtab_and_line>
264f9890
PA
11995bkpt_probe_decode_location_spec (struct breakpoint *b,
11996 location_spec *locspec,
11997 program_space *search_pspace)
55aa24fb 11998{
264f9890
PA
11999 std::vector<symtab_and_line> sals
12000 = parse_probes (locspec, search_pspace, NULL);
6c5b2ebe 12001 if (sals.empty ())
55aa24fb 12002 error (_("probe not found"));
6c5b2ebe 12003 return sals;
55aa24fb
SDJ
12004}
12005
779dcceb
TT
12006int
12007tracepoint::breakpoint_hit (const struct bp_location *bl,
12008 const address_space *aspace, CORE_ADDR bp_addr,
12009 const target_waitstatus &ws)
348d480f
PA
12010{
12011 /* By definition, the inferior does not report stops at
12012 tracepoints. */
12013 return 0;
74960c60
VP
12014}
12015
779dcceb
TT
12016void
12017tracepoint::print_one_detail (struct ui_out *uiout) const
74960c60 12018{
779dcceb 12019 if (!static_trace_marker_id.empty ())
348d480f 12020 {
7b572efb
TT
12021 gdb_assert (type == bp_static_tracepoint
12022 || type == bp_static_marker_tracepoint);
cc59ec59 12023
6a831f06
PA
12024 uiout->message ("\tmarker id is %pF\n",
12025 string_field ("static-tracepoint-marker-string-id",
779dcceb 12026 static_trace_marker_id.c_str ()));
348d480f 12027 }
0d381245
VP
12028}
12029
779dcceb 12030void
b713485d 12031tracepoint::print_mention () const
a474d7c2 12032{
112e8700 12033 if (current_uiout->is_mi_like_p ())
348d480f 12034 return;
cc59ec59 12035
779dcceb 12036 switch (type)
348d480f
PA
12037 {
12038 case bp_tracepoint:
6cb06a8c 12039 gdb_printf (_("Tracepoint"));
779dcceb 12040 gdb_printf (_(" %d"), number);
348d480f
PA
12041 break;
12042 case bp_fast_tracepoint:
6cb06a8c 12043 gdb_printf (_("Fast tracepoint"));
779dcceb 12044 gdb_printf (_(" %d"), number);
348d480f
PA
12045 break;
12046 case bp_static_tracepoint:
7b572efb 12047 case bp_static_marker_tracepoint:
6cb06a8c 12048 gdb_printf (_("Static tracepoint"));
779dcceb 12049 gdb_printf (_(" %d"), number);
348d480f
PA
12050 break;
12051 default:
f34652de 12052 internal_error (_("unhandled tracepoint type %d"), (int) type);
348d480f
PA
12053 }
12054
779dcceb 12055 say_where (this);
a474d7c2
PA
12056}
12057
779dcceb 12058void
4d1ae558 12059tracepoint::print_recreate (struct ui_file *fp) const
a474d7c2 12060{
779dcceb 12061 if (type == bp_fast_tracepoint)
6cb06a8c 12062 gdb_printf (fp, "ftrace");
7b572efb
TT
12063 else if (type == bp_static_tracepoint
12064 || type == bp_static_marker_tracepoint)
6cb06a8c 12065 gdb_printf (fp, "strace");
779dcceb 12066 else if (type == bp_tracepoint)
6cb06a8c 12067 gdb_printf (fp, "trace");
348d480f 12068 else
f34652de 12069 internal_error (_("unhandled tracepoint type %d"), (int) type);
cc59ec59 12070
709438c7 12071 gdb_printf (fp, " %s", locspec->to_string ());
04d0163c 12072 print_recreate_thread (fp);
d9b3f62e 12073
779dcceb
TT
12074 if (pass_count)
12075 gdb_printf (fp, " passcount %d\n", pass_count);
a474d7c2
PA
12076}
12077
bac7c5cf 12078/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12079
12080static void
264f9890
PA
12081tracepoint_probe_create_sals_from_location_spec
12082 (location_spec *locspec,
03ada39e 12083 struct linespec_result *canonical)
55aa24fb
SDJ
12084{
12085 /* We use the same method for breakpoint on probes. */
264f9890 12086 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
55aa24fb
SDJ
12087}
12088
f293a0b5
TT
12089void
12090dprintf_breakpoint::re_set ()
5c2b4418 12091{
b3d5660a 12092 re_set_default ();
5c2b4418 12093
f00aae0f 12094 /* extra_string should never be non-NULL for dprintf. */
f293a0b5 12095 gdb_assert (extra_string != NULL);
5c2b4418
HZ
12096
12097 /* 1 - connect to target 1, that can run breakpoint commands.
12098 2 - create a dprintf, which resolves fine.
12099 3 - disconnect from target 1
12100 4 - connect to target 2, that can NOT run breakpoint commands.
12101
12102 After steps #3/#4, you'll want the dprintf command list to
12103 be updated, because target 1 and 2 may well return different
12104 answers for target_can_run_breakpoint_commands().
12105 Given absence of finer grained resetting, we get to do
12106 it all the time. */
f293a0b5
TT
12107 if (extra_string != NULL)
12108 update_dprintf_command_list (this);
5c2b4418
HZ
12109}
12110
f293a0b5 12111/* Implement the "print_recreate" method for dprintf. */
2d9442cc 12112
f293a0b5 12113void
4d1ae558 12114dprintf_breakpoint::print_recreate (struct ui_file *fp) const
2d9442cc 12115{
709438c7 12116 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
04d0163c 12117 print_recreate_thread (fp);
2d9442cc
HZ
12118}
12119
f293a0b5 12120/* Implement the "after_condition_true" method for dprintf.
9d6e6e84
HZ
12121
12122 dprintf's are implemented with regular commands in their command
12123 list, but we run the commands here instead of before presenting the
12124 stop to the user, as dprintf's don't actually cause a stop. This
12125 also makes it so that the commands of multiple dprintfs at the same
12126 address are all handled. */
12127
f293a0b5
TT
12128void
12129dprintf_breakpoint::after_condition_true (struct bpstat *bs)
9d6e6e84 12130{
9d6e6e84
HZ
12131 /* dprintf's never cause a stop. This wasn't set in the
12132 check_status hook instead because that would make the dprintf's
12133 condition not be evaluated. */
12134 bs->stop = 0;
12135
12136 /* Run the command list here. Take ownership of it instead of
12137 copying. We never want these commands to run later in
12138 bpstat_do_actions, if a breakpoint that causes a stop happens to
12139 be set at same address as this dprintf, or even if running the
12140 commands here throws. */
9c95aea1
KB
12141 counted_command_line cmds = std::move (bs->commands);
12142 gdb_assert (cmds != nullptr);
12143 execute_control_commands (cmds.get (), 0);
9d6e6e84
HZ
12144}
12145
983af33b
SDJ
12146/* The breakpoint_ops structure to be used on static tracepoints with
12147 markers (`-m'). */
12148
12149static void
264f9890
PA
12150strace_marker_create_sals_from_location_spec (location_spec *locspec,
12151 struct linespec_result *canonical)
983af33b
SDJ
12152{
12153 struct linespec_sals lsal;
f00aae0f 12154 const char *arg_start, *arg;
983af33b 12155
40d97ee2 12156 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 12157 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12158
f2fc3015
TT
12159 std::string str (arg_start, arg - arg_start);
12160 const char *ptr = str.c_str ();
264f9890
PA
12161 canonical->locspec
12162 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
983af33b 12163
709438c7 12164 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12165 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12166}
12167
12168static void
12169strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12170 struct linespec_result *canonical,
e1e01040
PA
12171 gdb::unique_xmalloc_ptr<char> cond_string,
12172 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12173 enum bptype type_wanted,
12174 enum bpdisp disposition,
12175 int thread,
12176 int task, int ignore_count,
983af33b 12177 int from_tty, int enabled,
44f238bb 12178 int internal, unsigned flags)
983af33b 12179{
6c5b2ebe 12180 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12181
12182 /* If the user is creating a static tracepoint by marker id
12183 (strace -m MARKER_ID), then store the sals index, so that
12184 breakpoint_re_set can try to match up which of the newly
12185 found markers corresponds to this one, and, don't try to
12186 expand multiple locations for each sal, given than SALS
12187 already should contain all sals for MARKER_ID. */
12188
6c5b2ebe 12189 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12190 {
5c1ddcb6 12191 location_spec_up locspec = canonical->locspec->clone ();
983af33b 12192
3b003a61
PA
12193 std::unique_ptr<tracepoint> tp
12194 (new tracepoint (gdbarch,
12195 type_wanted,
12196 lsal.sals[i],
264f9890 12197 std::move (locspec),
3b003a61
PA
12198 NULL,
12199 std::move (cond_string),
12200 std::move (extra_string),
12201 disposition,
12202 thread, task, ignore_count,
12203 from_tty, enabled, flags,
12204 canonical->special_display));
12205
983af33b
SDJ
12206 /* Given that its possible to have multiple markers with
12207 the same string id, if the user is creating a static
12208 tracepoint by marker id ("strace -m MARKER_ID"), then
12209 store the sals index, so that breakpoint_re_set can
12210 try to match up which of the newly found markers
12211 corresponds to this one */
12212 tp->static_trace_marker_id_idx = i;
12213
b270e6f9 12214 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12215 }
12216}
12217
6d7a8c56 12218std::vector<symtab_and_line>
264f9890
PA
12219static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12220 program_space *search_pspace)
983af33b 12221{
40d97ee2 12222 const char *s = as_linespec_location_spec (locspec)->spec_string;
983af33b 12223
6c5b2ebe 12224 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
6d7a8c56 12225 if (sals.size () > static_trace_marker_id_idx)
983af33b 12226 {
6d7a8c56 12227 sals[0] = sals[static_trace_marker_id_idx];
6c5b2ebe
PA
12228 sals.resize (1);
12229 return sals;
983af33b
SDJ
12230 }
12231 else
6d7a8c56 12232 error (_("marker %s not found"), static_trace_marker_id.c_str ());
983af33b
SDJ
12233}
12234
8613a476
TT
12235/* Static tracepoints with marker (`-m'). */
12236static struct breakpoint_ops strace_marker_breakpoint_ops =
12237{
264f9890 12238 strace_marker_create_sals_from_location_spec,
8613a476
TT
12239 strace_marker_create_breakpoints_sal,
12240};
983af33b 12241
d8de7963 12242static bool
983af33b
SDJ
12243strace_marker_p (struct breakpoint *b)
12244{
7b572efb 12245 return b->type == bp_static_marker_tracepoint;
983af33b
SDJ
12246}
12247
53a5351d 12248/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 12249 structures. */
c906108c
SS
12250
12251void
fba45db2 12252delete_breakpoint (struct breakpoint *bpt)
c906108c 12253{
8a3fe4f8 12254 gdb_assert (bpt != NULL);
c906108c 12255
4a64f543
MS
12256 /* Has this bp already been deleted? This can happen because
12257 multiple lists can hold pointers to bp's. bpstat lists are
12258 especial culprits.
12259
12260 One example of this happening is a watchpoint's scope bp. When
12261 the scope bp triggers, we notice that the watchpoint is out of
12262 scope, and delete it. We also delete its scope bp. But the
12263 scope bp is marked "auto-deleting", and is already on a bpstat.
12264 That bpstat is then checked for auto-deleting bp's, which are
12265 deleted.
12266
12267 A real solution to this problem might involve reference counts in
12268 bp's, and/or giving them pointers back to their referencing
12269 bpstat's, and teaching delete_breakpoint to only free a bp's
12270 storage when no more references were extent. A cheaper bandaid
12271 was chosen. */
c906108c
SS
12272 if (bpt->type == bp_none)
12273 return;
12274
4a64f543
MS
12275 /* At least avoid this stale reference until the reference counting
12276 of breakpoints gets resolved. */
d0fb5eae 12277 if (bpt->related_breakpoint != bpt)
e5a0a904 12278 {
d0fb5eae 12279 struct breakpoint *related;
3a5c3e22 12280 struct watchpoint *w;
d0fb5eae
JK
12281
12282 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 12283 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 12284 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
12285 w = (struct watchpoint *) bpt;
12286 else
12287 w = NULL;
12288 if (w != NULL)
12289 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
12290
12291 /* Unlink bpt from the bpt->related_breakpoint ring. */
12292 for (related = bpt; related->related_breakpoint != bpt;
12293 related = related->related_breakpoint);
12294 related->related_breakpoint = bpt->related_breakpoint;
12295 bpt->related_breakpoint = bpt;
e5a0a904
JK
12296 }
12297
a9634178
TJB
12298 /* watch_command_1 creates a watchpoint but only sets its number if
12299 update_watchpoint succeeds in creating its bp_locations. If there's
12300 a problem in that process, we'll be asked to delete the half-created
12301 watchpoint. In that case, don't announce the deletion. */
12302 if (bpt->number)
76727919 12303 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 12304
c906108c
SS
12305 if (breakpoint_chain == bpt)
12306 breakpoint_chain = bpt->next;
12307
43892fdf 12308 for (breakpoint *b : all_breakpoints ())
c906108c 12309 if (b->next == bpt)
01add95b
SM
12310 {
12311 b->next = bpt->next;
12312 break;
12313 }
c906108c 12314
f431efe5
PA
12315 /* Be sure no bpstat's are pointing at the breakpoint after it's
12316 been freed. */
12317 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 12318 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
12319 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12320 commands are associated with the bpstat; if we remove it here,
12321 then the later call to bpstat_do_actions (&stop_bpstat); in
12322 event-top.c won't do anything, and temporary breakpoints with
12323 commands won't work. */
12324
12325 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12326
4a64f543
MS
12327 /* Now that breakpoint is removed from breakpoint list, update the
12328 global location list. This will remove locations that used to
12329 belong to this breakpoint. Do this before freeing the breakpoint
12330 itself, since remove_breakpoint looks at location's owner. It
12331 might be better design to have location completely
12332 self-contained, but it's not the case now. */
44702360 12333 update_global_location_list (UGLL_DONT_INSERT);
74960c60 12334
4a64f543
MS
12335 /* On the chance that someone will soon try again to delete this
12336 same bp, we mark it as deleted before freeing its storage. */
c906108c 12337 bpt->type = bp_none;
4d01a485 12338 delete bpt;
c906108c
SS
12339}
12340
51be5b68
PA
12341/* Iterator function to call a user-provided callback function once
12342 for each of B and its related breakpoints. */
12343
12344static void
12345iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 12346 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
12347{
12348 struct breakpoint *related;
12349
12350 related = b;
12351 do
12352 {
12353 struct breakpoint *next;
12354
12355 /* FUNCTION may delete RELATED. */
12356 next = related->related_breakpoint;
12357
12358 if (next == related)
12359 {
12360 /* RELATED is the last ring entry. */
48649e1b 12361 function (related);
51be5b68
PA
12362
12363 /* FUNCTION may have deleted it, so we'd never reach back to
12364 B. There's nothing left to do anyway, so just break
12365 out. */
12366 break;
12367 }
12368 else
48649e1b 12369 function (related);
51be5b68
PA
12370
12371 related = next;
12372 }
12373 while (related != b);
12374}
95a42b64 12375
4495129a 12376static void
981a3fb3 12377delete_command (const char *arg, int from_tty)
c906108c 12378{
ea9365bb
TT
12379 dont_repeat ();
12380
c906108c
SS
12381 if (arg == 0)
12382 {
12383 int breaks_to_delete = 0;
12384
46c6471b 12385 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
12386 internal breakpoints, these have to be deleted with an
12387 explicit breakpoint number argument. */
43892fdf 12388 for (breakpoint *b : all_breakpoints ())
46c6471b 12389 if (user_breakpoint_p (b))
973d738b
DJ
12390 {
12391 breaks_to_delete = 1;
12392 break;
12393 }
c906108c
SS
12394
12395 /* Ask user only if there are some breakpoints to delete. */
12396 if (!from_tty
e2e0b3e5 12397 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
12398 for (breakpoint *b : all_breakpoints_safe ())
12399 if (user_breakpoint_p (b))
12400 delete_breakpoint (b);
c906108c
SS
12401 }
12402 else
48649e1b 12403 map_breakpoint_numbers
b926417a 12404 (arg, [&] (breakpoint *br)
48649e1b 12405 {
b926417a 12406 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 12407 });
c906108c
SS
12408}
12409
c2f4122d
PA
12410/* Return true if all locations of B bound to PSPACE are pending. If
12411 PSPACE is NULL, all locations of all program spaces are
12412 considered. */
12413
d8de7963 12414static bool
c2f4122d 12415all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 12416{
40cb8ca5 12417 for (bp_location *loc : b->locations ())
c2f4122d
PA
12418 if ((pspace == NULL
12419 || loc->pspace == pspace)
12420 && !loc->shlib_disabled
8645ff69 12421 && !loc->pspace->executing_startup)
d8de7963
AB
12422 return false;
12423 return true;
fe3f5fa8
VP
12424}
12425
776592bf 12426/* Subroutine of update_breakpoint_locations to simplify it.
d8de7963 12427 Return true if multiple fns in list LOC have the same name.
776592bf
DE
12428 Null names are ignored. */
12429
d8de7963 12430static bool
776592bf
DE
12431ambiguous_names_p (struct bp_location *loc)
12432{
12433 struct bp_location *l;
2698f5ea 12434 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 12435 xcalloc, xfree));
776592bf
DE
12436
12437 for (l = loc; l != NULL; l = l->next)
12438 {
12439 const char **slot;
23d6ee64 12440 const char *name = l->function_name.get ();
776592bf
DE
12441
12442 /* Allow for some names to be NULL, ignore them. */
12443 if (name == NULL)
12444 continue;
12445
c1fb9836 12446 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 12447 INSERT);
4a64f543
MS
12448 /* NOTE: We can assume slot != NULL here because xcalloc never
12449 returns NULL. */
776592bf 12450 if (*slot != NULL)
d8de7963 12451 return true;
776592bf
DE
12452 *slot = name;
12453 }
12454
d8de7963 12455 return false;
776592bf
DE
12456}
12457
0fb4aa4b
PA
12458/* When symbols change, it probably means the sources changed as well,
12459 and it might mean the static tracepoint markers are no longer at
12460 the same address or line numbers they used to be at last we
12461 checked. Losing your static tracepoints whenever you rebuild is
12462 undesirable. This function tries to resync/rematch gdb static
12463 tracepoints with the markers on the target, for static tracepoints
12464 that have not been set by marker id. Static tracepoint that have
12465 been set by marker id are reset by marker id in breakpoint_re_set.
12466 The heuristic is:
12467
12468 1) For a tracepoint set at a specific address, look for a marker at
12469 the old PC. If one is found there, assume to be the same marker.
12470 If the name / string id of the marker found is different from the
12471 previous known name, assume that means the user renamed the marker
12472 in the sources, and output a warning.
12473
12474 2) For a tracepoint set at a given line number, look for a marker
12475 at the new address of the old line number. If one is found there,
12476 assume to be the same marker. If the name / string id of the
12477 marker found is different from the previous known name, assume that
12478 means the user renamed the marker in the sources, and output a
12479 warning.
12480
12481 3) If a marker is no longer found at the same address or line, it
12482 may mean the marker no longer exists. But it may also just mean
12483 the code changed a bit. Maybe the user added a few lines of code
12484 that made the marker move up or down (in line number terms). Ask
12485 the target for info about the marker with the string id as we knew
12486 it. If found, update line number and address in the matching
12487 static tracepoint. This will get confused if there's more than one
12488 marker with the same ID (possible in UST, although unadvised
12489 precisely because it confuses tools). */
12490
12491static struct symtab_and_line
12492update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12493{
d9b3f62e 12494 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
12495 struct static_tracepoint_marker marker;
12496 CORE_ADDR pc;
0fb4aa4b
PA
12497
12498 pc = sal.pc;
12499 if (sal.line)
12500 find_line_pc (sal.symtab, sal.line, &pc);
12501
12502 if (target_static_tracepoint_marker_at (pc, &marker))
12503 {
5d9310c4 12504 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 12505 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
12506 b->number, tp->static_trace_marker_id.c_str (),
12507 marker.str_id.c_str ());
0fb4aa4b 12508
5d9310c4 12509 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
12510
12511 return sal;
12512 }
12513
12514 /* Old marker wasn't found on target at lineno. Try looking it up
12515 by string ID. */
12516 if (!sal.explicit_pc
12517 && sal.line != 0
12518 && sal.symtab != NULL
5d9310c4 12519 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 12520 {
5d9310c4
SM
12521 std::vector<static_tracepoint_marker> markers
12522 = target_static_tracepoint_markers_by_strid
12523 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 12524
5d9310c4 12525 if (!markers.empty ())
0fb4aa4b 12526 {
0fb4aa4b 12527 struct symbol *sym;
80e1d417 12528 struct static_tracepoint_marker *tpmarker;
79a45e25 12529 struct ui_out *uiout = current_uiout;
0fb4aa4b 12530
5d9310c4 12531 tpmarker = &markers[0];
0fb4aa4b 12532
5d9310c4 12533 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
12534
12535 warning (_("marker for static tracepoint %d (%s) not "
12536 "found at previous line number"),
5d9310c4 12537 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 12538
51abb421 12539 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 12540 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 12541 uiout->text ("Now in ");
0fb4aa4b
PA
12542 if (sym)
12543 {
987012b8 12544 uiout->field_string ("func", sym->print_name (),
e43b10e1 12545 function_name_style.style ());
112e8700 12546 uiout->text (" at ");
0fb4aa4b 12547 }
112e8700 12548 uiout->field_string ("file",
cbe56571 12549 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 12550 file_name_style.style ());
112e8700 12551 uiout->text (":");
0fb4aa4b 12552
112e8700 12553 if (uiout->is_mi_like_p ())
0fb4aa4b 12554 {
0b0865da 12555 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 12556
112e8700 12557 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
12558 }
12559
381befee 12560 uiout->field_signed ("line", sal2.line);
112e8700 12561 uiout->text ("\n");
0fb4aa4b 12562
80e1d417 12563 b->loc->line_number = sal2.line;
2f202fde 12564 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 12565
40d97ee2
PA
12566 std::unique_ptr<explicit_location_spec> els
12567 (new explicit_location_spec ());
12568 els->source_filename
12569 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12570 els->line_offset.offset = b->loc->line_number;
12571 els->line_offset.sign = LINE_OFFSET_NONE;
12572
12573 b->locspec = std::move (els);
0fb4aa4b
PA
12574
12575 /* Might be nice to check if function changed, and warn if
12576 so. */
0fb4aa4b
PA
12577 }
12578 }
12579 return sal;
12580}
12581
d8de7963 12582/* Returns true iff locations A and B are sufficiently same that
8d3788bd
VP
12583 we don't need to report breakpoint as changed. */
12584
d8de7963 12585static bool
8d3788bd
VP
12586locations_are_equal (struct bp_location *a, struct bp_location *b)
12587{
12588 while (a && b)
12589 {
12590 if (a->address != b->address)
d8de7963 12591 return false;
8d3788bd
VP
12592
12593 if (a->shlib_disabled != b->shlib_disabled)
d8de7963 12594 return false;
8d3788bd
VP
12595
12596 if (a->enabled != b->enabled)
d8de7963 12597 return false;
8d3788bd 12598
b5fa468f 12599 if (a->disabled_by_cond != b->disabled_by_cond)
d8de7963 12600 return false;
b5fa468f 12601
8d3788bd
VP
12602 a = a->next;
12603 b = b->next;
12604 }
12605
12606 if ((a == NULL) != (b == NULL))
d8de7963 12607 return false;
8d3788bd 12608
d8de7963 12609 return true;
8d3788bd
VP
12610}
12611
c2f4122d
PA
12612/* Split all locations of B that are bound to PSPACE out of B's
12613 location list to a separate list and return that list's head. If
12614 PSPACE is NULL, hoist out all locations of B. */
12615
12616static struct bp_location *
12617hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12618{
12619 struct bp_location head;
12620 struct bp_location *i = b->loc;
12621 struct bp_location **i_link = &b->loc;
12622 struct bp_location *hoisted = &head;
12623
12624 if (pspace == NULL)
12625 {
12626 i = b->loc;
12627 b->loc = NULL;
12628 return i;
12629 }
12630
12631 head.next = NULL;
12632
12633 while (i != NULL)
12634 {
12635 if (i->pspace == pspace)
12636 {
12637 *i_link = i->next;
12638 i->next = NULL;
12639 hoisted->next = i;
12640 hoisted = i;
12641 }
12642 else
12643 i_link = &i->next;
12644 i = *i_link;
12645 }
12646
12647 return head.next;
12648}
12649
12650/* Create new breakpoint locations for B (a hardware or software
12651 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12652 zero, then B is a ranged breakpoint. Only recreates locations for
12653 FILTER_PSPACE. Locations of other program spaces are left
12654 untouched. */
f1310107 12655
0e30163f 12656void
74421c0b 12657update_breakpoint_locations (code_breakpoint *b,
c2f4122d 12658 struct program_space *filter_pspace,
6c5b2ebe
PA
12659 gdb::array_view<const symtab_and_line> sals,
12660 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 12661{
c2f4122d 12662 struct bp_location *existing_locations;
0d381245 12663
6c5b2ebe 12664 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
12665 {
12666 /* Ranged breakpoints have only one start location and one end
12667 location. */
12668 b->enable_state = bp_disabled;
6cb06a8c
TT
12669 gdb_printf (gdb_stderr,
12670 _("Could not reset ranged breakpoint %d: "
12671 "multiple locations found\n"),
12672 b->number);
f8eba3c6
TT
12673 return;
12674 }
f1310107 12675
4a64f543
MS
12676 /* If there's no new locations, and all existing locations are
12677 pending, don't do anything. This optimizes the common case where
12678 all locations are in the same shared library, that was unloaded.
12679 We'd like to retain the location, so that when the library is
12680 loaded again, we don't loose the enabled/disabled status of the
12681 individual locations. */
6c5b2ebe 12682 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
12683 return;
12684
c2f4122d 12685 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 12686
6c5b2ebe 12687 for (const auto &sal : sals)
fe3f5fa8 12688 {
f8eba3c6
TT
12689 struct bp_location *new_loc;
12690
6c5b2ebe 12691 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 12692
06615628 12693 new_loc = b->add_location (sal);
fe3f5fa8 12694
0d381245
VP
12695 /* Reparse conditions, they might contain references to the
12696 old symtab. */
12697 if (b->cond_string != NULL)
12698 {
bbc13ae3 12699 const char *s;
fe3f5fa8 12700
6f781ee3 12701 s = b->cond_string.get ();
a70b8144 12702 try
0d381245 12703 {
6c5b2ebe
PA
12704 new_loc->cond = parse_exp_1 (&s, sal.pc,
12705 block_for_pc (sal.pc),
0d381245
VP
12706 0);
12707 }
230d2906 12708 catch (const gdb_exception_error &e)
0d381245 12709 {
b5fa468f 12710 new_loc->disabled_by_cond = true;
0d381245
VP
12711 }
12712 }
fe3f5fa8 12713
6c5b2ebe 12714 if (!sals_end.empty ())
f1310107 12715 {
6c5b2ebe 12716 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 12717
6c5b2ebe 12718 new_loc->length = end - sals[0].pc + 1;
f1310107 12719 }
0d381245 12720 }
fe3f5fa8 12721
4a64f543
MS
12722 /* If possible, carry over 'disable' status from existing
12723 breakpoints. */
0d381245
VP
12724 {
12725 struct bp_location *e = existing_locations;
776592bf
DE
12726 /* If there are multiple breakpoints with the same function name,
12727 e.g. for inline functions, comparing function names won't work.
12728 Instead compare pc addresses; this is just a heuristic as things
12729 may have moved, but in practice it gives the correct answer
12730 often enough until a better solution is found. */
12731 int have_ambiguous_names = ambiguous_names_p (b->loc);
12732
0d381245
VP
12733 for (; e; e = e->next)
12734 {
b5fa468f 12735 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 12736 {
776592bf
DE
12737 if (have_ambiguous_names)
12738 {
40cb8ca5 12739 for (bp_location *l : b->locations ())
7f32a4d5
PA
12740 {
12741 /* Ignore software vs hardware location type at
12742 this point, because with "set breakpoint
12743 auto-hw", after a re-set, locations that were
12744 hardware can end up as software, or vice versa.
12745 As mentioned above, this is an heuristic and in
12746 practice should give the correct answer often
12747 enough. */
12748 if (breakpoint_locations_match (e, l, true))
12749 {
b5fa468f
TBA
12750 l->enabled = e->enabled;
12751 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
12752 break;
12753 }
12754 }
776592bf
DE
12755 }
12756 else
12757 {
40cb8ca5 12758 for (bp_location *l : b->locations ())
776592bf 12759 if (l->function_name
23d6ee64
TT
12760 && strcmp (e->function_name.get (),
12761 l->function_name.get ()) == 0)
776592bf 12762 {
b5fa468f
TBA
12763 l->enabled = e->enabled;
12764 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
12765 break;
12766 }
12767 }
0d381245
VP
12768 }
12769 }
12770 }
fe3f5fa8 12771
8d3788bd 12772 if (!locations_are_equal (existing_locations, b->loc))
76727919 12773 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
12774}
12775
264f9890 12776/* Find the SaL locations corresponding to the given LOCSPEC.
ef23e705
TJB
12777 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12778
2c9a6d72
TT
12779std::vector<symtab_and_line>
12780code_breakpoint::location_spec_to_sals (location_spec *locspec,
12781 struct program_space *search_pspace,
12782 int *found)
ef23e705 12783{
cc06b668 12784 struct gdb_exception exception;
ef23e705 12785
6c5b2ebe
PA
12786 std::vector<symtab_and_line> sals;
12787
a70b8144 12788 try
ef23e705 12789 {
2c9a6d72 12790 sals = decode_location_spec (locspec, search_pspace);
ef23e705 12791 }
94aeb44b 12792 catch (gdb_exception_error &e)
ef23e705 12793 {
d8de7963 12794 int not_found_and_ok = false;
492d29ea 12795
ef23e705
TJB
12796 /* For pending breakpoints, it's expected that parsing will
12797 fail until the right shared library is loaded. User has
12798 already told to create pending breakpoints and don't need
12799 extra messages. If breakpoint is in bp_shlib_disabled
12800 state, then user already saw the message about that
12801 breakpoint being disabled, and don't want to see more
12802 errors. */
58438ac1 12803 if (e.error == NOT_FOUND_ERROR
2c9a6d72
TT
12804 && (condition_not_parsed
12805 || (loc != NULL
c2f4122d 12806 && search_pspace != NULL
2c9a6d72
TT
12807 && loc->pspace != search_pspace)
12808 || (loc && loc->shlib_disabled)
12809 || (loc && loc->pspace->executing_startup)
12810 || enable_state == bp_disabled))
d8de7963 12811 not_found_and_ok = true;
ef23e705
TJB
12812
12813 if (!not_found_and_ok)
12814 {
12815 /* We surely don't want to warn about the same breakpoint
12816 10 times. One solution, implemented here, is disable
12817 the breakpoint on error. Another solution would be to
12818 have separate 'warning emitted' flag. Since this
12819 happens only when a binary has changed, I don't know
12820 which approach is better. */
2c9a6d72 12821 enable_state = bp_disabled;
eedc3f4f 12822 throw;
ef23e705 12823 }
94aeb44b
TT
12824
12825 exception = std::move (e);
ef23e705
TJB
12826 }
12827
492d29ea 12828 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 12829 {
6c5b2ebe
PA
12830 for (auto &sal : sals)
12831 resolve_sal_pc (&sal);
2c9a6d72 12832 if (condition_not_parsed && extra_string != NULL)
ef23e705 12833 {
2c9a6d72
TT
12834 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12835 int local_thread, local_task;
ef23e705 12836
2c9a6d72
TT
12837 find_condition_and_thread_for_sals (sals, extra_string.get (),
12838 &local_cond, &local_thread,
12839 &local_task, &local_extra);
12840 gdb_assert (cond_string == nullptr);
12841 if (local_cond != nullptr)
12842 cond_string = std::move (local_cond);
12843 thread = local_thread;
12844 task = local_task;
12845 if (local_extra != nullptr)
12846 extra_string = std::move (local_extra);
12847 condition_not_parsed = 0;
ef23e705
TJB
12848 }
12849
2c9a6d72
TT
12850 if (type == bp_static_tracepoint)
12851 sals[0] = update_static_tracepoint (this, sals[0]);
ef23e705 12852
58438ac1
TT
12853 *found = 1;
12854 }
12855 else
12856 *found = 0;
ef23e705
TJB
12857
12858 return sals;
12859}
12860
348d480f
PA
12861/* The default re_set method, for typical hardware or software
12862 breakpoints. Reevaluate the breakpoint and recreate its
12863 locations. */
12864
b3d5660a
TT
12865void
12866code_breakpoint::re_set_default ()
ef23e705 12867{
c2f4122d 12868 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 12869 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 12870
6c5b2ebe 12871 int found;
2c9a6d72
TT
12872 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12873 filter_pspace,
12874 &found);
ef23e705 12875 if (found)
6c5b2ebe 12876 expanded = std::move (sals);
ef23e705 12877
b3d5660a 12878 if (locspec_range_end != nullptr)
f1310107 12879 {
6c5b2ebe 12880 std::vector<symtab_and_line> sals_end
2c9a6d72 12881 = location_spec_to_sals (locspec_range_end.get (),
264f9890 12882 filter_pspace, &found);
f1310107 12883 if (found)
6c5b2ebe 12884 expanded_end = std::move (sals_end);
f1310107
TJB
12885 }
12886
b3d5660a 12887 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
28010a5d
PA
12888}
12889
983af33b
SDJ
12890/* Default method for creating SALs from an address string. It basically
12891 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12892
12893static void
264f9890
PA
12894create_sals_from_location_spec_default (location_spec *locspec,
12895 struct linespec_result *canonical)
983af33b 12896{
264f9890 12897 parse_breakpoint_sals (locspec, canonical);
983af33b
SDJ
12898}
12899
bf469271 12900/* Reset a breakpoint. */
c906108c 12901
bf469271
PA
12902static void
12903breakpoint_re_set_one (breakpoint *b)
c906108c 12904{
fdf44873
TT
12905 input_radix = b->input_radix;
12906 set_language (b->language);
c906108c 12907
c47614fe 12908 b->re_set ();
c906108c
SS
12909}
12910
c2f4122d
PA
12911/* Re-set breakpoint locations for the current program space.
12912 Locations bound to other program spaces are left untouched. */
12913
c906108c 12914void
69de3c6a 12915breakpoint_re_set (void)
c906108c 12916{
c5aa993b 12917 {
fdf44873
TT
12918 scoped_restore_current_language save_language;
12919 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 12920 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 12921
8e817061
JB
12922 /* breakpoint_re_set_one sets the current_language to the language
12923 of the breakpoint it is resetting (see prepare_re_set_context)
12924 before re-evaluating the breakpoint's location. This change can
12925 unfortunately get undone by accident if the language_mode is set
12926 to auto, and we either switch frames, or more likely in this context,
12927 we select the current frame.
12928
12929 We prevent this by temporarily turning the language_mode to
12930 language_mode_manual. We restore it once all breakpoints
12931 have been reset. */
12932 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12933 language_mode = language_mode_manual;
12934
5ed8105e
PA
12935 /* Note: we must not try to insert locations until after all
12936 breakpoints have been re-set. Otherwise, e.g., when re-setting
12937 breakpoint 1, we'd insert the locations of breakpoint 2, which
12938 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 12939
1428b37a 12940 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 12941 {
a70b8144 12942 try
bf469271
PA
12943 {
12944 breakpoint_re_set_one (b);
12945 }
230d2906 12946 catch (const gdb_exception &ex)
bf469271
PA
12947 {
12948 exception_fprintf (gdb_stderr, ex,
12949 "Error in re-setting breakpoint %d: ",
12950 b->number);
12951 }
5ed8105e 12952 }
5ed8105e
PA
12953
12954 jit_breakpoint_re_set ();
12955 }
6c95b8df 12956
af02033e
PP
12957 create_overlay_event_breakpoint ();
12958 create_longjmp_master_breakpoint ();
12959 create_std_terminate_master_breakpoint ();
186c406b 12960 create_exception_master_breakpoint ();
2a7f3dff
PA
12961
12962 /* Now we can insert. */
12963 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
12964}
12965\f
c906108c
SS
12966/* Reset the thread number of this breakpoint:
12967
12968 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12969 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12970void
fba45db2 12971breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12972{
12973 if (b->thread != -1)
12974 {
00431a78 12975 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
12976
12977 /* We're being called after following a fork. The new fork is
12978 selected as current, and unless this was a vfork will have a
12979 different program space from the original thread. Reset that
12980 as well. */
12981 b->loc->pspace = current_program_space;
c906108c
SS
12982 }
12983}
12984
03ac34d5
MS
12985/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12986 If from_tty is nonzero, it prints a message to that effect,
12987 which ends with a period (no newline). */
12988
c906108c 12989void
fba45db2 12990set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12991{
c906108c
SS
12992 if (count < 0)
12993 count = 0;
12994
43892fdf 12995 for (breakpoint *b : all_breakpoints ())
c906108c 12996 if (b->number == bptnum)
01add95b
SM
12997 {
12998 if (is_tracepoint (b))
12999 {
13000 if (from_tty && count != 0)
6cb06a8c
TT
13001 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13002 bptnum);
01add95b
SM
13003 return;
13004 }
13005
13006 b->ignore_count = count;
13007 if (from_tty)
13008 {
13009 if (count == 0)
6cb06a8c
TT
13010 gdb_printf (_("Will stop next time "
13011 "breakpoint %d is reached."),
13012 bptnum);
01add95b 13013 else if (count == 1)
6cb06a8c
TT
13014 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13015 bptnum);
01add95b 13016 else
6cb06a8c
TT
13017 gdb_printf (_("Will ignore next %d "
13018 "crossings of breakpoint %d."),
13019 count, bptnum);
01add95b
SM
13020 }
13021 gdb::observers::breakpoint_modified.notify (b);
13022 return;
13023 }
c906108c 13024
8a3fe4f8 13025 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13026}
13027
c906108c
SS
13028/* Command to set ignore-count of breakpoint N to COUNT. */
13029
13030static void
0b39b52e 13031ignore_command (const char *args, int from_tty)
c906108c 13032{
0b39b52e 13033 const char *p = args;
52f0bd74 13034 int num;
c906108c
SS
13035
13036 if (p == 0)
e2e0b3e5 13037 error_no_arg (_("a breakpoint number"));
c5aa993b 13038
c906108c 13039 num = get_number (&p);
5c44784c 13040 if (num == 0)
8a3fe4f8 13041 error (_("bad breakpoint number: '%s'"), args);
c906108c 13042 if (*p == 0)
8a3fe4f8 13043 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13044
13045 set_ignore_count (num,
13046 longest_to_int (value_as_long (parse_and_eval (p))),
13047 from_tty);
221ea385 13048 if (from_tty)
6cb06a8c 13049 gdb_printf ("\n");
c906108c
SS
13050}
13051\f
d0fe4701
XR
13052
13053/* Call FUNCTION on each of the breakpoints with numbers in the range
13054 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13055
13056static void
d0fe4701
XR
13057map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13058 gdb::function_view<void (breakpoint *)> function)
c906108c 13059{
d0fe4701
XR
13060 if (bp_num_range.first == 0)
13061 {
13062 warning (_("bad breakpoint number at or near '%d'"),
13063 bp_num_range.first);
13064 }
13065 else
c906108c 13066 {
d0fe4701 13067 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13068 {
d0fe4701
XR
13069 bool match = false;
13070
1428b37a 13071 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 13072 if (b->number == i)
5c44784c 13073 {
bfd28288 13074 match = true;
48649e1b 13075 function (b);
11cf8741 13076 break;
5c44784c 13077 }
bfd28288 13078 if (!match)
6cb06a8c 13079 gdb_printf (_("No breakpoint number %d.\n"), i);
c5aa993b 13080 }
c906108c
SS
13081 }
13082}
13083
d0fe4701
XR
13084/* Call FUNCTION on each of the breakpoints whose numbers are given in
13085 ARGS. */
13086
13087static void
13088map_breakpoint_numbers (const char *args,
13089 gdb::function_view<void (breakpoint *)> function)
13090{
13091 if (args == NULL || *args == '\0')
13092 error_no_arg (_("one or more breakpoint numbers"));
13093
13094 number_or_range_parser parser (args);
13095
13096 while (!parser.finished ())
13097 {
13098 int num = parser.get_number ();
13099 map_breakpoint_number_range (std::make_pair (num, num), function);
13100 }
13101}
13102
13103/* Return the breakpoint location structure corresponding to the
13104 BP_NUM and LOC_NUM values. */
13105
0d381245 13106static struct bp_location *
d0fe4701 13107find_location_by_number (int bp_num, int loc_num)
0d381245 13108{
43892fdf 13109 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
13110
13111 if (!b || b->number != bp_num)
d0fe4701 13112 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13113
0d381245 13114 if (loc_num == 0)
d0fe4701 13115 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13116
d0fe4701 13117 int n = 0;
40cb8ca5 13118 for (bp_location *loc : b->locations ())
d0fe4701
XR
13119 if (++n == loc_num)
13120 return loc;
13121
13122 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13123}
13124
95e95a6d
PA
13125/* Modes of operation for extract_bp_num. */
13126enum class extract_bp_kind
13127{
13128 /* Extracting a breakpoint number. */
13129 bp,
13130
13131 /* Extracting a location number. */
13132 loc,
13133};
13134
13135/* Extract a breakpoint or location number (as determined by KIND)
13136 from the string starting at START. TRAILER is a character which
13137 can be found after the number. If you don't want a trailer, use
13138 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13139 string. This always returns a positive integer. */
13140
13141static int
13142extract_bp_num (extract_bp_kind kind, const char *start,
13143 int trailer, const char **end_out = NULL)
13144{
13145 const char *end = start;
13146 int num = get_number_trailer (&end, trailer);
13147 if (num < 0)
13148 error (kind == extract_bp_kind::bp
13149 ? _("Negative breakpoint number '%.*s'")
13150 : _("Negative breakpoint location number '%.*s'"),
13151 int (end - start), start);
13152 if (num == 0)
13153 error (kind == extract_bp_kind::bp
13154 ? _("Bad breakpoint number '%.*s'")
13155 : _("Bad breakpoint location number '%.*s'"),
13156 int (end - start), start);
13157
13158 if (end_out != NULL)
13159 *end_out = end;
13160 return num;
13161}
13162
13163/* Extract a breakpoint or location range (as determined by KIND) in
13164 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13165 representing the (inclusive) range. The returned pair's elements
13166 are always positive integers. */
13167
13168static std::pair<int, int>
13169extract_bp_or_bp_range (extract_bp_kind kind,
13170 const std::string &arg,
13171 std::string::size_type arg_offset)
13172{
13173 std::pair<int, int> range;
13174 const char *bp_loc = &arg[arg_offset];
13175 std::string::size_type dash = arg.find ('-', arg_offset);
13176 if (dash != std::string::npos)
13177 {
13178 /* bp_loc is a range (x-z). */
13179 if (arg.length () == dash + 1)
13180 error (kind == extract_bp_kind::bp
13181 ? _("Bad breakpoint number at or near: '%s'")
13182 : _("Bad breakpoint location number at or near: '%s'"),
13183 bp_loc);
13184
13185 const char *end;
13186 const char *start_first = bp_loc;
13187 const char *start_second = &arg[dash + 1];
13188 range.first = extract_bp_num (kind, start_first, '-');
13189 range.second = extract_bp_num (kind, start_second, '\0', &end);
13190
13191 if (range.first > range.second)
13192 error (kind == extract_bp_kind::bp
13193 ? _("Inverted breakpoint range at '%.*s'")
13194 : _("Inverted breakpoint location range at '%.*s'"),
13195 int (end - start_first), start_first);
13196 }
13197 else
13198 {
13199 /* bp_loc is a single value. */
13200 range.first = extract_bp_num (kind, bp_loc, '\0');
13201 range.second = range.first;
13202 }
13203 return range;
13204}
13205
d0fe4701
XR
13206/* Extract the breakpoint/location range specified by ARG. Returns
13207 the breakpoint range in BP_NUM_RANGE, and the location range in
13208 BP_LOC_RANGE.
13209
13210 ARG may be in any of the following forms:
13211
13212 x where 'x' is a breakpoint number.
13213 x-y where 'x' and 'y' specify a breakpoint numbers range.
13214 x.y where 'x' is a breakpoint number and 'y' a location number.
13215 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13216 location number range.
13217*/
13218
cc638e86 13219static void
d0fe4701
XR
13220extract_bp_number_and_location (const std::string &arg,
13221 std::pair<int, int> &bp_num_range,
13222 std::pair<int, int> &bp_loc_range)
13223{
13224 std::string::size_type dot = arg.find ('.');
13225
13226 if (dot != std::string::npos)
13227 {
13228 /* Handle 'x.y' and 'x.y-z' cases. */
13229
13230 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 13231 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 13232
95e95a6d
PA
13233 bp_num_range.first
13234 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13235 bp_num_range.second = bp_num_range.first;
d0fe4701 13236
95e95a6d
PA
13237 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13238 arg, dot + 1);
d0fe4701
XR
13239 }
13240 else
13241 {
13242 /* Handle x and x-y cases. */
d0fe4701 13243
95e95a6d 13244 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
13245 bp_loc_range.first = 0;
13246 bp_loc_range.second = 0;
13247 }
d0fe4701
XR
13248}
13249
13250/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13251 specifies whether to enable or disable. */
13252
13253static void
13254enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13255{
13256 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13257 if (loc != NULL)
13258 {
b5fa468f
TBA
13259 if (loc->disabled_by_cond && enable)
13260 error (_("Breakpoint %d's condition is invalid at location %d, "
13261 "cannot enable."), bp_num, loc_num);
13262
d0fe4701
XR
13263 if (loc->enabled != enable)
13264 {
13265 loc->enabled = enable;
13266 mark_breakpoint_location_modified (loc);
13267 }
13268 if (target_supports_enable_disable_tracepoint ()
13269 && current_trace_status ()->running && loc->owner
13270 && is_tracepoint (loc->owner))
13271 target_disable_tracepoint (loc);
13272 }
13273 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13274
e5213e2c
SF
13275 gdb::observers::breakpoint_modified.notify (loc->owner);
13276}
13277
13278/* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13279 owner. 1-based indexing. -1 signals NOT FOUND. */
13280
13281static int
d8a77e4c 13282find_loc_num_by_location (const bp_location *loc)
e5213e2c
SF
13283{
13284 if (loc != nullptr && loc->owner != nullptr)
13285 {
13286 /* Locations use 1-based indexing. */
13287 int loc_num = 1;
13288 for (bp_location *it : loc->owner->locations ())
13289 {
13290 if (it == loc)
13291 return loc_num;
13292 loc_num++;
13293 }
13294 }
13295 return -1;
13296}
13297
13298/* Enable or disable a breakpoint location LOC. ENABLE
13299 specifies whether to enable or disable. */
13300
13301void
13302enable_disable_bp_location (bp_location *loc, bool enable)
13303{
13304 if (loc == nullptr)
13305 error (_("Breakpoint location is invalid."));
13306
13307 if (loc->owner == nullptr)
13308 error (_("Breakpoint location does not have an owner breakpoint."));
13309
13310 if (loc->disabled_by_cond && enable)
13311 {
13312 int loc_num = find_loc_num_by_location (loc);
13313 if (loc_num == -1)
13314 error (_("Breakpoint location LOC_NUM could not be found."));
13315 else
13316 error (_("Breakpoint %d's condition is invalid at location %d, "
13317 "cannot enable."), loc->owner->number, loc_num);
13318 }
13319
13320 if (loc->enabled != enable)
13321 {
13322 loc->enabled = enable;
13323 mark_breakpoint_location_modified (loc);
13324 }
13325
13326 if (target_supports_enable_disable_tracepoint ()
13327 && current_trace_status ()->running && loc->owner
13328 && is_tracepoint (loc->owner))
13329 target_disable_tracepoint (loc);
13330
13331 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13332 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
13333}
13334
13335/* Enable or disable a range of breakpoint locations. BP_NUM is the
13336 number of the breakpoint, and BP_LOC_RANGE specifies the
13337 (inclusive) range of location numbers of that breakpoint to
13338 enable/disable. ENABLE specifies whether to enable or disable the
13339 location. */
13340
13341static void
13342enable_disable_breakpoint_location_range (int bp_num,
13343 std::pair<int, int> &bp_loc_range,
13344 bool enable)
13345{
13346 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13347 enable_disable_bp_num_loc (bp_num, i, enable);
13348}
0d381245 13349
1900040c
MS
13350/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13351 If from_tty is nonzero, it prints a message to that effect,
13352 which ends with a period (no newline). */
13353
c906108c 13354void
fba45db2 13355disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
13356{
13357 /* Never disable a watchpoint scope breakpoint; we want to
13358 hit them when we leave scope so we can delete both the
13359 watchpoint and its scope breakpoint at that time. */
13360 if (bpt->type == bp_watchpoint_scope)
13361 return;
13362
b5de0fa7 13363 bpt->enable_state = bp_disabled;
c906108c 13364
b775012e
LM
13365 /* Mark breakpoint locations modified. */
13366 mark_breakpoint_modified (bpt);
13367
d248b706
KY
13368 if (target_supports_enable_disable_tracepoint ()
13369 && current_trace_status ()->running && is_tracepoint (bpt))
13370 {
40cb8ca5 13371 for (bp_location *location : bpt->locations ())
d248b706
KY
13372 target_disable_tracepoint (location);
13373 }
13374
44702360 13375 update_global_location_list (UGLL_DONT_INSERT);
c906108c 13376
76727919 13377 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13378}
13379
d0fe4701
XR
13380/* Enable or disable the breakpoint(s) or breakpoint location(s)
13381 specified in ARGS. ARGS may be in any of the formats handled by
13382 extract_bp_number_and_location. ENABLE specifies whether to enable
13383 or disable the breakpoints/locations. */
13384
c906108c 13385static void
d0fe4701 13386enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 13387{
c906108c 13388 if (args == 0)
46c6471b 13389 {
43892fdf 13390 for (breakpoint *bpt : all_breakpoints ())
46c6471b 13391 if (user_breakpoint_p (bpt))
d0fe4701
XR
13392 {
13393 if (enable)
13394 enable_breakpoint (bpt);
13395 else
13396 disable_breakpoint (bpt);
13397 }
46c6471b 13398 }
9eaabc75 13399 else
0d381245 13400 {
cb791d59 13401 std::string num = extract_arg (&args);
9eaabc75 13402
cb791d59 13403 while (!num.empty ())
d248b706 13404 {
d0fe4701 13405 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 13406
cc638e86
PA
13407 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13408
13409 if (bp_loc_range.first == bp_loc_range.second
78805ff8
PW
13410 && (bp_loc_range.first == 0
13411 || (bp_loc_range.first == 1
13412 && bp_num_range.first == bp_num_range.second
13413 && !has_multiple_locations (bp_num_range.first))))
d0fe4701 13414 {
78805ff8
PW
13415 /* Handle breakpoint ids with formats 'x' or 'x-z'
13416 or 'y.1' where y has only one code location. */
cc638e86
PA
13417 map_breakpoint_number_range (bp_num_range,
13418 enable
13419 ? enable_breakpoint
13420 : disable_breakpoint);
13421 }
13422 else
13423 {
13424 /* Handle breakpoint ids with formats 'x.y' or
13425 'x.y-z'. */
13426 enable_disable_breakpoint_location_range
13427 (bp_num_range.first, bp_loc_range, enable);
b775012e 13428 }
9eaabc75 13429 num = extract_arg (&args);
d248b706 13430 }
0d381245 13431 }
c906108c
SS
13432}
13433
d0fe4701
XR
13434/* The disable command disables the specified breakpoints/locations
13435 (or all defined breakpoints) so they're no longer effective in
13436 stopping the inferior. ARGS may be in any of the forms defined in
13437 extract_bp_number_and_location. */
13438
13439static void
13440disable_command (const char *args, int from_tty)
13441{
13442 enable_disable_command (args, from_tty, false);
13443}
13444
c906108c 13445static void
816338b5
SS
13446enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13447 int count)
c906108c 13448{
afe38095 13449 int target_resources_ok;
c906108c
SS
13450
13451 if (bpt->type == bp_hardware_breakpoint)
13452 {
13453 int i;
c5aa993b 13454 i = hw_breakpoint_used_count ();
53a5351d 13455 target_resources_ok =
d92524f1 13456 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 13457 i + 1, 0);
c906108c 13458 if (target_resources_ok == 0)
8a3fe4f8 13459 error (_("No hardware breakpoint support in the target."));
c906108c 13460 else if (target_resources_ok < 0)
8a3fe4f8 13461 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
13462 }
13463
cc60f2e3 13464 if (is_watchpoint (bpt))
c906108c 13465 {
d07205c2 13466 /* Initialize it just to avoid a GCC false warning. */
f486487f 13467 enum enable_state orig_enable_state = bp_disabled;
dde02812 13468
a70b8144 13469 try
c906108c 13470 {
3a5c3e22
PA
13471 struct watchpoint *w = (struct watchpoint *) bpt;
13472
1e718ff1
TJB
13473 orig_enable_state = bpt->enable_state;
13474 bpt->enable_state = bp_enabled;
d8de7963 13475 update_watchpoint (w, true /* reparse */);
c906108c 13476 }
230d2906 13477 catch (const gdb_exception &e)
c5aa993b 13478 {
1e718ff1 13479 bpt->enable_state = orig_enable_state;
dde02812
ES
13480 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13481 bpt->number);
13482 return;
c5aa993b 13483 }
c906108c 13484 }
0101ce28 13485
b775012e
LM
13486 bpt->enable_state = bp_enabled;
13487
13488 /* Mark breakpoint locations modified. */
13489 mark_breakpoint_modified (bpt);
13490
d248b706
KY
13491 if (target_supports_enable_disable_tracepoint ()
13492 && current_trace_status ()->running && is_tracepoint (bpt))
13493 {
40cb8ca5 13494 for (bp_location *location : bpt->locations ())
d248b706
KY
13495 target_enable_tracepoint (location);
13496 }
13497
b4c291bb 13498 bpt->disposition = disposition;
816338b5 13499 bpt->enable_count = count;
44702360 13500 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 13501
76727919 13502 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13503}
13504
fe3f5fa8 13505
c906108c 13506void
fba45db2 13507enable_breakpoint (struct breakpoint *bpt)
c906108c 13508{
816338b5 13509 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
13510}
13511
d0fe4701
XR
13512/* The enable command enables the specified breakpoints/locations (or
13513 all defined breakpoints) so they once again become (or continue to
13514 be) effective in stopping the inferior. ARGS may be in any of the
13515 forms defined in extract_bp_number_and_location. */
c906108c 13516
c906108c 13517static void
981a3fb3 13518enable_command (const char *args, int from_tty)
c906108c 13519{
d0fe4701 13520 enable_disable_command (args, from_tty, true);
c906108c
SS
13521}
13522
c906108c 13523static void
4495129a 13524enable_once_command (const char *args, int from_tty)
c906108c 13525{
48649e1b
TT
13526 map_breakpoint_numbers
13527 (args, [&] (breakpoint *b)
13528 {
13529 iterate_over_related_breakpoints
13530 (b, [&] (breakpoint *bpt)
13531 {
13532 enable_breakpoint_disp (bpt, disp_disable, 1);
13533 });
13534 });
816338b5
SS
13535}
13536
13537static void
4495129a 13538enable_count_command (const char *args, int from_tty)
816338b5 13539{
b9d61307
SM
13540 int count;
13541
13542 if (args == NULL)
13543 error_no_arg (_("hit count"));
13544
13545 count = get_number (&args);
816338b5 13546
48649e1b
TT
13547 map_breakpoint_numbers
13548 (args, [&] (breakpoint *b)
13549 {
13550 iterate_over_related_breakpoints
13551 (b, [&] (breakpoint *bpt)
13552 {
13553 enable_breakpoint_disp (bpt, disp_disable, count);
13554 });
13555 });
c906108c
SS
13556}
13557
c906108c 13558static void
4495129a 13559enable_delete_command (const char *args, int from_tty)
c906108c 13560{
48649e1b
TT
13561 map_breakpoint_numbers
13562 (args, [&] (breakpoint *b)
13563 {
13564 iterate_over_related_breakpoints
13565 (b, [&] (breakpoint *bpt)
13566 {
13567 enable_breakpoint_disp (bpt, disp_del, 1);
13568 });
13569 });
c906108c
SS
13570}
13571\f
1f3b5d1b
PP
13572/* Invalidate last known value of any hardware watchpoint if
13573 the memory which that value represents has been written to by
13574 GDB itself. */
13575
13576static void
8de0566d
YQ
13577invalidate_bp_value_on_memory_change (struct inferior *inferior,
13578 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
13579 const bfd_byte *data)
13580{
43892fdf 13581 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 13582 if (bp->enable_state == bp_enabled
3a5c3e22 13583 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 13584 {
3a5c3e22 13585 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 13586
850645cf 13587 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 13588 {
40cb8ca5 13589 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
13590 if (loc->loc_type == bp_loc_hardware_watchpoint
13591 && loc->address + loc->length > addr
13592 && addr + len > loc->address)
13593 {
3a5c3e22 13594 wp->val = NULL;
4c1d86d9 13595 wp->val_valid = false;
3a5c3e22
PA
13596 }
13597 }
1f3b5d1b
PP
13598 }
13599}
13600
8181d85f
DJ
13601/* Create and insert a breakpoint for software single step. */
13602
13603void
6c95b8df 13604insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 13605 const address_space *aspace,
4a64f543 13606 CORE_ADDR next_pc)
8181d85f 13607{
7c16b83e
PA
13608 struct thread_info *tp = inferior_thread ();
13609 struct symtab_and_line sal;
13610 CORE_ADDR pc = next_pc;
8181d85f 13611
34b7e8a6
PA
13612 if (tp->control.single_step_breakpoints == NULL)
13613 {
960bc2bd 13614 std::unique_ptr<breakpoint> b
7ab97995
PA
13615 (new momentary_breakpoint (gdbarch, bp_single_step,
13616 current_program_space,
13617 null_frame_id,
13618 tp->global_num));
960bc2bd 13619
34b7e8a6 13620 tp->control.single_step_breakpoints
960bc2bd 13621 = add_to_breakpoint_chain (std::move (b));
34b7e8a6 13622 }
8181d85f 13623
7c16b83e
PA
13624 sal = find_pc_line (pc, 0);
13625 sal.pc = pc;
13626 sal.section = find_pc_overlay (pc);
13627 sal.explicit_pc = 1;
960bc2bd
PA
13628
13629 auto *ss_bp
98ed24fb
TT
13630 = (gdb::checked_static_cast<momentary_breakpoint *>
13631 (tp->control.single_step_breakpoints));
960bc2bd 13632 ss_bp->add_location (sal);
8181d85f 13633
7c16b83e 13634 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
13635}
13636
93f9a11f
YQ
13637/* Insert single step breakpoints according to the current state. */
13638
13639int
13640insert_single_step_breakpoints (struct gdbarch *gdbarch)
13641{
f5ea389a 13642 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 13643 std::vector<CORE_ADDR> next_pcs;
93f9a11f 13644
f5ea389a 13645 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 13646
a0ff9e1a 13647 if (!next_pcs.empty ())
93f9a11f 13648 {
bd2b40ac 13649 frame_info_ptr frame = get_current_frame ();
8b86c959 13650 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 13651
a0ff9e1a 13652 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
13653 insert_single_step_breakpoint (gdbarch, aspace, pc);
13654
93f9a11f
YQ
13655 return 1;
13656 }
13657 else
13658 return 0;
13659}
13660
34b7e8a6 13661/* See breakpoint.h. */
f02253f1
HZ
13662
13663int
7c16b83e 13664breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 13665 const address_space *aspace,
7c16b83e 13666 CORE_ADDR pc)
1aafd4da 13667{
40cb8ca5 13668 for (bp_location *loc : bp->locations ())
7c16b83e
PA
13669 if (loc->inserted
13670 && breakpoint_location_address_match (loc, aspace, pc))
13671 return 1;
1aafd4da 13672
7c16b83e 13673 return 0;
ef370185
JB
13674}
13675
13676/* Check whether a software single-step breakpoint is inserted at
13677 PC. */
13678
13679int
accd0bcd 13680single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
13681 CORE_ADDR pc)
13682{
43892fdf 13683 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
13684 {
13685 if (bpt->type == bp_single_step
13686 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13687 return 1;
13688 }
13689 return 0;
1aafd4da
UW
13690}
13691
1042e4c0
SS
13692/* Tracepoint-specific operations. */
13693
13694/* Set tracepoint count to NUM. */
13695static void
13696set_tracepoint_count (int num)
13697{
13698 tracepoint_count = num;
4fa62494 13699 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
13700}
13701
70221824 13702static void
0b39b52e 13703trace_command (const char *arg, int from_tty)
1042e4c0 13704{
264f9890
PA
13705 location_spec_up locspec = string_to_location_spec (&arg,
13706 current_language);
13707 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13708 (locspec.get (), true /* is_tracepoint */);
55aa24fb 13709
558a9d82 13710 create_breakpoint (get_current_arch (),
264f9890 13711 locspec.get (),
10a636cc 13712 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13713 0 /* tempflag */,
13714 bp_tracepoint /* type_wanted */,
13715 0 /* Ignore count */,
13716 pending_break_support,
13717 ops,
13718 from_tty,
13719 1 /* enabled */,
13720 0 /* internal */, 0);
1042e4c0
SS
13721}
13722
70221824 13723static void
0b39b52e 13724ftrace_command (const char *arg, int from_tty)
7a697b8d 13725{
264f9890
PA
13726 location_spec_up locspec = string_to_location_spec (&arg,
13727 current_language);
558a9d82 13728 create_breakpoint (get_current_arch (),
264f9890 13729 locspec.get (),
10a636cc 13730 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13731 0 /* tempflag */,
13732 bp_fast_tracepoint /* type_wanted */,
13733 0 /* Ignore count */,
13734 pending_break_support,
74421c0b 13735 &code_breakpoint_ops,
558a9d82
YQ
13736 from_tty,
13737 1 /* enabled */,
13738 0 /* internal */, 0);
0fb4aa4b
PA
13739}
13740
13741/* strace command implementation. Creates a static tracepoint. */
13742
70221824 13743static void
0b39b52e 13744strace_command (const char *arg, int from_tty)
0fb4aa4b 13745{
a678887d 13746 const struct breakpoint_ops *ops;
264f9890 13747 location_spec_up locspec;
7b572efb 13748 enum bptype type;
983af33b
SDJ
13749
13750 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13751 or with a normal static tracepoint. */
61012eef 13752 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
13753 {
13754 ops = &strace_marker_breakpoint_ops;
264f9890
PA
13755 locspec = new_linespec_location_spec (&arg,
13756 symbol_name_match_type::FULL);
7b572efb 13757 type = bp_static_marker_tracepoint;
f00aae0f 13758 }
983af33b 13759 else
f00aae0f 13760 {
74421c0b 13761 ops = &code_breakpoint_ops;
264f9890 13762 locspec = string_to_location_spec (&arg, current_language);
7b572efb 13763 type = bp_static_tracepoint;
f00aae0f 13764 }
983af33b 13765
558a9d82 13766 create_breakpoint (get_current_arch (),
264f9890 13767 locspec.get (),
10a636cc 13768 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82 13769 0 /* tempflag */,
7b572efb 13770 type /* type_wanted */,
558a9d82
YQ
13771 0 /* Ignore count */,
13772 pending_break_support,
13773 ops,
13774 from_tty,
13775 1 /* enabled */,
13776 0 /* internal */, 0);
7a697b8d
SS
13777}
13778
409873ef
SS
13779/* Set up a fake reader function that gets command lines from a linked
13780 list that was acquired during tracepoint uploading. */
13781
13782static struct uploaded_tp *this_utp;
3149d8c1 13783static int next_cmd;
409873ef 13784
f8631e5e
SM
13785static const char *
13786read_uploaded_action (std::string &buffer)
409873ef 13787{
a18ba4e4 13788 char *rslt = nullptr;
409873ef 13789
a18ba4e4
SM
13790 if (next_cmd < this_utp->cmd_strings.size ())
13791 {
67aa1f3c 13792 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
13793 next_cmd++;
13794 }
409873ef
SS
13795
13796 return rslt;
13797}
13798
00bf0b85
SS
13799/* Given information about a tracepoint as recorded on a target (which
13800 can be either a live system or a trace file), attempt to create an
13801 equivalent GDB tracepoint. This is not a reliable process, since
13802 the target does not necessarily have all the information used when
13803 the tracepoint was originally defined. */
13804
d9b3f62e 13805struct tracepoint *
00bf0b85 13806create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 13807{
f2fc3015
TT
13808 const char *addr_str;
13809 char small_buf[100];
d9b3f62e 13810 struct tracepoint *tp;
fd9b8c24 13811
409873ef 13812 if (utp->at_string)
67aa1f3c 13813 addr_str = utp->at_string.get ();
409873ef
SS
13814 else
13815 {
13816 /* In the absence of a source location, fall back to raw
13817 address. Since there is no way to confirm that the address
13818 means the same thing as when the trace was started, warn the
13819 user. */
3e43a32a
MS
13820 warning (_("Uploaded tracepoint %d has no "
13821 "source location, using raw address"),
409873ef 13822 utp->number);
8c042590 13823 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
13824 addr_str = small_buf;
13825 }
13826
13827 /* There's not much we can do with a sequence of bytecodes. */
13828 if (utp->cond && !utp->cond_string)
3e43a32a
MS
13829 warning (_("Uploaded tracepoint %d condition "
13830 "has no source form, ignoring it"),
409873ef 13831 utp->number);
d5551862 13832
264f9890
PA
13833 location_spec_up locspec = string_to_location_spec (&addr_str,
13834 current_language);
8cdf0e15 13835 if (!create_breakpoint (get_current_arch (),
264f9890 13836 locspec.get (),
67aa1f3c 13837 utp->cond_string.get (), -1, addr_str,
10a636cc 13838 false /* force_condition */,
e7e0cddf 13839 0 /* parse cond/thread */,
8cdf0e15 13840 0 /* tempflag */,
0fb4aa4b 13841 utp->type /* type_wanted */,
8cdf0e15
VP
13842 0 /* Ignore count */,
13843 pending_break_support,
74421c0b 13844 &code_breakpoint_ops,
8cdf0e15 13845 0 /* from_tty */,
84f4c1fe 13846 utp->enabled /* enabled */,
44f238bb
PA
13847 0 /* internal */,
13848 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 13849 return NULL;
fd9b8c24 13850
409873ef 13851 /* Get the tracepoint we just created. */
fd9b8c24
PA
13852 tp = get_tracepoint (tracepoint_count);
13853 gdb_assert (tp != NULL);
d5551862 13854
00bf0b85
SS
13855 if (utp->pass > 0)
13856 {
8c042590 13857 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 13858 tp->number);
00bf0b85 13859
409873ef 13860 trace_pass_command (small_buf, 0);
00bf0b85
SS
13861 }
13862
409873ef
SS
13863 /* If we have uploaded versions of the original commands, set up a
13864 special-purpose "reader" function and call the usual command line
13865 reader, then pass the result to the breakpoint command-setting
13866 function. */
a18ba4e4 13867 if (!utp->cmd_strings.empty ())
00bf0b85 13868 {
12973681 13869 counted_command_line cmd_list;
00bf0b85 13870
409873ef 13871 this_utp = utp;
3149d8c1 13872 next_cmd = 0;
d5551862 13873
60b3cef2 13874 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 13875
c1fc2657 13876 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 13877 }
a18ba4e4
SM
13878 else if (!utp->actions.empty ()
13879 || !utp->step_actions.empty ())
3e43a32a
MS
13880 warning (_("Uploaded tracepoint %d actions "
13881 "have no source form, ignoring them"),
409873ef 13882 utp->number);
00bf0b85 13883
f196051f 13884 /* Copy any status information that might be available. */
c1fc2657 13885 tp->hit_count = utp->hit_count;
f196051f
SS
13886 tp->traceframe_usage = utp->traceframe_usage;
13887
00bf0b85 13888 return tp;
d9b3f62e 13889}
00bf0b85 13890
1042e4c0
SS
13891/* Print information on tracepoint number TPNUM_EXP, or all if
13892 omitted. */
13893
13894static void
1d12d88f 13895info_tracepoints_command (const char *args, int from_tty)
1042e4c0 13896{
79a45e25 13897 struct ui_out *uiout = current_uiout;
e5a67952 13898 int num_printed;
1042e4c0 13899
5c458ae8 13900 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
13901
13902 if (num_printed == 0)
1042e4c0 13903 {
e5a67952 13904 if (args == NULL || *args == '\0')
112e8700 13905 uiout->message ("No tracepoints.\n");
d77f58be 13906 else
112e8700 13907 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 13908 }
ad443146
SS
13909
13910 default_collect_info ();
1042e4c0
SS
13911}
13912
4a64f543 13913/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13914 Not supported by all targets. */
13915static void
5fed81ff 13916enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13917{
13918 enable_command (args, from_tty);
13919}
13920
4a64f543 13921/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13922 Not supported by all targets. */
13923static void
5fed81ff 13924disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13925{
13926 disable_command (args, from_tty);
13927}
13928
4a64f543 13929/* Remove a tracepoint (or all if no argument). */
1042e4c0 13930static void
4495129a 13931delete_trace_command (const char *arg, int from_tty)
1042e4c0 13932{
1042e4c0
SS
13933 dont_repeat ();
13934
13935 if (arg == 0)
13936 {
13937 int breaks_to_delete = 0;
13938
13939 /* Delete all breakpoints if no argument.
dda83cd7
SM
13940 Do not delete internal or call-dummy breakpoints, these
13941 have to be deleted with an explicit breakpoint number
4a64f543 13942 argument. */
f6d17b2b 13943 for (breakpoint *tp : all_tracepoints ())
1428b37a 13944 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
13945 {
13946 breaks_to_delete = 1;
13947 break;
13948 }
1042e4c0
SS
13949
13950 /* Ask user only if there are some breakpoints to delete. */
13951 if (!from_tty
13952 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13953 {
1428b37a 13954 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 13955 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 13956 delete_breakpoint (b);
1042e4c0
SS
13957 }
13958 }
13959 else
48649e1b 13960 map_breakpoint_numbers
b926417a 13961 (arg, [&] (breakpoint *br)
48649e1b 13962 {
b926417a 13963 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13964 });
1042e4c0
SS
13965}
13966
197f0a60
TT
13967/* Helper function for trace_pass_command. */
13968
13969static void
d9b3f62e 13970trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 13971{
d9b3f62e 13972 tp->pass_count = count;
76727919 13973 gdb::observers::breakpoint_modified.notify (tp);
197f0a60 13974 if (from_tty)
6cb06a8c
TT
13975 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13976 tp->number, count);
197f0a60
TT
13977}
13978
1042e4c0
SS
13979/* Set passcount for tracepoint.
13980
13981 First command argument is passcount, second is tracepoint number.
13982 If tracepoint number omitted, apply to most recently defined.
13983 Also accepts special argument "all". */
13984
13985static void
0b39b52e 13986trace_pass_command (const char *args, int from_tty)
1042e4c0 13987{
d9b3f62e 13988 struct tracepoint *t1;
0b39b52e 13989 ULONGEST count;
1042e4c0
SS
13990
13991 if (args == 0 || *args == 0)
3e43a32a
MS
13992 error (_("passcount command requires an "
13993 "argument (count + optional TP num)"));
1042e4c0 13994
0b39b52e 13995 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 13996
529480d0 13997 args = skip_spaces (args);
1042e4c0
SS
13998 if (*args && strncasecmp (args, "all", 3) == 0)
13999 {
14000 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14001 if (*args)
14002 error (_("Junk at end of arguments."));
1042e4c0 14003
f6d17b2b 14004 for (breakpoint *b : all_tracepoints ())
01add95b
SM
14005 {
14006 t1 = (struct tracepoint *) b;
14007 trace_pass_set_count (t1, count, from_tty);
14008 }
197f0a60
TT
14009 }
14010 else if (*args == '\0')
1042e4c0 14011 {
5fa1d40e 14012 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14013 if (t1)
197f0a60
TT
14014 trace_pass_set_count (t1, count, from_tty);
14015 }
14016 else
14017 {
bfd28288
PA
14018 number_or_range_parser parser (args);
14019 while (!parser.finished ())
1042e4c0 14020 {
bfd28288 14021 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14022 if (t1)
14023 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14024 }
14025 }
1042e4c0
SS
14026}
14027
d9b3f62e 14028struct tracepoint *
1042e4c0
SS
14029get_tracepoint (int num)
14030{
f6d17b2b 14031 for (breakpoint *t : all_tracepoints ())
1042e4c0 14032 if (t->number == num)
d9b3f62e 14033 return (struct tracepoint *) t;
1042e4c0
SS
14034
14035 return NULL;
14036}
14037
d5551862
SS
14038/* Find the tracepoint with the given target-side number (which may be
14039 different from the tracepoint number after disconnecting and
14040 reconnecting). */
14041
d9b3f62e 14042struct tracepoint *
d5551862
SS
14043get_tracepoint_by_number_on_target (int num)
14044{
f6d17b2b 14045 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
14046 {
14047 struct tracepoint *t = (struct tracepoint *) b;
14048
14049 if (t->number_on_target == num)
14050 return t;
14051 }
d5551862
SS
14052
14053 return NULL;
14054}
14055
1042e4c0 14056/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14057 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14058 If the argument is missing, the most recent tracepoint
14059 (tracepoint_count) is returned. */
14060
d9b3f62e 14061struct tracepoint *
0b39b52e 14062get_tracepoint_by_number (const char **arg,
bfd28288 14063 number_or_range_parser *parser)
1042e4c0 14064{
1042e4c0 14065 int tpnum;
0b39b52e 14066 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14067
bfd28288 14068 if (parser != NULL)
197f0a60 14069 {
bfd28288
PA
14070 gdb_assert (!parser->finished ());
14071 tpnum = parser->get_number ();
197f0a60
TT
14072 }
14073 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14074 tpnum = tracepoint_count;
1042e4c0 14075 else
197f0a60 14076 tpnum = get_number (arg);
1042e4c0
SS
14077
14078 if (tpnum <= 0)
14079 {
14080 if (instring && *instring)
6cb06a8c
TT
14081 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14082 instring);
1042e4c0 14083 else
6cb06a8c 14084 gdb_printf (_("No previous tracepoint\n"));
1042e4c0
SS
14085 return NULL;
14086 }
14087
f6d17b2b 14088 for (breakpoint *t : all_tracepoints ())
1042e4c0 14089 if (t->number == tpnum)
d9b3f62e 14090 return (struct tracepoint *) t;
1042e4c0 14091
6cb06a8c 14092 gdb_printf ("No tracepoint number %d.\n", tpnum);
1042e4c0
SS
14093 return NULL;
14094}
14095
d9b3f62e 14096void
04d0163c 14097breakpoint::print_recreate_thread (struct ui_file *fp) const
d9b3f62e 14098{
04d0163c
TT
14099 if (thread != -1)
14100 gdb_printf (fp, " thread %d", thread);
d9b3f62e 14101
04d0163c
TT
14102 if (task != 0)
14103 gdb_printf (fp, " task %d", task);
d9b3f62e 14104
6cb06a8c 14105 gdb_printf (fp, "\n");
d9b3f62e
PA
14106}
14107
6149aea9
PA
14108/* Save information on user settable breakpoints (watchpoints, etc) to
14109 a new script file named FILENAME. If FILTER is non-NULL, call it
14110 on each breakpoint and only include the ones for which it returns
f2478a7e 14111 true. */
6149aea9 14112
1042e4c0 14113static void
4495129a 14114save_breakpoints (const char *filename, int from_tty,
f2478a7e 14115 bool (*filter) (const struct breakpoint *))
1042e4c0 14116{
d8de7963 14117 bool any = false;
6149aea9 14118 int extra_trace_bits = 0;
1042e4c0 14119
6149aea9
PA
14120 if (filename == 0 || *filename == 0)
14121 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14122
14123 /* See if we have anything to save. */
43892fdf 14124 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14125 {
14126 /* Skip internal and momentary breakpoints. */
14127 if (!user_breakpoint_p (tp))
14128 continue;
6149aea9 14129
01add95b
SM
14130 /* If we have a filter, only save the breakpoints it accepts. */
14131 if (filter && !filter (tp))
14132 continue;
6149aea9 14133
d8de7963 14134 any = true;
6149aea9 14135
01add95b
SM
14136 if (is_tracepoint (tp))
14137 {
14138 extra_trace_bits = 1;
6149aea9 14139
01add95b
SM
14140 /* We can stop searching. */
14141 break;
14142 }
14143 }
6149aea9
PA
14144
14145 if (!any)
1042e4c0 14146 {
6149aea9 14147 warning (_("Nothing to save."));
1042e4c0
SS
14148 return;
14149 }
14150
ee0c3293 14151 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14152
14153 stdio_file fp;
14154
ee0c3293 14155 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14156 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14157 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14158
6149aea9 14159 if (extra_trace_bits)
d7e74731 14160 save_trace_state_variables (&fp);
8bf6485c 14161
43892fdf 14162 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14163 {
14164 /* Skip internal and momentary breakpoints. */
14165 if (!user_breakpoint_p (tp))
14166 continue;
8bf6485c 14167
01add95b
SM
14168 /* If we have a filter, only save the breakpoints it accepts. */
14169 if (filter && !filter (tp))
14170 continue;
6149aea9 14171
c47614fe 14172 tp->print_recreate (&fp);
1042e4c0 14173
01add95b
SM
14174 /* Note, we can't rely on tp->number for anything, as we can't
14175 assume the recreated breakpoint numbers will match. Use $bpnum
14176 instead. */
6149aea9 14177
01add95b 14178 if (tp->cond_string)
6f781ee3 14179 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
6149aea9 14180
01add95b
SM
14181 if (tp->ignore_count)
14182 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14183
01add95b
SM
14184 if (tp->type != bp_dprintf && tp->commands)
14185 {
14186 fp.puts (" commands\n");
1042e4c0 14187
992aeed8
TT
14188 ui_out_redirect_pop redir (current_uiout, &fp);
14189 print_command_lines (current_uiout, tp->commands.get (), 2);
6149aea9 14190
01add95b
SM
14191 fp.puts (" end\n");
14192 }
6149aea9 14193
01add95b
SM
14194 if (tp->enable_state == bp_disabled)
14195 fp.puts ("disable $bpnum\n");
6149aea9 14196
01add95b
SM
14197 /* If this is a multi-location breakpoint, check if the locations
14198 should be individually disabled. Watchpoint locations are
14199 special, and not user visible. */
14200 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14201 {
14202 int n = 1;
40cb8ca5 14203
01add95b
SM
14204 for (bp_location *loc : tp->locations ())
14205 {
14206 if (!loc->enabled)
14207 fp.printf ("disable $bpnum.%d\n", n);
14208
14209 n++;
14210 }
14211 }
14212 }
8bf6485c 14213
e0700ba4
SM
14214 if (extra_trace_bits && !default_collect.empty ())
14215 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14216
1042e4c0 14217 if (from_tty)
6cb06a8c 14218 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14219}
14220
14221/* The `save breakpoints' command. */
14222
14223static void
4495129a 14224save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14225{
14226 save_breakpoints (args, from_tty, NULL);
14227}
14228
14229/* The `save tracepoints' command. */
14230
14231static void
4495129a 14232save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14233{
14234 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14235}
14236
c906108c 14237\f
629500fa
KS
14238/* This help string is used to consolidate all the help string for specifying
14239 locations used by several commands. */
14240
264f9890 14241#define LOCATION_SPEC_HELP_STRING \
629500fa
KS
14242"Linespecs are colon-separated lists of location parameters, such as\n\
14243source filename, function name, label name, and line number.\n\
14244Example: To specify the start of a label named \"the_top\" in the\n\
14245function \"fact\" in the file \"factorial.c\", use\n\
14246\"factorial.c:fact:the_top\".\n\
14247\n\
14248Address locations begin with \"*\" and specify an exact address in the\n\
14249program. Example: To specify the fourth byte past the start function\n\
14250\"main\", use \"*main + 4\".\n\
14251\n\
14252Explicit locations are similar to linespecs but use an option/argument\n\
14253syntax to specify location parameters.\n\
14254Example: To specify the start of the label named \"the_top\" in the\n\
14255function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14256-function fact -label the_top\".\n\
14257\n\
14258By default, a specified function is matched against the program's\n\
14259functions in all scopes. For C++, this means in all namespaces and\n\
14260classes. For Ada, this means in all packages. E.g., in C++,\n\
14261\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14262\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14263specified name as a complete fully-qualified name instead."
629500fa 14264
4a64f543
MS
14265/* This help string is used for the break, hbreak, tbreak and thbreak
14266 commands. It is defined as a macro to prevent duplication.
14267 COMMAND should be a string constant containing the name of the
14268 command. */
629500fa 14269
31e2b00f 14270#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14271command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14272\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14273PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14274probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
14275guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14276`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14277LOCATION may be a linespec, address, or explicit location as described\n\
14278below.\n\
14279\n\
dc10affe
PA
14280With no LOCATION, uses current execution address of the selected\n\
14281stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14282\n\
14283THREADNUM is the number from \"info threads\".\n\
14284CONDITION is a boolean expression.\n\
733d554a
TBA
14285\n\
14286With the \"-force-condition\" flag, the condition is defined even when\n\
14287it is invalid for all current locations.\n\
264f9890 14288\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14289Multiple breakpoints at one place are permitted, and useful if their\n\
14290conditions are different.\n\
31e2b00f
AS
14291\n\
14292Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14293
44feb3ce
TT
14294/* List of subcommands for "catch". */
14295static struct cmd_list_element *catch_cmdlist;
14296
14297/* List of subcommands for "tcatch". */
14298static struct cmd_list_element *tcatch_cmdlist;
14299
9ac4176b 14300void
a121b7c1 14301add_catch_command (const char *name, const char *docstring,
5538b03c 14302 cmd_func_ftype *func,
625e8578 14303 completer_ftype *completer,
44feb3ce
TT
14304 void *user_data_catch,
14305 void *user_data_tcatch)
14306{
14307 struct cmd_list_element *command;
14308
0450cc4c 14309 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14310 &catch_cmdlist);
5538b03c 14311 command->func = func;
0f8e2034 14312 command->set_context (user_data_catch);
a96d9b2e 14313 set_cmd_completer (command, completer);
44feb3ce 14314
0450cc4c 14315 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14316 &tcatch_cmdlist);
5538b03c 14317 command->func = func;
0f8e2034 14318 command->set_context (user_data_tcatch);
a96d9b2e 14319 set_cmd_completer (command, completer);
44feb3ce
TT
14320}
14321
d8de7963
AB
14322/* False if any of the breakpoint's locations could be a location where
14323 functions have been inlined, true otherwise. */
0574c78f 14324
d8de7963 14325static bool
0574c78f
GB
14326is_non_inline_function (struct breakpoint *b)
14327{
14328 /* The shared library event breakpoint is set on the address of a
14329 non-inline function. */
d8de7963 14330 return (b->type == bp_shlib_event);
0574c78f
GB
14331}
14332
14333/* Nonzero if the specified PC cannot be a location where functions
14334 have been inlined. */
14335
14336int
accd0bcd 14337pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14338 const target_waitstatus &ws)
0574c78f 14339{
43892fdf 14340 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
14341 {
14342 if (!is_non_inline_function (b))
14343 continue;
14344
40cb8ca5 14345 for (bp_location *bl : b->locations ())
0574c78f
GB
14346 {
14347 if (!bl->shlib_disabled
09ac7c10 14348 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14349 return 1;
14350 }
14351 }
14352
14353 return 0;
14354}
14355
2f202fde
JK
14356/* Remove any references to OBJFILE which is going to be freed. */
14357
14358void
14359breakpoint_free_objfile (struct objfile *objfile)
14360{
48d7020b 14361 for (bp_location *loc : all_bp_locations ())
3c86fae3 14362 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
2f202fde
JK
14363 loc->symtab = NULL;
14364}
14365
8bfd80db
YQ
14366/* Chain containing all defined "enable breakpoint" subcommands. */
14367
14368static struct cmd_list_element *enablebreaklist = NULL;
14369
8588b356
SM
14370/* See breakpoint.h. */
14371
14372cmd_list_element *commands_cmd_element = nullptr;
14373
6c265988 14374void _initialize_breakpoint ();
c906108c 14375void
6c265988 14376_initialize_breakpoint ()
c906108c
SS
14377{
14378 struct cmd_list_element *c;
14379
c90e7d63
SM
14380 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14381 "breakpoint");
14382 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14383 "breakpoint");
14384 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14385 "breakpoint");
84acb35a 14386
c906108c
SS
14387 breakpoint_chain = 0;
14388 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14389 before a breakpoint is set. */
14390 breakpoint_count = 0;
14391
1042e4c0
SS
14392 tracepoint_count = 0;
14393
1bedd215
AC
14394 add_com ("ignore", class_breakpoint, ignore_command, _("\
14395Set ignore-count of breakpoint number N to COUNT.\n\
14396Usage is `ignore N COUNT'."));
c906108c 14397
8588b356
SM
14398 commands_cmd_element = add_com ("commands", class_breakpoint,
14399 commands_command, _("\
18da0c51
MG
14400Set commands to be executed when the given breakpoints are hit.\n\
14401Give a space-separated breakpoint list as argument after \"commands\".\n\
14402A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14403(e.g. `5-7').\n\
c906108c
SS
14404With no argument, the targeted breakpoint is the last one set.\n\
14405The commands themselves follow starting on the next line.\n\
14406Type a line containing \"end\" to indicate the end of them.\n\
14407Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14408then no output is printed when it is hit, except what the commands print."));
c906108c 14409
b1d4d8d1
TBA
14410 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14411 static std::string condition_command_help
14412 = gdb::option::build_help (_("\
1bedd215 14413Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14414Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14415is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14416\n\
14417Options:\n\
14418%OPTIONS%"), cc_opts);
14419
14420 c = add_com ("condition", class_breakpoint, condition_command,
14421 condition_command_help.c_str ());
14422 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14423
1bedd215 14424 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14425Set a temporary breakpoint.\n\
c906108c
SS
14426Like \"break\" except the breakpoint is only temporary,\n\
14427so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14428by using \"enable delete\" on the breakpoint number.\n\
14429\n"
14430BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14431 set_cmd_completer (c, location_completer);
c94fdfd0 14432
1bedd215 14433 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14434Set a hardware assisted breakpoint.\n\
c906108c 14435Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14436some target hardware may not have this support.\n\
14437\n"
14438BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14439 set_cmd_completer (c, location_completer);
c906108c 14440
1bedd215 14441 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14442Set a temporary hardware assisted breakpoint.\n\
c906108c 14443Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14444so it will be deleted when hit.\n\
14445\n"
14446BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14447 set_cmd_completer (c, location_completer);
c906108c 14448
3947f654
SM
14449 cmd_list_element *enable_cmd
14450 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14451Enable all or some breakpoints.\n\
14452Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14453Give breakpoint numbers (separated by spaces) as arguments.\n\
14454With no subcommand, breakpoints are enabled until you command otherwise.\n\
14455This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14456With a subcommand you can enable temporarily."),
3947f654 14457 &enablelist, 1, &cmdlist);
c906108c 14458
3947f654 14459 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14460
84951ab5 14461 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14462Enable all or some breakpoints.\n\
14463Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14464Give breakpoint numbers (separated by spaces) as arguments.\n\
14465This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14466May be abbreviated to simply \"enable\"."),
2f822da5 14467 &enablebreaklist, 1, &enablelist);
c906108c 14468
1a966eab 14469 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14470Enable some breakpoints for one hit.\n\
14471Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14472If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14473 &enablebreaklist);
14474
1a966eab 14475 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14476Enable some breakpoints and delete when hit.\n\
14477Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14478If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14479 &enablebreaklist);
14480
816338b5 14481 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14482Enable some breakpoints for COUNT hits.\n\
14483Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14484If a breakpoint is hit while enabled in this fashion,\n\
14485the count is decremented; when it reaches zero, the breakpoint is disabled."),
14486 &enablebreaklist);
14487
1a966eab 14488 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14489Enable some breakpoints and delete when hit.\n\
14490Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14491If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14492 &enablelist);
14493
1a966eab 14494 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14495Enable some breakpoints for one hit.\n\
14496Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14497If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14498 &enablelist);
14499
14500 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14501Enable some breakpoints for COUNT hits.\n\
14502Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14503If a breakpoint is hit while enabled in this fashion,\n\
14504the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14505 &enablelist);
14506
3947f654
SM
14507 cmd_list_element *disable_cmd
14508 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14509Disable all or some breakpoints.\n\
14510Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14511Arguments are breakpoint numbers with spaces in between.\n\
14512To disable all breakpoints, give no argument.\n\
64b9b334 14513A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14514 &disablelist, 1, &cmdlist);
14515 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14516 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14517
57b4f16e 14518 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14519Disable all or some breakpoints.\n\
14520Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14521Arguments are breakpoint numbers with spaces in between.\n\
14522To disable all breakpoints, give no argument.\n\
64b9b334 14523A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14524This command may be abbreviated \"disable\"."),
c906108c
SS
14525 &disablelist);
14526
3947f654
SM
14527 cmd_list_element *delete_cmd
14528 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14529Delete all or some breakpoints.\n\
14530Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
14531Arguments are breakpoint numbers with spaces in between.\n\
14532To delete all breakpoints, give no argument.\n\
14533\n\
590042fc 14534Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14535 &deletelist, 1, &cmdlist);
14536 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14537 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14538
57b4f16e 14539 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14540Delete all or some breakpoints or auto-display expressions.\n\
14541Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14542Arguments are breakpoint numbers with spaces in between.\n\
14543To delete all breakpoints, give no argument.\n\
1a966eab 14544This command may be abbreviated \"delete\"."),
c906108c
SS
14545 &deletelist);
14546
3947f654
SM
14547 cmd_list_element *clear_cmd
14548 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14549Clear breakpoint at specified location.\n\
14550Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14551\n\
14552With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14553is executing in.\n"
264f9890 14554"\n" LOCATION_SPEC_HELP_STRING "\n\n\
1bedd215 14555See also the \"delete\" command which clears breakpoints by number."));
3947f654 14556 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14557
3947f654
SM
14558 cmd_list_element *break_cmd
14559 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14560Set breakpoint at specified location.\n"
31e2b00f 14561BREAK_ARGS_HELP ("break")));
3947f654 14562 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14563
3947f654
SM
14564 add_com_alias ("b", break_cmd, class_run, 1);
14565 add_com_alias ("br", break_cmd, class_run, 1);
14566 add_com_alias ("bre", break_cmd, class_run, 1);
14567 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14568
e0f25bd9
SM
14569 cmd_list_element *info_breakpoints_cmd
14570 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14571Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14572The \"Type\" column indicates one of:\n\
14573\tbreakpoint - normal breakpoint\n\
14574\twatchpoint - watchpoint\n\
14575The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14576the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14577breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14578address and file/line number respectively.\n\
14579\n\
14580Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14581are set to the address of the last breakpoint listed unless the command\n\
14582is prefixed with \"server \".\n\n\
c906108c 14583Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14584breakpoint set."));
c906108c 14585
e0f25bd9 14586 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14587
1a966eab
AC
14588 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14589Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14590The \"Type\" column indicates one of:\n\
14591\tbreakpoint - normal breakpoint\n\
14592\twatchpoint - watchpoint\n\
14593\tlongjmp - internal breakpoint used to step through longjmp()\n\
14594\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14595\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14596\tfinish - internal breakpoint used by the \"finish\" command\n\
14597The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14598the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14599breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14600address and file/line number respectively.\n\
14601\n\
14602Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14603are set to the address of the last breakpoint listed unless the command\n\
14604is prefixed with \"server \".\n\n\
c906108c 14605Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14606breakpoint set."),
c906108c
SS
14607 &maintenanceinfolist);
14608
3b6acaee 14609 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 14610Set catchpoints to catch events."),
2f822da5 14611 &catch_cmdlist,
3b6acaee 14612 0/*allow-unknown*/, &cmdlist);
44feb3ce 14613
3b6acaee 14614 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 14615Set temporary catchpoints to catch events."),
2f822da5 14616 &tcatch_cmdlist,
3b6acaee 14617 0/*allow-unknown*/, &cmdlist);
44feb3ce 14618
a15a5258
AB
14619 const auto opts = make_watch_options_def_group (nullptr);
14620
14621 static const std::string watch_help = gdb::option::build_help (_("\
14622Set a watchpoint for EXPRESSION.\n\
14623Usage: watch [-location] EXPRESSION\n\
14624\n\
14625Options:\n\
14626%OPTIONS%\n\
14627\n\
c906108c 14628A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
14629an expression changes."), opts);
14630 c = add_com ("watch", class_breakpoint, watch_command,
14631 watch_help.c_str ());
14632 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14633
14634 static const std::string rwatch_help = gdb::option::build_help (_("\
14635Set a read watchpoint for EXPRESSION.\n\
14636Usage: rwatch [-location] EXPRESSION\n\
14637\n\
14638Options:\n\
14639%OPTIONS%\n\
14640\n\
14641A read watchpoint stops execution of your program whenever the value of\n\
14642an expression is read."), opts);
14643 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14644 rwatch_help.c_str ());
14645 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14646
14647 static const std::string awatch_help = gdb::option::build_help (_("\
14648Set an access watchpoint for EXPRESSION.\n\
14649Usage: awatch [-location] EXPRESSION\n\
14650\n\
14651Options:\n\
14652%OPTIONS%\n\
14653\n\
14654An access watchpoint stops execution of your program whenever the value\n\
14655of an expression is either read or written."), opts);
14656 c = add_com ("awatch", class_breakpoint, awatch_command,
14657 awatch_help.c_str ());
14658 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 14659
11db9430 14660 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 14661Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14662
920d2a44
AC
14663 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14664 respond to changes - contrary to the description. */
85c07804
AC
14665 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14666 &can_use_hw_watchpoints, _("\
14667Set debugger's willingness to use watchpoint hardware."), _("\
14668Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14669If zero, gdb will not use hardware for new watchpoints, even if\n\
14670such is available. (However, any hardware watchpoints that were\n\
14671created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14672hardware.)"),
14673 NULL,
920d2a44 14674 show_can_use_hw_watchpoints,
85c07804 14675 &setlist, &showlist);
c906108c
SS
14676
14677 can_use_hw_watchpoints = 1;
fa8d40ab 14678
1042e4c0
SS
14679 /* Tracepoint manipulation commands. */
14680
3947f654
SM
14681 cmd_list_element *trace_cmd
14682 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 14683Set a tracepoint at specified location.\n\
1042e4c0
SS
14684\n"
14685BREAK_ARGS_HELP ("trace") "\n\
14686Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 14687 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 14688
3947f654
SM
14689 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14690 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14691 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14692 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 14693
7a697b8d 14694 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 14695Set a fast tracepoint at specified location.\n\
7a697b8d
SS
14696\n"
14697BREAK_ARGS_HELP ("ftrace") "\n\
14698Do \"help tracepoints\" for info on other tracepoint commands."));
14699 set_cmd_completer (c, location_completer);
14700
0fb4aa4b 14701 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 14702Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
14703\n\
14704strace [LOCATION] [if CONDITION]\n\
629500fa
KS
14705LOCATION may be a linespec, explicit, or address location (described below) \n\
14706or -m MARKER_ID.\n\n\
14707If a marker id is specified, probe the marker with that name. With\n\
14708no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
14709Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14710This collects arbitrary user data passed in the probe point call to the\n\
14711tracing library. You can inspect it when analyzing the trace buffer,\n\
14712by printing the $_sdata variable like any other convenience variable.\n\
14713\n\
14714CONDITION is a boolean expression.\n\
264f9890 14715\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14716Multiple tracepoints at one place are permitted, and useful if their\n\
14717conditions are different.\n\
0fb4aa4b
PA
14718\n\
14719Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14720Do \"help tracepoints\" for info on other tracepoint commands."));
14721 set_cmd_completer (c, location_completer);
14722
e0f25bd9
SM
14723 cmd_list_element *info_tracepoints_cmd
14724 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 14725Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14726Convenience variable \"$tpnum\" contains the number of the\n\
14727last tracepoint set."));
14728
e0f25bd9 14729 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 14730
5e84b7ee
SM
14731 cmd_list_element *delete_tracepoints_cmd
14732 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
14733Delete specified tracepoints.\n\
14734Arguments are tracepoint numbers, separated by spaces.\n\
14735No argument means delete all tracepoints."),
14736 &deletelist);
5e84b7ee 14737 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
14738
14739 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14740Disable specified tracepoints.\n\
14741Arguments are tracepoint numbers, separated by spaces.\n\
14742No argument means disable all tracepoints."),
14743 &disablelist);
14744 deprecate_cmd (c, "disable");
14745
14746 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14747Enable specified tracepoints.\n\
14748Arguments are tracepoint numbers, separated by spaces.\n\
14749No argument means enable all tracepoints."),
14750 &enablelist);
14751 deprecate_cmd (c, "enable");
14752
14753 add_com ("passcount", class_trace, trace_pass_command, _("\
14754Set the passcount for a tracepoint.\n\
14755The trace will end when the tracepoint has been passed 'count' times.\n\
14756Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14757if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14758
0743fc83
TT
14759 add_basic_prefix_cmd ("save", class_breakpoint,
14760 _("Save breakpoint definitions as a script."),
2f822da5 14761 &save_cmdlist,
0743fc83 14762 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
14763
14764 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14765Save current breakpoint definitions as a script.\n\
cce7e648 14766This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14767catchpoints, tracepoints). Use the 'source' command in another debug\n\
14768session to restore them."),
14769 &save_cmdlist);
14770 set_cmd_completer (c, filename_completer);
14771
3947f654
SM
14772 cmd_list_element *save_tracepoints_cmd
14773 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14774Save current tracepoint definitions as a script.\n\
6149aea9
PA
14775Use the 'source' command in another debug session to restore them."),
14776 &save_cmdlist);
3947f654 14777 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 14778
3947f654 14779 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
14780 deprecate_cmd (c, "save tracepoints");
14781
f54bdb6d
SM
14782 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14783 _("\
590042fc 14784Breakpoint specific settings.\n\
fa8d40ab 14785Configure various breakpoint-specific variables such as\n\
590042fc 14786pending breakpoint behavior."),
f54bdb6d 14787 _("\
590042fc 14788Breakpoint specific settings.\n\
fa8d40ab 14789Configure various breakpoint-specific variables such as\n\
590042fc 14790pending breakpoint behavior."),
f54bdb6d
SM
14791 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14792 &setlist, &showlist);
fa8d40ab 14793
7915a72c
AC
14794 add_setshow_auto_boolean_cmd ("pending", no_class,
14795 &pending_break_support, _("\
14796Set debugger's behavior regarding pending breakpoints."), _("\
14797Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14798If on, an unrecognized breakpoint location will cause gdb to create a\n\
14799pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14800an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14801user-query to see if a pending breakpoint should be created."),
2c5b56ce 14802 NULL,
920d2a44 14803 show_pending_break_support,
6e1d7d6c
AC
14804 &breakpoint_set_cmdlist,
14805 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14806
14807 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14808
14809 add_setshow_boolean_cmd ("auto-hw", no_class,
14810 &automatic_hardware_breakpoints, _("\
14811Set automatic usage of hardware breakpoints."), _("\
14812Show automatic usage of hardware breakpoints."), _("\
14813If set, the debugger will automatically use hardware breakpoints for\n\
14814breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14815a warning will be emitted for such breakpoints."),
14816 NULL,
14817 show_automatic_hardware_breakpoints,
14818 &breakpoint_set_cmdlist,
14819 &breakpoint_show_cmdlist);
74960c60 14820
a25a5a45
PA
14821 add_setshow_boolean_cmd ("always-inserted", class_support,
14822 &always_inserted_mode, _("\
74960c60
VP
14823Set mode for inserting breakpoints."), _("\
14824Show mode for inserting breakpoints."), _("\
a25a5a45
PA
14825When this mode is on, breakpoints are inserted immediately as soon as\n\
14826they're created, kept inserted even when execution stops, and removed\n\
14827only when the user deletes them. When this mode is off (the default),\n\
14828breakpoints are inserted only when execution continues, and removed\n\
14829when execution stops."),
72d0e2c5
YQ
14830 NULL,
14831 &show_always_inserted_mode,
14832 &breakpoint_set_cmdlist,
14833 &breakpoint_show_cmdlist);
f1310107 14834
b775012e
LM
14835 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14836 condition_evaluation_enums,
14837 &condition_evaluation_mode_1, _("\
14838Set mode of breakpoint condition evaluation."), _("\
14839Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 14840When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
14841evaluated on the host's side by GDB. When it is set to \"target\",\n\
14842breakpoint conditions will be downloaded to the target (if the target\n\
14843supports such feature) and conditions will be evaluated on the target's side.\n\
14844If this is set to \"auto\" (default), this will be automatically set to\n\
14845\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 14846be set to \"host\"."),
b775012e
LM
14847 &set_condition_evaluation_mode,
14848 &show_condition_evaluation_mode,
14849 &breakpoint_set_cmdlist,
14850 &breakpoint_show_cmdlist);
14851
f1310107
TJB
14852 add_com ("break-range", class_breakpoint, break_range_command, _("\
14853Set a breakpoint for an address range.\n\
14854break-range START-LOCATION, END-LOCATION\n\
14855where START-LOCATION and END-LOCATION can be one of the following:\n\
14856 LINENUM, for that line in the current file,\n\
14857 FILE:LINENUM, for that line in that file,\n\
14858 +OFFSET, for that number of lines after the current line\n\
dda83cd7 14859 or the start of the range\n\
f1310107
TJB
14860 FUNCTION, for the first line in that function,\n\
14861 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14862 *ADDRESS, for the instruction at that address.\n\
14863\n\
14864The breakpoint will stop execution of the inferior whenever it executes\n\
14865an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14866range (including START-LOCATION and END-LOCATION)."));
14867
e7e0cddf 14868 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 14869Set a dynamic printf at specified location.\n\
e7e0cddf 14870dprintf location,format string,arg1,arg2,...\n\
629500fa 14871location may be a linespec, explicit, or address location.\n"
264f9890 14872"\n" LOCATION_SPEC_HELP_STRING));
e7e0cddf
SS
14873 set_cmd_completer (c, location_completer);
14874
14875 add_setshow_enum_cmd ("dprintf-style", class_support,
14876 dprintf_style_enums, &dprintf_style, _("\
14877Set the style of usage for dynamic printf."), _("\
14878Show the style of usage for dynamic printf."), _("\
14879This setting chooses how GDB will do a dynamic printf.\n\
14880If the value is \"gdb\", then the printing is done by GDB to its own\n\
14881console, as with the \"printf\" command.\n\
14882If the value is \"call\", the print is done by calling a function in your\n\
14883program; by default printf(), but you can choose a different function or\n\
14884output stream by setting dprintf-function and dprintf-channel."),
14885 update_dprintf_commands, NULL,
14886 &setlist, &showlist);
14887
e7e0cddf
SS
14888 add_setshow_string_cmd ("dprintf-function", class_support,
14889 &dprintf_function, _("\
590042fc
PW
14890Set the function to use for dynamic printf."), _("\
14891Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
14892 update_dprintf_commands, NULL,
14893 &setlist, &showlist);
14894
e7e0cddf
SS
14895 add_setshow_string_cmd ("dprintf-channel", class_support,
14896 &dprintf_channel, _("\
590042fc
PW
14897Set the channel to use for dynamic printf."), _("\
14898Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
14899 update_dprintf_commands, NULL,
14900 &setlist, &showlist);
14901
d3ce09f5
SS
14902 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14903 &disconnected_dprintf, _("\
14904Set whether dprintf continues after GDB disconnects."), _("\
14905Show whether dprintf continues after GDB disconnects."), _("\
14906Use this to let dprintf commands continue to hit and produce output\n\
14907even if GDB disconnects or detaches from the target."),
14908 NULL,
14909 NULL,
14910 &setlist, &showlist);
14911
14912 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
14913Target agent only formatted printing, like the C \"printf\" function.\n\
14914Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14915This supports most C printf format specifications, like %s, %d, etc.\n\
14916This is useful for formatted output in user-defined commands."));
d3ce09f5 14917
491144b5 14918 automatic_hardware_breakpoints = true;
f3b1572e 14919
c90e7d63
SM
14920 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14921 "breakpoint");
14922 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14923 "breakpoint");
c906108c 14924}