]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Turn value_bitsize into method
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
213516ef 3 Copyright (C) 1986-2023 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
0a9ccb9d 1457/* See breakpoint.h. */
45a43567
TT
1458
1459void
1460breakpoint_set_thread (struct breakpoint *b, int thread)
1461{
0a9ccb9d
AB
1462 /* It is invalid to set the thread field to anything other than -1 (which
1463 means no thread restriction) if a task restriction is already in
1464 place. */
2ecee236 1465 gdb_assert (thread == -1 || b->task == -1);
0a9ccb9d 1466
45a43567
TT
1467 int old_thread = b->thread;
1468
1469 b->thread = thread;
1470 if (old_thread != thread)
76727919 1471 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1472}
1473
0a9ccb9d 1474/* See breakpoint.h. */
45a43567
TT
1475
1476void
1477breakpoint_set_task (struct breakpoint *b, int task)
1478{
2ecee236 1479 /* It is invalid to set the task field to anything other than -1 (which
0a9ccb9d
AB
1480 means no task restriction) if a thread restriction is already in
1481 place. */
2ecee236 1482 gdb_assert (task == -1 || b->thread == -1);
0a9ccb9d 1483
45a43567
TT
1484 int old_task = b->task;
1485
1486 b->task = task;
1487 if (old_task != task)
76727919 1488 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1489}
1490
95a42b64 1491static void
896b6bda 1492commands_command_1 (const char *arg, int from_tty,
4a64f543 1493 struct command_line *control)
95a42b64 1494{
d1b0a7bf 1495 counted_command_line cmd;
999700cd
PW
1496 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1497 NULL after the call to read_command_lines if the user provides an empty
1498 list of command by just typing "end". */
1499 bool cmd_read = false;
95a42b64 1500
896b6bda
PA
1501 std::string new_arg;
1502
95a42b64
TT
1503 if (arg == NULL || !*arg)
1504 {
da1df1db 1505 /* Argument not explicitly given. Synthesize it. */
86b17b60 1506 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1507 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1508 breakpoint_count);
95a42b64 1509 else if (breakpoint_count > 0)
896b6bda 1510 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1511 }
da1df1db
TBA
1512 else
1513 {
1514 /* Create a copy of ARG. This is needed because the "commands"
1515 command may be coming from a script. In that case, the read
1516 line buffer is going to be overwritten in the lambda of
1517 'map_breakpoint_numbers' below when reading the next line
1518 before we are are done parsing the breakpoint numbers. */
1519 new_arg = arg;
1520 }
1521 arg = new_arg.c_str ();
48649e1b
TT
1522
1523 map_breakpoint_numbers
1524 (arg, [&] (breakpoint *b)
1525 {
999700cd 1526 if (!cmd_read)
48649e1b 1527 {
999700cd 1528 gdb_assert (cmd == NULL);
48649e1b 1529 if (control != NULL)
12973681 1530 cmd = control->body_list_0;
48649e1b
TT
1531 else
1532 {
81b1e71c
TT
1533 std::string str
1534 = string_printf (_("Type commands for breakpoint(s) "
1535 "%s, one per line."),
1536 arg);
48649e1b 1537
60b3cef2
TT
1538 auto do_validate = [=] (const char *line)
1539 {
1540 validate_actionline (line, b);
1541 };
1542 gdb::function_view<void (const char *)> validator;
1543 if (is_tracepoint (b))
1544 validator = do_validate;
1545
1546 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1547 }
999700cd 1548 cmd_read = true;
48649e1b
TT
1549 }
1550
1551 /* If a breakpoint was on the list more than once, we don't need to
1552 do anything. */
1553 if (b->commands != cmd)
1554 {
d1b0a7bf 1555 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1556 b->commands = cmd;
76727919 1557 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1558 }
1559 });
95a42b64
TT
1560}
1561
1562static void
0b39b52e 1563commands_command (const char *arg, int from_tty)
95a42b64
TT
1564{
1565 commands_command_1 (arg, from_tty, NULL);
c906108c 1566}
40c03ae8
EZ
1567
1568/* Like commands_command, but instead of reading the commands from
1569 input stream, takes them from an already parsed command structure.
1570
1571 This is used by cli-script.c to DTRT with breakpoint commands
1572 that are part of if and while bodies. */
1573enum command_control_type
896b6bda 1574commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1575{
95a42b64
TT
1576 commands_command_1 (arg, 0, cmd);
1577 return simple_control;
40c03ae8 1578}
876fa593 1579
d8de7963 1580/* Return true if BL->TARGET_INFO contains valid information. */
876fa593 1581
d8de7963 1582static bool
876fa593
JK
1583bp_location_has_shadow (struct bp_location *bl)
1584{
1585 if (bl->loc_type != bp_loc_software_breakpoint)
d8de7963 1586 return false;
876fa593 1587 if (!bl->inserted)
d8de7963 1588 return false;
876fa593 1589 if (bl->target_info.shadow_len == 0)
e5dd4106 1590 /* BL isn't valid, or doesn't shadow memory. */
d8de7963
AB
1591 return false;
1592 return true;
876fa593
JK
1593}
1594
9d497a19
PA
1595/* Update BUF, which is LEN bytes read from the target address
1596 MEMADDR, by replacing a memory breakpoint with its shadowed
1597 contents.
1598
1599 If READBUF is not NULL, this buffer must not overlap with the of
1600 the breakpoint location's shadow_contents buffer. Otherwise, a
1601 failed assertion internal error will be raised. */
1602
1603static void
1604one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1605 const gdb_byte *writebuf_org,
1606 ULONGEST memaddr, LONGEST len,
1607 struct bp_target_info *target_info,
1608 struct gdbarch *gdbarch)
1609{
1610 /* Now do full processing of the found relevant range of elements. */
1611 CORE_ADDR bp_addr = 0;
1612 int bp_size = 0;
1613 int bptoffset = 0;
1614
1615 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1616 current_program_space->aspace, 0))
1617 {
1618 /* The breakpoint is inserted in a different address space. */
1619 return;
1620 }
1621
1622 /* Addresses and length of the part of the breakpoint that
1623 we need to copy. */
1624 bp_addr = target_info->placed_address;
1625 bp_size = target_info->shadow_len;
1626
1627 if (bp_addr + bp_size <= memaddr)
1628 {
1629 /* The breakpoint is entirely before the chunk of memory we are
1630 reading. */
1631 return;
1632 }
1633
1634 if (bp_addr >= memaddr + len)
1635 {
1636 /* The breakpoint is entirely after the chunk of memory we are
1637 reading. */
1638 return;
1639 }
1640
1641 /* Offset within shadow_contents. */
1642 if (bp_addr < memaddr)
1643 {
1644 /* Only copy the second part of the breakpoint. */
1645 bp_size -= memaddr - bp_addr;
1646 bptoffset = memaddr - bp_addr;
1647 bp_addr = memaddr;
1648 }
1649
1650 if (bp_addr + bp_size > memaddr + len)
1651 {
1652 /* Only copy the first part of the breakpoint. */
1653 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1654 }
1655
1656 if (readbuf != NULL)
1657 {
1658 /* Verify that the readbuf buffer does not overlap with the
1659 shadow_contents buffer. */
1660 gdb_assert (target_info->shadow_contents >= readbuf + len
1661 || readbuf >= (target_info->shadow_contents
1662 + target_info->shadow_len));
1663
1664 /* Update the read buffer with this inserted breakpoint's
1665 shadow. */
1666 memcpy (readbuf + bp_addr - memaddr,
1667 target_info->shadow_contents + bptoffset, bp_size);
1668 }
1669 else
1670 {
1671 const unsigned char *bp;
0d5ed153
MR
1672 CORE_ADDR addr = target_info->reqstd_address;
1673 int placed_size;
9d497a19
PA
1674
1675 /* Update the shadow with what we want to write to memory. */
1676 memcpy (target_info->shadow_contents + bptoffset,
1677 writebuf_org + bp_addr - memaddr, bp_size);
1678
1679 /* Determine appropriate breakpoint contents and size for this
1680 address. */
0d5ed153 1681 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1682
1683 /* Update the final write buffer with this inserted
1684 breakpoint's INSN. */
1685 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1686 }
1687}
1688
8defab1a 1689/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1690 by replacing any memory breakpoints with their shadowed contents.
1691
35c63cd8
JB
1692 If READBUF is not NULL, this buffer must not overlap with any of
1693 the breakpoint location's shadow_contents buffers. Otherwise,
1694 a failed assertion internal error will be raised.
1695
876fa593 1696 The range of shadowed area by each bp_location is:
f5336ca5
PA
1697 bl->address - bp_locations_placed_address_before_address_max
1698 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1699 The range we were requested to resolve shadows for is:
1700 memaddr ... memaddr + len
1701 Thus the safe cutoff boundaries for performance optimization are
35df4500 1702 memaddr + len <= (bl->address
f5336ca5 1703 - bp_locations_placed_address_before_address_max)
876fa593 1704 and:
f5336ca5 1705 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1706
8defab1a 1707void
f0ba3972
PA
1708breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1709 const gdb_byte *writebuf_org,
1710 ULONGEST memaddr, LONGEST len)
c906108c 1711{
4a64f543
MS
1712 /* Left boundary, right boundary and median element of our binary
1713 search. */
876fa593
JK
1714 unsigned bc_l, bc_r, bc;
1715
4a64f543
MS
1716 /* Find BC_L which is a leftmost element which may affect BUF
1717 content. It is safe to report lower value but a failure to
1718 report higher one. */
876fa593
JK
1719
1720 bc_l = 0;
5d51cd5d 1721 bc_r = bp_locations.size ();
876fa593
JK
1722 while (bc_l + 1 < bc_r)
1723 {
35df4500 1724 struct bp_location *bl;
876fa593
JK
1725
1726 bc = (bc_l + bc_r) / 2;
f5336ca5 1727 bl = bp_locations[bc];
876fa593 1728
4a64f543
MS
1729 /* Check first BL->ADDRESS will not overflow due to the added
1730 constant. Then advance the left boundary only if we are sure
1731 the BC element can in no way affect the BUF content (MEMADDR
1732 to MEMADDR + LEN range).
876fa593 1733
f5336ca5 1734 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1735 offset so that we cannot miss a breakpoint with its shadow
1736 range tail still reaching MEMADDR. */
c5aa993b 1737
f5336ca5 1738 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1739 >= bl->address)
f5336ca5 1740 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1741 <= memaddr))
876fa593
JK
1742 bc_l = bc;
1743 else
1744 bc_r = bc;
1745 }
1746
128070bb
PA
1747 /* Due to the binary search above, we need to make sure we pick the
1748 first location that's at BC_L's address. E.g., if there are
1749 multiple locations at the same address, BC_L may end up pointing
1750 at a duplicate location, and miss the "master"/"inserted"
1751 location. Say, given locations L1, L2 and L3 at addresses A and
1752 B:
1753
1754 L1@A, L2@A, L3@B, ...
1755
1756 BC_L could end up pointing at location L2, while the "master"
1757 location could be L1. Since the `loc->inserted' flag is only set
1758 on "master" locations, we'd forget to restore the shadow of L1
1759 and L2. */
1760 while (bc_l > 0
f5336ca5 1761 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1762 bc_l--;
1763
876fa593
JK
1764 /* Now do full processing of the found relevant range of elements. */
1765
5d51cd5d 1766 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1767 {
1768 struct bp_location *bl = bp_locations[bc];
876fa593 1769
01add95b
SM
1770 /* bp_location array has BL->OWNER always non-NULL. */
1771 if (bl->owner->type == bp_none)
1772 warning (_("reading through apparently deleted breakpoint #%d?"),
1773 bl->owner->number);
ffce0d52 1774
01add95b
SM
1775 /* Performance optimization: any further element can no longer affect BUF
1776 content. */
876fa593 1777
01add95b
SM
1778 if (bl->address >= bp_locations_placed_address_before_address_max
1779 && (memaddr + len
1780 <= (bl->address
1781 - bp_locations_placed_address_before_address_max)))
1782 break;
876fa593 1783
01add95b
SM
1784 if (!bp_location_has_shadow (bl))
1785 continue;
6c95b8df 1786
01add95b
SM
1787 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1788 memaddr, len, &bl->target_info, bl->gdbarch);
1789 }
c906108c 1790}
9d497a19 1791
f2478a7e 1792/* See breakpoint.h. */
b775012e 1793
f2478a7e 1794bool
b775012e
LM
1795is_breakpoint (const struct breakpoint *bpt)
1796{
1797 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1798 || bpt->type == bp_hardware_breakpoint
1799 || bpt->type == bp_dprintf);
b775012e
LM
1800}
1801
60e1c644
PA
1802/* Return true if BPT is of any hardware watchpoint kind. */
1803
f2478a7e 1804static bool
d77f58be 1805is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1806{
1807 return (bpt->type == bp_hardware_watchpoint
1808 || bpt->type == bp_read_watchpoint
1809 || bpt->type == bp_access_watchpoint);
1810}
7270d8f2 1811
f2478a7e 1812/* See breakpoint.h. */
60e1c644 1813
f2478a7e 1814bool
d77f58be 1815is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1816{
1817 return (is_hardware_watchpoint (bpt)
1818 || bpt->type == bp_watchpoint);
1819}
1820
3a5c3e22
PA
1821/* Returns true if the current thread and its running state are safe
1822 to evaluate or update watchpoint B. Watchpoints on local
1823 expressions need to be evaluated in the context of the thread that
1824 was current when the watchpoint was created, and, that thread needs
1825 to be stopped to be able to select the correct frame context.
1826 Watchpoints on global expressions can be evaluated on any thread,
1827 and in any state. It is presently left to the target allowing
1828 memory accesses when threads are running. */
f6bc2008 1829
d8de7963 1830static bool
3a5c3e22 1831watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1832{
c1fc2657 1833 return (b->pspace == current_program_space
d7e15655
TT
1834 && (b->watchpoint_thread == null_ptid
1835 || (inferior_ptid == b->watchpoint_thread
611841bb 1836 && !inferior_thread ()->executing ())));
f6bc2008
PA
1837}
1838
d0fb5eae
JK
1839/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1840 associated bp_watchpoint_scope breakpoint. */
1841
1842static void
3a5c3e22 1843watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1844{
c1fc2657 1845 if (w->related_breakpoint != w)
d0fb5eae 1846 {
c1fc2657
SM
1847 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1848 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1849 w->related_breakpoint->disposition = disp_del_at_next_stop;
1850 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1851 w->related_breakpoint = w;
d0fb5eae 1852 }
c1fc2657 1853 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1854}
1855
bb9d5f81
PP
1856/* Extract a bitfield value from value VAL using the bit parameters contained in
1857 watchpoint W. */
1858
1859static struct value *
1860extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1861{
1862 struct value *bit_val;
1863
1864 if (val == NULL)
1865 return NULL;
1866
d0c97917 1867 bit_val = allocate_value (val->type ());
bb9d5f81
PP
1868
1869 unpack_value_bitfield (bit_val,
1870 w->val_bitpos,
1871 w->val_bitsize,
50888e42 1872 value_contents_for_printing (val).data (),
bb9d5f81
PP
1873 value_offset (val),
1874 val);
1875
1876 return bit_val;
1877}
1878
6e14e441
PA
1879/* Allocate a dummy location and add it to B. This is required
1880 because bpstat_stop_status requires a location to be able to report
1881 stops. */
c6d81124
PA
1882
1883static void
6e14e441
PA
1884add_dummy_location (struct breakpoint *b,
1885 struct program_space *pspace)
c6d81124 1886{
6e14e441 1887 gdb_assert (b->loc == NULL);
c6d81124 1888
6e14e441 1889 b->loc = new bp_location (b, bp_loc_other);
c6d81124 1890 b->loc->pspace = pspace;
c6d81124
PA
1891}
1892
567e1b4e 1893/* Assuming that B is a watchpoint:
d8de7963 1894 - Reparse watchpoint expression, if REPARSE is true
a5606eee 1895 - Evaluate expression and store the result in B->val
567e1b4e
JB
1896 - Evaluate the condition if there is one, and store the result
1897 in b->loc->cond.
a5606eee
VP
1898 - Update the list of values that must be watched in B->loc.
1899
4a64f543
MS
1900 If the watchpoint disposition is disp_del_at_next_stop, then do
1901 nothing. If this is local watchpoint that is out of scope, delete
1902 it.
1903
1904 Even with `set breakpoint always-inserted on' the watchpoints are
1905 removed + inserted on each stop here. Normal breakpoints must
1906 never be removed because they might be missed by a running thread
1907 when debugging in non-stop mode. On the other hand, hardware
1908 watchpoints (is_hardware_watchpoint; processed here) are specific
1909 to each LWP since they are stored in each LWP's hardware debug
1910 registers. Therefore, such LWP must be stopped first in order to
1911 be able to modify its hardware watchpoints.
1912
1913 Hardware watchpoints must be reset exactly once after being
1914 presented to the user. It cannot be done sooner, because it would
1915 reset the data used to present the watchpoint hit to the user. And
1916 it must not be done later because it could display the same single
1917 watchpoint hit during multiple GDB stops. Note that the latter is
1918 relevant only to the hardware watchpoint types bp_read_watchpoint
1919 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1920 not user-visible - its hit is suppressed if the memory content has
1921 not changed.
1922
1923 The following constraints influence the location where we can reset
1924 hardware watchpoints:
1925
1926 * target_stopped_by_watchpoint and target_stopped_data_address are
1927 called several times when GDB stops.
1928
1929 [linux]
1930 * Multiple hardware watchpoints can be hit at the same time,
1931 causing GDB to stop. GDB only presents one hardware watchpoint
1932 hit at a time as the reason for stopping, and all the other hits
1933 are presented later, one after the other, each time the user
1934 requests the execution to be resumed. Execution is not resumed
1935 for the threads still having pending hit event stored in
1936 LWP_INFO->STATUS. While the watchpoint is already removed from
1937 the inferior on the first stop the thread hit event is kept being
1938 reported from its cached value by linux_nat_stopped_data_address
1939 until the real thread resume happens after the watchpoint gets
1940 presented and thus its LWP_INFO->STATUS gets reset.
1941
1942 Therefore the hardware watchpoint hit can get safely reset on the
1943 watchpoint removal from inferior. */
a79d3c27 1944
b40ce68a 1945static void
d8de7963 1946update_watchpoint (struct watchpoint *b, bool reparse)
7270d8f2 1947{
705b6305 1948 bool within_current_scope;
a5606eee 1949
f6bc2008
PA
1950 /* If this is a local watchpoint, we only want to check if the
1951 watchpoint frame is in scope if the current thread is the thread
1952 that was used to create the watchpoint. */
1953 if (!watchpoint_in_thread_scope (b))
1954 return;
1955
c1fc2657 1956 if (b->disposition == disp_del_at_next_stop)
a5606eee 1957 return;
705b6305
AB
1958
1959 gdb::optional<scoped_restore_selected_frame> restore_frame;
a5606eee
VP
1960
1961 /* Determine if the watchpoint is within scope. */
1962 if (b->exp_valid_block == NULL)
705b6305 1963 within_current_scope = true;
a5606eee
VP
1964 else
1965 {
bd2b40ac 1966 frame_info_ptr fi = get_current_frame ();
b5db5dfc
UW
1967 struct gdbarch *frame_arch = get_frame_arch (fi);
1968 CORE_ADDR frame_pc = get_frame_pc (fi);
1969
c9cf6e20
MG
1970 /* If we're at a point where the stack has been destroyed
1971 (e.g. in a function epilogue), unwinding may not work
1972 properly. Do not attempt to recreate locations at this
b5db5dfc 1973 point. See similar comments in watchpoint_check. */
c9cf6e20 1974 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1975 return;
66076460
DJ
1976
1977 /* Save the current frame's ID so we can restore it after
dda83cd7 1978 evaluating the watchpoint expression on its own frame. */
66076460 1979 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1980 took a frame parameter, so that we didn't have to change the
1981 selected frame. */
705b6305 1982 restore_frame.emplace ();
66076460 1983
a5606eee
VP
1984 fi = frame_find_by_id (b->watchpoint_frame);
1985 within_current_scope = (fi != NULL);
1986 if (within_current_scope)
1987 select_frame (fi);
1988 }
1989
b5db5dfc
UW
1990 /* We don't free locations. They are stored in the bp_location array
1991 and update_global_location_list will eventually delete them and
1992 remove breakpoints if needed. */
c1fc2657 1993 b->loc = NULL;
b5db5dfc 1994
a5606eee
VP
1995 if (within_current_scope && reparse)
1996 {
bbc13ae3 1997 const char *s;
d63d0675 1998
4d01a485 1999 b->exp.reset ();
a4c50be3
TT
2000 s = (b->exp_string_reparse
2001 ? b->exp_string_reparse.get ()
2002 : b->exp_string.get ());
1bb9788d 2003 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
2004 /* If the meaning of expression itself changed, the old value is
2005 no longer relevant. We don't want to report a watchpoint hit
2006 to the user when the old value and the new value may actually
2007 be completely different objects. */
fa4727a6 2008 b->val = NULL;
4c1d86d9 2009 b->val_valid = false;
60e1c644
PA
2010
2011 /* Note that unlike with breakpoints, the watchpoint's condition
2012 expression is stored in the breakpoint object, not in the
2013 locations (re)created below. */
c1fc2657 2014 if (b->cond_string != NULL)
60e1c644 2015 {
4d01a485 2016 b->cond_exp.reset ();
60e1c644 2017
6f781ee3 2018 s = b->cond_string.get ();
1bb9788d 2019 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 2020 }
a5606eee 2021 }
a5606eee
VP
2022
2023 /* If we failed to parse the expression, for example because
2024 it refers to a global variable in a not-yet-loaded shared library,
2025 don't try to insert watchpoint. We don't automatically delete
2026 such watchpoint, though, since failure to parse expression
2027 is different from out-of-scope watchpoint. */
55f6301a 2028 if (!target_has_execution ())
2d134ed3
PA
2029 {
2030 /* Without execution, memory can't change. No use to try and
2031 set watchpoint locations. The watchpoint will be reset when
2032 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
2033 if (!can_use_hw_watchpoints)
2034 {
c47614fe 2035 if (b->works_in_software_mode ())
c1fc2657 2036 b->type = bp_watchpoint;
e8369a73 2037 else
638aa5a1
AB
2038 error (_("Can't set read/access watchpoint when "
2039 "hardware watchpoints are disabled."));
e8369a73 2040 }
2d134ed3
PA
2041 }
2042 else if (within_current_scope && b->exp)
a5606eee 2043 {
a6535de1 2044 std::vector<value_ref_ptr> val_chain;
8d49165d 2045 struct value *v, *result;
2d134ed3 2046 struct program_space *frame_pspace;
a5606eee 2047
1eaebe02 2048 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 2049 &val_chain, false);
a5606eee 2050
a5606eee
VP
2051 /* Avoid setting b->val if it's already set. The meaning of
2052 b->val is 'the last value' user saw, and we should update
2053 it only if we reported that last value to user. As it
9c06b0b4
TJB
2054 happens, the code that reports it updates b->val directly.
2055 We don't keep track of the memory value for masked
2056 watchpoints. */
c1fc2657 2057 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 2058 {
bb9d5f81 2059 if (b->val_bitsize != 0)
850645cf
TT
2060 v = extract_bitfield_from_watchpoint_value (b, v);
2061 b->val = release_value (v);
4c1d86d9 2062 b->val_valid = true;
fa4727a6 2063 }
a5606eee 2064
2d134ed3
PA
2065 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2066
a5606eee 2067 /* Look at each value on the value chain. */
a6535de1
TT
2068 gdb_assert (!val_chain.empty ());
2069 for (const value_ref_ptr &iter : val_chain)
a5606eee 2070 {
a6535de1
TT
2071 v = iter.get ();
2072
a5606eee
VP
2073 /* If it's a memory location, and GDB actually needed
2074 its contents to evaluate the expression, then we
fa4727a6
DJ
2075 must watch it. If the first value returned is
2076 still lazy, that means an error occurred reading it;
2077 watch it anyway in case it becomes readable. */
a5606eee 2078 if (VALUE_LVAL (v) == lval_memory
a6535de1 2079 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee 2080 {
d0c97917 2081 struct type *vtype = check_typedef (v->type ());
7270d8f2 2082
a5606eee
VP
2083 /* We only watch structs and arrays if user asked
2084 for it explicitly, never if they just happen to
2085 appear in the middle of some value chain. */
fa4727a6 2086 if (v == result
78134374
SM
2087 || (vtype->code () != TYPE_CODE_STRUCT
2088 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
2089 {
2090 CORE_ADDR addr;
f486487f 2091 enum target_hw_bp_type type;
a5606eee 2092 struct bp_location *loc, **tmp;
bb9d5f81
PP
2093 int bitpos = 0, bitsize = 0;
2094
f49d5fa2 2095 if (v->bitsize () != 0)
bb9d5f81
PP
2096 {
2097 /* Extract the bit parameters out from the bitfield
2098 sub-expression. */
2099 bitpos = value_bitpos (v);
f49d5fa2 2100 bitsize = v->bitsize ();
bb9d5f81
PP
2101 }
2102 else if (v == result && b->val_bitsize != 0)
2103 {
2104 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2105 lvalue whose bit parameters are saved in the fields
2106 VAL_BITPOS and VAL_BITSIZE. */
2107 bitpos = b->val_bitpos;
2108 bitsize = b->val_bitsize;
2109 }
a5606eee 2110
42ae5230 2111 addr = value_address (v);
bb9d5f81
PP
2112 if (bitsize != 0)
2113 {
2114 /* Skip the bytes that don't contain the bitfield. */
2115 addr += bitpos / 8;
2116 }
2117
a5606eee 2118 type = hw_write;
c1fc2657 2119 if (b->type == bp_read_watchpoint)
a5606eee 2120 type = hw_read;
c1fc2657 2121 else if (b->type == bp_access_watchpoint)
a5606eee 2122 type = hw_access;
3a5c3e22 2123
ee89d0a4 2124 loc = b->allocate_location ();
c1fc2657 2125 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2126 ;
2127 *tmp = loc;
d0c97917 2128 loc->gdbarch = v->type ()->arch ();
6c95b8df
PA
2129
2130 loc->pspace = frame_pspace;
d88cb738
LM
2131 loc->address
2132 = gdbarch_remove_non_address_bits (loc->gdbarch, addr);
bb9d5f81
PP
2133
2134 if (bitsize != 0)
2135 {
2136 /* Just cover the bytes that make up the bitfield. */
2137 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2138 }
2139 else
d0c97917 2140 loc->length = v->type ()->length ();
bb9d5f81 2141
a5606eee
VP
2142 loc->watchpoint_type = type;
2143 }
2144 }
9fa40276
TJB
2145 }
2146
2147 /* Change the type of breakpoint between hardware assisted or
bc45f536
CL
2148 an ordinary watchpoint depending on the hardware support and
2149 free hardware slots. Recheck the number of free hardware slots
2150 as the value chain may have changed. */
9fa40276 2151 {
e09342b5 2152 int reg_cnt;
9fa40276 2153 enum bp_loc_type loc_type;
a5606eee 2154
a9634178 2155 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2156
2157 if (reg_cnt)
9fa40276
TJB
2158 {
2159 int i, target_resources_ok, other_type_used;
a1398e0c 2160 enum bptype type;
9fa40276 2161
a9634178
TJB
2162 /* Use an exact watchpoint when there's only one memory region to be
2163 watched, and only one debug register is needed to watch it. */
2164 b->exact = target_exact_watchpoints && reg_cnt == 1;
2165
9fa40276 2166 /* We need to determine how many resources are already
e09342b5
TJB
2167 used for all other hardware watchpoints plus this one
2168 to see if we still have enough resources to also fit
a1398e0c
PA
2169 this watchpoint in as well. */
2170
2171 /* If this is a software watchpoint, we try to turn it
2172 to a hardware one -- count resources as if B was of
2173 hardware watchpoint type. */
c1fc2657 2174 type = b->type;
a1398e0c
PA
2175 if (type == bp_watchpoint)
2176 type = bp_hardware_watchpoint;
2177
2178 /* This watchpoint may or may not have been placed on
2179 the list yet at this point (it won't be in the list
2180 if we're trying to create it for the first time,
2181 through watch_command), so always account for it
2182 manually. */
2183
2184 /* Count resources used by all watchpoints except B. */
c1fc2657 2185 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2186
2187 /* Add in the resources needed for B. */
c1fc2657 2188 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2189
2190 target_resources_ok
2191 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2192 if (target_resources_ok <= 0)
a9634178 2193 {
5a61e176 2194 bool sw_mode = b->works_in_software_mode ();
9c06b0b4
TJB
2195
2196 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2197 error (_("Target does not support this type of "
2198 "hardware watchpoint."));
9c06b0b4
TJB
2199 else if (target_resources_ok < 0 && !sw_mode)
2200 error (_("There are not enough available hardware "
2201 "resources for this watchpoint."));
a1398e0c
PA
2202
2203 /* Downgrade to software watchpoint. */
c1fc2657 2204 b->type = bp_watchpoint;
a1398e0c
PA
2205 }
2206 else
2207 {
2208 /* If this was a software watchpoint, we've just
2209 found we have enough resources to turn it to a
2210 hardware watchpoint. Otherwise, this is a
2211 nop. */
c1fc2657 2212 b->type = type;
a9634178 2213 }
9fa40276 2214 }
c47614fe 2215 else if (!b->works_in_software_mode ())
638aa5a1
AB
2216 {
2217 if (!can_use_hw_watchpoints)
2218 error (_("Can't set read/access watchpoint when "
2219 "hardware watchpoints are disabled."));
2220 else
2221 error (_("Expression cannot be implemented with "
2222 "read/access watchpoint."));
2223 }
9fa40276 2224 else
c1fc2657 2225 b->type = bp_watchpoint;
9fa40276 2226
6e14e441 2227 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
9fa40276 2228 : bp_loc_hardware_watchpoint);
40cb8ca5 2229 for (bp_location *bl : b->locations ())
9fa40276
TJB
2230 bl->loc_type = loc_type;
2231 }
2232
c7437ca6
PA
2233 /* If a software watchpoint is not watching any memory, then the
2234 above left it without any location set up. But,
2235 bpstat_stop_status requires a location to be able to report
2236 stops, so make sure there's at least a dummy one. */
c1fc2657 2237 if (b->type == bp_watchpoint && b->loc == NULL)
6e14e441 2238 add_dummy_location (b, frame_pspace);
a5606eee
VP
2239 }
2240 else if (!within_current_scope)
7270d8f2 2241 {
6cb06a8c 2242 gdb_printf (_("\
ac74f770
MS
2243Watchpoint %d deleted because the program has left the block\n\
2244in which its expression is valid.\n"),
6cb06a8c 2245 b->number);
d0fb5eae 2246 watchpoint_del_at_next_stop (b);
7270d8f2 2247 }
7270d8f2
OF
2248}
2249
d8de7963 2250/* Returns true iff breakpoint location should be
1e4d1764
YQ
2251 inserted in the inferior. We don't differentiate the type of BL's owner
2252 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2253 breakpoint_ops is not defined, because in insert_bp_location,
2254 tracepoint's insert_location will not be called. */
d8de7963
AB
2255
2256static bool
35df4500 2257should_be_inserted (struct bp_location *bl)
74960c60 2258{
35df4500 2259 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
d8de7963 2260 return false;
74960c60 2261
35df4500 2262 if (bl->owner->disposition == disp_del_at_next_stop)
d8de7963 2263 return false;
74960c60 2264
b5fa468f
TBA
2265 if (!bl->enabled || bl->disabled_by_cond
2266 || bl->shlib_disabled || bl->duplicate)
d8de7963 2267 return false;
74960c60 2268
f8eba3c6 2269 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
d8de7963 2270 return false;
f8eba3c6 2271
56710373
PA
2272 /* This is set for example, when we're attached to the parent of a
2273 vfork, and have detached from the child. The child is running
2274 free, and we expect it to do an exec or exit, at which point the
2275 OS makes the parent schedulable again (and the target reports
2276 that the vfork is done). Until the child is done with the shared
2277 memory region, do not insert breakpoints in the parent, otherwise
2278 the child could still trip on the parent's breakpoints. Since
2279 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2280 if (bl->pspace->breakpoints_not_allowed)
d8de7963 2281 return false;
56710373 2282
31e77af2 2283 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2284 location, except if the breakpoint is a single-step breakpoint,
2285 and the breakpoint's thread is the thread which is stepping past
2286 a breakpoint. */
31e77af2
PA
2287 if ((bl->loc_type == bp_loc_software_breakpoint
2288 || bl->loc_type == bp_loc_hardware_breakpoint)
2289 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2290 bl->address)
2291 /* The single-step breakpoint may be inserted at the location
2292 we're trying to step if the instruction branches to itself.
2293 However, the instruction won't be executed at all and it may
2294 break the semantics of the instruction, for example, the
2295 instruction is a conditional branch or updates some flags.
2296 We can't fix it unless GDB is able to emulate the instruction
2297 or switch to displaced stepping. */
2298 && !(bl->owner->type == bp_single_step
2299 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2300 {
1eb8556f
SM
2301 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2302 paddress (bl->gdbarch, bl->address));
d8de7963 2303 return false;
e558d7c1 2304 }
31e77af2 2305
963f9c80
PA
2306 /* Don't insert watchpoints if we're trying to step past the
2307 instruction that triggered one. */
2308 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2309 && stepping_past_nonsteppable_watchpoint ())
2310 {
1eb8556f 2311 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2312 "skipping watchpoint at %s:%d",
1eb8556f 2313 paddress (bl->gdbarch, bl->address), bl->length);
d8de7963 2314 return false;
963f9c80
PA
2315 }
2316
d8de7963 2317 return true;
74960c60
VP
2318}
2319
934709f0
PW
2320/* Same as should_be_inserted but does the check assuming
2321 that the location is not duplicated. */
2322
aaa141a0 2323static bool
934709f0
PW
2324unduplicated_should_be_inserted (struct bp_location *bl)
2325{
aaa141a0
AB
2326 scoped_restore restore_bl_duplicate
2327 = make_scoped_restore (&bl->duplicate, 0);
934709f0 2328
aaa141a0 2329 return should_be_inserted (bl);
934709f0
PW
2330}
2331
b775012e
LM
2332/* Parses a conditional described by an expression COND into an
2333 agent expression bytecode suitable for evaluation
2334 by the bytecode interpreter. Return NULL if there was
2335 any error during parsing. */
2336
833177a4 2337static agent_expr_up
b775012e
LM
2338parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2339{
833177a4 2340 if (cond == NULL)
b775012e
LM
2341 return NULL;
2342
833177a4
PA
2343 agent_expr_up aexpr;
2344
b775012e
LM
2345 /* We don't want to stop processing, so catch any errors
2346 that may show up. */
a70b8144 2347 try
b775012e 2348 {
036e657b 2349 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2350 }
2351
230d2906 2352 catch (const gdb_exception_error &ex)
b775012e
LM
2353 {
2354 /* If we got here, it means the condition could not be parsed to a valid
2355 bytecode expression and thus can't be evaluated on the target's side.
2356 It's no use iterating through the conditions. */
b775012e
LM
2357 }
2358
2359 /* We have a valid agent expression. */
2360 return aexpr;
2361}
2362
2363/* Based on location BL, create a list of breakpoint conditions to be
2364 passed on to the target. If we have duplicated locations with different
2365 conditions, we will add such conditions to the list. The idea is that the
2366 target will evaluate the list of conditions and will only notify GDB when
2367 one of them is true. */
2368
2369static void
2370build_target_condition_list (struct bp_location *bl)
2371{
d8de7963 2372 bool null_condition_or_parse_error = false;
b775012e 2373 int modified = bl->needs_update;
b775012e 2374
8b4f3082 2375 /* Release conditions left over from a previous insert. */
3cde5c42 2376 bl->target_info.conditions.clear ();
8b4f3082 2377
b775012e
LM
2378 /* This is only meaningful if the target is
2379 evaluating conditions and if the user has
2380 opted for condition evaluation on the target's
2381 side. */
2382 if (gdb_evaluates_breakpoint_condition_p ()
2383 || !target_supports_evaluation_of_breakpoint_conditions ())
2384 return;
2385
e0d9a270
SM
2386 auto loc_range = all_bp_locations_at_addr (bl->address);
2387
b775012e 2388 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2389 conditions or conditions that fail to parse to a valid agent
2390 expression bytecode. If any of these happen, then it's no use to
2391 send conditions to the target since this location will always
2392 trigger and generate a response back to GDB. Note we consider
2393 all locations at the same address irrespective of type, i.e.,
2394 even if the locations aren't considered duplicates (e.g.,
2395 software breakpoint and hardware breakpoint at the same
2396 address). */
e0d9a270 2397 for (bp_location *loc : loc_range)
b775012e 2398 {
b775012e
LM
2399 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2400 {
2401 if (modified)
2402 {
b775012e
LM
2403 /* Re-parse the conditions since something changed. In that
2404 case we already freed the condition bytecodes (see
2405 force_breakpoint_reinsertion). We just
2406 need to parse the condition to bytecodes again. */
833177a4
PA
2407 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2408 loc->cond.get ());
b775012e
LM
2409 }
2410
2411 /* If we have a NULL bytecode expression, it means something
2412 went wrong or we have a null condition expression. */
2413 if (!loc->cond_bytecode)
2414 {
d8de7963 2415 null_condition_or_parse_error = true;
b775012e
LM
2416 break;
2417 }
2418 }
2419 }
2420
2421 /* If any of these happened, it means we will have to evaluate the conditions
2422 for the location's address on gdb's side. It is no use keeping bytecodes
2423 for all the other duplicate locations, thus we free all of them here.
2424
2425 This is so we have a finer control over which locations' conditions are
2426 being evaluated by GDB or the remote stub. */
2427 if (null_condition_or_parse_error)
2428 {
e0d9a270 2429 for (bp_location *loc : loc_range)
b775012e 2430 {
b775012e
LM
2431 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2432 {
2433 /* Only go as far as the first NULL bytecode is
2434 located. */
2435 if (!loc->cond_bytecode)
2436 return;
2437
833177a4 2438 loc->cond_bytecode.reset ();
b775012e
LM
2439 }
2440 }
2441 }
2442
7f32a4d5
PA
2443 /* No NULL conditions or failed bytecode generation. Build a
2444 condition list for this location's address. If we have software
2445 and hardware locations at the same address, they aren't
2446 considered duplicates, but we still marge all the conditions
2447 anyway, as it's simpler, and doesn't really make a practical
2448 difference. */
e0d9a270
SM
2449 for (bp_location *loc : loc_range)
2450 if (loc->cond
2451 && is_breakpoint (loc->owner)
2452 && loc->pspace->num == bl->pspace->num
2453 && loc->owner->enable_state == bp_enabled
2454 && loc->enabled
2455 && !loc->disabled_by_cond)
2456 {
2457 /* Add the condition to the vector. This will be used later
2458 to send the conditions to the target. */
2459 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2460 }
b775012e
LM
2461
2462 return;
2463}
2464
d3ce09f5
SS
2465/* Parses a command described by string CMD into an agent expression
2466 bytecode suitable for evaluation by the bytecode interpreter.
2467 Return NULL if there was any error during parsing. */
2468
833177a4 2469static agent_expr_up
d3ce09f5
SS
2470parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2471{
bbc13ae3
KS
2472 const char *cmdrest;
2473 const char *format_start, *format_end;
d3ce09f5
SS
2474 struct gdbarch *gdbarch = get_current_arch ();
2475
833177a4 2476 if (cmd == NULL)
d3ce09f5
SS
2477 return NULL;
2478
2479 cmdrest = cmd;
2480
2481 if (*cmdrest == ',')
2482 ++cmdrest;
f1735a53 2483 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2484
2485 if (*cmdrest++ != '"')
2486 error (_("No format string following the location"));
2487
2488 format_start = cmdrest;
2489
8e481c3b 2490 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2491
2492 format_end = cmdrest;
2493
2494 if (*cmdrest++ != '"')
2495 error (_("Bad format string, non-terminated '\"'."));
2496
f1735a53 2497 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2498
2499 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2500 error (_("Invalid argument syntax"));
2501
2502 if (*cmdrest == ',')
2503 cmdrest++;
f1735a53 2504 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2505
2506 /* For each argument, make an expression. */
2507
8e481c3b 2508 std::vector<struct expression *> argvec;
d3ce09f5
SS
2509 while (*cmdrest != '\0')
2510 {
bbc13ae3 2511 const char *cmd1;
d3ce09f5
SS
2512
2513 cmd1 = cmdrest;
4d01a485 2514 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2515 argvec.push_back (expr.release ());
d3ce09f5
SS
2516 cmdrest = cmd1;
2517 if (*cmdrest == ',')
2518 ++cmdrest;
2519 }
2520
833177a4
PA
2521 agent_expr_up aexpr;
2522
d3ce09f5
SS
2523 /* We don't want to stop processing, so catch any errors
2524 that may show up. */
a70b8144 2525 try
d3ce09f5 2526 {
036e657b
JB
2527 aexpr = gen_printf (scope, gdbarch, 0, 0,
2528 format_start, format_end - format_start,
8e481c3b 2529 argvec.size (), argvec.data ());
d3ce09f5 2530 }
230d2906 2531 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2532 {
2533 /* If we got here, it means the command could not be parsed to a valid
2534 bytecode expression and thus can't be evaluated on the target's side.
2535 It's no use iterating through the other commands. */
d3ce09f5 2536 }
492d29ea 2537
d3ce09f5
SS
2538 /* We have a valid agent expression, return it. */
2539 return aexpr;
2540}
2541
2542/* Based on location BL, create a list of breakpoint commands to be
2543 passed on to the target. If we have duplicated locations with
2544 different commands, we will add any such to the list. */
2545
2546static void
2547build_target_command_list (struct bp_location *bl)
2548{
d8de7963 2549 bool null_command_or_parse_error = false;
d3ce09f5 2550 int modified = bl->needs_update;
d3ce09f5 2551
3cde5c42
PA
2552 /* Clear commands left over from a previous insert. */
2553 bl->target_info.tcommands.clear ();
8b4f3082 2554
41fac0cf 2555 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2556 return;
2557
41fac0cf
PA
2558 /* For now, limit to agent-style dprintf breakpoints. */
2559 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2560 return;
2561
e0d9a270
SM
2562 auto loc_range = all_bp_locations_at_addr (bl->address);
2563
7f32a4d5
PA
2564 /* For now, if we have any location at the same address that isn't a
2565 dprintf, don't install the target-side commands, as that would
2566 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2567 control. */
e0d9a270
SM
2568 for (bp_location *loc : loc_range)
2569 if (is_breakpoint (loc->owner)
2570 && loc->pspace->num == bl->pspace->num
2571 && loc->owner->type != bp_dprintf)
2572 return;
41fac0cf 2573
d3ce09f5
SS
2574 /* Do a first pass to check for locations with no assigned
2575 conditions or conditions that fail to parse to a valid agent expression
2576 bytecode. If any of these happen, then it's no use to send conditions
2577 to the target since this location will always trigger and generate a
2578 response back to GDB. */
e0d9a270 2579 for (bp_location *loc : loc_range)
d3ce09f5 2580 {
d3ce09f5
SS
2581 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2582 {
2583 if (modified)
2584 {
d3ce09f5
SS
2585 /* Re-parse the commands since something changed. In that
2586 case we already freed the command bytecodes (see
2587 force_breakpoint_reinsertion). We just
2588 need to parse the command to bytecodes again. */
833177a4
PA
2589 loc->cmd_bytecode
2590 = parse_cmd_to_aexpr (bl->address,
6f781ee3 2591 loc->owner->extra_string.get ());
d3ce09f5
SS
2592 }
2593
2594 /* If we have a NULL bytecode expression, it means something
2595 went wrong or we have a null command expression. */
2596 if (!loc->cmd_bytecode)
2597 {
d8de7963 2598 null_command_or_parse_error = true;
d3ce09f5
SS
2599 break;
2600 }
2601 }
2602 }
2603
2604 /* If anything failed, then we're not doing target-side commands,
2605 and so clean up. */
2606 if (null_command_or_parse_error)
2607 {
e0d9a270
SM
2608 for (bp_location *loc : loc_range)
2609 if (is_breakpoint (loc->owner)
2610 && loc->pspace->num == bl->pspace->num)
2611 {
2612 /* Only go as far as the first NULL bytecode is
2613 located. */
2614 if (loc->cmd_bytecode == NULL)
2615 return;
d3ce09f5 2616
e0d9a270
SM
2617 loc->cmd_bytecode.reset ();
2618 }
d3ce09f5
SS
2619 }
2620
7f32a4d5
PA
2621 /* No NULL commands or failed bytecode generation. Build a command
2622 list for all duplicate locations at this location's address.
2623 Note that here we must care for whether the breakpoint location
2624 types are considered duplicates, otherwise, say, if we have a
2625 software and hardware location at the same address, the target
2626 could end up running the commands twice. For the moment, we only
2627 support targets-side commands with dprintf, but it doesn't hurt
2628 to be pedantically correct in case that changes. */
e0d9a270
SM
2629 for (bp_location *loc : loc_range)
2630 if (breakpoint_locations_match (bl, loc)
2631 && loc->owner->extra_string
2632 && is_breakpoint (loc->owner)
2633 && loc->pspace->num == bl->pspace->num
2634 && loc->owner->enable_state == bp_enabled
2635 && loc->enabled
2636 && !loc->disabled_by_cond)
2637 {
2638 /* Add the command to the vector. This will be used later
2639 to send the commands to the target. */
2640 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2641 }
d3ce09f5
SS
2642
2643 bl->target_info.persist = 0;
2644 /* Maybe flag this location as persistent. */
2645 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2646 bl->target_info.persist = 1;
2647}
2648
833b7ab5
YQ
2649/* Return the kind of breakpoint on address *ADDR. Get the kind
2650 of breakpoint according to ADDR except single-step breakpoint.
2651 Get the kind of single-step breakpoint according to the current
2652 registers state. */
cd6c3b4f
YQ
2653
2654static int
b2ff9ed3 2655breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
cd6c3b4f 2656{
833b7ab5
YQ
2657 if (bl->owner->type == bp_single_step)
2658 {
2659 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2660 struct regcache *regcache;
2661
00431a78 2662 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2663
2664 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2665 regcache, addr);
2666 }
2667 else
2668 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2669}
2670
47e2c30a
TV
2671/* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2672 E is either the currently handled exception, or a copy, or a sliced copy,
2673 so we can't rethrow that one, but we can use it to inspect the properties
2674 of the currently handled exception. */
2675
2676static void
2677rethrow_on_target_close_error (const gdb_exception &e)
2678{
2679 if (e.reason == 0)
2680 return;
2681 /* Can't set the breakpoint. */
2682
2683 if (e.error != TARGET_CLOSE_ERROR)
2684 return;
2685
2686 /* If the target has closed then it will have deleted any breakpoints
2687 inserted within the target inferior, as a result any further attempts
2688 to interact with the breakpoint objects is not possible. Just rethrow
2689 the error. Don't use e to rethrow, to prevent object slicing of the
2690 exception. */
2691 throw;
2692}
b2829fcf 2693
35df4500
TJB
2694/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2695 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2696 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2697 Returns 0 for success, 1 if the bp_location type is not supported or
2698 -1 for failure.
879bfdc2 2699
4a64f543
MS
2700 NOTE drow/2003-09-09: This routine could be broken down to an
2701 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2702static int
35df4500 2703insert_bp_location (struct bp_location *bl,
26bb91f3 2704 struct ui_file *tmp_error_stream,
3fbb6ffa 2705 int *disabled_breaks,
dd61ec5c
MW
2706 int *hw_breakpoint_error,
2707 int *hw_bp_error_explained_already)
879bfdc2 2708{
cc06b668 2709 gdb_exception bp_excpt;
879bfdc2 2710
b775012e 2711 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2712 return 0;
2713
35c63cd8
JB
2714 /* Note we don't initialize bl->target_info, as that wipes out
2715 the breakpoint location's shadow_contents if the breakpoint
2716 is still inserted at that location. This in turn breaks
2717 target_read_memory which depends on these buffers when
2718 a memory read is requested at the breakpoint location:
2719 Once the target_info has been wiped, we fail to see that
2720 we have a breakpoint inserted at that address and thus
2721 read the breakpoint instead of returning the data saved in
2722 the breakpoint location's shadow contents. */
0d5ed153 2723 bl->target_info.reqstd_address = bl->address;
35df4500 2724 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2725 bl->target_info.length = bl->length;
8181d85f 2726
b775012e
LM
2727 /* When working with target-side conditions, we must pass all the conditions
2728 for the same breakpoint address down to the target since GDB will not
2729 insert those locations. With a list of breakpoint conditions, the target
2730 can decide when to stop and notify GDB. */
2731
2732 if (is_breakpoint (bl->owner))
2733 {
2734 build_target_condition_list (bl);
d3ce09f5
SS
2735 build_target_command_list (bl);
2736 /* Reset the modification marker. */
b775012e
LM
2737 bl->needs_update = 0;
2738 }
2739
7f32a4d5
PA
2740 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2741 set at a read-only address, then a breakpoint location will have
2742 been changed to hardware breakpoint before we get here. If it is
2743 "off" however, error out before actually trying to insert the
2744 breakpoint, with a nicer error message. */
35df4500 2745 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2746 && !automatic_hardware_breakpoints)
879bfdc2 2747 {
7f32a4d5 2748 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2749
7f32a4d5
PA
2750 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2751 {
6cb06a8c
TT
2752 gdb_printf (tmp_error_stream,
2753 _("Cannot insert breakpoint %d.\n"
2754 "Cannot set software breakpoint "
2755 "at read-only address %s\n"),
2756 bl->owner->number,
2757 paddress (bl->gdbarch, bl->address));
7f32a4d5 2758 return 1;
765dc015 2759 }
7f32a4d5
PA
2760 }
2761
2762 if (bl->loc_type == bp_loc_software_breakpoint
2763 || bl->loc_type == bp_loc_hardware_breakpoint)
2764 {
879bfdc2
DJ
2765 /* First check to see if we have to handle an overlay. */
2766 if (overlay_debugging == ovly_off
35df4500
TJB
2767 || bl->section == NULL
2768 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2769 {
2770 /* No overlay handling: just set the breakpoint. */
a70b8144 2771 try
dd61ec5c 2772 {
0000e5cc
PA
2773 int val;
2774
c47614fe 2775 val = bl->owner->insert_location (bl);
0000e5cc 2776 if (val)
688fca4f 2777 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2778 }
94aeb44b 2779 catch (gdb_exception &e)
dd61ec5c 2780 {
47e2c30a 2781 rethrow_on_target_close_error (e);
94aeb44b 2782 bp_excpt = std::move (e);
dd61ec5c 2783 }
879bfdc2
DJ
2784 }
2785 else
2786 {
4a64f543 2787 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2788 Shall we set a breakpoint at the LMA? */
2789 if (!overlay_events_enabled)
2790 {
2791 /* Yes -- overlay event support is not active,
2792 so we must try to set a breakpoint at the LMA.
2793 This will not work for a hardware breakpoint. */
35df4500 2794 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2795 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2796 bl->owner->number);
879bfdc2
DJ
2797 else
2798 {
35df4500
TJB
2799 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2800 bl->section);
879bfdc2 2801 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2802 bl->overlay_target_info = bl->target_info;
0d5ed153 2803 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2804
2805 /* No overlay handling: just set the breakpoint. */
a70b8144 2806 try
0000e5cc
PA
2807 {
2808 int val;
2809
579c6ad9 2810 bl->overlay_target_info.kind
cd6c3b4f
YQ
2811 = breakpoint_kind (bl, &addr);
2812 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2813 val = target_insert_breakpoint (bl->gdbarch,
2814 &bl->overlay_target_info);
2815 if (val)
688fca4f
PA
2816 bp_excpt
2817 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2818 }
94aeb44b 2819 catch (gdb_exception &e)
0000e5cc 2820 {
47e2c30a 2821 rethrow_on_target_close_error (e);
94aeb44b 2822 bp_excpt = std::move (e);
0000e5cc
PA
2823 }
2824
688fca4f 2825 if (bp_excpt.reason != 0)
6cb06a8c
TT
2826 gdb_printf (tmp_error_stream,
2827 "Overlay breakpoint %d "
2828 "failed: in ROM?\n",
2829 bl->owner->number);
879bfdc2
DJ
2830 }
2831 }
2832 /* Shall we set a breakpoint at the VMA? */
35df4500 2833 if (section_is_mapped (bl->section))
879bfdc2
DJ
2834 {
2835 /* Yes. This overlay section is mapped into memory. */
a70b8144 2836 try
dda83cd7 2837 {
0000e5cc
PA
2838 int val;
2839
c47614fe 2840 val = bl->owner->insert_location (bl);
0000e5cc 2841 if (val)
688fca4f 2842 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2843 }
94aeb44b 2844 catch (gdb_exception &e)
dda83cd7 2845 {
47e2c30a 2846 rethrow_on_target_close_error (e);
94aeb44b 2847 bp_excpt = std::move (e);
dda83cd7 2848 }
879bfdc2
DJ
2849 }
2850 else
2851 {
2852 /* No. This breakpoint will not be inserted.
2853 No error, but do not mark the bp as 'inserted'. */
2854 return 0;
2855 }
2856 }
2857
688fca4f 2858 if (bp_excpt.reason != 0)
879bfdc2
DJ
2859 {
2860 /* Can't set the breakpoint. */
80dc83fd
AB
2861 gdb_assert (bl->owner != nullptr);
2862
0000e5cc
PA
2863 /* In some cases, we might not be able to insert a
2864 breakpoint in a shared library that has already been
2865 removed, but we have not yet processed the shlib unload
2866 event. Unfortunately, some targets that implement
076855f9
PA
2867 breakpoint insertion themselves can't tell why the
2868 breakpoint insertion failed (e.g., the remote target
2869 doesn't define error codes), so we must treat generic
2870 errors as memory errors. */
688fca4f
PA
2871 if (bp_excpt.reason == RETURN_ERROR
2872 && (bp_excpt.error == GENERIC_ERROR
2873 || bp_excpt.error == MEMORY_ERROR)
076855f9 2874 && bl->loc_type == bp_loc_software_breakpoint
08351840 2875 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2876 || shared_objfile_contains_address_p (bl->pspace,
2877 bl->address)))
879bfdc2 2878 {
4a64f543 2879 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2880 bl->shlib_disabled = 1;
76727919 2881 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2882 if (!*disabled_breaks)
2883 {
6cb06a8c
TT
2884 gdb_printf (tmp_error_stream,
2885 "Cannot insert breakpoint %d.\n",
2886 bl->owner->number);
2887 gdb_printf (tmp_error_stream,
2888 "Temporarily disabling shared "
2889 "library breakpoints:\n");
3fbb6ffa
TJB
2890 }
2891 *disabled_breaks = 1;
6cb06a8c
TT
2892 gdb_printf (tmp_error_stream,
2893 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2894 return 0;
879bfdc2
DJ
2895 }
2896 else
879bfdc2 2897 {
35df4500 2898 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2899 {
0000e5cc 2900 *hw_breakpoint_error = 1;
688fca4f 2901 *hw_bp_error_explained_already = bp_excpt.message != NULL;
6cb06a8c
TT
2902 gdb_printf (tmp_error_stream,
2903 "Cannot insert hardware breakpoint %d%s",
2904 bl->owner->number,
2905 bp_excpt.message ? ":" : ".\n");
dda83cd7 2906 if (bp_excpt.message != NULL)
6cb06a8c
TT
2907 gdb_printf (tmp_error_stream, "%s.\n",
2908 bp_excpt.what ());
879bfdc2
DJ
2909 }
2910 else
2911 {
688fca4f 2912 if (bp_excpt.message == NULL)
0000e5cc 2913 {
1ccbe998 2914 std::string message
0000e5cc
PA
2915 = memory_error_message (TARGET_XFER_E_IO,
2916 bl->gdbarch, bl->address);
0000e5cc 2917
6cb06a8c
TT
2918 gdb_printf (tmp_error_stream,
2919 "Cannot insert breakpoint %d.\n"
2920 "%s\n",
2921 bl->owner->number, message.c_str ());
0000e5cc
PA
2922 }
2923 else
2924 {
6cb06a8c
TT
2925 gdb_printf (tmp_error_stream,
2926 "Cannot insert breakpoint %d: %s\n",
2927 bl->owner->number,
2928 bp_excpt.what ());
0000e5cc 2929 }
879bfdc2 2930 }
0000e5cc 2931 return 1;
879bfdc2
DJ
2932
2933 }
2934 }
2935 else
35df4500 2936 bl->inserted = 1;
879bfdc2 2937
0000e5cc 2938 return 0;
879bfdc2
DJ
2939 }
2940
35df4500 2941 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2942 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2943 watchpoints. It's not clear that it's necessary... */
35df4500 2944 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2945 {
0000e5cc
PA
2946 int val;
2947
c47614fe 2948 val = bl->owner->insert_location (bl);
85d721b8
PA
2949
2950 /* If trying to set a read-watchpoint, and it turns out it's not
2951 supported, try emulating one with an access watchpoint. */
35df4500 2952 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2953 {
85d721b8
PA
2954 /* But don't try to insert it, if there's already another
2955 hw_access location that would be considered a duplicate
2956 of this one. */
48d7020b 2957 for (bp_location *loc : all_bp_locations ())
35df4500 2958 if (loc != bl
85d721b8 2959 && loc->watchpoint_type == hw_access
35df4500 2960 && watchpoint_locations_match (bl, loc))
85d721b8 2961 {
35df4500
TJB
2962 bl->duplicate = 1;
2963 bl->inserted = 1;
2964 bl->target_info = loc->target_info;
2965 bl->watchpoint_type = hw_access;
85d721b8
PA
2966 val = 0;
2967 break;
2968 }
2969
2970 if (val == 1)
2971 {
77b06cd7 2972 bl->watchpoint_type = hw_access;
c47614fe 2973 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2974
2975 if (val)
2976 /* Back to the original value. */
2977 bl->watchpoint_type = hw_read;
85d721b8
PA
2978 }
2979 }
2980
35df4500 2981 bl->inserted = (val == 0);
879bfdc2
DJ
2982 }
2983
35df4500 2984 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2985 {
0000e5cc
PA
2986 int val;
2987
c47614fe 2988 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2989 if (val)
2990 {
2991 bl->owner->enable_state = bp_disabled;
2992
2993 if (val == 1)
2994 warning (_("\
2995Error inserting catchpoint %d: Your system does not support this type\n\
2996of catchpoint."), bl->owner->number);
2997 else
2998 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2999 }
3000
3001 bl->inserted = (val == 0);
1640b821
DJ
3002
3003 /* We've already printed an error message if there was a problem
3004 inserting this catchpoint, and we've disabled the catchpoint,
3005 so just return success. */
3006 return 0;
879bfdc2
DJ
3007 }
3008
3009 return 0;
3010}
3011
6c95b8df
PA
3012/* This function is called when program space PSPACE is about to be
3013 deleted. It takes care of updating breakpoints to not reference
3014 PSPACE anymore. */
3015
3016void
3017breakpoint_program_space_exit (struct program_space *pspace)
3018{
6c95b8df 3019 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
3020 for (breakpoint *b : all_breakpoints_safe ())
3021 if (b->pspace == pspace)
3022 delete_breakpoint (b);
6c95b8df
PA
3023
3024 /* Breakpoints set through other program spaces could have locations
3025 bound to PSPACE as well. Remove those. */
48d7020b 3026 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
3027 {
3028 struct bp_location *tmp;
3029
3030 if (loc->pspace == pspace)
3031 {
2bdf28a0 3032 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3033 if (loc->owner->loc == loc)
3034 loc->owner->loc = loc->next;
3035 else
3036 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3037 if (tmp->next == loc)
3038 {
3039 tmp->next = loc->next;
3040 break;
3041 }
3042 }
3043 }
3044
3045 /* Now update the global location list to permanently delete the
3046 removed locations above. */
44702360 3047 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3048}
3049
74960c60
VP
3050/* Make sure all breakpoints are inserted in inferior.
3051 Throws exception on any error.
3052 A breakpoint that is already inserted won't be inserted
3053 again, so calling this function twice is safe. */
3054void
3055insert_breakpoints (void)
3056{
43892fdf 3057 for (breakpoint *bpt : all_breakpoints ())
74960c60 3058 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3059 {
3060 struct watchpoint *w = (struct watchpoint *) bpt;
3061
d8de7963 3062 update_watchpoint (w, false /* don't reparse. */);
3a5c3e22 3063 }
74960c60 3064
04086b45
PA
3065 /* Updating watchpoints creates new locations, so update the global
3066 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3067 ignore breakpoints_always_inserted_mode. Also,
3068 update_global_location_list tries to "upgrade" software
3069 breakpoints to hardware breakpoints to handle "set breakpoint
3070 auto-hw", so we need to call it even if we don't have new
3071 locations. */
04086b45 3072 update_global_location_list (UGLL_INSERT);
74960c60
VP
3073}
3074
b775012e
LM
3075/* This is used when we need to synch breakpoint conditions between GDB and the
3076 target. It is the case with deleting and disabling of breakpoints when using
3077 always-inserted mode. */
3078
3079static void
3080update_inserted_breakpoint_locations (void)
3081{
b775012e
LM
3082 int error_flag = 0;
3083 int val = 0;
3084 int disabled_breaks = 0;
3085 int hw_breakpoint_error = 0;
dd61ec5c 3086 int hw_bp_details_reported = 0;
b775012e 3087
d7e74731 3088 string_file tmp_error_stream;
b775012e
LM
3089
3090 /* Explicitly mark the warning -- this will only be printed if
3091 there was an error. */
d7e74731 3092 tmp_error_stream.puts ("Warning:\n");
b775012e 3093
5ed8105e 3094 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3095
48d7020b 3096 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3097 {
3098 /* We only want to update software breakpoints and hardware
3099 breakpoints. */
3100 if (!is_breakpoint (bl->owner))
3101 continue;
3102
3103 /* We only want to update locations that are already inserted
3104 and need updating. This is to avoid unwanted insertion during
3105 deletion of breakpoints. */
4daf1902 3106 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3107 continue;
3108
3109 switch_to_program_space_and_thread (bl->pspace);
3110
3111 /* For targets that support global breakpoints, there's no need
3112 to select an inferior to insert breakpoint to. In fact, even
3113 if we aren't attached to any process yet, we should still
3114 insert breakpoints. */
f5656ead 3115 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3116 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3117 continue;
3118
d7e74731 3119 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3120 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3121 if (val)
3122 error_flag = val;
3123 }
3124
3125 if (error_flag)
3126 {
223ffa71 3127 target_terminal::ours_for_output ();
b775012e
LM
3128 error_stream (tmp_error_stream);
3129 }
b775012e
LM
3130}
3131
c30eee59 3132/* Used when starting or continuing the program. */
c906108c 3133
74960c60
VP
3134static void
3135insert_breakpoint_locations (void)
c906108c 3136{
eacd795a 3137 int error_flag = 0;
c906108c 3138 int val = 0;
3fbb6ffa 3139 int disabled_breaks = 0;
81d0cc19 3140 int hw_breakpoint_error = 0;
dd61ec5c 3141 int hw_bp_error_explained_already = 0;
c906108c 3142
d7e74731
PA
3143 string_file tmp_error_stream;
3144
81d0cc19
GS
3145 /* Explicitly mark the warning -- this will only be printed if
3146 there was an error. */
d7e74731 3147 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3148
5ed8105e 3149 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3150
48d7020b 3151 for (bp_location *bl : all_bp_locations ())
879bfdc2 3152 {
b775012e 3153 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3154 continue;
3155
4a64f543
MS
3156 /* There is no point inserting thread-specific breakpoints if
3157 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3158 has BL->OWNER always non-NULL. */
35df4500 3159 if (bl->owner->thread != -1
5d5658a1 3160 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3161 continue;
3162
35df4500 3163 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3164
3165 /* For targets that support global breakpoints, there's no need
3166 to select an inferior to insert breakpoint to. In fact, even
3167 if we aren't attached to any process yet, we should still
3168 insert breakpoints. */
f5656ead 3169 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3170 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3171 continue;
3172
d7e74731 3173 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3174 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3175 if (val)
eacd795a 3176 error_flag = val;
879bfdc2 3177 }
c906108c 3178
4a64f543
MS
3179 /* If we failed to insert all locations of a watchpoint, remove
3180 them, as half-inserted watchpoint is of limited use. */
43892fdf 3181 for (breakpoint *bpt : all_breakpoints ())
a5606eee 3182 {
d8de7963 3183 bool some_failed = false;
a5606eee
VP
3184
3185 if (!is_hardware_watchpoint (bpt))
3186 continue;
3187
d6b74ac4 3188 if (!breakpoint_enabled (bpt))
a5606eee 3189 continue;
74960c60
VP
3190
3191 if (bpt->disposition == disp_del_at_next_stop)
3192 continue;
a5606eee 3193
40cb8ca5 3194 for (bp_location *loc : bpt->locations ())
56710373 3195 if (!loc->inserted && should_be_inserted (loc))
a5606eee 3196 {
d8de7963 3197 some_failed = true;
a5606eee
VP
3198 break;
3199 }
40cb8ca5 3200
a5606eee
VP
3201 if (some_failed)
3202 {
40cb8ca5 3203 for (bp_location *loc : bpt->locations ())
a5606eee 3204 if (loc->inserted)
834c0d03 3205 remove_breakpoint (loc);
a5606eee
VP
3206
3207 hw_breakpoint_error = 1;
d7e74731
PA
3208 tmp_error_stream.printf ("Could not insert "
3209 "hardware watchpoint %d.\n",
3210 bpt->number);
eacd795a 3211 error_flag = -1;
a5606eee
VP
3212 }
3213 }
3214
eacd795a 3215 if (error_flag)
81d0cc19
GS
3216 {
3217 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3218 message about possibly exhausted resources. */
dd61ec5c 3219 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3220 {
d7e74731 3221 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3222You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3223 }
223ffa71 3224 target_terminal::ours_for_output ();
81d0cc19
GS
3225 error_stream (tmp_error_stream);
3226 }
c906108c
SS
3227}
3228
c30eee59
TJB
3229/* Used when the program stops.
3230 Returns zero if successful, or non-zero if there was a problem
3231 removing a breakpoint location. */
3232
c906108c 3233int
fba45db2 3234remove_breakpoints (void)
c906108c 3235{
3a1bae8e 3236 int val = 0;
c906108c 3237
48d7020b 3238 for (bp_location *bl : all_bp_locations ())
1e4d1764 3239 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3240 val |= remove_breakpoint (bl);
01add95b 3241
3a1bae8e 3242 return val;
c906108c
SS
3243}
3244
49fa26b0
PA
3245/* When a thread exits, remove breakpoints that are related to
3246 that thread. */
3247
3248static void
3249remove_threaded_breakpoints (struct thread_info *tp, int silent)
3250{
1428b37a 3251 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3252 {
5d5658a1 3253 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3254 {
3255 b->disposition = disp_del_at_next_stop;
3256
6cb06a8c 3257 gdb_printf (_("\
43792cf0 3258Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
6cb06a8c 3259 b->number, print_thread_id (tp));
49fa26b0
PA
3260
3261 /* Hide it from the user. */
3262 b->number = 0;
3263 }
3264 }
3265}
3266
f3869b1a 3267/* See breakpoint.h. */
6c95b8df 3268
f3869b1a 3269void
00431a78 3270remove_breakpoints_inf (inferior *inf)
6c95b8df 3271{
6c95b8df 3272 int val;
6c95b8df 3273
48d7020b 3274 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3275 {
3276 if (bl->pspace != inf->pspace)
3277 continue;
6c95b8df 3278
01add95b
SM
3279 if (bl->inserted && !bl->target_info.persist)
3280 {
3281 val = remove_breakpoint (bl);
3282 if (val != 0)
3283 return;
3284 }
3285 }
6c95b8df
PA
3286}
3287
e58b0e63
PA
3288static int internal_breakpoint_number = -1;
3289
84f4c1fe
PM
3290/* Set the breakpoint number of B, depending on the value of INTERNAL.
3291 If INTERNAL is non-zero, the breakpoint number will be populated
3292 from internal_breakpoint_number and that variable decremented.
e5dd4106 3293 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3294 breakpoint_count and that value incremented. Internal breakpoints
3295 do not set the internal var bpnum. */
3296static void
3297set_breakpoint_number (int internal, struct breakpoint *b)
3298{
3299 if (internal)
3300 b->number = internal_breakpoint_number--;
3301 else
3302 {
3303 set_breakpoint_count (breakpoint_count + 1);
3304 b->number = breakpoint_count;
3305 }
3306}
3307
829b6b37
TV
3308/* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3309
e62c965a 3310static struct breakpoint *
a6d9a66e 3311create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3312 CORE_ADDR address, enum bptype type)
e62c965a 3313{
752a2291
PA
3314 std::unique_ptr<internal_breakpoint> b
3315 (new internal_breakpoint (gdbarch, type, address));
e62c965a 3316
e62c965a 3317 b->number = internal_breakpoint_number--;
e62c965a 3318
752a2291 3319 return add_to_breakpoint_chain (std::move (b));
e62c965a
PP
3320}
3321
829b6b37
TV
3322/* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3323 GDBARCH. */
3324
3325static struct breakpoint *
3326create_internal_breakpoint (struct gdbarch *gdbarch,
3327 struct bound_minimal_symbol &msym, enum bptype type)
3328{
3329 CORE_ADDR address;
3330
3331 address = msym.value_address ();
3332
3333 address = gdbarch_convert_from_func_ptr_addr
3334 (gdbarch, address, current_inferior ()->top_target ());
3335
3336 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3337 related to addresses in $pc. We're getting the address from the
3338 minimal symbol table. */
3339
3340 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3341 with elsewhere? Needs testing on vax. */
3342
3343 if (gdbarch_skip_entrypoint_p (gdbarch))
3344 address = gdbarch_skip_entrypoint (gdbarch, address);
3345
3346 return create_internal_breakpoint (gdbarch, address, type);
3347}
3348
17450429
PP
3349static const char *const longjmp_names[] =
3350 {
3351 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3352 };
3353#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3354
3355/* Per-objfile data private to breakpoint.c. */
3356struct breakpoint_objfile_data
3357{
3358 /* Minimal symbol for "_ovly_debug_event" (if any). */
f6b3ad54 3359 struct bound_minimal_symbol overlay_msym;
17450429
PP
3360
3361 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
f6b3ad54 3362 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3363
28106bc2 3364 /* True if we have looked for longjmp probes. */
43dce439 3365 int longjmp_searched = 0;
28106bc2 3366
45461e0d
SM
3367 /* SystemTap probe points for longjmp (if any). These are non-owning
3368 references. */
3369 std::vector<probe *> longjmp_probes;
28106bc2 3370
17450429 3371 /* Minimal symbol for "std::terminate()" (if any). */
f6b3ad54 3372 struct bound_minimal_symbol terminate_msym;
17450429
PP
3373
3374 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
f6b3ad54 3375 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3376
3377 /* True if we have looked for exception probes. */
43dce439 3378 int exception_searched = 0;
28106bc2 3379
45461e0d
SM
3380 /* SystemTap probe points for unwinding (if any). These are non-owning
3381 references. */
3382 std::vector<probe *> exception_probes;
17450429
PP
3383};
3384
08b8a139 3385static const registry<objfile>::key<breakpoint_objfile_data>
51d3063a 3386 breakpoint_objfile_key;
17450429
PP
3387
3388/* Minimal symbol not found sentinel. */
3389static struct minimal_symbol msym_not_found;
3390
3391/* Returns TRUE if MSYM point to the "not found" sentinel. */
3392
d8de7963 3393static bool
17450429
PP
3394msym_not_found_p (const struct minimal_symbol *msym)
3395{
3396 return msym == &msym_not_found;
3397}
3398
3399/* Return per-objfile data needed by breakpoint.c.
3400 Allocate the data if necessary. */
3401
3402static struct breakpoint_objfile_data *
3403get_breakpoint_objfile_data (struct objfile *objfile)
3404{
3405 struct breakpoint_objfile_data *bp_objfile_data;
3406
51d3063a 3407 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3408 if (bp_objfile_data == NULL)
51d3063a 3409 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3410 return bp_objfile_data;
3411}
3412
e62c965a 3413static void
af02033e 3414create_overlay_event_breakpoint (void)
e62c965a 3415{
af02033e 3416 const char *const func_name = "_ovly_debug_event";
e62c965a 3417
2030c079 3418 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3419 {
3420 struct breakpoint *b;
17450429
PP
3421 struct breakpoint_objfile_data *bp_objfile_data;
3422 CORE_ADDR addr;
69de3c6a 3423
17450429
PP
3424 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3425
3b7344d5 3426 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3427 continue;
3428
3b7344d5 3429 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3430 {
3b7344d5 3431 struct bound_minimal_symbol m;
17450429
PP
3432
3433 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3434 if (m.minsym == NULL)
17450429
PP
3435 {
3436 /* Avoid future lookups in this objfile. */
3b7344d5 3437 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3438 continue;
3439 }
3440 bp_objfile_data->overlay_msym = m;
3441 }
e62c965a 3442
4aeddc50 3443 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3444 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3445 bp_overlay_event);
40d97ee2 3446 b->locspec = new_explicit_location_spec_function (func_name);
e62c965a 3447
69de3c6a 3448 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3449 {
3450 b->enable_state = bp_enabled;
3451 overlay_events_enabled = 1;
3452 }
69de3c6a
PP
3453 else
3454 {
dda83cd7
SM
3455 b->enable_state = bp_disabled;
3456 overlay_events_enabled = 0;
69de3c6a 3457 }
e62c965a 3458 }
e62c965a
PP
3459}
3460
2a7f6487
TV
3461/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3462 true if a breakpoint was installed. */
3463
3464static bool
3465create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3466{
2a7f6487
TV
3467 struct gdbarch *gdbarch = objfile->arch ();
3468 struct breakpoint_objfile_data *bp_objfile_data
3469 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3470
2a7f6487 3471 if (!bp_objfile_data->longjmp_searched)
94c93c35 3472 {
2a7f6487
TV
3473 std::vector<probe *> ret
3474 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3475
2a7f6487 3476 if (!ret.empty ())
94c93c35 3477 {
2a7f6487
TV
3478 /* We are only interested in checking one element. */
3479 probe *p = ret[0];
0fd8e87f 3480
2a7f6487
TV
3481 if (!p->can_evaluate_arguments ())
3482 {
3483 /* We cannot use the probe interface here,
3484 because it does not know how to evaluate
3485 arguments. */
3486 ret.clear ();
3487 }
3488 }
3489 bp_objfile_data->longjmp_probes = ret;
3490 bp_objfile_data->longjmp_searched = 1;
3491 }
0fd8e87f 3492
2a7f6487
TV
3493 if (bp_objfile_data->longjmp_probes.empty ())
3494 return false;
17450429 3495
2a7f6487
TV
3496 for (probe *p : bp_objfile_data->longjmp_probes)
3497 {
3498 struct breakpoint *b;
25f9533e 3499
2a7f6487
TV
3500 b = create_internal_breakpoint (gdbarch,
3501 p->get_relocated_address (objfile),
9efa3c7f 3502 bp_longjmp_master);
264f9890 3503 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
2a7f6487
TV
3504 b->enable_state = bp_disabled;
3505 }
aed57c53 3506
2a7f6487
TV
3507 return true;
3508}
25f9533e 3509
2a7f6487
TV
3510/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3511 Return true if at least one breakpoint was installed. */
3512
3513static bool
3514create_longjmp_master_breakpoint_names (objfile *objfile)
3515{
3516 struct gdbarch *gdbarch = objfile->arch ();
3517 if (!gdbarch_get_longjmp_target_p (gdbarch))
3518 return false;
3519
3520 struct breakpoint_objfile_data *bp_objfile_data
3521 = get_breakpoint_objfile_data (objfile);
3522 unsigned int installed_bp = 0;
3523
3524 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3525 {
3526 struct breakpoint *b;
3527 const char *func_name;
3528 CORE_ADDR addr;
2a7f6487
TV
3529
3530 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3531 continue;
28106bc2 3532
2a7f6487
TV
3533 func_name = longjmp_names[i];
3534 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3535 {
3536 struct bound_minimal_symbol m;
3537
3538 m = lookup_minimal_symbol_text (func_name, objfile);
3539 if (m.minsym == NULL)
3540 {
3541 /* Prevent future lookups in this objfile. */
3542 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3543 continue;
3544 }
2a7f6487
TV
3545 bp_objfile_data->longjmp_msym[i] = m;
3546 }
28106bc2 3547
4aeddc50 3548 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3549 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
40d97ee2 3550 b->locspec = new_explicit_location_spec_function (func_name);
2a7f6487
TV
3551 b->enable_state = bp_disabled;
3552 installed_bp++;
3553 }
28106bc2 3554
2a7f6487
TV
3555 return installed_bp > 0;
3556}
0fd8e87f 3557
2a7f6487 3558/* Create a master longjmp breakpoint. */
17450429 3559
2a7f6487
TV
3560static void
3561create_longjmp_master_breakpoint (void)
3562{
3563 scoped_restore_current_program_space restore_pspace;
aed57c53 3564
2a7f6487
TV
3565 for (struct program_space *pspace : program_spaces)
3566 {
3567 set_current_program_space (pspace);
17450429 3568
2a7f6487
TV
3569 for (objfile *obj : current_program_space->objfiles ())
3570 {
3571 /* Skip separate debug object, it's handled in the loop below. */
3572 if (obj->separate_debug_objfile_backlink != nullptr)
3573 continue;
3574
3575 /* Try a probe kind breakpoint on main objfile. */
3576 if (create_longjmp_master_breakpoint_probe (obj))
3577 continue;
3578
3579 /* Try longjmp_names kind breakpoints on main and separate_debug
3580 objfiles. */
3581 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3582 if (create_longjmp_master_breakpoint_names (debug_objfile))
3583 break;
94c93c35
TT
3584 }
3585 }
0fd8e87f
UW
3586}
3587
af02033e 3588/* Create a master std::terminate breakpoint. */
aa7d318d 3589static void
af02033e 3590create_std_terminate_master_breakpoint (void)
aa7d318d 3591{
af02033e 3592 const char *const func_name = "std::terminate()";
aa7d318d 3593
5ed8105e 3594 scoped_restore_current_program_space restore_pspace;
aa7d318d 3595
94c93c35
TT
3596 for (struct program_space *pspace : program_spaces)
3597 {
94c93c35 3598 set_current_program_space (pspace);
17450429 3599
94c93c35
TT
3600 for (objfile *objfile : current_program_space->objfiles ())
3601 {
3602 struct breakpoint *b;
3603 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3604
94c93c35 3605 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3606
94c93c35
TT
3607 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3608 continue;
17450429 3609
94c93c35
TT
3610 if (bp_objfile_data->terminate_msym.minsym == NULL)
3611 {
3612 struct bound_minimal_symbol m;
17450429 3613
94c93c35 3614 m = lookup_minimal_symbol (func_name, NULL, objfile);
60f62e2b
SM
3615 if (m.minsym == NULL || (m.minsym->type () != mst_text
3616 && m.minsym->type () != mst_file_text))
94c93c35
TT
3617 {
3618 /* Prevent future lookups in this objfile. */
3619 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3620 continue;
3621 }
3622 bp_objfile_data->terminate_msym = m;
3623 }
aa7d318d 3624
829b6b37
TV
3625 b = create_internal_breakpoint (objfile->arch (),
3626 bp_objfile_data->terminate_msym,
9efa3c7f 3627 bp_std_terminate_master);
40d97ee2 3628 b->locspec = new_explicit_location_spec_function (func_name);
94c93c35
TT
3629 b->enable_state = bp_disabled;
3630 }
3631 }
aa7d318d
TT
3632}
3633
1940319c
TV
3634/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3635 probe. Return true if a breakpoint was installed. */
186c406b 3636
1940319c
TV
3637static bool
3638create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3639{
1940319c
TV
3640 struct breakpoint *b;
3641 struct gdbarch *gdbarch;
3642 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3643
1940319c 3644 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3645
1940319c
TV
3646 /* We prefer the SystemTap probe point if it exists. */
3647 if (!bp_objfile_data->exception_searched)
3648 {
3649 std::vector<probe *> ret
3650 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3651
1940319c 3652 if (!ret.empty ())
28106bc2 3653 {
1940319c
TV
3654 /* We are only interested in checking one element. */
3655 probe *p = ret[0];
25f9533e 3656
1940319c 3657 if (!p->can_evaluate_arguments ())
25f9533e 3658 {
1940319c
TV
3659 /* We cannot use the probe interface here, because it does
3660 not know how to evaluate arguments. */
3661 ret.clear ();
25f9533e 3662 }
28106bc2 3663 }
1940319c
TV
3664 bp_objfile_data->exception_probes = ret;
3665 bp_objfile_data->exception_searched = 1;
3666 }
28106bc2 3667
1940319c
TV
3668 if (bp_objfile_data->exception_probes.empty ())
3669 return false;
45461e0d 3670
1940319c 3671 gdbarch = objfile->arch ();
28106bc2 3672
1940319c
TV
3673 for (probe *p : bp_objfile_data->exception_probes)
3674 {
3675 b = create_internal_breakpoint (gdbarch,
3676 p->get_relocated_address (objfile),
9efa3c7f 3677 bp_exception_master);
264f9890 3678 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
1940319c
TV
3679 b->enable_state = bp_disabled;
3680 }
28106bc2 3681
1940319c
TV
3682 return true;
3683}
28106bc2 3684
1940319c
TV
3685/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3686 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3687
1940319c
TV
3688static bool
3689create_exception_master_breakpoint_hook (objfile *objfile)
3690{
3691 const char *const func_name = "_Unwind_DebugHook";
3692 struct breakpoint *b;
3693 struct gdbarch *gdbarch;
3694 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3695
1940319c 3696 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3697
1940319c
TV
3698 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3699 return false;
17450429 3700
1940319c
TV
3701 gdbarch = objfile->arch ();
3702
3703 if (bp_objfile_data->exception_msym.minsym == NULL)
3704 {
3705 struct bound_minimal_symbol debug_hook;
3706
3707 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3708 if (debug_hook.minsym == NULL)
3709 {
3710 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3711 return false;
186c406b 3712 }
17450429 3713
1940319c
TV
3714 bp_objfile_data->exception_msym = debug_hook;
3715 }
3716
829b6b37
TV
3717 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3718 bp_exception_master);
40d97ee2 3719 b->locspec = new_explicit_location_spec_function (func_name);
1940319c
TV
3720 b->enable_state = bp_disabled;
3721
3722 return true;
3723}
3724
3725/* Install a master breakpoint on the unwinder's debug hook. */
3726
3727static void
3728create_exception_master_breakpoint (void)
3729{
3730 for (objfile *obj : current_program_space->objfiles ())
3731 {
3732 /* Skip separate debug object. */
3733 if (obj->separate_debug_objfile_backlink)
3734 continue;
3735
3736 /* Try a probe kind breakpoint. */
3737 if (create_exception_master_breakpoint_probe (obj))
3738 continue;
3739
7c6944ab
TV
3740 /* Iterate over main and separate debug objects and try an
3741 _Unwind_DebugHook kind breakpoint. */
3742 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3743 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3744 break;
186c406b 3745 }
186c406b
TT
3746}
3747
9ef9e6a6
KS
3748/* Does B have a location spec? */
3749
d8de7963 3750static bool
264f9890 3751breakpoint_location_spec_empty_p (const struct breakpoint *b)
9ef9e6a6 3752{
238dc9af 3753 return (b->locspec != nullptr && b->locspec->empty_p ());
9ef9e6a6
KS
3754}
3755
c906108c 3756void
fba45db2 3757update_breakpoints_after_exec (void)
c906108c 3758{
25b22b0a
PA
3759 /* We're about to delete breakpoints from GDB's lists. If the
3760 INSERTED flag is true, GDB will try to lift the breakpoints by
3761 writing the breakpoints' "shadow contents" back into memory. The
3762 "shadow contents" are NOT valid after an exec, so GDB should not
3763 do that. Instead, the target is responsible from marking
3764 breakpoints out as soon as it detects an exec. We don't do that
3765 here instead, because there may be other attempts to delete
3766 breakpoints after detecting an exec and before reaching here. */
48d7020b 3767 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3768 if (bploc->pspace == current_program_space)
3769 gdb_assert (!bploc->inserted);
c906108c 3770
1428b37a 3771 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3772 {
3773 if (b->pspace != current_program_space)
c5aa993b 3774 continue;
c906108c 3775
01add95b
SM
3776 /* Solib breakpoints must be explicitly reset after an exec(). */
3777 if (b->type == bp_shlib_event)
3778 {
3779 delete_breakpoint (b);
3780 continue;
3781 }
4efc6507 3782
01add95b
SM
3783 /* JIT breakpoints must be explicitly reset after an exec(). */
3784 if (b->type == bp_jit_event)
3785 {
3786 delete_breakpoint (b);
3787 continue;
3788 }
c4093a6a 3789
01add95b
SM
3790 /* Thread event breakpoints must be set anew after an exec(),
3791 as must overlay event and longjmp master breakpoints. */
3792 if (b->type == bp_thread_event || b->type == bp_overlay_event
3793 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3794 || b->type == bp_exception_master)
3795 {
3796 delete_breakpoint (b);
3797 continue;
3798 }
c5aa993b 3799
01add95b
SM
3800 /* Step-resume breakpoints are meaningless after an exec(). */
3801 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3802 {
3803 delete_breakpoint (b);
3804 continue;
3805 }
7c16b83e 3806
01add95b
SM
3807 /* Just like single-step breakpoints. */
3808 if (b->type == bp_single_step)
3809 {
3810 delete_breakpoint (b);
3811 continue;
3812 }
611c83ae 3813
01add95b
SM
3814 /* Longjmp and longjmp-resume breakpoints are also meaningless
3815 after an exec. */
3816 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3817 || b->type == bp_longjmp_call_dummy
3818 || b->type == bp_exception || b->type == bp_exception_resume)
3819 {
3820 delete_breakpoint (b);
3821 continue;
3822 }
ce78b96d 3823
01add95b
SM
3824 if (b->type == bp_catchpoint)
3825 {
3826 /* For now, none of the bp_catchpoint breakpoints need to
3827 do anything at this point. In the future, if some of
3828 the catchpoints need to something, we will need to add
3829 a new method, and call this method from here. */
3830 continue;
3831 }
c5aa993b 3832
01add95b
SM
3833 /* bp_finish is a special case. The only way we ought to be able
3834 to see one of these when an exec() has happened, is if the user
3835 caught a vfork, and then said "finish". Ordinarily a finish just
3836 carries them to the call-site of the current callee, by setting
3837 a temporary bp there and resuming. But in this case, the finish
3838 will carry them entirely through the vfork & exec.
3839
3840 We don't want to allow a bp_finish to remain inserted now. But
3841 we can't safely delete it, 'cause finish_command has a handle to
3842 the bp on a bpstat, and will later want to delete it. There's a
3843 chance (and I've seen it happen) that if we delete the bp_finish
3844 here, that its storage will get reused by the time finish_command
3845 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3846 We really must allow finish_command to delete a bp_finish.
3847
3848 In the absence of a general solution for the "how do we know
3849 it's safe to delete something others may have handles to?"
3850 problem, what we'll do here is just uninsert the bp_finish, and
3851 let finish_command delete it.
3852
3853 (We know the bp_finish is "doomed" in the sense that it's
3854 momentary, and will be deleted as soon as finish_command sees
3855 the inferior stopped. So it doesn't matter that the bp's
3856 address is probably bogus in the new a.out, unlike e.g., the
3857 solib breakpoints.) */
3858
3859 if (b->type == bp_finish)
3860 {
3861 continue;
3862 }
3863
3864 /* Without a symbolic address, we have little hope of the
3865 pre-exec() address meaning the same thing in the post-exec()
3866 a.out. */
264f9890 3867 if (breakpoint_location_spec_empty_p (b))
01add95b
SM
3868 {
3869 delete_breakpoint (b);
3870 continue;
3871 }
3872 }
c906108c
SS
3873}
3874
3875int
d80ee84f 3876detach_breakpoints (ptid_t ptid)
c906108c 3877{
3a1bae8e 3878 int val = 0;
2989a365 3879 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3880 struct inferior *inf = current_inferior ();
c5aa993b 3881
e99b03dc 3882 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3883 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3884
6c95b8df 3885 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3886 inferior_ptid = ptid;
48d7020b 3887 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3888 {
3889 if (bl->pspace != inf->pspace)
3890 continue;
3891
3892 /* This function must physically remove breakpoints locations
3893 from the specified ptid, without modifying the breakpoint
6e14e441
PA
3894 package's state. Locations of type bp_loc_other and
3895 bp_loc_software_watchpoint are only maintained at GDB side,
3896 so there is no need to remove them. Moreover, removing these
01add95b 3897 would modify the breakpoint package's state. */
6e14e441
PA
3898 if (bl->loc_type == bp_loc_other
3899 || bl->loc_type == bp_loc_software_watchpoint)
01add95b
SM
3900 continue;
3901
3902 if (bl->inserted)
3903 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3904 }
d03285ec 3905
3a1bae8e 3906 return val;
c906108c
SS
3907}
3908
35df4500 3909/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3910 Note that this is used to detach breakpoints from a child fork.
3911 When we get here, the child isn't in the inferior list, and neither
3912 do we have objects to represent its address space --- we should
35df4500 3913 *not* look at bl->pspace->aspace here. */
6c95b8df 3914
c906108c 3915static int
b2b6a7da 3916remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3917{
3918 int val;
c5aa993b 3919
35df4500
TJB
3920 /* BL is never in moribund_locations by our callers. */
3921 gdb_assert (bl->owner != NULL);
2bdf28a0 3922
74960c60
VP
3923 /* The type of none suggests that owner is actually deleted.
3924 This should not ever happen. */
35df4500 3925 gdb_assert (bl->owner->type != bp_none);
0bde7532 3926
35df4500
TJB
3927 if (bl->loc_type == bp_loc_software_breakpoint
3928 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3929 {
c02f5703
MS
3930 /* "Normal" instruction breakpoint: either the standard
3931 trap-instruction bp (bp_breakpoint), or a
3932 bp_hardware_breakpoint. */
3933
3934 /* First check to see if we have to handle an overlay. */
3935 if (overlay_debugging == ovly_off
35df4500
TJB
3936 || bl->section == NULL
3937 || !(section_is_overlay (bl->section)))
c02f5703
MS
3938 {
3939 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3940
3941 /* If we're trying to uninsert a memory breakpoint that we
3942 know is set in a dynamic object that is marked
3943 shlib_disabled, then either the dynamic object was
3944 removed with "remove-symbol-file" or with
3945 "nosharedlibrary". In the former case, we don't know
3946 whether another dynamic object might have loaded over the
3947 breakpoint's address -- the user might well let us know
3948 about it next with add-symbol-file (the whole point of
d03de421 3949 add-symbol-file is letting the user manually maintain a
08351840
PA
3950 list of dynamically loaded objects). If we have the
3951 breakpoint's shadow memory, that is, this is a software
3952 breakpoint managed by GDB, check whether the breakpoint
3953 is still inserted in memory, to avoid overwriting wrong
3954 code with stale saved shadow contents. Note that HW
3955 breakpoints don't have shadow memory, as they're
3956 implemented using a mechanism that is not dependent on
3957 being able to modify the target's memory, and as such
3958 they should always be removed. */
3959 if (bl->shlib_disabled
3960 && bl->target_info.shadow_len != 0
3961 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3962 val = 0;
3963 else
c47614fe 3964 val = bl->owner->remove_location (bl, reason);
c02f5703 3965 }
c906108c
SS
3966 else
3967 {
4a64f543 3968 /* This breakpoint is in an overlay section.
c02f5703
MS
3969 Did we set a breakpoint at the LMA? */
3970 if (!overlay_events_enabled)
3971 {
3972 /* Yes -- overlay event support is not active, so we
3973 should have set a breakpoint at the LMA. Remove it.
3974 */
c02f5703
MS
3975 /* Ignore any failures: if the LMA is in ROM, we will
3976 have already warned when we failed to insert it. */
35df4500
TJB
3977 if (bl->loc_type == bp_loc_hardware_breakpoint)
3978 target_remove_hw_breakpoint (bl->gdbarch,
3979 &bl->overlay_target_info);
c02f5703 3980 else
35df4500 3981 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3982 &bl->overlay_target_info,
3983 reason);
c02f5703
MS
3984 }
3985 /* Did we set a breakpoint at the VMA?
3986 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3987 if (bl->inserted)
c906108c 3988 {
c02f5703
MS
3989 /* Yes -- remove it. Previously we did not bother to
3990 remove the breakpoint if the section had been
3991 unmapped, but let's not rely on that being safe. We
3992 don't know what the overlay manager might do. */
aa67235e
UW
3993
3994 /* However, we should remove *software* breakpoints only
3995 if the section is still mapped, or else we overwrite
3996 wrong code with the saved shadow contents. */
348d480f
PA
3997 if (bl->loc_type == bp_loc_hardware_breakpoint
3998 || section_is_mapped (bl->section))
c47614fe 3999 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
4000 else
4001 val = 0;
c906108c 4002 }
c02f5703
MS
4003 else
4004 {
4005 /* No -- not inserted, so no need to remove. No error. */
4006 val = 0;
4007 }
c906108c 4008 }
879d1e6b 4009
08351840
PA
4010 /* In some cases, we might not be able to remove a breakpoint in
4011 a shared library that has already been removed, but we have
4012 not yet processed the shlib unload event. Similarly for an
4013 unloaded add-symbol-file object - the user might not yet have
4014 had the chance to remove-symbol-file it. shlib_disabled will
4015 be set if the library/object has already been removed, but
4016 the breakpoint hasn't been uninserted yet, e.g., after
4017 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4018 always-inserted mode. */
076855f9 4019 if (val
08351840
PA
4020 && (bl->loc_type == bp_loc_software_breakpoint
4021 && (bl->shlib_disabled
4022 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4023 || shared_objfile_contains_address_p (bl->pspace,
4024 bl->address))))
879d1e6b
UW
4025 val = 0;
4026
c906108c
SS
4027 if (val)
4028 return val;
b2b6a7da 4029 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4030 }
35df4500 4031 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4032 {
b2b6a7da 4033 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 4034 bl->owner->remove_location (bl, reason);
2e70b7b9 4035
c906108c 4036 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4037 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4038 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4039 bl->owner->number);
c906108c 4040 }
35df4500 4041 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
4042 && breakpoint_enabled (bl->owner)
4043 && !bl->duplicate)
ce78b96d 4044 {
c47614fe 4045 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
4046 if (val)
4047 return val;
77b06cd7 4048
b2b6a7da 4049 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4050 }
c906108c
SS
4051
4052 return 0;
4053}
4054
6c95b8df 4055static int
834c0d03 4056remove_breakpoint (struct bp_location *bl)
6c95b8df 4057{
35df4500
TJB
4058 /* BL is never in moribund_locations by our callers. */
4059 gdb_assert (bl->owner != NULL);
2bdf28a0 4060
6c95b8df
PA
4061 /* The type of none suggests that owner is actually deleted.
4062 This should not ever happen. */
35df4500 4063 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4064
5ed8105e 4065 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4066
35df4500 4067 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4068
5ed8105e 4069 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4070}
4071
c906108c
SS
4072/* Clear the "inserted" flag in all breakpoints. */
4073
25b22b0a 4074void
fba45db2 4075mark_breakpoints_out (void)
c906108c 4076{
48d7020b 4077 for (bp_location *bl : all_bp_locations ())
66c4b3e8 4078 if (bl->pspace == current_program_space)
35df4500 4079 bl->inserted = 0;
c906108c
SS
4080}
4081
53a5351d
JM
4082/* Clear the "inserted" flag in all breakpoints and delete any
4083 breakpoints which should go away between runs of the program.
c906108c
SS
4084
4085 Plus other such housekeeping that has to be done for breakpoints
4086 between runs.
4087
53a5351d
JM
4088 Note: this function gets called at the end of a run (by
4089 generic_mourn_inferior) and when a run begins (by
4a64f543 4090 init_wait_for_inferior). */
c906108c
SS
4091
4092
4093
4094void
fba45db2 4095breakpoint_init_inferior (enum inf_context context)
c906108c 4096{
6c95b8df 4097 struct program_space *pspace = current_program_space;
c906108c 4098
50c71eaf
PA
4099 /* If breakpoint locations are shared across processes, then there's
4100 nothing to do. */
f5656ead 4101 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4102 return;
4103
1a853c52 4104 mark_breakpoints_out ();
075f6582 4105
1428b37a 4106 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
4107 {
4108 if (b->loc && b->loc->pspace != pspace)
4109 continue;
6c95b8df 4110
01add95b
SM
4111 switch (b->type)
4112 {
4113 case bp_call_dummy:
4114 case bp_longjmp_call_dummy:
c906108c 4115
01add95b
SM
4116 /* If the call dummy breakpoint is at the entry point it will
4117 cause problems when the inferior is rerun, so we better get
4118 rid of it. */
ab92d69b 4119
01add95b 4120 case bp_watchpoint_scope:
ab92d69b 4121
01add95b 4122 /* Also get rid of scope breakpoints. */
ab92d69b 4123
01add95b 4124 case bp_shlib_event:
ab92d69b 4125
01add95b
SM
4126 /* Also remove solib event breakpoints. Their addresses may
4127 have changed since the last time we ran the program.
4128 Actually we may now be debugging against different target;
4129 and so the solib backend that installed this breakpoint may
4130 not be used in by the target. E.g.,
ab92d69b 4131
01add95b
SM
4132 (gdb) file prog-linux
4133 (gdb) run # native linux target
4134 ...
4135 (gdb) kill
4136 (gdb) file prog-win.exe
4137 (gdb) tar rem :9999 # remote Windows gdbserver.
4138 */
c906108c 4139
01add95b 4140 case bp_step_resume:
f59f708a 4141
01add95b 4142 /* Also remove step-resume breakpoints. */
f59f708a 4143
01add95b 4144 case bp_single_step:
7c16b83e 4145
01add95b 4146 /* Also remove single-step breakpoints. */
7c16b83e 4147
01add95b
SM
4148 delete_breakpoint (b);
4149 break;
c906108c 4150
01add95b
SM
4151 case bp_watchpoint:
4152 case bp_hardware_watchpoint:
4153 case bp_read_watchpoint:
4154 case bp_access_watchpoint:
4155 {
4156 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4157
01add95b
SM
4158 /* Likewise for watchpoints on local expressions. */
4159 if (w->exp_valid_block != NULL)
4160 delete_breakpoint (b);
4161 else
4162 {
4163 /* Get rid of existing locations, which are no longer
4164 valid. New ones will be created in
4165 update_watchpoint, when the inferior is restarted.
4166 The next update_global_location_list call will
4167 garbage collect them. */
4168 b->loc = NULL;
4169
4170 if (context == inf_starting)
4171 {
4172 /* Reset val field to force reread of starting value in
4173 insert_breakpoints. */
4174 w->val.reset (nullptr);
4175 w->val_valid = false;
4176 }
4177 }
4178 }
4179 break;
4180 default:
4181 break;
3a5c3e22 4182 }
01add95b 4183 }
1c5cfe86
PA
4184
4185 /* Get rid of the moribund locations. */
1123588c 4186 for (bp_location *bl : moribund_locations)
35df4500 4187 decref_bp_location (&bl);
1123588c 4188 moribund_locations.clear ();
c906108c
SS
4189}
4190
6c95b8df
PA
4191/* These functions concern about actual breakpoints inserted in the
4192 target --- to e.g. check if we need to do decr_pc adjustment or if
4193 we need to hop over the bkpt --- so we check for address space
4194 match, not program space. */
4195
c2c6d25f
JM
4196/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4197 exists at PC. It returns ordinary_breakpoint_here if it's an
4198 ordinary breakpoint, or permanent_breakpoint_here if it's a
4199 permanent breakpoint.
4200 - When continuing from a location with an ordinary breakpoint, we
4201 actually single step once before calling insert_breakpoints.
e5dd4106 4202 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4203 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4204 the target, to advance the PC past the breakpoint. */
c906108c 4205
c2c6d25f 4206enum breakpoint_here
accd0bcd 4207breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4208{
d8de7963 4209 bool any_breakpoint_here = false;
c906108c 4210
48d7020b 4211 for (bp_location *bl : all_bp_locations ())
075f6582 4212 {
35df4500
TJB
4213 if (bl->loc_type != bp_loc_software_breakpoint
4214 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4215 continue;
4216
f1310107 4217 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4218 if ((breakpoint_enabled (bl->owner)
1a853c52 4219 || bl->permanent)
f1310107 4220 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4221 {
4222 if (overlay_debugging
35df4500
TJB
4223 && section_is_overlay (bl->section)
4224 && !section_is_mapped (bl->section))
075f6582 4225 continue; /* unmapped overlay -- can't be a match */
1a853c52 4226 else if (bl->permanent)
075f6582
DJ
4227 return permanent_breakpoint_here;
4228 else
d8de7963 4229 any_breakpoint_here = true;
075f6582
DJ
4230 }
4231 }
c906108c 4232
f486487f 4233 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4234}
4235
d35ae833
PA
4236/* See breakpoint.h. */
4237
4238int
accd0bcd 4239breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4240 CORE_ADDR addr, ULONGEST len)
4241{
48d7020b 4242 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4243 {
4244 if (bl->loc_type != bp_loc_software_breakpoint
4245 && bl->loc_type != bp_loc_hardware_breakpoint)
4246 continue;
4247
4248 if ((breakpoint_enabled (bl->owner)
4249 || bl->permanent)
4250 && breakpoint_location_address_range_overlap (bl, aspace,
4251 addr, len))
4252 {
4253 if (overlay_debugging
4254 && section_is_overlay (bl->section)
4255 && !section_is_mapped (bl->section))
4256 {
4257 /* Unmapped overlay -- can't be a match. */
4258 continue;
4259 }
4260
4261 return 1;
4262 }
4263 }
4264
4265 return 0;
4266}
4267
1c5cfe86
PA
4268/* Return true if there's a moribund breakpoint at PC. */
4269
4270int
accd0bcd 4271moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4272{
1123588c 4273 for (bp_location *loc : moribund_locations)
f1310107 4274 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4275 return 1;
4276
4277 return 0;
4278}
c2c6d25f 4279
d8de7963 4280/* Returns true iff BL is inserted at PC, in address space ASPACE. */
f7ce857f 4281
d8de7963 4282static bool
b2ff9ed3 4283bp_location_inserted_here_p (const struct bp_location *bl,
accd0bcd 4284 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4285{
4286 if (bl->inserted
4287 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4288 aspace, pc))
4289 {
d8de7963
AB
4290 /* An unmapped overlay can't be a match. */
4291 return !(overlay_debugging
4292 && section_is_overlay (bl->section)
4293 && !section_is_mapped (bl->section));
f7ce857f 4294 }
d8de7963 4295 return false;
f7ce857f
PA
4296}
4297
a1fd2fa5 4298/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4299
4300int
accd0bcd 4301breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4302{
e0d9a270 4303 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4304 {
35df4500
TJB
4305 if (bl->loc_type != bp_loc_software_breakpoint
4306 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4307 continue;
4308
f7ce857f
PA
4309 if (bp_location_inserted_here_p (bl, aspace, pc))
4310 return 1;
c5aa993b 4311 }
c36b740a
VP
4312 return 0;
4313}
4314
a1fd2fa5
PA
4315/* This function returns non-zero iff there is a software breakpoint
4316 inserted at PC. */
c36b740a
VP
4317
4318int
accd0bcd 4319software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4320 CORE_ADDR pc)
4fa8626c 4321{
e0d9a270 4322 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4323 {
35df4500 4324 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4325 continue;
4326
f7ce857f
PA
4327 if (bp_location_inserted_here_p (bl, aspace, pc))
4328 return 1;
4fa8626c
DJ
4329 }
4330
4331 return 0;
9c02b525
PA
4332}
4333
4334/* See breakpoint.h. */
4335
4336int
accd0bcd 4337hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4338 CORE_ADDR pc)
4339{
e0d9a270 4340 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4341 {
9c02b525
PA
4342 if (bl->loc_type != bp_loc_hardware_breakpoint)
4343 continue;
4344
4345 if (bp_location_inserted_here_p (bl, aspace, pc))
4346 return 1;
4347 }
4348
4349 return 0;
4fa8626c
DJ
4350}
4351
9093389c 4352int
accd0bcd 4353hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4354 CORE_ADDR addr, ULONGEST len)
4355{
43892fdf 4356 for (breakpoint *bpt : all_breakpoints ())
9093389c 4357 {
9093389c
PA
4358 if (bpt->type != bp_hardware_watchpoint
4359 && bpt->type != bp_access_watchpoint)
4360 continue;
4361
4362 if (!breakpoint_enabled (bpt))
4363 continue;
4364
40cb8ca5 4365 for (bp_location *loc : bpt->locations ())
9093389c
PA
4366 if (loc->pspace->aspace == aspace && loc->inserted)
4367 {
4368 CORE_ADDR l, h;
4369
4370 /* Check for intersection. */
768adc05
PA
4371 l = std::max<CORE_ADDR> (loc->address, addr);
4372 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4373 if (l < h)
4374 return 1;
4375 }
4376 }
4377 return 0;
4378}
c5aa993b 4379
f2478a7e 4380/* See breakpoint.h. */
c906108c 4381
f2478a7e
SM
4382bool
4383is_catchpoint (struct breakpoint *b)
c906108c 4384{
f2478a7e 4385 return (b->type == bp_catchpoint);
c906108c
SS
4386}
4387
c906108c
SS
4388/* Clear a bpstat so that it says we are not at any breakpoint.
4389 Also free any storage that is part of a bpstat. */
4390
4391void
313f3b21 4392bpstat_clear (bpstat **bsp)
c906108c 4393{
313f3b21
SM
4394 bpstat *p;
4395 bpstat *q;
c906108c
SS
4396
4397 if (bsp == 0)
4398 return;
4399 p = *bsp;
4400 while (p != NULL)
4401 {
4402 q = p->next;
04afa70c 4403 delete p;
c906108c
SS
4404 p = q;
4405 }
4406 *bsp = NULL;
4407}
4408
313f3b21 4409bpstat::bpstat (const bpstat &other)
04afa70c
TT
4410 : next (NULL),
4411 bp_location_at (other.bp_location_at),
4412 breakpoint_at (other.breakpoint_at),
4413 commands (other.commands),
04afa70c
TT
4414 print (other.print),
4415 stop (other.stop),
4416 print_it (other.print_it)
4417{
850645cf
TT
4418 if (other.old_val != NULL)
4419 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4420}
4421
c906108c
SS
4422/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4423 is part of the bpstat is copied as well. */
4424
313f3b21
SM
4425bpstat *
4426bpstat_copy (bpstat *bs)
c906108c 4427{
313f3b21
SM
4428 bpstat *p = nullptr;
4429 bpstat *tmp;
4430 bpstat *retval = nullptr;
c906108c
SS
4431
4432 if (bs == NULL)
4433 return bs;
4434
4435 for (; bs != NULL; bs = bs->next)
4436 {
313f3b21 4437 tmp = new bpstat (*bs);
31cc81e9 4438
c906108c
SS
4439 if (p == NULL)
4440 /* This is the first thing in the chain. */
4441 retval = tmp;
4442 else
4443 p->next = tmp;
4444 p = tmp;
4445 }
4446 p->next = NULL;
4447 return retval;
4448}
4449
4a64f543 4450/* Find the bpstat associated with this breakpoint. */
c906108c 4451
313f3b21
SM
4452bpstat *
4453bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4454{
c5aa993b
JM
4455 if (bsp == NULL)
4456 return NULL;
c906108c 4457
c5aa993b
JM
4458 for (; bsp != NULL; bsp = bsp->next)
4459 {
f431efe5 4460 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4461 return bsp;
4462 }
c906108c
SS
4463 return NULL;
4464}
4465
ab04a2af
TT
4466/* See breakpoint.h. */
4467
4c462cb0 4468bool
313f3b21 4469bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4470{
ab04a2af
TT
4471 for (; bsp != NULL; bsp = bsp->next)
4472 {
427cd150
TT
4473 if (bsp->breakpoint_at == NULL)
4474 {
4475 /* A moribund location can never explain a signal other than
4476 GDB_SIGNAL_TRAP. */
4477 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4478 return true;
427cd150
TT
4479 }
4480 else
47591c29 4481 {
c47614fe 4482 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4483 return true;
47591c29 4484 }
ab04a2af
TT
4485 }
4486
4c462cb0 4487 return false;
ab04a2af
TT
4488}
4489
78805ff8 4490/* See breakpoint.h. */
c906108c
SS
4491
4492int
313f3b21 4493bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4494{
4495 struct breakpoint *b;
4496
4497 if ((*bsp) == NULL)
4498 return 0; /* No more breakpoint values */
8671a17b 4499
4a64f543
MS
4500 /* We assume we'll never have several bpstats that correspond to a
4501 single breakpoint -- otherwise, this function might return the
4502 same number more than once and this will look ugly. */
f431efe5 4503 b = (*bsp)->breakpoint_at;
8671a17b
PA
4504 *bsp = (*bsp)->next;
4505 if (b == NULL)
4506 return -1; /* breakpoint that's been deleted since */
4507
4508 *num = b->number; /* We have its number */
4509 return 1;
c906108c
SS
4510}
4511
78805ff8
PW
4512/* See breakpoint.h */
4513
4514int
4515bpstat_locno (const bpstat *bs)
4516{
4517 const struct breakpoint *b = bs->breakpoint_at;
4518 const struct bp_location *bl = bs->bp_location_at.get ();
4519
4520 int locno = 0;
4521
28a072f4 4522 if (b != nullptr && b->loc != nullptr && b->loc->next != nullptr)
78805ff8
PW
4523 {
4524 const bp_location *bl_i;
4525
4526 for (bl_i = b->loc;
4527 bl_i != bl && bl_i->next != nullptr;
4528 bl_i = bl_i->next)
4529 locno++;
4530
4531 if (bl_i == bl)
4532 locno++;
4533 else
4534 {
4535 warning (_("location number not found for breakpoint %d address %s."),
4536 b->number, paddress (bl->gdbarch, bl->address));
4537 locno = 0;
4538 }
4539 }
4540
4541 return locno;
4542}
4543
4544/* See breakpoint.h. */
4545
4546void
4547print_num_locno (const bpstat *bs, struct ui_out *uiout)
4548{
4549 struct breakpoint *b = bs->breakpoint_at;
4550
4551 if (b == nullptr)
4552 uiout->text (_("deleted breakpoint"));
4553 else
4554 {
4555 uiout->field_signed ("bkptno", b->number);
4556
4557 int locno = bpstat_locno (bs);
4558 if (locno != 0)
4559 uiout->message (".%pF", signed_field ("locno", locno));
4560 }
4561}
4562
e93ca019 4563/* See breakpoint.h. */
c906108c
SS
4564
4565void
e93ca019 4566bpstat_clear_actions (void)
c906108c 4567{
313f3b21 4568 bpstat *bs;
e93ca019 4569
00431a78 4570 if (inferior_ptid == null_ptid)
e93ca019
JK
4571 return;
4572
00431a78 4573 thread_info *tp = inferior_thread ();
e93ca019 4574 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4575 {
d1b0a7bf 4576 bs->commands = NULL;
850645cf 4577 bs->old_val.reset (nullptr);
c906108c
SS
4578 }
4579}
4580
f3b1572e
PA
4581/* Called when a command is about to proceed the inferior. */
4582
4583static void
4584breakpoint_about_to_proceed (void)
4585{
d7e15655 4586 if (inferior_ptid != null_ptid)
f3b1572e
PA
4587 {
4588 struct thread_info *tp = inferior_thread ();
4589
4590 /* Allow inferior function calls in breakpoint commands to not
4591 interrupt the command list. When the call finishes
4592 successfully, the inferior will be standing at the same
4593 breakpoint as if nothing happened. */
16c381f0 4594 if (tp->control.in_infcall)
f3b1572e
PA
4595 return;
4596 }
4597
4598 breakpoint_proceeded = 1;
4599}
4600
d8de7963 4601/* Return true iff CMD as the first line of a command sequence is `silent'
abf85f46
JK
4602 or its equivalent. */
4603
d8de7963 4604static bool
abf85f46
JK
4605command_line_is_silent (struct command_line *cmd)
4606{
4f45d445 4607 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4608}
4609
83f35083
PW
4610/* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4611 A locno 0 is changed to 1 to e.g. let the user do
4612 (gdb) disable $_hit_bpnum.$_hit_locno
4613 for a single location breakpoint. */
4614
78805ff8 4615static void
83f35083 4616set_hit_convenience_vars (int bpnum, int locno)
78805ff8 4617{
83f35083
PW
4618 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4619 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4620 (locno > 0 ? locno : 1));
78805ff8
PW
4621}
4622
4a64f543
MS
4623/* Execute all the commands associated with all the breakpoints at
4624 this location. Any of these commands could cause the process to
4625 proceed beyond this point, etc. We look out for such changes by
4626 checking the global "breakpoint_proceeded" after each command.
c906108c 4627
347bddb7
PA
4628 Returns true if a breakpoint command resumed the inferior. In that
4629 case, it is the caller's responsibility to recall it again with the
4630 bpstat of the current thread. */
4631
d8de7963 4632static bool
313f3b21 4633bpstat_do_actions_1 (bpstat **bsp)
c906108c 4634{
313f3b21 4635 bpstat *bs;
d8de7963 4636 bool again = false;
c906108c
SS
4637
4638 /* Avoid endless recursion if a `source' command is contained
4639 in bs->commands. */
4640 if (executing_breakpoint_commands)
d8de7963 4641 return false;
c906108c 4642
81b1e71c
TT
4643 scoped_restore save_executing
4644 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4645
1ac32117 4646 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4647
4a64f543 4648 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4649 bs = *bsp;
4650
78805ff8 4651 /* The $_hit_* convenience variables are set before running the
83f35083
PW
4652 commands of BS. In case we have several bs, after the loop,
4653 we set again the variables to the first printed bpnum and locno.
4654 For multiple breakpoints, this ensures the variables are set to the
4655 breakpoint printed for the user. */
4656 int printed_hit_bpnum = -1;
4657 int printed_hit_locno = -1;
78805ff8 4658
c906108c
SS
4659 breakpoint_proceeded = 0;
4660 for (; bs != NULL; bs = bs->next)
4661 {
d1b0a7bf 4662 struct command_line *cmd = NULL;
6c50ab1c 4663
83f35083
PW
4664 /* Set the _hit_* convenience variables before running BS's commands. */
4665 {
4666 const struct breakpoint *b = bs->breakpoint_at;
4667 if (b != nullptr)
4668 {
4669 int locno = bpstat_locno (bs);
4670
4671 set_hit_convenience_vars (b->number, locno);
4672 if (printed_hit_locno == -1 && bs->print)
4673 {
4674 printed_hit_bpnum = b->number;
4675 printed_hit_locno = locno;
4676 }
4677 }
4678 }
78805ff8 4679
6c50ab1c
JB
4680 /* Take ownership of the BSP's command tree, if it has one.
4681
dda83cd7
SM
4682 The command tree could legitimately contain commands like
4683 'step' and 'next', which call clear_proceed_status, which
83f35083 4684 frees the bpstat BS and its command tree. To make sure this doesn't
dda83cd7
SM
4685 free the tree we're executing out from under us, we need to
4686 take ownership of the tree ourselves. Since a given bpstat's
4687 commands are only executed once, we don't need to copy it; we
4688 can clear the pointer in the bpstat, and make sure we free
4689 the tree when we're done. */
d1b0a7bf 4690 counted_command_line ccmd = bs->commands;
9add0f1b 4691 bs->commands = NULL;
d1b0a7bf
TT
4692 if (ccmd != NULL)
4693 cmd = ccmd.get ();
abf85f46
JK
4694 if (command_line_is_silent (cmd))
4695 {
4696 /* The action has been already done by bpstat_stop_status. */
4697 cmd = cmd->next;
4698 }
6c50ab1c 4699
c906108c
SS
4700 while (cmd != NULL)
4701 {
4702 execute_control_command (cmd);
83f35083
PW
4703 /* After execute_control_command, if breakpoint_proceeded is true,
4704 BS has been freed and cannot be accessed anymore. */
c906108c
SS
4705
4706 if (breakpoint_proceeded)
4707 break;
4708 else
4709 cmd = cmd->next;
4710 }
6c50ab1c 4711
c906108c 4712 if (breakpoint_proceeded)
32c1e744 4713 {
cb814510 4714 if (current_ui->async)
347bddb7
PA
4715 /* If we are in async mode, then the target might be still
4716 running, not stopped at any breakpoint, so nothing for
4717 us to do here -- just return to the event loop. */
4718 ;
32c1e744
VP
4719 else
4720 /* In sync mode, when execute_control_command returns
4721 we're already standing on the next breakpoint.
347bddb7
PA
4722 Breakpoint commands for that stop were not run, since
4723 execute_command does not run breakpoint commands --
4724 only command_line_handler does, but that one is not
4725 involved in execution of breakpoint commands. So, we
4726 can now execute breakpoint commands. It should be
4727 noted that making execute_command do bpstat actions is
4728 not an option -- in this case we'll have recursive
4729 invocation of bpstat for each breakpoint with a
4730 command, and can easily blow up GDB stack. Instead, we
4731 return true, which will trigger the caller to recall us
4732 with the new stop_bpstat. */
d8de7963 4733 again = true;
347bddb7 4734 break;
32c1e744 4735 }
c906108c 4736 }
78805ff8
PW
4737
4738 /* Now that we have executed the commands of all bs, set the _hit_*
83f35083
PW
4739 convenience variables to the printed values. */
4740 if (printed_hit_locno != -1)
4741 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
78805ff8 4742
347bddb7
PA
4743 return again;
4744}
4745
00431a78
PA
4746/* Helper for bpstat_do_actions. Get the current thread, if there's
4747 one, is alive and has execution. Return NULL otherwise. */
4748
4749static thread_info *
4750get_bpstat_thread ()
4751{
55f6301a 4752 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4753 return NULL;
4754
4755 thread_info *tp = inferior_thread ();
611841bb 4756 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4757 return NULL;
4758 return tp;
4759}
4760
347bddb7
PA
4761void
4762bpstat_do_actions (void)
4763{
694c6bf5 4764 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4765 thread_info *tp;
353d1d73 4766
347bddb7 4767 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4768 while ((tp = get_bpstat_thread ()) != NULL)
4769 {
4770 /* Since in sync mode, bpstat_do_actions may resume the
4771 inferior, and only return when it is stopped at the next
4772 breakpoint, we keep doing breakpoint actions until it returns
4773 false to indicate the inferior was not resumed. */
4774 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4775 break;
4776 }
353d1d73 4777
694c6bf5 4778 cleanup_if_error.release ();
c906108c
SS
4779}
4780
fa4727a6
DJ
4781/* Print out the (old or new) value associated with a watchpoint. */
4782
4783static void
4784watchpoint_value_print (struct value *val, struct ui_file *stream)
4785{
4786 if (val == NULL)
7f6aba03 4787 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4788 else
79a45b7d
TT
4789 {
4790 struct value_print_options opts;
4791 get_user_print_options (&opts);
4792 value_print (val, stream, &opts);
4793 }
fa4727a6
DJ
4794}
4795
f303dbd6
PA
4796/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4797 debugging multiple threads. */
4798
4799void
4800maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4801{
112e8700 4802 if (uiout->is_mi_like_p ())
f303dbd6
PA
4803 return;
4804
112e8700 4805 uiout->text ("\n");
f303dbd6
PA
4806
4807 if (show_thread_that_caused_stop ())
4808 {
f303dbd6
PA
4809 struct thread_info *thr = inferior_thread ();
4810
112e8700 4811 uiout->text ("Thread ");
33eca680 4812 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 4813
25558938 4814 const char *name = thread_name (thr);
f303dbd6
PA
4815 if (name != NULL)
4816 {
112e8700 4817 uiout->text (" \"");
33eca680 4818 uiout->field_string ("name", name);
112e8700 4819 uiout->text ("\"");
f303dbd6
PA
4820 }
4821
112e8700 4822 uiout->text (" hit ");
f303dbd6
PA
4823 }
4824}
4825
e514a9d6 4826/* Generic routine for printing messages indicating why we
4a64f543 4827 stopped. The behavior of this function depends on the value
e514a9d6
JM
4828 'print_it' in the bpstat structure. Under some circumstances we
4829 may decide not to print anything here and delegate the task to
4a64f543 4830 normal_stop(). */
e514a9d6
JM
4831
4832static enum print_stop_action
313f3b21 4833print_bp_stop_message (bpstat *bs)
e514a9d6
JM
4834{
4835 switch (bs->print_it)
4836 {
4837 case print_it_noop:
4a64f543 4838 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4839 return PRINT_UNKNOWN;
4840 break;
4841
4842 case print_it_done:
4843 /* We still want to print the frame, but we already printed the
dda83cd7 4844 relevant messages. */
e514a9d6
JM
4845 return PRINT_SRC_AND_LOC;
4846 break;
4847
4848 case print_it_normal:
4f8d1dc6 4849 {
f431efe5
PA
4850 struct breakpoint *b = bs->breakpoint_at;
4851
1a6a67de
TJB
4852 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4853 which has since been deleted. */
4854 if (b == NULL)
4855 return PRINT_UNKNOWN;
4856
348d480f 4857 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 4858 return b->print_it (bs);
4f8d1dc6 4859 }
348d480f 4860 break;
3086aeae 4861
e514a9d6 4862 default:
f34652de 4863 internal_error (_("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4864 break;
c906108c 4865 }
c906108c
SS
4866}
4867
2f9ee862 4868/* See breakpoint.h. */
edcc5120 4869
2f9ee862 4870void
4e9e993a 4871print_solib_event (bool is_catchpoint)
edcc5120 4872{
6fb16ce6 4873 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4874 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4875
4876 if (!is_catchpoint)
4877 {
4878 if (any_added || any_deleted)
112e8700 4879 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4880 else
112e8700
SM
4881 current_uiout->text (_("Stopped due to shared library event (no "
4882 "libraries added or removed)\n"));
edcc5120
TT
4883 }
4884
112e8700
SM
4885 if (current_uiout->is_mi_like_p ())
4886 current_uiout->field_string ("reason",
4887 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4888
4889 if (any_deleted)
4890 {
112e8700 4891 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4892 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4893 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4894 {
6fb16ce6
SM
4895 const std::string &name = current_program_space->deleted_solibs[ix];
4896
edcc5120 4897 if (ix > 0)
112e8700
SM
4898 current_uiout->text (" ");
4899 current_uiout->field_string ("library", name);
4900 current_uiout->text ("\n");
edcc5120 4901 }
edcc5120
TT
4902 }
4903
4904 if (any_added)
4905 {
112e8700 4906 current_uiout->text (_(" Inferior loaded "));
10f489e5 4907 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4908 bool first = true;
52941706 4909 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4910 {
bcb430e4 4911 if (!first)
112e8700 4912 current_uiout->text (" ");
bcb430e4 4913 first = false;
112e8700
SM
4914 current_uiout->field_string ("library", iter->so_name);
4915 current_uiout->text ("\n");
edcc5120 4916 }
edcc5120
TT
4917 }
4918}
4919
e514a9d6
JM
4920/* Print a message indicating what happened. This is called from
4921 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4922 list - a list of the eventpoints that caused this stop. KIND is
4923 the target_waitkind for the stopping event. This
e514a9d6
JM
4924 routine calls the generic print routine for printing a message
4925 about reasons for stopping. This will print (for example) the
4926 "Breakpoint n," part of the output. The return value of this
4927 routine is one of:
c906108c 4928
4a64f543 4929 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4930 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4931 code to print the location. An example is
c5aa993b
JM
4932 "Breakpoint 1, " which should be followed by
4933 the location.
917317f4 4934 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4935 to also print the location part of the message.
4936 An example is the catch/throw messages, which
4a64f543 4937 don't require a location appended to the end.
917317f4 4938 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4939 further info to be printed. */
c906108c 4940
917317f4 4941enum print_stop_action
403c71fd 4942bpstat_print (bpstat *bs, target_waitkind kind)
c906108c 4943{
f486487f 4944 enum print_stop_action val;
c5aa993b 4945
c906108c 4946 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4947 (Currently all watchpoints go on the bpstat whether hit or not.
4948 That probably could (should) be changed, provided care is taken
c906108c 4949 with respect to bpstat_explains_signal). */
e514a9d6
JM
4950 for (; bs; bs = bs->next)
4951 {
4952 val = print_bp_stop_message (bs);
4953 if (val == PRINT_SRC_ONLY
78805ff8 4954 || val == PRINT_SRC_AND_LOC
e514a9d6
JM
4955 || val == PRINT_NOTHING)
4956 return val;
4957 }
c906108c 4958
36dfb11c
TT
4959 /* If we had hit a shared library event breakpoint,
4960 print_bp_stop_message would print out this message. If we hit an
4961 OS-level shared library event, do the same thing. */
4962 if (kind == TARGET_WAITKIND_LOADED)
4963 {
4e9e993a 4964 print_solib_event (false);
36dfb11c
TT
4965 return PRINT_NOTHING;
4966 }
4967
e514a9d6 4968 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4969 with and nothing was printed. */
917317f4 4970 return PRINT_UNKNOWN;
c906108c
SS
4971}
4972
bf469271 4973/* Evaluate the boolean expression EXP and return the result. */
c906108c 4974
bf469271
PA
4975static bool
4976breakpoint_cond_eval (expression *exp)
c906108c 4977{
65558ca5
TT
4978 scoped_value_mark mark;
4979 return value_true (evaluate_expression (exp));
c906108c
SS
4980}
4981
5760d0ab 4982/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4983
313f3b21 4984bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 4985 : next (NULL),
b6433ede 4986 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4987 breakpoint_at (bl->owner),
4988 commands (NULL),
04afa70c
TT
4989 print (0),
4990 stop (0),
4991 print_it (print_it_normal)
c906108c 4992{
04afa70c
TT
4993 **bs_link_pointer = this;
4994 *bs_link_pointer = &next;
4995}
4996
313f3b21 4997bpstat::bpstat ()
04afa70c 4998 : next (NULL),
04afa70c
TT
4999 breakpoint_at (NULL),
5000 commands (NULL),
04afa70c
TT
5001 print (0),
5002 stop (0),
5003 print_it (print_it_normal)
5004{
c906108c
SS
5005}
5006\f
d983da9c
DJ
5007/* The target has stopped with waitstatus WS. Check if any hardware
5008 watchpoints have triggered, according to the target. */
5009
5010int
c272a98c 5011watchpoints_triggered (const target_waitstatus &ws)
d983da9c 5012{
57810aa7 5013 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 5014 CORE_ADDR addr;
d983da9c
DJ
5015
5016 if (!stopped_by_watchpoint)
5017 {
5018 /* We were not stopped by a watchpoint. Mark all watchpoints
5019 as not triggered. */
43892fdf 5020 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5021 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5022 {
5023 struct watchpoint *w = (struct watchpoint *) b;
5024
5025 w->watchpoint_triggered = watch_triggered_no;
5026 }
d983da9c
DJ
5027
5028 return 0;
5029 }
5030
328d42d8 5031 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
5032 {
5033 /* We were stopped by a watchpoint, but we don't know where.
5034 Mark all watchpoints as unknown. */
43892fdf 5035 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5036 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5037 {
5038 struct watchpoint *w = (struct watchpoint *) b;
5039
5040 w->watchpoint_triggered = watch_triggered_unknown;
5041 }
d983da9c 5042
3c4797ba 5043 return 1;
d983da9c
DJ
5044 }
5045
5046 /* The target could report the data address. Mark watchpoints
5047 affected by this data address as triggered, and all others as not
5048 triggered. */
5049
43892fdf 5050 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5051 if (is_hardware_watchpoint (b))
d983da9c 5052 {
3a5c3e22 5053 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 5054
3a5c3e22 5055 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 5056 for (bp_location *loc : b->locations ())
9c06b0b4 5057 {
3a5c3e22 5058 if (is_masked_watchpoint (b))
9c06b0b4 5059 {
3a5c3e22
PA
5060 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5061 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5062
5063 if (newaddr == start)
5064 {
3a5c3e22 5065 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5066 break;
5067 }
5068 }
5069 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
5070 else if (target_watchpoint_addr_within_range
5071 (current_inferior ()->top_target (), addr, loc->address,
5072 loc->length))
9c06b0b4 5073 {
3a5c3e22 5074 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5075 break;
5076 }
5077 }
d983da9c
DJ
5078 }
5079
5080 return 1;
5081}
5082
bf469271
PA
5083/* Possible return values for watchpoint_check. */
5084enum wp_check_result
5085 {
5086 /* The watchpoint has been deleted. */
5087 WP_DELETED = 1,
5088
5089 /* The value has changed. */
5090 WP_VALUE_CHANGED = 2,
5091
5092 /* The value has not changed. */
5093 WP_VALUE_NOT_CHANGED = 3,
5094
5095 /* Ignore this watchpoint, no matter if the value changed or not. */
5096 WP_IGNORE = 4,
5097 };
c906108c
SS
5098
5099#define BP_TEMPFLAG 1
5100#define BP_HARDWAREFLAG 2
5101
4a64f543 5102/* Evaluate watchpoint condition expression and check if its value
bf469271 5103 changed. */
553e4c11 5104
bf469271 5105static wp_check_result
313f3b21 5106watchpoint_check (bpstat *bs)
c906108c 5107{
3a5c3e22 5108 struct watchpoint *b;
bd2b40ac 5109 frame_info_ptr fr;
d8de7963 5110 bool within_current_scope;
c906108c 5111
f431efe5 5112 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5113 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5114 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5115
f6bc2008
PA
5116 /* If this is a local watchpoint, we only want to check if the
5117 watchpoint frame is in scope if the current thread is the thread
5118 that was used to create the watchpoint. */
5119 if (!watchpoint_in_thread_scope (b))
60e1c644 5120 return WP_IGNORE;
f6bc2008 5121
c906108c 5122 if (b->exp_valid_block == NULL)
d8de7963 5123 within_current_scope = true;
c906108c
SS
5124 else
5125 {
bd2b40ac 5126 frame_info_ptr frame = get_current_frame ();
edb3359d
DJ
5127 struct gdbarch *frame_arch = get_frame_arch (frame);
5128 CORE_ADDR frame_pc = get_frame_pc (frame);
5129
c9cf6e20 5130 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5131 still in the function but the stack frame has already been
5132 invalidated. Since we can't rely on the values of local
5133 variables after the stack has been destroyed, we are treating
5134 the watchpoint in that state as `not changed' without further
5135 checking. Don't mark watchpoints as changed if the current
5136 frame is in an epilogue - even if they are in some other
5137 frame, our view of the stack is likely to be wrong and
5138 frame_find_by_id could error out. */
c9cf6e20 5139 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5140 return WP_IGNORE;
a0f49112 5141
101dcfbe 5142 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5143 within_current_scope = (fr != NULL);
69fbadd5
DJ
5144
5145 /* If we've gotten confused in the unwinder, we might have
5146 returned a frame that can't describe this variable. */
edb3359d
DJ
5147 if (within_current_scope)
5148 {
5149 struct symbol *function;
5150
5151 function = get_frame_function (fr);
5152 if (function == NULL
4aeddc50 5153 || !contained_in (b->exp_valid_block, function->value_block ()))
d8de7963 5154 within_current_scope = false;
edb3359d 5155 }
69fbadd5 5156
edb3359d 5157 if (within_current_scope)
c906108c
SS
5158 /* If we end up stopping, the current frame will get selected
5159 in normal_stop. So this call to select_frame won't affect
5160 the user. */
0f7d239c 5161 select_frame (fr);
c906108c 5162 }
c5aa993b 5163
c906108c
SS
5164 if (within_current_scope)
5165 {
4a64f543 5166 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5167 time before we return to the command level and call
5168 free_all_values. We can't call free_all_values because we
5169 might be in the middle of evaluating a function call. */
c906108c 5170
9c06b0b4 5171 struct value *mark;
fa4727a6
DJ
5172 struct value *new_val;
5173
c1fc2657 5174 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5175 /* Since we don't know the exact trigger address (from
5176 stopped_data_address), just tell the user we've triggered
5177 a mask watchpoint. */
5178 return WP_VALUE_CHANGED;
5179
5180 mark = value_mark ();
1eaebe02 5181 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5182 NULL, NULL, false);
218d2fc6 5183
bb9d5f81
PP
5184 if (b->val_bitsize != 0)
5185 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5186
4a64f543
MS
5187 /* We use value_equal_contents instead of value_equal because
5188 the latter coerces an array to a pointer, thus comparing just
5189 the address of the array instead of its contents. This is
5190 not what we want. */
fa4727a6 5191 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5192 || (b->val != NULL && !value_equal_contents (b->val.get (),
5193 new_val)))
c906108c 5194 {
c906108c 5195 bs->old_val = b->val;
850645cf 5196 b->val = release_value (new_val);
4c1d86d9 5197 b->val_valid = true;
850645cf
TT
5198 if (new_val != NULL)
5199 value_free_to_mark (mark);
c906108c
SS
5200 return WP_VALUE_CHANGED;
5201 }
5202 else
5203 {
60e1c644 5204 /* Nothing changed. */
c906108c 5205 value_free_to_mark (mark);
c906108c
SS
5206 return WP_VALUE_NOT_CHANGED;
5207 }
5208 }
5209 else
5210 {
5211 /* This seems like the only logical thing to do because
dda83cd7
SM
5212 if we temporarily ignored the watchpoint, then when
5213 we reenter the block in which it is valid it contains
5214 garbage (in the case of a function, it may have two
5215 garbage values, one before and one after the prologue).
5216 So we can't even detect the first assignment to it and
5217 watch after that (since the garbage may or may not equal
5218 the first value assigned). */
348d480f 5219 /* We print all the stop information in
c47614fe
TT
5220 breakpointprint_it, but in this case, by the time we
5221 call breakpoint->print_it this bp will be deleted
348d480f
PA
5222 already. So we have no choice but print the information
5223 here. */
468afe6c 5224
0e454242 5225 SWITCH_THRU_ALL_UIS ()
dda83cd7 5226 {
468afe6c
PA
5227 struct ui_out *uiout = current_uiout;
5228
112e8700
SM
5229 if (uiout->is_mi_like_p ())
5230 uiout->field_string
5231 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5232 uiout->message ("\nWatchpoint %pF deleted because the program has "
5233 "left the block in\n"
5234 "which its expression is valid.\n",
5235 signed_field ("wpnum", b->number));
468afe6c 5236 }
4ce44c66 5237
cdac0397 5238 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5239 b->commands = NULL;
d0fb5eae 5240 watchpoint_del_at_next_stop (b);
c906108c
SS
5241
5242 return WP_DELETED;
5243 }
5244}
5245
18a18393 5246/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5247 breakpoint location BL. This function does not check if we should
5248 stop, only if BL explains the stop. */
5249
d8de7963 5250static bool
6c95b8df 5251bpstat_check_location (const struct bp_location *bl,
accd0bcd 5252 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5253 const target_waitstatus &ws)
18a18393
VP
5254{
5255 struct breakpoint *b = bl->owner;
5256
348d480f 5257 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5258 gdb_assert (b != NULL);
5259
c47614fe 5260 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5261}
5262
3a5c3e22 5263/* Determine if the watched values have actually changed, and we
4ec2227a 5264 should stop. If not, set BS->stop to false. */
3a5c3e22 5265
18a18393 5266static void
313f3b21 5267bpstat_check_watchpoint (bpstat *bs)
18a18393 5268{
2bdf28a0 5269 const struct bp_location *bl;
3a5c3e22 5270 struct watchpoint *b;
2bdf28a0
JK
5271
5272 /* BS is built for existing struct breakpoint. */
b6433ede 5273 bl = bs->bp_location_at.get ();
2bdf28a0 5274 gdb_assert (bl != NULL);
3a5c3e22 5275 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5276 gdb_assert (b != NULL);
18a18393 5277
18a18393 5278 {
d8de7963
AB
5279 bool must_check_value = false;
5280
c1fc2657 5281 if (b->type == bp_watchpoint)
18a18393
VP
5282 /* For a software watchpoint, we must always check the
5283 watched value. */
d8de7963 5284 must_check_value = true;
18a18393
VP
5285 else if (b->watchpoint_triggered == watch_triggered_yes)
5286 /* We have a hardware watchpoint (read, write, or access)
5287 and the target earlier reported an address watched by
5288 this watchpoint. */
d8de7963 5289 must_check_value = true;
18a18393 5290 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5291 && b->type == bp_hardware_watchpoint)
18a18393
VP
5292 /* We were stopped by a hardware watchpoint, but the target could
5293 not report the data address. We must check the watchpoint's
5294 value. Access and read watchpoints are out of luck; without
5295 a data address, we can't figure it out. */
d8de7963 5296 must_check_value = true;
3a5c3e22 5297
18a18393
VP
5298 if (must_check_value)
5299 {
bf469271
PA
5300 wp_check_result e;
5301
a70b8144 5302 try
bf469271
PA
5303 {
5304 e = watchpoint_check (bs);
5305 }
230d2906 5306 catch (const gdb_exception &ex)
bf469271
PA
5307 {
5308 exception_fprintf (gdb_stderr, ex,
5309 "Error evaluating expression "
5310 "for watchpoint %d\n",
5311 b->number);
5312
5313 SWITCH_THRU_ALL_UIS ()
5314 {
6cb06a8c
TT
5315 gdb_printf (_("Watchpoint %d deleted.\n"),
5316 b->number);
bf469271
PA
5317 }
5318 watchpoint_del_at_next_stop (b);
5319 e = WP_DELETED;
5320 }
bf469271 5321
18a18393
VP
5322 switch (e)
5323 {
5324 case WP_DELETED:
5325 /* We've already printed what needs to be printed. */
5326 bs->print_it = print_it_done;
5327 /* Stop. */
5328 break;
60e1c644
PA
5329 case WP_IGNORE:
5330 bs->print_it = print_it_noop;
4ec2227a 5331 bs->stop = false;
60e1c644 5332 break;
18a18393 5333 case WP_VALUE_CHANGED:
c1fc2657 5334 if (b->type == bp_read_watchpoint)
18a18393 5335 {
85d721b8
PA
5336 /* There are two cases to consider here:
5337
4a64f543 5338 1. We're watching the triggered memory for reads.
85d721b8
PA
5339 In that case, trust the target, and always report
5340 the watchpoint hit to the user. Even though
5341 reads don't cause value changes, the value may
5342 have changed since the last time it was read, and
5343 since we're not trapping writes, we will not see
5344 those, and as such we should ignore our notion of
5345 old value.
5346
4a64f543 5347 2. We're watching the triggered memory for both
85d721b8
PA
5348 reads and writes. There are two ways this may
5349 happen:
5350
4a64f543 5351 2.1. This is a target that can't break on data
85d721b8
PA
5352 reads only, but can break on accesses (reads or
5353 writes), such as e.g., x86. We detect this case
5354 at the time we try to insert read watchpoints.
5355
4a64f543 5356 2.2. Otherwise, the target supports read
85d721b8
PA
5357 watchpoints, but, the user set an access or write
5358 watchpoint watching the same memory as this read
5359 watchpoint.
5360
5361 If we're watching memory writes as well as reads,
5362 ignore watchpoint hits when we find that the
5363 value hasn't changed, as reads don't cause
5364 changes. This still gives false positives when
5365 the program writes the same value to memory as
5366 what there was already in memory (we will confuse
5367 it for a read), but it's much better than
5368 nothing. */
5369
5370 int other_write_watchpoint = 0;
5371
5372 if (bl->watchpoint_type == hw_read)
5373 {
43892fdf 5374 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5375 if (other_b->type == bp_hardware_watchpoint
5376 || other_b->type == bp_access_watchpoint)
85d721b8 5377 {
3a5c3e22
PA
5378 struct watchpoint *other_w =
5379 (struct watchpoint *) other_b;
5380
5381 if (other_w->watchpoint_triggered
5382 == watch_triggered_yes)
5383 {
5384 other_write_watchpoint = 1;
5385 break;
5386 }
85d721b8
PA
5387 }
5388 }
5389
5390 if (other_write_watchpoint
5391 || bl->watchpoint_type == hw_access)
5392 {
5393 /* We're watching the same memory for writes,
5394 and the value changed since the last time we
5395 updated it, so this trap must be for a write.
5396 Ignore it. */
5397 bs->print_it = print_it_noop;
4ec2227a 5398 bs->stop = false;
85d721b8 5399 }
18a18393
VP
5400 }
5401 break;
5402 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5403 if (b->type == bp_hardware_watchpoint
5404 || b->type == bp_watchpoint)
18a18393
VP
5405 {
5406 /* Don't stop: write watchpoints shouldn't fire if
5407 the value hasn't changed. */
5408 bs->print_it = print_it_noop;
4ec2227a 5409 bs->stop = false;
18a18393
VP
5410 }
5411 /* Stop. */
5412 break;
5413 default:
5414 /* Can't happen. */
18a18393
VP
5415 break;
5416 }
5417 }
d8de7963 5418 else /* !must_check_value */
18a18393
VP
5419 {
5420 /* This is a case where some watchpoint(s) triggered, but
5421 not at the address of this watchpoint, or else no
5422 watchpoint triggered after all. So don't print
5423 anything for this watchpoint. */
5424 bs->print_it = print_it_noop;
4ec2227a 5425 bs->stop = false;
18a18393
VP
5426 }
5427 }
5428}
5429
7d4df6a4
DE
5430/* For breakpoints that are currently marked as telling gdb to stop,
5431 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5432 of breakpoint referred to by BS. If we should not stop for this
5433 breakpoint, set BS->stop to 0. */
f431efe5 5434
18a18393 5435static void
313f3b21 5436bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5437{
d8a77e4c
AB
5438 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5439
2bdf28a0
JK
5440 const struct bp_location *bl;
5441 struct breakpoint *b;
bf469271
PA
5442 /* Assume stop. */
5443 bool condition_result = true;
7d4df6a4
DE
5444 struct expression *cond;
5445
5446 gdb_assert (bs->stop);
2bdf28a0
JK
5447
5448 /* BS is built for existing struct breakpoint. */
b6433ede 5449 bl = bs->bp_location_at.get ();
2bdf28a0 5450 gdb_assert (bl != NULL);
f431efe5 5451 b = bs->breakpoint_at;
2bdf28a0 5452 gdb_assert (b != NULL);
18a18393 5453
d8a77e4c
AB
5454 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5455 thread->ptid.to_string ().c_str (),
5456 b->number, find_loc_num_by_location (bl));
5457
b775012e
LM
5458 /* Even if the target evaluated the condition on its end and notified GDB, we
5459 need to do so again since GDB does not know if we stopped due to a
5460 breakpoint or a single step breakpoint. */
5461
18a18393 5462 if (frame_id_p (b->frame_id)
a0cbd650 5463 && b->frame_id != get_stack_frame_id (get_current_frame ()))
18a18393 5464 {
d8a77e4c
AB
5465 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5466 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5467 b->frame_id.to_string ().c_str ());
4ec2227a 5468 bs->stop = false;
7d4df6a4
DE
5469 return;
5470 }
60e1c644 5471
12ab52e9
PA
5472 /* If this is a thread/task-specific breakpoint, don't waste cpu
5473 evaluating the condition if this isn't the specified
5474 thread/task. */
00431a78 5475 if ((b->thread != -1 && b->thread != thread->global_num)
2ecee236 5476 || (b->task != -1 && b->task != ada_get_task_number (thread)))
6c1b0f7b 5477 {
d8a77e4c 5478 infrun_debug_printf ("incorrect thread or task, not stopping");
4ec2227a 5479 bs->stop = false;
6c1b0f7b
DE
5480 return;
5481 }
5482
6dddc817
DE
5483 /* Evaluate extension language breakpoints that have a "stop" method
5484 implemented. */
5485 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5486
7d4df6a4
DE
5487 if (is_watchpoint (b))
5488 {
5489 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5490
4d01a485 5491 cond = w->cond_exp.get ();
7d4df6a4
DE
5492 }
5493 else
4d01a485 5494 cond = bl->cond.get ();
60e1c644 5495
80e0c6dc 5496 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
7d4df6a4 5497 {
d8de7963 5498 bool within_current_scope = true;
7d4df6a4 5499 struct watchpoint * w;
60e1c644 5500
65558ca5
TT
5501 /* We use scoped_value_mark because it could be a long time
5502 before we return to the command level and call
5503 free_all_values. We can't call free_all_values because we
5504 might be in the middle of evaluating a function call. */
5505 scoped_value_mark mark;
7d4df6a4
DE
5506
5507 if (is_watchpoint (b))
5508 w = (struct watchpoint *) b;
5509 else
5510 w = NULL;
5511
5512 /* Need to select the frame, with all that implies so that
5513 the conditions will have the right context. Because we
5514 use the frame, we will not see an inlined function's
5515 variables when we arrive at a breakpoint at the start
5516 of the inlined function; the current frame will be the
5517 call site. */
5518 if (w == NULL || w->cond_exp_valid_block == NULL)
5519 select_frame (get_current_frame ());
5520 else
18a18393 5521 {
bd2b40ac 5522 frame_info_ptr frame;
7d4df6a4
DE
5523
5524 /* For local watchpoint expressions, which particular
5525 instance of a local is being watched matters, so we
5526 keep track of the frame to evaluate the expression
5527 in. To evaluate the condition however, it doesn't
5528 really matter which instantiation of the function
5529 where the condition makes sense triggers the
5530 watchpoint. This allows an expression like "watch
5531 global if q > 10" set in `func', catch writes to
5532 global on all threads that call `func', or catch
5533 writes on all recursive calls of `func' by a single
5534 thread. We simply always evaluate the condition in
5535 the innermost frame that's executing where it makes
5536 sense to evaluate the condition. It seems
5537 intuitive. */
5538 frame = block_innermost_frame (w->cond_exp_valid_block);
5539 if (frame != NULL)
5540 select_frame (frame);
5541 else
d8de7963 5542 within_current_scope = false;
18a18393 5543 }
7d4df6a4 5544 if (within_current_scope)
bf469271 5545 {
a70b8144 5546 try
bf469271
PA
5547 {
5548 condition_result = breakpoint_cond_eval (cond);
5549 }
230d2906 5550 catch (const gdb_exception &ex)
bf469271
PA
5551 {
5552 exception_fprintf (gdb_stderr, ex,
5553 "Error in testing breakpoint condition:\n");
5554 }
bf469271 5555 }
7d4df6a4 5556 else
18a18393 5557 {
7d4df6a4
DE
5558 warning (_("Watchpoint condition cannot be tested "
5559 "in the current scope"));
5560 /* If we failed to set the right context for this
5561 watchpoint, unconditionally report it. */
18a18393 5562 }
7d4df6a4 5563 /* FIXME-someday, should give breakpoint #. */
18a18393 5564 }
7d4df6a4 5565
80e0c6dc 5566 if (cond != nullptr && !condition_result)
7d4df6a4 5567 {
d8a77e4c 5568 infrun_debug_printf ("condition_result = false, not stopping");
4ec2227a 5569 bs->stop = false;
d8a77e4c 5570 return;
7d4df6a4 5571 }
7d4df6a4
DE
5572 else if (b->ignore_count > 0)
5573 {
d8a77e4c
AB
5574 infrun_debug_printf ("ignore count %d, not stopping",
5575 b->ignore_count);
7d4df6a4 5576 b->ignore_count--;
4ec2227a 5577 bs->stop = false;
7d4df6a4
DE
5578 /* Increase the hit count even though we don't stop. */
5579 ++(b->hit_count);
76727919 5580 gdb::observers::breakpoint_modified.notify (b);
d8a77e4c
AB
5581 return;
5582 }
5583
5584 if (bs->stop)
5585 infrun_debug_printf ("stopping at this breakpoint");
5586 else
5587 infrun_debug_printf ("not stopping at this breakpoint");
18a18393
VP
5588}
5589
1cf4d951
PA
5590/* Returns true if we need to track moribund locations of LOC's type
5591 on the current target. */
5592
d8de7963 5593static bool
b2ff9ed3 5594need_moribund_for_location_type (const struct bp_location *loc)
1cf4d951
PA
5595{
5596 return ((loc->loc_type == bp_loc_software_breakpoint
5597 && !target_supports_stopped_by_sw_breakpoint ())
5598 || (loc->loc_type == bp_loc_hardware_breakpoint
5599 && !target_supports_stopped_by_hw_breakpoint ()));
5600}
5601
ddfe970e 5602/* See breakpoint.h. */
c906108c 5603
313f3b21 5604bpstat *
ddfe970e 5605build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5606 const target_waitstatus &ws)
c906108c 5607{
313f3b21 5608 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5609
43892fdf 5610 for (breakpoint *b : all_breakpoints ())
429374b8 5611 {
1a853c52 5612 if (!breakpoint_enabled (b))
429374b8 5613 continue;
a5606eee 5614
40cb8ca5 5615 for (bp_location *bl : b->locations ())
429374b8 5616 {
4a64f543
MS
5617 /* For hardware watchpoints, we look only at the first
5618 location. The watchpoint_check function will work on the
5619 entire expression, not the individual locations. For
5620 read watchpoints, the watchpoints_triggered function has
5621 checked all locations already. */
429374b8
JK
5622 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5623 break;
18a18393 5624
b5fa468f 5625 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5626 continue;
c5aa993b 5627
09ac7c10 5628 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5629 continue;
c5aa993b 5630
4a64f543
MS
5631 /* Come here if it's a watchpoint, or if the break address
5632 matches. */
c5aa993b 5633
313f3b21 5634 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5635 explain stop. */
c5aa993b 5636
f431efe5
PA
5637 /* Assume we stop. Should we find a watchpoint that is not
5638 actually triggered, or if the condition of the breakpoint
5639 evaluates as false, we'll reset 'stop' to 0. */
4ec2227a
TT
5640 bs->stop = true;
5641 bs->print = true;
d983da9c 5642
f431efe5
PA
5643 /* If this is a scope breakpoint, mark the associated
5644 watchpoint as triggered so that we will handle the
5645 out-of-scope event. We'll get to the watchpoint next
5646 iteration. */
d0fb5eae 5647 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5648 {
5649 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5650
5651 w->watchpoint_triggered = watch_triggered_yes;
5652 }
f431efe5
PA
5653 }
5654 }
5655
7c16b83e 5656 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5657 if (!target_supports_stopped_by_sw_breakpoint ()
5658 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5659 {
1123588c 5660 for (bp_location *loc : moribund_locations)
f431efe5 5661 {
1cf4d951
PA
5662 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5663 && need_moribund_for_location_type (loc))
5664 {
313f3b21 5665 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951 5666 /* For hits of moribund locations, we should just proceed. */
4ec2227a
TT
5667 bs->stop = false;
5668 bs->print = false;
1cf4d951
PA
5669 bs->print_it = print_it_noop;
5670 }
f431efe5
PA
5671 }
5672 }
5673
ddfe970e
KS
5674 return bs_head;
5675}
5676
5677/* See breakpoint.h. */
5678
313f3b21 5679bpstat *
ddfe970e 5680bpstat_stop_status (const address_space *aspace,
00431a78 5681 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5682 const target_waitstatus &ws,
313f3b21 5683 bpstat *stop_chain)
ddfe970e
KS
5684{
5685 struct breakpoint *b = NULL;
5686 /* First item of allocated bpstat's. */
313f3b21
SM
5687 bpstat *bs_head = stop_chain;
5688 bpstat *bs;
ddfe970e
KS
5689 int need_remove_insert;
5690 int removed_any;
5691
5692 /* First, build the bpstat chain with locations that explain a
5693 target stop, while being careful to not set the target running,
5694 as that may invalidate locations (in particular watchpoint
5695 locations are recreated). Resuming will happen here with
5696 breakpoint conditions or watchpoint expressions that include
5697 inferior function calls. */
5698 if (bs_head == NULL)
5699 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5700
edcc5120
TT
5701 /* A bit of special processing for shlib breakpoints. We need to
5702 process solib loading here, so that the lists of loaded and
5703 unloaded libraries are correct before we handle "catch load" and
5704 "catch unload". */
5705 for (bs = bs_head; bs != NULL; bs = bs->next)
5706 {
5d268276 5707 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5708 {
5709 handle_solib_event ();
5710 break;
5711 }
5712 }
5713
f431efe5
PA
5714 /* Now go through the locations that caused the target to stop, and
5715 check whether we're interested in reporting this stop to higher
5716 layers, or whether we should resume the target transparently. */
5717
5718 removed_any = 0;
5719
5760d0ab 5720 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5721 {
5722 if (!bs->stop)
5723 continue;
5724
f431efe5 5725 b = bs->breakpoint_at;
c47614fe 5726 b->check_status (bs);
348d480f 5727 if (bs->stop)
28010a5d 5728 {
00431a78 5729 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5730
429374b8
JK
5731 if (bs->stop)
5732 {
5733 ++(b->hit_count);
c906108c 5734
4a64f543 5735 /* We will stop here. */
429374b8
JK
5736 if (b->disposition == disp_disable)
5737 {
816338b5 5738 --(b->enable_count);
1a853c52 5739 if (b->enable_count <= 0)
429374b8 5740 b->enable_state = bp_disabled;
f431efe5 5741 removed_any = 1;
429374b8 5742 }
bd7ccaa9 5743 gdb::observers::breakpoint_modified.notify (b);
429374b8 5744 if (b->silent)
4ec2227a 5745 bs->print = false;
429374b8 5746 bs->commands = b->commands;
abf85f46 5747 if (command_line_is_silent (bs->commands
d1b0a7bf 5748 ? bs->commands.get () : NULL))
4ec2227a 5749 bs->print = false;
9d6e6e84 5750
c47614fe 5751 b->after_condition_true (bs);
429374b8
JK
5752 }
5753
348d480f 5754 }
a9b3a50f
PA
5755
5756 /* Print nothing for this entry if we don't stop or don't
5757 print. */
5758 if (!bs->stop || !bs->print)
5759 bs->print_it = print_it_noop;
429374b8 5760 }
876fa593 5761
d983da9c
DJ
5762 /* If we aren't stopping, the value of some hardware watchpoint may
5763 not have changed, but the intermediate memory locations we are
5764 watching may have. Don't bother if we're stopping; this will get
5765 done later. */
d832cb68 5766 need_remove_insert = 0;
5760d0ab
JK
5767 if (! bpstat_causes_stop (bs_head))
5768 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5769 if (!bs->stop
f431efe5
PA
5770 && bs->breakpoint_at
5771 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5772 {
3a5c3e22
PA
5773 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5774
d8de7963 5775 update_watchpoint (w, false /* don't reparse. */);
d832cb68 5776 need_remove_insert = 1;
d983da9c
DJ
5777 }
5778
d832cb68 5779 if (need_remove_insert)
44702360 5780 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5781 else if (removed_any)
44702360 5782 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5783
5760d0ab 5784 return bs_head;
c906108c 5785}
628fe4e4 5786
d37e0847
PA
5787/* See breakpoint.h. */
5788
5789bpstat *
5790bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5791 thread_info *thread, const target_waitstatus &ws)
5792{
5793 gdb_assert (!target_stopped_by_watchpoint ());
5794
5795 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5796 previous stop to avoid confusing bpstat_stop_status. */
5797 watchpoints_triggered (ws);
5798
5799 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5800}
5801
628fe4e4 5802static void
61c26be8 5803handle_jit_event (CORE_ADDR address)
628fe4e4 5804{
628fe4e4
JK
5805 struct gdbarch *gdbarch;
5806
1eb8556f 5807 infrun_debug_printf ("handling bp_jit_event");
243a9253 5808
628fe4e4
JK
5809 /* Switch terminal for any messages produced by
5810 breakpoint_re_set. */
223ffa71 5811 target_terminal::ours_for_output ();
628fe4e4 5812
61c26be8
MS
5813 gdbarch = get_frame_arch (get_current_frame ());
5814 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5815 thus it is expected that its objectfile can be found through
5816 minimal symbol lookup. If it doesn't work (and assert fails), it
5817 most likely means that `jit_breakpoint_re_set` was changes and this
5818 function needs to be updated too. */
5819 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5820 gdb_assert (jit_bp_sym.objfile != nullptr);
508ccf9b
TV
5821 objfile *objfile = jit_bp_sym.objfile;
5822 if (objfile->separate_debug_objfile_backlink)
5823 objfile = objfile->separate_debug_objfile_backlink;
5824 jit_event_handler (gdbarch, objfile);
628fe4e4 5825
223ffa71 5826 target_terminal::inferior ();
628fe4e4
JK
5827}
5828
5829/* Prepare WHAT final decision for infrun. */
5830
5831/* Decide what infrun needs to do with this bpstat. */
5832
c906108c 5833struct bpstat_what
313f3b21 5834bpstat_what (bpstat *bs_head)
c906108c 5835{
c906108c 5836 struct bpstat_what retval;
313f3b21 5837 bpstat *bs;
c906108c 5838
628fe4e4 5839 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5840 retval.call_dummy = STOP_NONE;
e2d0f980 5841 retval.is_longjmp = false;
628fe4e4 5842
0e30163f 5843 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5844 {
628fe4e4
JK
5845 /* Extract this BS's action. After processing each BS, we check
5846 if its action overrides all we've seem so far. */
5847 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5848 enum bptype bptype;
5849
c906108c 5850 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5851 {
5852 /* I suspect this can happen if it was a momentary
5853 breakpoint which has since been deleted. */
5854 bptype = bp_none;
5855 }
20874c92 5856 else
f431efe5 5857 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5858
5859 switch (bptype)
c906108c
SS
5860 {
5861 case bp_none:
628fe4e4 5862 break;
c906108c
SS
5863 case bp_breakpoint:
5864 case bp_hardware_breakpoint:
7c16b83e 5865 case bp_single_step:
c906108c
SS
5866 case bp_until:
5867 case bp_finish:
a9b3a50f 5868 case bp_shlib_event:
c906108c
SS
5869 if (bs->stop)
5870 {
5871 if (bs->print)
628fe4e4 5872 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5873 else
628fe4e4 5874 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5875 }
5876 else
628fe4e4 5877 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5878 break;
5879 case bp_watchpoint:
5880 case bp_hardware_watchpoint:
5881 case bp_read_watchpoint:
5882 case bp_access_watchpoint:
5883 if (bs->stop)
5884 {
5885 if (bs->print)
628fe4e4 5886 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5887 else
628fe4e4 5888 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5889 }
5890 else
628fe4e4
JK
5891 {
5892 /* There was a watchpoint, but we're not stopping.
5893 This requires no further action. */
5894 }
c906108c
SS
5895 break;
5896 case bp_longjmp:
e2e4d78b 5897 case bp_longjmp_call_dummy:
186c406b 5898 case bp_exception:
0a39bb32
PA
5899 if (bs->stop)
5900 {
5901 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5902 retval.is_longjmp = bptype != bp_exception;
5903 }
5904 else
5905 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5906 break;
5907 case bp_longjmp_resume:
186c406b 5908 case bp_exception_resume:
0a39bb32
PA
5909 if (bs->stop)
5910 {
5911 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5912 retval.is_longjmp = bptype == bp_longjmp_resume;
5913 }
5914 else
5915 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5916 break;
5917 case bp_step_resume:
5918 if (bs->stop)
628fe4e4
JK
5919 this_action = BPSTAT_WHAT_STEP_RESUME;
5920 else
c906108c 5921 {
628fe4e4
JK
5922 /* It is for the wrong frame. */
5923 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5924 }
c906108c 5925 break;
2c03e5be
PA
5926 case bp_hp_step_resume:
5927 if (bs->stop)
5928 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5929 else
5930 {
5931 /* It is for the wrong frame. */
5932 this_action = BPSTAT_WHAT_SINGLE;
5933 }
5934 break;
c906108c 5935 case bp_watchpoint_scope:
c4093a6a 5936 case bp_thread_event:
1900040c 5937 case bp_overlay_event:
0fd8e87f 5938 case bp_longjmp_master:
aa7d318d 5939 case bp_std_terminate_master:
186c406b 5940 case bp_exception_master:
628fe4e4 5941 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5942 break;
ce78b96d 5943 case bp_catchpoint:
c5aa993b
JM
5944 if (bs->stop)
5945 {
5946 if (bs->print)
628fe4e4 5947 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5948 else
628fe4e4 5949 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5950 }
5951 else
628fe4e4 5952 {
cb1e4e32
PA
5953 /* Some catchpoints are implemented with breakpoints.
5954 For those, we need to step over the breakpoint. */
6e14e441
PA
5955 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5956 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
cb1e4e32 5957 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5958 }
5959 break;
628fe4e4 5960 case bp_jit_event:
628fe4e4 5961 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5962 break;
c906108c 5963 case bp_call_dummy:
53a5351d
JM
5964 /* Make sure the action is stop (silent or noisy),
5965 so infrun.c pops the dummy frame. */
aa7d318d 5966 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5967 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5968 break;
5969 case bp_std_terminate:
5970 /* Make sure the action is stop (silent or noisy),
5971 so infrun.c pops the dummy frame. */
aa7d318d 5972 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5973 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5974 break;
1042e4c0 5975 case bp_tracepoint:
7a697b8d 5976 case bp_fast_tracepoint:
0fb4aa4b 5977 case bp_static_tracepoint:
7b572efb 5978 case bp_static_marker_tracepoint:
1042e4c0
SS
5979 /* Tracepoint hits should not be reported back to GDB, and
5980 if one got through somehow, it should have been filtered
5981 out already. */
f34652de 5982 internal_error (_("bpstat_what: tracepoint encountered"));
0e30163f
JK
5983 break;
5984 case bp_gnu_ifunc_resolver:
5985 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5986 this_action = BPSTAT_WHAT_SINGLE;
5987 break;
5988 case bp_gnu_ifunc_resolver_return:
5989 /* The breakpoint will be removed, execution will restart from the
5990 PC of the former breakpoint. */
5991 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5992 break;
e7e0cddf
SS
5993
5994 case bp_dprintf:
a11cfd87
HZ
5995 if (bs->stop)
5996 this_action = BPSTAT_WHAT_STOP_SILENT;
5997 else
5998 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5999 break;
6000
628fe4e4 6001 default:
f34652de 6002 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 6003 }
628fe4e4 6004
325fac50 6005 retval.main_action = std::max (retval.main_action, this_action);
c906108c 6006 }
628fe4e4 6007
243a9253
PA
6008 return retval;
6009}
628fe4e4 6010
243a9253 6011void
313f3b21 6012bpstat_run_callbacks (bpstat *bs_head)
243a9253 6013{
313f3b21 6014 bpstat *bs;
628fe4e4 6015
0e30163f
JK
6016 for (bs = bs_head; bs != NULL; bs = bs->next)
6017 {
6018 struct breakpoint *b = bs->breakpoint_at;
6019
6020 if (b == NULL)
6021 continue;
6022 switch (b->type)
6023 {
243a9253 6024 case bp_jit_event:
61c26be8 6025 handle_jit_event (bs->bp_location_at->address);
243a9253 6026 break;
0e30163f 6027 case bp_gnu_ifunc_resolver:
74421c0b 6028 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
0e30163f
JK
6029 break;
6030 case bp_gnu_ifunc_resolver_return:
74421c0b 6031 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
0e30163f
JK
6032 break;
6033 }
6034 }
c906108c
SS
6035}
6036
4c462cb0 6037/* See breakpoint.h. */
c906108c 6038
4c462cb0
SM
6039bool
6040bpstat_should_step ()
c906108c 6041{
43892fdf 6042 for (breakpoint *b : all_breakpoints ())
717a8278 6043 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 6044 return true;
43892fdf 6045
4c462cb0 6046 return false;
c906108c
SS
6047}
6048
4c462cb0
SM
6049/* See breakpoint.h. */
6050
6051bool
313f3b21 6052bpstat_causes_stop (bpstat *bs)
67822962
PA
6053{
6054 for (; bs != NULL; bs = bs->next)
6055 if (bs->stop)
4c462cb0 6056 return true;
67822962 6057
4c462cb0 6058 return false;
67822962
PA
6059}
6060
c906108c 6061\f
c5aa993b 6062
6c92c339 6063/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
6064 so it starts at the position corresponding to the table column
6065 named COL_NAME in the currently active table of UIOUT. */
6066
6c92c339 6067static int
170b53b2
UW
6068wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6069{
170b53b2 6070 int i, total_width, width, align;
c5209615 6071 const char *text;
170b53b2
UW
6072
6073 total_width = 0;
112e8700 6074 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6075 {
6076 if (strcmp (text, col_name) == 0)
6c92c339 6077 return total_width;
170b53b2
UW
6078
6079 total_width += width + 1;
6080 }
6081
6c92c339 6082 return 0;
170b53b2
UW
6083}
6084
b775012e
LM
6085/* Determine if the locations of this breakpoint will have their conditions
6086 evaluated by the target, host or a mix of both. Returns the following:
6087
6088 "host": Host evals condition.
6089 "host or target": Host or Target evals condition.
6090 "target": Target evals condition.
6091*/
6092
6093static const char *
a67bcaba 6094bp_condition_evaluator (const breakpoint *b)
b775012e 6095{
b775012e
LM
6096 char host_evals = 0;
6097 char target_evals = 0;
6098
6099 if (!b)
6100 return NULL;
6101
6102 if (!is_breakpoint (b))
6103 return NULL;
6104
6105 if (gdb_evaluates_breakpoint_condition_p ()
6106 || !target_supports_evaluation_of_breakpoint_conditions ())
6107 return condition_evaluation_host;
6108
40cb8ca5 6109 for (bp_location *bl : b->locations ())
b775012e
LM
6110 {
6111 if (bl->cond_bytecode)
6112 target_evals++;
6113 else
6114 host_evals++;
6115 }
6116
6117 if (host_evals && target_evals)
6118 return condition_evaluation_both;
6119 else if (target_evals)
6120 return condition_evaluation_target;
6121 else
6122 return condition_evaluation_host;
6123}
6124
6125/* Determine the breakpoint location's condition evaluator. This is
6126 similar to bp_condition_evaluator, but for locations. */
6127
6128static const char *
b2ff9ed3 6129bp_location_condition_evaluator (const struct bp_location *bl)
b775012e
LM
6130{
6131 if (bl && !is_breakpoint (bl->owner))
6132 return NULL;
6133
6134 if (gdb_evaluates_breakpoint_condition_p ()
6135 || !target_supports_evaluation_of_breakpoint_conditions ())
6136 return condition_evaluation_host;
6137
6138 if (bl && bl->cond_bytecode)
6139 return condition_evaluation_target;
6140 else
6141 return condition_evaluation_host;
6142}
6143
859825b8
JK
6144/* Print the LOC location out of the list of B->LOC locations. */
6145
170b53b2 6146static void
a67bcaba 6147print_breakpoint_location (const breakpoint *b,
170b53b2 6148 struct bp_location *loc)
0d381245 6149{
79a45e25 6150 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6151
6152 scoped_restore_current_program_space restore_pspace;
6c95b8df 6153
859825b8
JK
6154 if (loc != NULL && loc->shlib_disabled)
6155 loc = NULL;
6156
6c95b8df
PA
6157 if (loc != NULL)
6158 set_current_program_space (loc->pspace);
6159
56435ebe 6160 if (b->display_canonical)
709438c7 6161 uiout->field_string ("what", b->locspec->to_string ());
2f202fde 6162 else if (loc && loc->symtab)
0d381245 6163 {
4a27f119
KS
6164 const struct symbol *sym = loc->symbol;
6165
0d381245
VP
6166 if (sym)
6167 {
112e8700 6168 uiout->text ("in ");
987012b8 6169 uiout->field_string ("func", sym->print_name (),
e43b10e1 6170 function_name_style.style ());
112e8700
SM
6171 uiout->text (" ");
6172 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6173 uiout->text ("at ");
0d381245 6174 }
112e8700 6175 uiout->field_string ("file",
cbe56571 6176 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6177 file_name_style.style ());
112e8700 6178 uiout->text (":");
05cba821 6179
112e8700
SM
6180 if (uiout->is_mi_like_p ())
6181 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6182
381befee 6183 uiout->field_signed ("line", loc->line_number);
0d381245 6184 }
859825b8 6185 else if (loc)
0d381245 6186 {
d7e74731 6187 string_file stb;
170b53b2 6188
d7e74731 6189 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6190 demangle, "");
112e8700 6191 uiout->field_stream ("at", stb);
0d381245 6192 }
859825b8 6193 else
f00aae0f 6194 {
709438c7 6195 uiout->field_string ("pending", b->locspec->to_string ());
f00aae0f
KS
6196 /* If extra_string is available, it could be holding a condition
6197 or dprintf arguments. In either case, make sure it is printed,
6198 too, but only for non-MI streams. */
112e8700 6199 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6200 {
6201 if (b->type == bp_dprintf)
112e8700 6202 uiout->text (",");
f00aae0f 6203 else
112e8700 6204 uiout->text (" ");
6f781ee3 6205 uiout->text (b->extra_string.get ());
f00aae0f
KS
6206 }
6207 }
6c95b8df 6208
b775012e
LM
6209 if (loc && is_breakpoint (b)
6210 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6211 && bp_condition_evaluator (b) == condition_evaluation_both)
6212 {
112e8700
SM
6213 uiout->text (" (");
6214 uiout->field_string ("evaluated-by",
b775012e 6215 bp_location_condition_evaluator (loc));
112e8700 6216 uiout->text (")");
b775012e 6217 }
0d381245
VP
6218}
6219
269b11a2
PA
6220static const char *
6221bptype_string (enum bptype type)
c906108c 6222{
c4093a6a
JM
6223 struct ep_type_description
6224 {
6225 enum bptype type;
a121b7c1 6226 const char *description;
c4093a6a
JM
6227 };
6228 static struct ep_type_description bptypes[] =
c906108c 6229 {
c5aa993b
JM
6230 {bp_none, "?deleted?"},
6231 {bp_breakpoint, "breakpoint"},
c906108c 6232 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6233 {bp_single_step, "sw single-step"},
c5aa993b
JM
6234 {bp_until, "until"},
6235 {bp_finish, "finish"},
6236 {bp_watchpoint, "watchpoint"},
c906108c 6237 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6238 {bp_read_watchpoint, "read watchpoint"},
6239 {bp_access_watchpoint, "acc watchpoint"},
6240 {bp_longjmp, "longjmp"},
6241 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6242 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6243 {bp_exception, "exception"},
6244 {bp_exception_resume, "exception resume"},
c5aa993b 6245 {bp_step_resume, "step resume"},
2c03e5be 6246 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6247 {bp_watchpoint_scope, "watchpoint scope"},
6248 {bp_call_dummy, "call dummy"},
aa7d318d 6249 {bp_std_terminate, "std::terminate"},
c5aa993b 6250 {bp_shlib_event, "shlib events"},
c4093a6a 6251 {bp_thread_event, "thread events"},
1900040c 6252 {bp_overlay_event, "overlay events"},
0fd8e87f 6253 {bp_longjmp_master, "longjmp master"},
aa7d318d 6254 {bp_std_terminate_master, "std::terminate master"},
186c406b 6255 {bp_exception_master, "exception master"},
ce78b96d 6256 {bp_catchpoint, "catchpoint"},
1042e4c0 6257 {bp_tracepoint, "tracepoint"},
7a697b8d 6258 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6259 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6260 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6261 {bp_dprintf, "dprintf"},
4efc6507 6262 {bp_jit_event, "jit events"},
0e30163f
JK
6263 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6264 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6265 };
269b11a2
PA
6266
6267 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6268 || ((int) type != bptypes[(int) type].type))
f34652de 6269 internal_error (_("bptypes table does not describe type #%d."),
269b11a2
PA
6270 (int) type);
6271
6272 return bptypes[(int) type].description;
6273}
6274
998580f1
MK
6275/* For MI, output a field named 'thread-groups' with a list as the value.
6276 For CLI, prefix the list with the string 'inf'. */
6277
6278static void
6279output_thread_groups (struct ui_out *uiout,
6280 const char *field_name,
5c632425 6281 const std::vector<int> &inf_nums,
998580f1
MK
6282 int mi_only)
6283{
112e8700 6284 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6285
6286 /* For backward compatibility, don't display inferiors in CLI unless
6287 there are several. Always display them for MI. */
6288 if (!is_mi && mi_only)
6289 return;
6290
10f489e5 6291 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6292
5c632425 6293 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6294 {
6295 if (is_mi)
6296 {
6297 char mi_group[10];
6298
5c632425 6299 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6300 uiout->field_string (NULL, mi_group);
998580f1
MK
6301 }
6302 else
6303 {
6304 if (i == 0)
112e8700 6305 uiout->text (" inf ");
998580f1 6306 else
112e8700 6307 uiout->text (", ");
998580f1 6308
5c632425 6309 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6310 }
6311 }
998580f1
MK
6312}
6313
9db0d853
SM
6314/* See breakpoint.h. */
6315
6316bool fix_breakpoint_script_output_globally = false;
6317
a38118e5
PA
6318/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6319 instead of going via breakpoint_ops::print_one. This makes "maint
6320 info breakpoints" show the software breakpoint locations of
6321 catchpoints, which are considered internal implementation
c01e038b
TT
6322 detail. Returns true if RAW_LOC is false and if the breakpoint's
6323 print_one method did something; false otherwise. */
269b11a2 6324
c01e038b 6325static bool
269b11a2
PA
6326print_one_breakpoint_location (struct breakpoint *b,
6327 struct bp_location *loc,
6328 int loc_number,
6329 struct bp_location **last_loc,
a38118e5 6330 int allflag, bool raw_loc)
269b11a2
PA
6331{
6332 struct command_line *l;
c2c6d25f 6333 static char bpenables[] = "nynny";
c906108c 6334
79a45e25 6335 struct ui_out *uiout = current_uiout;
d8de7963
AB
6336 bool header_of_multiple = false;
6337 bool part_of_multiple = (loc != NULL);
79a45b7d
TT
6338 struct value_print_options opts;
6339
6340 get_user_print_options (&opts);
0d381245
VP
6341
6342 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6343 /* See comment in print_one_breakpoint concerning treatment of
6344 breakpoints with single disabled location. */
0d381245
VP
6345 if (loc == NULL
6346 && (b->loc != NULL
b5fa468f
TBA
6347 && (b->loc->next != NULL
6348 || !b->loc->enabled || b->loc->disabled_by_cond)))
d8de7963 6349 header_of_multiple = true;
0d381245
VP
6350 if (loc == NULL)
6351 loc = b->loc;
6352
c4093a6a
JM
6353 annotate_record ();
6354
6355 /* 1 */
6356 annotate_field (0);
0d381245 6357 if (part_of_multiple)
528e1572 6358 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6359 else
381befee 6360 uiout->field_signed ("number", b->number);
c4093a6a
JM
6361
6362 /* 2 */
6363 annotate_field (1);
0d381245 6364 if (part_of_multiple)
112e8700 6365 uiout->field_skip ("type");
269b11a2 6366 else
112e8700 6367 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6368
6369 /* 3 */
6370 annotate_field (2);
0d381245 6371 if (part_of_multiple)
112e8700 6372 uiout->field_skip ("disp");
0d381245 6373 else
112e8700 6374 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6375
c4093a6a
JM
6376 /* 4 */
6377 annotate_field (3);
0d381245 6378 if (part_of_multiple)
fbcda577
PA
6379 {
6380 /* For locations that are disabled because of an invalid
6381 condition, display "N*" on the CLI, where "*" refers to a
6382 footnote below the table. For MI, simply display a "N"
6383 without a footnote. On the CLI, for enabled locations whose
6384 breakpoint is disabled, display "y-". */
6385 auto get_enable_state = [uiout, loc] () -> const char *
6386 {
6387 if (uiout->is_mi_like_p ())
6388 {
6389 if (loc->disabled_by_cond)
6390 return "N";
6391 else if (!loc->enabled)
6392 return "n";
6393 else
6394 return "y";
6395 }
6396 else
6397 {
6398 if (loc->disabled_by_cond)
6399 return "N*";
6400 else if (!loc->enabled)
6401 return "n";
6402 else if (!breakpoint_enabled (loc->owner))
6403 return "y-";
6404 else
6405 return "y";
6406 }
6407 };
6408 uiout->field_string ("enabled", get_enable_state ());
6409 }
0d381245 6410 else
112e8700 6411 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6412
c4093a6a 6413 /* 5 and 6 */
c01e038b 6414 bool result = false;
c47614fe 6415 if (!raw_loc && b->print_one (last_loc))
c01e038b 6416 result = true;
3086aeae 6417 else
a38118e5
PA
6418 {
6419 if (is_watchpoint (b))
3a5c3e22
PA
6420 {
6421 struct watchpoint *w = (struct watchpoint *) b;
6422
6423 /* Field 4, the address, is omitted (which makes the columns
6424 not line up too nicely with the headers, but the effect
6425 is relatively readable). */
6426 if (opts.addressprint)
112e8700 6427 uiout->field_skip ("addr");
3a5c3e22 6428 annotate_field (5);
a4c50be3 6429 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6430 }
f06f1252
TT
6431 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6432 || is_ada_exception_catchpoint (b))
a38118e5
PA
6433 {
6434 if (opts.addressprint)
6435 {
6436 annotate_field (4);
6437 if (header_of_multiple)
7f6aba03
TT
6438 uiout->field_string ("addr", "<MULTIPLE>",
6439 metadata_style.style ());
a38118e5 6440 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6441 uiout->field_string ("addr", "<PENDING>",
6442 metadata_style.style ());
a38118e5
PA
6443 else
6444 uiout->field_core_addr ("addr",
6445 loc->gdbarch, loc->address);
6446 }
6447 annotate_field (5);
6448 if (!header_of_multiple)
6449 print_breakpoint_location (b, loc);
6450 if (b->loc)
6451 *last_loc = b->loc;
6452 }
6453 }
6c95b8df 6454
998580f1 6455 if (loc != NULL && !header_of_multiple)
6c95b8df 6456 {
5c632425 6457 std::vector<int> inf_nums;
998580f1 6458 int mi_only = 1;
6c95b8df 6459
08036331 6460 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6461 {
6462 if (inf->pspace == loc->pspace)
5c632425 6463 inf_nums.push_back (inf->num);
6c95b8df 6464 }
998580f1 6465
dda83cd7 6466 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6467 there are several. Always display for MI. */
6468 if (allflag
6469 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6470 && (program_spaces.size () > 1
998580f1
MK
6471 || number_of_inferiors () > 1)
6472 /* LOC is for existing B, it cannot be in
6473 moribund_locations and thus having NULL OWNER. */
6474 && loc->owner->type != bp_catchpoint))
6475 mi_only = 0;
5c632425 6476 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6477 }
6478
944b1b18
AB
6479 /* In the MI output, each location of a thread or task specific
6480 breakpoint includes the relevant thread or task ID. This is done for
6481 backwards compatibility reasons.
6482
6483 For the CLI output, the thread/task information is printed on a
6484 separate line, see the 'stop only in thread' and 'stop only in task'
6485 output below. */
6486 if (!header_of_multiple && uiout->is_mi_like_p ())
c4093a6a 6487 {
4a306c9a 6488 if (b->thread != -1)
944b1b18 6489 uiout->field_signed ("thread", b->thread);
2ecee236 6490 else if (b->task != -1)
944b1b18 6491 uiout->field_signed ("task", b->task);
c4093a6a 6492 }
f1310107 6493
112e8700 6494 uiout->text ("\n");
f1310107 6495
348d480f 6496 if (!part_of_multiple)
c47614fe 6497 b->print_one_detail (uiout);
f1310107 6498
0d381245 6499 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6500 {
6501 annotate_field (6);
112e8700 6502 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6503 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6504 the frame ID. */
112e8700 6505 uiout->field_core_addr ("frame",
5af949e3 6506 b->gdbarch, b->frame_id.stack_addr);
112e8700 6507 uiout->text ("\n");
c4093a6a
JM
6508 }
6509
28010a5d 6510 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6511 {
6512 annotate_field (7);
d77f58be 6513 if (is_tracepoint (b))
112e8700 6514 uiout->text ("\ttrace only if ");
1042e4c0 6515 else
112e8700 6516 uiout->text ("\tstop only if ");
6f781ee3 6517 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6518
6519 /* Print whether the target is doing the breakpoint's condition
6520 evaluation. If GDB is doing the evaluation, don't print anything. */
6521 if (is_breakpoint (b)
6522 && breakpoint_condition_evaluation_mode ()
6523 == condition_evaluation_target)
6524 {
6a831f06
PA
6525 uiout->message (" (%pF evals)",
6526 string_field ("evaluated-by",
6527 bp_condition_evaluator (b)));
b775012e 6528 }
112e8700 6529 uiout->text ("\n");
0101ce28
JJ
6530 }
6531
0d381245 6532 if (!part_of_multiple && b->thread != -1)
c4093a6a 6533 {
4a64f543 6534 /* FIXME should make an annotation for this. */
112e8700
SM
6535 uiout->text ("\tstop only in thread ");
6536 if (uiout->is_mi_like_p ())
381befee 6537 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6538 else
6539 {
6540 struct thread_info *thr = find_thread_global_id (b->thread);
6541
112e8700 6542 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6543 }
112e8700 6544 uiout->text ("\n");
c4093a6a 6545 }
944b1b18 6546
2ecee236 6547 if (!part_of_multiple && b->task != -1)
944b1b18
AB
6548 {
6549 uiout->text ("\tstop only in task ");
6550 uiout->field_signed ("task", b->task);
6551 uiout->text ("\n");
6552 }
6553
556ec64d
YQ
6554 if (!part_of_multiple)
6555 {
6556 if (b->hit_count)
31f56a27
YQ
6557 {
6558 /* FIXME should make an annotation for this. */
6559 if (is_catchpoint (b))
112e8700 6560 uiout->text ("\tcatchpoint");
31f56a27 6561 else if (is_tracepoint (b))
112e8700 6562 uiout->text ("\ttracepoint");
31f56a27 6563 else
112e8700
SM
6564 uiout->text ("\tbreakpoint");
6565 uiout->text (" already hit ");
381befee 6566 uiout->field_signed ("times", b->hit_count);
31f56a27 6567 if (b->hit_count == 1)
112e8700 6568 uiout->text (" time\n");
31f56a27 6569 else
112e8700 6570 uiout->text (" times\n");
31f56a27 6571 }
556ec64d
YQ
6572 else
6573 {
31f56a27 6574 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6575 if (uiout->is_mi_like_p ())
381befee 6576 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6577 }
6578 }
8b93c638 6579
0d381245 6580 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6581 {
6582 annotate_field (8);
6a831f06
PA
6583 uiout->message ("\tignore next %pF hits\n",
6584 signed_field ("ignore", b->ignore_count));
c4093a6a 6585 }
059fb39f 6586
816338b5
SS
6587 /* Note that an enable count of 1 corresponds to "enable once"
6588 behavior, which is reported by the combination of enablement and
6589 disposition, so we don't need to mention it here. */
6590 if (!part_of_multiple && b->enable_count > 1)
6591 {
6592 annotate_field (8);
112e8700 6593 uiout->text ("\tdisable after ");
816338b5
SS
6594 /* Tweak the wording to clarify that ignore and enable counts
6595 are distinct, and have additive effect. */
6596 if (b->ignore_count)
112e8700 6597 uiout->text ("additional ");
816338b5 6598 else
112e8700 6599 uiout->text ("next ");
381befee 6600 uiout->field_signed ("enable", b->enable_count);
112e8700 6601 uiout->text (" hits\n");
816338b5
SS
6602 }
6603
f196051f
SS
6604 if (!part_of_multiple && is_tracepoint (b))
6605 {
6606 struct tracepoint *tp = (struct tracepoint *) b;
6607
6608 if (tp->traceframe_usage)
6609 {
112e8700 6610 uiout->text ("\ttrace buffer usage ");
381befee 6611 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6612 uiout->text (" bytes\n");
f196051f
SS
6613 }
6614 }
d3ce09f5 6615
d1b0a7bf 6616 l = b->commands ? b->commands.get () : NULL;
059fb39f 6617 if (!part_of_multiple && l)
c4093a6a
JM
6618 {
6619 annotate_field (9);
9db0d853
SM
6620
6621 bool use_fixed_output =
6622 (uiout->test_flags (fix_breakpoint_script_output)
6623 || fix_breakpoint_script_output_globally);
6624
6625 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6626 gdb::optional<ui_out_emit_list> list_emitter;
6627
6628 if (use_fixed_output)
6629 list_emitter.emplace (uiout, "script");
6630 else
6631 tuple_emitter.emplace (uiout, "script");
6632
8b93c638 6633 print_command_lines (uiout, l, 4);
c4093a6a 6634 }
d24317b4 6635
d9b3f62e 6636 if (is_tracepoint (b))
1042e4c0 6637 {
d9b3f62e
PA
6638 struct tracepoint *t = (struct tracepoint *) b;
6639
6640 if (!part_of_multiple && t->pass_count)
6641 {
6642 annotate_field (10);
112e8700 6643 uiout->text ("\tpass count ");
381befee 6644 uiout->field_signed ("pass", t->pass_count);
112e8700 6645 uiout->text (" \n");
d9b3f62e 6646 }
f2a8bc8a
YQ
6647
6648 /* Don't display it when tracepoint or tracepoint location is
6649 pending. */
6650 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6651 {
6652 annotate_field (11);
6653
112e8700
SM
6654 if (uiout->is_mi_like_p ())
6655 uiout->field_string ("installed",
f2a8bc8a
YQ
6656 loc->inserted ? "y" : "n");
6657 else
6658 {
6659 if (loc->inserted)
112e8700 6660 uiout->text ("\t");
f2a8bc8a 6661 else
112e8700
SM
6662 uiout->text ("\tnot ");
6663 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6664 }
6665 }
1042e4c0
SS
6666 }
6667
112e8700 6668 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6669 {
3a5c3e22
PA
6670 if (is_watchpoint (b))
6671 {
6672 struct watchpoint *w = (struct watchpoint *) b;
6673
a4c50be3 6674 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6675 }
264f9890
PA
6676 else if (b->locspec != nullptr)
6677 {
709438c7 6678 const char *str = b->locspec->to_string ();
264f9890
PA
6679 if (str != nullptr)
6680 uiout->field_string ("original-location", str);
6681 }
d24317b4 6682 }
c01e038b
TT
6683
6684 return result;
c4093a6a 6685}
c5aa993b 6686
13674803
SM
6687/* See breakpoint.h. */
6688
6689bool fix_multi_location_breakpoint_output_globally = false;
6690
0d381245
VP
6691static void
6692print_one_breakpoint (struct breakpoint *b,
4a64f543 6693 struct bp_location **last_loc,
6c95b8df 6694 int allflag)
0d381245 6695{
79a45e25 6696 struct ui_out *uiout = current_uiout;
13674803
SM
6697 bool use_fixed_output
6698 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6699 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6700
b4be1b06 6701 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
c01e038b
TT
6702 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6703 allflag, false);
8d3788bd 6704
b4be1b06
SM
6705 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6706 are outside. */
6707 if (!use_fixed_output)
6708 bkpt_tuple_emitter.reset ();
0d381245
VP
6709
6710 /* If this breakpoint has custom print function,
6711 it's already printed. Otherwise, print individual
6712 locations, if any. */
c01e038b 6713 if (!printed || allflag)
0d381245 6714 {
4a64f543
MS
6715 /* If breakpoint has a single location that is disabled, we
6716 print it as if it had several locations, since otherwise it's
6717 hard to represent "breakpoint enabled, location disabled"
6718 situation.
6719
6720 Note that while hardware watchpoints have several locations
a38118e5
PA
6721 internally, that's not a property exposed to users.
6722
6723 Likewise, while catchpoints may be implemented with
6724 breakpoints (e.g., catch throw), that's not a property
6725 exposed to users. We do however display the internal
6726 breakpoint locations with "maint info breakpoints". */
6727 if (!is_hardware_watchpoint (b)
f06f1252
TT
6728 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6729 || is_ada_exception_catchpoint (b))
a38118e5 6730 && (allflag
b5fa468f
TBA
6731 || (b->loc && (b->loc->next
6732 || !b->loc->enabled
6733 || b->loc->disabled_by_cond))))
0d381245 6734 {
b4be1b06
SM
6735 gdb::optional<ui_out_emit_list> locations_list;
6736
6737 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6738 MI record. For later versions, place breakpoint locations in a
6739 list. */
6740 if (uiout->is_mi_like_p () && use_fixed_output)
6741 locations_list.emplace (uiout, "locations");
8d3788bd 6742
b4be1b06 6743 int n = 1;
40cb8ca5 6744 for (bp_location *loc : b->locations ())
8d3788bd 6745 {
b4be1b06 6746 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6747 print_one_breakpoint_location (b, loc, n, last_loc,
6748 allflag, allflag);
40cb8ca5 6749 n++;
8d3788bd 6750 }
0d381245
VP
6751 }
6752 }
6753}
6754
a6d9a66e
UW
6755static int
6756breakpoint_address_bits (struct breakpoint *b)
6757{
6758 int print_address_bits = 0;
a6d9a66e 6759
40cb8ca5 6760 for (bp_location *loc : b->locations ())
a6d9a66e 6761 {
6e14e441
PA
6762 if (!bl_address_is_meaningful (loc))
6763 continue;
c7437ca6 6764
6e14e441 6765 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6766 if (addr_bit > print_address_bits)
6767 print_address_bits = addr_bit;
6768 }
6769
6770 return print_address_bits;
6771}
0d381245 6772
65630365 6773/* See breakpoint.h. */
c5aa993b 6774
65630365
PA
6775void
6776print_breakpoint (breakpoint *b)
c4093a6a 6777{
a6d9a66e 6778 struct bp_location *dummy_loc = NULL;
65630365 6779 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6780}
c5aa993b 6781
09d682a4
TT
6782/* Return true if this breakpoint was set by the user, false if it is
6783 internal or momentary. */
6784
6785int
6786user_breakpoint_p (struct breakpoint *b)
6787{
46c6471b 6788 return b->number > 0;
09d682a4
TT
6789}
6790
93daf339
TT
6791/* See breakpoint.h. */
6792
6793int
6794pending_breakpoint_p (struct breakpoint *b)
6795{
6796 return b->loc == NULL;
6797}
6798
5c458ae8
SM
6799/* Print information on breakpoints (including watchpoints and tracepoints).
6800
6801 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6802 understood by number_or_range_parser. Only breakpoints included in this
6803 list are then printed.
6804
6805 If SHOW_INTERNAL is true, print internal breakpoints.
6806
6807 If FILTER is non-NULL, call it on each breakpoint and only include the
6808 ones for which it returns true.
6809
6810 Return the total number of breakpoints listed. */
c906108c 6811
d77f58be 6812static int
5c458ae8 6813breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6814 bool (*filter) (const struct breakpoint *))
c4093a6a 6815{
a6d9a66e 6816 struct bp_location *last_loc = NULL;
7f3b0473 6817 int nr_printable_breakpoints;
79a45b7d 6818 struct value_print_options opts;
a6d9a66e 6819 int print_address_bits = 0;
269b11a2 6820 int print_type_col_width = 14;
79a45e25 6821 struct ui_out *uiout = current_uiout;
b5fa468f 6822 bool has_disabled_by_cond_location = false;
269b11a2 6823
79a45b7d
TT
6824 get_user_print_options (&opts);
6825
4a64f543
MS
6826 /* Compute the number of rows in the table, as well as the size
6827 required for address fields. */
7f3b0473 6828 nr_printable_breakpoints = 0;
43892fdf 6829 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6830 {
6831 /* If we have a filter, only list the breakpoints it accepts. */
6832 if (filter && !filter (b))
6833 continue;
6834
5c458ae8 6835 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6836 accept. Skip the others. */
5c458ae8 6837 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6838 {
5c458ae8 6839 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6840 continue;
5c458ae8 6841 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6842 continue;
6843 }
269b11a2 6844
5c458ae8 6845 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6846 {
6847 int addr_bit, type_len;
a6d9a66e 6848
e5a67952
MS
6849 addr_bit = breakpoint_address_bits (b);
6850 if (addr_bit > print_address_bits)
6851 print_address_bits = addr_bit;
269b11a2 6852
e5a67952
MS
6853 type_len = strlen (bptype_string (b->type));
6854 if (type_len > print_type_col_width)
6855 print_type_col_width = type_len;
6856
6857 nr_printable_breakpoints++;
6858 }
6859 }
7f3b0473 6860
4a2b031d
TT
6861 {
6862 ui_out_emit_table table_emitter (uiout,
6863 opts.addressprint ? 6 : 5,
6864 nr_printable_breakpoints,
6865 "BreakpointTable");
6866
6867 if (nr_printable_breakpoints > 0)
6868 annotate_breakpoints_headers ();
6869 if (nr_printable_breakpoints > 0)
6870 annotate_field (0);
6871 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6872 if (nr_printable_breakpoints > 0)
6873 annotate_field (1);
6874 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6875 if (nr_printable_breakpoints > 0)
6876 annotate_field (2);
6877 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6878 if (nr_printable_breakpoints > 0)
6879 annotate_field (3);
6880 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6881 if (opts.addressprint)
6882 {
6883 if (nr_printable_breakpoints > 0)
6884 annotate_field (4);
6885 if (print_address_bits <= 32)
6886 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6887 else
6888 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6889 }
6890 if (nr_printable_breakpoints > 0)
6891 annotate_field (5);
6892 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6893 uiout->table_body ();
6894 if (nr_printable_breakpoints > 0)
6895 annotate_breakpoints_table ();
6896
43892fdf 6897 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6898 {
6899 QUIT;
6900 /* If we have a filter, only list the breakpoints it accepts. */
6901 if (filter && !filter (b))
6902 continue;
e5a67952 6903
5c458ae8 6904 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6905 accept. Skip the others. */
e5a67952 6906
5c458ae8 6907 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6908 {
5c458ae8 6909 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6910 {
5c458ae8 6911 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6912 continue;
6913 }
6914 else /* all others */
6915 {
5c458ae8 6916 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6917 continue;
6918 }
6919 }
6920 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6921 show_internal is set. */
6922 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6923 {
6924 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6925 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6926 if (loc->disabled_by_cond)
6927 has_disabled_by_cond_location = true;
6928 }
4a2b031d
TT
6929 }
6930 }
698384cd 6931
7f3b0473 6932 if (nr_printable_breakpoints == 0)
c906108c 6933 {
4a64f543
MS
6934 /* If there's a filter, let the caller decide how to report
6935 empty list. */
d77f58be
SS
6936 if (!filter)
6937 {
5c458ae8 6938 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6939 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6940 else
112e8700 6941 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6942 bp_num_list);
d77f58be 6943 }
c906108c
SS
6944 }
6945 else
c4093a6a 6946 {
a6d9a66e
UW
6947 if (last_loc && !server_command)
6948 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6949
85c88e2a 6950 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6951 uiout->message (_("(*): Breakpoint condition is invalid at this "
6952 "location.\n"));
c4093a6a 6953 }
c906108c 6954
4a64f543 6955 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6956 there have been breakpoints? */
c906108c 6957 annotate_breakpoints_table_end ();
d77f58be
SS
6958
6959 return nr_printable_breakpoints;
c906108c
SS
6960}
6961
ad443146
SS
6962/* Display the value of default-collect in a way that is generally
6963 compatible with the breakpoint list. */
6964
6965static void
6966default_collect_info (void)
6967{
79a45e25
PA
6968 struct ui_out *uiout = current_uiout;
6969
ad443146
SS
6970 /* If it has no value (which is frequently the case), say nothing; a
6971 message like "No default-collect." gets in user's face when it's
6972 not wanted. */
e0700ba4 6973 if (default_collect.empty ())
ad443146
SS
6974 return;
6975
6976 /* The following phrase lines up nicely with per-tracepoint collect
6977 actions. */
112e8700
SM
6978 uiout->text ("default collect ");
6979 uiout->field_string ("default-collect", default_collect);
6980 uiout->text (" \n");
ad443146
SS
6981}
6982
c906108c 6983static void
0b39b52e 6984info_breakpoints_command (const char *args, int from_tty)
c906108c 6985{
5c458ae8 6986 breakpoint_1 (args, false, NULL);
ad443146
SS
6987
6988 default_collect_info ();
d77f58be
SS
6989}
6990
6991static void
1d12d88f 6992info_watchpoints_command (const char *args, int from_tty)
d77f58be 6993{
5c458ae8 6994 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6995 struct ui_out *uiout = current_uiout;
d77f58be
SS
6996
6997 if (num_printed == 0)
6998 {
e5a67952 6999 if (args == NULL || *args == '\0')
112e8700 7000 uiout->message ("No watchpoints.\n");
d77f58be 7001 else
112e8700 7002 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 7003 }
c906108c
SS
7004}
7005
7a292a7a 7006static void
4495129a 7007maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 7008{
5c458ae8 7009 breakpoint_1 (args, true, NULL);
ad443146
SS
7010
7011 default_collect_info ();
c906108c
SS
7012}
7013
d8de7963 7014static bool
714835d5 7015breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7016 struct program_space *pspace,
714835d5 7017 CORE_ADDR pc, struct obj_section *section)
0d381245 7018{
40cb8ca5 7019 for (bp_location *bl : b->locations ())
0d381245 7020 {
6c95b8df
PA
7021 if (bl->pspace == pspace
7022 && bl->address == pc
0d381245 7023 && (!overlay_debugging || bl->section == section))
d8de7963 7024 return true;
0d381245 7025 }
d8de7963 7026 return false;
0d381245
VP
7027}
7028
bd21b6c9 7029/* See breakpoint.h. */
c906108c 7030
bd21b6c9 7031void
6c95b8df
PA
7032describe_other_breakpoints (struct gdbarch *gdbarch,
7033 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7034 struct obj_section *section, int thread)
c906108c 7035{
52f0bd74 7036 int others = 0;
c906108c 7037
43892fdf 7038 for (breakpoint *b : all_breakpoints ())
672f9b60 7039 others += (user_breakpoint_p (b)
dda83cd7 7040 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 7041
c906108c
SS
7042 if (others > 0)
7043 {
a3f17187 7044 if (others == 1)
6cb06a8c 7045 gdb_printf (_("Note: breakpoint "));
a3f17187 7046 else /* if (others == ???) */
6cb06a8c 7047 gdb_printf (_("Note: breakpoints "));
43892fdf 7048 for (breakpoint *b : all_breakpoints ())
672f9b60 7049 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7050 {
7051 others--;
6cb06a8c 7052 gdb_printf ("%d", b->number);
0d381245 7053 if (b->thread == -1 && thread != -1)
6cb06a8c 7054 gdb_printf (" (all threads)");
0d381245 7055 else if (b->thread != -1)
ce068c5f
AB
7056 {
7057 struct thread_info *thr = find_thread_global_id (b->thread);
7058 gdb_printf (" (thread %s)", print_thread_id (thr));
7059 }
8282ad74 7060 else if (b->task != -1)
f1f517e8 7061 gdb_printf (" (task %d)", b->task);
6cb06a8c
TT
7062 gdb_printf ("%s%s ",
7063 ((b->enable_state == bp_disabled
7064 || b->enable_state == bp_call_disabled)
7065 ? " (disabled)"
7066 : ""),
7067 (others > 1) ? ","
7068 : ((others == 1) ? " and" : ""));
0d381245 7069 }
6a831f06
PA
7070 current_uiout->message (_("also set at pc %ps.\n"),
7071 styled_string (address_style.style (),
7072 paddress (gdbarch, pc)));
c906108c
SS
7073 }
7074}
7075\f
c906108c 7076
cb1e4e32
PA
7077/* Return true iff it is meaningful to use the address member of LOC.
7078 For some breakpoint types, the locations' address members are
7079 irrelevant and it makes no sense to attempt to compare them to
7080 other addresses (or use them for any other purpose either).
2d134ed3 7081
cb1e4e32
PA
7082 More specifically, software watchpoints and catchpoints that are
7083 not backed by breakpoints always have a zero valued location
7084 address and we don't want to mark breakpoints of any of these types
7085 to be a duplicate of an actual breakpoint location at address
7086 zero. */
e4f237da 7087
cb1e4e32
PA
7088static bool
7089bl_address_is_meaningful (bp_location *loc)
e4f237da 7090{
cb1e4e32 7091 return loc->loc_type != bp_loc_other;
2d134ed3
PA
7092}
7093
7094/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7095 true if LOC1 and LOC2 represent the same watchpoint location. */
7096
d8de7963 7097static bool
b2ff9ed3
AB
7098watchpoint_locations_match (const struct bp_location *loc1,
7099 const struct bp_location *loc2)
2d134ed3 7100{
3a5c3e22
PA
7101 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7102 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7103
7104 /* Both of them must exist. */
7105 gdb_assert (w1 != NULL);
7106 gdb_assert (w2 != NULL);
2bdf28a0 7107
4a64f543
MS
7108 /* If the target can evaluate the condition expression in hardware,
7109 then we we need to insert both watchpoints even if they are at
7110 the same place. Otherwise the watchpoint will only trigger when
7111 the condition of whichever watchpoint was inserted evaluates to
7112 true, not giving a chance for GDB to check the condition of the
7113 other watchpoint. */
3a5c3e22 7114 if ((w1->cond_exp
4a64f543
MS
7115 && target_can_accel_watchpoint_condition (loc1->address,
7116 loc1->length,
0cf6dd15 7117 loc1->watchpoint_type,
4d01a485 7118 w1->cond_exp.get ()))
3a5c3e22 7119 || (w2->cond_exp
4a64f543
MS
7120 && target_can_accel_watchpoint_condition (loc2->address,
7121 loc2->length,
0cf6dd15 7122 loc2->watchpoint_type,
4d01a485 7123 w2->cond_exp.get ())))
d8de7963 7124 return false;
0cf6dd15 7125
85d721b8
PA
7126 /* Note that this checks the owner's type, not the location's. In
7127 case the target does not support read watchpoints, but does
7128 support access watchpoints, we'll have bp_read_watchpoint
7129 watchpoints with hw_access locations. Those should be considered
7130 duplicates of hw_read locations. The hw_read locations will
7131 become hw_access locations later. */
2d134ed3
PA
7132 return (loc1->owner->type == loc2->owner->type
7133 && loc1->pspace->aspace == loc2->pspace->aspace
7134 && loc1->address == loc2->address
7135 && loc1->length == loc2->length);
e4f237da
KB
7136}
7137
31e77af2 7138/* See breakpoint.h. */
6c95b8df 7139
31e77af2 7140int
accd0bcd
YQ
7141breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7142 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 7143{
f5656ead 7144 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7145 || aspace1 == aspace2)
7146 && addr1 == addr2);
7147}
7148
f1310107
TJB
7149/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7150 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7151 matches ASPACE2. On targets that have global breakpoints, the address
7152 space doesn't really matter. */
7153
d8de7963 7154static bool
accd0bcd
YQ
7155breakpoint_address_match_range (const address_space *aspace1,
7156 CORE_ADDR addr1,
7157 int len1, const address_space *aspace2,
f1310107
TJB
7158 CORE_ADDR addr2)
7159{
f5656ead 7160 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7161 || aspace1 == aspace2)
7162 && addr2 >= addr1 && addr2 < addr1 + len1);
7163}
7164
7165/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7166 a ranged breakpoint. In most targets, a match happens only if ASPACE
7167 matches the breakpoint's address space. On targets that have global
7168 breakpoints, the address space doesn't really matter. */
7169
d8de7963 7170static bool
f1310107 7171breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 7172 const address_space *aspace,
f1310107
TJB
7173 CORE_ADDR addr)
7174{
7175 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7176 aspace, addr)
7177 || (bl->length
7178 && breakpoint_address_match_range (bl->pspace->aspace,
7179 bl->address, bl->length,
7180 aspace, addr)));
7181}
7182
d35ae833
PA
7183/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7184 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7185 match happens only if ASPACE matches the breakpoint's address
7186 space. On targets that have global breakpoints, the address space
7187 doesn't really matter. */
7188
d8de7963 7189static bool
d35ae833 7190breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 7191 const address_space *aspace,
d35ae833
PA
7192 CORE_ADDR addr, int len)
7193{
7194 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7195 || bl->pspace->aspace == aspace)
7196 {
7197 int bl_len = bl->length != 0 ? bl->length : 1;
7198
7199 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7200 return 1;
7201 }
7202 return 0;
7203}
7204
1e4d1764
YQ
7205/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7206 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7207 true, otherwise returns false. */
7208
d8de7963 7209static bool
b2ff9ed3
AB
7210tracepoint_locations_match (const struct bp_location *loc1,
7211 const struct bp_location *loc2)
1e4d1764
YQ
7212{
7213 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7214 /* Since tracepoint locations are never duplicated with others', tracepoint
7215 locations at the same address of different tracepoints are regarded as
7216 different locations. */
7217 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7218 else
d8de7963 7219 return false;
1e4d1764
YQ
7220}
7221
2d134ed3 7222/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7223 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7224 the same location. If SW_HW_BPS_MATCH is true, then software
7225 breakpoint locations and hardware breakpoint locations match,
7226 otherwise they don't. */
2d134ed3 7227
d8de7963 7228static bool
b2ff9ed3
AB
7229breakpoint_locations_match (const struct bp_location *loc1,
7230 const struct bp_location *loc2,
7f32a4d5 7231 bool sw_hw_bps_match)
2d134ed3 7232{
2bdf28a0
JK
7233 int hw_point1, hw_point2;
7234
7235 /* Both of them must not be in moribund_locations. */
7236 gdb_assert (loc1->owner != NULL);
7237 gdb_assert (loc2->owner != NULL);
7238
7239 hw_point1 = is_hardware_watchpoint (loc1->owner);
7240 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7241
7242 if (hw_point1 != hw_point2)
d8de7963 7243 return false;
2d134ed3
PA
7244 else if (hw_point1)
7245 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7246 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7247 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7248 else
7f32a4d5
PA
7249 /* We compare bp_location.length in order to cover ranged
7250 breakpoints. Keep this in sync with
7251 bp_location_is_less_than. */
f1310107
TJB
7252 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7253 loc2->pspace->aspace, loc2->address)
7f32a4d5 7254 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7255 && loc1->length == loc2->length);
2d134ed3
PA
7256}
7257
76897487
KB
7258static void
7259breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
d8de7963 7260 int bnum, bool have_bnum)
76897487 7261{
f63fbe86
MS
7262 /* The longest string possibly returned by hex_string_custom
7263 is 50 chars. These must be at least that big for safety. */
7264 char astr1[64];
7265 char astr2[64];
76897487 7266
bb599908
PH
7267 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7268 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7269 if (have_bnum)
8a3fe4f8 7270 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7271 bnum, astr1, astr2);
76897487 7272 else
8a3fe4f8 7273 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7274}
7275
4a64f543
MS
7276/* Adjust a breakpoint's address to account for architectural
7277 constraints on breakpoint placement. Return the adjusted address.
7278 Note: Very few targets require this kind of adjustment. For most
7279 targets, this function is simply the identity function. */
76897487
KB
7280
7281static CORE_ADDR
a6d9a66e 7282adjust_breakpoint_address (struct gdbarch *gdbarch,
60270718
AB
7283 CORE_ADDR bpaddr, enum bptype bptype,
7284 struct program_space *pspace)
76897487 7285{
60270718
AB
7286 gdb_assert (pspace != nullptr);
7287
a0de8c21
YQ
7288 if (bptype == bp_watchpoint
7289 || bptype == bp_hardware_watchpoint
7290 || bptype == bp_read_watchpoint
7291 || bptype == bp_access_watchpoint
7292 || bptype == bp_catchpoint)
88f7da05
KB
7293 {
7294 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7295 have their addresses modified. */
88f7da05
KB
7296 return bpaddr;
7297 }
7c16b83e
PA
7298 else if (bptype == bp_single_step)
7299 {
7300 /* Single-step breakpoints should not have their addresses
7301 modified. If there's any architectural constrain that
7302 applies to this address, then it should have already been
7303 taken into account when the breakpoint was created in the
7304 first place. If we didn't do this, stepping through e.g.,
7305 Thumb-2 IT blocks would break. */
7306 return bpaddr;
7307 }
76897487
KB
7308 else
7309 {
a0de8c21
YQ
7310 CORE_ADDR adjusted_bpaddr = bpaddr;
7311
60270718
AB
7312 /* Some targets have architectural constraints on the placement
7313 of breakpoint instructions. Obtain the adjusted address. */
a0de8c21
YQ
7314 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7315 {
60270718
AB
7316 /* Targets that implement this adjustment function will likely
7317 inspect either the symbol table, target memory at BPADDR, or
7318 even state registers, so ensure a suitable thread (and its
7319 associated program space) are currently selected. */
7320 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7321 switch_to_program_space_and_thread (pspace);
7322 adjusted_bpaddr
7323 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
a0de8c21 7324 }
76897487 7325
d88cb738
LM
7326 adjusted_bpaddr
7327 = gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
76897487
KB
7328
7329 /* An adjusted breakpoint address can significantly alter
dda83cd7 7330 a user's expectations. Print a warning if an adjustment
76897487
KB
7331 is required. */
7332 if (adjusted_bpaddr != bpaddr)
d8de7963 7333 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
76897487
KB
7334
7335 return adjusted_bpaddr;
7336 }
7337}
7338
cb1e4e32
PA
7339static bp_loc_type
7340bp_location_from_bp_type (bptype type)
7cc221ef 7341{
cb1e4e32 7342 switch (type)
e049a4b5
DJ
7343 {
7344 case bp_breakpoint:
7c16b83e 7345 case bp_single_step:
e049a4b5
DJ
7346 case bp_until:
7347 case bp_finish:
7348 case bp_longjmp:
7349 case bp_longjmp_resume:
e2e4d78b 7350 case bp_longjmp_call_dummy:
186c406b
TT
7351 case bp_exception:
7352 case bp_exception_resume:
e049a4b5 7353 case bp_step_resume:
2c03e5be 7354 case bp_hp_step_resume:
e049a4b5
DJ
7355 case bp_watchpoint_scope:
7356 case bp_call_dummy:
aa7d318d 7357 case bp_std_terminate:
e049a4b5
DJ
7358 case bp_shlib_event:
7359 case bp_thread_event:
7360 case bp_overlay_event:
4efc6507 7361 case bp_jit_event:
0fd8e87f 7362 case bp_longjmp_master:
aa7d318d 7363 case bp_std_terminate_master:
186c406b 7364 case bp_exception_master:
0e30163f
JK
7365 case bp_gnu_ifunc_resolver:
7366 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7367 case bp_dprintf:
cb1e4e32 7368 return bp_loc_software_breakpoint;
e049a4b5 7369 case bp_hardware_breakpoint:
cb1e4e32 7370 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7371 case bp_hardware_watchpoint:
7372 case bp_read_watchpoint:
7373 case bp_access_watchpoint:
cb1e4e32 7374 return bp_loc_hardware_watchpoint;
e049a4b5 7375 case bp_watchpoint:
6e14e441 7376 return bp_loc_software_watchpoint;
ce78b96d 7377 case bp_catchpoint:
15c3d785
PA
7378 case bp_tracepoint:
7379 case bp_fast_tracepoint:
0fb4aa4b 7380 case bp_static_tracepoint:
7b572efb 7381 case bp_static_marker_tracepoint:
cb1e4e32 7382 return bp_loc_other;
e049a4b5 7383 default:
f34652de 7384 internal_error (_("unknown breakpoint type"));
e049a4b5 7385 }
cb1e4e32
PA
7386}
7387
7388bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7389{
7390 this->owner = owner;
7391 this->cond_bytecode = NULL;
7392 this->shlib_disabled = 0;
7393 this->enabled = 1;
b5fa468f 7394 this->disabled_by_cond = false;
cb1e4e32
PA
7395
7396 this->loc_type = type;
e049a4b5 7397
cb1e4e32
PA
7398 if (this->loc_type == bp_loc_software_breakpoint
7399 || this->loc_type == bp_loc_hardware_breakpoint)
7400 mark_breakpoint_location_modified (this);
7401
b6433ede 7402 incref ();
cb1e4e32
PA
7403}
7404
7405bp_location::bp_location (breakpoint *owner)
7406 : bp_location::bp_location (owner,
7407 bp_location_from_bp_type (owner->type))
7408{
28010a5d
PA
7409}
7410
f431efe5
PA
7411/* Decrement reference count. If the reference count reaches 0,
7412 destroy the bp_location. Sets *BLP to NULL. */
7413
7414static void
7415decref_bp_location (struct bp_location **blp)
7416{
b6433ede 7417 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7418 *blp = NULL;
7419}
7420
346774a9 7421/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7422
b270e6f9
TT
7423static breakpoint *
7424add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7425{
346774a9 7426 struct breakpoint *b1;
b270e6f9 7427 struct breakpoint *result = b.get ();
c906108c 7428
346774a9
PA
7429 /* Add this breakpoint to the end of the chain so that a list of
7430 breakpoints will come out in order of increasing numbers. */
7431
7432 b1 = breakpoint_chain;
7433 if (b1 == 0)
b270e6f9 7434 breakpoint_chain = b.release ();
346774a9
PA
7435 else
7436 {
7437 while (b1->next)
7438 b1 = b1->next;
b270e6f9 7439 b1->next = b.release ();
346774a9 7440 }
b270e6f9
TT
7441
7442 return result;
346774a9
PA
7443}
7444
0ba852ab 7445/* Initialize loc->function_name. */
0e30163f 7446
0d381245 7447static void
0ba852ab 7448set_breakpoint_location_function (struct bp_location *loc)
0d381245 7449{
2bdf28a0
JK
7450 gdb_assert (loc->owner != NULL);
7451
0d381245 7452 if (loc->owner->type == bp_breakpoint
1042e4c0 7453 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7454 || is_tracepoint (loc->owner))
0d381245 7455 {
2c02bd72 7456 const char *function_name;
0e30163f 7457
3467ec66 7458 if (loc->msymbol != NULL
60f62e2b
SM
7459 && (loc->msymbol->type () == mst_text_gnu_ifunc
7460 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7461 {
7462 struct breakpoint *b = loc->owner;
7463
c9d95fa3 7464 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7465
7466 if (b->type == bp_breakpoint && b->loc == loc
7467 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7468 {
7469 /* Create only the whole new breakpoint of this type but do not
7470 mess more complicated breakpoints with multiple locations. */
7471 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7472 /* Remember the resolver's address for use by the return
dda83cd7 7473 breakpoint. */
3467ec66 7474 loc->related_address = loc->address;
0e30163f
JK
7475 }
7476 }
3467ec66
PA
7477 else
7478 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7479
2c02bd72 7480 if (function_name)
23d6ee64 7481 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7482 }
7483}
7484
a6d9a66e 7485/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7486struct gdbarch *
a6d9a66e
UW
7487get_sal_arch (struct symtab_and_line sal)
7488{
7489 if (sal.section)
08feed99 7490 return sal.section->objfile->arch ();
a6d9a66e 7491 if (sal.symtab)
3c86fae3 7492 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7493
7494 return NULL;
7495}
7496
53a5351d 7497/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7498 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7499 initiated the operation. */
c906108c
SS
7500
7501void
186c406b 7502set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7503{
5d5658a1 7504 int thread = tp->global_num;
0fd8e87f
UW
7505
7506 /* To avoid having to rescan all objfile symbols at every step,
7507 we maintain a list of continually-inserted but always disabled
7508 longjmp "master" breakpoints. Here, we simply create momentary
7509 clones of those and enable them for the requested thread. */
1428b37a 7510 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7511 if (b->pspace == current_program_space
186c406b
TT
7512 && (b->type == bp_longjmp_master
7513 || b->type == bp_exception_master))
0fd8e87f 7514 {
06edf0c0 7515 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
e2e4d78b
JK
7516 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7517 after their removal. */
7ab97995 7518 momentary_breakpoint_from_master (b, type, 1, thread);
0fd8e87f 7519 }
186c406b
TT
7520
7521 tp->initiating_frame = frame;
c906108c
SS
7522}
7523
611c83ae 7524/* Delete all longjmp breakpoints from THREAD. */
c906108c 7525void
611c83ae 7526delete_longjmp_breakpoint (int thread)
c906108c 7527{
1428b37a 7528 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7529 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7530 {
7531 if (b->thread == thread)
7532 delete_breakpoint (b);
7533 }
c906108c
SS
7534}
7535
f59f708a
PA
7536void
7537delete_longjmp_breakpoint_at_next_stop (int thread)
7538{
1428b37a 7539 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7540 if (b->type == bp_longjmp || b->type == bp_exception)
7541 {
7542 if (b->thread == thread)
7543 b->disposition = disp_del_at_next_stop;
7544 }
7545}
7546
e2e4d78b
JK
7547/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7548 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7549 pointer to any of them. Return NULL if this system cannot place longjmp
7550 breakpoints. */
7551
7552struct breakpoint *
7553set_longjmp_breakpoint_for_call_dummy (void)
7554{
43892fdf 7555 breakpoint *retval = nullptr;
e2e4d78b 7556
43892fdf 7557 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7558 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7559 {
7ab97995
PA
7560 int thread = inferior_thread ()->global_num;
7561 breakpoint *new_b
7562 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7563 1, thread);
e2e4d78b
JK
7564
7565 /* Link NEW_B into the chain of RETVAL breakpoints. */
7566
7567 gdb_assert (new_b->related_breakpoint == new_b);
7568 if (retval == NULL)
7569 retval = new_b;
7570 new_b->related_breakpoint = retval;
7571 while (retval->related_breakpoint != new_b->related_breakpoint)
7572 retval = retval->related_breakpoint;
7573 retval->related_breakpoint = new_b;
7574 }
7575
7576 return retval;
7577}
7578
7579/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7580 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7581 stack.
7582
b4b3e2de
AB
7583 If the unwind fails then there is not sufficient information to discard
7584 dummy frames. In this case, elide the clean up and the dummy frames will
7585 be cleaned up next time this function is called from a location where
7586 unwinding is possible. */
e2e4d78b
JK
7587
7588void
b67a2c6f 7589check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7590{
7591 struct breakpoint *b, *b_tmp;
7592
7593 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7594 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7595 {
7596 struct breakpoint *dummy_b = b->related_breakpoint;
7597
b4b3e2de
AB
7598 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7599 chained off b->related_breakpoint. */
e2e4d78b
JK
7600 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7601 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7602
7603 /* If there was no bp_call_dummy breakpoint then there's nothing
7604 more to do. Or, if the dummy frame associated with the
7605 bp_call_dummy is still on the stack then we need to leave this
7606 bp_call_dummy in place. */
e2e4d78b
JK
7607 if (dummy_b->type != bp_call_dummy
7608 || frame_find_by_id (dummy_b->frame_id) != NULL)
7609 continue;
b4b3e2de
AB
7610
7611 /* We didn't find the dummy frame on the stack, this could be
7612 because we have longjmp'd to a stack frame that is previous to
7613 the dummy frame, or it could be because the stack unwind is
7614 broken at some point between the longjmp frame and the dummy
7615 frame.
7616
7617 Next we figure out why the stack unwind stopped. If it looks
7618 like the unwind is complete then we assume the dummy frame has
7619 been jumped over, however, if the unwind stopped for an
7620 unexpected reason then we assume the stack unwind is currently
7621 broken, and that we will (eventually) return to the dummy
7622 frame.
7623
7624 It might be tempting to consider using frame_id_inner here, but
7625 that is not safe. There is no guarantee that the stack frames
7626 we are looking at here are even on the same stack as the
7627 original dummy frame, hence frame_id_inner can't be used. See
7628 the comments on frame_id_inner for more details. */
7629 bool unwind_finished_unexpectedly = false;
bd2b40ac 7630 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
b4b3e2de 7631 {
bd2b40ac 7632 frame_info_ptr prev = get_prev_frame (fi);
b4b3e2de
AB
7633 if (prev == nullptr)
7634 {
7635 /* FI is the last stack frame. Why did this frame not
7636 unwind further? */
7637 auto stop_reason = get_frame_unwind_stop_reason (fi);
7638 if (stop_reason != UNWIND_NO_REASON
7639 && stop_reason != UNWIND_OUTERMOST)
7640 unwind_finished_unexpectedly = true;
7641 }
7642 fi = prev;
7643 }
7644 if (unwind_finished_unexpectedly)
7645 continue;
7646
00431a78 7647 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7648
7649 while (b->related_breakpoint != b)
7650 {
7651 if (b_tmp == b->related_breakpoint)
7652 b_tmp = b->related_breakpoint->next;
7653 delete_breakpoint (b->related_breakpoint);
7654 }
7655 delete_breakpoint (b);
7656 }
7657}
7658
1900040c
MS
7659void
7660enable_overlay_breakpoints (void)
7661{
43892fdf 7662 for (breakpoint *b : all_breakpoints ())
1900040c 7663 if (b->type == bp_overlay_event)
01add95b
SM
7664 {
7665 b->enable_state = bp_enabled;
7666 update_global_location_list (UGLL_MAY_INSERT);
7667 overlay_events_enabled = 1;
7668 }
1900040c
MS
7669}
7670
7671void
7672disable_overlay_breakpoints (void)
7673{
43892fdf 7674 for (breakpoint *b : all_breakpoints ())
1900040c 7675 if (b->type == bp_overlay_event)
01add95b
SM
7676 {
7677 b->enable_state = bp_disabled;
7678 update_global_location_list (UGLL_DONT_INSERT);
7679 overlay_events_enabled = 0;
7680 }
1900040c
MS
7681}
7682
aa7d318d
TT
7683/* Set an active std::terminate breakpoint for each std::terminate
7684 master breakpoint. */
7685void
7686set_std_terminate_breakpoint (void)
7687{
1428b37a 7688 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7689 if (b->pspace == current_program_space
7690 && b->type == bp_std_terminate_master)
7691 {
7ab97995
PA
7692 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7693 inferior_thread ()->global_num);
aa7d318d
TT
7694 }
7695}
7696
7697/* Delete all the std::terminate breakpoints. */
7698void
7699delete_std_terminate_breakpoint (void)
7700{
1428b37a 7701 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7702 if (b->type == bp_std_terminate)
7703 delete_breakpoint (b);
7704}
7705
c4093a6a 7706struct breakpoint *
a6d9a66e 7707create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7708{
7709 struct breakpoint *b;
c4093a6a 7710
9efa3c7f 7711 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 7712
b5de0fa7 7713 b->enable_state = bp_enabled;
264f9890
PA
7714 /* locspec has to be used or breakpoint_re_set will delete me. */
7715 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
c4093a6a 7716
44702360 7717 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7718
c4093a6a
JM
7719 return b;
7720}
7721
0101ce28
JJ
7722struct lang_and_radix
7723 {
7724 enum language lang;
7725 int radix;
7726 };
7727
4efc6507
DE
7728/* Create a breakpoint for JIT code registration and unregistration. */
7729
7730struct breakpoint *
7731create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7732{
9efa3c7f 7733 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 7734}
0101ce28 7735
03673fc7
PP
7736/* Remove JIT code registration and unregistration breakpoint(s). */
7737
7738void
7739remove_jit_event_breakpoints (void)
7740{
1428b37a 7741 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7742 if (b->type == bp_jit_event
7743 && b->loc->pspace == current_program_space)
7744 delete_breakpoint (b);
7745}
7746
cae688ec
JJ
7747void
7748remove_solib_event_breakpoints (void)
7749{
1428b37a 7750 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7751 if (b->type == bp_shlib_event
7752 && b->loc->pspace == current_program_space)
cae688ec
JJ
7753 delete_breakpoint (b);
7754}
7755
f37f681c
PA
7756/* See breakpoint.h. */
7757
7758void
7759remove_solib_event_breakpoints_at_next_stop (void)
7760{
1428b37a 7761 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7762 if (b->type == bp_shlib_event
7763 && b->loc->pspace == current_program_space)
7764 b->disposition = disp_del_at_next_stop;
7765}
7766
04086b45
PA
7767/* Helper for create_solib_event_breakpoint /
7768 create_and_insert_solib_event_breakpoint. Allows specifying which
7769 INSERT_MODE to pass through to update_global_location_list. */
7770
7771static struct breakpoint *
7772create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7773 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7774{
7775 struct breakpoint *b;
7776
9efa3c7f 7777 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 7778 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7779 return b;
7780}
7781
04086b45
PA
7782struct breakpoint *
7783create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7784{
7785 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7786}
7787
f37f681c
PA
7788/* See breakpoint.h. */
7789
7790struct breakpoint *
7791create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7792{
7793 struct breakpoint *b;
7794
04086b45
PA
7795 /* Explicitly tell update_global_location_list to insert
7796 locations. */
7797 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7798 if (!b->loc->inserted)
7799 {
7800 delete_breakpoint (b);
7801 return NULL;
7802 }
7803 return b;
7804}
7805
cae688ec
JJ
7806/* Disable any breakpoints that are on code in shared libraries. Only
7807 apply to enabled breakpoints, disabled ones can just stay disabled. */
7808
7809void
cb851954 7810disable_breakpoints_in_shlibs (void)
cae688ec 7811{
48d7020b 7812 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7813 {
7814 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7815 struct breakpoint *b = loc->owner;
7816
7817 /* We apply the check to all breakpoints, including disabled for
7818 those with loc->duplicate set. This is so that when breakpoint
7819 becomes enabled, or the duplicate is removed, gdb will try to
7820 insert all breakpoints. If we don't set shlib_disabled here,
7821 we'll try to insert those breakpoints and fail. */
7822 if (((b->type == bp_breakpoint)
7823 || (b->type == bp_jit_event)
7824 || (b->type == bp_hardware_breakpoint)
7825 || (is_tracepoint (b)))
7826 && loc->pspace == current_program_space
7827 && !loc->shlib_disabled
7828 && solib_name_from_address (loc->pspace, loc->address)
7829 )
7830 {
7831 loc->shlib_disabled = 1;
7832 }
7833 }
cae688ec
JJ
7834}
7835
63644780
NB
7836/* Disable any breakpoints and tracepoints that are in SOLIB upon
7837 notification of unloaded_shlib. Only apply to enabled breakpoints,
7838 disabled ones can just stay disabled. */
84acb35a 7839
75149521 7840static void
84acb35a
JJ
7841disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7842{
d8de7963 7843 bool disabled_shlib_breaks = false;
84acb35a 7844
48d7020b 7845 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7846 {
7847 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7848 struct breakpoint *b = loc->owner;
7849
7850 if (solib->pspace == loc->pspace
7851 && !loc->shlib_disabled
7852 && (((b->type == bp_breakpoint
7853 || b->type == bp_jit_event
7854 || b->type == bp_hardware_breakpoint)
7855 && (loc->loc_type == bp_loc_hardware_breakpoint
7856 || loc->loc_type == bp_loc_software_breakpoint))
7857 || is_tracepoint (b))
7858 && solib_contains_address_p (solib, loc->address))
7859 {
7860 loc->shlib_disabled = 1;
7861 /* At this point, we cannot rely on remove_breakpoint
7862 succeeding so we must mark the breakpoint as not inserted
7863 to prevent future errors occurring in remove_breakpoints. */
7864 loc->inserted = 0;
7865
7866 /* This may cause duplicate notifications for the same breakpoint. */
7867 gdb::observers::breakpoint_modified.notify (b);
7868
7869 if (!disabled_shlib_breaks)
7870 {
7871 target_terminal::ours_for_output ();
7872 warning (_("Temporarily disabling breakpoints "
7873 "for unloaded shared library \"%s\""),
7874 solib->so_name);
7875 }
d8de7963 7876 disabled_shlib_breaks = true;
01add95b
SM
7877 }
7878 }
84acb35a
JJ
7879}
7880
63644780
NB
7881/* Disable any breakpoints and tracepoints in OBJFILE upon
7882 notification of free_objfile. Only apply to enabled breakpoints,
7883 disabled ones can just stay disabled. */
7884
7885static void
7886disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7887{
63644780
NB
7888 if (objfile == NULL)
7889 return;
7890
d03de421
PA
7891 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7892 managed by the user with add-symbol-file/remove-symbol-file.
7893 Similarly to how breakpoints in shared libraries are handled in
7894 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7895 shlib_disabled so they end up uninserted on the next global
7896 location list update. Shared libraries not loaded by the user
7897 aren't handled here -- they're already handled in
7898 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7899 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7900 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7901 main objfile). */
7902 if ((objfile->flags & OBJF_SHARED) == 0
7903 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7904 return;
7905
43892fdf 7906 for (breakpoint *b : all_breakpoints ())
63644780 7907 {
d8de7963 7908 bool bp_modified = false;
63644780
NB
7909
7910 if (!is_breakpoint (b) && !is_tracepoint (b))
7911 continue;
7912
40cb8ca5 7913 for (bp_location *loc : b->locations ())
63644780
NB
7914 {
7915 CORE_ADDR loc_addr = loc->address;
7916
7917 if (loc->loc_type != bp_loc_hardware_breakpoint
7918 && loc->loc_type != bp_loc_software_breakpoint)
7919 continue;
7920
7921 if (loc->shlib_disabled != 0)
7922 continue;
7923
7924 if (objfile->pspace != loc->pspace)
7925 continue;
7926
7927 if (loc->loc_type != bp_loc_hardware_breakpoint
7928 && loc->loc_type != bp_loc_software_breakpoint)
7929 continue;
7930
7931 if (is_addr_in_objfile (loc_addr, objfile))
7932 {
7933 loc->shlib_disabled = 1;
08351840
PA
7934 /* At this point, we don't know whether the object was
7935 unmapped from the inferior or not, so leave the
7936 inserted flag alone. We'll handle failure to
7937 uninsert quietly, in case the object was indeed
7938 unmapped. */
63644780
NB
7939
7940 mark_breakpoint_location_modified (loc);
7941
d8de7963 7942 bp_modified = true;
63644780
NB
7943 }
7944 }
7945
7946 if (bp_modified)
76727919 7947 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7948 }
7949}
7950
b650a282 7951/* See breakpoint.h. */
ce78b96d 7952
92bb0228
PA
7953breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7954 bool temp, const char *cond_string_)
7955 : type (bptype),
7956 disposition (temp ? disp_del : disp_donttouch),
7957 gdbarch (gdbarch_),
7958 language (current_language->la_language),
7959 input_radix (::input_radix),
7960 cond_string (cond_string_ != nullptr
7961 ? make_unique_xstrdup (cond_string_)
7962 : nullptr),
7963 related_breakpoint (this)
7964{
7965}
7966
7967/* See breakpoint.h. */
7968
fed1c982 7969catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
92bb0228
PA
7970 const char *cond_string)
7971 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
c906108c 7972{
acd0955b 7973 add_dummy_location (this, current_program_space);
c5aa993b 7974
acd0955b 7975 pspace = current_program_space;
346774a9
PA
7976}
7977
b82d4ec9 7978breakpoint *
b270e6f9 7979install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 7980{
b270e6f9 7981 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 7982 set_breakpoint_number (internal, b);
558a9d82
YQ
7983 if (is_tracepoint (b))
7984 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
7985 if (!internal)
7986 mention (b);
76727919 7987 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
7988
7989 if (update_gll)
44702360 7990 update_global_location_list (UGLL_MAY_INSERT);
b82d4ec9
PA
7991
7992 return b;
c56053d2
PA
7993}
7994
c906108c 7995static int
fba45db2 7996hw_breakpoint_used_count (void)
c906108c 7997{
c906108c
SS
7998 int i = 0;
7999
43892fdf 8000 for (breakpoint *b : all_breakpoints ())
d6b74ac4 8001 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 8002 for (bp_location *bl : b->locations ())
f1310107
TJB
8003 {
8004 /* Special types of hardware breakpoints may use more than
8005 one register. */
c47614fe 8006 i += b->resources_needed (bl);
f1310107 8007 }
c906108c
SS
8008
8009 return i;
8010}
8011
a1398e0c
PA
8012/* Returns the resources B would use if it were a hardware
8013 watchpoint. */
8014
c906108c 8015static int
a1398e0c 8016hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8017{
c906108c
SS
8018 int i = 0;
8019
a1398e0c
PA
8020 if (!breakpoint_enabled (b))
8021 return 0;
8022
40cb8ca5 8023 for (bp_location *bl : b->locations ())
a1398e0c
PA
8024 {
8025 /* Special types of hardware watchpoints may use more than
8026 one register. */
c47614fe 8027 i += b->resources_needed (bl);
a1398e0c
PA
8028 }
8029
8030 return i;
8031}
8032
8033/* Returns the sum the used resources of all hardware watchpoints of
8034 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8035 the sum of the used resources of all hardware watchpoints of other
8036 types _not_ TYPE. */
8037
8038static int
8039hw_watchpoint_used_count_others (struct breakpoint *except,
8040 enum bptype type, int *other_type_used)
8041{
8042 int i = 0;
a1398e0c 8043
c906108c 8044 *other_type_used = 0;
43892fdf 8045 for (breakpoint *b : all_breakpoints ())
e09342b5 8046 {
a1398e0c
PA
8047 if (b == except)
8048 continue;
e09342b5
TJB
8049 if (!breakpoint_enabled (b))
8050 continue;
8051
a1398e0c
PA
8052 if (b->type == type)
8053 i += hw_watchpoint_use_count (b);
8054 else if (is_hardware_watchpoint (b))
8055 *other_type_used = 1;
e09342b5
TJB
8056 }
8057
c906108c
SS
8058 return i;
8059}
8060
c906108c 8061void
fba45db2 8062disable_watchpoints_before_interactive_call_start (void)
c906108c 8063{
43892fdf 8064 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8065 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8066 {
b5de0fa7 8067 b->enable_state = bp_call_disabled;
44702360 8068 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8069 }
c906108c
SS
8070}
8071
8072void
fba45db2 8073enable_watchpoints_after_interactive_call_stop (void)
c906108c 8074{
43892fdf 8075 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8076 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8077 {
b5de0fa7 8078 b->enable_state = bp_enabled;
44702360 8079 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8080 }
c906108c
SS
8081}
8082
8bea4e01
UW
8083void
8084disable_breakpoints_before_startup (void)
8085{
6c95b8df 8086 current_program_space->executing_startup = 1;
44702360 8087 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8088}
8089
8090void
8091enable_breakpoints_after_startup (void)
8092{
6c95b8df 8093 current_program_space->executing_startup = 0;
f8eba3c6 8094 breakpoint_re_set ();
8bea4e01
UW
8095}
8096
7a3e3265
PA
8097/* Allocate a new momentary breakpoint. */
8098
7ab97995 8099template<typename... Arg>
7a3e3265 8100static momentary_breakpoint *
7ab97995
PA
8101new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8102 Arg&&... args)
7a3e3265
PA
8103{
8104 if (type == bp_longjmp || type == bp_exception)
7ab97995
PA
8105 return new longjmp_breakpoint (gdbarch, type,
8106 std::forward<Arg> (args)...);
7a3e3265 8107 else
7ab97995
PA
8108 return new momentary_breakpoint (gdbarch, type,
8109 std::forward<Arg> (args)...);
7a3e3265
PA
8110}
8111
7c16b83e
PA
8112/* Set a momentary breakpoint of type TYPE at address specified by
8113 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8114 frame. */
c906108c 8115
454dafbd 8116breakpoint_up
a6d9a66e
UW
8117set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8118 struct frame_id frame_id, enum bptype type)
c906108c 8119{
193facb3
JK
8120 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8121 tail-called one. */
8122 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8123
7a3e3265 8124 std::unique_ptr<momentary_breakpoint> b
7ab97995
PA
8125 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8126 inferior_thread ()->global_num));
7a3e3265
PA
8127
8128 b->add_location (sal);
c906108c 8129
7a3e3265
PA
8130 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8131
44702360 8132 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8133
7a3e3265 8134 return bp;
c906108c 8135}
611c83ae 8136
06edf0c0 8137/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8138 The new breakpoint will have type TYPE, use OPS as its
8139 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8140
06edf0c0
PA
8141static struct breakpoint *
8142momentary_breakpoint_from_master (struct breakpoint *orig,
8143 enum bptype type,
7ab97995
PA
8144 int loc_enabled,
8145 int thread)
e58b0e63 8146{
7a3e3265 8147 std::unique_ptr<breakpoint> copy
7ab97995
PA
8148 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8149 orig->frame_id, thread));
ee89d0a4 8150 copy->loc = copy->allocate_location ();
0ba852ab 8151 set_breakpoint_location_function (copy->loc);
e58b0e63 8152
a6d9a66e 8153 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8154 copy->loc->requested_address = orig->loc->requested_address;
8155 copy->loc->address = orig->loc->address;
8156 copy->loc->section = orig->loc->section;
6c95b8df 8157 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8158 copy->loc->probe = orig->loc->probe;
f8eba3c6 8159 copy->loc->line_number = orig->loc->line_number;
2f202fde 8160 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8161 copy->loc->enabled = loc_enabled;
e58b0e63 8162
7a3e3265 8163 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
44702360 8164 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7a3e3265 8165 return b;
e58b0e63
PA
8166}
8167
06edf0c0
PA
8168/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8169 ORIG is NULL. */
8170
8171struct breakpoint *
8172clone_momentary_breakpoint (struct breakpoint *orig)
8173{
8174 /* If there's nothing to clone, then return nothing. */
8175 if (orig == NULL)
8176 return NULL;
8177
7ab97995
PA
8178 return momentary_breakpoint_from_master (orig, orig->type, 0,
8179 orig->thread);
06edf0c0
PA
8180}
8181
454dafbd 8182breakpoint_up
a6d9a66e
UW
8183set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8184 enum bptype type)
611c83ae
PA
8185{
8186 struct symtab_and_line sal;
8187
8188 sal = find_pc_line (pc, 0);
8189 sal.pc = pc;
8190 sal.section = find_pc_overlay (pc);
8191 sal.explicit_pc = 1;
8192
a6d9a66e 8193 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8194}
c906108c 8195\f
c5aa993b 8196
c906108c
SS
8197/* Tell the user we have just set a breakpoint B. */
8198
8199static void
b713485d 8200mention (const breakpoint *b)
c906108c 8201{
c47614fe 8202 b->print_mention ();
2d33446d 8203 current_uiout->text ("\n");
c906108c 8204}
c906108c 8205\f
c5aa993b 8206
5133a315 8207static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8208
7f32a4d5
PA
8209/* Handle "set breakpoint auto-hw on".
8210
8211 If the explicitly specified breakpoint type is not hardware
8212 breakpoint, check the memory map to see whether the breakpoint
8213 address is in read-only memory.
8214
8215 - location type is not hardware breakpoint, memory is read-only.
8216 We change the type of the location to hardware breakpoint.
8217
8218 - location type is hardware breakpoint, memory is read-write. This
8219 means we've previously made the location hardware one, but then the
8220 memory map changed, so we undo.
8221*/
8222
8223static void
8224handle_automatic_hardware_breakpoints (bp_location *bl)
8225{
8226 if (automatic_hardware_breakpoints
8227 && bl->owner->type != bp_hardware_breakpoint
8228 && (bl->loc_type == bp_loc_software_breakpoint
8229 || bl->loc_type == bp_loc_hardware_breakpoint))
8230 {
8231 /* When breakpoints are removed, remove_breakpoints will use
8232 location types we've just set here, the only possible problem
8233 is that memory map has changed during running program, but
8234 it's not going to work anyway with current gdb. */
8235 mem_region *mr = lookup_mem_region (bl->address);
8236
8237 if (mr != nullptr)
8238 {
8239 enum bp_loc_type new_type;
8240
8241 if (mr->attrib.mode != MEM_RW)
8242 new_type = bp_loc_hardware_breakpoint;
8243 else
8244 new_type = bp_loc_software_breakpoint;
8245
8246 if (new_type != bl->loc_type)
8247 {
8248 static bool said = false;
8249
8250 bl->loc_type = new_type;
8251 if (!said)
8252 {
6cb06a8c
TT
8253 gdb_printf (_("Note: automatically using "
8254 "hardware breakpoints for "
8255 "read-only addresses.\n"));
7f32a4d5
PA
8256 said = true;
8257 }
8258 }
8259 }
8260 }
8261}
8262
06615628 8263bp_location *
74421c0b 8264code_breakpoint::add_location (const symtab_and_line &sal)
0d381245 8265{
06615628 8266 struct bp_location *new_loc, **tmp;
3742cc8b 8267 CORE_ADDR adjusted_address;
06615628 8268 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8269
8270 if (loc_gdbarch == NULL)
06615628 8271 loc_gdbarch = gdbarch;
3742cc8b
YQ
8272
8273 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8274 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8275 location will be placed on the location chain. Adjustment of the
8276 breakpoint may cause target_read_memory() to be called and we do
8277 not want its scan of the location chain to find a breakpoint and
8278 location that's only been partially initialized. */
8279 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
60270718
AB
8280 sal.pc, type,
8281 sal.pspace);
0d381245 8282
d30113d4 8283 /* Sort the locations by their ADDRESS. */
06615628
PA
8284 new_loc = allocate_location ();
8285 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
d30113d4 8286 tmp = &((*tmp)->next))
0d381245 8287 ;
06615628
PA
8288 new_loc->next = *tmp;
8289 *tmp = new_loc;
8290
8291 new_loc->requested_address = sal.pc;
8292 new_loc->address = adjusted_address;
8293 new_loc->pspace = sal.pspace;
8294 new_loc->probe.prob = sal.prob;
8295 new_loc->probe.objfile = sal.objfile;
8296 gdb_assert (new_loc->pspace != NULL);
8297 new_loc->section = sal.section;
8298 new_loc->gdbarch = loc_gdbarch;
8299 new_loc->line_number = sal.line;
8300 new_loc->symtab = sal.symtab;
8301 new_loc->symbol = sal.symbol;
8302 new_loc->msymbol = sal.msymbol;
8303 new_loc->objfile = sal.objfile;
8304
8305 set_breakpoint_location_function (new_loc);
1a853c52 8306
6ae88661
LM
8307 /* While by definition, permanent breakpoints are already present in the
8308 code, we don't mark the location as inserted. Normally one would expect
8309 that GDB could rely on that breakpoint instruction to stop the program,
8310 thus removing the need to insert its own breakpoint, except that executing
8311 the breakpoint instruction can kill the target instead of reporting a
8312 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8313 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8314 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8315 breakpoint be inserted normally results in QEMU knowing about the GDB
8316 breakpoint, and thus trap before the breakpoint instruction is executed.
8317 (If GDB later needs to continue execution past the permanent breakpoint,
8318 it manually increments the PC, thus avoiding executing the breakpoint
8319 instruction.) */
06615628
PA
8320 if (bp_loc_is_permanent (new_loc))
8321 new_loc->permanent = 1;
1a853c52 8322
06615628 8323 return new_loc;
0d381245 8324}
514f746b
AR
8325\f
8326
5133a315
LM
8327/* Return true if LOC is pointing to a permanent breakpoint,
8328 return false otherwise. */
1cf4d951 8329
5133a315 8330static bool
1cf4d951
PA
8331bp_loc_is_permanent (struct bp_location *loc)
8332{
514f746b
AR
8333 gdb_assert (loc != NULL);
8334
cb1e4e32
PA
8335 /* If we have a non-breakpoint-backed catchpoint or a software
8336 watchpoint, just return 0. We should not attempt to read from
8337 the addresses the locations of these breakpoint types point to.
5133a315 8338 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8339 memory. */
cb1e4e32 8340 if (!bl_address_is_meaningful (loc))
5133a315 8341 return false;
244558af 8342
5ed8105e 8343 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8344 switch_to_program_space_and_thread (loc->pspace);
5133a315 8345 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8346}
8347
e7e0cddf
SS
8348/* Build a command list for the dprintf corresponding to the current
8349 settings of the dprintf style options. */
8350
8351static void
8352update_dprintf_command_list (struct breakpoint *b)
8353{
6f781ee3 8354 const char *dprintf_args = b->extra_string.get ();
8579fd13 8355 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf
SS
8356
8357 if (!dprintf_args)
8358 return;
8359
8360 dprintf_args = skip_spaces (dprintf_args);
8361
8362 /* Allow a comma, as it may have terminated a location, but don't
8363 insist on it. */
8364 if (*dprintf_args == ',')
8365 ++dprintf_args;
8366 dprintf_args = skip_spaces (dprintf_args);
8367
8368 if (*dprintf_args != '"')
8369 error (_("Bad format string, missing '\"'."));
8370
d3ce09f5 8371 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8372 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8373 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8374 {
e0700ba4 8375 if (dprintf_function.empty ())
e7e0cddf
SS
8376 error (_("No function supplied for dprintf call"));
8377
e0700ba4 8378 if (!dprintf_channel.empty ())
e7e0cddf 8379 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8380 dprintf_function.c_str (),
8381 dprintf_channel.c_str (),
e7e0cddf
SS
8382 dprintf_args);
8383 else
8384 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8385 dprintf_function.c_str (),
e7e0cddf
SS
8386 dprintf_args);
8387 }
d3ce09f5
SS
8388 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8389 {
8390 if (target_can_run_breakpoint_commands ())
8391 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8392 else
8393 {
8394 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8395 printf_line = xstrprintf ("printf %s", dprintf_args);
8396 }
8397 }
e7e0cddf 8398 else
f34652de 8399 internal_error (_("Invalid dprintf style."));
e7e0cddf 8400
f28045c2 8401 gdb_assert (printf_line != NULL);
e7e0cddf 8402
12973681
TT
8403 /* Manufacture a printf sequence. */
8404 struct command_line *printf_cmd_line
8579fd13 8405 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8406 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8407 command_lines_deleter ()));
e7e0cddf
SS
8408}
8409
8410/* Update all dprintf commands, making their command lists reflect
8411 current style settings. */
8412
8413static void
eb4c3f4a 8414update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8415 struct cmd_list_element *c)
8416{
43892fdf
SM
8417 for (breakpoint *b : all_breakpoints ())
8418 if (b->type == bp_dprintf)
e7e0cddf 8419 update_dprintf_command_list (b);
e7e0cddf 8420}
c3f6f71d 8421
74421c0b 8422code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
3b003a61
PA
8423 enum bptype type_,
8424 gdb::array_view<const symtab_and_line> sals,
264f9890 8425 location_spec_up &&locspec_,
3b003a61
PA
8426 gdb::unique_xmalloc_ptr<char> filter_,
8427 gdb::unique_xmalloc_ptr<char> cond_string_,
8428 gdb::unique_xmalloc_ptr<char> extra_string_,
8429 enum bpdisp disposition_,
8430 int thread_, int task_, int ignore_count_,
8431 int from_tty,
8432 int enabled_, unsigned flags,
8433 int display_canonical_)
8434 : breakpoint (gdbarch_, type_)
018d34a4 8435{
0d381245 8436 int i;
018d34a4
VP
8437
8438 if (type == bp_hardware_breakpoint)
8439 {
fbbd034e
AS
8440 int target_resources_ok;
8441
8442 i = hw_breakpoint_used_count ();
8443 target_resources_ok =
8444 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8445 i + 1, 0);
8446 if (target_resources_ok == 0)
8447 error (_("No hardware breakpoint support in the target."));
8448 else if (target_resources_ok < 0)
8449 error (_("Hardware breakpoints used exceeds limit."));
8450 }
8451
6c5b2ebe 8452 gdb_assert (!sals.empty ());
6c95b8df 8453
0a9ccb9d 8454 /* At most one of thread or task can be set on any breakpoint. */
2ecee236 8455 gdb_assert (thread == -1 || task == -1);
3b003a61
PA
8456 thread = thread_;
8457 task = task_;
0d381245 8458
3b003a61
PA
8459 cond_string = std::move (cond_string_);
8460 extra_string = std::move (extra_string_);
8461 ignore_count = ignore_count_;
8462 enable_state = enabled_ ? bp_enabled : bp_disabled;
8463 disposition = disposition_;
5af949e3 8464
3b003a61
PA
8465 if (type == bp_static_tracepoint
8466 || type == bp_static_marker_tracepoint)
8467 {
98ed24fb 8468 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
3b003a61 8469 struct static_tracepoint_marker marker;
0d381245 8470
3b003a61 8471 if (strace_marker_p (this))
0d381245 8472 {
3b003a61
PA
8473 /* We already know the marker exists, otherwise, we wouldn't
8474 see a sal for it. */
709438c7 8475 const char *p = &locspec_->to_string ()[3];
3b003a61 8476 const char *endp;
0fb4aa4b 8477
3b003a61 8478 p = skip_spaces (p);
0fb4aa4b 8479
3b003a61 8480 endp = skip_to_space (p);
0fb4aa4b 8481
3b003a61 8482 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8483
3b003a61
PA
8484 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8485 t->static_trace_marker_id.c_str ());
8486 }
8487 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8488 {
8489 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8490
3b003a61
PA
8491 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8492 t->static_trace_marker_id.c_str ());
0d381245
VP
8493 }
8494 else
3b003a61
PA
8495 warning (_("Couldn't determine the static tracepoint marker to probe"));
8496 }
8497
8498 for (const auto &sal : sals)
8499 {
8500 if (from_tty)
018d34a4 8501 {
3b003a61
PA
8502 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8503 if (loc_gdbarch == nullptr)
8504 loc_gdbarch = gdbarch;
8505
8506 describe_other_breakpoints (loc_gdbarch,
8507 sal.pspace, sal.pc, sal.section, thread);
0d381245
VP
8508 }
8509
3b003a61
PA
8510 bp_location *new_loc = add_location (sal);
8511 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8512 new_loc->inserted = 1;
8513
b5fa468f
TBA
8514 /* Do not set breakpoint locations conditions yet. As locations
8515 are inserted, they get sorted based on their addresses. Let
8516 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8517
8518 /* Dynamic printf requires and uses additional arguments on the
8519 command line, otherwise it's an error. */
8520 if (type == bp_dprintf)
8521 {
3b003a61
PA
8522 if (extra_string != nullptr)
8523 update_dprintf_command_list (this);
e7e0cddf
SS
8524 else
8525 error (_("Format string required"));
8526 }
3b003a61
PA
8527 else if (extra_string != nullptr)
8528 error (_("Garbage '%s' at end of command"), extra_string.get ());
855a6e68 8529 }
018d34a4 8530
b5fa468f
TBA
8531 /* The order of the locations is now stable. Set the location
8532 condition using the location's number. */
8533 int loc_num = 1;
3b003a61 8534 for (bp_location *bl : locations ())
b5fa468f 8535 {
3b003a61
PA
8536 if (cond_string != nullptr)
8537 set_breakpoint_location_condition (cond_string.get (), bl,
8538 number, loc_num);
b5fa468f
TBA
8539
8540 ++loc_num;
8541 }
8542
3b003a61 8543 display_canonical = display_canonical_;
264f9890
PA
8544 if (locspec_ != nullptr)
8545 locspec = std::move (locspec_);
018d34a4 8546 else
264f9890 8547 locspec = new_address_location_spec (this->loc->address, NULL, 0);
3b003a61 8548 filter = std::move (filter_);
d9b3f62e 8549}
018d34a4 8550
d9b3f62e
PA
8551static void
8552create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8553 gdb::array_view<const symtab_and_line> sals,
264f9890 8554 location_spec_up &&locspec,
e1e01040
PA
8555 gdb::unique_xmalloc_ptr<char> filter,
8556 gdb::unique_xmalloc_ptr<char> cond_string,
8557 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8558 enum bptype type, enum bpdisp disposition,
8559 int thread, int task, int ignore_count,
ef4848c7 8560 int from_tty,
44f238bb
PA
8561 int enabled, int internal, unsigned flags,
8562 int display_canonical)
d9b3f62e 8563{
74421c0b 8564 std::unique_ptr<code_breakpoint> b
3b003a61
PA
8565 = new_breakpoint_from_type (gdbarch,
8566 type,
8567 sals,
264f9890 8568 std::move (locspec),
3b003a61
PA
8569 std::move (filter),
8570 std::move (cond_string),
8571 std::move (extra_string),
8572 disposition,
8573 thread, task, ignore_count,
8574 from_tty,
8575 enabled, flags,
8576 display_canonical);
d9b3f62e 8577
b270e6f9 8578 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8579}
8580
8581/* Add SALS.nelts breakpoints to the breakpoint table. For each
8582 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8583 value. COND_STRING, if not NULL, specified the condition to be
8584 used for all breakpoints. Essentially the only case where
8585 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8586 function. In that case, it's still not possible to specify
8587 separate conditions for different overloaded functions, so
8588 we take just a single condition string.
8589
c3f6f71d 8590 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8591 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8592 array contents). If the function fails (error() is called), the
8593 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8594 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8595
8596static void
8cdf0e15 8597create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8598 struct linespec_result *canonical,
e1e01040
PA
8599 gdb::unique_xmalloc_ptr<char> cond_string,
8600 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8601 enum bptype type, enum bpdisp disposition,
8602 int thread, int task, int ignore_count,
ef4848c7 8603 int from_tty,
44f238bb 8604 int enabled, int internal, unsigned flags)
c906108c 8605{
f8eba3c6 8606 if (canonical->pre_expanded)
6c5b2ebe 8607 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8608
6c5b2ebe 8609 for (const auto &lsal : canonical->lsals)
c3f6f71d 8610 {
f00aae0f 8611 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8612 'break', without arguments. */
264f9890 8613 location_spec_up locspec
5c1ddcb6
PA
8614 = (canonical->locspec != nullptr
8615 ? canonical->locspec->clone ()
8616 : nullptr);
e1e01040 8617 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8618 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8619
6c5b2ebe 8620 create_breakpoint_sal (gdbarch, lsal.sals,
264f9890 8621 std::move (locspec),
e1e01040
PA
8622 std::move (filter_string),
8623 std::move (cond_string),
8624 std::move (extra_string),
e7e0cddf 8625 type, disposition,
ef4848c7 8626 thread, task, ignore_count,
44f238bb 8627 from_tty, enabled, internal, flags,
56435ebe 8628 canonical->special_display);
c3f6f71d 8629 }
c3f6f71d 8630}
c906108c 8631
264f9890 8632/* Parse LOCSPEC which is assumed to be a SAL specification possibly
c3f6f71d 8633 followed by conditionals. On return, SALS contains an array of SAL
264f9890
PA
8634 addresses found. LOCSPEC points to the end of the SAL (for
8635 linespec locspecs).
9998af43
TJB
8636
8637 The array and the line spec strings are allocated on the heap, it is
8638 the caller's responsibility to free them. */
c906108c 8639
b9362cc7 8640static void
264f9890 8641parse_breakpoint_sals (location_spec *locspec,
58438ac1 8642 struct linespec_result *canonical)
c3f6f71d 8643{
f00aae0f
KS
8644 struct symtab_and_line cursal;
8645
7464aeaa 8646 if (locspec->type () == LINESPEC_LOCATION_SPEC)
f00aae0f 8647 {
40d97ee2 8648 const char *spec = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 8649
a20714ff 8650 if (spec == NULL)
f00aae0f
KS
8651 {
8652 /* The last displayed codepoint, if it's valid, is our default
8653 breakpoint address. */
8654 if (last_displayed_sal_is_valid ())
8655 {
f00aae0f
KS
8656 /* Set sal's pspace, pc, symtab, and line to the values
8657 corresponding to the last call to print_frame_info.
8658 Be sure to reinitialize LINE with NOTCURRENT == 0
8659 as the breakpoint line number is inappropriate otherwise.
8660 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8661 symtab_and_line sal = get_last_displayed_sal ();
8662 CORE_ADDR pc = sal.pc;
8663
f00aae0f
KS
8664 sal = find_pc_line (pc, 0);
8665
8666 /* "break" without arguments is equivalent to "break *PC"
8667 where PC is the last displayed codepoint's address. So
8668 make sure to set sal.explicit_pc to prevent GDB from
8669 trying to expand the list of sals to include all other
8670 instances with the same symtab and line. */
8671 sal.pc = pc;
8672 sal.explicit_pc = 1;
8673
6c5b2ebe
PA
8674 struct linespec_sals lsal;
8675 lsal.sals = {sal};
f00aae0f
KS
8676 lsal.canonical = NULL;
8677
6c5b2ebe 8678 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8679 return;
8680 }
8681 else
8682 error (_("No default breakpoint address now."));
c906108c 8683 }
c906108c 8684 }
f00aae0f
KS
8685
8686 /* Force almost all breakpoints to be in terms of the
8687 current_source_symtab (which is decode_line_1's default).
8688 This should produce the results we want almost all of the
8689 time while leaving default_breakpoint_* alone.
8690
8691 ObjC: However, don't match an Objective-C method name which
8692 may have a '+' or '-' succeeded by a '['. */
8693 cursal = get_current_source_symtab_and_line ();
8694 if (last_displayed_sal_is_valid ())
c906108c 8695 {
a20714ff 8696 const char *spec = NULL;
cc80f267 8697
7464aeaa 8698 if (locspec->type () == LINESPEC_LOCATION_SPEC)
40d97ee2 8699 spec = as_linespec_location_spec (locspec)->spec_string;
cc80f267 8700
f00aae0f 8701 if (!cursal.symtab
a20714ff
PA
8702 || (spec != NULL
8703 && strchr ("+-", spec[0]) != NULL
8704 && spec[1] != '['))
f00aae0f 8705 {
264f9890 8706 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
8707 get_last_displayed_symtab (),
8708 get_last_displayed_line (),
8709 canonical, NULL, NULL);
8710 return;
8711 }
c906108c 8712 }
f00aae0f 8713
264f9890 8714 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 8715 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 8716}
c906108c 8717
c906108c 8718
c3f6f71d 8719/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8720 inserted as a breakpoint. If it can't throw an error. */
c906108c 8721
b9362cc7 8722static void
6c5b2ebe 8723breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 8724{
6c5b2ebe
PA
8725 for (auto &sal : sals)
8726 resolve_sal_pc (&sal);
c3f6f71d
JM
8727}
8728
7a697b8d
SS
8729/* Fast tracepoints may have restrictions on valid locations. For
8730 instance, a fast tracepoint using a jump instead of a trap will
8731 likely have to overwrite more bytes than a trap would, and so can
8732 only be placed where the instruction is longer than the jump, or a
8733 multi-instruction sequence does not have a jump into the middle of
8734 it, etc. */
8735
8736static void
8737check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 8738 gdb::array_view<const symtab_and_line> sals)
7a697b8d 8739{
6c5b2ebe 8740 for (const auto &sal : sals)
7a697b8d 8741 {
f8eba3c6
TT
8742 struct gdbarch *sarch;
8743
6c5b2ebe 8744 sarch = get_sal_arch (sal);
f8eba3c6
TT
8745 /* We fall back to GDBARCH if there is no architecture
8746 associated with SAL. */
8747 if (sarch == NULL)
8748 sarch = gdbarch;
281d762b
TT
8749 std::string msg;
8750 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 8751 error (_("May not have a fast tracepoint at %s%s"),
281d762b 8752 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
8753 }
8754}
8755
018d34a4
VP
8756/* Given TOK, a string specification of condition and thread, as
8757 accepted by the 'break' command, extract the condition
8758 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8759 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8760 If no condition is found, *COND_STRING is set to NULL.
8761 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8762
8763static void
bbc13ae3 8764find_condition_and_thread (const char *tok, CORE_ADDR pc,
6f781ee3
TT
8765 gdb::unique_xmalloc_ptr<char> *cond_string,
8766 int *thread, int *task,
8767 gdb::unique_xmalloc_ptr<char> *rest)
018d34a4 8768{
6f781ee3 8769 cond_string->reset ();
018d34a4 8770 *thread = -1;
2ecee236 8771 *task = -1;
6f781ee3 8772 rest->reset ();
733d554a 8773 bool force = false;
ed1d1739 8774
018d34a4
VP
8775 while (tok && *tok)
8776 {
bbc13ae3 8777 const char *end_tok;
018d34a4 8778 int toklen;
bbc13ae3
KS
8779 const char *cond_start = NULL;
8780 const char *cond_end = NULL;
cc59ec59 8781
f1735a53 8782 tok = skip_spaces (tok);
e7e0cddf
SS
8783
8784 if ((*tok == '"' || *tok == ',') && rest)
8785 {
6f781ee3 8786 rest->reset (savestring (tok, strlen (tok)));
e7e0cddf
SS
8787 return;
8788 }
8789
f1735a53 8790 end_tok = skip_to_space (tok);
d634f2de 8791
018d34a4 8792 toklen = end_tok - tok;
d634f2de 8793
018d34a4
VP
8794 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8795 {
8796 tok = cond_start = end_tok + 1;
733d554a
TBA
8797 try
8798 {
8799 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8800 }
8801 catch (const gdb_exception_error &)
8802 {
8803 if (!force)
8804 throw;
8805 else
8806 tok = tok + strlen (tok);
8807 }
018d34a4 8808 cond_end = tok;
6f781ee3 8809 cond_string->reset (savestring (cond_start, cond_end - cond_start));
018d34a4 8810 }
733d554a
TBA
8811 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8812 {
5759831a 8813 tok = tok + toklen;
733d554a
TBA
8814 force = true;
8815 }
018d34a4
VP
8816 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8817 {
5d5658a1
PA
8818 const char *tmptok;
8819 struct thread_info *thr;
d634f2de 8820
980dbf36
AB
8821 if (*thread != -1)
8822 error(_("You can specify only one thread."));
8823
2ecee236 8824 if (*task != -1)
0a9ccb9d
AB
8825 error (_("You can specify only one of thread or task."));
8826
018d34a4 8827 tok = end_tok + 1;
5d5658a1 8828 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
8829 if (tok == tmptok)
8830 error (_("Junk after thread keyword."));
5d5658a1 8831 *thread = thr->global_num;
bbc13ae3 8832 tok = tmptok;
018d34a4 8833 }
4a306c9a
JB
8834 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8835 {
8836 char *tmptok;
8837
2ecee236 8838 if (*task != -1)
980dbf36
AB
8839 error(_("You can specify only one task."));
8840
0a9ccb9d
AB
8841 if (*thread != -1)
8842 error (_("You can specify only one of thread or task."));
8843
4a306c9a 8844 tok = end_tok + 1;
bbc13ae3 8845 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
8846 if (tok == tmptok)
8847 error (_("Junk after task keyword."));
8848 if (!valid_task_id (*task))
b6199126 8849 error (_("Unknown task %d."), *task);
bbc13ae3 8850 tok = tmptok;
4a306c9a 8851 }
e7e0cddf
SS
8852 else if (rest)
8853 {
6f781ee3 8854 rest->reset (savestring (tok, strlen (tok)));
ccab2054 8855 return;
e7e0cddf 8856 }
018d34a4
VP
8857 else
8858 error (_("Junk at end of arguments."));
8859 }
8860}
8861
b5fa468f
TBA
8862/* Call 'find_condition_and_thread' for each sal in SALS until a parse
8863 succeeds. The parsed values are written to COND_STRING, THREAD,
8864 TASK, and REST. See the comment of 'find_condition_and_thread'
8865 for the description of these parameters and INPUT. */
8866
8867static void
8868find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
6f781ee3
TT
8869 const char *input,
8870 gdb::unique_xmalloc_ptr<char> *cond_string,
8871 int *thread, int *task,
8872 gdb::unique_xmalloc_ptr<char> *rest)
b5fa468f
TBA
8873{
8874 int num_failures = 0;
8875 for (auto &sal : sals)
8876 {
6f781ee3 8877 gdb::unique_xmalloc_ptr<char> cond;
0a9ccb9d 8878 int thread_id = -1;
2ecee236 8879 int task_id = -1;
6f781ee3 8880 gdb::unique_xmalloc_ptr<char> remaining;
b5fa468f
TBA
8881
8882 /* Here we want to parse 'arg' to separate condition from thread
8883 number. But because parsing happens in a context and the
8884 contexts of sals might be different, try each until there is
8885 success. Finding one successful parse is sufficient for our
8886 goal. When setting the breakpoint we'll re-parse the
8887 condition in the context of each sal. */
8888 try
8889 {
8890 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8891 &task_id, &remaining);
6f781ee3 8892 *cond_string = std::move (cond);
0a9ccb9d 8893 /* At most one of thread or task can be set. */
2ecee236 8894 gdb_assert (thread_id == -1 || task_id == -1);
b5fa468f
TBA
8895 *thread = thread_id;
8896 *task = task_id;
6f781ee3 8897 *rest = std::move (remaining);
b5fa468f
TBA
8898 break;
8899 }
8900 catch (const gdb_exception_error &e)
8901 {
8902 num_failures++;
8903 /* If no sal remains, do not continue. */
8904 if (num_failures == sals.size ())
8905 throw;
8906 }
8907 }
8908}
8909
0fb4aa4b
PA
8910/* Decode a static tracepoint marker spec. */
8911
6c5b2ebe 8912static std::vector<symtab_and_line>
f00aae0f 8913decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 8914{
f00aae0f
KS
8915 const char *p = &(*arg_p)[3];
8916 const char *endp;
0fb4aa4b 8917
f1735a53 8918 p = skip_spaces (p);
0fb4aa4b 8919
f1735a53 8920 endp = skip_to_space (p);
0fb4aa4b 8921
81b1e71c 8922 std::string marker_str (p, endp - p);
0fb4aa4b 8923
5d9310c4
SM
8924 std::vector<static_tracepoint_marker> markers
8925 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8926 if (markers.empty ())
81b1e71c
TT
8927 error (_("No known static tracepoint marker named %s"),
8928 marker_str.c_str ());
0fb4aa4b 8929
6c5b2ebe 8930 std::vector<symtab_and_line> sals;
5d9310c4 8931 sals.reserve (markers.size ());
0fb4aa4b 8932
5d9310c4 8933 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 8934 {
5d9310c4
SM
8935 symtab_and_line sal = find_pc_line (marker.address, 0);
8936 sal.pc = marker.address;
6c5b2ebe 8937 sals.push_back (sal);
5d9310c4 8938 }
0fb4aa4b 8939
0fb4aa4b
PA
8940 *arg_p = endp;
8941 return sals;
8942}
8943
bac7c5cf
GB
8944/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8945 according to IS_TRACEPOINT. */
8946
8947static const struct breakpoint_ops *
264f9890
PA
8948breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8949 bool is_tracepoint)
bac7c5cf
GB
8950{
8951 if (is_tracepoint)
8952 {
264f9890 8953 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8954 return &tracepoint_probe_breakpoint_ops;
8955 else
74421c0b 8956 return &code_breakpoint_ops;
bac7c5cf
GB
8957 }
8958 else
8959 {
264f9890 8960 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8961 return &bkpt_probe_breakpoint_ops;
8962 else
74421c0b 8963 return &code_breakpoint_ops;
bac7c5cf
GB
8964 }
8965}
8966
8967/* See breakpoint.h. */
8968
8969const struct breakpoint_ops *
264f9890
PA
8970breakpoint_ops_for_location_spec (const location_spec *locspec,
8971 bool is_tracepoint)
bac7c5cf 8972{
264f9890
PA
8973 if (locspec != nullptr)
8974 return (breakpoint_ops_for_location_spec_type
7464aeaa 8975 (locspec->type (), is_tracepoint));
74421c0b 8976 return &code_breakpoint_ops;
bac7c5cf
GB
8977}
8978
f00aae0f 8979/* See breakpoint.h. */
0101ce28 8980
8cdf0e15
VP
8981int
8982create_breakpoint (struct gdbarch *gdbarch,
264f9890 8983 location_spec *locspec,
e1e01040
PA
8984 const char *cond_string,
8985 int thread, const char *extra_string,
10a636cc 8986 bool force_condition, int parse_extra,
0fb4aa4b 8987 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8988 int ignore_count,
8989 enum auto_boolean pending_break_support,
c0a91b2b 8990 const struct breakpoint_ops *ops,
44f238bb
PA
8991 int from_tty, int enabled, int internal,
8992 unsigned flags)
c3f6f71d 8993{
7efd8fc2 8994 struct linespec_result canonical;
d8de7963 8995 bool pending = false;
2ecee236 8996 int task = -1;
86b17b60 8997 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8998
348d480f
PA
8999 gdb_assert (ops != NULL);
9000
f00aae0f
KS
9001 /* If extra_string isn't useful, set it to NULL. */
9002 if (extra_string != NULL && *extra_string == '\0')
9003 extra_string = NULL;
9004
a70b8144 9005 try
b78a6381 9006 {
264f9890 9007 ops->create_sals_from_location_spec (locspec, &canonical);
b78a6381 9008 }
230d2906 9009 catch (const gdb_exception_error &e)
0101ce28 9010 {
492d29ea
PA
9011 /* If caller is interested in rc value from parse, set
9012 value. */
9013 if (e.error == NOT_FOUND_ERROR)
0101ce28 9014 {
05ff989b
AC
9015 /* If pending breakpoint support is turned off, throw
9016 error. */
fa8d40ab
JJ
9017
9018 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9019 throw;
723a2275
VP
9020
9021 exception_print (gdb_stderr, e);
fa8d40ab 9022
dda83cd7 9023 /* If pending breakpoint support is auto query and the user
05ff989b 9024 selects no, then simply return the error code. */
059fb39f 9025 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9026 && !nquery (_("Make %s pending on future shared library load? "),
9027 bptype_string (type_wanted)))
fd9b8c24 9028 return 0;
fa8d40ab 9029
05ff989b
AC
9030 /* At this point, either the user was queried about setting
9031 a pending breakpoint and selected yes, or pending
9032 breakpoint behavior is on and thus a pending breakpoint
9033 is defaulted on behalf of the user. */
d8de7963 9034 pending = true;
0101ce28 9035 }
492d29ea 9036 else
eedc3f4f 9037 throw;
0101ce28 9038 }
492d29ea 9039
6c5b2ebe 9040 if (!pending && canonical.lsals.empty ())
492d29ea 9041 return 0;
c3f6f71d 9042
c3f6f71d
JM
9043 /* Resolve all line numbers to PC's and verify that the addresses
9044 are ok for the target. */
0101ce28 9045 if (!pending)
f8eba3c6 9046 {
6c5b2ebe
PA
9047 for (auto &lsal : canonical.lsals)
9048 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9049 }
c3f6f71d 9050
7a697b8d 9051 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9052 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9053 {
6c5b2ebe
PA
9054 for (const auto &lsal : canonical.lsals)
9055 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9056 }
7a697b8d 9057
c3f6f71d
JM
9058 /* Verify that condition can be parsed, before setting any
9059 breakpoints. Allocate a separate condition expression for each
4a64f543 9060 breakpoint. */
0101ce28 9061 if (!pending)
c3f6f71d 9062 {
e1e01040
PA
9063 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9064 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9065
f00aae0f 9066 if (parse_extra)
dda83cd7 9067 {
6f781ee3
TT
9068 gdb::unique_xmalloc_ptr<char> rest;
9069 gdb::unique_xmalloc_ptr<char> cond;
52d361e1 9070
6c5b2ebe 9071 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9072
b5fa468f
TBA
9073 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9074 &cond, &thread, &task, &rest);
6f781ee3
TT
9075 cond_string_copy = std::move (cond);
9076 extra_string_copy = std::move (rest);
dda83cd7 9077 }
2f069f6f 9078 else
dda83cd7 9079 {
f00aae0f
KS
9080 if (type_wanted != bp_dprintf
9081 && extra_string != NULL && *extra_string != '\0')
9082 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9083
10a636cc
TBA
9084 /* Check the validity of the condition. We should error out
9085 if the condition is invalid at all of the locations and
9086 if it is not forced. In the PARSE_EXTRA case above, this
9087 check is done when parsing the EXTRA_STRING. */
9088 if (cond_string != nullptr && !force_condition)
9089 {
9090 int num_failures = 0;
9091 const linespec_sals &lsal = canonical.lsals[0];
9092 for (const auto &sal : lsal.sals)
9093 {
9094 const char *cond = cond_string;
9095 try
9096 {
9097 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9098 /* One success is sufficient to keep going. */
9099 break;
9100 }
9101 catch (const gdb_exception_error &)
9102 {
9103 num_failures++;
9104 /* If this is the last sal, error out. */
9105 if (num_failures == lsal.sals.size ())
9106 throw;
9107 }
9108 }
9109 }
9110
0878d0fa
YQ
9111 /* Create a private copy of condition string. */
9112 if (cond_string)
e1e01040 9113 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9114 /* Create a private copy of any extra string. */
9115 if (extra_string)
e1e01040 9116 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9117 }
0fb4aa4b 9118
52d361e1 9119 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9120 std::move (cond_string_copy),
9121 std::move (extra_string_copy),
9122 type_wanted,
d9b3f62e 9123 tempflag ? disp_del : disp_donttouch,
ef4848c7 9124 thread, task, ignore_count,
44f238bb 9125 from_tty, enabled, internal, flags);
c906108c 9126 }
0101ce28
JJ
9127 else
9128 {
73063f51
TT
9129 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9130 type_wanted);
5c1ddcb6 9131 b->locspec = locspec->clone ();
bfccc43c 9132
f00aae0f
KS
9133 if (parse_extra)
9134 b->cond_string = NULL;
e12c7713
MK
9135 else
9136 {
9137 /* Create a private copy of condition string. */
6f781ee3
TT
9138 b->cond_string.reset (cond_string != NULL
9139 ? xstrdup (cond_string)
9140 : NULL);
15630549 9141 b->thread = thread;
e12c7713 9142 }
f00aae0f
KS
9143
9144 /* Create a private copy of any extra string. */
6f781ee3
TT
9145 b->extra_string.reset (extra_string != NULL
9146 ? xstrdup (extra_string)
9147 : NULL);
0101ce28 9148 b->ignore_count = ignore_count;
0101ce28 9149 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9150 b->condition_not_parsed = 1;
41447f92 9151 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9152 if ((type_wanted != bp_breakpoint
dda83cd7 9153 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9154 b->pspace = current_program_space;
8bea4e01 9155
b270e6f9 9156 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9157 }
9158
6c5b2ebe 9159 if (canonical.lsals.size () > 1)
95a42b64 9160 {
3e43a32a
MS
9161 warning (_("Multiple breakpoints were set.\nUse the "
9162 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9163 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9164 }
9165
44702360 9166 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9167
9168 return 1;
c3f6f71d 9169}
c906108c 9170
348d480f 9171/* Set a breakpoint.
72b2ff0e
VP
9172 ARG is a string describing breakpoint address,
9173 condition, and thread.
9174 FLAG specifies if a breakpoint is hardware on,
9175 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9176 and BP_TEMPFLAG. */
348d480f 9177
98deb0da 9178static void
f2fc3015 9179break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9180{
72b2ff0e 9181 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9182 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9183 ? bp_hardware_breakpoint
9184 : bp_breakpoint);
f00aae0f 9185
264f9890
PA
9186 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9187 const struct breakpoint_ops *ops
9188 = breakpoint_ops_for_location_spec (locspec.get (),
9189 false /* is_tracepoint */);
c3f6f71d 9190
8cdf0e15 9191 create_breakpoint (get_current_arch (),
264f9890 9192 locspec.get (),
10a636cc 9193 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9194 tempflag, type_wanted,
8cdf0e15
VP
9195 0 /* Ignore count */,
9196 pending_break_support,
55aa24fb 9197 ops,
8cdf0e15 9198 from_tty,
84f4c1fe 9199 1 /* enabled */,
44f238bb
PA
9200 0 /* internal */,
9201 0);
c906108c
SS
9202}
9203
c906108c
SS
9204/* Helper function for break_command_1 and disassemble_command. */
9205
9206void
fba45db2 9207resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9208{
9209 CORE_ADDR pc;
9210
9211 if (sal->pc == 0 && sal->symtab != NULL)
9212 {
9213 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9214 error (_("No line %d in file \"%s\"."),
05cba821 9215 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9216 sal->pc = pc;
6a048695 9217
4a64f543 9218 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9219 number, then skip the function prologue if necessary. */
6a048695 9220 if (sal->explicit_line)
059acae7 9221 skip_prologue_sal (sal);
c906108c
SS
9222 }
9223
9224 if (sal->section == 0 && sal->symtab != NULL)
9225 {
346d1dfe 9226 const struct blockvector *bv;
3977b71f 9227 const struct block *b;
c5aa993b 9228 struct symbol *sym;
c906108c 9229
43f3e411 9230 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9231 sal->symtab->compunit ());
c906108c
SS
9232 if (bv != NULL)
9233 {
7f0df278 9234 sym = block_linkage_function (b);
c906108c 9235 if (sym != NULL)
dae58e04
TT
9236 sal->section
9237 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9238 else
9239 {
4a64f543 9240 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9241 just have to look harder. This case can be executed
9242 if we have line numbers but no functions (as can
9243 happen in assembly source). */
c906108c 9244
5ed8105e 9245 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9246 switch_to_program_space_and_thread (sal->pspace);
c906108c 9247
5ed8105e 9248 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9249 if (msym.minsym)
1db66e34 9250 sal->section = msym.obj_section ();
c906108c
SS
9251 }
9252 }
9253 }
9254}
9255
9256void
0b39b52e 9257break_command (const char *arg, int from_tty)
c906108c 9258{
db107f19 9259 break_command_1 (arg, 0, from_tty);
c906108c
SS
9260}
9261
c906108c 9262void
0b39b52e 9263tbreak_command (const char *arg, int from_tty)
c906108c 9264{
db107f19 9265 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9266}
9267
c906108c 9268static void
0b39b52e 9269hbreak_command (const char *arg, int from_tty)
c906108c 9270{
db107f19 9271 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9272}
9273
9274static void
0b39b52e 9275thbreak_command (const char *arg, int from_tty)
c906108c 9276{
db107f19 9277 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9278}
9279
e7e0cddf
SS
9280/* The dynamic printf command is mostly like a regular breakpoint, but
9281 with a prewired command list consisting of a single output command,
9282 built from extra arguments supplied on the dprintf command
9283 line. */
9284
da821c7b 9285static void
0b39b52e 9286dprintf_command (const char *arg, int from_tty)
e7e0cddf 9287{
264f9890 9288 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f
KS
9289
9290 /* If non-NULL, ARG should have been advanced past the location;
9291 the next character must be ','. */
9292 if (arg != NULL)
9293 {
9294 if (arg[0] != ',' || arg[1] == '\0')
9295 error (_("Format string required"));
9296 else
9297 {
9298 /* Skip the comma. */
9299 ++arg;
9300 }
9301 }
9302
e7e0cddf 9303 create_breakpoint (get_current_arch (),
264f9890 9304 locspec.get (),
10a636cc 9305 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9306 0, bp_dprintf,
9307 0 /* Ignore count */,
9308 pending_break_support,
74421c0b 9309 &code_breakpoint_ops,
e7e0cddf
SS
9310 from_tty,
9311 1 /* enabled */,
9312 0 /* internal */,
9313 0);
9314}
9315
d3ce09f5 9316static void
0b39b52e 9317agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9318{
9319 error (_("May only run agent-printf on the target"));
9320}
9321
ec45bb67 9322/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9323
ec45bb67
TT
9324int
9325ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9326 const address_space *aspace,
9327 CORE_ADDR bp_addr,
9328 const target_waitstatus &ws)
f1310107 9329{
c272a98c
SM
9330 if (ws.kind () != TARGET_WAITKIND_STOPPED
9331 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9332 return 0;
9333
f1310107
TJB
9334 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9335 bl->length, aspace, bp_addr);
9336}
9337
ec45bb67 9338/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9339
ec45bb67
TT
9340int
9341ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9342{
9343 return target_ranged_break_num_registers ();
9344}
9345
ec45bb67 9346/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9347
ec45bb67 9348enum print_stop_action
7bd86313 9349ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9350{
ec45bb67 9351 struct bp_location *bl = loc;
79a45e25 9352 struct ui_out *uiout = current_uiout;
f1310107 9353
ec45bb67 9354 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9355
9356 /* Ranged breakpoints have only one location. */
9357 gdb_assert (bl && bl->next == NULL);
9358
ec45bb67 9359 annotate_breakpoint (number);
f303dbd6
PA
9360
9361 maybe_print_thread_hit_breakpoint (uiout);
9362
ec45bb67 9363 if (disposition == disp_del)
112e8700 9364 uiout->text ("Temporary ranged breakpoint ");
f1310107 9365 else
112e8700
SM
9366 uiout->text ("Ranged breakpoint ");
9367 if (uiout->is_mi_like_p ())
f1310107 9368 {
112e8700 9369 uiout->field_string ("reason",
f1310107 9370 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9371 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9372 }
78805ff8 9373 print_num_locno (bs, uiout);
112e8700 9374 uiout->text (", ");
f1310107
TJB
9375
9376 return PRINT_SRC_AND_LOC;
9377}
9378
ec45bb67 9379/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9380
ec45bb67 9381bool
a67bcaba 9382ranged_breakpoint::print_one (bp_location **last_loc) const
f1310107 9383{
ec45bb67 9384 struct bp_location *bl = loc;
f1310107 9385 struct value_print_options opts;
79a45e25 9386 struct ui_out *uiout = current_uiout;
f1310107
TJB
9387
9388 /* Ranged breakpoints have only one location. */
9389 gdb_assert (bl && bl->next == NULL);
9390
9391 get_user_print_options (&opts);
9392
9393 if (opts.addressprint)
9394 /* We don't print the address range here, it will be printed later
f7968895 9395 by ranged_breakpoint::print_one_detail. */
112e8700 9396 uiout->field_skip ("addr");
f1310107 9397 annotate_field (5);
ec45bb67 9398 print_breakpoint_location (this, bl);
f1310107 9399 *last_loc = bl;
c01e038b
TT
9400
9401 return true;
f1310107
TJB
9402}
9403
ec45bb67 9404/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9405
ec45bb67
TT
9406void
9407ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9408{
9409 CORE_ADDR address_start, address_end;
ec45bb67 9410 struct bp_location *bl = loc;
d7e74731 9411 string_file stb;
f1310107
TJB
9412
9413 gdb_assert (bl);
9414
9415 address_start = bl->address;
9416 address_end = address_start + bl->length - 1;
9417
112e8700 9418 uiout->text ("\taddress range: ");
d7e74731
PA
9419 stb.printf ("[%s, %s]",
9420 print_core_address (bl->gdbarch, address_start),
9421 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9422 uiout->field_stream ("addr", stb);
9423 uiout->text ("\n");
f1310107
TJB
9424}
9425
ec45bb67 9426/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9427
ec45bb67 9428void
b713485d 9429ranged_breakpoint::print_mention () const
f1310107 9430{
ec45bb67 9431 struct bp_location *bl = loc;
79a45e25 9432 struct ui_out *uiout = current_uiout;
f1310107
TJB
9433
9434 gdb_assert (bl);
ec45bb67 9435 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9436
2d33446d 9437 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
ec45bb67 9438 number, paddress (bl->gdbarch, bl->address),
2d33446d 9439 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9440}
9441
ec45bb67 9442/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9443
ec45bb67 9444void
4d1ae558 9445ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9446{
6cb06a8c 9447 gdb_printf (fp, "break-range %s, %s",
709438c7
PA
9448 locspec->to_string (),
9449 locspec_range_end->to_string ());
04d0163c 9450 print_recreate_thread (fp);
f1310107
TJB
9451}
9452
f1310107
TJB
9453/* Find the address where the end of the breakpoint range should be
9454 placed, given the SAL of the end of the range. This is so that if
9455 the user provides a line number, the end of the range is set to the
9456 last instruction of the given line. */
9457
9458static CORE_ADDR
9459find_breakpoint_range_end (struct symtab_and_line sal)
9460{
9461 CORE_ADDR end;
9462
9463 /* If the user provided a PC value, use it. Otherwise,
9464 find the address of the end of the given location. */
9465 if (sal.explicit_pc)
9466 end = sal.pc;
9467 else
9468 {
9469 int ret;
9470 CORE_ADDR start;
9471
9472 ret = find_line_pc_range (sal, &start, &end);
9473 if (!ret)
9474 error (_("Could not find location of the end of the range."));
9475
9476 /* find_line_pc_range returns the start of the next line. */
9477 end--;
9478 }
9479
9480 return end;
9481}
9482
9483/* Implement the "break-range" CLI command. */
9484
9485static void
0b39b52e 9486break_range_command (const char *arg, int from_tty)
f1310107 9487{
f2fc3015 9488 const char *arg_start;
f1310107
TJB
9489 struct linespec_result canonical_start, canonical_end;
9490 int bp_count, can_use_bp, length;
9491 CORE_ADDR end;
f1310107
TJB
9492
9493 /* We don't support software ranged breakpoints. */
9494 if (target_ranged_break_num_registers () < 0)
9495 error (_("This target does not support hardware ranged breakpoints."));
9496
9497 bp_count = hw_breakpoint_used_count ();
9498 bp_count += target_ranged_break_num_registers ();
9499 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9500 bp_count, 0);
9501 if (can_use_bp < 0)
9502 error (_("Hardware breakpoints used exceeds limit."));
9503
f8eba3c6 9504 arg = skip_spaces (arg);
f1310107
TJB
9505 if (arg == NULL || arg[0] == '\0')
9506 error(_("No address range specified."));
9507
f8eba3c6 9508 arg_start = arg;
264f9890
PA
9509 location_spec_up start_locspec
9510 = string_to_location_spec (&arg, current_language);
9511 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
f1310107
TJB
9512
9513 if (arg[0] != ',')
9514 error (_("Too few arguments."));
6c5b2ebe 9515 else if (canonical_start.lsals.empty ())
f1310107 9516 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9517
6c5b2ebe 9518 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9519
6c5b2ebe
PA
9520 if (canonical_start.lsals.size () > 1
9521 || lsal_start.sals.size () != 1)
f1310107
TJB
9522 error (_("Cannot create a ranged breakpoint with multiple locations."));
9523
6c5b2ebe 9524 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9525 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9526
9527 arg++; /* Skip the comma. */
f8eba3c6 9528 arg = skip_spaces (arg);
f1310107 9529
264f9890 9530 /* Parse the end location specification. */
f1310107 9531
f1310107
TJB
9532 arg_start = arg;
9533
f8eba3c6 9534 /* We call decode_line_full directly here instead of using
264f9890
PA
9535 parse_breakpoint_sals because we need to specify the start
9536 location spec's symtab and line as the default symtab and line
9537 for the end of the range. This makes it possible to have ranges
9538 like "foo.c:27, +14", where +14 means 14 lines from the start
9539 location spec. */
9540 location_spec_up end_locspec
9541 = string_to_location_spec (&arg, current_language);
9542 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9543 sal_start.symtab, sal_start.line,
9544 &canonical_end, NULL, NULL);
9545
6c5b2ebe 9546 if (canonical_end.lsals.empty ())
f1310107 9547 error (_("Could not find location of the end of the range."));
f8eba3c6 9548
6c5b2ebe
PA
9549 const linespec_sals &lsal_end = canonical_end.lsals[0];
9550 if (canonical_end.lsals.size () > 1
9551 || lsal_end.sals.size () != 1)
f1310107
TJB
9552 error (_("Cannot create a ranged breakpoint with multiple locations."));
9553
6c5b2ebe 9554 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9555
9556 end = find_breakpoint_range_end (sal_end);
9557 if (sal_start.pc > end)
177b42fe 9558 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9559
9560 length = end - sal_start.pc + 1;
9561 if (length < 0)
9562 /* Length overflowed. */
9563 error (_("Address range too large."));
9564 else if (length == 1)
9565 {
9566 /* This range is simple enough to be handled by
9567 the `hbreak' command. */
81b1e71c 9568 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9569
9570 return;
9571 }
9572
b925bf21
PA
9573 /* Now set up the breakpoint and install it. */
9574
9575 std::unique_ptr<breakpoint> br
9576 (new ranged_breakpoint (get_current_arch (),
9577 sal_start, length,
264f9890
PA
9578 std::move (start_locspec),
9579 std::move (end_locspec)));
f1310107 9580
27a62b43 9581 install_breakpoint (false, std::move (br), true);
f1310107
TJB
9582}
9583
4a64f543
MS
9584/* Return non-zero if EXP is verified as constant. Returned zero
9585 means EXP is variable. Also the constant detection may fail for
9586 some constant expressions and in such case still falsely return
9587 zero. */
2e6e3d9c 9588
dab72643 9589static bool
65d79d4b
SDJ
9590watchpoint_exp_is_const (const struct expression *exp)
9591{
1eaebe02 9592 return exp->op->constant_p ();
65d79d4b
SDJ
9593}
9594
3a292923 9595/* Implement the "re_set" method for watchpoints. */
348d480f 9596
3a292923
TT
9597void
9598watchpoint::re_set ()
348d480f
PA
9599{
9600 /* Watchpoint can be either on expression using entirely global
9601 variables, or it can be on local variables.
9602
9603 Watchpoints of the first kind are never auto-deleted, and even
9604 persist across program restarts. Since they can use variables
9605 from shared libraries, we need to reparse expression as libraries
9606 are loaded and unloaded.
9607
9608 Watchpoints on local variables can also change meaning as result
9609 of solib event. For example, if a watchpoint uses both a local
9610 and a global variables in expression, it's a local watchpoint,
9611 but unloading of a shared library will make the expression
9612 invalid. This is not a very common use case, but we still
9613 re-evaluate expression, to avoid surprises to the user.
9614
9615 Note that for local watchpoints, we re-evaluate it only if
9616 watchpoints frame id is still valid. If it's not, it means the
9617 watchpoint is out of scope and will be deleted soon. In fact,
9618 I'm not sure we'll ever be called in this case.
9619
9620 If a local watchpoint's frame id is still valid, then
3a292923 9621 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9622
3a5c3e22
PA
9623 Don't do anything about disabled watchpoints, since they will be
9624 reevaluated again when enabled. */
d8de7963 9625 update_watchpoint (this, true /* reparse */);
348d480f
PA
9626}
9627
3a292923 9628/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9629
3a292923
TT
9630int
9631watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9632{
3a292923 9633 int length = exact ? 1 : bl->length;
e09342b5
TJB
9634
9635 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9636 cond_exp.get ());
77b06cd7
TJB
9637}
9638
3a292923 9639/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9640
3a292923
TT
9641int
9642watchpoint::remove_location (struct bp_location *bl,
9643 enum remove_bp_reason reason)
77b06cd7 9644{
3a292923 9645 int length = exact ? 1 : bl->length;
e09342b5
TJB
9646
9647 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9648 cond_exp.get ());
e09342b5
TJB
9649}
9650
3a292923
TT
9651int
9652watchpoint::breakpoint_hit (const struct bp_location *bl,
9653 const address_space *aspace, CORE_ADDR bp_addr,
9654 const target_waitstatus &ws)
e09342b5 9655{
348d480f 9656 struct breakpoint *b = bl->owner;
77b06cd7 9657
348d480f
PA
9658 /* Continuable hardware watchpoints are treated as non-existent if the
9659 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9660 some data address). Otherwise gdb won't stop on a break instruction
9661 in the code (not from a breakpoint) when a hardware watchpoint has
9662 been defined. Also skip watchpoints which we know did not trigger
9663 (did not match the data address). */
9664 if (is_hardware_watchpoint (b)
3a292923 9665 && watchpoint_triggered == watch_triggered_no)
348d480f 9666 return 0;
9c06b0b4 9667
348d480f 9668 return 1;
9c06b0b4
TJB
9669}
9670
3a292923
TT
9671void
9672watchpoint::check_status (bpstat *bs)
9c06b0b4 9673{
348d480f 9674 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9675
348d480f 9676 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9677}
9678
3a292923
TT
9679/* Implement the "resources_needed" method for hardware
9680 watchpoints. */
9c06b0b4 9681
3a292923
TT
9682int
9683watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9684{
3a292923 9685 int length = exact? 1 : bl->length;
348d480f
PA
9686
9687 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9688}
9689
3a292923
TT
9690/* Implement the "works_in_software_mode" method for hardware
9691 watchpoints. */
9c06b0b4 9692
5a61e176 9693bool
3a292923 9694watchpoint::works_in_software_mode () const
9c06b0b4 9695{
efa80663 9696 /* Read and access watchpoints only work with hardware support. */
3a292923 9697 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9698}
9699
3a292923 9700enum print_stop_action
7bd86313 9701watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9702{
348d480f 9703 struct breakpoint *b;
348d480f 9704 enum print_stop_action result;
79a45e25 9705 struct ui_out *uiout = current_uiout;
348d480f
PA
9706
9707 gdb_assert (bs->bp_location_at != NULL);
9708
348d480f
PA
9709 b = bs->breakpoint_at;
9710
f303dbd6
PA
9711 annotate_watchpoint (b->number);
9712 maybe_print_thread_hit_breakpoint (uiout);
9713
d7e74731
PA
9714 string_file stb;
9715
76f9c9cf 9716 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
9717 switch (b->type)
9718 {
348d480f 9719 case bp_watchpoint:
9c06b0b4 9720 case bp_hardware_watchpoint:
112e8700
SM
9721 if (uiout->is_mi_like_p ())
9722 uiout->field_string
9723 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 9724 mention (b);
76f9c9cf 9725 tuple_emitter.emplace (uiout, "value");
112e8700 9726 uiout->text ("\nOld value = ");
850645cf 9727 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9728 uiout->field_stream ("old", stb);
9729 uiout->text ("\nNew value = ");
3a292923 9730 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9731 uiout->field_stream ("new", stb);
9732 uiout->text ("\n");
348d480f
PA
9733 /* More than one watchpoint may have been triggered. */
9734 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9735 break;
9736
9737 case bp_read_watchpoint:
112e8700
SM
9738 if (uiout->is_mi_like_p ())
9739 uiout->field_string
9740 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 9741 mention (b);
76f9c9cf 9742 tuple_emitter.emplace (uiout, "value");
112e8700 9743 uiout->text ("\nValue = ");
3a292923 9744 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9745 uiout->field_stream ("value", stb);
9746 uiout->text ("\n");
348d480f 9747 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9748 break;
9749
9750 case bp_access_watchpoint:
348d480f
PA
9751 if (bs->old_val != NULL)
9752 {
112e8700
SM
9753 if (uiout->is_mi_like_p ())
9754 uiout->field_string
9755 ("reason",
348d480f
PA
9756 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9757 mention (b);
76f9c9cf 9758 tuple_emitter.emplace (uiout, "value");
112e8700 9759 uiout->text ("\nOld value = ");
850645cf 9760 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9761 uiout->field_stream ("old", stb);
9762 uiout->text ("\nNew value = ");
348d480f
PA
9763 }
9764 else
9765 {
9766 mention (b);
112e8700
SM
9767 if (uiout->is_mi_like_p ())
9768 uiout->field_string
9769 ("reason",
348d480f 9770 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 9771 tuple_emitter.emplace (uiout, "value");
112e8700 9772 uiout->text ("\nValue = ");
348d480f 9773 }
3a292923 9774 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9775 uiout->field_stream ("new", stb);
9776 uiout->text ("\n");
348d480f 9777 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9778 break;
9779 default:
348d480f 9780 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9781 }
9782
348d480f
PA
9783 return result;
9784}
9785
3a292923 9786/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 9787
3a292923 9788void
b713485d 9789watchpoint::print_mention () const
348d480f 9790{
79a45e25 9791 struct ui_out *uiout = current_uiout;
46b9c129 9792 const char *tuple_name;
348d480f 9793
3a292923 9794 switch (type)
348d480f
PA
9795 {
9796 case bp_watchpoint:
112e8700 9797 uiout->text ("Watchpoint ");
46b9c129 9798 tuple_name = "wpt";
348d480f
PA
9799 break;
9800 case bp_hardware_watchpoint:
112e8700 9801 uiout->text ("Hardware watchpoint ");
46b9c129 9802 tuple_name = "wpt";
348d480f
PA
9803 break;
9804 case bp_read_watchpoint:
112e8700 9805 uiout->text ("Hardware read watchpoint ");
46b9c129 9806 tuple_name = "hw-rwpt";
348d480f
PA
9807 break;
9808 case bp_access_watchpoint:
112e8700 9809 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 9810 tuple_name = "hw-awpt";
348d480f
PA
9811 break;
9812 default:
f34652de 9813 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9814 }
9815
46b9c129 9816 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9817 uiout->field_signed ("number", number);
112e8700 9818 uiout->text (": ");
3a292923 9819 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
9820}
9821
3a292923 9822/* Implement the "print_recreate" method for watchpoints. */
348d480f 9823
3a292923 9824void
4d1ae558 9825watchpoint::print_recreate (struct ui_file *fp) const
348d480f 9826{
3a292923 9827 switch (type)
348d480f
PA
9828 {
9829 case bp_watchpoint:
9830 case bp_hardware_watchpoint:
6cb06a8c 9831 gdb_printf (fp, "watch");
348d480f
PA
9832 break;
9833 case bp_read_watchpoint:
6cb06a8c 9834 gdb_printf (fp, "rwatch");
348d480f
PA
9835 break;
9836 case bp_access_watchpoint:
6cb06a8c 9837 gdb_printf (fp, "awatch");
348d480f
PA
9838 break;
9839 default:
f34652de 9840 internal_error (_("Invalid watchpoint type."));
348d480f
PA
9841 }
9842
3a292923 9843 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 9844 print_recreate_thread (fp);
348d480f
PA
9845}
9846
3a292923 9847/* Implement the "explains_signal" method for watchpoints. */
427cd150 9848
a6860f3a 9849bool
3a292923 9850watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
9851{
9852 /* A software watchpoint cannot cause a signal other than
9853 GDB_SIGNAL_TRAP. */
3a292923 9854 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 9855 return false;
427cd150 9856
a6860f3a 9857 return true;
427cd150
TT
9858}
9859
3a292923
TT
9860struct masked_watchpoint : public watchpoint
9861{
73063f51
TT
9862 using watchpoint::watchpoint;
9863
3a292923
TT
9864 int insert_location (struct bp_location *) override;
9865 int remove_location (struct bp_location *,
9866 enum remove_bp_reason reason) override;
9867 int resources_needed (const struct bp_location *) override;
5a61e176 9868 bool works_in_software_mode () const override;
7bd86313 9869 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 9870 void print_one_detail (struct ui_out *) const override;
b713485d 9871 void print_mention () const override;
4d1ae558 9872 void print_recreate (struct ui_file *fp) const override;
3a292923 9873};
348d480f 9874
3a292923 9875/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 9876
3a292923
TT
9877int
9878masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 9879{
3a292923 9880 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
9881 bl->watchpoint_type);
9882}
9883
3a292923 9884/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 9885
3a292923
TT
9886int
9887masked_watchpoint::remove_location (struct bp_location *bl,
9888 enum remove_bp_reason reason)
348d480f 9889{
3a292923 9890 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 9891 bl->watchpoint_type);
348d480f
PA
9892}
9893
3a292923
TT
9894/* Implement the "resources_needed" method for masked hardware
9895 watchpoints. */
348d480f 9896
3a292923
TT
9897int
9898masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 9899{
3a292923 9900 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
9901}
9902
3a292923
TT
9903/* Implement the "works_in_software_mode" method for masked hardware
9904 watchpoints. */
348d480f 9905
5a61e176 9906bool
3a292923 9907masked_watchpoint::works_in_software_mode () const
348d480f 9908{
5a61e176 9909 return false;
348d480f
PA
9910}
9911
3a292923
TT
9912/* Implement the "print_it" method for masked hardware
9913 watchpoints. */
348d480f 9914
3a292923 9915enum print_stop_action
7bd86313 9916masked_watchpoint::print_it (const bpstat *bs) const
348d480f
PA
9917{
9918 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9919 struct ui_out *uiout = current_uiout;
348d480f
PA
9920
9921 /* Masked watchpoints have only one location. */
9922 gdb_assert (b->loc && b->loc->next == NULL);
9923
f303dbd6
PA
9924 annotate_watchpoint (b->number);
9925 maybe_print_thread_hit_breakpoint (uiout);
9926
348d480f
PA
9927 switch (b->type)
9928 {
9929 case bp_hardware_watchpoint:
112e8700
SM
9930 if (uiout->is_mi_like_p ())
9931 uiout->field_string
9932 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9933 break;
9934
9935 case bp_read_watchpoint:
112e8700
SM
9936 if (uiout->is_mi_like_p ())
9937 uiout->field_string
9938 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9939 break;
9940
9941 case bp_access_watchpoint:
112e8700
SM
9942 if (uiout->is_mi_like_p ())
9943 uiout->field_string
9944 ("reason",
348d480f
PA
9945 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9946 break;
9947 default:
f34652de 9948 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9949 }
9950
9951 mention (b);
112e8700 9952 uiout->text (_("\n\
9c06b0b4
TJB
9953Check the underlying instruction at PC for the memory\n\
9954address and value which triggered this watchpoint.\n"));
112e8700 9955 uiout->text ("\n");
9c06b0b4
TJB
9956
9957 /* More than one watchpoint may have been triggered. */
9958 return PRINT_UNKNOWN;
9959}
9960
3a292923
TT
9961/* Implement the "print_one_detail" method for masked hardware
9962 watchpoints. */
9c06b0b4 9963
3a292923
TT
9964void
9965masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
9966{
9967 /* Masked watchpoints have only one location. */
3a292923 9968 gdb_assert (loc && loc->next == NULL);
9c06b0b4 9969
112e8700 9970 uiout->text ("\tmask ");
3a292923 9971 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
112e8700 9972 uiout->text ("\n");
9c06b0b4
TJB
9973}
9974
3a292923
TT
9975/* Implement the "print_mention" method for masked hardware
9976 watchpoints. */
9c06b0b4 9977
3a292923 9978void
b713485d 9979masked_watchpoint::print_mention () const
9c06b0b4 9980{
79a45e25 9981 struct ui_out *uiout = current_uiout;
46b9c129 9982 const char *tuple_name;
9c06b0b4 9983
3a292923 9984 switch (type)
9c06b0b4
TJB
9985 {
9986 case bp_hardware_watchpoint:
112e8700 9987 uiout->text ("Masked hardware watchpoint ");
46b9c129 9988 tuple_name = "wpt";
9c06b0b4
TJB
9989 break;
9990 case bp_read_watchpoint:
112e8700 9991 uiout->text ("Masked hardware read watchpoint ");
46b9c129 9992 tuple_name = "hw-rwpt";
9c06b0b4
TJB
9993 break;
9994 case bp_access_watchpoint:
112e8700 9995 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 9996 tuple_name = "hw-awpt";
9c06b0b4
TJB
9997 break;
9998 default:
f34652de 9999 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10000 }
10001
46b9c129 10002 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 10003 uiout->field_signed ("number", number);
112e8700 10004 uiout->text (": ");
3a292923 10005 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
10006}
10007
3a292923
TT
10008/* Implement the "print_recreate" method for masked hardware
10009 watchpoints. */
9c06b0b4 10010
3a292923 10011void
4d1ae558 10012masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 10013{
3a292923 10014 switch (type)
9c06b0b4
TJB
10015 {
10016 case bp_hardware_watchpoint:
6cb06a8c 10017 gdb_printf (fp, "watch");
9c06b0b4
TJB
10018 break;
10019 case bp_read_watchpoint:
6cb06a8c 10020 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
10021 break;
10022 case bp_access_watchpoint:
6cb06a8c 10023 gdb_printf (fp, "awatch");
9c06b0b4
TJB
10024 break;
10025 default:
f34652de 10026 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10027 }
10028
3a292923
TT
10029 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10030 phex (hw_wp_mask, sizeof (CORE_ADDR)));
04d0163c 10031 print_recreate_thread (fp);
9c06b0b4
TJB
10032}
10033
9c06b0b4
TJB
10034/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10035
f2478a7e 10036static bool
9c06b0b4
TJB
10037is_masked_watchpoint (const struct breakpoint *b)
10038{
3a292923 10039 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
10040}
10041
53a5351d 10042/* accessflag: hw_write: watch write,
dda83cd7 10043 hw_read: watch read,
53a5351d 10044 hw_access: watch access (read or write) */
c906108c 10045static void
bbc13ae3 10046watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10047 bool just_location, bool internal)
c906108c 10048{
c1fc2657 10049 struct breakpoint *scope_breakpoint = NULL;
270140bd 10050 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10051 struct value *result;
bb9d5f81 10052 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10053 const char *exp_start = NULL;
10054 const char *exp_end = NULL;
10055 const char *tok, *end_tok;
9c06b0b4 10056 int toklen = -1;
bbc13ae3
KS
10057 const char *cond_start = NULL;
10058 const char *cond_end = NULL;
c906108c 10059 enum bptype bp_type;
37e4754d 10060 int thread = -1;
9c06b0b4
TJB
10061 /* Flag to indicate whether we are going to use masks for
10062 the hardware watchpoint. */
2e362716 10063 bool use_mask = false;
9c06b0b4 10064 CORE_ADDR mask = 0;
2ecee236 10065 int task = -1;
c906108c 10066
37e4754d
LM
10067 /* Make sure that we actually have parameters to parse. */
10068 if (arg != NULL && arg[0] != '\0')
10069 {
bbc13ae3
KS
10070 const char *value_start;
10071
10072 exp_end = arg + strlen (arg);
37e4754d 10073
9c06b0b4
TJB
10074 /* Look for "parameter value" pairs at the end
10075 of the arguments string. */
bbc13ae3 10076 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10077 {
10078 /* Skip whitespace at the end of the argument list. */
10079 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10080 tok--;
10081
10082 /* Find the beginning of the last token.
10083 This is the value of the parameter. */
10084 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10085 tok--;
10086 value_start = tok + 1;
10087
10088 /* Skip whitespace. */
10089 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10090 tok--;
10091
10092 end_tok = tok;
10093
10094 /* Find the beginning of the second to last token.
10095 This is the parameter itself. */
10096 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10097 tok--;
10098 tok++;
10099 toklen = end_tok - tok + 1;
10100
61012eef 10101 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10102 {
5d5658a1 10103 struct thread_info *thr;
9c06b0b4
TJB
10104 /* At this point we've found a "thread" token, which means
10105 the user is trying to set a watchpoint that triggers
10106 only in a specific thread. */
5d5658a1 10107 const char *endp;
37e4754d 10108
9c06b0b4
TJB
10109 if (thread != -1)
10110 error(_("You can specify only one thread."));
37e4754d 10111
2ecee236 10112 if (task != -1)
0a9ccb9d
AB
10113 error (_("You can specify only one of thread or task."));
10114
9c06b0b4 10115 /* Extract the thread ID from the next token. */
5d5658a1 10116 thr = parse_thread_id (value_start, &endp);
37e4754d 10117
5d5658a1 10118 /* Check if the user provided a valid thread ID. */
9c06b0b4 10119 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10120 invalid_thread_id_error (value_start);
9c06b0b4 10121
5d5658a1 10122 thread = thr->global_num;
9c06b0b4 10123 }
8a18382f
TT
10124 else if (toklen == 4 && startswith (tok, "task"))
10125 {
10126 char *tmp;
10127
2ecee236 10128 if (task != -1)
980dbf36
AB
10129 error(_("You can specify only one task."));
10130
0a9ccb9d
AB
10131 if (thread != -1)
10132 error (_("You can specify only one of thread or task."));
10133
8a18382f
TT
10134 task = strtol (value_start, &tmp, 0);
10135 if (tmp == value_start)
10136 error (_("Junk after task keyword."));
10137 if (!valid_task_id (task))
10138 error (_("Unknown task %d."), task);
10139 }
61012eef 10140 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10141 {
10142 /* We've found a "mask" token, which means the user wants to
10143 create a hardware watchpoint that is going to have the mask
10144 facility. */
65558ca5 10145 struct value *mask_value;
37e4754d 10146
9c06b0b4
TJB
10147 if (use_mask)
10148 error(_("You can specify only one mask."));
37e4754d 10149
2e362716 10150 use_mask = just_location = true;
37e4754d 10151
65558ca5 10152 scoped_value_mark mark;
9c06b0b4
TJB
10153 mask_value = parse_to_comma_and_eval (&value_start);
10154 mask = value_as_address (mask_value);
9c06b0b4
TJB
10155 }
10156 else
10157 /* We didn't recognize what we found. We should stop here. */
10158 break;
37e4754d 10159
9c06b0b4
TJB
10160 /* Truncate the string and get rid of the "parameter value" pair before
10161 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10162 exp_end = tok;
9c06b0b4 10163 }
37e4754d 10164 }
bbc13ae3
KS
10165 else
10166 exp_end = arg;
37e4754d 10167
bbc13ae3
KS
10168 /* Parse the rest of the arguments. From here on out, everything
10169 is in terms of a newly allocated string instead of the original
10170 ARG. */
81b1e71c
TT
10171 std::string expression (arg, exp_end - arg);
10172 exp_start = arg = expression.c_str ();
699bd4cf
TT
10173 innermost_block_tracker tracker;
10174 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10175 exp_end = arg;
fa8a61dc
TT
10176 /* Remove trailing whitespace from the expression before saving it.
10177 This makes the eventual display of the expression string a bit
10178 prettier. */
10179 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10180 --exp_end;
10181
65d79d4b 10182 /* Checking if the expression is not constant. */
4d01a485 10183 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10184 {
10185 int len;
10186
10187 len = exp_end - exp_start;
10188 while (len > 0 && isspace (exp_start[len - 1]))
10189 len--;
10190 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10191 }
10192
699bd4cf 10193 exp_valid_block = tracker.block ();
b926417a 10194 struct value *mark = value_mark ();
850645cf 10195 struct value *val_as_value = nullptr;
1eaebe02
TT
10196 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10197 just_location);
06a64a0b 10198
850645cf 10199 if (val_as_value != NULL && just_location)
bb9d5f81 10200 {
850645cf 10201 saved_bitpos = value_bitpos (val_as_value);
f49d5fa2 10202 saved_bitsize = val_as_value->bitsize ();
bb9d5f81
PP
10203 }
10204
850645cf 10205 value_ref_ptr val;
06a64a0b
TT
10206 if (just_location)
10207 {
9c06b0b4
TJB
10208 int ret;
10209
06a64a0b 10210 exp_valid_block = NULL;
850645cf 10211 val = release_value (value_addr (result));
06a64a0b 10212 value_free_to_mark (mark);
9c06b0b4
TJB
10213
10214 if (use_mask)
10215 {
850645cf 10216 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10217 mask);
10218 if (ret == -1)
10219 error (_("This target does not support masked watchpoints."));
10220 else if (ret == -2)
10221 error (_("Invalid mask or memory region."));
10222 }
06a64a0b 10223 }
850645cf
TT
10224 else if (val_as_value != NULL)
10225 val = release_value (val_as_value);
c906108c 10226
f1735a53
TT
10227 tok = skip_spaces (arg);
10228 end_tok = skip_to_space (tok);
c906108c
SS
10229
10230 toklen = end_tok - tok;
10231 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10232 {
10233 tok = cond_start = end_tok + 1;
699bd4cf
TT
10234 innermost_block_tracker if_tracker;
10235 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10236
10237 /* The watchpoint expression may not be local, but the condition
10238 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10239 cond_exp_valid_block = if_tracker.block ();
60e1c644 10240
c906108c
SS
10241 cond_end = tok;
10242 }
10243 if (*tok)
8a3fe4f8 10244 error (_("Junk at end of command."));
c906108c 10245
bd2b40ac 10246 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
441d7c93
PA
10247
10248 /* Save this because create_internal_breakpoint below invalidates
10249 'wp_frame'. */
10250 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10251
10252 /* If the expression is "local", then set up a "watchpoint scope"
10253 breakpoint at the point where we've left the scope of the watchpoint
10254 expression. Create the scope breakpoint before the watchpoint, so
10255 that we will encounter it first in bpstat_stop_status. */
441d7c93 10256 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10257 {
441d7c93
PA
10258 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10259
10260 if (frame_id_p (caller_frame_id))
edb3359d 10261 {
441d7c93
PA
10262 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10263 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10264
24b21115 10265 scope_breakpoint
441d7c93 10266 = create_internal_breakpoint (caller_arch, caller_pc,
9efa3c7f 10267 bp_watchpoint_scope);
d983da9c 10268
441d7c93
PA
10269 /* create_internal_breakpoint could invalidate WP_FRAME. */
10270 wp_frame = NULL;
10271
edb3359d 10272 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10273
edb3359d
DJ
10274 /* Automatically delete the breakpoint when it hits. */
10275 scope_breakpoint->disposition = disp_del;
d983da9c 10276
edb3359d 10277 /* Only break in the proper frame (help with recursion). */
441d7c93 10278 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10279
edb3359d 10280 /* Set the address at which we will stop. */
441d7c93
PA
10281 scope_breakpoint->loc->gdbarch = caller_arch;
10282 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10283 scope_breakpoint->loc->address
a6d9a66e
UW
10284 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10285 scope_breakpoint->loc->requested_address,
60270718
AB
10286 scope_breakpoint->type,
10287 current_program_space);
edb3359d 10288 }
d983da9c
DJ
10289 }
10290
e8369a73
AB
10291 /* Now set up the breakpoint. We create all watchpoints as hardware
10292 watchpoints here even if hardware watchpoints are turned off, a call
10293 to update_watchpoint later in this function will cause the type to
10294 drop back to bp_watchpoint (software watchpoint) if required. */
10295
10296 if (accessflag == hw_read)
10297 bp_type = bp_read_watchpoint;
10298 else if (accessflag == hw_access)
10299 bp_type = bp_access_watchpoint;
10300 else
10301 bp_type = bp_hardware_watchpoint;
3a5c3e22 10302
3a292923 10303 std::unique_ptr<watchpoint> w;
348d480f 10304 if (use_mask)
73063f51 10305 w.reset (new masked_watchpoint (nullptr, bp_type));
348d480f 10306 else
73063f51 10307 w.reset (new watchpoint (nullptr, bp_type));
3a292923 10308
0a9ccb9d 10309 /* At most one of thread or task can be set on a watchpoint. */
2ecee236 10310 gdb_assert (thread == -1 || task == -1);
c1fc2657 10311 w->thread = thread;
8a18382f 10312 w->task = task;
c1fc2657
SM
10313 w->disposition = disp_donttouch;
10314 w->pspace = current_program_space;
b22e99fd 10315 w->exp = std::move (exp);
3a5c3e22
PA
10316 w->exp_valid_block = exp_valid_block;
10317 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10318 if (just_location)
10319 {
d0c97917 10320 struct type *t = val.get ()->type ();
850645cf 10321 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10322
43cc5389 10323 w->exp_string_reparse
a4c50be3 10324 = current_language->watch_location_expression (t, addr);
06a64a0b 10325
8579fd13
AB
10326 w->exp_string = xstrprintf ("-location %.*s",
10327 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10328 }
10329 else
a4c50be3 10330 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10331
10332 if (use_mask)
10333 {
3a5c3e22 10334 w->hw_wp_mask = mask;
9c06b0b4
TJB
10335 }
10336 else
10337 {
3a5c3e22 10338 w->val = val;
bb9d5f81
PP
10339 w->val_bitpos = saved_bitpos;
10340 w->val_bitsize = saved_bitsize;
4c1d86d9 10341 w->val_valid = true;
9c06b0b4 10342 }
77b06cd7 10343
c906108c 10344 if (cond_start)
6f781ee3 10345 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10346 else
c1fc2657 10347 w->cond_string = 0;
c5aa993b 10348
441d7c93 10349 if (frame_id_p (watchpoint_frame))
f6bc2008 10350 {
441d7c93 10351 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10352 w->watchpoint_thread = inferior_ptid;
f6bc2008 10353 }
c906108c 10354 else
f6bc2008 10355 {
3a5c3e22
PA
10356 w->watchpoint_frame = null_frame_id;
10357 w->watchpoint_thread = null_ptid;
f6bc2008 10358 }
c906108c 10359
d983da9c 10360 if (scope_breakpoint != NULL)
c906108c 10361 {
d983da9c
DJ
10362 /* The scope breakpoint is related to the watchpoint. We will
10363 need to act on them together. */
c1fc2657 10364 w->related_breakpoint = scope_breakpoint;
b270e6f9 10365 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10366 }
d983da9c 10367
06a64a0b
TT
10368 if (!just_location)
10369 value_free_to_mark (mark);
2d134ed3 10370
b270e6f9
TT
10371 /* Finally update the new watchpoint. This creates the locations
10372 that should be inserted. */
d8de7963 10373 update_watchpoint (w.get (), true /* reparse */);
a9634178 10374
b270e6f9 10375 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10376}
10377
e09342b5 10378/* Return count of debug registers needed to watch the given expression.
e09342b5 10379 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10380
c906108c 10381static int
a6535de1 10382can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10383{
10384 int found_memory_cnt = 0;
10385
10386 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10387 if (!can_use_hw_watchpoints)
c906108c 10388 return 0;
c5aa993b 10389
a6535de1
TT
10390 gdb_assert (!vals.empty ());
10391 struct value *head = vals[0].get ();
10392
5c44784c
JM
10393 /* Make sure that the value of the expression depends only upon
10394 memory contents, and values computed from them within GDB. If we
10395 find any register references or function calls, we can't use a
10396 hardware watchpoint.
10397
10398 The idea here is that evaluating an expression generates a series
10399 of values, one holding the value of every subexpression. (The
10400 expression a*b+c has five subexpressions: a, b, a*b, c, and
10401 a*b+c.) GDB's values hold almost enough information to establish
10402 the criteria given above --- they identify memory lvalues,
10403 register lvalues, computed values, etcetera. So we can evaluate
10404 the expression, and then scan the chain of values that leaves
10405 behind to decide whether we can detect any possible change to the
10406 expression's final value using only hardware watchpoints.
10407
10408 However, I don't think that the values returned by inferior
10409 function calls are special in any way. So this function may not
10410 notice that an expression involving an inferior function call
10411 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10412 for (const value_ref_ptr &iter : vals)
c906108c 10413 {
a6535de1
TT
10414 struct value *v = iter.get ();
10415
5c44784c 10416 if (VALUE_LVAL (v) == lval_memory)
c906108c 10417 {
8464be76
DJ
10418 if (v != head && value_lazy (v))
10419 /* A lazy memory lvalue in the chain is one that GDB never
10420 needed to fetch; we either just used its address (e.g.,
10421 `a' in `a.b') or we never needed it at all (e.g., `a'
10422 in `a,b'). This doesn't apply to HEAD; if that is
10423 lazy then it was not readable, but watch it anyway. */
5c44784c 10424 ;
53a5351d 10425 else
5c44784c
JM
10426 {
10427 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10428 it with hardware watchpoints. */
d0c97917 10429 struct type *vtype = check_typedef (v->type ());
2e70b7b9
MS
10430
10431 /* We only watch structs and arrays if user asked for it
10432 explicitly, never if they just happen to appear in a
10433 middle of some value chain. */
10434 if (v == head
78134374
SM
10435 || (vtype->code () != TYPE_CODE_STRUCT
10436 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10437 {
42ae5230 10438 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10439 int len;
10440 int num_regs;
10441
a9634178 10442 len = (target_exact_watchpoints
e09342b5 10443 && is_scalar_type_recursive (vtype))?
d0c97917 10444 1 : v->type ()->length ();
2e70b7b9 10445
e09342b5
TJB
10446 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10447 if (!num_regs)
2e70b7b9
MS
10448 return 0;
10449 else
e09342b5 10450 found_memory_cnt += num_regs;
2e70b7b9 10451 }
5c44784c 10452 }
c5aa993b 10453 }
5086187c
AC
10454 else if (VALUE_LVAL (v) != not_lval
10455 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10456 return 0; /* These are values from the history (e.g., $1). */
5086187c 10457 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10458 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10459 }
10460
10461 /* The expression itself looks suitable for using a hardware
10462 watchpoint, but give the target machine a chance to reject it. */
10463 return found_memory_cnt;
10464}
10465
8b93c638 10466void
2e362716 10467watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10468{
84f4c1fe 10469 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10470}
10471
a15a5258
AB
10472/* Options for the watch, awatch, and rwatch commands. */
10473
10474struct watch_options
10475{
10476 /* For -location. */
10477 bool location = false;
10478};
10479
10480/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10481
10482 Historically GDB always accepted both '-location' and '-l' flags for
10483 these commands (both flags being synonyms). When converting to the
10484 newer option scheme only '-location' is added here. That's fine (for
10485 backward compatibility) as any non-ambiguous prefix of a flag will be
10486 accepted, so '-l', '-loc', are now all accepted.
10487
10488 What this means is that, if in the future, we add any new flag here
10489 that starts with '-l' then this will break backward compatibility, so
10490 please, don't do that! */
10491
10492static const gdb::option::option_def watch_option_defs[] = {
10493 gdb::option::flag_option_def<watch_options> {
10494 "location",
10495 [] (watch_options *opt) { return &opt->location; },
10496 N_("\
10497This evaluates EXPRESSION and watches the memory to which is refers.\n\
10498-l can be used as a short form of -location."),
10499 },
10500};
10501
10502/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10503 commands. */
10504
10505static gdb::option::option_def_group
10506make_watch_options_def_group (watch_options *opts)
10507{
10508 return {{watch_option_defs}, opts};
10509}
10510
06a64a0b
TT
10511/* A helper function that looks for the "-location" argument and then
10512 calls watch_command_1. */
10513
10514static void
0b39b52e 10515watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10516{
a15a5258
AB
10517 watch_options opts;
10518 auto grp = make_watch_options_def_group (&opts);
10519 gdb::option::process_options
10520 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10521 if (arg != nullptr && *arg == '\0')
10522 arg = nullptr;
10523
10524 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10525}
06a64a0b 10526
a15a5258
AB
10527/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10528static void
10529watch_command_completer (struct cmd_list_element *ignore,
10530 completion_tracker &tracker,
10531 const char *text, const char * /*word*/)
10532{
10533 const auto group = make_watch_options_def_group (nullptr);
10534 if (gdb::option::complete_options
10535 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10536 return;
06a64a0b 10537
a15a5258
AB
10538 const char *word = advance_to_expression_complete_word_point (tracker, text);
10539 expression_completer (ignore, tracker, text, word);
8b93c638 10540}
8926118c 10541
c5aa993b 10542static void
0b39b52e 10543watch_command (const char *arg, int from_tty)
c906108c 10544{
06a64a0b 10545 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10546}
10547
8b93c638 10548void
2e362716 10549rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10550{
84f4c1fe 10551 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10552}
8926118c 10553
c5aa993b 10554static void
0b39b52e 10555rwatch_command (const char *arg, int from_tty)
c906108c 10556{
06a64a0b 10557 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10558}
10559
8b93c638 10560void
2e362716 10561awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10562{
84f4c1fe 10563 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10564}
8926118c 10565
c5aa993b 10566static void
0b39b52e 10567awatch_command (const char *arg, int from_tty)
c906108c 10568{
06a64a0b 10569 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10570}
c906108c 10571\f
c5aa993b 10572
cfc31633
PA
10573/* Data for the FSM that manages the until(location)/advance commands
10574 in infcmd.c. Here because it uses the mechanisms of
10575 breakpoints. */
c906108c 10576
46e3ed7f 10577struct until_break_fsm : public thread_fsm
bfec99b2 10578{
46e3ed7f 10579 /* The thread that was current when the command was executed. */
cfc31633
PA
10580 int thread;
10581
df631783
PA
10582 /* The breakpoint set at the return address in the caller frame,
10583 plus breakpoints at all the destination locations. */
10584 std::vector<breakpoint_up> breakpoints;
cfc31633 10585
46e3ed7f 10586 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 10587 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
10588 : thread_fsm (cmd_interp),
10589 thread (thread),
df631783 10590 breakpoints (std::move (breakpoints))
46e3ed7f
TT
10591 {
10592 }
cfc31633 10593
46e3ed7f
TT
10594 void clean_up (struct thread_info *thread) override;
10595 bool should_stop (struct thread_info *thread) override;
10596 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10597};
10598
cfc31633
PA
10599/* Implementation of the 'should_stop' FSM method for the
10600 until(location)/advance commands. */
10601
46e3ed7f
TT
10602bool
10603until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10604{
df631783
PA
10605 for (const breakpoint_up &bp : breakpoints)
10606 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10607 bp.get ()) != NULL)
10608 {
10609 set_finished ();
10610 break;
10611 }
cfc31633 10612
46e3ed7f 10613 return true;
cfc31633
PA
10614}
10615
10616/* Implementation of the 'clean_up' FSM method for the
10617 until(location)/advance commands. */
10618
46e3ed7f
TT
10619void
10620until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10621{
cfc31633 10622 /* Clean up our temporary breakpoints. */
df631783 10623 breakpoints.clear ();
46e3ed7f 10624 delete_longjmp_breakpoint (thread);
cfc31633
PA
10625}
10626
10627/* Implementation of the 'async_reply_reason' FSM method for the
10628 until(location)/advance commands. */
10629
46e3ed7f
TT
10630enum async_reply_reason
10631until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10632{
10633 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10634}
10635
c906108c 10636void
f2fc3015 10637until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10638{
bd2b40ac 10639 frame_info_ptr frame;
8556afb4
PA
10640 struct gdbarch *frame_gdbarch;
10641 struct frame_id stack_frame_id;
10642 struct frame_id caller_frame_id;
186c406b
TT
10643 int thread;
10644 struct thread_info *tp;
c906108c 10645
70509625 10646 clear_proceed_status (0);
c906108c
SS
10647
10648 /* Set a breakpoint where the user wants it and at return from
4a64f543 10649 this function. */
c5aa993b 10650
264f9890 10651 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f 10652
6c5b2ebe
PA
10653 std::vector<symtab_and_line> sals
10654 = (last_displayed_sal_is_valid ()
264f9890 10655 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
6c5b2ebe
PA
10656 get_last_displayed_symtab (),
10657 get_last_displayed_line ())
264f9890 10658 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 10659 NULL, NULL, 0));
c5aa993b 10660
df631783 10661 if (sals.empty ())
8a3fe4f8 10662 error (_("Couldn't get information on specified line."));
c5aa993b 10663
c906108c 10664 if (*arg)
8a3fe4f8 10665 error (_("Junk at end of arguments."));
c5aa993b 10666
186c406b 10667 tp = inferior_thread ();
5d5658a1 10668 thread = tp->global_num;
186c406b 10669
8556afb4
PA
10670 /* Note linespec handling above invalidates the frame chain.
10671 Installing a breakpoint also invalidates the frame chain (as it
10672 may need to switch threads), so do any frame handling before
10673 that. */
10674
10675 frame = get_selected_frame (NULL);
10676 frame_gdbarch = get_frame_arch (frame);
10677 stack_frame_id = get_stack_frame_id (frame);
10678 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10679
ae66c1fc
EZ
10680 /* Keep within the current frame, or in frames called by the current
10681 one. */
edb3359d 10682
df631783 10683 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
10684
10685 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10686
883bc8d1 10687 if (frame_id_p (caller_frame_id))
c906108c 10688 {
883bc8d1 10689 struct symtab_and_line sal2;
cfc31633 10690 struct gdbarch *caller_gdbarch;
883bc8d1
PA
10691
10692 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10693 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 10694 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
10695
10696 breakpoint_up caller_breakpoint
10697 = set_momentary_breakpoint (caller_gdbarch, sal2,
10698 caller_frame_id, bp_until);
10699 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 10700
0f443d1b 10701 set_longjmp_breakpoint (tp, stack_frame_id);
5419bdae 10702 lj_deleter.emplace (thread);
c906108c 10703 }
c5aa993b 10704
c70a6932
JK
10705 /* set_momentary_breakpoint could invalidate FRAME. */
10706 frame = NULL;
10707
df631783
PA
10708 /* If the user told us to continue until a specified location, we
10709 don't specify a frame at which we need to stop. Otherwise,
10710 specify the selected frame, because we want to stop only at the
10711 very same frame. */
10712 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10713
10714 for (symtab_and_line &sal : sals)
10715 {
10716 resolve_sal_pc (&sal);
10717
10718 breakpoint_up location_breakpoint
10719 = set_momentary_breakpoint (frame_gdbarch, sal,
10720 stop_frame_id, bp_until);
10721 breakpoints.emplace_back (std::move (location_breakpoint));
10722 }
883bc8d1 10723
573269a8
LS
10724 tp->set_thread_fsm
10725 (std::unique_ptr<thread_fsm>
10726 (new until_break_fsm (command_interp (), tp->global_num,
10727 std::move (breakpoints))));
f107f563 10728
5419bdae
TT
10729 if (lj_deleter)
10730 lj_deleter->release ();
f107f563 10731
cfc31633 10732 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 10733}
ae66c1fc 10734
c906108c
SS
10735\f
10736
81b1e71c 10737/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
10738
10739static int
81b1e71c 10740compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 10741{
81b1e71c
TT
10742 uintptr_t ua = (uintptr_t) a;
10743 uintptr_t ub = (uintptr_t) b;
8a2c437b 10744
81b1e71c 10745 if (a->number < b->number)
8a2c437b 10746 return -1;
81b1e71c 10747 else if (a->number > b->number)
8a2c437b
TT
10748 return 1;
10749
10750 /* Now sort by address, in case we see, e..g, two breakpoints with
10751 the number 0. */
10752 if (ua < ub)
10753 return -1;
94b0e70d 10754 return ua > ub ? 1 : 0;
8a2c437b
TT
10755}
10756
80f8a6eb 10757/* Delete breakpoints by address or line. */
c906108c
SS
10758
10759static void
0b39b52e 10760clear_command (const char *arg, int from_tty)
c906108c 10761{
c906108c 10762 int default_match;
c906108c 10763
6c5b2ebe
PA
10764 std::vector<symtab_and_line> decoded_sals;
10765 symtab_and_line last_sal;
10766 gdb::array_view<symtab_and_line> sals;
c906108c
SS
10767 if (arg)
10768 {
6c5b2ebe
PA
10769 decoded_sals
10770 = decode_line_with_current_source (arg,
10771 (DECODE_LINE_FUNFIRSTLINE
10772 | DECODE_LINE_LIST_MODE));
c906108c 10773 default_match = 0;
6c5b2ebe 10774 sals = decoded_sals;
c906108c
SS
10775 }
10776 else
10777 {
1bfeeb0f
JL
10778 /* Set sal's line, symtab, pc, and pspace to the values
10779 corresponding to the last call to print_frame_info. If the
10780 codepoint is not valid, this will set all the fields to 0. */
51abb421 10781 last_sal = get_last_displayed_sal ();
6c5b2ebe 10782 if (last_sal.symtab == 0)
8a3fe4f8 10783 error (_("No source file specified."));
c906108c 10784
c906108c 10785 default_match = 1;
6c5b2ebe 10786 sals = last_sal;
c906108c
SS
10787 }
10788
4a64f543
MS
10789 /* We don't call resolve_sal_pc here. That's not as bad as it
10790 seems, because all existing breakpoints typically have both
10791 file/line and pc set. So, if clear is given file/line, we can
10792 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10793
10794 We only support clearing given the address explicitly
10795 present in breakpoint table. Say, we've set breakpoint
4a64f543 10796 at file:line. There were several PC values for that file:line,
ed0616c6 10797 due to optimization, all in one block.
4a64f543
MS
10798
10799 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10800 PC corresponding to the same file:line, the breakpoint won't
10801 be cleared. We probably can still clear the breakpoint, but
10802 since the other PC value is never presented to user, user
10803 can only find it by guessing, and it does not seem important
10804 to support that. */
10805
4a64f543
MS
10806 /* For each line spec given, delete bps which correspond to it. Do
10807 it in two passes, solely to preserve the current behavior that
10808 from_tty is forced true if we delete more than one
10809 breakpoint. */
c906108c 10810
81b1e71c 10811 std::vector<struct breakpoint *> found;
6c5b2ebe 10812 for (const auto &sal : sals)
c906108c 10813 {
05cba821
JK
10814 const char *sal_fullname;
10815
c906108c 10816 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
10817 If line given (pc == 0), clear all bpts on specified line.
10818 If defaulting, clear all bpts on default line
10819 or at default pc.
c5aa993b 10820
dda83cd7 10821 defaulting sal.pc != 0 tests to do
c5aa993b 10822
dda83cd7
SM
10823 0 1 pc
10824 1 1 pc _and_ line
10825 0 0 line
10826 1 0 <can't happen> */
c906108c 10827
05cba821
JK
10828 sal_fullname = (sal.symtab == NULL
10829 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 10830
4a64f543 10831 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 10832 for (breakpoint *b : all_breakpoints ())
c5aa993b 10833 {
0d381245 10834 int match = 0;
4a64f543 10835 /* Are we going to delete b? */
a5c69b1e
EL
10836 if (b->type != bp_none && !is_watchpoint (b)
10837 && user_breakpoint_p (b))
0d381245 10838 {
40cb8ca5 10839 for (bp_location *loc : b->locations ())
0d381245 10840 {
f8eba3c6
TT
10841 /* If the user specified file:line, don't allow a PC
10842 match. This matches historical gdb behavior. */
10843 int pc_match = (!sal.explicit_line
10844 && sal.pc
10845 && (loc->pspace == sal.pspace)
10846 && (loc->address == sal.pc)
10847 && (!section_is_overlay (loc->section)
10848 || loc->section == sal.section));
4aac40c8
TT
10849 int line_match = 0;
10850
10851 if ((default_match || sal.explicit_line)
2f202fde 10852 && loc->symtab != NULL
05cba821 10853 && sal_fullname != NULL
4aac40c8 10854 && sal.pspace == loc->pspace
05cba821
JK
10855 && loc->line_number == sal.line
10856 && filename_cmp (symtab_to_fullname (loc->symtab),
10857 sal_fullname) == 0)
10858 line_match = 1;
4aac40c8 10859
0d381245
VP
10860 if (pc_match || line_match)
10861 {
10862 match = 1;
10863 break;
10864 }
10865 }
10866 }
10867
10868 if (match)
81b1e71c 10869 found.push_back (b);
c906108c 10870 }
80f8a6eb 10871 }
8a2c437b 10872
80f8a6eb 10873 /* Now go thru the 'found' chain and delete them. */
81b1e71c 10874 if (found.empty ())
80f8a6eb
MS
10875 {
10876 if (arg)
8a3fe4f8 10877 error (_("No breakpoint at %s."), arg);
80f8a6eb 10878 else
8a3fe4f8 10879 error (_("No breakpoint at this line."));
80f8a6eb 10880 }
c906108c 10881
8a2c437b 10882 /* Remove duplicates from the vec. */
81b1e71c 10883 std::sort (found.begin (), found.end (),
b926417a 10884 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10885 {
b926417a 10886 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
10887 });
10888 found.erase (std::unique (found.begin (), found.end (),
b926417a 10889 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10890 {
b926417a 10891 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
10892 }),
10893 found.end ());
8a2c437b 10894
81b1e71c 10895 if (found.size () > 1)
4a64f543 10896 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10897 if (from_tty)
a3f17187 10898 {
81b1e71c 10899 if (found.size () == 1)
6cb06a8c 10900 gdb_printf (_("Deleted breakpoint "));
a3f17187 10901 else
6cb06a8c 10902 gdb_printf (_("Deleted breakpoints "));
a3f17187 10903 }
d6e956e5 10904
81b1e71c 10905 for (breakpoint *iter : found)
80f8a6eb 10906 {
c5aa993b 10907 if (from_tty)
6cb06a8c 10908 gdb_printf ("%d ", iter->number);
81b1e71c 10909 delete_breakpoint (iter);
c906108c 10910 }
80f8a6eb 10911 if (from_tty)
a11ac3b3 10912 gdb_putc ('\n');
c906108c
SS
10913}
10914\f
10915/* Delete breakpoint in BS if they are `delete' breakpoints and
10916 all breakpoints that are marked for deletion, whether hit or not.
10917 This is called after any breakpoint is hit, or after errors. */
10918
10919void
313f3b21 10920breakpoint_auto_delete (bpstat *bs)
c906108c 10921{
c906108c 10922 for (; bs; bs = bs->next)
f431efe5
PA
10923 if (bs->breakpoint_at
10924 && bs->breakpoint_at->disposition == disp_del
c906108c 10925 && bs->stop)
f431efe5 10926 delete_breakpoint (bs->breakpoint_at);
c906108c 10927
1428b37a 10928 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 10929 if (b->disposition == disp_del_at_next_stop)
c5aa993b 10930 delete_breakpoint (b);
c906108c
SS
10931}
10932
4a64f543 10933/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 10934 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
10935 bl_address_is_meaningful says), secondarily by ordering first
10936 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 10937 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
10938
10939static int
39ef2f62 10940bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 10941{
876fa593 10942 if (a->address != b->address)
39ef2f62 10943 return a->address < b->address;
876fa593 10944
dea2aa5f
LM
10945 /* Sort locations at the same address by their pspace number, keeping
10946 locations of the same inferior (in a multi-inferior environment)
10947 grouped. */
10948
10949 if (a->pspace->num != b->pspace->num)
39ef2f62 10950 return a->pspace->num < b->pspace->num;
dea2aa5f 10951
876fa593 10952 /* Sort permanent breakpoints first. */
1a853c52 10953 if (a->permanent != b->permanent)
39ef2f62 10954 return a->permanent > b->permanent;
876fa593 10955
7f32a4d5
PA
10956 /* Sort by type in order to make duplicate determination easier.
10957 See update_global_location_list. This is kept in sync with
10958 breakpoint_locations_match. */
10959 if (a->loc_type < b->loc_type)
10960 return true;
10961
10962 /* Likewise, for range-breakpoints, sort by length. */
10963 if (a->loc_type == bp_loc_hardware_breakpoint
10964 && b->loc_type == bp_loc_hardware_breakpoint
10965 && a->length < b->length)
10966 return true;
10967
c56a97f9
JK
10968 /* Make the internal GDB representation stable across GDB runs
10969 where A and B memory inside GDB can differ. Breakpoint locations of
10970 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
10971
10972 if (a->owner->number != b->owner->number)
39ef2f62 10973 return a->owner->number < b->owner->number;
876fa593 10974
39ef2f62 10975 return a < b;
876fa593
JK
10976}
10977
f5336ca5
PA
10978/* Set bp_locations_placed_address_before_address_max and
10979 bp_locations_shadow_len_after_address_max according to the current
10980 content of the bp_locations array. */
f7545552
TT
10981
10982static void
f5336ca5 10983bp_locations_target_extensions_update (void)
f7545552 10984{
f5336ca5
PA
10985 bp_locations_placed_address_before_address_max = 0;
10986 bp_locations_shadow_len_after_address_max = 0;
876fa593 10987
48d7020b 10988 for (bp_location *bl : all_bp_locations ())
876fa593
JK
10989 {
10990 CORE_ADDR start, end, addr;
10991
10992 if (!bp_location_has_shadow (bl))
10993 continue;
10994
10995 start = bl->target_info.placed_address;
10996 end = start + bl->target_info.shadow_len;
10997
10998 gdb_assert (bl->address >= start);
10999 addr = bl->address - start;
f5336ca5
PA
11000 if (addr > bp_locations_placed_address_before_address_max)
11001 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11002
11003 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11004
11005 gdb_assert (bl->address < end);
11006 addr = end - bl->address;
f5336ca5
PA
11007 if (addr > bp_locations_shadow_len_after_address_max)
11008 bp_locations_shadow_len_after_address_max = addr;
876fa593 11009 }
f7545552
TT
11010}
11011
1e4d1764
YQ
11012/* Download tracepoint locations if they haven't been. */
11013
11014static void
11015download_tracepoint_locations (void)
11016{
dd2e65cc 11017 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11018
5ed8105e 11019 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11020
f6d17b2b 11021 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
11022 {
11023 struct tracepoint *t;
d8de7963 11024 bool bp_location_downloaded = false;
1e4d1764 11025
7ed2c994 11026 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11027 ? !may_insert_fast_tracepoints
11028 : !may_insert_tracepoints))
11029 continue;
11030
dd2e65cc
YQ
11031 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11032 {
11033 if (target_can_download_tracepoint ())
11034 can_download_tracepoint = TRIBOOL_TRUE;
11035 else
11036 can_download_tracepoint = TRIBOOL_FALSE;
11037 }
11038
11039 if (can_download_tracepoint == TRIBOOL_FALSE)
11040 break;
11041
40cb8ca5 11042 for (bp_location *bl : b->locations ())
7ed2c994
YQ
11043 {
11044 /* In tracepoint, locations are _never_ duplicated, so
11045 should_be_inserted is equivalent to
11046 unduplicated_should_be_inserted. */
11047 if (!should_be_inserted (bl) || bl->inserted)
11048 continue;
1e4d1764 11049
7ed2c994 11050 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11051
7ed2c994 11052 target_download_tracepoint (bl);
1e4d1764 11053
7ed2c994 11054 bl->inserted = 1;
d8de7963 11055 bp_location_downloaded = true;
7ed2c994
YQ
11056 }
11057 t = (struct tracepoint *) b;
11058 t->number_on_target = b->number;
f2a8bc8a 11059 if (bp_location_downloaded)
76727919 11060 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11061 }
1e4d1764
YQ
11062}
11063
934709f0
PW
11064/* Swap the insertion/duplication state between two locations. */
11065
11066static void
11067swap_insertion (struct bp_location *left, struct bp_location *right)
11068{
11069 const int left_inserted = left->inserted;
11070 const int left_duplicate = left->duplicate;
b775012e 11071 const int left_needs_update = left->needs_update;
934709f0
PW
11072 const struct bp_target_info left_target_info = left->target_info;
11073
1e4d1764
YQ
11074 /* Locations of tracepoints can never be duplicated. */
11075 if (is_tracepoint (left->owner))
11076 gdb_assert (!left->duplicate);
11077 if (is_tracepoint (right->owner))
11078 gdb_assert (!right->duplicate);
11079
934709f0
PW
11080 left->inserted = right->inserted;
11081 left->duplicate = right->duplicate;
b775012e 11082 left->needs_update = right->needs_update;
934709f0
PW
11083 left->target_info = right->target_info;
11084 right->inserted = left_inserted;
11085 right->duplicate = left_duplicate;
b775012e 11086 right->needs_update = left_needs_update;
934709f0
PW
11087 right->target_info = left_target_info;
11088}
11089
b775012e
LM
11090/* Force the re-insertion of the locations at ADDRESS. This is called
11091 once a new/deleted/modified duplicate location is found and we are evaluating
11092 conditions on the target's side. Such conditions need to be updated on
11093 the target. */
11094
11095static void
11096force_breakpoint_reinsertion (struct bp_location *bl)
11097{
b775012e
LM
11098 CORE_ADDR address = 0;
11099 int pspace_num;
11100
11101 address = bl->address;
11102 pspace_num = bl->pspace->num;
11103
11104 /* This is only meaningful if the target is
11105 evaluating conditions and if the user has
11106 opted for condition evaluation on the target's
11107 side. */
11108 if (gdb_evaluates_breakpoint_condition_p ()
11109 || !target_supports_evaluation_of_breakpoint_conditions ())
11110 return;
11111
11112 /* Flag all breakpoint locations with this address and
11113 the same program space as the location
11114 as "its condition has changed". We need to
11115 update the conditions on the target's side. */
e0d9a270 11116 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11117 {
b775012e
LM
11118 if (!is_breakpoint (loc->owner)
11119 || pspace_num != loc->pspace->num)
11120 continue;
11121
11122 /* Flag the location appropriately. We use a different state to
11123 let everyone know that we already updated the set of locations
11124 with addr bl->address and program space bl->pspace. This is so
11125 we don't have to keep calling these functions just to mark locations
11126 that have already been marked. */
11127 loc->condition_changed = condition_updated;
11128
11129 /* Free the agent expression bytecode as well. We will compute
11130 it later on. */
833177a4 11131 loc->cond_bytecode.reset ();
b775012e
LM
11132 }
11133}
7f32a4d5 11134
44702360
PA
11135/* Called whether new breakpoints are created, or existing breakpoints
11136 deleted, to update the global location list and recompute which
11137 locations are duplicate of which.
b775012e 11138
04086b45
PA
11139 The INSERT_MODE flag determines whether locations may not, may, or
11140 shall be inserted now. See 'enum ugll_insert_mode' for more
11141 info. */
b60e7edf 11142
0d381245 11143static void
44702360 11144update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11145{
b775012e
LM
11146 /* Last breakpoint location address that was marked for update. */
11147 CORE_ADDR last_addr = 0;
11148 /* Last breakpoint location program space that was marked for update. */
11149 int last_pspace_num = -1;
f7545552 11150
2d134ed3
PA
11151 /* Used in the duplicates detection below. When iterating over all
11152 bp_locations, points to the first bp_location of a given address.
11153 Breakpoints and watchpoints of different types are never
11154 duplicates of each other. Keep one pointer for each type of
11155 breakpoint/watchpoint, so we only need to loop over all locations
11156 once. */
11157 struct bp_location *bp_loc_first; /* breakpoint */
11158 struct bp_location *wp_loc_first; /* hardware watchpoint */
11159 struct bp_location *awp_loc_first; /* access watchpoint */
11160 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11161
f5336ca5
PA
11162 /* Saved former bp_locations array which we compare against the newly
11163 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11164 std::vector<bp_location *> old_locations = std::move (bp_locations);
11165 bp_locations.clear ();
876fa593 11166
43892fdf 11167 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11168 for (bp_location *loc : b->locations ())
5d51cd5d 11169 bp_locations.push_back (loc);
7f32a4d5
PA
11170
11171 /* See if we need to "upgrade" a software breakpoint to a hardware
11172 breakpoint. Do this before deciding whether locations are
11173 duplicates. Also do this before sorting because sorting order
11174 depends on location type. */
5d51cd5d
SM
11175 for (bp_location *loc : bp_locations)
11176 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11177 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11178
5d51cd5d 11179 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11180 bp_location_is_less_than);
876fa593 11181
f5336ca5 11182 bp_locations_target_extensions_update ();
74960c60 11183
4a64f543
MS
11184 /* Identify bp_location instances that are no longer present in the
11185 new list, and therefore should be freed. Note that it's not
11186 necessary that those locations should be removed from inferior --
11187 if there's another location at the same address (previously
11188 marked as duplicate), we don't need to remove/insert the
11189 location.
876fa593 11190
4a64f543
MS
11191 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11192 and former bp_location array state respectively. */
876fa593 11193
5d51cd5d
SM
11194 size_t loc_i = 0;
11195 for (bp_location *old_loc : old_locations)
74960c60 11196 {
e5dd4106 11197 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11198 not, we have to free it. */
d8de7963 11199 bool found_object = false;
20874c92 11200 /* Tells if the location should remain inserted in the target. */
d8de7963
AB
11201 bool keep_in_target = false;
11202 bool removed = false;
876fa593 11203
4a64f543
MS
11204 /* Skip LOCP entries which will definitely never be needed.
11205 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11206 while (loc_i < bp_locations.size ()
11207 && bp_locations[loc_i]->address < old_loc->address)
11208 loc_i++;
c7d46a38 11209
5d51cd5d
SM
11210 for (size_t loc2_i = loc_i;
11211 (loc2_i < bp_locations.size ()
11212 && bp_locations[loc2_i]->address == old_loc->address);
11213 loc2_i++)
c7d46a38 11214 {
b775012e
LM
11215 /* Check if this is a new/duplicated location or a duplicated
11216 location that had its condition modified. If so, we want to send
11217 its condition to the target if evaluation of conditions is taking
11218 place there. */
5d51cd5d 11219 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11220 && (last_addr != old_loc->address
11221 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11222 {
5d51cd5d 11223 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11224 last_pspace_num = old_loc->pspace->num;
c7d46a38 11225 }
b775012e 11226
5d51cd5d 11227 if (bp_locations[loc2_i] == old_loc)
d8de7963 11228 found_object = true;
c7d46a38 11229 }
74960c60 11230
b775012e
LM
11231 /* We have already handled this address, update it so that we don't
11232 have to go through updates again. */
11233 last_addr = old_loc->address;
11234
11235 /* Target-side condition evaluation: Handle deleted locations. */
11236 if (!found_object)
11237 force_breakpoint_reinsertion (old_loc);
11238
4a64f543
MS
11239 /* If this location is no longer present, and inserted, look if
11240 there's maybe a new location at the same address. If so,
11241 mark that one inserted, and don't remove this one. This is
11242 needed so that we don't have a time window where a breakpoint
11243 at certain location is not inserted. */
74960c60 11244
876fa593 11245 if (old_loc->inserted)
0d381245 11246 {
4a64f543
MS
11247 /* If the location is inserted now, we might have to remove
11248 it. */
74960c60 11249
876fa593 11250 if (found_object && should_be_inserted (old_loc))
74960c60 11251 {
4a64f543
MS
11252 /* The location is still present in the location list,
11253 and still should be inserted. Don't do anything. */
d8de7963 11254 keep_in_target = true;
74960c60
VP
11255 }
11256 else
11257 {
b775012e
LM
11258 /* This location still exists, but it won't be kept in the
11259 target since it may have been disabled. We proceed to
11260 remove its target-side condition. */
11261
4a64f543
MS
11262 /* The location is either no longer present, or got
11263 disabled. See if there's another location at the
11264 same address, in which case we don't need to remove
11265 this one from the target. */
876fa593 11266
2bdf28a0 11267 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11268 if (bl_address_is_meaningful (old_loc))
876fa593 11269 {
5d51cd5d
SM
11270 for (size_t loc2_i = loc_i;
11271 (loc2_i < bp_locations.size ()
11272 && bp_locations[loc2_i]->address == old_loc->address);
11273 loc2_i++)
876fa593 11274 {
5d51cd5d 11275 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11276
7f32a4d5
PA
11277 if (loc2 == old_loc)
11278 continue;
11279
2d134ed3 11280 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11281 {
85d721b8
PA
11282 /* Read watchpoint locations are switched to
11283 access watchpoints, if the former are not
11284 supported, but the latter are. */
11285 if (is_hardware_watchpoint (old_loc->owner))
11286 {
11287 gdb_assert (is_hardware_watchpoint (loc2->owner));
11288 loc2->watchpoint_type = old_loc->watchpoint_type;
11289 }
11290
934709f0
PW
11291 /* loc2 is a duplicated location. We need to check
11292 if it should be inserted in case it will be
11293 unduplicated. */
7f32a4d5 11294 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11295 {
934709f0 11296 swap_insertion (old_loc, loc2);
d8de7963 11297 keep_in_target = true;
c7d46a38
PA
11298 break;
11299 }
876fa593
JK
11300 }
11301 }
11302 }
74960c60
VP
11303 }
11304
20874c92
VP
11305 if (!keep_in_target)
11306 {
834c0d03 11307 if (remove_breakpoint (old_loc))
20874c92 11308 {
4a64f543
MS
11309 /* This is just about all we can do. We could keep
11310 this location on the global list, and try to
11311 remove it next time, but there's no particular
11312 reason why we will succeed next time.
20874c92 11313
4a64f543
MS
11314 Note that at this point, old_loc->owner is still
11315 valid, as delete_breakpoint frees the breakpoint
11316 only after calling us. */
6cb06a8c
TT
11317 gdb_printf (_("warning: Error removing "
11318 "breakpoint %d\n"),
11319 old_loc->owner->number);
20874c92 11320 }
d8de7963 11321 removed = true;
20874c92 11322 }
0d381245 11323 }
74960c60
VP
11324
11325 if (!found_object)
1c5cfe86 11326 {
fbea99ea 11327 if (removed && target_is_non_stop_p ()
1cf4d951 11328 && need_moribund_for_location_type (old_loc))
20874c92 11329 {
db82e815
PA
11330 /* This location was removed from the target. In
11331 non-stop mode, a race condition is possible where
11332 we've removed a breakpoint, but stop events for that
11333 breakpoint are already queued and will arrive later.
11334 We apply an heuristic to be able to distinguish such
11335 SIGTRAPs from other random SIGTRAPs: we keep this
11336 breakpoint location for a bit, and will retire it
11337 after we see some number of events. The theory here
11338 is that reporting of events should, "on the average",
11339 be fair, so after a while we'll see events from all
11340 threads that have anything of interest, and no longer
11341 need to keep this breakpoint location around. We
11342 don't hold locations forever so to reduce chances of
11343 mistaking a non-breakpoint SIGTRAP for a breakpoint
11344 SIGTRAP.
11345
11346 The heuristic failing can be disastrous on
11347 decr_pc_after_break targets.
11348
11349 On decr_pc_after_break targets, like e.g., x86-linux,
11350 if we fail to recognize a late breakpoint SIGTRAP,
11351 because events_till_retirement has reached 0 too
11352 soon, we'll fail to do the PC adjustment, and report
11353 a random SIGTRAP to the user. When the user resumes
11354 the inferior, it will most likely immediately crash
2dec564e 11355 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11356 corrupted, because of being resumed e.g., in the
11357 middle of a multi-byte instruction, or skipped a
11358 one-byte instruction. This was actually seen happen
11359 on native x86-linux, and should be less rare on
11360 targets that do not support new thread events, like
11361 remote, due to the heuristic depending on
11362 thread_count.
11363
11364 Mistaking a random SIGTRAP for a breakpoint trap
11365 causes similar symptoms (PC adjustment applied when
11366 it shouldn't), but then again, playing with SIGTRAPs
11367 behind the debugger's back is asking for trouble.
11368
11369 Since hardware watchpoint traps are always
11370 distinguishable from other traps, so we don't need to
11371 apply keep hardware watchpoint moribund locations
11372 around. We simply always ignore hardware watchpoint
11373 traps we can no longer explain. */
11374
5b6d1e4f
PA
11375 process_stratum_target *proc_target = nullptr;
11376 for (inferior *inf : all_inferiors ())
11377 if (inf->pspace == old_loc->pspace)
11378 {
11379 proc_target = inf->process_target ();
11380 break;
11381 }
11382 if (proc_target != nullptr)
11383 old_loc->events_till_retirement
11384 = 3 * (thread_count (proc_target) + 1);
11385 else
11386 old_loc->events_till_retirement = 1;
876fa593 11387 old_loc->owner = NULL;
20874c92 11388
1123588c 11389 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11390 }
11391 else
f431efe5
PA
11392 {
11393 old_loc->owner = NULL;
11394 decref_bp_location (&old_loc);
11395 }
20874c92 11396 }
74960c60 11397 }
1c5cfe86 11398
348d480f
PA
11399 /* Rescan breakpoints at the same address and section, marking the
11400 first one as "first" and any others as "duplicates". This is so
11401 that the bpt instruction is only inserted once. If we have a
11402 permanent breakpoint at the same place as BPT, make that one the
11403 official one, and the rest as duplicates. Permanent breakpoints
11404 are sorted first for the same address.
11405
11406 Do the same for hardware watchpoints, but also considering the
11407 watchpoint's type (regular/access/read) and length. */
11408
11409 bp_loc_first = NULL;
11410 wp_loc_first = NULL;
11411 awp_loc_first = NULL;
11412 rwp_loc_first = NULL;
40cb8ca5 11413
48d7020b 11414 for (bp_location *loc : all_bp_locations ())
348d480f
PA
11415 {
11416 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11417 non-NULL. */
348d480f 11418 struct bp_location **loc_first_p;
43892fdf 11419 breakpoint *b = loc->owner;
348d480f 11420
6f380991 11421 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11422 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11423 /* Don't detect duplicate for tracepoint locations because they are
11424 never duplicated. See the comments in field `duplicate' of
11425 `struct bp_location'. */
348d480f 11426 || is_tracepoint (b))
b775012e
LM
11427 {
11428 /* Clear the condition modification flag. */
11429 loc->condition_changed = condition_unchanged;
11430 continue;
11431 }
348d480f 11432
348d480f
PA
11433 if (b->type == bp_hardware_watchpoint)
11434 loc_first_p = &wp_loc_first;
11435 else if (b->type == bp_read_watchpoint)
11436 loc_first_p = &rwp_loc_first;
11437 else if (b->type == bp_access_watchpoint)
11438 loc_first_p = &awp_loc_first;
11439 else
11440 loc_first_p = &bp_loc_first;
11441
11442 if (*loc_first_p == NULL
11443 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11444 || !breakpoint_locations_match (loc, *loc_first_p))
11445 {
11446 *loc_first_p = loc;
11447 loc->duplicate = 0;
b775012e
LM
11448
11449 if (is_breakpoint (loc->owner) && loc->condition_changed)
11450 {
11451 loc->needs_update = 1;
11452 /* Clear the condition modification flag. */
11453 loc->condition_changed = condition_unchanged;
11454 }
348d480f
PA
11455 continue;
11456 }
11457
934709f0
PW
11458
11459 /* This and the above ensure the invariant that the first location
11460 is not duplicated, and is the inserted one.
11461 All following are marked as duplicated, and are not inserted. */
11462 if (loc->inserted)
11463 swap_insertion (loc, *loc_first_p);
348d480f
PA
11464 loc->duplicate = 1;
11465
b775012e
LM
11466 /* Clear the condition modification flag. */
11467 loc->condition_changed = condition_unchanged;
348d480f
PA
11468 }
11469
a25a5a45 11470 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11471 {
04086b45 11472 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11473 insert_breakpoint_locations ();
11474 else
11475 {
44702360
PA
11476 /* Even though the caller told us to not insert new
11477 locations, we may still need to update conditions on the
11478 target's side of breakpoints that were already inserted
11479 if the target is evaluating breakpoint conditions. We
b775012e
LM
11480 only update conditions for locations that are marked
11481 "needs_update". */
11482 update_inserted_breakpoint_locations ();
11483 }
11484 }
348d480f 11485
04086b45 11486 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 11487 download_tracepoint_locations ();
348d480f
PA
11488}
11489
11490void
11491breakpoint_retire_moribund (void)
11492{
1123588c
TT
11493 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11494 {
11495 struct bp_location *loc = moribund_locations[ix];
11496 if (--(loc->events_till_retirement) == 0)
11497 {
11498 decref_bp_location (&loc);
11499 unordered_remove (moribund_locations, ix);
11500 --ix;
11501 }
11502 }
348d480f
PA
11503}
11504
11505static void
44702360 11506update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 11507{
348d480f 11508
a70b8144 11509 try
492d29ea
PA
11510 {
11511 update_global_location_list (insert_mode);
11512 }
230d2906 11513 catch (const gdb_exception_error &e)
492d29ea
PA
11514 {
11515 }
348d480f
PA
11516}
11517
11518/* Clear BKP from a BPS. */
11519
11520static void
313f3b21 11521bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
348d480f 11522{
313f3b21 11523 bpstat *bs;
348d480f
PA
11524
11525 for (bs = bps; bs; bs = bs->next)
11526 if (bs->breakpoint_at == bpt)
11527 {
11528 bs->breakpoint_at = NULL;
11529 bs->old_val = NULL;
11530 /* bs->commands will be freed later. */
11531 }
11532}
11533
11534/* Callback for iterate_over_threads. */
11535static int
11536bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11537{
9a3c8263 11538 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
11539
11540 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11541 return 0;
11542}
11543
7987c463 11544/* See breakpoint.h. */
348d480f 11545
7987c463
TT
11546void
11547code_breakpoint::say_where () const
348d480f
PA
11548{
11549 struct value_print_options opts;
11550
11551 get_user_print_options (&opts);
11552
11553 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11554 single string. */
7987c463 11555 if (loc == NULL)
348d480f 11556 {
f00aae0f 11557 /* For pending locations, the output differs slightly based
7987c463 11558 on extra_string. If this is non-NULL, it contains either
f00aae0f 11559 a condition or dprintf arguments. */
7987c463 11560 if (extra_string == NULL)
f00aae0f 11561 {
7987c463 11562 gdb_printf (_(" (%s) pending."), locspec->to_string ());
f00aae0f 11563 }
7987c463 11564 else if (type == bp_dprintf)
f00aae0f 11565 {
6cb06a8c 11566 gdb_printf (_(" (%s,%s) pending."),
7987c463
TT
11567 locspec->to_string (),
11568 extra_string.get ());
f00aae0f
KS
11569 }
11570 else
11571 {
6cb06a8c 11572 gdb_printf (_(" (%s %s) pending."),
7987c463
TT
11573 locspec->to_string (),
11574 extra_string.get ());
f00aae0f 11575 }
348d480f
PA
11576 }
11577 else
11578 {
7987c463 11579 if (opts.addressprint || loc->symtab == NULL)
6cb06a8c
TT
11580 gdb_printf (" at %ps",
11581 styled_string (address_style.style (),
7987c463
TT
11582 paddress (loc->gdbarch,
11583 loc->address)));
11584 if (loc->symtab != NULL)
f8eba3c6
TT
11585 {
11586 /* If there is a single location, we can print the location
11587 more nicely. */
7987c463 11588 if (loc->next == NULL)
0bb296cb 11589 {
6a831f06 11590 const char *filename
7987c463 11591 = symtab_to_filename_for_display (loc->symtab);
6cb06a8c
TT
11592 gdb_printf (": file %ps, line %d.",
11593 styled_string (file_name_style.style (),
11594 filename),
7987c463 11595 loc->line_number);
0bb296cb 11596 }
f8eba3c6
TT
11597 else
11598 /* This is not ideal, but each location may have a
11599 different file name, and this at least reflects the
11600 real situation somewhat. */
7987c463 11601 gdb_printf (": %s.", locspec->to_string ());
f8eba3c6 11602 }
348d480f 11603
7987c463 11604 if (loc->next)
348d480f 11605 {
066620dc 11606 struct bp_location *iter = loc;
348d480f 11607 int n = 0;
066620dc 11608 for (; iter; iter = iter->next)
348d480f 11609 ++n;
6cb06a8c 11610 gdb_printf (" (%d locations)", n);
348d480f
PA
11611 }
11612 }
11613}
11614
40cb8ca5
SM
11615/* See breakpoint.h. */
11616
a67bcaba 11617bp_location_range breakpoint::locations () const
40cb8ca5 11618{
9be25986 11619 return bp_location_range (this->loc);
40cb8ca5
SM
11620}
11621
4c6a92b1
TT
11622struct bp_location *
11623breakpoint::allocate_location ()
11624{
11625 return new bp_location (this);
11626}
11627
2060206e
PA
11628#define internal_error_pure_virtual_called() \
11629 gdb_assert_not_reached ("pure virtual function called")
11630
4c6a92b1
TT
11631int
11632breakpoint::insert_location (struct bp_location *bl)
11633{
11634 internal_error_pure_virtual_called ();
11635}
11636
4c6a92b1
TT
11637int
11638breakpoint::remove_location (struct bp_location *bl,
11639 enum remove_bp_reason reason)
11640{
11641 internal_error_pure_virtual_called ();
11642}
11643
4c6a92b1
TT
11644int
11645breakpoint::breakpoint_hit (const struct bp_location *bl,
11646 const address_space *aspace,
11647 CORE_ADDR bp_addr,
11648 const target_waitstatus &ws)
11649{
11650 internal_error_pure_virtual_called ();
11651}
11652
4c6a92b1
TT
11653int
11654breakpoint::resources_needed (const struct bp_location *bl)
11655{
11656 internal_error_pure_virtual_called ();
11657}
11658
4c6a92b1 11659enum print_stop_action
7bd86313 11660breakpoint::print_it (const bpstat *bs) const
4c6a92b1
TT
11661{
11662 internal_error_pure_virtual_called ();
11663}
11664
4c6a92b1 11665void
b713485d 11666breakpoint::print_mention () const
4c6a92b1
TT
11667{
11668 internal_error_pure_virtual_called ();
11669}
11670
4c6a92b1 11671void
4d1ae558 11672breakpoint::print_recreate (struct ui_file *fp) const
4c6a92b1
TT
11673{
11674 internal_error_pure_virtual_called ();
11675}
11676
2060206e
PA
11677/* Default breakpoint_ops methods. */
11678
2b5ab5b8 11679void
74421c0b 11680code_breakpoint::re_set ()
348d480f 11681{
06edf0c0 11682 /* FIXME: is this still reachable? */
264f9890 11683 if (breakpoint_location_spec_empty_p (this))
06edf0c0 11684 {
f00aae0f 11685 /* Anything without a location can't be re-set. */
2b5ab5b8 11686 delete_breakpoint (this);
06edf0c0 11687 return;
348d480f 11688 }
06edf0c0 11689
b3d5660a 11690 re_set_default ();
348d480f
PA
11691}
11692
2b5ab5b8 11693int
74421c0b 11694code_breakpoint::insert_location (struct bp_location *bl)
348d480f 11695{
cd6c3b4f
YQ
11696 CORE_ADDR addr = bl->target_info.reqstd_address;
11697
579c6ad9 11698 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
11699 bl->target_info.placed_address = addr;
11700
5d926615 11701 int result;
348d480f 11702 if (bl->loc_type == bp_loc_hardware_breakpoint)
5d926615 11703 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 11704 else
5d926615
TT
11705 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11706
11707 if (result == 0 && bl->probe.prob != nullptr)
11708 {
11709 /* The insertion was successful, now let's set the probe's semaphore
11710 if needed. */
11711 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11712 }
11713
11714 return result;
348d480f
PA
11715}
11716
2b5ab5b8 11717int
74421c0b 11718code_breakpoint::remove_location (struct bp_location *bl,
2b5ab5b8 11719 enum remove_bp_reason reason)
348d480f 11720{
5d926615
TT
11721 if (bl->probe.prob != nullptr)
11722 {
11723 /* Let's clear the semaphore before removing the location. */
11724 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11725 }
11726
348d480f
PA
11727 if (bl->loc_type == bp_loc_hardware_breakpoint)
11728 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11729 else
73971819 11730 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
11731}
11732
2b5ab5b8 11733int
74421c0b 11734code_breakpoint::breakpoint_hit (const struct bp_location *bl,
2b5ab5b8
TT
11735 const address_space *aspace,
11736 CORE_ADDR bp_addr,
11737 const target_waitstatus &ws)
348d480f 11738{
c272a98c
SM
11739 if (ws.kind () != TARGET_WAITKIND_STOPPED
11740 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
11741 return 0;
11742
348d480f
PA
11743 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11744 aspace, bp_addr))
11745 return 0;
11746
11747 if (overlay_debugging /* unmapped overlay section */
11748 && section_is_overlay (bl->section)
11749 && !section_is_mapped (bl->section))
11750 return 0;
11751
11752 return 1;
11753}
11754
f293a0b5
TT
11755int
11756dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11757 const address_space *aspace,
11758 CORE_ADDR bp_addr,
11759 const target_waitstatus &ws)
cd1608cc
PA
11760{
11761 if (dprintf_style == dprintf_style_agent
11762 && target_can_run_breakpoint_commands ())
11763 {
11764 /* An agent-style dprintf never causes a stop. If we see a trap
11765 for this address it must be for a breakpoint that happens to
11766 be set at the same address. */
11767 return 0;
11768 }
11769
f293a0b5 11770 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
cd1608cc
PA
11771}
11772
7dd8e7ae
TT
11773int
11774ordinary_breakpoint::resources_needed (const struct bp_location *bl)
348d480f 11775{
7dd8e7ae 11776 gdb_assert (type == bp_hardware_breakpoint);
348d480f
PA
11777
11778 return 1;
11779}
11780
7dd8e7ae 11781enum print_stop_action
7bd86313 11782ordinary_breakpoint::print_it (const bpstat *bs) const
348d480f 11783{
348d480f 11784 const struct bp_location *bl;
001c8c33 11785 int bp_temp;
79a45e25 11786 struct ui_out *uiout = current_uiout;
348d480f 11787
b6433ede 11788 bl = bs->bp_location_at.get ();
348d480f 11789
7dd8e7ae 11790 bp_temp = disposition == disp_del;
001c8c33
PA
11791 if (bl->address != bl->requested_address)
11792 breakpoint_adjustment_warning (bl->requested_address,
11793 bl->address,
d8de7963 11794 number, true);
7dd8e7ae 11795 annotate_breakpoint (number);
f303dbd6
PA
11796 maybe_print_thread_hit_breakpoint (uiout);
11797
112e8700 11798 if (uiout->is_mi_like_p ())
348d480f 11799 {
112e8700 11800 uiout->field_string ("reason",
001c8c33 11801 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7dd8e7ae 11802 uiout->field_string ("disp", bpdisp_text (disposition));
06edf0c0 11803 }
78805ff8 11804
6a831f06 11805 if (bp_temp)
78805ff8 11806 uiout->text ("Temporary breakpoint ");
6a831f06 11807 else
78805ff8
PW
11808 uiout->text ("Breakpoint ");
11809 print_num_locno (bs, uiout);
11810 uiout->text (", ");
06edf0c0 11811
001c8c33 11812 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11813}
11814
7dd8e7ae 11815void
b713485d 11816ordinary_breakpoint::print_mention () const
06edf0c0 11817{
112e8700 11818 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
11819 return;
11820
7dd8e7ae 11821 switch (type)
06edf0c0
PA
11822 {
11823 case bp_breakpoint:
11824 case bp_gnu_ifunc_resolver:
7dd8e7ae 11825 if (disposition == disp_del)
6cb06a8c 11826 gdb_printf (_("Temporary breakpoint"));
06edf0c0 11827 else
6cb06a8c 11828 gdb_printf (_("Breakpoint"));
7dd8e7ae
TT
11829 gdb_printf (_(" %d"), number);
11830 if (type == bp_gnu_ifunc_resolver)
6cb06a8c 11831 gdb_printf (_(" at gnu-indirect-function resolver"));
06edf0c0
PA
11832 break;
11833 case bp_hardware_breakpoint:
7dd8e7ae 11834 gdb_printf (_("Hardware assisted breakpoint %d"), number);
06edf0c0 11835 break;
e7e0cddf 11836 case bp_dprintf:
7dd8e7ae 11837 gdb_printf (_("Dprintf %d"), number);
e7e0cddf 11838 break;
06edf0c0
PA
11839 }
11840
7987c463 11841 say_where ();
06edf0c0
PA
11842}
11843
7dd8e7ae 11844void
4d1ae558 11845ordinary_breakpoint::print_recreate (struct ui_file *fp) const
06edf0c0 11846{
7dd8e7ae 11847 if (type == bp_breakpoint && disposition == disp_del)
6cb06a8c 11848 gdb_printf (fp, "tbreak");
7dd8e7ae 11849 else if (type == bp_breakpoint)
6cb06a8c 11850 gdb_printf (fp, "break");
7dd8e7ae
TT
11851 else if (type == bp_hardware_breakpoint
11852 && disposition == disp_del)
6cb06a8c 11853 gdb_printf (fp, "thbreak");
7dd8e7ae 11854 else if (type == bp_hardware_breakpoint)
6cb06a8c 11855 gdb_printf (fp, "hbreak");
06edf0c0 11856 else
f34652de 11857 internal_error (_("unhandled breakpoint type %d"), (int) type);
06edf0c0 11858
709438c7 11859 gdb_printf (fp, " %s", locspec->to_string ());
f00aae0f
KS
11860
11861 /* Print out extra_string if this breakpoint is pending. It might
11862 contain, for example, conditions that were set by the user. */
7dd8e7ae
TT
11863 if (loc == NULL && extra_string != NULL)
11864 gdb_printf (fp, " %s", extra_string.get ());
f00aae0f 11865
04d0163c 11866 print_recreate_thread (fp);
06edf0c0
PA
11867}
11868
2b5ab5b8 11869std::vector<symtab_and_line>
264f9890
PA
11870code_breakpoint::decode_location_spec (location_spec *locspec,
11871 program_space *search_pspace)
983af33b 11872{
7464aeaa 11873 if (locspec->type () == PROBE_LOCATION_SPEC)
264f9890 11874 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
5d926615 11875
8f5bc641
TT
11876 struct linespec_result canonical;
11877
11878 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
11879 NULL, 0, &canonical, multiple_symbols_all,
11880 filter.get ());
11881
11882 /* We should get 0 or 1 resulting SALs. */
11883 gdb_assert (canonical.lsals.size () < 2);
11884
11885 if (!canonical.lsals.empty ())
11886 {
11887 const linespec_sals &lsal = canonical.lsals[0];
11888 return std::move (lsal.sals);
11889 }
11890 return {};
983af33b
SDJ
11891}
11892
06edf0c0
PA
11893/* Virtual table for internal breakpoints. */
11894
c359fff5
TT
11895void
11896internal_breakpoint::re_set ()
06edf0c0 11897{
c359fff5 11898 switch (type)
06edf0c0
PA
11899 {
11900 /* Delete overlay event and longjmp master breakpoints; they
11901 will be reset later by breakpoint_re_set. */
11902 case bp_overlay_event:
11903 case bp_longjmp_master:
11904 case bp_std_terminate_master:
11905 case bp_exception_master:
c359fff5 11906 delete_breakpoint (this);
06edf0c0
PA
11907 break;
11908
11909 /* This breakpoint is special, it's set up when the inferior
dda83cd7 11910 starts and we really don't want to touch it. */
06edf0c0
PA
11911 case bp_shlib_event:
11912
11913 /* Like bp_shlib_event, this breakpoint type is special. Once
11914 it is set up, we do not want to touch it. */
11915 case bp_thread_event:
11916 break;
11917 }
11918}
11919
c359fff5
TT
11920void
11921internal_breakpoint::check_status (bpstat *bs)
06edf0c0 11922{
c359fff5 11923 if (type == bp_shlib_event)
a9b3a50f
PA
11924 {
11925 /* If requested, stop when the dynamic linker notifies GDB of
11926 events. This allows the user to get control and place
11927 breakpoints in initializer routines for dynamically loaded
11928 objects (among other things). */
4ec2227a
TT
11929 bs->stop = stop_on_solib_events != 0;
11930 bs->print = stop_on_solib_events != 0;
a9b3a50f
PA
11931 }
11932 else
4ec2227a 11933 bs->stop = false;
06edf0c0
PA
11934}
11935
c359fff5 11936enum print_stop_action
7bd86313 11937internal_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11938{
c359fff5 11939 switch (type)
06edf0c0 11940 {
348d480f
PA
11941 case bp_shlib_event:
11942 /* Did we stop because the user set the stop_on_solib_events
11943 variable? (If so, we report this as a generic, "Stopped due
11944 to shlib event" message.) */
4e9e993a 11945 print_solib_event (false);
348d480f
PA
11946 break;
11947
11948 case bp_thread_event:
11949 /* Not sure how we will get here.
11950 GDB should not stop for these breakpoints. */
6cb06a8c 11951 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11952 break;
11953
11954 case bp_overlay_event:
11955 /* By analogy with the thread event, GDB should not stop for these. */
6cb06a8c 11956 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11957 break;
11958
11959 case bp_longjmp_master:
11960 /* These should never be enabled. */
6cb06a8c 11961 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11962 break;
11963
11964 case bp_std_terminate_master:
11965 /* These should never be enabled. */
6cb06a8c
TT
11966 gdb_printf (_("std::terminate Master Breakpoint: "
11967 "gdb should not stop!\n"));
348d480f
PA
11968 break;
11969
11970 case bp_exception_master:
11971 /* These should never be enabled. */
6cb06a8c
TT
11972 gdb_printf (_("Exception Master Breakpoint: "
11973 "gdb should not stop!\n"));
06edf0c0
PA
11974 break;
11975 }
11976
001c8c33 11977 return PRINT_NOTHING;
06edf0c0
PA
11978}
11979
c359fff5 11980void
b713485d 11981internal_breakpoint::print_mention () const
06edf0c0
PA
11982{
11983 /* Nothing to mention. These breakpoints are internal. */
11984}
11985
06edf0c0
PA
11986/* Virtual table for momentary breakpoints */
11987
1fd30a47
TT
11988void
11989momentary_breakpoint::re_set ()
06edf0c0
PA
11990{
11991 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 11992 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
11993 Otherwise these should have been blown away via the cleanup chain
11994 or by breakpoint_init_inferior when we rerun the executable. */
11995}
11996
1fd30a47
TT
11997void
11998momentary_breakpoint::check_status (bpstat *bs)
06edf0c0
PA
11999{
12000 /* Nothing. The point of these breakpoints is causing a stop. */
12001}
12002
1fd30a47 12003enum print_stop_action
7bd86313 12004momentary_breakpoint::print_it (const bpstat *bs) const
06edf0c0 12005{
001c8c33 12006 return PRINT_UNKNOWN;
348d480f
PA
12007}
12008
1fd30a47 12009void
b713485d 12010momentary_breakpoint::print_mention () const
348d480f 12011{
06edf0c0 12012 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12013}
12014
e2e4d78b
JK
12015/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12016
12017 It gets cleared already on the removal of the first one of such placed
12018 breakpoints. This is OK as they get all removed altogether. */
12019
c1fc2657 12020longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12021{
c1fc2657 12022 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12023
c1fc2657 12024 if (tp != NULL)
e2e4d78b 12025 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12026}
12027
55aa24fb 12028static void
264f9890
PA
12029bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12030 struct linespec_result *canonical)
03ada39e 12031
55aa24fb
SDJ
12032{
12033 struct linespec_sals lsal;
12034
264f9890 12035 lsal.sals = parse_probes (locspec, NULL, canonical);
709438c7 12036 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12037 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12038}
12039
6c5b2ebe 12040static std::vector<symtab_and_line>
264f9890
PA
12041bkpt_probe_decode_location_spec (struct breakpoint *b,
12042 location_spec *locspec,
12043 program_space *search_pspace)
55aa24fb 12044{
264f9890
PA
12045 std::vector<symtab_and_line> sals
12046 = parse_probes (locspec, search_pspace, NULL);
6c5b2ebe 12047 if (sals.empty ())
55aa24fb 12048 error (_("probe not found"));
6c5b2ebe 12049 return sals;
55aa24fb
SDJ
12050}
12051
779dcceb
TT
12052int
12053tracepoint::breakpoint_hit (const struct bp_location *bl,
12054 const address_space *aspace, CORE_ADDR bp_addr,
12055 const target_waitstatus &ws)
348d480f
PA
12056{
12057 /* By definition, the inferior does not report stops at
12058 tracepoints. */
12059 return 0;
74960c60
VP
12060}
12061
779dcceb
TT
12062void
12063tracepoint::print_one_detail (struct ui_out *uiout) const
74960c60 12064{
779dcceb 12065 if (!static_trace_marker_id.empty ())
348d480f 12066 {
7b572efb
TT
12067 gdb_assert (type == bp_static_tracepoint
12068 || type == bp_static_marker_tracepoint);
cc59ec59 12069
6a831f06
PA
12070 uiout->message ("\tmarker id is %pF\n",
12071 string_field ("static-tracepoint-marker-string-id",
779dcceb 12072 static_trace_marker_id.c_str ()));
348d480f 12073 }
0d381245
VP
12074}
12075
779dcceb 12076void
b713485d 12077tracepoint::print_mention () const
a474d7c2 12078{
112e8700 12079 if (current_uiout->is_mi_like_p ())
348d480f 12080 return;
cc59ec59 12081
779dcceb 12082 switch (type)
348d480f
PA
12083 {
12084 case bp_tracepoint:
6cb06a8c 12085 gdb_printf (_("Tracepoint"));
779dcceb 12086 gdb_printf (_(" %d"), number);
348d480f
PA
12087 break;
12088 case bp_fast_tracepoint:
6cb06a8c 12089 gdb_printf (_("Fast tracepoint"));
779dcceb 12090 gdb_printf (_(" %d"), number);
348d480f
PA
12091 break;
12092 case bp_static_tracepoint:
7b572efb 12093 case bp_static_marker_tracepoint:
6cb06a8c 12094 gdb_printf (_("Static tracepoint"));
779dcceb 12095 gdb_printf (_(" %d"), number);
348d480f
PA
12096 break;
12097 default:
f34652de 12098 internal_error (_("unhandled tracepoint type %d"), (int) type);
348d480f
PA
12099 }
12100
7987c463 12101 say_where ();
a474d7c2
PA
12102}
12103
779dcceb 12104void
4d1ae558 12105tracepoint::print_recreate (struct ui_file *fp) const
a474d7c2 12106{
779dcceb 12107 if (type == bp_fast_tracepoint)
6cb06a8c 12108 gdb_printf (fp, "ftrace");
7b572efb
TT
12109 else if (type == bp_static_tracepoint
12110 || type == bp_static_marker_tracepoint)
6cb06a8c 12111 gdb_printf (fp, "strace");
779dcceb 12112 else if (type == bp_tracepoint)
6cb06a8c 12113 gdb_printf (fp, "trace");
348d480f 12114 else
f34652de 12115 internal_error (_("unhandled tracepoint type %d"), (int) type);
cc59ec59 12116
709438c7 12117 gdb_printf (fp, " %s", locspec->to_string ());
04d0163c 12118 print_recreate_thread (fp);
d9b3f62e 12119
779dcceb
TT
12120 if (pass_count)
12121 gdb_printf (fp, " passcount %d\n", pass_count);
a474d7c2
PA
12122}
12123
bac7c5cf 12124/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12125
12126static void
264f9890
PA
12127tracepoint_probe_create_sals_from_location_spec
12128 (location_spec *locspec,
03ada39e 12129 struct linespec_result *canonical)
55aa24fb
SDJ
12130{
12131 /* We use the same method for breakpoint on probes. */
264f9890 12132 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
55aa24fb
SDJ
12133}
12134
f293a0b5
TT
12135void
12136dprintf_breakpoint::re_set ()
5c2b4418 12137{
b3d5660a 12138 re_set_default ();
5c2b4418 12139
f00aae0f 12140 /* extra_string should never be non-NULL for dprintf. */
f293a0b5 12141 gdb_assert (extra_string != NULL);
5c2b4418
HZ
12142
12143 /* 1 - connect to target 1, that can run breakpoint commands.
12144 2 - create a dprintf, which resolves fine.
12145 3 - disconnect from target 1
12146 4 - connect to target 2, that can NOT run breakpoint commands.
12147
12148 After steps #3/#4, you'll want the dprintf command list to
12149 be updated, because target 1 and 2 may well return different
12150 answers for target_can_run_breakpoint_commands().
12151 Given absence of finer grained resetting, we get to do
12152 it all the time. */
f293a0b5
TT
12153 if (extra_string != NULL)
12154 update_dprintf_command_list (this);
5c2b4418
HZ
12155}
12156
f293a0b5 12157/* Implement the "print_recreate" method for dprintf. */
2d9442cc 12158
f293a0b5 12159void
4d1ae558 12160dprintf_breakpoint::print_recreate (struct ui_file *fp) const
2d9442cc 12161{
709438c7 12162 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
04d0163c 12163 print_recreate_thread (fp);
2d9442cc
HZ
12164}
12165
f293a0b5 12166/* Implement the "after_condition_true" method for dprintf.
9d6e6e84
HZ
12167
12168 dprintf's are implemented with regular commands in their command
12169 list, but we run the commands here instead of before presenting the
12170 stop to the user, as dprintf's don't actually cause a stop. This
12171 also makes it so that the commands of multiple dprintfs at the same
12172 address are all handled. */
12173
f293a0b5
TT
12174void
12175dprintf_breakpoint::after_condition_true (struct bpstat *bs)
9d6e6e84 12176{
9d6e6e84
HZ
12177 /* dprintf's never cause a stop. This wasn't set in the
12178 check_status hook instead because that would make the dprintf's
12179 condition not be evaluated. */
4ec2227a 12180 bs->stop = false;
9d6e6e84
HZ
12181
12182 /* Run the command list here. Take ownership of it instead of
12183 copying. We never want these commands to run later in
12184 bpstat_do_actions, if a breakpoint that causes a stop happens to
12185 be set at same address as this dprintf, or even if running the
12186 commands here throws. */
9c95aea1
KB
12187 counted_command_line cmds = std::move (bs->commands);
12188 gdb_assert (cmds != nullptr);
12189 execute_control_commands (cmds.get (), 0);
9d6e6e84
HZ
12190}
12191
983af33b
SDJ
12192/* The breakpoint_ops structure to be used on static tracepoints with
12193 markers (`-m'). */
12194
12195static void
264f9890
PA
12196strace_marker_create_sals_from_location_spec (location_spec *locspec,
12197 struct linespec_result *canonical)
983af33b
SDJ
12198{
12199 struct linespec_sals lsal;
f00aae0f 12200 const char *arg_start, *arg;
983af33b 12201
40d97ee2 12202 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 12203 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12204
f2fc3015
TT
12205 std::string str (arg_start, arg - arg_start);
12206 const char *ptr = str.c_str ();
264f9890
PA
12207 canonical->locspec
12208 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
983af33b 12209
709438c7 12210 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12211 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12212}
12213
12214static void
12215strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12216 struct linespec_result *canonical,
e1e01040
PA
12217 gdb::unique_xmalloc_ptr<char> cond_string,
12218 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12219 enum bptype type_wanted,
12220 enum bpdisp disposition,
12221 int thread,
12222 int task, int ignore_count,
983af33b 12223 int from_tty, int enabled,
44f238bb 12224 int internal, unsigned flags)
983af33b 12225{
6c5b2ebe 12226 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12227
12228 /* If the user is creating a static tracepoint by marker id
12229 (strace -m MARKER_ID), then store the sals index, so that
12230 breakpoint_re_set can try to match up which of the newly
12231 found markers corresponds to this one, and, don't try to
12232 expand multiple locations for each sal, given than SALS
12233 already should contain all sals for MARKER_ID. */
12234
6c5b2ebe 12235 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12236 {
5c1ddcb6 12237 location_spec_up locspec = canonical->locspec->clone ();
983af33b 12238
3b003a61
PA
12239 std::unique_ptr<tracepoint> tp
12240 (new tracepoint (gdbarch,
12241 type_wanted,
12242 lsal.sals[i],
264f9890 12243 std::move (locspec),
3b003a61
PA
12244 NULL,
12245 std::move (cond_string),
12246 std::move (extra_string),
12247 disposition,
12248 thread, task, ignore_count,
12249 from_tty, enabled, flags,
12250 canonical->special_display));
12251
983af33b
SDJ
12252 /* Given that its possible to have multiple markers with
12253 the same string id, if the user is creating a static
12254 tracepoint by marker id ("strace -m MARKER_ID"), then
12255 store the sals index, so that breakpoint_re_set can
12256 try to match up which of the newly found markers
12257 corresponds to this one */
12258 tp->static_trace_marker_id_idx = i;
12259
b270e6f9 12260 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12261 }
12262}
12263
6d7a8c56 12264std::vector<symtab_and_line>
264f9890
PA
12265static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12266 program_space *search_pspace)
983af33b 12267{
40d97ee2 12268 const char *s = as_linespec_location_spec (locspec)->spec_string;
983af33b 12269
6c5b2ebe 12270 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
6d7a8c56 12271 if (sals.size () > static_trace_marker_id_idx)
983af33b 12272 {
6d7a8c56 12273 sals[0] = sals[static_trace_marker_id_idx];
6c5b2ebe
PA
12274 sals.resize (1);
12275 return sals;
983af33b
SDJ
12276 }
12277 else
6d7a8c56 12278 error (_("marker %s not found"), static_trace_marker_id.c_str ());
983af33b
SDJ
12279}
12280
8613a476
TT
12281/* Static tracepoints with marker (`-m'). */
12282static struct breakpoint_ops strace_marker_breakpoint_ops =
12283{
264f9890 12284 strace_marker_create_sals_from_location_spec,
8613a476
TT
12285 strace_marker_create_breakpoints_sal,
12286};
983af33b 12287
d8de7963 12288static bool
983af33b
SDJ
12289strace_marker_p (struct breakpoint *b)
12290{
7b572efb 12291 return b->type == bp_static_marker_tracepoint;
983af33b
SDJ
12292}
12293
53a5351d 12294/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 12295 structures. */
c906108c
SS
12296
12297void
fba45db2 12298delete_breakpoint (struct breakpoint *bpt)
c906108c 12299{
8a3fe4f8 12300 gdb_assert (bpt != NULL);
c906108c 12301
4a64f543
MS
12302 /* Has this bp already been deleted? This can happen because
12303 multiple lists can hold pointers to bp's. bpstat lists are
12304 especial culprits.
12305
12306 One example of this happening is a watchpoint's scope bp. When
12307 the scope bp triggers, we notice that the watchpoint is out of
12308 scope, and delete it. We also delete its scope bp. But the
12309 scope bp is marked "auto-deleting", and is already on a bpstat.
12310 That bpstat is then checked for auto-deleting bp's, which are
12311 deleted.
12312
12313 A real solution to this problem might involve reference counts in
12314 bp's, and/or giving them pointers back to their referencing
12315 bpstat's, and teaching delete_breakpoint to only free a bp's
12316 storage when no more references were extent. A cheaper bandaid
12317 was chosen. */
c906108c
SS
12318 if (bpt->type == bp_none)
12319 return;
12320
4a64f543
MS
12321 /* At least avoid this stale reference until the reference counting
12322 of breakpoints gets resolved. */
d0fb5eae 12323 if (bpt->related_breakpoint != bpt)
e5a0a904 12324 {
d0fb5eae 12325 struct breakpoint *related;
3a5c3e22 12326 struct watchpoint *w;
d0fb5eae
JK
12327
12328 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 12329 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 12330 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
12331 w = (struct watchpoint *) bpt;
12332 else
12333 w = NULL;
12334 if (w != NULL)
12335 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
12336
12337 /* Unlink bpt from the bpt->related_breakpoint ring. */
12338 for (related = bpt; related->related_breakpoint != bpt;
12339 related = related->related_breakpoint);
12340 related->related_breakpoint = bpt->related_breakpoint;
12341 bpt->related_breakpoint = bpt;
e5a0a904
JK
12342 }
12343
a9634178
TJB
12344 /* watch_command_1 creates a watchpoint but only sets its number if
12345 update_watchpoint succeeds in creating its bp_locations. If there's
12346 a problem in that process, we'll be asked to delete the half-created
12347 watchpoint. In that case, don't announce the deletion. */
12348 if (bpt->number)
76727919 12349 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 12350
c906108c
SS
12351 if (breakpoint_chain == bpt)
12352 breakpoint_chain = bpt->next;
12353
43892fdf 12354 for (breakpoint *b : all_breakpoints ())
c906108c 12355 if (b->next == bpt)
01add95b
SM
12356 {
12357 b->next = bpt->next;
12358 break;
12359 }
c906108c 12360
f431efe5
PA
12361 /* Be sure no bpstat's are pointing at the breakpoint after it's
12362 been freed. */
12363 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 12364 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
12365 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12366 commands are associated with the bpstat; if we remove it here,
12367 then the later call to bpstat_do_actions (&stop_bpstat); in
12368 event-top.c won't do anything, and temporary breakpoints with
12369 commands won't work. */
12370
12371 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12372
4a64f543
MS
12373 /* Now that breakpoint is removed from breakpoint list, update the
12374 global location list. This will remove locations that used to
12375 belong to this breakpoint. Do this before freeing the breakpoint
12376 itself, since remove_breakpoint looks at location's owner. It
12377 might be better design to have location completely
12378 self-contained, but it's not the case now. */
44702360 12379 update_global_location_list (UGLL_DONT_INSERT);
74960c60 12380
4a64f543
MS
12381 /* On the chance that someone will soon try again to delete this
12382 same bp, we mark it as deleted before freeing its storage. */
c906108c 12383 bpt->type = bp_none;
4d01a485 12384 delete bpt;
c906108c
SS
12385}
12386
51be5b68
PA
12387/* Iterator function to call a user-provided callback function once
12388 for each of B and its related breakpoints. */
12389
12390static void
12391iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 12392 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
12393{
12394 struct breakpoint *related;
12395
12396 related = b;
12397 do
12398 {
12399 struct breakpoint *next;
12400
12401 /* FUNCTION may delete RELATED. */
12402 next = related->related_breakpoint;
12403
12404 if (next == related)
12405 {
12406 /* RELATED is the last ring entry. */
48649e1b 12407 function (related);
51be5b68
PA
12408
12409 /* FUNCTION may have deleted it, so we'd never reach back to
12410 B. There's nothing left to do anyway, so just break
12411 out. */
12412 break;
12413 }
12414 else
48649e1b 12415 function (related);
51be5b68
PA
12416
12417 related = next;
12418 }
12419 while (related != b);
12420}
95a42b64 12421
4495129a 12422static void
981a3fb3 12423delete_command (const char *arg, int from_tty)
c906108c 12424{
ea9365bb
TT
12425 dont_repeat ();
12426
c906108c
SS
12427 if (arg == 0)
12428 {
12429 int breaks_to_delete = 0;
12430
46c6471b 12431 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
12432 internal breakpoints, these have to be deleted with an
12433 explicit breakpoint number argument. */
43892fdf 12434 for (breakpoint *b : all_breakpoints ())
46c6471b 12435 if (user_breakpoint_p (b))
973d738b
DJ
12436 {
12437 breaks_to_delete = 1;
12438 break;
12439 }
c906108c
SS
12440
12441 /* Ask user only if there are some breakpoints to delete. */
12442 if (!from_tty
e2e0b3e5 12443 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
12444 for (breakpoint *b : all_breakpoints_safe ())
12445 if (user_breakpoint_p (b))
12446 delete_breakpoint (b);
c906108c
SS
12447 }
12448 else
48649e1b 12449 map_breakpoint_numbers
b926417a 12450 (arg, [&] (breakpoint *br)
48649e1b 12451 {
b926417a 12452 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 12453 });
c906108c
SS
12454}
12455
c2f4122d
PA
12456/* Return true if all locations of B bound to PSPACE are pending. If
12457 PSPACE is NULL, all locations of all program spaces are
12458 considered. */
12459
d8de7963 12460static bool
c2f4122d 12461all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 12462{
40cb8ca5 12463 for (bp_location *loc : b->locations ())
c2f4122d
PA
12464 if ((pspace == NULL
12465 || loc->pspace == pspace)
12466 && !loc->shlib_disabled
8645ff69 12467 && !loc->pspace->executing_startup)
d8de7963
AB
12468 return false;
12469 return true;
fe3f5fa8
VP
12470}
12471
776592bf 12472/* Subroutine of update_breakpoint_locations to simplify it.
d8de7963 12473 Return true if multiple fns in list LOC have the same name.
776592bf
DE
12474 Null names are ignored. */
12475
d8de7963 12476static bool
776592bf
DE
12477ambiguous_names_p (struct bp_location *loc)
12478{
12479 struct bp_location *l;
2698f5ea 12480 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 12481 xcalloc, xfree));
776592bf
DE
12482
12483 for (l = loc; l != NULL; l = l->next)
12484 {
12485 const char **slot;
23d6ee64 12486 const char *name = l->function_name.get ();
776592bf
DE
12487
12488 /* Allow for some names to be NULL, ignore them. */
12489 if (name == NULL)
12490 continue;
12491
c1fb9836 12492 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 12493 INSERT);
4a64f543
MS
12494 /* NOTE: We can assume slot != NULL here because xcalloc never
12495 returns NULL. */
776592bf 12496 if (*slot != NULL)
d8de7963 12497 return true;
776592bf
DE
12498 *slot = name;
12499 }
12500
d8de7963 12501 return false;
776592bf
DE
12502}
12503
0fb4aa4b
PA
12504/* When symbols change, it probably means the sources changed as well,
12505 and it might mean the static tracepoint markers are no longer at
12506 the same address or line numbers they used to be at last we
12507 checked. Losing your static tracepoints whenever you rebuild is
12508 undesirable. This function tries to resync/rematch gdb static
12509 tracepoints with the markers on the target, for static tracepoints
12510 that have not been set by marker id. Static tracepoint that have
12511 been set by marker id are reset by marker id in breakpoint_re_set.
12512 The heuristic is:
12513
12514 1) For a tracepoint set at a specific address, look for a marker at
12515 the old PC. If one is found there, assume to be the same marker.
12516 If the name / string id of the marker found is different from the
12517 previous known name, assume that means the user renamed the marker
12518 in the sources, and output a warning.
12519
12520 2) For a tracepoint set at a given line number, look for a marker
12521 at the new address of the old line number. If one is found there,
12522 assume to be the same marker. If the name / string id of the
12523 marker found is different from the previous known name, assume that
12524 means the user renamed the marker in the sources, and output a
12525 warning.
12526
12527 3) If a marker is no longer found at the same address or line, it
12528 may mean the marker no longer exists. But it may also just mean
12529 the code changed a bit. Maybe the user added a few lines of code
12530 that made the marker move up or down (in line number terms). Ask
12531 the target for info about the marker with the string id as we knew
12532 it. If found, update line number and address in the matching
12533 static tracepoint. This will get confused if there's more than one
12534 marker with the same ID (possible in UST, although unadvised
12535 precisely because it confuses tools). */
12536
12537static struct symtab_and_line
12538update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12539{
d9b3f62e 12540 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
12541 struct static_tracepoint_marker marker;
12542 CORE_ADDR pc;
0fb4aa4b
PA
12543
12544 pc = sal.pc;
12545 if (sal.line)
12546 find_line_pc (sal.symtab, sal.line, &pc);
12547
12548 if (target_static_tracepoint_marker_at (pc, &marker))
12549 {
5d9310c4 12550 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 12551 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
12552 b->number, tp->static_trace_marker_id.c_str (),
12553 marker.str_id.c_str ());
0fb4aa4b 12554
5d9310c4 12555 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
12556
12557 return sal;
12558 }
12559
12560 /* Old marker wasn't found on target at lineno. Try looking it up
12561 by string ID. */
12562 if (!sal.explicit_pc
12563 && sal.line != 0
12564 && sal.symtab != NULL
5d9310c4 12565 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 12566 {
5d9310c4
SM
12567 std::vector<static_tracepoint_marker> markers
12568 = target_static_tracepoint_markers_by_strid
12569 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 12570
5d9310c4 12571 if (!markers.empty ())
0fb4aa4b 12572 {
0fb4aa4b 12573 struct symbol *sym;
80e1d417 12574 struct static_tracepoint_marker *tpmarker;
79a45e25 12575 struct ui_out *uiout = current_uiout;
0fb4aa4b 12576
5d9310c4 12577 tpmarker = &markers[0];
0fb4aa4b 12578
5d9310c4 12579 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
12580
12581 warning (_("marker for static tracepoint %d (%s) not "
12582 "found at previous line number"),
5d9310c4 12583 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 12584
51abb421 12585 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 12586 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 12587 uiout->text ("Now in ");
0fb4aa4b
PA
12588 if (sym)
12589 {
987012b8 12590 uiout->field_string ("func", sym->print_name (),
e43b10e1 12591 function_name_style.style ());
112e8700 12592 uiout->text (" at ");
0fb4aa4b 12593 }
112e8700 12594 uiout->field_string ("file",
cbe56571 12595 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 12596 file_name_style.style ());
112e8700 12597 uiout->text (":");
0fb4aa4b 12598
112e8700 12599 if (uiout->is_mi_like_p ())
0fb4aa4b 12600 {
0b0865da 12601 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 12602
112e8700 12603 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
12604 }
12605
381befee 12606 uiout->field_signed ("line", sal2.line);
112e8700 12607 uiout->text ("\n");
0fb4aa4b 12608
80e1d417 12609 b->loc->line_number = sal2.line;
2f202fde 12610 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 12611
40d97ee2
PA
12612 std::unique_ptr<explicit_location_spec> els
12613 (new explicit_location_spec ());
12614 els->source_filename
12615 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12616 els->line_offset.offset = b->loc->line_number;
12617 els->line_offset.sign = LINE_OFFSET_NONE;
12618
12619 b->locspec = std::move (els);
0fb4aa4b
PA
12620
12621 /* Might be nice to check if function changed, and warn if
12622 so. */
0fb4aa4b
PA
12623 }
12624 }
12625 return sal;
12626}
12627
d8de7963 12628/* Returns true iff locations A and B are sufficiently same that
8d3788bd
VP
12629 we don't need to report breakpoint as changed. */
12630
d8de7963 12631static bool
8d3788bd
VP
12632locations_are_equal (struct bp_location *a, struct bp_location *b)
12633{
12634 while (a && b)
12635 {
12636 if (a->address != b->address)
d8de7963 12637 return false;
8d3788bd
VP
12638
12639 if (a->shlib_disabled != b->shlib_disabled)
d8de7963 12640 return false;
8d3788bd
VP
12641
12642 if (a->enabled != b->enabled)
d8de7963 12643 return false;
8d3788bd 12644
b5fa468f 12645 if (a->disabled_by_cond != b->disabled_by_cond)
d8de7963 12646 return false;
b5fa468f 12647
8d3788bd
VP
12648 a = a->next;
12649 b = b->next;
12650 }
12651
12652 if ((a == NULL) != (b == NULL))
d8de7963 12653 return false;
8d3788bd 12654
d8de7963 12655 return true;
8d3788bd
VP
12656}
12657
c2f4122d
PA
12658/* Split all locations of B that are bound to PSPACE out of B's
12659 location list to a separate list and return that list's head. If
12660 PSPACE is NULL, hoist out all locations of B. */
12661
12662static struct bp_location *
12663hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12664{
12665 struct bp_location head;
12666 struct bp_location *i = b->loc;
12667 struct bp_location **i_link = &b->loc;
12668 struct bp_location *hoisted = &head;
12669
12670 if (pspace == NULL)
12671 {
12672 i = b->loc;
12673 b->loc = NULL;
12674 return i;
12675 }
12676
12677 head.next = NULL;
12678
12679 while (i != NULL)
12680 {
12681 if (i->pspace == pspace)
12682 {
12683 *i_link = i->next;
12684 i->next = NULL;
12685 hoisted->next = i;
12686 hoisted = i;
12687 }
12688 else
12689 i_link = &i->next;
12690 i = *i_link;
12691 }
12692
12693 return head.next;
12694}
12695
12696/* Create new breakpoint locations for B (a hardware or software
12697 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12698 zero, then B is a ranged breakpoint. Only recreates locations for
12699 FILTER_PSPACE. Locations of other program spaces are left
12700 untouched. */
f1310107 12701
0e30163f 12702void
74421c0b 12703update_breakpoint_locations (code_breakpoint *b,
c2f4122d 12704 struct program_space *filter_pspace,
6c5b2ebe
PA
12705 gdb::array_view<const symtab_and_line> sals,
12706 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 12707{
c2f4122d 12708 struct bp_location *existing_locations;
0d381245 12709
6c5b2ebe 12710 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
12711 {
12712 /* Ranged breakpoints have only one start location and one end
12713 location. */
12714 b->enable_state = bp_disabled;
6cb06a8c
TT
12715 gdb_printf (gdb_stderr,
12716 _("Could not reset ranged breakpoint %d: "
12717 "multiple locations found\n"),
12718 b->number);
f8eba3c6
TT
12719 return;
12720 }
f1310107 12721
4a64f543
MS
12722 /* If there's no new locations, and all existing locations are
12723 pending, don't do anything. This optimizes the common case where
12724 all locations are in the same shared library, that was unloaded.
12725 We'd like to retain the location, so that when the library is
12726 loaded again, we don't loose the enabled/disabled status of the
12727 individual locations. */
6c5b2ebe 12728 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
12729 return;
12730
c2f4122d 12731 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 12732
6c5b2ebe 12733 for (const auto &sal : sals)
fe3f5fa8 12734 {
f8eba3c6
TT
12735 struct bp_location *new_loc;
12736
6c5b2ebe 12737 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 12738
06615628 12739 new_loc = b->add_location (sal);
fe3f5fa8 12740
0d381245
VP
12741 /* Reparse conditions, they might contain references to the
12742 old symtab. */
12743 if (b->cond_string != NULL)
12744 {
bbc13ae3 12745 const char *s;
fe3f5fa8 12746
6f781ee3 12747 s = b->cond_string.get ();
a70b8144 12748 try
0d381245 12749 {
6c5b2ebe
PA
12750 new_loc->cond = parse_exp_1 (&s, sal.pc,
12751 block_for_pc (sal.pc),
0d381245
VP
12752 0);
12753 }
230d2906 12754 catch (const gdb_exception_error &e)
0d381245 12755 {
b5fa468f 12756 new_loc->disabled_by_cond = true;
0d381245
VP
12757 }
12758 }
fe3f5fa8 12759
6c5b2ebe 12760 if (!sals_end.empty ())
f1310107 12761 {
6c5b2ebe 12762 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 12763
6c5b2ebe 12764 new_loc->length = end - sals[0].pc + 1;
f1310107 12765 }
0d381245 12766 }
fe3f5fa8 12767
4a64f543
MS
12768 /* If possible, carry over 'disable' status from existing
12769 breakpoints. */
0d381245
VP
12770 {
12771 struct bp_location *e = existing_locations;
776592bf
DE
12772 /* If there are multiple breakpoints with the same function name,
12773 e.g. for inline functions, comparing function names won't work.
12774 Instead compare pc addresses; this is just a heuristic as things
12775 may have moved, but in practice it gives the correct answer
12776 often enough until a better solution is found. */
12777 int have_ambiguous_names = ambiguous_names_p (b->loc);
12778
0d381245
VP
12779 for (; e; e = e->next)
12780 {
b5fa468f 12781 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 12782 {
776592bf
DE
12783 if (have_ambiguous_names)
12784 {
40cb8ca5 12785 for (bp_location *l : b->locations ())
7f32a4d5
PA
12786 {
12787 /* Ignore software vs hardware location type at
12788 this point, because with "set breakpoint
12789 auto-hw", after a re-set, locations that were
12790 hardware can end up as software, or vice versa.
12791 As mentioned above, this is an heuristic and in
12792 practice should give the correct answer often
12793 enough. */
12794 if (breakpoint_locations_match (e, l, true))
12795 {
b5fa468f
TBA
12796 l->enabled = e->enabled;
12797 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
12798 break;
12799 }
12800 }
776592bf
DE
12801 }
12802 else
12803 {
40cb8ca5 12804 for (bp_location *l : b->locations ())
776592bf 12805 if (l->function_name
23d6ee64
TT
12806 && strcmp (e->function_name.get (),
12807 l->function_name.get ()) == 0)
776592bf 12808 {
b5fa468f
TBA
12809 l->enabled = e->enabled;
12810 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
12811 break;
12812 }
12813 }
0d381245
VP
12814 }
12815 }
12816 }
fe3f5fa8 12817
8d3788bd 12818 if (!locations_are_equal (existing_locations, b->loc))
76727919 12819 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
12820}
12821
264f9890 12822/* Find the SaL locations corresponding to the given LOCSPEC.
ef23e705
TJB
12823 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12824
2c9a6d72
TT
12825std::vector<symtab_and_line>
12826code_breakpoint::location_spec_to_sals (location_spec *locspec,
12827 struct program_space *search_pspace,
12828 int *found)
ef23e705 12829{
cc06b668 12830 struct gdb_exception exception;
ef23e705 12831
6c5b2ebe
PA
12832 std::vector<symtab_and_line> sals;
12833
a70b8144 12834 try
ef23e705 12835 {
2c9a6d72 12836 sals = decode_location_spec (locspec, search_pspace);
ef23e705 12837 }
94aeb44b 12838 catch (gdb_exception_error &e)
ef23e705 12839 {
d8de7963 12840 int not_found_and_ok = false;
492d29ea 12841
ef23e705
TJB
12842 /* For pending breakpoints, it's expected that parsing will
12843 fail until the right shared library is loaded. User has
12844 already told to create pending breakpoints and don't need
12845 extra messages. If breakpoint is in bp_shlib_disabled
12846 state, then user already saw the message about that
12847 breakpoint being disabled, and don't want to see more
12848 errors. */
58438ac1 12849 if (e.error == NOT_FOUND_ERROR
2c9a6d72
TT
12850 && (condition_not_parsed
12851 || (loc != NULL
c2f4122d 12852 && search_pspace != NULL
2c9a6d72
TT
12853 && loc->pspace != search_pspace)
12854 || (loc && loc->shlib_disabled)
12855 || (loc && loc->pspace->executing_startup)
12856 || enable_state == bp_disabled))
d8de7963 12857 not_found_and_ok = true;
ef23e705
TJB
12858
12859 if (!not_found_and_ok)
12860 {
12861 /* We surely don't want to warn about the same breakpoint
12862 10 times. One solution, implemented here, is disable
12863 the breakpoint on error. Another solution would be to
12864 have separate 'warning emitted' flag. Since this
12865 happens only when a binary has changed, I don't know
12866 which approach is better. */
2c9a6d72 12867 enable_state = bp_disabled;
eedc3f4f 12868 throw;
ef23e705 12869 }
94aeb44b
TT
12870
12871 exception = std::move (e);
ef23e705
TJB
12872 }
12873
492d29ea 12874 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 12875 {
6c5b2ebe
PA
12876 for (auto &sal : sals)
12877 resolve_sal_pc (&sal);
2c9a6d72 12878 if (condition_not_parsed && extra_string != NULL)
ef23e705 12879 {
2c9a6d72
TT
12880 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12881 int local_thread, local_task;
ef23e705 12882
2c9a6d72
TT
12883 find_condition_and_thread_for_sals (sals, extra_string.get (),
12884 &local_cond, &local_thread,
12885 &local_task, &local_extra);
12886 gdb_assert (cond_string == nullptr);
12887 if (local_cond != nullptr)
12888 cond_string = std::move (local_cond);
12889 thread = local_thread;
12890 task = local_task;
12891 if (local_extra != nullptr)
12892 extra_string = std::move (local_extra);
12893 condition_not_parsed = 0;
ef23e705
TJB
12894 }
12895
2c9a6d72
TT
12896 if (type == bp_static_tracepoint)
12897 sals[0] = update_static_tracepoint (this, sals[0]);
ef23e705 12898
58438ac1
TT
12899 *found = 1;
12900 }
12901 else
12902 *found = 0;
ef23e705
TJB
12903
12904 return sals;
12905}
12906
348d480f
PA
12907/* The default re_set method, for typical hardware or software
12908 breakpoints. Reevaluate the breakpoint and recreate its
12909 locations. */
12910
b3d5660a
TT
12911void
12912code_breakpoint::re_set_default ()
ef23e705 12913{
c2f4122d 12914 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 12915 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 12916
6c5b2ebe 12917 int found;
2c9a6d72
TT
12918 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12919 filter_pspace,
12920 &found);
ef23e705 12921 if (found)
6c5b2ebe 12922 expanded = std::move (sals);
ef23e705 12923
b3d5660a 12924 if (locspec_range_end != nullptr)
f1310107 12925 {
6c5b2ebe 12926 std::vector<symtab_and_line> sals_end
2c9a6d72 12927 = location_spec_to_sals (locspec_range_end.get (),
264f9890 12928 filter_pspace, &found);
f1310107 12929 if (found)
6c5b2ebe 12930 expanded_end = std::move (sals_end);
f1310107
TJB
12931 }
12932
b3d5660a 12933 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
28010a5d
PA
12934}
12935
983af33b
SDJ
12936/* Default method for creating SALs from an address string. It basically
12937 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12938
12939static void
264f9890
PA
12940create_sals_from_location_spec_default (location_spec *locspec,
12941 struct linespec_result *canonical)
983af33b 12942{
264f9890 12943 parse_breakpoint_sals (locspec, canonical);
983af33b
SDJ
12944}
12945
bf469271 12946/* Reset a breakpoint. */
c906108c 12947
bf469271
PA
12948static void
12949breakpoint_re_set_one (breakpoint *b)
c906108c 12950{
fdf44873
TT
12951 input_radix = b->input_radix;
12952 set_language (b->language);
c906108c 12953
c47614fe 12954 b->re_set ();
c906108c
SS
12955}
12956
c2f4122d
PA
12957/* Re-set breakpoint locations for the current program space.
12958 Locations bound to other program spaces are left untouched. */
12959
c906108c 12960void
69de3c6a 12961breakpoint_re_set (void)
c906108c 12962{
c5aa993b 12963 {
fdf44873
TT
12964 scoped_restore_current_language save_language;
12965 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 12966 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 12967
8e817061
JB
12968 /* breakpoint_re_set_one sets the current_language to the language
12969 of the breakpoint it is resetting (see prepare_re_set_context)
12970 before re-evaluating the breakpoint's location. This change can
12971 unfortunately get undone by accident if the language_mode is set
12972 to auto, and we either switch frames, or more likely in this context,
12973 we select the current frame.
12974
12975 We prevent this by temporarily turning the language_mode to
12976 language_mode_manual. We restore it once all breakpoints
12977 have been reset. */
12978 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12979 language_mode = language_mode_manual;
12980
5ed8105e
PA
12981 /* Note: we must not try to insert locations until after all
12982 breakpoints have been re-set. Otherwise, e.g., when re-setting
12983 breakpoint 1, we'd insert the locations of breakpoint 2, which
12984 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 12985
1428b37a 12986 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 12987 {
a70b8144 12988 try
bf469271
PA
12989 {
12990 breakpoint_re_set_one (b);
12991 }
230d2906 12992 catch (const gdb_exception &ex)
bf469271
PA
12993 {
12994 exception_fprintf (gdb_stderr, ex,
12995 "Error in re-setting breakpoint %d: ",
12996 b->number);
12997 }
5ed8105e 12998 }
5ed8105e
PA
12999
13000 jit_breakpoint_re_set ();
13001 }
6c95b8df 13002
af02033e
PP
13003 create_overlay_event_breakpoint ();
13004 create_longjmp_master_breakpoint ();
13005 create_std_terminate_master_breakpoint ();
186c406b 13006 create_exception_master_breakpoint ();
2a7f3dff
PA
13007
13008 /* Now we can insert. */
13009 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13010}
13011\f
c906108c
SS
13012/* Reset the thread number of this breakpoint:
13013
13014 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13015 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13016void
fba45db2 13017breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13018{
13019 if (b->thread != -1)
13020 {
00431a78 13021 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13022
13023 /* We're being called after following a fork. The new fork is
13024 selected as current, and unless this was a vfork will have a
13025 different program space from the original thread. Reset that
13026 as well. */
13027 b->loc->pspace = current_program_space;
c906108c
SS
13028 }
13029}
13030
03ac34d5
MS
13031/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13032 If from_tty is nonzero, it prints a message to that effect,
13033 which ends with a period (no newline). */
13034
c906108c 13035void
fba45db2 13036set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13037{
c906108c
SS
13038 if (count < 0)
13039 count = 0;
13040
43892fdf 13041 for (breakpoint *b : all_breakpoints ())
c906108c 13042 if (b->number == bptnum)
01add95b
SM
13043 {
13044 if (is_tracepoint (b))
13045 {
13046 if (from_tty && count != 0)
6cb06a8c
TT
13047 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13048 bptnum);
01add95b
SM
13049 return;
13050 }
13051
13052 b->ignore_count = count;
13053 if (from_tty)
13054 {
13055 if (count == 0)
6cb06a8c
TT
13056 gdb_printf (_("Will stop next time "
13057 "breakpoint %d is reached."),
13058 bptnum);
01add95b 13059 else if (count == 1)
6cb06a8c
TT
13060 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13061 bptnum);
01add95b 13062 else
6cb06a8c
TT
13063 gdb_printf (_("Will ignore next %d "
13064 "crossings of breakpoint %d."),
13065 count, bptnum);
01add95b
SM
13066 }
13067 gdb::observers::breakpoint_modified.notify (b);
13068 return;
13069 }
c906108c 13070
8a3fe4f8 13071 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13072}
13073
c906108c
SS
13074/* Command to set ignore-count of breakpoint N to COUNT. */
13075
13076static void
0b39b52e 13077ignore_command (const char *args, int from_tty)
c906108c 13078{
0b39b52e 13079 const char *p = args;
52f0bd74 13080 int num;
c906108c
SS
13081
13082 if (p == 0)
e2e0b3e5 13083 error_no_arg (_("a breakpoint number"));
c5aa993b 13084
c906108c 13085 num = get_number (&p);
5c44784c 13086 if (num == 0)
8a3fe4f8 13087 error (_("bad breakpoint number: '%s'"), args);
c906108c 13088 if (*p == 0)
8a3fe4f8 13089 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13090
13091 set_ignore_count (num,
13092 longest_to_int (value_as_long (parse_and_eval (p))),
13093 from_tty);
221ea385 13094 if (from_tty)
6cb06a8c 13095 gdb_printf ("\n");
c906108c
SS
13096}
13097\f
d0fe4701
XR
13098
13099/* Call FUNCTION on each of the breakpoints with numbers in the range
13100 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13101
13102static void
d0fe4701
XR
13103map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13104 gdb::function_view<void (breakpoint *)> function)
c906108c 13105{
d0fe4701
XR
13106 if (bp_num_range.first == 0)
13107 {
13108 warning (_("bad breakpoint number at or near '%d'"),
13109 bp_num_range.first);
13110 }
13111 else
c906108c 13112 {
d0fe4701 13113 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13114 {
d0fe4701
XR
13115 bool match = false;
13116
1428b37a 13117 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 13118 if (b->number == i)
5c44784c 13119 {
bfd28288 13120 match = true;
48649e1b 13121 function (b);
11cf8741 13122 break;
5c44784c 13123 }
bfd28288 13124 if (!match)
6cb06a8c 13125 gdb_printf (_("No breakpoint number %d.\n"), i);
c5aa993b 13126 }
c906108c
SS
13127 }
13128}
13129
d0fe4701
XR
13130/* Call FUNCTION on each of the breakpoints whose numbers are given in
13131 ARGS. */
13132
13133static void
13134map_breakpoint_numbers (const char *args,
13135 gdb::function_view<void (breakpoint *)> function)
13136{
13137 if (args == NULL || *args == '\0')
13138 error_no_arg (_("one or more breakpoint numbers"));
13139
13140 number_or_range_parser parser (args);
13141
13142 while (!parser.finished ())
13143 {
13144 int num = parser.get_number ();
13145 map_breakpoint_number_range (std::make_pair (num, num), function);
13146 }
13147}
13148
13149/* Return the breakpoint location structure corresponding to the
13150 BP_NUM and LOC_NUM values. */
13151
0d381245 13152static struct bp_location *
d0fe4701 13153find_location_by_number (int bp_num, int loc_num)
0d381245 13154{
43892fdf 13155 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
13156
13157 if (!b || b->number != bp_num)
d0fe4701 13158 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13159
0d381245 13160 if (loc_num == 0)
d0fe4701 13161 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13162
d0fe4701 13163 int n = 0;
40cb8ca5 13164 for (bp_location *loc : b->locations ())
d0fe4701
XR
13165 if (++n == loc_num)
13166 return loc;
13167
13168 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13169}
13170
95e95a6d
PA
13171/* Modes of operation for extract_bp_num. */
13172enum class extract_bp_kind
13173{
13174 /* Extracting a breakpoint number. */
13175 bp,
13176
13177 /* Extracting a location number. */
13178 loc,
13179};
13180
13181/* Extract a breakpoint or location number (as determined by KIND)
13182 from the string starting at START. TRAILER is a character which
13183 can be found after the number. If you don't want a trailer, use
13184 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13185 string. This always returns a positive integer. */
13186
13187static int
13188extract_bp_num (extract_bp_kind kind, const char *start,
13189 int trailer, const char **end_out = NULL)
13190{
13191 const char *end = start;
13192 int num = get_number_trailer (&end, trailer);
13193 if (num < 0)
13194 error (kind == extract_bp_kind::bp
13195 ? _("Negative breakpoint number '%.*s'")
13196 : _("Negative breakpoint location number '%.*s'"),
13197 int (end - start), start);
13198 if (num == 0)
13199 error (kind == extract_bp_kind::bp
13200 ? _("Bad breakpoint number '%.*s'")
13201 : _("Bad breakpoint location number '%.*s'"),
13202 int (end - start), start);
13203
13204 if (end_out != NULL)
13205 *end_out = end;
13206 return num;
13207}
13208
13209/* Extract a breakpoint or location range (as determined by KIND) in
13210 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13211 representing the (inclusive) range. The returned pair's elements
13212 are always positive integers. */
13213
13214static std::pair<int, int>
13215extract_bp_or_bp_range (extract_bp_kind kind,
13216 const std::string &arg,
13217 std::string::size_type arg_offset)
13218{
13219 std::pair<int, int> range;
13220 const char *bp_loc = &arg[arg_offset];
13221 std::string::size_type dash = arg.find ('-', arg_offset);
13222 if (dash != std::string::npos)
13223 {
13224 /* bp_loc is a range (x-z). */
13225 if (arg.length () == dash + 1)
13226 error (kind == extract_bp_kind::bp
13227 ? _("Bad breakpoint number at or near: '%s'")
13228 : _("Bad breakpoint location number at or near: '%s'"),
13229 bp_loc);
13230
13231 const char *end;
13232 const char *start_first = bp_loc;
13233 const char *start_second = &arg[dash + 1];
13234 range.first = extract_bp_num (kind, start_first, '-');
13235 range.second = extract_bp_num (kind, start_second, '\0', &end);
13236
13237 if (range.first > range.second)
13238 error (kind == extract_bp_kind::bp
13239 ? _("Inverted breakpoint range at '%.*s'")
13240 : _("Inverted breakpoint location range at '%.*s'"),
13241 int (end - start_first), start_first);
13242 }
13243 else
13244 {
13245 /* bp_loc is a single value. */
13246 range.first = extract_bp_num (kind, bp_loc, '\0');
13247 range.second = range.first;
13248 }
13249 return range;
13250}
13251
d0fe4701
XR
13252/* Extract the breakpoint/location range specified by ARG. Returns
13253 the breakpoint range in BP_NUM_RANGE, and the location range in
13254 BP_LOC_RANGE.
13255
13256 ARG may be in any of the following forms:
13257
13258 x where 'x' is a breakpoint number.
13259 x-y where 'x' and 'y' specify a breakpoint numbers range.
13260 x.y where 'x' is a breakpoint number and 'y' a location number.
13261 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13262 location number range.
13263*/
13264
cc638e86 13265static void
d0fe4701
XR
13266extract_bp_number_and_location (const std::string &arg,
13267 std::pair<int, int> &bp_num_range,
13268 std::pair<int, int> &bp_loc_range)
13269{
13270 std::string::size_type dot = arg.find ('.');
13271
13272 if (dot != std::string::npos)
13273 {
13274 /* Handle 'x.y' and 'x.y-z' cases. */
13275
13276 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 13277 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 13278
95e95a6d
PA
13279 bp_num_range.first
13280 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13281 bp_num_range.second = bp_num_range.first;
d0fe4701 13282
95e95a6d
PA
13283 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13284 arg, dot + 1);
d0fe4701
XR
13285 }
13286 else
13287 {
13288 /* Handle x and x-y cases. */
d0fe4701 13289
95e95a6d 13290 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
13291 bp_loc_range.first = 0;
13292 bp_loc_range.second = 0;
13293 }
d0fe4701
XR
13294}
13295
13296/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13297 specifies whether to enable or disable. */
13298
13299static void
13300enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13301{
13302 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13303 if (loc != NULL)
13304 {
b5fa468f
TBA
13305 if (loc->disabled_by_cond && enable)
13306 error (_("Breakpoint %d's condition is invalid at location %d, "
13307 "cannot enable."), bp_num, loc_num);
13308
d0fe4701
XR
13309 if (loc->enabled != enable)
13310 {
13311 loc->enabled = enable;
13312 mark_breakpoint_location_modified (loc);
13313 }
13314 if (target_supports_enable_disable_tracepoint ()
13315 && current_trace_status ()->running && loc->owner
13316 && is_tracepoint (loc->owner))
13317 target_disable_tracepoint (loc);
13318 }
13319 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13320
e5213e2c
SF
13321 gdb::observers::breakpoint_modified.notify (loc->owner);
13322}
13323
13324/* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13325 owner. 1-based indexing. -1 signals NOT FOUND. */
13326
13327static int
d8a77e4c 13328find_loc_num_by_location (const bp_location *loc)
e5213e2c
SF
13329{
13330 if (loc != nullptr && loc->owner != nullptr)
13331 {
13332 /* Locations use 1-based indexing. */
13333 int loc_num = 1;
13334 for (bp_location *it : loc->owner->locations ())
13335 {
13336 if (it == loc)
13337 return loc_num;
13338 loc_num++;
13339 }
13340 }
13341 return -1;
13342}
13343
13344/* Enable or disable a breakpoint location LOC. ENABLE
13345 specifies whether to enable or disable. */
13346
13347void
13348enable_disable_bp_location (bp_location *loc, bool enable)
13349{
13350 if (loc == nullptr)
13351 error (_("Breakpoint location is invalid."));
13352
13353 if (loc->owner == nullptr)
13354 error (_("Breakpoint location does not have an owner breakpoint."));
13355
13356 if (loc->disabled_by_cond && enable)
13357 {
13358 int loc_num = find_loc_num_by_location (loc);
13359 if (loc_num == -1)
13360 error (_("Breakpoint location LOC_NUM could not be found."));
13361 else
13362 error (_("Breakpoint %d's condition is invalid at location %d, "
13363 "cannot enable."), loc->owner->number, loc_num);
13364 }
13365
13366 if (loc->enabled != enable)
13367 {
13368 loc->enabled = enable;
13369 mark_breakpoint_location_modified (loc);
13370 }
13371
13372 if (target_supports_enable_disable_tracepoint ()
13373 && current_trace_status ()->running && loc->owner
13374 && is_tracepoint (loc->owner))
13375 target_disable_tracepoint (loc);
13376
13377 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13378 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
13379}
13380
13381/* Enable or disable a range of breakpoint locations. BP_NUM is the
13382 number of the breakpoint, and BP_LOC_RANGE specifies the
13383 (inclusive) range of location numbers of that breakpoint to
13384 enable/disable. ENABLE specifies whether to enable or disable the
13385 location. */
13386
13387static void
13388enable_disable_breakpoint_location_range (int bp_num,
13389 std::pair<int, int> &bp_loc_range,
13390 bool enable)
13391{
13392 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13393 enable_disable_bp_num_loc (bp_num, i, enable);
13394}
0d381245 13395
1900040c
MS
13396/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13397 If from_tty is nonzero, it prints a message to that effect,
13398 which ends with a period (no newline). */
13399
c906108c 13400void
fba45db2 13401disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
13402{
13403 /* Never disable a watchpoint scope breakpoint; we want to
13404 hit them when we leave scope so we can delete both the
13405 watchpoint and its scope breakpoint at that time. */
13406 if (bpt->type == bp_watchpoint_scope)
13407 return;
13408
b5de0fa7 13409 bpt->enable_state = bp_disabled;
c906108c 13410
b775012e
LM
13411 /* Mark breakpoint locations modified. */
13412 mark_breakpoint_modified (bpt);
13413
d248b706
KY
13414 if (target_supports_enable_disable_tracepoint ()
13415 && current_trace_status ()->running && is_tracepoint (bpt))
13416 {
40cb8ca5 13417 for (bp_location *location : bpt->locations ())
d248b706
KY
13418 target_disable_tracepoint (location);
13419 }
13420
44702360 13421 update_global_location_list (UGLL_DONT_INSERT);
c906108c 13422
76727919 13423 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13424}
13425
d0fe4701
XR
13426/* Enable or disable the breakpoint(s) or breakpoint location(s)
13427 specified in ARGS. ARGS may be in any of the formats handled by
13428 extract_bp_number_and_location. ENABLE specifies whether to enable
13429 or disable the breakpoints/locations. */
13430
c906108c 13431static void
d0fe4701 13432enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 13433{
c906108c 13434 if (args == 0)
46c6471b 13435 {
43892fdf 13436 for (breakpoint *bpt : all_breakpoints ())
46c6471b 13437 if (user_breakpoint_p (bpt))
d0fe4701
XR
13438 {
13439 if (enable)
13440 enable_breakpoint (bpt);
13441 else
13442 disable_breakpoint (bpt);
13443 }
46c6471b 13444 }
9eaabc75 13445 else
0d381245 13446 {
cb791d59 13447 std::string num = extract_arg (&args);
9eaabc75 13448
cb791d59 13449 while (!num.empty ())
d248b706 13450 {
d0fe4701 13451 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 13452
cc638e86
PA
13453 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13454
13455 if (bp_loc_range.first == bp_loc_range.second
78805ff8
PW
13456 && (bp_loc_range.first == 0
13457 || (bp_loc_range.first == 1
13458 && bp_num_range.first == bp_num_range.second
13459 && !has_multiple_locations (bp_num_range.first))))
d0fe4701 13460 {
78805ff8
PW
13461 /* Handle breakpoint ids with formats 'x' or 'x-z'
13462 or 'y.1' where y has only one code location. */
cc638e86
PA
13463 map_breakpoint_number_range (bp_num_range,
13464 enable
13465 ? enable_breakpoint
13466 : disable_breakpoint);
13467 }
13468 else
13469 {
13470 /* Handle breakpoint ids with formats 'x.y' or
13471 'x.y-z'. */
13472 enable_disable_breakpoint_location_range
13473 (bp_num_range.first, bp_loc_range, enable);
b775012e 13474 }
9eaabc75 13475 num = extract_arg (&args);
d248b706 13476 }
0d381245 13477 }
c906108c
SS
13478}
13479
d0fe4701
XR
13480/* The disable command disables the specified breakpoints/locations
13481 (or all defined breakpoints) so they're no longer effective in
13482 stopping the inferior. ARGS may be in any of the forms defined in
13483 extract_bp_number_and_location. */
13484
13485static void
13486disable_command (const char *args, int from_tty)
13487{
13488 enable_disable_command (args, from_tty, false);
13489}
13490
c906108c 13491static void
816338b5
SS
13492enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13493 int count)
c906108c 13494{
afe38095 13495 int target_resources_ok;
c906108c
SS
13496
13497 if (bpt->type == bp_hardware_breakpoint)
13498 {
13499 int i;
c5aa993b 13500 i = hw_breakpoint_used_count ();
53a5351d 13501 target_resources_ok =
d92524f1 13502 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 13503 i + 1, 0);
c906108c 13504 if (target_resources_ok == 0)
8a3fe4f8 13505 error (_("No hardware breakpoint support in the target."));
c906108c 13506 else if (target_resources_ok < 0)
8a3fe4f8 13507 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
13508 }
13509
cc60f2e3 13510 if (is_watchpoint (bpt))
c906108c 13511 {
d07205c2 13512 /* Initialize it just to avoid a GCC false warning. */
f486487f 13513 enum enable_state orig_enable_state = bp_disabled;
dde02812 13514
a70b8144 13515 try
c906108c 13516 {
3a5c3e22
PA
13517 struct watchpoint *w = (struct watchpoint *) bpt;
13518
1e718ff1
TJB
13519 orig_enable_state = bpt->enable_state;
13520 bpt->enable_state = bp_enabled;
d8de7963 13521 update_watchpoint (w, true /* reparse */);
c906108c 13522 }
230d2906 13523 catch (const gdb_exception &e)
c5aa993b 13524 {
1e718ff1 13525 bpt->enable_state = orig_enable_state;
dde02812
ES
13526 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13527 bpt->number);
13528 return;
c5aa993b 13529 }
c906108c 13530 }
0101ce28 13531
b775012e
LM
13532 bpt->enable_state = bp_enabled;
13533
13534 /* Mark breakpoint locations modified. */
13535 mark_breakpoint_modified (bpt);
13536
d248b706
KY
13537 if (target_supports_enable_disable_tracepoint ()
13538 && current_trace_status ()->running && is_tracepoint (bpt))
13539 {
40cb8ca5 13540 for (bp_location *location : bpt->locations ())
d248b706
KY
13541 target_enable_tracepoint (location);
13542 }
13543
b4c291bb 13544 bpt->disposition = disposition;
816338b5 13545 bpt->enable_count = count;
44702360 13546 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 13547
76727919 13548 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13549}
13550
fe3f5fa8 13551
c906108c 13552void
fba45db2 13553enable_breakpoint (struct breakpoint *bpt)
c906108c 13554{
816338b5 13555 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
13556}
13557
d0fe4701
XR
13558/* The enable command enables the specified breakpoints/locations (or
13559 all defined breakpoints) so they once again become (or continue to
13560 be) effective in stopping the inferior. ARGS may be in any of the
13561 forms defined in extract_bp_number_and_location. */
c906108c 13562
c906108c 13563static void
981a3fb3 13564enable_command (const char *args, int from_tty)
c906108c 13565{
d0fe4701 13566 enable_disable_command (args, from_tty, true);
c906108c
SS
13567}
13568
c906108c 13569static void
4495129a 13570enable_once_command (const char *args, int from_tty)
c906108c 13571{
48649e1b
TT
13572 map_breakpoint_numbers
13573 (args, [&] (breakpoint *b)
13574 {
13575 iterate_over_related_breakpoints
13576 (b, [&] (breakpoint *bpt)
13577 {
13578 enable_breakpoint_disp (bpt, disp_disable, 1);
13579 });
13580 });
816338b5
SS
13581}
13582
13583static void
4495129a 13584enable_count_command (const char *args, int from_tty)
816338b5 13585{
b9d61307
SM
13586 int count;
13587
13588 if (args == NULL)
13589 error_no_arg (_("hit count"));
13590
13591 count = get_number (&args);
816338b5 13592
48649e1b
TT
13593 map_breakpoint_numbers
13594 (args, [&] (breakpoint *b)
13595 {
13596 iterate_over_related_breakpoints
13597 (b, [&] (breakpoint *bpt)
13598 {
13599 enable_breakpoint_disp (bpt, disp_disable, count);
13600 });
13601 });
c906108c
SS
13602}
13603
c906108c 13604static void
4495129a 13605enable_delete_command (const char *args, int from_tty)
c906108c 13606{
48649e1b
TT
13607 map_breakpoint_numbers
13608 (args, [&] (breakpoint *b)
13609 {
13610 iterate_over_related_breakpoints
13611 (b, [&] (breakpoint *bpt)
13612 {
13613 enable_breakpoint_disp (bpt, disp_del, 1);
13614 });
13615 });
c906108c
SS
13616}
13617\f
1f3b5d1b
PP
13618/* Invalidate last known value of any hardware watchpoint if
13619 the memory which that value represents has been written to by
13620 GDB itself. */
13621
13622static void
8de0566d
YQ
13623invalidate_bp_value_on_memory_change (struct inferior *inferior,
13624 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
13625 const bfd_byte *data)
13626{
43892fdf 13627 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 13628 if (bp->enable_state == bp_enabled
3a5c3e22 13629 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 13630 {
3a5c3e22 13631 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 13632
850645cf 13633 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 13634 {
40cb8ca5 13635 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
13636 if (loc->loc_type == bp_loc_hardware_watchpoint
13637 && loc->address + loc->length > addr
13638 && addr + len > loc->address)
13639 {
3a5c3e22 13640 wp->val = NULL;
4c1d86d9 13641 wp->val_valid = false;
3a5c3e22
PA
13642 }
13643 }
1f3b5d1b
PP
13644 }
13645}
13646
8181d85f
DJ
13647/* Create and insert a breakpoint for software single step. */
13648
13649void
6c95b8df 13650insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 13651 const address_space *aspace,
4a64f543 13652 CORE_ADDR next_pc)
8181d85f 13653{
7c16b83e
PA
13654 struct thread_info *tp = inferior_thread ();
13655 struct symtab_and_line sal;
13656 CORE_ADDR pc = next_pc;
8181d85f 13657
34b7e8a6
PA
13658 if (tp->control.single_step_breakpoints == NULL)
13659 {
960bc2bd 13660 std::unique_ptr<breakpoint> b
7ab97995
PA
13661 (new momentary_breakpoint (gdbarch, bp_single_step,
13662 current_program_space,
13663 null_frame_id,
13664 tp->global_num));
960bc2bd 13665
34b7e8a6 13666 tp->control.single_step_breakpoints
960bc2bd 13667 = add_to_breakpoint_chain (std::move (b));
34b7e8a6 13668 }
8181d85f 13669
7c16b83e
PA
13670 sal = find_pc_line (pc, 0);
13671 sal.pc = pc;
13672 sal.section = find_pc_overlay (pc);
13673 sal.explicit_pc = 1;
960bc2bd
PA
13674
13675 auto *ss_bp
98ed24fb
TT
13676 = (gdb::checked_static_cast<momentary_breakpoint *>
13677 (tp->control.single_step_breakpoints));
960bc2bd 13678 ss_bp->add_location (sal);
8181d85f 13679
7c16b83e 13680 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
13681}
13682
93f9a11f
YQ
13683/* Insert single step breakpoints according to the current state. */
13684
13685int
13686insert_single_step_breakpoints (struct gdbarch *gdbarch)
13687{
f5ea389a 13688 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 13689 std::vector<CORE_ADDR> next_pcs;
93f9a11f 13690
f5ea389a 13691 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 13692
a0ff9e1a 13693 if (!next_pcs.empty ())
93f9a11f 13694 {
bd2b40ac 13695 frame_info_ptr frame = get_current_frame ();
8b86c959 13696 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 13697
a0ff9e1a 13698 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
13699 insert_single_step_breakpoint (gdbarch, aspace, pc);
13700
93f9a11f
YQ
13701 return 1;
13702 }
13703 else
13704 return 0;
13705}
13706
34b7e8a6 13707/* See breakpoint.h. */
f02253f1
HZ
13708
13709int
7c16b83e 13710breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 13711 const address_space *aspace,
7c16b83e 13712 CORE_ADDR pc)
1aafd4da 13713{
40cb8ca5 13714 for (bp_location *loc : bp->locations ())
7c16b83e
PA
13715 if (loc->inserted
13716 && breakpoint_location_address_match (loc, aspace, pc))
13717 return 1;
1aafd4da 13718
7c16b83e 13719 return 0;
ef370185
JB
13720}
13721
13722/* Check whether a software single-step breakpoint is inserted at
13723 PC. */
13724
13725int
accd0bcd 13726single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
13727 CORE_ADDR pc)
13728{
43892fdf 13729 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
13730 {
13731 if (bpt->type == bp_single_step
13732 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13733 return 1;
13734 }
13735 return 0;
1aafd4da
UW
13736}
13737
1042e4c0
SS
13738/* Tracepoint-specific operations. */
13739
13740/* Set tracepoint count to NUM. */
13741static void
13742set_tracepoint_count (int num)
13743{
13744 tracepoint_count = num;
4fa62494 13745 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
13746}
13747
70221824 13748static void
0b39b52e 13749trace_command (const char *arg, int from_tty)
1042e4c0 13750{
264f9890
PA
13751 location_spec_up locspec = string_to_location_spec (&arg,
13752 current_language);
13753 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13754 (locspec.get (), true /* is_tracepoint */);
55aa24fb 13755
558a9d82 13756 create_breakpoint (get_current_arch (),
264f9890 13757 locspec.get (),
10a636cc 13758 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13759 0 /* tempflag */,
13760 bp_tracepoint /* type_wanted */,
13761 0 /* Ignore count */,
13762 pending_break_support,
13763 ops,
13764 from_tty,
13765 1 /* enabled */,
13766 0 /* internal */, 0);
1042e4c0
SS
13767}
13768
70221824 13769static void
0b39b52e 13770ftrace_command (const char *arg, int from_tty)
7a697b8d 13771{
264f9890
PA
13772 location_spec_up locspec = string_to_location_spec (&arg,
13773 current_language);
558a9d82 13774 create_breakpoint (get_current_arch (),
264f9890 13775 locspec.get (),
10a636cc 13776 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13777 0 /* tempflag */,
13778 bp_fast_tracepoint /* type_wanted */,
13779 0 /* Ignore count */,
13780 pending_break_support,
74421c0b 13781 &code_breakpoint_ops,
558a9d82
YQ
13782 from_tty,
13783 1 /* enabled */,
13784 0 /* internal */, 0);
0fb4aa4b
PA
13785}
13786
13787/* strace command implementation. Creates a static tracepoint. */
13788
70221824 13789static void
0b39b52e 13790strace_command (const char *arg, int from_tty)
0fb4aa4b 13791{
a678887d 13792 const struct breakpoint_ops *ops;
264f9890 13793 location_spec_up locspec;
7b572efb 13794 enum bptype type;
983af33b
SDJ
13795
13796 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13797 or with a normal static tracepoint. */
61012eef 13798 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
13799 {
13800 ops = &strace_marker_breakpoint_ops;
264f9890
PA
13801 locspec = new_linespec_location_spec (&arg,
13802 symbol_name_match_type::FULL);
7b572efb 13803 type = bp_static_marker_tracepoint;
f00aae0f 13804 }
983af33b 13805 else
f00aae0f 13806 {
74421c0b 13807 ops = &code_breakpoint_ops;
264f9890 13808 locspec = string_to_location_spec (&arg, current_language);
7b572efb 13809 type = bp_static_tracepoint;
f00aae0f 13810 }
983af33b 13811
558a9d82 13812 create_breakpoint (get_current_arch (),
264f9890 13813 locspec.get (),
10a636cc 13814 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82 13815 0 /* tempflag */,
7b572efb 13816 type /* type_wanted */,
558a9d82
YQ
13817 0 /* Ignore count */,
13818 pending_break_support,
13819 ops,
13820 from_tty,
13821 1 /* enabled */,
13822 0 /* internal */, 0);
7a697b8d
SS
13823}
13824
409873ef
SS
13825/* Set up a fake reader function that gets command lines from a linked
13826 list that was acquired during tracepoint uploading. */
13827
13828static struct uploaded_tp *this_utp;
3149d8c1 13829static int next_cmd;
409873ef 13830
f8631e5e
SM
13831static const char *
13832read_uploaded_action (std::string &buffer)
409873ef 13833{
a18ba4e4 13834 char *rslt = nullptr;
409873ef 13835
a18ba4e4
SM
13836 if (next_cmd < this_utp->cmd_strings.size ())
13837 {
67aa1f3c 13838 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
13839 next_cmd++;
13840 }
409873ef
SS
13841
13842 return rslt;
13843}
13844
00bf0b85
SS
13845/* Given information about a tracepoint as recorded on a target (which
13846 can be either a live system or a trace file), attempt to create an
13847 equivalent GDB tracepoint. This is not a reliable process, since
13848 the target does not necessarily have all the information used when
13849 the tracepoint was originally defined. */
13850
d9b3f62e 13851struct tracepoint *
00bf0b85 13852create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 13853{
f2fc3015
TT
13854 const char *addr_str;
13855 char small_buf[100];
d9b3f62e 13856 struct tracepoint *tp;
fd9b8c24 13857
409873ef 13858 if (utp->at_string)
67aa1f3c 13859 addr_str = utp->at_string.get ();
409873ef
SS
13860 else
13861 {
13862 /* In the absence of a source location, fall back to raw
13863 address. Since there is no way to confirm that the address
13864 means the same thing as when the trace was started, warn the
13865 user. */
3e43a32a
MS
13866 warning (_("Uploaded tracepoint %d has no "
13867 "source location, using raw address"),
409873ef 13868 utp->number);
8c042590 13869 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
13870 addr_str = small_buf;
13871 }
13872
13873 /* There's not much we can do with a sequence of bytecodes. */
13874 if (utp->cond && !utp->cond_string)
3e43a32a
MS
13875 warning (_("Uploaded tracepoint %d condition "
13876 "has no source form, ignoring it"),
409873ef 13877 utp->number);
d5551862 13878
264f9890
PA
13879 location_spec_up locspec = string_to_location_spec (&addr_str,
13880 current_language);
8cdf0e15 13881 if (!create_breakpoint (get_current_arch (),
264f9890 13882 locspec.get (),
67aa1f3c 13883 utp->cond_string.get (), -1, addr_str,
10a636cc 13884 false /* force_condition */,
e7e0cddf 13885 0 /* parse cond/thread */,
8cdf0e15 13886 0 /* tempflag */,
0fb4aa4b 13887 utp->type /* type_wanted */,
8cdf0e15
VP
13888 0 /* Ignore count */,
13889 pending_break_support,
74421c0b 13890 &code_breakpoint_ops,
8cdf0e15 13891 0 /* from_tty */,
84f4c1fe 13892 utp->enabled /* enabled */,
44f238bb
PA
13893 0 /* internal */,
13894 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 13895 return NULL;
fd9b8c24 13896
409873ef 13897 /* Get the tracepoint we just created. */
fd9b8c24
PA
13898 tp = get_tracepoint (tracepoint_count);
13899 gdb_assert (tp != NULL);
d5551862 13900
00bf0b85
SS
13901 if (utp->pass > 0)
13902 {
8c042590 13903 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 13904 tp->number);
00bf0b85 13905
409873ef 13906 trace_pass_command (small_buf, 0);
00bf0b85
SS
13907 }
13908
409873ef
SS
13909 /* If we have uploaded versions of the original commands, set up a
13910 special-purpose "reader" function and call the usual command line
13911 reader, then pass the result to the breakpoint command-setting
13912 function. */
a18ba4e4 13913 if (!utp->cmd_strings.empty ())
00bf0b85 13914 {
12973681 13915 counted_command_line cmd_list;
00bf0b85 13916
409873ef 13917 this_utp = utp;
3149d8c1 13918 next_cmd = 0;
d5551862 13919
60b3cef2 13920 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 13921
c1fc2657 13922 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 13923 }
a18ba4e4
SM
13924 else if (!utp->actions.empty ()
13925 || !utp->step_actions.empty ())
3e43a32a
MS
13926 warning (_("Uploaded tracepoint %d actions "
13927 "have no source form, ignoring them"),
409873ef 13928 utp->number);
00bf0b85 13929
f196051f 13930 /* Copy any status information that might be available. */
c1fc2657 13931 tp->hit_count = utp->hit_count;
f196051f
SS
13932 tp->traceframe_usage = utp->traceframe_usage;
13933
00bf0b85 13934 return tp;
d9b3f62e 13935}
00bf0b85 13936
1042e4c0
SS
13937/* Print information on tracepoint number TPNUM_EXP, or all if
13938 omitted. */
13939
13940static void
1d12d88f 13941info_tracepoints_command (const char *args, int from_tty)
1042e4c0 13942{
79a45e25 13943 struct ui_out *uiout = current_uiout;
e5a67952 13944 int num_printed;
1042e4c0 13945
5c458ae8 13946 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
13947
13948 if (num_printed == 0)
1042e4c0 13949 {
e5a67952 13950 if (args == NULL || *args == '\0')
112e8700 13951 uiout->message ("No tracepoints.\n");
d77f58be 13952 else
112e8700 13953 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 13954 }
ad443146
SS
13955
13956 default_collect_info ();
1042e4c0
SS
13957}
13958
4a64f543 13959/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13960 Not supported by all targets. */
13961static void
5fed81ff 13962enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13963{
13964 enable_command (args, from_tty);
13965}
13966
4a64f543 13967/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13968 Not supported by all targets. */
13969static void
5fed81ff 13970disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13971{
13972 disable_command (args, from_tty);
13973}
13974
4a64f543 13975/* Remove a tracepoint (or all if no argument). */
1042e4c0 13976static void
4495129a 13977delete_trace_command (const char *arg, int from_tty)
1042e4c0 13978{
1042e4c0
SS
13979 dont_repeat ();
13980
13981 if (arg == 0)
13982 {
13983 int breaks_to_delete = 0;
13984
13985 /* Delete all breakpoints if no argument.
dda83cd7
SM
13986 Do not delete internal or call-dummy breakpoints, these
13987 have to be deleted with an explicit breakpoint number
4a64f543 13988 argument. */
f6d17b2b 13989 for (breakpoint *tp : all_tracepoints ())
1428b37a 13990 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
13991 {
13992 breaks_to_delete = 1;
13993 break;
13994 }
1042e4c0
SS
13995
13996 /* Ask user only if there are some breakpoints to delete. */
13997 if (!from_tty
13998 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13999 {
1428b37a 14000 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 14001 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14002 delete_breakpoint (b);
1042e4c0
SS
14003 }
14004 }
14005 else
48649e1b 14006 map_breakpoint_numbers
b926417a 14007 (arg, [&] (breakpoint *br)
48649e1b 14008 {
b926417a 14009 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14010 });
1042e4c0
SS
14011}
14012
197f0a60
TT
14013/* Helper function for trace_pass_command. */
14014
14015static void
d9b3f62e 14016trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14017{
d9b3f62e 14018 tp->pass_count = count;
76727919 14019 gdb::observers::breakpoint_modified.notify (tp);
197f0a60 14020 if (from_tty)
6cb06a8c
TT
14021 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14022 tp->number, count);
197f0a60
TT
14023}
14024
1042e4c0
SS
14025/* Set passcount for tracepoint.
14026
14027 First command argument is passcount, second is tracepoint number.
14028 If tracepoint number omitted, apply to most recently defined.
14029 Also accepts special argument "all". */
14030
14031static void
0b39b52e 14032trace_pass_command (const char *args, int from_tty)
1042e4c0 14033{
d9b3f62e 14034 struct tracepoint *t1;
0b39b52e 14035 ULONGEST count;
1042e4c0
SS
14036
14037 if (args == 0 || *args == 0)
3e43a32a
MS
14038 error (_("passcount command requires an "
14039 "argument (count + optional TP num)"));
1042e4c0 14040
0b39b52e 14041 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14042
529480d0 14043 args = skip_spaces (args);
1042e4c0
SS
14044 if (*args && strncasecmp (args, "all", 3) == 0)
14045 {
14046 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14047 if (*args)
14048 error (_("Junk at end of arguments."));
1042e4c0 14049
f6d17b2b 14050 for (breakpoint *b : all_tracepoints ())
01add95b
SM
14051 {
14052 t1 = (struct tracepoint *) b;
14053 trace_pass_set_count (t1, count, from_tty);
14054 }
197f0a60
TT
14055 }
14056 else if (*args == '\0')
1042e4c0 14057 {
5fa1d40e 14058 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14059 if (t1)
197f0a60
TT
14060 trace_pass_set_count (t1, count, from_tty);
14061 }
14062 else
14063 {
bfd28288
PA
14064 number_or_range_parser parser (args);
14065 while (!parser.finished ())
1042e4c0 14066 {
bfd28288 14067 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14068 if (t1)
14069 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14070 }
14071 }
1042e4c0
SS
14072}
14073
d9b3f62e 14074struct tracepoint *
1042e4c0
SS
14075get_tracepoint (int num)
14076{
f6d17b2b 14077 for (breakpoint *t : all_tracepoints ())
1042e4c0 14078 if (t->number == num)
d9b3f62e 14079 return (struct tracepoint *) t;
1042e4c0
SS
14080
14081 return NULL;
14082}
14083
d5551862
SS
14084/* Find the tracepoint with the given target-side number (which may be
14085 different from the tracepoint number after disconnecting and
14086 reconnecting). */
14087
d9b3f62e 14088struct tracepoint *
d5551862
SS
14089get_tracepoint_by_number_on_target (int num)
14090{
f6d17b2b 14091 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
14092 {
14093 struct tracepoint *t = (struct tracepoint *) b;
14094
14095 if (t->number_on_target == num)
14096 return t;
14097 }
d5551862
SS
14098
14099 return NULL;
14100}
14101
1042e4c0 14102/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14103 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14104 If the argument is missing, the most recent tracepoint
14105 (tracepoint_count) is returned. */
14106
d9b3f62e 14107struct tracepoint *
0b39b52e 14108get_tracepoint_by_number (const char **arg,
bfd28288 14109 number_or_range_parser *parser)
1042e4c0 14110{
1042e4c0 14111 int tpnum;
0b39b52e 14112 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14113
bfd28288 14114 if (parser != NULL)
197f0a60 14115 {
bfd28288
PA
14116 gdb_assert (!parser->finished ());
14117 tpnum = parser->get_number ();
197f0a60
TT
14118 }
14119 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14120 tpnum = tracepoint_count;
1042e4c0 14121 else
197f0a60 14122 tpnum = get_number (arg);
1042e4c0
SS
14123
14124 if (tpnum <= 0)
14125 {
14126 if (instring && *instring)
6cb06a8c
TT
14127 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14128 instring);
1042e4c0 14129 else
6cb06a8c 14130 gdb_printf (_("No previous tracepoint\n"));
1042e4c0
SS
14131 return NULL;
14132 }
14133
f6d17b2b 14134 for (breakpoint *t : all_tracepoints ())
1042e4c0 14135 if (t->number == tpnum)
d9b3f62e 14136 return (struct tracepoint *) t;
1042e4c0 14137
6cb06a8c 14138 gdb_printf ("No tracepoint number %d.\n", tpnum);
1042e4c0
SS
14139 return NULL;
14140}
14141
d9b3f62e 14142void
04d0163c 14143breakpoint::print_recreate_thread (struct ui_file *fp) const
d9b3f62e 14144{
04d0163c
TT
14145 if (thread != -1)
14146 gdb_printf (fp, " thread %d", thread);
d9b3f62e 14147
2ecee236 14148 if (task != -1)
04d0163c 14149 gdb_printf (fp, " task %d", task);
d9b3f62e 14150
6cb06a8c 14151 gdb_printf (fp, "\n");
d9b3f62e
PA
14152}
14153
6149aea9
PA
14154/* Save information on user settable breakpoints (watchpoints, etc) to
14155 a new script file named FILENAME. If FILTER is non-NULL, call it
14156 on each breakpoint and only include the ones for which it returns
f2478a7e 14157 true. */
6149aea9 14158
1042e4c0 14159static void
4495129a 14160save_breakpoints (const char *filename, int from_tty,
f2478a7e 14161 bool (*filter) (const struct breakpoint *))
1042e4c0 14162{
d8de7963 14163 bool any = false;
6149aea9 14164 int extra_trace_bits = 0;
1042e4c0 14165
6149aea9
PA
14166 if (filename == 0 || *filename == 0)
14167 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14168
14169 /* See if we have anything to save. */
43892fdf 14170 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14171 {
14172 /* Skip internal and momentary breakpoints. */
14173 if (!user_breakpoint_p (tp))
14174 continue;
6149aea9 14175
01add95b
SM
14176 /* If we have a filter, only save the breakpoints it accepts. */
14177 if (filter && !filter (tp))
14178 continue;
6149aea9 14179
d8de7963 14180 any = true;
6149aea9 14181
01add95b
SM
14182 if (is_tracepoint (tp))
14183 {
14184 extra_trace_bits = 1;
6149aea9 14185
01add95b
SM
14186 /* We can stop searching. */
14187 break;
14188 }
14189 }
6149aea9
PA
14190
14191 if (!any)
1042e4c0 14192 {
6149aea9 14193 warning (_("Nothing to save."));
1042e4c0
SS
14194 return;
14195 }
14196
ee0c3293 14197 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14198
14199 stdio_file fp;
14200
ee0c3293 14201 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14202 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14203 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14204
6149aea9 14205 if (extra_trace_bits)
d7e74731 14206 save_trace_state_variables (&fp);
8bf6485c 14207
43892fdf 14208 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14209 {
14210 /* Skip internal and momentary breakpoints. */
14211 if (!user_breakpoint_p (tp))
14212 continue;
8bf6485c 14213
01add95b
SM
14214 /* If we have a filter, only save the breakpoints it accepts. */
14215 if (filter && !filter (tp))
14216 continue;
6149aea9 14217
c47614fe 14218 tp->print_recreate (&fp);
1042e4c0 14219
01add95b
SM
14220 /* Note, we can't rely on tp->number for anything, as we can't
14221 assume the recreated breakpoint numbers will match. Use $bpnum
14222 instead. */
6149aea9 14223
01add95b 14224 if (tp->cond_string)
6f781ee3 14225 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
6149aea9 14226
01add95b
SM
14227 if (tp->ignore_count)
14228 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14229
01add95b
SM
14230 if (tp->type != bp_dprintf && tp->commands)
14231 {
14232 fp.puts (" commands\n");
1042e4c0 14233
992aeed8
TT
14234 ui_out_redirect_pop redir (current_uiout, &fp);
14235 print_command_lines (current_uiout, tp->commands.get (), 2);
6149aea9 14236
01add95b
SM
14237 fp.puts (" end\n");
14238 }
6149aea9 14239
01add95b
SM
14240 if (tp->enable_state == bp_disabled)
14241 fp.puts ("disable $bpnum\n");
6149aea9 14242
01add95b
SM
14243 /* If this is a multi-location breakpoint, check if the locations
14244 should be individually disabled. Watchpoint locations are
14245 special, and not user visible. */
14246 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14247 {
14248 int n = 1;
40cb8ca5 14249
01add95b
SM
14250 for (bp_location *loc : tp->locations ())
14251 {
14252 if (!loc->enabled)
14253 fp.printf ("disable $bpnum.%d\n", n);
14254
14255 n++;
14256 }
14257 }
14258 }
8bf6485c 14259
e0700ba4
SM
14260 if (extra_trace_bits && !default_collect.empty ())
14261 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14262
1042e4c0 14263 if (from_tty)
6cb06a8c 14264 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14265}
14266
14267/* The `save breakpoints' command. */
14268
14269static void
4495129a 14270save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14271{
14272 save_breakpoints (args, from_tty, NULL);
14273}
14274
14275/* The `save tracepoints' command. */
14276
14277static void
4495129a 14278save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14279{
14280 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14281}
14282
c906108c 14283\f
629500fa
KS
14284/* This help string is used to consolidate all the help string for specifying
14285 locations used by several commands. */
14286
264f9890 14287#define LOCATION_SPEC_HELP_STRING \
629500fa
KS
14288"Linespecs are colon-separated lists of location parameters, such as\n\
14289source filename, function name, label name, and line number.\n\
14290Example: To specify the start of a label named \"the_top\" in the\n\
14291function \"fact\" in the file \"factorial.c\", use\n\
14292\"factorial.c:fact:the_top\".\n\
14293\n\
14294Address locations begin with \"*\" and specify an exact address in the\n\
14295program. Example: To specify the fourth byte past the start function\n\
14296\"main\", use \"*main + 4\".\n\
14297\n\
14298Explicit locations are similar to linespecs but use an option/argument\n\
14299syntax to specify location parameters.\n\
14300Example: To specify the start of the label named \"the_top\" in the\n\
14301function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14302-function fact -label the_top\".\n\
14303\n\
14304By default, a specified function is matched against the program's\n\
14305functions in all scopes. For C++, this means in all namespaces and\n\
14306classes. For Ada, this means in all packages. E.g., in C++,\n\
14307\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14308\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14309specified name as a complete fully-qualified name instead."
629500fa 14310
4a64f543
MS
14311/* This help string is used for the break, hbreak, tbreak and thbreak
14312 commands. It is defined as a macro to prevent duplication.
14313 COMMAND should be a string constant containing the name of the
14314 command. */
629500fa 14315
31e2b00f 14316#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14317command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14318\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14319PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14320probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
14321guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14322`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14323LOCATION may be a linespec, address, or explicit location as described\n\
14324below.\n\
14325\n\
dc10affe
PA
14326With no LOCATION, uses current execution address of the selected\n\
14327stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14328\n\
14329THREADNUM is the number from \"info threads\".\n\
14330CONDITION is a boolean expression.\n\
733d554a
TBA
14331\n\
14332With the \"-force-condition\" flag, the condition is defined even when\n\
14333it is invalid for all current locations.\n\
264f9890 14334\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14335Multiple breakpoints at one place are permitted, and useful if their\n\
14336conditions are different.\n\
31e2b00f
AS
14337\n\
14338Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14339
44feb3ce
TT
14340/* List of subcommands for "catch". */
14341static struct cmd_list_element *catch_cmdlist;
14342
14343/* List of subcommands for "tcatch". */
14344static struct cmd_list_element *tcatch_cmdlist;
14345
9ac4176b 14346void
a121b7c1 14347add_catch_command (const char *name, const char *docstring,
5538b03c 14348 cmd_func_ftype *func,
625e8578 14349 completer_ftype *completer,
44feb3ce
TT
14350 void *user_data_catch,
14351 void *user_data_tcatch)
14352{
14353 struct cmd_list_element *command;
14354
0450cc4c 14355 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14356 &catch_cmdlist);
5538b03c 14357 command->func = func;
0f8e2034 14358 command->set_context (user_data_catch);
a96d9b2e 14359 set_cmd_completer (command, completer);
44feb3ce 14360
0450cc4c 14361 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14362 &tcatch_cmdlist);
5538b03c 14363 command->func = func;
0f8e2034 14364 command->set_context (user_data_tcatch);
a96d9b2e 14365 set_cmd_completer (command, completer);
44feb3ce
TT
14366}
14367
d8de7963
AB
14368/* False if any of the breakpoint's locations could be a location where
14369 functions have been inlined, true otherwise. */
0574c78f 14370
d8de7963 14371static bool
0574c78f
GB
14372is_non_inline_function (struct breakpoint *b)
14373{
14374 /* The shared library event breakpoint is set on the address of a
14375 non-inline function. */
d8de7963 14376 return (b->type == bp_shlib_event);
0574c78f
GB
14377}
14378
14379/* Nonzero if the specified PC cannot be a location where functions
14380 have been inlined. */
14381
14382int
accd0bcd 14383pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14384 const target_waitstatus &ws)
0574c78f 14385{
43892fdf 14386 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
14387 {
14388 if (!is_non_inline_function (b))
14389 continue;
14390
40cb8ca5 14391 for (bp_location *bl : b->locations ())
0574c78f
GB
14392 {
14393 if (!bl->shlib_disabled
09ac7c10 14394 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14395 return 1;
14396 }
14397 }
14398
14399 return 0;
14400}
14401
2f202fde
JK
14402/* Remove any references to OBJFILE which is going to be freed. */
14403
14404void
14405breakpoint_free_objfile (struct objfile *objfile)
14406{
48d7020b 14407 for (bp_location *loc : all_bp_locations ())
3c86fae3 14408 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
2f202fde
JK
14409 loc->symtab = NULL;
14410}
14411
8bfd80db
YQ
14412/* Chain containing all defined "enable breakpoint" subcommands. */
14413
14414static struct cmd_list_element *enablebreaklist = NULL;
14415
8588b356
SM
14416/* See breakpoint.h. */
14417
14418cmd_list_element *commands_cmd_element = nullptr;
14419
6c265988 14420void _initialize_breakpoint ();
c906108c 14421void
6c265988 14422_initialize_breakpoint ()
c906108c
SS
14423{
14424 struct cmd_list_element *c;
14425
c90e7d63
SM
14426 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14427 "breakpoint");
14428 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14429 "breakpoint");
14430 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14431 "breakpoint");
84acb35a 14432
c906108c
SS
14433 breakpoint_chain = 0;
14434 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14435 before a breakpoint is set. */
14436 breakpoint_count = 0;
14437
1042e4c0
SS
14438 tracepoint_count = 0;
14439
1bedd215
AC
14440 add_com ("ignore", class_breakpoint, ignore_command, _("\
14441Set ignore-count of breakpoint number N to COUNT.\n\
14442Usage is `ignore N COUNT'."));
c906108c 14443
8588b356
SM
14444 commands_cmd_element = add_com ("commands", class_breakpoint,
14445 commands_command, _("\
18da0c51
MG
14446Set commands to be executed when the given breakpoints are hit.\n\
14447Give a space-separated breakpoint list as argument after \"commands\".\n\
14448A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14449(e.g. `5-7').\n\
c906108c
SS
14450With no argument, the targeted breakpoint is the last one set.\n\
14451The commands themselves follow starting on the next line.\n\
14452Type a line containing \"end\" to indicate the end of them.\n\
14453Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14454then no output is printed when it is hit, except what the commands print."));
c906108c 14455
b1d4d8d1
TBA
14456 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14457 static std::string condition_command_help
14458 = gdb::option::build_help (_("\
1bedd215 14459Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14460Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14461is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14462\n\
14463Options:\n\
14464%OPTIONS%"), cc_opts);
14465
14466 c = add_com ("condition", class_breakpoint, condition_command,
14467 condition_command_help.c_str ());
14468 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14469
1bedd215 14470 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14471Set a temporary breakpoint.\n\
c906108c
SS
14472Like \"break\" except the breakpoint is only temporary,\n\
14473so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14474by using \"enable delete\" on the breakpoint number.\n\
14475\n"
14476BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14477 set_cmd_completer (c, location_completer);
c94fdfd0 14478
1bedd215 14479 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14480Set a hardware assisted breakpoint.\n\
c906108c 14481Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14482some target hardware may not have this support.\n\
14483\n"
14484BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14485 set_cmd_completer (c, location_completer);
c906108c 14486
1bedd215 14487 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14488Set a temporary hardware assisted breakpoint.\n\
c906108c 14489Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14490so it will be deleted when hit.\n\
14491\n"
14492BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14493 set_cmd_completer (c, location_completer);
c906108c 14494
3947f654
SM
14495 cmd_list_element *enable_cmd
14496 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14497Enable all or some breakpoints.\n\
14498Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14499Give breakpoint numbers (separated by spaces) as arguments.\n\
14500With no subcommand, breakpoints are enabled until you command otherwise.\n\
14501This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14502With a subcommand you can enable temporarily."),
3947f654 14503 &enablelist, 1, &cmdlist);
c906108c 14504
3947f654 14505 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14506
84951ab5 14507 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14508Enable all or some breakpoints.\n\
14509Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14510Give breakpoint numbers (separated by spaces) as arguments.\n\
14511This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14512May be abbreviated to simply \"enable\"."),
2f822da5 14513 &enablebreaklist, 1, &enablelist);
c906108c 14514
1a966eab 14515 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14516Enable some breakpoints for one hit.\n\
14517Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14518If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14519 &enablebreaklist);
14520
1a966eab 14521 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14522Enable some breakpoints and delete when hit.\n\
14523Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14524If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14525 &enablebreaklist);
14526
816338b5 14527 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14528Enable some breakpoints for COUNT hits.\n\
14529Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14530If a breakpoint is hit while enabled in this fashion,\n\
14531the count is decremented; when it reaches zero, the breakpoint is disabled."),
14532 &enablebreaklist);
14533
1a966eab 14534 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14535Enable some breakpoints and delete when hit.\n\
14536Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14537If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14538 &enablelist);
14539
1a966eab 14540 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14541Enable some breakpoints for one hit.\n\
14542Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14543If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14544 &enablelist);
14545
14546 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14547Enable some breakpoints for COUNT hits.\n\
14548Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14549If a breakpoint is hit while enabled in this fashion,\n\
14550the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14551 &enablelist);
14552
3947f654
SM
14553 cmd_list_element *disable_cmd
14554 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14555Disable all or some breakpoints.\n\
14556Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14557Arguments are breakpoint numbers with spaces in between.\n\
14558To disable all breakpoints, give no argument.\n\
64b9b334 14559A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14560 &disablelist, 1, &cmdlist);
14561 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14562 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14563
57b4f16e 14564 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14565Disable all or some breakpoints.\n\
14566Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14567Arguments are breakpoint numbers with spaces in between.\n\
14568To disable all breakpoints, give no argument.\n\
64b9b334 14569A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14570This command may be abbreviated \"disable\"."),
c906108c
SS
14571 &disablelist);
14572
3947f654
SM
14573 cmd_list_element *delete_cmd
14574 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14575Delete all or some breakpoints.\n\
14576Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
14577Arguments are breakpoint numbers with spaces in between.\n\
14578To delete all breakpoints, give no argument.\n\
14579\n\
590042fc 14580Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14581 &deletelist, 1, &cmdlist);
14582 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14583 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14584
57b4f16e 14585 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14586Delete all or some breakpoints or auto-display expressions.\n\
14587Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14588Arguments are breakpoint numbers with spaces in between.\n\
14589To delete all breakpoints, give no argument.\n\
1a966eab 14590This command may be abbreviated \"delete\"."),
c906108c
SS
14591 &deletelist);
14592
3947f654
SM
14593 cmd_list_element *clear_cmd
14594 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14595Clear breakpoint at specified location.\n\
14596Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14597\n\
14598With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14599is executing in.\n"
264f9890 14600"\n" LOCATION_SPEC_HELP_STRING "\n\n\
1bedd215 14601See also the \"delete\" command which clears breakpoints by number."));
3947f654 14602 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14603
3947f654
SM
14604 cmd_list_element *break_cmd
14605 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14606Set breakpoint at specified location.\n"
31e2b00f 14607BREAK_ARGS_HELP ("break")));
3947f654 14608 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14609
3947f654
SM
14610 add_com_alias ("b", break_cmd, class_run, 1);
14611 add_com_alias ("br", break_cmd, class_run, 1);
14612 add_com_alias ("bre", break_cmd, class_run, 1);
14613 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14614
e0f25bd9
SM
14615 cmd_list_element *info_breakpoints_cmd
14616 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14617Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14618The \"Type\" column indicates one of:\n\
14619\tbreakpoint - normal breakpoint\n\
14620\twatchpoint - watchpoint\n\
14621The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14622the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14623breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14624address and file/line number respectively.\n\
14625\n\
14626Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14627are set to the address of the last breakpoint listed unless the command\n\
14628is prefixed with \"server \".\n\n\
c906108c 14629Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14630breakpoint set."));
c906108c 14631
e0f25bd9 14632 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14633
1a966eab
AC
14634 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14635Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14636The \"Type\" column indicates one of:\n\
14637\tbreakpoint - normal breakpoint\n\
14638\twatchpoint - watchpoint\n\
14639\tlongjmp - internal breakpoint used to step through longjmp()\n\
14640\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14641\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14642\tfinish - internal breakpoint used by the \"finish\" command\n\
14643The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14644the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14645breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14646address and file/line number respectively.\n\
14647\n\
14648Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14649are set to the address of the last breakpoint listed unless the command\n\
14650is prefixed with \"server \".\n\n\
c906108c 14651Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14652breakpoint set."),
c906108c
SS
14653 &maintenanceinfolist);
14654
3b6acaee 14655 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 14656Set catchpoints to catch events."),
2f822da5 14657 &catch_cmdlist,
3b6acaee 14658 0/*allow-unknown*/, &cmdlist);
44feb3ce 14659
3b6acaee 14660 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 14661Set temporary catchpoints to catch events."),
2f822da5 14662 &tcatch_cmdlist,
3b6acaee 14663 0/*allow-unknown*/, &cmdlist);
44feb3ce 14664
a15a5258
AB
14665 const auto opts = make_watch_options_def_group (nullptr);
14666
14667 static const std::string watch_help = gdb::option::build_help (_("\
14668Set a watchpoint for EXPRESSION.\n\
14669Usage: watch [-location] EXPRESSION\n\
14670\n\
14671Options:\n\
14672%OPTIONS%\n\
14673\n\
c906108c 14674A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
14675an expression changes."), opts);
14676 c = add_com ("watch", class_breakpoint, watch_command,
14677 watch_help.c_str ());
14678 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14679
14680 static const std::string rwatch_help = gdb::option::build_help (_("\
14681Set a read watchpoint for EXPRESSION.\n\
14682Usage: rwatch [-location] EXPRESSION\n\
14683\n\
14684Options:\n\
14685%OPTIONS%\n\
14686\n\
14687A read watchpoint stops execution of your program whenever the value of\n\
14688an expression is read."), opts);
14689 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14690 rwatch_help.c_str ());
14691 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14692
14693 static const std::string awatch_help = gdb::option::build_help (_("\
14694Set an access watchpoint for EXPRESSION.\n\
14695Usage: awatch [-location] EXPRESSION\n\
14696\n\
14697Options:\n\
14698%OPTIONS%\n\
14699\n\
14700An access watchpoint stops execution of your program whenever the value\n\
14701of an expression is either read or written."), opts);
14702 c = add_com ("awatch", class_breakpoint, awatch_command,
14703 awatch_help.c_str ());
14704 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 14705
11db9430 14706 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 14707Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14708
920d2a44
AC
14709 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14710 respond to changes - contrary to the description. */
85c07804
AC
14711 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14712 &can_use_hw_watchpoints, _("\
14713Set debugger's willingness to use watchpoint hardware."), _("\
14714Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14715If zero, gdb will not use hardware for new watchpoints, even if\n\
14716such is available. (However, any hardware watchpoints that were\n\
14717created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14718hardware.)"),
14719 NULL,
920d2a44 14720 show_can_use_hw_watchpoints,
85c07804 14721 &setlist, &showlist);
c906108c
SS
14722
14723 can_use_hw_watchpoints = 1;
fa8d40ab 14724
1042e4c0
SS
14725 /* Tracepoint manipulation commands. */
14726
3947f654
SM
14727 cmd_list_element *trace_cmd
14728 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 14729Set a tracepoint at specified location.\n\
1042e4c0
SS
14730\n"
14731BREAK_ARGS_HELP ("trace") "\n\
14732Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 14733 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 14734
3947f654
SM
14735 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14736 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14737 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14738 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 14739
7a697b8d 14740 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 14741Set a fast tracepoint at specified location.\n\
7a697b8d
SS
14742\n"
14743BREAK_ARGS_HELP ("ftrace") "\n\
14744Do \"help tracepoints\" for info on other tracepoint commands."));
14745 set_cmd_completer (c, location_completer);
14746
0fb4aa4b 14747 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 14748Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
14749\n\
14750strace [LOCATION] [if CONDITION]\n\
629500fa
KS
14751LOCATION may be a linespec, explicit, or address location (described below) \n\
14752or -m MARKER_ID.\n\n\
14753If a marker id is specified, probe the marker with that name. With\n\
14754no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
14755Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14756This collects arbitrary user data passed in the probe point call to the\n\
14757tracing library. You can inspect it when analyzing the trace buffer,\n\
14758by printing the $_sdata variable like any other convenience variable.\n\
14759\n\
14760CONDITION is a boolean expression.\n\
264f9890 14761\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14762Multiple tracepoints at one place are permitted, and useful if their\n\
14763conditions are different.\n\
0fb4aa4b
PA
14764\n\
14765Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14766Do \"help tracepoints\" for info on other tracepoint commands."));
14767 set_cmd_completer (c, location_completer);
14768
e0f25bd9
SM
14769 cmd_list_element *info_tracepoints_cmd
14770 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 14771Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14772Convenience variable \"$tpnum\" contains the number of the\n\
14773last tracepoint set."));
14774
e0f25bd9 14775 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 14776
5e84b7ee
SM
14777 cmd_list_element *delete_tracepoints_cmd
14778 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
14779Delete specified tracepoints.\n\
14780Arguments are tracepoint numbers, separated by spaces.\n\
14781No argument means delete all tracepoints."),
14782 &deletelist);
5e84b7ee 14783 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
14784
14785 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14786Disable specified tracepoints.\n\
14787Arguments are tracepoint numbers, separated by spaces.\n\
14788No argument means disable all tracepoints."),
14789 &disablelist);
14790 deprecate_cmd (c, "disable");
14791
14792 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14793Enable specified tracepoints.\n\
14794Arguments are tracepoint numbers, separated by spaces.\n\
14795No argument means enable all tracepoints."),
14796 &enablelist);
14797 deprecate_cmd (c, "enable");
14798
14799 add_com ("passcount", class_trace, trace_pass_command, _("\
14800Set the passcount for a tracepoint.\n\
14801The trace will end when the tracepoint has been passed 'count' times.\n\
14802Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14803if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14804
0743fc83
TT
14805 add_basic_prefix_cmd ("save", class_breakpoint,
14806 _("Save breakpoint definitions as a script."),
2f822da5 14807 &save_cmdlist,
0743fc83 14808 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
14809
14810 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14811Save current breakpoint definitions as a script.\n\
cce7e648 14812This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14813catchpoints, tracepoints). Use the 'source' command in another debug\n\
14814session to restore them."),
14815 &save_cmdlist);
14816 set_cmd_completer (c, filename_completer);
14817
3947f654
SM
14818 cmd_list_element *save_tracepoints_cmd
14819 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14820Save current tracepoint definitions as a script.\n\
6149aea9
PA
14821Use the 'source' command in another debug session to restore them."),
14822 &save_cmdlist);
3947f654 14823 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 14824
3947f654 14825 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
14826 deprecate_cmd (c, "save tracepoints");
14827
f54bdb6d
SM
14828 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14829 _("\
590042fc 14830Breakpoint specific settings.\n\
fa8d40ab 14831Configure various breakpoint-specific variables such as\n\
590042fc 14832pending breakpoint behavior."),
f54bdb6d 14833 _("\
590042fc 14834Breakpoint specific settings.\n\
fa8d40ab 14835Configure various breakpoint-specific variables such as\n\
590042fc 14836pending breakpoint behavior."),
f54bdb6d
SM
14837 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14838 &setlist, &showlist);
fa8d40ab 14839
7915a72c
AC
14840 add_setshow_auto_boolean_cmd ("pending", no_class,
14841 &pending_break_support, _("\
14842Set debugger's behavior regarding pending breakpoints."), _("\
14843Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14844If on, an unrecognized breakpoint location will cause gdb to create a\n\
14845pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14846an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14847user-query to see if a pending breakpoint should be created."),
2c5b56ce 14848 NULL,
920d2a44 14849 show_pending_break_support,
6e1d7d6c
AC
14850 &breakpoint_set_cmdlist,
14851 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14852
14853 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14854
14855 add_setshow_boolean_cmd ("auto-hw", no_class,
14856 &automatic_hardware_breakpoints, _("\
14857Set automatic usage of hardware breakpoints."), _("\
14858Show automatic usage of hardware breakpoints."), _("\
14859If set, the debugger will automatically use hardware breakpoints for\n\
14860breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14861a warning will be emitted for such breakpoints."),
14862 NULL,
14863 show_automatic_hardware_breakpoints,
14864 &breakpoint_set_cmdlist,
14865 &breakpoint_show_cmdlist);
74960c60 14866
a25a5a45
PA
14867 add_setshow_boolean_cmd ("always-inserted", class_support,
14868 &always_inserted_mode, _("\
74960c60
VP
14869Set mode for inserting breakpoints."), _("\
14870Show mode for inserting breakpoints."), _("\
a25a5a45
PA
14871When this mode is on, breakpoints are inserted immediately as soon as\n\
14872they're created, kept inserted even when execution stops, and removed\n\
14873only when the user deletes them. When this mode is off (the default),\n\
14874breakpoints are inserted only when execution continues, and removed\n\
14875when execution stops."),
72d0e2c5
YQ
14876 NULL,
14877 &show_always_inserted_mode,
14878 &breakpoint_set_cmdlist,
14879 &breakpoint_show_cmdlist);
f1310107 14880
b775012e
LM
14881 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14882 condition_evaluation_enums,
14883 &condition_evaluation_mode_1, _("\
14884Set mode of breakpoint condition evaluation."), _("\
14885Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 14886When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
14887evaluated on the host's side by GDB. When it is set to \"target\",\n\
14888breakpoint conditions will be downloaded to the target (if the target\n\
14889supports such feature) and conditions will be evaluated on the target's side.\n\
14890If this is set to \"auto\" (default), this will be automatically set to\n\
14891\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 14892be set to \"host\"."),
b775012e
LM
14893 &set_condition_evaluation_mode,
14894 &show_condition_evaluation_mode,
14895 &breakpoint_set_cmdlist,
14896 &breakpoint_show_cmdlist);
14897
f1310107
TJB
14898 add_com ("break-range", class_breakpoint, break_range_command, _("\
14899Set a breakpoint for an address range.\n\
14900break-range START-LOCATION, END-LOCATION\n\
14901where START-LOCATION and END-LOCATION can be one of the following:\n\
14902 LINENUM, for that line in the current file,\n\
14903 FILE:LINENUM, for that line in that file,\n\
14904 +OFFSET, for that number of lines after the current line\n\
dda83cd7 14905 or the start of the range\n\
f1310107
TJB
14906 FUNCTION, for the first line in that function,\n\
14907 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14908 *ADDRESS, for the instruction at that address.\n\
14909\n\
14910The breakpoint will stop execution of the inferior whenever it executes\n\
14911an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14912range (including START-LOCATION and END-LOCATION)."));
14913
e7e0cddf 14914 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 14915Set a dynamic printf at specified location.\n\
e7e0cddf 14916dprintf location,format string,arg1,arg2,...\n\
629500fa 14917location may be a linespec, explicit, or address location.\n"
264f9890 14918"\n" LOCATION_SPEC_HELP_STRING));
e7e0cddf
SS
14919 set_cmd_completer (c, location_completer);
14920
14921 add_setshow_enum_cmd ("dprintf-style", class_support,
14922 dprintf_style_enums, &dprintf_style, _("\
14923Set the style of usage for dynamic printf."), _("\
14924Show the style of usage for dynamic printf."), _("\
14925This setting chooses how GDB will do a dynamic printf.\n\
14926If the value is \"gdb\", then the printing is done by GDB to its own\n\
14927console, as with the \"printf\" command.\n\
14928If the value is \"call\", the print is done by calling a function in your\n\
14929program; by default printf(), but you can choose a different function or\n\
14930output stream by setting dprintf-function and dprintf-channel."),
14931 update_dprintf_commands, NULL,
14932 &setlist, &showlist);
14933
e7e0cddf
SS
14934 add_setshow_string_cmd ("dprintf-function", class_support,
14935 &dprintf_function, _("\
590042fc
PW
14936Set the function to use for dynamic printf."), _("\
14937Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
14938 update_dprintf_commands, NULL,
14939 &setlist, &showlist);
14940
e7e0cddf
SS
14941 add_setshow_string_cmd ("dprintf-channel", class_support,
14942 &dprintf_channel, _("\
590042fc
PW
14943Set the channel to use for dynamic printf."), _("\
14944Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
14945 update_dprintf_commands, NULL,
14946 &setlist, &showlist);
14947
d3ce09f5
SS
14948 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14949 &disconnected_dprintf, _("\
14950Set whether dprintf continues after GDB disconnects."), _("\
14951Show whether dprintf continues after GDB disconnects."), _("\
14952Use this to let dprintf commands continue to hit and produce output\n\
14953even if GDB disconnects or detaches from the target."),
14954 NULL,
14955 NULL,
14956 &setlist, &showlist);
14957
14958 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
14959Target agent only formatted printing, like the C \"printf\" function.\n\
14960Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14961This supports most C printf format specifications, like %s, %d, etc.\n\
14962This is useful for formatted output in user-defined commands."));
d3ce09f5 14963
491144b5 14964 automatic_hardware_breakpoints = true;
f3b1572e 14965
c90e7d63
SM
14966 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14967 "breakpoint");
14968 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14969 "breakpoint");
c906108c 14970}