]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Don't throw quit while handling inferior events, part II
[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
317c3ed9 1867 bit_val = value::allocate (val->type ());
bb9d5f81 1868
6c49729e
TT
1869 val->unpack_bitfield (bit_val,
1870 w->val_bitpos,
1871 w->val_bitsize,
1872 val->contents_for_printing ().data (),
1873 val->offset ());
bb9d5f81
PP
1874
1875 return bit_val;
1876}
1877
6e14e441
PA
1878/* Allocate a dummy location and add it to B. This is required
1879 because bpstat_stop_status requires a location to be able to report
1880 stops. */
c6d81124
PA
1881
1882static void
6e14e441
PA
1883add_dummy_location (struct breakpoint *b,
1884 struct program_space *pspace)
c6d81124 1885{
6e14e441 1886 gdb_assert (b->loc == NULL);
c6d81124 1887
6e14e441 1888 b->loc = new bp_location (b, bp_loc_other);
c6d81124 1889 b->loc->pspace = pspace;
c6d81124
PA
1890}
1891
567e1b4e 1892/* Assuming that B is a watchpoint:
d8de7963 1893 - Reparse watchpoint expression, if REPARSE is true
a5606eee 1894 - Evaluate expression and store the result in B->val
567e1b4e
JB
1895 - Evaluate the condition if there is one, and store the result
1896 in b->loc->cond.
a5606eee
VP
1897 - Update the list of values that must be watched in B->loc.
1898
4a64f543
MS
1899 If the watchpoint disposition is disp_del_at_next_stop, then do
1900 nothing. If this is local watchpoint that is out of scope, delete
1901 it.
1902
1903 Even with `set breakpoint always-inserted on' the watchpoints are
1904 removed + inserted on each stop here. Normal breakpoints must
1905 never be removed because they might be missed by a running thread
1906 when debugging in non-stop mode. On the other hand, hardware
1907 watchpoints (is_hardware_watchpoint; processed here) are specific
1908 to each LWP since they are stored in each LWP's hardware debug
1909 registers. Therefore, such LWP must be stopped first in order to
1910 be able to modify its hardware watchpoints.
1911
1912 Hardware watchpoints must be reset exactly once after being
1913 presented to the user. It cannot be done sooner, because it would
1914 reset the data used to present the watchpoint hit to the user. And
1915 it must not be done later because it could display the same single
1916 watchpoint hit during multiple GDB stops. Note that the latter is
1917 relevant only to the hardware watchpoint types bp_read_watchpoint
1918 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1919 not user-visible - its hit is suppressed if the memory content has
1920 not changed.
1921
1922 The following constraints influence the location where we can reset
1923 hardware watchpoints:
1924
1925 * target_stopped_by_watchpoint and target_stopped_data_address are
1926 called several times when GDB stops.
1927
1928 [linux]
1929 * Multiple hardware watchpoints can be hit at the same time,
1930 causing GDB to stop. GDB only presents one hardware watchpoint
1931 hit at a time as the reason for stopping, and all the other hits
1932 are presented later, one after the other, each time the user
1933 requests the execution to be resumed. Execution is not resumed
1934 for the threads still having pending hit event stored in
1935 LWP_INFO->STATUS. While the watchpoint is already removed from
1936 the inferior on the first stop the thread hit event is kept being
1937 reported from its cached value by linux_nat_stopped_data_address
1938 until the real thread resume happens after the watchpoint gets
1939 presented and thus its LWP_INFO->STATUS gets reset.
1940
1941 Therefore the hardware watchpoint hit can get safely reset on the
1942 watchpoint removal from inferior. */
a79d3c27 1943
b40ce68a 1944static void
d8de7963 1945update_watchpoint (struct watchpoint *b, bool reparse)
7270d8f2 1946{
705b6305 1947 bool within_current_scope;
a5606eee 1948
f6bc2008
PA
1949 /* If this is a local watchpoint, we only want to check if the
1950 watchpoint frame is in scope if the current thread is the thread
1951 that was used to create the watchpoint. */
1952 if (!watchpoint_in_thread_scope (b))
1953 return;
1954
c1fc2657 1955 if (b->disposition == disp_del_at_next_stop)
a5606eee 1956 return;
705b6305
AB
1957
1958 gdb::optional<scoped_restore_selected_frame> restore_frame;
a5606eee
VP
1959
1960 /* Determine if the watchpoint is within scope. */
1961 if (b->exp_valid_block == NULL)
705b6305 1962 within_current_scope = true;
a5606eee
VP
1963 else
1964 {
bd2b40ac 1965 frame_info_ptr fi = get_current_frame ();
b5db5dfc
UW
1966 struct gdbarch *frame_arch = get_frame_arch (fi);
1967 CORE_ADDR frame_pc = get_frame_pc (fi);
1968
c9cf6e20
MG
1969 /* If we're at a point where the stack has been destroyed
1970 (e.g. in a function epilogue), unwinding may not work
1971 properly. Do not attempt to recreate locations at this
b5db5dfc 1972 point. See similar comments in watchpoint_check. */
c9cf6e20 1973 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1974 return;
66076460
DJ
1975
1976 /* Save the current frame's ID so we can restore it after
dda83cd7 1977 evaluating the watchpoint expression on its own frame. */
66076460 1978 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1979 took a frame parameter, so that we didn't have to change the
1980 selected frame. */
705b6305 1981 restore_frame.emplace ();
66076460 1982
a5606eee
VP
1983 fi = frame_find_by_id (b->watchpoint_frame);
1984 within_current_scope = (fi != NULL);
1985 if (within_current_scope)
1986 select_frame (fi);
1987 }
1988
b5db5dfc
UW
1989 /* We don't free locations. They are stored in the bp_location array
1990 and update_global_location_list will eventually delete them and
1991 remove breakpoints if needed. */
c1fc2657 1992 b->loc = NULL;
b5db5dfc 1993
a5606eee
VP
1994 if (within_current_scope && reparse)
1995 {
bbc13ae3 1996 const char *s;
d63d0675 1997
4d01a485 1998 b->exp.reset ();
a4c50be3
TT
1999 s = (b->exp_string_reparse
2000 ? b->exp_string_reparse.get ()
2001 : b->exp_string.get ());
1bb9788d 2002 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
2003 /* If the meaning of expression itself changed, the old value is
2004 no longer relevant. We don't want to report a watchpoint hit
2005 to the user when the old value and the new value may actually
2006 be completely different objects. */
fa4727a6 2007 b->val = NULL;
4c1d86d9 2008 b->val_valid = false;
60e1c644
PA
2009
2010 /* Note that unlike with breakpoints, the watchpoint's condition
2011 expression is stored in the breakpoint object, not in the
2012 locations (re)created below. */
c1fc2657 2013 if (b->cond_string != NULL)
60e1c644 2014 {
4d01a485 2015 b->cond_exp.reset ();
60e1c644 2016
6f781ee3 2017 s = b->cond_string.get ();
1bb9788d 2018 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 2019 }
a5606eee 2020 }
a5606eee
VP
2021
2022 /* If we failed to parse the expression, for example because
2023 it refers to a global variable in a not-yet-loaded shared library,
2024 don't try to insert watchpoint. We don't automatically delete
2025 such watchpoint, though, since failure to parse expression
2026 is different from out-of-scope watchpoint. */
55f6301a 2027 if (!target_has_execution ())
2d134ed3
PA
2028 {
2029 /* Without execution, memory can't change. No use to try and
2030 set watchpoint locations. The watchpoint will be reset when
2031 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
2032 if (!can_use_hw_watchpoints)
2033 {
c47614fe 2034 if (b->works_in_software_mode ())
c1fc2657 2035 b->type = bp_watchpoint;
e8369a73 2036 else
638aa5a1
AB
2037 error (_("Can't set read/access watchpoint when "
2038 "hardware watchpoints are disabled."));
e8369a73 2039 }
2d134ed3
PA
2040 }
2041 else if (within_current_scope && b->exp)
a5606eee 2042 {
a6535de1 2043 std::vector<value_ref_ptr> val_chain;
8d49165d 2044 struct value *v, *result;
2d134ed3 2045 struct program_space *frame_pspace;
a5606eee 2046
1eaebe02 2047 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 2048 &val_chain, false);
a5606eee 2049
a5606eee
VP
2050 /* Avoid setting b->val if it's already set. The meaning of
2051 b->val is 'the last value' user saw, and we should update
2052 it only if we reported that last value to user. As it
9c06b0b4
TJB
2053 happens, the code that reports it updates b->val directly.
2054 We don't keep track of the memory value for masked
2055 watchpoints. */
c1fc2657 2056 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 2057 {
bb9d5f81 2058 if (b->val_bitsize != 0)
850645cf
TT
2059 v = extract_bitfield_from_watchpoint_value (b, v);
2060 b->val = release_value (v);
4c1d86d9 2061 b->val_valid = true;
fa4727a6 2062 }
a5606eee 2063
2d134ed3
PA
2064 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2065
a5606eee 2066 /* Look at each value on the value chain. */
a6535de1
TT
2067 gdb_assert (!val_chain.empty ());
2068 for (const value_ref_ptr &iter : val_chain)
a5606eee 2069 {
a6535de1
TT
2070 v = iter.get ();
2071
a5606eee
VP
2072 /* If it's a memory location, and GDB actually needed
2073 its contents to evaluate the expression, then we
fa4727a6
DJ
2074 must watch it. If the first value returned is
2075 still lazy, that means an error occurred reading it;
2076 watch it anyway in case it becomes readable. */
736355f2 2077 if (v->lval () == lval_memory
3ee3b270 2078 && (v == val_chain[0] || ! v->lazy ()))
a5606eee 2079 {
d0c97917 2080 struct type *vtype = check_typedef (v->type ());
7270d8f2 2081
a5606eee
VP
2082 /* We only watch structs and arrays if user asked
2083 for it explicitly, never if they just happen to
2084 appear in the middle of some value chain. */
fa4727a6 2085 if (v == result
78134374
SM
2086 || (vtype->code () != TYPE_CODE_STRUCT
2087 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
2088 {
2089 CORE_ADDR addr;
f486487f 2090 enum target_hw_bp_type type;
a5606eee 2091 struct bp_location *loc, **tmp;
bb9d5f81
PP
2092 int bitpos = 0, bitsize = 0;
2093
f49d5fa2 2094 if (v->bitsize () != 0)
bb9d5f81
PP
2095 {
2096 /* Extract the bit parameters out from the bitfield
2097 sub-expression. */
5011c493 2098 bitpos = v->bitpos ();
f49d5fa2 2099 bitsize = v->bitsize ();
bb9d5f81
PP
2100 }
2101 else if (v == result && b->val_bitsize != 0)
2102 {
2103 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2104 lvalue whose bit parameters are saved in the fields
2105 VAL_BITPOS and VAL_BITSIZE. */
2106 bitpos = b->val_bitpos;
2107 bitsize = b->val_bitsize;
2108 }
a5606eee 2109
9feb2d07 2110 addr = v->address ();
bb9d5f81
PP
2111 if (bitsize != 0)
2112 {
2113 /* Skip the bytes that don't contain the bitfield. */
2114 addr += bitpos / 8;
2115 }
2116
a5606eee 2117 type = hw_write;
c1fc2657 2118 if (b->type == bp_read_watchpoint)
a5606eee 2119 type = hw_read;
c1fc2657 2120 else if (b->type == bp_access_watchpoint)
a5606eee 2121 type = hw_access;
3a5c3e22 2122
ee89d0a4 2123 loc = b->allocate_location ();
c1fc2657 2124 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2125 ;
2126 *tmp = loc;
d0c97917 2127 loc->gdbarch = v->type ()->arch ();
6c95b8df
PA
2128
2129 loc->pspace = frame_pspace;
d88cb738
LM
2130 loc->address
2131 = gdbarch_remove_non_address_bits (loc->gdbarch, addr);
bb9d5f81
PP
2132
2133 if (bitsize != 0)
2134 {
2135 /* Just cover the bytes that make up the bitfield. */
2136 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2137 }
2138 else
d0c97917 2139 loc->length = v->type ()->length ();
bb9d5f81 2140
a5606eee
VP
2141 loc->watchpoint_type = type;
2142 }
2143 }
9fa40276
TJB
2144 }
2145
2146 /* Change the type of breakpoint between hardware assisted or
bc45f536
CL
2147 an ordinary watchpoint depending on the hardware support and
2148 free hardware slots. Recheck the number of free hardware slots
2149 as the value chain may have changed. */
9fa40276 2150 {
e09342b5 2151 int reg_cnt;
9fa40276 2152 enum bp_loc_type loc_type;
a5606eee 2153
a9634178 2154 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2155
2156 if (reg_cnt)
9fa40276
TJB
2157 {
2158 int i, target_resources_ok, other_type_used;
a1398e0c 2159 enum bptype type;
9fa40276 2160
a9634178
TJB
2161 /* Use an exact watchpoint when there's only one memory region to be
2162 watched, and only one debug register is needed to watch it. */
2163 b->exact = target_exact_watchpoints && reg_cnt == 1;
2164
9fa40276 2165 /* We need to determine how many resources are already
e09342b5
TJB
2166 used for all other hardware watchpoints plus this one
2167 to see if we still have enough resources to also fit
a1398e0c
PA
2168 this watchpoint in as well. */
2169
2170 /* If this is a software watchpoint, we try to turn it
2171 to a hardware one -- count resources as if B was of
2172 hardware watchpoint type. */
c1fc2657 2173 type = b->type;
a1398e0c
PA
2174 if (type == bp_watchpoint)
2175 type = bp_hardware_watchpoint;
2176
2177 /* This watchpoint may or may not have been placed on
2178 the list yet at this point (it won't be in the list
2179 if we're trying to create it for the first time,
2180 through watch_command), so always account for it
2181 manually. */
2182
2183 /* Count resources used by all watchpoints except B. */
c1fc2657 2184 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2185
2186 /* Add in the resources needed for B. */
c1fc2657 2187 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2188
2189 target_resources_ok
2190 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2191 if (target_resources_ok <= 0)
a9634178 2192 {
5a61e176 2193 bool sw_mode = b->works_in_software_mode ();
9c06b0b4
TJB
2194
2195 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2196 error (_("Target does not support this type of "
2197 "hardware watchpoint."));
9c06b0b4
TJB
2198 else if (target_resources_ok < 0 && !sw_mode)
2199 error (_("There are not enough available hardware "
2200 "resources for this watchpoint."));
a1398e0c
PA
2201
2202 /* Downgrade to software watchpoint. */
c1fc2657 2203 b->type = bp_watchpoint;
a1398e0c
PA
2204 }
2205 else
2206 {
2207 /* If this was a software watchpoint, we've just
2208 found we have enough resources to turn it to a
2209 hardware watchpoint. Otherwise, this is a
2210 nop. */
c1fc2657 2211 b->type = type;
a9634178 2212 }
9fa40276 2213 }
c47614fe 2214 else if (!b->works_in_software_mode ())
638aa5a1
AB
2215 {
2216 if (!can_use_hw_watchpoints)
2217 error (_("Can't set read/access watchpoint when "
2218 "hardware watchpoints are disabled."));
2219 else
2220 error (_("Expression cannot be implemented with "
2221 "read/access watchpoint."));
2222 }
9fa40276 2223 else
c1fc2657 2224 b->type = bp_watchpoint;
9fa40276 2225
6e14e441 2226 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
9fa40276 2227 : bp_loc_hardware_watchpoint);
40cb8ca5 2228 for (bp_location *bl : b->locations ())
9fa40276
TJB
2229 bl->loc_type = loc_type;
2230 }
2231
c7437ca6
PA
2232 /* If a software watchpoint is not watching any memory, then the
2233 above left it without any location set up. But,
2234 bpstat_stop_status requires a location to be able to report
2235 stops, so make sure there's at least a dummy one. */
c1fc2657 2236 if (b->type == bp_watchpoint && b->loc == NULL)
6e14e441 2237 add_dummy_location (b, frame_pspace);
a5606eee
VP
2238 }
2239 else if (!within_current_scope)
7270d8f2 2240 {
6cb06a8c 2241 gdb_printf (_("\
ac74f770
MS
2242Watchpoint %d deleted because the program has left the block\n\
2243in which its expression is valid.\n"),
6cb06a8c 2244 b->number);
d0fb5eae 2245 watchpoint_del_at_next_stop (b);
7270d8f2 2246 }
7270d8f2
OF
2247}
2248
d8de7963 2249/* Returns true iff breakpoint location should be
1e4d1764
YQ
2250 inserted in the inferior. We don't differentiate the type of BL's owner
2251 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2252 breakpoint_ops is not defined, because in insert_bp_location,
2253 tracepoint's insert_location will not be called. */
d8de7963
AB
2254
2255static bool
35df4500 2256should_be_inserted (struct bp_location *bl)
74960c60 2257{
35df4500 2258 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
d8de7963 2259 return false;
74960c60 2260
35df4500 2261 if (bl->owner->disposition == disp_del_at_next_stop)
d8de7963 2262 return false;
74960c60 2263
b5fa468f
TBA
2264 if (!bl->enabled || bl->disabled_by_cond
2265 || bl->shlib_disabled || bl->duplicate)
d8de7963 2266 return false;
74960c60 2267
f8eba3c6 2268 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
d8de7963 2269 return false;
f8eba3c6 2270
56710373
PA
2271 /* This is set for example, when we're attached to the parent of a
2272 vfork, and have detached from the child. The child is running
2273 free, and we expect it to do an exec or exit, at which point the
2274 OS makes the parent schedulable again (and the target reports
2275 that the vfork is done). Until the child is done with the shared
2276 memory region, do not insert breakpoints in the parent, otherwise
2277 the child could still trip on the parent's breakpoints. Since
2278 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2279 if (bl->pspace->breakpoints_not_allowed)
d8de7963 2280 return false;
56710373 2281
31e77af2 2282 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2283 location, except if the breakpoint is a single-step breakpoint,
2284 and the breakpoint's thread is the thread which is stepping past
2285 a breakpoint. */
31e77af2
PA
2286 if ((bl->loc_type == bp_loc_software_breakpoint
2287 || bl->loc_type == bp_loc_hardware_breakpoint)
2288 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2289 bl->address)
2290 /* The single-step breakpoint may be inserted at the location
2291 we're trying to step if the instruction branches to itself.
2292 However, the instruction won't be executed at all and it may
2293 break the semantics of the instruction, for example, the
2294 instruction is a conditional branch or updates some flags.
2295 We can't fix it unless GDB is able to emulate the instruction
2296 or switch to displaced stepping. */
2297 && !(bl->owner->type == bp_single_step
2298 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2299 {
1eb8556f
SM
2300 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2301 paddress (bl->gdbarch, bl->address));
d8de7963 2302 return false;
e558d7c1 2303 }
31e77af2 2304
963f9c80
PA
2305 /* Don't insert watchpoints if we're trying to step past the
2306 instruction that triggered one. */
2307 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2308 && stepping_past_nonsteppable_watchpoint ())
2309 {
1eb8556f 2310 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2311 "skipping watchpoint at %s:%d",
1eb8556f 2312 paddress (bl->gdbarch, bl->address), bl->length);
d8de7963 2313 return false;
963f9c80
PA
2314 }
2315
d8de7963 2316 return true;
74960c60
VP
2317}
2318
934709f0
PW
2319/* Same as should_be_inserted but does the check assuming
2320 that the location is not duplicated. */
2321
aaa141a0 2322static bool
934709f0
PW
2323unduplicated_should_be_inserted (struct bp_location *bl)
2324{
aaa141a0
AB
2325 scoped_restore restore_bl_duplicate
2326 = make_scoped_restore (&bl->duplicate, 0);
934709f0 2327
aaa141a0 2328 return should_be_inserted (bl);
934709f0
PW
2329}
2330
b775012e
LM
2331/* Parses a conditional described by an expression COND into an
2332 agent expression bytecode suitable for evaluation
2333 by the bytecode interpreter. Return NULL if there was
2334 any error during parsing. */
2335
833177a4 2336static agent_expr_up
b775012e
LM
2337parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2338{
833177a4 2339 if (cond == NULL)
b775012e
LM
2340 return NULL;
2341
833177a4
PA
2342 agent_expr_up aexpr;
2343
b775012e
LM
2344 /* We don't want to stop processing, so catch any errors
2345 that may show up. */
a70b8144 2346 try
b775012e 2347 {
036e657b 2348 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2349 }
2350
230d2906 2351 catch (const gdb_exception_error &ex)
b775012e
LM
2352 {
2353 /* If we got here, it means the condition could not be parsed to a valid
2354 bytecode expression and thus can't be evaluated on the target's side.
2355 It's no use iterating through the conditions. */
b775012e
LM
2356 }
2357
2358 /* We have a valid agent expression. */
2359 return aexpr;
2360}
2361
2362/* Based on location BL, create a list of breakpoint conditions to be
2363 passed on to the target. If we have duplicated locations with different
2364 conditions, we will add such conditions to the list. The idea is that the
2365 target will evaluate the list of conditions and will only notify GDB when
2366 one of them is true. */
2367
2368static void
2369build_target_condition_list (struct bp_location *bl)
2370{
d8de7963 2371 bool null_condition_or_parse_error = false;
b775012e 2372 int modified = bl->needs_update;
b775012e 2373
8b4f3082 2374 /* Release conditions left over from a previous insert. */
3cde5c42 2375 bl->target_info.conditions.clear ();
8b4f3082 2376
b775012e
LM
2377 /* This is only meaningful if the target is
2378 evaluating conditions and if the user has
2379 opted for condition evaluation on the target's
2380 side. */
2381 if (gdb_evaluates_breakpoint_condition_p ()
2382 || !target_supports_evaluation_of_breakpoint_conditions ())
2383 return;
2384
e0d9a270
SM
2385 auto loc_range = all_bp_locations_at_addr (bl->address);
2386
b775012e 2387 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2388 conditions or conditions that fail to parse to a valid agent
2389 expression bytecode. If any of these happen, then it's no use to
2390 send conditions to the target since this location will always
2391 trigger and generate a response back to GDB. Note we consider
2392 all locations at the same address irrespective of type, i.e.,
2393 even if the locations aren't considered duplicates (e.g.,
2394 software breakpoint and hardware breakpoint at the same
2395 address). */
e0d9a270 2396 for (bp_location *loc : loc_range)
b775012e 2397 {
b775012e
LM
2398 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2399 {
2400 if (modified)
2401 {
b775012e
LM
2402 /* Re-parse the conditions since something changed. In that
2403 case we already freed the condition bytecodes (see
2404 force_breakpoint_reinsertion). We just
2405 need to parse the condition to bytecodes again. */
833177a4
PA
2406 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2407 loc->cond.get ());
b775012e
LM
2408 }
2409
2410 /* If we have a NULL bytecode expression, it means something
2411 went wrong or we have a null condition expression. */
2412 if (!loc->cond_bytecode)
2413 {
d8de7963 2414 null_condition_or_parse_error = true;
b775012e
LM
2415 break;
2416 }
2417 }
2418 }
2419
2420 /* If any of these happened, it means we will have to evaluate the conditions
2421 for the location's address on gdb's side. It is no use keeping bytecodes
2422 for all the other duplicate locations, thus we free all of them here.
2423
2424 This is so we have a finer control over which locations' conditions are
2425 being evaluated by GDB or the remote stub. */
2426 if (null_condition_or_parse_error)
2427 {
e0d9a270 2428 for (bp_location *loc : loc_range)
b775012e 2429 {
b775012e
LM
2430 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2431 {
2432 /* Only go as far as the first NULL bytecode is
2433 located. */
2434 if (!loc->cond_bytecode)
2435 return;
2436
833177a4 2437 loc->cond_bytecode.reset ();
b775012e
LM
2438 }
2439 }
2440 }
2441
7f32a4d5
PA
2442 /* No NULL conditions or failed bytecode generation. Build a
2443 condition list for this location's address. If we have software
2444 and hardware locations at the same address, they aren't
2445 considered duplicates, but we still marge all the conditions
2446 anyway, as it's simpler, and doesn't really make a practical
2447 difference. */
e0d9a270
SM
2448 for (bp_location *loc : loc_range)
2449 if (loc->cond
2450 && is_breakpoint (loc->owner)
2451 && loc->pspace->num == bl->pspace->num
2452 && loc->owner->enable_state == bp_enabled
2453 && loc->enabled
2454 && !loc->disabled_by_cond)
2455 {
2456 /* Add the condition to the vector. This will be used later
2457 to send the conditions to the target. */
2458 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2459 }
b775012e
LM
2460
2461 return;
2462}
2463
d3ce09f5
SS
2464/* Parses a command described by string CMD into an agent expression
2465 bytecode suitable for evaluation by the bytecode interpreter.
2466 Return NULL if there was any error during parsing. */
2467
833177a4 2468static agent_expr_up
d3ce09f5
SS
2469parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2470{
bbc13ae3
KS
2471 const char *cmdrest;
2472 const char *format_start, *format_end;
d3ce09f5
SS
2473 struct gdbarch *gdbarch = get_current_arch ();
2474
833177a4 2475 if (cmd == NULL)
d3ce09f5
SS
2476 return NULL;
2477
2478 cmdrest = cmd;
2479
2480 if (*cmdrest == ',')
2481 ++cmdrest;
f1735a53 2482 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2483
2484 if (*cmdrest++ != '"')
2485 error (_("No format string following the location"));
2486
2487 format_start = cmdrest;
2488
8e481c3b 2489 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2490
2491 format_end = cmdrest;
2492
2493 if (*cmdrest++ != '"')
2494 error (_("Bad format string, non-terminated '\"'."));
2495
f1735a53 2496 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2497
2498 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2499 error (_("Invalid argument syntax"));
2500
2501 if (*cmdrest == ',')
2502 cmdrest++;
f1735a53 2503 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2504
2505 /* For each argument, make an expression. */
2506
8e481c3b 2507 std::vector<struct expression *> argvec;
d3ce09f5
SS
2508 while (*cmdrest != '\0')
2509 {
bbc13ae3 2510 const char *cmd1;
d3ce09f5
SS
2511
2512 cmd1 = cmdrest;
4d01a485 2513 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2514 argvec.push_back (expr.release ());
d3ce09f5
SS
2515 cmdrest = cmd1;
2516 if (*cmdrest == ',')
2517 ++cmdrest;
2518 }
2519
833177a4
PA
2520 agent_expr_up aexpr;
2521
d3ce09f5
SS
2522 /* We don't want to stop processing, so catch any errors
2523 that may show up. */
a70b8144 2524 try
d3ce09f5 2525 {
036e657b
JB
2526 aexpr = gen_printf (scope, gdbarch, 0, 0,
2527 format_start, format_end - format_start,
8e481c3b 2528 argvec.size (), argvec.data ());
d3ce09f5 2529 }
230d2906 2530 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2531 {
2532 /* If we got here, it means the command could not be parsed to a valid
2533 bytecode expression and thus can't be evaluated on the target's side.
2534 It's no use iterating through the other commands. */
d3ce09f5 2535 }
492d29ea 2536
d3ce09f5
SS
2537 /* We have a valid agent expression, return it. */
2538 return aexpr;
2539}
2540
2541/* Based on location BL, create a list of breakpoint commands to be
2542 passed on to the target. If we have duplicated locations with
2543 different commands, we will add any such to the list. */
2544
2545static void
2546build_target_command_list (struct bp_location *bl)
2547{
d8de7963 2548 bool null_command_or_parse_error = false;
d3ce09f5 2549 int modified = bl->needs_update;
d3ce09f5 2550
3cde5c42
PA
2551 /* Clear commands left over from a previous insert. */
2552 bl->target_info.tcommands.clear ();
8b4f3082 2553
41fac0cf 2554 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2555 return;
2556
41fac0cf
PA
2557 /* For now, limit to agent-style dprintf breakpoints. */
2558 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2559 return;
2560
e0d9a270
SM
2561 auto loc_range = all_bp_locations_at_addr (bl->address);
2562
7f32a4d5
PA
2563 /* For now, if we have any location at the same address that isn't a
2564 dprintf, don't install the target-side commands, as that would
2565 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2566 control. */
e0d9a270
SM
2567 for (bp_location *loc : loc_range)
2568 if (is_breakpoint (loc->owner)
2569 && loc->pspace->num == bl->pspace->num
2570 && loc->owner->type != bp_dprintf)
2571 return;
41fac0cf 2572
d3ce09f5
SS
2573 /* Do a first pass to check for locations with no assigned
2574 conditions or conditions that fail to parse to a valid agent expression
2575 bytecode. If any of these happen, then it's no use to send conditions
2576 to the target since this location will always trigger and generate a
2577 response back to GDB. */
e0d9a270 2578 for (bp_location *loc : loc_range)
d3ce09f5 2579 {
d3ce09f5
SS
2580 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2581 {
2582 if (modified)
2583 {
d3ce09f5
SS
2584 /* Re-parse the commands since something changed. In that
2585 case we already freed the command bytecodes (see
2586 force_breakpoint_reinsertion). We just
2587 need to parse the command to bytecodes again. */
833177a4
PA
2588 loc->cmd_bytecode
2589 = parse_cmd_to_aexpr (bl->address,
6f781ee3 2590 loc->owner->extra_string.get ());
d3ce09f5
SS
2591 }
2592
2593 /* If we have a NULL bytecode expression, it means something
2594 went wrong or we have a null command expression. */
2595 if (!loc->cmd_bytecode)
2596 {
d8de7963 2597 null_command_or_parse_error = true;
d3ce09f5
SS
2598 break;
2599 }
2600 }
2601 }
2602
2603 /* If anything failed, then we're not doing target-side commands,
2604 and so clean up. */
2605 if (null_command_or_parse_error)
2606 {
e0d9a270
SM
2607 for (bp_location *loc : loc_range)
2608 if (is_breakpoint (loc->owner)
2609 && loc->pspace->num == bl->pspace->num)
2610 {
2611 /* Only go as far as the first NULL bytecode is
2612 located. */
2613 if (loc->cmd_bytecode == NULL)
2614 return;
d3ce09f5 2615
e0d9a270
SM
2616 loc->cmd_bytecode.reset ();
2617 }
d3ce09f5
SS
2618 }
2619
7f32a4d5
PA
2620 /* No NULL commands or failed bytecode generation. Build a command
2621 list for all duplicate locations at this location's address.
2622 Note that here we must care for whether the breakpoint location
2623 types are considered duplicates, otherwise, say, if we have a
2624 software and hardware location at the same address, the target
2625 could end up running the commands twice. For the moment, we only
2626 support targets-side commands with dprintf, but it doesn't hurt
2627 to be pedantically correct in case that changes. */
e0d9a270
SM
2628 for (bp_location *loc : loc_range)
2629 if (breakpoint_locations_match (bl, loc)
2630 && loc->owner->extra_string
2631 && is_breakpoint (loc->owner)
2632 && loc->pspace->num == bl->pspace->num
2633 && loc->owner->enable_state == bp_enabled
2634 && loc->enabled
2635 && !loc->disabled_by_cond)
2636 {
2637 /* Add the command to the vector. This will be used later
2638 to send the commands to the target. */
2639 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2640 }
d3ce09f5
SS
2641
2642 bl->target_info.persist = 0;
2643 /* Maybe flag this location as persistent. */
2644 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2645 bl->target_info.persist = 1;
2646}
2647
833b7ab5
YQ
2648/* Return the kind of breakpoint on address *ADDR. Get the kind
2649 of breakpoint according to ADDR except single-step breakpoint.
2650 Get the kind of single-step breakpoint according to the current
2651 registers state. */
cd6c3b4f
YQ
2652
2653static int
b2ff9ed3 2654breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
cd6c3b4f 2655{
833b7ab5
YQ
2656 if (bl->owner->type == bp_single_step)
2657 {
2658 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2659 struct regcache *regcache;
2660
00431a78 2661 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2662
2663 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2664 regcache, addr);
2665 }
2666 else
2667 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2668}
2669
47e2c30a
TV
2670/* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2671 E is either the currently handled exception, or a copy, or a sliced copy,
2672 so we can't rethrow that one, but we can use it to inspect the properties
2673 of the currently handled exception. */
2674
2675static void
2676rethrow_on_target_close_error (const gdb_exception &e)
2677{
2678 if (e.reason == 0)
2679 return;
2680 /* Can't set the breakpoint. */
2681
2682 if (e.error != TARGET_CLOSE_ERROR)
2683 return;
2684
2685 /* If the target has closed then it will have deleted any breakpoints
2686 inserted within the target inferior, as a result any further attempts
2687 to interact with the breakpoint objects is not possible. Just rethrow
2688 the error. Don't use e to rethrow, to prevent object slicing of the
2689 exception. */
2690 throw;
2691}
b2829fcf 2692
35df4500
TJB
2693/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2694 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2695 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2696 Returns 0 for success, 1 if the bp_location type is not supported or
2697 -1 for failure.
879bfdc2 2698
4a64f543
MS
2699 NOTE drow/2003-09-09: This routine could be broken down to an
2700 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2701static int
35df4500 2702insert_bp_location (struct bp_location *bl,
26bb91f3 2703 struct ui_file *tmp_error_stream,
3fbb6ffa 2704 int *disabled_breaks,
dd61ec5c
MW
2705 int *hw_breakpoint_error,
2706 int *hw_bp_error_explained_already)
879bfdc2 2707{
cc06b668 2708 gdb_exception bp_excpt;
879bfdc2 2709
b775012e 2710 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2711 return 0;
2712
35c63cd8
JB
2713 /* Note we don't initialize bl->target_info, as that wipes out
2714 the breakpoint location's shadow_contents if the breakpoint
2715 is still inserted at that location. This in turn breaks
2716 target_read_memory which depends on these buffers when
2717 a memory read is requested at the breakpoint location:
2718 Once the target_info has been wiped, we fail to see that
2719 we have a breakpoint inserted at that address and thus
2720 read the breakpoint instead of returning the data saved in
2721 the breakpoint location's shadow contents. */
0d5ed153 2722 bl->target_info.reqstd_address = bl->address;
35df4500 2723 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2724 bl->target_info.length = bl->length;
8181d85f 2725
b775012e
LM
2726 /* When working with target-side conditions, we must pass all the conditions
2727 for the same breakpoint address down to the target since GDB will not
2728 insert those locations. With a list of breakpoint conditions, the target
2729 can decide when to stop and notify GDB. */
2730
2731 if (is_breakpoint (bl->owner))
2732 {
2733 build_target_condition_list (bl);
d3ce09f5
SS
2734 build_target_command_list (bl);
2735 /* Reset the modification marker. */
b775012e
LM
2736 bl->needs_update = 0;
2737 }
2738
7f32a4d5
PA
2739 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2740 set at a read-only address, then a breakpoint location will have
2741 been changed to hardware breakpoint before we get here. If it is
2742 "off" however, error out before actually trying to insert the
2743 breakpoint, with a nicer error message. */
35df4500 2744 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2745 && !automatic_hardware_breakpoints)
879bfdc2 2746 {
7f32a4d5 2747 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2748
7f32a4d5
PA
2749 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2750 {
6cb06a8c
TT
2751 gdb_printf (tmp_error_stream,
2752 _("Cannot insert breakpoint %d.\n"
2753 "Cannot set software breakpoint "
2754 "at read-only address %s\n"),
2755 bl->owner->number,
2756 paddress (bl->gdbarch, bl->address));
7f32a4d5 2757 return 1;
765dc015 2758 }
7f32a4d5
PA
2759 }
2760
2761 if (bl->loc_type == bp_loc_software_breakpoint
2762 || bl->loc_type == bp_loc_hardware_breakpoint)
2763 {
879bfdc2
DJ
2764 /* First check to see if we have to handle an overlay. */
2765 if (overlay_debugging == ovly_off
35df4500
TJB
2766 || bl->section == NULL
2767 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2768 {
2769 /* No overlay handling: just set the breakpoint. */
a70b8144 2770 try
dd61ec5c 2771 {
0000e5cc
PA
2772 int val;
2773
c47614fe 2774 val = bl->owner->insert_location (bl);
0000e5cc 2775 if (val)
688fca4f 2776 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2777 }
94aeb44b 2778 catch (gdb_exception &e)
dd61ec5c 2779 {
47e2c30a 2780 rethrow_on_target_close_error (e);
94aeb44b 2781 bp_excpt = std::move (e);
dd61ec5c 2782 }
879bfdc2
DJ
2783 }
2784 else
2785 {
4a64f543 2786 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2787 Shall we set a breakpoint at the LMA? */
2788 if (!overlay_events_enabled)
2789 {
2790 /* Yes -- overlay event support is not active,
2791 so we must try to set a breakpoint at the LMA.
2792 This will not work for a hardware breakpoint. */
35df4500 2793 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2794 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2795 bl->owner->number);
879bfdc2
DJ
2796 else
2797 {
35df4500
TJB
2798 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2799 bl->section);
879bfdc2 2800 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2801 bl->overlay_target_info = bl->target_info;
0d5ed153 2802 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2803
2804 /* No overlay handling: just set the breakpoint. */
a70b8144 2805 try
0000e5cc
PA
2806 {
2807 int val;
2808
579c6ad9 2809 bl->overlay_target_info.kind
cd6c3b4f
YQ
2810 = breakpoint_kind (bl, &addr);
2811 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2812 val = target_insert_breakpoint (bl->gdbarch,
2813 &bl->overlay_target_info);
2814 if (val)
688fca4f
PA
2815 bp_excpt
2816 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2817 }
94aeb44b 2818 catch (gdb_exception &e)
0000e5cc 2819 {
47e2c30a 2820 rethrow_on_target_close_error (e);
94aeb44b 2821 bp_excpt = std::move (e);
0000e5cc
PA
2822 }
2823
688fca4f 2824 if (bp_excpt.reason != 0)
6cb06a8c
TT
2825 gdb_printf (tmp_error_stream,
2826 "Overlay breakpoint %d "
2827 "failed: in ROM?\n",
2828 bl->owner->number);
879bfdc2
DJ
2829 }
2830 }
2831 /* Shall we set a breakpoint at the VMA? */
35df4500 2832 if (section_is_mapped (bl->section))
879bfdc2
DJ
2833 {
2834 /* Yes. This overlay section is mapped into memory. */
a70b8144 2835 try
dda83cd7 2836 {
0000e5cc
PA
2837 int val;
2838
c47614fe 2839 val = bl->owner->insert_location (bl);
0000e5cc 2840 if (val)
688fca4f 2841 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2842 }
94aeb44b 2843 catch (gdb_exception &e)
dda83cd7 2844 {
47e2c30a 2845 rethrow_on_target_close_error (e);
94aeb44b 2846 bp_excpt = std::move (e);
dda83cd7 2847 }
879bfdc2
DJ
2848 }
2849 else
2850 {
2851 /* No. This breakpoint will not be inserted.
2852 No error, but do not mark the bp as 'inserted'. */
2853 return 0;
2854 }
2855 }
2856
688fca4f 2857 if (bp_excpt.reason != 0)
879bfdc2
DJ
2858 {
2859 /* Can't set the breakpoint. */
80dc83fd
AB
2860 gdb_assert (bl->owner != nullptr);
2861
0000e5cc
PA
2862 /* In some cases, we might not be able to insert a
2863 breakpoint in a shared library that has already been
2864 removed, but we have not yet processed the shlib unload
2865 event. Unfortunately, some targets that implement
076855f9
PA
2866 breakpoint insertion themselves can't tell why the
2867 breakpoint insertion failed (e.g., the remote target
2868 doesn't define error codes), so we must treat generic
2869 errors as memory errors. */
688fca4f
PA
2870 if (bp_excpt.reason == RETURN_ERROR
2871 && (bp_excpt.error == GENERIC_ERROR
2872 || bp_excpt.error == MEMORY_ERROR)
076855f9 2873 && bl->loc_type == bp_loc_software_breakpoint
08351840 2874 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2875 || shared_objfile_contains_address_p (bl->pspace,
2876 bl->address)))
879bfdc2 2877 {
4a64f543 2878 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2879 bl->shlib_disabled = 1;
76727919 2880 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2881 if (!*disabled_breaks)
2882 {
6cb06a8c
TT
2883 gdb_printf (tmp_error_stream,
2884 "Cannot insert breakpoint %d.\n",
2885 bl->owner->number);
2886 gdb_printf (tmp_error_stream,
2887 "Temporarily disabling shared "
2888 "library breakpoints:\n");
3fbb6ffa
TJB
2889 }
2890 *disabled_breaks = 1;
6cb06a8c
TT
2891 gdb_printf (tmp_error_stream,
2892 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2893 return 0;
879bfdc2
DJ
2894 }
2895 else
879bfdc2 2896 {
35df4500 2897 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2898 {
0000e5cc 2899 *hw_breakpoint_error = 1;
688fca4f 2900 *hw_bp_error_explained_already = bp_excpt.message != NULL;
6cb06a8c
TT
2901 gdb_printf (tmp_error_stream,
2902 "Cannot insert hardware breakpoint %d%s",
2903 bl->owner->number,
2904 bp_excpt.message ? ":" : ".\n");
dda83cd7 2905 if (bp_excpt.message != NULL)
6cb06a8c
TT
2906 gdb_printf (tmp_error_stream, "%s.\n",
2907 bp_excpt.what ());
879bfdc2
DJ
2908 }
2909 else
2910 {
688fca4f 2911 if (bp_excpt.message == NULL)
0000e5cc 2912 {
1ccbe998 2913 std::string message
0000e5cc
PA
2914 = memory_error_message (TARGET_XFER_E_IO,
2915 bl->gdbarch, bl->address);
0000e5cc 2916
6cb06a8c
TT
2917 gdb_printf (tmp_error_stream,
2918 "Cannot insert breakpoint %d.\n"
2919 "%s\n",
2920 bl->owner->number, message.c_str ());
0000e5cc
PA
2921 }
2922 else
2923 {
6cb06a8c
TT
2924 gdb_printf (tmp_error_stream,
2925 "Cannot insert breakpoint %d: %s\n",
2926 bl->owner->number,
2927 bp_excpt.what ());
0000e5cc 2928 }
879bfdc2 2929 }
0000e5cc 2930 return 1;
879bfdc2
DJ
2931
2932 }
2933 }
2934 else
35df4500 2935 bl->inserted = 1;
879bfdc2 2936
0000e5cc 2937 return 0;
879bfdc2
DJ
2938 }
2939
35df4500 2940 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2941 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2942 watchpoints. It's not clear that it's necessary... */
35df4500 2943 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2944 {
0000e5cc
PA
2945 int val;
2946
c47614fe 2947 val = bl->owner->insert_location (bl);
85d721b8
PA
2948
2949 /* If trying to set a read-watchpoint, and it turns out it's not
2950 supported, try emulating one with an access watchpoint. */
35df4500 2951 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2952 {
85d721b8
PA
2953 /* But don't try to insert it, if there's already another
2954 hw_access location that would be considered a duplicate
2955 of this one. */
48d7020b 2956 for (bp_location *loc : all_bp_locations ())
35df4500 2957 if (loc != bl
85d721b8 2958 && loc->watchpoint_type == hw_access
35df4500 2959 && watchpoint_locations_match (bl, loc))
85d721b8 2960 {
35df4500
TJB
2961 bl->duplicate = 1;
2962 bl->inserted = 1;
2963 bl->target_info = loc->target_info;
2964 bl->watchpoint_type = hw_access;
85d721b8
PA
2965 val = 0;
2966 break;
2967 }
2968
2969 if (val == 1)
2970 {
77b06cd7 2971 bl->watchpoint_type = hw_access;
c47614fe 2972 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2973
2974 if (val)
2975 /* Back to the original value. */
2976 bl->watchpoint_type = hw_read;
85d721b8
PA
2977 }
2978 }
2979
35df4500 2980 bl->inserted = (val == 0);
879bfdc2
DJ
2981 }
2982
35df4500 2983 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2984 {
0000e5cc
PA
2985 int val;
2986
c47614fe 2987 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2988 if (val)
2989 {
2990 bl->owner->enable_state = bp_disabled;
2991
2992 if (val == 1)
2993 warning (_("\
2994Error inserting catchpoint %d: Your system does not support this type\n\
2995of catchpoint."), bl->owner->number);
2996 else
2997 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2998 }
2999
3000 bl->inserted = (val == 0);
1640b821
DJ
3001
3002 /* We've already printed an error message if there was a problem
3003 inserting this catchpoint, and we've disabled the catchpoint,
3004 so just return success. */
3005 return 0;
879bfdc2
DJ
3006 }
3007
3008 return 0;
3009}
3010
6c95b8df
PA
3011/* This function is called when program space PSPACE is about to be
3012 deleted. It takes care of updating breakpoints to not reference
3013 PSPACE anymore. */
3014
3015void
3016breakpoint_program_space_exit (struct program_space *pspace)
3017{
6c95b8df 3018 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
3019 for (breakpoint *b : all_breakpoints_safe ())
3020 if (b->pspace == pspace)
3021 delete_breakpoint (b);
6c95b8df
PA
3022
3023 /* Breakpoints set through other program spaces could have locations
3024 bound to PSPACE as well. Remove those. */
48d7020b 3025 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
3026 {
3027 struct bp_location *tmp;
3028
3029 if (loc->pspace == pspace)
3030 {
2bdf28a0 3031 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3032 if (loc->owner->loc == loc)
3033 loc->owner->loc = loc->next;
3034 else
3035 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3036 if (tmp->next == loc)
3037 {
3038 tmp->next = loc->next;
3039 break;
3040 }
3041 }
3042 }
3043
3044 /* Now update the global location list to permanently delete the
3045 removed locations above. */
44702360 3046 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3047}
3048
74960c60
VP
3049/* Make sure all breakpoints are inserted in inferior.
3050 Throws exception on any error.
3051 A breakpoint that is already inserted won't be inserted
3052 again, so calling this function twice is safe. */
3053void
3054insert_breakpoints (void)
3055{
43892fdf 3056 for (breakpoint *bpt : all_breakpoints ())
74960c60 3057 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3058 {
3059 struct watchpoint *w = (struct watchpoint *) bpt;
3060
d8de7963 3061 update_watchpoint (w, false /* don't reparse. */);
3a5c3e22 3062 }
74960c60 3063
04086b45
PA
3064 /* Updating watchpoints creates new locations, so update the global
3065 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3066 ignore breakpoints_always_inserted_mode. Also,
3067 update_global_location_list tries to "upgrade" software
3068 breakpoints to hardware breakpoints to handle "set breakpoint
3069 auto-hw", so we need to call it even if we don't have new
3070 locations. */
04086b45 3071 update_global_location_list (UGLL_INSERT);
74960c60
VP
3072}
3073
b775012e
LM
3074/* This is used when we need to synch breakpoint conditions between GDB and the
3075 target. It is the case with deleting and disabling of breakpoints when using
3076 always-inserted mode. */
3077
3078static void
3079update_inserted_breakpoint_locations (void)
3080{
b775012e
LM
3081 int error_flag = 0;
3082 int val = 0;
3083 int disabled_breaks = 0;
3084 int hw_breakpoint_error = 0;
dd61ec5c 3085 int hw_bp_details_reported = 0;
b775012e 3086
d7e74731 3087 string_file tmp_error_stream;
b775012e
LM
3088
3089 /* Explicitly mark the warning -- this will only be printed if
3090 there was an error. */
d7e74731 3091 tmp_error_stream.puts ("Warning:\n");
b775012e 3092
5ed8105e 3093 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3094
48d7020b 3095 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3096 {
3097 /* We only want to update software breakpoints and hardware
3098 breakpoints. */
3099 if (!is_breakpoint (bl->owner))
3100 continue;
3101
3102 /* We only want to update locations that are already inserted
3103 and need updating. This is to avoid unwanted insertion during
3104 deletion of breakpoints. */
4daf1902 3105 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3106 continue;
3107
3108 switch_to_program_space_and_thread (bl->pspace);
3109
3110 /* For targets that support global breakpoints, there's no need
3111 to select an inferior to insert breakpoint to. In fact, even
3112 if we aren't attached to any process yet, we should still
3113 insert breakpoints. */
f5656ead 3114 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3115 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3116 continue;
3117
d7e74731 3118 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3119 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3120 if (val)
3121 error_flag = val;
3122 }
3123
3124 if (error_flag)
3125 {
223ffa71 3126 target_terminal::ours_for_output ();
b775012e
LM
3127 error_stream (tmp_error_stream);
3128 }
b775012e
LM
3129}
3130
c30eee59 3131/* Used when starting or continuing the program. */
c906108c 3132
74960c60
VP
3133static void
3134insert_breakpoint_locations (void)
c906108c 3135{
eacd795a 3136 int error_flag = 0;
c906108c 3137 int val = 0;
3fbb6ffa 3138 int disabled_breaks = 0;
81d0cc19 3139 int hw_breakpoint_error = 0;
dd61ec5c 3140 int hw_bp_error_explained_already = 0;
c906108c 3141
d7e74731
PA
3142 string_file tmp_error_stream;
3143
81d0cc19
GS
3144 /* Explicitly mark the warning -- this will only be printed if
3145 there was an error. */
d7e74731 3146 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3147
5ed8105e 3148 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3149
48d7020b 3150 for (bp_location *bl : all_bp_locations ())
879bfdc2 3151 {
b775012e 3152 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3153 continue;
3154
4a64f543
MS
3155 /* There is no point inserting thread-specific breakpoints if
3156 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3157 has BL->OWNER always non-NULL. */
35df4500 3158 if (bl->owner->thread != -1
5d5658a1 3159 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3160 continue;
3161
35df4500 3162 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3163
3164 /* For targets that support global breakpoints, there's no need
3165 to select an inferior to insert breakpoint to. In fact, even
3166 if we aren't attached to any process yet, we should still
3167 insert breakpoints. */
f5656ead 3168 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3169 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3170 continue;
3171
d7e74731 3172 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3173 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3174 if (val)
eacd795a 3175 error_flag = val;
879bfdc2 3176 }
c906108c 3177
4a64f543
MS
3178 /* If we failed to insert all locations of a watchpoint, remove
3179 them, as half-inserted watchpoint is of limited use. */
43892fdf 3180 for (breakpoint *bpt : all_breakpoints ())
a5606eee 3181 {
d8de7963 3182 bool some_failed = false;
a5606eee
VP
3183
3184 if (!is_hardware_watchpoint (bpt))
3185 continue;
3186
d6b74ac4 3187 if (!breakpoint_enabled (bpt))
a5606eee 3188 continue;
74960c60
VP
3189
3190 if (bpt->disposition == disp_del_at_next_stop)
3191 continue;
a5606eee 3192
40cb8ca5 3193 for (bp_location *loc : bpt->locations ())
56710373 3194 if (!loc->inserted && should_be_inserted (loc))
a5606eee 3195 {
d8de7963 3196 some_failed = true;
a5606eee
VP
3197 break;
3198 }
40cb8ca5 3199
a5606eee
VP
3200 if (some_failed)
3201 {
40cb8ca5 3202 for (bp_location *loc : bpt->locations ())
a5606eee 3203 if (loc->inserted)
834c0d03 3204 remove_breakpoint (loc);
a5606eee
VP
3205
3206 hw_breakpoint_error = 1;
d7e74731
PA
3207 tmp_error_stream.printf ("Could not insert "
3208 "hardware watchpoint %d.\n",
3209 bpt->number);
eacd795a 3210 error_flag = -1;
a5606eee
VP
3211 }
3212 }
3213
eacd795a 3214 if (error_flag)
81d0cc19
GS
3215 {
3216 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3217 message about possibly exhausted resources. */
dd61ec5c 3218 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3219 {
d7e74731 3220 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3221You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3222 }
223ffa71 3223 target_terminal::ours_for_output ();
81d0cc19
GS
3224 error_stream (tmp_error_stream);
3225 }
c906108c
SS
3226}
3227
c30eee59
TJB
3228/* Used when the program stops.
3229 Returns zero if successful, or non-zero if there was a problem
3230 removing a breakpoint location. */
3231
c906108c 3232int
fba45db2 3233remove_breakpoints (void)
c906108c 3234{
3a1bae8e 3235 int val = 0;
c906108c 3236
48d7020b 3237 for (bp_location *bl : all_bp_locations ())
1e4d1764 3238 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3239 val |= remove_breakpoint (bl);
01add95b 3240
3a1bae8e 3241 return val;
c906108c
SS
3242}
3243
49fa26b0
PA
3244/* When a thread exits, remove breakpoints that are related to
3245 that thread. */
3246
3247static void
3248remove_threaded_breakpoints (struct thread_info *tp, int silent)
3249{
1428b37a 3250 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3251 {
5d5658a1 3252 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3253 {
3254 b->disposition = disp_del_at_next_stop;
3255
6cb06a8c 3256 gdb_printf (_("\
43792cf0 3257Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
6cb06a8c 3258 b->number, print_thread_id (tp));
49fa26b0
PA
3259
3260 /* Hide it from the user. */
3261 b->number = 0;
3262 }
3263 }
3264}
3265
f3869b1a 3266/* See breakpoint.h. */
6c95b8df 3267
f3869b1a 3268void
00431a78 3269remove_breakpoints_inf (inferior *inf)
6c95b8df 3270{
6c95b8df 3271 int val;
6c95b8df 3272
48d7020b 3273 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3274 {
3275 if (bl->pspace != inf->pspace)
3276 continue;
6c95b8df 3277
01add95b
SM
3278 if (bl->inserted && !bl->target_info.persist)
3279 {
3280 val = remove_breakpoint (bl);
3281 if (val != 0)
3282 return;
3283 }
3284 }
6c95b8df
PA
3285}
3286
e58b0e63
PA
3287static int internal_breakpoint_number = -1;
3288
84f4c1fe
PM
3289/* Set the breakpoint number of B, depending on the value of INTERNAL.
3290 If INTERNAL is non-zero, the breakpoint number will be populated
3291 from internal_breakpoint_number and that variable decremented.
e5dd4106 3292 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3293 breakpoint_count and that value incremented. Internal breakpoints
3294 do not set the internal var bpnum. */
3295static void
3296set_breakpoint_number (int internal, struct breakpoint *b)
3297{
3298 if (internal)
3299 b->number = internal_breakpoint_number--;
3300 else
3301 {
3302 set_breakpoint_count (breakpoint_count + 1);
3303 b->number = breakpoint_count;
3304 }
3305}
3306
829b6b37
TV
3307/* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3308
e62c965a 3309static struct breakpoint *
a6d9a66e 3310create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3311 CORE_ADDR address, enum bptype type)
e62c965a 3312{
752a2291
PA
3313 std::unique_ptr<internal_breakpoint> b
3314 (new internal_breakpoint (gdbarch, type, address));
e62c965a 3315
e62c965a 3316 b->number = internal_breakpoint_number--;
e62c965a 3317
752a2291 3318 return add_to_breakpoint_chain (std::move (b));
e62c965a
PP
3319}
3320
829b6b37
TV
3321/* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3322 GDBARCH. */
3323
3324static struct breakpoint *
3325create_internal_breakpoint (struct gdbarch *gdbarch,
3326 struct bound_minimal_symbol &msym, enum bptype type)
3327{
3328 CORE_ADDR address;
3329
3330 address = msym.value_address ();
3331
3332 address = gdbarch_convert_from_func_ptr_addr
3333 (gdbarch, address, current_inferior ()->top_target ());
3334
3335 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3336 related to addresses in $pc. We're getting the address from the
3337 minimal symbol table. */
3338
3339 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3340 with elsewhere? Needs testing on vax. */
3341
3342 if (gdbarch_skip_entrypoint_p (gdbarch))
3343 address = gdbarch_skip_entrypoint (gdbarch, address);
3344
3345 return create_internal_breakpoint (gdbarch, address, type);
3346}
3347
17450429
PP
3348static const char *const longjmp_names[] =
3349 {
3350 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3351 };
3352#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3353
3354/* Per-objfile data private to breakpoint.c. */
3355struct breakpoint_objfile_data
3356{
3357 /* Minimal symbol for "_ovly_debug_event" (if any). */
f6b3ad54 3358 struct bound_minimal_symbol overlay_msym;
17450429
PP
3359
3360 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
f6b3ad54 3361 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3362
28106bc2 3363 /* True if we have looked for longjmp probes. */
43dce439 3364 int longjmp_searched = 0;
28106bc2 3365
45461e0d
SM
3366 /* SystemTap probe points for longjmp (if any). These are non-owning
3367 references. */
3368 std::vector<probe *> longjmp_probes;
28106bc2 3369
17450429 3370 /* Minimal symbol for "std::terminate()" (if any). */
f6b3ad54 3371 struct bound_minimal_symbol terminate_msym;
17450429
PP
3372
3373 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
f6b3ad54 3374 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3375
3376 /* True if we have looked for exception probes. */
43dce439 3377 int exception_searched = 0;
28106bc2 3378
45461e0d
SM
3379 /* SystemTap probe points for unwinding (if any). These are non-owning
3380 references. */
3381 std::vector<probe *> exception_probes;
17450429
PP
3382};
3383
08b8a139 3384static const registry<objfile>::key<breakpoint_objfile_data>
51d3063a 3385 breakpoint_objfile_key;
17450429
PP
3386
3387/* Minimal symbol not found sentinel. */
3388static struct minimal_symbol msym_not_found;
3389
3390/* Returns TRUE if MSYM point to the "not found" sentinel. */
3391
d8de7963 3392static bool
17450429
PP
3393msym_not_found_p (const struct minimal_symbol *msym)
3394{
3395 return msym == &msym_not_found;
3396}
3397
3398/* Return per-objfile data needed by breakpoint.c.
3399 Allocate the data if necessary. */
3400
3401static struct breakpoint_objfile_data *
3402get_breakpoint_objfile_data (struct objfile *objfile)
3403{
3404 struct breakpoint_objfile_data *bp_objfile_data;
3405
51d3063a 3406 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3407 if (bp_objfile_data == NULL)
51d3063a 3408 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3409 return bp_objfile_data;
3410}
3411
e62c965a 3412static void
af02033e 3413create_overlay_event_breakpoint (void)
e62c965a 3414{
af02033e 3415 const char *const func_name = "_ovly_debug_event";
e62c965a 3416
2030c079 3417 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3418 {
3419 struct breakpoint *b;
17450429
PP
3420 struct breakpoint_objfile_data *bp_objfile_data;
3421 CORE_ADDR addr;
69de3c6a 3422
17450429
PP
3423 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3424
3b7344d5 3425 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3426 continue;
3427
3b7344d5 3428 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3429 {
3b7344d5 3430 struct bound_minimal_symbol m;
17450429
PP
3431
3432 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3433 if (m.minsym == NULL)
17450429
PP
3434 {
3435 /* Avoid future lookups in this objfile. */
3b7344d5 3436 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3437 continue;
3438 }
3439 bp_objfile_data->overlay_msym = m;
3440 }
e62c965a 3441
4aeddc50 3442 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3443 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3444 bp_overlay_event);
40d97ee2 3445 b->locspec = new_explicit_location_spec_function (func_name);
e62c965a 3446
69de3c6a 3447 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3448 {
3449 b->enable_state = bp_enabled;
3450 overlay_events_enabled = 1;
3451 }
69de3c6a
PP
3452 else
3453 {
dda83cd7
SM
3454 b->enable_state = bp_disabled;
3455 overlay_events_enabled = 0;
69de3c6a 3456 }
e62c965a 3457 }
e62c965a
PP
3458}
3459
2a7f6487
TV
3460/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3461 true if a breakpoint was installed. */
3462
3463static bool
3464create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3465{
2a7f6487
TV
3466 struct gdbarch *gdbarch = objfile->arch ();
3467 struct breakpoint_objfile_data *bp_objfile_data
3468 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3469
2a7f6487 3470 if (!bp_objfile_data->longjmp_searched)
94c93c35 3471 {
2a7f6487
TV
3472 std::vector<probe *> ret
3473 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3474
2a7f6487 3475 if (!ret.empty ())
94c93c35 3476 {
2a7f6487
TV
3477 /* We are only interested in checking one element. */
3478 probe *p = ret[0];
0fd8e87f 3479
2a7f6487
TV
3480 if (!p->can_evaluate_arguments ())
3481 {
3482 /* We cannot use the probe interface here,
3483 because it does not know how to evaluate
3484 arguments. */
3485 ret.clear ();
3486 }
3487 }
3488 bp_objfile_data->longjmp_probes = ret;
3489 bp_objfile_data->longjmp_searched = 1;
3490 }
0fd8e87f 3491
2a7f6487
TV
3492 if (bp_objfile_data->longjmp_probes.empty ())
3493 return false;
17450429 3494
2a7f6487
TV
3495 for (probe *p : bp_objfile_data->longjmp_probes)
3496 {
3497 struct breakpoint *b;
25f9533e 3498
2a7f6487
TV
3499 b = create_internal_breakpoint (gdbarch,
3500 p->get_relocated_address (objfile),
9efa3c7f 3501 bp_longjmp_master);
264f9890 3502 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
2a7f6487
TV
3503 b->enable_state = bp_disabled;
3504 }
aed57c53 3505
2a7f6487
TV
3506 return true;
3507}
25f9533e 3508
2a7f6487
TV
3509/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3510 Return true if at least one breakpoint was installed. */
3511
3512static bool
3513create_longjmp_master_breakpoint_names (objfile *objfile)
3514{
3515 struct gdbarch *gdbarch = objfile->arch ();
3516 if (!gdbarch_get_longjmp_target_p (gdbarch))
3517 return false;
3518
3519 struct breakpoint_objfile_data *bp_objfile_data
3520 = get_breakpoint_objfile_data (objfile);
3521 unsigned int installed_bp = 0;
3522
3523 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3524 {
3525 struct breakpoint *b;
3526 const char *func_name;
3527 CORE_ADDR addr;
2a7f6487
TV
3528
3529 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3530 continue;
28106bc2 3531
2a7f6487
TV
3532 func_name = longjmp_names[i];
3533 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3534 {
3535 struct bound_minimal_symbol m;
3536
3537 m = lookup_minimal_symbol_text (func_name, objfile);
3538 if (m.minsym == NULL)
3539 {
3540 /* Prevent future lookups in this objfile. */
3541 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3542 continue;
3543 }
2a7f6487
TV
3544 bp_objfile_data->longjmp_msym[i] = m;
3545 }
28106bc2 3546
4aeddc50 3547 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3548 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
40d97ee2 3549 b->locspec = new_explicit_location_spec_function (func_name);
2a7f6487
TV
3550 b->enable_state = bp_disabled;
3551 installed_bp++;
3552 }
28106bc2 3553
2a7f6487
TV
3554 return installed_bp > 0;
3555}
0fd8e87f 3556
2a7f6487 3557/* Create a master longjmp breakpoint. */
17450429 3558
2a7f6487
TV
3559static void
3560create_longjmp_master_breakpoint (void)
3561{
3562 scoped_restore_current_program_space restore_pspace;
aed57c53 3563
2a7f6487
TV
3564 for (struct program_space *pspace : program_spaces)
3565 {
3566 set_current_program_space (pspace);
17450429 3567
2a7f6487
TV
3568 for (objfile *obj : current_program_space->objfiles ())
3569 {
3570 /* Skip separate debug object, it's handled in the loop below. */
3571 if (obj->separate_debug_objfile_backlink != nullptr)
3572 continue;
3573
3574 /* Try a probe kind breakpoint on main objfile. */
3575 if (create_longjmp_master_breakpoint_probe (obj))
3576 continue;
3577
3578 /* Try longjmp_names kind breakpoints on main and separate_debug
3579 objfiles. */
3580 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3581 if (create_longjmp_master_breakpoint_names (debug_objfile))
3582 break;
94c93c35
TT
3583 }
3584 }
0fd8e87f
UW
3585}
3586
af02033e 3587/* Create a master std::terminate breakpoint. */
aa7d318d 3588static void
af02033e 3589create_std_terminate_master_breakpoint (void)
aa7d318d 3590{
af02033e 3591 const char *const func_name = "std::terminate()";
aa7d318d 3592
5ed8105e 3593 scoped_restore_current_program_space restore_pspace;
aa7d318d 3594
94c93c35
TT
3595 for (struct program_space *pspace : program_spaces)
3596 {
94c93c35 3597 set_current_program_space (pspace);
17450429 3598
94c93c35
TT
3599 for (objfile *objfile : current_program_space->objfiles ())
3600 {
3601 struct breakpoint *b;
3602 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3603
94c93c35 3604 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3605
94c93c35
TT
3606 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3607 continue;
17450429 3608
94c93c35
TT
3609 if (bp_objfile_data->terminate_msym.minsym == NULL)
3610 {
3611 struct bound_minimal_symbol m;
17450429 3612
94c93c35 3613 m = lookup_minimal_symbol (func_name, NULL, objfile);
60f62e2b
SM
3614 if (m.minsym == NULL || (m.minsym->type () != mst_text
3615 && m.minsym->type () != mst_file_text))
94c93c35
TT
3616 {
3617 /* Prevent future lookups in this objfile. */
3618 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3619 continue;
3620 }
3621 bp_objfile_data->terminate_msym = m;
3622 }
aa7d318d 3623
829b6b37
TV
3624 b = create_internal_breakpoint (objfile->arch (),
3625 bp_objfile_data->terminate_msym,
9efa3c7f 3626 bp_std_terminate_master);
40d97ee2 3627 b->locspec = new_explicit_location_spec_function (func_name);
94c93c35
TT
3628 b->enable_state = bp_disabled;
3629 }
3630 }
aa7d318d
TT
3631}
3632
1940319c
TV
3633/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3634 probe. Return true if a breakpoint was installed. */
186c406b 3635
1940319c
TV
3636static bool
3637create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3638{
1940319c
TV
3639 struct breakpoint *b;
3640 struct gdbarch *gdbarch;
3641 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3642
1940319c 3643 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3644
1940319c
TV
3645 /* We prefer the SystemTap probe point if it exists. */
3646 if (!bp_objfile_data->exception_searched)
3647 {
3648 std::vector<probe *> ret
3649 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3650
1940319c 3651 if (!ret.empty ())
28106bc2 3652 {
1940319c
TV
3653 /* We are only interested in checking one element. */
3654 probe *p = ret[0];
25f9533e 3655
1940319c 3656 if (!p->can_evaluate_arguments ())
25f9533e 3657 {
1940319c
TV
3658 /* We cannot use the probe interface here, because it does
3659 not know how to evaluate arguments. */
3660 ret.clear ();
25f9533e 3661 }
28106bc2 3662 }
1940319c
TV
3663 bp_objfile_data->exception_probes = ret;
3664 bp_objfile_data->exception_searched = 1;
3665 }
28106bc2 3666
1940319c
TV
3667 if (bp_objfile_data->exception_probes.empty ())
3668 return false;
45461e0d 3669
1940319c 3670 gdbarch = objfile->arch ();
28106bc2 3671
1940319c
TV
3672 for (probe *p : bp_objfile_data->exception_probes)
3673 {
3674 b = create_internal_breakpoint (gdbarch,
3675 p->get_relocated_address (objfile),
9efa3c7f 3676 bp_exception_master);
264f9890 3677 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
1940319c
TV
3678 b->enable_state = bp_disabled;
3679 }
28106bc2 3680
1940319c
TV
3681 return true;
3682}
28106bc2 3683
1940319c
TV
3684/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3685 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3686
1940319c
TV
3687static bool
3688create_exception_master_breakpoint_hook (objfile *objfile)
3689{
3690 const char *const func_name = "_Unwind_DebugHook";
3691 struct breakpoint *b;
3692 struct gdbarch *gdbarch;
3693 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3694
1940319c 3695 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3696
1940319c
TV
3697 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3698 return false;
17450429 3699
1940319c
TV
3700 gdbarch = objfile->arch ();
3701
3702 if (bp_objfile_data->exception_msym.minsym == NULL)
3703 {
3704 struct bound_minimal_symbol debug_hook;
3705
3706 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3707 if (debug_hook.minsym == NULL)
3708 {
3709 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3710 return false;
186c406b 3711 }
17450429 3712
1940319c
TV
3713 bp_objfile_data->exception_msym = debug_hook;
3714 }
3715
829b6b37
TV
3716 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3717 bp_exception_master);
40d97ee2 3718 b->locspec = new_explicit_location_spec_function (func_name);
1940319c
TV
3719 b->enable_state = bp_disabled;
3720
3721 return true;
3722}
3723
3724/* Install a master breakpoint on the unwinder's debug hook. */
3725
3726static void
3727create_exception_master_breakpoint (void)
3728{
3729 for (objfile *obj : current_program_space->objfiles ())
3730 {
3731 /* Skip separate debug object. */
3732 if (obj->separate_debug_objfile_backlink)
3733 continue;
3734
3735 /* Try a probe kind breakpoint. */
3736 if (create_exception_master_breakpoint_probe (obj))
3737 continue;
3738
7c6944ab
TV
3739 /* Iterate over main and separate debug objects and try an
3740 _Unwind_DebugHook kind breakpoint. */
3741 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3742 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3743 break;
186c406b 3744 }
186c406b
TT
3745}
3746
9ef9e6a6
KS
3747/* Does B have a location spec? */
3748
d8de7963 3749static bool
264f9890 3750breakpoint_location_spec_empty_p (const struct breakpoint *b)
9ef9e6a6 3751{
238dc9af 3752 return (b->locspec != nullptr && b->locspec->empty_p ());
9ef9e6a6
KS
3753}
3754
c906108c 3755void
fba45db2 3756update_breakpoints_after_exec (void)
c906108c 3757{
25b22b0a
PA
3758 /* We're about to delete breakpoints from GDB's lists. If the
3759 INSERTED flag is true, GDB will try to lift the breakpoints by
3760 writing the breakpoints' "shadow contents" back into memory. The
3761 "shadow contents" are NOT valid after an exec, so GDB should not
3762 do that. Instead, the target is responsible from marking
3763 breakpoints out as soon as it detects an exec. We don't do that
3764 here instead, because there may be other attempts to delete
3765 breakpoints after detecting an exec and before reaching here. */
48d7020b 3766 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3767 if (bploc->pspace == current_program_space)
3768 gdb_assert (!bploc->inserted);
c906108c 3769
1428b37a 3770 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3771 {
3772 if (b->pspace != current_program_space)
c5aa993b 3773 continue;
c906108c 3774
01add95b
SM
3775 /* Solib breakpoints must be explicitly reset after an exec(). */
3776 if (b->type == bp_shlib_event)
3777 {
3778 delete_breakpoint (b);
3779 continue;
3780 }
4efc6507 3781
01add95b
SM
3782 /* JIT breakpoints must be explicitly reset after an exec(). */
3783 if (b->type == bp_jit_event)
3784 {
3785 delete_breakpoint (b);
3786 continue;
3787 }
c4093a6a 3788
01add95b
SM
3789 /* Thread event breakpoints must be set anew after an exec(),
3790 as must overlay event and longjmp master breakpoints. */
3791 if (b->type == bp_thread_event || b->type == bp_overlay_event
3792 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3793 || b->type == bp_exception_master)
3794 {
3795 delete_breakpoint (b);
3796 continue;
3797 }
c5aa993b 3798
01add95b
SM
3799 /* Step-resume breakpoints are meaningless after an exec(). */
3800 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3801 {
3802 delete_breakpoint (b);
3803 continue;
3804 }
7c16b83e 3805
01add95b
SM
3806 /* Just like single-step breakpoints. */
3807 if (b->type == bp_single_step)
3808 {
3809 delete_breakpoint (b);
3810 continue;
3811 }
611c83ae 3812
01add95b
SM
3813 /* Longjmp and longjmp-resume breakpoints are also meaningless
3814 after an exec. */
3815 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3816 || b->type == bp_longjmp_call_dummy
3817 || b->type == bp_exception || b->type == bp_exception_resume)
3818 {
3819 delete_breakpoint (b);
3820 continue;
3821 }
ce78b96d 3822
01add95b
SM
3823 if (b->type == bp_catchpoint)
3824 {
3825 /* For now, none of the bp_catchpoint breakpoints need to
3826 do anything at this point. In the future, if some of
3827 the catchpoints need to something, we will need to add
3828 a new method, and call this method from here. */
3829 continue;
3830 }
c5aa993b 3831
01add95b
SM
3832 /* bp_finish is a special case. The only way we ought to be able
3833 to see one of these when an exec() has happened, is if the user
3834 caught a vfork, and then said "finish". Ordinarily a finish just
3835 carries them to the call-site of the current callee, by setting
3836 a temporary bp there and resuming. But in this case, the finish
3837 will carry them entirely through the vfork & exec.
3838
3839 We don't want to allow a bp_finish to remain inserted now. But
3840 we can't safely delete it, 'cause finish_command has a handle to
3841 the bp on a bpstat, and will later want to delete it. There's a
3842 chance (and I've seen it happen) that if we delete the bp_finish
3843 here, that its storage will get reused by the time finish_command
3844 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3845 We really must allow finish_command to delete a bp_finish.
3846
3847 In the absence of a general solution for the "how do we know
3848 it's safe to delete something others may have handles to?"
3849 problem, what we'll do here is just uninsert the bp_finish, and
3850 let finish_command delete it.
3851
3852 (We know the bp_finish is "doomed" in the sense that it's
3853 momentary, and will be deleted as soon as finish_command sees
3854 the inferior stopped. So it doesn't matter that the bp's
3855 address is probably bogus in the new a.out, unlike e.g., the
3856 solib breakpoints.) */
3857
3858 if (b->type == bp_finish)
3859 {
3860 continue;
3861 }
3862
3863 /* Without a symbolic address, we have little hope of the
3864 pre-exec() address meaning the same thing in the post-exec()
3865 a.out. */
264f9890 3866 if (breakpoint_location_spec_empty_p (b))
01add95b
SM
3867 {
3868 delete_breakpoint (b);
3869 continue;
3870 }
3871 }
c906108c
SS
3872}
3873
3874int
d80ee84f 3875detach_breakpoints (ptid_t ptid)
c906108c 3876{
3a1bae8e 3877 int val = 0;
2989a365 3878 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3879 struct inferior *inf = current_inferior ();
c5aa993b 3880
e99b03dc 3881 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3882 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3883
6c95b8df 3884 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3885 inferior_ptid = ptid;
48d7020b 3886 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3887 {
3888 if (bl->pspace != inf->pspace)
3889 continue;
3890
3891 /* This function must physically remove breakpoints locations
3892 from the specified ptid, without modifying the breakpoint
6e14e441
PA
3893 package's state. Locations of type bp_loc_other and
3894 bp_loc_software_watchpoint are only maintained at GDB side,
3895 so there is no need to remove them. Moreover, removing these
01add95b 3896 would modify the breakpoint package's state. */
6e14e441
PA
3897 if (bl->loc_type == bp_loc_other
3898 || bl->loc_type == bp_loc_software_watchpoint)
01add95b
SM
3899 continue;
3900
3901 if (bl->inserted)
3902 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3903 }
d03285ec 3904
3a1bae8e 3905 return val;
c906108c
SS
3906}
3907
35df4500 3908/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3909 Note that this is used to detach breakpoints from a child fork.
3910 When we get here, the child isn't in the inferior list, and neither
3911 do we have objects to represent its address space --- we should
35df4500 3912 *not* look at bl->pspace->aspace here. */
6c95b8df 3913
c906108c 3914static int
b2b6a7da 3915remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3916{
3917 int val;
c5aa993b 3918
35df4500
TJB
3919 /* BL is never in moribund_locations by our callers. */
3920 gdb_assert (bl->owner != NULL);
2bdf28a0 3921
74960c60
VP
3922 /* The type of none suggests that owner is actually deleted.
3923 This should not ever happen. */
35df4500 3924 gdb_assert (bl->owner->type != bp_none);
0bde7532 3925
35df4500
TJB
3926 if (bl->loc_type == bp_loc_software_breakpoint
3927 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3928 {
c02f5703
MS
3929 /* "Normal" instruction breakpoint: either the standard
3930 trap-instruction bp (bp_breakpoint), or a
3931 bp_hardware_breakpoint. */
3932
3933 /* First check to see if we have to handle an overlay. */
3934 if (overlay_debugging == ovly_off
35df4500
TJB
3935 || bl->section == NULL
3936 || !(section_is_overlay (bl->section)))
c02f5703
MS
3937 {
3938 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3939
3940 /* If we're trying to uninsert a memory breakpoint that we
3941 know is set in a dynamic object that is marked
3942 shlib_disabled, then either the dynamic object was
3943 removed with "remove-symbol-file" or with
3944 "nosharedlibrary". In the former case, we don't know
3945 whether another dynamic object might have loaded over the
3946 breakpoint's address -- the user might well let us know
3947 about it next with add-symbol-file (the whole point of
d03de421 3948 add-symbol-file is letting the user manually maintain a
08351840
PA
3949 list of dynamically loaded objects). If we have the
3950 breakpoint's shadow memory, that is, this is a software
3951 breakpoint managed by GDB, check whether the breakpoint
3952 is still inserted in memory, to avoid overwriting wrong
3953 code with stale saved shadow contents. Note that HW
3954 breakpoints don't have shadow memory, as they're
3955 implemented using a mechanism that is not dependent on
3956 being able to modify the target's memory, and as such
3957 they should always be removed. */
3958 if (bl->shlib_disabled
3959 && bl->target_info.shadow_len != 0
3960 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3961 val = 0;
3962 else
c47614fe 3963 val = bl->owner->remove_location (bl, reason);
c02f5703 3964 }
c906108c
SS
3965 else
3966 {
4a64f543 3967 /* This breakpoint is in an overlay section.
c02f5703
MS
3968 Did we set a breakpoint at the LMA? */
3969 if (!overlay_events_enabled)
3970 {
3971 /* Yes -- overlay event support is not active, so we
3972 should have set a breakpoint at the LMA. Remove it.
3973 */
c02f5703
MS
3974 /* Ignore any failures: if the LMA is in ROM, we will
3975 have already warned when we failed to insert it. */
35df4500
TJB
3976 if (bl->loc_type == bp_loc_hardware_breakpoint)
3977 target_remove_hw_breakpoint (bl->gdbarch,
3978 &bl->overlay_target_info);
c02f5703 3979 else
35df4500 3980 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3981 &bl->overlay_target_info,
3982 reason);
c02f5703
MS
3983 }
3984 /* Did we set a breakpoint at the VMA?
3985 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3986 if (bl->inserted)
c906108c 3987 {
c02f5703
MS
3988 /* Yes -- remove it. Previously we did not bother to
3989 remove the breakpoint if the section had been
3990 unmapped, but let's not rely on that being safe. We
3991 don't know what the overlay manager might do. */
aa67235e
UW
3992
3993 /* However, we should remove *software* breakpoints only
3994 if the section is still mapped, or else we overwrite
3995 wrong code with the saved shadow contents. */
348d480f
PA
3996 if (bl->loc_type == bp_loc_hardware_breakpoint
3997 || section_is_mapped (bl->section))
c47614fe 3998 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
3999 else
4000 val = 0;
c906108c 4001 }
c02f5703
MS
4002 else
4003 {
4004 /* No -- not inserted, so no need to remove. No error. */
4005 val = 0;
4006 }
c906108c 4007 }
879d1e6b 4008
08351840
PA
4009 /* In some cases, we might not be able to remove a breakpoint in
4010 a shared library that has already been removed, but we have
4011 not yet processed the shlib unload event. Similarly for an
4012 unloaded add-symbol-file object - the user might not yet have
4013 had the chance to remove-symbol-file it. shlib_disabled will
4014 be set if the library/object has already been removed, but
4015 the breakpoint hasn't been uninserted yet, e.g., after
4016 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4017 always-inserted mode. */
076855f9 4018 if (val
08351840
PA
4019 && (bl->loc_type == bp_loc_software_breakpoint
4020 && (bl->shlib_disabled
4021 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4022 || shared_objfile_contains_address_p (bl->pspace,
4023 bl->address))))
879d1e6b
UW
4024 val = 0;
4025
c906108c
SS
4026 if (val)
4027 return val;
b2b6a7da 4028 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4029 }
35df4500 4030 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4031 {
b2b6a7da 4032 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 4033 bl->owner->remove_location (bl, reason);
2e70b7b9 4034
c906108c 4035 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4036 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4037 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4038 bl->owner->number);
c906108c 4039 }
35df4500 4040 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
4041 && breakpoint_enabled (bl->owner)
4042 && !bl->duplicate)
ce78b96d 4043 {
c47614fe 4044 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
4045 if (val)
4046 return val;
77b06cd7 4047
b2b6a7da 4048 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4049 }
c906108c
SS
4050
4051 return 0;
4052}
4053
6c95b8df 4054static int
834c0d03 4055remove_breakpoint (struct bp_location *bl)
6c95b8df 4056{
35df4500
TJB
4057 /* BL is never in moribund_locations by our callers. */
4058 gdb_assert (bl->owner != NULL);
2bdf28a0 4059
6c95b8df
PA
4060 /* The type of none suggests that owner is actually deleted.
4061 This should not ever happen. */
35df4500 4062 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4063
5ed8105e 4064 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4065
35df4500 4066 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4067
5ed8105e 4068 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4069}
4070
c906108c
SS
4071/* Clear the "inserted" flag in all breakpoints. */
4072
25b22b0a 4073void
fba45db2 4074mark_breakpoints_out (void)
c906108c 4075{
48d7020b 4076 for (bp_location *bl : all_bp_locations ())
66c4b3e8 4077 if (bl->pspace == current_program_space)
35df4500 4078 bl->inserted = 0;
c906108c
SS
4079}
4080
53a5351d
JM
4081/* Clear the "inserted" flag in all breakpoints and delete any
4082 breakpoints which should go away between runs of the program.
c906108c
SS
4083
4084 Plus other such housekeeping that has to be done for breakpoints
4085 between runs.
4086
53a5351d
JM
4087 Note: this function gets called at the end of a run (by
4088 generic_mourn_inferior) and when a run begins (by
4a64f543 4089 init_wait_for_inferior). */
c906108c
SS
4090
4091
4092
4093void
fba45db2 4094breakpoint_init_inferior (enum inf_context context)
c906108c 4095{
6c95b8df 4096 struct program_space *pspace = current_program_space;
c906108c 4097
50c71eaf
PA
4098 /* If breakpoint locations are shared across processes, then there's
4099 nothing to do. */
f5656ead 4100 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4101 return;
4102
1a853c52 4103 mark_breakpoints_out ();
075f6582 4104
1428b37a 4105 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
4106 {
4107 if (b->loc && b->loc->pspace != pspace)
4108 continue;
6c95b8df 4109
01add95b
SM
4110 switch (b->type)
4111 {
4112 case bp_call_dummy:
4113 case bp_longjmp_call_dummy:
c906108c 4114
01add95b
SM
4115 /* If the call dummy breakpoint is at the entry point it will
4116 cause problems when the inferior is rerun, so we better get
4117 rid of it. */
ab92d69b 4118
01add95b 4119 case bp_watchpoint_scope:
ab92d69b 4120
01add95b 4121 /* Also get rid of scope breakpoints. */
ab92d69b 4122
01add95b 4123 case bp_shlib_event:
ab92d69b 4124
01add95b
SM
4125 /* Also remove solib event breakpoints. Their addresses may
4126 have changed since the last time we ran the program.
4127 Actually we may now be debugging against different target;
4128 and so the solib backend that installed this breakpoint may
4129 not be used in by the target. E.g.,
ab92d69b 4130
01add95b
SM
4131 (gdb) file prog-linux
4132 (gdb) run # native linux target
4133 ...
4134 (gdb) kill
4135 (gdb) file prog-win.exe
4136 (gdb) tar rem :9999 # remote Windows gdbserver.
4137 */
c906108c 4138
01add95b 4139 case bp_step_resume:
f59f708a 4140
01add95b 4141 /* Also remove step-resume breakpoints. */
f59f708a 4142
01add95b 4143 case bp_single_step:
7c16b83e 4144
01add95b 4145 /* Also remove single-step breakpoints. */
7c16b83e 4146
01add95b
SM
4147 delete_breakpoint (b);
4148 break;
c906108c 4149
01add95b
SM
4150 case bp_watchpoint:
4151 case bp_hardware_watchpoint:
4152 case bp_read_watchpoint:
4153 case bp_access_watchpoint:
4154 {
4155 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4156
01add95b
SM
4157 /* Likewise for watchpoints on local expressions. */
4158 if (w->exp_valid_block != NULL)
4159 delete_breakpoint (b);
4160 else
4161 {
4162 /* Get rid of existing locations, which are no longer
4163 valid. New ones will be created in
4164 update_watchpoint, when the inferior is restarted.
4165 The next update_global_location_list call will
4166 garbage collect them. */
4167 b->loc = NULL;
4168
4169 if (context == inf_starting)
4170 {
4171 /* Reset val field to force reread of starting value in
4172 insert_breakpoints. */
4173 w->val.reset (nullptr);
4174 w->val_valid = false;
4175 }
4176 }
4177 }
4178 break;
4179 default:
4180 break;
3a5c3e22 4181 }
01add95b 4182 }
1c5cfe86
PA
4183
4184 /* Get rid of the moribund locations. */
1123588c 4185 for (bp_location *bl : moribund_locations)
35df4500 4186 decref_bp_location (&bl);
1123588c 4187 moribund_locations.clear ();
c906108c
SS
4188}
4189
6c95b8df
PA
4190/* These functions concern about actual breakpoints inserted in the
4191 target --- to e.g. check if we need to do decr_pc adjustment or if
4192 we need to hop over the bkpt --- so we check for address space
4193 match, not program space. */
4194
c2c6d25f
JM
4195/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4196 exists at PC. It returns ordinary_breakpoint_here if it's an
4197 ordinary breakpoint, or permanent_breakpoint_here if it's a
4198 permanent breakpoint.
4199 - When continuing from a location with an ordinary breakpoint, we
4200 actually single step once before calling insert_breakpoints.
e5dd4106 4201 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4202 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4203 the target, to advance the PC past the breakpoint. */
c906108c 4204
c2c6d25f 4205enum breakpoint_here
accd0bcd 4206breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4207{
d8de7963 4208 bool any_breakpoint_here = false;
c906108c 4209
48d7020b 4210 for (bp_location *bl : all_bp_locations ())
075f6582 4211 {
35df4500
TJB
4212 if (bl->loc_type != bp_loc_software_breakpoint
4213 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4214 continue;
4215
f1310107 4216 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4217 if ((breakpoint_enabled (bl->owner)
1a853c52 4218 || bl->permanent)
f1310107 4219 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4220 {
4221 if (overlay_debugging
35df4500
TJB
4222 && section_is_overlay (bl->section)
4223 && !section_is_mapped (bl->section))
075f6582 4224 continue; /* unmapped overlay -- can't be a match */
1a853c52 4225 else if (bl->permanent)
075f6582
DJ
4226 return permanent_breakpoint_here;
4227 else
d8de7963 4228 any_breakpoint_here = true;
075f6582
DJ
4229 }
4230 }
c906108c 4231
f486487f 4232 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4233}
4234
d35ae833
PA
4235/* See breakpoint.h. */
4236
4237int
accd0bcd 4238breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4239 CORE_ADDR addr, ULONGEST len)
4240{
48d7020b 4241 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4242 {
4243 if (bl->loc_type != bp_loc_software_breakpoint
4244 && bl->loc_type != bp_loc_hardware_breakpoint)
4245 continue;
4246
4247 if ((breakpoint_enabled (bl->owner)
4248 || bl->permanent)
4249 && breakpoint_location_address_range_overlap (bl, aspace,
4250 addr, len))
4251 {
4252 if (overlay_debugging
4253 && section_is_overlay (bl->section)
4254 && !section_is_mapped (bl->section))
4255 {
4256 /* Unmapped overlay -- can't be a match. */
4257 continue;
4258 }
4259
4260 return 1;
4261 }
4262 }
4263
4264 return 0;
4265}
4266
1c5cfe86
PA
4267/* Return true if there's a moribund breakpoint at PC. */
4268
4269int
accd0bcd 4270moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4271{
1123588c 4272 for (bp_location *loc : moribund_locations)
f1310107 4273 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4274 return 1;
4275
4276 return 0;
4277}
c2c6d25f 4278
d8de7963 4279/* Returns true iff BL is inserted at PC, in address space ASPACE. */
f7ce857f 4280
d8de7963 4281static bool
b2ff9ed3 4282bp_location_inserted_here_p (const struct bp_location *bl,
accd0bcd 4283 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4284{
4285 if (bl->inserted
4286 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4287 aspace, pc))
4288 {
d8de7963
AB
4289 /* An unmapped overlay can't be a match. */
4290 return !(overlay_debugging
4291 && section_is_overlay (bl->section)
4292 && !section_is_mapped (bl->section));
f7ce857f 4293 }
d8de7963 4294 return false;
f7ce857f
PA
4295}
4296
a1fd2fa5 4297/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4298
4299int
accd0bcd 4300breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4301{
e0d9a270 4302 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4303 {
35df4500
TJB
4304 if (bl->loc_type != bp_loc_software_breakpoint
4305 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4306 continue;
4307
f7ce857f
PA
4308 if (bp_location_inserted_here_p (bl, aspace, pc))
4309 return 1;
c5aa993b 4310 }
c36b740a
VP
4311 return 0;
4312}
4313
a1fd2fa5
PA
4314/* This function returns non-zero iff there is a software breakpoint
4315 inserted at PC. */
c36b740a
VP
4316
4317int
accd0bcd 4318software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4319 CORE_ADDR pc)
4fa8626c 4320{
e0d9a270 4321 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4322 {
35df4500 4323 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4324 continue;
4325
f7ce857f
PA
4326 if (bp_location_inserted_here_p (bl, aspace, pc))
4327 return 1;
4fa8626c
DJ
4328 }
4329
4330 return 0;
9c02b525
PA
4331}
4332
4333/* See breakpoint.h. */
4334
4335int
accd0bcd 4336hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4337 CORE_ADDR pc)
4338{
e0d9a270 4339 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4340 {
9c02b525
PA
4341 if (bl->loc_type != bp_loc_hardware_breakpoint)
4342 continue;
4343
4344 if (bp_location_inserted_here_p (bl, aspace, pc))
4345 return 1;
4346 }
4347
4348 return 0;
4fa8626c
DJ
4349}
4350
9093389c 4351int
accd0bcd 4352hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4353 CORE_ADDR addr, ULONGEST len)
4354{
43892fdf 4355 for (breakpoint *bpt : all_breakpoints ())
9093389c 4356 {
9093389c
PA
4357 if (bpt->type != bp_hardware_watchpoint
4358 && bpt->type != bp_access_watchpoint)
4359 continue;
4360
4361 if (!breakpoint_enabled (bpt))
4362 continue;
4363
40cb8ca5 4364 for (bp_location *loc : bpt->locations ())
9093389c
PA
4365 if (loc->pspace->aspace == aspace && loc->inserted)
4366 {
4367 CORE_ADDR l, h;
4368
4369 /* Check for intersection. */
768adc05
PA
4370 l = std::max<CORE_ADDR> (loc->address, addr);
4371 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4372 if (l < h)
4373 return 1;
4374 }
4375 }
4376 return 0;
4377}
c5aa993b 4378
f2478a7e 4379/* See breakpoint.h. */
c906108c 4380
f2478a7e
SM
4381bool
4382is_catchpoint (struct breakpoint *b)
c906108c 4383{
f2478a7e 4384 return (b->type == bp_catchpoint);
c906108c
SS
4385}
4386
c906108c
SS
4387/* Clear a bpstat so that it says we are not at any breakpoint.
4388 Also free any storage that is part of a bpstat. */
4389
4390void
313f3b21 4391bpstat_clear (bpstat **bsp)
c906108c 4392{
313f3b21
SM
4393 bpstat *p;
4394 bpstat *q;
c906108c
SS
4395
4396 if (bsp == 0)
4397 return;
4398 p = *bsp;
4399 while (p != NULL)
4400 {
4401 q = p->next;
04afa70c 4402 delete p;
c906108c
SS
4403 p = q;
4404 }
4405 *bsp = NULL;
4406}
4407
313f3b21 4408bpstat::bpstat (const bpstat &other)
04afa70c
TT
4409 : next (NULL),
4410 bp_location_at (other.bp_location_at),
4411 breakpoint_at (other.breakpoint_at),
4412 commands (other.commands),
04afa70c
TT
4413 print (other.print),
4414 stop (other.stop),
4415 print_it (other.print_it)
4416{
850645cf 4417 if (other.old_val != NULL)
f28085df 4418 old_val = release_value (other.old_val->copy ());
04afa70c
TT
4419}
4420
c906108c
SS
4421/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4422 is part of the bpstat is copied as well. */
4423
313f3b21
SM
4424bpstat *
4425bpstat_copy (bpstat *bs)
c906108c 4426{
313f3b21
SM
4427 bpstat *p = nullptr;
4428 bpstat *tmp;
4429 bpstat *retval = nullptr;
c906108c
SS
4430
4431 if (bs == NULL)
4432 return bs;
4433
4434 for (; bs != NULL; bs = bs->next)
4435 {
313f3b21 4436 tmp = new bpstat (*bs);
31cc81e9 4437
c906108c
SS
4438 if (p == NULL)
4439 /* This is the first thing in the chain. */
4440 retval = tmp;
4441 else
4442 p->next = tmp;
4443 p = tmp;
4444 }
4445 p->next = NULL;
4446 return retval;
4447}
4448
4a64f543 4449/* Find the bpstat associated with this breakpoint. */
c906108c 4450
313f3b21
SM
4451bpstat *
4452bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4453{
c5aa993b
JM
4454 if (bsp == NULL)
4455 return NULL;
c906108c 4456
c5aa993b
JM
4457 for (; bsp != NULL; bsp = bsp->next)
4458 {
f431efe5 4459 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4460 return bsp;
4461 }
c906108c
SS
4462 return NULL;
4463}
4464
ab04a2af
TT
4465/* See breakpoint.h. */
4466
4c462cb0 4467bool
313f3b21 4468bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4469{
ab04a2af
TT
4470 for (; bsp != NULL; bsp = bsp->next)
4471 {
427cd150
TT
4472 if (bsp->breakpoint_at == NULL)
4473 {
4474 /* A moribund location can never explain a signal other than
4475 GDB_SIGNAL_TRAP. */
4476 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4477 return true;
427cd150
TT
4478 }
4479 else
47591c29 4480 {
c47614fe 4481 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4482 return true;
47591c29 4483 }
ab04a2af
TT
4484 }
4485
4c462cb0 4486 return false;
ab04a2af
TT
4487}
4488
78805ff8 4489/* See breakpoint.h. */
c906108c
SS
4490
4491int
313f3b21 4492bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4493{
4494 struct breakpoint *b;
4495
4496 if ((*bsp) == NULL)
4497 return 0; /* No more breakpoint values */
8671a17b 4498
4a64f543
MS
4499 /* We assume we'll never have several bpstats that correspond to a
4500 single breakpoint -- otherwise, this function might return the
4501 same number more than once and this will look ugly. */
f431efe5 4502 b = (*bsp)->breakpoint_at;
8671a17b
PA
4503 *bsp = (*bsp)->next;
4504 if (b == NULL)
4505 return -1; /* breakpoint that's been deleted since */
4506
4507 *num = b->number; /* We have its number */
4508 return 1;
c906108c
SS
4509}
4510
78805ff8
PW
4511/* See breakpoint.h */
4512
4513int
4514bpstat_locno (const bpstat *bs)
4515{
4516 const struct breakpoint *b = bs->breakpoint_at;
4517 const struct bp_location *bl = bs->bp_location_at.get ();
4518
4519 int locno = 0;
4520
28a072f4 4521 if (b != nullptr && b->loc != nullptr && b->loc->next != nullptr)
78805ff8
PW
4522 {
4523 const bp_location *bl_i;
4524
4525 for (bl_i = b->loc;
4526 bl_i != bl && bl_i->next != nullptr;
4527 bl_i = bl_i->next)
4528 locno++;
4529
4530 if (bl_i == bl)
4531 locno++;
4532 else
4533 {
4534 warning (_("location number not found for breakpoint %d address %s."),
4535 b->number, paddress (bl->gdbarch, bl->address));
4536 locno = 0;
4537 }
4538 }
4539
4540 return locno;
4541}
4542
4543/* See breakpoint.h. */
4544
4545void
4546print_num_locno (const bpstat *bs, struct ui_out *uiout)
4547{
4548 struct breakpoint *b = bs->breakpoint_at;
4549
4550 if (b == nullptr)
4551 uiout->text (_("deleted breakpoint"));
4552 else
4553 {
4554 uiout->field_signed ("bkptno", b->number);
4555
4556 int locno = bpstat_locno (bs);
4557 if (locno != 0)
4558 uiout->message (".%pF", signed_field ("locno", locno));
4559 }
4560}
4561
e93ca019 4562/* See breakpoint.h. */
c906108c
SS
4563
4564void
e93ca019 4565bpstat_clear_actions (void)
c906108c 4566{
313f3b21 4567 bpstat *bs;
e93ca019 4568
00431a78 4569 if (inferior_ptid == null_ptid)
e93ca019
JK
4570 return;
4571
00431a78 4572 thread_info *tp = inferior_thread ();
e93ca019 4573 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4574 {
d1b0a7bf 4575 bs->commands = NULL;
850645cf 4576 bs->old_val.reset (nullptr);
c906108c
SS
4577 }
4578}
4579
f3b1572e
PA
4580/* Called when a command is about to proceed the inferior. */
4581
4582static void
4583breakpoint_about_to_proceed (void)
4584{
d7e15655 4585 if (inferior_ptid != null_ptid)
f3b1572e
PA
4586 {
4587 struct thread_info *tp = inferior_thread ();
4588
4589 /* Allow inferior function calls in breakpoint commands to not
4590 interrupt the command list. When the call finishes
4591 successfully, the inferior will be standing at the same
4592 breakpoint as if nothing happened. */
16c381f0 4593 if (tp->control.in_infcall)
f3b1572e
PA
4594 return;
4595 }
4596
4597 breakpoint_proceeded = 1;
4598}
4599
d8de7963 4600/* Return true iff CMD as the first line of a command sequence is `silent'
abf85f46
JK
4601 or its equivalent. */
4602
d8de7963 4603static bool
abf85f46
JK
4604command_line_is_silent (struct command_line *cmd)
4605{
4f45d445 4606 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4607}
4608
83f35083
PW
4609/* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4610 A locno 0 is changed to 1 to e.g. let the user do
4611 (gdb) disable $_hit_bpnum.$_hit_locno
4612 for a single location breakpoint. */
4613
78805ff8 4614static void
83f35083 4615set_hit_convenience_vars (int bpnum, int locno)
78805ff8 4616{
83f35083
PW
4617 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4618 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4619 (locno > 0 ? locno : 1));
78805ff8
PW
4620}
4621
4a64f543
MS
4622/* Execute all the commands associated with all the breakpoints at
4623 this location. Any of these commands could cause the process to
4624 proceed beyond this point, etc. We look out for such changes by
4625 checking the global "breakpoint_proceeded" after each command.
c906108c 4626
347bddb7
PA
4627 Returns true if a breakpoint command resumed the inferior. In that
4628 case, it is the caller's responsibility to recall it again with the
4629 bpstat of the current thread. */
4630
d8de7963 4631static bool
313f3b21 4632bpstat_do_actions_1 (bpstat **bsp)
c906108c 4633{
313f3b21 4634 bpstat *bs;
d8de7963 4635 bool again = false;
c906108c
SS
4636
4637 /* Avoid endless recursion if a `source' command is contained
4638 in bs->commands. */
4639 if (executing_breakpoint_commands)
d8de7963 4640 return false;
c906108c 4641
81b1e71c
TT
4642 scoped_restore save_executing
4643 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4644
1ac32117 4645 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4646
4a64f543 4647 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4648 bs = *bsp;
4649
78805ff8 4650 /* The $_hit_* convenience variables are set before running the
83f35083
PW
4651 commands of BS. In case we have several bs, after the loop,
4652 we set again the variables to the first printed bpnum and locno.
4653 For multiple breakpoints, this ensures the variables are set to the
4654 breakpoint printed for the user. */
4655 int printed_hit_bpnum = -1;
4656 int printed_hit_locno = -1;
78805ff8 4657
c906108c
SS
4658 breakpoint_proceeded = 0;
4659 for (; bs != NULL; bs = bs->next)
4660 {
d1b0a7bf 4661 struct command_line *cmd = NULL;
6c50ab1c 4662
83f35083
PW
4663 /* Set the _hit_* convenience variables before running BS's commands. */
4664 {
4665 const struct breakpoint *b = bs->breakpoint_at;
4666 if (b != nullptr)
4667 {
4668 int locno = bpstat_locno (bs);
4669
4670 set_hit_convenience_vars (b->number, locno);
4671 if (printed_hit_locno == -1 && bs->print)
4672 {
4673 printed_hit_bpnum = b->number;
4674 printed_hit_locno = locno;
4675 }
4676 }
4677 }
78805ff8 4678
6c50ab1c
JB
4679 /* Take ownership of the BSP's command tree, if it has one.
4680
dda83cd7
SM
4681 The command tree could legitimately contain commands like
4682 'step' and 'next', which call clear_proceed_status, which
83f35083 4683 frees the bpstat BS and its command tree. To make sure this doesn't
dda83cd7
SM
4684 free the tree we're executing out from under us, we need to
4685 take ownership of the tree ourselves. Since a given bpstat's
4686 commands are only executed once, we don't need to copy it; we
4687 can clear the pointer in the bpstat, and make sure we free
4688 the tree when we're done. */
d1b0a7bf 4689 counted_command_line ccmd = bs->commands;
9add0f1b 4690 bs->commands = NULL;
d1b0a7bf
TT
4691 if (ccmd != NULL)
4692 cmd = ccmd.get ();
abf85f46
JK
4693 if (command_line_is_silent (cmd))
4694 {
4695 /* The action has been already done by bpstat_stop_status. */
4696 cmd = cmd->next;
4697 }
6c50ab1c 4698
c906108c
SS
4699 while (cmd != NULL)
4700 {
4701 execute_control_command (cmd);
83f35083
PW
4702 /* After execute_control_command, if breakpoint_proceeded is true,
4703 BS has been freed and cannot be accessed anymore. */
c906108c
SS
4704
4705 if (breakpoint_proceeded)
4706 break;
4707 else
4708 cmd = cmd->next;
4709 }
6c50ab1c 4710
c906108c 4711 if (breakpoint_proceeded)
32c1e744 4712 {
cb814510 4713 if (current_ui->async)
347bddb7
PA
4714 /* If we are in async mode, then the target might be still
4715 running, not stopped at any breakpoint, so nothing for
4716 us to do here -- just return to the event loop. */
4717 ;
32c1e744
VP
4718 else
4719 /* In sync mode, when execute_control_command returns
4720 we're already standing on the next breakpoint.
347bddb7
PA
4721 Breakpoint commands for that stop were not run, since
4722 execute_command does not run breakpoint commands --
4723 only command_line_handler does, but that one is not
4724 involved in execution of breakpoint commands. So, we
4725 can now execute breakpoint commands. It should be
4726 noted that making execute_command do bpstat actions is
4727 not an option -- in this case we'll have recursive
4728 invocation of bpstat for each breakpoint with a
4729 command, and can easily blow up GDB stack. Instead, we
4730 return true, which will trigger the caller to recall us
4731 with the new stop_bpstat. */
d8de7963 4732 again = true;
347bddb7 4733 break;
32c1e744 4734 }
c906108c 4735 }
78805ff8
PW
4736
4737 /* Now that we have executed the commands of all bs, set the _hit_*
83f35083
PW
4738 convenience variables to the printed values. */
4739 if (printed_hit_locno != -1)
4740 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
78805ff8 4741
347bddb7
PA
4742 return again;
4743}
4744
00431a78
PA
4745/* Helper for bpstat_do_actions. Get the current thread, if there's
4746 one, is alive and has execution. Return NULL otherwise. */
4747
4748static thread_info *
4749get_bpstat_thread ()
4750{
55f6301a 4751 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4752 return NULL;
4753
4754 thread_info *tp = inferior_thread ();
611841bb 4755 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4756 return NULL;
4757 return tp;
4758}
4759
347bddb7
PA
4760void
4761bpstat_do_actions (void)
4762{
694c6bf5 4763 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4764 thread_info *tp;
353d1d73 4765
347bddb7 4766 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4767 while ((tp = get_bpstat_thread ()) != NULL)
4768 {
4769 /* Since in sync mode, bpstat_do_actions may resume the
4770 inferior, and only return when it is stopped at the next
4771 breakpoint, we keep doing breakpoint actions until it returns
4772 false to indicate the inferior was not resumed. */
4773 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4774 break;
4775 }
353d1d73 4776
694c6bf5 4777 cleanup_if_error.release ();
c906108c
SS
4778}
4779
fa4727a6
DJ
4780/* Print out the (old or new) value associated with a watchpoint. */
4781
4782static void
4783watchpoint_value_print (struct value *val, struct ui_file *stream)
4784{
4785 if (val == NULL)
7f6aba03 4786 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4787 else
79a45b7d
TT
4788 {
4789 struct value_print_options opts;
4790 get_user_print_options (&opts);
4791 value_print (val, stream, &opts);
4792 }
fa4727a6
DJ
4793}
4794
f303dbd6
PA
4795/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4796 debugging multiple threads. */
4797
4798void
4799maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4800{
112e8700 4801 if (uiout->is_mi_like_p ())
f303dbd6
PA
4802 return;
4803
112e8700 4804 uiout->text ("\n");
f303dbd6
PA
4805
4806 if (show_thread_that_caused_stop ())
4807 {
f303dbd6
PA
4808 struct thread_info *thr = inferior_thread ();
4809
112e8700 4810 uiout->text ("Thread ");
33eca680 4811 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 4812
25558938 4813 const char *name = thread_name (thr);
f303dbd6
PA
4814 if (name != NULL)
4815 {
112e8700 4816 uiout->text (" \"");
33eca680 4817 uiout->field_string ("name", name);
112e8700 4818 uiout->text ("\"");
f303dbd6
PA
4819 }
4820
112e8700 4821 uiout->text (" hit ");
f303dbd6
PA
4822 }
4823}
4824
e514a9d6 4825/* Generic routine for printing messages indicating why we
4a64f543 4826 stopped. The behavior of this function depends on the value
e514a9d6
JM
4827 'print_it' in the bpstat structure. Under some circumstances we
4828 may decide not to print anything here and delegate the task to
4a64f543 4829 normal_stop(). */
e514a9d6
JM
4830
4831static enum print_stop_action
313f3b21 4832print_bp_stop_message (bpstat *bs)
e514a9d6
JM
4833{
4834 switch (bs->print_it)
4835 {
4836 case print_it_noop:
4a64f543 4837 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4838 return PRINT_UNKNOWN;
4839 break;
4840
4841 case print_it_done:
4842 /* We still want to print the frame, but we already printed the
dda83cd7 4843 relevant messages. */
e514a9d6
JM
4844 return PRINT_SRC_AND_LOC;
4845 break;
4846
4847 case print_it_normal:
4f8d1dc6 4848 {
f431efe5
PA
4849 struct breakpoint *b = bs->breakpoint_at;
4850
1a6a67de
TJB
4851 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4852 which has since been deleted. */
4853 if (b == NULL)
4854 return PRINT_UNKNOWN;
4855
348d480f 4856 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 4857 return b->print_it (bs);
4f8d1dc6 4858 }
348d480f 4859 break;
3086aeae 4860
e514a9d6 4861 default:
f34652de 4862 internal_error (_("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4863 break;
c906108c 4864 }
c906108c
SS
4865}
4866
2f9ee862 4867/* See breakpoint.h. */
edcc5120 4868
2f9ee862 4869void
4e9e993a 4870print_solib_event (bool is_catchpoint)
edcc5120 4871{
6fb16ce6 4872 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4873 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4874
4875 if (!is_catchpoint)
4876 {
4877 if (any_added || any_deleted)
112e8700 4878 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4879 else
112e8700
SM
4880 current_uiout->text (_("Stopped due to shared library event (no "
4881 "libraries added or removed)\n"));
edcc5120
TT
4882 }
4883
112e8700
SM
4884 if (current_uiout->is_mi_like_p ())
4885 current_uiout->field_string ("reason",
4886 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4887
4888 if (any_deleted)
4889 {
112e8700 4890 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4891 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4892 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4893 {
6fb16ce6
SM
4894 const std::string &name = current_program_space->deleted_solibs[ix];
4895
edcc5120 4896 if (ix > 0)
112e8700
SM
4897 current_uiout->text (" ");
4898 current_uiout->field_string ("library", name);
4899 current_uiout->text ("\n");
edcc5120 4900 }
edcc5120
TT
4901 }
4902
4903 if (any_added)
4904 {
112e8700 4905 current_uiout->text (_(" Inferior loaded "));
10f489e5 4906 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4907 bool first = true;
52941706 4908 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4909 {
bcb430e4 4910 if (!first)
112e8700 4911 current_uiout->text (" ");
bcb430e4 4912 first = false;
112e8700
SM
4913 current_uiout->field_string ("library", iter->so_name);
4914 current_uiout->text ("\n");
edcc5120 4915 }
edcc5120
TT
4916 }
4917}
4918
e514a9d6
JM
4919/* Print a message indicating what happened. This is called from
4920 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4921 list - a list of the eventpoints that caused this stop. KIND is
4922 the target_waitkind for the stopping event. This
e514a9d6
JM
4923 routine calls the generic print routine for printing a message
4924 about reasons for stopping. This will print (for example) the
4925 "Breakpoint n," part of the output. The return value of this
4926 routine is one of:
c906108c 4927
4a64f543 4928 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4929 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4930 code to print the location. An example is
c5aa993b
JM
4931 "Breakpoint 1, " which should be followed by
4932 the location.
917317f4 4933 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4934 to also print the location part of the message.
4935 An example is the catch/throw messages, which
4a64f543 4936 don't require a location appended to the end.
917317f4 4937 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4938 further info to be printed. */
c906108c 4939
917317f4 4940enum print_stop_action
403c71fd 4941bpstat_print (bpstat *bs, target_waitkind kind)
c906108c 4942{
f486487f 4943 enum print_stop_action val;
c5aa993b 4944
c906108c 4945 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4946 (Currently all watchpoints go on the bpstat whether hit or not.
4947 That probably could (should) be changed, provided care is taken
c906108c 4948 with respect to bpstat_explains_signal). */
e514a9d6
JM
4949 for (; bs; bs = bs->next)
4950 {
4951 val = print_bp_stop_message (bs);
4952 if (val == PRINT_SRC_ONLY
78805ff8 4953 || val == PRINT_SRC_AND_LOC
e514a9d6
JM
4954 || val == PRINT_NOTHING)
4955 return val;
4956 }
c906108c 4957
36dfb11c
TT
4958 /* If we had hit a shared library event breakpoint,
4959 print_bp_stop_message would print out this message. If we hit an
4960 OS-level shared library event, do the same thing. */
4961 if (kind == TARGET_WAITKIND_LOADED)
4962 {
4e9e993a 4963 print_solib_event (false);
36dfb11c
TT
4964 return PRINT_NOTHING;
4965 }
4966
e514a9d6 4967 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4968 with and nothing was printed. */
917317f4 4969 return PRINT_UNKNOWN;
c906108c
SS
4970}
4971
bf469271 4972/* Evaluate the boolean expression EXP and return the result. */
c906108c 4973
bf469271
PA
4974static bool
4975breakpoint_cond_eval (expression *exp)
c906108c 4976{
65558ca5
TT
4977 scoped_value_mark mark;
4978 return value_true (evaluate_expression (exp));
c906108c
SS
4979}
4980
5760d0ab 4981/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4982
313f3b21 4983bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 4984 : next (NULL),
b6433ede 4985 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4986 breakpoint_at (bl->owner),
4987 commands (NULL),
04afa70c
TT
4988 print (0),
4989 stop (0),
4990 print_it (print_it_normal)
c906108c 4991{
04afa70c
TT
4992 **bs_link_pointer = this;
4993 *bs_link_pointer = &next;
4994}
4995
313f3b21 4996bpstat::bpstat ()
04afa70c 4997 : next (NULL),
04afa70c
TT
4998 breakpoint_at (NULL),
4999 commands (NULL),
04afa70c
TT
5000 print (0),
5001 stop (0),
5002 print_it (print_it_normal)
5003{
c906108c
SS
5004}
5005\f
d983da9c
DJ
5006/* The target has stopped with waitstatus WS. Check if any hardware
5007 watchpoints have triggered, according to the target. */
5008
5009int
c272a98c 5010watchpoints_triggered (const target_waitstatus &ws)
d983da9c 5011{
57810aa7 5012 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 5013 CORE_ADDR addr;
d983da9c
DJ
5014
5015 if (!stopped_by_watchpoint)
5016 {
5017 /* We were not stopped by a watchpoint. Mark all watchpoints
5018 as not triggered. */
43892fdf 5019 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5020 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5021 {
5022 struct watchpoint *w = (struct watchpoint *) b;
5023
5024 w->watchpoint_triggered = watch_triggered_no;
5025 }
d983da9c
DJ
5026
5027 return 0;
5028 }
5029
328d42d8 5030 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
5031 {
5032 /* We were stopped by a watchpoint, but we don't know where.
5033 Mark all watchpoints as unknown. */
43892fdf 5034 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5035 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5036 {
5037 struct watchpoint *w = (struct watchpoint *) b;
5038
5039 w->watchpoint_triggered = watch_triggered_unknown;
5040 }
d983da9c 5041
3c4797ba 5042 return 1;
d983da9c
DJ
5043 }
5044
5045 /* The target could report the data address. Mark watchpoints
5046 affected by this data address as triggered, and all others as not
5047 triggered. */
5048
43892fdf 5049 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5050 if (is_hardware_watchpoint (b))
d983da9c 5051 {
3a5c3e22 5052 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 5053
3a5c3e22 5054 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 5055 for (bp_location *loc : b->locations ())
9c06b0b4 5056 {
3a5c3e22 5057 if (is_masked_watchpoint (b))
9c06b0b4 5058 {
3a5c3e22
PA
5059 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5060 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5061
5062 if (newaddr == start)
5063 {
3a5c3e22 5064 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5065 break;
5066 }
5067 }
5068 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
5069 else if (target_watchpoint_addr_within_range
5070 (current_inferior ()->top_target (), addr, loc->address,
5071 loc->length))
9c06b0b4 5072 {
3a5c3e22 5073 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5074 break;
5075 }
5076 }
d983da9c
DJ
5077 }
5078
5079 return 1;
5080}
5081
bf469271
PA
5082/* Possible return values for watchpoint_check. */
5083enum wp_check_result
5084 {
5085 /* The watchpoint has been deleted. */
5086 WP_DELETED = 1,
5087
5088 /* The value has changed. */
5089 WP_VALUE_CHANGED = 2,
5090
5091 /* The value has not changed. */
5092 WP_VALUE_NOT_CHANGED = 3,
5093
5094 /* Ignore this watchpoint, no matter if the value changed or not. */
5095 WP_IGNORE = 4,
5096 };
c906108c
SS
5097
5098#define BP_TEMPFLAG 1
5099#define BP_HARDWAREFLAG 2
5100
4a64f543 5101/* Evaluate watchpoint condition expression and check if its value
bf469271 5102 changed. */
553e4c11 5103
bf469271 5104static wp_check_result
313f3b21 5105watchpoint_check (bpstat *bs)
c906108c 5106{
3a5c3e22 5107 struct watchpoint *b;
bd2b40ac 5108 frame_info_ptr fr;
d8de7963 5109 bool within_current_scope;
c906108c 5110
f431efe5 5111 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5112 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5113 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5114
f6bc2008
PA
5115 /* If this is a local watchpoint, we only want to check if the
5116 watchpoint frame is in scope if the current thread is the thread
5117 that was used to create the watchpoint. */
5118 if (!watchpoint_in_thread_scope (b))
60e1c644 5119 return WP_IGNORE;
f6bc2008 5120
c906108c 5121 if (b->exp_valid_block == NULL)
d8de7963 5122 within_current_scope = true;
c906108c
SS
5123 else
5124 {
bd2b40ac 5125 frame_info_ptr frame = get_current_frame ();
edb3359d
DJ
5126 struct gdbarch *frame_arch = get_frame_arch (frame);
5127 CORE_ADDR frame_pc = get_frame_pc (frame);
5128
c9cf6e20 5129 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5130 still in the function but the stack frame has already been
5131 invalidated. Since we can't rely on the values of local
5132 variables after the stack has been destroyed, we are treating
5133 the watchpoint in that state as `not changed' without further
5134 checking. Don't mark watchpoints as changed if the current
5135 frame is in an epilogue - even if they are in some other
5136 frame, our view of the stack is likely to be wrong and
5137 frame_find_by_id could error out. */
c9cf6e20 5138 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5139 return WP_IGNORE;
a0f49112 5140
101dcfbe 5141 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5142 within_current_scope = (fr != NULL);
69fbadd5
DJ
5143
5144 /* If we've gotten confused in the unwinder, we might have
5145 returned a frame that can't describe this variable. */
edb3359d
DJ
5146 if (within_current_scope)
5147 {
5148 struct symbol *function;
5149
5150 function = get_frame_function (fr);
5151 if (function == NULL
4aeddc50 5152 || !contained_in (b->exp_valid_block, function->value_block ()))
d8de7963 5153 within_current_scope = false;
edb3359d 5154 }
69fbadd5 5155
edb3359d 5156 if (within_current_scope)
c906108c
SS
5157 /* If we end up stopping, the current frame will get selected
5158 in normal_stop. So this call to select_frame won't affect
5159 the user. */
0f7d239c 5160 select_frame (fr);
c906108c 5161 }
c5aa993b 5162
c906108c
SS
5163 if (within_current_scope)
5164 {
4a64f543 5165 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5166 time before we return to the command level and call
5167 free_all_values. We can't call free_all_values because we
5168 might be in the middle of evaluating a function call. */
c906108c 5169
9c06b0b4 5170 struct value *mark;
fa4727a6
DJ
5171 struct value *new_val;
5172
c1fc2657 5173 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5174 /* Since we don't know the exact trigger address (from
5175 stopped_data_address), just tell the user we've triggered
5176 a mask watchpoint. */
5177 return WP_VALUE_CHANGED;
5178
5179 mark = value_mark ();
1eaebe02 5180 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5181 NULL, NULL, false);
218d2fc6 5182
bb9d5f81
PP
5183 if (b->val_bitsize != 0)
5184 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5185
4a64f543
MS
5186 /* We use value_equal_contents instead of value_equal because
5187 the latter coerces an array to a pointer, thus comparing just
5188 the address of the array instead of its contents. This is
5189 not what we want. */
fa4727a6 5190 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5191 || (b->val != NULL && !value_equal_contents (b->val.get (),
5192 new_val)))
c906108c 5193 {
c906108c 5194 bs->old_val = b->val;
850645cf 5195 b->val = release_value (new_val);
4c1d86d9 5196 b->val_valid = true;
850645cf
TT
5197 if (new_val != NULL)
5198 value_free_to_mark (mark);
c906108c
SS
5199 return WP_VALUE_CHANGED;
5200 }
5201 else
5202 {
60e1c644 5203 /* Nothing changed. */
c906108c 5204 value_free_to_mark (mark);
c906108c
SS
5205 return WP_VALUE_NOT_CHANGED;
5206 }
5207 }
5208 else
5209 {
5210 /* This seems like the only logical thing to do because
dda83cd7
SM
5211 if we temporarily ignored the watchpoint, then when
5212 we reenter the block in which it is valid it contains
5213 garbage (in the case of a function, it may have two
5214 garbage values, one before and one after the prologue).
5215 So we can't even detect the first assignment to it and
5216 watch after that (since the garbage may or may not equal
5217 the first value assigned). */
348d480f 5218 /* We print all the stop information in
c47614fe
TT
5219 breakpointprint_it, but in this case, by the time we
5220 call breakpoint->print_it this bp will be deleted
348d480f
PA
5221 already. So we have no choice but print the information
5222 here. */
468afe6c 5223
0e454242 5224 SWITCH_THRU_ALL_UIS ()
dda83cd7 5225 {
468afe6c
PA
5226 struct ui_out *uiout = current_uiout;
5227
112e8700
SM
5228 if (uiout->is_mi_like_p ())
5229 uiout->field_string
5230 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5231 uiout->message ("\nWatchpoint %pF deleted because the program has "
5232 "left the block in\n"
5233 "which its expression is valid.\n",
5234 signed_field ("wpnum", b->number));
468afe6c 5235 }
4ce44c66 5236
cdac0397 5237 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5238 b->commands = NULL;
d0fb5eae 5239 watchpoint_del_at_next_stop (b);
c906108c
SS
5240
5241 return WP_DELETED;
5242 }
5243}
5244
18a18393 5245/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5246 breakpoint location BL. This function does not check if we should
5247 stop, only if BL explains the stop. */
5248
d8de7963 5249static bool
6c95b8df 5250bpstat_check_location (const struct bp_location *bl,
accd0bcd 5251 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5252 const target_waitstatus &ws)
18a18393
VP
5253{
5254 struct breakpoint *b = bl->owner;
5255
348d480f 5256 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5257 gdb_assert (b != NULL);
5258
c47614fe 5259 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5260}
5261
3a5c3e22 5262/* Determine if the watched values have actually changed, and we
4ec2227a 5263 should stop. If not, set BS->stop to false. */
3a5c3e22 5264
18a18393 5265static void
313f3b21 5266bpstat_check_watchpoint (bpstat *bs)
18a18393 5267{
2bdf28a0 5268 const struct bp_location *bl;
3a5c3e22 5269 struct watchpoint *b;
2bdf28a0
JK
5270
5271 /* BS is built for existing struct breakpoint. */
b6433ede 5272 bl = bs->bp_location_at.get ();
2bdf28a0 5273 gdb_assert (bl != NULL);
3a5c3e22 5274 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5275 gdb_assert (b != NULL);
18a18393 5276
18a18393 5277 {
d8de7963
AB
5278 bool must_check_value = false;
5279
c1fc2657 5280 if (b->type == bp_watchpoint)
18a18393
VP
5281 /* For a software watchpoint, we must always check the
5282 watched value. */
d8de7963 5283 must_check_value = true;
18a18393
VP
5284 else if (b->watchpoint_triggered == watch_triggered_yes)
5285 /* We have a hardware watchpoint (read, write, or access)
5286 and the target earlier reported an address watched by
5287 this watchpoint. */
d8de7963 5288 must_check_value = true;
18a18393 5289 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5290 && b->type == bp_hardware_watchpoint)
18a18393
VP
5291 /* We were stopped by a hardware watchpoint, but the target could
5292 not report the data address. We must check the watchpoint's
5293 value. Access and read watchpoints are out of luck; without
5294 a data address, we can't figure it out. */
d8de7963 5295 must_check_value = true;
3a5c3e22 5296
18a18393
VP
5297 if (must_check_value)
5298 {
bf469271
PA
5299 wp_check_result e;
5300
a70b8144 5301 try
bf469271
PA
5302 {
5303 e = watchpoint_check (bs);
5304 }
230d2906 5305 catch (const gdb_exception &ex)
bf469271
PA
5306 {
5307 exception_fprintf (gdb_stderr, ex,
5308 "Error evaluating expression "
5309 "for watchpoint %d\n",
5310 b->number);
5311
5312 SWITCH_THRU_ALL_UIS ()
5313 {
6cb06a8c
TT
5314 gdb_printf (_("Watchpoint %d deleted.\n"),
5315 b->number);
bf469271
PA
5316 }
5317 watchpoint_del_at_next_stop (b);
5318 e = WP_DELETED;
5319 }
bf469271 5320
18a18393
VP
5321 switch (e)
5322 {
5323 case WP_DELETED:
5324 /* We've already printed what needs to be printed. */
5325 bs->print_it = print_it_done;
5326 /* Stop. */
5327 break;
60e1c644
PA
5328 case WP_IGNORE:
5329 bs->print_it = print_it_noop;
4ec2227a 5330 bs->stop = false;
60e1c644 5331 break;
18a18393 5332 case WP_VALUE_CHANGED:
c1fc2657 5333 if (b->type == bp_read_watchpoint)
18a18393 5334 {
85d721b8
PA
5335 /* There are two cases to consider here:
5336
4a64f543 5337 1. We're watching the triggered memory for reads.
85d721b8
PA
5338 In that case, trust the target, and always report
5339 the watchpoint hit to the user. Even though
5340 reads don't cause value changes, the value may
5341 have changed since the last time it was read, and
5342 since we're not trapping writes, we will not see
5343 those, and as such we should ignore our notion of
5344 old value.
5345
4a64f543 5346 2. We're watching the triggered memory for both
85d721b8
PA
5347 reads and writes. There are two ways this may
5348 happen:
5349
4a64f543 5350 2.1. This is a target that can't break on data
85d721b8
PA
5351 reads only, but can break on accesses (reads or
5352 writes), such as e.g., x86. We detect this case
5353 at the time we try to insert read watchpoints.
5354
4a64f543 5355 2.2. Otherwise, the target supports read
85d721b8
PA
5356 watchpoints, but, the user set an access or write
5357 watchpoint watching the same memory as this read
5358 watchpoint.
5359
5360 If we're watching memory writes as well as reads,
5361 ignore watchpoint hits when we find that the
5362 value hasn't changed, as reads don't cause
5363 changes. This still gives false positives when
5364 the program writes the same value to memory as
5365 what there was already in memory (we will confuse
5366 it for a read), but it's much better than
5367 nothing. */
5368
5369 int other_write_watchpoint = 0;
5370
5371 if (bl->watchpoint_type == hw_read)
5372 {
43892fdf 5373 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5374 if (other_b->type == bp_hardware_watchpoint
5375 || other_b->type == bp_access_watchpoint)
85d721b8 5376 {
3a5c3e22
PA
5377 struct watchpoint *other_w =
5378 (struct watchpoint *) other_b;
5379
5380 if (other_w->watchpoint_triggered
5381 == watch_triggered_yes)
5382 {
5383 other_write_watchpoint = 1;
5384 break;
5385 }
85d721b8
PA
5386 }
5387 }
5388
5389 if (other_write_watchpoint
5390 || bl->watchpoint_type == hw_access)
5391 {
5392 /* We're watching the same memory for writes,
5393 and the value changed since the last time we
5394 updated it, so this trap must be for a write.
5395 Ignore it. */
5396 bs->print_it = print_it_noop;
4ec2227a 5397 bs->stop = false;
85d721b8 5398 }
18a18393
VP
5399 }
5400 break;
5401 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5402 if (b->type == bp_hardware_watchpoint
5403 || b->type == bp_watchpoint)
18a18393
VP
5404 {
5405 /* Don't stop: write watchpoints shouldn't fire if
5406 the value hasn't changed. */
5407 bs->print_it = print_it_noop;
4ec2227a 5408 bs->stop = false;
18a18393
VP
5409 }
5410 /* Stop. */
5411 break;
5412 default:
5413 /* Can't happen. */
18a18393
VP
5414 break;
5415 }
5416 }
d8de7963 5417 else /* !must_check_value */
18a18393
VP
5418 {
5419 /* This is a case where some watchpoint(s) triggered, but
5420 not at the address of this watchpoint, or else no
5421 watchpoint triggered after all. So don't print
5422 anything for this watchpoint. */
5423 bs->print_it = print_it_noop;
4ec2227a 5424 bs->stop = false;
18a18393
VP
5425 }
5426 }
5427}
5428
7d4df6a4
DE
5429/* For breakpoints that are currently marked as telling gdb to stop,
5430 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5431 of breakpoint referred to by BS. If we should not stop for this
5432 breakpoint, set BS->stop to 0. */
f431efe5 5433
18a18393 5434static void
313f3b21 5435bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5436{
d8a77e4c
AB
5437 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5438
2bdf28a0
JK
5439 const struct bp_location *bl;
5440 struct breakpoint *b;
bf469271
PA
5441 /* Assume stop. */
5442 bool condition_result = true;
7d4df6a4
DE
5443 struct expression *cond;
5444
5445 gdb_assert (bs->stop);
2bdf28a0
JK
5446
5447 /* BS is built for existing struct breakpoint. */
b6433ede 5448 bl = bs->bp_location_at.get ();
2bdf28a0 5449 gdb_assert (bl != NULL);
f431efe5 5450 b = bs->breakpoint_at;
2bdf28a0 5451 gdb_assert (b != NULL);
18a18393 5452
d8a77e4c
AB
5453 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5454 thread->ptid.to_string ().c_str (),
5455 b->number, find_loc_num_by_location (bl));
5456
b775012e
LM
5457 /* Even if the target evaluated the condition on its end and notified GDB, we
5458 need to do so again since GDB does not know if we stopped due to a
5459 breakpoint or a single step breakpoint. */
5460
18a18393 5461 if (frame_id_p (b->frame_id)
a0cbd650 5462 && b->frame_id != get_stack_frame_id (get_current_frame ()))
18a18393 5463 {
d8a77e4c
AB
5464 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5465 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5466 b->frame_id.to_string ().c_str ());
4ec2227a 5467 bs->stop = false;
7d4df6a4
DE
5468 return;
5469 }
60e1c644 5470
12ab52e9
PA
5471 /* If this is a thread/task-specific breakpoint, don't waste cpu
5472 evaluating the condition if this isn't the specified
5473 thread/task. */
00431a78 5474 if ((b->thread != -1 && b->thread != thread->global_num)
2ecee236 5475 || (b->task != -1 && b->task != ada_get_task_number (thread)))
6c1b0f7b 5476 {
d8a77e4c 5477 infrun_debug_printf ("incorrect thread or task, not stopping");
4ec2227a 5478 bs->stop = false;
6c1b0f7b
DE
5479 return;
5480 }
5481
6dddc817
DE
5482 /* Evaluate extension language breakpoints that have a "stop" method
5483 implemented. */
5484 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5485
7d4df6a4
DE
5486 if (is_watchpoint (b))
5487 {
5488 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5489
4d01a485 5490 cond = w->cond_exp.get ();
7d4df6a4
DE
5491 }
5492 else
4d01a485 5493 cond = bl->cond.get ();
60e1c644 5494
80e0c6dc 5495 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
7d4df6a4 5496 {
d8de7963 5497 bool within_current_scope = true;
7d4df6a4 5498 struct watchpoint * w;
60e1c644 5499
65558ca5
TT
5500 /* We use scoped_value_mark because it could be a long time
5501 before we return to the command level and call
5502 free_all_values. We can't call free_all_values because we
5503 might be in the middle of evaluating a function call. */
5504 scoped_value_mark mark;
7d4df6a4
DE
5505
5506 if (is_watchpoint (b))
5507 w = (struct watchpoint *) b;
5508 else
5509 w = NULL;
5510
5511 /* Need to select the frame, with all that implies so that
5512 the conditions will have the right context. Because we
5513 use the frame, we will not see an inlined function's
5514 variables when we arrive at a breakpoint at the start
5515 of the inlined function; the current frame will be the
5516 call site. */
5517 if (w == NULL || w->cond_exp_valid_block == NULL)
5518 select_frame (get_current_frame ());
5519 else
18a18393 5520 {
bd2b40ac 5521 frame_info_ptr frame;
7d4df6a4
DE
5522
5523 /* For local watchpoint expressions, which particular
5524 instance of a local is being watched matters, so we
5525 keep track of the frame to evaluate the expression
5526 in. To evaluate the condition however, it doesn't
5527 really matter which instantiation of the function
5528 where the condition makes sense triggers the
5529 watchpoint. This allows an expression like "watch
5530 global if q > 10" set in `func', catch writes to
5531 global on all threads that call `func', or catch
5532 writes on all recursive calls of `func' by a single
5533 thread. We simply always evaluate the condition in
5534 the innermost frame that's executing where it makes
5535 sense to evaluate the condition. It seems
5536 intuitive. */
5537 frame = block_innermost_frame (w->cond_exp_valid_block);
5538 if (frame != NULL)
5539 select_frame (frame);
5540 else
d8de7963 5541 within_current_scope = false;
18a18393 5542 }
7d4df6a4 5543 if (within_current_scope)
bf469271 5544 {
a70b8144 5545 try
bf469271
PA
5546 {
5547 condition_result = breakpoint_cond_eval (cond);
5548 }
230d2906 5549 catch (const gdb_exception &ex)
bf469271
PA
5550 {
5551 exception_fprintf (gdb_stderr, ex,
5552 "Error in testing breakpoint condition:\n");
5553 }
bf469271 5554 }
7d4df6a4 5555 else
18a18393 5556 {
7d4df6a4
DE
5557 warning (_("Watchpoint condition cannot be tested "
5558 "in the current scope"));
5559 /* If we failed to set the right context for this
5560 watchpoint, unconditionally report it. */
18a18393 5561 }
7d4df6a4 5562 /* FIXME-someday, should give breakpoint #. */
18a18393 5563 }
7d4df6a4 5564
80e0c6dc 5565 if (cond != nullptr && !condition_result)
7d4df6a4 5566 {
d8a77e4c 5567 infrun_debug_printf ("condition_result = false, not stopping");
4ec2227a 5568 bs->stop = false;
d8a77e4c 5569 return;
7d4df6a4 5570 }
7d4df6a4
DE
5571 else if (b->ignore_count > 0)
5572 {
d8a77e4c
AB
5573 infrun_debug_printf ("ignore count %d, not stopping",
5574 b->ignore_count);
7d4df6a4 5575 b->ignore_count--;
4ec2227a 5576 bs->stop = false;
7d4df6a4
DE
5577 /* Increase the hit count even though we don't stop. */
5578 ++(b->hit_count);
76727919 5579 gdb::observers::breakpoint_modified.notify (b);
d8a77e4c
AB
5580 return;
5581 }
5582
5583 if (bs->stop)
5584 infrun_debug_printf ("stopping at this breakpoint");
5585 else
5586 infrun_debug_printf ("not stopping at this breakpoint");
18a18393
VP
5587}
5588
1cf4d951
PA
5589/* Returns true if we need to track moribund locations of LOC's type
5590 on the current target. */
5591
d8de7963 5592static bool
b2ff9ed3 5593need_moribund_for_location_type (const struct bp_location *loc)
1cf4d951
PA
5594{
5595 return ((loc->loc_type == bp_loc_software_breakpoint
5596 && !target_supports_stopped_by_sw_breakpoint ())
5597 || (loc->loc_type == bp_loc_hardware_breakpoint
5598 && !target_supports_stopped_by_hw_breakpoint ()));
5599}
5600
ddfe970e 5601/* See breakpoint.h. */
c906108c 5602
313f3b21 5603bpstat *
ddfe970e 5604build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5605 const target_waitstatus &ws)
c906108c 5606{
313f3b21 5607 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5608
43892fdf 5609 for (breakpoint *b : all_breakpoints ())
429374b8 5610 {
1a853c52 5611 if (!breakpoint_enabled (b))
429374b8 5612 continue;
a5606eee 5613
40cb8ca5 5614 for (bp_location *bl : b->locations ())
429374b8 5615 {
4a64f543
MS
5616 /* For hardware watchpoints, we look only at the first
5617 location. The watchpoint_check function will work on the
5618 entire expression, not the individual locations. For
5619 read watchpoints, the watchpoints_triggered function has
5620 checked all locations already. */
429374b8
JK
5621 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5622 break;
18a18393 5623
b5fa468f 5624 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5625 continue;
c5aa993b 5626
09ac7c10 5627 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5628 continue;
c5aa993b 5629
4a64f543
MS
5630 /* Come here if it's a watchpoint, or if the break address
5631 matches. */
c5aa993b 5632
313f3b21 5633 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5634 explain stop. */
c5aa993b 5635
f431efe5
PA
5636 /* Assume we stop. Should we find a watchpoint that is not
5637 actually triggered, or if the condition of the breakpoint
5638 evaluates as false, we'll reset 'stop' to 0. */
4ec2227a
TT
5639 bs->stop = true;
5640 bs->print = true;
d983da9c 5641
f431efe5
PA
5642 /* If this is a scope breakpoint, mark the associated
5643 watchpoint as triggered so that we will handle the
5644 out-of-scope event. We'll get to the watchpoint next
5645 iteration. */
d0fb5eae 5646 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5647 {
5648 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5649
5650 w->watchpoint_triggered = watch_triggered_yes;
5651 }
f431efe5
PA
5652 }
5653 }
5654
7c16b83e 5655 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5656 if (!target_supports_stopped_by_sw_breakpoint ()
5657 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5658 {
1123588c 5659 for (bp_location *loc : moribund_locations)
f431efe5 5660 {
1cf4d951
PA
5661 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5662 && need_moribund_for_location_type (loc))
5663 {
313f3b21 5664 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951 5665 /* For hits of moribund locations, we should just proceed. */
4ec2227a
TT
5666 bs->stop = false;
5667 bs->print = false;
1cf4d951
PA
5668 bs->print_it = print_it_noop;
5669 }
f431efe5
PA
5670 }
5671 }
5672
ddfe970e
KS
5673 return bs_head;
5674}
5675
5676/* See breakpoint.h. */
5677
313f3b21 5678bpstat *
ddfe970e 5679bpstat_stop_status (const address_space *aspace,
00431a78 5680 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5681 const target_waitstatus &ws,
313f3b21 5682 bpstat *stop_chain)
ddfe970e
KS
5683{
5684 struct breakpoint *b = NULL;
5685 /* First item of allocated bpstat's. */
313f3b21
SM
5686 bpstat *bs_head = stop_chain;
5687 bpstat *bs;
ddfe970e
KS
5688 int need_remove_insert;
5689 int removed_any;
5690
5691 /* First, build the bpstat chain with locations that explain a
5692 target stop, while being careful to not set the target running,
5693 as that may invalidate locations (in particular watchpoint
5694 locations are recreated). Resuming will happen here with
5695 breakpoint conditions or watchpoint expressions that include
5696 inferior function calls. */
5697 if (bs_head == NULL)
5698 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5699
edcc5120
TT
5700 /* A bit of special processing for shlib breakpoints. We need to
5701 process solib loading here, so that the lists of loaded and
5702 unloaded libraries are correct before we handle "catch load" and
5703 "catch unload". */
5704 for (bs = bs_head; bs != NULL; bs = bs->next)
5705 {
5d268276 5706 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5707 {
5708 handle_solib_event ();
5709 break;
5710 }
5711 }
5712
f431efe5
PA
5713 /* Now go through the locations that caused the target to stop, and
5714 check whether we're interested in reporting this stop to higher
5715 layers, or whether we should resume the target transparently. */
5716
5717 removed_any = 0;
5718
5760d0ab 5719 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5720 {
5721 if (!bs->stop)
5722 continue;
5723
f431efe5 5724 b = bs->breakpoint_at;
c47614fe 5725 b->check_status (bs);
348d480f 5726 if (bs->stop)
28010a5d 5727 {
00431a78 5728 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5729
429374b8
JK
5730 if (bs->stop)
5731 {
5732 ++(b->hit_count);
c906108c 5733
4a64f543 5734 /* We will stop here. */
429374b8
JK
5735 if (b->disposition == disp_disable)
5736 {
816338b5 5737 --(b->enable_count);
1a853c52 5738 if (b->enable_count <= 0)
429374b8 5739 b->enable_state = bp_disabled;
f431efe5 5740 removed_any = 1;
429374b8 5741 }
bd7ccaa9 5742 gdb::observers::breakpoint_modified.notify (b);
429374b8 5743 if (b->silent)
4ec2227a 5744 bs->print = false;
429374b8 5745 bs->commands = b->commands;
abf85f46 5746 if (command_line_is_silent (bs->commands
d1b0a7bf 5747 ? bs->commands.get () : NULL))
4ec2227a 5748 bs->print = false;
9d6e6e84 5749
c47614fe 5750 b->after_condition_true (bs);
429374b8
JK
5751 }
5752
348d480f 5753 }
a9b3a50f
PA
5754
5755 /* Print nothing for this entry if we don't stop or don't
5756 print. */
5757 if (!bs->stop || !bs->print)
5758 bs->print_it = print_it_noop;
429374b8 5759 }
876fa593 5760
d983da9c
DJ
5761 /* If we aren't stopping, the value of some hardware watchpoint may
5762 not have changed, but the intermediate memory locations we are
5763 watching may have. Don't bother if we're stopping; this will get
5764 done later. */
d832cb68 5765 need_remove_insert = 0;
5760d0ab
JK
5766 if (! bpstat_causes_stop (bs_head))
5767 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5768 if (!bs->stop
f431efe5
PA
5769 && bs->breakpoint_at
5770 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5771 {
3a5c3e22
PA
5772 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5773
d8de7963 5774 update_watchpoint (w, false /* don't reparse. */);
d832cb68 5775 need_remove_insert = 1;
d983da9c
DJ
5776 }
5777
d832cb68 5778 if (need_remove_insert)
44702360 5779 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5780 else if (removed_any)
44702360 5781 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5782
5760d0ab 5783 return bs_head;
c906108c 5784}
628fe4e4 5785
d37e0847
PA
5786/* See breakpoint.h. */
5787
5788bpstat *
5789bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5790 thread_info *thread, const target_waitstatus &ws)
5791{
5792 gdb_assert (!target_stopped_by_watchpoint ());
5793
5794 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5795 previous stop to avoid confusing bpstat_stop_status. */
5796 watchpoints_triggered (ws);
5797
5798 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5799}
5800
628fe4e4 5801static void
61c26be8 5802handle_jit_event (CORE_ADDR address)
628fe4e4 5803{
628fe4e4
JK
5804 struct gdbarch *gdbarch;
5805
1eb8556f 5806 infrun_debug_printf ("handling bp_jit_event");
243a9253 5807
628fe4e4
JK
5808 /* Switch terminal for any messages produced by
5809 breakpoint_re_set. */
223ffa71 5810 target_terminal::ours_for_output ();
628fe4e4 5811
61c26be8
MS
5812 gdbarch = get_frame_arch (get_current_frame ());
5813 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5814 thus it is expected that its objectfile can be found through
5815 minimal symbol lookup. If it doesn't work (and assert fails), it
5816 most likely means that `jit_breakpoint_re_set` was changes and this
5817 function needs to be updated too. */
5818 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5819 gdb_assert (jit_bp_sym.objfile != nullptr);
508ccf9b
TV
5820 objfile *objfile = jit_bp_sym.objfile;
5821 if (objfile->separate_debug_objfile_backlink)
5822 objfile = objfile->separate_debug_objfile_backlink;
5823 jit_event_handler (gdbarch, objfile);
628fe4e4 5824
223ffa71 5825 target_terminal::inferior ();
628fe4e4
JK
5826}
5827
5828/* Prepare WHAT final decision for infrun. */
5829
5830/* Decide what infrun needs to do with this bpstat. */
5831
c906108c 5832struct bpstat_what
313f3b21 5833bpstat_what (bpstat *bs_head)
c906108c 5834{
c906108c 5835 struct bpstat_what retval;
313f3b21 5836 bpstat *bs;
c906108c 5837
628fe4e4 5838 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5839 retval.call_dummy = STOP_NONE;
e2d0f980 5840 retval.is_longjmp = false;
628fe4e4 5841
0e30163f 5842 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5843 {
628fe4e4
JK
5844 /* Extract this BS's action. After processing each BS, we check
5845 if its action overrides all we've seem so far. */
5846 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5847 enum bptype bptype;
5848
c906108c 5849 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5850 {
5851 /* I suspect this can happen if it was a momentary
5852 breakpoint which has since been deleted. */
5853 bptype = bp_none;
5854 }
20874c92 5855 else
f431efe5 5856 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5857
5858 switch (bptype)
c906108c
SS
5859 {
5860 case bp_none:
628fe4e4 5861 break;
c906108c
SS
5862 case bp_breakpoint:
5863 case bp_hardware_breakpoint:
7c16b83e 5864 case bp_single_step:
c906108c
SS
5865 case bp_until:
5866 case bp_finish:
a9b3a50f 5867 case bp_shlib_event:
c906108c
SS
5868 if (bs->stop)
5869 {
5870 if (bs->print)
628fe4e4 5871 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5872 else
628fe4e4 5873 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5874 }
5875 else
628fe4e4 5876 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5877 break;
5878 case bp_watchpoint:
5879 case bp_hardware_watchpoint:
5880 case bp_read_watchpoint:
5881 case bp_access_watchpoint:
5882 if (bs->stop)
5883 {
5884 if (bs->print)
628fe4e4 5885 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5886 else
628fe4e4 5887 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5888 }
5889 else
628fe4e4
JK
5890 {
5891 /* There was a watchpoint, but we're not stopping.
5892 This requires no further action. */
5893 }
c906108c
SS
5894 break;
5895 case bp_longjmp:
e2e4d78b 5896 case bp_longjmp_call_dummy:
186c406b 5897 case bp_exception:
0a39bb32
PA
5898 if (bs->stop)
5899 {
5900 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5901 retval.is_longjmp = bptype != bp_exception;
5902 }
5903 else
5904 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5905 break;
5906 case bp_longjmp_resume:
186c406b 5907 case bp_exception_resume:
0a39bb32
PA
5908 if (bs->stop)
5909 {
5910 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5911 retval.is_longjmp = bptype == bp_longjmp_resume;
5912 }
5913 else
5914 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5915 break;
5916 case bp_step_resume:
5917 if (bs->stop)
628fe4e4
JK
5918 this_action = BPSTAT_WHAT_STEP_RESUME;
5919 else
c906108c 5920 {
628fe4e4
JK
5921 /* It is for the wrong frame. */
5922 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5923 }
c906108c 5924 break;
2c03e5be
PA
5925 case bp_hp_step_resume:
5926 if (bs->stop)
5927 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5928 else
5929 {
5930 /* It is for the wrong frame. */
5931 this_action = BPSTAT_WHAT_SINGLE;
5932 }
5933 break;
c906108c 5934 case bp_watchpoint_scope:
c4093a6a 5935 case bp_thread_event:
1900040c 5936 case bp_overlay_event:
0fd8e87f 5937 case bp_longjmp_master:
aa7d318d 5938 case bp_std_terminate_master:
186c406b 5939 case bp_exception_master:
628fe4e4 5940 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5941 break;
ce78b96d 5942 case bp_catchpoint:
c5aa993b
JM
5943 if (bs->stop)
5944 {
5945 if (bs->print)
628fe4e4 5946 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5947 else
628fe4e4 5948 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5949 }
5950 else
628fe4e4 5951 {
cb1e4e32
PA
5952 /* Some catchpoints are implemented with breakpoints.
5953 For those, we need to step over the breakpoint. */
6e14e441
PA
5954 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5955 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
cb1e4e32 5956 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5957 }
5958 break;
628fe4e4 5959 case bp_jit_event:
628fe4e4 5960 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5961 break;
c906108c 5962 case bp_call_dummy:
53a5351d
JM
5963 /* Make sure the action is stop (silent or noisy),
5964 so infrun.c pops the dummy frame. */
aa7d318d 5965 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5966 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5967 break;
5968 case bp_std_terminate:
5969 /* Make sure the action is stop (silent or noisy),
5970 so infrun.c pops the dummy frame. */
aa7d318d 5971 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5972 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5973 break;
1042e4c0 5974 case bp_tracepoint:
7a697b8d 5975 case bp_fast_tracepoint:
0fb4aa4b 5976 case bp_static_tracepoint:
7b572efb 5977 case bp_static_marker_tracepoint:
1042e4c0
SS
5978 /* Tracepoint hits should not be reported back to GDB, and
5979 if one got through somehow, it should have been filtered
5980 out already. */
f34652de 5981 internal_error (_("bpstat_what: tracepoint encountered"));
0e30163f
JK
5982 break;
5983 case bp_gnu_ifunc_resolver:
5984 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5985 this_action = BPSTAT_WHAT_SINGLE;
5986 break;
5987 case bp_gnu_ifunc_resolver_return:
5988 /* The breakpoint will be removed, execution will restart from the
5989 PC of the former breakpoint. */
5990 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5991 break;
e7e0cddf
SS
5992
5993 case bp_dprintf:
a11cfd87
HZ
5994 if (bs->stop)
5995 this_action = BPSTAT_WHAT_STOP_SILENT;
5996 else
5997 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5998 break;
5999
628fe4e4 6000 default:
f34652de 6001 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 6002 }
628fe4e4 6003
325fac50 6004 retval.main_action = std::max (retval.main_action, this_action);
c906108c 6005 }
628fe4e4 6006
243a9253
PA
6007 return retval;
6008}
628fe4e4 6009
243a9253 6010void
313f3b21 6011bpstat_run_callbacks (bpstat *bs_head)
243a9253 6012{
313f3b21 6013 bpstat *bs;
628fe4e4 6014
0e30163f
JK
6015 for (bs = bs_head; bs != NULL; bs = bs->next)
6016 {
6017 struct breakpoint *b = bs->breakpoint_at;
6018
6019 if (b == NULL)
6020 continue;
6021 switch (b->type)
6022 {
243a9253 6023 case bp_jit_event:
61c26be8 6024 handle_jit_event (bs->bp_location_at->address);
243a9253 6025 break;
0e30163f 6026 case bp_gnu_ifunc_resolver:
74421c0b 6027 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
0e30163f
JK
6028 break;
6029 case bp_gnu_ifunc_resolver_return:
74421c0b 6030 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
0e30163f
JK
6031 break;
6032 }
6033 }
c906108c
SS
6034}
6035
4c462cb0 6036/* See breakpoint.h. */
c906108c 6037
4c462cb0
SM
6038bool
6039bpstat_should_step ()
c906108c 6040{
43892fdf 6041 for (breakpoint *b : all_breakpoints ())
717a8278 6042 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 6043 return true;
43892fdf 6044
4c462cb0 6045 return false;
c906108c
SS
6046}
6047
4c462cb0
SM
6048/* See breakpoint.h. */
6049
6050bool
313f3b21 6051bpstat_causes_stop (bpstat *bs)
67822962
PA
6052{
6053 for (; bs != NULL; bs = bs->next)
6054 if (bs->stop)
4c462cb0 6055 return true;
67822962 6056
4c462cb0 6057 return false;
67822962
PA
6058}
6059
c906108c 6060\f
c5aa993b 6061
6c92c339 6062/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
6063 so it starts at the position corresponding to the table column
6064 named COL_NAME in the currently active table of UIOUT. */
6065
6c92c339 6066static int
170b53b2
UW
6067wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6068{
170b53b2 6069 int i, total_width, width, align;
c5209615 6070 const char *text;
170b53b2
UW
6071
6072 total_width = 0;
112e8700 6073 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6074 {
6075 if (strcmp (text, col_name) == 0)
6c92c339 6076 return total_width;
170b53b2
UW
6077
6078 total_width += width + 1;
6079 }
6080
6c92c339 6081 return 0;
170b53b2
UW
6082}
6083
b775012e
LM
6084/* Determine if the locations of this breakpoint will have their conditions
6085 evaluated by the target, host or a mix of both. Returns the following:
6086
6087 "host": Host evals condition.
6088 "host or target": Host or Target evals condition.
6089 "target": Target evals condition.
6090*/
6091
6092static const char *
a67bcaba 6093bp_condition_evaluator (const breakpoint *b)
b775012e 6094{
b775012e
LM
6095 char host_evals = 0;
6096 char target_evals = 0;
6097
6098 if (!b)
6099 return NULL;
6100
6101 if (!is_breakpoint (b))
6102 return NULL;
6103
6104 if (gdb_evaluates_breakpoint_condition_p ()
6105 || !target_supports_evaluation_of_breakpoint_conditions ())
6106 return condition_evaluation_host;
6107
40cb8ca5 6108 for (bp_location *bl : b->locations ())
b775012e
LM
6109 {
6110 if (bl->cond_bytecode)
6111 target_evals++;
6112 else
6113 host_evals++;
6114 }
6115
6116 if (host_evals && target_evals)
6117 return condition_evaluation_both;
6118 else if (target_evals)
6119 return condition_evaluation_target;
6120 else
6121 return condition_evaluation_host;
6122}
6123
6124/* Determine the breakpoint location's condition evaluator. This is
6125 similar to bp_condition_evaluator, but for locations. */
6126
6127static const char *
b2ff9ed3 6128bp_location_condition_evaluator (const struct bp_location *bl)
b775012e
LM
6129{
6130 if (bl && !is_breakpoint (bl->owner))
6131 return NULL;
6132
6133 if (gdb_evaluates_breakpoint_condition_p ()
6134 || !target_supports_evaluation_of_breakpoint_conditions ())
6135 return condition_evaluation_host;
6136
6137 if (bl && bl->cond_bytecode)
6138 return condition_evaluation_target;
6139 else
6140 return condition_evaluation_host;
6141}
6142
859825b8
JK
6143/* Print the LOC location out of the list of B->LOC locations. */
6144
170b53b2 6145static void
a67bcaba 6146print_breakpoint_location (const breakpoint *b,
170b53b2 6147 struct bp_location *loc)
0d381245 6148{
79a45e25 6149 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6150
6151 scoped_restore_current_program_space restore_pspace;
6c95b8df 6152
859825b8
JK
6153 if (loc != NULL && loc->shlib_disabled)
6154 loc = NULL;
6155
6c95b8df
PA
6156 if (loc != NULL)
6157 set_current_program_space (loc->pspace);
6158
56435ebe 6159 if (b->display_canonical)
709438c7 6160 uiout->field_string ("what", b->locspec->to_string ());
2f202fde 6161 else if (loc && loc->symtab)
0d381245 6162 {
4a27f119
KS
6163 const struct symbol *sym = loc->symbol;
6164
0d381245
VP
6165 if (sym)
6166 {
112e8700 6167 uiout->text ("in ");
987012b8 6168 uiout->field_string ("func", sym->print_name (),
e43b10e1 6169 function_name_style.style ());
112e8700
SM
6170 uiout->text (" ");
6171 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6172 uiout->text ("at ");
0d381245 6173 }
112e8700 6174 uiout->field_string ("file",
cbe56571 6175 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6176 file_name_style.style ());
112e8700 6177 uiout->text (":");
05cba821 6178
112e8700
SM
6179 if (uiout->is_mi_like_p ())
6180 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6181
381befee 6182 uiout->field_signed ("line", loc->line_number);
0d381245 6183 }
859825b8 6184 else if (loc)
0d381245 6185 {
d7e74731 6186 string_file stb;
170b53b2 6187
d7e74731 6188 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6189 demangle, "");
112e8700 6190 uiout->field_stream ("at", stb);
0d381245 6191 }
859825b8 6192 else
f00aae0f 6193 {
709438c7 6194 uiout->field_string ("pending", b->locspec->to_string ());
f00aae0f
KS
6195 /* If extra_string is available, it could be holding a condition
6196 or dprintf arguments. In either case, make sure it is printed,
6197 too, but only for non-MI streams. */
112e8700 6198 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6199 {
6200 if (b->type == bp_dprintf)
112e8700 6201 uiout->text (",");
f00aae0f 6202 else
112e8700 6203 uiout->text (" ");
6f781ee3 6204 uiout->text (b->extra_string.get ());
f00aae0f
KS
6205 }
6206 }
6c95b8df 6207
b775012e
LM
6208 if (loc && is_breakpoint (b)
6209 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6210 && bp_condition_evaluator (b) == condition_evaluation_both)
6211 {
112e8700
SM
6212 uiout->text (" (");
6213 uiout->field_string ("evaluated-by",
b775012e 6214 bp_location_condition_evaluator (loc));
112e8700 6215 uiout->text (")");
b775012e 6216 }
0d381245
VP
6217}
6218
269b11a2
PA
6219static const char *
6220bptype_string (enum bptype type)
c906108c 6221{
c4093a6a
JM
6222 struct ep_type_description
6223 {
6224 enum bptype type;
a121b7c1 6225 const char *description;
c4093a6a
JM
6226 };
6227 static struct ep_type_description bptypes[] =
c906108c 6228 {
c5aa993b
JM
6229 {bp_none, "?deleted?"},
6230 {bp_breakpoint, "breakpoint"},
c906108c 6231 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6232 {bp_single_step, "sw single-step"},
c5aa993b
JM
6233 {bp_until, "until"},
6234 {bp_finish, "finish"},
6235 {bp_watchpoint, "watchpoint"},
c906108c 6236 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6237 {bp_read_watchpoint, "read watchpoint"},
6238 {bp_access_watchpoint, "acc watchpoint"},
6239 {bp_longjmp, "longjmp"},
6240 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6241 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6242 {bp_exception, "exception"},
6243 {bp_exception_resume, "exception resume"},
c5aa993b 6244 {bp_step_resume, "step resume"},
2c03e5be 6245 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6246 {bp_watchpoint_scope, "watchpoint scope"},
6247 {bp_call_dummy, "call dummy"},
aa7d318d 6248 {bp_std_terminate, "std::terminate"},
c5aa993b 6249 {bp_shlib_event, "shlib events"},
c4093a6a 6250 {bp_thread_event, "thread events"},
1900040c 6251 {bp_overlay_event, "overlay events"},
0fd8e87f 6252 {bp_longjmp_master, "longjmp master"},
aa7d318d 6253 {bp_std_terminate_master, "std::terminate master"},
186c406b 6254 {bp_exception_master, "exception master"},
ce78b96d 6255 {bp_catchpoint, "catchpoint"},
1042e4c0 6256 {bp_tracepoint, "tracepoint"},
7a697b8d 6257 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6258 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6259 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6260 {bp_dprintf, "dprintf"},
4efc6507 6261 {bp_jit_event, "jit events"},
0e30163f
JK
6262 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6263 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6264 };
269b11a2
PA
6265
6266 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6267 || ((int) type != bptypes[(int) type].type))
f34652de 6268 internal_error (_("bptypes table does not describe type #%d."),
269b11a2
PA
6269 (int) type);
6270
6271 return bptypes[(int) type].description;
6272}
6273
998580f1
MK
6274/* For MI, output a field named 'thread-groups' with a list as the value.
6275 For CLI, prefix the list with the string 'inf'. */
6276
6277static void
6278output_thread_groups (struct ui_out *uiout,
6279 const char *field_name,
5c632425 6280 const std::vector<int> &inf_nums,
998580f1
MK
6281 int mi_only)
6282{
112e8700 6283 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6284
6285 /* For backward compatibility, don't display inferiors in CLI unless
6286 there are several. Always display them for MI. */
6287 if (!is_mi && mi_only)
6288 return;
6289
10f489e5 6290 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6291
5c632425 6292 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6293 {
6294 if (is_mi)
6295 {
6296 char mi_group[10];
6297
5c632425 6298 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6299 uiout->field_string (NULL, mi_group);
998580f1
MK
6300 }
6301 else
6302 {
6303 if (i == 0)
112e8700 6304 uiout->text (" inf ");
998580f1 6305 else
112e8700 6306 uiout->text (", ");
998580f1 6307
5c632425 6308 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6309 }
6310 }
998580f1
MK
6311}
6312
9db0d853
SM
6313/* See breakpoint.h. */
6314
6315bool fix_breakpoint_script_output_globally = false;
6316
a38118e5
PA
6317/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6318 instead of going via breakpoint_ops::print_one. This makes "maint
6319 info breakpoints" show the software breakpoint locations of
6320 catchpoints, which are considered internal implementation
c01e038b
TT
6321 detail. Returns true if RAW_LOC is false and if the breakpoint's
6322 print_one method did something; false otherwise. */
269b11a2 6323
c01e038b 6324static bool
269b11a2
PA
6325print_one_breakpoint_location (struct breakpoint *b,
6326 struct bp_location *loc,
6327 int loc_number,
6328 struct bp_location **last_loc,
a38118e5 6329 int allflag, bool raw_loc)
269b11a2
PA
6330{
6331 struct command_line *l;
c2c6d25f 6332 static char bpenables[] = "nynny";
c906108c 6333
79a45e25 6334 struct ui_out *uiout = current_uiout;
d8de7963
AB
6335 bool header_of_multiple = false;
6336 bool part_of_multiple = (loc != NULL);
79a45b7d
TT
6337 struct value_print_options opts;
6338
6339 get_user_print_options (&opts);
0d381245
VP
6340
6341 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6342 /* See comment in print_one_breakpoint concerning treatment of
6343 breakpoints with single disabled location. */
0d381245
VP
6344 if (loc == NULL
6345 && (b->loc != NULL
b5fa468f
TBA
6346 && (b->loc->next != NULL
6347 || !b->loc->enabled || b->loc->disabled_by_cond)))
d8de7963 6348 header_of_multiple = true;
0d381245
VP
6349 if (loc == NULL)
6350 loc = b->loc;
6351
c4093a6a
JM
6352 annotate_record ();
6353
6354 /* 1 */
6355 annotate_field (0);
0d381245 6356 if (part_of_multiple)
528e1572 6357 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6358 else
381befee 6359 uiout->field_signed ("number", b->number);
c4093a6a
JM
6360
6361 /* 2 */
6362 annotate_field (1);
0d381245 6363 if (part_of_multiple)
112e8700 6364 uiout->field_skip ("type");
269b11a2 6365 else
112e8700 6366 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6367
6368 /* 3 */
6369 annotate_field (2);
0d381245 6370 if (part_of_multiple)
112e8700 6371 uiout->field_skip ("disp");
0d381245 6372 else
112e8700 6373 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6374
c4093a6a
JM
6375 /* 4 */
6376 annotate_field (3);
0d381245 6377 if (part_of_multiple)
fbcda577
PA
6378 {
6379 /* For locations that are disabled because of an invalid
6380 condition, display "N*" on the CLI, where "*" refers to a
6381 footnote below the table. For MI, simply display a "N"
6382 without a footnote. On the CLI, for enabled locations whose
6383 breakpoint is disabled, display "y-". */
6384 auto get_enable_state = [uiout, loc] () -> const char *
6385 {
6386 if (uiout->is_mi_like_p ())
6387 {
6388 if (loc->disabled_by_cond)
6389 return "N";
6390 else if (!loc->enabled)
6391 return "n";
6392 else
6393 return "y";
6394 }
6395 else
6396 {
6397 if (loc->disabled_by_cond)
6398 return "N*";
6399 else if (!loc->enabled)
6400 return "n";
6401 else if (!breakpoint_enabled (loc->owner))
6402 return "y-";
6403 else
6404 return "y";
6405 }
6406 };
6407 uiout->field_string ("enabled", get_enable_state ());
6408 }
0d381245 6409 else
112e8700 6410 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6411
c4093a6a 6412 /* 5 and 6 */
c01e038b 6413 bool result = false;
c47614fe 6414 if (!raw_loc && b->print_one (last_loc))
c01e038b 6415 result = true;
3086aeae 6416 else
a38118e5
PA
6417 {
6418 if (is_watchpoint (b))
3a5c3e22
PA
6419 {
6420 struct watchpoint *w = (struct watchpoint *) b;
6421
6422 /* Field 4, the address, is omitted (which makes the columns
6423 not line up too nicely with the headers, but the effect
6424 is relatively readable). */
6425 if (opts.addressprint)
112e8700 6426 uiout->field_skip ("addr");
3a5c3e22 6427 annotate_field (5);
a4c50be3 6428 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6429 }
f06f1252
TT
6430 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6431 || is_ada_exception_catchpoint (b))
a38118e5
PA
6432 {
6433 if (opts.addressprint)
6434 {
6435 annotate_field (4);
6436 if (header_of_multiple)
7f6aba03
TT
6437 uiout->field_string ("addr", "<MULTIPLE>",
6438 metadata_style.style ());
a38118e5 6439 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6440 uiout->field_string ("addr", "<PENDING>",
6441 metadata_style.style ());
a38118e5
PA
6442 else
6443 uiout->field_core_addr ("addr",
6444 loc->gdbarch, loc->address);
6445 }
6446 annotate_field (5);
6447 if (!header_of_multiple)
6448 print_breakpoint_location (b, loc);
6449 if (b->loc)
6450 *last_loc = b->loc;
6451 }
6452 }
6c95b8df 6453
998580f1 6454 if (loc != NULL && !header_of_multiple)
6c95b8df 6455 {
5c632425 6456 std::vector<int> inf_nums;
998580f1 6457 int mi_only = 1;
6c95b8df 6458
08036331 6459 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6460 {
6461 if (inf->pspace == loc->pspace)
5c632425 6462 inf_nums.push_back (inf->num);
6c95b8df 6463 }
998580f1 6464
dda83cd7 6465 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6466 there are several. Always display for MI. */
6467 if (allflag
6468 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6469 && (program_spaces.size () > 1
998580f1
MK
6470 || number_of_inferiors () > 1)
6471 /* LOC is for existing B, it cannot be in
6472 moribund_locations and thus having NULL OWNER. */
6473 && loc->owner->type != bp_catchpoint))
6474 mi_only = 0;
5c632425 6475 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6476 }
6477
944b1b18
AB
6478 /* In the MI output, each location of a thread or task specific
6479 breakpoint includes the relevant thread or task ID. This is done for
6480 backwards compatibility reasons.
6481
6482 For the CLI output, the thread/task information is printed on a
6483 separate line, see the 'stop only in thread' and 'stop only in task'
6484 output below. */
6485 if (!header_of_multiple && uiout->is_mi_like_p ())
c4093a6a 6486 {
4a306c9a 6487 if (b->thread != -1)
944b1b18 6488 uiout->field_signed ("thread", b->thread);
2ecee236 6489 else if (b->task != -1)
944b1b18 6490 uiout->field_signed ("task", b->task);
c4093a6a 6491 }
f1310107 6492
112e8700 6493 uiout->text ("\n");
f1310107 6494
348d480f 6495 if (!part_of_multiple)
c47614fe 6496 b->print_one_detail (uiout);
f1310107 6497
0d381245 6498 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6499 {
6500 annotate_field (6);
112e8700 6501 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6502 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6503 the frame ID. */
112e8700 6504 uiout->field_core_addr ("frame",
5af949e3 6505 b->gdbarch, b->frame_id.stack_addr);
112e8700 6506 uiout->text ("\n");
c4093a6a
JM
6507 }
6508
28010a5d 6509 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6510 {
6511 annotate_field (7);
d77f58be 6512 if (is_tracepoint (b))
112e8700 6513 uiout->text ("\ttrace only if ");
1042e4c0 6514 else
112e8700 6515 uiout->text ("\tstop only if ");
6f781ee3 6516 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6517
6518 /* Print whether the target is doing the breakpoint's condition
6519 evaluation. If GDB is doing the evaluation, don't print anything. */
6520 if (is_breakpoint (b)
6521 && breakpoint_condition_evaluation_mode ()
6522 == condition_evaluation_target)
6523 {
6a831f06
PA
6524 uiout->message (" (%pF evals)",
6525 string_field ("evaluated-by",
6526 bp_condition_evaluator (b)));
b775012e 6527 }
112e8700 6528 uiout->text ("\n");
0101ce28
JJ
6529 }
6530
0d381245 6531 if (!part_of_multiple && b->thread != -1)
c4093a6a 6532 {
4a64f543 6533 /* FIXME should make an annotation for this. */
112e8700
SM
6534 uiout->text ("\tstop only in thread ");
6535 if (uiout->is_mi_like_p ())
381befee 6536 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6537 else
6538 {
6539 struct thread_info *thr = find_thread_global_id (b->thread);
6540
112e8700 6541 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6542 }
112e8700 6543 uiout->text ("\n");
c4093a6a 6544 }
944b1b18 6545
2ecee236 6546 if (!part_of_multiple && b->task != -1)
944b1b18
AB
6547 {
6548 uiout->text ("\tstop only in task ");
6549 uiout->field_signed ("task", b->task);
6550 uiout->text ("\n");
6551 }
6552
556ec64d
YQ
6553 if (!part_of_multiple)
6554 {
6555 if (b->hit_count)
31f56a27
YQ
6556 {
6557 /* FIXME should make an annotation for this. */
6558 if (is_catchpoint (b))
112e8700 6559 uiout->text ("\tcatchpoint");
31f56a27 6560 else if (is_tracepoint (b))
112e8700 6561 uiout->text ("\ttracepoint");
31f56a27 6562 else
112e8700
SM
6563 uiout->text ("\tbreakpoint");
6564 uiout->text (" already hit ");
381befee 6565 uiout->field_signed ("times", b->hit_count);
31f56a27 6566 if (b->hit_count == 1)
112e8700 6567 uiout->text (" time\n");
31f56a27 6568 else
112e8700 6569 uiout->text (" times\n");
31f56a27 6570 }
556ec64d
YQ
6571 else
6572 {
31f56a27 6573 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6574 if (uiout->is_mi_like_p ())
381befee 6575 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6576 }
6577 }
8b93c638 6578
0d381245 6579 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6580 {
6581 annotate_field (8);
6a831f06
PA
6582 uiout->message ("\tignore next %pF hits\n",
6583 signed_field ("ignore", b->ignore_count));
c4093a6a 6584 }
059fb39f 6585
816338b5
SS
6586 /* Note that an enable count of 1 corresponds to "enable once"
6587 behavior, which is reported by the combination of enablement and
6588 disposition, so we don't need to mention it here. */
6589 if (!part_of_multiple && b->enable_count > 1)
6590 {
6591 annotate_field (8);
112e8700 6592 uiout->text ("\tdisable after ");
816338b5
SS
6593 /* Tweak the wording to clarify that ignore and enable counts
6594 are distinct, and have additive effect. */
6595 if (b->ignore_count)
112e8700 6596 uiout->text ("additional ");
816338b5 6597 else
112e8700 6598 uiout->text ("next ");
381befee 6599 uiout->field_signed ("enable", b->enable_count);
112e8700 6600 uiout->text (" hits\n");
816338b5
SS
6601 }
6602
f196051f
SS
6603 if (!part_of_multiple && is_tracepoint (b))
6604 {
6605 struct tracepoint *tp = (struct tracepoint *) b;
6606
6607 if (tp->traceframe_usage)
6608 {
112e8700 6609 uiout->text ("\ttrace buffer usage ");
381befee 6610 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6611 uiout->text (" bytes\n");
f196051f
SS
6612 }
6613 }
d3ce09f5 6614
d1b0a7bf 6615 l = b->commands ? b->commands.get () : NULL;
059fb39f 6616 if (!part_of_multiple && l)
c4093a6a
JM
6617 {
6618 annotate_field (9);
9db0d853
SM
6619
6620 bool use_fixed_output =
6621 (uiout->test_flags (fix_breakpoint_script_output)
6622 || fix_breakpoint_script_output_globally);
6623
6624 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6625 gdb::optional<ui_out_emit_list> list_emitter;
6626
6627 if (use_fixed_output)
6628 list_emitter.emplace (uiout, "script");
6629 else
6630 tuple_emitter.emplace (uiout, "script");
6631
8b93c638 6632 print_command_lines (uiout, l, 4);
c4093a6a 6633 }
d24317b4 6634
d9b3f62e 6635 if (is_tracepoint (b))
1042e4c0 6636 {
d9b3f62e
PA
6637 struct tracepoint *t = (struct tracepoint *) b;
6638
6639 if (!part_of_multiple && t->pass_count)
6640 {
6641 annotate_field (10);
112e8700 6642 uiout->text ("\tpass count ");
381befee 6643 uiout->field_signed ("pass", t->pass_count);
112e8700 6644 uiout->text (" \n");
d9b3f62e 6645 }
f2a8bc8a
YQ
6646
6647 /* Don't display it when tracepoint or tracepoint location is
6648 pending. */
6649 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6650 {
6651 annotate_field (11);
6652
112e8700
SM
6653 if (uiout->is_mi_like_p ())
6654 uiout->field_string ("installed",
f2a8bc8a
YQ
6655 loc->inserted ? "y" : "n");
6656 else
6657 {
6658 if (loc->inserted)
112e8700 6659 uiout->text ("\t");
f2a8bc8a 6660 else
112e8700
SM
6661 uiout->text ("\tnot ");
6662 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6663 }
6664 }
1042e4c0
SS
6665 }
6666
112e8700 6667 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6668 {
3a5c3e22
PA
6669 if (is_watchpoint (b))
6670 {
6671 struct watchpoint *w = (struct watchpoint *) b;
6672
a4c50be3 6673 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6674 }
264f9890
PA
6675 else if (b->locspec != nullptr)
6676 {
709438c7 6677 const char *str = b->locspec->to_string ();
264f9890
PA
6678 if (str != nullptr)
6679 uiout->field_string ("original-location", str);
6680 }
d24317b4 6681 }
c01e038b
TT
6682
6683 return result;
c4093a6a 6684}
c5aa993b 6685
13674803
SM
6686/* See breakpoint.h. */
6687
6688bool fix_multi_location_breakpoint_output_globally = false;
6689
0d381245
VP
6690static void
6691print_one_breakpoint (struct breakpoint *b,
4a64f543 6692 struct bp_location **last_loc,
6c95b8df 6693 int allflag)
0d381245 6694{
79a45e25 6695 struct ui_out *uiout = current_uiout;
13674803
SM
6696 bool use_fixed_output
6697 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6698 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6699
b4be1b06 6700 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
c01e038b
TT
6701 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6702 allflag, false);
8d3788bd 6703
b4be1b06
SM
6704 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6705 are outside. */
6706 if (!use_fixed_output)
6707 bkpt_tuple_emitter.reset ();
0d381245
VP
6708
6709 /* If this breakpoint has custom print function,
6710 it's already printed. Otherwise, print individual
6711 locations, if any. */
c01e038b 6712 if (!printed || allflag)
0d381245 6713 {
4a64f543
MS
6714 /* If breakpoint has a single location that is disabled, we
6715 print it as if it had several locations, since otherwise it's
6716 hard to represent "breakpoint enabled, location disabled"
6717 situation.
6718
6719 Note that while hardware watchpoints have several locations
a38118e5
PA
6720 internally, that's not a property exposed to users.
6721
6722 Likewise, while catchpoints may be implemented with
6723 breakpoints (e.g., catch throw), that's not a property
6724 exposed to users. We do however display the internal
6725 breakpoint locations with "maint info breakpoints". */
6726 if (!is_hardware_watchpoint (b)
f06f1252
TT
6727 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6728 || is_ada_exception_catchpoint (b))
a38118e5 6729 && (allflag
b5fa468f
TBA
6730 || (b->loc && (b->loc->next
6731 || !b->loc->enabled
6732 || b->loc->disabled_by_cond))))
0d381245 6733 {
b4be1b06
SM
6734 gdb::optional<ui_out_emit_list> locations_list;
6735
6736 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6737 MI record. For later versions, place breakpoint locations in a
6738 list. */
6739 if (uiout->is_mi_like_p () && use_fixed_output)
6740 locations_list.emplace (uiout, "locations");
8d3788bd 6741
b4be1b06 6742 int n = 1;
40cb8ca5 6743 for (bp_location *loc : b->locations ())
8d3788bd 6744 {
b4be1b06 6745 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6746 print_one_breakpoint_location (b, loc, n, last_loc,
6747 allflag, allflag);
40cb8ca5 6748 n++;
8d3788bd 6749 }
0d381245
VP
6750 }
6751 }
6752}
6753
a6d9a66e
UW
6754static int
6755breakpoint_address_bits (struct breakpoint *b)
6756{
6757 int print_address_bits = 0;
a6d9a66e 6758
40cb8ca5 6759 for (bp_location *loc : b->locations ())
a6d9a66e 6760 {
6e14e441
PA
6761 if (!bl_address_is_meaningful (loc))
6762 continue;
c7437ca6 6763
6e14e441 6764 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6765 if (addr_bit > print_address_bits)
6766 print_address_bits = addr_bit;
6767 }
6768
6769 return print_address_bits;
6770}
0d381245 6771
65630365 6772/* See breakpoint.h. */
c5aa993b 6773
65630365
PA
6774void
6775print_breakpoint (breakpoint *b)
c4093a6a 6776{
a6d9a66e 6777 struct bp_location *dummy_loc = NULL;
65630365 6778 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6779}
c5aa993b 6780
09d682a4
TT
6781/* Return true if this breakpoint was set by the user, false if it is
6782 internal or momentary. */
6783
6784int
6785user_breakpoint_p (struct breakpoint *b)
6786{
46c6471b 6787 return b->number > 0;
09d682a4
TT
6788}
6789
93daf339
TT
6790/* See breakpoint.h. */
6791
6792int
6793pending_breakpoint_p (struct breakpoint *b)
6794{
6795 return b->loc == NULL;
6796}
6797
5c458ae8
SM
6798/* Print information on breakpoints (including watchpoints and tracepoints).
6799
6800 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6801 understood by number_or_range_parser. Only breakpoints included in this
6802 list are then printed.
6803
6804 If SHOW_INTERNAL is true, print internal breakpoints.
6805
6806 If FILTER is non-NULL, call it on each breakpoint and only include the
6807 ones for which it returns true.
6808
6809 Return the total number of breakpoints listed. */
c906108c 6810
d77f58be 6811static int
5c458ae8 6812breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6813 bool (*filter) (const struct breakpoint *))
c4093a6a 6814{
a6d9a66e 6815 struct bp_location *last_loc = NULL;
7f3b0473 6816 int nr_printable_breakpoints;
79a45b7d 6817 struct value_print_options opts;
a6d9a66e 6818 int print_address_bits = 0;
269b11a2 6819 int print_type_col_width = 14;
79a45e25 6820 struct ui_out *uiout = current_uiout;
b5fa468f 6821 bool has_disabled_by_cond_location = false;
269b11a2 6822
79a45b7d
TT
6823 get_user_print_options (&opts);
6824
4a64f543
MS
6825 /* Compute the number of rows in the table, as well as the size
6826 required for address fields. */
7f3b0473 6827 nr_printable_breakpoints = 0;
43892fdf 6828 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6829 {
6830 /* If we have a filter, only list the breakpoints it accepts. */
6831 if (filter && !filter (b))
6832 continue;
6833
5c458ae8 6834 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6835 accept. Skip the others. */
5c458ae8 6836 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6837 {
5c458ae8 6838 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6839 continue;
5c458ae8 6840 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6841 continue;
6842 }
269b11a2 6843
5c458ae8 6844 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6845 {
6846 int addr_bit, type_len;
a6d9a66e 6847
e5a67952
MS
6848 addr_bit = breakpoint_address_bits (b);
6849 if (addr_bit > print_address_bits)
6850 print_address_bits = addr_bit;
269b11a2 6851
e5a67952
MS
6852 type_len = strlen (bptype_string (b->type));
6853 if (type_len > print_type_col_width)
6854 print_type_col_width = type_len;
6855
6856 nr_printable_breakpoints++;
6857 }
6858 }
7f3b0473 6859
4a2b031d
TT
6860 {
6861 ui_out_emit_table table_emitter (uiout,
6862 opts.addressprint ? 6 : 5,
6863 nr_printable_breakpoints,
6864 "BreakpointTable");
6865
6866 if (nr_printable_breakpoints > 0)
6867 annotate_breakpoints_headers ();
6868 if (nr_printable_breakpoints > 0)
6869 annotate_field (0);
6870 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6871 if (nr_printable_breakpoints > 0)
6872 annotate_field (1);
6873 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6874 if (nr_printable_breakpoints > 0)
6875 annotate_field (2);
6876 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6877 if (nr_printable_breakpoints > 0)
6878 annotate_field (3);
6879 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6880 if (opts.addressprint)
6881 {
6882 if (nr_printable_breakpoints > 0)
6883 annotate_field (4);
6884 if (print_address_bits <= 32)
6885 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6886 else
6887 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6888 }
6889 if (nr_printable_breakpoints > 0)
6890 annotate_field (5);
6891 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6892 uiout->table_body ();
6893 if (nr_printable_breakpoints > 0)
6894 annotate_breakpoints_table ();
6895
43892fdf 6896 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6897 {
6898 QUIT;
6899 /* If we have a filter, only list the breakpoints it accepts. */
6900 if (filter && !filter (b))
6901 continue;
e5a67952 6902
5c458ae8 6903 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6904 accept. Skip the others. */
e5a67952 6905
5c458ae8 6906 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6907 {
5c458ae8 6908 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6909 {
5c458ae8 6910 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6911 continue;
6912 }
6913 else /* all others */
6914 {
5c458ae8 6915 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6916 continue;
6917 }
6918 }
6919 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6920 show_internal is set. */
6921 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6922 {
6923 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6924 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6925 if (loc->disabled_by_cond)
6926 has_disabled_by_cond_location = true;
6927 }
4a2b031d
TT
6928 }
6929 }
698384cd 6930
7f3b0473 6931 if (nr_printable_breakpoints == 0)
c906108c 6932 {
4a64f543
MS
6933 /* If there's a filter, let the caller decide how to report
6934 empty list. */
d77f58be
SS
6935 if (!filter)
6936 {
5c458ae8 6937 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6938 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6939 else
112e8700 6940 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6941 bp_num_list);
d77f58be 6942 }
c906108c
SS
6943 }
6944 else
c4093a6a 6945 {
a6d9a66e
UW
6946 if (last_loc && !server_command)
6947 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6948
85c88e2a 6949 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6950 uiout->message (_("(*): Breakpoint condition is invalid at this "
6951 "location.\n"));
c4093a6a 6952 }
c906108c 6953
4a64f543 6954 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6955 there have been breakpoints? */
c906108c 6956 annotate_breakpoints_table_end ();
d77f58be
SS
6957
6958 return nr_printable_breakpoints;
c906108c
SS
6959}
6960
ad443146
SS
6961/* Display the value of default-collect in a way that is generally
6962 compatible with the breakpoint list. */
6963
6964static void
6965default_collect_info (void)
6966{
79a45e25
PA
6967 struct ui_out *uiout = current_uiout;
6968
ad443146
SS
6969 /* If it has no value (which is frequently the case), say nothing; a
6970 message like "No default-collect." gets in user's face when it's
6971 not wanted. */
e0700ba4 6972 if (default_collect.empty ())
ad443146
SS
6973 return;
6974
6975 /* The following phrase lines up nicely with per-tracepoint collect
6976 actions. */
112e8700
SM
6977 uiout->text ("default collect ");
6978 uiout->field_string ("default-collect", default_collect);
6979 uiout->text (" \n");
ad443146
SS
6980}
6981
c906108c 6982static void
0b39b52e 6983info_breakpoints_command (const char *args, int from_tty)
c906108c 6984{
5c458ae8 6985 breakpoint_1 (args, false, NULL);
ad443146
SS
6986
6987 default_collect_info ();
d77f58be
SS
6988}
6989
6990static void
1d12d88f 6991info_watchpoints_command (const char *args, int from_tty)
d77f58be 6992{
5c458ae8 6993 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6994 struct ui_out *uiout = current_uiout;
d77f58be
SS
6995
6996 if (num_printed == 0)
6997 {
e5a67952 6998 if (args == NULL || *args == '\0')
112e8700 6999 uiout->message ("No watchpoints.\n");
d77f58be 7000 else
112e8700 7001 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 7002 }
c906108c
SS
7003}
7004
7a292a7a 7005static void
4495129a 7006maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 7007{
5c458ae8 7008 breakpoint_1 (args, true, NULL);
ad443146
SS
7009
7010 default_collect_info ();
c906108c
SS
7011}
7012
d8de7963 7013static bool
714835d5 7014breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7015 struct program_space *pspace,
714835d5 7016 CORE_ADDR pc, struct obj_section *section)
0d381245 7017{
40cb8ca5 7018 for (bp_location *bl : b->locations ())
0d381245 7019 {
6c95b8df
PA
7020 if (bl->pspace == pspace
7021 && bl->address == pc
0d381245 7022 && (!overlay_debugging || bl->section == section))
d8de7963 7023 return true;
0d381245 7024 }
d8de7963 7025 return false;
0d381245
VP
7026}
7027
bd21b6c9 7028/* See breakpoint.h. */
c906108c 7029
bd21b6c9 7030void
6c95b8df
PA
7031describe_other_breakpoints (struct gdbarch *gdbarch,
7032 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7033 struct obj_section *section, int thread)
c906108c 7034{
52f0bd74 7035 int others = 0;
c906108c 7036
43892fdf 7037 for (breakpoint *b : all_breakpoints ())
672f9b60 7038 others += (user_breakpoint_p (b)
dda83cd7 7039 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 7040
c906108c
SS
7041 if (others > 0)
7042 {
a3f17187 7043 if (others == 1)
6cb06a8c 7044 gdb_printf (_("Note: breakpoint "));
a3f17187 7045 else /* if (others == ???) */
6cb06a8c 7046 gdb_printf (_("Note: breakpoints "));
43892fdf 7047 for (breakpoint *b : all_breakpoints ())
672f9b60 7048 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7049 {
7050 others--;
6cb06a8c 7051 gdb_printf ("%d", b->number);
0d381245 7052 if (b->thread == -1 && thread != -1)
6cb06a8c 7053 gdb_printf (" (all threads)");
0d381245 7054 else if (b->thread != -1)
ce068c5f
AB
7055 {
7056 struct thread_info *thr = find_thread_global_id (b->thread);
7057 gdb_printf (" (thread %s)", print_thread_id (thr));
7058 }
8282ad74 7059 else if (b->task != -1)
f1f517e8 7060 gdb_printf (" (task %d)", b->task);
6cb06a8c
TT
7061 gdb_printf ("%s%s ",
7062 ((b->enable_state == bp_disabled
7063 || b->enable_state == bp_call_disabled)
7064 ? " (disabled)"
7065 : ""),
7066 (others > 1) ? ","
7067 : ((others == 1) ? " and" : ""));
0d381245 7068 }
6a831f06
PA
7069 current_uiout->message (_("also set at pc %ps.\n"),
7070 styled_string (address_style.style (),
7071 paddress (gdbarch, pc)));
c906108c
SS
7072 }
7073}
7074\f
c906108c 7075
cb1e4e32
PA
7076/* Return true iff it is meaningful to use the address member of LOC.
7077 For some breakpoint types, the locations' address members are
7078 irrelevant and it makes no sense to attempt to compare them to
7079 other addresses (or use them for any other purpose either).
2d134ed3 7080
cb1e4e32
PA
7081 More specifically, software watchpoints and catchpoints that are
7082 not backed by breakpoints always have a zero valued location
7083 address and we don't want to mark breakpoints of any of these types
7084 to be a duplicate of an actual breakpoint location at address
7085 zero. */
e4f237da 7086
cb1e4e32
PA
7087static bool
7088bl_address_is_meaningful (bp_location *loc)
e4f237da 7089{
cb1e4e32 7090 return loc->loc_type != bp_loc_other;
2d134ed3
PA
7091}
7092
7093/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7094 true if LOC1 and LOC2 represent the same watchpoint location. */
7095
d8de7963 7096static bool
b2ff9ed3
AB
7097watchpoint_locations_match (const struct bp_location *loc1,
7098 const struct bp_location *loc2)
2d134ed3 7099{
3a5c3e22
PA
7100 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7101 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7102
7103 /* Both of them must exist. */
7104 gdb_assert (w1 != NULL);
7105 gdb_assert (w2 != NULL);
2bdf28a0 7106
4a64f543
MS
7107 /* If the target can evaluate the condition expression in hardware,
7108 then we we need to insert both watchpoints even if they are at
7109 the same place. Otherwise the watchpoint will only trigger when
7110 the condition of whichever watchpoint was inserted evaluates to
7111 true, not giving a chance for GDB to check the condition of the
7112 other watchpoint. */
3a5c3e22 7113 if ((w1->cond_exp
4a64f543
MS
7114 && target_can_accel_watchpoint_condition (loc1->address,
7115 loc1->length,
0cf6dd15 7116 loc1->watchpoint_type,
4d01a485 7117 w1->cond_exp.get ()))
3a5c3e22 7118 || (w2->cond_exp
4a64f543
MS
7119 && target_can_accel_watchpoint_condition (loc2->address,
7120 loc2->length,
0cf6dd15 7121 loc2->watchpoint_type,
4d01a485 7122 w2->cond_exp.get ())))
d8de7963 7123 return false;
0cf6dd15 7124
85d721b8
PA
7125 /* Note that this checks the owner's type, not the location's. In
7126 case the target does not support read watchpoints, but does
7127 support access watchpoints, we'll have bp_read_watchpoint
7128 watchpoints with hw_access locations. Those should be considered
7129 duplicates of hw_read locations. The hw_read locations will
7130 become hw_access locations later. */
2d134ed3
PA
7131 return (loc1->owner->type == loc2->owner->type
7132 && loc1->pspace->aspace == loc2->pspace->aspace
7133 && loc1->address == loc2->address
7134 && loc1->length == loc2->length);
e4f237da
KB
7135}
7136
31e77af2 7137/* See breakpoint.h. */
6c95b8df 7138
31e77af2 7139int
accd0bcd
YQ
7140breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7141 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 7142{
f5656ead 7143 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7144 || aspace1 == aspace2)
7145 && addr1 == addr2);
7146}
7147
f1310107
TJB
7148/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7149 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7150 matches ASPACE2. On targets that have global breakpoints, the address
7151 space doesn't really matter. */
7152
d8de7963 7153static bool
accd0bcd
YQ
7154breakpoint_address_match_range (const address_space *aspace1,
7155 CORE_ADDR addr1,
7156 int len1, const address_space *aspace2,
f1310107
TJB
7157 CORE_ADDR addr2)
7158{
f5656ead 7159 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7160 || aspace1 == aspace2)
7161 && addr2 >= addr1 && addr2 < addr1 + len1);
7162}
7163
7164/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7165 a ranged breakpoint. In most targets, a match happens only if ASPACE
7166 matches the breakpoint's address space. On targets that have global
7167 breakpoints, the address space doesn't really matter. */
7168
d8de7963 7169static bool
f1310107 7170breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 7171 const address_space *aspace,
f1310107
TJB
7172 CORE_ADDR addr)
7173{
7174 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7175 aspace, addr)
7176 || (bl->length
7177 && breakpoint_address_match_range (bl->pspace->aspace,
7178 bl->address, bl->length,
7179 aspace, addr)));
7180}
7181
d35ae833
PA
7182/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7183 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7184 match happens only if ASPACE matches the breakpoint's address
7185 space. On targets that have global breakpoints, the address space
7186 doesn't really matter. */
7187
d8de7963 7188static bool
d35ae833 7189breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 7190 const address_space *aspace,
d35ae833
PA
7191 CORE_ADDR addr, int len)
7192{
7193 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7194 || bl->pspace->aspace == aspace)
7195 {
7196 int bl_len = bl->length != 0 ? bl->length : 1;
7197
7198 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7199 return 1;
7200 }
7201 return 0;
7202}
7203
1e4d1764
YQ
7204/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7205 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7206 true, otherwise returns false. */
7207
d8de7963 7208static bool
b2ff9ed3
AB
7209tracepoint_locations_match (const struct bp_location *loc1,
7210 const struct bp_location *loc2)
1e4d1764
YQ
7211{
7212 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7213 /* Since tracepoint locations are never duplicated with others', tracepoint
7214 locations at the same address of different tracepoints are regarded as
7215 different locations. */
7216 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7217 else
d8de7963 7218 return false;
1e4d1764
YQ
7219}
7220
2d134ed3 7221/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7222 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7223 the same location. If SW_HW_BPS_MATCH is true, then software
7224 breakpoint locations and hardware breakpoint locations match,
7225 otherwise they don't. */
2d134ed3 7226
d8de7963 7227static bool
b2ff9ed3
AB
7228breakpoint_locations_match (const struct bp_location *loc1,
7229 const struct bp_location *loc2,
7f32a4d5 7230 bool sw_hw_bps_match)
2d134ed3 7231{
2bdf28a0
JK
7232 int hw_point1, hw_point2;
7233
7234 /* Both of them must not be in moribund_locations. */
7235 gdb_assert (loc1->owner != NULL);
7236 gdb_assert (loc2->owner != NULL);
7237
7238 hw_point1 = is_hardware_watchpoint (loc1->owner);
7239 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7240
7241 if (hw_point1 != hw_point2)
d8de7963 7242 return false;
2d134ed3
PA
7243 else if (hw_point1)
7244 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7245 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7246 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7247 else
7f32a4d5
PA
7248 /* We compare bp_location.length in order to cover ranged
7249 breakpoints. Keep this in sync with
7250 bp_location_is_less_than. */
f1310107
TJB
7251 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7252 loc2->pspace->aspace, loc2->address)
7f32a4d5 7253 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7254 && loc1->length == loc2->length);
2d134ed3
PA
7255}
7256
76897487
KB
7257static void
7258breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
d8de7963 7259 int bnum, bool have_bnum)
76897487 7260{
f63fbe86
MS
7261 /* The longest string possibly returned by hex_string_custom
7262 is 50 chars. These must be at least that big for safety. */
7263 char astr1[64];
7264 char astr2[64];
76897487 7265
bb599908
PH
7266 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7267 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7268 if (have_bnum)
8a3fe4f8 7269 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7270 bnum, astr1, astr2);
76897487 7271 else
8a3fe4f8 7272 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7273}
7274
4a64f543
MS
7275/* Adjust a breakpoint's address to account for architectural
7276 constraints on breakpoint placement. Return the adjusted address.
7277 Note: Very few targets require this kind of adjustment. For most
7278 targets, this function is simply the identity function. */
76897487
KB
7279
7280static CORE_ADDR
a6d9a66e 7281adjust_breakpoint_address (struct gdbarch *gdbarch,
60270718
AB
7282 CORE_ADDR bpaddr, enum bptype bptype,
7283 struct program_space *pspace)
76897487 7284{
60270718
AB
7285 gdb_assert (pspace != nullptr);
7286
a0de8c21
YQ
7287 if (bptype == bp_watchpoint
7288 || bptype == bp_hardware_watchpoint
7289 || bptype == bp_read_watchpoint
7290 || bptype == bp_access_watchpoint
7291 || bptype == bp_catchpoint)
88f7da05
KB
7292 {
7293 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7294 have their addresses modified. */
88f7da05
KB
7295 return bpaddr;
7296 }
7c16b83e
PA
7297 else if (bptype == bp_single_step)
7298 {
7299 /* Single-step breakpoints should not have their addresses
7300 modified. If there's any architectural constrain that
7301 applies to this address, then it should have already been
7302 taken into account when the breakpoint was created in the
7303 first place. If we didn't do this, stepping through e.g.,
7304 Thumb-2 IT blocks would break. */
7305 return bpaddr;
7306 }
76897487
KB
7307 else
7308 {
a0de8c21
YQ
7309 CORE_ADDR adjusted_bpaddr = bpaddr;
7310
60270718
AB
7311 /* Some targets have architectural constraints on the placement
7312 of breakpoint instructions. Obtain the adjusted address. */
a0de8c21
YQ
7313 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7314 {
60270718
AB
7315 /* Targets that implement this adjustment function will likely
7316 inspect either the symbol table, target memory at BPADDR, or
7317 even state registers, so ensure a suitable thread (and its
7318 associated program space) are currently selected. */
7319 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7320 switch_to_program_space_and_thread (pspace);
7321 adjusted_bpaddr
7322 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
a0de8c21 7323 }
76897487 7324
d88cb738
LM
7325 adjusted_bpaddr
7326 = gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
76897487
KB
7327
7328 /* An adjusted breakpoint address can significantly alter
dda83cd7 7329 a user's expectations. Print a warning if an adjustment
76897487
KB
7330 is required. */
7331 if (adjusted_bpaddr != bpaddr)
d8de7963 7332 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
76897487
KB
7333
7334 return adjusted_bpaddr;
7335 }
7336}
7337
cb1e4e32
PA
7338static bp_loc_type
7339bp_location_from_bp_type (bptype type)
7cc221ef 7340{
cb1e4e32 7341 switch (type)
e049a4b5
DJ
7342 {
7343 case bp_breakpoint:
7c16b83e 7344 case bp_single_step:
e049a4b5
DJ
7345 case bp_until:
7346 case bp_finish:
7347 case bp_longjmp:
7348 case bp_longjmp_resume:
e2e4d78b 7349 case bp_longjmp_call_dummy:
186c406b
TT
7350 case bp_exception:
7351 case bp_exception_resume:
e049a4b5 7352 case bp_step_resume:
2c03e5be 7353 case bp_hp_step_resume:
e049a4b5
DJ
7354 case bp_watchpoint_scope:
7355 case bp_call_dummy:
aa7d318d 7356 case bp_std_terminate:
e049a4b5
DJ
7357 case bp_shlib_event:
7358 case bp_thread_event:
7359 case bp_overlay_event:
4efc6507 7360 case bp_jit_event:
0fd8e87f 7361 case bp_longjmp_master:
aa7d318d 7362 case bp_std_terminate_master:
186c406b 7363 case bp_exception_master:
0e30163f
JK
7364 case bp_gnu_ifunc_resolver:
7365 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7366 case bp_dprintf:
cb1e4e32 7367 return bp_loc_software_breakpoint;
e049a4b5 7368 case bp_hardware_breakpoint:
cb1e4e32 7369 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7370 case bp_hardware_watchpoint:
7371 case bp_read_watchpoint:
7372 case bp_access_watchpoint:
cb1e4e32 7373 return bp_loc_hardware_watchpoint;
e049a4b5 7374 case bp_watchpoint:
6e14e441 7375 return bp_loc_software_watchpoint;
ce78b96d 7376 case bp_catchpoint:
15c3d785
PA
7377 case bp_tracepoint:
7378 case bp_fast_tracepoint:
0fb4aa4b 7379 case bp_static_tracepoint:
7b572efb 7380 case bp_static_marker_tracepoint:
cb1e4e32 7381 return bp_loc_other;
e049a4b5 7382 default:
f34652de 7383 internal_error (_("unknown breakpoint type"));
e049a4b5 7384 }
cb1e4e32
PA
7385}
7386
7387bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7388{
7389 this->owner = owner;
7390 this->cond_bytecode = NULL;
7391 this->shlib_disabled = 0;
7392 this->enabled = 1;
b5fa468f 7393 this->disabled_by_cond = false;
cb1e4e32
PA
7394
7395 this->loc_type = type;
e049a4b5 7396
cb1e4e32
PA
7397 if (this->loc_type == bp_loc_software_breakpoint
7398 || this->loc_type == bp_loc_hardware_breakpoint)
7399 mark_breakpoint_location_modified (this);
7400
b6433ede 7401 incref ();
cb1e4e32
PA
7402}
7403
7404bp_location::bp_location (breakpoint *owner)
7405 : bp_location::bp_location (owner,
7406 bp_location_from_bp_type (owner->type))
7407{
28010a5d
PA
7408}
7409
f431efe5
PA
7410/* Decrement reference count. If the reference count reaches 0,
7411 destroy the bp_location. Sets *BLP to NULL. */
7412
7413static void
7414decref_bp_location (struct bp_location **blp)
7415{
b6433ede 7416 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7417 *blp = NULL;
7418}
7419
346774a9 7420/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7421
b270e6f9
TT
7422static breakpoint *
7423add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7424{
346774a9 7425 struct breakpoint *b1;
b270e6f9 7426 struct breakpoint *result = b.get ();
c906108c 7427
346774a9
PA
7428 /* Add this breakpoint to the end of the chain so that a list of
7429 breakpoints will come out in order of increasing numbers. */
7430
7431 b1 = breakpoint_chain;
7432 if (b1 == 0)
b270e6f9 7433 breakpoint_chain = b.release ();
346774a9
PA
7434 else
7435 {
7436 while (b1->next)
7437 b1 = b1->next;
b270e6f9 7438 b1->next = b.release ();
346774a9 7439 }
b270e6f9
TT
7440
7441 return result;
346774a9
PA
7442}
7443
0ba852ab 7444/* Initialize loc->function_name. */
0e30163f 7445
0d381245 7446static void
0ba852ab 7447set_breakpoint_location_function (struct bp_location *loc)
0d381245 7448{
2bdf28a0
JK
7449 gdb_assert (loc->owner != NULL);
7450
0d381245 7451 if (loc->owner->type == bp_breakpoint
1042e4c0 7452 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7453 || is_tracepoint (loc->owner))
0d381245 7454 {
2c02bd72 7455 const char *function_name;
0e30163f 7456
3467ec66 7457 if (loc->msymbol != NULL
60f62e2b
SM
7458 && (loc->msymbol->type () == mst_text_gnu_ifunc
7459 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7460 {
7461 struct breakpoint *b = loc->owner;
7462
c9d95fa3 7463 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7464
7465 if (b->type == bp_breakpoint && b->loc == loc
7466 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7467 {
7468 /* Create only the whole new breakpoint of this type but do not
7469 mess more complicated breakpoints with multiple locations. */
7470 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7471 /* Remember the resolver's address for use by the return
dda83cd7 7472 breakpoint. */
3467ec66 7473 loc->related_address = loc->address;
0e30163f
JK
7474 }
7475 }
3467ec66
PA
7476 else
7477 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7478
2c02bd72 7479 if (function_name)
23d6ee64 7480 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7481 }
7482}
7483
a6d9a66e 7484/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7485struct gdbarch *
a6d9a66e
UW
7486get_sal_arch (struct symtab_and_line sal)
7487{
7488 if (sal.section)
08feed99 7489 return sal.section->objfile->arch ();
a6d9a66e 7490 if (sal.symtab)
3c86fae3 7491 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7492
7493 return NULL;
7494}
7495
53a5351d 7496/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7497 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7498 initiated the operation. */
c906108c
SS
7499
7500void
186c406b 7501set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7502{
5d5658a1 7503 int thread = tp->global_num;
0fd8e87f
UW
7504
7505 /* To avoid having to rescan all objfile symbols at every step,
7506 we maintain a list of continually-inserted but always disabled
7507 longjmp "master" breakpoints. Here, we simply create momentary
7508 clones of those and enable them for the requested thread. */
1428b37a 7509 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7510 if (b->pspace == current_program_space
186c406b
TT
7511 && (b->type == bp_longjmp_master
7512 || b->type == bp_exception_master))
0fd8e87f 7513 {
06edf0c0 7514 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
e2e4d78b
JK
7515 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7516 after their removal. */
7ab97995 7517 momentary_breakpoint_from_master (b, type, 1, thread);
0fd8e87f 7518 }
186c406b
TT
7519
7520 tp->initiating_frame = frame;
c906108c
SS
7521}
7522
611c83ae 7523/* Delete all longjmp breakpoints from THREAD. */
c906108c 7524void
611c83ae 7525delete_longjmp_breakpoint (int thread)
c906108c 7526{
1428b37a 7527 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7528 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7529 {
7530 if (b->thread == thread)
7531 delete_breakpoint (b);
7532 }
c906108c
SS
7533}
7534
f59f708a
PA
7535void
7536delete_longjmp_breakpoint_at_next_stop (int thread)
7537{
1428b37a 7538 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7539 if (b->type == bp_longjmp || b->type == bp_exception)
7540 {
7541 if (b->thread == thread)
7542 b->disposition = disp_del_at_next_stop;
7543 }
7544}
7545
e2e4d78b
JK
7546/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7547 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7548 pointer to any of them. Return NULL if this system cannot place longjmp
7549 breakpoints. */
7550
7551struct breakpoint *
7552set_longjmp_breakpoint_for_call_dummy (void)
7553{
43892fdf 7554 breakpoint *retval = nullptr;
e2e4d78b 7555
43892fdf 7556 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7557 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7558 {
7ab97995
PA
7559 int thread = inferior_thread ()->global_num;
7560 breakpoint *new_b
7561 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7562 1, thread);
e2e4d78b
JK
7563
7564 /* Link NEW_B into the chain of RETVAL breakpoints. */
7565
7566 gdb_assert (new_b->related_breakpoint == new_b);
7567 if (retval == NULL)
7568 retval = new_b;
7569 new_b->related_breakpoint = retval;
7570 while (retval->related_breakpoint != new_b->related_breakpoint)
7571 retval = retval->related_breakpoint;
7572 retval->related_breakpoint = new_b;
7573 }
7574
7575 return retval;
7576}
7577
7578/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7579 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7580 stack.
7581
b4b3e2de
AB
7582 If the unwind fails then there is not sufficient information to discard
7583 dummy frames. In this case, elide the clean up and the dummy frames will
7584 be cleaned up next time this function is called from a location where
7585 unwinding is possible. */
e2e4d78b
JK
7586
7587void
b67a2c6f 7588check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7589{
7590 struct breakpoint *b, *b_tmp;
7591
7592 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7593 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7594 {
7595 struct breakpoint *dummy_b = b->related_breakpoint;
7596
b4b3e2de
AB
7597 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7598 chained off b->related_breakpoint. */
e2e4d78b
JK
7599 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7600 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7601
7602 /* If there was no bp_call_dummy breakpoint then there's nothing
7603 more to do. Or, if the dummy frame associated with the
7604 bp_call_dummy is still on the stack then we need to leave this
7605 bp_call_dummy in place. */
e2e4d78b
JK
7606 if (dummy_b->type != bp_call_dummy
7607 || frame_find_by_id (dummy_b->frame_id) != NULL)
7608 continue;
b4b3e2de
AB
7609
7610 /* We didn't find the dummy frame on the stack, this could be
7611 because we have longjmp'd to a stack frame that is previous to
7612 the dummy frame, or it could be because the stack unwind is
7613 broken at some point between the longjmp frame and the dummy
7614 frame.
7615
7616 Next we figure out why the stack unwind stopped. If it looks
7617 like the unwind is complete then we assume the dummy frame has
7618 been jumped over, however, if the unwind stopped for an
7619 unexpected reason then we assume the stack unwind is currently
7620 broken, and that we will (eventually) return to the dummy
7621 frame.
7622
7623 It might be tempting to consider using frame_id_inner here, but
7624 that is not safe. There is no guarantee that the stack frames
7625 we are looking at here are even on the same stack as the
7626 original dummy frame, hence frame_id_inner can't be used. See
7627 the comments on frame_id_inner for more details. */
7628 bool unwind_finished_unexpectedly = false;
bd2b40ac 7629 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
b4b3e2de 7630 {
bd2b40ac 7631 frame_info_ptr prev = get_prev_frame (fi);
b4b3e2de
AB
7632 if (prev == nullptr)
7633 {
7634 /* FI is the last stack frame. Why did this frame not
7635 unwind further? */
7636 auto stop_reason = get_frame_unwind_stop_reason (fi);
7637 if (stop_reason != UNWIND_NO_REASON
7638 && stop_reason != UNWIND_OUTERMOST)
7639 unwind_finished_unexpectedly = true;
7640 }
7641 fi = prev;
7642 }
7643 if (unwind_finished_unexpectedly)
7644 continue;
7645
00431a78 7646 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7647
7648 while (b->related_breakpoint != b)
7649 {
7650 if (b_tmp == b->related_breakpoint)
7651 b_tmp = b->related_breakpoint->next;
7652 delete_breakpoint (b->related_breakpoint);
7653 }
7654 delete_breakpoint (b);
7655 }
7656}
7657
1900040c
MS
7658void
7659enable_overlay_breakpoints (void)
7660{
43892fdf 7661 for (breakpoint *b : all_breakpoints ())
1900040c 7662 if (b->type == bp_overlay_event)
01add95b
SM
7663 {
7664 b->enable_state = bp_enabled;
7665 update_global_location_list (UGLL_MAY_INSERT);
7666 overlay_events_enabled = 1;
7667 }
1900040c
MS
7668}
7669
7670void
7671disable_overlay_breakpoints (void)
7672{
43892fdf 7673 for (breakpoint *b : all_breakpoints ())
1900040c 7674 if (b->type == bp_overlay_event)
01add95b
SM
7675 {
7676 b->enable_state = bp_disabled;
7677 update_global_location_list (UGLL_DONT_INSERT);
7678 overlay_events_enabled = 0;
7679 }
1900040c
MS
7680}
7681
aa7d318d
TT
7682/* Set an active std::terminate breakpoint for each std::terminate
7683 master breakpoint. */
7684void
7685set_std_terminate_breakpoint (void)
7686{
1428b37a 7687 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7688 if (b->pspace == current_program_space
7689 && b->type == bp_std_terminate_master)
7690 {
7ab97995
PA
7691 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7692 inferior_thread ()->global_num);
aa7d318d
TT
7693 }
7694}
7695
7696/* Delete all the std::terminate breakpoints. */
7697void
7698delete_std_terminate_breakpoint (void)
7699{
1428b37a 7700 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7701 if (b->type == bp_std_terminate)
7702 delete_breakpoint (b);
7703}
7704
c4093a6a 7705struct breakpoint *
a6d9a66e 7706create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7707{
7708 struct breakpoint *b;
c4093a6a 7709
9efa3c7f 7710 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 7711
b5de0fa7 7712 b->enable_state = bp_enabled;
264f9890
PA
7713 /* locspec has to be used or breakpoint_re_set will delete me. */
7714 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
c4093a6a 7715
44702360 7716 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7717
c4093a6a
JM
7718 return b;
7719}
7720
0101ce28
JJ
7721struct lang_and_radix
7722 {
7723 enum language lang;
7724 int radix;
7725 };
7726
4efc6507
DE
7727/* Create a breakpoint for JIT code registration and unregistration. */
7728
7729struct breakpoint *
7730create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7731{
9efa3c7f 7732 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 7733}
0101ce28 7734
03673fc7
PP
7735/* Remove JIT code registration and unregistration breakpoint(s). */
7736
7737void
7738remove_jit_event_breakpoints (void)
7739{
1428b37a 7740 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7741 if (b->type == bp_jit_event
7742 && b->loc->pspace == current_program_space)
7743 delete_breakpoint (b);
7744}
7745
cae688ec
JJ
7746void
7747remove_solib_event_breakpoints (void)
7748{
1428b37a 7749 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7750 if (b->type == bp_shlib_event
7751 && b->loc->pspace == current_program_space)
cae688ec
JJ
7752 delete_breakpoint (b);
7753}
7754
f37f681c
PA
7755/* See breakpoint.h. */
7756
7757void
7758remove_solib_event_breakpoints_at_next_stop (void)
7759{
1428b37a 7760 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7761 if (b->type == bp_shlib_event
7762 && b->loc->pspace == current_program_space)
7763 b->disposition = disp_del_at_next_stop;
7764}
7765
04086b45
PA
7766/* Helper for create_solib_event_breakpoint /
7767 create_and_insert_solib_event_breakpoint. Allows specifying which
7768 INSERT_MODE to pass through to update_global_location_list. */
7769
7770static struct breakpoint *
7771create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7772 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7773{
7774 struct breakpoint *b;
7775
9efa3c7f 7776 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 7777 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7778 return b;
7779}
7780
04086b45
PA
7781struct breakpoint *
7782create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7783{
7784 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7785}
7786
f37f681c
PA
7787/* See breakpoint.h. */
7788
7789struct breakpoint *
7790create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7791{
7792 struct breakpoint *b;
7793
04086b45
PA
7794 /* Explicitly tell update_global_location_list to insert
7795 locations. */
7796 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7797 if (!b->loc->inserted)
7798 {
7799 delete_breakpoint (b);
7800 return NULL;
7801 }
7802 return b;
7803}
7804
cae688ec
JJ
7805/* Disable any breakpoints that are on code in shared libraries. Only
7806 apply to enabled breakpoints, disabled ones can just stay disabled. */
7807
7808void
cb851954 7809disable_breakpoints_in_shlibs (void)
cae688ec 7810{
48d7020b 7811 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7812 {
7813 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7814 struct breakpoint *b = loc->owner;
7815
7816 /* We apply the check to all breakpoints, including disabled for
7817 those with loc->duplicate set. This is so that when breakpoint
7818 becomes enabled, or the duplicate is removed, gdb will try to
7819 insert all breakpoints. If we don't set shlib_disabled here,
7820 we'll try to insert those breakpoints and fail. */
7821 if (((b->type == bp_breakpoint)
7822 || (b->type == bp_jit_event)
7823 || (b->type == bp_hardware_breakpoint)
7824 || (is_tracepoint (b)))
7825 && loc->pspace == current_program_space
7826 && !loc->shlib_disabled
7827 && solib_name_from_address (loc->pspace, loc->address)
7828 )
7829 {
7830 loc->shlib_disabled = 1;
7831 }
7832 }
cae688ec
JJ
7833}
7834
63644780
NB
7835/* Disable any breakpoints and tracepoints that are in SOLIB upon
7836 notification of unloaded_shlib. Only apply to enabled breakpoints,
7837 disabled ones can just stay disabled. */
84acb35a 7838
75149521 7839static void
84acb35a
JJ
7840disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7841{
d8de7963 7842 bool disabled_shlib_breaks = false;
84acb35a 7843
48d7020b 7844 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7845 {
7846 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7847 struct breakpoint *b = loc->owner;
7848
7849 if (solib->pspace == loc->pspace
7850 && !loc->shlib_disabled
7851 && (((b->type == bp_breakpoint
7852 || b->type == bp_jit_event
7853 || b->type == bp_hardware_breakpoint)
7854 && (loc->loc_type == bp_loc_hardware_breakpoint
7855 || loc->loc_type == bp_loc_software_breakpoint))
7856 || is_tracepoint (b))
7857 && solib_contains_address_p (solib, loc->address))
7858 {
7859 loc->shlib_disabled = 1;
7860 /* At this point, we cannot rely on remove_breakpoint
7861 succeeding so we must mark the breakpoint as not inserted
7862 to prevent future errors occurring in remove_breakpoints. */
7863 loc->inserted = 0;
7864
7865 /* This may cause duplicate notifications for the same breakpoint. */
7866 gdb::observers::breakpoint_modified.notify (b);
7867
7868 if (!disabled_shlib_breaks)
7869 {
7870 target_terminal::ours_for_output ();
7871 warning (_("Temporarily disabling breakpoints "
7872 "for unloaded shared library \"%s\""),
7873 solib->so_name);
7874 }
d8de7963 7875 disabled_shlib_breaks = true;
01add95b
SM
7876 }
7877 }
84acb35a
JJ
7878}
7879
63644780
NB
7880/* Disable any breakpoints and tracepoints in OBJFILE upon
7881 notification of free_objfile. Only apply to enabled breakpoints,
7882 disabled ones can just stay disabled. */
7883
7884static void
7885disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7886{
63644780
NB
7887 if (objfile == NULL)
7888 return;
7889
d03de421
PA
7890 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7891 managed by the user with add-symbol-file/remove-symbol-file.
7892 Similarly to how breakpoints in shared libraries are handled in
7893 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7894 shlib_disabled so they end up uninserted on the next global
7895 location list update. Shared libraries not loaded by the user
7896 aren't handled here -- they're already handled in
7897 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7898 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7899 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7900 main objfile). */
7901 if ((objfile->flags & OBJF_SHARED) == 0
7902 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7903 return;
7904
43892fdf 7905 for (breakpoint *b : all_breakpoints ())
63644780 7906 {
d8de7963 7907 bool bp_modified = false;
63644780
NB
7908
7909 if (!is_breakpoint (b) && !is_tracepoint (b))
7910 continue;
7911
40cb8ca5 7912 for (bp_location *loc : b->locations ())
63644780
NB
7913 {
7914 CORE_ADDR loc_addr = loc->address;
7915
7916 if (loc->loc_type != bp_loc_hardware_breakpoint
7917 && loc->loc_type != bp_loc_software_breakpoint)
7918 continue;
7919
7920 if (loc->shlib_disabled != 0)
7921 continue;
7922
7923 if (objfile->pspace != loc->pspace)
7924 continue;
7925
7926 if (loc->loc_type != bp_loc_hardware_breakpoint
7927 && loc->loc_type != bp_loc_software_breakpoint)
7928 continue;
7929
7930 if (is_addr_in_objfile (loc_addr, objfile))
7931 {
7932 loc->shlib_disabled = 1;
08351840
PA
7933 /* At this point, we don't know whether the object was
7934 unmapped from the inferior or not, so leave the
7935 inserted flag alone. We'll handle failure to
7936 uninsert quietly, in case the object was indeed
7937 unmapped. */
63644780
NB
7938
7939 mark_breakpoint_location_modified (loc);
7940
d8de7963 7941 bp_modified = true;
63644780
NB
7942 }
7943 }
7944
7945 if (bp_modified)
76727919 7946 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7947 }
7948}
7949
b650a282 7950/* See breakpoint.h. */
ce78b96d 7951
92bb0228
PA
7952breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7953 bool temp, const char *cond_string_)
7954 : type (bptype),
7955 disposition (temp ? disp_del : disp_donttouch),
7956 gdbarch (gdbarch_),
7957 language (current_language->la_language),
7958 input_radix (::input_radix),
7959 cond_string (cond_string_ != nullptr
7960 ? make_unique_xstrdup (cond_string_)
7961 : nullptr),
7962 related_breakpoint (this)
7963{
7964}
7965
7966/* See breakpoint.h. */
7967
fed1c982 7968catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
92bb0228
PA
7969 const char *cond_string)
7970 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
c906108c 7971{
acd0955b 7972 add_dummy_location (this, current_program_space);
c5aa993b 7973
acd0955b 7974 pspace = current_program_space;
346774a9
PA
7975}
7976
b82d4ec9 7977breakpoint *
b270e6f9 7978install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 7979{
b270e6f9 7980 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 7981 set_breakpoint_number (internal, b);
558a9d82
YQ
7982 if (is_tracepoint (b))
7983 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
7984 if (!internal)
7985 mention (b);
76727919 7986 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
7987
7988 if (update_gll)
44702360 7989 update_global_location_list (UGLL_MAY_INSERT);
b82d4ec9
PA
7990
7991 return b;
c56053d2
PA
7992}
7993
c906108c 7994static int
fba45db2 7995hw_breakpoint_used_count (void)
c906108c 7996{
c906108c
SS
7997 int i = 0;
7998
43892fdf 7999 for (breakpoint *b : all_breakpoints ())
d6b74ac4 8000 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 8001 for (bp_location *bl : b->locations ())
f1310107
TJB
8002 {
8003 /* Special types of hardware breakpoints may use more than
8004 one register. */
c47614fe 8005 i += b->resources_needed (bl);
f1310107 8006 }
c906108c
SS
8007
8008 return i;
8009}
8010
a1398e0c
PA
8011/* Returns the resources B would use if it were a hardware
8012 watchpoint. */
8013
c906108c 8014static int
a1398e0c 8015hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8016{
c906108c
SS
8017 int i = 0;
8018
a1398e0c
PA
8019 if (!breakpoint_enabled (b))
8020 return 0;
8021
40cb8ca5 8022 for (bp_location *bl : b->locations ())
a1398e0c
PA
8023 {
8024 /* Special types of hardware watchpoints may use more than
8025 one register. */
c47614fe 8026 i += b->resources_needed (bl);
a1398e0c
PA
8027 }
8028
8029 return i;
8030}
8031
8032/* Returns the sum the used resources of all hardware watchpoints of
8033 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8034 the sum of the used resources of all hardware watchpoints of other
8035 types _not_ TYPE. */
8036
8037static int
8038hw_watchpoint_used_count_others (struct breakpoint *except,
8039 enum bptype type, int *other_type_used)
8040{
8041 int i = 0;
a1398e0c 8042
c906108c 8043 *other_type_used = 0;
43892fdf 8044 for (breakpoint *b : all_breakpoints ())
e09342b5 8045 {
a1398e0c
PA
8046 if (b == except)
8047 continue;
e09342b5
TJB
8048 if (!breakpoint_enabled (b))
8049 continue;
8050
a1398e0c
PA
8051 if (b->type == type)
8052 i += hw_watchpoint_use_count (b);
8053 else if (is_hardware_watchpoint (b))
8054 *other_type_used = 1;
e09342b5
TJB
8055 }
8056
c906108c
SS
8057 return i;
8058}
8059
c906108c 8060void
fba45db2 8061disable_watchpoints_before_interactive_call_start (void)
c906108c 8062{
43892fdf 8063 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8064 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8065 {
b5de0fa7 8066 b->enable_state = bp_call_disabled;
44702360 8067 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8068 }
c906108c
SS
8069}
8070
8071void
fba45db2 8072enable_watchpoints_after_interactive_call_stop (void)
c906108c 8073{
43892fdf 8074 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8075 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8076 {
b5de0fa7 8077 b->enable_state = bp_enabled;
44702360 8078 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8079 }
c906108c
SS
8080}
8081
8bea4e01
UW
8082void
8083disable_breakpoints_before_startup (void)
8084{
6c95b8df 8085 current_program_space->executing_startup = 1;
44702360 8086 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8087}
8088
8089void
8090enable_breakpoints_after_startup (void)
8091{
6c95b8df 8092 current_program_space->executing_startup = 0;
f8eba3c6 8093 breakpoint_re_set ();
8bea4e01
UW
8094}
8095
7a3e3265
PA
8096/* Allocate a new momentary breakpoint. */
8097
7ab97995 8098template<typename... Arg>
7a3e3265 8099static momentary_breakpoint *
7ab97995
PA
8100new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8101 Arg&&... args)
7a3e3265
PA
8102{
8103 if (type == bp_longjmp || type == bp_exception)
7ab97995
PA
8104 return new longjmp_breakpoint (gdbarch, type,
8105 std::forward<Arg> (args)...);
7a3e3265 8106 else
7ab97995
PA
8107 return new momentary_breakpoint (gdbarch, type,
8108 std::forward<Arg> (args)...);
7a3e3265
PA
8109}
8110
7c16b83e
PA
8111/* Set a momentary breakpoint of type TYPE at address specified by
8112 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8113 frame. */
c906108c 8114
454dafbd 8115breakpoint_up
a6d9a66e
UW
8116set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8117 struct frame_id frame_id, enum bptype type)
c906108c 8118{
193facb3
JK
8119 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8120 tail-called one. */
8121 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8122
7a3e3265 8123 std::unique_ptr<momentary_breakpoint> b
7ab97995
PA
8124 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8125 inferior_thread ()->global_num));
7a3e3265
PA
8126
8127 b->add_location (sal);
c906108c 8128
7a3e3265
PA
8129 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8130
44702360 8131 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8132
7a3e3265 8133 return bp;
c906108c 8134}
611c83ae 8135
06edf0c0 8136/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8137 The new breakpoint will have type TYPE, use OPS as its
8138 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8139
06edf0c0
PA
8140static struct breakpoint *
8141momentary_breakpoint_from_master (struct breakpoint *orig,
8142 enum bptype type,
7ab97995
PA
8143 int loc_enabled,
8144 int thread)
e58b0e63 8145{
7a3e3265 8146 std::unique_ptr<breakpoint> copy
7ab97995
PA
8147 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8148 orig->frame_id, thread));
ee89d0a4 8149 copy->loc = copy->allocate_location ();
0ba852ab 8150 set_breakpoint_location_function (copy->loc);
e58b0e63 8151
a6d9a66e 8152 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8153 copy->loc->requested_address = orig->loc->requested_address;
8154 copy->loc->address = orig->loc->address;
8155 copy->loc->section = orig->loc->section;
6c95b8df 8156 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8157 copy->loc->probe = orig->loc->probe;
f8eba3c6 8158 copy->loc->line_number = orig->loc->line_number;
2f202fde 8159 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8160 copy->loc->enabled = loc_enabled;
e58b0e63 8161
7a3e3265 8162 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
44702360 8163 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7a3e3265 8164 return b;
e58b0e63
PA
8165}
8166
06edf0c0
PA
8167/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8168 ORIG is NULL. */
8169
8170struct breakpoint *
8171clone_momentary_breakpoint (struct breakpoint *orig)
8172{
8173 /* If there's nothing to clone, then return nothing. */
8174 if (orig == NULL)
8175 return NULL;
8176
7ab97995
PA
8177 return momentary_breakpoint_from_master (orig, orig->type, 0,
8178 orig->thread);
06edf0c0
PA
8179}
8180
454dafbd 8181breakpoint_up
a6d9a66e
UW
8182set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8183 enum bptype type)
611c83ae
PA
8184{
8185 struct symtab_and_line sal;
8186
8187 sal = find_pc_line (pc, 0);
8188 sal.pc = pc;
8189 sal.section = find_pc_overlay (pc);
8190 sal.explicit_pc = 1;
8191
a6d9a66e 8192 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8193}
c906108c 8194\f
c5aa993b 8195
c906108c
SS
8196/* Tell the user we have just set a breakpoint B. */
8197
8198static void
b713485d 8199mention (const breakpoint *b)
c906108c 8200{
c47614fe 8201 b->print_mention ();
2d33446d 8202 current_uiout->text ("\n");
c906108c 8203}
c906108c 8204\f
c5aa993b 8205
5133a315 8206static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8207
7f32a4d5
PA
8208/* Handle "set breakpoint auto-hw on".
8209
8210 If the explicitly specified breakpoint type is not hardware
8211 breakpoint, check the memory map to see whether the breakpoint
8212 address is in read-only memory.
8213
8214 - location type is not hardware breakpoint, memory is read-only.
8215 We change the type of the location to hardware breakpoint.
8216
8217 - location type is hardware breakpoint, memory is read-write. This
8218 means we've previously made the location hardware one, but then the
8219 memory map changed, so we undo.
8220*/
8221
8222static void
8223handle_automatic_hardware_breakpoints (bp_location *bl)
8224{
8225 if (automatic_hardware_breakpoints
8226 && bl->owner->type != bp_hardware_breakpoint
8227 && (bl->loc_type == bp_loc_software_breakpoint
8228 || bl->loc_type == bp_loc_hardware_breakpoint))
8229 {
8230 /* When breakpoints are removed, remove_breakpoints will use
8231 location types we've just set here, the only possible problem
8232 is that memory map has changed during running program, but
8233 it's not going to work anyway with current gdb. */
8234 mem_region *mr = lookup_mem_region (bl->address);
8235
8236 if (mr != nullptr)
8237 {
8238 enum bp_loc_type new_type;
8239
8240 if (mr->attrib.mode != MEM_RW)
8241 new_type = bp_loc_hardware_breakpoint;
8242 else
8243 new_type = bp_loc_software_breakpoint;
8244
8245 if (new_type != bl->loc_type)
8246 {
8247 static bool said = false;
8248
8249 bl->loc_type = new_type;
8250 if (!said)
8251 {
6cb06a8c
TT
8252 gdb_printf (_("Note: automatically using "
8253 "hardware breakpoints for "
8254 "read-only addresses.\n"));
7f32a4d5
PA
8255 said = true;
8256 }
8257 }
8258 }
8259 }
8260}
8261
06615628 8262bp_location *
74421c0b 8263code_breakpoint::add_location (const symtab_and_line &sal)
0d381245 8264{
06615628 8265 struct bp_location *new_loc, **tmp;
3742cc8b 8266 CORE_ADDR adjusted_address;
06615628 8267 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8268
8269 if (loc_gdbarch == NULL)
06615628 8270 loc_gdbarch = gdbarch;
3742cc8b
YQ
8271
8272 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8273 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8274 location will be placed on the location chain. Adjustment of the
8275 breakpoint may cause target_read_memory() to be called and we do
8276 not want its scan of the location chain to find a breakpoint and
8277 location that's only been partially initialized. */
8278 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
60270718
AB
8279 sal.pc, type,
8280 sal.pspace);
0d381245 8281
d30113d4 8282 /* Sort the locations by their ADDRESS. */
06615628
PA
8283 new_loc = allocate_location ();
8284 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
d30113d4 8285 tmp = &((*tmp)->next))
0d381245 8286 ;
06615628
PA
8287 new_loc->next = *tmp;
8288 *tmp = new_loc;
8289
8290 new_loc->requested_address = sal.pc;
8291 new_loc->address = adjusted_address;
8292 new_loc->pspace = sal.pspace;
8293 new_loc->probe.prob = sal.prob;
8294 new_loc->probe.objfile = sal.objfile;
8295 gdb_assert (new_loc->pspace != NULL);
8296 new_loc->section = sal.section;
8297 new_loc->gdbarch = loc_gdbarch;
8298 new_loc->line_number = sal.line;
8299 new_loc->symtab = sal.symtab;
8300 new_loc->symbol = sal.symbol;
8301 new_loc->msymbol = sal.msymbol;
8302 new_loc->objfile = sal.objfile;
8303
8304 set_breakpoint_location_function (new_loc);
1a853c52 8305
6ae88661
LM
8306 /* While by definition, permanent breakpoints are already present in the
8307 code, we don't mark the location as inserted. Normally one would expect
8308 that GDB could rely on that breakpoint instruction to stop the program,
8309 thus removing the need to insert its own breakpoint, except that executing
8310 the breakpoint instruction can kill the target instead of reporting a
8311 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8312 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8313 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8314 breakpoint be inserted normally results in QEMU knowing about the GDB
8315 breakpoint, and thus trap before the breakpoint instruction is executed.
8316 (If GDB later needs to continue execution past the permanent breakpoint,
8317 it manually increments the PC, thus avoiding executing the breakpoint
8318 instruction.) */
06615628
PA
8319 if (bp_loc_is_permanent (new_loc))
8320 new_loc->permanent = 1;
1a853c52 8321
06615628 8322 return new_loc;
0d381245 8323}
514f746b
AR
8324\f
8325
5133a315
LM
8326/* Return true if LOC is pointing to a permanent breakpoint,
8327 return false otherwise. */
1cf4d951 8328
5133a315 8329static bool
1cf4d951
PA
8330bp_loc_is_permanent (struct bp_location *loc)
8331{
514f746b
AR
8332 gdb_assert (loc != NULL);
8333
cb1e4e32
PA
8334 /* If we have a non-breakpoint-backed catchpoint or a software
8335 watchpoint, just return 0. We should not attempt to read from
8336 the addresses the locations of these breakpoint types point to.
5133a315 8337 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8338 memory. */
cb1e4e32 8339 if (!bl_address_is_meaningful (loc))
5133a315 8340 return false;
244558af 8341
5ed8105e 8342 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8343 switch_to_program_space_and_thread (loc->pspace);
5133a315 8344 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8345}
8346
e7e0cddf
SS
8347/* Build a command list for the dprintf corresponding to the current
8348 settings of the dprintf style options. */
8349
8350static void
8351update_dprintf_command_list (struct breakpoint *b)
8352{
6f781ee3 8353 const char *dprintf_args = b->extra_string.get ();
8579fd13 8354 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf
SS
8355
8356 if (!dprintf_args)
8357 return;
8358
8359 dprintf_args = skip_spaces (dprintf_args);
8360
8361 /* Allow a comma, as it may have terminated a location, but don't
8362 insist on it. */
8363 if (*dprintf_args == ',')
8364 ++dprintf_args;
8365 dprintf_args = skip_spaces (dprintf_args);
8366
8367 if (*dprintf_args != '"')
8368 error (_("Bad format string, missing '\"'."));
8369
d3ce09f5 8370 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8371 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8372 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8373 {
e0700ba4 8374 if (dprintf_function.empty ())
e7e0cddf
SS
8375 error (_("No function supplied for dprintf call"));
8376
e0700ba4 8377 if (!dprintf_channel.empty ())
e7e0cddf 8378 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8379 dprintf_function.c_str (),
8380 dprintf_channel.c_str (),
e7e0cddf
SS
8381 dprintf_args);
8382 else
8383 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8384 dprintf_function.c_str (),
e7e0cddf
SS
8385 dprintf_args);
8386 }
d3ce09f5
SS
8387 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8388 {
8389 if (target_can_run_breakpoint_commands ())
8390 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8391 else
8392 {
8393 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8394 printf_line = xstrprintf ("printf %s", dprintf_args);
8395 }
8396 }
e7e0cddf 8397 else
f34652de 8398 internal_error (_("Invalid dprintf style."));
e7e0cddf 8399
f28045c2 8400 gdb_assert (printf_line != NULL);
e7e0cddf 8401
12973681
TT
8402 /* Manufacture a printf sequence. */
8403 struct command_line *printf_cmd_line
8579fd13 8404 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8405 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8406 command_lines_deleter ()));
e7e0cddf
SS
8407}
8408
8409/* Update all dprintf commands, making their command lists reflect
8410 current style settings. */
8411
8412static void
eb4c3f4a 8413update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8414 struct cmd_list_element *c)
8415{
43892fdf
SM
8416 for (breakpoint *b : all_breakpoints ())
8417 if (b->type == bp_dprintf)
e7e0cddf 8418 update_dprintf_command_list (b);
e7e0cddf 8419}
c3f6f71d 8420
74421c0b 8421code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
3b003a61
PA
8422 enum bptype type_,
8423 gdb::array_view<const symtab_and_line> sals,
264f9890 8424 location_spec_up &&locspec_,
3b003a61
PA
8425 gdb::unique_xmalloc_ptr<char> filter_,
8426 gdb::unique_xmalloc_ptr<char> cond_string_,
8427 gdb::unique_xmalloc_ptr<char> extra_string_,
8428 enum bpdisp disposition_,
8429 int thread_, int task_, int ignore_count_,
8430 int from_tty,
8431 int enabled_, unsigned flags,
8432 int display_canonical_)
8433 : breakpoint (gdbarch_, type_)
018d34a4 8434{
0d381245 8435 int i;
018d34a4
VP
8436
8437 if (type == bp_hardware_breakpoint)
8438 {
fbbd034e
AS
8439 int target_resources_ok;
8440
8441 i = hw_breakpoint_used_count ();
8442 target_resources_ok =
8443 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8444 i + 1, 0);
8445 if (target_resources_ok == 0)
8446 error (_("No hardware breakpoint support in the target."));
8447 else if (target_resources_ok < 0)
8448 error (_("Hardware breakpoints used exceeds limit."));
8449 }
8450
6c5b2ebe 8451 gdb_assert (!sals.empty ());
6c95b8df 8452
0a9ccb9d 8453 /* At most one of thread or task can be set on any breakpoint. */
2ecee236 8454 gdb_assert (thread == -1 || task == -1);
3b003a61
PA
8455 thread = thread_;
8456 task = task_;
0d381245 8457
3b003a61
PA
8458 cond_string = std::move (cond_string_);
8459 extra_string = std::move (extra_string_);
8460 ignore_count = ignore_count_;
8461 enable_state = enabled_ ? bp_enabled : bp_disabled;
8462 disposition = disposition_;
5af949e3 8463
3b003a61
PA
8464 if (type == bp_static_tracepoint
8465 || type == bp_static_marker_tracepoint)
8466 {
98ed24fb 8467 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
3b003a61 8468 struct static_tracepoint_marker marker;
0d381245 8469
3b003a61 8470 if (strace_marker_p (this))
0d381245 8471 {
3b003a61
PA
8472 /* We already know the marker exists, otherwise, we wouldn't
8473 see a sal for it. */
709438c7 8474 const char *p = &locspec_->to_string ()[3];
3b003a61 8475 const char *endp;
0fb4aa4b 8476
3b003a61 8477 p = skip_spaces (p);
0fb4aa4b 8478
3b003a61 8479 endp = skip_to_space (p);
0fb4aa4b 8480
3b003a61 8481 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8482
3b003a61
PA
8483 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8484 t->static_trace_marker_id.c_str ());
8485 }
8486 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8487 {
8488 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8489
3b003a61
PA
8490 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8491 t->static_trace_marker_id.c_str ());
0d381245
VP
8492 }
8493 else
3b003a61
PA
8494 warning (_("Couldn't determine the static tracepoint marker to probe"));
8495 }
8496
8497 for (const auto &sal : sals)
8498 {
8499 if (from_tty)
018d34a4 8500 {
3b003a61
PA
8501 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8502 if (loc_gdbarch == nullptr)
8503 loc_gdbarch = gdbarch;
8504
8505 describe_other_breakpoints (loc_gdbarch,
8506 sal.pspace, sal.pc, sal.section, thread);
0d381245
VP
8507 }
8508
3b003a61
PA
8509 bp_location *new_loc = add_location (sal);
8510 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8511 new_loc->inserted = 1;
8512
b5fa468f
TBA
8513 /* Do not set breakpoint locations conditions yet. As locations
8514 are inserted, they get sorted based on their addresses. Let
8515 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8516
8517 /* Dynamic printf requires and uses additional arguments on the
8518 command line, otherwise it's an error. */
8519 if (type == bp_dprintf)
8520 {
3b003a61
PA
8521 if (extra_string != nullptr)
8522 update_dprintf_command_list (this);
e7e0cddf
SS
8523 else
8524 error (_("Format string required"));
8525 }
3b003a61
PA
8526 else if (extra_string != nullptr)
8527 error (_("Garbage '%s' at end of command"), extra_string.get ());
855a6e68 8528 }
018d34a4 8529
b5fa468f
TBA
8530 /* The order of the locations is now stable. Set the location
8531 condition using the location's number. */
8532 int loc_num = 1;
3b003a61 8533 for (bp_location *bl : locations ())
b5fa468f 8534 {
3b003a61
PA
8535 if (cond_string != nullptr)
8536 set_breakpoint_location_condition (cond_string.get (), bl,
8537 number, loc_num);
b5fa468f
TBA
8538
8539 ++loc_num;
8540 }
8541
3b003a61 8542 display_canonical = display_canonical_;
264f9890
PA
8543 if (locspec_ != nullptr)
8544 locspec = std::move (locspec_);
018d34a4 8545 else
264f9890 8546 locspec = new_address_location_spec (this->loc->address, NULL, 0);
3b003a61 8547 filter = std::move (filter_);
d9b3f62e 8548}
018d34a4 8549
d9b3f62e
PA
8550static void
8551create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8552 gdb::array_view<const symtab_and_line> sals,
264f9890 8553 location_spec_up &&locspec,
e1e01040
PA
8554 gdb::unique_xmalloc_ptr<char> filter,
8555 gdb::unique_xmalloc_ptr<char> cond_string,
8556 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8557 enum bptype type, enum bpdisp disposition,
8558 int thread, int task, int ignore_count,
ef4848c7 8559 int from_tty,
44f238bb
PA
8560 int enabled, int internal, unsigned flags,
8561 int display_canonical)
d9b3f62e 8562{
74421c0b 8563 std::unique_ptr<code_breakpoint> b
3b003a61
PA
8564 = new_breakpoint_from_type (gdbarch,
8565 type,
8566 sals,
264f9890 8567 std::move (locspec),
3b003a61
PA
8568 std::move (filter),
8569 std::move (cond_string),
8570 std::move (extra_string),
8571 disposition,
8572 thread, task, ignore_count,
8573 from_tty,
8574 enabled, flags,
8575 display_canonical);
d9b3f62e 8576
b270e6f9 8577 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8578}
8579
8580/* Add SALS.nelts breakpoints to the breakpoint table. For each
8581 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8582 value. COND_STRING, if not NULL, specified the condition to be
8583 used for all breakpoints. Essentially the only case where
8584 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8585 function. In that case, it's still not possible to specify
8586 separate conditions for different overloaded functions, so
8587 we take just a single condition string.
8588
c3f6f71d 8589 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8590 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8591 array contents). If the function fails (error() is called), the
8592 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8593 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8594
8595static void
8cdf0e15 8596create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8597 struct linespec_result *canonical,
e1e01040
PA
8598 gdb::unique_xmalloc_ptr<char> cond_string,
8599 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8600 enum bptype type, enum bpdisp disposition,
8601 int thread, int task, int ignore_count,
ef4848c7 8602 int from_tty,
44f238bb 8603 int enabled, int internal, unsigned flags)
c906108c 8604{
f8eba3c6 8605 if (canonical->pre_expanded)
6c5b2ebe 8606 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8607
6c5b2ebe 8608 for (const auto &lsal : canonical->lsals)
c3f6f71d 8609 {
f00aae0f 8610 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8611 'break', without arguments. */
264f9890 8612 location_spec_up locspec
5c1ddcb6
PA
8613 = (canonical->locspec != nullptr
8614 ? canonical->locspec->clone ()
8615 : nullptr);
e1e01040 8616 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8617 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8618
6c5b2ebe 8619 create_breakpoint_sal (gdbarch, lsal.sals,
264f9890 8620 std::move (locspec),
e1e01040
PA
8621 std::move (filter_string),
8622 std::move (cond_string),
8623 std::move (extra_string),
e7e0cddf 8624 type, disposition,
ef4848c7 8625 thread, task, ignore_count,
44f238bb 8626 from_tty, enabled, internal, flags,
56435ebe 8627 canonical->special_display);
c3f6f71d 8628 }
c3f6f71d 8629}
c906108c 8630
264f9890 8631/* Parse LOCSPEC which is assumed to be a SAL specification possibly
c3f6f71d 8632 followed by conditionals. On return, SALS contains an array of SAL
264f9890
PA
8633 addresses found. LOCSPEC points to the end of the SAL (for
8634 linespec locspecs).
9998af43
TJB
8635
8636 The array and the line spec strings are allocated on the heap, it is
8637 the caller's responsibility to free them. */
c906108c 8638
b9362cc7 8639static void
264f9890 8640parse_breakpoint_sals (location_spec *locspec,
58438ac1 8641 struct linespec_result *canonical)
c3f6f71d 8642{
f00aae0f
KS
8643 struct symtab_and_line cursal;
8644
7464aeaa 8645 if (locspec->type () == LINESPEC_LOCATION_SPEC)
f00aae0f 8646 {
40d97ee2 8647 const char *spec = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 8648
a20714ff 8649 if (spec == NULL)
f00aae0f
KS
8650 {
8651 /* The last displayed codepoint, if it's valid, is our default
8652 breakpoint address. */
8653 if (last_displayed_sal_is_valid ())
8654 {
f00aae0f
KS
8655 /* Set sal's pspace, pc, symtab, and line to the values
8656 corresponding to the last call to print_frame_info.
8657 Be sure to reinitialize LINE with NOTCURRENT == 0
8658 as the breakpoint line number is inappropriate otherwise.
8659 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8660 symtab_and_line sal = get_last_displayed_sal ();
8661 CORE_ADDR pc = sal.pc;
8662
f00aae0f
KS
8663 sal = find_pc_line (pc, 0);
8664
8665 /* "break" without arguments is equivalent to "break *PC"
8666 where PC is the last displayed codepoint's address. So
8667 make sure to set sal.explicit_pc to prevent GDB from
8668 trying to expand the list of sals to include all other
8669 instances with the same symtab and line. */
8670 sal.pc = pc;
8671 sal.explicit_pc = 1;
8672
6c5b2ebe
PA
8673 struct linespec_sals lsal;
8674 lsal.sals = {sal};
f00aae0f
KS
8675 lsal.canonical = NULL;
8676
6c5b2ebe 8677 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8678 return;
8679 }
8680 else
8681 error (_("No default breakpoint address now."));
c906108c 8682 }
c906108c 8683 }
f00aae0f
KS
8684
8685 /* Force almost all breakpoints to be in terms of the
8686 current_source_symtab (which is decode_line_1's default).
8687 This should produce the results we want almost all of the
8688 time while leaving default_breakpoint_* alone.
8689
8690 ObjC: However, don't match an Objective-C method name which
8691 may have a '+' or '-' succeeded by a '['. */
8692 cursal = get_current_source_symtab_and_line ();
8693 if (last_displayed_sal_is_valid ())
c906108c 8694 {
a20714ff 8695 const char *spec = NULL;
cc80f267 8696
7464aeaa 8697 if (locspec->type () == LINESPEC_LOCATION_SPEC)
40d97ee2 8698 spec = as_linespec_location_spec (locspec)->spec_string;
cc80f267 8699
f00aae0f 8700 if (!cursal.symtab
a20714ff
PA
8701 || (spec != NULL
8702 && strchr ("+-", spec[0]) != NULL
8703 && spec[1] != '['))
f00aae0f 8704 {
264f9890 8705 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
8706 get_last_displayed_symtab (),
8707 get_last_displayed_line (),
8708 canonical, NULL, NULL);
8709 return;
8710 }
c906108c 8711 }
f00aae0f 8712
264f9890 8713 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 8714 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 8715}
c906108c 8716
c906108c 8717
c3f6f71d 8718/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8719 inserted as a breakpoint. If it can't throw an error. */
c906108c 8720
b9362cc7 8721static void
6c5b2ebe 8722breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 8723{
6c5b2ebe
PA
8724 for (auto &sal : sals)
8725 resolve_sal_pc (&sal);
c3f6f71d
JM
8726}
8727
7a697b8d
SS
8728/* Fast tracepoints may have restrictions on valid locations. For
8729 instance, a fast tracepoint using a jump instead of a trap will
8730 likely have to overwrite more bytes than a trap would, and so can
8731 only be placed where the instruction is longer than the jump, or a
8732 multi-instruction sequence does not have a jump into the middle of
8733 it, etc. */
8734
8735static void
8736check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 8737 gdb::array_view<const symtab_and_line> sals)
7a697b8d 8738{
6c5b2ebe 8739 for (const auto &sal : sals)
7a697b8d 8740 {
f8eba3c6
TT
8741 struct gdbarch *sarch;
8742
6c5b2ebe 8743 sarch = get_sal_arch (sal);
f8eba3c6
TT
8744 /* We fall back to GDBARCH if there is no architecture
8745 associated with SAL. */
8746 if (sarch == NULL)
8747 sarch = gdbarch;
281d762b
TT
8748 std::string msg;
8749 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 8750 error (_("May not have a fast tracepoint at %s%s"),
281d762b 8751 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
8752 }
8753}
8754
018d34a4
VP
8755/* Given TOK, a string specification of condition and thread, as
8756 accepted by the 'break' command, extract the condition
8757 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8758 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8759 If no condition is found, *COND_STRING is set to NULL.
8760 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8761
8762static void
bbc13ae3 8763find_condition_and_thread (const char *tok, CORE_ADDR pc,
6f781ee3
TT
8764 gdb::unique_xmalloc_ptr<char> *cond_string,
8765 int *thread, int *task,
8766 gdb::unique_xmalloc_ptr<char> *rest)
018d34a4 8767{
6f781ee3 8768 cond_string->reset ();
018d34a4 8769 *thread = -1;
2ecee236 8770 *task = -1;
6f781ee3 8771 rest->reset ();
733d554a 8772 bool force = false;
ed1d1739 8773
018d34a4
VP
8774 while (tok && *tok)
8775 {
bbc13ae3 8776 const char *end_tok;
018d34a4 8777 int toklen;
bbc13ae3
KS
8778 const char *cond_start = NULL;
8779 const char *cond_end = NULL;
cc59ec59 8780
f1735a53 8781 tok = skip_spaces (tok);
e7e0cddf
SS
8782
8783 if ((*tok == '"' || *tok == ',') && rest)
8784 {
6f781ee3 8785 rest->reset (savestring (tok, strlen (tok)));
e7e0cddf
SS
8786 return;
8787 }
8788
f1735a53 8789 end_tok = skip_to_space (tok);
d634f2de 8790
018d34a4 8791 toklen = end_tok - tok;
d634f2de 8792
018d34a4
VP
8793 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8794 {
8795 tok = cond_start = end_tok + 1;
733d554a
TBA
8796 try
8797 {
8798 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8799 }
8800 catch (const gdb_exception_error &)
8801 {
8802 if (!force)
8803 throw;
8804 else
8805 tok = tok + strlen (tok);
8806 }
018d34a4 8807 cond_end = tok;
6f781ee3 8808 cond_string->reset (savestring (cond_start, cond_end - cond_start));
018d34a4 8809 }
733d554a
TBA
8810 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8811 {
5759831a 8812 tok = tok + toklen;
733d554a
TBA
8813 force = true;
8814 }
018d34a4
VP
8815 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8816 {
5d5658a1
PA
8817 const char *tmptok;
8818 struct thread_info *thr;
d634f2de 8819
980dbf36
AB
8820 if (*thread != -1)
8821 error(_("You can specify only one thread."));
8822
2ecee236 8823 if (*task != -1)
0a9ccb9d
AB
8824 error (_("You can specify only one of thread or task."));
8825
018d34a4 8826 tok = end_tok + 1;
5d5658a1 8827 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
8828 if (tok == tmptok)
8829 error (_("Junk after thread keyword."));
5d5658a1 8830 *thread = thr->global_num;
bbc13ae3 8831 tok = tmptok;
018d34a4 8832 }
4a306c9a
JB
8833 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8834 {
8835 char *tmptok;
8836
2ecee236 8837 if (*task != -1)
980dbf36
AB
8838 error(_("You can specify only one task."));
8839
0a9ccb9d
AB
8840 if (*thread != -1)
8841 error (_("You can specify only one of thread or task."));
8842
4a306c9a 8843 tok = end_tok + 1;
bbc13ae3 8844 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
8845 if (tok == tmptok)
8846 error (_("Junk after task keyword."));
8847 if (!valid_task_id (*task))
b6199126 8848 error (_("Unknown task %d."), *task);
bbc13ae3 8849 tok = tmptok;
4a306c9a 8850 }
e7e0cddf
SS
8851 else if (rest)
8852 {
6f781ee3 8853 rest->reset (savestring (tok, strlen (tok)));
ccab2054 8854 return;
e7e0cddf 8855 }
018d34a4
VP
8856 else
8857 error (_("Junk at end of arguments."));
8858 }
8859}
8860
b5fa468f
TBA
8861/* Call 'find_condition_and_thread' for each sal in SALS until a parse
8862 succeeds. The parsed values are written to COND_STRING, THREAD,
8863 TASK, and REST. See the comment of 'find_condition_and_thread'
8864 for the description of these parameters and INPUT. */
8865
8866static void
8867find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
6f781ee3
TT
8868 const char *input,
8869 gdb::unique_xmalloc_ptr<char> *cond_string,
8870 int *thread, int *task,
8871 gdb::unique_xmalloc_ptr<char> *rest)
b5fa468f
TBA
8872{
8873 int num_failures = 0;
8874 for (auto &sal : sals)
8875 {
6f781ee3 8876 gdb::unique_xmalloc_ptr<char> cond;
0a9ccb9d 8877 int thread_id = -1;
2ecee236 8878 int task_id = -1;
6f781ee3 8879 gdb::unique_xmalloc_ptr<char> remaining;
b5fa468f
TBA
8880
8881 /* Here we want to parse 'arg' to separate condition from thread
8882 number. But because parsing happens in a context and the
8883 contexts of sals might be different, try each until there is
8884 success. Finding one successful parse is sufficient for our
8885 goal. When setting the breakpoint we'll re-parse the
8886 condition in the context of each sal. */
8887 try
8888 {
8889 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8890 &task_id, &remaining);
6f781ee3 8891 *cond_string = std::move (cond);
0a9ccb9d 8892 /* At most one of thread or task can be set. */
2ecee236 8893 gdb_assert (thread_id == -1 || task_id == -1);
b5fa468f
TBA
8894 *thread = thread_id;
8895 *task = task_id;
6f781ee3 8896 *rest = std::move (remaining);
b5fa468f
TBA
8897 break;
8898 }
8899 catch (const gdb_exception_error &e)
8900 {
8901 num_failures++;
8902 /* If no sal remains, do not continue. */
8903 if (num_failures == sals.size ())
8904 throw;
8905 }
8906 }
8907}
8908
0fb4aa4b
PA
8909/* Decode a static tracepoint marker spec. */
8910
6c5b2ebe 8911static std::vector<symtab_and_line>
f00aae0f 8912decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 8913{
f00aae0f
KS
8914 const char *p = &(*arg_p)[3];
8915 const char *endp;
0fb4aa4b 8916
f1735a53 8917 p = skip_spaces (p);
0fb4aa4b 8918
f1735a53 8919 endp = skip_to_space (p);
0fb4aa4b 8920
81b1e71c 8921 std::string marker_str (p, endp - p);
0fb4aa4b 8922
5d9310c4
SM
8923 std::vector<static_tracepoint_marker> markers
8924 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8925 if (markers.empty ())
81b1e71c
TT
8926 error (_("No known static tracepoint marker named %s"),
8927 marker_str.c_str ());
0fb4aa4b 8928
6c5b2ebe 8929 std::vector<symtab_and_line> sals;
5d9310c4 8930 sals.reserve (markers.size ());
0fb4aa4b 8931
5d9310c4 8932 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 8933 {
5d9310c4
SM
8934 symtab_and_line sal = find_pc_line (marker.address, 0);
8935 sal.pc = marker.address;
6c5b2ebe 8936 sals.push_back (sal);
5d9310c4 8937 }
0fb4aa4b 8938
0fb4aa4b
PA
8939 *arg_p = endp;
8940 return sals;
8941}
8942
bac7c5cf
GB
8943/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8944 according to IS_TRACEPOINT. */
8945
8946static const struct breakpoint_ops *
264f9890
PA
8947breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8948 bool is_tracepoint)
bac7c5cf
GB
8949{
8950 if (is_tracepoint)
8951 {
264f9890 8952 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8953 return &tracepoint_probe_breakpoint_ops;
8954 else
74421c0b 8955 return &code_breakpoint_ops;
bac7c5cf
GB
8956 }
8957 else
8958 {
264f9890 8959 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8960 return &bkpt_probe_breakpoint_ops;
8961 else
74421c0b 8962 return &code_breakpoint_ops;
bac7c5cf
GB
8963 }
8964}
8965
8966/* See breakpoint.h. */
8967
8968const struct breakpoint_ops *
264f9890
PA
8969breakpoint_ops_for_location_spec (const location_spec *locspec,
8970 bool is_tracepoint)
bac7c5cf 8971{
264f9890
PA
8972 if (locspec != nullptr)
8973 return (breakpoint_ops_for_location_spec_type
7464aeaa 8974 (locspec->type (), is_tracepoint));
74421c0b 8975 return &code_breakpoint_ops;
bac7c5cf
GB
8976}
8977
f00aae0f 8978/* See breakpoint.h. */
0101ce28 8979
8cdf0e15
VP
8980int
8981create_breakpoint (struct gdbarch *gdbarch,
264f9890 8982 location_spec *locspec,
e1e01040
PA
8983 const char *cond_string,
8984 int thread, const char *extra_string,
10a636cc 8985 bool force_condition, int parse_extra,
0fb4aa4b 8986 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8987 int ignore_count,
8988 enum auto_boolean pending_break_support,
c0a91b2b 8989 const struct breakpoint_ops *ops,
44f238bb
PA
8990 int from_tty, int enabled, int internal,
8991 unsigned flags)
c3f6f71d 8992{
7efd8fc2 8993 struct linespec_result canonical;
d8de7963 8994 bool pending = false;
2ecee236 8995 int task = -1;
86b17b60 8996 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8997
348d480f
PA
8998 gdb_assert (ops != NULL);
8999
f00aae0f
KS
9000 /* If extra_string isn't useful, set it to NULL. */
9001 if (extra_string != NULL && *extra_string == '\0')
9002 extra_string = NULL;
9003
a70b8144 9004 try
b78a6381 9005 {
264f9890 9006 ops->create_sals_from_location_spec (locspec, &canonical);
b78a6381 9007 }
230d2906 9008 catch (const gdb_exception_error &e)
0101ce28 9009 {
492d29ea
PA
9010 /* If caller is interested in rc value from parse, set
9011 value. */
9012 if (e.error == NOT_FOUND_ERROR)
0101ce28 9013 {
05ff989b
AC
9014 /* If pending breakpoint support is turned off, throw
9015 error. */
fa8d40ab
JJ
9016
9017 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9018 throw;
723a2275
VP
9019
9020 exception_print (gdb_stderr, e);
fa8d40ab 9021
dda83cd7 9022 /* If pending breakpoint support is auto query and the user
05ff989b 9023 selects no, then simply return the error code. */
059fb39f 9024 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9025 && !nquery (_("Make %s pending on future shared library load? "),
9026 bptype_string (type_wanted)))
fd9b8c24 9027 return 0;
fa8d40ab 9028
05ff989b
AC
9029 /* At this point, either the user was queried about setting
9030 a pending breakpoint and selected yes, or pending
9031 breakpoint behavior is on and thus a pending breakpoint
9032 is defaulted on behalf of the user. */
d8de7963 9033 pending = true;
0101ce28 9034 }
492d29ea 9035 else
eedc3f4f 9036 throw;
0101ce28 9037 }
492d29ea 9038
6c5b2ebe 9039 if (!pending && canonical.lsals.empty ())
492d29ea 9040 return 0;
c3f6f71d 9041
c3f6f71d
JM
9042 /* Resolve all line numbers to PC's and verify that the addresses
9043 are ok for the target. */
0101ce28 9044 if (!pending)
f8eba3c6 9045 {
6c5b2ebe
PA
9046 for (auto &lsal : canonical.lsals)
9047 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9048 }
c3f6f71d 9049
7a697b8d 9050 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9051 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9052 {
6c5b2ebe
PA
9053 for (const auto &lsal : canonical.lsals)
9054 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9055 }
7a697b8d 9056
c3f6f71d
JM
9057 /* Verify that condition can be parsed, before setting any
9058 breakpoints. Allocate a separate condition expression for each
4a64f543 9059 breakpoint. */
0101ce28 9060 if (!pending)
c3f6f71d 9061 {
e1e01040
PA
9062 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9063 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9064
f00aae0f 9065 if (parse_extra)
dda83cd7 9066 {
6f781ee3
TT
9067 gdb::unique_xmalloc_ptr<char> rest;
9068 gdb::unique_xmalloc_ptr<char> cond;
52d361e1 9069
6c5b2ebe 9070 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9071
b5fa468f
TBA
9072 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9073 &cond, &thread, &task, &rest);
6f781ee3
TT
9074 cond_string_copy = std::move (cond);
9075 extra_string_copy = std::move (rest);
dda83cd7 9076 }
2f069f6f 9077 else
dda83cd7 9078 {
f00aae0f
KS
9079 if (type_wanted != bp_dprintf
9080 && extra_string != NULL && *extra_string != '\0')
9081 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9082
10a636cc
TBA
9083 /* Check the validity of the condition. We should error out
9084 if the condition is invalid at all of the locations and
9085 if it is not forced. In the PARSE_EXTRA case above, this
9086 check is done when parsing the EXTRA_STRING. */
9087 if (cond_string != nullptr && !force_condition)
9088 {
9089 int num_failures = 0;
9090 const linespec_sals &lsal = canonical.lsals[0];
9091 for (const auto &sal : lsal.sals)
9092 {
9093 const char *cond = cond_string;
9094 try
9095 {
9096 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9097 /* One success is sufficient to keep going. */
9098 break;
9099 }
9100 catch (const gdb_exception_error &)
9101 {
9102 num_failures++;
9103 /* If this is the last sal, error out. */
9104 if (num_failures == lsal.sals.size ())
9105 throw;
9106 }
9107 }
9108 }
9109
0878d0fa
YQ
9110 /* Create a private copy of condition string. */
9111 if (cond_string)
e1e01040 9112 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9113 /* Create a private copy of any extra string. */
9114 if (extra_string)
e1e01040 9115 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9116 }
0fb4aa4b 9117
52d361e1 9118 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9119 std::move (cond_string_copy),
9120 std::move (extra_string_copy),
9121 type_wanted,
d9b3f62e 9122 tempflag ? disp_del : disp_donttouch,
ef4848c7 9123 thread, task, ignore_count,
44f238bb 9124 from_tty, enabled, internal, flags);
c906108c 9125 }
0101ce28
JJ
9126 else
9127 {
73063f51
TT
9128 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9129 type_wanted);
5c1ddcb6 9130 b->locspec = locspec->clone ();
bfccc43c 9131
f00aae0f
KS
9132 if (parse_extra)
9133 b->cond_string = NULL;
e12c7713
MK
9134 else
9135 {
9136 /* Create a private copy of condition string. */
6f781ee3
TT
9137 b->cond_string.reset (cond_string != NULL
9138 ? xstrdup (cond_string)
9139 : NULL);
15630549 9140 b->thread = thread;
e12c7713 9141 }
f00aae0f
KS
9142
9143 /* Create a private copy of any extra string. */
6f781ee3
TT
9144 b->extra_string.reset (extra_string != NULL
9145 ? xstrdup (extra_string)
9146 : NULL);
0101ce28 9147 b->ignore_count = ignore_count;
0101ce28 9148 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9149 b->condition_not_parsed = 1;
41447f92 9150 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9151 if ((type_wanted != bp_breakpoint
dda83cd7 9152 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9153 b->pspace = current_program_space;
8bea4e01 9154
b270e6f9 9155 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9156 }
9157
6c5b2ebe 9158 if (canonical.lsals.size () > 1)
95a42b64 9159 {
3e43a32a
MS
9160 warning (_("Multiple breakpoints were set.\nUse the "
9161 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9162 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9163 }
9164
44702360 9165 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9166
9167 return 1;
c3f6f71d 9168}
c906108c 9169
348d480f 9170/* Set a breakpoint.
72b2ff0e
VP
9171 ARG is a string describing breakpoint address,
9172 condition, and thread.
9173 FLAG specifies if a breakpoint is hardware on,
9174 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9175 and BP_TEMPFLAG. */
348d480f 9176
98deb0da 9177static void
f2fc3015 9178break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9179{
72b2ff0e 9180 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9181 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9182 ? bp_hardware_breakpoint
9183 : bp_breakpoint);
f00aae0f 9184
264f9890
PA
9185 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9186 const struct breakpoint_ops *ops
9187 = breakpoint_ops_for_location_spec (locspec.get (),
9188 false /* is_tracepoint */);
c3f6f71d 9189
8cdf0e15 9190 create_breakpoint (get_current_arch (),
264f9890 9191 locspec.get (),
10a636cc 9192 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9193 tempflag, type_wanted,
8cdf0e15
VP
9194 0 /* Ignore count */,
9195 pending_break_support,
55aa24fb 9196 ops,
8cdf0e15 9197 from_tty,
84f4c1fe 9198 1 /* enabled */,
44f238bb
PA
9199 0 /* internal */,
9200 0);
c906108c
SS
9201}
9202
c906108c
SS
9203/* Helper function for break_command_1 and disassemble_command. */
9204
9205void
fba45db2 9206resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9207{
9208 CORE_ADDR pc;
9209
9210 if (sal->pc == 0 && sal->symtab != NULL)
9211 {
9212 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9213 error (_("No line %d in file \"%s\"."),
05cba821 9214 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9215 sal->pc = pc;
6a048695 9216
4a64f543 9217 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9218 number, then skip the function prologue if necessary. */
6a048695 9219 if (sal->explicit_line)
059acae7 9220 skip_prologue_sal (sal);
c906108c
SS
9221 }
9222
9223 if (sal->section == 0 && sal->symtab != NULL)
9224 {
346d1dfe 9225 const struct blockvector *bv;
3977b71f 9226 const struct block *b;
c5aa993b 9227 struct symbol *sym;
c906108c 9228
43f3e411 9229 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9230 sal->symtab->compunit ());
c906108c
SS
9231 if (bv != NULL)
9232 {
7f0df278 9233 sym = block_linkage_function (b);
c906108c 9234 if (sym != NULL)
dae58e04
TT
9235 sal->section
9236 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9237 else
9238 {
4a64f543 9239 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9240 just have to look harder. This case can be executed
9241 if we have line numbers but no functions (as can
9242 happen in assembly source). */
c906108c 9243
5ed8105e 9244 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9245 switch_to_program_space_and_thread (sal->pspace);
c906108c 9246
5ed8105e 9247 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9248 if (msym.minsym)
1db66e34 9249 sal->section = msym.obj_section ();
c906108c
SS
9250 }
9251 }
9252 }
9253}
9254
9255void
0b39b52e 9256break_command (const char *arg, int from_tty)
c906108c 9257{
db107f19 9258 break_command_1 (arg, 0, from_tty);
c906108c
SS
9259}
9260
c906108c 9261void
0b39b52e 9262tbreak_command (const char *arg, int from_tty)
c906108c 9263{
db107f19 9264 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9265}
9266
c906108c 9267static void
0b39b52e 9268hbreak_command (const char *arg, int from_tty)
c906108c 9269{
db107f19 9270 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9271}
9272
9273static void
0b39b52e 9274thbreak_command (const char *arg, int from_tty)
c906108c 9275{
db107f19 9276 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9277}
9278
e7e0cddf
SS
9279/* The dynamic printf command is mostly like a regular breakpoint, but
9280 with a prewired command list consisting of a single output command,
9281 built from extra arguments supplied on the dprintf command
9282 line. */
9283
da821c7b 9284static void
0b39b52e 9285dprintf_command (const char *arg, int from_tty)
e7e0cddf 9286{
264f9890 9287 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f
KS
9288
9289 /* If non-NULL, ARG should have been advanced past the location;
9290 the next character must be ','. */
9291 if (arg != NULL)
9292 {
9293 if (arg[0] != ',' || arg[1] == '\0')
9294 error (_("Format string required"));
9295 else
9296 {
9297 /* Skip the comma. */
9298 ++arg;
9299 }
9300 }
9301
e7e0cddf 9302 create_breakpoint (get_current_arch (),
264f9890 9303 locspec.get (),
10a636cc 9304 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9305 0, bp_dprintf,
9306 0 /* Ignore count */,
9307 pending_break_support,
74421c0b 9308 &code_breakpoint_ops,
e7e0cddf
SS
9309 from_tty,
9310 1 /* enabled */,
9311 0 /* internal */,
9312 0);
9313}
9314
d3ce09f5 9315static void
0b39b52e 9316agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9317{
9318 error (_("May only run agent-printf on the target"));
9319}
9320
ec45bb67 9321/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9322
ec45bb67
TT
9323int
9324ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9325 const address_space *aspace,
9326 CORE_ADDR bp_addr,
9327 const target_waitstatus &ws)
f1310107 9328{
c272a98c
SM
9329 if (ws.kind () != TARGET_WAITKIND_STOPPED
9330 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9331 return 0;
9332
f1310107
TJB
9333 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9334 bl->length, aspace, bp_addr);
9335}
9336
ec45bb67 9337/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9338
ec45bb67
TT
9339int
9340ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9341{
9342 return target_ranged_break_num_registers ();
9343}
9344
ec45bb67 9345/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9346
ec45bb67 9347enum print_stop_action
7bd86313 9348ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9349{
ec45bb67 9350 struct bp_location *bl = loc;
79a45e25 9351 struct ui_out *uiout = current_uiout;
f1310107 9352
ec45bb67 9353 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9354
9355 /* Ranged breakpoints have only one location. */
9356 gdb_assert (bl && bl->next == NULL);
9357
ec45bb67 9358 annotate_breakpoint (number);
f303dbd6
PA
9359
9360 maybe_print_thread_hit_breakpoint (uiout);
9361
ec45bb67 9362 if (disposition == disp_del)
112e8700 9363 uiout->text ("Temporary ranged breakpoint ");
f1310107 9364 else
112e8700
SM
9365 uiout->text ("Ranged breakpoint ");
9366 if (uiout->is_mi_like_p ())
f1310107 9367 {
112e8700 9368 uiout->field_string ("reason",
f1310107 9369 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9370 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9371 }
78805ff8 9372 print_num_locno (bs, uiout);
112e8700 9373 uiout->text (", ");
f1310107
TJB
9374
9375 return PRINT_SRC_AND_LOC;
9376}
9377
ec45bb67 9378/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9379
ec45bb67 9380bool
a67bcaba 9381ranged_breakpoint::print_one (bp_location **last_loc) const
f1310107 9382{
ec45bb67 9383 struct bp_location *bl = loc;
f1310107 9384 struct value_print_options opts;
79a45e25 9385 struct ui_out *uiout = current_uiout;
f1310107
TJB
9386
9387 /* Ranged breakpoints have only one location. */
9388 gdb_assert (bl && bl->next == NULL);
9389
9390 get_user_print_options (&opts);
9391
9392 if (opts.addressprint)
9393 /* We don't print the address range here, it will be printed later
f7968895 9394 by ranged_breakpoint::print_one_detail. */
112e8700 9395 uiout->field_skip ("addr");
f1310107 9396 annotate_field (5);
ec45bb67 9397 print_breakpoint_location (this, bl);
f1310107 9398 *last_loc = bl;
c01e038b
TT
9399
9400 return true;
f1310107
TJB
9401}
9402
ec45bb67 9403/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9404
ec45bb67
TT
9405void
9406ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9407{
9408 CORE_ADDR address_start, address_end;
ec45bb67 9409 struct bp_location *bl = loc;
d7e74731 9410 string_file stb;
f1310107
TJB
9411
9412 gdb_assert (bl);
9413
9414 address_start = bl->address;
9415 address_end = address_start + bl->length - 1;
9416
112e8700 9417 uiout->text ("\taddress range: ");
d7e74731
PA
9418 stb.printf ("[%s, %s]",
9419 print_core_address (bl->gdbarch, address_start),
9420 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9421 uiout->field_stream ("addr", stb);
9422 uiout->text ("\n");
f1310107
TJB
9423}
9424
ec45bb67 9425/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9426
ec45bb67 9427void
b713485d 9428ranged_breakpoint::print_mention () const
f1310107 9429{
ec45bb67 9430 struct bp_location *bl = loc;
79a45e25 9431 struct ui_out *uiout = current_uiout;
f1310107
TJB
9432
9433 gdb_assert (bl);
ec45bb67 9434 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9435
2d33446d 9436 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
ec45bb67 9437 number, paddress (bl->gdbarch, bl->address),
2d33446d 9438 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9439}
9440
ec45bb67 9441/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9442
ec45bb67 9443void
4d1ae558 9444ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9445{
6cb06a8c 9446 gdb_printf (fp, "break-range %s, %s",
709438c7
PA
9447 locspec->to_string (),
9448 locspec_range_end->to_string ());
04d0163c 9449 print_recreate_thread (fp);
f1310107
TJB
9450}
9451
f1310107
TJB
9452/* Find the address where the end of the breakpoint range should be
9453 placed, given the SAL of the end of the range. This is so that if
9454 the user provides a line number, the end of the range is set to the
9455 last instruction of the given line. */
9456
9457static CORE_ADDR
9458find_breakpoint_range_end (struct symtab_and_line sal)
9459{
9460 CORE_ADDR end;
9461
9462 /* If the user provided a PC value, use it. Otherwise,
9463 find the address of the end of the given location. */
9464 if (sal.explicit_pc)
9465 end = sal.pc;
9466 else
9467 {
9468 int ret;
9469 CORE_ADDR start;
9470
9471 ret = find_line_pc_range (sal, &start, &end);
9472 if (!ret)
9473 error (_("Could not find location of the end of the range."));
9474
9475 /* find_line_pc_range returns the start of the next line. */
9476 end--;
9477 }
9478
9479 return end;
9480}
9481
9482/* Implement the "break-range" CLI command. */
9483
9484static void
0b39b52e 9485break_range_command (const char *arg, int from_tty)
f1310107 9486{
f2fc3015 9487 const char *arg_start;
f1310107
TJB
9488 struct linespec_result canonical_start, canonical_end;
9489 int bp_count, can_use_bp, length;
9490 CORE_ADDR end;
f1310107
TJB
9491
9492 /* We don't support software ranged breakpoints. */
9493 if (target_ranged_break_num_registers () < 0)
9494 error (_("This target does not support hardware ranged breakpoints."));
9495
9496 bp_count = hw_breakpoint_used_count ();
9497 bp_count += target_ranged_break_num_registers ();
9498 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9499 bp_count, 0);
9500 if (can_use_bp < 0)
9501 error (_("Hardware breakpoints used exceeds limit."));
9502
f8eba3c6 9503 arg = skip_spaces (arg);
f1310107
TJB
9504 if (arg == NULL || arg[0] == '\0')
9505 error(_("No address range specified."));
9506
f8eba3c6 9507 arg_start = arg;
264f9890
PA
9508 location_spec_up start_locspec
9509 = string_to_location_spec (&arg, current_language);
9510 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
f1310107
TJB
9511
9512 if (arg[0] != ',')
9513 error (_("Too few arguments."));
6c5b2ebe 9514 else if (canonical_start.lsals.empty ())
f1310107 9515 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9516
6c5b2ebe 9517 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9518
6c5b2ebe
PA
9519 if (canonical_start.lsals.size () > 1
9520 || lsal_start.sals.size () != 1)
f1310107
TJB
9521 error (_("Cannot create a ranged breakpoint with multiple locations."));
9522
6c5b2ebe 9523 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9524 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9525
9526 arg++; /* Skip the comma. */
f8eba3c6 9527 arg = skip_spaces (arg);
f1310107 9528
264f9890 9529 /* Parse the end location specification. */
f1310107 9530
f1310107
TJB
9531 arg_start = arg;
9532
f8eba3c6 9533 /* We call decode_line_full directly here instead of using
264f9890
PA
9534 parse_breakpoint_sals because we need to specify the start
9535 location spec's symtab and line as the default symtab and line
9536 for the end of the range. This makes it possible to have ranges
9537 like "foo.c:27, +14", where +14 means 14 lines from the start
9538 location spec. */
9539 location_spec_up end_locspec
9540 = string_to_location_spec (&arg, current_language);
9541 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9542 sal_start.symtab, sal_start.line,
9543 &canonical_end, NULL, NULL);
9544
6c5b2ebe 9545 if (canonical_end.lsals.empty ())
f1310107 9546 error (_("Could not find location of the end of the range."));
f8eba3c6 9547
6c5b2ebe
PA
9548 const linespec_sals &lsal_end = canonical_end.lsals[0];
9549 if (canonical_end.lsals.size () > 1
9550 || lsal_end.sals.size () != 1)
f1310107
TJB
9551 error (_("Cannot create a ranged breakpoint with multiple locations."));
9552
6c5b2ebe 9553 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9554
9555 end = find_breakpoint_range_end (sal_end);
9556 if (sal_start.pc > end)
177b42fe 9557 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9558
9559 length = end - sal_start.pc + 1;
9560 if (length < 0)
9561 /* Length overflowed. */
9562 error (_("Address range too large."));
9563 else if (length == 1)
9564 {
9565 /* This range is simple enough to be handled by
9566 the `hbreak' command. */
81b1e71c 9567 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9568
9569 return;
9570 }
9571
b925bf21
PA
9572 /* Now set up the breakpoint and install it. */
9573
9574 std::unique_ptr<breakpoint> br
9575 (new ranged_breakpoint (get_current_arch (),
9576 sal_start, length,
264f9890
PA
9577 std::move (start_locspec),
9578 std::move (end_locspec)));
f1310107 9579
27a62b43 9580 install_breakpoint (false, std::move (br), true);
f1310107
TJB
9581}
9582
4a64f543
MS
9583/* Return non-zero if EXP is verified as constant. Returned zero
9584 means EXP is variable. Also the constant detection may fail for
9585 some constant expressions and in such case still falsely return
9586 zero. */
2e6e3d9c 9587
dab72643 9588static bool
65d79d4b
SDJ
9589watchpoint_exp_is_const (const struct expression *exp)
9590{
1eaebe02 9591 return exp->op->constant_p ();
65d79d4b
SDJ
9592}
9593
3a292923 9594/* Implement the "re_set" method for watchpoints. */
348d480f 9595
3a292923
TT
9596void
9597watchpoint::re_set ()
348d480f
PA
9598{
9599 /* Watchpoint can be either on expression using entirely global
9600 variables, or it can be on local variables.
9601
9602 Watchpoints of the first kind are never auto-deleted, and even
9603 persist across program restarts. Since they can use variables
9604 from shared libraries, we need to reparse expression as libraries
9605 are loaded and unloaded.
9606
9607 Watchpoints on local variables can also change meaning as result
9608 of solib event. For example, if a watchpoint uses both a local
9609 and a global variables in expression, it's a local watchpoint,
9610 but unloading of a shared library will make the expression
9611 invalid. This is not a very common use case, but we still
9612 re-evaluate expression, to avoid surprises to the user.
9613
9614 Note that for local watchpoints, we re-evaluate it only if
9615 watchpoints frame id is still valid. If it's not, it means the
9616 watchpoint is out of scope and will be deleted soon. In fact,
9617 I'm not sure we'll ever be called in this case.
9618
9619 If a local watchpoint's frame id is still valid, then
3a292923 9620 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9621
3a5c3e22
PA
9622 Don't do anything about disabled watchpoints, since they will be
9623 reevaluated again when enabled. */
d8de7963 9624 update_watchpoint (this, true /* reparse */);
348d480f
PA
9625}
9626
3a292923 9627/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9628
3a292923
TT
9629int
9630watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9631{
3a292923 9632 int length = exact ? 1 : bl->length;
e09342b5
TJB
9633
9634 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9635 cond_exp.get ());
77b06cd7
TJB
9636}
9637
3a292923 9638/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9639
3a292923
TT
9640int
9641watchpoint::remove_location (struct bp_location *bl,
9642 enum remove_bp_reason reason)
77b06cd7 9643{
3a292923 9644 int length = exact ? 1 : bl->length;
e09342b5
TJB
9645
9646 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9647 cond_exp.get ());
e09342b5
TJB
9648}
9649
3a292923
TT
9650int
9651watchpoint::breakpoint_hit (const struct bp_location *bl,
9652 const address_space *aspace, CORE_ADDR bp_addr,
9653 const target_waitstatus &ws)
e09342b5 9654{
348d480f 9655 struct breakpoint *b = bl->owner;
77b06cd7 9656
348d480f
PA
9657 /* Continuable hardware watchpoints are treated as non-existent if the
9658 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9659 some data address). Otherwise gdb won't stop on a break instruction
9660 in the code (not from a breakpoint) when a hardware watchpoint has
9661 been defined. Also skip watchpoints which we know did not trigger
9662 (did not match the data address). */
9663 if (is_hardware_watchpoint (b)
3a292923 9664 && watchpoint_triggered == watch_triggered_no)
348d480f 9665 return 0;
9c06b0b4 9666
348d480f 9667 return 1;
9c06b0b4
TJB
9668}
9669
3a292923
TT
9670void
9671watchpoint::check_status (bpstat *bs)
9c06b0b4 9672{
348d480f 9673 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9674
348d480f 9675 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9676}
9677
3a292923
TT
9678/* Implement the "resources_needed" method for hardware
9679 watchpoints. */
9c06b0b4 9680
3a292923
TT
9681int
9682watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9683{
3a292923 9684 int length = exact? 1 : bl->length;
348d480f
PA
9685
9686 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9687}
9688
3a292923
TT
9689/* Implement the "works_in_software_mode" method for hardware
9690 watchpoints. */
9c06b0b4 9691
5a61e176 9692bool
3a292923 9693watchpoint::works_in_software_mode () const
9c06b0b4 9694{
efa80663 9695 /* Read and access watchpoints only work with hardware support. */
3a292923 9696 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9697}
9698
3a292923 9699enum print_stop_action
7bd86313 9700watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9701{
348d480f 9702 struct breakpoint *b;
348d480f 9703 enum print_stop_action result;
79a45e25 9704 struct ui_out *uiout = current_uiout;
348d480f
PA
9705
9706 gdb_assert (bs->bp_location_at != NULL);
9707
348d480f
PA
9708 b = bs->breakpoint_at;
9709
f303dbd6
PA
9710 annotate_watchpoint (b->number);
9711 maybe_print_thread_hit_breakpoint (uiout);
9712
d7e74731
PA
9713 string_file stb;
9714
76f9c9cf 9715 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
9716 switch (b->type)
9717 {
348d480f 9718 case bp_watchpoint:
9c06b0b4 9719 case bp_hardware_watchpoint:
112e8700
SM
9720 if (uiout->is_mi_like_p ())
9721 uiout->field_string
9722 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 9723 mention (b);
76f9c9cf 9724 tuple_emitter.emplace (uiout, "value");
112e8700 9725 uiout->text ("\nOld value = ");
850645cf 9726 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9727 uiout->field_stream ("old", stb);
9728 uiout->text ("\nNew value = ");
3a292923 9729 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9730 uiout->field_stream ("new", stb);
9731 uiout->text ("\n");
348d480f
PA
9732 /* More than one watchpoint may have been triggered. */
9733 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9734 break;
9735
9736 case bp_read_watchpoint:
112e8700
SM
9737 if (uiout->is_mi_like_p ())
9738 uiout->field_string
9739 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 9740 mention (b);
76f9c9cf 9741 tuple_emitter.emplace (uiout, "value");
112e8700 9742 uiout->text ("\nValue = ");
3a292923 9743 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9744 uiout->field_stream ("value", stb);
9745 uiout->text ("\n");
348d480f 9746 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9747 break;
9748
9749 case bp_access_watchpoint:
348d480f
PA
9750 if (bs->old_val != NULL)
9751 {
112e8700
SM
9752 if (uiout->is_mi_like_p ())
9753 uiout->field_string
9754 ("reason",
348d480f
PA
9755 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9756 mention (b);
76f9c9cf 9757 tuple_emitter.emplace (uiout, "value");
112e8700 9758 uiout->text ("\nOld value = ");
850645cf 9759 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9760 uiout->field_stream ("old", stb);
9761 uiout->text ("\nNew value = ");
348d480f
PA
9762 }
9763 else
9764 {
9765 mention (b);
112e8700
SM
9766 if (uiout->is_mi_like_p ())
9767 uiout->field_string
9768 ("reason",
348d480f 9769 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 9770 tuple_emitter.emplace (uiout, "value");
112e8700 9771 uiout->text ("\nValue = ");
348d480f 9772 }
3a292923 9773 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9774 uiout->field_stream ("new", stb);
9775 uiout->text ("\n");
348d480f 9776 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9777 break;
9778 default:
348d480f 9779 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9780 }
9781
348d480f
PA
9782 return result;
9783}
9784
3a292923 9785/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 9786
3a292923 9787void
b713485d 9788watchpoint::print_mention () const
348d480f 9789{
79a45e25 9790 struct ui_out *uiout = current_uiout;
46b9c129 9791 const char *tuple_name;
348d480f 9792
3a292923 9793 switch (type)
348d480f
PA
9794 {
9795 case bp_watchpoint:
112e8700 9796 uiout->text ("Watchpoint ");
46b9c129 9797 tuple_name = "wpt";
348d480f
PA
9798 break;
9799 case bp_hardware_watchpoint:
112e8700 9800 uiout->text ("Hardware watchpoint ");
46b9c129 9801 tuple_name = "wpt";
348d480f
PA
9802 break;
9803 case bp_read_watchpoint:
112e8700 9804 uiout->text ("Hardware read watchpoint ");
46b9c129 9805 tuple_name = "hw-rwpt";
348d480f
PA
9806 break;
9807 case bp_access_watchpoint:
112e8700 9808 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 9809 tuple_name = "hw-awpt";
348d480f
PA
9810 break;
9811 default:
f34652de 9812 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9813 }
9814
46b9c129 9815 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9816 uiout->field_signed ("number", number);
112e8700 9817 uiout->text (": ");
3a292923 9818 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
9819}
9820
3a292923 9821/* Implement the "print_recreate" method for watchpoints. */
348d480f 9822
3a292923 9823void
4d1ae558 9824watchpoint::print_recreate (struct ui_file *fp) const
348d480f 9825{
3a292923 9826 switch (type)
348d480f
PA
9827 {
9828 case bp_watchpoint:
9829 case bp_hardware_watchpoint:
6cb06a8c 9830 gdb_printf (fp, "watch");
348d480f
PA
9831 break;
9832 case bp_read_watchpoint:
6cb06a8c 9833 gdb_printf (fp, "rwatch");
348d480f
PA
9834 break;
9835 case bp_access_watchpoint:
6cb06a8c 9836 gdb_printf (fp, "awatch");
348d480f
PA
9837 break;
9838 default:
f34652de 9839 internal_error (_("Invalid watchpoint type."));
348d480f
PA
9840 }
9841
3a292923 9842 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 9843 print_recreate_thread (fp);
348d480f
PA
9844}
9845
3a292923 9846/* Implement the "explains_signal" method for watchpoints. */
427cd150 9847
a6860f3a 9848bool
3a292923 9849watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
9850{
9851 /* A software watchpoint cannot cause a signal other than
9852 GDB_SIGNAL_TRAP. */
3a292923 9853 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 9854 return false;
427cd150 9855
a6860f3a 9856 return true;
427cd150
TT
9857}
9858
3a292923
TT
9859struct masked_watchpoint : public watchpoint
9860{
73063f51
TT
9861 using watchpoint::watchpoint;
9862
3a292923
TT
9863 int insert_location (struct bp_location *) override;
9864 int remove_location (struct bp_location *,
9865 enum remove_bp_reason reason) override;
9866 int resources_needed (const struct bp_location *) override;
5a61e176 9867 bool works_in_software_mode () const override;
7bd86313 9868 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 9869 void print_one_detail (struct ui_out *) const override;
b713485d 9870 void print_mention () const override;
4d1ae558 9871 void print_recreate (struct ui_file *fp) const override;
3a292923 9872};
348d480f 9873
3a292923 9874/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 9875
3a292923
TT
9876int
9877masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 9878{
3a292923 9879 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
9880 bl->watchpoint_type);
9881}
9882
3a292923 9883/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 9884
3a292923
TT
9885int
9886masked_watchpoint::remove_location (struct bp_location *bl,
9887 enum remove_bp_reason reason)
348d480f 9888{
3a292923 9889 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 9890 bl->watchpoint_type);
348d480f
PA
9891}
9892
3a292923
TT
9893/* Implement the "resources_needed" method for masked hardware
9894 watchpoints. */
348d480f 9895
3a292923
TT
9896int
9897masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 9898{
3a292923 9899 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
9900}
9901
3a292923
TT
9902/* Implement the "works_in_software_mode" method for masked hardware
9903 watchpoints. */
348d480f 9904
5a61e176 9905bool
3a292923 9906masked_watchpoint::works_in_software_mode () const
348d480f 9907{
5a61e176 9908 return false;
348d480f
PA
9909}
9910
3a292923
TT
9911/* Implement the "print_it" method for masked hardware
9912 watchpoints. */
348d480f 9913
3a292923 9914enum print_stop_action
7bd86313 9915masked_watchpoint::print_it (const bpstat *bs) const
348d480f
PA
9916{
9917 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9918 struct ui_out *uiout = current_uiout;
348d480f
PA
9919
9920 /* Masked watchpoints have only one location. */
9921 gdb_assert (b->loc && b->loc->next == NULL);
9922
f303dbd6
PA
9923 annotate_watchpoint (b->number);
9924 maybe_print_thread_hit_breakpoint (uiout);
9925
348d480f
PA
9926 switch (b->type)
9927 {
9928 case bp_hardware_watchpoint:
112e8700
SM
9929 if (uiout->is_mi_like_p ())
9930 uiout->field_string
9931 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9932 break;
9933
9934 case bp_read_watchpoint:
112e8700
SM
9935 if (uiout->is_mi_like_p ())
9936 uiout->field_string
9937 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9938 break;
9939
9940 case bp_access_watchpoint:
112e8700
SM
9941 if (uiout->is_mi_like_p ())
9942 uiout->field_string
9943 ("reason",
348d480f
PA
9944 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9945 break;
9946 default:
f34652de 9947 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9948 }
9949
9950 mention (b);
112e8700 9951 uiout->text (_("\n\
9c06b0b4
TJB
9952Check the underlying instruction at PC for the memory\n\
9953address and value which triggered this watchpoint.\n"));
112e8700 9954 uiout->text ("\n");
9c06b0b4
TJB
9955
9956 /* More than one watchpoint may have been triggered. */
9957 return PRINT_UNKNOWN;
9958}
9959
3a292923
TT
9960/* Implement the "print_one_detail" method for masked hardware
9961 watchpoints. */
9c06b0b4 9962
3a292923
TT
9963void
9964masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
9965{
9966 /* Masked watchpoints have only one location. */
3a292923 9967 gdb_assert (loc && loc->next == NULL);
9c06b0b4 9968
112e8700 9969 uiout->text ("\tmask ");
3a292923 9970 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
112e8700 9971 uiout->text ("\n");
9c06b0b4
TJB
9972}
9973
3a292923
TT
9974/* Implement the "print_mention" method for masked hardware
9975 watchpoints. */
9c06b0b4 9976
3a292923 9977void
b713485d 9978masked_watchpoint::print_mention () const
9c06b0b4 9979{
79a45e25 9980 struct ui_out *uiout = current_uiout;
46b9c129 9981 const char *tuple_name;
9c06b0b4 9982
3a292923 9983 switch (type)
9c06b0b4
TJB
9984 {
9985 case bp_hardware_watchpoint:
112e8700 9986 uiout->text ("Masked hardware watchpoint ");
46b9c129 9987 tuple_name = "wpt";
9c06b0b4
TJB
9988 break;
9989 case bp_read_watchpoint:
112e8700 9990 uiout->text ("Masked hardware read watchpoint ");
46b9c129 9991 tuple_name = "hw-rwpt";
9c06b0b4
TJB
9992 break;
9993 case bp_access_watchpoint:
112e8700 9994 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 9995 tuple_name = "hw-awpt";
9c06b0b4
TJB
9996 break;
9997 default:
f34652de 9998 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
9999 }
10000
46b9c129 10001 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 10002 uiout->field_signed ("number", number);
112e8700 10003 uiout->text (": ");
3a292923 10004 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
10005}
10006
3a292923
TT
10007/* Implement the "print_recreate" method for masked hardware
10008 watchpoints. */
9c06b0b4 10009
3a292923 10010void
4d1ae558 10011masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 10012{
3a292923 10013 switch (type)
9c06b0b4
TJB
10014 {
10015 case bp_hardware_watchpoint:
6cb06a8c 10016 gdb_printf (fp, "watch");
9c06b0b4
TJB
10017 break;
10018 case bp_read_watchpoint:
6cb06a8c 10019 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
10020 break;
10021 case bp_access_watchpoint:
6cb06a8c 10022 gdb_printf (fp, "awatch");
9c06b0b4
TJB
10023 break;
10024 default:
f34652de 10025 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10026 }
10027
3a292923
TT
10028 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10029 phex (hw_wp_mask, sizeof (CORE_ADDR)));
04d0163c 10030 print_recreate_thread (fp);
9c06b0b4
TJB
10031}
10032
9c06b0b4
TJB
10033/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10034
f2478a7e 10035static bool
9c06b0b4
TJB
10036is_masked_watchpoint (const struct breakpoint *b)
10037{
3a292923 10038 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
10039}
10040
53a5351d 10041/* accessflag: hw_write: watch write,
dda83cd7 10042 hw_read: watch read,
53a5351d 10043 hw_access: watch access (read or write) */
c906108c 10044static void
bbc13ae3 10045watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10046 bool just_location, bool internal)
c906108c 10047{
c1fc2657 10048 struct breakpoint *scope_breakpoint = NULL;
270140bd 10049 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10050 struct value *result;
bb9d5f81 10051 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10052 const char *exp_start = NULL;
10053 const char *exp_end = NULL;
10054 const char *tok, *end_tok;
9c06b0b4 10055 int toklen = -1;
bbc13ae3
KS
10056 const char *cond_start = NULL;
10057 const char *cond_end = NULL;
c906108c 10058 enum bptype bp_type;
37e4754d 10059 int thread = -1;
9c06b0b4
TJB
10060 /* Flag to indicate whether we are going to use masks for
10061 the hardware watchpoint. */
2e362716 10062 bool use_mask = false;
9c06b0b4 10063 CORE_ADDR mask = 0;
2ecee236 10064 int task = -1;
c906108c 10065
37e4754d
LM
10066 /* Make sure that we actually have parameters to parse. */
10067 if (arg != NULL && arg[0] != '\0')
10068 {
bbc13ae3
KS
10069 const char *value_start;
10070
10071 exp_end = arg + strlen (arg);
37e4754d 10072
9c06b0b4
TJB
10073 /* Look for "parameter value" pairs at the end
10074 of the arguments string. */
bbc13ae3 10075 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10076 {
10077 /* Skip whitespace at the end of the argument list. */
10078 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10079 tok--;
10080
10081 /* Find the beginning of the last token.
10082 This is the value of the parameter. */
10083 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10084 tok--;
10085 value_start = tok + 1;
10086
10087 /* Skip whitespace. */
10088 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10089 tok--;
10090
10091 end_tok = tok;
10092
10093 /* Find the beginning of the second to last token.
10094 This is the parameter itself. */
10095 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10096 tok--;
10097 tok++;
10098 toklen = end_tok - tok + 1;
10099
61012eef 10100 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10101 {
5d5658a1 10102 struct thread_info *thr;
9c06b0b4
TJB
10103 /* At this point we've found a "thread" token, which means
10104 the user is trying to set a watchpoint that triggers
10105 only in a specific thread. */
5d5658a1 10106 const char *endp;
37e4754d 10107
9c06b0b4
TJB
10108 if (thread != -1)
10109 error(_("You can specify only one thread."));
37e4754d 10110
2ecee236 10111 if (task != -1)
0a9ccb9d
AB
10112 error (_("You can specify only one of thread or task."));
10113
9c06b0b4 10114 /* Extract the thread ID from the next token. */
5d5658a1 10115 thr = parse_thread_id (value_start, &endp);
37e4754d 10116
5d5658a1 10117 /* Check if the user provided a valid thread ID. */
9c06b0b4 10118 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10119 invalid_thread_id_error (value_start);
9c06b0b4 10120
5d5658a1 10121 thread = thr->global_num;
9c06b0b4 10122 }
8a18382f
TT
10123 else if (toklen == 4 && startswith (tok, "task"))
10124 {
10125 char *tmp;
10126
2ecee236 10127 if (task != -1)
980dbf36
AB
10128 error(_("You can specify only one task."));
10129
0a9ccb9d
AB
10130 if (thread != -1)
10131 error (_("You can specify only one of thread or task."));
10132
8a18382f
TT
10133 task = strtol (value_start, &tmp, 0);
10134 if (tmp == value_start)
10135 error (_("Junk after task keyword."));
10136 if (!valid_task_id (task))
10137 error (_("Unknown task %d."), task);
10138 }
61012eef 10139 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10140 {
10141 /* We've found a "mask" token, which means the user wants to
10142 create a hardware watchpoint that is going to have the mask
10143 facility. */
65558ca5 10144 struct value *mask_value;
37e4754d 10145
9c06b0b4
TJB
10146 if (use_mask)
10147 error(_("You can specify only one mask."));
37e4754d 10148
2e362716 10149 use_mask = just_location = true;
37e4754d 10150
65558ca5 10151 scoped_value_mark mark;
9c06b0b4
TJB
10152 mask_value = parse_to_comma_and_eval (&value_start);
10153 mask = value_as_address (mask_value);
9c06b0b4
TJB
10154 }
10155 else
10156 /* We didn't recognize what we found. We should stop here. */
10157 break;
37e4754d 10158
9c06b0b4
TJB
10159 /* Truncate the string and get rid of the "parameter value" pair before
10160 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10161 exp_end = tok;
9c06b0b4 10162 }
37e4754d 10163 }
bbc13ae3
KS
10164 else
10165 exp_end = arg;
37e4754d 10166
bbc13ae3
KS
10167 /* Parse the rest of the arguments. From here on out, everything
10168 is in terms of a newly allocated string instead of the original
10169 ARG. */
81b1e71c
TT
10170 std::string expression (arg, exp_end - arg);
10171 exp_start = arg = expression.c_str ();
699bd4cf
TT
10172 innermost_block_tracker tracker;
10173 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10174 exp_end = arg;
fa8a61dc
TT
10175 /* Remove trailing whitespace from the expression before saving it.
10176 This makes the eventual display of the expression string a bit
10177 prettier. */
10178 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10179 --exp_end;
10180
65d79d4b 10181 /* Checking if the expression is not constant. */
4d01a485 10182 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10183 {
10184 int len;
10185
10186 len = exp_end - exp_start;
10187 while (len > 0 && isspace (exp_start[len - 1]))
10188 len--;
10189 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10190 }
10191
699bd4cf 10192 exp_valid_block = tracker.block ();
b926417a 10193 struct value *mark = value_mark ();
850645cf 10194 struct value *val_as_value = nullptr;
1eaebe02
TT
10195 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10196 just_location);
06a64a0b 10197
850645cf 10198 if (val_as_value != NULL && just_location)
bb9d5f81 10199 {
5011c493 10200 saved_bitpos = val_as_value->bitpos ();
f49d5fa2 10201 saved_bitsize = val_as_value->bitsize ();
bb9d5f81
PP
10202 }
10203
850645cf 10204 value_ref_ptr val;
06a64a0b
TT
10205 if (just_location)
10206 {
9c06b0b4
TJB
10207 int ret;
10208
06a64a0b 10209 exp_valid_block = NULL;
850645cf 10210 val = release_value (value_addr (result));
06a64a0b 10211 value_free_to_mark (mark);
9c06b0b4
TJB
10212
10213 if (use_mask)
10214 {
850645cf 10215 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10216 mask);
10217 if (ret == -1)
10218 error (_("This target does not support masked watchpoints."));
10219 else if (ret == -2)
10220 error (_("Invalid mask or memory region."));
10221 }
06a64a0b 10222 }
850645cf
TT
10223 else if (val_as_value != NULL)
10224 val = release_value (val_as_value);
c906108c 10225
f1735a53
TT
10226 tok = skip_spaces (arg);
10227 end_tok = skip_to_space (tok);
c906108c
SS
10228
10229 toklen = end_tok - tok;
10230 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10231 {
10232 tok = cond_start = end_tok + 1;
699bd4cf
TT
10233 innermost_block_tracker if_tracker;
10234 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10235
10236 /* The watchpoint expression may not be local, but the condition
10237 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10238 cond_exp_valid_block = if_tracker.block ();
60e1c644 10239
c906108c
SS
10240 cond_end = tok;
10241 }
10242 if (*tok)
8a3fe4f8 10243 error (_("Junk at end of command."));
c906108c 10244
bd2b40ac 10245 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
441d7c93
PA
10246
10247 /* Save this because create_internal_breakpoint below invalidates
10248 'wp_frame'. */
10249 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10250
10251 /* If the expression is "local", then set up a "watchpoint scope"
10252 breakpoint at the point where we've left the scope of the watchpoint
10253 expression. Create the scope breakpoint before the watchpoint, so
10254 that we will encounter it first in bpstat_stop_status. */
441d7c93 10255 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10256 {
441d7c93
PA
10257 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10258
10259 if (frame_id_p (caller_frame_id))
edb3359d 10260 {
441d7c93
PA
10261 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10262 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10263
24b21115 10264 scope_breakpoint
441d7c93 10265 = create_internal_breakpoint (caller_arch, caller_pc,
9efa3c7f 10266 bp_watchpoint_scope);
d983da9c 10267
441d7c93
PA
10268 /* create_internal_breakpoint could invalidate WP_FRAME. */
10269 wp_frame = NULL;
10270
edb3359d 10271 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10272
edb3359d
DJ
10273 /* Automatically delete the breakpoint when it hits. */
10274 scope_breakpoint->disposition = disp_del;
d983da9c 10275
edb3359d 10276 /* Only break in the proper frame (help with recursion). */
441d7c93 10277 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10278
edb3359d 10279 /* Set the address at which we will stop. */
441d7c93
PA
10280 scope_breakpoint->loc->gdbarch = caller_arch;
10281 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10282 scope_breakpoint->loc->address
a6d9a66e
UW
10283 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10284 scope_breakpoint->loc->requested_address,
60270718
AB
10285 scope_breakpoint->type,
10286 current_program_space);
edb3359d 10287 }
d983da9c
DJ
10288 }
10289
e8369a73
AB
10290 /* Now set up the breakpoint. We create all watchpoints as hardware
10291 watchpoints here even if hardware watchpoints are turned off, a call
10292 to update_watchpoint later in this function will cause the type to
10293 drop back to bp_watchpoint (software watchpoint) if required. */
10294
10295 if (accessflag == hw_read)
10296 bp_type = bp_read_watchpoint;
10297 else if (accessflag == hw_access)
10298 bp_type = bp_access_watchpoint;
10299 else
10300 bp_type = bp_hardware_watchpoint;
3a5c3e22 10301
3a292923 10302 std::unique_ptr<watchpoint> w;
348d480f 10303 if (use_mask)
73063f51 10304 w.reset (new masked_watchpoint (nullptr, bp_type));
348d480f 10305 else
73063f51 10306 w.reset (new watchpoint (nullptr, bp_type));
3a292923 10307
0a9ccb9d 10308 /* At most one of thread or task can be set on a watchpoint. */
2ecee236 10309 gdb_assert (thread == -1 || task == -1);
c1fc2657 10310 w->thread = thread;
8a18382f 10311 w->task = task;
c1fc2657
SM
10312 w->disposition = disp_donttouch;
10313 w->pspace = current_program_space;
b22e99fd 10314 w->exp = std::move (exp);
3a5c3e22
PA
10315 w->exp_valid_block = exp_valid_block;
10316 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10317 if (just_location)
10318 {
f28085df 10319 struct type *t = val->type ();
850645cf 10320 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10321
43cc5389 10322 w->exp_string_reparse
a4c50be3 10323 = current_language->watch_location_expression (t, addr);
06a64a0b 10324
8579fd13
AB
10325 w->exp_string = xstrprintf ("-location %.*s",
10326 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10327 }
10328 else
a4c50be3 10329 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10330
10331 if (use_mask)
10332 {
3a5c3e22 10333 w->hw_wp_mask = mask;
9c06b0b4
TJB
10334 }
10335 else
10336 {
3a5c3e22 10337 w->val = val;
bb9d5f81
PP
10338 w->val_bitpos = saved_bitpos;
10339 w->val_bitsize = saved_bitsize;
4c1d86d9 10340 w->val_valid = true;
9c06b0b4 10341 }
77b06cd7 10342
c906108c 10343 if (cond_start)
6f781ee3 10344 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10345 else
c1fc2657 10346 w->cond_string = 0;
c5aa993b 10347
441d7c93 10348 if (frame_id_p (watchpoint_frame))
f6bc2008 10349 {
441d7c93 10350 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10351 w->watchpoint_thread = inferior_ptid;
f6bc2008 10352 }
c906108c 10353 else
f6bc2008 10354 {
3a5c3e22
PA
10355 w->watchpoint_frame = null_frame_id;
10356 w->watchpoint_thread = null_ptid;
f6bc2008 10357 }
c906108c 10358
d983da9c 10359 if (scope_breakpoint != NULL)
c906108c 10360 {
d983da9c
DJ
10361 /* The scope breakpoint is related to the watchpoint. We will
10362 need to act on them together. */
c1fc2657 10363 w->related_breakpoint = scope_breakpoint;
b270e6f9 10364 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10365 }
d983da9c 10366
06a64a0b
TT
10367 if (!just_location)
10368 value_free_to_mark (mark);
2d134ed3 10369
b270e6f9
TT
10370 /* Finally update the new watchpoint. This creates the locations
10371 that should be inserted. */
d8de7963 10372 update_watchpoint (w.get (), true /* reparse */);
a9634178 10373
b270e6f9 10374 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10375}
10376
e09342b5 10377/* Return count of debug registers needed to watch the given expression.
e09342b5 10378 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10379
c906108c 10380static int
a6535de1 10381can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10382{
10383 int found_memory_cnt = 0;
10384
10385 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10386 if (!can_use_hw_watchpoints)
c906108c 10387 return 0;
c5aa993b 10388
a6535de1
TT
10389 gdb_assert (!vals.empty ());
10390 struct value *head = vals[0].get ();
10391
5c44784c
JM
10392 /* Make sure that the value of the expression depends only upon
10393 memory contents, and values computed from them within GDB. If we
10394 find any register references or function calls, we can't use a
10395 hardware watchpoint.
10396
10397 The idea here is that evaluating an expression generates a series
10398 of values, one holding the value of every subexpression. (The
10399 expression a*b+c has five subexpressions: a, b, a*b, c, and
10400 a*b+c.) GDB's values hold almost enough information to establish
10401 the criteria given above --- they identify memory lvalues,
10402 register lvalues, computed values, etcetera. So we can evaluate
10403 the expression, and then scan the chain of values that leaves
10404 behind to decide whether we can detect any possible change to the
10405 expression's final value using only hardware watchpoints.
10406
10407 However, I don't think that the values returned by inferior
10408 function calls are special in any way. So this function may not
10409 notice that an expression involving an inferior function call
10410 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10411 for (const value_ref_ptr &iter : vals)
c906108c 10412 {
a6535de1
TT
10413 struct value *v = iter.get ();
10414
736355f2 10415 if (v->lval () == lval_memory)
c906108c 10416 {
3ee3b270 10417 if (v != head && v->lazy ())
8464be76
DJ
10418 /* A lazy memory lvalue in the chain is one that GDB never
10419 needed to fetch; we either just used its address (e.g.,
10420 `a' in `a.b') or we never needed it at all (e.g., `a'
10421 in `a,b'). This doesn't apply to HEAD; if that is
10422 lazy then it was not readable, but watch it anyway. */
5c44784c 10423 ;
53a5351d 10424 else
5c44784c
JM
10425 {
10426 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10427 it with hardware watchpoints. */
d0c97917 10428 struct type *vtype = check_typedef (v->type ());
2e70b7b9
MS
10429
10430 /* We only watch structs and arrays if user asked for it
10431 explicitly, never if they just happen to appear in a
10432 middle of some value chain. */
10433 if (v == head
78134374
SM
10434 || (vtype->code () != TYPE_CODE_STRUCT
10435 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10436 {
9feb2d07 10437 CORE_ADDR vaddr = v->address ();
e09342b5
TJB
10438 int len;
10439 int num_regs;
10440
a9634178 10441 len = (target_exact_watchpoints
e09342b5 10442 && is_scalar_type_recursive (vtype))?
d0c97917 10443 1 : v->type ()->length ();
2e70b7b9 10444
e09342b5
TJB
10445 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10446 if (!num_regs)
2e70b7b9
MS
10447 return 0;
10448 else
e09342b5 10449 found_memory_cnt += num_regs;
2e70b7b9 10450 }
5c44784c 10451 }
c5aa993b 10452 }
736355f2 10453 else if (v->lval () != not_lval
4b53ca88 10454 && v->deprecated_modifiable () == 0)
38b6c3b3 10455 return 0; /* These are values from the history (e.g., $1). */
736355f2 10456 else if (v->lval () == lval_register)
38b6c3b3 10457 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10458 }
10459
10460 /* The expression itself looks suitable for using a hardware
10461 watchpoint, but give the target machine a chance to reject it. */
10462 return found_memory_cnt;
10463}
10464
8b93c638 10465void
2e362716 10466watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10467{
84f4c1fe 10468 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10469}
10470
a15a5258
AB
10471/* Options for the watch, awatch, and rwatch commands. */
10472
10473struct watch_options
10474{
10475 /* For -location. */
10476 bool location = false;
10477};
10478
10479/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10480
10481 Historically GDB always accepted both '-location' and '-l' flags for
10482 these commands (both flags being synonyms). When converting to the
10483 newer option scheme only '-location' is added here. That's fine (for
10484 backward compatibility) as any non-ambiguous prefix of a flag will be
10485 accepted, so '-l', '-loc', are now all accepted.
10486
10487 What this means is that, if in the future, we add any new flag here
10488 that starts with '-l' then this will break backward compatibility, so
10489 please, don't do that! */
10490
10491static const gdb::option::option_def watch_option_defs[] = {
10492 gdb::option::flag_option_def<watch_options> {
10493 "location",
10494 [] (watch_options *opt) { return &opt->location; },
10495 N_("\
10496This evaluates EXPRESSION and watches the memory to which is refers.\n\
10497-l can be used as a short form of -location."),
10498 },
10499};
10500
10501/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10502 commands. */
10503
10504static gdb::option::option_def_group
10505make_watch_options_def_group (watch_options *opts)
10506{
10507 return {{watch_option_defs}, opts};
10508}
10509
06a64a0b
TT
10510/* A helper function that looks for the "-location" argument and then
10511 calls watch_command_1. */
10512
10513static void
0b39b52e 10514watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10515{
a15a5258
AB
10516 watch_options opts;
10517 auto grp = make_watch_options_def_group (&opts);
10518 gdb::option::process_options
10519 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10520 if (arg != nullptr && *arg == '\0')
10521 arg = nullptr;
10522
10523 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10524}
06a64a0b 10525
a15a5258
AB
10526/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10527static void
10528watch_command_completer (struct cmd_list_element *ignore,
10529 completion_tracker &tracker,
10530 const char *text, const char * /*word*/)
10531{
10532 const auto group = make_watch_options_def_group (nullptr);
10533 if (gdb::option::complete_options
10534 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10535 return;
06a64a0b 10536
a15a5258
AB
10537 const char *word = advance_to_expression_complete_word_point (tracker, text);
10538 expression_completer (ignore, tracker, text, word);
8b93c638 10539}
8926118c 10540
c5aa993b 10541static void
0b39b52e 10542watch_command (const char *arg, int from_tty)
c906108c 10543{
06a64a0b 10544 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10545}
10546
8b93c638 10547void
2e362716 10548rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10549{
84f4c1fe 10550 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10551}
8926118c 10552
c5aa993b 10553static void
0b39b52e 10554rwatch_command (const char *arg, int from_tty)
c906108c 10555{
06a64a0b 10556 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10557}
10558
8b93c638 10559void
2e362716 10560awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10561{
84f4c1fe 10562 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10563}
8926118c 10564
c5aa993b 10565static void
0b39b52e 10566awatch_command (const char *arg, int from_tty)
c906108c 10567{
06a64a0b 10568 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10569}
c906108c 10570\f
c5aa993b 10571
cfc31633
PA
10572/* Data for the FSM that manages the until(location)/advance commands
10573 in infcmd.c. Here because it uses the mechanisms of
10574 breakpoints. */
c906108c 10575
46e3ed7f 10576struct until_break_fsm : public thread_fsm
bfec99b2 10577{
46e3ed7f 10578 /* The thread that was current when the command was executed. */
cfc31633
PA
10579 int thread;
10580
df631783
PA
10581 /* The breakpoint set at the return address in the caller frame,
10582 plus breakpoints at all the destination locations. */
10583 std::vector<breakpoint_up> breakpoints;
cfc31633 10584
46e3ed7f 10585 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 10586 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
10587 : thread_fsm (cmd_interp),
10588 thread (thread),
df631783 10589 breakpoints (std::move (breakpoints))
46e3ed7f
TT
10590 {
10591 }
cfc31633 10592
46e3ed7f
TT
10593 void clean_up (struct thread_info *thread) override;
10594 bool should_stop (struct thread_info *thread) override;
10595 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10596};
10597
cfc31633
PA
10598/* Implementation of the 'should_stop' FSM method for the
10599 until(location)/advance commands. */
10600
46e3ed7f
TT
10601bool
10602until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10603{
df631783
PA
10604 for (const breakpoint_up &bp : breakpoints)
10605 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10606 bp.get ()) != NULL)
10607 {
10608 set_finished ();
10609 break;
10610 }
cfc31633 10611
46e3ed7f 10612 return true;
cfc31633
PA
10613}
10614
10615/* Implementation of the 'clean_up' FSM method for the
10616 until(location)/advance commands. */
10617
46e3ed7f
TT
10618void
10619until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10620{
cfc31633 10621 /* Clean up our temporary breakpoints. */
df631783 10622 breakpoints.clear ();
46e3ed7f 10623 delete_longjmp_breakpoint (thread);
cfc31633
PA
10624}
10625
10626/* Implementation of the 'async_reply_reason' FSM method for the
10627 until(location)/advance commands. */
10628
46e3ed7f
TT
10629enum async_reply_reason
10630until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10631{
10632 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10633}
10634
c906108c 10635void
f2fc3015 10636until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10637{
bd2b40ac 10638 frame_info_ptr frame;
8556afb4
PA
10639 struct gdbarch *frame_gdbarch;
10640 struct frame_id stack_frame_id;
10641 struct frame_id caller_frame_id;
186c406b
TT
10642 int thread;
10643 struct thread_info *tp;
c906108c 10644
70509625 10645 clear_proceed_status (0);
c906108c
SS
10646
10647 /* Set a breakpoint where the user wants it and at return from
4a64f543 10648 this function. */
c5aa993b 10649
264f9890 10650 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f 10651
6c5b2ebe
PA
10652 std::vector<symtab_and_line> sals
10653 = (last_displayed_sal_is_valid ()
264f9890 10654 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
6c5b2ebe
PA
10655 get_last_displayed_symtab (),
10656 get_last_displayed_line ())
264f9890 10657 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 10658 NULL, NULL, 0));
c5aa993b 10659
df631783 10660 if (sals.empty ())
8a3fe4f8 10661 error (_("Couldn't get information on specified line."));
c5aa993b 10662
c906108c 10663 if (*arg)
8a3fe4f8 10664 error (_("Junk at end of arguments."));
c5aa993b 10665
186c406b 10666 tp = inferior_thread ();
5d5658a1 10667 thread = tp->global_num;
186c406b 10668
8556afb4
PA
10669 /* Note linespec handling above invalidates the frame chain.
10670 Installing a breakpoint also invalidates the frame chain (as it
10671 may need to switch threads), so do any frame handling before
10672 that. */
10673
10674 frame = get_selected_frame (NULL);
10675 frame_gdbarch = get_frame_arch (frame);
10676 stack_frame_id = get_stack_frame_id (frame);
10677 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10678
ae66c1fc
EZ
10679 /* Keep within the current frame, or in frames called by the current
10680 one. */
edb3359d 10681
df631783 10682 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
10683
10684 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10685
883bc8d1 10686 if (frame_id_p (caller_frame_id))
c906108c 10687 {
883bc8d1 10688 struct symtab_and_line sal2;
cfc31633 10689 struct gdbarch *caller_gdbarch;
883bc8d1
PA
10690
10691 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10692 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 10693 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
10694
10695 breakpoint_up caller_breakpoint
10696 = set_momentary_breakpoint (caller_gdbarch, sal2,
10697 caller_frame_id, bp_until);
10698 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 10699
0f443d1b 10700 set_longjmp_breakpoint (tp, stack_frame_id);
5419bdae 10701 lj_deleter.emplace (thread);
c906108c 10702 }
c5aa993b 10703
c70a6932
JK
10704 /* set_momentary_breakpoint could invalidate FRAME. */
10705 frame = NULL;
10706
df631783
PA
10707 /* If the user told us to continue until a specified location, we
10708 don't specify a frame at which we need to stop. Otherwise,
10709 specify the selected frame, because we want to stop only at the
10710 very same frame. */
10711 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10712
10713 for (symtab_and_line &sal : sals)
10714 {
10715 resolve_sal_pc (&sal);
10716
10717 breakpoint_up location_breakpoint
10718 = set_momentary_breakpoint (frame_gdbarch, sal,
10719 stop_frame_id, bp_until);
10720 breakpoints.emplace_back (std::move (location_breakpoint));
10721 }
883bc8d1 10722
573269a8
LS
10723 tp->set_thread_fsm
10724 (std::unique_ptr<thread_fsm>
10725 (new until_break_fsm (command_interp (), tp->global_num,
10726 std::move (breakpoints))));
f107f563 10727
5419bdae
TT
10728 if (lj_deleter)
10729 lj_deleter->release ();
f107f563 10730
cfc31633 10731 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 10732}
ae66c1fc 10733
c906108c
SS
10734\f
10735
81b1e71c 10736/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
10737
10738static int
81b1e71c 10739compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 10740{
81b1e71c
TT
10741 uintptr_t ua = (uintptr_t) a;
10742 uintptr_t ub = (uintptr_t) b;
8a2c437b 10743
81b1e71c 10744 if (a->number < b->number)
8a2c437b 10745 return -1;
81b1e71c 10746 else if (a->number > b->number)
8a2c437b
TT
10747 return 1;
10748
10749 /* Now sort by address, in case we see, e..g, two breakpoints with
10750 the number 0. */
10751 if (ua < ub)
10752 return -1;
94b0e70d 10753 return ua > ub ? 1 : 0;
8a2c437b
TT
10754}
10755
80f8a6eb 10756/* Delete breakpoints by address or line. */
c906108c
SS
10757
10758static void
0b39b52e 10759clear_command (const char *arg, int from_tty)
c906108c 10760{
c906108c 10761 int default_match;
c906108c 10762
6c5b2ebe
PA
10763 std::vector<symtab_and_line> decoded_sals;
10764 symtab_and_line last_sal;
10765 gdb::array_view<symtab_and_line> sals;
c906108c
SS
10766 if (arg)
10767 {
6c5b2ebe
PA
10768 decoded_sals
10769 = decode_line_with_current_source (arg,
10770 (DECODE_LINE_FUNFIRSTLINE
10771 | DECODE_LINE_LIST_MODE));
c906108c 10772 default_match = 0;
6c5b2ebe 10773 sals = decoded_sals;
c906108c
SS
10774 }
10775 else
10776 {
1bfeeb0f
JL
10777 /* Set sal's line, symtab, pc, and pspace to the values
10778 corresponding to the last call to print_frame_info. If the
10779 codepoint is not valid, this will set all the fields to 0. */
51abb421 10780 last_sal = get_last_displayed_sal ();
6c5b2ebe 10781 if (last_sal.symtab == 0)
8a3fe4f8 10782 error (_("No source file specified."));
c906108c 10783
c906108c 10784 default_match = 1;
6c5b2ebe 10785 sals = last_sal;
c906108c
SS
10786 }
10787
4a64f543
MS
10788 /* We don't call resolve_sal_pc here. That's not as bad as it
10789 seems, because all existing breakpoints typically have both
10790 file/line and pc set. So, if clear is given file/line, we can
10791 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10792
10793 We only support clearing given the address explicitly
10794 present in breakpoint table. Say, we've set breakpoint
4a64f543 10795 at file:line. There were several PC values for that file:line,
ed0616c6 10796 due to optimization, all in one block.
4a64f543
MS
10797
10798 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10799 PC corresponding to the same file:line, the breakpoint won't
10800 be cleared. We probably can still clear the breakpoint, but
10801 since the other PC value is never presented to user, user
10802 can only find it by guessing, and it does not seem important
10803 to support that. */
10804
4a64f543
MS
10805 /* For each line spec given, delete bps which correspond to it. Do
10806 it in two passes, solely to preserve the current behavior that
10807 from_tty is forced true if we delete more than one
10808 breakpoint. */
c906108c 10809
81b1e71c 10810 std::vector<struct breakpoint *> found;
6c5b2ebe 10811 for (const auto &sal : sals)
c906108c 10812 {
05cba821
JK
10813 const char *sal_fullname;
10814
c906108c 10815 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
10816 If line given (pc == 0), clear all bpts on specified line.
10817 If defaulting, clear all bpts on default line
10818 or at default pc.
c5aa993b 10819
dda83cd7 10820 defaulting sal.pc != 0 tests to do
c5aa993b 10821
dda83cd7
SM
10822 0 1 pc
10823 1 1 pc _and_ line
10824 0 0 line
10825 1 0 <can't happen> */
c906108c 10826
05cba821
JK
10827 sal_fullname = (sal.symtab == NULL
10828 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 10829
4a64f543 10830 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 10831 for (breakpoint *b : all_breakpoints ())
c5aa993b 10832 {
0d381245 10833 int match = 0;
4a64f543 10834 /* Are we going to delete b? */
a5c69b1e
EL
10835 if (b->type != bp_none && !is_watchpoint (b)
10836 && user_breakpoint_p (b))
0d381245 10837 {
40cb8ca5 10838 for (bp_location *loc : b->locations ())
0d381245 10839 {
f8eba3c6
TT
10840 /* If the user specified file:line, don't allow a PC
10841 match. This matches historical gdb behavior. */
10842 int pc_match = (!sal.explicit_line
10843 && sal.pc
10844 && (loc->pspace == sal.pspace)
10845 && (loc->address == sal.pc)
10846 && (!section_is_overlay (loc->section)
10847 || loc->section == sal.section));
4aac40c8
TT
10848 int line_match = 0;
10849
10850 if ((default_match || sal.explicit_line)
2f202fde 10851 && loc->symtab != NULL
05cba821 10852 && sal_fullname != NULL
4aac40c8 10853 && sal.pspace == loc->pspace
05cba821
JK
10854 && loc->line_number == sal.line
10855 && filename_cmp (symtab_to_fullname (loc->symtab),
10856 sal_fullname) == 0)
10857 line_match = 1;
4aac40c8 10858
0d381245
VP
10859 if (pc_match || line_match)
10860 {
10861 match = 1;
10862 break;
10863 }
10864 }
10865 }
10866
10867 if (match)
81b1e71c 10868 found.push_back (b);
c906108c 10869 }
80f8a6eb 10870 }
8a2c437b 10871
80f8a6eb 10872 /* Now go thru the 'found' chain and delete them. */
81b1e71c 10873 if (found.empty ())
80f8a6eb
MS
10874 {
10875 if (arg)
8a3fe4f8 10876 error (_("No breakpoint at %s."), arg);
80f8a6eb 10877 else
8a3fe4f8 10878 error (_("No breakpoint at this line."));
80f8a6eb 10879 }
c906108c 10880
8a2c437b 10881 /* Remove duplicates from the vec. */
81b1e71c 10882 std::sort (found.begin (), found.end (),
b926417a 10883 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10884 {
b926417a 10885 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
10886 });
10887 found.erase (std::unique (found.begin (), found.end (),
b926417a 10888 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10889 {
b926417a 10890 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
10891 }),
10892 found.end ());
8a2c437b 10893
81b1e71c 10894 if (found.size () > 1)
4a64f543 10895 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10896 if (from_tty)
a3f17187 10897 {
81b1e71c 10898 if (found.size () == 1)
6cb06a8c 10899 gdb_printf (_("Deleted breakpoint "));
a3f17187 10900 else
6cb06a8c 10901 gdb_printf (_("Deleted breakpoints "));
a3f17187 10902 }
d6e956e5 10903
81b1e71c 10904 for (breakpoint *iter : found)
80f8a6eb 10905 {
c5aa993b 10906 if (from_tty)
6cb06a8c 10907 gdb_printf ("%d ", iter->number);
81b1e71c 10908 delete_breakpoint (iter);
c906108c 10909 }
80f8a6eb 10910 if (from_tty)
a11ac3b3 10911 gdb_putc ('\n');
c906108c
SS
10912}
10913\f
10914/* Delete breakpoint in BS if they are `delete' breakpoints and
10915 all breakpoints that are marked for deletion, whether hit or not.
10916 This is called after any breakpoint is hit, or after errors. */
10917
10918void
313f3b21 10919breakpoint_auto_delete (bpstat *bs)
c906108c 10920{
c906108c 10921 for (; bs; bs = bs->next)
f431efe5
PA
10922 if (bs->breakpoint_at
10923 && bs->breakpoint_at->disposition == disp_del
c906108c 10924 && bs->stop)
f431efe5 10925 delete_breakpoint (bs->breakpoint_at);
c906108c 10926
1428b37a 10927 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 10928 if (b->disposition == disp_del_at_next_stop)
c5aa993b 10929 delete_breakpoint (b);
c906108c
SS
10930}
10931
4a64f543 10932/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 10933 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
10934 bl_address_is_meaningful says), secondarily by ordering first
10935 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 10936 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
10937
10938static int
39ef2f62 10939bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 10940{
876fa593 10941 if (a->address != b->address)
39ef2f62 10942 return a->address < b->address;
876fa593 10943
dea2aa5f
LM
10944 /* Sort locations at the same address by their pspace number, keeping
10945 locations of the same inferior (in a multi-inferior environment)
10946 grouped. */
10947
10948 if (a->pspace->num != b->pspace->num)
39ef2f62 10949 return a->pspace->num < b->pspace->num;
dea2aa5f 10950
876fa593 10951 /* Sort permanent breakpoints first. */
1a853c52 10952 if (a->permanent != b->permanent)
39ef2f62 10953 return a->permanent > b->permanent;
876fa593 10954
7f32a4d5
PA
10955 /* Sort by type in order to make duplicate determination easier.
10956 See update_global_location_list. This is kept in sync with
10957 breakpoint_locations_match. */
10958 if (a->loc_type < b->loc_type)
10959 return true;
10960
10961 /* Likewise, for range-breakpoints, sort by length. */
10962 if (a->loc_type == bp_loc_hardware_breakpoint
10963 && b->loc_type == bp_loc_hardware_breakpoint
10964 && a->length < b->length)
10965 return true;
10966
c56a97f9
JK
10967 /* Make the internal GDB representation stable across GDB runs
10968 where A and B memory inside GDB can differ. Breakpoint locations of
10969 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
10970
10971 if (a->owner->number != b->owner->number)
39ef2f62 10972 return a->owner->number < b->owner->number;
876fa593 10973
39ef2f62 10974 return a < b;
876fa593
JK
10975}
10976
f5336ca5
PA
10977/* Set bp_locations_placed_address_before_address_max and
10978 bp_locations_shadow_len_after_address_max according to the current
10979 content of the bp_locations array. */
f7545552
TT
10980
10981static void
f5336ca5 10982bp_locations_target_extensions_update (void)
f7545552 10983{
f5336ca5
PA
10984 bp_locations_placed_address_before_address_max = 0;
10985 bp_locations_shadow_len_after_address_max = 0;
876fa593 10986
48d7020b 10987 for (bp_location *bl : all_bp_locations ())
876fa593
JK
10988 {
10989 CORE_ADDR start, end, addr;
10990
10991 if (!bp_location_has_shadow (bl))
10992 continue;
10993
10994 start = bl->target_info.placed_address;
10995 end = start + bl->target_info.shadow_len;
10996
10997 gdb_assert (bl->address >= start);
10998 addr = bl->address - start;
f5336ca5
PA
10999 if (addr > bp_locations_placed_address_before_address_max)
11000 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11001
11002 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11003
11004 gdb_assert (bl->address < end);
11005 addr = end - bl->address;
f5336ca5
PA
11006 if (addr > bp_locations_shadow_len_after_address_max)
11007 bp_locations_shadow_len_after_address_max = addr;
876fa593 11008 }
f7545552
TT
11009}
11010
1e4d1764
YQ
11011/* Download tracepoint locations if they haven't been. */
11012
11013static void
11014download_tracepoint_locations (void)
11015{
dd2e65cc 11016 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11017
5ed8105e 11018 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11019
f6d17b2b 11020 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
11021 {
11022 struct tracepoint *t;
d8de7963 11023 bool bp_location_downloaded = false;
1e4d1764 11024
7ed2c994 11025 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11026 ? !may_insert_fast_tracepoints
11027 : !may_insert_tracepoints))
11028 continue;
11029
dd2e65cc
YQ
11030 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11031 {
11032 if (target_can_download_tracepoint ())
11033 can_download_tracepoint = TRIBOOL_TRUE;
11034 else
11035 can_download_tracepoint = TRIBOOL_FALSE;
11036 }
11037
11038 if (can_download_tracepoint == TRIBOOL_FALSE)
11039 break;
11040
40cb8ca5 11041 for (bp_location *bl : b->locations ())
7ed2c994
YQ
11042 {
11043 /* In tracepoint, locations are _never_ duplicated, so
11044 should_be_inserted is equivalent to
11045 unduplicated_should_be_inserted. */
11046 if (!should_be_inserted (bl) || bl->inserted)
11047 continue;
1e4d1764 11048
7ed2c994 11049 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11050
7ed2c994 11051 target_download_tracepoint (bl);
1e4d1764 11052
7ed2c994 11053 bl->inserted = 1;
d8de7963 11054 bp_location_downloaded = true;
7ed2c994
YQ
11055 }
11056 t = (struct tracepoint *) b;
11057 t->number_on_target = b->number;
f2a8bc8a 11058 if (bp_location_downloaded)
76727919 11059 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11060 }
1e4d1764
YQ
11061}
11062
934709f0
PW
11063/* Swap the insertion/duplication state between two locations. */
11064
11065static void
11066swap_insertion (struct bp_location *left, struct bp_location *right)
11067{
11068 const int left_inserted = left->inserted;
11069 const int left_duplicate = left->duplicate;
b775012e 11070 const int left_needs_update = left->needs_update;
934709f0
PW
11071 const struct bp_target_info left_target_info = left->target_info;
11072
1e4d1764
YQ
11073 /* Locations of tracepoints can never be duplicated. */
11074 if (is_tracepoint (left->owner))
11075 gdb_assert (!left->duplicate);
11076 if (is_tracepoint (right->owner))
11077 gdb_assert (!right->duplicate);
11078
934709f0
PW
11079 left->inserted = right->inserted;
11080 left->duplicate = right->duplicate;
b775012e 11081 left->needs_update = right->needs_update;
934709f0
PW
11082 left->target_info = right->target_info;
11083 right->inserted = left_inserted;
11084 right->duplicate = left_duplicate;
b775012e 11085 right->needs_update = left_needs_update;
934709f0
PW
11086 right->target_info = left_target_info;
11087}
11088
b775012e
LM
11089/* Force the re-insertion of the locations at ADDRESS. This is called
11090 once a new/deleted/modified duplicate location is found and we are evaluating
11091 conditions on the target's side. Such conditions need to be updated on
11092 the target. */
11093
11094static void
11095force_breakpoint_reinsertion (struct bp_location *bl)
11096{
b775012e
LM
11097 CORE_ADDR address = 0;
11098 int pspace_num;
11099
11100 address = bl->address;
11101 pspace_num = bl->pspace->num;
11102
11103 /* This is only meaningful if the target is
11104 evaluating conditions and if the user has
11105 opted for condition evaluation on the target's
11106 side. */
11107 if (gdb_evaluates_breakpoint_condition_p ()
11108 || !target_supports_evaluation_of_breakpoint_conditions ())
11109 return;
11110
11111 /* Flag all breakpoint locations with this address and
11112 the same program space as the location
11113 as "its condition has changed". We need to
11114 update the conditions on the target's side. */
e0d9a270 11115 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11116 {
b775012e
LM
11117 if (!is_breakpoint (loc->owner)
11118 || pspace_num != loc->pspace->num)
11119 continue;
11120
11121 /* Flag the location appropriately. We use a different state to
11122 let everyone know that we already updated the set of locations
11123 with addr bl->address and program space bl->pspace. This is so
11124 we don't have to keep calling these functions just to mark locations
11125 that have already been marked. */
11126 loc->condition_changed = condition_updated;
11127
11128 /* Free the agent expression bytecode as well. We will compute
11129 it later on. */
833177a4 11130 loc->cond_bytecode.reset ();
b775012e
LM
11131 }
11132}
7f32a4d5 11133
44702360
PA
11134/* Called whether new breakpoints are created, or existing breakpoints
11135 deleted, to update the global location list and recompute which
11136 locations are duplicate of which.
b775012e 11137
04086b45
PA
11138 The INSERT_MODE flag determines whether locations may not, may, or
11139 shall be inserted now. See 'enum ugll_insert_mode' for more
11140 info. */
b60e7edf 11141
0d381245 11142static void
44702360 11143update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11144{
b775012e
LM
11145 /* Last breakpoint location address that was marked for update. */
11146 CORE_ADDR last_addr = 0;
11147 /* Last breakpoint location program space that was marked for update. */
11148 int last_pspace_num = -1;
f7545552 11149
2d134ed3
PA
11150 /* Used in the duplicates detection below. When iterating over all
11151 bp_locations, points to the first bp_location of a given address.
11152 Breakpoints and watchpoints of different types are never
11153 duplicates of each other. Keep one pointer for each type of
11154 breakpoint/watchpoint, so we only need to loop over all locations
11155 once. */
11156 struct bp_location *bp_loc_first; /* breakpoint */
11157 struct bp_location *wp_loc_first; /* hardware watchpoint */
11158 struct bp_location *awp_loc_first; /* access watchpoint */
11159 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11160
f5336ca5
PA
11161 /* Saved former bp_locations array which we compare against the newly
11162 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11163 std::vector<bp_location *> old_locations = std::move (bp_locations);
11164 bp_locations.clear ();
876fa593 11165
43892fdf 11166 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11167 for (bp_location *loc : b->locations ())
5d51cd5d 11168 bp_locations.push_back (loc);
7f32a4d5
PA
11169
11170 /* See if we need to "upgrade" a software breakpoint to a hardware
11171 breakpoint. Do this before deciding whether locations are
11172 duplicates. Also do this before sorting because sorting order
11173 depends on location type. */
5d51cd5d
SM
11174 for (bp_location *loc : bp_locations)
11175 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11176 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11177
5d51cd5d 11178 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11179 bp_location_is_less_than);
876fa593 11180
f5336ca5 11181 bp_locations_target_extensions_update ();
74960c60 11182
4a64f543
MS
11183 /* Identify bp_location instances that are no longer present in the
11184 new list, and therefore should be freed. Note that it's not
11185 necessary that those locations should be removed from inferior --
11186 if there's another location at the same address (previously
11187 marked as duplicate), we don't need to remove/insert the
11188 location.
876fa593 11189
4a64f543
MS
11190 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11191 and former bp_location array state respectively. */
876fa593 11192
5d51cd5d
SM
11193 size_t loc_i = 0;
11194 for (bp_location *old_loc : old_locations)
74960c60 11195 {
e5dd4106 11196 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11197 not, we have to free it. */
d8de7963 11198 bool found_object = false;
20874c92 11199 /* Tells if the location should remain inserted in the target. */
d8de7963
AB
11200 bool keep_in_target = false;
11201 bool removed = false;
876fa593 11202
4a64f543
MS
11203 /* Skip LOCP entries which will definitely never be needed.
11204 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11205 while (loc_i < bp_locations.size ()
11206 && bp_locations[loc_i]->address < old_loc->address)
11207 loc_i++;
c7d46a38 11208
5d51cd5d
SM
11209 for (size_t loc2_i = loc_i;
11210 (loc2_i < bp_locations.size ()
11211 && bp_locations[loc2_i]->address == old_loc->address);
11212 loc2_i++)
c7d46a38 11213 {
b775012e
LM
11214 /* Check if this is a new/duplicated location or a duplicated
11215 location that had its condition modified. If so, we want to send
11216 its condition to the target if evaluation of conditions is taking
11217 place there. */
5d51cd5d 11218 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11219 && (last_addr != old_loc->address
11220 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11221 {
5d51cd5d 11222 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11223 last_pspace_num = old_loc->pspace->num;
c7d46a38 11224 }
b775012e 11225
5d51cd5d 11226 if (bp_locations[loc2_i] == old_loc)
d8de7963 11227 found_object = true;
c7d46a38 11228 }
74960c60 11229
b775012e
LM
11230 /* We have already handled this address, update it so that we don't
11231 have to go through updates again. */
11232 last_addr = old_loc->address;
11233
11234 /* Target-side condition evaluation: Handle deleted locations. */
11235 if (!found_object)
11236 force_breakpoint_reinsertion (old_loc);
11237
4a64f543
MS
11238 /* If this location is no longer present, and inserted, look if
11239 there's maybe a new location at the same address. If so,
11240 mark that one inserted, and don't remove this one. This is
11241 needed so that we don't have a time window where a breakpoint
11242 at certain location is not inserted. */
74960c60 11243
876fa593 11244 if (old_loc->inserted)
0d381245 11245 {
4a64f543
MS
11246 /* If the location is inserted now, we might have to remove
11247 it. */
74960c60 11248
876fa593 11249 if (found_object && should_be_inserted (old_loc))
74960c60 11250 {
4a64f543
MS
11251 /* The location is still present in the location list,
11252 and still should be inserted. Don't do anything. */
d8de7963 11253 keep_in_target = true;
74960c60
VP
11254 }
11255 else
11256 {
b775012e
LM
11257 /* This location still exists, but it won't be kept in the
11258 target since it may have been disabled. We proceed to
11259 remove its target-side condition. */
11260
4a64f543
MS
11261 /* The location is either no longer present, or got
11262 disabled. See if there's another location at the
11263 same address, in which case we don't need to remove
11264 this one from the target. */
876fa593 11265
2bdf28a0 11266 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11267 if (bl_address_is_meaningful (old_loc))
876fa593 11268 {
5d51cd5d
SM
11269 for (size_t loc2_i = loc_i;
11270 (loc2_i < bp_locations.size ()
11271 && bp_locations[loc2_i]->address == old_loc->address);
11272 loc2_i++)
876fa593 11273 {
5d51cd5d 11274 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11275
7f32a4d5
PA
11276 if (loc2 == old_loc)
11277 continue;
11278
2d134ed3 11279 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11280 {
85d721b8
PA
11281 /* Read watchpoint locations are switched to
11282 access watchpoints, if the former are not
11283 supported, but the latter are. */
11284 if (is_hardware_watchpoint (old_loc->owner))
11285 {
11286 gdb_assert (is_hardware_watchpoint (loc2->owner));
11287 loc2->watchpoint_type = old_loc->watchpoint_type;
11288 }
11289
934709f0
PW
11290 /* loc2 is a duplicated location. We need to check
11291 if it should be inserted in case it will be
11292 unduplicated. */
7f32a4d5 11293 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11294 {
934709f0 11295 swap_insertion (old_loc, loc2);
d8de7963 11296 keep_in_target = true;
c7d46a38
PA
11297 break;
11298 }
876fa593
JK
11299 }
11300 }
11301 }
74960c60
VP
11302 }
11303
20874c92
VP
11304 if (!keep_in_target)
11305 {
834c0d03 11306 if (remove_breakpoint (old_loc))
20874c92 11307 {
4a64f543
MS
11308 /* This is just about all we can do. We could keep
11309 this location on the global list, and try to
11310 remove it next time, but there's no particular
11311 reason why we will succeed next time.
20874c92 11312
4a64f543
MS
11313 Note that at this point, old_loc->owner is still
11314 valid, as delete_breakpoint frees the breakpoint
11315 only after calling us. */
6cb06a8c
TT
11316 gdb_printf (_("warning: Error removing "
11317 "breakpoint %d\n"),
11318 old_loc->owner->number);
20874c92 11319 }
d8de7963 11320 removed = true;
20874c92 11321 }
0d381245 11322 }
74960c60
VP
11323
11324 if (!found_object)
1c5cfe86 11325 {
fbea99ea 11326 if (removed && target_is_non_stop_p ()
1cf4d951 11327 && need_moribund_for_location_type (old_loc))
20874c92 11328 {
db82e815
PA
11329 /* This location was removed from the target. In
11330 non-stop mode, a race condition is possible where
11331 we've removed a breakpoint, but stop events for that
11332 breakpoint are already queued and will arrive later.
11333 We apply an heuristic to be able to distinguish such
11334 SIGTRAPs from other random SIGTRAPs: we keep this
11335 breakpoint location for a bit, and will retire it
11336 after we see some number of events. The theory here
11337 is that reporting of events should, "on the average",
11338 be fair, so after a while we'll see events from all
11339 threads that have anything of interest, and no longer
11340 need to keep this breakpoint location around. We
11341 don't hold locations forever so to reduce chances of
11342 mistaking a non-breakpoint SIGTRAP for a breakpoint
11343 SIGTRAP.
11344
11345 The heuristic failing can be disastrous on
11346 decr_pc_after_break targets.
11347
11348 On decr_pc_after_break targets, like e.g., x86-linux,
11349 if we fail to recognize a late breakpoint SIGTRAP,
11350 because events_till_retirement has reached 0 too
11351 soon, we'll fail to do the PC adjustment, and report
11352 a random SIGTRAP to the user. When the user resumes
11353 the inferior, it will most likely immediately crash
2dec564e 11354 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11355 corrupted, because of being resumed e.g., in the
11356 middle of a multi-byte instruction, or skipped a
11357 one-byte instruction. This was actually seen happen
11358 on native x86-linux, and should be less rare on
11359 targets that do not support new thread events, like
11360 remote, due to the heuristic depending on
11361 thread_count.
11362
11363 Mistaking a random SIGTRAP for a breakpoint trap
11364 causes similar symptoms (PC adjustment applied when
11365 it shouldn't), but then again, playing with SIGTRAPs
11366 behind the debugger's back is asking for trouble.
11367
11368 Since hardware watchpoint traps are always
11369 distinguishable from other traps, so we don't need to
11370 apply keep hardware watchpoint moribund locations
11371 around. We simply always ignore hardware watchpoint
11372 traps we can no longer explain. */
11373
5b6d1e4f
PA
11374 process_stratum_target *proc_target = nullptr;
11375 for (inferior *inf : all_inferiors ())
11376 if (inf->pspace == old_loc->pspace)
11377 {
11378 proc_target = inf->process_target ();
11379 break;
11380 }
11381 if (proc_target != nullptr)
11382 old_loc->events_till_retirement
11383 = 3 * (thread_count (proc_target) + 1);
11384 else
11385 old_loc->events_till_retirement = 1;
876fa593 11386 old_loc->owner = NULL;
20874c92 11387
1123588c 11388 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11389 }
11390 else
f431efe5
PA
11391 {
11392 old_loc->owner = NULL;
11393 decref_bp_location (&old_loc);
11394 }
20874c92 11395 }
74960c60 11396 }
1c5cfe86 11397
348d480f
PA
11398 /* Rescan breakpoints at the same address and section, marking the
11399 first one as "first" and any others as "duplicates". This is so
11400 that the bpt instruction is only inserted once. If we have a
11401 permanent breakpoint at the same place as BPT, make that one the
11402 official one, and the rest as duplicates. Permanent breakpoints
11403 are sorted first for the same address.
11404
11405 Do the same for hardware watchpoints, but also considering the
11406 watchpoint's type (regular/access/read) and length. */
11407
11408 bp_loc_first = NULL;
11409 wp_loc_first = NULL;
11410 awp_loc_first = NULL;
11411 rwp_loc_first = NULL;
40cb8ca5 11412
48d7020b 11413 for (bp_location *loc : all_bp_locations ())
348d480f
PA
11414 {
11415 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11416 non-NULL. */
348d480f 11417 struct bp_location **loc_first_p;
43892fdf 11418 breakpoint *b = loc->owner;
348d480f 11419
6f380991 11420 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11421 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11422 /* Don't detect duplicate for tracepoint locations because they are
11423 never duplicated. See the comments in field `duplicate' of
11424 `struct bp_location'. */
348d480f 11425 || is_tracepoint (b))
b775012e
LM
11426 {
11427 /* Clear the condition modification flag. */
11428 loc->condition_changed = condition_unchanged;
11429 continue;
11430 }
348d480f 11431
348d480f
PA
11432 if (b->type == bp_hardware_watchpoint)
11433 loc_first_p = &wp_loc_first;
11434 else if (b->type == bp_read_watchpoint)
11435 loc_first_p = &rwp_loc_first;
11436 else if (b->type == bp_access_watchpoint)
11437 loc_first_p = &awp_loc_first;
11438 else
11439 loc_first_p = &bp_loc_first;
11440
11441 if (*loc_first_p == NULL
11442 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11443 || !breakpoint_locations_match (loc, *loc_first_p))
11444 {
11445 *loc_first_p = loc;
11446 loc->duplicate = 0;
b775012e
LM
11447
11448 if (is_breakpoint (loc->owner) && loc->condition_changed)
11449 {
11450 loc->needs_update = 1;
11451 /* Clear the condition modification flag. */
11452 loc->condition_changed = condition_unchanged;
11453 }
348d480f
PA
11454 continue;
11455 }
11456
934709f0
PW
11457
11458 /* This and the above ensure the invariant that the first location
11459 is not duplicated, and is the inserted one.
11460 All following are marked as duplicated, and are not inserted. */
11461 if (loc->inserted)
11462 swap_insertion (loc, *loc_first_p);
348d480f
PA
11463 loc->duplicate = 1;
11464
b775012e
LM
11465 /* Clear the condition modification flag. */
11466 loc->condition_changed = condition_unchanged;
348d480f
PA
11467 }
11468
a25a5a45 11469 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11470 {
04086b45 11471 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11472 insert_breakpoint_locations ();
11473 else
11474 {
44702360
PA
11475 /* Even though the caller told us to not insert new
11476 locations, we may still need to update conditions on the
11477 target's side of breakpoints that were already inserted
11478 if the target is evaluating breakpoint conditions. We
b775012e
LM
11479 only update conditions for locations that are marked
11480 "needs_update". */
11481 update_inserted_breakpoint_locations ();
11482 }
11483 }
348d480f 11484
04086b45 11485 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 11486 download_tracepoint_locations ();
348d480f
PA
11487}
11488
11489void
11490breakpoint_retire_moribund (void)
11491{
1123588c
TT
11492 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11493 {
11494 struct bp_location *loc = moribund_locations[ix];
11495 if (--(loc->events_till_retirement) == 0)
11496 {
11497 decref_bp_location (&loc);
11498 unordered_remove (moribund_locations, ix);
11499 --ix;
11500 }
11501 }
348d480f
PA
11502}
11503
11504static void
44702360 11505update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 11506{
348d480f 11507
a70b8144 11508 try
492d29ea
PA
11509 {
11510 update_global_location_list (insert_mode);
11511 }
230d2906 11512 catch (const gdb_exception_error &e)
492d29ea
PA
11513 {
11514 }
348d480f
PA
11515}
11516
11517/* Clear BKP from a BPS. */
11518
11519static void
313f3b21 11520bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
348d480f 11521{
313f3b21 11522 bpstat *bs;
348d480f
PA
11523
11524 for (bs = bps; bs; bs = bs->next)
11525 if (bs->breakpoint_at == bpt)
11526 {
11527 bs->breakpoint_at = NULL;
11528 bs->old_val = NULL;
11529 /* bs->commands will be freed later. */
11530 }
11531}
11532
11533/* Callback for iterate_over_threads. */
11534static int
11535bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11536{
9a3c8263 11537 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
11538
11539 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11540 return 0;
11541}
11542
7987c463 11543/* See breakpoint.h. */
348d480f 11544
7987c463
TT
11545void
11546code_breakpoint::say_where () const
348d480f
PA
11547{
11548 struct value_print_options opts;
11549
11550 get_user_print_options (&opts);
11551
11552 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11553 single string. */
7987c463 11554 if (loc == NULL)
348d480f 11555 {
f00aae0f 11556 /* For pending locations, the output differs slightly based
7987c463 11557 on extra_string. If this is non-NULL, it contains either
f00aae0f 11558 a condition or dprintf arguments. */
7987c463 11559 if (extra_string == NULL)
f00aae0f 11560 {
7987c463 11561 gdb_printf (_(" (%s) pending."), locspec->to_string ());
f00aae0f 11562 }
7987c463 11563 else if (type == bp_dprintf)
f00aae0f 11564 {
6cb06a8c 11565 gdb_printf (_(" (%s,%s) pending."),
7987c463
TT
11566 locspec->to_string (),
11567 extra_string.get ());
f00aae0f
KS
11568 }
11569 else
11570 {
6cb06a8c 11571 gdb_printf (_(" (%s %s) pending."),
7987c463
TT
11572 locspec->to_string (),
11573 extra_string.get ());
f00aae0f 11574 }
348d480f
PA
11575 }
11576 else
11577 {
7987c463 11578 if (opts.addressprint || loc->symtab == NULL)
6cb06a8c
TT
11579 gdb_printf (" at %ps",
11580 styled_string (address_style.style (),
7987c463
TT
11581 paddress (loc->gdbarch,
11582 loc->address)));
11583 if (loc->symtab != NULL)
f8eba3c6
TT
11584 {
11585 /* If there is a single location, we can print the location
11586 more nicely. */
7987c463 11587 if (loc->next == NULL)
0bb296cb 11588 {
6a831f06 11589 const char *filename
7987c463 11590 = symtab_to_filename_for_display (loc->symtab);
6cb06a8c
TT
11591 gdb_printf (": file %ps, line %d.",
11592 styled_string (file_name_style.style (),
11593 filename),
7987c463 11594 loc->line_number);
0bb296cb 11595 }
f8eba3c6
TT
11596 else
11597 /* This is not ideal, but each location may have a
11598 different file name, and this at least reflects the
11599 real situation somewhat. */
7987c463 11600 gdb_printf (": %s.", locspec->to_string ());
f8eba3c6 11601 }
348d480f 11602
7987c463 11603 if (loc->next)
348d480f 11604 {
066620dc 11605 struct bp_location *iter = loc;
348d480f 11606 int n = 0;
066620dc 11607 for (; iter; iter = iter->next)
348d480f 11608 ++n;
6cb06a8c 11609 gdb_printf (" (%d locations)", n);
348d480f
PA
11610 }
11611 }
11612}
11613
40cb8ca5
SM
11614/* See breakpoint.h. */
11615
a67bcaba 11616bp_location_range breakpoint::locations () const
40cb8ca5 11617{
9be25986 11618 return bp_location_range (this->loc);
40cb8ca5
SM
11619}
11620
4c6a92b1
TT
11621struct bp_location *
11622breakpoint::allocate_location ()
11623{
11624 return new bp_location (this);
11625}
11626
2060206e
PA
11627#define internal_error_pure_virtual_called() \
11628 gdb_assert_not_reached ("pure virtual function called")
11629
4c6a92b1
TT
11630int
11631breakpoint::insert_location (struct bp_location *bl)
11632{
11633 internal_error_pure_virtual_called ();
11634}
11635
4c6a92b1
TT
11636int
11637breakpoint::remove_location (struct bp_location *bl,
11638 enum remove_bp_reason reason)
11639{
11640 internal_error_pure_virtual_called ();
11641}
11642
4c6a92b1
TT
11643int
11644breakpoint::breakpoint_hit (const struct bp_location *bl,
11645 const address_space *aspace,
11646 CORE_ADDR bp_addr,
11647 const target_waitstatus &ws)
11648{
11649 internal_error_pure_virtual_called ();
11650}
11651
4c6a92b1
TT
11652int
11653breakpoint::resources_needed (const struct bp_location *bl)
11654{
11655 internal_error_pure_virtual_called ();
11656}
11657
4c6a92b1 11658enum print_stop_action
7bd86313 11659breakpoint::print_it (const bpstat *bs) const
4c6a92b1
TT
11660{
11661 internal_error_pure_virtual_called ();
11662}
11663
4c6a92b1 11664void
b713485d 11665breakpoint::print_mention () const
4c6a92b1
TT
11666{
11667 internal_error_pure_virtual_called ();
11668}
11669
4c6a92b1 11670void
4d1ae558 11671breakpoint::print_recreate (struct ui_file *fp) const
4c6a92b1
TT
11672{
11673 internal_error_pure_virtual_called ();
11674}
11675
2060206e
PA
11676/* Default breakpoint_ops methods. */
11677
2b5ab5b8 11678void
74421c0b 11679code_breakpoint::re_set ()
348d480f 11680{
06edf0c0 11681 /* FIXME: is this still reachable? */
264f9890 11682 if (breakpoint_location_spec_empty_p (this))
06edf0c0 11683 {
f00aae0f 11684 /* Anything without a location can't be re-set. */
2b5ab5b8 11685 delete_breakpoint (this);
06edf0c0 11686 return;
348d480f 11687 }
06edf0c0 11688
b3d5660a 11689 re_set_default ();
348d480f
PA
11690}
11691
2b5ab5b8 11692int
74421c0b 11693code_breakpoint::insert_location (struct bp_location *bl)
348d480f 11694{
cd6c3b4f
YQ
11695 CORE_ADDR addr = bl->target_info.reqstd_address;
11696
579c6ad9 11697 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
11698 bl->target_info.placed_address = addr;
11699
5d926615 11700 int result;
348d480f 11701 if (bl->loc_type == bp_loc_hardware_breakpoint)
5d926615 11702 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 11703 else
5d926615
TT
11704 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11705
11706 if (result == 0 && bl->probe.prob != nullptr)
11707 {
11708 /* The insertion was successful, now let's set the probe's semaphore
11709 if needed. */
11710 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11711 }
11712
11713 return result;
348d480f
PA
11714}
11715
2b5ab5b8 11716int
74421c0b 11717code_breakpoint::remove_location (struct bp_location *bl,
2b5ab5b8 11718 enum remove_bp_reason reason)
348d480f 11719{
5d926615
TT
11720 if (bl->probe.prob != nullptr)
11721 {
11722 /* Let's clear the semaphore before removing the location. */
11723 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11724 }
11725
348d480f
PA
11726 if (bl->loc_type == bp_loc_hardware_breakpoint)
11727 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11728 else
73971819 11729 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
11730}
11731
2b5ab5b8 11732int
74421c0b 11733code_breakpoint::breakpoint_hit (const struct bp_location *bl,
2b5ab5b8
TT
11734 const address_space *aspace,
11735 CORE_ADDR bp_addr,
11736 const target_waitstatus &ws)
348d480f 11737{
c272a98c
SM
11738 if (ws.kind () != TARGET_WAITKIND_STOPPED
11739 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
11740 return 0;
11741
348d480f
PA
11742 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11743 aspace, bp_addr))
11744 return 0;
11745
11746 if (overlay_debugging /* unmapped overlay section */
11747 && section_is_overlay (bl->section)
11748 && !section_is_mapped (bl->section))
11749 return 0;
11750
11751 return 1;
11752}
11753
f293a0b5
TT
11754int
11755dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11756 const address_space *aspace,
11757 CORE_ADDR bp_addr,
11758 const target_waitstatus &ws)
cd1608cc
PA
11759{
11760 if (dprintf_style == dprintf_style_agent
11761 && target_can_run_breakpoint_commands ())
11762 {
11763 /* An agent-style dprintf never causes a stop. If we see a trap
11764 for this address it must be for a breakpoint that happens to
11765 be set at the same address. */
11766 return 0;
11767 }
11768
f293a0b5 11769 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
cd1608cc
PA
11770}
11771
7dd8e7ae
TT
11772int
11773ordinary_breakpoint::resources_needed (const struct bp_location *bl)
348d480f 11774{
7dd8e7ae 11775 gdb_assert (type == bp_hardware_breakpoint);
348d480f
PA
11776
11777 return 1;
11778}
11779
7dd8e7ae 11780enum print_stop_action
7bd86313 11781ordinary_breakpoint::print_it (const bpstat *bs) const
348d480f 11782{
348d480f 11783 const struct bp_location *bl;
001c8c33 11784 int bp_temp;
79a45e25 11785 struct ui_out *uiout = current_uiout;
348d480f 11786
b6433ede 11787 bl = bs->bp_location_at.get ();
348d480f 11788
7dd8e7ae 11789 bp_temp = disposition == disp_del;
001c8c33
PA
11790 if (bl->address != bl->requested_address)
11791 breakpoint_adjustment_warning (bl->requested_address,
11792 bl->address,
d8de7963 11793 number, true);
7dd8e7ae 11794 annotate_breakpoint (number);
f303dbd6
PA
11795 maybe_print_thread_hit_breakpoint (uiout);
11796
112e8700 11797 if (uiout->is_mi_like_p ())
348d480f 11798 {
112e8700 11799 uiout->field_string ("reason",
001c8c33 11800 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7dd8e7ae 11801 uiout->field_string ("disp", bpdisp_text (disposition));
06edf0c0 11802 }
78805ff8 11803
6a831f06 11804 if (bp_temp)
78805ff8 11805 uiout->text ("Temporary breakpoint ");
6a831f06 11806 else
78805ff8
PW
11807 uiout->text ("Breakpoint ");
11808 print_num_locno (bs, uiout);
11809 uiout->text (", ");
06edf0c0 11810
001c8c33 11811 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11812}
11813
7dd8e7ae 11814void
b713485d 11815ordinary_breakpoint::print_mention () const
06edf0c0 11816{
112e8700 11817 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
11818 return;
11819
7dd8e7ae 11820 switch (type)
06edf0c0
PA
11821 {
11822 case bp_breakpoint:
11823 case bp_gnu_ifunc_resolver:
7dd8e7ae 11824 if (disposition == disp_del)
6cb06a8c 11825 gdb_printf (_("Temporary breakpoint"));
06edf0c0 11826 else
6cb06a8c 11827 gdb_printf (_("Breakpoint"));
7dd8e7ae
TT
11828 gdb_printf (_(" %d"), number);
11829 if (type == bp_gnu_ifunc_resolver)
6cb06a8c 11830 gdb_printf (_(" at gnu-indirect-function resolver"));
06edf0c0
PA
11831 break;
11832 case bp_hardware_breakpoint:
7dd8e7ae 11833 gdb_printf (_("Hardware assisted breakpoint %d"), number);
06edf0c0 11834 break;
e7e0cddf 11835 case bp_dprintf:
7dd8e7ae 11836 gdb_printf (_("Dprintf %d"), number);
e7e0cddf 11837 break;
06edf0c0
PA
11838 }
11839
7987c463 11840 say_where ();
06edf0c0
PA
11841}
11842
7dd8e7ae 11843void
4d1ae558 11844ordinary_breakpoint::print_recreate (struct ui_file *fp) const
06edf0c0 11845{
7dd8e7ae 11846 if (type == bp_breakpoint && disposition == disp_del)
6cb06a8c 11847 gdb_printf (fp, "tbreak");
7dd8e7ae 11848 else if (type == bp_breakpoint)
6cb06a8c 11849 gdb_printf (fp, "break");
7dd8e7ae
TT
11850 else if (type == bp_hardware_breakpoint
11851 && disposition == disp_del)
6cb06a8c 11852 gdb_printf (fp, "thbreak");
7dd8e7ae 11853 else if (type == bp_hardware_breakpoint)
6cb06a8c 11854 gdb_printf (fp, "hbreak");
06edf0c0 11855 else
f34652de 11856 internal_error (_("unhandled breakpoint type %d"), (int) type);
06edf0c0 11857
709438c7 11858 gdb_printf (fp, " %s", locspec->to_string ());
f00aae0f
KS
11859
11860 /* Print out extra_string if this breakpoint is pending. It might
11861 contain, for example, conditions that were set by the user. */
7dd8e7ae
TT
11862 if (loc == NULL && extra_string != NULL)
11863 gdb_printf (fp, " %s", extra_string.get ());
f00aae0f 11864
04d0163c 11865 print_recreate_thread (fp);
06edf0c0
PA
11866}
11867
2b5ab5b8 11868std::vector<symtab_and_line>
264f9890
PA
11869code_breakpoint::decode_location_spec (location_spec *locspec,
11870 program_space *search_pspace)
983af33b 11871{
7464aeaa 11872 if (locspec->type () == PROBE_LOCATION_SPEC)
264f9890 11873 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
5d926615 11874
8f5bc641
TT
11875 struct linespec_result canonical;
11876
11877 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
11878 NULL, 0, &canonical, multiple_symbols_all,
11879 filter.get ());
11880
11881 /* We should get 0 or 1 resulting SALs. */
11882 gdb_assert (canonical.lsals.size () < 2);
11883
11884 if (!canonical.lsals.empty ())
11885 {
11886 const linespec_sals &lsal = canonical.lsals[0];
11887 return std::move (lsal.sals);
11888 }
11889 return {};
983af33b
SDJ
11890}
11891
06edf0c0
PA
11892/* Virtual table for internal breakpoints. */
11893
c359fff5
TT
11894void
11895internal_breakpoint::re_set ()
06edf0c0 11896{
c359fff5 11897 switch (type)
06edf0c0
PA
11898 {
11899 /* Delete overlay event and longjmp master breakpoints; they
11900 will be reset later by breakpoint_re_set. */
11901 case bp_overlay_event:
11902 case bp_longjmp_master:
11903 case bp_std_terminate_master:
11904 case bp_exception_master:
c359fff5 11905 delete_breakpoint (this);
06edf0c0
PA
11906 break;
11907
11908 /* This breakpoint is special, it's set up when the inferior
dda83cd7 11909 starts and we really don't want to touch it. */
06edf0c0
PA
11910 case bp_shlib_event:
11911
11912 /* Like bp_shlib_event, this breakpoint type is special. Once
11913 it is set up, we do not want to touch it. */
11914 case bp_thread_event:
11915 break;
11916 }
11917}
11918
c359fff5
TT
11919void
11920internal_breakpoint::check_status (bpstat *bs)
06edf0c0 11921{
c359fff5 11922 if (type == bp_shlib_event)
a9b3a50f
PA
11923 {
11924 /* If requested, stop when the dynamic linker notifies GDB of
11925 events. This allows the user to get control and place
11926 breakpoints in initializer routines for dynamically loaded
11927 objects (among other things). */
4ec2227a
TT
11928 bs->stop = stop_on_solib_events != 0;
11929 bs->print = stop_on_solib_events != 0;
a9b3a50f
PA
11930 }
11931 else
4ec2227a 11932 bs->stop = false;
06edf0c0
PA
11933}
11934
c359fff5 11935enum print_stop_action
7bd86313 11936internal_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11937{
c359fff5 11938 switch (type)
06edf0c0 11939 {
348d480f
PA
11940 case bp_shlib_event:
11941 /* Did we stop because the user set the stop_on_solib_events
11942 variable? (If so, we report this as a generic, "Stopped due
11943 to shlib event" message.) */
4e9e993a 11944 print_solib_event (false);
348d480f
PA
11945 break;
11946
11947 case bp_thread_event:
11948 /* Not sure how we will get here.
11949 GDB should not stop for these breakpoints. */
6cb06a8c 11950 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11951 break;
11952
11953 case bp_overlay_event:
11954 /* By analogy with the thread event, GDB should not stop for these. */
6cb06a8c 11955 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11956 break;
11957
11958 case bp_longjmp_master:
11959 /* These should never be enabled. */
6cb06a8c 11960 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11961 break;
11962
11963 case bp_std_terminate_master:
11964 /* These should never be enabled. */
6cb06a8c
TT
11965 gdb_printf (_("std::terminate Master Breakpoint: "
11966 "gdb should not stop!\n"));
348d480f
PA
11967 break;
11968
11969 case bp_exception_master:
11970 /* These should never be enabled. */
6cb06a8c
TT
11971 gdb_printf (_("Exception Master Breakpoint: "
11972 "gdb should not stop!\n"));
06edf0c0
PA
11973 break;
11974 }
11975
001c8c33 11976 return PRINT_NOTHING;
06edf0c0
PA
11977}
11978
c359fff5 11979void
b713485d 11980internal_breakpoint::print_mention () const
06edf0c0
PA
11981{
11982 /* Nothing to mention. These breakpoints are internal. */
11983}
11984
06edf0c0
PA
11985/* Virtual table for momentary breakpoints */
11986
1fd30a47
TT
11987void
11988momentary_breakpoint::re_set ()
06edf0c0
PA
11989{
11990 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 11991 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
11992 Otherwise these should have been blown away via the cleanup chain
11993 or by breakpoint_init_inferior when we rerun the executable. */
11994}
11995
1fd30a47
TT
11996void
11997momentary_breakpoint::check_status (bpstat *bs)
06edf0c0
PA
11998{
11999 /* Nothing. The point of these breakpoints is causing a stop. */
12000}
12001
1fd30a47 12002enum print_stop_action
7bd86313 12003momentary_breakpoint::print_it (const bpstat *bs) const
06edf0c0 12004{
001c8c33 12005 return PRINT_UNKNOWN;
348d480f
PA
12006}
12007
1fd30a47 12008void
b713485d 12009momentary_breakpoint::print_mention () const
348d480f 12010{
06edf0c0 12011 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12012}
12013
e2e4d78b
JK
12014/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12015
12016 It gets cleared already on the removal of the first one of such placed
12017 breakpoints. This is OK as they get all removed altogether. */
12018
c1fc2657 12019longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12020{
c1fc2657 12021 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12022
c1fc2657 12023 if (tp != NULL)
e2e4d78b 12024 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12025}
12026
55aa24fb 12027static void
264f9890
PA
12028bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12029 struct linespec_result *canonical)
03ada39e 12030
55aa24fb
SDJ
12031{
12032 struct linespec_sals lsal;
12033
264f9890 12034 lsal.sals = parse_probes (locspec, NULL, canonical);
709438c7 12035 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12036 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12037}
12038
6c5b2ebe 12039static std::vector<symtab_and_line>
264f9890
PA
12040bkpt_probe_decode_location_spec (struct breakpoint *b,
12041 location_spec *locspec,
12042 program_space *search_pspace)
55aa24fb 12043{
264f9890
PA
12044 std::vector<symtab_and_line> sals
12045 = parse_probes (locspec, search_pspace, NULL);
6c5b2ebe 12046 if (sals.empty ())
55aa24fb 12047 error (_("probe not found"));
6c5b2ebe 12048 return sals;
55aa24fb
SDJ
12049}
12050
779dcceb
TT
12051int
12052tracepoint::breakpoint_hit (const struct bp_location *bl,
12053 const address_space *aspace, CORE_ADDR bp_addr,
12054 const target_waitstatus &ws)
348d480f
PA
12055{
12056 /* By definition, the inferior does not report stops at
12057 tracepoints. */
12058 return 0;
74960c60
VP
12059}
12060
779dcceb
TT
12061void
12062tracepoint::print_one_detail (struct ui_out *uiout) const
74960c60 12063{
779dcceb 12064 if (!static_trace_marker_id.empty ())
348d480f 12065 {
7b572efb
TT
12066 gdb_assert (type == bp_static_tracepoint
12067 || type == bp_static_marker_tracepoint);
cc59ec59 12068
6a831f06
PA
12069 uiout->message ("\tmarker id is %pF\n",
12070 string_field ("static-tracepoint-marker-string-id",
779dcceb 12071 static_trace_marker_id.c_str ()));
348d480f 12072 }
0d381245
VP
12073}
12074
779dcceb 12075void
b713485d 12076tracepoint::print_mention () const
a474d7c2 12077{
112e8700 12078 if (current_uiout->is_mi_like_p ())
348d480f 12079 return;
cc59ec59 12080
779dcceb 12081 switch (type)
348d480f
PA
12082 {
12083 case bp_tracepoint:
6cb06a8c 12084 gdb_printf (_("Tracepoint"));
779dcceb 12085 gdb_printf (_(" %d"), number);
348d480f
PA
12086 break;
12087 case bp_fast_tracepoint:
6cb06a8c 12088 gdb_printf (_("Fast tracepoint"));
779dcceb 12089 gdb_printf (_(" %d"), number);
348d480f
PA
12090 break;
12091 case bp_static_tracepoint:
7b572efb 12092 case bp_static_marker_tracepoint:
6cb06a8c 12093 gdb_printf (_("Static tracepoint"));
779dcceb 12094 gdb_printf (_(" %d"), number);
348d480f
PA
12095 break;
12096 default:
f34652de 12097 internal_error (_("unhandled tracepoint type %d"), (int) type);
348d480f
PA
12098 }
12099
7987c463 12100 say_where ();
a474d7c2
PA
12101}
12102
779dcceb 12103void
4d1ae558 12104tracepoint::print_recreate (struct ui_file *fp) const
a474d7c2 12105{
779dcceb 12106 if (type == bp_fast_tracepoint)
6cb06a8c 12107 gdb_printf (fp, "ftrace");
7b572efb
TT
12108 else if (type == bp_static_tracepoint
12109 || type == bp_static_marker_tracepoint)
6cb06a8c 12110 gdb_printf (fp, "strace");
779dcceb 12111 else if (type == bp_tracepoint)
6cb06a8c 12112 gdb_printf (fp, "trace");
348d480f 12113 else
f34652de 12114 internal_error (_("unhandled tracepoint type %d"), (int) type);
cc59ec59 12115
709438c7 12116 gdb_printf (fp, " %s", locspec->to_string ());
04d0163c 12117 print_recreate_thread (fp);
d9b3f62e 12118
779dcceb
TT
12119 if (pass_count)
12120 gdb_printf (fp, " passcount %d\n", pass_count);
a474d7c2
PA
12121}
12122
bac7c5cf 12123/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12124
12125static void
264f9890
PA
12126tracepoint_probe_create_sals_from_location_spec
12127 (location_spec *locspec,
03ada39e 12128 struct linespec_result *canonical)
55aa24fb
SDJ
12129{
12130 /* We use the same method for breakpoint on probes. */
264f9890 12131 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
55aa24fb
SDJ
12132}
12133
f293a0b5
TT
12134void
12135dprintf_breakpoint::re_set ()
5c2b4418 12136{
b3d5660a 12137 re_set_default ();
5c2b4418 12138
f00aae0f 12139 /* extra_string should never be non-NULL for dprintf. */
f293a0b5 12140 gdb_assert (extra_string != NULL);
5c2b4418
HZ
12141
12142 /* 1 - connect to target 1, that can run breakpoint commands.
12143 2 - create a dprintf, which resolves fine.
12144 3 - disconnect from target 1
12145 4 - connect to target 2, that can NOT run breakpoint commands.
12146
12147 After steps #3/#4, you'll want the dprintf command list to
12148 be updated, because target 1 and 2 may well return different
12149 answers for target_can_run_breakpoint_commands().
12150 Given absence of finer grained resetting, we get to do
12151 it all the time. */
f293a0b5
TT
12152 if (extra_string != NULL)
12153 update_dprintf_command_list (this);
5c2b4418
HZ
12154}
12155
f293a0b5 12156/* Implement the "print_recreate" method for dprintf. */
2d9442cc 12157
f293a0b5 12158void
4d1ae558 12159dprintf_breakpoint::print_recreate (struct ui_file *fp) const
2d9442cc 12160{
709438c7 12161 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
04d0163c 12162 print_recreate_thread (fp);
2d9442cc
HZ
12163}
12164
f293a0b5 12165/* Implement the "after_condition_true" method for dprintf.
9d6e6e84
HZ
12166
12167 dprintf's are implemented with regular commands in their command
12168 list, but we run the commands here instead of before presenting the
12169 stop to the user, as dprintf's don't actually cause a stop. This
12170 also makes it so that the commands of multiple dprintfs at the same
12171 address are all handled. */
12172
f293a0b5
TT
12173void
12174dprintf_breakpoint::after_condition_true (struct bpstat *bs)
9d6e6e84 12175{
9d6e6e84
HZ
12176 /* dprintf's never cause a stop. This wasn't set in the
12177 check_status hook instead because that would make the dprintf's
12178 condition not be evaluated. */
4ec2227a 12179 bs->stop = false;
9d6e6e84
HZ
12180
12181 /* Run the command list here. Take ownership of it instead of
12182 copying. We never want these commands to run later in
12183 bpstat_do_actions, if a breakpoint that causes a stop happens to
12184 be set at same address as this dprintf, or even if running the
12185 commands here throws. */
9c95aea1
KB
12186 counted_command_line cmds = std::move (bs->commands);
12187 gdb_assert (cmds != nullptr);
12188 execute_control_commands (cmds.get (), 0);
9d6e6e84
HZ
12189}
12190
983af33b
SDJ
12191/* The breakpoint_ops structure to be used on static tracepoints with
12192 markers (`-m'). */
12193
12194static void
264f9890
PA
12195strace_marker_create_sals_from_location_spec (location_spec *locspec,
12196 struct linespec_result *canonical)
983af33b
SDJ
12197{
12198 struct linespec_sals lsal;
f00aae0f 12199 const char *arg_start, *arg;
983af33b 12200
40d97ee2 12201 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 12202 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12203
f2fc3015
TT
12204 std::string str (arg_start, arg - arg_start);
12205 const char *ptr = str.c_str ();
264f9890
PA
12206 canonical->locspec
12207 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
983af33b 12208
709438c7 12209 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12210 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12211}
12212
12213static void
12214strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12215 struct linespec_result *canonical,
e1e01040
PA
12216 gdb::unique_xmalloc_ptr<char> cond_string,
12217 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12218 enum bptype type_wanted,
12219 enum bpdisp disposition,
12220 int thread,
12221 int task, int ignore_count,
983af33b 12222 int from_tty, int enabled,
44f238bb 12223 int internal, unsigned flags)
983af33b 12224{
6c5b2ebe 12225 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12226
12227 /* If the user is creating a static tracepoint by marker id
12228 (strace -m MARKER_ID), then store the sals index, so that
12229 breakpoint_re_set can try to match up which of the newly
12230 found markers corresponds to this one, and, don't try to
12231 expand multiple locations for each sal, given than SALS
12232 already should contain all sals for MARKER_ID. */
12233
6c5b2ebe 12234 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12235 {
5c1ddcb6 12236 location_spec_up locspec = canonical->locspec->clone ();
983af33b 12237
3b003a61
PA
12238 std::unique_ptr<tracepoint> tp
12239 (new tracepoint (gdbarch,
12240 type_wanted,
12241 lsal.sals[i],
264f9890 12242 std::move (locspec),
3b003a61
PA
12243 NULL,
12244 std::move (cond_string),
12245 std::move (extra_string),
12246 disposition,
12247 thread, task, ignore_count,
12248 from_tty, enabled, flags,
12249 canonical->special_display));
12250
983af33b
SDJ
12251 /* Given that its possible to have multiple markers with
12252 the same string id, if the user is creating a static
12253 tracepoint by marker id ("strace -m MARKER_ID"), then
12254 store the sals index, so that breakpoint_re_set can
12255 try to match up which of the newly found markers
12256 corresponds to this one */
12257 tp->static_trace_marker_id_idx = i;
12258
b270e6f9 12259 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12260 }
12261}
12262
6d7a8c56 12263std::vector<symtab_and_line>
264f9890
PA
12264static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12265 program_space *search_pspace)
983af33b 12266{
40d97ee2 12267 const char *s = as_linespec_location_spec (locspec)->spec_string;
983af33b 12268
6c5b2ebe 12269 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
6d7a8c56 12270 if (sals.size () > static_trace_marker_id_idx)
983af33b 12271 {
6d7a8c56 12272 sals[0] = sals[static_trace_marker_id_idx];
6c5b2ebe
PA
12273 sals.resize (1);
12274 return sals;
983af33b
SDJ
12275 }
12276 else
6d7a8c56 12277 error (_("marker %s not found"), static_trace_marker_id.c_str ());
983af33b
SDJ
12278}
12279
8613a476
TT
12280/* Static tracepoints with marker (`-m'). */
12281static struct breakpoint_ops strace_marker_breakpoint_ops =
12282{
264f9890 12283 strace_marker_create_sals_from_location_spec,
8613a476
TT
12284 strace_marker_create_breakpoints_sal,
12285};
983af33b 12286
d8de7963 12287static bool
983af33b
SDJ
12288strace_marker_p (struct breakpoint *b)
12289{
7b572efb 12290 return b->type == bp_static_marker_tracepoint;
983af33b
SDJ
12291}
12292
53a5351d 12293/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 12294 structures. */
c906108c
SS
12295
12296void
fba45db2 12297delete_breakpoint (struct breakpoint *bpt)
c906108c 12298{
8a3fe4f8 12299 gdb_assert (bpt != NULL);
c906108c 12300
4a64f543
MS
12301 /* Has this bp already been deleted? This can happen because
12302 multiple lists can hold pointers to bp's. bpstat lists are
12303 especial culprits.
12304
12305 One example of this happening is a watchpoint's scope bp. When
12306 the scope bp triggers, we notice that the watchpoint is out of
12307 scope, and delete it. We also delete its scope bp. But the
12308 scope bp is marked "auto-deleting", and is already on a bpstat.
12309 That bpstat is then checked for auto-deleting bp's, which are
12310 deleted.
12311
12312 A real solution to this problem might involve reference counts in
12313 bp's, and/or giving them pointers back to their referencing
12314 bpstat's, and teaching delete_breakpoint to only free a bp's
12315 storage when no more references were extent. A cheaper bandaid
12316 was chosen. */
c906108c
SS
12317 if (bpt->type == bp_none)
12318 return;
12319
4a64f543
MS
12320 /* At least avoid this stale reference until the reference counting
12321 of breakpoints gets resolved. */
d0fb5eae 12322 if (bpt->related_breakpoint != bpt)
e5a0a904 12323 {
d0fb5eae 12324 struct breakpoint *related;
3a5c3e22 12325 struct watchpoint *w;
d0fb5eae
JK
12326
12327 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 12328 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 12329 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
12330 w = (struct watchpoint *) bpt;
12331 else
12332 w = NULL;
12333 if (w != NULL)
12334 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
12335
12336 /* Unlink bpt from the bpt->related_breakpoint ring. */
12337 for (related = bpt; related->related_breakpoint != bpt;
12338 related = related->related_breakpoint);
12339 related->related_breakpoint = bpt->related_breakpoint;
12340 bpt->related_breakpoint = bpt;
e5a0a904
JK
12341 }
12342
a9634178
TJB
12343 /* watch_command_1 creates a watchpoint but only sets its number if
12344 update_watchpoint succeeds in creating its bp_locations. If there's
12345 a problem in that process, we'll be asked to delete the half-created
12346 watchpoint. In that case, don't announce the deletion. */
12347 if (bpt->number)
76727919 12348 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 12349
c906108c
SS
12350 if (breakpoint_chain == bpt)
12351 breakpoint_chain = bpt->next;
12352
43892fdf 12353 for (breakpoint *b : all_breakpoints ())
c906108c 12354 if (b->next == bpt)
01add95b
SM
12355 {
12356 b->next = bpt->next;
12357 break;
12358 }
c906108c 12359
f431efe5
PA
12360 /* Be sure no bpstat's are pointing at the breakpoint after it's
12361 been freed. */
12362 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 12363 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
12364 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12365 commands are associated with the bpstat; if we remove it here,
12366 then the later call to bpstat_do_actions (&stop_bpstat); in
12367 event-top.c won't do anything, and temporary breakpoints with
12368 commands won't work. */
12369
12370 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12371
4a64f543
MS
12372 /* Now that breakpoint is removed from breakpoint list, update the
12373 global location list. This will remove locations that used to
12374 belong to this breakpoint. Do this before freeing the breakpoint
12375 itself, since remove_breakpoint looks at location's owner. It
12376 might be better design to have location completely
12377 self-contained, but it's not the case now. */
44702360 12378 update_global_location_list (UGLL_DONT_INSERT);
74960c60 12379
4a64f543
MS
12380 /* On the chance that someone will soon try again to delete this
12381 same bp, we mark it as deleted before freeing its storage. */
c906108c 12382 bpt->type = bp_none;
4d01a485 12383 delete bpt;
c906108c
SS
12384}
12385
51be5b68
PA
12386/* Iterator function to call a user-provided callback function once
12387 for each of B and its related breakpoints. */
12388
12389static void
12390iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 12391 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
12392{
12393 struct breakpoint *related;
12394
12395 related = b;
12396 do
12397 {
12398 struct breakpoint *next;
12399
12400 /* FUNCTION may delete RELATED. */
12401 next = related->related_breakpoint;
12402
12403 if (next == related)
12404 {
12405 /* RELATED is the last ring entry. */
48649e1b 12406 function (related);
51be5b68
PA
12407
12408 /* FUNCTION may have deleted it, so we'd never reach back to
12409 B. There's nothing left to do anyway, so just break
12410 out. */
12411 break;
12412 }
12413 else
48649e1b 12414 function (related);
51be5b68
PA
12415
12416 related = next;
12417 }
12418 while (related != b);
12419}
95a42b64 12420
4495129a 12421static void
981a3fb3 12422delete_command (const char *arg, int from_tty)
c906108c 12423{
ea9365bb
TT
12424 dont_repeat ();
12425
c906108c
SS
12426 if (arg == 0)
12427 {
12428 int breaks_to_delete = 0;
12429
46c6471b 12430 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
12431 internal breakpoints, these have to be deleted with an
12432 explicit breakpoint number argument. */
43892fdf 12433 for (breakpoint *b : all_breakpoints ())
46c6471b 12434 if (user_breakpoint_p (b))
973d738b
DJ
12435 {
12436 breaks_to_delete = 1;
12437 break;
12438 }
c906108c
SS
12439
12440 /* Ask user only if there are some breakpoints to delete. */
12441 if (!from_tty
e2e0b3e5 12442 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
12443 for (breakpoint *b : all_breakpoints_safe ())
12444 if (user_breakpoint_p (b))
12445 delete_breakpoint (b);
c906108c
SS
12446 }
12447 else
48649e1b 12448 map_breakpoint_numbers
b926417a 12449 (arg, [&] (breakpoint *br)
48649e1b 12450 {
b926417a 12451 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 12452 });
c906108c
SS
12453}
12454
c2f4122d
PA
12455/* Return true if all locations of B bound to PSPACE are pending. If
12456 PSPACE is NULL, all locations of all program spaces are
12457 considered. */
12458
d8de7963 12459static bool
c2f4122d 12460all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 12461{
40cb8ca5 12462 for (bp_location *loc : b->locations ())
c2f4122d
PA
12463 if ((pspace == NULL
12464 || loc->pspace == pspace)
12465 && !loc->shlib_disabled
8645ff69 12466 && !loc->pspace->executing_startup)
d8de7963
AB
12467 return false;
12468 return true;
fe3f5fa8
VP
12469}
12470
776592bf 12471/* Subroutine of update_breakpoint_locations to simplify it.
d8de7963 12472 Return true if multiple fns in list LOC have the same name.
776592bf
DE
12473 Null names are ignored. */
12474
d8de7963 12475static bool
776592bf
DE
12476ambiguous_names_p (struct bp_location *loc)
12477{
12478 struct bp_location *l;
2698f5ea 12479 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 12480 xcalloc, xfree));
776592bf
DE
12481
12482 for (l = loc; l != NULL; l = l->next)
12483 {
12484 const char **slot;
23d6ee64 12485 const char *name = l->function_name.get ();
776592bf
DE
12486
12487 /* Allow for some names to be NULL, ignore them. */
12488 if (name == NULL)
12489 continue;
12490
c1fb9836 12491 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 12492 INSERT);
4a64f543
MS
12493 /* NOTE: We can assume slot != NULL here because xcalloc never
12494 returns NULL. */
776592bf 12495 if (*slot != NULL)
d8de7963 12496 return true;
776592bf
DE
12497 *slot = name;
12498 }
12499
d8de7963 12500 return false;
776592bf
DE
12501}
12502
0fb4aa4b
PA
12503/* When symbols change, it probably means the sources changed as well,
12504 and it might mean the static tracepoint markers are no longer at
12505 the same address or line numbers they used to be at last we
12506 checked. Losing your static tracepoints whenever you rebuild is
12507 undesirable. This function tries to resync/rematch gdb static
12508 tracepoints with the markers on the target, for static tracepoints
12509 that have not been set by marker id. Static tracepoint that have
12510 been set by marker id are reset by marker id in breakpoint_re_set.
12511 The heuristic is:
12512
12513 1) For a tracepoint set at a specific address, look for a marker at
12514 the old PC. If one is found there, assume to be the same marker.
12515 If the name / string id of the marker found is different from the
12516 previous known name, assume that means the user renamed the marker
12517 in the sources, and output a warning.
12518
12519 2) For a tracepoint set at a given line number, look for a marker
12520 at the new address of the old line number. If one is found there,
12521 assume to be the same marker. If the name / string id of the
12522 marker found is different from the previous known name, assume that
12523 means the user renamed the marker in the sources, and output a
12524 warning.
12525
12526 3) If a marker is no longer found at the same address or line, it
12527 may mean the marker no longer exists. But it may also just mean
12528 the code changed a bit. Maybe the user added a few lines of code
12529 that made the marker move up or down (in line number terms). Ask
12530 the target for info about the marker with the string id as we knew
12531 it. If found, update line number and address in the matching
12532 static tracepoint. This will get confused if there's more than one
12533 marker with the same ID (possible in UST, although unadvised
12534 precisely because it confuses tools). */
12535
12536static struct symtab_and_line
12537update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12538{
d9b3f62e 12539 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
12540 struct static_tracepoint_marker marker;
12541 CORE_ADDR pc;
0fb4aa4b
PA
12542
12543 pc = sal.pc;
12544 if (sal.line)
12545 find_line_pc (sal.symtab, sal.line, &pc);
12546
12547 if (target_static_tracepoint_marker_at (pc, &marker))
12548 {
5d9310c4 12549 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 12550 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
12551 b->number, tp->static_trace_marker_id.c_str (),
12552 marker.str_id.c_str ());
0fb4aa4b 12553
5d9310c4 12554 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
12555
12556 return sal;
12557 }
12558
12559 /* Old marker wasn't found on target at lineno. Try looking it up
12560 by string ID. */
12561 if (!sal.explicit_pc
12562 && sal.line != 0
12563 && sal.symtab != NULL
5d9310c4 12564 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 12565 {
5d9310c4
SM
12566 std::vector<static_tracepoint_marker> markers
12567 = target_static_tracepoint_markers_by_strid
12568 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 12569
5d9310c4 12570 if (!markers.empty ())
0fb4aa4b 12571 {
0fb4aa4b 12572 struct symbol *sym;
80e1d417 12573 struct static_tracepoint_marker *tpmarker;
79a45e25 12574 struct ui_out *uiout = current_uiout;
0fb4aa4b 12575
5d9310c4 12576 tpmarker = &markers[0];
0fb4aa4b 12577
5d9310c4 12578 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
12579
12580 warning (_("marker for static tracepoint %d (%s) not "
12581 "found at previous line number"),
5d9310c4 12582 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 12583
51abb421 12584 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 12585 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 12586 uiout->text ("Now in ");
0fb4aa4b
PA
12587 if (sym)
12588 {
987012b8 12589 uiout->field_string ("func", sym->print_name (),
e43b10e1 12590 function_name_style.style ());
112e8700 12591 uiout->text (" at ");
0fb4aa4b 12592 }
112e8700 12593 uiout->field_string ("file",
cbe56571 12594 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 12595 file_name_style.style ());
112e8700 12596 uiout->text (":");
0fb4aa4b 12597
112e8700 12598 if (uiout->is_mi_like_p ())
0fb4aa4b 12599 {
0b0865da 12600 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 12601
112e8700 12602 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
12603 }
12604
381befee 12605 uiout->field_signed ("line", sal2.line);
112e8700 12606 uiout->text ("\n");
0fb4aa4b 12607
80e1d417 12608 b->loc->line_number = sal2.line;
2f202fde 12609 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 12610
40d97ee2
PA
12611 std::unique_ptr<explicit_location_spec> els
12612 (new explicit_location_spec ());
12613 els->source_filename
12614 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12615 els->line_offset.offset = b->loc->line_number;
12616 els->line_offset.sign = LINE_OFFSET_NONE;
12617
12618 b->locspec = std::move (els);
0fb4aa4b
PA
12619
12620 /* Might be nice to check if function changed, and warn if
12621 so. */
0fb4aa4b
PA
12622 }
12623 }
12624 return sal;
12625}
12626
d8de7963 12627/* Returns true iff locations A and B are sufficiently same that
8d3788bd
VP
12628 we don't need to report breakpoint as changed. */
12629
d8de7963 12630static bool
8d3788bd
VP
12631locations_are_equal (struct bp_location *a, struct bp_location *b)
12632{
12633 while (a && b)
12634 {
12635 if (a->address != b->address)
d8de7963 12636 return false;
8d3788bd
VP
12637
12638 if (a->shlib_disabled != b->shlib_disabled)
d8de7963 12639 return false;
8d3788bd
VP
12640
12641 if (a->enabled != b->enabled)
d8de7963 12642 return false;
8d3788bd 12643
b5fa468f 12644 if (a->disabled_by_cond != b->disabled_by_cond)
d8de7963 12645 return false;
b5fa468f 12646
8d3788bd
VP
12647 a = a->next;
12648 b = b->next;
12649 }
12650
12651 if ((a == NULL) != (b == NULL))
d8de7963 12652 return false;
8d3788bd 12653
d8de7963 12654 return true;
8d3788bd
VP
12655}
12656
c2f4122d
PA
12657/* Split all locations of B that are bound to PSPACE out of B's
12658 location list to a separate list and return that list's head. If
12659 PSPACE is NULL, hoist out all locations of B. */
12660
12661static struct bp_location *
12662hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12663{
12664 struct bp_location head;
12665 struct bp_location *i = b->loc;
12666 struct bp_location **i_link = &b->loc;
12667 struct bp_location *hoisted = &head;
12668
12669 if (pspace == NULL)
12670 {
12671 i = b->loc;
12672 b->loc = NULL;
12673 return i;
12674 }
12675
12676 head.next = NULL;
12677
12678 while (i != NULL)
12679 {
12680 if (i->pspace == pspace)
12681 {
12682 *i_link = i->next;
12683 i->next = NULL;
12684 hoisted->next = i;
12685 hoisted = i;
12686 }
12687 else
12688 i_link = &i->next;
12689 i = *i_link;
12690 }
12691
12692 return head.next;
12693}
12694
12695/* Create new breakpoint locations for B (a hardware or software
12696 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12697 zero, then B is a ranged breakpoint. Only recreates locations for
12698 FILTER_PSPACE. Locations of other program spaces are left
12699 untouched. */
f1310107 12700
0e30163f 12701void
74421c0b 12702update_breakpoint_locations (code_breakpoint *b,
c2f4122d 12703 struct program_space *filter_pspace,
6c5b2ebe
PA
12704 gdb::array_view<const symtab_and_line> sals,
12705 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 12706{
c2f4122d 12707 struct bp_location *existing_locations;
0d381245 12708
6c5b2ebe 12709 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
12710 {
12711 /* Ranged breakpoints have only one start location and one end
12712 location. */
12713 b->enable_state = bp_disabled;
6cb06a8c
TT
12714 gdb_printf (gdb_stderr,
12715 _("Could not reset ranged breakpoint %d: "
12716 "multiple locations found\n"),
12717 b->number);
f8eba3c6
TT
12718 return;
12719 }
f1310107 12720
4a64f543
MS
12721 /* If there's no new locations, and all existing locations are
12722 pending, don't do anything. This optimizes the common case where
12723 all locations are in the same shared library, that was unloaded.
12724 We'd like to retain the location, so that when the library is
12725 loaded again, we don't loose the enabled/disabled status of the
12726 individual locations. */
6c5b2ebe 12727 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
12728 return;
12729
c2f4122d 12730 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 12731
6c5b2ebe 12732 for (const auto &sal : sals)
fe3f5fa8 12733 {
f8eba3c6
TT
12734 struct bp_location *new_loc;
12735
6c5b2ebe 12736 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 12737
06615628 12738 new_loc = b->add_location (sal);
fe3f5fa8 12739
0d381245
VP
12740 /* Reparse conditions, they might contain references to the
12741 old symtab. */
12742 if (b->cond_string != NULL)
12743 {
bbc13ae3 12744 const char *s;
fe3f5fa8 12745
6f781ee3 12746 s = b->cond_string.get ();
a70b8144 12747 try
0d381245 12748 {
6c5b2ebe
PA
12749 new_loc->cond = parse_exp_1 (&s, sal.pc,
12750 block_for_pc (sal.pc),
0d381245
VP
12751 0);
12752 }
230d2906 12753 catch (const gdb_exception_error &e)
0d381245 12754 {
b5fa468f 12755 new_loc->disabled_by_cond = true;
0d381245
VP
12756 }
12757 }
fe3f5fa8 12758
6c5b2ebe 12759 if (!sals_end.empty ())
f1310107 12760 {
6c5b2ebe 12761 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 12762
6c5b2ebe 12763 new_loc->length = end - sals[0].pc + 1;
f1310107 12764 }
0d381245 12765 }
fe3f5fa8 12766
4a64f543
MS
12767 /* If possible, carry over 'disable' status from existing
12768 breakpoints. */
0d381245
VP
12769 {
12770 struct bp_location *e = existing_locations;
776592bf
DE
12771 /* If there are multiple breakpoints with the same function name,
12772 e.g. for inline functions, comparing function names won't work.
12773 Instead compare pc addresses; this is just a heuristic as things
12774 may have moved, but in practice it gives the correct answer
12775 often enough until a better solution is found. */
12776 int have_ambiguous_names = ambiguous_names_p (b->loc);
12777
0d381245
VP
12778 for (; e; e = e->next)
12779 {
b5fa468f 12780 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 12781 {
776592bf
DE
12782 if (have_ambiguous_names)
12783 {
40cb8ca5 12784 for (bp_location *l : b->locations ())
7f32a4d5
PA
12785 {
12786 /* Ignore software vs hardware location type at
12787 this point, because with "set breakpoint
12788 auto-hw", after a re-set, locations that were
12789 hardware can end up as software, or vice versa.
12790 As mentioned above, this is an heuristic and in
12791 practice should give the correct answer often
12792 enough. */
12793 if (breakpoint_locations_match (e, l, true))
12794 {
b5fa468f
TBA
12795 l->enabled = e->enabled;
12796 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
12797 break;
12798 }
12799 }
776592bf
DE
12800 }
12801 else
12802 {
40cb8ca5 12803 for (bp_location *l : b->locations ())
776592bf 12804 if (l->function_name
23d6ee64
TT
12805 && strcmp (e->function_name.get (),
12806 l->function_name.get ()) == 0)
776592bf 12807 {
b5fa468f
TBA
12808 l->enabled = e->enabled;
12809 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
12810 break;
12811 }
12812 }
0d381245
VP
12813 }
12814 }
12815 }
fe3f5fa8 12816
8d3788bd 12817 if (!locations_are_equal (existing_locations, b->loc))
76727919 12818 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
12819}
12820
264f9890 12821/* Find the SaL locations corresponding to the given LOCSPEC.
ef23e705
TJB
12822 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12823
2c9a6d72
TT
12824std::vector<symtab_and_line>
12825code_breakpoint::location_spec_to_sals (location_spec *locspec,
12826 struct program_space *search_pspace,
12827 int *found)
ef23e705 12828{
cc06b668 12829 struct gdb_exception exception;
ef23e705 12830
6c5b2ebe
PA
12831 std::vector<symtab_and_line> sals;
12832
a70b8144 12833 try
ef23e705 12834 {
2c9a6d72 12835 sals = decode_location_spec (locspec, search_pspace);
ef23e705 12836 }
94aeb44b 12837 catch (gdb_exception_error &e)
ef23e705 12838 {
d8de7963 12839 int not_found_and_ok = false;
492d29ea 12840
ef23e705
TJB
12841 /* For pending breakpoints, it's expected that parsing will
12842 fail until the right shared library is loaded. User has
12843 already told to create pending breakpoints and don't need
12844 extra messages. If breakpoint is in bp_shlib_disabled
12845 state, then user already saw the message about that
12846 breakpoint being disabled, and don't want to see more
12847 errors. */
58438ac1 12848 if (e.error == NOT_FOUND_ERROR
2c9a6d72
TT
12849 && (condition_not_parsed
12850 || (loc != NULL
c2f4122d 12851 && search_pspace != NULL
2c9a6d72
TT
12852 && loc->pspace != search_pspace)
12853 || (loc && loc->shlib_disabled)
12854 || (loc && loc->pspace->executing_startup)
12855 || enable_state == bp_disabled))
d8de7963 12856 not_found_and_ok = true;
ef23e705
TJB
12857
12858 if (!not_found_and_ok)
12859 {
12860 /* We surely don't want to warn about the same breakpoint
12861 10 times. One solution, implemented here, is disable
12862 the breakpoint on error. Another solution would be to
12863 have separate 'warning emitted' flag. Since this
12864 happens only when a binary has changed, I don't know
12865 which approach is better. */
2c9a6d72 12866 enable_state = bp_disabled;
eedc3f4f 12867 throw;
ef23e705 12868 }
94aeb44b
TT
12869
12870 exception = std::move (e);
ef23e705
TJB
12871 }
12872
492d29ea 12873 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 12874 {
6c5b2ebe
PA
12875 for (auto &sal : sals)
12876 resolve_sal_pc (&sal);
2c9a6d72 12877 if (condition_not_parsed && extra_string != NULL)
ef23e705 12878 {
2c9a6d72
TT
12879 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12880 int local_thread, local_task;
ef23e705 12881
2c9a6d72
TT
12882 find_condition_and_thread_for_sals (sals, extra_string.get (),
12883 &local_cond, &local_thread,
12884 &local_task, &local_extra);
12885 gdb_assert (cond_string == nullptr);
12886 if (local_cond != nullptr)
12887 cond_string = std::move (local_cond);
12888 thread = local_thread;
12889 task = local_task;
12890 if (local_extra != nullptr)
12891 extra_string = std::move (local_extra);
12892 condition_not_parsed = 0;
ef23e705
TJB
12893 }
12894
2c9a6d72
TT
12895 if (type == bp_static_tracepoint)
12896 sals[0] = update_static_tracepoint (this, sals[0]);
ef23e705 12897
58438ac1
TT
12898 *found = 1;
12899 }
12900 else
12901 *found = 0;
ef23e705
TJB
12902
12903 return sals;
12904}
12905
348d480f
PA
12906/* The default re_set method, for typical hardware or software
12907 breakpoints. Reevaluate the breakpoint and recreate its
12908 locations. */
12909
b3d5660a
TT
12910void
12911code_breakpoint::re_set_default ()
ef23e705 12912{
c2f4122d 12913 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 12914 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 12915
6c5b2ebe 12916 int found;
2c9a6d72
TT
12917 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12918 filter_pspace,
12919 &found);
ef23e705 12920 if (found)
6c5b2ebe 12921 expanded = std::move (sals);
ef23e705 12922
b3d5660a 12923 if (locspec_range_end != nullptr)
f1310107 12924 {
6c5b2ebe 12925 std::vector<symtab_and_line> sals_end
2c9a6d72 12926 = location_spec_to_sals (locspec_range_end.get (),
264f9890 12927 filter_pspace, &found);
f1310107 12928 if (found)
6c5b2ebe 12929 expanded_end = std::move (sals_end);
f1310107
TJB
12930 }
12931
b3d5660a 12932 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
28010a5d
PA
12933}
12934
983af33b
SDJ
12935/* Default method for creating SALs from an address string. It basically
12936 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12937
12938static void
264f9890
PA
12939create_sals_from_location_spec_default (location_spec *locspec,
12940 struct linespec_result *canonical)
983af33b 12941{
264f9890 12942 parse_breakpoint_sals (locspec, canonical);
983af33b
SDJ
12943}
12944
bf469271 12945/* Reset a breakpoint. */
c906108c 12946
bf469271
PA
12947static void
12948breakpoint_re_set_one (breakpoint *b)
c906108c 12949{
fdf44873
TT
12950 input_radix = b->input_radix;
12951 set_language (b->language);
c906108c 12952
c47614fe 12953 b->re_set ();
c906108c
SS
12954}
12955
c2f4122d
PA
12956/* Re-set breakpoint locations for the current program space.
12957 Locations bound to other program spaces are left untouched. */
12958
c906108c 12959void
69de3c6a 12960breakpoint_re_set (void)
c906108c 12961{
c5aa993b 12962 {
fdf44873
TT
12963 scoped_restore_current_language save_language;
12964 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 12965 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 12966
8e817061
JB
12967 /* breakpoint_re_set_one sets the current_language to the language
12968 of the breakpoint it is resetting (see prepare_re_set_context)
12969 before re-evaluating the breakpoint's location. This change can
12970 unfortunately get undone by accident if the language_mode is set
12971 to auto, and we either switch frames, or more likely in this context,
12972 we select the current frame.
12973
12974 We prevent this by temporarily turning the language_mode to
12975 language_mode_manual. We restore it once all breakpoints
12976 have been reset. */
12977 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12978 language_mode = language_mode_manual;
12979
5ed8105e
PA
12980 /* Note: we must not try to insert locations until after all
12981 breakpoints have been re-set. Otherwise, e.g., when re-setting
12982 breakpoint 1, we'd insert the locations of breakpoint 2, which
12983 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 12984
1428b37a 12985 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 12986 {
a70b8144 12987 try
bf469271
PA
12988 {
12989 breakpoint_re_set_one (b);
12990 }
230d2906 12991 catch (const gdb_exception &ex)
bf469271
PA
12992 {
12993 exception_fprintf (gdb_stderr, ex,
12994 "Error in re-setting breakpoint %d: ",
12995 b->number);
12996 }
5ed8105e 12997 }
5ed8105e
PA
12998
12999 jit_breakpoint_re_set ();
13000 }
6c95b8df 13001
af02033e
PP
13002 create_overlay_event_breakpoint ();
13003 create_longjmp_master_breakpoint ();
13004 create_std_terminate_master_breakpoint ();
186c406b 13005 create_exception_master_breakpoint ();
2a7f3dff
PA
13006
13007 /* Now we can insert. */
13008 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13009}
13010\f
c906108c
SS
13011/* Reset the thread number of this breakpoint:
13012
13013 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13014 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13015void
fba45db2 13016breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13017{
13018 if (b->thread != -1)
13019 {
00431a78 13020 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13021
13022 /* We're being called after following a fork. The new fork is
13023 selected as current, and unless this was a vfork will have a
13024 different program space from the original thread. Reset that
13025 as well. */
13026 b->loc->pspace = current_program_space;
c906108c
SS
13027 }
13028}
13029
03ac34d5
MS
13030/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13031 If from_tty is nonzero, it prints a message to that effect,
13032 which ends with a period (no newline). */
13033
c906108c 13034void
fba45db2 13035set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13036{
c906108c
SS
13037 if (count < 0)
13038 count = 0;
13039
43892fdf 13040 for (breakpoint *b : all_breakpoints ())
c906108c 13041 if (b->number == bptnum)
01add95b
SM
13042 {
13043 if (is_tracepoint (b))
13044 {
13045 if (from_tty && count != 0)
6cb06a8c
TT
13046 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13047 bptnum);
01add95b
SM
13048 return;
13049 }
13050
13051 b->ignore_count = count;
13052 if (from_tty)
13053 {
13054 if (count == 0)
6cb06a8c
TT
13055 gdb_printf (_("Will stop next time "
13056 "breakpoint %d is reached."),
13057 bptnum);
01add95b 13058 else if (count == 1)
6cb06a8c
TT
13059 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13060 bptnum);
01add95b 13061 else
6cb06a8c
TT
13062 gdb_printf (_("Will ignore next %d "
13063 "crossings of breakpoint %d."),
13064 count, bptnum);
01add95b
SM
13065 }
13066 gdb::observers::breakpoint_modified.notify (b);
13067 return;
13068 }
c906108c 13069
8a3fe4f8 13070 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13071}
13072
c906108c
SS
13073/* Command to set ignore-count of breakpoint N to COUNT. */
13074
13075static void
0b39b52e 13076ignore_command (const char *args, int from_tty)
c906108c 13077{
0b39b52e 13078 const char *p = args;
52f0bd74 13079 int num;
c906108c
SS
13080
13081 if (p == 0)
e2e0b3e5 13082 error_no_arg (_("a breakpoint number"));
c5aa993b 13083
c906108c 13084 num = get_number (&p);
5c44784c 13085 if (num == 0)
8a3fe4f8 13086 error (_("bad breakpoint number: '%s'"), args);
c906108c 13087 if (*p == 0)
8a3fe4f8 13088 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13089
13090 set_ignore_count (num,
13091 longest_to_int (value_as_long (parse_and_eval (p))),
13092 from_tty);
221ea385 13093 if (from_tty)
6cb06a8c 13094 gdb_printf ("\n");
c906108c
SS
13095}
13096\f
d0fe4701
XR
13097
13098/* Call FUNCTION on each of the breakpoints with numbers in the range
13099 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13100
13101static void
d0fe4701
XR
13102map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13103 gdb::function_view<void (breakpoint *)> function)
c906108c 13104{
d0fe4701
XR
13105 if (bp_num_range.first == 0)
13106 {
13107 warning (_("bad breakpoint number at or near '%d'"),
13108 bp_num_range.first);
13109 }
13110 else
c906108c 13111 {
d0fe4701 13112 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13113 {
d0fe4701
XR
13114 bool match = false;
13115
1428b37a 13116 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 13117 if (b->number == i)
5c44784c 13118 {
bfd28288 13119 match = true;
48649e1b 13120 function (b);
11cf8741 13121 break;
5c44784c 13122 }
bfd28288 13123 if (!match)
6cb06a8c 13124 gdb_printf (_("No breakpoint number %d.\n"), i);
c5aa993b 13125 }
c906108c
SS
13126 }
13127}
13128
d0fe4701
XR
13129/* Call FUNCTION on each of the breakpoints whose numbers are given in
13130 ARGS. */
13131
13132static void
13133map_breakpoint_numbers (const char *args,
13134 gdb::function_view<void (breakpoint *)> function)
13135{
13136 if (args == NULL || *args == '\0')
13137 error_no_arg (_("one or more breakpoint numbers"));
13138
13139 number_or_range_parser parser (args);
13140
13141 while (!parser.finished ())
13142 {
13143 int num = parser.get_number ();
13144 map_breakpoint_number_range (std::make_pair (num, num), function);
13145 }
13146}
13147
13148/* Return the breakpoint location structure corresponding to the
13149 BP_NUM and LOC_NUM values. */
13150
0d381245 13151static struct bp_location *
d0fe4701 13152find_location_by_number (int bp_num, int loc_num)
0d381245 13153{
43892fdf 13154 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
13155
13156 if (!b || b->number != bp_num)
d0fe4701 13157 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13158
0d381245 13159 if (loc_num == 0)
d0fe4701 13160 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13161
d0fe4701 13162 int n = 0;
40cb8ca5 13163 for (bp_location *loc : b->locations ())
d0fe4701
XR
13164 if (++n == loc_num)
13165 return loc;
13166
13167 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13168}
13169
95e95a6d
PA
13170/* Modes of operation for extract_bp_num. */
13171enum class extract_bp_kind
13172{
13173 /* Extracting a breakpoint number. */
13174 bp,
13175
13176 /* Extracting a location number. */
13177 loc,
13178};
13179
13180/* Extract a breakpoint or location number (as determined by KIND)
13181 from the string starting at START. TRAILER is a character which
13182 can be found after the number. If you don't want a trailer, use
13183 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13184 string. This always returns a positive integer. */
13185
13186static int
13187extract_bp_num (extract_bp_kind kind, const char *start,
13188 int trailer, const char **end_out = NULL)
13189{
13190 const char *end = start;
13191 int num = get_number_trailer (&end, trailer);
13192 if (num < 0)
13193 error (kind == extract_bp_kind::bp
13194 ? _("Negative breakpoint number '%.*s'")
13195 : _("Negative breakpoint location number '%.*s'"),
13196 int (end - start), start);
13197 if (num == 0)
13198 error (kind == extract_bp_kind::bp
13199 ? _("Bad breakpoint number '%.*s'")
13200 : _("Bad breakpoint location number '%.*s'"),
13201 int (end - start), start);
13202
13203 if (end_out != NULL)
13204 *end_out = end;
13205 return num;
13206}
13207
13208/* Extract a breakpoint or location range (as determined by KIND) in
13209 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13210 representing the (inclusive) range. The returned pair's elements
13211 are always positive integers. */
13212
13213static std::pair<int, int>
13214extract_bp_or_bp_range (extract_bp_kind kind,
13215 const std::string &arg,
13216 std::string::size_type arg_offset)
13217{
13218 std::pair<int, int> range;
13219 const char *bp_loc = &arg[arg_offset];
13220 std::string::size_type dash = arg.find ('-', arg_offset);
13221 if (dash != std::string::npos)
13222 {
13223 /* bp_loc is a range (x-z). */
13224 if (arg.length () == dash + 1)
13225 error (kind == extract_bp_kind::bp
13226 ? _("Bad breakpoint number at or near: '%s'")
13227 : _("Bad breakpoint location number at or near: '%s'"),
13228 bp_loc);
13229
13230 const char *end;
13231 const char *start_first = bp_loc;
13232 const char *start_second = &arg[dash + 1];
13233 range.first = extract_bp_num (kind, start_first, '-');
13234 range.second = extract_bp_num (kind, start_second, '\0', &end);
13235
13236 if (range.first > range.second)
13237 error (kind == extract_bp_kind::bp
13238 ? _("Inverted breakpoint range at '%.*s'")
13239 : _("Inverted breakpoint location range at '%.*s'"),
13240 int (end - start_first), start_first);
13241 }
13242 else
13243 {
13244 /* bp_loc is a single value. */
13245 range.first = extract_bp_num (kind, bp_loc, '\0');
13246 range.second = range.first;
13247 }
13248 return range;
13249}
13250
d0fe4701
XR
13251/* Extract the breakpoint/location range specified by ARG. Returns
13252 the breakpoint range in BP_NUM_RANGE, and the location range in
13253 BP_LOC_RANGE.
13254
13255 ARG may be in any of the following forms:
13256
13257 x where 'x' is a breakpoint number.
13258 x-y where 'x' and 'y' specify a breakpoint numbers range.
13259 x.y where 'x' is a breakpoint number and 'y' a location number.
13260 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13261 location number range.
13262*/
13263
cc638e86 13264static void
d0fe4701
XR
13265extract_bp_number_and_location (const std::string &arg,
13266 std::pair<int, int> &bp_num_range,
13267 std::pair<int, int> &bp_loc_range)
13268{
13269 std::string::size_type dot = arg.find ('.');
13270
13271 if (dot != std::string::npos)
13272 {
13273 /* Handle 'x.y' and 'x.y-z' cases. */
13274
13275 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 13276 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 13277
95e95a6d
PA
13278 bp_num_range.first
13279 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13280 bp_num_range.second = bp_num_range.first;
d0fe4701 13281
95e95a6d
PA
13282 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13283 arg, dot + 1);
d0fe4701
XR
13284 }
13285 else
13286 {
13287 /* Handle x and x-y cases. */
d0fe4701 13288
95e95a6d 13289 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
13290 bp_loc_range.first = 0;
13291 bp_loc_range.second = 0;
13292 }
d0fe4701
XR
13293}
13294
13295/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13296 specifies whether to enable or disable. */
13297
13298static void
13299enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13300{
13301 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13302 if (loc != NULL)
13303 {
b5fa468f
TBA
13304 if (loc->disabled_by_cond && enable)
13305 error (_("Breakpoint %d's condition is invalid at location %d, "
13306 "cannot enable."), bp_num, loc_num);
13307
d0fe4701
XR
13308 if (loc->enabled != enable)
13309 {
13310 loc->enabled = enable;
13311 mark_breakpoint_location_modified (loc);
13312 }
13313 if (target_supports_enable_disable_tracepoint ()
13314 && current_trace_status ()->running && loc->owner
13315 && is_tracepoint (loc->owner))
13316 target_disable_tracepoint (loc);
13317 }
13318 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13319
e5213e2c
SF
13320 gdb::observers::breakpoint_modified.notify (loc->owner);
13321}
13322
13323/* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13324 owner. 1-based indexing. -1 signals NOT FOUND. */
13325
13326static int
d8a77e4c 13327find_loc_num_by_location (const bp_location *loc)
e5213e2c
SF
13328{
13329 if (loc != nullptr && loc->owner != nullptr)
13330 {
13331 /* Locations use 1-based indexing. */
13332 int loc_num = 1;
13333 for (bp_location *it : loc->owner->locations ())
13334 {
13335 if (it == loc)
13336 return loc_num;
13337 loc_num++;
13338 }
13339 }
13340 return -1;
13341}
13342
13343/* Enable or disable a breakpoint location LOC. ENABLE
13344 specifies whether to enable or disable. */
13345
13346void
13347enable_disable_bp_location (bp_location *loc, bool enable)
13348{
13349 if (loc == nullptr)
13350 error (_("Breakpoint location is invalid."));
13351
13352 if (loc->owner == nullptr)
13353 error (_("Breakpoint location does not have an owner breakpoint."));
13354
13355 if (loc->disabled_by_cond && enable)
13356 {
13357 int loc_num = find_loc_num_by_location (loc);
13358 if (loc_num == -1)
13359 error (_("Breakpoint location LOC_NUM could not be found."));
13360 else
13361 error (_("Breakpoint %d's condition is invalid at location %d, "
13362 "cannot enable."), loc->owner->number, loc_num);
13363 }
13364
13365 if (loc->enabled != enable)
13366 {
13367 loc->enabled = enable;
13368 mark_breakpoint_location_modified (loc);
13369 }
13370
13371 if (target_supports_enable_disable_tracepoint ()
13372 && current_trace_status ()->running && loc->owner
13373 && is_tracepoint (loc->owner))
13374 target_disable_tracepoint (loc);
13375
13376 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13377 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
13378}
13379
13380/* Enable or disable a range of breakpoint locations. BP_NUM is the
13381 number of the breakpoint, and BP_LOC_RANGE specifies the
13382 (inclusive) range of location numbers of that breakpoint to
13383 enable/disable. ENABLE specifies whether to enable or disable the
13384 location. */
13385
13386static void
13387enable_disable_breakpoint_location_range (int bp_num,
13388 std::pair<int, int> &bp_loc_range,
13389 bool enable)
13390{
13391 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13392 enable_disable_bp_num_loc (bp_num, i, enable);
13393}
0d381245 13394
1900040c
MS
13395/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13396 If from_tty is nonzero, it prints a message to that effect,
13397 which ends with a period (no newline). */
13398
c906108c 13399void
fba45db2 13400disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
13401{
13402 /* Never disable a watchpoint scope breakpoint; we want to
13403 hit them when we leave scope so we can delete both the
13404 watchpoint and its scope breakpoint at that time. */
13405 if (bpt->type == bp_watchpoint_scope)
13406 return;
13407
b5de0fa7 13408 bpt->enable_state = bp_disabled;
c906108c 13409
b775012e
LM
13410 /* Mark breakpoint locations modified. */
13411 mark_breakpoint_modified (bpt);
13412
d248b706
KY
13413 if (target_supports_enable_disable_tracepoint ()
13414 && current_trace_status ()->running && is_tracepoint (bpt))
13415 {
40cb8ca5 13416 for (bp_location *location : bpt->locations ())
d248b706
KY
13417 target_disable_tracepoint (location);
13418 }
13419
44702360 13420 update_global_location_list (UGLL_DONT_INSERT);
c906108c 13421
76727919 13422 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13423}
13424
d0fe4701
XR
13425/* Enable or disable the breakpoint(s) or breakpoint location(s)
13426 specified in ARGS. ARGS may be in any of the formats handled by
13427 extract_bp_number_and_location. ENABLE specifies whether to enable
13428 or disable the breakpoints/locations. */
13429
c906108c 13430static void
d0fe4701 13431enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 13432{
c906108c 13433 if (args == 0)
46c6471b 13434 {
43892fdf 13435 for (breakpoint *bpt : all_breakpoints ())
46c6471b 13436 if (user_breakpoint_p (bpt))
d0fe4701
XR
13437 {
13438 if (enable)
13439 enable_breakpoint (bpt);
13440 else
13441 disable_breakpoint (bpt);
13442 }
46c6471b 13443 }
9eaabc75 13444 else
0d381245 13445 {
cb791d59 13446 std::string num = extract_arg (&args);
9eaabc75 13447
cb791d59 13448 while (!num.empty ())
d248b706 13449 {
d0fe4701 13450 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 13451
cc638e86
PA
13452 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13453
13454 if (bp_loc_range.first == bp_loc_range.second
78805ff8
PW
13455 && (bp_loc_range.first == 0
13456 || (bp_loc_range.first == 1
13457 && bp_num_range.first == bp_num_range.second
13458 && !has_multiple_locations (bp_num_range.first))))
d0fe4701 13459 {
78805ff8
PW
13460 /* Handle breakpoint ids with formats 'x' or 'x-z'
13461 or 'y.1' where y has only one code location. */
cc638e86
PA
13462 map_breakpoint_number_range (bp_num_range,
13463 enable
13464 ? enable_breakpoint
13465 : disable_breakpoint);
13466 }
13467 else
13468 {
13469 /* Handle breakpoint ids with formats 'x.y' or
13470 'x.y-z'. */
13471 enable_disable_breakpoint_location_range
13472 (bp_num_range.first, bp_loc_range, enable);
b775012e 13473 }
9eaabc75 13474 num = extract_arg (&args);
d248b706 13475 }
0d381245 13476 }
c906108c
SS
13477}
13478
d0fe4701
XR
13479/* The disable command disables the specified breakpoints/locations
13480 (or all defined breakpoints) so they're no longer effective in
13481 stopping the inferior. ARGS may be in any of the forms defined in
13482 extract_bp_number_and_location. */
13483
13484static void
13485disable_command (const char *args, int from_tty)
13486{
13487 enable_disable_command (args, from_tty, false);
13488}
13489
c906108c 13490static void
816338b5
SS
13491enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13492 int count)
c906108c 13493{
afe38095 13494 int target_resources_ok;
c906108c
SS
13495
13496 if (bpt->type == bp_hardware_breakpoint)
13497 {
13498 int i;
c5aa993b 13499 i = hw_breakpoint_used_count ();
53a5351d 13500 target_resources_ok =
d92524f1 13501 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 13502 i + 1, 0);
c906108c 13503 if (target_resources_ok == 0)
8a3fe4f8 13504 error (_("No hardware breakpoint support in the target."));
c906108c 13505 else if (target_resources_ok < 0)
8a3fe4f8 13506 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
13507 }
13508
cc60f2e3 13509 if (is_watchpoint (bpt))
c906108c 13510 {
d07205c2 13511 /* Initialize it just to avoid a GCC false warning. */
f486487f 13512 enum enable_state orig_enable_state = bp_disabled;
dde02812 13513
a70b8144 13514 try
c906108c 13515 {
3a5c3e22
PA
13516 struct watchpoint *w = (struct watchpoint *) bpt;
13517
1e718ff1
TJB
13518 orig_enable_state = bpt->enable_state;
13519 bpt->enable_state = bp_enabled;
d8de7963 13520 update_watchpoint (w, true /* reparse */);
c906108c 13521 }
230d2906 13522 catch (const gdb_exception &e)
c5aa993b 13523 {
1e718ff1 13524 bpt->enable_state = orig_enable_state;
dde02812
ES
13525 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13526 bpt->number);
13527 return;
c5aa993b 13528 }
c906108c 13529 }
0101ce28 13530
b775012e
LM
13531 bpt->enable_state = bp_enabled;
13532
13533 /* Mark breakpoint locations modified. */
13534 mark_breakpoint_modified (bpt);
13535
d248b706
KY
13536 if (target_supports_enable_disable_tracepoint ()
13537 && current_trace_status ()->running && is_tracepoint (bpt))
13538 {
40cb8ca5 13539 for (bp_location *location : bpt->locations ())
d248b706
KY
13540 target_enable_tracepoint (location);
13541 }
13542
b4c291bb 13543 bpt->disposition = disposition;
816338b5 13544 bpt->enable_count = count;
44702360 13545 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 13546
76727919 13547 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13548}
13549
fe3f5fa8 13550
c906108c 13551void
fba45db2 13552enable_breakpoint (struct breakpoint *bpt)
c906108c 13553{
816338b5 13554 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
13555}
13556
d0fe4701
XR
13557/* The enable command enables the specified breakpoints/locations (or
13558 all defined breakpoints) so they once again become (or continue to
13559 be) effective in stopping the inferior. ARGS may be in any of the
13560 forms defined in extract_bp_number_and_location. */
c906108c 13561
c906108c 13562static void
981a3fb3 13563enable_command (const char *args, int from_tty)
c906108c 13564{
d0fe4701 13565 enable_disable_command (args, from_tty, true);
c906108c
SS
13566}
13567
c906108c 13568static void
4495129a 13569enable_once_command (const char *args, int from_tty)
c906108c 13570{
48649e1b
TT
13571 map_breakpoint_numbers
13572 (args, [&] (breakpoint *b)
13573 {
13574 iterate_over_related_breakpoints
13575 (b, [&] (breakpoint *bpt)
13576 {
13577 enable_breakpoint_disp (bpt, disp_disable, 1);
13578 });
13579 });
816338b5
SS
13580}
13581
13582static void
4495129a 13583enable_count_command (const char *args, int from_tty)
816338b5 13584{
b9d61307
SM
13585 int count;
13586
13587 if (args == NULL)
13588 error_no_arg (_("hit count"));
13589
13590 count = get_number (&args);
816338b5 13591
48649e1b
TT
13592 map_breakpoint_numbers
13593 (args, [&] (breakpoint *b)
13594 {
13595 iterate_over_related_breakpoints
13596 (b, [&] (breakpoint *bpt)
13597 {
13598 enable_breakpoint_disp (bpt, disp_disable, count);
13599 });
13600 });
c906108c
SS
13601}
13602
c906108c 13603static void
4495129a 13604enable_delete_command (const char *args, int from_tty)
c906108c 13605{
48649e1b
TT
13606 map_breakpoint_numbers
13607 (args, [&] (breakpoint *b)
13608 {
13609 iterate_over_related_breakpoints
13610 (b, [&] (breakpoint *bpt)
13611 {
13612 enable_breakpoint_disp (bpt, disp_del, 1);
13613 });
13614 });
c906108c
SS
13615}
13616\f
1f3b5d1b
PP
13617/* Invalidate last known value of any hardware watchpoint if
13618 the memory which that value represents has been written to by
13619 GDB itself. */
13620
13621static void
8de0566d
YQ
13622invalidate_bp_value_on_memory_change (struct inferior *inferior,
13623 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
13624 const bfd_byte *data)
13625{
43892fdf 13626 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 13627 if (bp->enable_state == bp_enabled
3a5c3e22 13628 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 13629 {
3a5c3e22 13630 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 13631
850645cf 13632 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 13633 {
40cb8ca5 13634 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
13635 if (loc->loc_type == bp_loc_hardware_watchpoint
13636 && loc->address + loc->length > addr
13637 && addr + len > loc->address)
13638 {
3a5c3e22 13639 wp->val = NULL;
4c1d86d9 13640 wp->val_valid = false;
3a5c3e22
PA
13641 }
13642 }
1f3b5d1b
PP
13643 }
13644}
13645
8181d85f
DJ
13646/* Create and insert a breakpoint for software single step. */
13647
13648void
6c95b8df 13649insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 13650 const address_space *aspace,
4a64f543 13651 CORE_ADDR next_pc)
8181d85f 13652{
7c16b83e
PA
13653 struct thread_info *tp = inferior_thread ();
13654 struct symtab_and_line sal;
13655 CORE_ADDR pc = next_pc;
8181d85f 13656
34b7e8a6
PA
13657 if (tp->control.single_step_breakpoints == NULL)
13658 {
960bc2bd 13659 std::unique_ptr<breakpoint> b
7ab97995
PA
13660 (new momentary_breakpoint (gdbarch, bp_single_step,
13661 current_program_space,
13662 null_frame_id,
13663 tp->global_num));
960bc2bd 13664
34b7e8a6 13665 tp->control.single_step_breakpoints
960bc2bd 13666 = add_to_breakpoint_chain (std::move (b));
34b7e8a6 13667 }
8181d85f 13668
7c16b83e
PA
13669 sal = find_pc_line (pc, 0);
13670 sal.pc = pc;
13671 sal.section = find_pc_overlay (pc);
13672 sal.explicit_pc = 1;
960bc2bd
PA
13673
13674 auto *ss_bp
98ed24fb
TT
13675 = (gdb::checked_static_cast<momentary_breakpoint *>
13676 (tp->control.single_step_breakpoints));
960bc2bd 13677 ss_bp->add_location (sal);
8181d85f 13678
7c16b83e 13679 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
13680}
13681
93f9a11f
YQ
13682/* Insert single step breakpoints according to the current state. */
13683
13684int
13685insert_single_step_breakpoints (struct gdbarch *gdbarch)
13686{
f5ea389a 13687 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 13688 std::vector<CORE_ADDR> next_pcs;
93f9a11f 13689
f5ea389a 13690 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 13691
a0ff9e1a 13692 if (!next_pcs.empty ())
93f9a11f 13693 {
bd2b40ac 13694 frame_info_ptr frame = get_current_frame ();
8b86c959 13695 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 13696
a0ff9e1a 13697 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
13698 insert_single_step_breakpoint (gdbarch, aspace, pc);
13699
93f9a11f
YQ
13700 return 1;
13701 }
13702 else
13703 return 0;
13704}
13705
34b7e8a6 13706/* See breakpoint.h. */
f02253f1
HZ
13707
13708int
7c16b83e 13709breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 13710 const address_space *aspace,
7c16b83e 13711 CORE_ADDR pc)
1aafd4da 13712{
40cb8ca5 13713 for (bp_location *loc : bp->locations ())
7c16b83e
PA
13714 if (loc->inserted
13715 && breakpoint_location_address_match (loc, aspace, pc))
13716 return 1;
1aafd4da 13717
7c16b83e 13718 return 0;
ef370185
JB
13719}
13720
13721/* Check whether a software single-step breakpoint is inserted at
13722 PC. */
13723
13724int
accd0bcd 13725single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
13726 CORE_ADDR pc)
13727{
43892fdf 13728 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
13729 {
13730 if (bpt->type == bp_single_step
13731 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13732 return 1;
13733 }
13734 return 0;
1aafd4da
UW
13735}
13736
1042e4c0
SS
13737/* Tracepoint-specific operations. */
13738
13739/* Set tracepoint count to NUM. */
13740static void
13741set_tracepoint_count (int num)
13742{
13743 tracepoint_count = num;
4fa62494 13744 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
13745}
13746
70221824 13747static void
0b39b52e 13748trace_command (const char *arg, int from_tty)
1042e4c0 13749{
264f9890
PA
13750 location_spec_up locspec = string_to_location_spec (&arg,
13751 current_language);
13752 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13753 (locspec.get (), true /* is_tracepoint */);
55aa24fb 13754
558a9d82 13755 create_breakpoint (get_current_arch (),
264f9890 13756 locspec.get (),
10a636cc 13757 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13758 0 /* tempflag */,
13759 bp_tracepoint /* type_wanted */,
13760 0 /* Ignore count */,
13761 pending_break_support,
13762 ops,
13763 from_tty,
13764 1 /* enabled */,
13765 0 /* internal */, 0);
1042e4c0
SS
13766}
13767
70221824 13768static void
0b39b52e 13769ftrace_command (const char *arg, int from_tty)
7a697b8d 13770{
264f9890
PA
13771 location_spec_up locspec = string_to_location_spec (&arg,
13772 current_language);
558a9d82 13773 create_breakpoint (get_current_arch (),
264f9890 13774 locspec.get (),
10a636cc 13775 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13776 0 /* tempflag */,
13777 bp_fast_tracepoint /* type_wanted */,
13778 0 /* Ignore count */,
13779 pending_break_support,
74421c0b 13780 &code_breakpoint_ops,
558a9d82
YQ
13781 from_tty,
13782 1 /* enabled */,
13783 0 /* internal */, 0);
0fb4aa4b
PA
13784}
13785
13786/* strace command implementation. Creates a static tracepoint. */
13787
70221824 13788static void
0b39b52e 13789strace_command (const char *arg, int from_tty)
0fb4aa4b 13790{
a678887d 13791 const struct breakpoint_ops *ops;
264f9890 13792 location_spec_up locspec;
7b572efb 13793 enum bptype type;
983af33b
SDJ
13794
13795 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13796 or with a normal static tracepoint. */
61012eef 13797 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
13798 {
13799 ops = &strace_marker_breakpoint_ops;
264f9890
PA
13800 locspec = new_linespec_location_spec (&arg,
13801 symbol_name_match_type::FULL);
7b572efb 13802 type = bp_static_marker_tracepoint;
f00aae0f 13803 }
983af33b 13804 else
f00aae0f 13805 {
74421c0b 13806 ops = &code_breakpoint_ops;
264f9890 13807 locspec = string_to_location_spec (&arg, current_language);
7b572efb 13808 type = bp_static_tracepoint;
f00aae0f 13809 }
983af33b 13810
558a9d82 13811 create_breakpoint (get_current_arch (),
264f9890 13812 locspec.get (),
10a636cc 13813 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82 13814 0 /* tempflag */,
7b572efb 13815 type /* type_wanted */,
558a9d82
YQ
13816 0 /* Ignore count */,
13817 pending_break_support,
13818 ops,
13819 from_tty,
13820 1 /* enabled */,
13821 0 /* internal */, 0);
7a697b8d
SS
13822}
13823
409873ef
SS
13824/* Set up a fake reader function that gets command lines from a linked
13825 list that was acquired during tracepoint uploading. */
13826
13827static struct uploaded_tp *this_utp;
3149d8c1 13828static int next_cmd;
409873ef 13829
f8631e5e
SM
13830static const char *
13831read_uploaded_action (std::string &buffer)
409873ef 13832{
a18ba4e4 13833 char *rslt = nullptr;
409873ef 13834
a18ba4e4
SM
13835 if (next_cmd < this_utp->cmd_strings.size ())
13836 {
67aa1f3c 13837 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
13838 next_cmd++;
13839 }
409873ef
SS
13840
13841 return rslt;
13842}
13843
00bf0b85
SS
13844/* Given information about a tracepoint as recorded on a target (which
13845 can be either a live system or a trace file), attempt to create an
13846 equivalent GDB tracepoint. This is not a reliable process, since
13847 the target does not necessarily have all the information used when
13848 the tracepoint was originally defined. */
13849
d9b3f62e 13850struct tracepoint *
00bf0b85 13851create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 13852{
f2fc3015
TT
13853 const char *addr_str;
13854 char small_buf[100];
d9b3f62e 13855 struct tracepoint *tp;
fd9b8c24 13856
409873ef 13857 if (utp->at_string)
67aa1f3c 13858 addr_str = utp->at_string.get ();
409873ef
SS
13859 else
13860 {
13861 /* In the absence of a source location, fall back to raw
13862 address. Since there is no way to confirm that the address
13863 means the same thing as when the trace was started, warn the
13864 user. */
3e43a32a
MS
13865 warning (_("Uploaded tracepoint %d has no "
13866 "source location, using raw address"),
409873ef 13867 utp->number);
8c042590 13868 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
13869 addr_str = small_buf;
13870 }
13871
13872 /* There's not much we can do with a sequence of bytecodes. */
13873 if (utp->cond && !utp->cond_string)
3e43a32a
MS
13874 warning (_("Uploaded tracepoint %d condition "
13875 "has no source form, ignoring it"),
409873ef 13876 utp->number);
d5551862 13877
264f9890
PA
13878 location_spec_up locspec = string_to_location_spec (&addr_str,
13879 current_language);
8cdf0e15 13880 if (!create_breakpoint (get_current_arch (),
264f9890 13881 locspec.get (),
67aa1f3c 13882 utp->cond_string.get (), -1, addr_str,
10a636cc 13883 false /* force_condition */,
e7e0cddf 13884 0 /* parse cond/thread */,
8cdf0e15 13885 0 /* tempflag */,
0fb4aa4b 13886 utp->type /* type_wanted */,
8cdf0e15
VP
13887 0 /* Ignore count */,
13888 pending_break_support,
74421c0b 13889 &code_breakpoint_ops,
8cdf0e15 13890 0 /* from_tty */,
84f4c1fe 13891 utp->enabled /* enabled */,
44f238bb
PA
13892 0 /* internal */,
13893 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 13894 return NULL;
fd9b8c24 13895
409873ef 13896 /* Get the tracepoint we just created. */
fd9b8c24
PA
13897 tp = get_tracepoint (tracepoint_count);
13898 gdb_assert (tp != NULL);
d5551862 13899
00bf0b85
SS
13900 if (utp->pass > 0)
13901 {
8c042590 13902 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 13903 tp->number);
00bf0b85 13904
409873ef 13905 trace_pass_command (small_buf, 0);
00bf0b85
SS
13906 }
13907
409873ef
SS
13908 /* If we have uploaded versions of the original commands, set up a
13909 special-purpose "reader" function and call the usual command line
13910 reader, then pass the result to the breakpoint command-setting
13911 function. */
a18ba4e4 13912 if (!utp->cmd_strings.empty ())
00bf0b85 13913 {
12973681 13914 counted_command_line cmd_list;
00bf0b85 13915
409873ef 13916 this_utp = utp;
3149d8c1 13917 next_cmd = 0;
d5551862 13918
60b3cef2 13919 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 13920
c1fc2657 13921 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 13922 }
a18ba4e4
SM
13923 else if (!utp->actions.empty ()
13924 || !utp->step_actions.empty ())
3e43a32a
MS
13925 warning (_("Uploaded tracepoint %d actions "
13926 "have no source form, ignoring them"),
409873ef 13927 utp->number);
00bf0b85 13928
f196051f 13929 /* Copy any status information that might be available. */
c1fc2657 13930 tp->hit_count = utp->hit_count;
f196051f
SS
13931 tp->traceframe_usage = utp->traceframe_usage;
13932
00bf0b85 13933 return tp;
d9b3f62e 13934}
00bf0b85 13935
1042e4c0
SS
13936/* Print information on tracepoint number TPNUM_EXP, or all if
13937 omitted. */
13938
13939static void
1d12d88f 13940info_tracepoints_command (const char *args, int from_tty)
1042e4c0 13941{
79a45e25 13942 struct ui_out *uiout = current_uiout;
e5a67952 13943 int num_printed;
1042e4c0 13944
5c458ae8 13945 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
13946
13947 if (num_printed == 0)
1042e4c0 13948 {
e5a67952 13949 if (args == NULL || *args == '\0')
112e8700 13950 uiout->message ("No tracepoints.\n");
d77f58be 13951 else
112e8700 13952 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 13953 }
ad443146
SS
13954
13955 default_collect_info ();
1042e4c0
SS
13956}
13957
4a64f543 13958/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13959 Not supported by all targets. */
13960static void
5fed81ff 13961enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13962{
13963 enable_command (args, from_tty);
13964}
13965
4a64f543 13966/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13967 Not supported by all targets. */
13968static void
5fed81ff 13969disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13970{
13971 disable_command (args, from_tty);
13972}
13973
4a64f543 13974/* Remove a tracepoint (or all if no argument). */
1042e4c0 13975static void
4495129a 13976delete_trace_command (const char *arg, int from_tty)
1042e4c0 13977{
1042e4c0
SS
13978 dont_repeat ();
13979
13980 if (arg == 0)
13981 {
13982 int breaks_to_delete = 0;
13983
13984 /* Delete all breakpoints if no argument.
dda83cd7
SM
13985 Do not delete internal or call-dummy breakpoints, these
13986 have to be deleted with an explicit breakpoint number
4a64f543 13987 argument. */
f6d17b2b 13988 for (breakpoint *tp : all_tracepoints ())
1428b37a 13989 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
13990 {
13991 breaks_to_delete = 1;
13992 break;
13993 }
1042e4c0
SS
13994
13995 /* Ask user only if there are some breakpoints to delete. */
13996 if (!from_tty
13997 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13998 {
1428b37a 13999 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 14000 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14001 delete_breakpoint (b);
1042e4c0
SS
14002 }
14003 }
14004 else
48649e1b 14005 map_breakpoint_numbers
b926417a 14006 (arg, [&] (breakpoint *br)
48649e1b 14007 {
b926417a 14008 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14009 });
1042e4c0
SS
14010}
14011
197f0a60
TT
14012/* Helper function for trace_pass_command. */
14013
14014static void
d9b3f62e 14015trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14016{
d9b3f62e 14017 tp->pass_count = count;
76727919 14018 gdb::observers::breakpoint_modified.notify (tp);
197f0a60 14019 if (from_tty)
6cb06a8c
TT
14020 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14021 tp->number, count);
197f0a60
TT
14022}
14023
1042e4c0
SS
14024/* Set passcount for tracepoint.
14025
14026 First command argument is passcount, second is tracepoint number.
14027 If tracepoint number omitted, apply to most recently defined.
14028 Also accepts special argument "all". */
14029
14030static void
0b39b52e 14031trace_pass_command (const char *args, int from_tty)
1042e4c0 14032{
d9b3f62e 14033 struct tracepoint *t1;
0b39b52e 14034 ULONGEST count;
1042e4c0
SS
14035
14036 if (args == 0 || *args == 0)
3e43a32a
MS
14037 error (_("passcount command requires an "
14038 "argument (count + optional TP num)"));
1042e4c0 14039
0b39b52e 14040 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14041
529480d0 14042 args = skip_spaces (args);
1042e4c0
SS
14043 if (*args && strncasecmp (args, "all", 3) == 0)
14044 {
14045 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14046 if (*args)
14047 error (_("Junk at end of arguments."));
1042e4c0 14048
f6d17b2b 14049 for (breakpoint *b : all_tracepoints ())
01add95b
SM
14050 {
14051 t1 = (struct tracepoint *) b;
14052 trace_pass_set_count (t1, count, from_tty);
14053 }
197f0a60
TT
14054 }
14055 else if (*args == '\0')
1042e4c0 14056 {
5fa1d40e 14057 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14058 if (t1)
197f0a60
TT
14059 trace_pass_set_count (t1, count, from_tty);
14060 }
14061 else
14062 {
bfd28288
PA
14063 number_or_range_parser parser (args);
14064 while (!parser.finished ())
1042e4c0 14065 {
bfd28288 14066 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14067 if (t1)
14068 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14069 }
14070 }
1042e4c0
SS
14071}
14072
d9b3f62e 14073struct tracepoint *
1042e4c0
SS
14074get_tracepoint (int num)
14075{
f6d17b2b 14076 for (breakpoint *t : all_tracepoints ())
1042e4c0 14077 if (t->number == num)
d9b3f62e 14078 return (struct tracepoint *) t;
1042e4c0
SS
14079
14080 return NULL;
14081}
14082
d5551862
SS
14083/* Find the tracepoint with the given target-side number (which may be
14084 different from the tracepoint number after disconnecting and
14085 reconnecting). */
14086
d9b3f62e 14087struct tracepoint *
d5551862
SS
14088get_tracepoint_by_number_on_target (int num)
14089{
f6d17b2b 14090 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
14091 {
14092 struct tracepoint *t = (struct tracepoint *) b;
14093
14094 if (t->number_on_target == num)
14095 return t;
14096 }
d5551862
SS
14097
14098 return NULL;
14099}
14100
1042e4c0 14101/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14102 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14103 If the argument is missing, the most recent tracepoint
14104 (tracepoint_count) is returned. */
14105
d9b3f62e 14106struct tracepoint *
0b39b52e 14107get_tracepoint_by_number (const char **arg,
bfd28288 14108 number_or_range_parser *parser)
1042e4c0 14109{
1042e4c0 14110 int tpnum;
0b39b52e 14111 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14112
bfd28288 14113 if (parser != NULL)
197f0a60 14114 {
bfd28288
PA
14115 gdb_assert (!parser->finished ());
14116 tpnum = parser->get_number ();
197f0a60
TT
14117 }
14118 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14119 tpnum = tracepoint_count;
1042e4c0 14120 else
197f0a60 14121 tpnum = get_number (arg);
1042e4c0
SS
14122
14123 if (tpnum <= 0)
14124 {
14125 if (instring && *instring)
6cb06a8c
TT
14126 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14127 instring);
1042e4c0 14128 else
6cb06a8c 14129 gdb_printf (_("No previous tracepoint\n"));
1042e4c0
SS
14130 return NULL;
14131 }
14132
f6d17b2b 14133 for (breakpoint *t : all_tracepoints ())
1042e4c0 14134 if (t->number == tpnum)
d9b3f62e 14135 return (struct tracepoint *) t;
1042e4c0 14136
6cb06a8c 14137 gdb_printf ("No tracepoint number %d.\n", tpnum);
1042e4c0
SS
14138 return NULL;
14139}
14140
d9b3f62e 14141void
04d0163c 14142breakpoint::print_recreate_thread (struct ui_file *fp) const
d9b3f62e 14143{
04d0163c
TT
14144 if (thread != -1)
14145 gdb_printf (fp, " thread %d", thread);
d9b3f62e 14146
2ecee236 14147 if (task != -1)
04d0163c 14148 gdb_printf (fp, " task %d", task);
d9b3f62e 14149
6cb06a8c 14150 gdb_printf (fp, "\n");
d9b3f62e
PA
14151}
14152
6149aea9
PA
14153/* Save information on user settable breakpoints (watchpoints, etc) to
14154 a new script file named FILENAME. If FILTER is non-NULL, call it
14155 on each breakpoint and only include the ones for which it returns
f2478a7e 14156 true. */
6149aea9 14157
1042e4c0 14158static void
4495129a 14159save_breakpoints (const char *filename, int from_tty,
f2478a7e 14160 bool (*filter) (const struct breakpoint *))
1042e4c0 14161{
d8de7963 14162 bool any = false;
6149aea9 14163 int extra_trace_bits = 0;
1042e4c0 14164
6149aea9
PA
14165 if (filename == 0 || *filename == 0)
14166 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14167
14168 /* See if we have anything to save. */
43892fdf 14169 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14170 {
14171 /* Skip internal and momentary breakpoints. */
14172 if (!user_breakpoint_p (tp))
14173 continue;
6149aea9 14174
01add95b
SM
14175 /* If we have a filter, only save the breakpoints it accepts. */
14176 if (filter && !filter (tp))
14177 continue;
6149aea9 14178
d8de7963 14179 any = true;
6149aea9 14180
01add95b
SM
14181 if (is_tracepoint (tp))
14182 {
14183 extra_trace_bits = 1;
6149aea9 14184
01add95b
SM
14185 /* We can stop searching. */
14186 break;
14187 }
14188 }
6149aea9
PA
14189
14190 if (!any)
1042e4c0 14191 {
6149aea9 14192 warning (_("Nothing to save."));
1042e4c0
SS
14193 return;
14194 }
14195
ee0c3293 14196 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14197
14198 stdio_file fp;
14199
ee0c3293 14200 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14201 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14202 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14203
6149aea9 14204 if (extra_trace_bits)
d7e74731 14205 save_trace_state_variables (&fp);
8bf6485c 14206
43892fdf 14207 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14208 {
14209 /* Skip internal and momentary breakpoints. */
14210 if (!user_breakpoint_p (tp))
14211 continue;
8bf6485c 14212
01add95b
SM
14213 /* If we have a filter, only save the breakpoints it accepts. */
14214 if (filter && !filter (tp))
14215 continue;
6149aea9 14216
c47614fe 14217 tp->print_recreate (&fp);
1042e4c0 14218
01add95b
SM
14219 /* Note, we can't rely on tp->number for anything, as we can't
14220 assume the recreated breakpoint numbers will match. Use $bpnum
14221 instead. */
6149aea9 14222
01add95b 14223 if (tp->cond_string)
6f781ee3 14224 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
6149aea9 14225
01add95b
SM
14226 if (tp->ignore_count)
14227 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14228
01add95b
SM
14229 if (tp->type != bp_dprintf && tp->commands)
14230 {
14231 fp.puts (" commands\n");
1042e4c0 14232
992aeed8
TT
14233 ui_out_redirect_pop redir (current_uiout, &fp);
14234 print_command_lines (current_uiout, tp->commands.get (), 2);
6149aea9 14235
01add95b
SM
14236 fp.puts (" end\n");
14237 }
6149aea9 14238
01add95b
SM
14239 if (tp->enable_state == bp_disabled)
14240 fp.puts ("disable $bpnum\n");
6149aea9 14241
01add95b
SM
14242 /* If this is a multi-location breakpoint, check if the locations
14243 should be individually disabled. Watchpoint locations are
14244 special, and not user visible. */
14245 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14246 {
14247 int n = 1;
40cb8ca5 14248
01add95b
SM
14249 for (bp_location *loc : tp->locations ())
14250 {
14251 if (!loc->enabled)
14252 fp.printf ("disable $bpnum.%d\n", n);
14253
14254 n++;
14255 }
14256 }
14257 }
8bf6485c 14258
e0700ba4
SM
14259 if (extra_trace_bits && !default_collect.empty ())
14260 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14261
1042e4c0 14262 if (from_tty)
6cb06a8c 14263 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14264}
14265
14266/* The `save breakpoints' command. */
14267
14268static void
4495129a 14269save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14270{
14271 save_breakpoints (args, from_tty, NULL);
14272}
14273
14274/* The `save tracepoints' command. */
14275
14276static void
4495129a 14277save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14278{
14279 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14280}
14281
c906108c 14282\f
629500fa
KS
14283/* This help string is used to consolidate all the help string for specifying
14284 locations used by several commands. */
14285
264f9890 14286#define LOCATION_SPEC_HELP_STRING \
629500fa
KS
14287"Linespecs are colon-separated lists of location parameters, such as\n\
14288source filename, function name, label name, and line number.\n\
14289Example: To specify the start of a label named \"the_top\" in the\n\
14290function \"fact\" in the file \"factorial.c\", use\n\
14291\"factorial.c:fact:the_top\".\n\
14292\n\
14293Address locations begin with \"*\" and specify an exact address in the\n\
14294program. Example: To specify the fourth byte past the start function\n\
14295\"main\", use \"*main + 4\".\n\
14296\n\
14297Explicit locations are similar to linespecs but use an option/argument\n\
14298syntax to specify location parameters.\n\
14299Example: To specify the start of the label named \"the_top\" in the\n\
14300function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14301-function fact -label the_top\".\n\
14302\n\
14303By default, a specified function is matched against the program's\n\
14304functions in all scopes. For C++, this means in all namespaces and\n\
14305classes. For Ada, this means in all packages. E.g., in C++,\n\
14306\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14307\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14308specified name as a complete fully-qualified name instead."
629500fa 14309
4a64f543
MS
14310/* This help string is used for the break, hbreak, tbreak and thbreak
14311 commands. It is defined as a macro to prevent duplication.
14312 COMMAND should be a string constant containing the name of the
14313 command. */
629500fa 14314
31e2b00f 14315#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14316command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14317\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14318PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14319probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
14320guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14321`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14322LOCATION may be a linespec, address, or explicit location as described\n\
14323below.\n\
14324\n\
dc10affe
PA
14325With no LOCATION, uses current execution address of the selected\n\
14326stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14327\n\
14328THREADNUM is the number from \"info threads\".\n\
14329CONDITION is a boolean expression.\n\
733d554a
TBA
14330\n\
14331With the \"-force-condition\" flag, the condition is defined even when\n\
14332it is invalid for all current locations.\n\
264f9890 14333\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14334Multiple breakpoints at one place are permitted, and useful if their\n\
14335conditions are different.\n\
31e2b00f
AS
14336\n\
14337Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14338
44feb3ce
TT
14339/* List of subcommands for "catch". */
14340static struct cmd_list_element *catch_cmdlist;
14341
14342/* List of subcommands for "tcatch". */
14343static struct cmd_list_element *tcatch_cmdlist;
14344
9ac4176b 14345void
a121b7c1 14346add_catch_command (const char *name, const char *docstring,
5538b03c 14347 cmd_func_ftype *func,
625e8578 14348 completer_ftype *completer,
44feb3ce
TT
14349 void *user_data_catch,
14350 void *user_data_tcatch)
14351{
14352 struct cmd_list_element *command;
14353
0450cc4c 14354 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14355 &catch_cmdlist);
5538b03c 14356 command->func = func;
0f8e2034 14357 command->set_context (user_data_catch);
a96d9b2e 14358 set_cmd_completer (command, completer);
44feb3ce 14359
0450cc4c 14360 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14361 &tcatch_cmdlist);
5538b03c 14362 command->func = func;
0f8e2034 14363 command->set_context (user_data_tcatch);
a96d9b2e 14364 set_cmd_completer (command, completer);
44feb3ce
TT
14365}
14366
d8de7963
AB
14367/* False if any of the breakpoint's locations could be a location where
14368 functions have been inlined, true otherwise. */
0574c78f 14369
d8de7963 14370static bool
0574c78f
GB
14371is_non_inline_function (struct breakpoint *b)
14372{
14373 /* The shared library event breakpoint is set on the address of a
14374 non-inline function. */
d8de7963 14375 return (b->type == bp_shlib_event);
0574c78f
GB
14376}
14377
14378/* Nonzero if the specified PC cannot be a location where functions
14379 have been inlined. */
14380
14381int
accd0bcd 14382pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14383 const target_waitstatus &ws)
0574c78f 14384{
43892fdf 14385 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
14386 {
14387 if (!is_non_inline_function (b))
14388 continue;
14389
40cb8ca5 14390 for (bp_location *bl : b->locations ())
0574c78f
GB
14391 {
14392 if (!bl->shlib_disabled
09ac7c10 14393 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14394 return 1;
14395 }
14396 }
14397
14398 return 0;
14399}
14400
2f202fde
JK
14401/* Remove any references to OBJFILE which is going to be freed. */
14402
14403void
14404breakpoint_free_objfile (struct objfile *objfile)
14405{
48d7020b 14406 for (bp_location *loc : all_bp_locations ())
3c86fae3 14407 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
2f202fde
JK
14408 loc->symtab = NULL;
14409}
14410
8bfd80db
YQ
14411/* Chain containing all defined "enable breakpoint" subcommands. */
14412
14413static struct cmd_list_element *enablebreaklist = NULL;
14414
8588b356
SM
14415/* See breakpoint.h. */
14416
14417cmd_list_element *commands_cmd_element = nullptr;
14418
6c265988 14419void _initialize_breakpoint ();
c906108c 14420void
6c265988 14421_initialize_breakpoint ()
c906108c
SS
14422{
14423 struct cmd_list_element *c;
14424
c90e7d63
SM
14425 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14426 "breakpoint");
14427 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14428 "breakpoint");
14429 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14430 "breakpoint");
84acb35a 14431
c906108c
SS
14432 breakpoint_chain = 0;
14433 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14434 before a breakpoint is set. */
14435 breakpoint_count = 0;
14436
1042e4c0
SS
14437 tracepoint_count = 0;
14438
1bedd215
AC
14439 add_com ("ignore", class_breakpoint, ignore_command, _("\
14440Set ignore-count of breakpoint number N to COUNT.\n\
14441Usage is `ignore N COUNT'."));
c906108c 14442
8588b356
SM
14443 commands_cmd_element = add_com ("commands", class_breakpoint,
14444 commands_command, _("\
18da0c51
MG
14445Set commands to be executed when the given breakpoints are hit.\n\
14446Give a space-separated breakpoint list as argument after \"commands\".\n\
14447A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14448(e.g. `5-7').\n\
c906108c
SS
14449With no argument, the targeted breakpoint is the last one set.\n\
14450The commands themselves follow starting on the next line.\n\
14451Type a line containing \"end\" to indicate the end of them.\n\
14452Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14453then no output is printed when it is hit, except what the commands print."));
c906108c 14454
b1d4d8d1
TBA
14455 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14456 static std::string condition_command_help
14457 = gdb::option::build_help (_("\
1bedd215 14458Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14459Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14460is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14461\n\
14462Options:\n\
14463%OPTIONS%"), cc_opts);
14464
14465 c = add_com ("condition", class_breakpoint, condition_command,
14466 condition_command_help.c_str ());
14467 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14468
1bedd215 14469 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14470Set a temporary breakpoint.\n\
c906108c
SS
14471Like \"break\" except the breakpoint is only temporary,\n\
14472so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14473by using \"enable delete\" on the breakpoint number.\n\
14474\n"
14475BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14476 set_cmd_completer (c, location_completer);
c94fdfd0 14477
1bedd215 14478 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14479Set a hardware assisted breakpoint.\n\
c906108c 14480Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14481some target hardware may not have this support.\n\
14482\n"
14483BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14484 set_cmd_completer (c, location_completer);
c906108c 14485
1bedd215 14486 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14487Set a temporary hardware assisted breakpoint.\n\
c906108c 14488Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14489so it will be deleted when hit.\n\
14490\n"
14491BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14492 set_cmd_completer (c, location_completer);
c906108c 14493
3947f654
SM
14494 cmd_list_element *enable_cmd
14495 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14496Enable all or some breakpoints.\n\
14497Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14498Give breakpoint numbers (separated by spaces) as arguments.\n\
14499With no subcommand, breakpoints are enabled until you command otherwise.\n\
14500This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14501With a subcommand you can enable temporarily."),
3947f654 14502 &enablelist, 1, &cmdlist);
c906108c 14503
3947f654 14504 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14505
84951ab5 14506 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14507Enable all or some breakpoints.\n\
14508Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14509Give breakpoint numbers (separated by spaces) as arguments.\n\
14510This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14511May be abbreviated to simply \"enable\"."),
2f822da5 14512 &enablebreaklist, 1, &enablelist);
c906108c 14513
1a966eab 14514 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14515Enable some breakpoints for one hit.\n\
14516Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14517If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14518 &enablebreaklist);
14519
1a966eab 14520 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14521Enable some breakpoints and delete when hit.\n\
14522Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14523If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14524 &enablebreaklist);
14525
816338b5 14526 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14527Enable some breakpoints for COUNT hits.\n\
14528Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14529If a breakpoint is hit while enabled in this fashion,\n\
14530the count is decremented; when it reaches zero, the breakpoint is disabled."),
14531 &enablebreaklist);
14532
1a966eab 14533 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14534Enable some breakpoints and delete when hit.\n\
14535Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14536If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14537 &enablelist);
14538
1a966eab 14539 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14540Enable some breakpoints for one hit.\n\
14541Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14542If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14543 &enablelist);
14544
14545 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14546Enable some breakpoints for COUNT hits.\n\
14547Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14548If a breakpoint is hit while enabled in this fashion,\n\
14549the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14550 &enablelist);
14551
3947f654
SM
14552 cmd_list_element *disable_cmd
14553 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14554Disable all or some breakpoints.\n\
14555Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14556Arguments are breakpoint numbers with spaces in between.\n\
14557To disable all breakpoints, give no argument.\n\
64b9b334 14558A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14559 &disablelist, 1, &cmdlist);
14560 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14561 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14562
57b4f16e 14563 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14564Disable all or some breakpoints.\n\
14565Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14566Arguments are breakpoint numbers with spaces in between.\n\
14567To disable all breakpoints, give no argument.\n\
64b9b334 14568A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14569This command may be abbreviated \"disable\"."),
c906108c
SS
14570 &disablelist);
14571
3947f654
SM
14572 cmd_list_element *delete_cmd
14573 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14574Delete all or some breakpoints.\n\
14575Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
14576Arguments are breakpoint numbers with spaces in between.\n\
14577To delete all breakpoints, give no argument.\n\
14578\n\
590042fc 14579Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14580 &deletelist, 1, &cmdlist);
14581 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14582 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14583
57b4f16e 14584 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14585Delete all or some breakpoints or auto-display expressions.\n\
14586Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14587Arguments are breakpoint numbers with spaces in between.\n\
14588To delete all breakpoints, give no argument.\n\
1a966eab 14589This command may be abbreviated \"delete\"."),
c906108c
SS
14590 &deletelist);
14591
3947f654
SM
14592 cmd_list_element *clear_cmd
14593 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14594Clear breakpoint at specified location.\n\
14595Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14596\n\
14597With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14598is executing in.\n"
264f9890 14599"\n" LOCATION_SPEC_HELP_STRING "\n\n\
1bedd215 14600See also the \"delete\" command which clears breakpoints by number."));
3947f654 14601 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14602
3947f654
SM
14603 cmd_list_element *break_cmd
14604 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14605Set breakpoint at specified location.\n"
31e2b00f 14606BREAK_ARGS_HELP ("break")));
3947f654 14607 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14608
3947f654
SM
14609 add_com_alias ("b", break_cmd, class_run, 1);
14610 add_com_alias ("br", break_cmd, class_run, 1);
14611 add_com_alias ("bre", break_cmd, class_run, 1);
14612 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14613
e0f25bd9
SM
14614 cmd_list_element *info_breakpoints_cmd
14615 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14616Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14617The \"Type\" column indicates one of:\n\
14618\tbreakpoint - normal breakpoint\n\
14619\twatchpoint - watchpoint\n\
14620The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14621the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14622breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14623address and file/line number respectively.\n\
14624\n\
14625Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14626are set to the address of the last breakpoint listed unless the command\n\
14627is prefixed with \"server \".\n\n\
c906108c 14628Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14629breakpoint set."));
c906108c 14630
e0f25bd9 14631 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14632
1a966eab
AC
14633 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14634Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14635The \"Type\" column indicates one of:\n\
14636\tbreakpoint - normal breakpoint\n\
14637\twatchpoint - watchpoint\n\
14638\tlongjmp - internal breakpoint used to step through longjmp()\n\
14639\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14640\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14641\tfinish - internal breakpoint used by the \"finish\" command\n\
14642The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14643the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14644breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14645address and file/line number respectively.\n\
14646\n\
14647Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14648are set to the address of the last breakpoint listed unless the command\n\
14649is prefixed with \"server \".\n\n\
c906108c 14650Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14651breakpoint set."),
c906108c
SS
14652 &maintenanceinfolist);
14653
3b6acaee 14654 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 14655Set catchpoints to catch events."),
2f822da5 14656 &catch_cmdlist,
3b6acaee 14657 0/*allow-unknown*/, &cmdlist);
44feb3ce 14658
3b6acaee 14659 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 14660Set temporary catchpoints to catch events."),
2f822da5 14661 &tcatch_cmdlist,
3b6acaee 14662 0/*allow-unknown*/, &cmdlist);
44feb3ce 14663
a15a5258
AB
14664 const auto opts = make_watch_options_def_group (nullptr);
14665
14666 static const std::string watch_help = gdb::option::build_help (_("\
14667Set a watchpoint for EXPRESSION.\n\
14668Usage: watch [-location] EXPRESSION\n\
14669\n\
14670Options:\n\
14671%OPTIONS%\n\
14672\n\
c906108c 14673A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
14674an expression changes."), opts);
14675 c = add_com ("watch", class_breakpoint, watch_command,
14676 watch_help.c_str ());
14677 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14678
14679 static const std::string rwatch_help = gdb::option::build_help (_("\
14680Set a read watchpoint for EXPRESSION.\n\
14681Usage: rwatch [-location] EXPRESSION\n\
14682\n\
14683Options:\n\
14684%OPTIONS%\n\
14685\n\
14686A read watchpoint stops execution of your program whenever the value of\n\
14687an expression is read."), opts);
14688 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14689 rwatch_help.c_str ());
14690 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14691
14692 static const std::string awatch_help = gdb::option::build_help (_("\
14693Set an access watchpoint for EXPRESSION.\n\
14694Usage: awatch [-location] EXPRESSION\n\
14695\n\
14696Options:\n\
14697%OPTIONS%\n\
14698\n\
14699An access watchpoint stops execution of your program whenever the value\n\
14700of an expression is either read or written."), opts);
14701 c = add_com ("awatch", class_breakpoint, awatch_command,
14702 awatch_help.c_str ());
14703 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 14704
11db9430 14705 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 14706Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14707
920d2a44
AC
14708 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14709 respond to changes - contrary to the description. */
85c07804
AC
14710 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14711 &can_use_hw_watchpoints, _("\
14712Set debugger's willingness to use watchpoint hardware."), _("\
14713Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14714If zero, gdb will not use hardware for new watchpoints, even if\n\
14715such is available. (However, any hardware watchpoints that were\n\
14716created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14717hardware.)"),
14718 NULL,
920d2a44 14719 show_can_use_hw_watchpoints,
85c07804 14720 &setlist, &showlist);
c906108c
SS
14721
14722 can_use_hw_watchpoints = 1;
fa8d40ab 14723
1042e4c0
SS
14724 /* Tracepoint manipulation commands. */
14725
3947f654
SM
14726 cmd_list_element *trace_cmd
14727 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 14728Set a tracepoint at specified location.\n\
1042e4c0
SS
14729\n"
14730BREAK_ARGS_HELP ("trace") "\n\
14731Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 14732 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 14733
3947f654
SM
14734 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14735 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14736 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14737 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 14738
7a697b8d 14739 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 14740Set a fast tracepoint at specified location.\n\
7a697b8d
SS
14741\n"
14742BREAK_ARGS_HELP ("ftrace") "\n\
14743Do \"help tracepoints\" for info on other tracepoint commands."));
14744 set_cmd_completer (c, location_completer);
14745
0fb4aa4b 14746 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 14747Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
14748\n\
14749strace [LOCATION] [if CONDITION]\n\
629500fa
KS
14750LOCATION may be a linespec, explicit, or address location (described below) \n\
14751or -m MARKER_ID.\n\n\
14752If a marker id is specified, probe the marker with that name. With\n\
14753no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
14754Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14755This collects arbitrary user data passed in the probe point call to the\n\
14756tracing library. You can inspect it when analyzing the trace buffer,\n\
14757by printing the $_sdata variable like any other convenience variable.\n\
14758\n\
14759CONDITION is a boolean expression.\n\
264f9890 14760\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14761Multiple tracepoints at one place are permitted, and useful if their\n\
14762conditions are different.\n\
0fb4aa4b
PA
14763\n\
14764Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14765Do \"help tracepoints\" for info on other tracepoint commands."));
14766 set_cmd_completer (c, location_completer);
14767
e0f25bd9
SM
14768 cmd_list_element *info_tracepoints_cmd
14769 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 14770Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14771Convenience variable \"$tpnum\" contains the number of the\n\
14772last tracepoint set."));
14773
e0f25bd9 14774 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 14775
5e84b7ee
SM
14776 cmd_list_element *delete_tracepoints_cmd
14777 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
14778Delete specified tracepoints.\n\
14779Arguments are tracepoint numbers, separated by spaces.\n\
14780No argument means delete all tracepoints."),
14781 &deletelist);
5e84b7ee 14782 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
14783
14784 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14785Disable specified tracepoints.\n\
14786Arguments are tracepoint numbers, separated by spaces.\n\
14787No argument means disable all tracepoints."),
14788 &disablelist);
14789 deprecate_cmd (c, "disable");
14790
14791 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14792Enable specified tracepoints.\n\
14793Arguments are tracepoint numbers, separated by spaces.\n\
14794No argument means enable all tracepoints."),
14795 &enablelist);
14796 deprecate_cmd (c, "enable");
14797
14798 add_com ("passcount", class_trace, trace_pass_command, _("\
14799Set the passcount for a tracepoint.\n\
14800The trace will end when the tracepoint has been passed 'count' times.\n\
14801Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14802if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14803
0743fc83
TT
14804 add_basic_prefix_cmd ("save", class_breakpoint,
14805 _("Save breakpoint definitions as a script."),
2f822da5 14806 &save_cmdlist,
0743fc83 14807 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
14808
14809 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14810Save current breakpoint definitions as a script.\n\
cce7e648 14811This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14812catchpoints, tracepoints). Use the 'source' command in another debug\n\
14813session to restore them."),
14814 &save_cmdlist);
14815 set_cmd_completer (c, filename_completer);
14816
3947f654
SM
14817 cmd_list_element *save_tracepoints_cmd
14818 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14819Save current tracepoint definitions as a script.\n\
6149aea9
PA
14820Use the 'source' command in another debug session to restore them."),
14821 &save_cmdlist);
3947f654 14822 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 14823
3947f654 14824 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
14825 deprecate_cmd (c, "save tracepoints");
14826
f54bdb6d
SM
14827 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14828 _("\
590042fc 14829Breakpoint specific settings.\n\
fa8d40ab 14830Configure various breakpoint-specific variables such as\n\
590042fc 14831pending breakpoint behavior."),
f54bdb6d 14832 _("\
590042fc 14833Breakpoint specific settings.\n\
fa8d40ab 14834Configure various breakpoint-specific variables such as\n\
590042fc 14835pending breakpoint behavior."),
f54bdb6d
SM
14836 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14837 &setlist, &showlist);
fa8d40ab 14838
7915a72c
AC
14839 add_setshow_auto_boolean_cmd ("pending", no_class,
14840 &pending_break_support, _("\
14841Set debugger's behavior regarding pending breakpoints."), _("\
14842Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14843If on, an unrecognized breakpoint location will cause gdb to create a\n\
14844pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14845an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14846user-query to see if a pending breakpoint should be created."),
2c5b56ce 14847 NULL,
920d2a44 14848 show_pending_break_support,
6e1d7d6c
AC
14849 &breakpoint_set_cmdlist,
14850 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14851
14852 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14853
14854 add_setshow_boolean_cmd ("auto-hw", no_class,
14855 &automatic_hardware_breakpoints, _("\
14856Set automatic usage of hardware breakpoints."), _("\
14857Show automatic usage of hardware breakpoints."), _("\
14858If set, the debugger will automatically use hardware breakpoints for\n\
14859breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14860a warning will be emitted for such breakpoints."),
14861 NULL,
14862 show_automatic_hardware_breakpoints,
14863 &breakpoint_set_cmdlist,
14864 &breakpoint_show_cmdlist);
74960c60 14865
a25a5a45
PA
14866 add_setshow_boolean_cmd ("always-inserted", class_support,
14867 &always_inserted_mode, _("\
74960c60
VP
14868Set mode for inserting breakpoints."), _("\
14869Show mode for inserting breakpoints."), _("\
a25a5a45
PA
14870When this mode is on, breakpoints are inserted immediately as soon as\n\
14871they're created, kept inserted even when execution stops, and removed\n\
14872only when the user deletes them. When this mode is off (the default),\n\
14873breakpoints are inserted only when execution continues, and removed\n\
14874when execution stops."),
72d0e2c5
YQ
14875 NULL,
14876 &show_always_inserted_mode,
14877 &breakpoint_set_cmdlist,
14878 &breakpoint_show_cmdlist);
f1310107 14879
b775012e
LM
14880 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14881 condition_evaluation_enums,
14882 &condition_evaluation_mode_1, _("\
14883Set mode of breakpoint condition evaluation."), _("\
14884Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 14885When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
14886evaluated on the host's side by GDB. When it is set to \"target\",\n\
14887breakpoint conditions will be downloaded to the target (if the target\n\
14888supports such feature) and conditions will be evaluated on the target's side.\n\
14889If this is set to \"auto\" (default), this will be automatically set to\n\
14890\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 14891be set to \"host\"."),
b775012e
LM
14892 &set_condition_evaluation_mode,
14893 &show_condition_evaluation_mode,
14894 &breakpoint_set_cmdlist,
14895 &breakpoint_show_cmdlist);
14896
f1310107
TJB
14897 add_com ("break-range", class_breakpoint, break_range_command, _("\
14898Set a breakpoint for an address range.\n\
14899break-range START-LOCATION, END-LOCATION\n\
14900where START-LOCATION and END-LOCATION can be one of the following:\n\
14901 LINENUM, for that line in the current file,\n\
14902 FILE:LINENUM, for that line in that file,\n\
14903 +OFFSET, for that number of lines after the current line\n\
dda83cd7 14904 or the start of the range\n\
f1310107
TJB
14905 FUNCTION, for the first line in that function,\n\
14906 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14907 *ADDRESS, for the instruction at that address.\n\
14908\n\
14909The breakpoint will stop execution of the inferior whenever it executes\n\
14910an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14911range (including START-LOCATION and END-LOCATION)."));
14912
e7e0cddf 14913 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 14914Set a dynamic printf at specified location.\n\
e7e0cddf 14915dprintf location,format string,arg1,arg2,...\n\
629500fa 14916location may be a linespec, explicit, or address location.\n"
264f9890 14917"\n" LOCATION_SPEC_HELP_STRING));
e7e0cddf
SS
14918 set_cmd_completer (c, location_completer);
14919
14920 add_setshow_enum_cmd ("dprintf-style", class_support,
14921 dprintf_style_enums, &dprintf_style, _("\
14922Set the style of usage for dynamic printf."), _("\
14923Show the style of usage for dynamic printf."), _("\
14924This setting chooses how GDB will do a dynamic printf.\n\
14925If the value is \"gdb\", then the printing is done by GDB to its own\n\
14926console, as with the \"printf\" command.\n\
14927If the value is \"call\", the print is done by calling a function in your\n\
14928program; by default printf(), but you can choose a different function or\n\
14929output stream by setting dprintf-function and dprintf-channel."),
14930 update_dprintf_commands, NULL,
14931 &setlist, &showlist);
14932
e7e0cddf
SS
14933 add_setshow_string_cmd ("dprintf-function", class_support,
14934 &dprintf_function, _("\
590042fc
PW
14935Set the function to use for dynamic printf."), _("\
14936Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
14937 update_dprintf_commands, NULL,
14938 &setlist, &showlist);
14939
e7e0cddf
SS
14940 add_setshow_string_cmd ("dprintf-channel", class_support,
14941 &dprintf_channel, _("\
590042fc
PW
14942Set the channel to use for dynamic printf."), _("\
14943Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
14944 update_dprintf_commands, NULL,
14945 &setlist, &showlist);
14946
d3ce09f5
SS
14947 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14948 &disconnected_dprintf, _("\
14949Set whether dprintf continues after GDB disconnects."), _("\
14950Show whether dprintf continues after GDB disconnects."), _("\
14951Use this to let dprintf commands continue to hit and produce output\n\
14952even if GDB disconnects or detaches from the target."),
14953 NULL,
14954 NULL,
14955 &setlist, &showlist);
14956
14957 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
14958Target agent only formatted printing, like the C \"printf\" function.\n\
14959Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14960This supports most C printf format specifications, like %s, %d, etc.\n\
14961This is useful for formatted output in user-defined commands."));
d3ce09f5 14962
491144b5 14963 automatic_hardware_breakpoints = true;
f3b1572e 14964
c90e7d63
SM
14965 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14966 "breakpoint");
14967 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14968 "breakpoint");
c906108c 14969}