]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
asan: csky floatformat_to_double uninitialised value
[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 }
b1ffd112 2843 catch (gdb_exception_error &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
35df4500 2941 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2942 {
0000e5cc
PA
2943 int val;
2944
c47614fe 2945 val = bl->owner->insert_location (bl);
85d721b8
PA
2946
2947 /* If trying to set a read-watchpoint, and it turns out it's not
2948 supported, try emulating one with an access watchpoint. */
35df4500 2949 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2950 {
85d721b8
PA
2951 /* But don't try to insert it, if there's already another
2952 hw_access location that would be considered a duplicate
2953 of this one. */
48d7020b 2954 for (bp_location *loc : all_bp_locations ())
35df4500 2955 if (loc != bl
85d721b8 2956 && loc->watchpoint_type == hw_access
35df4500 2957 && watchpoint_locations_match (bl, loc))
85d721b8 2958 {
35df4500
TJB
2959 bl->duplicate = 1;
2960 bl->inserted = 1;
2961 bl->target_info = loc->target_info;
2962 bl->watchpoint_type = hw_access;
85d721b8
PA
2963 val = 0;
2964 break;
2965 }
2966
2967 if (val == 1)
2968 {
77b06cd7 2969 bl->watchpoint_type = hw_access;
c47614fe 2970 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2971
2972 if (val)
2973 /* Back to the original value. */
2974 bl->watchpoint_type = hw_read;
85d721b8
PA
2975 }
2976 }
2977
35df4500 2978 bl->inserted = (val == 0);
879bfdc2
DJ
2979 }
2980
35df4500 2981 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2982 {
0000e5cc
PA
2983 int val;
2984
c47614fe 2985 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2986 if (val)
2987 {
2988 bl->owner->enable_state = bp_disabled;
2989
2990 if (val == 1)
2991 warning (_("\
2992Error inserting catchpoint %d: Your system does not support this type\n\
2993of catchpoint."), bl->owner->number);
2994 else
2995 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2996 }
2997
2998 bl->inserted = (val == 0);
1640b821
DJ
2999
3000 /* We've already printed an error message if there was a problem
3001 inserting this catchpoint, and we've disabled the catchpoint,
3002 so just return success. */
3003 return 0;
879bfdc2
DJ
3004 }
3005
3006 return 0;
3007}
3008
6c95b8df
PA
3009/* This function is called when program space PSPACE is about to be
3010 deleted. It takes care of updating breakpoints to not reference
3011 PSPACE anymore. */
3012
3013void
3014breakpoint_program_space_exit (struct program_space *pspace)
3015{
6c95b8df 3016 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
3017 for (breakpoint *b : all_breakpoints_safe ())
3018 if (b->pspace == pspace)
3019 delete_breakpoint (b);
6c95b8df
PA
3020
3021 /* Breakpoints set through other program spaces could have locations
3022 bound to PSPACE as well. Remove those. */
48d7020b 3023 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
3024 {
3025 struct bp_location *tmp;
3026
3027 if (loc->pspace == pspace)
3028 {
2bdf28a0 3029 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3030 if (loc->owner->loc == loc)
3031 loc->owner->loc = loc->next;
3032 else
3033 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3034 if (tmp->next == loc)
3035 {
3036 tmp->next = loc->next;
3037 break;
3038 }
3039 }
3040 }
3041
3042 /* Now update the global location list to permanently delete the
3043 removed locations above. */
44702360 3044 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3045}
3046
74960c60
VP
3047/* Make sure all breakpoints are inserted in inferior.
3048 Throws exception on any error.
3049 A breakpoint that is already inserted won't be inserted
3050 again, so calling this function twice is safe. */
3051void
3052insert_breakpoints (void)
3053{
43892fdf 3054 for (breakpoint *bpt : all_breakpoints ())
74960c60 3055 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3056 {
3057 struct watchpoint *w = (struct watchpoint *) bpt;
3058
d8de7963 3059 update_watchpoint (w, false /* don't reparse. */);
3a5c3e22 3060 }
74960c60 3061
04086b45
PA
3062 /* Updating watchpoints creates new locations, so update the global
3063 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3064 ignore breakpoints_always_inserted_mode. Also,
3065 update_global_location_list tries to "upgrade" software
3066 breakpoints to hardware breakpoints to handle "set breakpoint
3067 auto-hw", so we need to call it even if we don't have new
3068 locations. */
04086b45 3069 update_global_location_list (UGLL_INSERT);
74960c60
VP
3070}
3071
b775012e
LM
3072/* This is used when we need to synch breakpoint conditions between GDB and the
3073 target. It is the case with deleting and disabling of breakpoints when using
3074 always-inserted mode. */
3075
3076static void
3077update_inserted_breakpoint_locations (void)
3078{
b775012e
LM
3079 int error_flag = 0;
3080 int val = 0;
3081 int disabled_breaks = 0;
3082 int hw_breakpoint_error = 0;
dd61ec5c 3083 int hw_bp_details_reported = 0;
b775012e 3084
d7e74731 3085 string_file tmp_error_stream;
b775012e
LM
3086
3087 /* Explicitly mark the warning -- this will only be printed if
3088 there was an error. */
d7e74731 3089 tmp_error_stream.puts ("Warning:\n");
b775012e 3090
5ed8105e 3091 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3092
48d7020b 3093 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3094 {
3095 /* We only want to update software breakpoints and hardware
3096 breakpoints. */
3097 if (!is_breakpoint (bl->owner))
3098 continue;
3099
3100 /* We only want to update locations that are already inserted
3101 and need updating. This is to avoid unwanted insertion during
3102 deletion of breakpoints. */
4daf1902 3103 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3104 continue;
3105
3106 switch_to_program_space_and_thread (bl->pspace);
3107
3108 /* For targets that support global breakpoints, there's no need
3109 to select an inferior to insert breakpoint to. In fact, even
3110 if we aren't attached to any process yet, we should still
3111 insert breakpoints. */
f5656ead 3112 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3113 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3114 continue;
3115
d7e74731 3116 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3117 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3118 if (val)
3119 error_flag = val;
3120 }
3121
3122 if (error_flag)
3123 {
223ffa71 3124 target_terminal::ours_for_output ();
b775012e
LM
3125 error_stream (tmp_error_stream);
3126 }
b775012e
LM
3127}
3128
c30eee59 3129/* Used when starting or continuing the program. */
c906108c 3130
74960c60
VP
3131static void
3132insert_breakpoint_locations (void)
c906108c 3133{
eacd795a 3134 int error_flag = 0;
c906108c 3135 int val = 0;
3fbb6ffa 3136 int disabled_breaks = 0;
81d0cc19 3137 int hw_breakpoint_error = 0;
dd61ec5c 3138 int hw_bp_error_explained_already = 0;
c906108c 3139
d7e74731
PA
3140 string_file tmp_error_stream;
3141
81d0cc19
GS
3142 /* Explicitly mark the warning -- this will only be printed if
3143 there was an error. */
d7e74731 3144 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3145
5ed8105e 3146 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3147
48d7020b 3148 for (bp_location *bl : all_bp_locations ())
879bfdc2 3149 {
b775012e 3150 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3151 continue;
3152
4a64f543
MS
3153 /* There is no point inserting thread-specific breakpoints if
3154 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3155 has BL->OWNER always non-NULL. */
35df4500 3156 if (bl->owner->thread != -1
5d5658a1 3157 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3158 continue;
3159
35df4500 3160 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3161
3162 /* For targets that support global breakpoints, there's no need
3163 to select an inferior to insert breakpoint to. In fact, even
3164 if we aren't attached to any process yet, we should still
3165 insert breakpoints. */
f5656ead 3166 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3167 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3168 continue;
3169
d7e74731 3170 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3171 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3172 if (val)
eacd795a 3173 error_flag = val;
879bfdc2 3174 }
c906108c 3175
4a64f543
MS
3176 /* If we failed to insert all locations of a watchpoint, remove
3177 them, as half-inserted watchpoint is of limited use. */
43892fdf 3178 for (breakpoint *bpt : all_breakpoints ())
a5606eee 3179 {
d8de7963 3180 bool some_failed = false;
a5606eee
VP
3181
3182 if (!is_hardware_watchpoint (bpt))
3183 continue;
3184
d6b74ac4 3185 if (!breakpoint_enabled (bpt))
a5606eee 3186 continue;
74960c60
VP
3187
3188 if (bpt->disposition == disp_del_at_next_stop)
3189 continue;
a5606eee 3190
40cb8ca5 3191 for (bp_location *loc : bpt->locations ())
56710373 3192 if (!loc->inserted && should_be_inserted (loc))
a5606eee 3193 {
d8de7963 3194 some_failed = true;
a5606eee
VP
3195 break;
3196 }
40cb8ca5 3197
a5606eee
VP
3198 if (some_failed)
3199 {
40cb8ca5 3200 for (bp_location *loc : bpt->locations ())
a5606eee 3201 if (loc->inserted)
834c0d03 3202 remove_breakpoint (loc);
a5606eee
VP
3203
3204 hw_breakpoint_error = 1;
d7e74731
PA
3205 tmp_error_stream.printf ("Could not insert "
3206 "hardware watchpoint %d.\n",
3207 bpt->number);
eacd795a 3208 error_flag = -1;
a5606eee
VP
3209 }
3210 }
3211
eacd795a 3212 if (error_flag)
81d0cc19
GS
3213 {
3214 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3215 message about possibly exhausted resources. */
dd61ec5c 3216 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3217 {
d7e74731 3218 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3219You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3220 }
223ffa71 3221 target_terminal::ours_for_output ();
81d0cc19
GS
3222 error_stream (tmp_error_stream);
3223 }
c906108c
SS
3224}
3225
c30eee59
TJB
3226/* Used when the program stops.
3227 Returns zero if successful, or non-zero if there was a problem
3228 removing a breakpoint location. */
3229
c906108c 3230int
fba45db2 3231remove_breakpoints (void)
c906108c 3232{
3a1bae8e 3233 int val = 0;
c906108c 3234
48d7020b 3235 for (bp_location *bl : all_bp_locations ())
1e4d1764 3236 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3237 val |= remove_breakpoint (bl);
01add95b 3238
3a1bae8e 3239 return val;
c906108c
SS
3240}
3241
49fa26b0
PA
3242/* When a thread exits, remove breakpoints that are related to
3243 that thread. */
3244
3245static void
3246remove_threaded_breakpoints (struct thread_info *tp, int silent)
3247{
1428b37a 3248 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3249 {
5d5658a1 3250 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0 3251 {
6cb06a8c 3252 gdb_printf (_("\
43792cf0 3253Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
6cb06a8c 3254 b->number, print_thread_id (tp));
2968b79f 3255 delete_breakpoint (b);
49fa26b0
PA
3256 }
3257 }
3258}
3259
f3869b1a 3260/* See breakpoint.h. */
6c95b8df 3261
f3869b1a 3262void
00431a78 3263remove_breakpoints_inf (inferior *inf)
6c95b8df 3264{
6c95b8df 3265 int val;
6c95b8df 3266
48d7020b 3267 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3268 {
3269 if (bl->pspace != inf->pspace)
3270 continue;
6c95b8df 3271
01add95b
SM
3272 if (bl->inserted && !bl->target_info.persist)
3273 {
3274 val = remove_breakpoint (bl);
3275 if (val != 0)
3276 return;
3277 }
3278 }
6c95b8df
PA
3279}
3280
e58b0e63
PA
3281static int internal_breakpoint_number = -1;
3282
84f4c1fe
PM
3283/* Set the breakpoint number of B, depending on the value of INTERNAL.
3284 If INTERNAL is non-zero, the breakpoint number will be populated
3285 from internal_breakpoint_number and that variable decremented.
e5dd4106 3286 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3287 breakpoint_count and that value incremented. Internal breakpoints
3288 do not set the internal var bpnum. */
3289static void
3290set_breakpoint_number (int internal, struct breakpoint *b)
3291{
3292 if (internal)
3293 b->number = internal_breakpoint_number--;
3294 else
3295 {
3296 set_breakpoint_count (breakpoint_count + 1);
3297 b->number = breakpoint_count;
3298 }
3299}
3300
829b6b37
TV
3301/* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3302
e62c965a 3303static struct breakpoint *
a6d9a66e 3304create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3305 CORE_ADDR address, enum bptype type)
e62c965a 3306{
752a2291
PA
3307 std::unique_ptr<internal_breakpoint> b
3308 (new internal_breakpoint (gdbarch, type, address));
e62c965a 3309
e62c965a 3310 b->number = internal_breakpoint_number--;
e62c965a 3311
752a2291 3312 return add_to_breakpoint_chain (std::move (b));
e62c965a
PP
3313}
3314
829b6b37
TV
3315/* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3316 GDBARCH. */
3317
3318static struct breakpoint *
3319create_internal_breakpoint (struct gdbarch *gdbarch,
3320 struct bound_minimal_symbol &msym, enum bptype type)
3321{
3322 CORE_ADDR address;
3323
3324 address = msym.value_address ();
3325
3326 address = gdbarch_convert_from_func_ptr_addr
3327 (gdbarch, address, current_inferior ()->top_target ());
3328
3329 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3330 related to addresses in $pc. We're getting the address from the
3331 minimal symbol table. */
3332
3333 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3334 with elsewhere? Needs testing on vax. */
3335
3336 if (gdbarch_skip_entrypoint_p (gdbarch))
3337 address = gdbarch_skip_entrypoint (gdbarch, address);
3338
3339 return create_internal_breakpoint (gdbarch, address, type);
3340}
3341
17450429
PP
3342static const char *const longjmp_names[] =
3343 {
3344 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3345 };
3346#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3347
3348/* Per-objfile data private to breakpoint.c. */
3349struct breakpoint_objfile_data
3350{
3351 /* Minimal symbol for "_ovly_debug_event" (if any). */
f6b3ad54 3352 struct bound_minimal_symbol overlay_msym;
17450429
PP
3353
3354 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
f6b3ad54 3355 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3356
28106bc2 3357 /* True if we have looked for longjmp probes. */
43dce439 3358 int longjmp_searched = 0;
28106bc2 3359
45461e0d
SM
3360 /* SystemTap probe points for longjmp (if any). These are non-owning
3361 references. */
3362 std::vector<probe *> longjmp_probes;
28106bc2 3363
17450429 3364 /* Minimal symbol for "std::terminate()" (if any). */
f6b3ad54 3365 struct bound_minimal_symbol terminate_msym;
17450429
PP
3366
3367 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
f6b3ad54 3368 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3369
3370 /* True if we have looked for exception probes. */
43dce439 3371 int exception_searched = 0;
28106bc2 3372
45461e0d
SM
3373 /* SystemTap probe points for unwinding (if any). These are non-owning
3374 references. */
3375 std::vector<probe *> exception_probes;
17450429
PP
3376};
3377
08b8a139 3378static const registry<objfile>::key<breakpoint_objfile_data>
51d3063a 3379 breakpoint_objfile_key;
17450429
PP
3380
3381/* Minimal symbol not found sentinel. */
3382static struct minimal_symbol msym_not_found;
3383
3384/* Returns TRUE if MSYM point to the "not found" sentinel. */
3385
d8de7963 3386static bool
17450429
PP
3387msym_not_found_p (const struct minimal_symbol *msym)
3388{
3389 return msym == &msym_not_found;
3390}
3391
3392/* Return per-objfile data needed by breakpoint.c.
3393 Allocate the data if necessary. */
3394
3395static struct breakpoint_objfile_data *
3396get_breakpoint_objfile_data (struct objfile *objfile)
3397{
3398 struct breakpoint_objfile_data *bp_objfile_data;
3399
51d3063a 3400 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3401 if (bp_objfile_data == NULL)
51d3063a 3402 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3403 return bp_objfile_data;
3404}
3405
e62c965a 3406static void
af02033e 3407create_overlay_event_breakpoint (void)
e62c965a 3408{
af02033e 3409 const char *const func_name = "_ovly_debug_event";
e62c965a 3410
2030c079 3411 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3412 {
3413 struct breakpoint *b;
17450429
PP
3414 struct breakpoint_objfile_data *bp_objfile_data;
3415 CORE_ADDR addr;
69de3c6a 3416
17450429
PP
3417 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3418
3b7344d5 3419 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3420 continue;
3421
3b7344d5 3422 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3423 {
3b7344d5 3424 struct bound_minimal_symbol m;
17450429
PP
3425
3426 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3427 if (m.minsym == NULL)
17450429
PP
3428 {
3429 /* Avoid future lookups in this objfile. */
3b7344d5 3430 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3431 continue;
3432 }
3433 bp_objfile_data->overlay_msym = m;
3434 }
e62c965a 3435
4aeddc50 3436 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3437 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3438 bp_overlay_event);
40d97ee2 3439 b->locspec = new_explicit_location_spec_function (func_name);
e62c965a 3440
69de3c6a 3441 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3442 {
3443 b->enable_state = bp_enabled;
3444 overlay_events_enabled = 1;
3445 }
69de3c6a
PP
3446 else
3447 {
dda83cd7
SM
3448 b->enable_state = bp_disabled;
3449 overlay_events_enabled = 0;
69de3c6a 3450 }
e62c965a 3451 }
e62c965a
PP
3452}
3453
2a7f6487
TV
3454/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3455 true if a breakpoint was installed. */
3456
3457static bool
3458create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3459{
2a7f6487
TV
3460 struct gdbarch *gdbarch = objfile->arch ();
3461 struct breakpoint_objfile_data *bp_objfile_data
3462 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3463
2a7f6487 3464 if (!bp_objfile_data->longjmp_searched)
94c93c35 3465 {
2a7f6487
TV
3466 std::vector<probe *> ret
3467 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3468
2a7f6487 3469 if (!ret.empty ())
94c93c35 3470 {
2a7f6487
TV
3471 /* We are only interested in checking one element. */
3472 probe *p = ret[0];
0fd8e87f 3473
2a7f6487
TV
3474 if (!p->can_evaluate_arguments ())
3475 {
3476 /* We cannot use the probe interface here,
3477 because it does not know how to evaluate
3478 arguments. */
3479 ret.clear ();
3480 }
3481 }
3482 bp_objfile_data->longjmp_probes = ret;
3483 bp_objfile_data->longjmp_searched = 1;
3484 }
0fd8e87f 3485
2a7f6487
TV
3486 if (bp_objfile_data->longjmp_probes.empty ())
3487 return false;
17450429 3488
2a7f6487
TV
3489 for (probe *p : bp_objfile_data->longjmp_probes)
3490 {
3491 struct breakpoint *b;
25f9533e 3492
2a7f6487
TV
3493 b = create_internal_breakpoint (gdbarch,
3494 p->get_relocated_address (objfile),
9efa3c7f 3495 bp_longjmp_master);
264f9890 3496 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
2a7f6487
TV
3497 b->enable_state = bp_disabled;
3498 }
aed57c53 3499
2a7f6487
TV
3500 return true;
3501}
25f9533e 3502
2a7f6487
TV
3503/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3504 Return true if at least one breakpoint was installed. */
3505
3506static bool
3507create_longjmp_master_breakpoint_names (objfile *objfile)
3508{
3509 struct gdbarch *gdbarch = objfile->arch ();
3510 if (!gdbarch_get_longjmp_target_p (gdbarch))
3511 return false;
3512
3513 struct breakpoint_objfile_data *bp_objfile_data
3514 = get_breakpoint_objfile_data (objfile);
3515 unsigned int installed_bp = 0;
3516
3517 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3518 {
3519 struct breakpoint *b;
3520 const char *func_name;
3521 CORE_ADDR addr;
2a7f6487
TV
3522
3523 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3524 continue;
28106bc2 3525
2a7f6487
TV
3526 func_name = longjmp_names[i];
3527 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3528 {
3529 struct bound_minimal_symbol m;
3530
3531 m = lookup_minimal_symbol_text (func_name, objfile);
3532 if (m.minsym == NULL)
3533 {
3534 /* Prevent future lookups in this objfile. */
3535 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3536 continue;
3537 }
2a7f6487
TV
3538 bp_objfile_data->longjmp_msym[i] = m;
3539 }
28106bc2 3540
4aeddc50 3541 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3542 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
40d97ee2 3543 b->locspec = new_explicit_location_spec_function (func_name);
2a7f6487
TV
3544 b->enable_state = bp_disabled;
3545 installed_bp++;
3546 }
28106bc2 3547
2a7f6487
TV
3548 return installed_bp > 0;
3549}
0fd8e87f 3550
2a7f6487 3551/* Create a master longjmp breakpoint. */
17450429 3552
2a7f6487
TV
3553static void
3554create_longjmp_master_breakpoint (void)
3555{
3556 scoped_restore_current_program_space restore_pspace;
aed57c53 3557
2a7f6487
TV
3558 for (struct program_space *pspace : program_spaces)
3559 {
3560 set_current_program_space (pspace);
17450429 3561
2a7f6487
TV
3562 for (objfile *obj : current_program_space->objfiles ())
3563 {
3564 /* Skip separate debug object, it's handled in the loop below. */
3565 if (obj->separate_debug_objfile_backlink != nullptr)
3566 continue;
3567
3568 /* Try a probe kind breakpoint on main objfile. */
3569 if (create_longjmp_master_breakpoint_probe (obj))
3570 continue;
3571
3572 /* Try longjmp_names kind breakpoints on main and separate_debug
3573 objfiles. */
3574 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3575 if (create_longjmp_master_breakpoint_names (debug_objfile))
3576 break;
94c93c35
TT
3577 }
3578 }
0fd8e87f
UW
3579}
3580
af02033e 3581/* Create a master std::terminate breakpoint. */
aa7d318d 3582static void
af02033e 3583create_std_terminate_master_breakpoint (void)
aa7d318d 3584{
af02033e 3585 const char *const func_name = "std::terminate()";
aa7d318d 3586
5ed8105e 3587 scoped_restore_current_program_space restore_pspace;
aa7d318d 3588
94c93c35
TT
3589 for (struct program_space *pspace : program_spaces)
3590 {
94c93c35 3591 set_current_program_space (pspace);
17450429 3592
94c93c35
TT
3593 for (objfile *objfile : current_program_space->objfiles ())
3594 {
3595 struct breakpoint *b;
3596 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3597
94c93c35 3598 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3599
94c93c35
TT
3600 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3601 continue;
17450429 3602
94c93c35
TT
3603 if (bp_objfile_data->terminate_msym.minsym == NULL)
3604 {
3605 struct bound_minimal_symbol m;
17450429 3606
94c93c35 3607 m = lookup_minimal_symbol (func_name, NULL, objfile);
60f62e2b
SM
3608 if (m.minsym == NULL || (m.minsym->type () != mst_text
3609 && m.minsym->type () != mst_file_text))
94c93c35
TT
3610 {
3611 /* Prevent future lookups in this objfile. */
3612 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3613 continue;
3614 }
3615 bp_objfile_data->terminate_msym = m;
3616 }
aa7d318d 3617
829b6b37
TV
3618 b = create_internal_breakpoint (objfile->arch (),
3619 bp_objfile_data->terminate_msym,
9efa3c7f 3620 bp_std_terminate_master);
40d97ee2 3621 b->locspec = new_explicit_location_spec_function (func_name);
94c93c35
TT
3622 b->enable_state = bp_disabled;
3623 }
3624 }
aa7d318d
TT
3625}
3626
1940319c
TV
3627/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3628 probe. Return true if a breakpoint was installed. */
186c406b 3629
1940319c
TV
3630static bool
3631create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3632{
1940319c
TV
3633 struct breakpoint *b;
3634 struct gdbarch *gdbarch;
3635 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3636
1940319c 3637 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3638
1940319c
TV
3639 /* We prefer the SystemTap probe point if it exists. */
3640 if (!bp_objfile_data->exception_searched)
3641 {
3642 std::vector<probe *> ret
3643 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3644
1940319c 3645 if (!ret.empty ())
28106bc2 3646 {
1940319c
TV
3647 /* We are only interested in checking one element. */
3648 probe *p = ret[0];
25f9533e 3649
1940319c 3650 if (!p->can_evaluate_arguments ())
25f9533e 3651 {
1940319c
TV
3652 /* We cannot use the probe interface here, because it does
3653 not know how to evaluate arguments. */
3654 ret.clear ();
25f9533e 3655 }
28106bc2 3656 }
1940319c
TV
3657 bp_objfile_data->exception_probes = ret;
3658 bp_objfile_data->exception_searched = 1;
3659 }
28106bc2 3660
1940319c
TV
3661 if (bp_objfile_data->exception_probes.empty ())
3662 return false;
45461e0d 3663
1940319c 3664 gdbarch = objfile->arch ();
28106bc2 3665
1940319c
TV
3666 for (probe *p : bp_objfile_data->exception_probes)
3667 {
3668 b = create_internal_breakpoint (gdbarch,
3669 p->get_relocated_address (objfile),
9efa3c7f 3670 bp_exception_master);
264f9890 3671 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
1940319c
TV
3672 b->enable_state = bp_disabled;
3673 }
28106bc2 3674
1940319c
TV
3675 return true;
3676}
28106bc2 3677
1940319c
TV
3678/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3679 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3680
1940319c
TV
3681static bool
3682create_exception_master_breakpoint_hook (objfile *objfile)
3683{
3684 const char *const func_name = "_Unwind_DebugHook";
3685 struct breakpoint *b;
3686 struct gdbarch *gdbarch;
3687 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3688
1940319c 3689 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3690
1940319c
TV
3691 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3692 return false;
17450429 3693
1940319c
TV
3694 gdbarch = objfile->arch ();
3695
3696 if (bp_objfile_data->exception_msym.minsym == NULL)
3697 {
3698 struct bound_minimal_symbol debug_hook;
3699
3700 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3701 if (debug_hook.minsym == NULL)
3702 {
3703 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3704 return false;
186c406b 3705 }
17450429 3706
1940319c
TV
3707 bp_objfile_data->exception_msym = debug_hook;
3708 }
3709
829b6b37
TV
3710 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3711 bp_exception_master);
40d97ee2 3712 b->locspec = new_explicit_location_spec_function (func_name);
1940319c
TV
3713 b->enable_state = bp_disabled;
3714
3715 return true;
3716}
3717
3718/* Install a master breakpoint on the unwinder's debug hook. */
3719
3720static void
3721create_exception_master_breakpoint (void)
3722{
3723 for (objfile *obj : current_program_space->objfiles ())
3724 {
3725 /* Skip separate debug object. */
3726 if (obj->separate_debug_objfile_backlink)
3727 continue;
3728
3729 /* Try a probe kind breakpoint. */
3730 if (create_exception_master_breakpoint_probe (obj))
3731 continue;
3732
7c6944ab
TV
3733 /* Iterate over main and separate debug objects and try an
3734 _Unwind_DebugHook kind breakpoint. */
3735 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3736 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3737 break;
186c406b 3738 }
186c406b
TT
3739}
3740
9ef9e6a6
KS
3741/* Does B have a location spec? */
3742
d8de7963 3743static bool
264f9890 3744breakpoint_location_spec_empty_p (const struct breakpoint *b)
9ef9e6a6 3745{
238dc9af 3746 return (b->locspec != nullptr && b->locspec->empty_p ());
9ef9e6a6
KS
3747}
3748
c906108c 3749void
fba45db2 3750update_breakpoints_after_exec (void)
c906108c 3751{
25b22b0a
PA
3752 /* We're about to delete breakpoints from GDB's lists. If the
3753 INSERTED flag is true, GDB will try to lift the breakpoints by
3754 writing the breakpoints' "shadow contents" back into memory. The
3755 "shadow contents" are NOT valid after an exec, so GDB should not
3756 do that. Instead, the target is responsible from marking
3757 breakpoints out as soon as it detects an exec. We don't do that
3758 here instead, because there may be other attempts to delete
3759 breakpoints after detecting an exec and before reaching here. */
48d7020b 3760 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3761 if (bploc->pspace == current_program_space)
3762 gdb_assert (!bploc->inserted);
c906108c 3763
1428b37a 3764 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3765 {
3766 if (b->pspace != current_program_space)
c5aa993b 3767 continue;
c906108c 3768
01add95b
SM
3769 /* Solib breakpoints must be explicitly reset after an exec(). */
3770 if (b->type == bp_shlib_event)
3771 {
3772 delete_breakpoint (b);
3773 continue;
3774 }
4efc6507 3775
01add95b
SM
3776 /* JIT breakpoints must be explicitly reset after an exec(). */
3777 if (b->type == bp_jit_event)
3778 {
3779 delete_breakpoint (b);
3780 continue;
3781 }
c4093a6a 3782
01add95b
SM
3783 /* Thread event breakpoints must be set anew after an exec(),
3784 as must overlay event and longjmp master breakpoints. */
3785 if (b->type == bp_thread_event || b->type == bp_overlay_event
3786 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3787 || b->type == bp_exception_master)
3788 {
3789 delete_breakpoint (b);
3790 continue;
3791 }
c5aa993b 3792
01add95b
SM
3793 /* Step-resume breakpoints are meaningless after an exec(). */
3794 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3795 {
3796 delete_breakpoint (b);
3797 continue;
3798 }
7c16b83e 3799
01add95b
SM
3800 /* Just like single-step breakpoints. */
3801 if (b->type == bp_single_step)
3802 {
3803 delete_breakpoint (b);
3804 continue;
3805 }
611c83ae 3806
01add95b
SM
3807 /* Longjmp and longjmp-resume breakpoints are also meaningless
3808 after an exec. */
3809 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3810 || b->type == bp_longjmp_call_dummy
3811 || b->type == bp_exception || b->type == bp_exception_resume)
3812 {
3813 delete_breakpoint (b);
3814 continue;
3815 }
ce78b96d 3816
01add95b
SM
3817 if (b->type == bp_catchpoint)
3818 {
3819 /* For now, none of the bp_catchpoint breakpoints need to
3820 do anything at this point. In the future, if some of
3821 the catchpoints need to something, we will need to add
3822 a new method, and call this method from here. */
3823 continue;
3824 }
c5aa993b 3825
01add95b
SM
3826 /* bp_finish is a special case. The only way we ought to be able
3827 to see one of these when an exec() has happened, is if the user
3828 caught a vfork, and then said "finish". Ordinarily a finish just
3829 carries them to the call-site of the current callee, by setting
3830 a temporary bp there and resuming. But in this case, the finish
3831 will carry them entirely through the vfork & exec.
3832
3833 We don't want to allow a bp_finish to remain inserted now. But
3834 we can't safely delete it, 'cause finish_command has a handle to
3835 the bp on a bpstat, and will later want to delete it. There's a
3836 chance (and I've seen it happen) that if we delete the bp_finish
3837 here, that its storage will get reused by the time finish_command
3838 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3839 We really must allow finish_command to delete a bp_finish.
3840
3841 In the absence of a general solution for the "how do we know
3842 it's safe to delete something others may have handles to?"
3843 problem, what we'll do here is just uninsert the bp_finish, and
3844 let finish_command delete it.
3845
3846 (We know the bp_finish is "doomed" in the sense that it's
3847 momentary, and will be deleted as soon as finish_command sees
3848 the inferior stopped. So it doesn't matter that the bp's
3849 address is probably bogus in the new a.out, unlike e.g., the
3850 solib breakpoints.) */
3851
3852 if (b->type == bp_finish)
3853 {
3854 continue;
3855 }
3856
3857 /* Without a symbolic address, we have little hope of the
3858 pre-exec() address meaning the same thing in the post-exec()
3859 a.out. */
264f9890 3860 if (breakpoint_location_spec_empty_p (b))
01add95b
SM
3861 {
3862 delete_breakpoint (b);
3863 continue;
3864 }
3865 }
c906108c
SS
3866}
3867
3868int
d80ee84f 3869detach_breakpoints (ptid_t ptid)
c906108c 3870{
3a1bae8e 3871 int val = 0;
2989a365 3872 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3873 struct inferior *inf = current_inferior ();
c5aa993b 3874
e99b03dc 3875 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3876 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3877
6c95b8df 3878 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3879 inferior_ptid = ptid;
48d7020b 3880 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3881 {
3882 if (bl->pspace != inf->pspace)
3883 continue;
3884
3885 /* This function must physically remove breakpoints locations
3886 from the specified ptid, without modifying the breakpoint
6e14e441
PA
3887 package's state. Locations of type bp_loc_other and
3888 bp_loc_software_watchpoint are only maintained at GDB side,
3889 so there is no need to remove them. Moreover, removing these
01add95b 3890 would modify the breakpoint package's state. */
6e14e441
PA
3891 if (bl->loc_type == bp_loc_other
3892 || bl->loc_type == bp_loc_software_watchpoint)
01add95b
SM
3893 continue;
3894
3895 if (bl->inserted)
3896 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3897 }
d03285ec 3898
3a1bae8e 3899 return val;
c906108c
SS
3900}
3901
35df4500 3902/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3903 Note that this is used to detach breakpoints from a child fork.
3904 When we get here, the child isn't in the inferior list, and neither
3905 do we have objects to represent its address space --- we should
35df4500 3906 *not* look at bl->pspace->aspace here. */
6c95b8df 3907
c906108c 3908static int
b2b6a7da 3909remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3910{
3911 int val;
c5aa993b 3912
35df4500
TJB
3913 /* BL is never in moribund_locations by our callers. */
3914 gdb_assert (bl->owner != NULL);
2bdf28a0 3915
74960c60
VP
3916 /* The type of none suggests that owner is actually deleted.
3917 This should not ever happen. */
35df4500 3918 gdb_assert (bl->owner->type != bp_none);
0bde7532 3919
35df4500
TJB
3920 if (bl->loc_type == bp_loc_software_breakpoint
3921 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3922 {
c02f5703
MS
3923 /* "Normal" instruction breakpoint: either the standard
3924 trap-instruction bp (bp_breakpoint), or a
3925 bp_hardware_breakpoint. */
3926
3927 /* First check to see if we have to handle an overlay. */
3928 if (overlay_debugging == ovly_off
35df4500
TJB
3929 || bl->section == NULL
3930 || !(section_is_overlay (bl->section)))
c02f5703
MS
3931 {
3932 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3933
3934 /* If we're trying to uninsert a memory breakpoint that we
3935 know is set in a dynamic object that is marked
3936 shlib_disabled, then either the dynamic object was
3937 removed with "remove-symbol-file" or with
3938 "nosharedlibrary". In the former case, we don't know
3939 whether another dynamic object might have loaded over the
3940 breakpoint's address -- the user might well let us know
3941 about it next with add-symbol-file (the whole point of
d03de421 3942 add-symbol-file is letting the user manually maintain a
08351840
PA
3943 list of dynamically loaded objects). If we have the
3944 breakpoint's shadow memory, that is, this is a software
3945 breakpoint managed by GDB, check whether the breakpoint
3946 is still inserted in memory, to avoid overwriting wrong
3947 code with stale saved shadow contents. Note that HW
3948 breakpoints don't have shadow memory, as they're
3949 implemented using a mechanism that is not dependent on
3950 being able to modify the target's memory, and as such
3951 they should always be removed. */
3952 if (bl->shlib_disabled
3953 && bl->target_info.shadow_len != 0
3954 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3955 val = 0;
3956 else
c47614fe 3957 val = bl->owner->remove_location (bl, reason);
c02f5703 3958 }
c906108c
SS
3959 else
3960 {
4a64f543 3961 /* This breakpoint is in an overlay section.
c02f5703
MS
3962 Did we set a breakpoint at the LMA? */
3963 if (!overlay_events_enabled)
3964 {
3965 /* Yes -- overlay event support is not active, so we
3966 should have set a breakpoint at the LMA. Remove it.
3967 */
c02f5703
MS
3968 /* Ignore any failures: if the LMA is in ROM, we will
3969 have already warned when we failed to insert it. */
35df4500
TJB
3970 if (bl->loc_type == bp_loc_hardware_breakpoint)
3971 target_remove_hw_breakpoint (bl->gdbarch,
3972 &bl->overlay_target_info);
c02f5703 3973 else
35df4500 3974 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3975 &bl->overlay_target_info,
3976 reason);
c02f5703
MS
3977 }
3978 /* Did we set a breakpoint at the VMA?
3979 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3980 if (bl->inserted)
c906108c 3981 {
c02f5703
MS
3982 /* Yes -- remove it. Previously we did not bother to
3983 remove the breakpoint if the section had been
3984 unmapped, but let's not rely on that being safe. We
3985 don't know what the overlay manager might do. */
aa67235e
UW
3986
3987 /* However, we should remove *software* breakpoints only
3988 if the section is still mapped, or else we overwrite
3989 wrong code with the saved shadow contents. */
348d480f
PA
3990 if (bl->loc_type == bp_loc_hardware_breakpoint
3991 || section_is_mapped (bl->section))
c47614fe 3992 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
3993 else
3994 val = 0;
c906108c 3995 }
c02f5703
MS
3996 else
3997 {
3998 /* No -- not inserted, so no need to remove. No error. */
3999 val = 0;
4000 }
c906108c 4001 }
879d1e6b 4002
08351840
PA
4003 /* In some cases, we might not be able to remove a breakpoint in
4004 a shared library that has already been removed, but we have
4005 not yet processed the shlib unload event. Similarly for an
4006 unloaded add-symbol-file object - the user might not yet have
4007 had the chance to remove-symbol-file it. shlib_disabled will
4008 be set if the library/object has already been removed, but
4009 the breakpoint hasn't been uninserted yet, e.g., after
4010 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4011 always-inserted mode. */
076855f9 4012 if (val
08351840
PA
4013 && (bl->loc_type == bp_loc_software_breakpoint
4014 && (bl->shlib_disabled
4015 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4016 || shared_objfile_contains_address_p (bl->pspace,
4017 bl->address))))
879d1e6b
UW
4018 val = 0;
4019
c906108c
SS
4020 if (val)
4021 return val;
b2b6a7da 4022 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4023 }
35df4500 4024 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4025 {
b2b6a7da 4026 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 4027 bl->owner->remove_location (bl, reason);
2e70b7b9 4028
c906108c 4029 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4030 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4031 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4032 bl->owner->number);
c906108c 4033 }
35df4500 4034 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
4035 && breakpoint_enabled (bl->owner)
4036 && !bl->duplicate)
ce78b96d 4037 {
c47614fe 4038 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
4039 if (val)
4040 return val;
77b06cd7 4041
b2b6a7da 4042 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4043 }
c906108c
SS
4044
4045 return 0;
4046}
4047
6c95b8df 4048static int
834c0d03 4049remove_breakpoint (struct bp_location *bl)
6c95b8df 4050{
35df4500
TJB
4051 /* BL is never in moribund_locations by our callers. */
4052 gdb_assert (bl->owner != NULL);
2bdf28a0 4053
6c95b8df
PA
4054 /* The type of none suggests that owner is actually deleted.
4055 This should not ever happen. */
35df4500 4056 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4057
5ed8105e 4058 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4059
35df4500 4060 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4061
5ed8105e 4062 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4063}
4064
c906108c
SS
4065/* Clear the "inserted" flag in all breakpoints. */
4066
25b22b0a 4067void
fba45db2 4068mark_breakpoints_out (void)
c906108c 4069{
48d7020b 4070 for (bp_location *bl : all_bp_locations ())
66c4b3e8 4071 if (bl->pspace == current_program_space)
35df4500 4072 bl->inserted = 0;
c906108c
SS
4073}
4074
53a5351d
JM
4075/* Clear the "inserted" flag in all breakpoints and delete any
4076 breakpoints which should go away between runs of the program.
c906108c
SS
4077
4078 Plus other such housekeeping that has to be done for breakpoints
4079 between runs.
4080
53a5351d
JM
4081 Note: this function gets called at the end of a run (by
4082 generic_mourn_inferior) and when a run begins (by
4a64f543 4083 init_wait_for_inferior). */
c906108c
SS
4084
4085
4086
4087void
fba45db2 4088breakpoint_init_inferior (enum inf_context context)
c906108c 4089{
6c95b8df 4090 struct program_space *pspace = current_program_space;
c906108c 4091
50c71eaf
PA
4092 /* If breakpoint locations are shared across processes, then there's
4093 nothing to do. */
f5656ead 4094 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4095 return;
4096
1a853c52 4097 mark_breakpoints_out ();
075f6582 4098
1428b37a 4099 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
4100 {
4101 if (b->loc && b->loc->pspace != pspace)
4102 continue;
6c95b8df 4103
01add95b
SM
4104 switch (b->type)
4105 {
4106 case bp_call_dummy:
4107 case bp_longjmp_call_dummy:
c906108c 4108
01add95b
SM
4109 /* If the call dummy breakpoint is at the entry point it will
4110 cause problems when the inferior is rerun, so we better get
4111 rid of it. */
ab92d69b 4112
01add95b 4113 case bp_watchpoint_scope:
ab92d69b 4114
01add95b 4115 /* Also get rid of scope breakpoints. */
ab92d69b 4116
01add95b 4117 case bp_shlib_event:
ab92d69b 4118
01add95b
SM
4119 /* Also remove solib event breakpoints. Their addresses may
4120 have changed since the last time we ran the program.
4121 Actually we may now be debugging against different target;
4122 and so the solib backend that installed this breakpoint may
4123 not be used in by the target. E.g.,
ab92d69b 4124
01add95b
SM
4125 (gdb) file prog-linux
4126 (gdb) run # native linux target
4127 ...
4128 (gdb) kill
4129 (gdb) file prog-win.exe
4130 (gdb) tar rem :9999 # remote Windows gdbserver.
4131 */
c906108c 4132
01add95b 4133 case bp_step_resume:
f59f708a 4134
01add95b 4135 /* Also remove step-resume breakpoints. */
f59f708a 4136
01add95b 4137 case bp_single_step:
7c16b83e 4138
01add95b 4139 /* Also remove single-step breakpoints. */
7c16b83e 4140
01add95b
SM
4141 delete_breakpoint (b);
4142 break;
c906108c 4143
01add95b
SM
4144 case bp_watchpoint:
4145 case bp_hardware_watchpoint:
4146 case bp_read_watchpoint:
4147 case bp_access_watchpoint:
4148 {
4149 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4150
01add95b
SM
4151 /* Likewise for watchpoints on local expressions. */
4152 if (w->exp_valid_block != NULL)
4153 delete_breakpoint (b);
4154 else
4155 {
4156 /* Get rid of existing locations, which are no longer
4157 valid. New ones will be created in
4158 update_watchpoint, when the inferior is restarted.
4159 The next update_global_location_list call will
4160 garbage collect them. */
4161 b->loc = NULL;
4162
4163 if (context == inf_starting)
4164 {
4165 /* Reset val field to force reread of starting value in
4166 insert_breakpoints. */
4167 w->val.reset (nullptr);
4168 w->val_valid = false;
4169 }
4170 }
4171 }
4172 break;
4173 default:
4174 break;
3a5c3e22 4175 }
01add95b 4176 }
1c5cfe86
PA
4177
4178 /* Get rid of the moribund locations. */
1123588c 4179 for (bp_location *bl : moribund_locations)
35df4500 4180 decref_bp_location (&bl);
1123588c 4181 moribund_locations.clear ();
c906108c
SS
4182}
4183
6c95b8df
PA
4184/* These functions concern about actual breakpoints inserted in the
4185 target --- to e.g. check if we need to do decr_pc adjustment or if
4186 we need to hop over the bkpt --- so we check for address space
4187 match, not program space. */
4188
c2c6d25f
JM
4189/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4190 exists at PC. It returns ordinary_breakpoint_here if it's an
4191 ordinary breakpoint, or permanent_breakpoint_here if it's a
4192 permanent breakpoint.
4193 - When continuing from a location with an ordinary breakpoint, we
4194 actually single step once before calling insert_breakpoints.
e5dd4106 4195 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4196 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4197 the target, to advance the PC past the breakpoint. */
c906108c 4198
c2c6d25f 4199enum breakpoint_here
accd0bcd 4200breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4201{
d8de7963 4202 bool any_breakpoint_here = false;
c906108c 4203
48d7020b 4204 for (bp_location *bl : all_bp_locations ())
075f6582 4205 {
35df4500
TJB
4206 if (bl->loc_type != bp_loc_software_breakpoint
4207 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4208 continue;
4209
f1310107 4210 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4211 if ((breakpoint_enabled (bl->owner)
1a853c52 4212 || bl->permanent)
f1310107 4213 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4214 {
4215 if (overlay_debugging
35df4500
TJB
4216 && section_is_overlay (bl->section)
4217 && !section_is_mapped (bl->section))
075f6582 4218 continue; /* unmapped overlay -- can't be a match */
1a853c52 4219 else if (bl->permanent)
075f6582
DJ
4220 return permanent_breakpoint_here;
4221 else
d8de7963 4222 any_breakpoint_here = true;
075f6582
DJ
4223 }
4224 }
c906108c 4225
f486487f 4226 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4227}
4228
d35ae833
PA
4229/* See breakpoint.h. */
4230
4231int
accd0bcd 4232breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4233 CORE_ADDR addr, ULONGEST len)
4234{
48d7020b 4235 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4236 {
4237 if (bl->loc_type != bp_loc_software_breakpoint
4238 && bl->loc_type != bp_loc_hardware_breakpoint)
4239 continue;
4240
4241 if ((breakpoint_enabled (bl->owner)
4242 || bl->permanent)
4243 && breakpoint_location_address_range_overlap (bl, aspace,
4244 addr, len))
4245 {
4246 if (overlay_debugging
4247 && section_is_overlay (bl->section)
4248 && !section_is_mapped (bl->section))
4249 {
4250 /* Unmapped overlay -- can't be a match. */
4251 continue;
4252 }
4253
4254 return 1;
4255 }
4256 }
4257
4258 return 0;
4259}
4260
1c5cfe86
PA
4261/* Return true if there's a moribund breakpoint at PC. */
4262
4263int
accd0bcd 4264moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4265{
1123588c 4266 for (bp_location *loc : moribund_locations)
f1310107 4267 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4268 return 1;
4269
4270 return 0;
4271}
c2c6d25f 4272
d8de7963 4273/* Returns true iff BL is inserted at PC, in address space ASPACE. */
f7ce857f 4274
d8de7963 4275static bool
b2ff9ed3 4276bp_location_inserted_here_p (const struct bp_location *bl,
accd0bcd 4277 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4278{
4279 if (bl->inserted
4280 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4281 aspace, pc))
4282 {
d8de7963
AB
4283 /* An unmapped overlay can't be a match. */
4284 return !(overlay_debugging
4285 && section_is_overlay (bl->section)
4286 && !section_is_mapped (bl->section));
f7ce857f 4287 }
d8de7963 4288 return false;
f7ce857f
PA
4289}
4290
a1fd2fa5 4291/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4292
4293int
accd0bcd 4294breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4295{
e0d9a270 4296 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4297 {
35df4500
TJB
4298 if (bl->loc_type != bp_loc_software_breakpoint
4299 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4300 continue;
4301
f7ce857f
PA
4302 if (bp_location_inserted_here_p (bl, aspace, pc))
4303 return 1;
c5aa993b 4304 }
c36b740a
VP
4305 return 0;
4306}
4307
a1fd2fa5
PA
4308/* This function returns non-zero iff there is a software breakpoint
4309 inserted at PC. */
c36b740a
VP
4310
4311int
accd0bcd 4312software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4313 CORE_ADDR pc)
4fa8626c 4314{
e0d9a270 4315 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4316 {
35df4500 4317 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4318 continue;
4319
f7ce857f
PA
4320 if (bp_location_inserted_here_p (bl, aspace, pc))
4321 return 1;
4fa8626c
DJ
4322 }
4323
4324 return 0;
9c02b525
PA
4325}
4326
4327/* See breakpoint.h. */
4328
4329int
accd0bcd 4330hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4331 CORE_ADDR pc)
4332{
e0d9a270 4333 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4334 {
9c02b525
PA
4335 if (bl->loc_type != bp_loc_hardware_breakpoint)
4336 continue;
4337
4338 if (bp_location_inserted_here_p (bl, aspace, pc))
4339 return 1;
4340 }
4341
4342 return 0;
4fa8626c
DJ
4343}
4344
9093389c 4345int
accd0bcd 4346hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4347 CORE_ADDR addr, ULONGEST len)
4348{
43892fdf 4349 for (breakpoint *bpt : all_breakpoints ())
9093389c 4350 {
9093389c
PA
4351 if (bpt->type != bp_hardware_watchpoint
4352 && bpt->type != bp_access_watchpoint)
4353 continue;
4354
4355 if (!breakpoint_enabled (bpt))
4356 continue;
4357
40cb8ca5 4358 for (bp_location *loc : bpt->locations ())
9093389c
PA
4359 if (loc->pspace->aspace == aspace && loc->inserted)
4360 {
4361 CORE_ADDR l, h;
4362
4363 /* Check for intersection. */
768adc05
PA
4364 l = std::max<CORE_ADDR> (loc->address, addr);
4365 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4366 if (l < h)
4367 return 1;
4368 }
4369 }
4370 return 0;
4371}
c5aa993b 4372
f2478a7e 4373/* See breakpoint.h. */
c906108c 4374
f2478a7e
SM
4375bool
4376is_catchpoint (struct breakpoint *b)
c906108c 4377{
f2478a7e 4378 return (b->type == bp_catchpoint);
c906108c
SS
4379}
4380
c906108c
SS
4381/* Clear a bpstat so that it says we are not at any breakpoint.
4382 Also free any storage that is part of a bpstat. */
4383
4384void
313f3b21 4385bpstat_clear (bpstat **bsp)
c906108c 4386{
313f3b21
SM
4387 bpstat *p;
4388 bpstat *q;
c906108c
SS
4389
4390 if (bsp == 0)
4391 return;
4392 p = *bsp;
4393 while (p != NULL)
4394 {
4395 q = p->next;
04afa70c 4396 delete p;
c906108c
SS
4397 p = q;
4398 }
4399 *bsp = NULL;
4400}
4401
313f3b21 4402bpstat::bpstat (const bpstat &other)
04afa70c
TT
4403 : next (NULL),
4404 bp_location_at (other.bp_location_at),
4405 breakpoint_at (other.breakpoint_at),
4406 commands (other.commands),
04afa70c
TT
4407 print (other.print),
4408 stop (other.stop),
4409 print_it (other.print_it)
4410{
850645cf 4411 if (other.old_val != NULL)
f28085df 4412 old_val = release_value (other.old_val->copy ());
04afa70c
TT
4413}
4414
c906108c
SS
4415/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4416 is part of the bpstat is copied as well. */
4417
313f3b21
SM
4418bpstat *
4419bpstat_copy (bpstat *bs)
c906108c 4420{
313f3b21
SM
4421 bpstat *p = nullptr;
4422 bpstat *tmp;
4423 bpstat *retval = nullptr;
c906108c
SS
4424
4425 if (bs == NULL)
4426 return bs;
4427
4428 for (; bs != NULL; bs = bs->next)
4429 {
313f3b21 4430 tmp = new bpstat (*bs);
31cc81e9 4431
c906108c
SS
4432 if (p == NULL)
4433 /* This is the first thing in the chain. */
4434 retval = tmp;
4435 else
4436 p->next = tmp;
4437 p = tmp;
4438 }
4439 p->next = NULL;
4440 return retval;
4441}
4442
4a64f543 4443/* Find the bpstat associated with this breakpoint. */
c906108c 4444
313f3b21
SM
4445bpstat *
4446bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4447{
c5aa993b
JM
4448 if (bsp == NULL)
4449 return NULL;
c906108c 4450
c5aa993b
JM
4451 for (; bsp != NULL; bsp = bsp->next)
4452 {
f431efe5 4453 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4454 return bsp;
4455 }
c906108c
SS
4456 return NULL;
4457}
4458
ab04a2af
TT
4459/* See breakpoint.h. */
4460
4c462cb0 4461bool
313f3b21 4462bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4463{
ab04a2af
TT
4464 for (; bsp != NULL; bsp = bsp->next)
4465 {
427cd150
TT
4466 if (bsp->breakpoint_at == NULL)
4467 {
4468 /* A moribund location can never explain a signal other than
4469 GDB_SIGNAL_TRAP. */
4470 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4471 return true;
427cd150
TT
4472 }
4473 else
47591c29 4474 {
c47614fe 4475 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4476 return true;
47591c29 4477 }
ab04a2af
TT
4478 }
4479
4c462cb0 4480 return false;
ab04a2af
TT
4481}
4482
78805ff8 4483/* See breakpoint.h. */
c906108c
SS
4484
4485int
313f3b21 4486bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4487{
4488 struct breakpoint *b;
4489
4490 if ((*bsp) == NULL)
4491 return 0; /* No more breakpoint values */
8671a17b 4492
4a64f543
MS
4493 /* We assume we'll never have several bpstats that correspond to a
4494 single breakpoint -- otherwise, this function might return the
4495 same number more than once and this will look ugly. */
f431efe5 4496 b = (*bsp)->breakpoint_at;
8671a17b
PA
4497 *bsp = (*bsp)->next;
4498 if (b == NULL)
4499 return -1; /* breakpoint that's been deleted since */
4500
4501 *num = b->number; /* We have its number */
4502 return 1;
c906108c
SS
4503}
4504
78805ff8
PW
4505/* See breakpoint.h */
4506
4507int
4508bpstat_locno (const bpstat *bs)
4509{
4510 const struct breakpoint *b = bs->breakpoint_at;
4511 const struct bp_location *bl = bs->bp_location_at.get ();
4512
4513 int locno = 0;
4514
28a072f4 4515 if (b != nullptr && b->loc != nullptr && b->loc->next != nullptr)
78805ff8
PW
4516 {
4517 const bp_location *bl_i;
4518
4519 for (bl_i = b->loc;
4520 bl_i != bl && bl_i->next != nullptr;
4521 bl_i = bl_i->next)
4522 locno++;
4523
4524 if (bl_i == bl)
4525 locno++;
4526 else
4527 {
4528 warning (_("location number not found for breakpoint %d address %s."),
4529 b->number, paddress (bl->gdbarch, bl->address));
4530 locno = 0;
4531 }
4532 }
4533
4534 return locno;
4535}
4536
4537/* See breakpoint.h. */
4538
4539void
4540print_num_locno (const bpstat *bs, struct ui_out *uiout)
4541{
4542 struct breakpoint *b = bs->breakpoint_at;
4543
4544 if (b == nullptr)
4545 uiout->text (_("deleted breakpoint"));
4546 else
4547 {
4548 uiout->field_signed ("bkptno", b->number);
4549
4550 int locno = bpstat_locno (bs);
4551 if (locno != 0)
4552 uiout->message (".%pF", signed_field ("locno", locno));
4553 }
4554}
4555
e93ca019 4556/* See breakpoint.h. */
c906108c
SS
4557
4558void
e93ca019 4559bpstat_clear_actions (void)
c906108c 4560{
313f3b21 4561 bpstat *bs;
e93ca019 4562
00431a78 4563 if (inferior_ptid == null_ptid)
e93ca019
JK
4564 return;
4565
00431a78 4566 thread_info *tp = inferior_thread ();
e93ca019 4567 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4568 {
d1b0a7bf 4569 bs->commands = NULL;
850645cf 4570 bs->old_val.reset (nullptr);
c906108c
SS
4571 }
4572}
4573
f3b1572e
PA
4574/* Called when a command is about to proceed the inferior. */
4575
4576static void
4577breakpoint_about_to_proceed (void)
4578{
d7e15655 4579 if (inferior_ptid != null_ptid)
f3b1572e
PA
4580 {
4581 struct thread_info *tp = inferior_thread ();
4582
4583 /* Allow inferior function calls in breakpoint commands to not
4584 interrupt the command list. When the call finishes
4585 successfully, the inferior will be standing at the same
4586 breakpoint as if nothing happened. */
16c381f0 4587 if (tp->control.in_infcall)
f3b1572e
PA
4588 return;
4589 }
4590
4591 breakpoint_proceeded = 1;
4592}
4593
d8de7963 4594/* Return true iff CMD as the first line of a command sequence is `silent'
abf85f46
JK
4595 or its equivalent. */
4596
d8de7963 4597static bool
abf85f46
JK
4598command_line_is_silent (struct command_line *cmd)
4599{
4f45d445 4600 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4601}
4602
83f35083
PW
4603/* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4604 A locno 0 is changed to 1 to e.g. let the user do
4605 (gdb) disable $_hit_bpnum.$_hit_locno
4606 for a single location breakpoint. */
4607
78805ff8 4608static void
83f35083 4609set_hit_convenience_vars (int bpnum, int locno)
78805ff8 4610{
83f35083
PW
4611 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4612 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4613 (locno > 0 ? locno : 1));
78805ff8
PW
4614}
4615
4a64f543
MS
4616/* Execute all the commands associated with all the breakpoints at
4617 this location. Any of these commands could cause the process to
4618 proceed beyond this point, etc. We look out for such changes by
4619 checking the global "breakpoint_proceeded" after each command.
c906108c 4620
347bddb7
PA
4621 Returns true if a breakpoint command resumed the inferior. In that
4622 case, it is the caller's responsibility to recall it again with the
4623 bpstat of the current thread. */
4624
d8de7963 4625static bool
313f3b21 4626bpstat_do_actions_1 (bpstat **bsp)
c906108c 4627{
313f3b21 4628 bpstat *bs;
d8de7963 4629 bool again = false;
c906108c
SS
4630
4631 /* Avoid endless recursion if a `source' command is contained
4632 in bs->commands. */
4633 if (executing_breakpoint_commands)
d8de7963 4634 return false;
c906108c 4635
81b1e71c
TT
4636 scoped_restore save_executing
4637 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4638
1ac32117 4639 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4640
4a64f543 4641 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4642 bs = *bsp;
4643
78805ff8 4644 /* The $_hit_* convenience variables are set before running the
83f35083
PW
4645 commands of BS. In case we have several bs, after the loop,
4646 we set again the variables to the first printed bpnum and locno.
4647 For multiple breakpoints, this ensures the variables are set to the
4648 breakpoint printed for the user. */
4649 int printed_hit_bpnum = -1;
4650 int printed_hit_locno = -1;
78805ff8 4651
c906108c
SS
4652 breakpoint_proceeded = 0;
4653 for (; bs != NULL; bs = bs->next)
4654 {
d1b0a7bf 4655 struct command_line *cmd = NULL;
6c50ab1c 4656
83f35083
PW
4657 /* Set the _hit_* convenience variables before running BS's commands. */
4658 {
4659 const struct breakpoint *b = bs->breakpoint_at;
4660 if (b != nullptr)
4661 {
4662 int locno = bpstat_locno (bs);
4663
4664 set_hit_convenience_vars (b->number, locno);
4665 if (printed_hit_locno == -1 && bs->print)
4666 {
4667 printed_hit_bpnum = b->number;
4668 printed_hit_locno = locno;
4669 }
4670 }
4671 }
78805ff8 4672
6c50ab1c
JB
4673 /* Take ownership of the BSP's command tree, if it has one.
4674
dda83cd7
SM
4675 The command tree could legitimately contain commands like
4676 'step' and 'next', which call clear_proceed_status, which
83f35083 4677 frees the bpstat BS and its command tree. To make sure this doesn't
dda83cd7
SM
4678 free the tree we're executing out from under us, we need to
4679 take ownership of the tree ourselves. Since a given bpstat's
4680 commands are only executed once, we don't need to copy it; we
4681 can clear the pointer in the bpstat, and make sure we free
4682 the tree when we're done. */
d1b0a7bf 4683 counted_command_line ccmd = bs->commands;
9add0f1b 4684 bs->commands = NULL;
d1b0a7bf
TT
4685 if (ccmd != NULL)
4686 cmd = ccmd.get ();
abf85f46
JK
4687 if (command_line_is_silent (cmd))
4688 {
4689 /* The action has been already done by bpstat_stop_status. */
4690 cmd = cmd->next;
4691 }
6c50ab1c 4692
c906108c
SS
4693 while (cmd != NULL)
4694 {
4695 execute_control_command (cmd);
83f35083
PW
4696 /* After execute_control_command, if breakpoint_proceeded is true,
4697 BS has been freed and cannot be accessed anymore. */
c906108c
SS
4698
4699 if (breakpoint_proceeded)
4700 break;
4701 else
4702 cmd = cmd->next;
4703 }
6c50ab1c 4704
c906108c 4705 if (breakpoint_proceeded)
32c1e744 4706 {
cb814510 4707 if (current_ui->async)
347bddb7
PA
4708 /* If we are in async mode, then the target might be still
4709 running, not stopped at any breakpoint, so nothing for
4710 us to do here -- just return to the event loop. */
4711 ;
32c1e744
VP
4712 else
4713 /* In sync mode, when execute_control_command returns
4714 we're already standing on the next breakpoint.
347bddb7
PA
4715 Breakpoint commands for that stop were not run, since
4716 execute_command does not run breakpoint commands --
4717 only command_line_handler does, but that one is not
4718 involved in execution of breakpoint commands. So, we
4719 can now execute breakpoint commands. It should be
4720 noted that making execute_command do bpstat actions is
4721 not an option -- in this case we'll have recursive
4722 invocation of bpstat for each breakpoint with a
4723 command, and can easily blow up GDB stack. Instead, we
4724 return true, which will trigger the caller to recall us
4725 with the new stop_bpstat. */
d8de7963 4726 again = true;
347bddb7 4727 break;
32c1e744 4728 }
c906108c 4729 }
78805ff8
PW
4730
4731 /* Now that we have executed the commands of all bs, set the _hit_*
83f35083
PW
4732 convenience variables to the printed values. */
4733 if (printed_hit_locno != -1)
4734 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
78805ff8 4735
347bddb7
PA
4736 return again;
4737}
4738
00431a78
PA
4739/* Helper for bpstat_do_actions. Get the current thread, if there's
4740 one, is alive and has execution. Return NULL otherwise. */
4741
4742static thread_info *
4743get_bpstat_thread ()
4744{
55f6301a 4745 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4746 return NULL;
4747
4748 thread_info *tp = inferior_thread ();
611841bb 4749 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4750 return NULL;
4751 return tp;
4752}
4753
347bddb7
PA
4754void
4755bpstat_do_actions (void)
4756{
694c6bf5 4757 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4758 thread_info *tp;
353d1d73 4759
347bddb7 4760 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4761 while ((tp = get_bpstat_thread ()) != NULL)
4762 {
4763 /* Since in sync mode, bpstat_do_actions may resume the
4764 inferior, and only return when it is stopped at the next
4765 breakpoint, we keep doing breakpoint actions until it returns
4766 false to indicate the inferior was not resumed. */
4767 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4768 break;
4769 }
353d1d73 4770
694c6bf5 4771 cleanup_if_error.release ();
c906108c
SS
4772}
4773
fa4727a6
DJ
4774/* Print out the (old or new) value associated with a watchpoint. */
4775
4776static void
4777watchpoint_value_print (struct value *val, struct ui_file *stream)
4778{
4779 if (val == NULL)
7f6aba03 4780 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4781 else
79a45b7d
TT
4782 {
4783 struct value_print_options opts;
4784 get_user_print_options (&opts);
4785 value_print (val, stream, &opts);
4786 }
fa4727a6
DJ
4787}
4788
f303dbd6
PA
4789/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4790 debugging multiple threads. */
4791
4792void
4793maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4794{
112e8700 4795 if (uiout->is_mi_like_p ())
f303dbd6
PA
4796 return;
4797
112e8700 4798 uiout->text ("\n");
f303dbd6
PA
4799
4800 if (show_thread_that_caused_stop ())
4801 {
f303dbd6
PA
4802 struct thread_info *thr = inferior_thread ();
4803
112e8700 4804 uiout->text ("Thread ");
33eca680 4805 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 4806
25558938 4807 const char *name = thread_name (thr);
f303dbd6
PA
4808 if (name != NULL)
4809 {
112e8700 4810 uiout->text (" \"");
33eca680 4811 uiout->field_string ("name", name);
112e8700 4812 uiout->text ("\"");
f303dbd6
PA
4813 }
4814
112e8700 4815 uiout->text (" hit ");
f303dbd6
PA
4816 }
4817}
4818
e514a9d6 4819/* Generic routine for printing messages indicating why we
4a64f543 4820 stopped. The behavior of this function depends on the value
e514a9d6
JM
4821 'print_it' in the bpstat structure. Under some circumstances we
4822 may decide not to print anything here and delegate the task to
4a64f543 4823 normal_stop(). */
e514a9d6
JM
4824
4825static enum print_stop_action
313f3b21 4826print_bp_stop_message (bpstat *bs)
e514a9d6
JM
4827{
4828 switch (bs->print_it)
4829 {
4830 case print_it_noop:
4a64f543 4831 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4832 return PRINT_UNKNOWN;
4833 break;
4834
4835 case print_it_done:
4836 /* We still want to print the frame, but we already printed the
dda83cd7 4837 relevant messages. */
e514a9d6
JM
4838 return PRINT_SRC_AND_LOC;
4839 break;
4840
4841 case print_it_normal:
4f8d1dc6 4842 {
f431efe5
PA
4843 struct breakpoint *b = bs->breakpoint_at;
4844
1a6a67de
TJB
4845 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4846 which has since been deleted. */
4847 if (b == NULL)
4848 return PRINT_UNKNOWN;
4849
348d480f 4850 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 4851 return b->print_it (bs);
4f8d1dc6 4852 }
348d480f 4853 break;
3086aeae 4854
e514a9d6 4855 default:
f34652de 4856 internal_error (_("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4857 break;
c906108c 4858 }
c906108c
SS
4859}
4860
2f9ee862 4861/* See breakpoint.h. */
edcc5120 4862
2f9ee862 4863void
4e9e993a 4864print_solib_event (bool is_catchpoint)
edcc5120 4865{
6fb16ce6 4866 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4867 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4868
4869 if (!is_catchpoint)
4870 {
4871 if (any_added || any_deleted)
112e8700 4872 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4873 else
112e8700
SM
4874 current_uiout->text (_("Stopped due to shared library event (no "
4875 "libraries added or removed)\n"));
edcc5120
TT
4876 }
4877
112e8700
SM
4878 if (current_uiout->is_mi_like_p ())
4879 current_uiout->field_string ("reason",
4880 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4881
4882 if (any_deleted)
4883 {
112e8700 4884 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4885 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4886 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4887 {
6fb16ce6
SM
4888 const std::string &name = current_program_space->deleted_solibs[ix];
4889
edcc5120 4890 if (ix > 0)
112e8700
SM
4891 current_uiout->text (" ");
4892 current_uiout->field_string ("library", name);
4893 current_uiout->text ("\n");
edcc5120 4894 }
edcc5120
TT
4895 }
4896
4897 if (any_added)
4898 {
112e8700 4899 current_uiout->text (_(" Inferior loaded "));
10f489e5 4900 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4901 bool first = true;
52941706 4902 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4903 {
bcb430e4 4904 if (!first)
112e8700 4905 current_uiout->text (" ");
bcb430e4 4906 first = false;
112e8700
SM
4907 current_uiout->field_string ("library", iter->so_name);
4908 current_uiout->text ("\n");
edcc5120 4909 }
edcc5120
TT
4910 }
4911}
4912
e514a9d6
JM
4913/* Print a message indicating what happened. This is called from
4914 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4915 list - a list of the eventpoints that caused this stop. KIND is
4916 the target_waitkind for the stopping event. This
e514a9d6
JM
4917 routine calls the generic print routine for printing a message
4918 about reasons for stopping. This will print (for example) the
4919 "Breakpoint n," part of the output. The return value of this
4920 routine is one of:
c906108c 4921
4a64f543 4922 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4923 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4924 code to print the location. An example is
c5aa993b
JM
4925 "Breakpoint 1, " which should be followed by
4926 the location.
917317f4 4927 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4928 to also print the location part of the message.
4929 An example is the catch/throw messages, which
4a64f543 4930 don't require a location appended to the end.
917317f4 4931 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4932 further info to be printed. */
c906108c 4933
917317f4 4934enum print_stop_action
403c71fd 4935bpstat_print (bpstat *bs, target_waitkind kind)
c906108c 4936{
f486487f 4937 enum print_stop_action val;
c5aa993b 4938
c906108c 4939 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4940 (Currently all watchpoints go on the bpstat whether hit or not.
4941 That probably could (should) be changed, provided care is taken
c906108c 4942 with respect to bpstat_explains_signal). */
e514a9d6
JM
4943 for (; bs; bs = bs->next)
4944 {
4945 val = print_bp_stop_message (bs);
4946 if (val == PRINT_SRC_ONLY
78805ff8 4947 || val == PRINT_SRC_AND_LOC
e514a9d6
JM
4948 || val == PRINT_NOTHING)
4949 return val;
4950 }
c906108c 4951
36dfb11c
TT
4952 /* If we had hit a shared library event breakpoint,
4953 print_bp_stop_message would print out this message. If we hit an
4954 OS-level shared library event, do the same thing. */
4955 if (kind == TARGET_WAITKIND_LOADED)
4956 {
4e9e993a 4957 print_solib_event (false);
36dfb11c
TT
4958 return PRINT_NOTHING;
4959 }
4960
e514a9d6 4961 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4962 with and nothing was printed. */
917317f4 4963 return PRINT_UNKNOWN;
c906108c
SS
4964}
4965
bf469271 4966/* Evaluate the boolean expression EXP and return the result. */
c906108c 4967
bf469271
PA
4968static bool
4969breakpoint_cond_eval (expression *exp)
c906108c 4970{
65558ca5
TT
4971 scoped_value_mark mark;
4972 return value_true (evaluate_expression (exp));
c906108c
SS
4973}
4974
5760d0ab 4975/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4976
313f3b21 4977bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 4978 : next (NULL),
b6433ede 4979 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4980 breakpoint_at (bl->owner),
4981 commands (NULL),
04afa70c
TT
4982 print (0),
4983 stop (0),
4984 print_it (print_it_normal)
c906108c 4985{
04afa70c
TT
4986 **bs_link_pointer = this;
4987 *bs_link_pointer = &next;
4988}
4989
313f3b21 4990bpstat::bpstat ()
04afa70c 4991 : next (NULL),
04afa70c
TT
4992 breakpoint_at (NULL),
4993 commands (NULL),
04afa70c
TT
4994 print (0),
4995 stop (0),
4996 print_it (print_it_normal)
4997{
c906108c
SS
4998}
4999\f
d983da9c
DJ
5000/* The target has stopped with waitstatus WS. Check if any hardware
5001 watchpoints have triggered, according to the target. */
5002
5003int
c272a98c 5004watchpoints_triggered (const target_waitstatus &ws)
d983da9c 5005{
57810aa7 5006 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 5007 CORE_ADDR addr;
d983da9c
DJ
5008
5009 if (!stopped_by_watchpoint)
5010 {
5011 /* We were not stopped by a watchpoint. Mark all watchpoints
5012 as not triggered. */
43892fdf 5013 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5014 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5015 {
5016 struct watchpoint *w = (struct watchpoint *) b;
5017
5018 w->watchpoint_triggered = watch_triggered_no;
5019 }
d983da9c
DJ
5020
5021 return 0;
5022 }
5023
328d42d8 5024 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
5025 {
5026 /* We were stopped by a watchpoint, but we don't know where.
5027 Mark all watchpoints as unknown. */
43892fdf 5028 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5029 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5030 {
5031 struct watchpoint *w = (struct watchpoint *) b;
5032
5033 w->watchpoint_triggered = watch_triggered_unknown;
5034 }
d983da9c 5035
3c4797ba 5036 return 1;
d983da9c
DJ
5037 }
5038
5039 /* The target could report the data address. Mark watchpoints
5040 affected by this data address as triggered, and all others as not
5041 triggered. */
5042
43892fdf 5043 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5044 if (is_hardware_watchpoint (b))
d983da9c 5045 {
3a5c3e22 5046 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 5047
3a5c3e22 5048 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 5049 for (bp_location *loc : b->locations ())
9c06b0b4 5050 {
3a5c3e22 5051 if (is_masked_watchpoint (b))
9c06b0b4 5052 {
3a5c3e22
PA
5053 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5054 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5055
5056 if (newaddr == start)
5057 {
3a5c3e22 5058 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5059 break;
5060 }
5061 }
5062 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
5063 else if (target_watchpoint_addr_within_range
5064 (current_inferior ()->top_target (), addr, loc->address,
5065 loc->length))
9c06b0b4 5066 {
3a5c3e22 5067 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5068 break;
5069 }
5070 }
d983da9c
DJ
5071 }
5072
5073 return 1;
5074}
5075
bf469271
PA
5076/* Possible return values for watchpoint_check. */
5077enum wp_check_result
5078 {
5079 /* The watchpoint has been deleted. */
5080 WP_DELETED = 1,
5081
5082 /* The value has changed. */
5083 WP_VALUE_CHANGED = 2,
5084
5085 /* The value has not changed. */
5086 WP_VALUE_NOT_CHANGED = 3,
5087
5088 /* Ignore this watchpoint, no matter if the value changed or not. */
5089 WP_IGNORE = 4,
5090 };
c906108c
SS
5091
5092#define BP_TEMPFLAG 1
5093#define BP_HARDWAREFLAG 2
5094
4a64f543 5095/* Evaluate watchpoint condition expression and check if its value
bf469271 5096 changed. */
553e4c11 5097
bf469271 5098static wp_check_result
313f3b21 5099watchpoint_check (bpstat *bs)
c906108c 5100{
3a5c3e22 5101 struct watchpoint *b;
bd2b40ac 5102 frame_info_ptr fr;
d8de7963 5103 bool within_current_scope;
c906108c 5104
f431efe5 5105 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5106 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5107 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5108
f6bc2008
PA
5109 /* If this is a local watchpoint, we only want to check if the
5110 watchpoint frame is in scope if the current thread is the thread
5111 that was used to create the watchpoint. */
5112 if (!watchpoint_in_thread_scope (b))
60e1c644 5113 return WP_IGNORE;
f6bc2008 5114
c906108c 5115 if (b->exp_valid_block == NULL)
d8de7963 5116 within_current_scope = true;
c906108c
SS
5117 else
5118 {
bd2b40ac 5119 frame_info_ptr frame = get_current_frame ();
edb3359d
DJ
5120 struct gdbarch *frame_arch = get_frame_arch (frame);
5121 CORE_ADDR frame_pc = get_frame_pc (frame);
5122
c9cf6e20 5123 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5124 still in the function but the stack frame has already been
5125 invalidated. Since we can't rely on the values of local
5126 variables after the stack has been destroyed, we are treating
5127 the watchpoint in that state as `not changed' without further
5128 checking. Don't mark watchpoints as changed if the current
5129 frame is in an epilogue - even if they are in some other
5130 frame, our view of the stack is likely to be wrong and
5131 frame_find_by_id could error out. */
c9cf6e20 5132 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5133 return WP_IGNORE;
a0f49112 5134
101dcfbe 5135 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5136 within_current_scope = (fr != NULL);
69fbadd5
DJ
5137
5138 /* If we've gotten confused in the unwinder, we might have
5139 returned a frame that can't describe this variable. */
edb3359d
DJ
5140 if (within_current_scope)
5141 {
5142 struct symbol *function;
5143
5144 function = get_frame_function (fr);
5145 if (function == NULL
0d191295 5146 || !function->value_block ()->contains (b->exp_valid_block))
d8de7963 5147 within_current_scope = false;
edb3359d 5148 }
69fbadd5 5149
edb3359d 5150 if (within_current_scope)
c906108c
SS
5151 /* If we end up stopping, the current frame will get selected
5152 in normal_stop. So this call to select_frame won't affect
5153 the user. */
0f7d239c 5154 select_frame (fr);
c906108c 5155 }
c5aa993b 5156
c906108c
SS
5157 if (within_current_scope)
5158 {
4a64f543 5159 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5160 time before we return to the command level and call
5161 free_all_values. We can't call free_all_values because we
5162 might be in the middle of evaluating a function call. */
c906108c 5163
9c06b0b4 5164 struct value *mark;
fa4727a6
DJ
5165 struct value *new_val;
5166
c1fc2657 5167 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5168 /* Since we don't know the exact trigger address (from
5169 stopped_data_address), just tell the user we've triggered
5170 a mask watchpoint. */
5171 return WP_VALUE_CHANGED;
5172
5173 mark = value_mark ();
1eaebe02 5174 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5175 NULL, NULL, false);
218d2fc6 5176
bb9d5f81
PP
5177 if (b->val_bitsize != 0)
5178 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5179
4a64f543
MS
5180 /* We use value_equal_contents instead of value_equal because
5181 the latter coerces an array to a pointer, thus comparing just
5182 the address of the array instead of its contents. This is
5183 not what we want. */
fa4727a6 5184 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5185 || (b->val != NULL && !value_equal_contents (b->val.get (),
5186 new_val)))
c906108c 5187 {
c906108c 5188 bs->old_val = b->val;
850645cf 5189 b->val = release_value (new_val);
4c1d86d9 5190 b->val_valid = true;
850645cf
TT
5191 if (new_val != NULL)
5192 value_free_to_mark (mark);
c906108c
SS
5193 return WP_VALUE_CHANGED;
5194 }
5195 else
5196 {
60e1c644 5197 /* Nothing changed. */
c906108c 5198 value_free_to_mark (mark);
c906108c
SS
5199 return WP_VALUE_NOT_CHANGED;
5200 }
5201 }
5202 else
5203 {
5204 /* This seems like the only logical thing to do because
dda83cd7
SM
5205 if we temporarily ignored the watchpoint, then when
5206 we reenter the block in which it is valid it contains
5207 garbage (in the case of a function, it may have two
5208 garbage values, one before and one after the prologue).
5209 So we can't even detect the first assignment to it and
5210 watch after that (since the garbage may or may not equal
5211 the first value assigned). */
348d480f 5212 /* We print all the stop information in
c47614fe
TT
5213 breakpointprint_it, but in this case, by the time we
5214 call breakpoint->print_it this bp will be deleted
348d480f
PA
5215 already. So we have no choice but print the information
5216 here. */
468afe6c 5217
0e454242 5218 SWITCH_THRU_ALL_UIS ()
dda83cd7 5219 {
468afe6c
PA
5220 struct ui_out *uiout = current_uiout;
5221
112e8700
SM
5222 if (uiout->is_mi_like_p ())
5223 uiout->field_string
5224 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5225 uiout->message ("\nWatchpoint %pF deleted because the program has "
5226 "left the block in\n"
5227 "which its expression is valid.\n",
5228 signed_field ("wpnum", b->number));
468afe6c 5229 }
4ce44c66 5230
cdac0397 5231 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5232 b->commands = NULL;
d0fb5eae 5233 watchpoint_del_at_next_stop (b);
c906108c
SS
5234
5235 return WP_DELETED;
5236 }
5237}
5238
18a18393 5239/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5240 breakpoint location BL. This function does not check if we should
5241 stop, only if BL explains the stop. */
5242
d8de7963 5243static bool
6c95b8df 5244bpstat_check_location (const struct bp_location *bl,
accd0bcd 5245 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5246 const target_waitstatus &ws)
18a18393
VP
5247{
5248 struct breakpoint *b = bl->owner;
5249
348d480f 5250 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5251 gdb_assert (b != NULL);
5252
c47614fe 5253 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5254}
5255
3a5c3e22 5256/* Determine if the watched values have actually changed, and we
4ec2227a 5257 should stop. If not, set BS->stop to false. */
3a5c3e22 5258
18a18393 5259static void
313f3b21 5260bpstat_check_watchpoint (bpstat *bs)
18a18393 5261{
2bdf28a0 5262 const struct bp_location *bl;
3a5c3e22 5263 struct watchpoint *b;
2bdf28a0
JK
5264
5265 /* BS is built for existing struct breakpoint. */
b6433ede 5266 bl = bs->bp_location_at.get ();
2bdf28a0 5267 gdb_assert (bl != NULL);
3a5c3e22 5268 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5269 gdb_assert (b != NULL);
18a18393 5270
18a18393 5271 {
d8de7963
AB
5272 bool must_check_value = false;
5273
c1fc2657 5274 if (b->type == bp_watchpoint)
18a18393
VP
5275 /* For a software watchpoint, we must always check the
5276 watched value. */
d8de7963 5277 must_check_value = true;
18a18393
VP
5278 else if (b->watchpoint_triggered == watch_triggered_yes)
5279 /* We have a hardware watchpoint (read, write, or access)
5280 and the target earlier reported an address watched by
5281 this watchpoint. */
d8de7963 5282 must_check_value = true;
18a18393 5283 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5284 && b->type == bp_hardware_watchpoint)
18a18393
VP
5285 /* We were stopped by a hardware watchpoint, but the target could
5286 not report the data address. We must check the watchpoint's
5287 value. Access and read watchpoints are out of luck; without
5288 a data address, we can't figure it out. */
d8de7963 5289 must_check_value = true;
3a5c3e22 5290
18a18393
VP
5291 if (must_check_value)
5292 {
bf469271
PA
5293 wp_check_result e;
5294
a70b8144 5295 try
bf469271
PA
5296 {
5297 e = watchpoint_check (bs);
5298 }
b1ffd112 5299 catch (const gdb_exception_error &ex)
bf469271
PA
5300 {
5301 exception_fprintf (gdb_stderr, ex,
5302 "Error evaluating expression "
5303 "for watchpoint %d\n",
5304 b->number);
5305
5306 SWITCH_THRU_ALL_UIS ()
5307 {
6cb06a8c
TT
5308 gdb_printf (_("Watchpoint %d deleted.\n"),
5309 b->number);
bf469271
PA
5310 }
5311 watchpoint_del_at_next_stop (b);
5312 e = WP_DELETED;
5313 }
bf469271 5314
18a18393
VP
5315 switch (e)
5316 {
5317 case WP_DELETED:
5318 /* We've already printed what needs to be printed. */
5319 bs->print_it = print_it_done;
5320 /* Stop. */
5321 break;
60e1c644
PA
5322 case WP_IGNORE:
5323 bs->print_it = print_it_noop;
4ec2227a 5324 bs->stop = false;
60e1c644 5325 break;
18a18393 5326 case WP_VALUE_CHANGED:
c1fc2657 5327 if (b->type == bp_read_watchpoint)
18a18393 5328 {
85d721b8
PA
5329 /* There are two cases to consider here:
5330
4a64f543 5331 1. We're watching the triggered memory for reads.
85d721b8
PA
5332 In that case, trust the target, and always report
5333 the watchpoint hit to the user. Even though
5334 reads don't cause value changes, the value may
5335 have changed since the last time it was read, and
5336 since we're not trapping writes, we will not see
5337 those, and as such we should ignore our notion of
5338 old value.
5339
4a64f543 5340 2. We're watching the triggered memory for both
85d721b8
PA
5341 reads and writes. There are two ways this may
5342 happen:
5343
4a64f543 5344 2.1. This is a target that can't break on data
85d721b8
PA
5345 reads only, but can break on accesses (reads or
5346 writes), such as e.g., x86. We detect this case
5347 at the time we try to insert read watchpoints.
5348
4a64f543 5349 2.2. Otherwise, the target supports read
85d721b8
PA
5350 watchpoints, but, the user set an access or write
5351 watchpoint watching the same memory as this read
5352 watchpoint.
5353
5354 If we're watching memory writes as well as reads,
5355 ignore watchpoint hits when we find that the
5356 value hasn't changed, as reads don't cause
5357 changes. This still gives false positives when
5358 the program writes the same value to memory as
5359 what there was already in memory (we will confuse
5360 it for a read), but it's much better than
5361 nothing. */
5362
5363 int other_write_watchpoint = 0;
5364
5365 if (bl->watchpoint_type == hw_read)
5366 {
43892fdf 5367 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5368 if (other_b->type == bp_hardware_watchpoint
5369 || other_b->type == bp_access_watchpoint)
85d721b8 5370 {
3a5c3e22
PA
5371 struct watchpoint *other_w =
5372 (struct watchpoint *) other_b;
5373
5374 if (other_w->watchpoint_triggered
5375 == watch_triggered_yes)
5376 {
5377 other_write_watchpoint = 1;
5378 break;
5379 }
85d721b8
PA
5380 }
5381 }
5382
5383 if (other_write_watchpoint
5384 || bl->watchpoint_type == hw_access)
5385 {
5386 /* We're watching the same memory for writes,
5387 and the value changed since the last time we
5388 updated it, so this trap must be for a write.
5389 Ignore it. */
5390 bs->print_it = print_it_noop;
4ec2227a 5391 bs->stop = false;
85d721b8 5392 }
18a18393
VP
5393 }
5394 break;
5395 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5396 if (b->type == bp_hardware_watchpoint
5397 || b->type == bp_watchpoint)
18a18393
VP
5398 {
5399 /* Don't stop: write watchpoints shouldn't fire if
5400 the value hasn't changed. */
5401 bs->print_it = print_it_noop;
4ec2227a 5402 bs->stop = false;
18a18393
VP
5403 }
5404 /* Stop. */
5405 break;
5406 default:
5407 /* Can't happen. */
18a18393
VP
5408 break;
5409 }
5410 }
d8de7963 5411 else /* !must_check_value */
18a18393
VP
5412 {
5413 /* This is a case where some watchpoint(s) triggered, but
5414 not at the address of this watchpoint, or else no
5415 watchpoint triggered after all. So don't print
5416 anything for this watchpoint. */
5417 bs->print_it = print_it_noop;
4ec2227a 5418 bs->stop = false;
18a18393
VP
5419 }
5420 }
5421}
5422
7d4df6a4
DE
5423/* For breakpoints that are currently marked as telling gdb to stop,
5424 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5425 of breakpoint referred to by BS. If we should not stop for this
5426 breakpoint, set BS->stop to 0. */
f431efe5 5427
18a18393 5428static void
313f3b21 5429bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5430{
d8a77e4c
AB
5431 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5432
2bdf28a0
JK
5433 const struct bp_location *bl;
5434 struct breakpoint *b;
bf469271
PA
5435 /* Assume stop. */
5436 bool condition_result = true;
7d4df6a4
DE
5437 struct expression *cond;
5438
5439 gdb_assert (bs->stop);
2bdf28a0
JK
5440
5441 /* BS is built for existing struct breakpoint. */
b6433ede 5442 bl = bs->bp_location_at.get ();
2bdf28a0 5443 gdb_assert (bl != NULL);
f431efe5 5444 b = bs->breakpoint_at;
2bdf28a0 5445 gdb_assert (b != NULL);
18a18393 5446
d8a77e4c
AB
5447 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5448 thread->ptid.to_string ().c_str (),
5449 b->number, find_loc_num_by_location (bl));
5450
b775012e
LM
5451 /* Even if the target evaluated the condition on its end and notified GDB, we
5452 need to do so again since GDB does not know if we stopped due to a
5453 breakpoint or a single step breakpoint. */
5454
18a18393 5455 if (frame_id_p (b->frame_id)
a0cbd650 5456 && b->frame_id != get_stack_frame_id (get_current_frame ()))
18a18393 5457 {
d8a77e4c
AB
5458 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5459 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5460 b->frame_id.to_string ().c_str ());
4ec2227a 5461 bs->stop = false;
7d4df6a4
DE
5462 return;
5463 }
60e1c644 5464
12ab52e9
PA
5465 /* If this is a thread/task-specific breakpoint, don't waste cpu
5466 evaluating the condition if this isn't the specified
5467 thread/task. */
00431a78 5468 if ((b->thread != -1 && b->thread != thread->global_num)
2ecee236 5469 || (b->task != -1 && b->task != ada_get_task_number (thread)))
6c1b0f7b 5470 {
d8a77e4c 5471 infrun_debug_printf ("incorrect thread or task, not stopping");
4ec2227a 5472 bs->stop = false;
6c1b0f7b
DE
5473 return;
5474 }
5475
6dddc817
DE
5476 /* Evaluate extension language breakpoints that have a "stop" method
5477 implemented. */
5478 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5479
7d4df6a4
DE
5480 if (is_watchpoint (b))
5481 {
5482 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5483
4d01a485 5484 cond = w->cond_exp.get ();
7d4df6a4
DE
5485 }
5486 else
4d01a485 5487 cond = bl->cond.get ();
60e1c644 5488
80e0c6dc 5489 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
7d4df6a4 5490 {
d8de7963 5491 bool within_current_scope = true;
7d4df6a4 5492 struct watchpoint * w;
60e1c644 5493
65558ca5
TT
5494 /* We use scoped_value_mark because it could be a long time
5495 before we return to the command level and call
5496 free_all_values. We can't call free_all_values because we
5497 might be in the middle of evaluating a function call. */
5498 scoped_value_mark mark;
7d4df6a4
DE
5499
5500 if (is_watchpoint (b))
5501 w = (struct watchpoint *) b;
5502 else
5503 w = NULL;
5504
5505 /* Need to select the frame, with all that implies so that
5506 the conditions will have the right context. Because we
5507 use the frame, we will not see an inlined function's
5508 variables when we arrive at a breakpoint at the start
5509 of the inlined function; the current frame will be the
5510 call site. */
5511 if (w == NULL || w->cond_exp_valid_block == NULL)
5512 select_frame (get_current_frame ());
5513 else
18a18393 5514 {
bd2b40ac 5515 frame_info_ptr frame;
7d4df6a4
DE
5516
5517 /* For local watchpoint expressions, which particular
5518 instance of a local is being watched matters, so we
5519 keep track of the frame to evaluate the expression
5520 in. To evaluate the condition however, it doesn't
5521 really matter which instantiation of the function
5522 where the condition makes sense triggers the
5523 watchpoint. This allows an expression like "watch
5524 global if q > 10" set in `func', catch writes to
5525 global on all threads that call `func', or catch
5526 writes on all recursive calls of `func' by a single
5527 thread. We simply always evaluate the condition in
5528 the innermost frame that's executing where it makes
5529 sense to evaluate the condition. It seems
5530 intuitive. */
5531 frame = block_innermost_frame (w->cond_exp_valid_block);
5532 if (frame != NULL)
5533 select_frame (frame);
5534 else
d8de7963 5535 within_current_scope = false;
18a18393 5536 }
7d4df6a4 5537 if (within_current_scope)
bf469271 5538 {
a70b8144 5539 try
bf469271
PA
5540 {
5541 condition_result = breakpoint_cond_eval (cond);
5542 }
b1ffd112 5543 catch (const gdb_exception_error &ex)
bf469271
PA
5544 {
5545 exception_fprintf (gdb_stderr, ex,
5546 "Error in testing breakpoint condition:\n");
5547 }
bf469271 5548 }
7d4df6a4 5549 else
18a18393 5550 {
7d4df6a4
DE
5551 warning (_("Watchpoint condition cannot be tested "
5552 "in the current scope"));
5553 /* If we failed to set the right context for this
5554 watchpoint, unconditionally report it. */
18a18393 5555 }
7d4df6a4 5556 /* FIXME-someday, should give breakpoint #. */
18a18393 5557 }
7d4df6a4 5558
80e0c6dc 5559 if (cond != nullptr && !condition_result)
7d4df6a4 5560 {
d8a77e4c 5561 infrun_debug_printf ("condition_result = false, not stopping");
4ec2227a 5562 bs->stop = false;
d8a77e4c 5563 return;
7d4df6a4 5564 }
7d4df6a4
DE
5565 else if (b->ignore_count > 0)
5566 {
d8a77e4c
AB
5567 infrun_debug_printf ("ignore count %d, not stopping",
5568 b->ignore_count);
7d4df6a4 5569 b->ignore_count--;
4ec2227a 5570 bs->stop = false;
7d4df6a4
DE
5571 /* Increase the hit count even though we don't stop. */
5572 ++(b->hit_count);
76727919 5573 gdb::observers::breakpoint_modified.notify (b);
d8a77e4c
AB
5574 return;
5575 }
5576
5577 if (bs->stop)
5578 infrun_debug_printf ("stopping at this breakpoint");
5579 else
5580 infrun_debug_printf ("not stopping at this breakpoint");
18a18393
VP
5581}
5582
1cf4d951
PA
5583/* Returns true if we need to track moribund locations of LOC's type
5584 on the current target. */
5585
d8de7963 5586static bool
b2ff9ed3 5587need_moribund_for_location_type (const struct bp_location *loc)
1cf4d951
PA
5588{
5589 return ((loc->loc_type == bp_loc_software_breakpoint
5590 && !target_supports_stopped_by_sw_breakpoint ())
5591 || (loc->loc_type == bp_loc_hardware_breakpoint
5592 && !target_supports_stopped_by_hw_breakpoint ()));
5593}
5594
ddfe970e 5595/* See breakpoint.h. */
c906108c 5596
313f3b21 5597bpstat *
ddfe970e 5598build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5599 const target_waitstatus &ws)
c906108c 5600{
313f3b21 5601 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5602
43892fdf 5603 for (breakpoint *b : all_breakpoints ())
429374b8 5604 {
1a853c52 5605 if (!breakpoint_enabled (b))
429374b8 5606 continue;
a5606eee 5607
40cb8ca5 5608 for (bp_location *bl : b->locations ())
429374b8 5609 {
4a64f543
MS
5610 /* For hardware watchpoints, we look only at the first
5611 location. The watchpoint_check function will work on the
5612 entire expression, not the individual locations. For
5613 read watchpoints, the watchpoints_triggered function has
5614 checked all locations already. */
429374b8
JK
5615 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5616 break;
18a18393 5617
b5fa468f 5618 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5619 continue;
c5aa993b 5620
09ac7c10 5621 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5622 continue;
c5aa993b 5623
4a64f543
MS
5624 /* Come here if it's a watchpoint, or if the break address
5625 matches. */
c5aa993b 5626
313f3b21 5627 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5628 explain stop. */
c5aa993b 5629
f431efe5
PA
5630 /* Assume we stop. Should we find a watchpoint that is not
5631 actually triggered, or if the condition of the breakpoint
5632 evaluates as false, we'll reset 'stop' to 0. */
4ec2227a
TT
5633 bs->stop = true;
5634 bs->print = true;
d983da9c 5635
f431efe5
PA
5636 /* If this is a scope breakpoint, mark the associated
5637 watchpoint as triggered so that we will handle the
5638 out-of-scope event. We'll get to the watchpoint next
5639 iteration. */
d0fb5eae 5640 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5641 {
5642 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5643
5644 w->watchpoint_triggered = watch_triggered_yes;
5645 }
f431efe5
PA
5646 }
5647 }
5648
7c16b83e 5649 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5650 if (!target_supports_stopped_by_sw_breakpoint ()
5651 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5652 {
1123588c 5653 for (bp_location *loc : moribund_locations)
f431efe5 5654 {
1cf4d951
PA
5655 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5656 && need_moribund_for_location_type (loc))
5657 {
313f3b21 5658 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951 5659 /* For hits of moribund locations, we should just proceed. */
4ec2227a
TT
5660 bs->stop = false;
5661 bs->print = false;
1cf4d951
PA
5662 bs->print_it = print_it_noop;
5663 }
f431efe5
PA
5664 }
5665 }
5666
ddfe970e
KS
5667 return bs_head;
5668}
5669
5670/* See breakpoint.h. */
5671
313f3b21 5672bpstat *
ddfe970e 5673bpstat_stop_status (const address_space *aspace,
00431a78 5674 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5675 const target_waitstatus &ws,
313f3b21 5676 bpstat *stop_chain)
ddfe970e
KS
5677{
5678 struct breakpoint *b = NULL;
5679 /* First item of allocated bpstat's. */
313f3b21
SM
5680 bpstat *bs_head = stop_chain;
5681 bpstat *bs;
ddfe970e
KS
5682 int need_remove_insert;
5683 int removed_any;
5684
5685 /* First, build the bpstat chain with locations that explain a
5686 target stop, while being careful to not set the target running,
5687 as that may invalidate locations (in particular watchpoint
5688 locations are recreated). Resuming will happen here with
5689 breakpoint conditions or watchpoint expressions that include
5690 inferior function calls. */
5691 if (bs_head == NULL)
5692 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5693
edcc5120
TT
5694 /* A bit of special processing for shlib breakpoints. We need to
5695 process solib loading here, so that the lists of loaded and
5696 unloaded libraries are correct before we handle "catch load" and
5697 "catch unload". */
5698 for (bs = bs_head; bs != NULL; bs = bs->next)
5699 {
5d268276 5700 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5701 {
5702 handle_solib_event ();
5703 break;
5704 }
5705 }
5706
f431efe5
PA
5707 /* Now go through the locations that caused the target to stop, and
5708 check whether we're interested in reporting this stop to higher
5709 layers, or whether we should resume the target transparently. */
5710
5711 removed_any = 0;
5712
5760d0ab 5713 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5714 {
5715 if (!bs->stop)
5716 continue;
5717
f431efe5 5718 b = bs->breakpoint_at;
c47614fe 5719 b->check_status (bs);
348d480f 5720 if (bs->stop)
28010a5d 5721 {
00431a78 5722 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5723
429374b8
JK
5724 if (bs->stop)
5725 {
5726 ++(b->hit_count);
c906108c 5727
4a64f543 5728 /* We will stop here. */
429374b8
JK
5729 if (b->disposition == disp_disable)
5730 {
816338b5 5731 --(b->enable_count);
1a853c52 5732 if (b->enable_count <= 0)
429374b8 5733 b->enable_state = bp_disabled;
f431efe5 5734 removed_any = 1;
429374b8 5735 }
bd7ccaa9 5736 gdb::observers::breakpoint_modified.notify (b);
429374b8 5737 if (b->silent)
4ec2227a 5738 bs->print = false;
429374b8 5739 bs->commands = b->commands;
abf85f46 5740 if (command_line_is_silent (bs->commands
d1b0a7bf 5741 ? bs->commands.get () : NULL))
4ec2227a 5742 bs->print = false;
9d6e6e84 5743
c47614fe 5744 b->after_condition_true (bs);
429374b8
JK
5745 }
5746
348d480f 5747 }
a9b3a50f
PA
5748
5749 /* Print nothing for this entry if we don't stop or don't
5750 print. */
5751 if (!bs->stop || !bs->print)
5752 bs->print_it = print_it_noop;
429374b8 5753 }
876fa593 5754
d983da9c
DJ
5755 /* If we aren't stopping, the value of some hardware watchpoint may
5756 not have changed, but the intermediate memory locations we are
5757 watching may have. Don't bother if we're stopping; this will get
5758 done later. */
d832cb68 5759 need_remove_insert = 0;
5760d0ab
JK
5760 if (! bpstat_causes_stop (bs_head))
5761 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5762 if (!bs->stop
f431efe5
PA
5763 && bs->breakpoint_at
5764 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5765 {
3a5c3e22
PA
5766 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5767
d8de7963 5768 update_watchpoint (w, false /* don't reparse. */);
d832cb68 5769 need_remove_insert = 1;
d983da9c
DJ
5770 }
5771
d832cb68 5772 if (need_remove_insert)
44702360 5773 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5774 else if (removed_any)
44702360 5775 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5776
5760d0ab 5777 return bs_head;
c906108c 5778}
628fe4e4 5779
d37e0847
PA
5780/* See breakpoint.h. */
5781
5782bpstat *
5783bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5784 thread_info *thread, const target_waitstatus &ws)
5785{
5786 gdb_assert (!target_stopped_by_watchpoint ());
5787
5788 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5789 previous stop to avoid confusing bpstat_stop_status. */
5790 watchpoints_triggered (ws);
5791
5792 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5793}
5794
628fe4e4 5795static void
61c26be8 5796handle_jit_event (CORE_ADDR address)
628fe4e4 5797{
628fe4e4
JK
5798 struct gdbarch *gdbarch;
5799
1eb8556f 5800 infrun_debug_printf ("handling bp_jit_event");
243a9253 5801
628fe4e4
JK
5802 /* Switch terminal for any messages produced by
5803 breakpoint_re_set. */
223ffa71 5804 target_terminal::ours_for_output ();
628fe4e4 5805
61c26be8
MS
5806 gdbarch = get_frame_arch (get_current_frame ());
5807 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5808 thus it is expected that its objectfile can be found through
5809 minimal symbol lookup. If it doesn't work (and assert fails), it
5810 most likely means that `jit_breakpoint_re_set` was changes and this
5811 function needs to be updated too. */
5812 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5813 gdb_assert (jit_bp_sym.objfile != nullptr);
508ccf9b
TV
5814 objfile *objfile = jit_bp_sym.objfile;
5815 if (objfile->separate_debug_objfile_backlink)
5816 objfile = objfile->separate_debug_objfile_backlink;
5817 jit_event_handler (gdbarch, objfile);
628fe4e4 5818
223ffa71 5819 target_terminal::inferior ();
628fe4e4
JK
5820}
5821
5822/* Prepare WHAT final decision for infrun. */
5823
5824/* Decide what infrun needs to do with this bpstat. */
5825
c906108c 5826struct bpstat_what
313f3b21 5827bpstat_what (bpstat *bs_head)
c906108c 5828{
c906108c 5829 struct bpstat_what retval;
313f3b21 5830 bpstat *bs;
c906108c 5831
628fe4e4 5832 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5833 retval.call_dummy = STOP_NONE;
e2d0f980 5834 retval.is_longjmp = false;
628fe4e4 5835
0e30163f 5836 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5837 {
628fe4e4
JK
5838 /* Extract this BS's action. After processing each BS, we check
5839 if its action overrides all we've seem so far. */
5840 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5841 enum bptype bptype;
5842
c906108c 5843 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5844 {
5845 /* I suspect this can happen if it was a momentary
5846 breakpoint which has since been deleted. */
5847 bptype = bp_none;
5848 }
20874c92 5849 else
f431efe5 5850 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5851
5852 switch (bptype)
c906108c
SS
5853 {
5854 case bp_none:
628fe4e4 5855 break;
c906108c
SS
5856 case bp_breakpoint:
5857 case bp_hardware_breakpoint:
7c16b83e 5858 case bp_single_step:
c906108c
SS
5859 case bp_until:
5860 case bp_finish:
a9b3a50f 5861 case bp_shlib_event:
c906108c
SS
5862 if (bs->stop)
5863 {
5864 if (bs->print)
628fe4e4 5865 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5866 else
628fe4e4 5867 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5868 }
5869 else
628fe4e4 5870 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5871 break;
5872 case bp_watchpoint:
5873 case bp_hardware_watchpoint:
5874 case bp_read_watchpoint:
5875 case bp_access_watchpoint:
5876 if (bs->stop)
5877 {
5878 if (bs->print)
628fe4e4 5879 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5880 else
628fe4e4 5881 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5882 }
5883 else
628fe4e4
JK
5884 {
5885 /* There was a watchpoint, but we're not stopping.
5886 This requires no further action. */
5887 }
c906108c
SS
5888 break;
5889 case bp_longjmp:
e2e4d78b 5890 case bp_longjmp_call_dummy:
186c406b 5891 case bp_exception:
0a39bb32
PA
5892 if (bs->stop)
5893 {
5894 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5895 retval.is_longjmp = bptype != bp_exception;
5896 }
5897 else
5898 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5899 break;
5900 case bp_longjmp_resume:
186c406b 5901 case bp_exception_resume:
0a39bb32
PA
5902 if (bs->stop)
5903 {
5904 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5905 retval.is_longjmp = bptype == bp_longjmp_resume;
5906 }
5907 else
5908 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5909 break;
5910 case bp_step_resume:
5911 if (bs->stop)
628fe4e4
JK
5912 this_action = BPSTAT_WHAT_STEP_RESUME;
5913 else
c906108c 5914 {
628fe4e4
JK
5915 /* It is for the wrong frame. */
5916 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5917 }
c906108c 5918 break;
2c03e5be
PA
5919 case bp_hp_step_resume:
5920 if (bs->stop)
5921 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5922 else
5923 {
5924 /* It is for the wrong frame. */
5925 this_action = BPSTAT_WHAT_SINGLE;
5926 }
5927 break;
c906108c 5928 case bp_watchpoint_scope:
c4093a6a 5929 case bp_thread_event:
1900040c 5930 case bp_overlay_event:
0fd8e87f 5931 case bp_longjmp_master:
aa7d318d 5932 case bp_std_terminate_master:
186c406b 5933 case bp_exception_master:
628fe4e4 5934 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5935 break;
ce78b96d 5936 case bp_catchpoint:
c5aa993b
JM
5937 if (bs->stop)
5938 {
5939 if (bs->print)
628fe4e4 5940 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5941 else
628fe4e4 5942 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5943 }
5944 else
628fe4e4 5945 {
cb1e4e32
PA
5946 /* Some catchpoints are implemented with breakpoints.
5947 For those, we need to step over the breakpoint. */
6e14e441
PA
5948 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5949 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
cb1e4e32 5950 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5951 }
5952 break;
628fe4e4 5953 case bp_jit_event:
628fe4e4 5954 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5955 break;
c906108c 5956 case bp_call_dummy:
53a5351d
JM
5957 /* Make sure the action is stop (silent or noisy),
5958 so infrun.c pops the dummy frame. */
aa7d318d 5959 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5960 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5961 break;
5962 case bp_std_terminate:
5963 /* Make sure the action is stop (silent or noisy),
5964 so infrun.c pops the dummy frame. */
aa7d318d 5965 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5966 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5967 break;
1042e4c0 5968 case bp_tracepoint:
7a697b8d 5969 case bp_fast_tracepoint:
0fb4aa4b 5970 case bp_static_tracepoint:
7b572efb 5971 case bp_static_marker_tracepoint:
1042e4c0
SS
5972 /* Tracepoint hits should not be reported back to GDB, and
5973 if one got through somehow, it should have been filtered
5974 out already. */
f34652de 5975 internal_error (_("bpstat_what: tracepoint encountered"));
0e30163f
JK
5976 break;
5977 case bp_gnu_ifunc_resolver:
5978 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5979 this_action = BPSTAT_WHAT_SINGLE;
5980 break;
5981 case bp_gnu_ifunc_resolver_return:
5982 /* The breakpoint will be removed, execution will restart from the
5983 PC of the former breakpoint. */
5984 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5985 break;
e7e0cddf
SS
5986
5987 case bp_dprintf:
a11cfd87
HZ
5988 if (bs->stop)
5989 this_action = BPSTAT_WHAT_STOP_SILENT;
5990 else
5991 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5992 break;
5993
628fe4e4 5994 default:
f34652de 5995 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5996 }
628fe4e4 5997
325fac50 5998 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5999 }
628fe4e4 6000
243a9253
PA
6001 return retval;
6002}
628fe4e4 6003
243a9253 6004void
313f3b21 6005bpstat_run_callbacks (bpstat *bs_head)
243a9253 6006{
313f3b21 6007 bpstat *bs;
628fe4e4 6008
0e30163f
JK
6009 for (bs = bs_head; bs != NULL; bs = bs->next)
6010 {
6011 struct breakpoint *b = bs->breakpoint_at;
6012
6013 if (b == NULL)
6014 continue;
6015 switch (b->type)
6016 {
243a9253 6017 case bp_jit_event:
61c26be8 6018 handle_jit_event (bs->bp_location_at->address);
243a9253 6019 break;
0e30163f 6020 case bp_gnu_ifunc_resolver:
74421c0b 6021 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
0e30163f
JK
6022 break;
6023 case bp_gnu_ifunc_resolver_return:
74421c0b 6024 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
0e30163f
JK
6025 break;
6026 }
6027 }
c906108c
SS
6028}
6029
4c462cb0 6030/* See breakpoint.h. */
c906108c 6031
4c462cb0
SM
6032bool
6033bpstat_should_step ()
c906108c 6034{
43892fdf 6035 for (breakpoint *b : all_breakpoints ())
717a8278 6036 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 6037 return true;
43892fdf 6038
4c462cb0 6039 return false;
c906108c
SS
6040}
6041
4c462cb0
SM
6042/* See breakpoint.h. */
6043
6044bool
313f3b21 6045bpstat_causes_stop (bpstat *bs)
67822962
PA
6046{
6047 for (; bs != NULL; bs = bs->next)
6048 if (bs->stop)
4c462cb0 6049 return true;
67822962 6050
4c462cb0 6051 return false;
67822962
PA
6052}
6053
c906108c 6054\f
c5aa993b 6055
6c92c339 6056/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
6057 so it starts at the position corresponding to the table column
6058 named COL_NAME in the currently active table of UIOUT. */
6059
6c92c339 6060static int
170b53b2
UW
6061wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6062{
170b53b2 6063 int i, total_width, width, align;
c5209615 6064 const char *text;
170b53b2
UW
6065
6066 total_width = 0;
112e8700 6067 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6068 {
6069 if (strcmp (text, col_name) == 0)
6c92c339 6070 return total_width;
170b53b2
UW
6071
6072 total_width += width + 1;
6073 }
6074
6c92c339 6075 return 0;
170b53b2
UW
6076}
6077
b775012e
LM
6078/* Determine if the locations of this breakpoint will have their conditions
6079 evaluated by the target, host or a mix of both. Returns the following:
6080
6081 "host": Host evals condition.
6082 "host or target": Host or Target evals condition.
6083 "target": Target evals condition.
6084*/
6085
6086static const char *
a67bcaba 6087bp_condition_evaluator (const breakpoint *b)
b775012e 6088{
b775012e
LM
6089 char host_evals = 0;
6090 char target_evals = 0;
6091
6092 if (!b)
6093 return NULL;
6094
6095 if (!is_breakpoint (b))
6096 return NULL;
6097
6098 if (gdb_evaluates_breakpoint_condition_p ()
6099 || !target_supports_evaluation_of_breakpoint_conditions ())
6100 return condition_evaluation_host;
6101
40cb8ca5 6102 for (bp_location *bl : b->locations ())
b775012e
LM
6103 {
6104 if (bl->cond_bytecode)
6105 target_evals++;
6106 else
6107 host_evals++;
6108 }
6109
6110 if (host_evals && target_evals)
6111 return condition_evaluation_both;
6112 else if (target_evals)
6113 return condition_evaluation_target;
6114 else
6115 return condition_evaluation_host;
6116}
6117
6118/* Determine the breakpoint location's condition evaluator. This is
6119 similar to bp_condition_evaluator, but for locations. */
6120
6121static const char *
b2ff9ed3 6122bp_location_condition_evaluator (const struct bp_location *bl)
b775012e
LM
6123{
6124 if (bl && !is_breakpoint (bl->owner))
6125 return NULL;
6126
6127 if (gdb_evaluates_breakpoint_condition_p ()
6128 || !target_supports_evaluation_of_breakpoint_conditions ())
6129 return condition_evaluation_host;
6130
6131 if (bl && bl->cond_bytecode)
6132 return condition_evaluation_target;
6133 else
6134 return condition_evaluation_host;
6135}
6136
859825b8
JK
6137/* Print the LOC location out of the list of B->LOC locations. */
6138
170b53b2 6139static void
a67bcaba 6140print_breakpoint_location (const breakpoint *b,
170b53b2 6141 struct bp_location *loc)
0d381245 6142{
79a45e25 6143 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6144
6145 scoped_restore_current_program_space restore_pspace;
6c95b8df 6146
859825b8
JK
6147 if (loc != NULL && loc->shlib_disabled)
6148 loc = NULL;
6149
6c95b8df
PA
6150 if (loc != NULL)
6151 set_current_program_space (loc->pspace);
6152
56435ebe 6153 if (b->display_canonical)
709438c7 6154 uiout->field_string ("what", b->locspec->to_string ());
2f202fde 6155 else if (loc && loc->symtab)
0d381245 6156 {
4a27f119
KS
6157 const struct symbol *sym = loc->symbol;
6158
0d381245
VP
6159 if (sym)
6160 {
112e8700 6161 uiout->text ("in ");
987012b8 6162 uiout->field_string ("func", sym->print_name (),
e43b10e1 6163 function_name_style.style ());
112e8700
SM
6164 uiout->text (" ");
6165 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6166 uiout->text ("at ");
0d381245 6167 }
112e8700 6168 uiout->field_string ("file",
cbe56571 6169 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6170 file_name_style.style ());
112e8700 6171 uiout->text (":");
05cba821 6172
112e8700
SM
6173 if (uiout->is_mi_like_p ())
6174 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6175
381befee 6176 uiout->field_signed ("line", loc->line_number);
0d381245 6177 }
859825b8 6178 else if (loc)
0d381245 6179 {
d7e74731 6180 string_file stb;
170b53b2 6181
d7e74731 6182 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6183 demangle, "");
112e8700 6184 uiout->field_stream ("at", stb);
0d381245 6185 }
859825b8 6186 else
f00aae0f 6187 {
709438c7 6188 uiout->field_string ("pending", b->locspec->to_string ());
f00aae0f
KS
6189 /* If extra_string is available, it could be holding a condition
6190 or dprintf arguments. In either case, make sure it is printed,
6191 too, but only for non-MI streams. */
112e8700 6192 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6193 {
6194 if (b->type == bp_dprintf)
112e8700 6195 uiout->text (",");
f00aae0f 6196 else
112e8700 6197 uiout->text (" ");
6f781ee3 6198 uiout->text (b->extra_string.get ());
f00aae0f
KS
6199 }
6200 }
6c95b8df 6201
b775012e
LM
6202 if (loc && is_breakpoint (b)
6203 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6204 && bp_condition_evaluator (b) == condition_evaluation_both)
6205 {
112e8700
SM
6206 uiout->text (" (");
6207 uiout->field_string ("evaluated-by",
b775012e 6208 bp_location_condition_evaluator (loc));
112e8700 6209 uiout->text (")");
b775012e 6210 }
0d381245
VP
6211}
6212
269b11a2
PA
6213static const char *
6214bptype_string (enum bptype type)
c906108c 6215{
c4093a6a
JM
6216 struct ep_type_description
6217 {
6218 enum bptype type;
a121b7c1 6219 const char *description;
c4093a6a
JM
6220 };
6221 static struct ep_type_description bptypes[] =
c906108c 6222 {
c5aa993b
JM
6223 {bp_none, "?deleted?"},
6224 {bp_breakpoint, "breakpoint"},
c906108c 6225 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6226 {bp_single_step, "sw single-step"},
c5aa993b
JM
6227 {bp_until, "until"},
6228 {bp_finish, "finish"},
6229 {bp_watchpoint, "watchpoint"},
c906108c 6230 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6231 {bp_read_watchpoint, "read watchpoint"},
6232 {bp_access_watchpoint, "acc watchpoint"},
6233 {bp_longjmp, "longjmp"},
6234 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6235 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6236 {bp_exception, "exception"},
6237 {bp_exception_resume, "exception resume"},
c5aa993b 6238 {bp_step_resume, "step resume"},
2c03e5be 6239 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6240 {bp_watchpoint_scope, "watchpoint scope"},
6241 {bp_call_dummy, "call dummy"},
aa7d318d 6242 {bp_std_terminate, "std::terminate"},
c5aa993b 6243 {bp_shlib_event, "shlib events"},
c4093a6a 6244 {bp_thread_event, "thread events"},
1900040c 6245 {bp_overlay_event, "overlay events"},
0fd8e87f 6246 {bp_longjmp_master, "longjmp master"},
aa7d318d 6247 {bp_std_terminate_master, "std::terminate master"},
186c406b 6248 {bp_exception_master, "exception master"},
ce78b96d 6249 {bp_catchpoint, "catchpoint"},
1042e4c0 6250 {bp_tracepoint, "tracepoint"},
7a697b8d 6251 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6252 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6253 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6254 {bp_dprintf, "dprintf"},
4efc6507 6255 {bp_jit_event, "jit events"},
0e30163f
JK
6256 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6257 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6258 };
269b11a2
PA
6259
6260 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6261 || ((int) type != bptypes[(int) type].type))
f34652de 6262 internal_error (_("bptypes table does not describe type #%d."),
269b11a2
PA
6263 (int) type);
6264
6265 return bptypes[(int) type].description;
6266}
6267
998580f1
MK
6268/* For MI, output a field named 'thread-groups' with a list as the value.
6269 For CLI, prefix the list with the string 'inf'. */
6270
6271static void
6272output_thread_groups (struct ui_out *uiout,
6273 const char *field_name,
5c632425 6274 const std::vector<int> &inf_nums,
998580f1
MK
6275 int mi_only)
6276{
112e8700 6277 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6278
6279 /* For backward compatibility, don't display inferiors in CLI unless
6280 there are several. Always display them for MI. */
6281 if (!is_mi && mi_only)
6282 return;
6283
10f489e5 6284 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6285
5c632425 6286 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6287 {
6288 if (is_mi)
6289 {
6290 char mi_group[10];
6291
5c632425 6292 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6293 uiout->field_string (NULL, mi_group);
998580f1
MK
6294 }
6295 else
6296 {
6297 if (i == 0)
112e8700 6298 uiout->text (" inf ");
998580f1 6299 else
112e8700 6300 uiout->text (", ");
998580f1 6301
5c632425 6302 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6303 }
6304 }
998580f1
MK
6305}
6306
9db0d853
SM
6307/* See breakpoint.h. */
6308
6309bool fix_breakpoint_script_output_globally = false;
6310
a38118e5
PA
6311/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6312 instead of going via breakpoint_ops::print_one. This makes "maint
6313 info breakpoints" show the software breakpoint locations of
6314 catchpoints, which are considered internal implementation
c01e038b
TT
6315 detail. Returns true if RAW_LOC is false and if the breakpoint's
6316 print_one method did something; false otherwise. */
269b11a2 6317
c01e038b 6318static bool
269b11a2
PA
6319print_one_breakpoint_location (struct breakpoint *b,
6320 struct bp_location *loc,
6321 int loc_number,
6322 struct bp_location **last_loc,
a38118e5 6323 int allflag, bool raw_loc)
269b11a2
PA
6324{
6325 struct command_line *l;
c2c6d25f 6326 static char bpenables[] = "nynny";
c906108c 6327
79a45e25 6328 struct ui_out *uiout = current_uiout;
d8de7963
AB
6329 bool header_of_multiple = false;
6330 bool part_of_multiple = (loc != NULL);
79a45b7d
TT
6331 struct value_print_options opts;
6332
6333 get_user_print_options (&opts);
0d381245
VP
6334
6335 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6336 /* See comment in print_one_breakpoint concerning treatment of
6337 breakpoints with single disabled location. */
0d381245
VP
6338 if (loc == NULL
6339 && (b->loc != NULL
b5fa468f
TBA
6340 && (b->loc->next != NULL
6341 || !b->loc->enabled || b->loc->disabled_by_cond)))
d8de7963 6342 header_of_multiple = true;
0d381245
VP
6343 if (loc == NULL)
6344 loc = b->loc;
6345
c4093a6a
JM
6346 annotate_record ();
6347
6348 /* 1 */
6349 annotate_field (0);
0d381245 6350 if (part_of_multiple)
528e1572 6351 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6352 else
381befee 6353 uiout->field_signed ("number", b->number);
c4093a6a
JM
6354
6355 /* 2 */
6356 annotate_field (1);
0d381245 6357 if (part_of_multiple)
112e8700 6358 uiout->field_skip ("type");
269b11a2 6359 else
112e8700 6360 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6361
6362 /* 3 */
6363 annotate_field (2);
0d381245 6364 if (part_of_multiple)
112e8700 6365 uiout->field_skip ("disp");
0d381245 6366 else
112e8700 6367 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6368
c4093a6a
JM
6369 /* 4 */
6370 annotate_field (3);
0d381245 6371 if (part_of_multiple)
fbcda577
PA
6372 {
6373 /* For locations that are disabled because of an invalid
6374 condition, display "N*" on the CLI, where "*" refers to a
6375 footnote below the table. For MI, simply display a "N"
6376 without a footnote. On the CLI, for enabled locations whose
6377 breakpoint is disabled, display "y-". */
6378 auto get_enable_state = [uiout, loc] () -> const char *
287de656 6379 {
fbcda577
PA
6380 if (uiout->is_mi_like_p ())
6381 {
6382 if (loc->disabled_by_cond)
6383 return "N";
6384 else if (!loc->enabled)
6385 return "n";
6386 else
6387 return "y";
6388 }
6389 else
6390 {
6391 if (loc->disabled_by_cond)
6392 return "N*";
6393 else if (!loc->enabled)
6394 return "n";
6395 else if (!breakpoint_enabled (loc->owner))
6396 return "y-";
6397 else
6398 return "y";
6399 }
6400 };
6401 uiout->field_string ("enabled", get_enable_state ());
6402 }
0d381245 6403 else
112e8700 6404 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6405
c4093a6a 6406 /* 5 and 6 */
c01e038b 6407 bool result = false;
c47614fe 6408 if (!raw_loc && b->print_one (last_loc))
c01e038b 6409 result = true;
3086aeae 6410 else
a38118e5
PA
6411 {
6412 if (is_watchpoint (b))
3a5c3e22
PA
6413 {
6414 struct watchpoint *w = (struct watchpoint *) b;
6415
6416 /* Field 4, the address, is omitted (which makes the columns
6417 not line up too nicely with the headers, but the effect
6418 is relatively readable). */
6419 if (opts.addressprint)
112e8700 6420 uiout->field_skip ("addr");
3a5c3e22 6421 annotate_field (5);
a4c50be3 6422 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6423 }
f06f1252
TT
6424 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6425 || is_ada_exception_catchpoint (b))
a38118e5
PA
6426 {
6427 if (opts.addressprint)
6428 {
6429 annotate_field (4);
6430 if (header_of_multiple)
7f6aba03
TT
6431 uiout->field_string ("addr", "<MULTIPLE>",
6432 metadata_style.style ());
a38118e5 6433 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6434 uiout->field_string ("addr", "<PENDING>",
6435 metadata_style.style ());
a38118e5
PA
6436 else
6437 uiout->field_core_addr ("addr",
6438 loc->gdbarch, loc->address);
6439 }
6440 annotate_field (5);
6441 if (!header_of_multiple)
6442 print_breakpoint_location (b, loc);
6443 if (b->loc)
6444 *last_loc = b->loc;
6445 }
6446 }
6c95b8df 6447
998580f1 6448 if (loc != NULL && !header_of_multiple)
6c95b8df 6449 {
5c632425 6450 std::vector<int> inf_nums;
998580f1 6451 int mi_only = 1;
6c95b8df 6452
08036331 6453 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6454 {
6455 if (inf->pspace == loc->pspace)
5c632425 6456 inf_nums.push_back (inf->num);
6c95b8df 6457 }
998580f1 6458
dda83cd7 6459 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6460 there are several. Always display for MI. */
6461 if (allflag
6462 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6463 && (program_spaces.size () > 1
998580f1
MK
6464 || number_of_inferiors () > 1)
6465 /* LOC is for existing B, it cannot be in
6466 moribund_locations and thus having NULL OWNER. */
6467 && loc->owner->type != bp_catchpoint))
6468 mi_only = 0;
5c632425 6469 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6470 }
6471
944b1b18
AB
6472 /* In the MI output, each location of a thread or task specific
6473 breakpoint includes the relevant thread or task ID. This is done for
6474 backwards compatibility reasons.
6475
6476 For the CLI output, the thread/task information is printed on a
6477 separate line, see the 'stop only in thread' and 'stop only in task'
6478 output below. */
2fd9a436 6479 if (part_of_multiple && uiout->is_mi_like_p ())
c4093a6a 6480 {
4a306c9a 6481 if (b->thread != -1)
944b1b18 6482 uiout->field_signed ("thread", b->thread);
2ecee236 6483 else if (b->task != -1)
944b1b18 6484 uiout->field_signed ("task", b->task);
c4093a6a 6485 }
f1310107 6486
112e8700 6487 uiout->text ("\n");
f1310107 6488
348d480f 6489 if (!part_of_multiple)
c47614fe 6490 b->print_one_detail (uiout);
f1310107 6491
0d381245 6492 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6493 {
6494 annotate_field (6);
112e8700 6495 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6496 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6497 the frame ID. */
112e8700 6498 uiout->field_core_addr ("frame",
5af949e3 6499 b->gdbarch, b->frame_id.stack_addr);
112e8700 6500 uiout->text ("\n");
c4093a6a
JM
6501 }
6502
28010a5d 6503 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6504 {
6505 annotate_field (7);
d77f58be 6506 if (is_tracepoint (b))
112e8700 6507 uiout->text ("\ttrace only if ");
1042e4c0 6508 else
112e8700 6509 uiout->text ("\tstop only if ");
6f781ee3 6510 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6511
6512 /* Print whether the target is doing the breakpoint's condition
6513 evaluation. If GDB is doing the evaluation, don't print anything. */
6514 if (is_breakpoint (b)
6515 && breakpoint_condition_evaluation_mode ()
6516 == condition_evaluation_target)
6517 {
6a831f06
PA
6518 uiout->message (" (%pF evals)",
6519 string_field ("evaluated-by",
6520 bp_condition_evaluator (b)));
b775012e 6521 }
112e8700 6522 uiout->text ("\n");
0101ce28
JJ
6523 }
6524
0d381245 6525 if (!part_of_multiple && b->thread != -1)
c4093a6a 6526 {
4a64f543 6527 /* FIXME should make an annotation for this. */
112e8700
SM
6528 uiout->text ("\tstop only in thread ");
6529 if (uiout->is_mi_like_p ())
381befee 6530 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6531 else
6532 {
6533 struct thread_info *thr = find_thread_global_id (b->thread);
6534
112e8700 6535 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6536 }
112e8700 6537 uiout->text ("\n");
c4093a6a 6538 }
944b1b18 6539
2ecee236 6540 if (!part_of_multiple && b->task != -1)
944b1b18
AB
6541 {
6542 uiout->text ("\tstop only in task ");
6543 uiout->field_signed ("task", b->task);
6544 uiout->text ("\n");
6545 }
6546
556ec64d
YQ
6547 if (!part_of_multiple)
6548 {
6549 if (b->hit_count)
31f56a27
YQ
6550 {
6551 /* FIXME should make an annotation for this. */
6552 if (is_catchpoint (b))
112e8700 6553 uiout->text ("\tcatchpoint");
31f56a27 6554 else if (is_tracepoint (b))
112e8700 6555 uiout->text ("\ttracepoint");
31f56a27 6556 else
112e8700
SM
6557 uiout->text ("\tbreakpoint");
6558 uiout->text (" already hit ");
381befee 6559 uiout->field_signed ("times", b->hit_count);
31f56a27 6560 if (b->hit_count == 1)
112e8700 6561 uiout->text (" time\n");
31f56a27 6562 else
112e8700 6563 uiout->text (" times\n");
31f56a27 6564 }
556ec64d
YQ
6565 else
6566 {
31f56a27 6567 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6568 if (uiout->is_mi_like_p ())
381befee 6569 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6570 }
6571 }
8b93c638 6572
0d381245 6573 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6574 {
6575 annotate_field (8);
6a831f06
PA
6576 uiout->message ("\tignore next %pF hits\n",
6577 signed_field ("ignore", b->ignore_count));
c4093a6a 6578 }
059fb39f 6579
816338b5
SS
6580 /* Note that an enable count of 1 corresponds to "enable once"
6581 behavior, which is reported by the combination of enablement and
6582 disposition, so we don't need to mention it here. */
6583 if (!part_of_multiple && b->enable_count > 1)
6584 {
6585 annotate_field (8);
112e8700 6586 uiout->text ("\tdisable after ");
816338b5
SS
6587 /* Tweak the wording to clarify that ignore and enable counts
6588 are distinct, and have additive effect. */
6589 if (b->ignore_count)
112e8700 6590 uiout->text ("additional ");
816338b5 6591 else
112e8700 6592 uiout->text ("next ");
381befee 6593 uiout->field_signed ("enable", b->enable_count);
112e8700 6594 uiout->text (" hits\n");
816338b5
SS
6595 }
6596
f196051f
SS
6597 if (!part_of_multiple && is_tracepoint (b))
6598 {
6599 struct tracepoint *tp = (struct tracepoint *) b;
6600
6601 if (tp->traceframe_usage)
6602 {
112e8700 6603 uiout->text ("\ttrace buffer usage ");
381befee 6604 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6605 uiout->text (" bytes\n");
f196051f
SS
6606 }
6607 }
d3ce09f5 6608
d1b0a7bf 6609 l = b->commands ? b->commands.get () : NULL;
059fb39f 6610 if (!part_of_multiple && l)
c4093a6a
JM
6611 {
6612 annotate_field (9);
9db0d853
SM
6613
6614 bool use_fixed_output =
6615 (uiout->test_flags (fix_breakpoint_script_output)
287de656 6616 || fix_breakpoint_script_output_globally);
9db0d853
SM
6617
6618 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6619 gdb::optional<ui_out_emit_list> list_emitter;
6620
6621 if (use_fixed_output)
6622 list_emitter.emplace (uiout, "script");
6623 else
6624 tuple_emitter.emplace (uiout, "script");
6625
8b93c638 6626 print_command_lines (uiout, l, 4);
c4093a6a 6627 }
d24317b4 6628
d9b3f62e 6629 if (is_tracepoint (b))
1042e4c0 6630 {
d9b3f62e
PA
6631 struct tracepoint *t = (struct tracepoint *) b;
6632
6633 if (!part_of_multiple && t->pass_count)
6634 {
6635 annotate_field (10);
112e8700 6636 uiout->text ("\tpass count ");
381befee 6637 uiout->field_signed ("pass", t->pass_count);
112e8700 6638 uiout->text (" \n");
d9b3f62e 6639 }
f2a8bc8a
YQ
6640
6641 /* Don't display it when tracepoint or tracepoint location is
6642 pending. */
6643 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6644 {
6645 annotate_field (11);
6646
112e8700
SM
6647 if (uiout->is_mi_like_p ())
6648 uiout->field_string ("installed",
f2a8bc8a
YQ
6649 loc->inserted ? "y" : "n");
6650 else
6651 {
6652 if (loc->inserted)
112e8700 6653 uiout->text ("\t");
f2a8bc8a 6654 else
112e8700
SM
6655 uiout->text ("\tnot ");
6656 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6657 }
6658 }
1042e4c0
SS
6659 }
6660
112e8700 6661 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6662 {
3a5c3e22
PA
6663 if (is_watchpoint (b))
6664 {
6665 struct watchpoint *w = (struct watchpoint *) b;
6666
a4c50be3 6667 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6668 }
264f9890
PA
6669 else if (b->locspec != nullptr)
6670 {
709438c7 6671 const char *str = b->locspec->to_string ();
264f9890
PA
6672 if (str != nullptr)
6673 uiout->field_string ("original-location", str);
6674 }
d24317b4 6675 }
c01e038b
TT
6676
6677 return result;
c4093a6a 6678}
c5aa993b 6679
13674803
SM
6680/* See breakpoint.h. */
6681
6682bool fix_multi_location_breakpoint_output_globally = false;
6683
0d381245
VP
6684static void
6685print_one_breakpoint (struct breakpoint *b,
4a64f543 6686 struct bp_location **last_loc,
6c95b8df 6687 int allflag)
0d381245 6688{
79a45e25 6689 struct ui_out *uiout = current_uiout;
13674803
SM
6690 bool use_fixed_output
6691 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6692 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6693
b4be1b06 6694 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
c01e038b
TT
6695 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6696 allflag, false);
8d3788bd 6697
b4be1b06
SM
6698 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6699 are outside. */
6700 if (!use_fixed_output)
6701 bkpt_tuple_emitter.reset ();
0d381245
VP
6702
6703 /* If this breakpoint has custom print function,
6704 it's already printed. Otherwise, print individual
6705 locations, if any. */
c01e038b 6706 if (!printed || allflag)
0d381245 6707 {
4a64f543
MS
6708 /* If breakpoint has a single location that is disabled, we
6709 print it as if it had several locations, since otherwise it's
6710 hard to represent "breakpoint enabled, location disabled"
6711 situation.
6712
6713 Note that while hardware watchpoints have several locations
a38118e5
PA
6714 internally, that's not a property exposed to users.
6715
6716 Likewise, while catchpoints may be implemented with
6717 breakpoints (e.g., catch throw), that's not a property
6718 exposed to users. We do however display the internal
6719 breakpoint locations with "maint info breakpoints". */
6720 if (!is_hardware_watchpoint (b)
f06f1252
TT
6721 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6722 || is_ada_exception_catchpoint (b))
a38118e5 6723 && (allflag
b5fa468f
TBA
6724 || (b->loc && (b->loc->next
6725 || !b->loc->enabled
6726 || b->loc->disabled_by_cond))))
0d381245 6727 {
b4be1b06
SM
6728 gdb::optional<ui_out_emit_list> locations_list;
6729
6730 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6731 MI record. For later versions, place breakpoint locations in a
6732 list. */
6733 if (uiout->is_mi_like_p () && use_fixed_output)
6734 locations_list.emplace (uiout, "locations");
8d3788bd 6735
b4be1b06 6736 int n = 1;
40cb8ca5 6737 for (bp_location *loc : b->locations ())
8d3788bd 6738 {
b4be1b06 6739 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6740 print_one_breakpoint_location (b, loc, n, last_loc,
6741 allflag, allflag);
40cb8ca5 6742 n++;
8d3788bd 6743 }
0d381245
VP
6744 }
6745 }
6746}
6747
a6d9a66e
UW
6748static int
6749breakpoint_address_bits (struct breakpoint *b)
6750{
6751 int print_address_bits = 0;
a6d9a66e 6752
40cb8ca5 6753 for (bp_location *loc : b->locations ())
a6d9a66e 6754 {
6e14e441
PA
6755 if (!bl_address_is_meaningful (loc))
6756 continue;
c7437ca6 6757
6e14e441 6758 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6759 if (addr_bit > print_address_bits)
6760 print_address_bits = addr_bit;
6761 }
6762
6763 return print_address_bits;
6764}
0d381245 6765
65630365 6766/* See breakpoint.h. */
c5aa993b 6767
65630365
PA
6768void
6769print_breakpoint (breakpoint *b)
c4093a6a 6770{
a6d9a66e 6771 struct bp_location *dummy_loc = NULL;
65630365 6772 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6773}
c5aa993b 6774
09d682a4
TT
6775/* Return true if this breakpoint was set by the user, false if it is
6776 internal or momentary. */
6777
6778int
6779user_breakpoint_p (struct breakpoint *b)
6780{
46c6471b 6781 return b->number > 0;
09d682a4
TT
6782}
6783
93daf339
TT
6784/* See breakpoint.h. */
6785
6786int
6787pending_breakpoint_p (struct breakpoint *b)
6788{
6789 return b->loc == NULL;
6790}
6791
5c458ae8
SM
6792/* Print information on breakpoints (including watchpoints and tracepoints).
6793
6794 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6795 understood by number_or_range_parser. Only breakpoints included in this
6796 list are then printed.
6797
6798 If SHOW_INTERNAL is true, print internal breakpoints.
6799
6800 If FILTER is non-NULL, call it on each breakpoint and only include the
6801 ones for which it returns true.
6802
6803 Return the total number of breakpoints listed. */
c906108c 6804
d77f58be 6805static int
5c458ae8 6806breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6807 bool (*filter) (const struct breakpoint *))
c4093a6a 6808{
a6d9a66e 6809 struct bp_location *last_loc = NULL;
7f3b0473 6810 int nr_printable_breakpoints;
79a45b7d 6811 struct value_print_options opts;
a6d9a66e 6812 int print_address_bits = 0;
269b11a2 6813 int print_type_col_width = 14;
79a45e25 6814 struct ui_out *uiout = current_uiout;
b5fa468f 6815 bool has_disabled_by_cond_location = false;
269b11a2 6816
79a45b7d
TT
6817 get_user_print_options (&opts);
6818
4a64f543
MS
6819 /* Compute the number of rows in the table, as well as the size
6820 required for address fields. */
7f3b0473 6821 nr_printable_breakpoints = 0;
43892fdf 6822 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6823 {
6824 /* If we have a filter, only list the breakpoints it accepts. */
6825 if (filter && !filter (b))
6826 continue;
6827
5c458ae8 6828 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6829 accept. Skip the others. */
5c458ae8 6830 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6831 {
5c458ae8 6832 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6833 continue;
5c458ae8 6834 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6835 continue;
6836 }
269b11a2 6837
5c458ae8 6838 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6839 {
6840 int addr_bit, type_len;
a6d9a66e 6841
e5a67952
MS
6842 addr_bit = breakpoint_address_bits (b);
6843 if (addr_bit > print_address_bits)
6844 print_address_bits = addr_bit;
269b11a2 6845
e5a67952
MS
6846 type_len = strlen (bptype_string (b->type));
6847 if (type_len > print_type_col_width)
6848 print_type_col_width = type_len;
6849
6850 nr_printable_breakpoints++;
6851 }
6852 }
7f3b0473 6853
4a2b031d
TT
6854 {
6855 ui_out_emit_table table_emitter (uiout,
6856 opts.addressprint ? 6 : 5,
6857 nr_printable_breakpoints,
6858 "BreakpointTable");
6859
6860 if (nr_printable_breakpoints > 0)
6861 annotate_breakpoints_headers ();
6862 if (nr_printable_breakpoints > 0)
6863 annotate_field (0);
6864 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6865 if (nr_printable_breakpoints > 0)
6866 annotate_field (1);
6867 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6868 if (nr_printable_breakpoints > 0)
6869 annotate_field (2);
6870 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6871 if (nr_printable_breakpoints > 0)
6872 annotate_field (3);
6873 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6874 if (opts.addressprint)
6875 {
6876 if (nr_printable_breakpoints > 0)
6877 annotate_field (4);
6878 if (print_address_bits <= 32)
6879 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6880 else
6881 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6882 }
6883 if (nr_printable_breakpoints > 0)
6884 annotate_field (5);
6885 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6886 uiout->table_body ();
6887 if (nr_printable_breakpoints > 0)
6888 annotate_breakpoints_table ();
6889
43892fdf 6890 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6891 {
6892 QUIT;
6893 /* If we have a filter, only list the breakpoints it accepts. */
6894 if (filter && !filter (b))
6895 continue;
e5a67952 6896
5c458ae8 6897 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6898 accept. Skip the others. */
e5a67952 6899
5c458ae8 6900 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6901 {
5c458ae8 6902 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6903 {
5c458ae8 6904 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6905 continue;
6906 }
6907 else /* all others */
6908 {
5c458ae8 6909 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6910 continue;
6911 }
6912 }
6913 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6914 show_internal is set. */
6915 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6916 {
6917 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6918 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6919 if (loc->disabled_by_cond)
6920 has_disabled_by_cond_location = true;
6921 }
4a2b031d
TT
6922 }
6923 }
698384cd 6924
7f3b0473 6925 if (nr_printable_breakpoints == 0)
c906108c 6926 {
4a64f543
MS
6927 /* If there's a filter, let the caller decide how to report
6928 empty list. */
d77f58be
SS
6929 if (!filter)
6930 {
5c458ae8 6931 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6932 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6933 else
112e8700 6934 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6935 bp_num_list);
d77f58be 6936 }
c906108c
SS
6937 }
6938 else
c4093a6a 6939 {
a6d9a66e
UW
6940 if (last_loc && !server_command)
6941 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6942
85c88e2a 6943 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6944 uiout->message (_("(*): Breakpoint condition is invalid at this "
6945 "location.\n"));
c4093a6a 6946 }
c906108c 6947
4a64f543 6948 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6949 there have been breakpoints? */
c906108c 6950 annotate_breakpoints_table_end ();
d77f58be
SS
6951
6952 return nr_printable_breakpoints;
c906108c
SS
6953}
6954
ad443146
SS
6955/* Display the value of default-collect in a way that is generally
6956 compatible with the breakpoint list. */
6957
6958static void
6959default_collect_info (void)
6960{
79a45e25
PA
6961 struct ui_out *uiout = current_uiout;
6962
ad443146
SS
6963 /* If it has no value (which is frequently the case), say nothing; a
6964 message like "No default-collect." gets in user's face when it's
6965 not wanted. */
e0700ba4 6966 if (default_collect.empty ())
ad443146
SS
6967 return;
6968
6969 /* The following phrase lines up nicely with per-tracepoint collect
6970 actions. */
112e8700
SM
6971 uiout->text ("default collect ");
6972 uiout->field_string ("default-collect", default_collect);
6973 uiout->text (" \n");
ad443146
SS
6974}
6975
c906108c 6976static void
0b39b52e 6977info_breakpoints_command (const char *args, int from_tty)
c906108c 6978{
5c458ae8 6979 breakpoint_1 (args, false, NULL);
ad443146
SS
6980
6981 default_collect_info ();
d77f58be
SS
6982}
6983
6984static void
1d12d88f 6985info_watchpoints_command (const char *args, int from_tty)
d77f58be 6986{
5c458ae8 6987 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6988 struct ui_out *uiout = current_uiout;
d77f58be
SS
6989
6990 if (num_printed == 0)
6991 {
e5a67952 6992 if (args == NULL || *args == '\0')
112e8700 6993 uiout->message ("No watchpoints.\n");
d77f58be 6994 else
112e8700 6995 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6996 }
c906108c
SS
6997}
6998
7a292a7a 6999static void
4495129a 7000maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 7001{
5c458ae8 7002 breakpoint_1 (args, true, NULL);
ad443146
SS
7003
7004 default_collect_info ();
c906108c
SS
7005}
7006
d8de7963 7007static bool
714835d5 7008breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7009 struct program_space *pspace,
714835d5 7010 CORE_ADDR pc, struct obj_section *section)
0d381245 7011{
40cb8ca5 7012 for (bp_location *bl : b->locations ())
0d381245 7013 {
6c95b8df
PA
7014 if (bl->pspace == pspace
7015 && bl->address == pc
0d381245 7016 && (!overlay_debugging || bl->section == section))
d8de7963 7017 return true;
0d381245 7018 }
d8de7963 7019 return false;
0d381245
VP
7020}
7021
bd21b6c9 7022/* See breakpoint.h. */
c906108c 7023
bd21b6c9 7024void
6c95b8df
PA
7025describe_other_breakpoints (struct gdbarch *gdbarch,
7026 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7027 struct obj_section *section, int thread)
c906108c 7028{
52f0bd74 7029 int others = 0;
c906108c 7030
43892fdf 7031 for (breakpoint *b : all_breakpoints ())
672f9b60 7032 others += (user_breakpoint_p (b)
dda83cd7 7033 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 7034
c906108c
SS
7035 if (others > 0)
7036 {
a3f17187 7037 if (others == 1)
6cb06a8c 7038 gdb_printf (_("Note: breakpoint "));
a3f17187 7039 else /* if (others == ???) */
6cb06a8c 7040 gdb_printf (_("Note: breakpoints "));
43892fdf 7041 for (breakpoint *b : all_breakpoints ())
672f9b60 7042 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7043 {
7044 others--;
6cb06a8c 7045 gdb_printf ("%d", b->number);
0d381245 7046 if (b->thread == -1 && thread != -1)
6cb06a8c 7047 gdb_printf (" (all threads)");
0d381245 7048 else if (b->thread != -1)
ce068c5f
AB
7049 {
7050 struct thread_info *thr = find_thread_global_id (b->thread);
7051 gdb_printf (" (thread %s)", print_thread_id (thr));
7052 }
8282ad74 7053 else if (b->task != -1)
f1f517e8 7054 gdb_printf (" (task %d)", b->task);
6cb06a8c
TT
7055 gdb_printf ("%s%s ",
7056 ((b->enable_state == bp_disabled
7057 || b->enable_state == bp_call_disabled)
7058 ? " (disabled)"
7059 : ""),
7060 (others > 1) ? ","
7061 : ((others == 1) ? " and" : ""));
0d381245 7062 }
6a831f06
PA
7063 current_uiout->message (_("also set at pc %ps.\n"),
7064 styled_string (address_style.style (),
7065 paddress (gdbarch, pc)));
c906108c
SS
7066 }
7067}
7068\f
c906108c 7069
cb1e4e32
PA
7070/* Return true iff it is meaningful to use the address member of LOC.
7071 For some breakpoint types, the locations' address members are
7072 irrelevant and it makes no sense to attempt to compare them to
7073 other addresses (or use them for any other purpose either).
2d134ed3 7074
cb1e4e32
PA
7075 More specifically, software watchpoints and catchpoints that are
7076 not backed by breakpoints always have a zero valued location
7077 address and we don't want to mark breakpoints of any of these types
7078 to be a duplicate of an actual breakpoint location at address
7079 zero. */
e4f237da 7080
cb1e4e32
PA
7081static bool
7082bl_address_is_meaningful (bp_location *loc)
e4f237da 7083{
cb1e4e32 7084 return loc->loc_type != bp_loc_other;
2d134ed3
PA
7085}
7086
7087/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7088 true if LOC1 and LOC2 represent the same watchpoint location. */
7089
d8de7963 7090static bool
b2ff9ed3
AB
7091watchpoint_locations_match (const struct bp_location *loc1,
7092 const struct bp_location *loc2)
2d134ed3 7093{
3a5c3e22
PA
7094 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7095 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7096
7097 /* Both of them must exist. */
7098 gdb_assert (w1 != NULL);
7099 gdb_assert (w2 != NULL);
2bdf28a0 7100
4a64f543
MS
7101 /* If the target can evaluate the condition expression in hardware,
7102 then we we need to insert both watchpoints even if they are at
7103 the same place. Otherwise the watchpoint will only trigger when
7104 the condition of whichever watchpoint was inserted evaluates to
7105 true, not giving a chance for GDB to check the condition of the
7106 other watchpoint. */
3a5c3e22 7107 if ((w1->cond_exp
4a64f543
MS
7108 && target_can_accel_watchpoint_condition (loc1->address,
7109 loc1->length,
0cf6dd15 7110 loc1->watchpoint_type,
4d01a485 7111 w1->cond_exp.get ()))
3a5c3e22 7112 || (w2->cond_exp
4a64f543
MS
7113 && target_can_accel_watchpoint_condition (loc2->address,
7114 loc2->length,
0cf6dd15 7115 loc2->watchpoint_type,
4d01a485 7116 w2->cond_exp.get ())))
d8de7963 7117 return false;
0cf6dd15 7118
85d721b8
PA
7119 /* Note that this checks the owner's type, not the location's. In
7120 case the target does not support read watchpoints, but does
7121 support access watchpoints, we'll have bp_read_watchpoint
7122 watchpoints with hw_access locations. Those should be considered
7123 duplicates of hw_read locations. The hw_read locations will
7124 become hw_access locations later. */
2d134ed3
PA
7125 return (loc1->owner->type == loc2->owner->type
7126 && loc1->pspace->aspace == loc2->pspace->aspace
7127 && loc1->address == loc2->address
7128 && loc1->length == loc2->length);
e4f237da
KB
7129}
7130
31e77af2 7131/* See breakpoint.h. */
6c95b8df 7132
31e77af2 7133int
accd0bcd
YQ
7134breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7135 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 7136{
f5656ead 7137 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7138 || aspace1 == aspace2)
7139 && addr1 == addr2);
7140}
7141
f1310107
TJB
7142/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7143 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7144 matches ASPACE2. On targets that have global breakpoints, the address
7145 space doesn't really matter. */
7146
d8de7963 7147static bool
accd0bcd
YQ
7148breakpoint_address_match_range (const address_space *aspace1,
7149 CORE_ADDR addr1,
7150 int len1, const address_space *aspace2,
f1310107
TJB
7151 CORE_ADDR addr2)
7152{
f5656ead 7153 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7154 || aspace1 == aspace2)
7155 && addr2 >= addr1 && addr2 < addr1 + len1);
7156}
7157
7158/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7159 a ranged breakpoint. In most targets, a match happens only if ASPACE
7160 matches the breakpoint's address space. On targets that have global
7161 breakpoints, the address space doesn't really matter. */
7162
d8de7963 7163static bool
f1310107 7164breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 7165 const address_space *aspace,
f1310107
TJB
7166 CORE_ADDR addr)
7167{
7168 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7169 aspace, addr)
7170 || (bl->length
7171 && breakpoint_address_match_range (bl->pspace->aspace,
7172 bl->address, bl->length,
7173 aspace, addr)));
7174}
7175
d35ae833
PA
7176/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7177 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7178 match happens only if ASPACE matches the breakpoint's address
7179 space. On targets that have global breakpoints, the address space
7180 doesn't really matter. */
7181
d8de7963 7182static bool
d35ae833 7183breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 7184 const address_space *aspace,
d35ae833
PA
7185 CORE_ADDR addr, int len)
7186{
7187 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7188 || bl->pspace->aspace == aspace)
7189 {
7190 int bl_len = bl->length != 0 ? bl->length : 1;
7191
7192 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7193 return 1;
7194 }
7195 return 0;
7196}
7197
1e4d1764
YQ
7198/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7199 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7200 true, otherwise returns false. */
7201
d8de7963 7202static bool
b2ff9ed3
AB
7203tracepoint_locations_match (const struct bp_location *loc1,
7204 const struct bp_location *loc2)
1e4d1764
YQ
7205{
7206 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7207 /* Since tracepoint locations are never duplicated with others', tracepoint
7208 locations at the same address of different tracepoints are regarded as
7209 different locations. */
7210 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7211 else
d8de7963 7212 return false;
1e4d1764
YQ
7213}
7214
2d134ed3 7215/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7216 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7217 the same location. If SW_HW_BPS_MATCH is true, then software
7218 breakpoint locations and hardware breakpoint locations match,
7219 otherwise they don't. */
2d134ed3 7220
d8de7963 7221static bool
b2ff9ed3
AB
7222breakpoint_locations_match (const struct bp_location *loc1,
7223 const struct bp_location *loc2,
7f32a4d5 7224 bool sw_hw_bps_match)
2d134ed3 7225{
2bdf28a0
JK
7226 int hw_point1, hw_point2;
7227
7228 /* Both of them must not be in moribund_locations. */
7229 gdb_assert (loc1->owner != NULL);
7230 gdb_assert (loc2->owner != NULL);
7231
7232 hw_point1 = is_hardware_watchpoint (loc1->owner);
7233 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7234
7235 if (hw_point1 != hw_point2)
d8de7963 7236 return false;
2d134ed3
PA
7237 else if (hw_point1)
7238 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7239 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7240 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7241 else
7f32a4d5
PA
7242 /* We compare bp_location.length in order to cover ranged
7243 breakpoints. Keep this in sync with
7244 bp_location_is_less_than. */
f1310107
TJB
7245 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7246 loc2->pspace->aspace, loc2->address)
7f32a4d5 7247 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7248 && loc1->length == loc2->length);
2d134ed3
PA
7249}
7250
76897487
KB
7251static void
7252breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
d8de7963 7253 int bnum, bool have_bnum)
76897487 7254{
f63fbe86
MS
7255 /* The longest string possibly returned by hex_string_custom
7256 is 50 chars. These must be at least that big for safety. */
7257 char astr1[64];
7258 char astr2[64];
76897487 7259
bb599908
PH
7260 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7261 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7262 if (have_bnum)
8a3fe4f8 7263 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7264 bnum, astr1, astr2);
76897487 7265 else
8a3fe4f8 7266 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7267}
7268
4a64f543
MS
7269/* Adjust a breakpoint's address to account for architectural
7270 constraints on breakpoint placement. Return the adjusted address.
7271 Note: Very few targets require this kind of adjustment. For most
7272 targets, this function is simply the identity function. */
76897487
KB
7273
7274static CORE_ADDR
a6d9a66e 7275adjust_breakpoint_address (struct gdbarch *gdbarch,
60270718
AB
7276 CORE_ADDR bpaddr, enum bptype bptype,
7277 struct program_space *pspace)
76897487 7278{
60270718
AB
7279 gdb_assert (pspace != nullptr);
7280
a0de8c21
YQ
7281 if (bptype == bp_watchpoint
7282 || bptype == bp_hardware_watchpoint
7283 || bptype == bp_read_watchpoint
7284 || bptype == bp_access_watchpoint
7285 || bptype == bp_catchpoint)
88f7da05
KB
7286 {
7287 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7288 have their addresses modified. */
88f7da05
KB
7289 return bpaddr;
7290 }
7c16b83e
PA
7291 else if (bptype == bp_single_step)
7292 {
7293 /* Single-step breakpoints should not have their addresses
7294 modified. If there's any architectural constrain that
7295 applies to this address, then it should have already been
7296 taken into account when the breakpoint was created in the
7297 first place. If we didn't do this, stepping through e.g.,
7298 Thumb-2 IT blocks would break. */
7299 return bpaddr;
7300 }
76897487
KB
7301 else
7302 {
a0de8c21
YQ
7303 CORE_ADDR adjusted_bpaddr = bpaddr;
7304
60270718
AB
7305 /* Some targets have architectural constraints on the placement
7306 of breakpoint instructions. Obtain the adjusted address. */
a0de8c21
YQ
7307 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7308 {
60270718
AB
7309 /* Targets that implement this adjustment function will likely
7310 inspect either the symbol table, target memory at BPADDR, or
7311 even state registers, so ensure a suitable thread (and its
7312 associated program space) are currently selected. */
7313 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7314 switch_to_program_space_and_thread (pspace);
7315 adjusted_bpaddr
7316 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
a0de8c21 7317 }
76897487 7318
d88cb738
LM
7319 adjusted_bpaddr
7320 = gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
76897487
KB
7321
7322 /* An adjusted breakpoint address can significantly alter
dda83cd7 7323 a user's expectations. Print a warning if an adjustment
76897487
KB
7324 is required. */
7325 if (adjusted_bpaddr != bpaddr)
d8de7963 7326 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
76897487
KB
7327
7328 return adjusted_bpaddr;
7329 }
7330}
7331
cb1e4e32
PA
7332static bp_loc_type
7333bp_location_from_bp_type (bptype type)
7cc221ef 7334{
cb1e4e32 7335 switch (type)
e049a4b5
DJ
7336 {
7337 case bp_breakpoint:
7c16b83e 7338 case bp_single_step:
e049a4b5
DJ
7339 case bp_until:
7340 case bp_finish:
7341 case bp_longjmp:
7342 case bp_longjmp_resume:
e2e4d78b 7343 case bp_longjmp_call_dummy:
186c406b
TT
7344 case bp_exception:
7345 case bp_exception_resume:
e049a4b5 7346 case bp_step_resume:
2c03e5be 7347 case bp_hp_step_resume:
e049a4b5
DJ
7348 case bp_watchpoint_scope:
7349 case bp_call_dummy:
aa7d318d 7350 case bp_std_terminate:
e049a4b5
DJ
7351 case bp_shlib_event:
7352 case bp_thread_event:
7353 case bp_overlay_event:
4efc6507 7354 case bp_jit_event:
0fd8e87f 7355 case bp_longjmp_master:
aa7d318d 7356 case bp_std_terminate_master:
186c406b 7357 case bp_exception_master:
0e30163f
JK
7358 case bp_gnu_ifunc_resolver:
7359 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7360 case bp_dprintf:
cb1e4e32 7361 return bp_loc_software_breakpoint;
0b63c811 7362
e049a4b5 7363 case bp_hardware_breakpoint:
cb1e4e32 7364 return bp_loc_hardware_breakpoint;
0b63c811 7365
e049a4b5
DJ
7366 case bp_hardware_watchpoint:
7367 case bp_read_watchpoint:
7368 case bp_access_watchpoint:
cb1e4e32 7369 return bp_loc_hardware_watchpoint;
0b63c811 7370
e049a4b5 7371 case bp_watchpoint:
6e14e441 7372 return bp_loc_software_watchpoint;
0b63c811 7373
15c3d785
PA
7374 case bp_tracepoint:
7375 case bp_fast_tracepoint:
0fb4aa4b 7376 case bp_static_tracepoint:
7b572efb 7377 case bp_static_marker_tracepoint:
0b63c811
SM
7378 return bp_loc_tracepoint;
7379
7380 case bp_catchpoint:
cb1e4e32 7381 return bp_loc_other;
0b63c811 7382
e049a4b5 7383 default:
f34652de 7384 internal_error (_("unknown breakpoint type"));
e049a4b5 7385 }
cb1e4e32
PA
7386}
7387
7388bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7389{
7390 this->owner = owner;
7391 this->cond_bytecode = NULL;
7392 this->shlib_disabled = 0;
7393 this->enabled = 1;
b5fa468f 7394 this->disabled_by_cond = false;
cb1e4e32
PA
7395
7396 this->loc_type = type;
e049a4b5 7397
cb1e4e32
PA
7398 if (this->loc_type == bp_loc_software_breakpoint
7399 || this->loc_type == bp_loc_hardware_breakpoint)
7400 mark_breakpoint_location_modified (this);
7401
b6433ede 7402 incref ();
cb1e4e32
PA
7403}
7404
7405bp_location::bp_location (breakpoint *owner)
7406 : bp_location::bp_location (owner,
7407 bp_location_from_bp_type (owner->type))
7408{
28010a5d
PA
7409}
7410
f431efe5
PA
7411/* Decrement reference count. If the reference count reaches 0,
7412 destroy the bp_location. Sets *BLP to NULL. */
7413
7414static void
7415decref_bp_location (struct bp_location **blp)
7416{
b6433ede 7417 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7418 *blp = NULL;
7419}
7420
346774a9 7421/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7422
b270e6f9
TT
7423static breakpoint *
7424add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7425{
346774a9 7426 struct breakpoint *b1;
b270e6f9 7427 struct breakpoint *result = b.get ();
c906108c 7428
346774a9
PA
7429 /* Add this breakpoint to the end of the chain so that a list of
7430 breakpoints will come out in order of increasing numbers. */
7431
7432 b1 = breakpoint_chain;
7433 if (b1 == 0)
b270e6f9 7434 breakpoint_chain = b.release ();
346774a9
PA
7435 else
7436 {
7437 while (b1->next)
7438 b1 = b1->next;
b270e6f9 7439 b1->next = b.release ();
346774a9 7440 }
b270e6f9
TT
7441
7442 return result;
346774a9
PA
7443}
7444
0ba852ab 7445/* Initialize loc->function_name. */
0e30163f 7446
0d381245 7447static void
0ba852ab 7448set_breakpoint_location_function (struct bp_location *loc)
0d381245 7449{
2bdf28a0
JK
7450 gdb_assert (loc->owner != NULL);
7451
0d381245 7452 if (loc->owner->type == bp_breakpoint
1042e4c0 7453 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7454 || is_tracepoint (loc->owner))
0d381245 7455 {
2c02bd72 7456 const char *function_name;
0e30163f 7457
3467ec66 7458 if (loc->msymbol != NULL
60f62e2b
SM
7459 && (loc->msymbol->type () == mst_text_gnu_ifunc
7460 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7461 {
7462 struct breakpoint *b = loc->owner;
7463
c9d95fa3 7464 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7465
7466 if (b->type == bp_breakpoint && b->loc == loc
7467 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7468 {
7469 /* Create only the whole new breakpoint of this type but do not
7470 mess more complicated breakpoints with multiple locations. */
7471 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7472 /* Remember the resolver's address for use by the return
dda83cd7 7473 breakpoint. */
3467ec66 7474 loc->related_address = loc->address;
0e30163f
JK
7475 }
7476 }
3467ec66
PA
7477 else
7478 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7479
2c02bd72 7480 if (function_name)
23d6ee64 7481 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7482 }
7483}
7484
a6d9a66e 7485/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7486struct gdbarch *
a6d9a66e
UW
7487get_sal_arch (struct symtab_and_line sal)
7488{
7489 if (sal.section)
08feed99 7490 return sal.section->objfile->arch ();
a6d9a66e 7491 if (sal.symtab)
3c86fae3 7492 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7493
7494 return NULL;
7495}
7496
53a5351d 7497/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7498 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7499 initiated the operation. */
c906108c
SS
7500
7501void
186c406b 7502set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7503{
5d5658a1 7504 int thread = tp->global_num;
0fd8e87f
UW
7505
7506 /* To avoid having to rescan all objfile symbols at every step,
7507 we maintain a list of continually-inserted but always disabled
7508 longjmp "master" breakpoints. Here, we simply create momentary
7509 clones of those and enable them for the requested thread. */
1428b37a 7510 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7511 if (b->pspace == current_program_space
186c406b
TT
7512 && (b->type == bp_longjmp_master
7513 || b->type == bp_exception_master))
0fd8e87f 7514 {
06edf0c0 7515 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
e2e4d78b
JK
7516 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7517 after their removal. */
7ab97995 7518 momentary_breakpoint_from_master (b, type, 1, thread);
0fd8e87f 7519 }
186c406b
TT
7520
7521 tp->initiating_frame = frame;
c906108c
SS
7522}
7523
611c83ae 7524/* Delete all longjmp breakpoints from THREAD. */
c906108c 7525void
611c83ae 7526delete_longjmp_breakpoint (int thread)
c906108c 7527{
1428b37a 7528 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7529 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7530 {
7531 if (b->thread == thread)
7532 delete_breakpoint (b);
7533 }
c906108c
SS
7534}
7535
f59f708a
PA
7536void
7537delete_longjmp_breakpoint_at_next_stop (int thread)
7538{
1428b37a 7539 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7540 if (b->type == bp_longjmp || b->type == bp_exception)
7541 {
7542 if (b->thread == thread)
7543 b->disposition = disp_del_at_next_stop;
7544 }
7545}
7546
e2e4d78b
JK
7547/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7548 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7549 pointer to any of them. Return NULL if this system cannot place longjmp
7550 breakpoints. */
7551
7552struct breakpoint *
7553set_longjmp_breakpoint_for_call_dummy (void)
7554{
43892fdf 7555 breakpoint *retval = nullptr;
e2e4d78b 7556
43892fdf 7557 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7558 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7559 {
7ab97995
PA
7560 int thread = inferior_thread ()->global_num;
7561 breakpoint *new_b
7562 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7563 1, thread);
e2e4d78b
JK
7564
7565 /* Link NEW_B into the chain of RETVAL breakpoints. */
7566
7567 gdb_assert (new_b->related_breakpoint == new_b);
7568 if (retval == NULL)
7569 retval = new_b;
7570 new_b->related_breakpoint = retval;
7571 while (retval->related_breakpoint != new_b->related_breakpoint)
7572 retval = retval->related_breakpoint;
7573 retval->related_breakpoint = new_b;
7574 }
7575
7576 return retval;
7577}
7578
7579/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7580 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7581 stack.
7582
b4b3e2de
AB
7583 If the unwind fails then there is not sufficient information to discard
7584 dummy frames. In this case, elide the clean up and the dummy frames will
7585 be cleaned up next time this function is called from a location where
7586 unwinding is possible. */
e2e4d78b
JK
7587
7588void
b67a2c6f 7589check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7590{
7591 struct breakpoint *b, *b_tmp;
7592
7593 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7594 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7595 {
7596 struct breakpoint *dummy_b = b->related_breakpoint;
7597
b4b3e2de
AB
7598 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7599 chained off b->related_breakpoint. */
e2e4d78b
JK
7600 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7601 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7602
7603 /* If there was no bp_call_dummy breakpoint then there's nothing
7604 more to do. Or, if the dummy frame associated with the
7605 bp_call_dummy is still on the stack then we need to leave this
7606 bp_call_dummy in place. */
e2e4d78b
JK
7607 if (dummy_b->type != bp_call_dummy
7608 || frame_find_by_id (dummy_b->frame_id) != NULL)
7609 continue;
b4b3e2de
AB
7610
7611 /* We didn't find the dummy frame on the stack, this could be
7612 because we have longjmp'd to a stack frame that is previous to
7613 the dummy frame, or it could be because the stack unwind is
7614 broken at some point between the longjmp frame and the dummy
7615 frame.
7616
7617 Next we figure out why the stack unwind stopped. If it looks
7618 like the unwind is complete then we assume the dummy frame has
7619 been jumped over, however, if the unwind stopped for an
7620 unexpected reason then we assume the stack unwind is currently
7621 broken, and that we will (eventually) return to the dummy
7622 frame.
7623
7624 It might be tempting to consider using frame_id_inner here, but
7625 that is not safe. There is no guarantee that the stack frames
7626 we are looking at here are even on the same stack as the
7627 original dummy frame, hence frame_id_inner can't be used. See
7628 the comments on frame_id_inner for more details. */
7629 bool unwind_finished_unexpectedly = false;
bd2b40ac 7630 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
b4b3e2de 7631 {
bd2b40ac 7632 frame_info_ptr prev = get_prev_frame (fi);
b4b3e2de
AB
7633 if (prev == nullptr)
7634 {
7635 /* FI is the last stack frame. Why did this frame not
7636 unwind further? */
7637 auto stop_reason = get_frame_unwind_stop_reason (fi);
7638 if (stop_reason != UNWIND_NO_REASON
7639 && stop_reason != UNWIND_OUTERMOST)
7640 unwind_finished_unexpectedly = true;
7641 }
7642 fi = prev;
7643 }
7644 if (unwind_finished_unexpectedly)
7645 continue;
7646
00431a78 7647 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7648
7649 while (b->related_breakpoint != b)
7650 {
7651 if (b_tmp == b->related_breakpoint)
7652 b_tmp = b->related_breakpoint->next;
7653 delete_breakpoint (b->related_breakpoint);
7654 }
7655 delete_breakpoint (b);
7656 }
7657}
7658
1900040c
MS
7659void
7660enable_overlay_breakpoints (void)
7661{
43892fdf 7662 for (breakpoint *b : all_breakpoints ())
1900040c 7663 if (b->type == bp_overlay_event)
01add95b
SM
7664 {
7665 b->enable_state = bp_enabled;
7666 update_global_location_list (UGLL_MAY_INSERT);
7667 overlay_events_enabled = 1;
7668 }
1900040c
MS
7669}
7670
7671void
7672disable_overlay_breakpoints (void)
7673{
43892fdf 7674 for (breakpoint *b : all_breakpoints ())
1900040c 7675 if (b->type == bp_overlay_event)
01add95b
SM
7676 {
7677 b->enable_state = bp_disabled;
7678 update_global_location_list (UGLL_DONT_INSERT);
7679 overlay_events_enabled = 0;
7680 }
1900040c
MS
7681}
7682
aa7d318d
TT
7683/* Set an active std::terminate breakpoint for each std::terminate
7684 master breakpoint. */
7685void
7686set_std_terminate_breakpoint (void)
7687{
1428b37a 7688 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7689 if (b->pspace == current_program_space
7690 && b->type == bp_std_terminate_master)
7691 {
7ab97995
PA
7692 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7693 inferior_thread ()->global_num);
aa7d318d
TT
7694 }
7695}
7696
7697/* Delete all the std::terminate breakpoints. */
7698void
7699delete_std_terminate_breakpoint (void)
7700{
1428b37a 7701 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7702 if (b->type == bp_std_terminate)
7703 delete_breakpoint (b);
7704}
7705
c4093a6a 7706struct breakpoint *
a6d9a66e 7707create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7708{
7709 struct breakpoint *b;
c4093a6a 7710
9efa3c7f 7711 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 7712
b5de0fa7 7713 b->enable_state = bp_enabled;
264f9890
PA
7714 /* locspec has to be used or breakpoint_re_set will delete me. */
7715 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
c4093a6a 7716
44702360 7717 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7718
c4093a6a
JM
7719 return b;
7720}
7721
0101ce28
JJ
7722struct lang_and_radix
7723 {
7724 enum language lang;
7725 int radix;
7726 };
7727
4efc6507
DE
7728/* Create a breakpoint for JIT code registration and unregistration. */
7729
7730struct breakpoint *
7731create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7732{
9efa3c7f 7733 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 7734}
0101ce28 7735
03673fc7
PP
7736/* Remove JIT code registration and unregistration breakpoint(s). */
7737
7738void
7739remove_jit_event_breakpoints (void)
7740{
1428b37a 7741 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7742 if (b->type == bp_jit_event
7743 && b->loc->pspace == current_program_space)
7744 delete_breakpoint (b);
7745}
7746
cae688ec
JJ
7747void
7748remove_solib_event_breakpoints (void)
7749{
1428b37a 7750 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7751 if (b->type == bp_shlib_event
7752 && b->loc->pspace == current_program_space)
cae688ec
JJ
7753 delete_breakpoint (b);
7754}
7755
f37f681c
PA
7756/* See breakpoint.h. */
7757
7758void
7759remove_solib_event_breakpoints_at_next_stop (void)
7760{
1428b37a 7761 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7762 if (b->type == bp_shlib_event
7763 && b->loc->pspace == current_program_space)
7764 b->disposition = disp_del_at_next_stop;
7765}
7766
04086b45
PA
7767/* Helper for create_solib_event_breakpoint /
7768 create_and_insert_solib_event_breakpoint. Allows specifying which
7769 INSERT_MODE to pass through to update_global_location_list. */
7770
7771static struct breakpoint *
7772create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7773 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7774{
7775 struct breakpoint *b;
7776
9efa3c7f 7777 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 7778 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7779 return b;
7780}
7781
04086b45
PA
7782struct breakpoint *
7783create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7784{
7785 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7786}
7787
f37f681c
PA
7788/* See breakpoint.h. */
7789
7790struct breakpoint *
7791create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7792{
7793 struct breakpoint *b;
7794
04086b45
PA
7795 /* Explicitly tell update_global_location_list to insert
7796 locations. */
7797 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7798 if (!b->loc->inserted)
7799 {
7800 delete_breakpoint (b);
7801 return NULL;
7802 }
7803 return b;
7804}
7805
cae688ec
JJ
7806/* Disable any breakpoints that are on code in shared libraries. Only
7807 apply to enabled breakpoints, disabled ones can just stay disabled. */
7808
7809void
cb851954 7810disable_breakpoints_in_shlibs (void)
cae688ec 7811{
48d7020b 7812 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7813 {
7814 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7815 struct breakpoint *b = loc->owner;
7816
7817 /* We apply the check to all breakpoints, including disabled for
7818 those with loc->duplicate set. This is so that when breakpoint
7819 becomes enabled, or the duplicate is removed, gdb will try to
7820 insert all breakpoints. If we don't set shlib_disabled here,
7821 we'll try to insert those breakpoints and fail. */
7822 if (((b->type == bp_breakpoint)
7823 || (b->type == bp_jit_event)
7824 || (b->type == bp_hardware_breakpoint)
7825 || (is_tracepoint (b)))
7826 && loc->pspace == current_program_space
7827 && !loc->shlib_disabled
7828 && solib_name_from_address (loc->pspace, loc->address)
7829 )
7830 {
7831 loc->shlib_disabled = 1;
7832 }
7833 }
cae688ec
JJ
7834}
7835
63644780
NB
7836/* Disable any breakpoints and tracepoints that are in SOLIB upon
7837 notification of unloaded_shlib. Only apply to enabled breakpoints,
7838 disabled ones can just stay disabled. */
84acb35a 7839
75149521 7840static void
84acb35a
JJ
7841disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7842{
d8de7963 7843 bool disabled_shlib_breaks = false;
84acb35a 7844
48d7020b 7845 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7846 {
7847 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7848 struct breakpoint *b = loc->owner;
7849
7850 if (solib->pspace == loc->pspace
7851 && !loc->shlib_disabled
7852 && (((b->type == bp_breakpoint
7853 || b->type == bp_jit_event
7854 || b->type == bp_hardware_breakpoint)
7855 && (loc->loc_type == bp_loc_hardware_breakpoint
7856 || loc->loc_type == bp_loc_software_breakpoint))
7857 || is_tracepoint (b))
7858 && solib_contains_address_p (solib, loc->address))
7859 {
7860 loc->shlib_disabled = 1;
7861 /* At this point, we cannot rely on remove_breakpoint
7862 succeeding so we must mark the breakpoint as not inserted
7863 to prevent future errors occurring in remove_breakpoints. */
7864 loc->inserted = 0;
7865
7866 /* This may cause duplicate notifications for the same breakpoint. */
7867 gdb::observers::breakpoint_modified.notify (b);
7868
7869 if (!disabled_shlib_breaks)
7870 {
7871 target_terminal::ours_for_output ();
7872 warning (_("Temporarily disabling breakpoints "
7873 "for unloaded shared library \"%s\""),
7874 solib->so_name);
7875 }
d8de7963 7876 disabled_shlib_breaks = true;
01add95b
SM
7877 }
7878 }
84acb35a
JJ
7879}
7880
63644780
NB
7881/* Disable any breakpoints and tracepoints in OBJFILE upon
7882 notification of free_objfile. Only apply to enabled breakpoints,
7883 disabled ones can just stay disabled. */
7884
7885static void
7886disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7887{
63644780
NB
7888 if (objfile == NULL)
7889 return;
7890
d03de421
PA
7891 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7892 managed by the user with add-symbol-file/remove-symbol-file.
7893 Similarly to how breakpoints in shared libraries are handled in
7894 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7895 shlib_disabled so they end up uninserted on the next global
7896 location list update. Shared libraries not loaded by the user
7897 aren't handled here -- they're already handled in
7898 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7899 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7900 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7901 main objfile). */
7902 if ((objfile->flags & OBJF_SHARED) == 0
7903 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7904 return;
7905
43892fdf 7906 for (breakpoint *b : all_breakpoints ())
63644780 7907 {
d8de7963 7908 bool bp_modified = false;
63644780
NB
7909
7910 if (!is_breakpoint (b) && !is_tracepoint (b))
7911 continue;
7912
40cb8ca5 7913 for (bp_location *loc : b->locations ())
63644780
NB
7914 {
7915 CORE_ADDR loc_addr = loc->address;
7916
7917 if (loc->loc_type != bp_loc_hardware_breakpoint
7918 && loc->loc_type != bp_loc_software_breakpoint)
7919 continue;
7920
7921 if (loc->shlib_disabled != 0)
7922 continue;
7923
7924 if (objfile->pspace != loc->pspace)
7925 continue;
7926
7927 if (loc->loc_type != bp_loc_hardware_breakpoint
7928 && loc->loc_type != bp_loc_software_breakpoint)
7929 continue;
7930
7931 if (is_addr_in_objfile (loc_addr, objfile))
7932 {
7933 loc->shlib_disabled = 1;
08351840
PA
7934 /* At this point, we don't know whether the object was
7935 unmapped from the inferior or not, so leave the
7936 inserted flag alone. We'll handle failure to
7937 uninsert quietly, in case the object was indeed
7938 unmapped. */
63644780
NB
7939
7940 mark_breakpoint_location_modified (loc);
7941
d8de7963 7942 bp_modified = true;
63644780
NB
7943 }
7944 }
7945
7946 if (bp_modified)
76727919 7947 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7948 }
7949}
7950
b650a282 7951/* See breakpoint.h. */
ce78b96d 7952
92bb0228
PA
7953breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7954 bool temp, const char *cond_string_)
7955 : type (bptype),
7956 disposition (temp ? disp_del : disp_donttouch),
7957 gdbarch (gdbarch_),
7958 language (current_language->la_language),
7959 input_radix (::input_radix),
7960 cond_string (cond_string_ != nullptr
7961 ? make_unique_xstrdup (cond_string_)
7962 : nullptr),
7963 related_breakpoint (this)
7964{
7965}
7966
7967/* See breakpoint.h. */
7968
fed1c982 7969catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
92bb0228
PA
7970 const char *cond_string)
7971 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
c906108c 7972{
acd0955b 7973 add_dummy_location (this, current_program_space);
c5aa993b 7974
acd0955b 7975 pspace = current_program_space;
346774a9
PA
7976}
7977
b82d4ec9 7978breakpoint *
b270e6f9 7979install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 7980{
b270e6f9 7981 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 7982 set_breakpoint_number (internal, b);
558a9d82
YQ
7983 if (is_tracepoint (b))
7984 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
7985 if (!internal)
7986 mention (b);
76727919 7987 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
7988
7989 if (update_gll)
44702360 7990 update_global_location_list (UGLL_MAY_INSERT);
b82d4ec9
PA
7991
7992 return b;
c56053d2
PA
7993}
7994
c906108c 7995static int
fba45db2 7996hw_breakpoint_used_count (void)
c906108c 7997{
c906108c
SS
7998 int i = 0;
7999
43892fdf 8000 for (breakpoint *b : all_breakpoints ())
d6b74ac4 8001 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 8002 for (bp_location *bl : b->locations ())
f1310107
TJB
8003 {
8004 /* Special types of hardware breakpoints may use more than
8005 one register. */
c47614fe 8006 i += b->resources_needed (bl);
f1310107 8007 }
c906108c
SS
8008
8009 return i;
8010}
8011
a1398e0c
PA
8012/* Returns the resources B would use if it were a hardware
8013 watchpoint. */
8014
c906108c 8015static int
a1398e0c 8016hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8017{
c906108c
SS
8018 int i = 0;
8019
a1398e0c
PA
8020 if (!breakpoint_enabled (b))
8021 return 0;
8022
40cb8ca5 8023 for (bp_location *bl : b->locations ())
a1398e0c
PA
8024 {
8025 /* Special types of hardware watchpoints may use more than
8026 one register. */
c47614fe 8027 i += b->resources_needed (bl);
a1398e0c
PA
8028 }
8029
8030 return i;
8031}
8032
8033/* Returns the sum the used resources of all hardware watchpoints of
8034 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8035 the sum of the used resources of all hardware watchpoints of other
8036 types _not_ TYPE. */
8037
8038static int
8039hw_watchpoint_used_count_others (struct breakpoint *except,
8040 enum bptype type, int *other_type_used)
8041{
8042 int i = 0;
a1398e0c 8043
c906108c 8044 *other_type_used = 0;
43892fdf 8045 for (breakpoint *b : all_breakpoints ())
e09342b5 8046 {
a1398e0c
PA
8047 if (b == except)
8048 continue;
e09342b5
TJB
8049 if (!breakpoint_enabled (b))
8050 continue;
8051
a1398e0c
PA
8052 if (b->type == type)
8053 i += hw_watchpoint_use_count (b);
8054 else if (is_hardware_watchpoint (b))
8055 *other_type_used = 1;
e09342b5
TJB
8056 }
8057
c906108c
SS
8058 return i;
8059}
8060
c906108c 8061void
fba45db2 8062disable_watchpoints_before_interactive_call_start (void)
c906108c 8063{
43892fdf 8064 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8065 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8066 {
b5de0fa7 8067 b->enable_state = bp_call_disabled;
44702360 8068 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8069 }
c906108c
SS
8070}
8071
8072void
fba45db2 8073enable_watchpoints_after_interactive_call_stop (void)
c906108c 8074{
43892fdf 8075 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8076 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8077 {
b5de0fa7 8078 b->enable_state = bp_enabled;
44702360 8079 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8080 }
c906108c
SS
8081}
8082
8bea4e01
UW
8083void
8084disable_breakpoints_before_startup (void)
8085{
6c95b8df 8086 current_program_space->executing_startup = 1;
44702360 8087 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8088}
8089
8090void
8091enable_breakpoints_after_startup (void)
8092{
6c95b8df 8093 current_program_space->executing_startup = 0;
f8eba3c6 8094 breakpoint_re_set ();
8bea4e01
UW
8095}
8096
7a3e3265
PA
8097/* Allocate a new momentary breakpoint. */
8098
7ab97995 8099template<typename... Arg>
7a3e3265 8100static momentary_breakpoint *
7ab97995
PA
8101new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8102 Arg&&... args)
7a3e3265
PA
8103{
8104 if (type == bp_longjmp || type == bp_exception)
7ab97995
PA
8105 return new longjmp_breakpoint (gdbarch, type,
8106 std::forward<Arg> (args)...);
7a3e3265 8107 else
7ab97995
PA
8108 return new momentary_breakpoint (gdbarch, type,
8109 std::forward<Arg> (args)...);
7a3e3265
PA
8110}
8111
7c16b83e
PA
8112/* Set a momentary breakpoint of type TYPE at address specified by
8113 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8114 frame. */
c906108c 8115
454dafbd 8116breakpoint_up
a6d9a66e
UW
8117set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8118 struct frame_id frame_id, enum bptype type)
c906108c 8119{
193facb3
JK
8120 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8121 tail-called one. */
8122 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8123
7a3e3265 8124 std::unique_ptr<momentary_breakpoint> b
7ab97995
PA
8125 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8126 inferior_thread ()->global_num));
7a3e3265
PA
8127
8128 b->add_location (sal);
c906108c 8129
7a3e3265
PA
8130 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8131
44702360 8132 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8133
7a3e3265 8134 return bp;
c906108c 8135}
611c83ae 8136
06edf0c0 8137/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8138 The new breakpoint will have type TYPE, use OPS as its
8139 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8140
06edf0c0
PA
8141static struct breakpoint *
8142momentary_breakpoint_from_master (struct breakpoint *orig,
8143 enum bptype type,
7ab97995
PA
8144 int loc_enabled,
8145 int thread)
e58b0e63 8146{
7a3e3265 8147 std::unique_ptr<breakpoint> copy
7ab97995
PA
8148 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8149 orig->frame_id, thread));
ee89d0a4 8150 copy->loc = copy->allocate_location ();
0ba852ab 8151 set_breakpoint_location_function (copy->loc);
e58b0e63 8152
a6d9a66e 8153 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8154 copy->loc->requested_address = orig->loc->requested_address;
8155 copy->loc->address = orig->loc->address;
8156 copy->loc->section = orig->loc->section;
6c95b8df 8157 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8158 copy->loc->probe = orig->loc->probe;
f8eba3c6 8159 copy->loc->line_number = orig->loc->line_number;
2f202fde 8160 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8161 copy->loc->enabled = loc_enabled;
e58b0e63 8162
7a3e3265 8163 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
44702360 8164 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7a3e3265 8165 return b;
e58b0e63
PA
8166}
8167
06edf0c0
PA
8168/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8169 ORIG is NULL. */
8170
8171struct breakpoint *
8172clone_momentary_breakpoint (struct breakpoint *orig)
8173{
8174 /* If there's nothing to clone, then return nothing. */
8175 if (orig == NULL)
8176 return NULL;
8177
7ab97995
PA
8178 return momentary_breakpoint_from_master (orig, orig->type, 0,
8179 orig->thread);
06edf0c0
PA
8180}
8181
454dafbd 8182breakpoint_up
a6d9a66e
UW
8183set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8184 enum bptype type)
611c83ae
PA
8185{
8186 struct symtab_and_line sal;
8187
8188 sal = find_pc_line (pc, 0);
8189 sal.pc = pc;
8190 sal.section = find_pc_overlay (pc);
8191 sal.explicit_pc = 1;
8192
a6d9a66e 8193 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8194}
c906108c 8195\f
c5aa993b 8196
c906108c
SS
8197/* Tell the user we have just set a breakpoint B. */
8198
8199static void
b713485d 8200mention (const breakpoint *b)
c906108c 8201{
c47614fe 8202 b->print_mention ();
2d33446d 8203 current_uiout->text ("\n");
c906108c 8204}
c906108c 8205\f
c5aa993b 8206
5133a315 8207static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8208
7f32a4d5
PA
8209/* Handle "set breakpoint auto-hw on".
8210
8211 If the explicitly specified breakpoint type is not hardware
8212 breakpoint, check the memory map to see whether the breakpoint
8213 address is in read-only memory.
8214
8215 - location type is not hardware breakpoint, memory is read-only.
8216 We change the type of the location to hardware breakpoint.
8217
8218 - location type is hardware breakpoint, memory is read-write. This
8219 means we've previously made the location hardware one, but then the
8220 memory map changed, so we undo.
8221*/
8222
8223static void
8224handle_automatic_hardware_breakpoints (bp_location *bl)
8225{
8226 if (automatic_hardware_breakpoints
8227 && bl->owner->type != bp_hardware_breakpoint
8228 && (bl->loc_type == bp_loc_software_breakpoint
8229 || bl->loc_type == bp_loc_hardware_breakpoint))
8230 {
8231 /* When breakpoints are removed, remove_breakpoints will use
8232 location types we've just set here, the only possible problem
8233 is that memory map has changed during running program, but
8234 it's not going to work anyway with current gdb. */
8235 mem_region *mr = lookup_mem_region (bl->address);
8236
8237 if (mr != nullptr)
8238 {
8239 enum bp_loc_type new_type;
8240
8241 if (mr->attrib.mode != MEM_RW)
8242 new_type = bp_loc_hardware_breakpoint;
8243 else
8244 new_type = bp_loc_software_breakpoint;
8245
8246 if (new_type != bl->loc_type)
8247 {
8248 static bool said = false;
8249
8250 bl->loc_type = new_type;
8251 if (!said)
8252 {
6cb06a8c
TT
8253 gdb_printf (_("Note: automatically using "
8254 "hardware breakpoints for "
8255 "read-only addresses.\n"));
7f32a4d5
PA
8256 said = true;
8257 }
8258 }
8259 }
8260 }
8261}
8262
06615628 8263bp_location *
74421c0b 8264code_breakpoint::add_location (const symtab_and_line &sal)
0d381245 8265{
06615628 8266 struct bp_location *new_loc, **tmp;
3742cc8b 8267 CORE_ADDR adjusted_address;
06615628 8268 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8269
8270 if (loc_gdbarch == NULL)
06615628 8271 loc_gdbarch = gdbarch;
3742cc8b
YQ
8272
8273 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8274 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8275 location will be placed on the location chain. Adjustment of the
8276 breakpoint may cause target_read_memory() to be called and we do
8277 not want its scan of the location chain to find a breakpoint and
8278 location that's only been partially initialized. */
8279 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
60270718
AB
8280 sal.pc, type,
8281 sal.pspace);
0d381245 8282
d30113d4 8283 /* Sort the locations by their ADDRESS. */
06615628
PA
8284 new_loc = allocate_location ();
8285 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
d30113d4 8286 tmp = &((*tmp)->next))
0d381245 8287 ;
06615628
PA
8288 new_loc->next = *tmp;
8289 *tmp = new_loc;
8290
8291 new_loc->requested_address = sal.pc;
8292 new_loc->address = adjusted_address;
8293 new_loc->pspace = sal.pspace;
8294 new_loc->probe.prob = sal.prob;
8295 new_loc->probe.objfile = sal.objfile;
8296 gdb_assert (new_loc->pspace != NULL);
8297 new_loc->section = sal.section;
8298 new_loc->gdbarch = loc_gdbarch;
8299 new_loc->line_number = sal.line;
8300 new_loc->symtab = sal.symtab;
8301 new_loc->symbol = sal.symbol;
8302 new_loc->msymbol = sal.msymbol;
8303 new_loc->objfile = sal.objfile;
8304
8305 set_breakpoint_location_function (new_loc);
1a853c52 8306
6ae88661
LM
8307 /* While by definition, permanent breakpoints are already present in the
8308 code, we don't mark the location as inserted. Normally one would expect
8309 that GDB could rely on that breakpoint instruction to stop the program,
8310 thus removing the need to insert its own breakpoint, except that executing
8311 the breakpoint instruction can kill the target instead of reporting a
8312 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8313 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8314 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8315 breakpoint be inserted normally results in QEMU knowing about the GDB
8316 breakpoint, and thus trap before the breakpoint instruction is executed.
8317 (If GDB later needs to continue execution past the permanent breakpoint,
8318 it manually increments the PC, thus avoiding executing the breakpoint
8319 instruction.) */
06615628
PA
8320 if (bp_loc_is_permanent (new_loc))
8321 new_loc->permanent = 1;
1a853c52 8322
06615628 8323 return new_loc;
0d381245 8324}
514f746b
AR
8325\f
8326
5133a315
LM
8327/* Return true if LOC is pointing to a permanent breakpoint,
8328 return false otherwise. */
1cf4d951 8329
5133a315 8330static bool
1cf4d951
PA
8331bp_loc_is_permanent (struct bp_location *loc)
8332{
514f746b
AR
8333 gdb_assert (loc != NULL);
8334
cb1e4e32
PA
8335 /* If we have a non-breakpoint-backed catchpoint or a software
8336 watchpoint, just return 0. We should not attempt to read from
8337 the addresses the locations of these breakpoint types point to.
5133a315 8338 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8339 memory. */
cb1e4e32 8340 if (!bl_address_is_meaningful (loc))
5133a315 8341 return false;
244558af 8342
5ed8105e 8343 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8344 switch_to_program_space_and_thread (loc->pspace);
5133a315 8345 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8346}
8347
e7e0cddf
SS
8348/* Build a command list for the dprintf corresponding to the current
8349 settings of the dprintf style options. */
8350
8351static void
8352update_dprintf_command_list (struct breakpoint *b)
8353{
6f781ee3 8354 const char *dprintf_args = b->extra_string.get ();
8579fd13 8355 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf
SS
8356
8357 if (!dprintf_args)
8358 return;
8359
8360 dprintf_args = skip_spaces (dprintf_args);
8361
8362 /* Allow a comma, as it may have terminated a location, but don't
8363 insist on it. */
8364 if (*dprintf_args == ',')
8365 ++dprintf_args;
8366 dprintf_args = skip_spaces (dprintf_args);
8367
8368 if (*dprintf_args != '"')
8369 error (_("Bad format string, missing '\"'."));
8370
d3ce09f5 8371 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8372 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8373 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8374 {
e0700ba4 8375 if (dprintf_function.empty ())
e7e0cddf
SS
8376 error (_("No function supplied for dprintf call"));
8377
e0700ba4 8378 if (!dprintf_channel.empty ())
e7e0cddf 8379 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8380 dprintf_function.c_str (),
8381 dprintf_channel.c_str (),
e7e0cddf
SS
8382 dprintf_args);
8383 else
8384 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8385 dprintf_function.c_str (),
e7e0cddf
SS
8386 dprintf_args);
8387 }
d3ce09f5
SS
8388 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8389 {
8390 if (target_can_run_breakpoint_commands ())
8391 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8392 else
8393 {
8394 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8395 printf_line = xstrprintf ("printf %s", dprintf_args);
8396 }
8397 }
e7e0cddf 8398 else
f34652de 8399 internal_error (_("Invalid dprintf style."));
e7e0cddf 8400
f28045c2 8401 gdb_assert (printf_line != NULL);
e7e0cddf 8402
12973681
TT
8403 /* Manufacture a printf sequence. */
8404 struct command_line *printf_cmd_line
8579fd13 8405 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8406 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8407 command_lines_deleter ()));
e7e0cddf
SS
8408}
8409
8410/* Update all dprintf commands, making their command lists reflect
8411 current style settings. */
8412
8413static void
eb4c3f4a 8414update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8415 struct cmd_list_element *c)
8416{
43892fdf
SM
8417 for (breakpoint *b : all_breakpoints ())
8418 if (b->type == bp_dprintf)
e7e0cddf 8419 update_dprintf_command_list (b);
e7e0cddf 8420}
c3f6f71d 8421
74421c0b 8422code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
3b003a61
PA
8423 enum bptype type_,
8424 gdb::array_view<const symtab_and_line> sals,
264f9890 8425 location_spec_up &&locspec_,
3b003a61
PA
8426 gdb::unique_xmalloc_ptr<char> filter_,
8427 gdb::unique_xmalloc_ptr<char> cond_string_,
8428 gdb::unique_xmalloc_ptr<char> extra_string_,
8429 enum bpdisp disposition_,
8430 int thread_, int task_, int ignore_count_,
8431 int from_tty,
8432 int enabled_, unsigned flags,
8433 int display_canonical_)
8434 : breakpoint (gdbarch_, type_)
018d34a4 8435{
0d381245 8436 int i;
018d34a4
VP
8437
8438 if (type == bp_hardware_breakpoint)
8439 {
fbbd034e
AS
8440 int target_resources_ok;
8441
8442 i = hw_breakpoint_used_count ();
8443 target_resources_ok =
8444 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8445 i + 1, 0);
8446 if (target_resources_ok == 0)
8447 error (_("No hardware breakpoint support in the target."));
8448 else if (target_resources_ok < 0)
8449 error (_("Hardware breakpoints used exceeds limit."));
8450 }
8451
6c5b2ebe 8452 gdb_assert (!sals.empty ());
6c95b8df 8453
0a9ccb9d 8454 /* At most one of thread or task can be set on any breakpoint. */
2ecee236 8455 gdb_assert (thread == -1 || task == -1);
3b003a61
PA
8456 thread = thread_;
8457 task = task_;
0d381245 8458
3b003a61
PA
8459 cond_string = std::move (cond_string_);
8460 extra_string = std::move (extra_string_);
8461 ignore_count = ignore_count_;
8462 enable_state = enabled_ ? bp_enabled : bp_disabled;
8463 disposition = disposition_;
5af949e3 8464
3b003a61
PA
8465 if (type == bp_static_tracepoint
8466 || type == bp_static_marker_tracepoint)
8467 {
98ed24fb 8468 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
3b003a61 8469 struct static_tracepoint_marker marker;
0d381245 8470
3b003a61 8471 if (strace_marker_p (this))
0d381245 8472 {
3b003a61
PA
8473 /* We already know the marker exists, otherwise, we wouldn't
8474 see a sal for it. */
709438c7 8475 const char *p = &locspec_->to_string ()[3];
3b003a61 8476 const char *endp;
0fb4aa4b 8477
3b003a61 8478 p = skip_spaces (p);
0fb4aa4b 8479
3b003a61 8480 endp = skip_to_space (p);
0fb4aa4b 8481
3b003a61 8482 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8483
3b003a61
PA
8484 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8485 t->static_trace_marker_id.c_str ());
8486 }
8487 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8488 {
8489 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8490
3b003a61
PA
8491 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8492 t->static_trace_marker_id.c_str ());
0d381245
VP
8493 }
8494 else
3b003a61
PA
8495 warning (_("Couldn't determine the static tracepoint marker to probe"));
8496 }
8497
8498 for (const auto &sal : sals)
8499 {
8500 if (from_tty)
018d34a4 8501 {
3b003a61
PA
8502 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8503 if (loc_gdbarch == nullptr)
8504 loc_gdbarch = gdbarch;
8505
8506 describe_other_breakpoints (loc_gdbarch,
8507 sal.pspace, sal.pc, sal.section, thread);
0d381245
VP
8508 }
8509
3b003a61
PA
8510 bp_location *new_loc = add_location (sal);
8511 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8512 new_loc->inserted = 1;
8513
b5fa468f
TBA
8514 /* Do not set breakpoint locations conditions yet. As locations
8515 are inserted, they get sorted based on their addresses. Let
8516 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8517
8518 /* Dynamic printf requires and uses additional arguments on the
8519 command line, otherwise it's an error. */
8520 if (type == bp_dprintf)
8521 {
3b003a61
PA
8522 if (extra_string != nullptr)
8523 update_dprintf_command_list (this);
e7e0cddf
SS
8524 else
8525 error (_("Format string required"));
8526 }
3b003a61
PA
8527 else if (extra_string != nullptr)
8528 error (_("Garbage '%s' at end of command"), extra_string.get ());
855a6e68 8529 }
018d34a4 8530
b5fa468f
TBA
8531 /* The order of the locations is now stable. Set the location
8532 condition using the location's number. */
8533 int loc_num = 1;
3b003a61 8534 for (bp_location *bl : locations ())
b5fa468f 8535 {
3b003a61
PA
8536 if (cond_string != nullptr)
8537 set_breakpoint_location_condition (cond_string.get (), bl,
8538 number, loc_num);
b5fa468f
TBA
8539
8540 ++loc_num;
8541 }
8542
3b003a61 8543 display_canonical = display_canonical_;
264f9890
PA
8544 if (locspec_ != nullptr)
8545 locspec = std::move (locspec_);
018d34a4 8546 else
264f9890 8547 locspec = new_address_location_spec (this->loc->address, NULL, 0);
3b003a61 8548 filter = std::move (filter_);
d9b3f62e 8549}
018d34a4 8550
d9b3f62e
PA
8551static void
8552create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8553 gdb::array_view<const symtab_and_line> sals,
264f9890 8554 location_spec_up &&locspec,
e1e01040
PA
8555 gdb::unique_xmalloc_ptr<char> filter,
8556 gdb::unique_xmalloc_ptr<char> cond_string,
8557 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8558 enum bptype type, enum bpdisp disposition,
8559 int thread, int task, int ignore_count,
ef4848c7 8560 int from_tty,
44f238bb
PA
8561 int enabled, int internal, unsigned flags,
8562 int display_canonical)
d9b3f62e 8563{
74421c0b 8564 std::unique_ptr<code_breakpoint> b
3b003a61
PA
8565 = new_breakpoint_from_type (gdbarch,
8566 type,
8567 sals,
264f9890 8568 std::move (locspec),
3b003a61
PA
8569 std::move (filter),
8570 std::move (cond_string),
8571 std::move (extra_string),
8572 disposition,
8573 thread, task, ignore_count,
8574 from_tty,
8575 enabled, flags,
8576 display_canonical);
d9b3f62e 8577
b270e6f9 8578 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8579}
8580
8581/* Add SALS.nelts breakpoints to the breakpoint table. For each
8582 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8583 value. COND_STRING, if not NULL, specified the condition to be
8584 used for all breakpoints. Essentially the only case where
8585 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8586 function. In that case, it's still not possible to specify
8587 separate conditions for different overloaded functions, so
8588 we take just a single condition string.
8589
c3f6f71d 8590 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8591 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8592 array contents). If the function fails (error() is called), the
8593 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8594 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8595
8596static void
8cdf0e15 8597create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8598 struct linespec_result *canonical,
e1e01040
PA
8599 gdb::unique_xmalloc_ptr<char> cond_string,
8600 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8601 enum bptype type, enum bpdisp disposition,
8602 int thread, int task, int ignore_count,
ef4848c7 8603 int from_tty,
44f238bb 8604 int enabled, int internal, unsigned flags)
c906108c 8605{
f8eba3c6 8606 if (canonical->pre_expanded)
6c5b2ebe 8607 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8608
6c5b2ebe 8609 for (const auto &lsal : canonical->lsals)
c3f6f71d 8610 {
f00aae0f 8611 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8612 'break', without arguments. */
264f9890 8613 location_spec_up locspec
5c1ddcb6
PA
8614 = (canonical->locspec != nullptr
8615 ? canonical->locspec->clone ()
8616 : nullptr);
e1e01040 8617 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8618 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8619
6c5b2ebe 8620 create_breakpoint_sal (gdbarch, lsal.sals,
264f9890 8621 std::move (locspec),
e1e01040
PA
8622 std::move (filter_string),
8623 std::move (cond_string),
8624 std::move (extra_string),
e7e0cddf 8625 type, disposition,
ef4848c7 8626 thread, task, ignore_count,
44f238bb 8627 from_tty, enabled, internal, flags,
56435ebe 8628 canonical->special_display);
c3f6f71d 8629 }
c3f6f71d 8630}
c906108c 8631
264f9890 8632/* Parse LOCSPEC which is assumed to be a SAL specification possibly
c3f6f71d 8633 followed by conditionals. On return, SALS contains an array of SAL
264f9890
PA
8634 addresses found. LOCSPEC points to the end of the SAL (for
8635 linespec locspecs).
9998af43
TJB
8636
8637 The array and the line spec strings are allocated on the heap, it is
8638 the caller's responsibility to free them. */
c906108c 8639
b9362cc7 8640static void
264f9890 8641parse_breakpoint_sals (location_spec *locspec,
58438ac1 8642 struct linespec_result *canonical)
c3f6f71d 8643{
f00aae0f
KS
8644 struct symtab_and_line cursal;
8645
7464aeaa 8646 if (locspec->type () == LINESPEC_LOCATION_SPEC)
f00aae0f 8647 {
40d97ee2 8648 const char *spec = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 8649
a20714ff 8650 if (spec == NULL)
f00aae0f
KS
8651 {
8652 /* The last displayed codepoint, if it's valid, is our default
8653 breakpoint address. */
8654 if (last_displayed_sal_is_valid ())
8655 {
f00aae0f
KS
8656 /* Set sal's pspace, pc, symtab, and line to the values
8657 corresponding to the last call to print_frame_info.
8658 Be sure to reinitialize LINE with NOTCURRENT == 0
8659 as the breakpoint line number is inappropriate otherwise.
8660 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8661 symtab_and_line sal = get_last_displayed_sal ();
8662 CORE_ADDR pc = sal.pc;
8663
f00aae0f
KS
8664 sal = find_pc_line (pc, 0);
8665
8666 /* "break" without arguments is equivalent to "break *PC"
8667 where PC is the last displayed codepoint's address. So
8668 make sure to set sal.explicit_pc to prevent GDB from
8669 trying to expand the list of sals to include all other
8670 instances with the same symtab and line. */
8671 sal.pc = pc;
8672 sal.explicit_pc = 1;
8673
6c5b2ebe
PA
8674 struct linespec_sals lsal;
8675 lsal.sals = {sal};
f00aae0f
KS
8676 lsal.canonical = NULL;
8677
6c5b2ebe 8678 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8679 return;
8680 }
8681 else
8682 error (_("No default breakpoint address now."));
c906108c 8683 }
c906108c 8684 }
f00aae0f
KS
8685
8686 /* Force almost all breakpoints to be in terms of the
8687 current_source_symtab (which is decode_line_1's default).
8688 This should produce the results we want almost all of the
8689 time while leaving default_breakpoint_* alone.
8690
8691 ObjC: However, don't match an Objective-C method name which
8692 may have a '+' or '-' succeeded by a '['. */
8693 cursal = get_current_source_symtab_and_line ();
8694 if (last_displayed_sal_is_valid ())
c906108c 8695 {
a20714ff 8696 const char *spec = NULL;
cc80f267 8697
7464aeaa 8698 if (locspec->type () == LINESPEC_LOCATION_SPEC)
40d97ee2 8699 spec = as_linespec_location_spec (locspec)->spec_string;
cc80f267 8700
f00aae0f 8701 if (!cursal.symtab
a20714ff
PA
8702 || (spec != NULL
8703 && strchr ("+-", spec[0]) != NULL
8704 && spec[1] != '['))
f00aae0f 8705 {
264f9890 8706 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
8707 get_last_displayed_symtab (),
8708 get_last_displayed_line (),
8709 canonical, NULL, NULL);
8710 return;
8711 }
c906108c 8712 }
f00aae0f 8713
264f9890 8714 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 8715 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 8716}
c906108c 8717
c906108c 8718
c3f6f71d 8719/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8720 inserted as a breakpoint. If it can't throw an error. */
c906108c 8721
b9362cc7 8722static void
6c5b2ebe 8723breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 8724{
6c5b2ebe
PA
8725 for (auto &sal : sals)
8726 resolve_sal_pc (&sal);
c3f6f71d
JM
8727}
8728
7a697b8d
SS
8729/* Fast tracepoints may have restrictions on valid locations. For
8730 instance, a fast tracepoint using a jump instead of a trap will
8731 likely have to overwrite more bytes than a trap would, and so can
8732 only be placed where the instruction is longer than the jump, or a
8733 multi-instruction sequence does not have a jump into the middle of
8734 it, etc. */
8735
8736static void
8737check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 8738 gdb::array_view<const symtab_and_line> sals)
7a697b8d 8739{
6c5b2ebe 8740 for (const auto &sal : sals)
7a697b8d 8741 {
f8eba3c6
TT
8742 struct gdbarch *sarch;
8743
6c5b2ebe 8744 sarch = get_sal_arch (sal);
f8eba3c6
TT
8745 /* We fall back to GDBARCH if there is no architecture
8746 associated with SAL. */
8747 if (sarch == NULL)
8748 sarch = gdbarch;
281d762b
TT
8749 std::string msg;
8750 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 8751 error (_("May not have a fast tracepoint at %s%s"),
281d762b 8752 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
8753 }
8754}
8755
018d34a4
VP
8756/* Given TOK, a string specification of condition and thread, as
8757 accepted by the 'break' command, extract the condition
8758 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8759 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8760 If no condition is found, *COND_STRING is set to NULL.
8761 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8762
8763static void
bbc13ae3 8764find_condition_and_thread (const char *tok, CORE_ADDR pc,
6f781ee3
TT
8765 gdb::unique_xmalloc_ptr<char> *cond_string,
8766 int *thread, int *task,
8767 gdb::unique_xmalloc_ptr<char> *rest)
018d34a4 8768{
6f781ee3 8769 cond_string->reset ();
018d34a4 8770 *thread = -1;
2ecee236 8771 *task = -1;
6f781ee3 8772 rest->reset ();
733d554a 8773 bool force = false;
ed1d1739 8774
018d34a4
VP
8775 while (tok && *tok)
8776 {
bbc13ae3 8777 const char *end_tok;
018d34a4 8778 int toklen;
bbc13ae3
KS
8779 const char *cond_start = NULL;
8780 const char *cond_end = NULL;
cc59ec59 8781
f1735a53 8782 tok = skip_spaces (tok);
e7e0cddf
SS
8783
8784 if ((*tok == '"' || *tok == ',') && rest)
8785 {
6f781ee3 8786 rest->reset (savestring (tok, strlen (tok)));
e7e0cddf
SS
8787 return;
8788 }
8789
f1735a53 8790 end_tok = skip_to_space (tok);
d634f2de 8791
018d34a4 8792 toklen = end_tok - tok;
d634f2de 8793
018d34a4
VP
8794 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8795 {
8796 tok = cond_start = end_tok + 1;
733d554a
TBA
8797 try
8798 {
8799 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8800 }
8801 catch (const gdb_exception_error &)
8802 {
8803 if (!force)
8804 throw;
8805 else
8806 tok = tok + strlen (tok);
8807 }
018d34a4 8808 cond_end = tok;
6f781ee3 8809 cond_string->reset (savestring (cond_start, cond_end - cond_start));
018d34a4 8810 }
733d554a
TBA
8811 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8812 {
5759831a 8813 tok = tok + toklen;
733d554a
TBA
8814 force = true;
8815 }
018d34a4
VP
8816 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8817 {
5d5658a1
PA
8818 const char *tmptok;
8819 struct thread_info *thr;
d634f2de 8820
980dbf36
AB
8821 if (*thread != -1)
8822 error(_("You can specify only one thread."));
8823
2ecee236 8824 if (*task != -1)
0a9ccb9d
AB
8825 error (_("You can specify only one of thread or task."));
8826
018d34a4 8827 tok = end_tok + 1;
5d5658a1 8828 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
8829 if (tok == tmptok)
8830 error (_("Junk after thread keyword."));
5d5658a1 8831 *thread = thr->global_num;
bbc13ae3 8832 tok = tmptok;
018d34a4 8833 }
4a306c9a
JB
8834 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8835 {
8836 char *tmptok;
8837
2ecee236 8838 if (*task != -1)
980dbf36
AB
8839 error(_("You can specify only one task."));
8840
0a9ccb9d
AB
8841 if (*thread != -1)
8842 error (_("You can specify only one of thread or task."));
8843
4a306c9a 8844 tok = end_tok + 1;
bbc13ae3 8845 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
8846 if (tok == tmptok)
8847 error (_("Junk after task keyword."));
8848 if (!valid_task_id (*task))
b6199126 8849 error (_("Unknown task %d."), *task);
bbc13ae3 8850 tok = tmptok;
4a306c9a 8851 }
e7e0cddf
SS
8852 else if (rest)
8853 {
6f781ee3 8854 rest->reset (savestring (tok, strlen (tok)));
ccab2054 8855 return;
e7e0cddf 8856 }
018d34a4
VP
8857 else
8858 error (_("Junk at end of arguments."));
8859 }
8860}
8861
b5fa468f
TBA
8862/* Call 'find_condition_and_thread' for each sal in SALS until a parse
8863 succeeds. The parsed values are written to COND_STRING, THREAD,
8864 TASK, and REST. See the comment of 'find_condition_and_thread'
8865 for the description of these parameters and INPUT. */
8866
8867static void
8868find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
6f781ee3
TT
8869 const char *input,
8870 gdb::unique_xmalloc_ptr<char> *cond_string,
8871 int *thread, int *task,
8872 gdb::unique_xmalloc_ptr<char> *rest)
b5fa468f
TBA
8873{
8874 int num_failures = 0;
8875 for (auto &sal : sals)
8876 {
6f781ee3 8877 gdb::unique_xmalloc_ptr<char> cond;
0a9ccb9d 8878 int thread_id = -1;
2ecee236 8879 int task_id = -1;
6f781ee3 8880 gdb::unique_xmalloc_ptr<char> remaining;
b5fa468f
TBA
8881
8882 /* Here we want to parse 'arg' to separate condition from thread
8883 number. But because parsing happens in a context and the
8884 contexts of sals might be different, try each until there is
8885 success. Finding one successful parse is sufficient for our
8886 goal. When setting the breakpoint we'll re-parse the
8887 condition in the context of each sal. */
8888 try
8889 {
8890 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8891 &task_id, &remaining);
6f781ee3 8892 *cond_string = std::move (cond);
0a9ccb9d 8893 /* At most one of thread or task can be set. */
2ecee236 8894 gdb_assert (thread_id == -1 || task_id == -1);
b5fa468f
TBA
8895 *thread = thread_id;
8896 *task = task_id;
6f781ee3 8897 *rest = std::move (remaining);
b5fa468f
TBA
8898 break;
8899 }
8900 catch (const gdb_exception_error &e)
8901 {
8902 num_failures++;
8903 /* If no sal remains, do not continue. */
8904 if (num_failures == sals.size ())
8905 throw;
8906 }
8907 }
8908}
8909
0fb4aa4b
PA
8910/* Decode a static tracepoint marker spec. */
8911
6c5b2ebe 8912static std::vector<symtab_and_line>
f00aae0f 8913decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 8914{
f00aae0f
KS
8915 const char *p = &(*arg_p)[3];
8916 const char *endp;
0fb4aa4b 8917
f1735a53 8918 p = skip_spaces (p);
0fb4aa4b 8919
f1735a53 8920 endp = skip_to_space (p);
0fb4aa4b 8921
81b1e71c 8922 std::string marker_str (p, endp - p);
0fb4aa4b 8923
5d9310c4
SM
8924 std::vector<static_tracepoint_marker> markers
8925 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8926 if (markers.empty ())
81b1e71c
TT
8927 error (_("No known static tracepoint marker named %s"),
8928 marker_str.c_str ());
0fb4aa4b 8929
6c5b2ebe 8930 std::vector<symtab_and_line> sals;
5d9310c4 8931 sals.reserve (markers.size ());
0fb4aa4b 8932
5d9310c4 8933 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 8934 {
5d9310c4
SM
8935 symtab_and_line sal = find_pc_line (marker.address, 0);
8936 sal.pc = marker.address;
6c5b2ebe 8937 sals.push_back (sal);
5d9310c4 8938 }
0fb4aa4b 8939
0fb4aa4b
PA
8940 *arg_p = endp;
8941 return sals;
8942}
8943
bac7c5cf
GB
8944/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8945 according to IS_TRACEPOINT. */
8946
8947static const struct breakpoint_ops *
264f9890
PA
8948breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8949 bool is_tracepoint)
bac7c5cf
GB
8950{
8951 if (is_tracepoint)
8952 {
264f9890 8953 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8954 return &tracepoint_probe_breakpoint_ops;
8955 else
74421c0b 8956 return &code_breakpoint_ops;
bac7c5cf
GB
8957 }
8958 else
8959 {
264f9890 8960 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8961 return &bkpt_probe_breakpoint_ops;
8962 else
74421c0b 8963 return &code_breakpoint_ops;
bac7c5cf
GB
8964 }
8965}
8966
8967/* See breakpoint.h. */
8968
8969const struct breakpoint_ops *
264f9890
PA
8970breakpoint_ops_for_location_spec (const location_spec *locspec,
8971 bool is_tracepoint)
bac7c5cf 8972{
264f9890
PA
8973 if (locspec != nullptr)
8974 return (breakpoint_ops_for_location_spec_type
7464aeaa 8975 (locspec->type (), is_tracepoint));
74421c0b 8976 return &code_breakpoint_ops;
bac7c5cf
GB
8977}
8978
f00aae0f 8979/* See breakpoint.h. */
0101ce28 8980
8cdf0e15
VP
8981int
8982create_breakpoint (struct gdbarch *gdbarch,
264f9890 8983 location_spec *locspec,
e1e01040
PA
8984 const char *cond_string,
8985 int thread, const char *extra_string,
10a636cc 8986 bool force_condition, int parse_extra,
0fb4aa4b 8987 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8988 int ignore_count,
8989 enum auto_boolean pending_break_support,
c0a91b2b 8990 const struct breakpoint_ops *ops,
44f238bb
PA
8991 int from_tty, int enabled, int internal,
8992 unsigned flags)
c3f6f71d 8993{
7efd8fc2 8994 struct linespec_result canonical;
d8de7963 8995 bool pending = false;
2ecee236 8996 int task = -1;
86b17b60 8997 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8998
348d480f
PA
8999 gdb_assert (ops != NULL);
9000
f00aae0f
KS
9001 /* If extra_string isn't useful, set it to NULL. */
9002 if (extra_string != NULL && *extra_string == '\0')
9003 extra_string = NULL;
9004
a70b8144 9005 try
b78a6381 9006 {
264f9890 9007 ops->create_sals_from_location_spec (locspec, &canonical);
b78a6381 9008 }
230d2906 9009 catch (const gdb_exception_error &e)
0101ce28 9010 {
492d29ea
PA
9011 /* If caller is interested in rc value from parse, set
9012 value. */
9013 if (e.error == NOT_FOUND_ERROR)
0101ce28 9014 {
05ff989b
AC
9015 /* If pending breakpoint support is turned off, throw
9016 error. */
fa8d40ab
JJ
9017
9018 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9019 throw;
723a2275
VP
9020
9021 exception_print (gdb_stderr, e);
fa8d40ab 9022
dda83cd7 9023 /* If pending breakpoint support is auto query and the user
05ff989b 9024 selects no, then simply return the error code. */
059fb39f 9025 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9026 && !nquery (_("Make %s pending on future shared library load? "),
9027 bptype_string (type_wanted)))
fd9b8c24 9028 return 0;
fa8d40ab 9029
05ff989b
AC
9030 /* At this point, either the user was queried about setting
9031 a pending breakpoint and selected yes, or pending
9032 breakpoint behavior is on and thus a pending breakpoint
9033 is defaulted on behalf of the user. */
d8de7963 9034 pending = true;
0101ce28 9035 }
492d29ea 9036 else
eedc3f4f 9037 throw;
0101ce28 9038 }
492d29ea 9039
6c5b2ebe 9040 if (!pending && canonical.lsals.empty ())
492d29ea 9041 return 0;
c3f6f71d 9042
c3f6f71d
JM
9043 /* Resolve all line numbers to PC's and verify that the addresses
9044 are ok for the target. */
0101ce28 9045 if (!pending)
f8eba3c6 9046 {
6c5b2ebe
PA
9047 for (auto &lsal : canonical.lsals)
9048 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9049 }
c3f6f71d 9050
7a697b8d 9051 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9052 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9053 {
6c5b2ebe
PA
9054 for (const auto &lsal : canonical.lsals)
9055 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9056 }
7a697b8d 9057
c3f6f71d
JM
9058 /* Verify that condition can be parsed, before setting any
9059 breakpoints. Allocate a separate condition expression for each
4a64f543 9060 breakpoint. */
0101ce28 9061 if (!pending)
c3f6f71d 9062 {
e1e01040
PA
9063 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9064 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9065
f00aae0f 9066 if (parse_extra)
dda83cd7 9067 {
6f781ee3
TT
9068 gdb::unique_xmalloc_ptr<char> rest;
9069 gdb::unique_xmalloc_ptr<char> cond;
52d361e1 9070
6c5b2ebe 9071 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9072
b5fa468f
TBA
9073 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9074 &cond, &thread, &task, &rest);
6f781ee3
TT
9075 cond_string_copy = std::move (cond);
9076 extra_string_copy = std::move (rest);
dda83cd7 9077 }
2f069f6f 9078 else
dda83cd7 9079 {
f00aae0f
KS
9080 if (type_wanted != bp_dprintf
9081 && extra_string != NULL && *extra_string != '\0')
9082 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9083
10a636cc
TBA
9084 /* Check the validity of the condition. We should error out
9085 if the condition is invalid at all of the locations and
9086 if it is not forced. In the PARSE_EXTRA case above, this
9087 check is done when parsing the EXTRA_STRING. */
9088 if (cond_string != nullptr && !force_condition)
9089 {
9090 int num_failures = 0;
9091 const linespec_sals &lsal = canonical.lsals[0];
9092 for (const auto &sal : lsal.sals)
9093 {
9094 const char *cond = cond_string;
9095 try
9096 {
9097 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9098 /* One success is sufficient to keep going. */
9099 break;
9100 }
9101 catch (const gdb_exception_error &)
9102 {
9103 num_failures++;
9104 /* If this is the last sal, error out. */
9105 if (num_failures == lsal.sals.size ())
9106 throw;
9107 }
9108 }
9109 }
9110
0878d0fa
YQ
9111 /* Create a private copy of condition string. */
9112 if (cond_string)
e1e01040 9113 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9114 /* Create a private copy of any extra string. */
9115 if (extra_string)
e1e01040 9116 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9117 }
0fb4aa4b 9118
52d361e1 9119 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9120 std::move (cond_string_copy),
9121 std::move (extra_string_copy),
9122 type_wanted,
d9b3f62e 9123 tempflag ? disp_del : disp_donttouch,
ef4848c7 9124 thread, task, ignore_count,
44f238bb 9125 from_tty, enabled, internal, flags);
c906108c 9126 }
0101ce28
JJ
9127 else
9128 {
73063f51
TT
9129 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9130 type_wanted);
5c1ddcb6 9131 b->locspec = locspec->clone ();
bfccc43c 9132
f00aae0f
KS
9133 if (parse_extra)
9134 b->cond_string = NULL;
e12c7713
MK
9135 else
9136 {
9137 /* Create a private copy of condition string. */
6f781ee3
TT
9138 b->cond_string.reset (cond_string != NULL
9139 ? xstrdup (cond_string)
9140 : NULL);
15630549 9141 b->thread = thread;
e12c7713 9142 }
f00aae0f
KS
9143
9144 /* Create a private copy of any extra string. */
6f781ee3
TT
9145 b->extra_string.reset (extra_string != NULL
9146 ? xstrdup (extra_string)
9147 : NULL);
0101ce28 9148 b->ignore_count = ignore_count;
0101ce28 9149 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9150 b->condition_not_parsed = 1;
41447f92 9151 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9152 if ((type_wanted != bp_breakpoint
dda83cd7 9153 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9154 b->pspace = current_program_space;
8bea4e01 9155
b270e6f9 9156 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9157 }
9158
6c5b2ebe 9159 if (canonical.lsals.size () > 1)
95a42b64 9160 {
3e43a32a
MS
9161 warning (_("Multiple breakpoints were set.\nUse the "
9162 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9163 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9164 }
9165
44702360 9166 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9167
9168 return 1;
c3f6f71d 9169}
c906108c 9170
348d480f 9171/* Set a breakpoint.
72b2ff0e
VP
9172 ARG is a string describing breakpoint address,
9173 condition, and thread.
9174 FLAG specifies if a breakpoint is hardware on,
9175 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9176 and BP_TEMPFLAG. */
348d480f 9177
98deb0da 9178static void
f2fc3015 9179break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9180{
72b2ff0e 9181 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9182 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9183 ? bp_hardware_breakpoint
9184 : bp_breakpoint);
f00aae0f 9185
264f9890
PA
9186 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9187 const struct breakpoint_ops *ops
9188 = breakpoint_ops_for_location_spec (locspec.get (),
9189 false /* is_tracepoint */);
c3f6f71d 9190
8cdf0e15 9191 create_breakpoint (get_current_arch (),
264f9890 9192 locspec.get (),
10a636cc 9193 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9194 tempflag, type_wanted,
8cdf0e15
VP
9195 0 /* Ignore count */,
9196 pending_break_support,
55aa24fb 9197 ops,
8cdf0e15 9198 from_tty,
84f4c1fe 9199 1 /* enabled */,
44f238bb
PA
9200 0 /* internal */,
9201 0);
c906108c
SS
9202}
9203
c906108c
SS
9204/* Helper function for break_command_1 and disassemble_command. */
9205
9206void
fba45db2 9207resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9208{
9209 CORE_ADDR pc;
9210
9211 if (sal->pc == 0 && sal->symtab != NULL)
9212 {
9213 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9214 error (_("No line %d in file \"%s\"."),
05cba821 9215 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9216 sal->pc = pc;
6a048695 9217
4a64f543 9218 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9219 number, then skip the function prologue if necessary. */
6a048695 9220 if (sal->explicit_line)
059acae7 9221 skip_prologue_sal (sal);
c906108c
SS
9222 }
9223
9224 if (sal->section == 0 && sal->symtab != NULL)
9225 {
346d1dfe 9226 const struct blockvector *bv;
3977b71f 9227 const struct block *b;
c5aa993b 9228 struct symbol *sym;
c906108c 9229
43f3e411 9230 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9231 sal->symtab->compunit ());
c906108c
SS
9232 if (bv != NULL)
9233 {
3c9d0506 9234 sym = b->linkage_function ();
c906108c 9235 if (sym != NULL)
dae58e04
TT
9236 sal->section
9237 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9238 else
9239 {
4a64f543 9240 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9241 just have to look harder. This case can be executed
9242 if we have line numbers but no functions (as can
9243 happen in assembly source). */
c906108c 9244
5ed8105e 9245 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9246 switch_to_program_space_and_thread (sal->pspace);
c906108c 9247
5ed8105e 9248 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9249 if (msym.minsym)
1db66e34 9250 sal->section = msym.obj_section ();
c906108c
SS
9251 }
9252 }
9253 }
9254}
9255
9256void
0b39b52e 9257break_command (const char *arg, int from_tty)
c906108c 9258{
db107f19 9259 break_command_1 (arg, 0, from_tty);
c906108c
SS
9260}
9261
c906108c 9262void
0b39b52e 9263tbreak_command (const char *arg, int from_tty)
c906108c 9264{
db107f19 9265 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9266}
9267
c906108c 9268static void
0b39b52e 9269hbreak_command (const char *arg, int from_tty)
c906108c 9270{
db107f19 9271 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9272}
9273
9274static void
0b39b52e 9275thbreak_command (const char *arg, int from_tty)
c906108c 9276{
db107f19 9277 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9278}
9279
e7e0cddf
SS
9280/* The dynamic printf command is mostly like a regular breakpoint, but
9281 with a prewired command list consisting of a single output command,
9282 built from extra arguments supplied on the dprintf command
9283 line. */
9284
da821c7b 9285static void
0b39b52e 9286dprintf_command (const char *arg, int from_tty)
e7e0cddf 9287{
264f9890 9288 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f
KS
9289
9290 /* If non-NULL, ARG should have been advanced past the location;
9291 the next character must be ','. */
9292 if (arg != NULL)
9293 {
9294 if (arg[0] != ',' || arg[1] == '\0')
9295 error (_("Format string required"));
9296 else
9297 {
9298 /* Skip the comma. */
9299 ++arg;
9300 }
9301 }
9302
e7e0cddf 9303 create_breakpoint (get_current_arch (),
264f9890 9304 locspec.get (),
10a636cc 9305 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9306 0, bp_dprintf,
9307 0 /* Ignore count */,
9308 pending_break_support,
74421c0b 9309 &code_breakpoint_ops,
e7e0cddf
SS
9310 from_tty,
9311 1 /* enabled */,
9312 0 /* internal */,
9313 0);
9314}
9315
d3ce09f5 9316static void
0b39b52e 9317agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9318{
9319 error (_("May only run agent-printf on the target"));
9320}
9321
ec45bb67 9322/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9323
ec45bb67
TT
9324int
9325ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9326 const address_space *aspace,
9327 CORE_ADDR bp_addr,
9328 const target_waitstatus &ws)
f1310107 9329{
c272a98c
SM
9330 if (ws.kind () != TARGET_WAITKIND_STOPPED
9331 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9332 return 0;
9333
f1310107
TJB
9334 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9335 bl->length, aspace, bp_addr);
9336}
9337
ec45bb67 9338/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9339
ec45bb67
TT
9340int
9341ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9342{
9343 return target_ranged_break_num_registers ();
9344}
9345
ec45bb67 9346/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9347
ec45bb67 9348enum print_stop_action
7bd86313 9349ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9350{
ec45bb67 9351 struct bp_location *bl = loc;
79a45e25 9352 struct ui_out *uiout = current_uiout;
f1310107 9353
ec45bb67 9354 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9355
9356 /* Ranged breakpoints have only one location. */
9357 gdb_assert (bl && bl->next == NULL);
9358
ec45bb67 9359 annotate_breakpoint (number);
f303dbd6
PA
9360
9361 maybe_print_thread_hit_breakpoint (uiout);
9362
ec45bb67 9363 if (disposition == disp_del)
112e8700 9364 uiout->text ("Temporary ranged breakpoint ");
f1310107 9365 else
112e8700
SM
9366 uiout->text ("Ranged breakpoint ");
9367 if (uiout->is_mi_like_p ())
f1310107 9368 {
112e8700 9369 uiout->field_string ("reason",
f1310107 9370 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9371 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9372 }
78805ff8 9373 print_num_locno (bs, uiout);
112e8700 9374 uiout->text (", ");
f1310107
TJB
9375
9376 return PRINT_SRC_AND_LOC;
9377}
9378
ec45bb67 9379/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9380
ec45bb67 9381bool
a67bcaba 9382ranged_breakpoint::print_one (bp_location **last_loc) const
f1310107 9383{
ec45bb67 9384 struct bp_location *bl = loc;
f1310107 9385 struct value_print_options opts;
79a45e25 9386 struct ui_out *uiout = current_uiout;
f1310107
TJB
9387
9388 /* Ranged breakpoints have only one location. */
9389 gdb_assert (bl && bl->next == NULL);
9390
9391 get_user_print_options (&opts);
9392
9393 if (opts.addressprint)
9394 /* We don't print the address range here, it will be printed later
f7968895 9395 by ranged_breakpoint::print_one_detail. */
112e8700 9396 uiout->field_skip ("addr");
f1310107 9397 annotate_field (5);
ec45bb67 9398 print_breakpoint_location (this, bl);
f1310107 9399 *last_loc = bl;
c01e038b
TT
9400
9401 return true;
f1310107
TJB
9402}
9403
ec45bb67 9404/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9405
ec45bb67
TT
9406void
9407ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9408{
9409 CORE_ADDR address_start, address_end;
ec45bb67 9410 struct bp_location *bl = loc;
d7e74731 9411 string_file stb;
f1310107
TJB
9412
9413 gdb_assert (bl);
9414
9415 address_start = bl->address;
9416 address_end = address_start + bl->length - 1;
9417
112e8700 9418 uiout->text ("\taddress range: ");
d7e74731
PA
9419 stb.printf ("[%s, %s]",
9420 print_core_address (bl->gdbarch, address_start),
9421 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9422 uiout->field_stream ("addr", stb);
9423 uiout->text ("\n");
f1310107
TJB
9424}
9425
ec45bb67 9426/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9427
ec45bb67 9428void
b713485d 9429ranged_breakpoint::print_mention () const
f1310107 9430{
ec45bb67 9431 struct bp_location *bl = loc;
79a45e25 9432 struct ui_out *uiout = current_uiout;
f1310107
TJB
9433
9434 gdb_assert (bl);
ec45bb67 9435 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9436
2d33446d 9437 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
ec45bb67 9438 number, paddress (bl->gdbarch, bl->address),
2d33446d 9439 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9440}
9441
ec45bb67 9442/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9443
ec45bb67 9444void
4d1ae558 9445ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9446{
6cb06a8c 9447 gdb_printf (fp, "break-range %s, %s",
709438c7
PA
9448 locspec->to_string (),
9449 locspec_range_end->to_string ());
04d0163c 9450 print_recreate_thread (fp);
f1310107
TJB
9451}
9452
f1310107
TJB
9453/* Find the address where the end of the breakpoint range should be
9454 placed, given the SAL of the end of the range. This is so that if
9455 the user provides a line number, the end of the range is set to the
9456 last instruction of the given line. */
9457
9458static CORE_ADDR
9459find_breakpoint_range_end (struct symtab_and_line sal)
9460{
9461 CORE_ADDR end;
9462
9463 /* If the user provided a PC value, use it. Otherwise,
9464 find the address of the end of the given location. */
9465 if (sal.explicit_pc)
9466 end = sal.pc;
9467 else
9468 {
9469 int ret;
9470 CORE_ADDR start;
9471
9472 ret = find_line_pc_range (sal, &start, &end);
9473 if (!ret)
9474 error (_("Could not find location of the end of the range."));
9475
9476 /* find_line_pc_range returns the start of the next line. */
9477 end--;
9478 }
9479
9480 return end;
9481}
9482
9483/* Implement the "break-range" CLI command. */
9484
9485static void
0b39b52e 9486break_range_command (const char *arg, int from_tty)
f1310107 9487{
f2fc3015 9488 const char *arg_start;
f1310107
TJB
9489 struct linespec_result canonical_start, canonical_end;
9490 int bp_count, can_use_bp, length;
9491 CORE_ADDR end;
f1310107
TJB
9492
9493 /* We don't support software ranged breakpoints. */
9494 if (target_ranged_break_num_registers () < 0)
9495 error (_("This target does not support hardware ranged breakpoints."));
9496
9497 bp_count = hw_breakpoint_used_count ();
9498 bp_count += target_ranged_break_num_registers ();
9499 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9500 bp_count, 0);
9501 if (can_use_bp < 0)
9502 error (_("Hardware breakpoints used exceeds limit."));
9503
f8eba3c6 9504 arg = skip_spaces (arg);
f1310107
TJB
9505 if (arg == NULL || arg[0] == '\0')
9506 error(_("No address range specified."));
9507
f8eba3c6 9508 arg_start = arg;
264f9890
PA
9509 location_spec_up start_locspec
9510 = string_to_location_spec (&arg, current_language);
9511 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
f1310107
TJB
9512
9513 if (arg[0] != ',')
9514 error (_("Too few arguments."));
6c5b2ebe 9515 else if (canonical_start.lsals.empty ())
f1310107 9516 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9517
6c5b2ebe 9518 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9519
6c5b2ebe
PA
9520 if (canonical_start.lsals.size () > 1
9521 || lsal_start.sals.size () != 1)
f1310107
TJB
9522 error (_("Cannot create a ranged breakpoint with multiple locations."));
9523
6c5b2ebe 9524 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9525 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9526
9527 arg++; /* Skip the comma. */
f8eba3c6 9528 arg = skip_spaces (arg);
f1310107 9529
264f9890 9530 /* Parse the end location specification. */
f1310107 9531
f1310107
TJB
9532 arg_start = arg;
9533
f8eba3c6 9534 /* We call decode_line_full directly here instead of using
264f9890
PA
9535 parse_breakpoint_sals because we need to specify the start
9536 location spec's symtab and line as the default symtab and line
9537 for the end of the range. This makes it possible to have ranges
9538 like "foo.c:27, +14", where +14 means 14 lines from the start
9539 location spec. */
9540 location_spec_up end_locspec
9541 = string_to_location_spec (&arg, current_language);
9542 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9543 sal_start.symtab, sal_start.line,
9544 &canonical_end, NULL, NULL);
9545
6c5b2ebe 9546 if (canonical_end.lsals.empty ())
f1310107 9547 error (_("Could not find location of the end of the range."));
f8eba3c6 9548
6c5b2ebe
PA
9549 const linespec_sals &lsal_end = canonical_end.lsals[0];
9550 if (canonical_end.lsals.size () > 1
9551 || lsal_end.sals.size () != 1)
f1310107
TJB
9552 error (_("Cannot create a ranged breakpoint with multiple locations."));
9553
6c5b2ebe 9554 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9555
9556 end = find_breakpoint_range_end (sal_end);
9557 if (sal_start.pc > end)
177b42fe 9558 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9559
9560 length = end - sal_start.pc + 1;
9561 if (length < 0)
9562 /* Length overflowed. */
9563 error (_("Address range too large."));
9564 else if (length == 1)
9565 {
9566 /* This range is simple enough to be handled by
9567 the `hbreak' command. */
81b1e71c 9568 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9569
9570 return;
9571 }
9572
b925bf21
PA
9573 /* Now set up the breakpoint and install it. */
9574
9575 std::unique_ptr<breakpoint> br
9576 (new ranged_breakpoint (get_current_arch (),
9577 sal_start, length,
264f9890
PA
9578 std::move (start_locspec),
9579 std::move (end_locspec)));
f1310107 9580
27a62b43 9581 install_breakpoint (false, std::move (br), true);
f1310107
TJB
9582}
9583
4a64f543
MS
9584/* Return non-zero if EXP is verified as constant. Returned zero
9585 means EXP is variable. Also the constant detection may fail for
9586 some constant expressions and in such case still falsely return
9587 zero. */
2e6e3d9c 9588
dab72643 9589static bool
65d79d4b
SDJ
9590watchpoint_exp_is_const (const struct expression *exp)
9591{
1eaebe02 9592 return exp->op->constant_p ();
65d79d4b
SDJ
9593}
9594
3a292923 9595/* Implement the "re_set" method for watchpoints. */
348d480f 9596
3a292923
TT
9597void
9598watchpoint::re_set ()
348d480f
PA
9599{
9600 /* Watchpoint can be either on expression using entirely global
9601 variables, or it can be on local variables.
9602
9603 Watchpoints of the first kind are never auto-deleted, and even
9604 persist across program restarts. Since they can use variables
9605 from shared libraries, we need to reparse expression as libraries
9606 are loaded and unloaded.
9607
9608 Watchpoints on local variables can also change meaning as result
9609 of solib event. For example, if a watchpoint uses both a local
9610 and a global variables in expression, it's a local watchpoint,
9611 but unloading of a shared library will make the expression
9612 invalid. This is not a very common use case, but we still
9613 re-evaluate expression, to avoid surprises to the user.
9614
9615 Note that for local watchpoints, we re-evaluate it only if
9616 watchpoints frame id is still valid. If it's not, it means the
9617 watchpoint is out of scope and will be deleted soon. In fact,
9618 I'm not sure we'll ever be called in this case.
9619
9620 If a local watchpoint's frame id is still valid, then
3a292923 9621 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9622
3a5c3e22
PA
9623 Don't do anything about disabled watchpoints, since they will be
9624 reevaluated again when enabled. */
d8de7963 9625 update_watchpoint (this, true /* reparse */);
348d480f
PA
9626}
9627
3a292923 9628/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9629
3a292923
TT
9630int
9631watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9632{
3a292923 9633 int length = exact ? 1 : bl->length;
e09342b5
TJB
9634
9635 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9636 cond_exp.get ());
77b06cd7
TJB
9637}
9638
3a292923 9639/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9640
3a292923
TT
9641int
9642watchpoint::remove_location (struct bp_location *bl,
9643 enum remove_bp_reason reason)
77b06cd7 9644{
3a292923 9645 int length = exact ? 1 : bl->length;
e09342b5
TJB
9646
9647 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9648 cond_exp.get ());
e09342b5
TJB
9649}
9650
3a292923
TT
9651int
9652watchpoint::breakpoint_hit (const struct bp_location *bl,
9653 const address_space *aspace, CORE_ADDR bp_addr,
9654 const target_waitstatus &ws)
e09342b5 9655{
348d480f 9656 struct breakpoint *b = bl->owner;
77b06cd7 9657
348d480f
PA
9658 /* Continuable hardware watchpoints are treated as non-existent if the
9659 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9660 some data address). Otherwise gdb won't stop on a break instruction
9661 in the code (not from a breakpoint) when a hardware watchpoint has
9662 been defined. Also skip watchpoints which we know did not trigger
9663 (did not match the data address). */
9664 if (is_hardware_watchpoint (b)
3a292923 9665 && watchpoint_triggered == watch_triggered_no)
348d480f 9666 return 0;
9c06b0b4 9667
348d480f 9668 return 1;
9c06b0b4
TJB
9669}
9670
3a292923
TT
9671void
9672watchpoint::check_status (bpstat *bs)
9c06b0b4 9673{
348d480f 9674 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9675
348d480f 9676 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9677}
9678
3a292923
TT
9679/* Implement the "resources_needed" method for hardware
9680 watchpoints. */
9c06b0b4 9681
3a292923
TT
9682int
9683watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9684{
3a292923 9685 int length = exact? 1 : bl->length;
348d480f
PA
9686
9687 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9688}
9689
3a292923
TT
9690/* Implement the "works_in_software_mode" method for hardware
9691 watchpoints. */
9c06b0b4 9692
5a61e176 9693bool
3a292923 9694watchpoint::works_in_software_mode () const
9c06b0b4 9695{
efa80663 9696 /* Read and access watchpoints only work with hardware support. */
3a292923 9697 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9698}
9699
3a292923 9700enum print_stop_action
7bd86313 9701watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9702{
348d480f 9703 struct breakpoint *b;
348d480f 9704 enum print_stop_action result;
79a45e25 9705 struct ui_out *uiout = current_uiout;
348d480f
PA
9706
9707 gdb_assert (bs->bp_location_at != NULL);
9708
348d480f
PA
9709 b = bs->breakpoint_at;
9710
f303dbd6
PA
9711 annotate_watchpoint (b->number);
9712 maybe_print_thread_hit_breakpoint (uiout);
9713
d7e74731
PA
9714 string_file stb;
9715
76f9c9cf 9716 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
9717 switch (b->type)
9718 {
348d480f 9719 case bp_watchpoint:
9c06b0b4 9720 case bp_hardware_watchpoint:
112e8700
SM
9721 if (uiout->is_mi_like_p ())
9722 uiout->field_string
9723 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 9724 mention (b);
76f9c9cf 9725 tuple_emitter.emplace (uiout, "value");
112e8700 9726 uiout->text ("\nOld value = ");
850645cf 9727 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9728 uiout->field_stream ("old", stb);
9729 uiout->text ("\nNew value = ");
3a292923 9730 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9731 uiout->field_stream ("new", stb);
9732 uiout->text ("\n");
348d480f
PA
9733 /* More than one watchpoint may have been triggered. */
9734 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9735 break;
9736
9737 case bp_read_watchpoint:
112e8700
SM
9738 if (uiout->is_mi_like_p ())
9739 uiout->field_string
9740 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 9741 mention (b);
76f9c9cf 9742 tuple_emitter.emplace (uiout, "value");
112e8700 9743 uiout->text ("\nValue = ");
3a292923 9744 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9745 uiout->field_stream ("value", stb);
9746 uiout->text ("\n");
348d480f 9747 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9748 break;
9749
9750 case bp_access_watchpoint:
348d480f
PA
9751 if (bs->old_val != NULL)
9752 {
112e8700
SM
9753 if (uiout->is_mi_like_p ())
9754 uiout->field_string
9755 ("reason",
348d480f
PA
9756 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9757 mention (b);
76f9c9cf 9758 tuple_emitter.emplace (uiout, "value");
112e8700 9759 uiout->text ("\nOld value = ");
850645cf 9760 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9761 uiout->field_stream ("old", stb);
9762 uiout->text ("\nNew value = ");
348d480f
PA
9763 }
9764 else
9765 {
9766 mention (b);
112e8700
SM
9767 if (uiout->is_mi_like_p ())
9768 uiout->field_string
9769 ("reason",
348d480f 9770 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 9771 tuple_emitter.emplace (uiout, "value");
112e8700 9772 uiout->text ("\nValue = ");
348d480f 9773 }
3a292923 9774 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9775 uiout->field_stream ("new", stb);
9776 uiout->text ("\n");
348d480f 9777 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9778 break;
9779 default:
348d480f 9780 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9781 }
9782
348d480f
PA
9783 return result;
9784}
9785
3a292923 9786/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 9787
3a292923 9788void
b713485d 9789watchpoint::print_mention () const
348d480f 9790{
79a45e25 9791 struct ui_out *uiout = current_uiout;
46b9c129 9792 const char *tuple_name;
348d480f 9793
3a292923 9794 switch (type)
348d480f
PA
9795 {
9796 case bp_watchpoint:
112e8700 9797 uiout->text ("Watchpoint ");
46b9c129 9798 tuple_name = "wpt";
348d480f
PA
9799 break;
9800 case bp_hardware_watchpoint:
112e8700 9801 uiout->text ("Hardware watchpoint ");
46b9c129 9802 tuple_name = "wpt";
348d480f
PA
9803 break;
9804 case bp_read_watchpoint:
112e8700 9805 uiout->text ("Hardware read watchpoint ");
46b9c129 9806 tuple_name = "hw-rwpt";
348d480f
PA
9807 break;
9808 case bp_access_watchpoint:
112e8700 9809 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 9810 tuple_name = "hw-awpt";
348d480f
PA
9811 break;
9812 default:
f34652de 9813 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9814 }
9815
46b9c129 9816 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9817 uiout->field_signed ("number", number);
112e8700 9818 uiout->text (": ");
3a292923 9819 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
9820}
9821
3a292923 9822/* Implement the "print_recreate" method for watchpoints. */
348d480f 9823
3a292923 9824void
4d1ae558 9825watchpoint::print_recreate (struct ui_file *fp) const
348d480f 9826{
3a292923 9827 switch (type)
348d480f
PA
9828 {
9829 case bp_watchpoint:
9830 case bp_hardware_watchpoint:
6cb06a8c 9831 gdb_printf (fp, "watch");
348d480f
PA
9832 break;
9833 case bp_read_watchpoint:
6cb06a8c 9834 gdb_printf (fp, "rwatch");
348d480f
PA
9835 break;
9836 case bp_access_watchpoint:
6cb06a8c 9837 gdb_printf (fp, "awatch");
348d480f
PA
9838 break;
9839 default:
f34652de 9840 internal_error (_("Invalid watchpoint type."));
348d480f
PA
9841 }
9842
3a292923 9843 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 9844 print_recreate_thread (fp);
348d480f
PA
9845}
9846
3a292923 9847/* Implement the "explains_signal" method for watchpoints. */
427cd150 9848
a6860f3a 9849bool
3a292923 9850watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
9851{
9852 /* A software watchpoint cannot cause a signal other than
9853 GDB_SIGNAL_TRAP. */
3a292923 9854 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 9855 return false;
427cd150 9856
a6860f3a 9857 return true;
427cd150
TT
9858}
9859
3a292923
TT
9860struct masked_watchpoint : public watchpoint
9861{
73063f51
TT
9862 using watchpoint::watchpoint;
9863
3a292923
TT
9864 int insert_location (struct bp_location *) override;
9865 int remove_location (struct bp_location *,
9866 enum remove_bp_reason reason) override;
9867 int resources_needed (const struct bp_location *) override;
5a61e176 9868 bool works_in_software_mode () const override;
7bd86313 9869 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 9870 void print_one_detail (struct ui_out *) const override;
b713485d 9871 void print_mention () const override;
4d1ae558 9872 void print_recreate (struct ui_file *fp) const override;
3a292923 9873};
348d480f 9874
3a292923 9875/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 9876
3a292923
TT
9877int
9878masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 9879{
3a292923 9880 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
9881 bl->watchpoint_type);
9882}
9883
3a292923 9884/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 9885
3a292923
TT
9886int
9887masked_watchpoint::remove_location (struct bp_location *bl,
9888 enum remove_bp_reason reason)
348d480f 9889{
3a292923 9890 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 9891 bl->watchpoint_type);
348d480f
PA
9892}
9893
3a292923
TT
9894/* Implement the "resources_needed" method for masked hardware
9895 watchpoints. */
348d480f 9896
3a292923
TT
9897int
9898masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 9899{
3a292923 9900 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
9901}
9902
3a292923
TT
9903/* Implement the "works_in_software_mode" method for masked hardware
9904 watchpoints. */
348d480f 9905
5a61e176 9906bool
3a292923 9907masked_watchpoint::works_in_software_mode () const
348d480f 9908{
5a61e176 9909 return false;
348d480f
PA
9910}
9911
3a292923
TT
9912/* Implement the "print_it" method for masked hardware
9913 watchpoints. */
348d480f 9914
3a292923 9915enum print_stop_action
7bd86313 9916masked_watchpoint::print_it (const bpstat *bs) const
348d480f
PA
9917{
9918 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9919 struct ui_out *uiout = current_uiout;
348d480f
PA
9920
9921 /* Masked watchpoints have only one location. */
9922 gdb_assert (b->loc && b->loc->next == NULL);
9923
f303dbd6
PA
9924 annotate_watchpoint (b->number);
9925 maybe_print_thread_hit_breakpoint (uiout);
9926
348d480f
PA
9927 switch (b->type)
9928 {
9929 case bp_hardware_watchpoint:
112e8700
SM
9930 if (uiout->is_mi_like_p ())
9931 uiout->field_string
9932 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9933 break;
9934
9935 case bp_read_watchpoint:
112e8700
SM
9936 if (uiout->is_mi_like_p ())
9937 uiout->field_string
9938 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9939 break;
9940
9941 case bp_access_watchpoint:
112e8700
SM
9942 if (uiout->is_mi_like_p ())
9943 uiout->field_string
9944 ("reason",
348d480f
PA
9945 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9946 break;
9947 default:
f34652de 9948 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9949 }
9950
9951 mention (b);
112e8700 9952 uiout->text (_("\n\
9c06b0b4
TJB
9953Check the underlying instruction at PC for the memory\n\
9954address and value which triggered this watchpoint.\n"));
112e8700 9955 uiout->text ("\n");
9c06b0b4
TJB
9956
9957 /* More than one watchpoint may have been triggered. */
9958 return PRINT_UNKNOWN;
9959}
9960
3a292923
TT
9961/* Implement the "print_one_detail" method for masked hardware
9962 watchpoints. */
9c06b0b4 9963
3a292923
TT
9964void
9965masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
9966{
9967 /* Masked watchpoints have only one location. */
3a292923 9968 gdb_assert (loc && loc->next == NULL);
9c06b0b4 9969
112e8700 9970 uiout->text ("\tmask ");
3a292923 9971 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
112e8700 9972 uiout->text ("\n");
9c06b0b4
TJB
9973}
9974
3a292923
TT
9975/* Implement the "print_mention" method for masked hardware
9976 watchpoints. */
9c06b0b4 9977
3a292923 9978void
b713485d 9979masked_watchpoint::print_mention () const
9c06b0b4 9980{
79a45e25 9981 struct ui_out *uiout = current_uiout;
46b9c129 9982 const char *tuple_name;
9c06b0b4 9983
3a292923 9984 switch (type)
9c06b0b4
TJB
9985 {
9986 case bp_hardware_watchpoint:
112e8700 9987 uiout->text ("Masked hardware watchpoint ");
46b9c129 9988 tuple_name = "wpt";
9c06b0b4
TJB
9989 break;
9990 case bp_read_watchpoint:
112e8700 9991 uiout->text ("Masked hardware read watchpoint ");
46b9c129 9992 tuple_name = "hw-rwpt";
9c06b0b4
TJB
9993 break;
9994 case bp_access_watchpoint:
112e8700 9995 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 9996 tuple_name = "hw-awpt";
9c06b0b4
TJB
9997 break;
9998 default:
f34652de 9999 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10000 }
10001
46b9c129 10002 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 10003 uiout->field_signed ("number", number);
112e8700 10004 uiout->text (": ");
3a292923 10005 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
10006}
10007
3a292923
TT
10008/* Implement the "print_recreate" method for masked hardware
10009 watchpoints. */
9c06b0b4 10010
3a292923 10011void
4d1ae558 10012masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 10013{
3a292923 10014 switch (type)
9c06b0b4
TJB
10015 {
10016 case bp_hardware_watchpoint:
6cb06a8c 10017 gdb_printf (fp, "watch");
9c06b0b4
TJB
10018 break;
10019 case bp_read_watchpoint:
6cb06a8c 10020 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
10021 break;
10022 case bp_access_watchpoint:
6cb06a8c 10023 gdb_printf (fp, "awatch");
9c06b0b4
TJB
10024 break;
10025 default:
f34652de 10026 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10027 }
10028
3a292923
TT
10029 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10030 phex (hw_wp_mask, sizeof (CORE_ADDR)));
04d0163c 10031 print_recreate_thread (fp);
9c06b0b4
TJB
10032}
10033
9c06b0b4
TJB
10034/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10035
f2478a7e 10036static bool
9c06b0b4
TJB
10037is_masked_watchpoint (const struct breakpoint *b)
10038{
3a292923 10039 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
10040}
10041
53a5351d 10042/* accessflag: hw_write: watch write,
dda83cd7 10043 hw_read: watch read,
53a5351d 10044 hw_access: watch access (read or write) */
c906108c 10045static void
bbc13ae3 10046watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10047 bool just_location, bool internal)
c906108c 10048{
c1fc2657 10049 struct breakpoint *scope_breakpoint = NULL;
270140bd 10050 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10051 struct value *result;
bb9d5f81 10052 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10053 const char *exp_start = NULL;
10054 const char *exp_end = NULL;
10055 const char *tok, *end_tok;
9c06b0b4 10056 int toklen = -1;
bbc13ae3
KS
10057 const char *cond_start = NULL;
10058 const char *cond_end = NULL;
c906108c 10059 enum bptype bp_type;
37e4754d 10060 int thread = -1;
9c06b0b4
TJB
10061 /* Flag to indicate whether we are going to use masks for
10062 the hardware watchpoint. */
2e362716 10063 bool use_mask = false;
9c06b0b4 10064 CORE_ADDR mask = 0;
2ecee236 10065 int task = -1;
c906108c 10066
37e4754d
LM
10067 /* Make sure that we actually have parameters to parse. */
10068 if (arg != NULL && arg[0] != '\0')
10069 {
bbc13ae3
KS
10070 const char *value_start;
10071
10072 exp_end = arg + strlen (arg);
37e4754d 10073
9c06b0b4
TJB
10074 /* Look for "parameter value" pairs at the end
10075 of the arguments string. */
bbc13ae3 10076 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10077 {
10078 /* Skip whitespace at the end of the argument list. */
10079 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10080 tok--;
10081
10082 /* Find the beginning of the last token.
10083 This is the value of the parameter. */
10084 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10085 tok--;
10086 value_start = tok + 1;
10087
10088 /* Skip whitespace. */
10089 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10090 tok--;
10091
10092 end_tok = tok;
10093
10094 /* Find the beginning of the second to last token.
10095 This is the parameter itself. */
10096 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10097 tok--;
10098 tok++;
10099 toklen = end_tok - tok + 1;
10100
61012eef 10101 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10102 {
5d5658a1 10103 struct thread_info *thr;
9c06b0b4
TJB
10104 /* At this point we've found a "thread" token, which means
10105 the user is trying to set a watchpoint that triggers
10106 only in a specific thread. */
5d5658a1 10107 const char *endp;
37e4754d 10108
9c06b0b4
TJB
10109 if (thread != -1)
10110 error(_("You can specify only one thread."));
37e4754d 10111
2ecee236 10112 if (task != -1)
0a9ccb9d
AB
10113 error (_("You can specify only one of thread or task."));
10114
9c06b0b4 10115 /* Extract the thread ID from the next token. */
5d5658a1 10116 thr = parse_thread_id (value_start, &endp);
37e4754d 10117
5d5658a1 10118 /* Check if the user provided a valid thread ID. */
9c06b0b4 10119 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10120 invalid_thread_id_error (value_start);
9c06b0b4 10121
5d5658a1 10122 thread = thr->global_num;
9c06b0b4 10123 }
8a18382f
TT
10124 else if (toklen == 4 && startswith (tok, "task"))
10125 {
10126 char *tmp;
10127
2ecee236 10128 if (task != -1)
980dbf36
AB
10129 error(_("You can specify only one task."));
10130
0a9ccb9d
AB
10131 if (thread != -1)
10132 error (_("You can specify only one of thread or task."));
10133
8a18382f
TT
10134 task = strtol (value_start, &tmp, 0);
10135 if (tmp == value_start)
10136 error (_("Junk after task keyword."));
10137 if (!valid_task_id (task))
10138 error (_("Unknown task %d."), task);
10139 }
61012eef 10140 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10141 {
10142 /* We've found a "mask" token, which means the user wants to
10143 create a hardware watchpoint that is going to have the mask
10144 facility. */
65558ca5 10145 struct value *mask_value;
37e4754d 10146
9c06b0b4
TJB
10147 if (use_mask)
10148 error(_("You can specify only one mask."));
37e4754d 10149
2e362716 10150 use_mask = just_location = true;
37e4754d 10151
65558ca5 10152 scoped_value_mark mark;
9c06b0b4
TJB
10153 mask_value = parse_to_comma_and_eval (&value_start);
10154 mask = value_as_address (mask_value);
9c06b0b4
TJB
10155 }
10156 else
10157 /* We didn't recognize what we found. We should stop here. */
10158 break;
37e4754d 10159
9c06b0b4
TJB
10160 /* Truncate the string and get rid of the "parameter value" pair before
10161 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10162 exp_end = tok;
9c06b0b4 10163 }
37e4754d 10164 }
bbc13ae3
KS
10165 else
10166 exp_end = arg;
37e4754d 10167
bbc13ae3
KS
10168 /* Parse the rest of the arguments. From here on out, everything
10169 is in terms of a newly allocated string instead of the original
10170 ARG. */
81b1e71c
TT
10171 std::string expression (arg, exp_end - arg);
10172 exp_start = arg = expression.c_str ();
699bd4cf
TT
10173 innermost_block_tracker tracker;
10174 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10175 exp_end = arg;
fa8a61dc
TT
10176 /* Remove trailing whitespace from the expression before saving it.
10177 This makes the eventual display of the expression string a bit
10178 prettier. */
10179 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10180 --exp_end;
10181
65d79d4b 10182 /* Checking if the expression is not constant. */
4d01a485 10183 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10184 {
10185 int len;
10186
10187 len = exp_end - exp_start;
10188 while (len > 0 && isspace (exp_start[len - 1]))
10189 len--;
10190 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10191 }
10192
699bd4cf 10193 exp_valid_block = tracker.block ();
b926417a 10194 struct value *mark = value_mark ();
850645cf 10195 struct value *val_as_value = nullptr;
1eaebe02
TT
10196 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10197 just_location);
06a64a0b 10198
850645cf 10199 if (val_as_value != NULL && just_location)
bb9d5f81 10200 {
5011c493 10201 saved_bitpos = val_as_value->bitpos ();
f49d5fa2 10202 saved_bitsize = val_as_value->bitsize ();
bb9d5f81
PP
10203 }
10204
850645cf 10205 value_ref_ptr val;
06a64a0b
TT
10206 if (just_location)
10207 {
9c06b0b4
TJB
10208 int ret;
10209
06a64a0b 10210 exp_valid_block = NULL;
850645cf 10211 val = release_value (value_addr (result));
06a64a0b 10212 value_free_to_mark (mark);
9c06b0b4
TJB
10213
10214 if (use_mask)
10215 {
850645cf 10216 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10217 mask);
10218 if (ret == -1)
10219 error (_("This target does not support masked watchpoints."));
10220 else if (ret == -2)
10221 error (_("Invalid mask or memory region."));
10222 }
06a64a0b 10223 }
850645cf
TT
10224 else if (val_as_value != NULL)
10225 val = release_value (val_as_value);
c906108c 10226
f1735a53
TT
10227 tok = skip_spaces (arg);
10228 end_tok = skip_to_space (tok);
c906108c
SS
10229
10230 toklen = end_tok - tok;
10231 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10232 {
10233 tok = cond_start = end_tok + 1;
699bd4cf
TT
10234 innermost_block_tracker if_tracker;
10235 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10236
10237 /* The watchpoint expression may not be local, but the condition
10238 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10239 cond_exp_valid_block = if_tracker.block ();
60e1c644 10240
c906108c
SS
10241 cond_end = tok;
10242 }
10243 if (*tok)
8a3fe4f8 10244 error (_("Junk at end of command."));
c906108c 10245
bd2b40ac 10246 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
441d7c93
PA
10247
10248 /* Save this because create_internal_breakpoint below invalidates
10249 'wp_frame'. */
10250 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10251
10252 /* If the expression is "local", then set up a "watchpoint scope"
10253 breakpoint at the point where we've left the scope of the watchpoint
10254 expression. Create the scope breakpoint before the watchpoint, so
10255 that we will encounter it first in bpstat_stop_status. */
441d7c93 10256 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10257 {
441d7c93
PA
10258 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10259
10260 if (frame_id_p (caller_frame_id))
edb3359d 10261 {
441d7c93
PA
10262 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10263 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10264
24b21115 10265 scope_breakpoint
441d7c93 10266 = create_internal_breakpoint (caller_arch, caller_pc,
9efa3c7f 10267 bp_watchpoint_scope);
d983da9c 10268
441d7c93
PA
10269 /* create_internal_breakpoint could invalidate WP_FRAME. */
10270 wp_frame = NULL;
10271
edb3359d 10272 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10273
edb3359d
DJ
10274 /* Automatically delete the breakpoint when it hits. */
10275 scope_breakpoint->disposition = disp_del;
d983da9c 10276
edb3359d 10277 /* Only break in the proper frame (help with recursion). */
441d7c93 10278 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10279
edb3359d 10280 /* Set the address at which we will stop. */
441d7c93
PA
10281 scope_breakpoint->loc->gdbarch = caller_arch;
10282 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10283 scope_breakpoint->loc->address
a6d9a66e
UW
10284 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10285 scope_breakpoint->loc->requested_address,
60270718
AB
10286 scope_breakpoint->type,
10287 current_program_space);
edb3359d 10288 }
d983da9c
DJ
10289 }
10290
e8369a73
AB
10291 /* Now set up the breakpoint. We create all watchpoints as hardware
10292 watchpoints here even if hardware watchpoints are turned off, a call
10293 to update_watchpoint later in this function will cause the type to
10294 drop back to bp_watchpoint (software watchpoint) if required. */
10295
10296 if (accessflag == hw_read)
10297 bp_type = bp_read_watchpoint;
10298 else if (accessflag == hw_access)
10299 bp_type = bp_access_watchpoint;
10300 else
10301 bp_type = bp_hardware_watchpoint;
3a5c3e22 10302
3a292923 10303 std::unique_ptr<watchpoint> w;
348d480f 10304 if (use_mask)
73063f51 10305 w.reset (new masked_watchpoint (nullptr, bp_type));
348d480f 10306 else
73063f51 10307 w.reset (new watchpoint (nullptr, bp_type));
3a292923 10308
0a9ccb9d 10309 /* At most one of thread or task can be set on a watchpoint. */
2ecee236 10310 gdb_assert (thread == -1 || task == -1);
c1fc2657 10311 w->thread = thread;
8a18382f 10312 w->task = task;
c1fc2657
SM
10313 w->disposition = disp_donttouch;
10314 w->pspace = current_program_space;
b22e99fd 10315 w->exp = std::move (exp);
3a5c3e22
PA
10316 w->exp_valid_block = exp_valid_block;
10317 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10318 if (just_location)
10319 {
f28085df 10320 struct type *t = val->type ();
850645cf 10321 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10322
43cc5389 10323 w->exp_string_reparse
a4c50be3 10324 = current_language->watch_location_expression (t, addr);
06a64a0b 10325
8579fd13
AB
10326 w->exp_string = xstrprintf ("-location %.*s",
10327 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10328 }
10329 else
a4c50be3 10330 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10331
10332 if (use_mask)
10333 {
3a5c3e22 10334 w->hw_wp_mask = mask;
9c06b0b4
TJB
10335 }
10336 else
10337 {
3a5c3e22 10338 w->val = val;
bb9d5f81
PP
10339 w->val_bitpos = saved_bitpos;
10340 w->val_bitsize = saved_bitsize;
4c1d86d9 10341 w->val_valid = true;
9c06b0b4 10342 }
77b06cd7 10343
c906108c 10344 if (cond_start)
6f781ee3 10345 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10346 else
c1fc2657 10347 w->cond_string = 0;
c5aa993b 10348
441d7c93 10349 if (frame_id_p (watchpoint_frame))
f6bc2008 10350 {
441d7c93 10351 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10352 w->watchpoint_thread = inferior_ptid;
f6bc2008 10353 }
c906108c 10354 else
f6bc2008 10355 {
3a5c3e22
PA
10356 w->watchpoint_frame = null_frame_id;
10357 w->watchpoint_thread = null_ptid;
f6bc2008 10358 }
c906108c 10359
d983da9c 10360 if (scope_breakpoint != NULL)
c906108c 10361 {
d983da9c
DJ
10362 /* The scope breakpoint is related to the watchpoint. We will
10363 need to act on them together. */
c1fc2657 10364 w->related_breakpoint = scope_breakpoint;
b270e6f9 10365 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10366 }
d983da9c 10367
06a64a0b
TT
10368 if (!just_location)
10369 value_free_to_mark (mark);
2d134ed3 10370
b270e6f9
TT
10371 /* Finally update the new watchpoint. This creates the locations
10372 that should be inserted. */
d8de7963 10373 update_watchpoint (w.get (), true /* reparse */);
a9634178 10374
b270e6f9 10375 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10376}
10377
e09342b5 10378/* Return count of debug registers needed to watch the given expression.
e09342b5 10379 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10380
c906108c 10381static int
a6535de1 10382can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10383{
10384 int found_memory_cnt = 0;
10385
10386 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10387 if (!can_use_hw_watchpoints)
c906108c 10388 return 0;
c5aa993b 10389
a6535de1
TT
10390 gdb_assert (!vals.empty ());
10391 struct value *head = vals[0].get ();
10392
5c44784c
JM
10393 /* Make sure that the value of the expression depends only upon
10394 memory contents, and values computed from them within GDB. If we
10395 find any register references or function calls, we can't use a
10396 hardware watchpoint.
10397
10398 The idea here is that evaluating an expression generates a series
10399 of values, one holding the value of every subexpression. (The
10400 expression a*b+c has five subexpressions: a, b, a*b, c, and
10401 a*b+c.) GDB's values hold almost enough information to establish
10402 the criteria given above --- they identify memory lvalues,
10403 register lvalues, computed values, etcetera. So we can evaluate
10404 the expression, and then scan the chain of values that leaves
10405 behind to decide whether we can detect any possible change to the
10406 expression's final value using only hardware watchpoints.
10407
10408 However, I don't think that the values returned by inferior
10409 function calls are special in any way. So this function may not
10410 notice that an expression involving an inferior function call
10411 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10412 for (const value_ref_ptr &iter : vals)
c906108c 10413 {
a6535de1
TT
10414 struct value *v = iter.get ();
10415
736355f2 10416 if (v->lval () == lval_memory)
c906108c 10417 {
3ee3b270 10418 if (v != head && v->lazy ())
8464be76
DJ
10419 /* A lazy memory lvalue in the chain is one that GDB never
10420 needed to fetch; we either just used its address (e.g.,
10421 `a' in `a.b') or we never needed it at all (e.g., `a'
10422 in `a,b'). This doesn't apply to HEAD; if that is
10423 lazy then it was not readable, but watch it anyway. */
5c44784c 10424 ;
53a5351d 10425 else
5c44784c
JM
10426 {
10427 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10428 it with hardware watchpoints. */
d0c97917 10429 struct type *vtype = check_typedef (v->type ());
2e70b7b9
MS
10430
10431 /* We only watch structs and arrays if user asked for it
10432 explicitly, never if they just happen to appear in a
10433 middle of some value chain. */
10434 if (v == head
78134374
SM
10435 || (vtype->code () != TYPE_CODE_STRUCT
10436 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10437 {
9feb2d07 10438 CORE_ADDR vaddr = v->address ();
e09342b5
TJB
10439 int len;
10440 int num_regs;
10441
a9634178 10442 len = (target_exact_watchpoints
e09342b5 10443 && is_scalar_type_recursive (vtype))?
d0c97917 10444 1 : v->type ()->length ();
2e70b7b9 10445
e09342b5
TJB
10446 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10447 if (!num_regs)
2e70b7b9
MS
10448 return 0;
10449 else
e09342b5 10450 found_memory_cnt += num_regs;
2e70b7b9 10451 }
5c44784c 10452 }
c5aa993b 10453 }
b2227e67 10454 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
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 }
b1ffd112 13522 catch (const gdb_exception_error &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 14144 if (thread != -1)
442716d4
AB
14145 {
14146 struct thread_info *thr = find_thread_global_id (thread);
14147 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14148 }
d9b3f62e 14149
2ecee236 14150 if (task != -1)
04d0163c 14151 gdb_printf (fp, " task %d", task);
d9b3f62e 14152
6cb06a8c 14153 gdb_printf (fp, "\n");
d9b3f62e
PA
14154}
14155
6149aea9
PA
14156/* Save information on user settable breakpoints (watchpoints, etc) to
14157 a new script file named FILENAME. If FILTER is non-NULL, call it
14158 on each breakpoint and only include the ones for which it returns
f2478a7e 14159 true. */
6149aea9 14160
1042e4c0 14161static void
4495129a 14162save_breakpoints (const char *filename, int from_tty,
f2478a7e 14163 bool (*filter) (const struct breakpoint *))
1042e4c0 14164{
d8de7963 14165 bool any = false;
6149aea9 14166 int extra_trace_bits = 0;
1042e4c0 14167
6149aea9
PA
14168 if (filename == 0 || *filename == 0)
14169 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14170
14171 /* See if we have anything to save. */
43892fdf 14172 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14173 {
14174 /* Skip internal and momentary breakpoints. */
14175 if (!user_breakpoint_p (tp))
14176 continue;
6149aea9 14177
01add95b
SM
14178 /* If we have a filter, only save the breakpoints it accepts. */
14179 if (filter && !filter (tp))
14180 continue;
6149aea9 14181
d8de7963 14182 any = true;
6149aea9 14183
01add95b
SM
14184 if (is_tracepoint (tp))
14185 {
14186 extra_trace_bits = 1;
6149aea9 14187
01add95b
SM
14188 /* We can stop searching. */
14189 break;
14190 }
14191 }
6149aea9
PA
14192
14193 if (!any)
1042e4c0 14194 {
6149aea9 14195 warning (_("Nothing to save."));
1042e4c0
SS
14196 return;
14197 }
14198
ee0c3293 14199 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14200
14201 stdio_file fp;
14202
ee0c3293 14203 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14204 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14205 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14206
6149aea9 14207 if (extra_trace_bits)
d7e74731 14208 save_trace_state_variables (&fp);
8bf6485c 14209
43892fdf 14210 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14211 {
14212 /* Skip internal and momentary breakpoints. */
14213 if (!user_breakpoint_p (tp))
14214 continue;
8bf6485c 14215
01add95b
SM
14216 /* If we have a filter, only save the breakpoints it accepts. */
14217 if (filter && !filter (tp))
14218 continue;
6149aea9 14219
c47614fe 14220 tp->print_recreate (&fp);
1042e4c0 14221
01add95b
SM
14222 /* Note, we can't rely on tp->number for anything, as we can't
14223 assume the recreated breakpoint numbers will match. Use $bpnum
14224 instead. */
6149aea9 14225
01add95b 14226 if (tp->cond_string)
6f781ee3 14227 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
6149aea9 14228
01add95b
SM
14229 if (tp->ignore_count)
14230 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14231
01add95b
SM
14232 if (tp->type != bp_dprintf && tp->commands)
14233 {
14234 fp.puts (" commands\n");
1042e4c0 14235
992aeed8
TT
14236 ui_out_redirect_pop redir (current_uiout, &fp);
14237 print_command_lines (current_uiout, tp->commands.get (), 2);
6149aea9 14238
01add95b
SM
14239 fp.puts (" end\n");
14240 }
6149aea9 14241
01add95b
SM
14242 if (tp->enable_state == bp_disabled)
14243 fp.puts ("disable $bpnum\n");
6149aea9 14244
01add95b
SM
14245 /* If this is a multi-location breakpoint, check if the locations
14246 should be individually disabled. Watchpoint locations are
14247 special, and not user visible. */
14248 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14249 {
14250 int n = 1;
40cb8ca5 14251
01add95b
SM
14252 for (bp_location *loc : tp->locations ())
14253 {
14254 if (!loc->enabled)
14255 fp.printf ("disable $bpnum.%d\n", n);
14256
14257 n++;
14258 }
14259 }
14260 }
8bf6485c 14261
e0700ba4
SM
14262 if (extra_trace_bits && !default_collect.empty ())
14263 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14264
1042e4c0 14265 if (from_tty)
6cb06a8c 14266 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14267}
14268
14269/* The `save breakpoints' command. */
14270
14271static void
4495129a 14272save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14273{
14274 save_breakpoints (args, from_tty, NULL);
14275}
14276
14277/* The `save tracepoints' command. */
14278
14279static void
4495129a 14280save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14281{
14282 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14283}
14284
c906108c 14285\f
629500fa
KS
14286/* This help string is used to consolidate all the help string for specifying
14287 locations used by several commands. */
14288
264f9890 14289#define LOCATION_SPEC_HELP_STRING \
629500fa
KS
14290"Linespecs are colon-separated lists of location parameters, such as\n\
14291source filename, function name, label name, and line number.\n\
14292Example: To specify the start of a label named \"the_top\" in the\n\
14293function \"fact\" in the file \"factorial.c\", use\n\
14294\"factorial.c:fact:the_top\".\n\
14295\n\
14296Address locations begin with \"*\" and specify an exact address in the\n\
14297program. Example: To specify the fourth byte past the start function\n\
14298\"main\", use \"*main + 4\".\n\
14299\n\
14300Explicit locations are similar to linespecs but use an option/argument\n\
14301syntax to specify location parameters.\n\
14302Example: To specify the start of the label named \"the_top\" in the\n\
14303function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14304-function fact -label the_top\".\n\
14305\n\
14306By default, a specified function is matched against the program's\n\
14307functions in all scopes. For C++, this means in all namespaces and\n\
14308classes. For Ada, this means in all packages. E.g., in C++,\n\
14309\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14310\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14311specified name as a complete fully-qualified name instead."
629500fa 14312
4a64f543
MS
14313/* This help string is used for the break, hbreak, tbreak and thbreak
14314 commands. It is defined as a macro to prevent duplication.
14315 COMMAND should be a string constant containing the name of the
14316 command. */
629500fa 14317
31e2b00f 14318#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14319command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14320\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14321PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14322probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
14323guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14324`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14325LOCATION may be a linespec, address, or explicit location as described\n\
14326below.\n\
14327\n\
dc10affe
PA
14328With no LOCATION, uses current execution address of the selected\n\
14329stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14330\n\
14331THREADNUM is the number from \"info threads\".\n\
14332CONDITION is a boolean expression.\n\
733d554a
TBA
14333\n\
14334With the \"-force-condition\" flag, the condition is defined even when\n\
14335it is invalid for all current locations.\n\
264f9890 14336\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14337Multiple breakpoints at one place are permitted, and useful if their\n\
14338conditions are different.\n\
31e2b00f
AS
14339\n\
14340Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14341
44feb3ce
TT
14342/* List of subcommands for "catch". */
14343static struct cmd_list_element *catch_cmdlist;
14344
14345/* List of subcommands for "tcatch". */
14346static struct cmd_list_element *tcatch_cmdlist;
14347
9ac4176b 14348void
a121b7c1 14349add_catch_command (const char *name, const char *docstring,
5538b03c 14350 cmd_func_ftype *func,
625e8578 14351 completer_ftype *completer,
44feb3ce
TT
14352 void *user_data_catch,
14353 void *user_data_tcatch)
14354{
14355 struct cmd_list_element *command;
14356
0450cc4c 14357 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14358 &catch_cmdlist);
5538b03c 14359 command->func = func;
0f8e2034 14360 command->set_context (user_data_catch);
a96d9b2e 14361 set_cmd_completer (command, completer);
44feb3ce 14362
0450cc4c 14363 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14364 &tcatch_cmdlist);
5538b03c 14365 command->func = func;
0f8e2034 14366 command->set_context (user_data_tcatch);
a96d9b2e 14367 set_cmd_completer (command, completer);
44feb3ce
TT
14368}
14369
d8de7963
AB
14370/* False if any of the breakpoint's locations could be a location where
14371 functions have been inlined, true otherwise. */
0574c78f 14372
d8de7963 14373static bool
0574c78f
GB
14374is_non_inline_function (struct breakpoint *b)
14375{
14376 /* The shared library event breakpoint is set on the address of a
14377 non-inline function. */
d8de7963 14378 return (b->type == bp_shlib_event);
0574c78f
GB
14379}
14380
14381/* Nonzero if the specified PC cannot be a location where functions
14382 have been inlined. */
14383
14384int
accd0bcd 14385pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14386 const target_waitstatus &ws)
0574c78f 14387{
43892fdf 14388 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
14389 {
14390 if (!is_non_inline_function (b))
14391 continue;
14392
40cb8ca5 14393 for (bp_location *bl : b->locations ())
0574c78f
GB
14394 {
14395 if (!bl->shlib_disabled
09ac7c10 14396 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14397 return 1;
14398 }
14399 }
14400
14401 return 0;
14402}
14403
2f202fde
JK
14404/* Remove any references to OBJFILE which is going to be freed. */
14405
14406void
14407breakpoint_free_objfile (struct objfile *objfile)
14408{
48d7020b 14409 for (bp_location *loc : all_bp_locations ())
3c86fae3 14410 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
2f202fde
JK
14411 loc->symtab = NULL;
14412}
14413
8bfd80db
YQ
14414/* Chain containing all defined "enable breakpoint" subcommands. */
14415
14416static struct cmd_list_element *enablebreaklist = NULL;
14417
8588b356
SM
14418/* See breakpoint.h. */
14419
14420cmd_list_element *commands_cmd_element = nullptr;
14421
6c265988 14422void _initialize_breakpoint ();
c906108c 14423void
6c265988 14424_initialize_breakpoint ()
c906108c
SS
14425{
14426 struct cmd_list_element *c;
14427
c90e7d63
SM
14428 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14429 "breakpoint");
14430 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14431 "breakpoint");
14432 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14433 "breakpoint");
84acb35a 14434
c906108c
SS
14435 breakpoint_chain = 0;
14436 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14437 before a breakpoint is set. */
14438 breakpoint_count = 0;
14439
1042e4c0
SS
14440 tracepoint_count = 0;
14441
1bedd215
AC
14442 add_com ("ignore", class_breakpoint, ignore_command, _("\
14443Set ignore-count of breakpoint number N to COUNT.\n\
14444Usage is `ignore N COUNT'."));
c906108c 14445
8588b356
SM
14446 commands_cmd_element = add_com ("commands", class_breakpoint,
14447 commands_command, _("\
18da0c51
MG
14448Set commands to be executed when the given breakpoints are hit.\n\
14449Give a space-separated breakpoint list as argument after \"commands\".\n\
14450A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14451(e.g. `5-7').\n\
c906108c
SS
14452With no argument, the targeted breakpoint is the last one set.\n\
14453The commands themselves follow starting on the next line.\n\
14454Type a line containing \"end\" to indicate the end of them.\n\
14455Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14456then no output is printed when it is hit, except what the commands print."));
c906108c 14457
b1d4d8d1
TBA
14458 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14459 static std::string condition_command_help
14460 = gdb::option::build_help (_("\
1bedd215 14461Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14462Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14463is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14464\n\
14465Options:\n\
14466%OPTIONS%"), cc_opts);
14467
14468 c = add_com ("condition", class_breakpoint, condition_command,
14469 condition_command_help.c_str ());
14470 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14471
1bedd215 14472 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14473Set a temporary breakpoint.\n\
c906108c
SS
14474Like \"break\" except the breakpoint is only temporary,\n\
14475so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14476by using \"enable delete\" on the breakpoint number.\n\
14477\n"
14478BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14479 set_cmd_completer (c, location_completer);
c94fdfd0 14480
1bedd215 14481 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14482Set a hardware assisted breakpoint.\n\
c906108c 14483Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14484some target hardware may not have this support.\n\
14485\n"
14486BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14487 set_cmd_completer (c, location_completer);
c906108c 14488
1bedd215 14489 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14490Set a temporary hardware assisted breakpoint.\n\
c906108c 14491Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14492so it will be deleted when hit.\n\
14493\n"
14494BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14495 set_cmd_completer (c, location_completer);
c906108c 14496
3947f654
SM
14497 cmd_list_element *enable_cmd
14498 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14499Enable all or some breakpoints.\n\
14500Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14501Give breakpoint numbers (separated by spaces) as arguments.\n\
14502With no subcommand, breakpoints are enabled until you command otherwise.\n\
14503This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14504With a subcommand you can enable temporarily."),
3947f654 14505 &enablelist, 1, &cmdlist);
c906108c 14506
3947f654 14507 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14508
84951ab5 14509 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14510Enable all or some breakpoints.\n\
14511Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14512Give breakpoint numbers (separated by spaces) as arguments.\n\
14513This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14514May be abbreviated to simply \"enable\"."),
2f822da5 14515 &enablebreaklist, 1, &enablelist);
c906108c 14516
1a966eab 14517 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14518Enable some breakpoints for one hit.\n\
14519Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14520If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14521 &enablebreaklist);
14522
1a966eab 14523 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14524Enable some breakpoints and delete when hit.\n\
14525Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14526If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14527 &enablebreaklist);
14528
816338b5 14529 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14530Enable some breakpoints for COUNT hits.\n\
14531Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14532If a breakpoint is hit while enabled in this fashion,\n\
14533the count is decremented; when it reaches zero, the breakpoint is disabled."),
14534 &enablebreaklist);
14535
1a966eab 14536 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14537Enable some breakpoints and delete when hit.\n\
14538Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14539If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14540 &enablelist);
14541
1a966eab 14542 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14543Enable some breakpoints for one hit.\n\
14544Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14545If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14546 &enablelist);
14547
14548 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14549Enable some breakpoints for COUNT hits.\n\
14550Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14551If a breakpoint is hit while enabled in this fashion,\n\
14552the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14553 &enablelist);
14554
3947f654
SM
14555 cmd_list_element *disable_cmd
14556 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14557Disable all or some breakpoints.\n\
14558Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14559Arguments are breakpoint numbers with spaces in between.\n\
14560To disable all breakpoints, give no argument.\n\
64b9b334 14561A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14562 &disablelist, 1, &cmdlist);
14563 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14564 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14565
57b4f16e 14566 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14567Disable all or some breakpoints.\n\
14568Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14569Arguments are breakpoint numbers with spaces in between.\n\
14570To disable all breakpoints, give no argument.\n\
64b9b334 14571A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14572This command may be abbreviated \"disable\"."),
c906108c
SS
14573 &disablelist);
14574
3947f654
SM
14575 cmd_list_element *delete_cmd
14576 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14577Delete all or some breakpoints.\n\
14578Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
14579Arguments are breakpoint numbers with spaces in between.\n\
14580To delete all breakpoints, give no argument.\n\
14581\n\
590042fc 14582Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14583 &deletelist, 1, &cmdlist);
14584 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14585 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14586
57b4f16e 14587 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14588Delete all or some breakpoints or auto-display expressions.\n\
14589Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14590Arguments are breakpoint numbers with spaces in between.\n\
14591To delete all breakpoints, give no argument.\n\
1a966eab 14592This command may be abbreviated \"delete\"."),
c906108c
SS
14593 &deletelist);
14594
3947f654
SM
14595 cmd_list_element *clear_cmd
14596 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14597Clear breakpoint at specified location.\n\
14598Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14599\n\
14600With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14601is executing in.\n"
264f9890 14602"\n" LOCATION_SPEC_HELP_STRING "\n\n\
1bedd215 14603See also the \"delete\" command which clears breakpoints by number."));
3947f654 14604 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14605
3947f654
SM
14606 cmd_list_element *break_cmd
14607 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14608Set breakpoint at specified location.\n"
31e2b00f 14609BREAK_ARGS_HELP ("break")));
3947f654 14610 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14611
3947f654
SM
14612 add_com_alias ("b", break_cmd, class_run, 1);
14613 add_com_alias ("br", break_cmd, class_run, 1);
14614 add_com_alias ("bre", break_cmd, class_run, 1);
14615 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14616
e0f25bd9
SM
14617 cmd_list_element *info_breakpoints_cmd
14618 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14619Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14620The \"Type\" column indicates one of:\n\
14621\tbreakpoint - normal breakpoint\n\
14622\twatchpoint - watchpoint\n\
14623The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14624the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14625breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14626address and file/line number respectively.\n\
14627\n\
14628Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14629are set to the address of the last breakpoint listed unless the command\n\
14630is prefixed with \"server \".\n\n\
c906108c 14631Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14632breakpoint set."));
c906108c 14633
e0f25bd9 14634 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14635
1a966eab
AC
14636 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14637Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14638The \"Type\" column indicates one of:\n\
14639\tbreakpoint - normal breakpoint\n\
14640\twatchpoint - watchpoint\n\
14641\tlongjmp - internal breakpoint used to step through longjmp()\n\
14642\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14643\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14644\tfinish - internal breakpoint used by the \"finish\" command\n\
14645The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14646the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14647breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14648address and file/line number respectively.\n\
14649\n\
14650Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14651are set to the address of the last breakpoint listed unless the command\n\
14652is prefixed with \"server \".\n\n\
c906108c 14653Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14654breakpoint set."),
c906108c
SS
14655 &maintenanceinfolist);
14656
3b6acaee 14657 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 14658Set catchpoints to catch events."),
2f822da5 14659 &catch_cmdlist,
3b6acaee 14660 0/*allow-unknown*/, &cmdlist);
44feb3ce 14661
3b6acaee 14662 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 14663Set temporary catchpoints to catch events."),
2f822da5 14664 &tcatch_cmdlist,
3b6acaee 14665 0/*allow-unknown*/, &cmdlist);
44feb3ce 14666
a15a5258
AB
14667 const auto opts = make_watch_options_def_group (nullptr);
14668
14669 static const std::string watch_help = gdb::option::build_help (_("\
14670Set a watchpoint for EXPRESSION.\n\
14671Usage: watch [-location] EXPRESSION\n\
14672\n\
14673Options:\n\
14674%OPTIONS%\n\
14675\n\
c906108c 14676A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
14677an expression changes."), opts);
14678 c = add_com ("watch", class_breakpoint, watch_command,
14679 watch_help.c_str ());
14680 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14681
14682 static const std::string rwatch_help = gdb::option::build_help (_("\
14683Set a read watchpoint for EXPRESSION.\n\
14684Usage: rwatch [-location] EXPRESSION\n\
14685\n\
14686Options:\n\
14687%OPTIONS%\n\
14688\n\
14689A read watchpoint stops execution of your program whenever the value of\n\
14690an expression is read."), opts);
14691 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14692 rwatch_help.c_str ());
14693 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14694
14695 static const std::string awatch_help = gdb::option::build_help (_("\
14696Set an access watchpoint for EXPRESSION.\n\
14697Usage: awatch [-location] EXPRESSION\n\
14698\n\
14699Options:\n\
14700%OPTIONS%\n\
14701\n\
14702An access watchpoint stops execution of your program whenever the value\n\
14703of an expression is either read or written."), opts);
14704 c = add_com ("awatch", class_breakpoint, awatch_command,
14705 awatch_help.c_str ());
14706 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 14707
11db9430 14708 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 14709Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14710
920d2a44
AC
14711 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14712 respond to changes - contrary to the description. */
85c07804
AC
14713 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14714 &can_use_hw_watchpoints, _("\
14715Set debugger's willingness to use watchpoint hardware."), _("\
14716Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14717If zero, gdb will not use hardware for new watchpoints, even if\n\
14718such is available. (However, any hardware watchpoints that were\n\
14719created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14720hardware.)"),
14721 NULL,
920d2a44 14722 show_can_use_hw_watchpoints,
85c07804 14723 &setlist, &showlist);
c906108c
SS
14724
14725 can_use_hw_watchpoints = 1;
fa8d40ab 14726
1042e4c0
SS
14727 /* Tracepoint manipulation commands. */
14728
3947f654
SM
14729 cmd_list_element *trace_cmd
14730 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 14731Set a tracepoint at specified location.\n\
1042e4c0
SS
14732\n"
14733BREAK_ARGS_HELP ("trace") "\n\
14734Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 14735 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 14736
3947f654
SM
14737 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14738 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14739 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14740 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 14741
7a697b8d 14742 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 14743Set a fast tracepoint at specified location.\n\
7a697b8d
SS
14744\n"
14745BREAK_ARGS_HELP ("ftrace") "\n\
14746Do \"help tracepoints\" for info on other tracepoint commands."));
14747 set_cmd_completer (c, location_completer);
14748
0fb4aa4b 14749 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 14750Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
14751\n\
14752strace [LOCATION] [if CONDITION]\n\
629500fa
KS
14753LOCATION may be a linespec, explicit, or address location (described below) \n\
14754or -m MARKER_ID.\n\n\
14755If a marker id is specified, probe the marker with that name. With\n\
14756no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
14757Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14758This collects arbitrary user data passed in the probe point call to the\n\
14759tracing library. You can inspect it when analyzing the trace buffer,\n\
14760by printing the $_sdata variable like any other convenience variable.\n\
14761\n\
14762CONDITION is a boolean expression.\n\
264f9890 14763\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14764Multiple tracepoints at one place are permitted, and useful if their\n\
14765conditions are different.\n\
0fb4aa4b
PA
14766\n\
14767Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14768Do \"help tracepoints\" for info on other tracepoint commands."));
14769 set_cmd_completer (c, location_completer);
14770
e0f25bd9
SM
14771 cmd_list_element *info_tracepoints_cmd
14772 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 14773Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14774Convenience variable \"$tpnum\" contains the number of the\n\
14775last tracepoint set."));
14776
e0f25bd9 14777 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 14778
5e84b7ee
SM
14779 cmd_list_element *delete_tracepoints_cmd
14780 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
14781Delete specified tracepoints.\n\
14782Arguments are tracepoint numbers, separated by spaces.\n\
14783No argument means delete all tracepoints."),
14784 &deletelist);
5e84b7ee 14785 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
14786
14787 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14788Disable specified tracepoints.\n\
14789Arguments are tracepoint numbers, separated by spaces.\n\
14790No argument means disable all tracepoints."),
14791 &disablelist);
14792 deprecate_cmd (c, "disable");
14793
14794 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14795Enable specified tracepoints.\n\
14796Arguments are tracepoint numbers, separated by spaces.\n\
14797No argument means enable all tracepoints."),
14798 &enablelist);
14799 deprecate_cmd (c, "enable");
14800
14801 add_com ("passcount", class_trace, trace_pass_command, _("\
14802Set the passcount for a tracepoint.\n\
14803The trace will end when the tracepoint has been passed 'count' times.\n\
14804Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14805if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14806
0743fc83
TT
14807 add_basic_prefix_cmd ("save", class_breakpoint,
14808 _("Save breakpoint definitions as a script."),
2f822da5 14809 &save_cmdlist,
0743fc83 14810 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
14811
14812 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14813Save current breakpoint definitions as a script.\n\
cce7e648 14814This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14815catchpoints, tracepoints). Use the 'source' command in another debug\n\
14816session to restore them."),
14817 &save_cmdlist);
14818 set_cmd_completer (c, filename_completer);
14819
3947f654
SM
14820 cmd_list_element *save_tracepoints_cmd
14821 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14822Save current tracepoint definitions as a script.\n\
6149aea9
PA
14823Use the 'source' command in another debug session to restore them."),
14824 &save_cmdlist);
3947f654 14825 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 14826
3947f654 14827 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
14828 deprecate_cmd (c, "save tracepoints");
14829
f54bdb6d
SM
14830 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14831 _("\
590042fc 14832Breakpoint specific settings.\n\
fa8d40ab 14833Configure various breakpoint-specific variables such as\n\
590042fc 14834pending breakpoint behavior."),
f54bdb6d 14835 _("\
590042fc 14836Breakpoint specific settings.\n\
fa8d40ab 14837Configure various breakpoint-specific variables such as\n\
590042fc 14838pending breakpoint behavior."),
f54bdb6d
SM
14839 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14840 &setlist, &showlist);
fa8d40ab 14841
7915a72c
AC
14842 add_setshow_auto_boolean_cmd ("pending", no_class,
14843 &pending_break_support, _("\
14844Set debugger's behavior regarding pending breakpoints."), _("\
14845Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14846If on, an unrecognized breakpoint location will cause gdb to create a\n\
14847pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14848an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14849user-query to see if a pending breakpoint should be created."),
2c5b56ce 14850 NULL,
920d2a44 14851 show_pending_break_support,
6e1d7d6c
AC
14852 &breakpoint_set_cmdlist,
14853 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14854
14855 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14856
14857 add_setshow_boolean_cmd ("auto-hw", no_class,
14858 &automatic_hardware_breakpoints, _("\
14859Set automatic usage of hardware breakpoints."), _("\
14860Show automatic usage of hardware breakpoints."), _("\
14861If set, the debugger will automatically use hardware breakpoints for\n\
14862breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14863a warning will be emitted for such breakpoints."),
14864 NULL,
14865 show_automatic_hardware_breakpoints,
14866 &breakpoint_set_cmdlist,
14867 &breakpoint_show_cmdlist);
74960c60 14868
a25a5a45
PA
14869 add_setshow_boolean_cmd ("always-inserted", class_support,
14870 &always_inserted_mode, _("\
74960c60
VP
14871Set mode for inserting breakpoints."), _("\
14872Show mode for inserting breakpoints."), _("\
a25a5a45
PA
14873When this mode is on, breakpoints are inserted immediately as soon as\n\
14874they're created, kept inserted even when execution stops, and removed\n\
14875only when the user deletes them. When this mode is off (the default),\n\
14876breakpoints are inserted only when execution continues, and removed\n\
14877when execution stops."),
72d0e2c5
YQ
14878 NULL,
14879 &show_always_inserted_mode,
14880 &breakpoint_set_cmdlist,
14881 &breakpoint_show_cmdlist);
f1310107 14882
b775012e
LM
14883 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14884 condition_evaluation_enums,
14885 &condition_evaluation_mode_1, _("\
14886Set mode of breakpoint condition evaluation."), _("\
14887Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 14888When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
14889evaluated on the host's side by GDB. When it is set to \"target\",\n\
14890breakpoint conditions will be downloaded to the target (if the target\n\
14891supports such feature) and conditions will be evaluated on the target's side.\n\
14892If this is set to \"auto\" (default), this will be automatically set to\n\
14893\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 14894be set to \"host\"."),
b775012e
LM
14895 &set_condition_evaluation_mode,
14896 &show_condition_evaluation_mode,
14897 &breakpoint_set_cmdlist,
14898 &breakpoint_show_cmdlist);
14899
f1310107
TJB
14900 add_com ("break-range", class_breakpoint, break_range_command, _("\
14901Set a breakpoint for an address range.\n\
14902break-range START-LOCATION, END-LOCATION\n\
14903where START-LOCATION and END-LOCATION can be one of the following:\n\
14904 LINENUM, for that line in the current file,\n\
14905 FILE:LINENUM, for that line in that file,\n\
14906 +OFFSET, for that number of lines after the current line\n\
dda83cd7 14907 or the start of the range\n\
f1310107
TJB
14908 FUNCTION, for the first line in that function,\n\
14909 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14910 *ADDRESS, for the instruction at that address.\n\
14911\n\
14912The breakpoint will stop execution of the inferior whenever it executes\n\
14913an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14914range (including START-LOCATION and END-LOCATION)."));
14915
e7e0cddf 14916 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 14917Set a dynamic printf at specified location.\n\
e7e0cddf 14918dprintf location,format string,arg1,arg2,...\n\
629500fa 14919location may be a linespec, explicit, or address location.\n"
264f9890 14920"\n" LOCATION_SPEC_HELP_STRING));
e7e0cddf
SS
14921 set_cmd_completer (c, location_completer);
14922
14923 add_setshow_enum_cmd ("dprintf-style", class_support,
14924 dprintf_style_enums, &dprintf_style, _("\
14925Set the style of usage for dynamic printf."), _("\
14926Show the style of usage for dynamic printf."), _("\
14927This setting chooses how GDB will do a dynamic printf.\n\
14928If the value is \"gdb\", then the printing is done by GDB to its own\n\
14929console, as with the \"printf\" command.\n\
14930If the value is \"call\", the print is done by calling a function in your\n\
14931program; by default printf(), but you can choose a different function or\n\
14932output stream by setting dprintf-function and dprintf-channel."),
14933 update_dprintf_commands, NULL,
14934 &setlist, &showlist);
14935
e7e0cddf
SS
14936 add_setshow_string_cmd ("dprintf-function", class_support,
14937 &dprintf_function, _("\
590042fc
PW
14938Set the function to use for dynamic printf."), _("\
14939Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
14940 update_dprintf_commands, NULL,
14941 &setlist, &showlist);
14942
e7e0cddf
SS
14943 add_setshow_string_cmd ("dprintf-channel", class_support,
14944 &dprintf_channel, _("\
590042fc
PW
14945Set the channel to use for dynamic printf."), _("\
14946Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
14947 update_dprintf_commands, NULL,
14948 &setlist, &showlist);
14949
d3ce09f5
SS
14950 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14951 &disconnected_dprintf, _("\
14952Set whether dprintf continues after GDB disconnects."), _("\
14953Show whether dprintf continues after GDB disconnects."), _("\
14954Use this to let dprintf commands continue to hit and produce output\n\
14955even if GDB disconnects or detaches from the target."),
14956 NULL,
14957 NULL,
14958 &setlist, &showlist);
14959
14960 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
14961Target agent only formatted printing, like the C \"printf\" function.\n\
14962Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14963This supports most C printf format specifications, like %s, %d, etc.\n\
14964This is useful for formatted output in user-defined commands."));
d3ce09f5 14965
491144b5 14966 automatic_hardware_breakpoints = true;
f3b1572e 14967
c90e7d63
SM
14968 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14969 "breakpoint");
14970 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14971 "breakpoint");
c906108c 14972}