]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
gdb: move struct ui and related things to ui.{c,h}
[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"
13d03262 56#include "ui.h"
4de283e4
TT
57#include "valprint.h"
58#include "jit.h"
65d79d4b 59#include "parser-defs.h"
d322d6d6 60#include "gdbsupport/gdb_regex.h"
55aa24fb 61#include "probe.h"
4de283e4 62#include "cli/cli-utils.h"
d55e5aa6 63#include "stack.h"
4de283e4
TT
64#include "ax-gdb.h"
65#include "dummy-frame.h"
66#include "interps.h"
268a13a5 67#include "gdbsupport/format.h"
cfc31633 68#include "thread-fsm.h"
5d5658a1 69#include "tid-parse.h"
4de283e4 70#include "cli/cli-style.h"
0f8e2034 71#include "cli/cli-decode.h"
d3ce09f5 72
1042e4c0 73/* readline include files */
073bbbb0 74#include "readline/tilde.h"
1042e4c0
SS
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
6dddc817 80#include "extension.h"
325fac50 81#include <algorithm>
5ed8105e 82#include "progspace-and-thread.h"
268a13a5
TT
83#include "gdbsupport/array-view.h"
84#include "gdbsupport/gdb_optional.h"
104c1213 85
4a64f543 86/* Prototypes for local functions. */
c906108c 87
896b6bda 88static void map_breakpoint_numbers (const char *,
48649e1b 89 gdb::function_view<void (breakpoint *)>);
c906108c 90
f00aae0f 91static void
264f9890
PA
92 create_sals_from_location_spec_default (location_spec *locspec,
93 linespec_result *canonical);
983af33b 94
eee031e2
TT
95static void create_breakpoints_sal (struct gdbarch *,
96 struct linespec_result *,
97 gdb::unique_xmalloc_ptr<char>,
98 gdb::unique_xmalloc_ptr<char>,
99 enum bptype,
100 enum bpdisp, int, int,
101 int,
eee031e2 102 int, int, int, unsigned);
983af33b 103
a6535de1
TT
104static int can_use_hardware_watchpoint
105 (const std::vector<value_ref_ptr> &vals);
c906108c 106
b713485d 107static void mention (const breakpoint *);
c906108c 108
7a3e3265 109static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
c906108c 110
752a2291
PA
111static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
112
06edf0c0
PA
113static struct breakpoint *
114 momentary_breakpoint_from_master (struct breakpoint *orig,
115 enum bptype type,
7ab97995 116 int loc_enabled, int thread);
06edf0c0 117
d8de7963 118static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
76897487 119
a6d9a66e
UW
120static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
121 CORE_ADDR bpaddr,
60270718
AB
122 enum bptype bptype,
123 struct program_space *pspace);
76897487 124
d8de7963
AB
125static bool watchpoint_locations_match (const struct bp_location *loc1,
126 const struct bp_location *loc2);
85d721b8 127
d8de7963
AB
128static bool breakpoint_locations_match (const struct bp_location *loc1,
129 const struct bp_location *loc2,
130 bool sw_hw_bps_match = false);
7f32a4d5 131
d8de7963
AB
132static bool breakpoint_location_address_match (struct bp_location *bl,
133 const struct address_space *aspace,
134 CORE_ADDR addr);
f1310107 135
d8de7963
AB
136static bool breakpoint_location_address_range_overlap (struct bp_location *,
137 const address_space *,
138 CORE_ADDR, int);
d35ae833 139
834c0d03 140static int remove_breakpoint (struct bp_location *);
b2b6a7da 141static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 142
313f3b21 143static enum print_stop_action print_bp_stop_message (bpstat *bs);
c906108c 144
a14ed312 145static int hw_breakpoint_used_count (void);
c906108c 146
a1398e0c
PA
147static int hw_watchpoint_use_count (struct breakpoint *);
148
149static int hw_watchpoint_used_count_others (struct breakpoint *except,
150 enum bptype type,
151 int *other_type_used);
c906108c 152
816338b5
SS
153static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
154 int count);
c906108c 155
f431efe5 156static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 157
264f9890 158static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
5d926615 159 (struct breakpoint *b,
264f9890 160 location_spec *locspec,
5d926615
TT
161 struct program_space *search_pspace);
162
6e14e441
PA
163static bool bl_address_is_meaningful (bp_location *loc);
164
d8a77e4c
AB
165static int find_loc_num_by_location (const bp_location *loc);
166
44702360
PA
167/* update_global_location_list's modes of operation wrt to whether to
168 insert locations now. */
169enum ugll_insert_mode
170{
171 /* Don't insert any breakpoint locations into the inferior, only
172 remove already-inserted locations that no longer should be
173 inserted. Functions that delete a breakpoint or breakpoints
174 should specify this mode, so that deleting a breakpoint doesn't
175 have the side effect of inserting the locations of other
176 breakpoints that are marked not-inserted, but should_be_inserted
177 returns true on them.
178
179 This behavior is useful is situations close to tear-down -- e.g.,
180 after an exec, while the target still has execution, but
181 breakpoint shadows of the previous executable image should *NOT*
182 be restored to the new image; or before detaching, where the
183 target still has execution and wants to delete breakpoints from
184 GDB's lists, and all breakpoints had already been removed from
185 the inferior. */
186 UGLL_DONT_INSERT,
187
a25a5a45
PA
188 /* May insert breakpoints iff breakpoints_should_be_inserted_now
189 claims breakpoints should be inserted now. */
04086b45
PA
190 UGLL_MAY_INSERT,
191
a25a5a45
PA
192 /* Insert locations now, irrespective of
193 breakpoints_should_be_inserted_now. E.g., say all threads are
194 stopped right now, and the user did "continue". We need to
195 insert breakpoints _before_ resuming the target, but
196 UGLL_MAY_INSERT wouldn't insert them, because
197 breakpoints_should_be_inserted_now returns false at that point,
198 as no thread is running yet. */
04086b45 199 UGLL_INSERT
44702360
PA
200};
201
202static void update_global_location_list (enum ugll_insert_mode);
a5606eee 203
44702360 204static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 205
74960c60 206static void insert_breakpoint_locations (void);
a5606eee 207
0b39b52e 208static void trace_pass_command (const char *, int);
1042e4c0 209
558a9d82
YQ
210static void set_tracepoint_count (int num);
211
f2478a7e 212static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 213
d8de7963
AB
214/* Return true if B refers to a static tracepoint set by marker ("-m"),
215 zero otherwise. */
983af33b 216
d8de7963 217static bool strace_marker_p (struct breakpoint *b);
0fb4aa4b 218
264f9890
PA
219static void bkpt_probe_create_sals_from_location_spec
220 (location_spec *locspec,
03ada39e 221 struct linespec_result *canonical);
264f9890
PA
222static void tracepoint_probe_create_sals_from_location_spec
223 (location_spec *locspec,
03ada39e 224 struct linespec_result *canonical);
8613a476 225
74421c0b 226const struct breakpoint_ops code_breakpoint_ops =
8613a476 227{
264f9890 228 create_sals_from_location_spec_default,
eee031e2 229 create_breakpoints_sal,
8613a476
TT
230};
231
55aa24fb 232/* Breakpoints set on probes. */
a678887d 233static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
8613a476 234{
264f9890 235 bkpt_probe_create_sals_from_location_spec,
eee031e2 236 create_breakpoints_sal,
8613a476 237};
55aa24fb 238
bac7c5cf 239/* Tracepoints set on probes. */
a678887d 240static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
8613a476 241{
264f9890 242 tracepoint_probe_create_sals_from_location_spec,
eee031e2 243 create_breakpoints_sal,
8613a476 244};
bac7c5cf 245
1c2cbcf1
PA
246/* Implementation of abstract dtors. These must exist to satisfy the
247 linker. */
248
249breakpoint::~breakpoint ()
250{
251}
252
74421c0b 253code_breakpoint::~code_breakpoint ()
1c2cbcf1
PA
254{
255}
256
257catchpoint::~catchpoint ()
258{
259}
260
098f1272 261/* The structure to be used in regular breakpoints. */
74421c0b 262struct ordinary_breakpoint : public code_breakpoint
098f1272 263{
74421c0b 264 using code_breakpoint::code_breakpoint;
73063f51 265
7dd8e7ae 266 int resources_needed (const struct bp_location *) override;
7bd86313 267 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 268 void print_mention () const override;
4d1ae558 269 void print_recreate (struct ui_file *fp) const override;
098f1272
TT
270};
271
9a71ed14
PA
272/* Internal breakpoints. These typically have a lifetime the same as
273 the program, and they end up installed on the breakpoint chain with
274 a negative breakpoint number. They're visible in "maint info
275 breakpoints", but not "info breakpoints". */
74421c0b 276struct internal_breakpoint : public code_breakpoint
098f1272 277{
752a2291
PA
278 internal_breakpoint (struct gdbarch *gdbarch,
279 enum bptype type, CORE_ADDR address)
74421c0b 280 : code_breakpoint (gdbarch, type)
752a2291
PA
281 {
282 symtab_and_line sal;
283 sal.pc = address;
284 sal.section = find_pc_overlay (sal.pc);
285 sal.pspace = current_program_space;
286 add_location (sal);
287
288 pspace = current_program_space;
289 disposition = disp_donttouch;
290 }
73063f51 291
c359fff5
TT
292 void re_set () override;
293 void check_status (struct bpstat *bs) override;
7bd86313 294 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 295 void print_mention () const override;
098f1272
TT
296};
297
9a71ed14
PA
298/* Momentary breakpoints. These typically have a lifetime of some run
299 control command only, are always thread-specific, and have 0 for
300 breakpoint number. I.e., there can be many momentary breakpoints
301 on the breakpoint chain and they all same the same number (zero).
302 They're visible in "maint info breakpoints", but not "info
303 breakpoints". */
74421c0b 304struct momentary_breakpoint : public code_breakpoint
098f1272 305{
7ab97995
PA
306 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
307 program_space *pspace_,
308 const struct frame_id &frame_id_,
309 int thread_)
74421c0b 310 : code_breakpoint (gdbarch_, bptype)
7ab97995
PA
311 {
312 /* If FRAME_ID is valid, it should be a real frame, not an inlined
313 or tail-called one. */
314 gdb_assert (!frame_id_artificial_p (frame_id));
315
316 /* Momentary breakpoints are always thread-specific. */
317 gdb_assert (thread_ > 0);
318
319 pspace = pspace_;
320 enable_state = bp_enabled;
321 disposition = disp_donttouch;
322 frame_id = frame_id_;
323 thread = thread_;
324 }
73063f51 325
1fd30a47
TT
326 void re_set () override;
327 void check_status (struct bpstat *bs) override;
7bd86313 328 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 329 void print_mention () const override;
098f1272
TT
330};
331
332/* DPrintf breakpoints. */
5ad71d67 333struct dprintf_breakpoint : public ordinary_breakpoint
098f1272 334{
73063f51
TT
335 using ordinary_breakpoint::ordinary_breakpoint;
336
f293a0b5
TT
337 void re_set () override;
338 int breakpoint_hit (const struct bp_location *bl,
339 const address_space *aspace,
340 CORE_ADDR bp_addr,
341 const target_waitstatus &ws) override;
4d1ae558 342 void print_recreate (struct ui_file *fp) const override;
f293a0b5 343 void after_condition_true (struct bpstat *bs) override;
098f1272
TT
344};
345
ec45bb67
TT
346/* Ranged breakpoints. */
347struct ranged_breakpoint : public ordinary_breakpoint
348{
b925bf21
PA
349 explicit ranged_breakpoint (struct gdbarch *gdbarch,
350 const symtab_and_line &sal_start,
351 int length,
264f9890
PA
352 location_spec_up start_locspec,
353 location_spec_up end_locspec)
73063f51
TT
354 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
355 {
b925bf21
PA
356 bp_location *bl = add_location (sal_start);
357 bl->length = length;
358
359 disposition = disp_donttouch;
360
264f9890
PA
361 locspec = std::move (start_locspec);
362 locspec_range_end = std::move (end_locspec);
73063f51
TT
363 }
364
ec45bb67
TT
365 int breakpoint_hit (const struct bp_location *bl,
366 const address_space *aspace,
367 CORE_ADDR bp_addr,
368 const target_waitstatus &ws) override;
369 int resources_needed (const struct bp_location *) override;
7bd86313 370 enum print_stop_action print_it (const bpstat *bs) const override;
a67bcaba 371 bool print_one (bp_location **) const override;
ec45bb67 372 void print_one_detail (struct ui_out *) const override;
b713485d 373 void print_mention () const override;
4d1ae558 374 void print_recreate (struct ui_file *fp) const override;
ec45bb67
TT
375};
376
6d7a8c56
TT
377/* Static tracepoints with marker (`-m'). */
378struct static_marker_tracepoint : public tracepoint
379{
73063f51
TT
380 using tracepoint::tracepoint;
381
264f9890
PA
382 std::vector<symtab_and_line> decode_location_spec
383 (struct location_spec *locspec,
6d7a8c56
TT
384 struct program_space *search_pspace) override;
385};
386
d3ce09f5
SS
387/* The style in which to perform a dynamic printf. This is a user
388 option because different output options have different tradeoffs;
389 if GDB does the printing, there is better error handling if there
390 is a problem with any of the arguments, but using an inferior
391 function lets you have special-purpose printers and sending of
392 output to the same place as compiled-in print functions. */
393
394static const char dprintf_style_gdb[] = "gdb";
395static const char dprintf_style_call[] = "call";
396static const char dprintf_style_agent[] = "agent";
397static const char *const dprintf_style_enums[] = {
398 dprintf_style_gdb,
399 dprintf_style_call,
400 dprintf_style_agent,
401 NULL
402};
403static const char *dprintf_style = dprintf_style_gdb;
404
405/* The function to use for dynamic printf if the preferred style is to
406 call into the inferior. The value is simply a string that is
407 copied into the command, so it can be anything that GDB can
408 evaluate to a callable address, not necessarily a function name. */
409
e0700ba4 410static std::string dprintf_function = "printf";
d3ce09f5
SS
411
412/* The channel to use for dynamic printf if the preferred style is to
413 call into the inferior; if a nonempty string, it will be passed to
414 the call as the first argument, with the format string as the
415 second. As with the dprintf function, this can be anything that
416 GDB knows how to evaluate, so in addition to common choices like
417 "stderr", this could be an app-specific expression like
418 "mystreams[curlogger]". */
419
e0700ba4 420static std::string dprintf_channel;
d3ce09f5
SS
421
422/* True if dprintf commands should continue to operate even if GDB
423 has disconnected. */
491144b5 424static bool disconnected_dprintf = true;
d3ce09f5 425
5cea2a26
PA
426struct command_line *
427breakpoint_commands (struct breakpoint *b)
428{
d1b0a7bf 429 return b->commands ? b->commands.get () : NULL;
5cea2a26 430}
3daf8fe5 431
f3b1572e
PA
432/* Flag indicating that a command has proceeded the inferior past the
433 current breakpoint. */
434
491144b5 435static bool breakpoint_proceeded;
f3b1572e 436
956a9fb9 437const char *
2cec12e5
AR
438bpdisp_text (enum bpdisp disp)
439{
4a64f543
MS
440 /* NOTE: the following values are a part of MI protocol and
441 represent values of 'disp' field returned when inferior stops at
442 a breakpoint. */
bc043ef3 443 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 444
2cec12e5
AR
445 return bpdisps[(int) disp];
446}
c906108c 447
4a64f543 448/* Prototypes for exported functions. */
c906108c 449/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 450 if such is available. */
c906108c
SS
451static int can_use_hw_watchpoints;
452
920d2a44
AC
453static void
454show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
455 struct cmd_list_element *c,
456 const char *value)
457{
6cb06a8c
TT
458 gdb_printf (file,
459 _("Debugger's willingness to use "
460 "watchpoint hardware is %s.\n"),
461 value);
920d2a44
AC
462}
463
fa8d40ab
JJ
464/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
465 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 466 for unrecognized breakpoint locations.
fa8d40ab
JJ
467 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
468static enum auto_boolean pending_break_support;
920d2a44
AC
469static void
470show_pending_break_support (struct ui_file *file, int from_tty,
471 struct cmd_list_element *c,
472 const char *value)
473{
6cb06a8c
TT
474 gdb_printf (file,
475 _("Debugger's behavior regarding "
476 "pending breakpoints is %s.\n"),
477 value);
920d2a44 478}
fa8d40ab 479
491144b5 480/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 481 set with "break" but falling in read-only memory.
491144b5 482 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 483 use hardware breakpoints. */
491144b5 484static bool automatic_hardware_breakpoints;
765dc015
VP
485static void
486show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
487 struct cmd_list_element *c,
488 const char *value)
489{
6cb06a8c
TT
490 gdb_printf (file,
491 _("Automatic usage of hardware breakpoints is %s.\n"),
492 value);
765dc015
VP
493}
494
a25a5a45
PA
495/* If on, GDB keeps breakpoints inserted even if the inferior is
496 stopped, and immediately inserts any new breakpoints as soon as
497 they're created. If off (default), GDB keeps breakpoints off of
498 the target as long as possible. That is, it delays inserting
499 breakpoints until the next resume, and removes them again when the
500 target fully stops. This is a bit safer in case GDB crashes while
501 processing user input. */
491144b5 502static bool always_inserted_mode = false;
72d0e2c5 503
33e5cbd6 504static void
74960c60 505show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 506 struct cmd_list_element *c, const char *value)
74960c60 507{
6cb06a8c
TT
508 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
509 value);
74960c60
VP
510}
511
b57bacec
PA
512/* See breakpoint.h. */
513
33e5cbd6 514int
a25a5a45 515breakpoints_should_be_inserted_now (void)
33e5cbd6 516{
a25a5a45
PA
517 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
518 {
519 /* If breakpoints are global, they should be inserted even if no
520 thread under gdb's control is running, or even if there are
521 no threads under GDB's control yet. */
522 return 1;
523 }
5b6d1e4f 524 else
a25a5a45 525 {
a25a5a45
PA
526 if (always_inserted_mode)
527 {
528 /* The user wants breakpoints inserted even if all threads
529 are stopped. */
530 return 1;
531 }
532
5b6d1e4f
PA
533 for (inferior *inf : all_inferiors ())
534 if (inf->has_execution ()
535 && threads_are_executing (inf->process_target ()))
536 return 1;
372316f1
PA
537
538 /* Don't remove breakpoints yet if, even though all threads are
539 stopped, we still have events to process. */
08036331 540 for (thread_info *tp : all_non_exited_threads ())
1edb66d8 541 if (tp->resumed () && tp->has_pending_waitstatus ())
372316f1 542 return 1;
a25a5a45
PA
543 }
544 return 0;
33e5cbd6 545}
765dc015 546
b775012e
LM
547static const char condition_evaluation_both[] = "host or target";
548
549/* Modes for breakpoint condition evaluation. */
550static const char condition_evaluation_auto[] = "auto";
551static const char condition_evaluation_host[] = "host";
552static const char condition_evaluation_target[] = "target";
553static const char *const condition_evaluation_enums[] = {
554 condition_evaluation_auto,
555 condition_evaluation_host,
556 condition_evaluation_target,
557 NULL
558};
559
560/* Global that holds the current mode for breakpoint condition evaluation. */
561static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
562
563/* Global that we use to display information to the user (gets its value from
564 condition_evaluation_mode_1. */
565static const char *condition_evaluation_mode = condition_evaluation_auto;
566
567/* Translate a condition evaluation mode MODE into either "host"
568 or "target". This is used mostly to translate from "auto" to the
569 real setting that is being used. It returns the translated
570 evaluation mode. */
571
572static const char *
573translate_condition_evaluation_mode (const char *mode)
574{
575 if (mode == condition_evaluation_auto)
576 {
577 if (target_supports_evaluation_of_breakpoint_conditions ())
578 return condition_evaluation_target;
579 else
580 return condition_evaluation_host;
581 }
582 else
583 return mode;
584}
585
586/* Discovers what condition_evaluation_auto translates to. */
587
588static const char *
589breakpoint_condition_evaluation_mode (void)
590{
591 return translate_condition_evaluation_mode (condition_evaluation_mode);
592}
593
594/* Return true if GDB should evaluate breakpoint conditions or false
595 otherwise. */
596
d8de7963 597static bool
b775012e
LM
598gdb_evaluates_breakpoint_condition_p (void)
599{
600 const char *mode = breakpoint_condition_evaluation_mode ();
601
602 return (mode == condition_evaluation_host);
603}
604
c906108c
SS
605/* Are we executing breakpoint commands? */
606static int executing_breakpoint_commands;
607
c02f5703
MS
608/* Are overlay event breakpoints enabled? */
609static int overlay_events_enabled;
610
e09342b5 611/* See description in breakpoint.h. */
491144b5 612bool target_exact_watchpoints = false;
e09342b5 613
c906108c 614/* Walk the following statement or block through all breakpoints.
e5dd4106 615 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 616 current breakpoint. */
c906108c 617
5c44784c
JM
618#define ALL_BREAKPOINTS_SAFE(B,TMP) \
619 for (B = breakpoint_chain; \
620 B ? (TMP=B->next, 1): 0; \
621 B = TMP)
c906108c 622
7cc221ef 623/* Chains of all breakpoints defined. */
c906108c 624
81e6b8eb 625static struct breakpoint *breakpoint_chain;
c906108c 626
240edef6 627/* See breakpoint.h. */
43892fdf 628
240edef6 629breakpoint_range
43892fdf
SM
630all_breakpoints ()
631{
632 return breakpoint_range (breakpoint_chain);
633}
634
240edef6 635/* See breakpoint.h. */
1428b37a 636
240edef6 637breakpoint_safe_range
1428b37a
SM
638all_breakpoints_safe ()
639{
640 return breakpoint_safe_range (all_breakpoints ());
641}
642
f6d17b2b
SM
643/* See breakpoint.h. */
644
645tracepoint_range
646all_tracepoints ()
647{
648 return tracepoint_range (breakpoint_chain);
649}
650
39ef2f62 651/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 652
5d51cd5d 653static std::vector<bp_location *> bp_locations;
876fa593 654
055c879f
SM
655/* See breakpoint.h. */
656
657const std::vector<bp_location *> &
48d7020b
SM
658all_bp_locations ()
659{
660 return bp_locations;
661}
662
e0d9a270
SM
663/* Range to iterate over breakpoint locations at a given address. */
664
665struct bp_locations_at_addr_range
666{
667 using iterator = std::vector<bp_location *>::iterator;
668
669 bp_locations_at_addr_range (CORE_ADDR addr)
670 {
671 struct compare
672 {
673 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
674 { return loc->address < addr_; }
675
676 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
677 { return addr_ < loc->address; }
678 };
679
680 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
681 addr, compare ());
682
683 m_begin = it_pair.first;
684 m_end = it_pair.second;
685 }
686
687 iterator begin () const
688 { return m_begin; }
689
690 iterator end () const
691 { return m_end; }
692
693private:
694 iterator m_begin;
695 iterator m_end;
696};
697
698/* Return a range to iterate over all breakpoint locations exactly at address
699 ADDR.
700
701 If it's needed to iterate multiple times on the same range, it's possible
702 to save the range in a local variable and use it multiple times:
703
704 auto range = all_bp_locations_at_addr (addr);
705
706 for (bp_location *loc : range)
707 // use loc
708
709 for (bp_location *loc : range)
710 // use loc
711
712 This saves a bit of time, as it avoids re-doing the binary searches to find
713 the range's boundaries. Just remember not to change the bp_locations vector
714 in the mean time, as it could make the range's iterators stale. */
715
716static bp_locations_at_addr_range
717all_bp_locations_at_addr (CORE_ADDR addr)
718{
719 return bp_locations_at_addr_range (addr);
720}
721
4a64f543 722/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 723 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 724 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 725 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 726 an address you need to read. */
876fa593 727
f5336ca5 728static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 729
4a64f543
MS
730/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
731 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
732 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
733 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 734 scan for shadow bytes for an address you need to read. */
876fa593 735
f5336ca5 736static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 737
4a64f543 738/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
739 from the bp_locations array, but for which a hit may still be
740 reported by a target. */
1123588c 741static std::vector<bp_location *> moribund_locations;
20874c92 742
c906108c
SS
743/* Number of last breakpoint made. */
744
95a42b64
TT
745static int breakpoint_count;
746
86b17b60
PA
747/* The value of `breakpoint_count' before the last command that
748 created breakpoints. If the last (break-like) command created more
749 than one breakpoint, then the difference between BREAKPOINT_COUNT
750 and PREV_BREAKPOINT_COUNT is more than one. */
751static int prev_breakpoint_count;
c906108c 752
1042e4c0
SS
753/* Number of last tracepoint made. */
754
95a42b64 755static int tracepoint_count;
1042e4c0 756
6149aea9
PA
757static struct cmd_list_element *breakpoint_set_cmdlist;
758static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 759struct cmd_list_element *save_cmdlist;
6149aea9 760
468d015d 761/* Return whether a breakpoint is an active enabled breakpoint. */
d8de7963 762static bool
468d015d
JJ
763breakpoint_enabled (struct breakpoint *b)
764{
0d381245 765 return (b->enable_state == bp_enabled);
468d015d
JJ
766}
767
c906108c
SS
768/* Set breakpoint count to NUM. */
769
95a42b64 770static void
fba45db2 771set_breakpoint_count (int num)
c906108c 772{
86b17b60 773 prev_breakpoint_count = breakpoint_count;
c906108c 774 breakpoint_count = num;
4fa62494 775 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
776}
777
86b17b60
PA
778/* Used by `start_rbreak_breakpoints' below, to record the current
779 breakpoint count before "rbreak" creates any breakpoint. */
780static int rbreak_start_breakpoint_count;
781
95a42b64
TT
782/* Called at the start an "rbreak" command to record the first
783 breakpoint made. */
86b17b60 784
c80049d3 785scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 786{
86b17b60 787 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
788}
789
790/* Called at the end of an "rbreak" command to record the last
791 breakpoint made. */
86b17b60 792
c80049d3 793scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 794{
86b17b60 795 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
796}
797
4a64f543 798/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
799
800void
fba45db2 801clear_breakpoint_hit_counts (void)
c906108c 802{
43892fdf 803 for (breakpoint *b : all_breakpoints ())
c906108c
SS
804 b->hit_count = 0;
805}
806
c906108c 807\f
48cb2d85
VP
808/* Return the breakpoint with the specified number, or NULL
809 if the number does not refer to an existing breakpoint. */
810
811struct breakpoint *
812get_breakpoint (int num)
813{
43892fdf 814 for (breakpoint *b : all_breakpoints ())
48cb2d85
VP
815 if (b->number == num)
816 return b;
817
43892fdf 818 return nullptr;
48cb2d85 819}
5c44784c 820
78805ff8
PW
821/* Return TRUE if NUM refer to an existing breakpoint that has
822 multiple code locations. */
823
824static bool
825has_multiple_locations (int num)
826{
827 for (breakpoint *b : all_breakpoints ())
828 if (b->number == num)
829 return b->loc != nullptr && b->loc->next != nullptr;
830
831 return false;
832}
833
c906108c 834\f
adc36818 835
b775012e
LM
836/* Mark locations as "conditions have changed" in case the target supports
837 evaluating conditions on its side. */
838
839static void
840mark_breakpoint_modified (struct breakpoint *b)
841{
b775012e
LM
842 /* This is only meaningful if the target is
843 evaluating conditions and if the user has
844 opted for condition evaluation on the target's
845 side. */
846 if (gdb_evaluates_breakpoint_condition_p ()
847 || !target_supports_evaluation_of_breakpoint_conditions ())
848 return;
849
850 if (!is_breakpoint (b))
851 return;
852
40cb8ca5 853 for (bp_location *loc : b->locations ())
b775012e
LM
854 loc->condition_changed = condition_modified;
855}
856
857/* Mark location as "conditions have changed" in case the target supports
858 evaluating conditions on its side. */
859
860static void
861mark_breakpoint_location_modified (struct bp_location *loc)
862{
863 /* This is only meaningful if the target is
864 evaluating conditions and if the user has
865 opted for condition evaluation on the target's
866 side. */
867 if (gdb_evaluates_breakpoint_condition_p ()
868 || !target_supports_evaluation_of_breakpoint_conditions ())
869
870 return;
871
872 if (!is_breakpoint (loc->owner))
873 return;
874
875 loc->condition_changed = condition_modified;
876}
877
878/* Sets the condition-evaluation mode using the static global
879 condition_evaluation_mode. */
880
881static void
eb4c3f4a 882set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
883 struct cmd_list_element *c)
884{
b775012e
LM
885 const char *old_mode, *new_mode;
886
887 if ((condition_evaluation_mode_1 == condition_evaluation_target)
888 && !target_supports_evaluation_of_breakpoint_conditions ())
889 {
890 condition_evaluation_mode_1 = condition_evaluation_mode;
891 warning (_("Target does not support breakpoint condition evaluation.\n"
892 "Using host evaluation mode instead."));
893 return;
894 }
895
896 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
897 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
898
abf1152a
JK
899 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
900 settings was "auto". */
901 condition_evaluation_mode = condition_evaluation_mode_1;
902
b775012e
LM
903 /* Only update the mode if the user picked a different one. */
904 if (new_mode != old_mode)
905 {
b775012e
LM
906 /* If the user switched to a different evaluation mode, we
907 need to synch the changes with the target as follows:
908
909 "host" -> "target": Send all (valid) conditions to the target.
910 "target" -> "host": Remove all the conditions from the target.
911 */
912
b775012e
LM
913 if (new_mode == condition_evaluation_target)
914 {
915 /* Mark everything modified and synch conditions with the
916 target. */
48d7020b 917 for (bp_location *loc : all_bp_locations ())
b775012e 918 mark_breakpoint_location_modified (loc);
24b21115 919 }
b775012e
LM
920 else
921 {
922 /* Manually mark non-duplicate locations to synch conditions
923 with the target. We do this to remove all the conditions the
924 target knows about. */
48d7020b 925 for (bp_location *loc : all_bp_locations ())
b775012e
LM
926 if (is_breakpoint (loc->owner) && loc->inserted)
927 loc->needs_update = 1;
928 }
929
930 /* Do the update. */
44702360 931 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
932 }
933
934 return;
935}
936
937/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
938 what "auto" is translating to. */
939
940static void
941show_condition_evaluation_mode (struct ui_file *file, int from_tty,
942 struct cmd_list_element *c, const char *value)
943{
944 if (condition_evaluation_mode == condition_evaluation_auto)
6cb06a8c
TT
945 gdb_printf (file,
946 _("Breakpoint condition evaluation "
947 "mode is %s (currently %s).\n"),
948 value,
949 breakpoint_condition_evaluation_mode ());
b775012e 950 else
6cb06a8c
TT
951 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
952 value);
b775012e
LM
953}
954
b5fa468f
TBA
955/* Parse COND_STRING in the context of LOC and set as the condition
956 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
957 the number of LOC within its owner. In case of parsing error, mark
958 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
959
960static void
961set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
962 int bp_num, int loc_num)
963{
964 bool has_junk = false;
965 try
966 {
967 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
968 block_for_pc (loc->address), 0);
969 if (*cond_string != 0)
970 has_junk = true;
971 else
972 {
973 loc->cond = std::move (new_exp);
974 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
975 gdb_printf (_("Breakpoint %d's condition is now valid at "
976 "location %d, enabling.\n"),
977 bp_num, loc_num);
b5fa468f
TBA
978
979 loc->disabled_by_cond = false;
980 }
981 }
982 catch (const gdb_exception_error &e)
983 {
984 if (loc->enabled)
985 {
986 /* Warn if a user-enabled location is now becoming disabled-by-cond.
987 BP_NUM is 0 if the breakpoint is being defined for the first
988 time using the "break ... if ..." command, and non-zero if
989 already defined. */
990 if (bp_num != 0)
991 warning (_("failed to validate condition at location %d.%d, "
992 "disabling:\n %s"), bp_num, loc_num, e.what ());
993 else
994 warning (_("failed to validate condition at location %d, "
995 "disabling:\n %s"), loc_num, e.what ());
996 }
997
998 loc->disabled_by_cond = true;
999 }
1000
1001 if (has_junk)
1002 error (_("Garbage '%s' follows condition"), cond_string);
1003}
1004
adc36818 1005void
7a26bd4d 1006set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 1007 int from_tty, bool force)
adc36818 1008{
4c55e970 1009 if (*exp == 0)
3a5c3e22 1010 {
6f781ee3 1011 b->cond_string.reset ();
3a5c3e22 1012
4c55e970 1013 if (is_watchpoint (b))
98ed24fb 1014 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
1015 else
1016 {
b5fa468f 1017 int loc_num = 1;
40cb8ca5 1018 for (bp_location *loc : b->locations ())
4c55e970
TBA
1019 {
1020 loc->cond.reset ();
b5fa468f 1021 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
1022 gdb_printf (_("Breakpoint %d's condition is now valid at "
1023 "location %d, enabling.\n"),
1024 b->number, loc_num);
b5fa468f
TBA
1025 loc->disabled_by_cond = false;
1026 loc_num++;
4c55e970
TBA
1027
1028 /* No need to free the condition agent expression
1029 bytecode (if we have one). We will handle this
1030 when we go through update_global_location_list. */
1031 }
1032 }
1e620590 1033
adc36818 1034 if (from_tty)
6cb06a8c 1035 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
adc36818
PM
1036 }
1037 else
1038 {
adc36818
PM
1039 if (is_watchpoint (b))
1040 {
699bd4cf 1041 innermost_block_tracker tracker;
78319c15 1042 const char *arg = exp;
4c55e970 1043 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 1044 if (*arg != 0)
adc36818 1045 error (_("Junk at end of expression"));
98ed24fb 1046 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
4c55e970 1047 w->cond_exp = std::move (new_exp);
699bd4cf 1048 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
1049 }
1050 else
1051 {
4c55e970
TBA
1052 /* Parse and set condition expressions. We make two passes.
1053 In the first, we parse the condition string to see if it
b5fa468f
TBA
1054 is valid in at least one location. If so, the condition
1055 would be accepted. So we go ahead and set the locations'
1056 conditions. In case no valid case is found, we throw
4c55e970
TBA
1057 the error and the condition string will be rejected.
1058 This two-pass approach is taken to avoid setting the
1059 state of locations in case of a reject. */
40cb8ca5 1060 for (bp_location *loc : b->locations ())
4c55e970 1061 {
b5fa468f
TBA
1062 try
1063 {
1064 const char *arg = exp;
1065 parse_exp_1 (&arg, loc->address,
1066 block_for_pc (loc->address), 0);
1067 if (*arg != 0)
1068 error (_("Junk at end of expression"));
1069 break;
1070 }
1071 catch (const gdb_exception_error &e)
1072 {
1073 /* Condition string is invalid. If this happens to
733d554a
TBA
1074 be the last loc, abandon (if not forced) or continue
1075 (if forced). */
1076 if (loc->next == nullptr && !force)
b5fa468f
TBA
1077 throw;
1078 }
4c55e970
TBA
1079 }
1080
b5fa468f 1081 /* If we reach here, the condition is valid at some locations. */
dda83cd7 1082 int loc_num = 1;
40cb8ca5
SM
1083 for (bp_location *loc : b->locations ())
1084 {
1085 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1086 loc_num++;
1087 }
adc36818 1088 }
1e620590
TBA
1089
1090 /* We know that the new condition parsed successfully. The
1091 condition string of the breakpoint can be safely updated. */
6f781ee3 1092 b->cond_string = make_unique_xstrdup (exp);
1e620590 1093 b->condition_not_parsed = 0;
adc36818 1094 }
b775012e
LM
1095 mark_breakpoint_modified (b);
1096
76727919 1097 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
1098}
1099
79aabb73
TBA
1100/* See breakpoint.h. */
1101
1102void
1103set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1104 bool force)
1105{
43892fdf 1106 for (breakpoint *b : all_breakpoints ())
79aabb73
TBA
1107 if (b->number == bpnum)
1108 {
1109 /* Check if this breakpoint has a "stop" method implemented in an
1110 extension language. This method and conditions entered into GDB
1111 from the CLI are mutually exclusive. */
1112 const struct extension_language_defn *extlang
1113 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1114
1115 if (extlang != NULL)
1116 {
1117 error (_("Only one stop condition allowed. There is currently"
1118 " a %s stop condition defined for this breakpoint."),
1119 ext_lang_capitalized_name (extlang));
1120 }
1121 set_breakpoint_condition (b, exp, from_tty, force);
1122
1123 if (is_breakpoint (b))
1124 update_global_location_list (UGLL_MAY_INSERT);
1125
1126 return;
1127 }
1128
1129 error (_("No breakpoint number %d."), bpnum);
1130}
1131
b1d4d8d1
TBA
1132/* The options for the "condition" command. */
1133
1134struct condition_command_opts
1135{
1136 /* For "-force". */
1137 bool force_condition = false;
1138};
1139
1140static const gdb::option::option_def condition_command_option_defs[] = {
1141
1142 gdb::option::flag_option_def<condition_command_opts> {
1143 "force",
1144 [] (condition_command_opts *opts) { return &opts->force_condition; },
1145 N_("Set the condition even if it is invalid for all current locations."),
1146 },
1147
1148};
1149
1150/* Create an option_def_group for the "condition" options, with
1151 CC_OPTS as context. */
1152
1153static inline gdb::option::option_def_group
1154make_condition_command_options_def_group (condition_command_opts *cc_opts)
1155{
1156 return {{condition_command_option_defs}, cc_opts};
1157}
1158
d55637df
TT
1159/* Completion for the "condition" command. */
1160
eb3ff9a5 1161static void
6f937416 1162condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1163 completion_tracker &tracker,
b1d4d8d1 1164 const char *text, const char * /*word*/)
d55637df 1165{
b1d4d8d1
TBA
1166 bool has_no_arguments = (*text == '\0');
1167 condition_command_opts cc_opts;
1168 const auto group = make_condition_command_options_def_group (&cc_opts);
1169 if (gdb::option::complete_options
1170 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1171 return;
d55637df 1172
f1735a53 1173 text = skip_spaces (text);
b1d4d8d1 1174 const char *space = skip_to_space (text);
d55637df
TT
1175 if (*space == '\0')
1176 {
1177 int len;
d55637df
TT
1178
1179 if (text[0] == '$')
1180 {
b1d4d8d1 1181 tracker.advance_custom_word_point_by (1);
d55637df 1182 /* We don't support completion of history indices. */
eb3ff9a5
PA
1183 if (!isdigit (text[1]))
1184 complete_internalvar (tracker, &text[1]);
1185 return;
d55637df
TT
1186 }
1187
b1d4d8d1
TBA
1188 /* Suggest the "-force" flag if no arguments are given. If
1189 arguments were passed, they either already include the flag,
1190 or we are beyond the point of suggesting it because it's
1191 positionally the first argument. */
1192 if (has_no_arguments)
1193 gdb::option::complete_on_all_options (tracker, group);
1194
d55637df
TT
1195 /* We're completing the breakpoint number. */
1196 len = strlen (text);
1197
43892fdf 1198 for (breakpoint *b : all_breakpoints ())
58ce7251
SDJ
1199 {
1200 char number[50];
1201
1202 xsnprintf (number, sizeof (number), "%d", b->number);
1203
1204 if (strncmp (number, text, len) == 0)
b02f78f9 1205 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1206 }
d55637df 1207
eb3ff9a5 1208 return;
d55637df
TT
1209 }
1210
b1d4d8d1
TBA
1211 /* We're completing the expression part. Skip the breakpoint num. */
1212 const char *exp_start = skip_spaces (space);
1213 tracker.advance_custom_word_point_by (exp_start - text);
1214 text = exp_start;
1215 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1216 expression_completer (cmd, tracker, text, word);
d55637df
TT
1217}
1218
c906108c
SS
1219/* condition N EXP -- set break condition of breakpoint N to EXP. */
1220
1221static void
0b39b52e 1222condition_command (const char *arg, int from_tty)
c906108c 1223{
0b39b52e 1224 const char *p;
52f0bd74 1225 int bnum;
c906108c
SS
1226
1227 if (arg == 0)
e2e0b3e5 1228 error_no_arg (_("breakpoint number"));
c906108c
SS
1229
1230 p = arg;
733d554a
TBA
1231
1232 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1233 condition_command_opts cc_opts;
1234 const auto group = make_condition_command_options_def_group (&cc_opts);
1235 gdb::option::process_options
1236 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1237
c906108c 1238 bnum = get_number (&p);
5c44784c 1239 if (bnum == 0)
8a3fe4f8 1240 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1241
79aabb73 1242 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1243}
1244
a7bdde9e
VP
1245/* Check that COMMAND do not contain commands that are suitable
1246 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1247 Throw if any such commands is found. */
1248
a7bdde9e
VP
1249static void
1250check_no_tracepoint_commands (struct command_line *commands)
1251{
1252 struct command_line *c;
cc59ec59 1253
a7bdde9e
VP
1254 for (c = commands; c; c = c->next)
1255 {
a7bdde9e 1256 if (c->control_type == while_stepping_control)
3e43a32a
MS
1257 error (_("The 'while-stepping' command can "
1258 "only be used for tracepoints"));
a7bdde9e 1259
12973681
TT
1260 check_no_tracepoint_commands (c->body_list_0.get ());
1261 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1262
1263 /* Not that command parsing removes leading whitespace and comment
4a64f543 1264 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1265 command directly. */
1266 if (strstr (c->line, "collect ") == c->line)
1267 error (_("The 'collect' command can only be used for tracepoints"));
1268
51661e93
VP
1269 if (strstr (c->line, "teval ") == c->line)
1270 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1271 }
1272}
1273
098f1272 1274struct longjmp_breakpoint : public momentary_breakpoint
3b0871f4 1275{
73063f51
TT
1276 using momentary_breakpoint::momentary_breakpoint;
1277
c1fc2657 1278 ~longjmp_breakpoint () override;
3b0871f4
SM
1279};
1280
d77f58be
SS
1281/* Encapsulate tests for different types of tracepoints. */
1282
3b0871f4
SM
1283static bool
1284is_tracepoint_type (bptype type)
d9b3f62e
PA
1285{
1286 return (type == bp_tracepoint
1287 || type == bp_fast_tracepoint
7b572efb
TT
1288 || type == bp_static_tracepoint
1289 || type == bp_static_marker_tracepoint);
d9b3f62e
PA
1290}
1291
f2478a7e
SM
1292/* See breakpoint.h. */
1293
1294bool
d77f58be 1295is_tracepoint (const struct breakpoint *b)
a7bdde9e 1296{
d9b3f62e 1297 return is_tracepoint_type (b->type);
a7bdde9e 1298}
d9b3f62e 1299
a5e364af
SM
1300/* Factory function to create an appropriate instance of breakpoint given
1301 TYPE. */
1302
3b003a61 1303template<typename... Arg>
74421c0b 1304static std::unique_ptr<code_breakpoint>
3b003a61
PA
1305new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1306 Arg&&... args)
a5e364af 1307{
74421c0b 1308 code_breakpoint *b;
a5e364af 1309
098f1272
TT
1310 switch (type)
1311 {
1312 case bp_breakpoint:
1313 case bp_hardware_breakpoint:
3b003a61
PA
1314 b = new ordinary_breakpoint (gdbarch, type,
1315 std::forward<Arg> (args)...);
098f1272
TT
1316 break;
1317
1318 case bp_fast_tracepoint:
1319 case bp_static_tracepoint:
1320 case bp_tracepoint:
3b003a61
PA
1321 b = new tracepoint (gdbarch, type,
1322 std::forward<Arg> (args)...);
098f1272
TT
1323 break;
1324
6d7a8c56 1325 case bp_static_marker_tracepoint:
3b003a61
PA
1326 b = new static_marker_tracepoint (gdbarch, type,
1327 std::forward<Arg> (args)...);
6d7a8c56
TT
1328 break;
1329
098f1272 1330 case bp_dprintf:
3b003a61
PA
1331 b = new dprintf_breakpoint (gdbarch, type,
1332 std::forward<Arg> (args)...);
098f1272
TT
1333 break;
1334
098f1272
TT
1335 default:
1336 gdb_assert_not_reached ("invalid type");
1337 }
a5e364af 1338
74421c0b 1339 return std::unique_ptr<code_breakpoint> (b);
a5e364af
SM
1340}
1341
e5dd4106 1342/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1343 breakpoint. This function will throw an exception if a problem is
1344 found. */
48cb2d85 1345
95a42b64
TT
1346static void
1347validate_commands_for_breakpoint (struct breakpoint *b,
1348 struct command_line *commands)
48cb2d85 1349{
d77f58be 1350 if (is_tracepoint (b))
a7bdde9e 1351 {
c9a6ce02 1352 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1353 struct command_line *c;
1354 struct command_line *while_stepping = 0;
c9a6ce02
PA
1355
1356 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1357 might have included a while-stepping action, while the new
1358 ones might not. */
c9a6ce02
PA
1359 t->step_count = 0;
1360
1361 /* We need to verify that each top-level element of commands is
1362 valid for tracepoints, that there's at most one
1363 while-stepping element, and that the while-stepping's body
1364 has valid tracing commands excluding nested while-stepping.
1365 We also need to validate the tracepoint action line in the
1366 context of the tracepoint --- validate_actionline actually
1367 has side effects, like setting the tracepoint's
1368 while-stepping STEP_COUNT, in addition to checking if the
1369 collect/teval actions parse and make sense in the
1370 tracepoint's context. */
a7bdde9e
VP
1371 for (c = commands; c; c = c->next)
1372 {
a7bdde9e
VP
1373 if (c->control_type == while_stepping_control)
1374 {
1375 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1376 error (_("The 'while-stepping' command "
1377 "cannot be used for fast tracepoint"));
7b572efb
TT
1378 else if (b->type == bp_static_tracepoint
1379 || b->type == bp_static_marker_tracepoint)
3e43a32a
MS
1380 error (_("The 'while-stepping' command "
1381 "cannot be used for static tracepoint"));
a7bdde9e
VP
1382
1383 if (while_stepping)
3e43a32a
MS
1384 error (_("The 'while-stepping' command "
1385 "can be used only once"));
a7bdde9e
VP
1386 else
1387 while_stepping = c;
1388 }
c9a6ce02
PA
1389
1390 validate_actionline (c->line, b);
a7bdde9e
VP
1391 }
1392 if (while_stepping)
1393 {
1394 struct command_line *c2;
1395
12973681
TT
1396 gdb_assert (while_stepping->body_list_1 == nullptr);
1397 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1398 for (; c2; c2 = c2->next)
1399 {
a7bdde9e
VP
1400 if (c2->control_type == while_stepping_control)
1401 error (_("The 'while-stepping' command cannot be nested"));
1402 }
1403 }
1404 }
1405 else
1406 {
1407 check_no_tracepoint_commands (commands);
1408 }
95a42b64
TT
1409}
1410
0fb4aa4b
PA
1411/* Return a vector of all the static tracepoints set at ADDR. The
1412 caller is responsible for releasing the vector. */
1413
f51e0e20 1414std::vector<breakpoint *>
0fb4aa4b
PA
1415static_tracepoints_here (CORE_ADDR addr)
1416{
f51e0e20 1417 std::vector<breakpoint *> found;
0fb4aa4b 1418
43892fdf 1419 for (breakpoint *b : all_breakpoints ())
7b572efb
TT
1420 if (b->type == bp_static_tracepoint
1421 || b->type == bp_static_marker_tracepoint)
0fb4aa4b 1422 {
40cb8ca5 1423 for (bp_location *loc : b->locations ())
0fb4aa4b 1424 if (loc->address == addr)
f51e0e20 1425 found.push_back (b);
0fb4aa4b
PA
1426 }
1427
1428 return found;
1429}
1430
95a42b64 1431/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1432 validate that only allowed commands are included. */
95a42b64
TT
1433
1434void
4a64f543 1435breakpoint_set_commands (struct breakpoint *b,
12973681 1436 counted_command_line &&commands)
95a42b64 1437{
93921405 1438 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1439
d1b0a7bf 1440 b->commands = std::move (commands);
76727919 1441 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1442}
1443
45a43567
TT
1444/* Set the internal `silent' flag on the breakpoint. Note that this
1445 is not the same as the "silent" that may appear in the breakpoint's
1446 commands. */
1447
1448void
1449breakpoint_set_silent (struct breakpoint *b, int silent)
1450{
1451 int old_silent = b->silent;
1452
1453 b->silent = silent;
1454 if (old_silent != silent)
76727919 1455 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1456}
1457
0a9ccb9d 1458/* See breakpoint.h. */
45a43567
TT
1459
1460void
1461breakpoint_set_thread (struct breakpoint *b, int thread)
1462{
0a9ccb9d
AB
1463 /* It is invalid to set the thread field to anything other than -1 (which
1464 means no thread restriction) if a task restriction is already in
1465 place. */
2ecee236 1466 gdb_assert (thread == -1 || b->task == -1);
0a9ccb9d 1467
45a43567
TT
1468 int old_thread = b->thread;
1469
1470 b->thread = thread;
1471 if (old_thread != thread)
76727919 1472 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1473}
1474
0a9ccb9d 1475/* See breakpoint.h. */
45a43567
TT
1476
1477void
1478breakpoint_set_task (struct breakpoint *b, int task)
1479{
2ecee236 1480 /* It is invalid to set the task field to anything other than -1 (which
0a9ccb9d
AB
1481 means no task restriction) if a thread restriction is already in
1482 place. */
2ecee236 1483 gdb_assert (task == -1 || b->thread == -1);
0a9ccb9d 1484
45a43567
TT
1485 int old_task = b->task;
1486
1487 b->task = task;
1488 if (old_task != task)
76727919 1489 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1490}
1491
95a42b64 1492static void
896b6bda 1493commands_command_1 (const char *arg, int from_tty,
4a64f543 1494 struct command_line *control)
95a42b64 1495{
d1b0a7bf 1496 counted_command_line cmd;
999700cd
PW
1497 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1498 NULL after the call to read_command_lines if the user provides an empty
1499 list of command by just typing "end". */
1500 bool cmd_read = false;
95a42b64 1501
896b6bda
PA
1502 std::string new_arg;
1503
95a42b64
TT
1504 if (arg == NULL || !*arg)
1505 {
da1df1db 1506 /* Argument not explicitly given. Synthesize it. */
86b17b60 1507 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1508 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1509 breakpoint_count);
95a42b64 1510 else if (breakpoint_count > 0)
896b6bda 1511 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1512 }
da1df1db
TBA
1513 else
1514 {
1515 /* Create a copy of ARG. This is needed because the "commands"
1516 command may be coming from a script. In that case, the read
1517 line buffer is going to be overwritten in the lambda of
1518 'map_breakpoint_numbers' below when reading the next line
1519 before we are are done parsing the breakpoint numbers. */
1520 new_arg = arg;
1521 }
1522 arg = new_arg.c_str ();
48649e1b
TT
1523
1524 map_breakpoint_numbers
1525 (arg, [&] (breakpoint *b)
1526 {
999700cd 1527 if (!cmd_read)
48649e1b 1528 {
999700cd 1529 gdb_assert (cmd == NULL);
48649e1b 1530 if (control != NULL)
12973681 1531 cmd = control->body_list_0;
48649e1b
TT
1532 else
1533 {
81b1e71c
TT
1534 std::string str
1535 = string_printf (_("Type commands for breakpoint(s) "
1536 "%s, one per line."),
1537 arg);
48649e1b 1538
60b3cef2
TT
1539 auto do_validate = [=] (const char *line)
1540 {
1541 validate_actionline (line, b);
1542 };
1543 gdb::function_view<void (const char *)> validator;
1544 if (is_tracepoint (b))
1545 validator = do_validate;
1546
1547 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1548 }
999700cd 1549 cmd_read = true;
48649e1b
TT
1550 }
1551
1552 /* If a breakpoint was on the list more than once, we don't need to
1553 do anything. */
1554 if (b->commands != cmd)
1555 {
d1b0a7bf 1556 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1557 b->commands = cmd;
76727919 1558 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1559 }
1560 });
95a42b64
TT
1561}
1562
1563static void
0b39b52e 1564commands_command (const char *arg, int from_tty)
95a42b64
TT
1565{
1566 commands_command_1 (arg, from_tty, NULL);
c906108c 1567}
40c03ae8
EZ
1568
1569/* Like commands_command, but instead of reading the commands from
1570 input stream, takes them from an already parsed command structure.
1571
1572 This is used by cli-script.c to DTRT with breakpoint commands
1573 that are part of if and while bodies. */
1574enum command_control_type
896b6bda 1575commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1576{
95a42b64
TT
1577 commands_command_1 (arg, 0, cmd);
1578 return simple_control;
40c03ae8 1579}
876fa593 1580
d8de7963 1581/* Return true if BL->TARGET_INFO contains valid information. */
876fa593 1582
d8de7963 1583static bool
876fa593
JK
1584bp_location_has_shadow (struct bp_location *bl)
1585{
1586 if (bl->loc_type != bp_loc_software_breakpoint)
d8de7963 1587 return false;
876fa593 1588 if (!bl->inserted)
d8de7963 1589 return false;
876fa593 1590 if (bl->target_info.shadow_len == 0)
e5dd4106 1591 /* BL isn't valid, or doesn't shadow memory. */
d8de7963
AB
1592 return false;
1593 return true;
876fa593
JK
1594}
1595
9d497a19
PA
1596/* Update BUF, which is LEN bytes read from the target address
1597 MEMADDR, by replacing a memory breakpoint with its shadowed
1598 contents.
1599
1600 If READBUF is not NULL, this buffer must not overlap with the of
1601 the breakpoint location's shadow_contents buffer. Otherwise, a
1602 failed assertion internal error will be raised. */
1603
1604static void
1605one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1606 const gdb_byte *writebuf_org,
1607 ULONGEST memaddr, LONGEST len,
1608 struct bp_target_info *target_info,
1609 struct gdbarch *gdbarch)
1610{
1611 /* Now do full processing of the found relevant range of elements. */
1612 CORE_ADDR bp_addr = 0;
1613 int bp_size = 0;
1614 int bptoffset = 0;
1615
1616 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1617 current_program_space->aspace, 0))
1618 {
1619 /* The breakpoint is inserted in a different address space. */
1620 return;
1621 }
1622
1623 /* Addresses and length of the part of the breakpoint that
1624 we need to copy. */
1625 bp_addr = target_info->placed_address;
1626 bp_size = target_info->shadow_len;
1627
1628 if (bp_addr + bp_size <= memaddr)
1629 {
1630 /* The breakpoint is entirely before the chunk of memory we are
1631 reading. */
1632 return;
1633 }
1634
1635 if (bp_addr >= memaddr + len)
1636 {
1637 /* The breakpoint is entirely after the chunk of memory we are
1638 reading. */
1639 return;
1640 }
1641
1642 /* Offset within shadow_contents. */
1643 if (bp_addr < memaddr)
1644 {
1645 /* Only copy the second part of the breakpoint. */
1646 bp_size -= memaddr - bp_addr;
1647 bptoffset = memaddr - bp_addr;
1648 bp_addr = memaddr;
1649 }
1650
1651 if (bp_addr + bp_size > memaddr + len)
1652 {
1653 /* Only copy the first part of the breakpoint. */
1654 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1655 }
1656
1657 if (readbuf != NULL)
1658 {
1659 /* Verify that the readbuf buffer does not overlap with the
1660 shadow_contents buffer. */
1661 gdb_assert (target_info->shadow_contents >= readbuf + len
1662 || readbuf >= (target_info->shadow_contents
1663 + target_info->shadow_len));
1664
1665 /* Update the read buffer with this inserted breakpoint's
1666 shadow. */
1667 memcpy (readbuf + bp_addr - memaddr,
1668 target_info->shadow_contents + bptoffset, bp_size);
1669 }
1670 else
1671 {
1672 const unsigned char *bp;
0d5ed153
MR
1673 CORE_ADDR addr = target_info->reqstd_address;
1674 int placed_size;
9d497a19
PA
1675
1676 /* Update the shadow with what we want to write to memory. */
1677 memcpy (target_info->shadow_contents + bptoffset,
1678 writebuf_org + bp_addr - memaddr, bp_size);
1679
1680 /* Determine appropriate breakpoint contents and size for this
1681 address. */
0d5ed153 1682 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1683
1684 /* Update the final write buffer with this inserted
1685 breakpoint's INSN. */
1686 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1687 }
1688}
1689
8defab1a 1690/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1691 by replacing any memory breakpoints with their shadowed contents.
1692
35c63cd8
JB
1693 If READBUF is not NULL, this buffer must not overlap with any of
1694 the breakpoint location's shadow_contents buffers. Otherwise,
1695 a failed assertion internal error will be raised.
1696
876fa593 1697 The range of shadowed area by each bp_location is:
f5336ca5
PA
1698 bl->address - bp_locations_placed_address_before_address_max
1699 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1700 The range we were requested to resolve shadows for is:
1701 memaddr ... memaddr + len
1702 Thus the safe cutoff boundaries for performance optimization are
35df4500 1703 memaddr + len <= (bl->address
f5336ca5 1704 - bp_locations_placed_address_before_address_max)
876fa593 1705 and:
f5336ca5 1706 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1707
8defab1a 1708void
f0ba3972
PA
1709breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1710 const gdb_byte *writebuf_org,
1711 ULONGEST memaddr, LONGEST len)
c906108c 1712{
4a64f543
MS
1713 /* Left boundary, right boundary and median element of our binary
1714 search. */
876fa593
JK
1715 unsigned bc_l, bc_r, bc;
1716
4a64f543
MS
1717 /* Find BC_L which is a leftmost element which may affect BUF
1718 content. It is safe to report lower value but a failure to
1719 report higher one. */
876fa593
JK
1720
1721 bc_l = 0;
5d51cd5d 1722 bc_r = bp_locations.size ();
876fa593
JK
1723 while (bc_l + 1 < bc_r)
1724 {
35df4500 1725 struct bp_location *bl;
876fa593
JK
1726
1727 bc = (bc_l + bc_r) / 2;
f5336ca5 1728 bl = bp_locations[bc];
876fa593 1729
4a64f543
MS
1730 /* Check first BL->ADDRESS will not overflow due to the added
1731 constant. Then advance the left boundary only if we are sure
1732 the BC element can in no way affect the BUF content (MEMADDR
1733 to MEMADDR + LEN range).
876fa593 1734
f5336ca5 1735 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1736 offset so that we cannot miss a breakpoint with its shadow
1737 range tail still reaching MEMADDR. */
c5aa993b 1738
f5336ca5 1739 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1740 >= bl->address)
f5336ca5 1741 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1742 <= memaddr))
876fa593
JK
1743 bc_l = bc;
1744 else
1745 bc_r = bc;
1746 }
1747
128070bb
PA
1748 /* Due to the binary search above, we need to make sure we pick the
1749 first location that's at BC_L's address. E.g., if there are
1750 multiple locations at the same address, BC_L may end up pointing
1751 at a duplicate location, and miss the "master"/"inserted"
1752 location. Say, given locations L1, L2 and L3 at addresses A and
1753 B:
1754
1755 L1@A, L2@A, L3@B, ...
1756
1757 BC_L could end up pointing at location L2, while the "master"
1758 location could be L1. Since the `loc->inserted' flag is only set
1759 on "master" locations, we'd forget to restore the shadow of L1
1760 and L2. */
1761 while (bc_l > 0
f5336ca5 1762 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1763 bc_l--;
1764
876fa593
JK
1765 /* Now do full processing of the found relevant range of elements. */
1766
5d51cd5d 1767 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1768 {
1769 struct bp_location *bl = bp_locations[bc];
876fa593 1770
01add95b
SM
1771 /* bp_location array has BL->OWNER always non-NULL. */
1772 if (bl->owner->type == bp_none)
1773 warning (_("reading through apparently deleted breakpoint #%d?"),
1774 bl->owner->number);
ffce0d52 1775
01add95b
SM
1776 /* Performance optimization: any further element can no longer affect BUF
1777 content. */
876fa593 1778
01add95b
SM
1779 if (bl->address >= bp_locations_placed_address_before_address_max
1780 && (memaddr + len
1781 <= (bl->address
1782 - bp_locations_placed_address_before_address_max)))
1783 break;
876fa593 1784
01add95b
SM
1785 if (!bp_location_has_shadow (bl))
1786 continue;
6c95b8df 1787
01add95b
SM
1788 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1789 memaddr, len, &bl->target_info, bl->gdbarch);
1790 }
c906108c 1791}
9d497a19 1792
f2478a7e 1793/* See breakpoint.h. */
b775012e 1794
f2478a7e 1795bool
b775012e
LM
1796is_breakpoint (const struct breakpoint *bpt)
1797{
1798 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1799 || bpt->type == bp_hardware_breakpoint
1800 || bpt->type == bp_dprintf);
b775012e
LM
1801}
1802
60e1c644
PA
1803/* Return true if BPT is of any hardware watchpoint kind. */
1804
f2478a7e 1805static bool
d77f58be 1806is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1807{
1808 return (bpt->type == bp_hardware_watchpoint
1809 || bpt->type == bp_read_watchpoint
1810 || bpt->type == bp_access_watchpoint);
1811}
7270d8f2 1812
f2478a7e 1813/* See breakpoint.h. */
60e1c644 1814
f2478a7e 1815bool
d77f58be 1816is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1817{
1818 return (is_hardware_watchpoint (bpt)
1819 || bpt->type == bp_watchpoint);
1820}
1821
3a5c3e22
PA
1822/* Returns true if the current thread and its running state are safe
1823 to evaluate or update watchpoint B. Watchpoints on local
1824 expressions need to be evaluated in the context of the thread that
1825 was current when the watchpoint was created, and, that thread needs
1826 to be stopped to be able to select the correct frame context.
1827 Watchpoints on global expressions can be evaluated on any thread,
1828 and in any state. It is presently left to the target allowing
1829 memory accesses when threads are running. */
f6bc2008 1830
d8de7963 1831static bool
3a5c3e22 1832watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1833{
c1fc2657 1834 return (b->pspace == current_program_space
d7e15655
TT
1835 && (b->watchpoint_thread == null_ptid
1836 || (inferior_ptid == b->watchpoint_thread
611841bb 1837 && !inferior_thread ()->executing ())));
f6bc2008
PA
1838}
1839
d0fb5eae
JK
1840/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1841 associated bp_watchpoint_scope breakpoint. */
1842
1843static void
3a5c3e22 1844watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1845{
c1fc2657 1846 if (w->related_breakpoint != w)
d0fb5eae 1847 {
c1fc2657
SM
1848 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1849 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1850 w->related_breakpoint->disposition = disp_del_at_next_stop;
1851 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1852 w->related_breakpoint = w;
d0fb5eae 1853 }
c1fc2657 1854 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1855}
1856
bb9d5f81
PP
1857/* Extract a bitfield value from value VAL using the bit parameters contained in
1858 watchpoint W. */
1859
1860static struct value *
1861extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1862{
1863 struct value *bit_val;
1864
1865 if (val == NULL)
1866 return NULL;
1867
317c3ed9 1868 bit_val = value::allocate (val->type ());
bb9d5f81 1869
6c49729e
TT
1870 val->unpack_bitfield (bit_val,
1871 w->val_bitpos,
1872 w->val_bitsize,
1873 val->contents_for_printing ().data (),
1874 val->offset ());
bb9d5f81
PP
1875
1876 return bit_val;
1877}
1878
6e14e441
PA
1879/* Allocate a dummy location and add it to B. This is required
1880 because bpstat_stop_status requires a location to be able to report
1881 stops. */
c6d81124
PA
1882
1883static void
6e14e441
PA
1884add_dummy_location (struct breakpoint *b,
1885 struct program_space *pspace)
c6d81124 1886{
6e14e441 1887 gdb_assert (b->loc == NULL);
c6d81124 1888
6e14e441 1889 b->loc = new bp_location (b, bp_loc_other);
c6d81124 1890 b->loc->pspace = pspace;
c6d81124
PA
1891}
1892
567e1b4e 1893/* Assuming that B is a watchpoint:
d8de7963 1894 - Reparse watchpoint expression, if REPARSE is true
a5606eee 1895 - Evaluate expression and store the result in B->val
567e1b4e
JB
1896 - Evaluate the condition if there is one, and store the result
1897 in b->loc->cond.
a5606eee
VP
1898 - Update the list of values that must be watched in B->loc.
1899
4a64f543
MS
1900 If the watchpoint disposition is disp_del_at_next_stop, then do
1901 nothing. If this is local watchpoint that is out of scope, delete
1902 it.
1903
1904 Even with `set breakpoint always-inserted on' the watchpoints are
1905 removed + inserted on each stop here. Normal breakpoints must
1906 never be removed because they might be missed by a running thread
1907 when debugging in non-stop mode. On the other hand, hardware
1908 watchpoints (is_hardware_watchpoint; processed here) are specific
1909 to each LWP since they are stored in each LWP's hardware debug
1910 registers. Therefore, such LWP must be stopped first in order to
1911 be able to modify its hardware watchpoints.
1912
1913 Hardware watchpoints must be reset exactly once after being
1914 presented to the user. It cannot be done sooner, because it would
1915 reset the data used to present the watchpoint hit to the user. And
1916 it must not be done later because it could display the same single
1917 watchpoint hit during multiple GDB stops. Note that the latter is
1918 relevant only to the hardware watchpoint types bp_read_watchpoint
1919 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1920 not user-visible - its hit is suppressed if the memory content has
1921 not changed.
1922
1923 The following constraints influence the location where we can reset
1924 hardware watchpoints:
1925
1926 * target_stopped_by_watchpoint and target_stopped_data_address are
1927 called several times when GDB stops.
1928
1929 [linux]
1930 * Multiple hardware watchpoints can be hit at the same time,
1931 causing GDB to stop. GDB only presents one hardware watchpoint
1932 hit at a time as the reason for stopping, and all the other hits
1933 are presented later, one after the other, each time the user
1934 requests the execution to be resumed. Execution is not resumed
1935 for the threads still having pending hit event stored in
1936 LWP_INFO->STATUS. While the watchpoint is already removed from
1937 the inferior on the first stop the thread hit event is kept being
1938 reported from its cached value by linux_nat_stopped_data_address
1939 until the real thread resume happens after the watchpoint gets
1940 presented and thus its LWP_INFO->STATUS gets reset.
1941
1942 Therefore the hardware watchpoint hit can get safely reset on the
1943 watchpoint removal from inferior. */
a79d3c27 1944
b40ce68a 1945static void
d8de7963 1946update_watchpoint (struct watchpoint *b, bool reparse)
7270d8f2 1947{
705b6305 1948 bool within_current_scope;
a5606eee 1949
f6bc2008
PA
1950 /* If this is a local watchpoint, we only want to check if the
1951 watchpoint frame is in scope if the current thread is the thread
1952 that was used to create the watchpoint. */
1953 if (!watchpoint_in_thread_scope (b))
1954 return;
1955
c1fc2657 1956 if (b->disposition == disp_del_at_next_stop)
a5606eee 1957 return;
705b6305
AB
1958
1959 gdb::optional<scoped_restore_selected_frame> restore_frame;
a5606eee
VP
1960
1961 /* Determine if the watchpoint is within scope. */
1962 if (b->exp_valid_block == NULL)
705b6305 1963 within_current_scope = true;
a5606eee
VP
1964 else
1965 {
bd2b40ac 1966 frame_info_ptr fi = get_current_frame ();
b5db5dfc
UW
1967 struct gdbarch *frame_arch = get_frame_arch (fi);
1968 CORE_ADDR frame_pc = get_frame_pc (fi);
1969
c9cf6e20
MG
1970 /* If we're at a point where the stack has been destroyed
1971 (e.g. in a function epilogue), unwinding may not work
1972 properly. Do not attempt to recreate locations at this
b5db5dfc 1973 point. See similar comments in watchpoint_check. */
c9cf6e20 1974 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1975 return;
66076460
DJ
1976
1977 /* Save the current frame's ID so we can restore it after
dda83cd7 1978 evaluating the watchpoint expression on its own frame. */
66076460 1979 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1980 took a frame parameter, so that we didn't have to change the
1981 selected frame. */
705b6305 1982 restore_frame.emplace ();
66076460 1983
a5606eee
VP
1984 fi = frame_find_by_id (b->watchpoint_frame);
1985 within_current_scope = (fi != NULL);
1986 if (within_current_scope)
1987 select_frame (fi);
1988 }
1989
b5db5dfc
UW
1990 /* We don't free locations. They are stored in the bp_location array
1991 and update_global_location_list will eventually delete them and
1992 remove breakpoints if needed. */
c1fc2657 1993 b->loc = NULL;
b5db5dfc 1994
a5606eee
VP
1995 if (within_current_scope && reparse)
1996 {
bbc13ae3 1997 const char *s;
d63d0675 1998
4d01a485 1999 b->exp.reset ();
a4c50be3
TT
2000 s = (b->exp_string_reparse
2001 ? b->exp_string_reparse.get ()
2002 : b->exp_string.get ());
1bb9788d 2003 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
2004 /* If the meaning of expression itself changed, the old value is
2005 no longer relevant. We don't want to report a watchpoint hit
2006 to the user when the old value and the new value may actually
2007 be completely different objects. */
fa4727a6 2008 b->val = NULL;
4c1d86d9 2009 b->val_valid = false;
60e1c644
PA
2010
2011 /* Note that unlike with breakpoints, the watchpoint's condition
2012 expression is stored in the breakpoint object, not in the
2013 locations (re)created below. */
c1fc2657 2014 if (b->cond_string != NULL)
60e1c644 2015 {
4d01a485 2016 b->cond_exp.reset ();
60e1c644 2017
6f781ee3 2018 s = b->cond_string.get ();
1bb9788d 2019 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 2020 }
a5606eee 2021 }
a5606eee
VP
2022
2023 /* If we failed to parse the expression, for example because
2024 it refers to a global variable in a not-yet-loaded shared library,
2025 don't try to insert watchpoint. We don't automatically delete
2026 such watchpoint, though, since failure to parse expression
2027 is different from out-of-scope watchpoint. */
55f6301a 2028 if (!target_has_execution ())
2d134ed3
PA
2029 {
2030 /* Without execution, memory can't change. No use to try and
2031 set watchpoint locations. The watchpoint will be reset when
2032 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
2033 if (!can_use_hw_watchpoints)
2034 {
c47614fe 2035 if (b->works_in_software_mode ())
c1fc2657 2036 b->type = bp_watchpoint;
e8369a73 2037 else
638aa5a1
AB
2038 error (_("Can't set read/access watchpoint when "
2039 "hardware watchpoints are disabled."));
e8369a73 2040 }
2d134ed3
PA
2041 }
2042 else if (within_current_scope && b->exp)
a5606eee 2043 {
a6535de1 2044 std::vector<value_ref_ptr> val_chain;
8d49165d 2045 struct value *v, *result;
2d134ed3 2046 struct program_space *frame_pspace;
a5606eee 2047
1eaebe02 2048 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 2049 &val_chain, false);
a5606eee 2050
a5606eee
VP
2051 /* Avoid setting b->val if it's already set. The meaning of
2052 b->val is 'the last value' user saw, and we should update
2053 it only if we reported that last value to user. As it
9c06b0b4
TJB
2054 happens, the code that reports it updates b->val directly.
2055 We don't keep track of the memory value for masked
2056 watchpoints. */
c1fc2657 2057 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 2058 {
bb9d5f81 2059 if (b->val_bitsize != 0)
850645cf
TT
2060 v = extract_bitfield_from_watchpoint_value (b, v);
2061 b->val = release_value (v);
4c1d86d9 2062 b->val_valid = true;
fa4727a6 2063 }
a5606eee 2064
2d134ed3
PA
2065 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2066
a5606eee 2067 /* Look at each value on the value chain. */
a6535de1
TT
2068 gdb_assert (!val_chain.empty ());
2069 for (const value_ref_ptr &iter : val_chain)
a5606eee 2070 {
a6535de1
TT
2071 v = iter.get ();
2072
a5606eee
VP
2073 /* If it's a memory location, and GDB actually needed
2074 its contents to evaluate the expression, then we
fa4727a6
DJ
2075 must watch it. If the first value returned is
2076 still lazy, that means an error occurred reading it;
2077 watch it anyway in case it becomes readable. */
736355f2 2078 if (v->lval () == lval_memory
3ee3b270 2079 && (v == val_chain[0] || ! v->lazy ()))
a5606eee 2080 {
d0c97917 2081 struct type *vtype = check_typedef (v->type ());
7270d8f2 2082
a5606eee
VP
2083 /* We only watch structs and arrays if user asked
2084 for it explicitly, never if they just happen to
2085 appear in the middle of some value chain. */
fa4727a6 2086 if (v == result
78134374
SM
2087 || (vtype->code () != TYPE_CODE_STRUCT
2088 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
2089 {
2090 CORE_ADDR addr;
f486487f 2091 enum target_hw_bp_type type;
a5606eee 2092 struct bp_location *loc, **tmp;
bb9d5f81
PP
2093 int bitpos = 0, bitsize = 0;
2094
f49d5fa2 2095 if (v->bitsize () != 0)
bb9d5f81
PP
2096 {
2097 /* Extract the bit parameters out from the bitfield
2098 sub-expression. */
5011c493 2099 bitpos = v->bitpos ();
f49d5fa2 2100 bitsize = v->bitsize ();
bb9d5f81
PP
2101 }
2102 else if (v == result && b->val_bitsize != 0)
2103 {
2104 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2105 lvalue whose bit parameters are saved in the fields
2106 VAL_BITPOS and VAL_BITSIZE. */
2107 bitpos = b->val_bitpos;
2108 bitsize = b->val_bitsize;
2109 }
a5606eee 2110
9feb2d07 2111 addr = v->address ();
bb9d5f81
PP
2112 if (bitsize != 0)
2113 {
2114 /* Skip the bytes that don't contain the bitfield. */
2115 addr += bitpos / 8;
2116 }
2117
a5606eee 2118 type = hw_write;
c1fc2657 2119 if (b->type == bp_read_watchpoint)
a5606eee 2120 type = hw_read;
c1fc2657 2121 else if (b->type == bp_access_watchpoint)
a5606eee 2122 type = hw_access;
3a5c3e22 2123
ee89d0a4 2124 loc = b->allocate_location ();
c1fc2657 2125 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2126 ;
2127 *tmp = loc;
d0c97917 2128 loc->gdbarch = v->type ()->arch ();
6c95b8df
PA
2129
2130 loc->pspace = frame_pspace;
d88cb738
LM
2131 loc->address
2132 = gdbarch_remove_non_address_bits (loc->gdbarch, addr);
bb9d5f81
PP
2133
2134 if (bitsize != 0)
2135 {
2136 /* Just cover the bytes that make up the bitfield. */
2137 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2138 }
2139 else
d0c97917 2140 loc->length = v->type ()->length ();
bb9d5f81 2141
a5606eee
VP
2142 loc->watchpoint_type = type;
2143 }
2144 }
9fa40276
TJB
2145 }
2146
07c1c91d
AB
2147 /* Helper function to bundle possibly emitting a warning along with
2148 changing the type of B to bp_watchpoint. */
2149 auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
2150 {
2151 /* Only warn for breakpoints that have been assigned a +ve number,
2152 anything else is either an internal watchpoint (which we don't
2153 currently create) or has not yet been finalized, in which case
2154 this change of type will be occurring before the user is told
2155 the type of this watchpoint. */
2156 if (bp->type == bp_hardware_watchpoint && bp->number > 0)
2157 warning (_("watchpoint %d downgraded to software watchpoint"),
2158 bp->number);
2159 bp->type = bp_watchpoint;
2160 };
2161
9fa40276 2162 /* Change the type of breakpoint between hardware assisted or
bc45f536
CL
2163 an ordinary watchpoint depending on the hardware support and
2164 free hardware slots. Recheck the number of free hardware slots
2165 as the value chain may have changed. */
9fa40276 2166 {
e09342b5 2167 int reg_cnt;
9fa40276 2168 enum bp_loc_type loc_type;
a5606eee 2169
a9634178 2170 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2171
2172 if (reg_cnt)
9fa40276
TJB
2173 {
2174 int i, target_resources_ok, other_type_used;
a1398e0c 2175 enum bptype type;
9fa40276 2176
a9634178
TJB
2177 /* Use an exact watchpoint when there's only one memory region to be
2178 watched, and only one debug register is needed to watch it. */
2179 b->exact = target_exact_watchpoints && reg_cnt == 1;
2180
9fa40276 2181 /* We need to determine how many resources are already
e09342b5
TJB
2182 used for all other hardware watchpoints plus this one
2183 to see if we still have enough resources to also fit
a1398e0c
PA
2184 this watchpoint in as well. */
2185
2186 /* If this is a software watchpoint, we try to turn it
2187 to a hardware one -- count resources as if B was of
2188 hardware watchpoint type. */
c1fc2657 2189 type = b->type;
a1398e0c
PA
2190 if (type == bp_watchpoint)
2191 type = bp_hardware_watchpoint;
2192
2193 /* This watchpoint may or may not have been placed on
2194 the list yet at this point (it won't be in the list
2195 if we're trying to create it for the first time,
2196 through watch_command), so always account for it
2197 manually. */
2198
2199 /* Count resources used by all watchpoints except B. */
c1fc2657 2200 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2201
2202 /* Add in the resources needed for B. */
c1fc2657 2203 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2204
2205 target_resources_ok
2206 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2207 if (target_resources_ok <= 0)
a9634178 2208 {
5a61e176 2209 bool sw_mode = b->works_in_software_mode ();
9c06b0b4
TJB
2210
2211 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2212 error (_("Target does not support this type of "
2213 "hardware watchpoint."));
9c06b0b4
TJB
2214 else if (target_resources_ok < 0 && !sw_mode)
2215 error (_("There are not enough available hardware "
2216 "resources for this watchpoint."));
a1398e0c
PA
2217
2218 /* Downgrade to software watchpoint. */
07c1c91d 2219 change_type_to_bp_watchpoint (b);
a1398e0c
PA
2220 }
2221 else
2222 {
2223 /* If this was a software watchpoint, we've just
2224 found we have enough resources to turn it to a
2225 hardware watchpoint. Otherwise, this is a
2226 nop. */
c1fc2657 2227 b->type = type;
a9634178 2228 }
9fa40276 2229 }
c47614fe 2230 else if (!b->works_in_software_mode ())
638aa5a1
AB
2231 {
2232 if (!can_use_hw_watchpoints)
2233 error (_("Can't set read/access watchpoint when "
2234 "hardware watchpoints are disabled."));
2235 else
2236 error (_("Expression cannot be implemented with "
2237 "read/access watchpoint."));
2238 }
9fa40276 2239 else
07c1c91d 2240 change_type_to_bp_watchpoint (b);
9fa40276 2241
6e14e441 2242 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
9fa40276 2243 : bp_loc_hardware_watchpoint);
40cb8ca5 2244 for (bp_location *bl : b->locations ())
9fa40276
TJB
2245 bl->loc_type = loc_type;
2246 }
2247
c7437ca6
PA
2248 /* If a software watchpoint is not watching any memory, then the
2249 above left it without any location set up. But,
2250 bpstat_stop_status requires a location to be able to report
2251 stops, so make sure there's at least a dummy one. */
c1fc2657 2252 if (b->type == bp_watchpoint && b->loc == NULL)
6e14e441 2253 add_dummy_location (b, frame_pspace);
a5606eee
VP
2254 }
2255 else if (!within_current_scope)
7270d8f2 2256 {
6cb06a8c 2257 gdb_printf (_("\
ac74f770
MS
2258Watchpoint %d deleted because the program has left the block\n\
2259in which its expression is valid.\n"),
6cb06a8c 2260 b->number);
d0fb5eae 2261 watchpoint_del_at_next_stop (b);
7270d8f2 2262 }
7270d8f2
OF
2263}
2264
d8de7963 2265/* Returns true iff breakpoint location should be
1e4d1764
YQ
2266 inserted in the inferior. We don't differentiate the type of BL's owner
2267 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2268 breakpoint_ops is not defined, because in insert_bp_location,
2269 tracepoint's insert_location will not be called. */
d8de7963
AB
2270
2271static bool
35df4500 2272should_be_inserted (struct bp_location *bl)
74960c60 2273{
35df4500 2274 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
d8de7963 2275 return false;
74960c60 2276
35df4500 2277 if (bl->owner->disposition == disp_del_at_next_stop)
d8de7963 2278 return false;
74960c60 2279
b5fa468f
TBA
2280 if (!bl->enabled || bl->disabled_by_cond
2281 || bl->shlib_disabled || bl->duplicate)
d8de7963 2282 return false;
74960c60 2283
f8eba3c6 2284 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
d8de7963 2285 return false;
f8eba3c6 2286
56710373
PA
2287 /* This is set for example, when we're attached to the parent of a
2288 vfork, and have detached from the child. The child is running
2289 free, and we expect it to do an exec or exit, at which point the
2290 OS makes the parent schedulable again (and the target reports
2291 that the vfork is done). Until the child is done with the shared
2292 memory region, do not insert breakpoints in the parent, otherwise
2293 the child could still trip on the parent's breakpoints. Since
2294 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2295 if (bl->pspace->breakpoints_not_allowed)
d8de7963 2296 return false;
56710373 2297
31e77af2 2298 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2299 location, except if the breakpoint is a single-step breakpoint,
2300 and the breakpoint's thread is the thread which is stepping past
2301 a breakpoint. */
31e77af2
PA
2302 if ((bl->loc_type == bp_loc_software_breakpoint
2303 || bl->loc_type == bp_loc_hardware_breakpoint)
2304 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2305 bl->address)
2306 /* The single-step breakpoint may be inserted at the location
2307 we're trying to step if the instruction branches to itself.
2308 However, the instruction won't be executed at all and it may
2309 break the semantics of the instruction, for example, the
2310 instruction is a conditional branch or updates some flags.
2311 We can't fix it unless GDB is able to emulate the instruction
2312 or switch to displaced stepping. */
2313 && !(bl->owner->type == bp_single_step
2314 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2315 {
1eb8556f
SM
2316 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2317 paddress (bl->gdbarch, bl->address));
d8de7963 2318 return false;
e558d7c1 2319 }
31e77af2 2320
963f9c80
PA
2321 /* Don't insert watchpoints if we're trying to step past the
2322 instruction that triggered one. */
2323 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2324 && stepping_past_nonsteppable_watchpoint ())
2325 {
1eb8556f 2326 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2327 "skipping watchpoint at %s:%d",
1eb8556f 2328 paddress (bl->gdbarch, bl->address), bl->length);
d8de7963 2329 return false;
963f9c80
PA
2330 }
2331
d8de7963 2332 return true;
74960c60
VP
2333}
2334
934709f0
PW
2335/* Same as should_be_inserted but does the check assuming
2336 that the location is not duplicated. */
2337
aaa141a0 2338static bool
934709f0
PW
2339unduplicated_should_be_inserted (struct bp_location *bl)
2340{
aaa141a0
AB
2341 scoped_restore restore_bl_duplicate
2342 = make_scoped_restore (&bl->duplicate, 0);
934709f0 2343
aaa141a0 2344 return should_be_inserted (bl);
934709f0
PW
2345}
2346
b775012e
LM
2347/* Parses a conditional described by an expression COND into an
2348 agent expression bytecode suitable for evaluation
2349 by the bytecode interpreter. Return NULL if there was
2350 any error during parsing. */
2351
833177a4 2352static agent_expr_up
b775012e
LM
2353parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2354{
833177a4 2355 if (cond == NULL)
b775012e
LM
2356 return NULL;
2357
833177a4
PA
2358 agent_expr_up aexpr;
2359
b775012e
LM
2360 /* We don't want to stop processing, so catch any errors
2361 that may show up. */
a70b8144 2362 try
b775012e 2363 {
036e657b 2364 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2365 }
2366
230d2906 2367 catch (const gdb_exception_error &ex)
b775012e
LM
2368 {
2369 /* If we got here, it means the condition could not be parsed to a valid
2370 bytecode expression and thus can't be evaluated on the target's side.
2371 It's no use iterating through the conditions. */
b775012e
LM
2372 }
2373
2374 /* We have a valid agent expression. */
2375 return aexpr;
2376}
2377
2378/* Based on location BL, create a list of breakpoint conditions to be
2379 passed on to the target. If we have duplicated locations with different
2380 conditions, we will add such conditions to the list. The idea is that the
2381 target will evaluate the list of conditions and will only notify GDB when
2382 one of them is true. */
2383
2384static void
2385build_target_condition_list (struct bp_location *bl)
2386{
d8de7963 2387 bool null_condition_or_parse_error = false;
b775012e 2388 int modified = bl->needs_update;
b775012e 2389
8b4f3082 2390 /* Release conditions left over from a previous insert. */
3cde5c42 2391 bl->target_info.conditions.clear ();
8b4f3082 2392
b775012e
LM
2393 /* This is only meaningful if the target is
2394 evaluating conditions and if the user has
2395 opted for condition evaluation on the target's
2396 side. */
2397 if (gdb_evaluates_breakpoint_condition_p ()
2398 || !target_supports_evaluation_of_breakpoint_conditions ())
2399 return;
2400
e0d9a270
SM
2401 auto loc_range = all_bp_locations_at_addr (bl->address);
2402
b775012e 2403 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2404 conditions or conditions that fail to parse to a valid agent
2405 expression bytecode. If any of these happen, then it's no use to
2406 send conditions to the target since this location will always
2407 trigger and generate a response back to GDB. Note we consider
2408 all locations at the same address irrespective of type, i.e.,
2409 even if the locations aren't considered duplicates (e.g.,
2410 software breakpoint and hardware breakpoint at the same
2411 address). */
e0d9a270 2412 for (bp_location *loc : loc_range)
b775012e 2413 {
b775012e
LM
2414 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2415 {
2416 if (modified)
2417 {
b775012e
LM
2418 /* Re-parse the conditions since something changed. In that
2419 case we already freed the condition bytecodes (see
2420 force_breakpoint_reinsertion). We just
2421 need to parse the condition to bytecodes again. */
833177a4
PA
2422 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2423 loc->cond.get ());
b775012e
LM
2424 }
2425
2426 /* If we have a NULL bytecode expression, it means something
2427 went wrong or we have a null condition expression. */
2428 if (!loc->cond_bytecode)
2429 {
d8de7963 2430 null_condition_or_parse_error = true;
b775012e
LM
2431 break;
2432 }
2433 }
2434 }
2435
2436 /* If any of these happened, it means we will have to evaluate the conditions
2437 for the location's address on gdb's side. It is no use keeping bytecodes
2438 for all the other duplicate locations, thus we free all of them here.
2439
2440 This is so we have a finer control over which locations' conditions are
2441 being evaluated by GDB or the remote stub. */
2442 if (null_condition_or_parse_error)
2443 {
e0d9a270 2444 for (bp_location *loc : loc_range)
b775012e 2445 {
b775012e
LM
2446 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2447 {
2448 /* Only go as far as the first NULL bytecode is
2449 located. */
2450 if (!loc->cond_bytecode)
2451 return;
2452
833177a4 2453 loc->cond_bytecode.reset ();
b775012e
LM
2454 }
2455 }
2456 }
2457
7f32a4d5
PA
2458 /* No NULL conditions or failed bytecode generation. Build a
2459 condition list for this location's address. If we have software
2460 and hardware locations at the same address, they aren't
2461 considered duplicates, but we still marge all the conditions
2462 anyway, as it's simpler, and doesn't really make a practical
2463 difference. */
e0d9a270
SM
2464 for (bp_location *loc : loc_range)
2465 if (loc->cond
2466 && is_breakpoint (loc->owner)
2467 && loc->pspace->num == bl->pspace->num
2468 && loc->owner->enable_state == bp_enabled
2469 && loc->enabled
2470 && !loc->disabled_by_cond)
2471 {
2472 /* Add the condition to the vector. This will be used later
2473 to send the conditions to the target. */
2474 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2475 }
b775012e
LM
2476
2477 return;
2478}
2479
d3ce09f5
SS
2480/* Parses a command described by string CMD into an agent expression
2481 bytecode suitable for evaluation by the bytecode interpreter.
2482 Return NULL if there was any error during parsing. */
2483
833177a4 2484static agent_expr_up
d3ce09f5
SS
2485parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2486{
bbc13ae3
KS
2487 const char *cmdrest;
2488 const char *format_start, *format_end;
d3ce09f5
SS
2489 struct gdbarch *gdbarch = get_current_arch ();
2490
833177a4 2491 if (cmd == NULL)
d3ce09f5
SS
2492 return NULL;
2493
2494 cmdrest = cmd;
2495
2496 if (*cmdrest == ',')
2497 ++cmdrest;
f1735a53 2498 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2499
2500 if (*cmdrest++ != '"')
2501 error (_("No format string following the location"));
2502
2503 format_start = cmdrest;
2504
8e481c3b 2505 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2506
2507 format_end = cmdrest;
2508
2509 if (*cmdrest++ != '"')
2510 error (_("Bad format string, non-terminated '\"'."));
2511
f1735a53 2512 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2513
2514 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2515 error (_("Invalid argument syntax"));
2516
2517 if (*cmdrest == ',')
2518 cmdrest++;
f1735a53 2519 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2520
2521 /* For each argument, make an expression. */
2522
8e481c3b 2523 std::vector<struct expression *> argvec;
d3ce09f5
SS
2524 while (*cmdrest != '\0')
2525 {
bbc13ae3 2526 const char *cmd1;
d3ce09f5
SS
2527
2528 cmd1 = cmdrest;
4d01a485 2529 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2530 argvec.push_back (expr.release ());
d3ce09f5
SS
2531 cmdrest = cmd1;
2532 if (*cmdrest == ',')
2533 ++cmdrest;
2534 }
2535
833177a4
PA
2536 agent_expr_up aexpr;
2537
d3ce09f5
SS
2538 /* We don't want to stop processing, so catch any errors
2539 that may show up. */
a70b8144 2540 try
d3ce09f5 2541 {
036e657b
JB
2542 aexpr = gen_printf (scope, gdbarch, 0, 0,
2543 format_start, format_end - format_start,
8e481c3b 2544 argvec.size (), argvec.data ());
d3ce09f5 2545 }
230d2906 2546 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2547 {
2548 /* If we got here, it means the command could not be parsed to a valid
2549 bytecode expression and thus can't be evaluated on the target's side.
2550 It's no use iterating through the other commands. */
d3ce09f5 2551 }
492d29ea 2552
d3ce09f5
SS
2553 /* We have a valid agent expression, return it. */
2554 return aexpr;
2555}
2556
2557/* Based on location BL, create a list of breakpoint commands to be
2558 passed on to the target. If we have duplicated locations with
2559 different commands, we will add any such to the list. */
2560
2561static void
2562build_target_command_list (struct bp_location *bl)
2563{
d8de7963 2564 bool null_command_or_parse_error = false;
d3ce09f5 2565 int modified = bl->needs_update;
d3ce09f5 2566
3cde5c42
PA
2567 /* Clear commands left over from a previous insert. */
2568 bl->target_info.tcommands.clear ();
8b4f3082 2569
41fac0cf 2570 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2571 return;
2572
41fac0cf
PA
2573 /* For now, limit to agent-style dprintf breakpoints. */
2574 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2575 return;
2576
e0d9a270
SM
2577 auto loc_range = all_bp_locations_at_addr (bl->address);
2578
7f32a4d5
PA
2579 /* For now, if we have any location at the same address that isn't a
2580 dprintf, don't install the target-side commands, as that would
2581 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2582 control. */
e0d9a270
SM
2583 for (bp_location *loc : loc_range)
2584 if (is_breakpoint (loc->owner)
2585 && loc->pspace->num == bl->pspace->num
2586 && loc->owner->type != bp_dprintf)
2587 return;
41fac0cf 2588
d3ce09f5
SS
2589 /* Do a first pass to check for locations with no assigned
2590 conditions or conditions that fail to parse to a valid agent expression
2591 bytecode. If any of these happen, then it's no use to send conditions
2592 to the target since this location will always trigger and generate a
2593 response back to GDB. */
e0d9a270 2594 for (bp_location *loc : loc_range)
d3ce09f5 2595 {
d3ce09f5
SS
2596 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2597 {
2598 if (modified)
2599 {
d3ce09f5
SS
2600 /* Re-parse the commands since something changed. In that
2601 case we already freed the command bytecodes (see
2602 force_breakpoint_reinsertion). We just
2603 need to parse the command to bytecodes again. */
833177a4
PA
2604 loc->cmd_bytecode
2605 = parse_cmd_to_aexpr (bl->address,
6f781ee3 2606 loc->owner->extra_string.get ());
d3ce09f5
SS
2607 }
2608
2609 /* If we have a NULL bytecode expression, it means something
2610 went wrong or we have a null command expression. */
2611 if (!loc->cmd_bytecode)
2612 {
d8de7963 2613 null_command_or_parse_error = true;
d3ce09f5
SS
2614 break;
2615 }
2616 }
2617 }
2618
2619 /* If anything failed, then we're not doing target-side commands,
2620 and so clean up. */
2621 if (null_command_or_parse_error)
2622 {
e0d9a270
SM
2623 for (bp_location *loc : loc_range)
2624 if (is_breakpoint (loc->owner)
2625 && loc->pspace->num == bl->pspace->num)
2626 {
2627 /* Only go as far as the first NULL bytecode is
2628 located. */
2629 if (loc->cmd_bytecode == NULL)
2630 return;
d3ce09f5 2631
e0d9a270
SM
2632 loc->cmd_bytecode.reset ();
2633 }
d3ce09f5
SS
2634 }
2635
7f32a4d5
PA
2636 /* No NULL commands or failed bytecode generation. Build a command
2637 list for all duplicate locations at this location's address.
2638 Note that here we must care for whether the breakpoint location
2639 types are considered duplicates, otherwise, say, if we have a
2640 software and hardware location at the same address, the target
2641 could end up running the commands twice. For the moment, we only
2642 support targets-side commands with dprintf, but it doesn't hurt
2643 to be pedantically correct in case that changes. */
e0d9a270
SM
2644 for (bp_location *loc : loc_range)
2645 if (breakpoint_locations_match (bl, loc)
2646 && loc->owner->extra_string
2647 && is_breakpoint (loc->owner)
2648 && loc->pspace->num == bl->pspace->num
2649 && loc->owner->enable_state == bp_enabled
2650 && loc->enabled
2651 && !loc->disabled_by_cond)
2652 {
2653 /* Add the command to the vector. This will be used later
2654 to send the commands to the target. */
2655 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2656 }
d3ce09f5
SS
2657
2658 bl->target_info.persist = 0;
2659 /* Maybe flag this location as persistent. */
2660 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2661 bl->target_info.persist = 1;
2662}
2663
833b7ab5
YQ
2664/* Return the kind of breakpoint on address *ADDR. Get the kind
2665 of breakpoint according to ADDR except single-step breakpoint.
2666 Get the kind of single-step breakpoint according to the current
2667 registers state. */
cd6c3b4f
YQ
2668
2669static int
b2ff9ed3 2670breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
cd6c3b4f 2671{
833b7ab5
YQ
2672 if (bl->owner->type == bp_single_step)
2673 {
2674 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2675 struct regcache *regcache;
2676
00431a78 2677 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2678
2679 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2680 regcache, addr);
2681 }
2682 else
2683 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2684}
2685
47e2c30a
TV
2686/* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2687 E is either the currently handled exception, or a copy, or a sliced copy,
2688 so we can't rethrow that one, but we can use it to inspect the properties
2689 of the currently handled exception. */
2690
2691static void
2692rethrow_on_target_close_error (const gdb_exception &e)
2693{
2694 if (e.reason == 0)
2695 return;
2696 /* Can't set the breakpoint. */
2697
2698 if (e.error != TARGET_CLOSE_ERROR)
2699 return;
2700
2701 /* If the target has closed then it will have deleted any breakpoints
2702 inserted within the target inferior, as a result any further attempts
2703 to interact with the breakpoint objects is not possible. Just rethrow
2704 the error. Don't use e to rethrow, to prevent object slicing of the
2705 exception. */
2706 throw;
2707}
b2829fcf 2708
35df4500
TJB
2709/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2710 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2711 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2712 Returns 0 for success, 1 if the bp_location type is not supported or
2713 -1 for failure.
879bfdc2 2714
4a64f543
MS
2715 NOTE drow/2003-09-09: This routine could be broken down to an
2716 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2717static int
35df4500 2718insert_bp_location (struct bp_location *bl,
26bb91f3 2719 struct ui_file *tmp_error_stream,
3fbb6ffa 2720 int *disabled_breaks,
dd61ec5c
MW
2721 int *hw_breakpoint_error,
2722 int *hw_bp_error_explained_already)
879bfdc2 2723{
cc06b668 2724 gdb_exception bp_excpt;
879bfdc2 2725
b775012e 2726 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2727 return 0;
2728
35c63cd8
JB
2729 /* Note we don't initialize bl->target_info, as that wipes out
2730 the breakpoint location's shadow_contents if the breakpoint
2731 is still inserted at that location. This in turn breaks
2732 target_read_memory which depends on these buffers when
2733 a memory read is requested at the breakpoint location:
2734 Once the target_info has been wiped, we fail to see that
2735 we have a breakpoint inserted at that address and thus
2736 read the breakpoint instead of returning the data saved in
2737 the breakpoint location's shadow contents. */
0d5ed153 2738 bl->target_info.reqstd_address = bl->address;
35df4500 2739 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2740 bl->target_info.length = bl->length;
8181d85f 2741
b775012e
LM
2742 /* When working with target-side conditions, we must pass all the conditions
2743 for the same breakpoint address down to the target since GDB will not
2744 insert those locations. With a list of breakpoint conditions, the target
2745 can decide when to stop and notify GDB. */
2746
2747 if (is_breakpoint (bl->owner))
2748 {
2749 build_target_condition_list (bl);
d3ce09f5
SS
2750 build_target_command_list (bl);
2751 /* Reset the modification marker. */
b775012e
LM
2752 bl->needs_update = 0;
2753 }
2754
7f32a4d5
PA
2755 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2756 set at a read-only address, then a breakpoint location will have
2757 been changed to hardware breakpoint before we get here. If it is
2758 "off" however, error out before actually trying to insert the
2759 breakpoint, with a nicer error message. */
35df4500 2760 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2761 && !automatic_hardware_breakpoints)
879bfdc2 2762 {
7f32a4d5 2763 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2764
7f32a4d5
PA
2765 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2766 {
6cb06a8c
TT
2767 gdb_printf (tmp_error_stream,
2768 _("Cannot insert breakpoint %d.\n"
2769 "Cannot set software breakpoint "
2770 "at read-only address %s\n"),
2771 bl->owner->number,
2772 paddress (bl->gdbarch, bl->address));
7f32a4d5 2773 return 1;
765dc015 2774 }
7f32a4d5
PA
2775 }
2776
2777 if (bl->loc_type == bp_loc_software_breakpoint
2778 || bl->loc_type == bp_loc_hardware_breakpoint)
2779 {
879bfdc2
DJ
2780 /* First check to see if we have to handle an overlay. */
2781 if (overlay_debugging == ovly_off
35df4500
TJB
2782 || bl->section == NULL
2783 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2784 {
2785 /* No overlay handling: just set the breakpoint. */
a70b8144 2786 try
dd61ec5c 2787 {
0000e5cc
PA
2788 int val;
2789
c47614fe 2790 val = bl->owner->insert_location (bl);
0000e5cc 2791 if (val)
688fca4f 2792 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2793 }
94aeb44b 2794 catch (gdb_exception &e)
dd61ec5c 2795 {
47e2c30a 2796 rethrow_on_target_close_error (e);
94aeb44b 2797 bp_excpt = std::move (e);
dd61ec5c 2798 }
879bfdc2
DJ
2799 }
2800 else
2801 {
4a64f543 2802 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2803 Shall we set a breakpoint at the LMA? */
2804 if (!overlay_events_enabled)
2805 {
2806 /* Yes -- overlay event support is not active,
2807 so we must try to set a breakpoint at the LMA.
2808 This will not work for a hardware breakpoint. */
35df4500 2809 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2810 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2811 bl->owner->number);
879bfdc2
DJ
2812 else
2813 {
35df4500
TJB
2814 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2815 bl->section);
879bfdc2 2816 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2817 bl->overlay_target_info = bl->target_info;
0d5ed153 2818 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2819
2820 /* No overlay handling: just set the breakpoint. */
a70b8144 2821 try
0000e5cc
PA
2822 {
2823 int val;
2824
579c6ad9 2825 bl->overlay_target_info.kind
cd6c3b4f
YQ
2826 = breakpoint_kind (bl, &addr);
2827 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2828 val = target_insert_breakpoint (bl->gdbarch,
2829 &bl->overlay_target_info);
2830 if (val)
688fca4f
PA
2831 bp_excpt
2832 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2833 }
94aeb44b 2834 catch (gdb_exception &e)
0000e5cc 2835 {
47e2c30a 2836 rethrow_on_target_close_error (e);
94aeb44b 2837 bp_excpt = std::move (e);
0000e5cc
PA
2838 }
2839
688fca4f 2840 if (bp_excpt.reason != 0)
6cb06a8c
TT
2841 gdb_printf (tmp_error_stream,
2842 "Overlay breakpoint %d "
2843 "failed: in ROM?\n",
2844 bl->owner->number);
879bfdc2
DJ
2845 }
2846 }
2847 /* Shall we set a breakpoint at the VMA? */
35df4500 2848 if (section_is_mapped (bl->section))
879bfdc2
DJ
2849 {
2850 /* Yes. This overlay section is mapped into memory. */
a70b8144 2851 try
dda83cd7 2852 {
0000e5cc
PA
2853 int val;
2854
c47614fe 2855 val = bl->owner->insert_location (bl);
0000e5cc 2856 if (val)
688fca4f 2857 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2858 }
b1ffd112 2859 catch (gdb_exception_error &e)
dda83cd7 2860 {
47e2c30a 2861 rethrow_on_target_close_error (e);
94aeb44b 2862 bp_excpt = std::move (e);
dda83cd7 2863 }
879bfdc2
DJ
2864 }
2865 else
2866 {
2867 /* No. This breakpoint will not be inserted.
2868 No error, but do not mark the bp as 'inserted'. */
2869 return 0;
2870 }
2871 }
2872
688fca4f 2873 if (bp_excpt.reason != 0)
879bfdc2
DJ
2874 {
2875 /* Can't set the breakpoint. */
80dc83fd
AB
2876 gdb_assert (bl->owner != nullptr);
2877
0000e5cc
PA
2878 /* In some cases, we might not be able to insert a
2879 breakpoint in a shared library that has already been
2880 removed, but we have not yet processed the shlib unload
2881 event. Unfortunately, some targets that implement
076855f9
PA
2882 breakpoint insertion themselves can't tell why the
2883 breakpoint insertion failed (e.g., the remote target
2884 doesn't define error codes), so we must treat generic
2885 errors as memory errors. */
688fca4f
PA
2886 if (bp_excpt.reason == RETURN_ERROR
2887 && (bp_excpt.error == GENERIC_ERROR
2888 || bp_excpt.error == MEMORY_ERROR)
076855f9 2889 && bl->loc_type == bp_loc_software_breakpoint
08351840 2890 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2891 || shared_objfile_contains_address_p (bl->pspace,
2892 bl->address)))
879bfdc2 2893 {
4a64f543 2894 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2895 bl->shlib_disabled = 1;
76727919 2896 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2897 if (!*disabled_breaks)
2898 {
6cb06a8c
TT
2899 gdb_printf (tmp_error_stream,
2900 "Cannot insert breakpoint %d.\n",
2901 bl->owner->number);
2902 gdb_printf (tmp_error_stream,
2903 "Temporarily disabling shared "
2904 "library breakpoints:\n");
3fbb6ffa
TJB
2905 }
2906 *disabled_breaks = 1;
6cb06a8c
TT
2907 gdb_printf (tmp_error_stream,
2908 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2909 return 0;
879bfdc2
DJ
2910 }
2911 else
879bfdc2 2912 {
35df4500 2913 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2914 {
0000e5cc 2915 *hw_breakpoint_error = 1;
688fca4f 2916 *hw_bp_error_explained_already = bp_excpt.message != NULL;
6cb06a8c
TT
2917 gdb_printf (tmp_error_stream,
2918 "Cannot insert hardware breakpoint %d%s",
2919 bl->owner->number,
2920 bp_excpt.message ? ":" : ".\n");
dda83cd7 2921 if (bp_excpt.message != NULL)
6cb06a8c
TT
2922 gdb_printf (tmp_error_stream, "%s.\n",
2923 bp_excpt.what ());
879bfdc2
DJ
2924 }
2925 else
2926 {
688fca4f 2927 if (bp_excpt.message == NULL)
0000e5cc 2928 {
1ccbe998 2929 std::string message
0000e5cc
PA
2930 = memory_error_message (TARGET_XFER_E_IO,
2931 bl->gdbarch, bl->address);
0000e5cc 2932
6cb06a8c
TT
2933 gdb_printf (tmp_error_stream,
2934 "Cannot insert breakpoint %d.\n"
2935 "%s\n",
2936 bl->owner->number, message.c_str ());
0000e5cc
PA
2937 }
2938 else
2939 {
6cb06a8c
TT
2940 gdb_printf (tmp_error_stream,
2941 "Cannot insert breakpoint %d: %s\n",
2942 bl->owner->number,
2943 bp_excpt.what ());
0000e5cc 2944 }
879bfdc2 2945 }
0000e5cc 2946 return 1;
879bfdc2
DJ
2947
2948 }
2949 }
2950 else
35df4500 2951 bl->inserted = 1;
879bfdc2 2952
0000e5cc 2953 return 0;
879bfdc2
DJ
2954 }
2955
35df4500 2956 else if (bl->loc_type == bp_loc_hardware_watchpoint
35df4500 2957 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2958 {
0000e5cc
PA
2959 int val;
2960
c47614fe 2961 val = bl->owner->insert_location (bl);
85d721b8
PA
2962
2963 /* If trying to set a read-watchpoint, and it turns out it's not
2964 supported, try emulating one with an access watchpoint. */
35df4500 2965 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2966 {
85d721b8
PA
2967 /* But don't try to insert it, if there's already another
2968 hw_access location that would be considered a duplicate
2969 of this one. */
48d7020b 2970 for (bp_location *loc : all_bp_locations ())
35df4500 2971 if (loc != bl
85d721b8 2972 && loc->watchpoint_type == hw_access
35df4500 2973 && watchpoint_locations_match (bl, loc))
85d721b8 2974 {
35df4500
TJB
2975 bl->duplicate = 1;
2976 bl->inserted = 1;
2977 bl->target_info = loc->target_info;
2978 bl->watchpoint_type = hw_access;
85d721b8
PA
2979 val = 0;
2980 break;
2981 }
2982
2983 if (val == 1)
2984 {
77b06cd7 2985 bl->watchpoint_type = hw_access;
c47614fe 2986 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2987
2988 if (val)
2989 /* Back to the original value. */
2990 bl->watchpoint_type = hw_read;
85d721b8
PA
2991 }
2992 }
2993
35df4500 2994 bl->inserted = (val == 0);
879bfdc2
DJ
2995 }
2996
35df4500 2997 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2998 {
0000e5cc
PA
2999 int val;
3000
c47614fe 3001 val = bl->owner->insert_location (bl);
77b06cd7
TJB
3002 if (val)
3003 {
3004 bl->owner->enable_state = bp_disabled;
3005
3006 if (val == 1)
3007 warning (_("\
3008Error inserting catchpoint %d: Your system does not support this type\n\
3009of catchpoint."), bl->owner->number);
3010 else
3011 warning (_("Error inserting catchpoint %d."), bl->owner->number);
3012 }
3013
3014 bl->inserted = (val == 0);
1640b821
DJ
3015
3016 /* We've already printed an error message if there was a problem
3017 inserting this catchpoint, and we've disabled the catchpoint,
3018 so just return success. */
3019 return 0;
879bfdc2
DJ
3020 }
3021
3022 return 0;
3023}
3024
6c95b8df
PA
3025/* This function is called when program space PSPACE is about to be
3026 deleted. It takes care of updating breakpoints to not reference
3027 PSPACE anymore. */
3028
3029void
3030breakpoint_program_space_exit (struct program_space *pspace)
3031{
6c95b8df 3032 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
3033 for (breakpoint *b : all_breakpoints_safe ())
3034 if (b->pspace == pspace)
3035 delete_breakpoint (b);
6c95b8df
PA
3036
3037 /* Breakpoints set through other program spaces could have locations
3038 bound to PSPACE as well. Remove those. */
48d7020b 3039 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
3040 {
3041 struct bp_location *tmp;
3042
3043 if (loc->pspace == pspace)
3044 {
2bdf28a0 3045 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3046 if (loc->owner->loc == loc)
3047 loc->owner->loc = loc->next;
3048 else
3049 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3050 if (tmp->next == loc)
3051 {
3052 tmp->next = loc->next;
3053 break;
3054 }
3055 }
3056 }
3057
3058 /* Now update the global location list to permanently delete the
3059 removed locations above. */
44702360 3060 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3061}
3062
74960c60
VP
3063/* Make sure all breakpoints are inserted in inferior.
3064 Throws exception on any error.
3065 A breakpoint that is already inserted won't be inserted
3066 again, so calling this function twice is safe. */
3067void
3068insert_breakpoints (void)
3069{
43892fdf 3070 for (breakpoint *bpt : all_breakpoints ())
74960c60 3071 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3072 {
3073 struct watchpoint *w = (struct watchpoint *) bpt;
3074
d8de7963 3075 update_watchpoint (w, false /* don't reparse. */);
3a5c3e22 3076 }
74960c60 3077
04086b45
PA
3078 /* Updating watchpoints creates new locations, so update the global
3079 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3080 ignore breakpoints_always_inserted_mode. Also,
3081 update_global_location_list tries to "upgrade" software
3082 breakpoints to hardware breakpoints to handle "set breakpoint
3083 auto-hw", so we need to call it even if we don't have new
3084 locations. */
04086b45 3085 update_global_location_list (UGLL_INSERT);
74960c60
VP
3086}
3087
b775012e
LM
3088/* This is used when we need to synch breakpoint conditions between GDB and the
3089 target. It is the case with deleting and disabling of breakpoints when using
3090 always-inserted mode. */
3091
3092static void
3093update_inserted_breakpoint_locations (void)
3094{
b775012e
LM
3095 int error_flag = 0;
3096 int val = 0;
3097 int disabled_breaks = 0;
3098 int hw_breakpoint_error = 0;
dd61ec5c 3099 int hw_bp_details_reported = 0;
b775012e 3100
d7e74731 3101 string_file tmp_error_stream;
b775012e
LM
3102
3103 /* Explicitly mark the warning -- this will only be printed if
3104 there was an error. */
d7e74731 3105 tmp_error_stream.puts ("Warning:\n");
b775012e 3106
5ed8105e 3107 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3108
48d7020b 3109 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3110 {
3111 /* We only want to update software breakpoints and hardware
3112 breakpoints. */
3113 if (!is_breakpoint (bl->owner))
3114 continue;
3115
3116 /* We only want to update locations that are already inserted
3117 and need updating. This is to avoid unwanted insertion during
3118 deletion of breakpoints. */
4daf1902 3119 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3120 continue;
3121
3122 switch_to_program_space_and_thread (bl->pspace);
3123
3124 /* For targets that support global breakpoints, there's no need
3125 to select an inferior to insert breakpoint to. In fact, even
3126 if we aren't attached to any process yet, we should still
3127 insert breakpoints. */
f5656ead 3128 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3129 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3130 continue;
3131
d7e74731 3132 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3133 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3134 if (val)
3135 error_flag = val;
3136 }
3137
3138 if (error_flag)
3139 {
223ffa71 3140 target_terminal::ours_for_output ();
b775012e
LM
3141 error_stream (tmp_error_stream);
3142 }
b775012e
LM
3143}
3144
c30eee59 3145/* Used when starting or continuing the program. */
c906108c 3146
74960c60
VP
3147static void
3148insert_breakpoint_locations (void)
c906108c 3149{
eacd795a 3150 int error_flag = 0;
c906108c 3151 int val = 0;
3fbb6ffa 3152 int disabled_breaks = 0;
81d0cc19 3153 int hw_breakpoint_error = 0;
dd61ec5c 3154 int hw_bp_error_explained_already = 0;
c906108c 3155
d7e74731
PA
3156 string_file tmp_error_stream;
3157
81d0cc19
GS
3158 /* Explicitly mark the warning -- this will only be printed if
3159 there was an error. */
d7e74731 3160 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3161
5ed8105e 3162 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3163
48d7020b 3164 for (bp_location *bl : all_bp_locations ())
879bfdc2 3165 {
b775012e 3166 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3167 continue;
3168
4a64f543
MS
3169 /* There is no point inserting thread-specific breakpoints if
3170 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3171 has BL->OWNER always non-NULL. */
35df4500 3172 if (bl->owner->thread != -1
5d5658a1 3173 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3174 continue;
3175
35df4500 3176 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3177
3178 /* For targets that support global breakpoints, there's no need
3179 to select an inferior to insert breakpoint to. In fact, even
3180 if we aren't attached to any process yet, we should still
3181 insert breakpoints. */
f5656ead 3182 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3183 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3184 continue;
3185
d7e74731 3186 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3187 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3188 if (val)
eacd795a 3189 error_flag = val;
879bfdc2 3190 }
c906108c 3191
4a64f543
MS
3192 /* If we failed to insert all locations of a watchpoint, remove
3193 them, as half-inserted watchpoint is of limited use. */
43892fdf 3194 for (breakpoint *bpt : all_breakpoints ())
a5606eee 3195 {
d8de7963 3196 bool some_failed = false;
a5606eee
VP
3197
3198 if (!is_hardware_watchpoint (bpt))
3199 continue;
3200
d6b74ac4 3201 if (!breakpoint_enabled (bpt))
a5606eee 3202 continue;
74960c60
VP
3203
3204 if (bpt->disposition == disp_del_at_next_stop)
3205 continue;
a5606eee 3206
40cb8ca5 3207 for (bp_location *loc : bpt->locations ())
56710373 3208 if (!loc->inserted && should_be_inserted (loc))
a5606eee 3209 {
d8de7963 3210 some_failed = true;
a5606eee
VP
3211 break;
3212 }
40cb8ca5 3213
a5606eee
VP
3214 if (some_failed)
3215 {
40cb8ca5 3216 for (bp_location *loc : bpt->locations ())
a5606eee 3217 if (loc->inserted)
834c0d03 3218 remove_breakpoint (loc);
a5606eee
VP
3219
3220 hw_breakpoint_error = 1;
d7e74731
PA
3221 tmp_error_stream.printf ("Could not insert "
3222 "hardware watchpoint %d.\n",
3223 bpt->number);
eacd795a 3224 error_flag = -1;
a5606eee
VP
3225 }
3226 }
3227
eacd795a 3228 if (error_flag)
81d0cc19
GS
3229 {
3230 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3231 message about possibly exhausted resources. */
dd61ec5c 3232 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3233 {
d7e74731 3234 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3235You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3236 }
223ffa71 3237 target_terminal::ours_for_output ();
81d0cc19
GS
3238 error_stream (tmp_error_stream);
3239 }
c906108c
SS
3240}
3241
c30eee59
TJB
3242/* Used when the program stops.
3243 Returns zero if successful, or non-zero if there was a problem
3244 removing a breakpoint location. */
3245
c906108c 3246int
fba45db2 3247remove_breakpoints (void)
c906108c 3248{
3a1bae8e 3249 int val = 0;
c906108c 3250
48d7020b 3251 for (bp_location *bl : all_bp_locations ())
1e4d1764 3252 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3253 val |= remove_breakpoint (bl);
01add95b 3254
3a1bae8e 3255 return val;
c906108c
SS
3256}
3257
49fa26b0
PA
3258/* When a thread exits, remove breakpoints that are related to
3259 that thread. */
3260
3261static void
3262remove_threaded_breakpoints (struct thread_info *tp, int silent)
3263{
1428b37a 3264 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3265 {
5d5658a1 3266 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0 3267 {
6cb06a8c 3268 gdb_printf (_("\
43792cf0 3269Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
6cb06a8c 3270 b->number, print_thread_id (tp));
2968b79f 3271 delete_breakpoint (b);
49fa26b0
PA
3272 }
3273 }
3274}
3275
f3869b1a 3276/* See breakpoint.h. */
6c95b8df 3277
f3869b1a 3278void
00431a78 3279remove_breakpoints_inf (inferior *inf)
6c95b8df 3280{
6c95b8df 3281 int val;
6c95b8df 3282
48d7020b 3283 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3284 {
3285 if (bl->pspace != inf->pspace)
3286 continue;
6c95b8df 3287
01add95b
SM
3288 if (bl->inserted && !bl->target_info.persist)
3289 {
3290 val = remove_breakpoint (bl);
3291 if (val != 0)
3292 return;
3293 }
3294 }
6c95b8df
PA
3295}
3296
e58b0e63
PA
3297static int internal_breakpoint_number = -1;
3298
84f4c1fe
PM
3299/* Set the breakpoint number of B, depending on the value of INTERNAL.
3300 If INTERNAL is non-zero, the breakpoint number will be populated
3301 from internal_breakpoint_number and that variable decremented.
e5dd4106 3302 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3303 breakpoint_count and that value incremented. Internal breakpoints
3304 do not set the internal var bpnum. */
3305static void
3306set_breakpoint_number (int internal, struct breakpoint *b)
3307{
3308 if (internal)
3309 b->number = internal_breakpoint_number--;
3310 else
3311 {
3312 set_breakpoint_count (breakpoint_count + 1);
3313 b->number = breakpoint_count;
3314 }
3315}
3316
829b6b37
TV
3317/* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3318
e62c965a 3319static struct breakpoint *
a6d9a66e 3320create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3321 CORE_ADDR address, enum bptype type)
e62c965a 3322{
752a2291
PA
3323 std::unique_ptr<internal_breakpoint> b
3324 (new internal_breakpoint (gdbarch, type, address));
e62c965a 3325
e62c965a 3326 b->number = internal_breakpoint_number--;
e62c965a 3327
752a2291 3328 return add_to_breakpoint_chain (std::move (b));
e62c965a
PP
3329}
3330
829b6b37
TV
3331/* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3332 GDBARCH. */
3333
3334static struct breakpoint *
3335create_internal_breakpoint (struct gdbarch *gdbarch,
3336 struct bound_minimal_symbol &msym, enum bptype type)
3337{
3338 CORE_ADDR address;
3339
3340 address = msym.value_address ();
3341
3342 address = gdbarch_convert_from_func_ptr_addr
3343 (gdbarch, address, current_inferior ()->top_target ());
3344
3345 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3346 related to addresses in $pc. We're getting the address from the
3347 minimal symbol table. */
3348
3349 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3350 with elsewhere? Needs testing on vax. */
3351
3352 if (gdbarch_skip_entrypoint_p (gdbarch))
3353 address = gdbarch_skip_entrypoint (gdbarch, address);
3354
3355 return create_internal_breakpoint (gdbarch, address, type);
3356}
3357
17450429
PP
3358static const char *const longjmp_names[] =
3359 {
3360 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3361 };
3362#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3363
3364/* Per-objfile data private to breakpoint.c. */
3365struct breakpoint_objfile_data
3366{
3367 /* Minimal symbol for "_ovly_debug_event" (if any). */
f6b3ad54 3368 struct bound_minimal_symbol overlay_msym;
17450429
PP
3369
3370 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
f6b3ad54 3371 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3372
28106bc2 3373 /* True if we have looked for longjmp probes. */
43dce439 3374 int longjmp_searched = 0;
28106bc2 3375
45461e0d
SM
3376 /* SystemTap probe points for longjmp (if any). These are non-owning
3377 references. */
3378 std::vector<probe *> longjmp_probes;
28106bc2 3379
17450429 3380 /* Minimal symbol for "std::terminate()" (if any). */
f6b3ad54 3381 struct bound_minimal_symbol terminate_msym;
17450429
PP
3382
3383 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
f6b3ad54 3384 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3385
3386 /* True if we have looked for exception probes. */
43dce439 3387 int exception_searched = 0;
28106bc2 3388
45461e0d
SM
3389 /* SystemTap probe points for unwinding (if any). These are non-owning
3390 references. */
3391 std::vector<probe *> exception_probes;
17450429
PP
3392};
3393
08b8a139 3394static const registry<objfile>::key<breakpoint_objfile_data>
51d3063a 3395 breakpoint_objfile_key;
17450429
PP
3396
3397/* Minimal symbol not found sentinel. */
3398static struct minimal_symbol msym_not_found;
3399
3400/* Returns TRUE if MSYM point to the "not found" sentinel. */
3401
d8de7963 3402static bool
17450429
PP
3403msym_not_found_p (const struct minimal_symbol *msym)
3404{
3405 return msym == &msym_not_found;
3406}
3407
3408/* Return per-objfile data needed by breakpoint.c.
3409 Allocate the data if necessary. */
3410
3411static struct breakpoint_objfile_data *
3412get_breakpoint_objfile_data (struct objfile *objfile)
3413{
3414 struct breakpoint_objfile_data *bp_objfile_data;
3415
51d3063a 3416 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3417 if (bp_objfile_data == NULL)
51d3063a 3418 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3419 return bp_objfile_data;
3420}
3421
e62c965a 3422static void
af02033e 3423create_overlay_event_breakpoint (void)
e62c965a 3424{
af02033e 3425 const char *const func_name = "_ovly_debug_event";
e62c965a 3426
2030c079 3427 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3428 {
3429 struct breakpoint *b;
17450429
PP
3430 struct breakpoint_objfile_data *bp_objfile_data;
3431 CORE_ADDR addr;
69de3c6a 3432
17450429
PP
3433 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3434
3b7344d5 3435 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3436 continue;
3437
3b7344d5 3438 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3439 {
3b7344d5 3440 struct bound_minimal_symbol m;
17450429
PP
3441
3442 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3443 if (m.minsym == NULL)
17450429
PP
3444 {
3445 /* Avoid future lookups in this objfile. */
3b7344d5 3446 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3447 continue;
3448 }
3449 bp_objfile_data->overlay_msym = m;
3450 }
e62c965a 3451
4aeddc50 3452 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3453 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3454 bp_overlay_event);
40d97ee2 3455 b->locspec = new_explicit_location_spec_function (func_name);
e62c965a 3456
69de3c6a 3457 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3458 {
3459 b->enable_state = bp_enabled;
3460 overlay_events_enabled = 1;
3461 }
69de3c6a
PP
3462 else
3463 {
dda83cd7
SM
3464 b->enable_state = bp_disabled;
3465 overlay_events_enabled = 0;
69de3c6a 3466 }
e62c965a 3467 }
e62c965a
PP
3468}
3469
2a7f6487
TV
3470/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3471 true if a breakpoint was installed. */
3472
3473static bool
3474create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3475{
2a7f6487
TV
3476 struct gdbarch *gdbarch = objfile->arch ();
3477 struct breakpoint_objfile_data *bp_objfile_data
3478 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3479
2a7f6487 3480 if (!bp_objfile_data->longjmp_searched)
94c93c35 3481 {
2a7f6487
TV
3482 std::vector<probe *> ret
3483 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3484
2a7f6487 3485 if (!ret.empty ())
94c93c35 3486 {
2a7f6487
TV
3487 /* We are only interested in checking one element. */
3488 probe *p = ret[0];
0fd8e87f 3489
2a7f6487
TV
3490 if (!p->can_evaluate_arguments ())
3491 {
3492 /* We cannot use the probe interface here,
3493 because it does not know how to evaluate
3494 arguments. */
3495 ret.clear ();
3496 }
3497 }
3498 bp_objfile_data->longjmp_probes = ret;
3499 bp_objfile_data->longjmp_searched = 1;
3500 }
0fd8e87f 3501
2a7f6487
TV
3502 if (bp_objfile_data->longjmp_probes.empty ())
3503 return false;
17450429 3504
2a7f6487
TV
3505 for (probe *p : bp_objfile_data->longjmp_probes)
3506 {
3507 struct breakpoint *b;
25f9533e 3508
2a7f6487
TV
3509 b = create_internal_breakpoint (gdbarch,
3510 p->get_relocated_address (objfile),
9efa3c7f 3511 bp_longjmp_master);
264f9890 3512 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
2a7f6487
TV
3513 b->enable_state = bp_disabled;
3514 }
aed57c53 3515
2a7f6487
TV
3516 return true;
3517}
25f9533e 3518
2a7f6487
TV
3519/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3520 Return true if at least one breakpoint was installed. */
3521
3522static bool
3523create_longjmp_master_breakpoint_names (objfile *objfile)
3524{
3525 struct gdbarch *gdbarch = objfile->arch ();
3526 if (!gdbarch_get_longjmp_target_p (gdbarch))
3527 return false;
3528
3529 struct breakpoint_objfile_data *bp_objfile_data
3530 = get_breakpoint_objfile_data (objfile);
3531 unsigned int installed_bp = 0;
3532
3533 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3534 {
3535 struct breakpoint *b;
3536 const char *func_name;
3537 CORE_ADDR addr;
2a7f6487
TV
3538
3539 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3540 continue;
28106bc2 3541
2a7f6487
TV
3542 func_name = longjmp_names[i];
3543 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3544 {
3545 struct bound_minimal_symbol m;
3546
3547 m = lookup_minimal_symbol_text (func_name, objfile);
3548 if (m.minsym == NULL)
3549 {
3550 /* Prevent future lookups in this objfile. */
3551 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3552 continue;
3553 }
2a7f6487
TV
3554 bp_objfile_data->longjmp_msym[i] = m;
3555 }
28106bc2 3556
4aeddc50 3557 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3558 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
40d97ee2 3559 b->locspec = new_explicit_location_spec_function (func_name);
2a7f6487
TV
3560 b->enable_state = bp_disabled;
3561 installed_bp++;
3562 }
28106bc2 3563
2a7f6487
TV
3564 return installed_bp > 0;
3565}
0fd8e87f 3566
2a7f6487 3567/* Create a master longjmp breakpoint. */
17450429 3568
2a7f6487
TV
3569static void
3570create_longjmp_master_breakpoint (void)
3571{
3572 scoped_restore_current_program_space restore_pspace;
aed57c53 3573
2a7f6487
TV
3574 for (struct program_space *pspace : program_spaces)
3575 {
3576 set_current_program_space (pspace);
17450429 3577
2a7f6487
TV
3578 for (objfile *obj : current_program_space->objfiles ())
3579 {
3580 /* Skip separate debug object, it's handled in the loop below. */
3581 if (obj->separate_debug_objfile_backlink != nullptr)
3582 continue;
3583
3584 /* Try a probe kind breakpoint on main objfile. */
3585 if (create_longjmp_master_breakpoint_probe (obj))
3586 continue;
3587
3588 /* Try longjmp_names kind breakpoints on main and separate_debug
3589 objfiles. */
3590 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3591 if (create_longjmp_master_breakpoint_names (debug_objfile))
3592 break;
94c93c35
TT
3593 }
3594 }
0fd8e87f
UW
3595}
3596
af02033e 3597/* Create a master std::terminate breakpoint. */
aa7d318d 3598static void
af02033e 3599create_std_terminate_master_breakpoint (void)
aa7d318d 3600{
af02033e 3601 const char *const func_name = "std::terminate()";
aa7d318d 3602
5ed8105e 3603 scoped_restore_current_program_space restore_pspace;
aa7d318d 3604
94c93c35
TT
3605 for (struct program_space *pspace : program_spaces)
3606 {
94c93c35 3607 set_current_program_space (pspace);
17450429 3608
94c93c35
TT
3609 for (objfile *objfile : current_program_space->objfiles ())
3610 {
3611 struct breakpoint *b;
3612 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3613
94c93c35 3614 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3615
94c93c35
TT
3616 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3617 continue;
17450429 3618
94c93c35
TT
3619 if (bp_objfile_data->terminate_msym.minsym == NULL)
3620 {
3621 struct bound_minimal_symbol m;
17450429 3622
94c93c35 3623 m = lookup_minimal_symbol (func_name, NULL, objfile);
60f62e2b
SM
3624 if (m.minsym == NULL || (m.minsym->type () != mst_text
3625 && m.minsym->type () != mst_file_text))
94c93c35
TT
3626 {
3627 /* Prevent future lookups in this objfile. */
3628 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3629 continue;
3630 }
3631 bp_objfile_data->terminate_msym = m;
3632 }
aa7d318d 3633
829b6b37
TV
3634 b = create_internal_breakpoint (objfile->arch (),
3635 bp_objfile_data->terminate_msym,
9efa3c7f 3636 bp_std_terminate_master);
40d97ee2 3637 b->locspec = new_explicit_location_spec_function (func_name);
94c93c35
TT
3638 b->enable_state = bp_disabled;
3639 }
3640 }
aa7d318d
TT
3641}
3642
1940319c
TV
3643/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3644 probe. Return true if a breakpoint was installed. */
186c406b 3645
1940319c
TV
3646static bool
3647create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3648{
1940319c
TV
3649 struct breakpoint *b;
3650 struct gdbarch *gdbarch;
3651 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3652
1940319c 3653 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3654
1940319c
TV
3655 /* We prefer the SystemTap probe point if it exists. */
3656 if (!bp_objfile_data->exception_searched)
3657 {
3658 std::vector<probe *> ret
3659 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3660
1940319c 3661 if (!ret.empty ())
28106bc2 3662 {
1940319c
TV
3663 /* We are only interested in checking one element. */
3664 probe *p = ret[0];
25f9533e 3665
1940319c 3666 if (!p->can_evaluate_arguments ())
25f9533e 3667 {
1940319c
TV
3668 /* We cannot use the probe interface here, because it does
3669 not know how to evaluate arguments. */
3670 ret.clear ();
25f9533e 3671 }
28106bc2 3672 }
1940319c
TV
3673 bp_objfile_data->exception_probes = ret;
3674 bp_objfile_data->exception_searched = 1;
3675 }
28106bc2 3676
1940319c
TV
3677 if (bp_objfile_data->exception_probes.empty ())
3678 return false;
45461e0d 3679
1940319c 3680 gdbarch = objfile->arch ();
28106bc2 3681
1940319c
TV
3682 for (probe *p : bp_objfile_data->exception_probes)
3683 {
3684 b = create_internal_breakpoint (gdbarch,
3685 p->get_relocated_address (objfile),
9efa3c7f 3686 bp_exception_master);
264f9890 3687 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
1940319c
TV
3688 b->enable_state = bp_disabled;
3689 }
28106bc2 3690
1940319c
TV
3691 return true;
3692}
28106bc2 3693
1940319c
TV
3694/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3695 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3696
1940319c
TV
3697static bool
3698create_exception_master_breakpoint_hook (objfile *objfile)
3699{
3700 const char *const func_name = "_Unwind_DebugHook";
3701 struct breakpoint *b;
3702 struct gdbarch *gdbarch;
3703 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3704
1940319c 3705 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3706
1940319c
TV
3707 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3708 return false;
17450429 3709
1940319c
TV
3710 gdbarch = objfile->arch ();
3711
3712 if (bp_objfile_data->exception_msym.minsym == NULL)
3713 {
3714 struct bound_minimal_symbol debug_hook;
3715
3716 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3717 if (debug_hook.minsym == NULL)
3718 {
3719 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3720 return false;
186c406b 3721 }
17450429 3722
1940319c
TV
3723 bp_objfile_data->exception_msym = debug_hook;
3724 }
3725
829b6b37
TV
3726 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3727 bp_exception_master);
40d97ee2 3728 b->locspec = new_explicit_location_spec_function (func_name);
1940319c
TV
3729 b->enable_state = bp_disabled;
3730
3731 return true;
3732}
3733
3734/* Install a master breakpoint on the unwinder's debug hook. */
3735
3736static void
3737create_exception_master_breakpoint (void)
3738{
3739 for (objfile *obj : current_program_space->objfiles ())
3740 {
3741 /* Skip separate debug object. */
3742 if (obj->separate_debug_objfile_backlink)
3743 continue;
3744
3745 /* Try a probe kind breakpoint. */
3746 if (create_exception_master_breakpoint_probe (obj))
3747 continue;
3748
7c6944ab
TV
3749 /* Iterate over main and separate debug objects and try an
3750 _Unwind_DebugHook kind breakpoint. */
3751 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3752 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3753 break;
186c406b 3754 }
186c406b
TT
3755}
3756
9ef9e6a6
KS
3757/* Does B have a location spec? */
3758
d8de7963 3759static bool
264f9890 3760breakpoint_location_spec_empty_p (const struct breakpoint *b)
9ef9e6a6 3761{
238dc9af 3762 return (b->locspec != nullptr && b->locspec->empty_p ());
9ef9e6a6
KS
3763}
3764
c906108c 3765void
fba45db2 3766update_breakpoints_after_exec (void)
c906108c 3767{
25b22b0a
PA
3768 /* We're about to delete breakpoints from GDB's lists. If the
3769 INSERTED flag is true, GDB will try to lift the breakpoints by
3770 writing the breakpoints' "shadow contents" back into memory. The
3771 "shadow contents" are NOT valid after an exec, so GDB should not
3772 do that. Instead, the target is responsible from marking
3773 breakpoints out as soon as it detects an exec. We don't do that
3774 here instead, because there may be other attempts to delete
3775 breakpoints after detecting an exec and before reaching here. */
48d7020b 3776 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3777 if (bploc->pspace == current_program_space)
3778 gdb_assert (!bploc->inserted);
c906108c 3779
1428b37a 3780 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3781 {
3782 if (b->pspace != current_program_space)
c5aa993b 3783 continue;
c906108c 3784
01add95b
SM
3785 /* Solib breakpoints must be explicitly reset after an exec(). */
3786 if (b->type == bp_shlib_event)
3787 {
3788 delete_breakpoint (b);
3789 continue;
3790 }
4efc6507 3791
01add95b
SM
3792 /* JIT breakpoints must be explicitly reset after an exec(). */
3793 if (b->type == bp_jit_event)
3794 {
3795 delete_breakpoint (b);
3796 continue;
3797 }
c4093a6a 3798
01add95b
SM
3799 /* Thread event breakpoints must be set anew after an exec(),
3800 as must overlay event and longjmp master breakpoints. */
3801 if (b->type == bp_thread_event || b->type == bp_overlay_event
3802 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3803 || b->type == bp_exception_master)
3804 {
3805 delete_breakpoint (b);
3806 continue;
3807 }
c5aa993b 3808
01add95b
SM
3809 /* Step-resume breakpoints are meaningless after an exec(). */
3810 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3811 {
3812 delete_breakpoint (b);
3813 continue;
3814 }
7c16b83e 3815
01add95b
SM
3816 /* Just like single-step breakpoints. */
3817 if (b->type == bp_single_step)
3818 {
3819 delete_breakpoint (b);
3820 continue;
3821 }
611c83ae 3822
01add95b
SM
3823 /* Longjmp and longjmp-resume breakpoints are also meaningless
3824 after an exec. */
3825 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3826 || b->type == bp_longjmp_call_dummy
3827 || b->type == bp_exception || b->type == bp_exception_resume)
3828 {
3829 delete_breakpoint (b);
3830 continue;
3831 }
ce78b96d 3832
01add95b
SM
3833 if (b->type == bp_catchpoint)
3834 {
3835 /* For now, none of the bp_catchpoint breakpoints need to
3836 do anything at this point. In the future, if some of
3837 the catchpoints need to something, we will need to add
3838 a new method, and call this method from here. */
3839 continue;
3840 }
c5aa993b 3841
01add95b
SM
3842 /* bp_finish is a special case. The only way we ought to be able
3843 to see one of these when an exec() has happened, is if the user
3844 caught a vfork, and then said "finish". Ordinarily a finish just
3845 carries them to the call-site of the current callee, by setting
3846 a temporary bp there and resuming. But in this case, the finish
3847 will carry them entirely through the vfork & exec.
3848
3849 We don't want to allow a bp_finish to remain inserted now. But
3850 we can't safely delete it, 'cause finish_command has a handle to
3851 the bp on a bpstat, and will later want to delete it. There's a
3852 chance (and I've seen it happen) that if we delete the bp_finish
3853 here, that its storage will get reused by the time finish_command
3854 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3855 We really must allow finish_command to delete a bp_finish.
3856
3857 In the absence of a general solution for the "how do we know
3858 it's safe to delete something others may have handles to?"
3859 problem, what we'll do here is just uninsert the bp_finish, and
3860 let finish_command delete it.
3861
3862 (We know the bp_finish is "doomed" in the sense that it's
3863 momentary, and will be deleted as soon as finish_command sees
3864 the inferior stopped. So it doesn't matter that the bp's
3865 address is probably bogus in the new a.out, unlike e.g., the
3866 solib breakpoints.) */
3867
3868 if (b->type == bp_finish)
3869 {
3870 continue;
3871 }
3872
3873 /* Without a symbolic address, we have little hope of the
3874 pre-exec() address meaning the same thing in the post-exec()
3875 a.out. */
264f9890 3876 if (breakpoint_location_spec_empty_p (b))
01add95b
SM
3877 {
3878 delete_breakpoint (b);
3879 continue;
3880 }
3881 }
c906108c
SS
3882}
3883
3884int
d80ee84f 3885detach_breakpoints (ptid_t ptid)
c906108c 3886{
3a1bae8e 3887 int val = 0;
2989a365 3888 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3889 struct inferior *inf = current_inferior ();
c5aa993b 3890
e99b03dc 3891 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3892 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3893
6c95b8df 3894 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3895 inferior_ptid = ptid;
48d7020b 3896 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3897 {
3898 if (bl->pspace != inf->pspace)
3899 continue;
3900
3901 /* This function must physically remove breakpoints locations
3902 from the specified ptid, without modifying the breakpoint
6e14e441
PA
3903 package's state. Locations of type bp_loc_other and
3904 bp_loc_software_watchpoint are only maintained at GDB side,
3905 so there is no need to remove them. Moreover, removing these
01add95b 3906 would modify the breakpoint package's state. */
6e14e441
PA
3907 if (bl->loc_type == bp_loc_other
3908 || bl->loc_type == bp_loc_software_watchpoint)
01add95b
SM
3909 continue;
3910
3911 if (bl->inserted)
3912 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3913 }
d03285ec 3914
3a1bae8e 3915 return val;
c906108c
SS
3916}
3917
35df4500 3918/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3919 Note that this is used to detach breakpoints from a child fork.
3920 When we get here, the child isn't in the inferior list, and neither
3921 do we have objects to represent its address space --- we should
35df4500 3922 *not* look at bl->pspace->aspace here. */
6c95b8df 3923
c906108c 3924static int
b2b6a7da 3925remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3926{
3927 int val;
c5aa993b 3928
35df4500
TJB
3929 /* BL is never in moribund_locations by our callers. */
3930 gdb_assert (bl->owner != NULL);
2bdf28a0 3931
74960c60
VP
3932 /* The type of none suggests that owner is actually deleted.
3933 This should not ever happen. */
35df4500 3934 gdb_assert (bl->owner->type != bp_none);
0bde7532 3935
35df4500
TJB
3936 if (bl->loc_type == bp_loc_software_breakpoint
3937 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3938 {
c02f5703
MS
3939 /* "Normal" instruction breakpoint: either the standard
3940 trap-instruction bp (bp_breakpoint), or a
3941 bp_hardware_breakpoint. */
3942
3943 /* First check to see if we have to handle an overlay. */
3944 if (overlay_debugging == ovly_off
35df4500
TJB
3945 || bl->section == NULL
3946 || !(section_is_overlay (bl->section)))
c02f5703
MS
3947 {
3948 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3949
3950 /* If we're trying to uninsert a memory breakpoint that we
3951 know is set in a dynamic object that is marked
3952 shlib_disabled, then either the dynamic object was
3953 removed with "remove-symbol-file" or with
3954 "nosharedlibrary". In the former case, we don't know
3955 whether another dynamic object might have loaded over the
3956 breakpoint's address -- the user might well let us know
3957 about it next with add-symbol-file (the whole point of
d03de421 3958 add-symbol-file is letting the user manually maintain a
08351840
PA
3959 list of dynamically loaded objects). If we have the
3960 breakpoint's shadow memory, that is, this is a software
3961 breakpoint managed by GDB, check whether the breakpoint
3962 is still inserted in memory, to avoid overwriting wrong
3963 code with stale saved shadow contents. Note that HW
3964 breakpoints don't have shadow memory, as they're
3965 implemented using a mechanism that is not dependent on
3966 being able to modify the target's memory, and as such
3967 they should always be removed. */
3968 if (bl->shlib_disabled
3969 && bl->target_info.shadow_len != 0
3970 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3971 val = 0;
3972 else
c47614fe 3973 val = bl->owner->remove_location (bl, reason);
c02f5703 3974 }
c906108c
SS
3975 else
3976 {
4a64f543 3977 /* This breakpoint is in an overlay section.
c02f5703
MS
3978 Did we set a breakpoint at the LMA? */
3979 if (!overlay_events_enabled)
3980 {
3981 /* Yes -- overlay event support is not active, so we
3982 should have set a breakpoint at the LMA. Remove it.
3983 */
c02f5703
MS
3984 /* Ignore any failures: if the LMA is in ROM, we will
3985 have already warned when we failed to insert it. */
35df4500
TJB
3986 if (bl->loc_type == bp_loc_hardware_breakpoint)
3987 target_remove_hw_breakpoint (bl->gdbarch,
3988 &bl->overlay_target_info);
c02f5703 3989 else
35df4500 3990 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3991 &bl->overlay_target_info,
3992 reason);
c02f5703
MS
3993 }
3994 /* Did we set a breakpoint at the VMA?
3995 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3996 if (bl->inserted)
c906108c 3997 {
c02f5703
MS
3998 /* Yes -- remove it. Previously we did not bother to
3999 remove the breakpoint if the section had been
4000 unmapped, but let's not rely on that being safe. We
4001 don't know what the overlay manager might do. */
aa67235e
UW
4002
4003 /* However, we should remove *software* breakpoints only
4004 if the section is still mapped, or else we overwrite
4005 wrong code with the saved shadow contents. */
348d480f
PA
4006 if (bl->loc_type == bp_loc_hardware_breakpoint
4007 || section_is_mapped (bl->section))
c47614fe 4008 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
4009 else
4010 val = 0;
c906108c 4011 }
c02f5703
MS
4012 else
4013 {
4014 /* No -- not inserted, so no need to remove. No error. */
4015 val = 0;
4016 }
c906108c 4017 }
879d1e6b 4018
08351840
PA
4019 /* In some cases, we might not be able to remove a breakpoint in
4020 a shared library that has already been removed, but we have
4021 not yet processed the shlib unload event. Similarly for an
4022 unloaded add-symbol-file object - the user might not yet have
4023 had the chance to remove-symbol-file it. shlib_disabled will
4024 be set if the library/object has already been removed, but
4025 the breakpoint hasn't been uninserted yet, e.g., after
4026 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4027 always-inserted mode. */
076855f9 4028 if (val
08351840
PA
4029 && (bl->loc_type == bp_loc_software_breakpoint
4030 && (bl->shlib_disabled
4031 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4032 || shared_objfile_contains_address_p (bl->pspace,
4033 bl->address))))
879d1e6b
UW
4034 val = 0;
4035
c906108c
SS
4036 if (val)
4037 return val;
b2b6a7da 4038 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4039 }
35df4500 4040 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4041 {
b2b6a7da 4042 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 4043 bl->owner->remove_location (bl, reason);
2e70b7b9 4044
c906108c 4045 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4046 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4047 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4048 bl->owner->number);
c906108c 4049 }
35df4500 4050 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
4051 && breakpoint_enabled (bl->owner)
4052 && !bl->duplicate)
ce78b96d 4053 {
c47614fe 4054 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
4055 if (val)
4056 return val;
77b06cd7 4057
b2b6a7da 4058 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4059 }
c906108c
SS
4060
4061 return 0;
4062}
4063
6c95b8df 4064static int
834c0d03 4065remove_breakpoint (struct bp_location *bl)
6c95b8df 4066{
35df4500
TJB
4067 /* BL is never in moribund_locations by our callers. */
4068 gdb_assert (bl->owner != NULL);
2bdf28a0 4069
6c95b8df
PA
4070 /* The type of none suggests that owner is actually deleted.
4071 This should not ever happen. */
35df4500 4072 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4073
5ed8105e 4074 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4075
35df4500 4076 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4077
5ed8105e 4078 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4079}
4080
c906108c
SS
4081/* Clear the "inserted" flag in all breakpoints. */
4082
25b22b0a 4083void
fba45db2 4084mark_breakpoints_out (void)
c906108c 4085{
48d7020b 4086 for (bp_location *bl : all_bp_locations ())
66c4b3e8 4087 if (bl->pspace == current_program_space)
35df4500 4088 bl->inserted = 0;
c906108c
SS
4089}
4090
53a5351d
JM
4091/* Clear the "inserted" flag in all breakpoints and delete any
4092 breakpoints which should go away between runs of the program.
c906108c
SS
4093
4094 Plus other such housekeeping that has to be done for breakpoints
4095 between runs.
4096
53a5351d
JM
4097 Note: this function gets called at the end of a run (by
4098 generic_mourn_inferior) and when a run begins (by
4a64f543 4099 init_wait_for_inferior). */
c906108c
SS
4100
4101
4102
4103void
fba45db2 4104breakpoint_init_inferior (enum inf_context context)
c906108c 4105{
6c95b8df 4106 struct program_space *pspace = current_program_space;
c906108c 4107
50c71eaf
PA
4108 /* If breakpoint locations are shared across processes, then there's
4109 nothing to do. */
f5656ead 4110 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4111 return;
4112
1a853c52 4113 mark_breakpoints_out ();
075f6582 4114
1428b37a 4115 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
4116 {
4117 if (b->loc && b->loc->pspace != pspace)
4118 continue;
6c95b8df 4119
01add95b
SM
4120 switch (b->type)
4121 {
4122 case bp_call_dummy:
4123 case bp_longjmp_call_dummy:
c906108c 4124
01add95b
SM
4125 /* If the call dummy breakpoint is at the entry point it will
4126 cause problems when the inferior is rerun, so we better get
4127 rid of it. */
ab92d69b 4128
01add95b 4129 case bp_watchpoint_scope:
ab92d69b 4130
01add95b 4131 /* Also get rid of scope breakpoints. */
ab92d69b 4132
01add95b 4133 case bp_shlib_event:
ab92d69b 4134
01add95b
SM
4135 /* Also remove solib event breakpoints. Their addresses may
4136 have changed since the last time we ran the program.
4137 Actually we may now be debugging against different target;
4138 and so the solib backend that installed this breakpoint may
4139 not be used in by the target. E.g.,
ab92d69b 4140
01add95b
SM
4141 (gdb) file prog-linux
4142 (gdb) run # native linux target
4143 ...
4144 (gdb) kill
4145 (gdb) file prog-win.exe
4146 (gdb) tar rem :9999 # remote Windows gdbserver.
4147 */
c906108c 4148
01add95b 4149 case bp_step_resume:
f59f708a 4150
01add95b 4151 /* Also remove step-resume breakpoints. */
f59f708a 4152
01add95b 4153 case bp_single_step:
7c16b83e 4154
01add95b 4155 /* Also remove single-step breakpoints. */
7c16b83e 4156
01add95b
SM
4157 delete_breakpoint (b);
4158 break;
c906108c 4159
01add95b
SM
4160 case bp_watchpoint:
4161 case bp_hardware_watchpoint:
4162 case bp_read_watchpoint:
4163 case bp_access_watchpoint:
4164 {
4165 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4166
01add95b
SM
4167 /* Likewise for watchpoints on local expressions. */
4168 if (w->exp_valid_block != NULL)
4169 delete_breakpoint (b);
4170 else
4171 {
4172 /* Get rid of existing locations, which are no longer
4173 valid. New ones will be created in
4174 update_watchpoint, when the inferior is restarted.
4175 The next update_global_location_list call will
4176 garbage collect them. */
4177 b->loc = NULL;
4178
4179 if (context == inf_starting)
4180 {
4181 /* Reset val field to force reread of starting value in
4182 insert_breakpoints. */
4183 w->val.reset (nullptr);
4184 w->val_valid = false;
4185 }
4186 }
4187 }
4188 break;
4189 default:
4190 break;
3a5c3e22 4191 }
01add95b 4192 }
1c5cfe86
PA
4193
4194 /* Get rid of the moribund locations. */
1123588c 4195 for (bp_location *bl : moribund_locations)
35df4500 4196 decref_bp_location (&bl);
1123588c 4197 moribund_locations.clear ();
c906108c
SS
4198}
4199
6c95b8df
PA
4200/* These functions concern about actual breakpoints inserted in the
4201 target --- to e.g. check if we need to do decr_pc adjustment or if
4202 we need to hop over the bkpt --- so we check for address space
4203 match, not program space. */
4204
c2c6d25f
JM
4205/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4206 exists at PC. It returns ordinary_breakpoint_here if it's an
4207 ordinary breakpoint, or permanent_breakpoint_here if it's a
4208 permanent breakpoint.
4209 - When continuing from a location with an ordinary breakpoint, we
4210 actually single step once before calling insert_breakpoints.
e5dd4106 4211 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4212 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4213 the target, to advance the PC past the breakpoint. */
c906108c 4214
c2c6d25f 4215enum breakpoint_here
accd0bcd 4216breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4217{
d8de7963 4218 bool any_breakpoint_here = false;
c906108c 4219
48d7020b 4220 for (bp_location *bl : all_bp_locations ())
075f6582 4221 {
35df4500
TJB
4222 if (bl->loc_type != bp_loc_software_breakpoint
4223 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4224 continue;
4225
f1310107 4226 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4227 if ((breakpoint_enabled (bl->owner)
1a853c52 4228 || bl->permanent)
f1310107 4229 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4230 {
4231 if (overlay_debugging
35df4500
TJB
4232 && section_is_overlay (bl->section)
4233 && !section_is_mapped (bl->section))
075f6582 4234 continue; /* unmapped overlay -- can't be a match */
1a853c52 4235 else if (bl->permanent)
075f6582
DJ
4236 return permanent_breakpoint_here;
4237 else
d8de7963 4238 any_breakpoint_here = true;
075f6582
DJ
4239 }
4240 }
c906108c 4241
f486487f 4242 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4243}
4244
d35ae833
PA
4245/* See breakpoint.h. */
4246
4247int
accd0bcd 4248breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4249 CORE_ADDR addr, ULONGEST len)
4250{
48d7020b 4251 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4252 {
4253 if (bl->loc_type != bp_loc_software_breakpoint
4254 && bl->loc_type != bp_loc_hardware_breakpoint)
4255 continue;
4256
4257 if ((breakpoint_enabled (bl->owner)
4258 || bl->permanent)
4259 && breakpoint_location_address_range_overlap (bl, aspace,
4260 addr, len))
4261 {
4262 if (overlay_debugging
4263 && section_is_overlay (bl->section)
4264 && !section_is_mapped (bl->section))
4265 {
4266 /* Unmapped overlay -- can't be a match. */
4267 continue;
4268 }
4269
4270 return 1;
4271 }
4272 }
4273
4274 return 0;
4275}
4276
1c5cfe86
PA
4277/* Return true if there's a moribund breakpoint at PC. */
4278
4279int
accd0bcd 4280moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4281{
1123588c 4282 for (bp_location *loc : moribund_locations)
f1310107 4283 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4284 return 1;
4285
4286 return 0;
4287}
c2c6d25f 4288
d8de7963 4289/* Returns true iff BL is inserted at PC, in address space ASPACE. */
f7ce857f 4290
d8de7963 4291static bool
b2ff9ed3 4292bp_location_inserted_here_p (const struct bp_location *bl,
accd0bcd 4293 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4294{
4295 if (bl->inserted
4296 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4297 aspace, pc))
4298 {
d8de7963
AB
4299 /* An unmapped overlay can't be a match. */
4300 return !(overlay_debugging
4301 && section_is_overlay (bl->section)
4302 && !section_is_mapped (bl->section));
f7ce857f 4303 }
d8de7963 4304 return false;
f7ce857f
PA
4305}
4306
a1fd2fa5 4307/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4308
4309int
accd0bcd 4310breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4311{
e0d9a270 4312 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4313 {
35df4500
TJB
4314 if (bl->loc_type != bp_loc_software_breakpoint
4315 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4316 continue;
4317
f7ce857f
PA
4318 if (bp_location_inserted_here_p (bl, aspace, pc))
4319 return 1;
c5aa993b 4320 }
c36b740a
VP
4321 return 0;
4322}
4323
a1fd2fa5
PA
4324/* This function returns non-zero iff there is a software breakpoint
4325 inserted at PC. */
c36b740a
VP
4326
4327int
accd0bcd 4328software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4329 CORE_ADDR pc)
4fa8626c 4330{
e0d9a270 4331 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4332 {
35df4500 4333 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4334 continue;
4335
f7ce857f
PA
4336 if (bp_location_inserted_here_p (bl, aspace, pc))
4337 return 1;
4fa8626c
DJ
4338 }
4339
4340 return 0;
9c02b525
PA
4341}
4342
4343/* See breakpoint.h. */
4344
4345int
accd0bcd 4346hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4347 CORE_ADDR pc)
4348{
e0d9a270 4349 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4350 {
9c02b525
PA
4351 if (bl->loc_type != bp_loc_hardware_breakpoint)
4352 continue;
4353
4354 if (bp_location_inserted_here_p (bl, aspace, pc))
4355 return 1;
4356 }
4357
4358 return 0;
4fa8626c
DJ
4359}
4360
9093389c 4361int
accd0bcd 4362hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4363 CORE_ADDR addr, ULONGEST len)
4364{
43892fdf 4365 for (breakpoint *bpt : all_breakpoints ())
9093389c 4366 {
9093389c
PA
4367 if (bpt->type != bp_hardware_watchpoint
4368 && bpt->type != bp_access_watchpoint)
4369 continue;
4370
4371 if (!breakpoint_enabled (bpt))
4372 continue;
4373
40cb8ca5 4374 for (bp_location *loc : bpt->locations ())
9093389c
PA
4375 if (loc->pspace->aspace == aspace && loc->inserted)
4376 {
4377 CORE_ADDR l, h;
4378
4379 /* Check for intersection. */
768adc05
PA
4380 l = std::max<CORE_ADDR> (loc->address, addr);
4381 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4382 if (l < h)
4383 return 1;
4384 }
4385 }
4386 return 0;
4387}
c5aa993b 4388
f2478a7e 4389/* See breakpoint.h. */
c906108c 4390
f2478a7e
SM
4391bool
4392is_catchpoint (struct breakpoint *b)
c906108c 4393{
f2478a7e 4394 return (b->type == bp_catchpoint);
c906108c
SS
4395}
4396
c906108c
SS
4397/* Clear a bpstat so that it says we are not at any breakpoint.
4398 Also free any storage that is part of a bpstat. */
4399
4400void
313f3b21 4401bpstat_clear (bpstat **bsp)
c906108c 4402{
313f3b21
SM
4403 bpstat *p;
4404 bpstat *q;
c906108c
SS
4405
4406 if (bsp == 0)
4407 return;
4408 p = *bsp;
4409 while (p != NULL)
4410 {
4411 q = p->next;
04afa70c 4412 delete p;
c906108c
SS
4413 p = q;
4414 }
4415 *bsp = NULL;
4416}
4417
313f3b21 4418bpstat::bpstat (const bpstat &other)
04afa70c
TT
4419 : next (NULL),
4420 bp_location_at (other.bp_location_at),
4421 breakpoint_at (other.breakpoint_at),
4422 commands (other.commands),
04afa70c
TT
4423 print (other.print),
4424 stop (other.stop),
4425 print_it (other.print_it)
4426{
850645cf 4427 if (other.old_val != NULL)
f28085df 4428 old_val = release_value (other.old_val->copy ());
04afa70c
TT
4429}
4430
c906108c
SS
4431/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4432 is part of the bpstat is copied as well. */
4433
313f3b21
SM
4434bpstat *
4435bpstat_copy (bpstat *bs)
c906108c 4436{
313f3b21
SM
4437 bpstat *p = nullptr;
4438 bpstat *tmp;
4439 bpstat *retval = nullptr;
c906108c
SS
4440
4441 if (bs == NULL)
4442 return bs;
4443
4444 for (; bs != NULL; bs = bs->next)
4445 {
313f3b21 4446 tmp = new bpstat (*bs);
31cc81e9 4447
c906108c
SS
4448 if (p == NULL)
4449 /* This is the first thing in the chain. */
4450 retval = tmp;
4451 else
4452 p->next = tmp;
4453 p = tmp;
4454 }
4455 p->next = NULL;
4456 return retval;
4457}
4458
4a64f543 4459/* Find the bpstat associated with this breakpoint. */
c906108c 4460
313f3b21
SM
4461bpstat *
4462bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4463{
c5aa993b
JM
4464 if (bsp == NULL)
4465 return NULL;
c906108c 4466
c5aa993b
JM
4467 for (; bsp != NULL; bsp = bsp->next)
4468 {
f431efe5 4469 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4470 return bsp;
4471 }
c906108c
SS
4472 return NULL;
4473}
4474
ab04a2af
TT
4475/* See breakpoint.h. */
4476
4c462cb0 4477bool
313f3b21 4478bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4479{
ab04a2af
TT
4480 for (; bsp != NULL; bsp = bsp->next)
4481 {
427cd150
TT
4482 if (bsp->breakpoint_at == NULL)
4483 {
4484 /* A moribund location can never explain a signal other than
4485 GDB_SIGNAL_TRAP. */
4486 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4487 return true;
427cd150
TT
4488 }
4489 else
47591c29 4490 {
c47614fe 4491 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4492 return true;
47591c29 4493 }
ab04a2af
TT
4494 }
4495
4c462cb0 4496 return false;
ab04a2af
TT
4497}
4498
78805ff8 4499/* See breakpoint.h. */
c906108c
SS
4500
4501int
313f3b21 4502bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4503{
4504 struct breakpoint *b;
4505
4506 if ((*bsp) == NULL)
4507 return 0; /* No more breakpoint values */
8671a17b 4508
4a64f543
MS
4509 /* We assume we'll never have several bpstats that correspond to a
4510 single breakpoint -- otherwise, this function might return the
4511 same number more than once and this will look ugly. */
f431efe5 4512 b = (*bsp)->breakpoint_at;
8671a17b
PA
4513 *bsp = (*bsp)->next;
4514 if (b == NULL)
4515 return -1; /* breakpoint that's been deleted since */
4516
4517 *num = b->number; /* We have its number */
4518 return 1;
c906108c
SS
4519}
4520
78805ff8
PW
4521/* See breakpoint.h */
4522
4523int
4524bpstat_locno (const bpstat *bs)
4525{
4526 const struct breakpoint *b = bs->breakpoint_at;
4527 const struct bp_location *bl = bs->bp_location_at.get ();
4528
4529 int locno = 0;
4530
28a072f4 4531 if (b != nullptr && b->loc != nullptr && b->loc->next != nullptr)
78805ff8
PW
4532 {
4533 const bp_location *bl_i;
4534
4535 for (bl_i = b->loc;
4536 bl_i != bl && bl_i->next != nullptr;
4537 bl_i = bl_i->next)
4538 locno++;
4539
4540 if (bl_i == bl)
4541 locno++;
4542 else
4543 {
4544 warning (_("location number not found for breakpoint %d address %s."),
4545 b->number, paddress (bl->gdbarch, bl->address));
4546 locno = 0;
4547 }
4548 }
4549
4550 return locno;
4551}
4552
4553/* See breakpoint.h. */
4554
4555void
4556print_num_locno (const bpstat *bs, struct ui_out *uiout)
4557{
4558 struct breakpoint *b = bs->breakpoint_at;
4559
4560 if (b == nullptr)
4561 uiout->text (_("deleted breakpoint"));
4562 else
4563 {
4564 uiout->field_signed ("bkptno", b->number);
4565
4566 int locno = bpstat_locno (bs);
4567 if (locno != 0)
4568 uiout->message (".%pF", signed_field ("locno", locno));
4569 }
4570}
4571
e93ca019 4572/* See breakpoint.h. */
c906108c
SS
4573
4574void
e93ca019 4575bpstat_clear_actions (void)
c906108c 4576{
313f3b21 4577 bpstat *bs;
e93ca019 4578
00431a78 4579 if (inferior_ptid == null_ptid)
e93ca019
JK
4580 return;
4581
00431a78 4582 thread_info *tp = inferior_thread ();
e93ca019 4583 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4584 {
d1b0a7bf 4585 bs->commands = NULL;
850645cf 4586 bs->old_val.reset (nullptr);
c906108c
SS
4587 }
4588}
4589
f3b1572e
PA
4590/* Called when a command is about to proceed the inferior. */
4591
4592static void
4593breakpoint_about_to_proceed (void)
4594{
d7e15655 4595 if (inferior_ptid != null_ptid)
f3b1572e
PA
4596 {
4597 struct thread_info *tp = inferior_thread ();
4598
4599 /* Allow inferior function calls in breakpoint commands to not
4600 interrupt the command list. When the call finishes
4601 successfully, the inferior will be standing at the same
4602 breakpoint as if nothing happened. */
16c381f0 4603 if (tp->control.in_infcall)
f3b1572e
PA
4604 return;
4605 }
4606
4607 breakpoint_proceeded = 1;
4608}
4609
d8de7963 4610/* Return true iff CMD as the first line of a command sequence is `silent'
abf85f46
JK
4611 or its equivalent. */
4612
d8de7963 4613static bool
abf85f46
JK
4614command_line_is_silent (struct command_line *cmd)
4615{
4f45d445 4616 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4617}
4618
83f35083
PW
4619/* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4620 A locno 0 is changed to 1 to e.g. let the user do
4621 (gdb) disable $_hit_bpnum.$_hit_locno
4622 for a single location breakpoint. */
4623
78805ff8 4624static void
83f35083 4625set_hit_convenience_vars (int bpnum, int locno)
78805ff8 4626{
83f35083
PW
4627 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4628 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4629 (locno > 0 ? locno : 1));
78805ff8
PW
4630}
4631
4a64f543
MS
4632/* Execute all the commands associated with all the breakpoints at
4633 this location. Any of these commands could cause the process to
4634 proceed beyond this point, etc. We look out for such changes by
4635 checking the global "breakpoint_proceeded" after each command.
c906108c 4636
347bddb7
PA
4637 Returns true if a breakpoint command resumed the inferior. In that
4638 case, it is the caller's responsibility to recall it again with the
4639 bpstat of the current thread. */
4640
d8de7963 4641static bool
313f3b21 4642bpstat_do_actions_1 (bpstat **bsp)
c906108c 4643{
313f3b21 4644 bpstat *bs;
d8de7963 4645 bool again = false;
c906108c
SS
4646
4647 /* Avoid endless recursion if a `source' command is contained
4648 in bs->commands. */
4649 if (executing_breakpoint_commands)
d8de7963 4650 return false;
c906108c 4651
81b1e71c
TT
4652 scoped_restore save_executing
4653 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4654
1ac32117 4655 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4656
4a64f543 4657 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4658 bs = *bsp;
4659
78805ff8 4660 /* The $_hit_* convenience variables are set before running the
83f35083
PW
4661 commands of BS. In case we have several bs, after the loop,
4662 we set again the variables to the first printed bpnum and locno.
4663 For multiple breakpoints, this ensures the variables are set to the
4664 breakpoint printed for the user. */
4665 int printed_hit_bpnum = -1;
4666 int printed_hit_locno = -1;
78805ff8 4667
c906108c
SS
4668 breakpoint_proceeded = 0;
4669 for (; bs != NULL; bs = bs->next)
4670 {
d1b0a7bf 4671 struct command_line *cmd = NULL;
6c50ab1c 4672
83f35083
PW
4673 /* Set the _hit_* convenience variables before running BS's commands. */
4674 {
4675 const struct breakpoint *b = bs->breakpoint_at;
4676 if (b != nullptr)
4677 {
4678 int locno = bpstat_locno (bs);
4679
4680 set_hit_convenience_vars (b->number, locno);
4681 if (printed_hit_locno == -1 && bs->print)
4682 {
4683 printed_hit_bpnum = b->number;
4684 printed_hit_locno = locno;
4685 }
4686 }
4687 }
78805ff8 4688
6c50ab1c
JB
4689 /* Take ownership of the BSP's command tree, if it has one.
4690
dda83cd7
SM
4691 The command tree could legitimately contain commands like
4692 'step' and 'next', which call clear_proceed_status, which
83f35083 4693 frees the bpstat BS and its command tree. To make sure this doesn't
dda83cd7
SM
4694 free the tree we're executing out from under us, we need to
4695 take ownership of the tree ourselves. Since a given bpstat's
4696 commands are only executed once, we don't need to copy it; we
4697 can clear the pointer in the bpstat, and make sure we free
4698 the tree when we're done. */
d1b0a7bf 4699 counted_command_line ccmd = bs->commands;
9add0f1b 4700 bs->commands = NULL;
d1b0a7bf
TT
4701 if (ccmd != NULL)
4702 cmd = ccmd.get ();
abf85f46
JK
4703 if (command_line_is_silent (cmd))
4704 {
4705 /* The action has been already done by bpstat_stop_status. */
4706 cmd = cmd->next;
4707 }
6c50ab1c 4708
c906108c
SS
4709 while (cmd != NULL)
4710 {
4711 execute_control_command (cmd);
83f35083
PW
4712 /* After execute_control_command, if breakpoint_proceeded is true,
4713 BS has been freed and cannot be accessed anymore. */
c906108c
SS
4714
4715 if (breakpoint_proceeded)
4716 break;
4717 else
4718 cmd = cmd->next;
4719 }
6c50ab1c 4720
c906108c 4721 if (breakpoint_proceeded)
32c1e744 4722 {
cb814510 4723 if (current_ui->async)
347bddb7
PA
4724 /* If we are in async mode, then the target might be still
4725 running, not stopped at any breakpoint, so nothing for
4726 us to do here -- just return to the event loop. */
4727 ;
32c1e744
VP
4728 else
4729 /* In sync mode, when execute_control_command returns
4730 we're already standing on the next breakpoint.
347bddb7
PA
4731 Breakpoint commands for that stop were not run, since
4732 execute_command does not run breakpoint commands --
4733 only command_line_handler does, but that one is not
4734 involved in execution of breakpoint commands. So, we
4735 can now execute breakpoint commands. It should be
4736 noted that making execute_command do bpstat actions is
4737 not an option -- in this case we'll have recursive
4738 invocation of bpstat for each breakpoint with a
4739 command, and can easily blow up GDB stack. Instead, we
4740 return true, which will trigger the caller to recall us
4741 with the new stop_bpstat. */
d8de7963 4742 again = true;
347bddb7 4743 break;
32c1e744 4744 }
c906108c 4745 }
78805ff8
PW
4746
4747 /* Now that we have executed the commands of all bs, set the _hit_*
83f35083
PW
4748 convenience variables to the printed values. */
4749 if (printed_hit_locno != -1)
4750 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
78805ff8 4751
347bddb7
PA
4752 return again;
4753}
4754
00431a78
PA
4755/* Helper for bpstat_do_actions. Get the current thread, if there's
4756 one, is alive and has execution. Return NULL otherwise. */
4757
4758static thread_info *
4759get_bpstat_thread ()
4760{
55f6301a 4761 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4762 return NULL;
4763
4764 thread_info *tp = inferior_thread ();
611841bb 4765 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4766 return NULL;
4767 return tp;
4768}
4769
347bddb7
PA
4770void
4771bpstat_do_actions (void)
4772{
694c6bf5 4773 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4774 thread_info *tp;
353d1d73 4775
347bddb7 4776 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4777 while ((tp = get_bpstat_thread ()) != NULL)
4778 {
4779 /* Since in sync mode, bpstat_do_actions may resume the
4780 inferior, and only return when it is stopped at the next
4781 breakpoint, we keep doing breakpoint actions until it returns
4782 false to indicate the inferior was not resumed. */
4783 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4784 break;
4785 }
353d1d73 4786
694c6bf5 4787 cleanup_if_error.release ();
c906108c
SS
4788}
4789
fa4727a6
DJ
4790/* Print out the (old or new) value associated with a watchpoint. */
4791
4792static void
4793watchpoint_value_print (struct value *val, struct ui_file *stream)
4794{
4795 if (val == NULL)
7f6aba03 4796 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4797 else
79a45b7d
TT
4798 {
4799 struct value_print_options opts;
4800 get_user_print_options (&opts);
4801 value_print (val, stream, &opts);
4802 }
fa4727a6
DJ
4803}
4804
f303dbd6
PA
4805/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4806 debugging multiple threads. */
4807
4808void
4809maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4810{
112e8700 4811 if (uiout->is_mi_like_p ())
f303dbd6
PA
4812 return;
4813
112e8700 4814 uiout->text ("\n");
f303dbd6
PA
4815
4816 if (show_thread_that_caused_stop ())
4817 {
f303dbd6
PA
4818 struct thread_info *thr = inferior_thread ();
4819
112e8700 4820 uiout->text ("Thread ");
33eca680 4821 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 4822
25558938 4823 const char *name = thread_name (thr);
f303dbd6
PA
4824 if (name != NULL)
4825 {
112e8700 4826 uiout->text (" \"");
33eca680 4827 uiout->field_string ("name", name);
112e8700 4828 uiout->text ("\"");
f303dbd6
PA
4829 }
4830
112e8700 4831 uiout->text (" hit ");
f303dbd6
PA
4832 }
4833}
4834
e514a9d6 4835/* Generic routine for printing messages indicating why we
4a64f543 4836 stopped. The behavior of this function depends on the value
e514a9d6
JM
4837 'print_it' in the bpstat structure. Under some circumstances we
4838 may decide not to print anything here and delegate the task to
4a64f543 4839 normal_stop(). */
e514a9d6
JM
4840
4841static enum print_stop_action
313f3b21 4842print_bp_stop_message (bpstat *bs)
e514a9d6
JM
4843{
4844 switch (bs->print_it)
4845 {
4846 case print_it_noop:
4a64f543 4847 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4848 return PRINT_UNKNOWN;
4849 break;
4850
4851 case print_it_done:
4852 /* We still want to print the frame, but we already printed the
dda83cd7 4853 relevant messages. */
e514a9d6
JM
4854 return PRINT_SRC_AND_LOC;
4855 break;
4856
4857 case print_it_normal:
4f8d1dc6 4858 {
f431efe5
PA
4859 struct breakpoint *b = bs->breakpoint_at;
4860
1a6a67de
TJB
4861 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4862 which has since been deleted. */
4863 if (b == NULL)
4864 return PRINT_UNKNOWN;
4865
348d480f 4866 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 4867 return b->print_it (bs);
4f8d1dc6 4868 }
348d480f 4869 break;
3086aeae 4870
e514a9d6 4871 default:
f34652de 4872 internal_error (_("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4873 break;
c906108c 4874 }
c906108c
SS
4875}
4876
2f9ee862 4877/* See breakpoint.h. */
edcc5120 4878
2f9ee862 4879void
4e9e993a 4880print_solib_event (bool is_catchpoint)
edcc5120 4881{
6fb16ce6 4882 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4883 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4884
4885 if (!is_catchpoint)
4886 {
4887 if (any_added || any_deleted)
112e8700 4888 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4889 else
112e8700
SM
4890 current_uiout->text (_("Stopped due to shared library event (no "
4891 "libraries added or removed)\n"));
edcc5120
TT
4892 }
4893
112e8700
SM
4894 if (current_uiout->is_mi_like_p ())
4895 current_uiout->field_string ("reason",
4896 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4897
4898 if (any_deleted)
4899 {
112e8700 4900 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4901 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4902 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4903 {
6fb16ce6
SM
4904 const std::string &name = current_program_space->deleted_solibs[ix];
4905
edcc5120 4906 if (ix > 0)
112e8700
SM
4907 current_uiout->text (" ");
4908 current_uiout->field_string ("library", name);
4909 current_uiout->text ("\n");
edcc5120 4910 }
edcc5120
TT
4911 }
4912
4913 if (any_added)
4914 {
112e8700 4915 current_uiout->text (_(" Inferior loaded "));
10f489e5 4916 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4917 bool first = true;
52941706 4918 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4919 {
bcb430e4 4920 if (!first)
112e8700 4921 current_uiout->text (" ");
bcb430e4 4922 first = false;
112e8700
SM
4923 current_uiout->field_string ("library", iter->so_name);
4924 current_uiout->text ("\n");
edcc5120 4925 }
edcc5120
TT
4926 }
4927}
4928
e514a9d6
JM
4929/* Print a message indicating what happened. This is called from
4930 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4931 list - a list of the eventpoints that caused this stop. KIND is
4932 the target_waitkind for the stopping event. This
e514a9d6
JM
4933 routine calls the generic print routine for printing a message
4934 about reasons for stopping. This will print (for example) the
4935 "Breakpoint n," part of the output. The return value of this
4936 routine is one of:
c906108c 4937
4a64f543 4938 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4939 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4940 code to print the location. An example is
c5aa993b
JM
4941 "Breakpoint 1, " which should be followed by
4942 the location.
917317f4 4943 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4944 to also print the location part of the message.
4945 An example is the catch/throw messages, which
4a64f543 4946 don't require a location appended to the end.
917317f4 4947 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4948 further info to be printed. */
c906108c 4949
917317f4 4950enum print_stop_action
403c71fd 4951bpstat_print (bpstat *bs, target_waitkind kind)
c906108c 4952{
f486487f 4953 enum print_stop_action val;
c5aa993b 4954
c906108c 4955 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4956 (Currently all watchpoints go on the bpstat whether hit or not.
4957 That probably could (should) be changed, provided care is taken
c906108c 4958 with respect to bpstat_explains_signal). */
e514a9d6
JM
4959 for (; bs; bs = bs->next)
4960 {
4961 val = print_bp_stop_message (bs);
4962 if (val == PRINT_SRC_ONLY
78805ff8 4963 || val == PRINT_SRC_AND_LOC
e514a9d6
JM
4964 || val == PRINT_NOTHING)
4965 return val;
4966 }
c906108c 4967
36dfb11c
TT
4968 /* If we had hit a shared library event breakpoint,
4969 print_bp_stop_message would print out this message. If we hit an
4970 OS-level shared library event, do the same thing. */
4971 if (kind == TARGET_WAITKIND_LOADED)
4972 {
4e9e993a 4973 print_solib_event (false);
36dfb11c
TT
4974 return PRINT_NOTHING;
4975 }
4976
e514a9d6 4977 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4978 with and nothing was printed. */
917317f4 4979 return PRINT_UNKNOWN;
c906108c
SS
4980}
4981
bf469271 4982/* Evaluate the boolean expression EXP and return the result. */
c906108c 4983
bf469271
PA
4984static bool
4985breakpoint_cond_eval (expression *exp)
c906108c 4986{
65558ca5 4987 scoped_value_mark mark;
43048e46 4988 return value_true (exp->evaluate ());
c906108c
SS
4989}
4990
5760d0ab 4991/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4992
313f3b21 4993bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 4994 : next (NULL),
b6433ede 4995 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4996 breakpoint_at (bl->owner),
4997 commands (NULL),
04afa70c
TT
4998 print (0),
4999 stop (0),
5000 print_it (print_it_normal)
c906108c 5001{
04afa70c
TT
5002 **bs_link_pointer = this;
5003 *bs_link_pointer = &next;
5004}
5005
313f3b21 5006bpstat::bpstat ()
04afa70c 5007 : next (NULL),
04afa70c
TT
5008 breakpoint_at (NULL),
5009 commands (NULL),
04afa70c
TT
5010 print (0),
5011 stop (0),
5012 print_it (print_it_normal)
5013{
c906108c
SS
5014}
5015\f
d983da9c
DJ
5016/* The target has stopped with waitstatus WS. Check if any hardware
5017 watchpoints have triggered, according to the target. */
5018
5019int
c272a98c 5020watchpoints_triggered (const target_waitstatus &ws)
d983da9c 5021{
57810aa7 5022 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 5023 CORE_ADDR addr;
d983da9c
DJ
5024
5025 if (!stopped_by_watchpoint)
5026 {
5027 /* We were not stopped by a watchpoint. Mark all watchpoints
5028 as not triggered. */
43892fdf 5029 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5030 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5031 {
5032 struct watchpoint *w = (struct watchpoint *) b;
5033
5034 w->watchpoint_triggered = watch_triggered_no;
5035 }
d983da9c
DJ
5036
5037 return 0;
5038 }
5039
328d42d8 5040 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
5041 {
5042 /* We were stopped by a watchpoint, but we don't know where.
5043 Mark all watchpoints as unknown. */
43892fdf 5044 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5045 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5046 {
5047 struct watchpoint *w = (struct watchpoint *) b;
5048
5049 w->watchpoint_triggered = watch_triggered_unknown;
5050 }
d983da9c 5051
3c4797ba 5052 return 1;
d983da9c
DJ
5053 }
5054
5055 /* The target could report the data address. Mark watchpoints
5056 affected by this data address as triggered, and all others as not
5057 triggered. */
5058
43892fdf 5059 for (breakpoint *b : all_breakpoints ())
cc60f2e3 5060 if (is_hardware_watchpoint (b))
d983da9c 5061 {
3a5c3e22 5062 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 5063
3a5c3e22 5064 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 5065 for (bp_location *loc : b->locations ())
9c06b0b4 5066 {
3a5c3e22 5067 if (is_masked_watchpoint (b))
9c06b0b4 5068 {
3a5c3e22
PA
5069 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5070 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5071
5072 if (newaddr == start)
5073 {
3a5c3e22 5074 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5075 break;
5076 }
5077 }
5078 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
5079 else if (target_watchpoint_addr_within_range
5080 (current_inferior ()->top_target (), addr, loc->address,
5081 loc->length))
9c06b0b4 5082 {
3a5c3e22 5083 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5084 break;
5085 }
5086 }
d983da9c
DJ
5087 }
5088
5089 return 1;
5090}
5091
bf469271
PA
5092/* Possible return values for watchpoint_check. */
5093enum wp_check_result
5094 {
5095 /* The watchpoint has been deleted. */
5096 WP_DELETED = 1,
5097
5098 /* The value has changed. */
5099 WP_VALUE_CHANGED = 2,
5100
5101 /* The value has not changed. */
5102 WP_VALUE_NOT_CHANGED = 3,
5103
5104 /* Ignore this watchpoint, no matter if the value changed or not. */
5105 WP_IGNORE = 4,
5106 };
c906108c
SS
5107
5108#define BP_TEMPFLAG 1
5109#define BP_HARDWAREFLAG 2
5110
4a64f543 5111/* Evaluate watchpoint condition expression and check if its value
bf469271 5112 changed. */
553e4c11 5113
bf469271 5114static wp_check_result
313f3b21 5115watchpoint_check (bpstat *bs)
c906108c 5116{
3a5c3e22 5117 struct watchpoint *b;
bd2b40ac 5118 frame_info_ptr fr;
d8de7963 5119 bool within_current_scope;
c906108c 5120
f431efe5 5121 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5122 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5123 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5124
f6bc2008
PA
5125 /* If this is a local watchpoint, we only want to check if the
5126 watchpoint frame is in scope if the current thread is the thread
5127 that was used to create the watchpoint. */
5128 if (!watchpoint_in_thread_scope (b))
60e1c644 5129 return WP_IGNORE;
f6bc2008 5130
c906108c 5131 if (b->exp_valid_block == NULL)
d8de7963 5132 within_current_scope = true;
c906108c
SS
5133 else
5134 {
bd2b40ac 5135 frame_info_ptr frame = get_current_frame ();
edb3359d
DJ
5136 struct gdbarch *frame_arch = get_frame_arch (frame);
5137 CORE_ADDR frame_pc = get_frame_pc (frame);
5138
c9cf6e20 5139 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5140 still in the function but the stack frame has already been
5141 invalidated. Since we can't rely on the values of local
5142 variables after the stack has been destroyed, we are treating
5143 the watchpoint in that state as `not changed' without further
5144 checking. Don't mark watchpoints as changed if the current
5145 frame is in an epilogue - even if they are in some other
5146 frame, our view of the stack is likely to be wrong and
5147 frame_find_by_id could error out. */
c9cf6e20 5148 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5149 return WP_IGNORE;
a0f49112 5150
101dcfbe 5151 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5152 within_current_scope = (fr != NULL);
69fbadd5
DJ
5153
5154 /* If we've gotten confused in the unwinder, we might have
5155 returned a frame that can't describe this variable. */
edb3359d
DJ
5156 if (within_current_scope)
5157 {
5158 struct symbol *function;
5159
5160 function = get_frame_function (fr);
5161 if (function == NULL
0d191295 5162 || !function->value_block ()->contains (b->exp_valid_block))
d8de7963 5163 within_current_scope = false;
edb3359d 5164 }
69fbadd5 5165
edb3359d 5166 if (within_current_scope)
c906108c
SS
5167 /* If we end up stopping, the current frame will get selected
5168 in normal_stop. So this call to select_frame won't affect
5169 the user. */
0f7d239c 5170 select_frame (fr);
c906108c 5171 }
c5aa993b 5172
c906108c
SS
5173 if (within_current_scope)
5174 {
4a64f543 5175 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5176 time before we return to the command level and call
5177 free_all_values. We can't call free_all_values because we
5178 might be in the middle of evaluating a function call. */
c906108c 5179
9c06b0b4 5180 struct value *mark;
fa4727a6
DJ
5181 struct value *new_val;
5182
c1fc2657 5183 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5184 /* Since we don't know the exact trigger address (from
5185 stopped_data_address), just tell the user we've triggered
5186 a mask watchpoint. */
5187 return WP_VALUE_CHANGED;
5188
5189 mark = value_mark ();
1eaebe02 5190 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5191 NULL, NULL, false);
218d2fc6 5192
bb9d5f81
PP
5193 if (b->val_bitsize != 0)
5194 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5195
4a64f543
MS
5196 /* We use value_equal_contents instead of value_equal because
5197 the latter coerces an array to a pointer, thus comparing just
5198 the address of the array instead of its contents. This is
5199 not what we want. */
fa4727a6 5200 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5201 || (b->val != NULL && !value_equal_contents (b->val.get (),
5202 new_val)))
c906108c 5203 {
c906108c 5204 bs->old_val = b->val;
850645cf 5205 b->val = release_value (new_val);
4c1d86d9 5206 b->val_valid = true;
850645cf
TT
5207 if (new_val != NULL)
5208 value_free_to_mark (mark);
c906108c
SS
5209 return WP_VALUE_CHANGED;
5210 }
5211 else
5212 {
60e1c644 5213 /* Nothing changed. */
c906108c 5214 value_free_to_mark (mark);
c906108c
SS
5215 return WP_VALUE_NOT_CHANGED;
5216 }
5217 }
5218 else
5219 {
5220 /* This seems like the only logical thing to do because
dda83cd7
SM
5221 if we temporarily ignored the watchpoint, then when
5222 we reenter the block in which it is valid it contains
5223 garbage (in the case of a function, it may have two
5224 garbage values, one before and one after the prologue).
5225 So we can't even detect the first assignment to it and
5226 watch after that (since the garbage may or may not equal
5227 the first value assigned). */
348d480f 5228 /* We print all the stop information in
c47614fe
TT
5229 breakpointprint_it, but in this case, by the time we
5230 call breakpoint->print_it this bp will be deleted
348d480f
PA
5231 already. So we have no choice but print the information
5232 here. */
468afe6c 5233
0e454242 5234 SWITCH_THRU_ALL_UIS ()
dda83cd7 5235 {
468afe6c
PA
5236 struct ui_out *uiout = current_uiout;
5237
112e8700
SM
5238 if (uiout->is_mi_like_p ())
5239 uiout->field_string
5240 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5241 uiout->message ("\nWatchpoint %pF deleted because the program has "
5242 "left the block in\n"
5243 "which its expression is valid.\n",
5244 signed_field ("wpnum", b->number));
468afe6c 5245 }
4ce44c66 5246
cdac0397 5247 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5248 b->commands = NULL;
d0fb5eae 5249 watchpoint_del_at_next_stop (b);
c906108c
SS
5250
5251 return WP_DELETED;
5252 }
5253}
5254
18a18393 5255/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5256 breakpoint location BL. This function does not check if we should
5257 stop, only if BL explains the stop. */
5258
d8de7963 5259static bool
6c95b8df 5260bpstat_check_location (const struct bp_location *bl,
accd0bcd 5261 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5262 const target_waitstatus &ws)
18a18393
VP
5263{
5264 struct breakpoint *b = bl->owner;
5265
348d480f 5266 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5267 gdb_assert (b != NULL);
5268
c47614fe 5269 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5270}
5271
3a5c3e22 5272/* Determine if the watched values have actually changed, and we
4ec2227a 5273 should stop. If not, set BS->stop to false. */
3a5c3e22 5274
18a18393 5275static void
313f3b21 5276bpstat_check_watchpoint (bpstat *bs)
18a18393 5277{
2bdf28a0 5278 const struct bp_location *bl;
3a5c3e22 5279 struct watchpoint *b;
2bdf28a0
JK
5280
5281 /* BS is built for existing struct breakpoint. */
b6433ede 5282 bl = bs->bp_location_at.get ();
2bdf28a0 5283 gdb_assert (bl != NULL);
3a5c3e22 5284 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5285 gdb_assert (b != NULL);
18a18393 5286
18a18393 5287 {
d8de7963
AB
5288 bool must_check_value = false;
5289
c1fc2657 5290 if (b->type == bp_watchpoint)
18a18393
VP
5291 /* For a software watchpoint, we must always check the
5292 watched value. */
d8de7963 5293 must_check_value = true;
18a18393
VP
5294 else if (b->watchpoint_triggered == watch_triggered_yes)
5295 /* We have a hardware watchpoint (read, write, or access)
5296 and the target earlier reported an address watched by
5297 this watchpoint. */
d8de7963 5298 must_check_value = true;
18a18393 5299 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5300 && b->type == bp_hardware_watchpoint)
18a18393
VP
5301 /* We were stopped by a hardware watchpoint, but the target could
5302 not report the data address. We must check the watchpoint's
5303 value. Access and read watchpoints are out of luck; without
5304 a data address, we can't figure it out. */
d8de7963 5305 must_check_value = true;
3a5c3e22 5306
18a18393
VP
5307 if (must_check_value)
5308 {
bf469271
PA
5309 wp_check_result e;
5310
a70b8144 5311 try
bf469271
PA
5312 {
5313 e = watchpoint_check (bs);
5314 }
b1ffd112 5315 catch (const gdb_exception_error &ex)
bf469271
PA
5316 {
5317 exception_fprintf (gdb_stderr, ex,
5318 "Error evaluating expression "
5319 "for watchpoint %d\n",
5320 b->number);
5321
5322 SWITCH_THRU_ALL_UIS ()
5323 {
6cb06a8c
TT
5324 gdb_printf (_("Watchpoint %d deleted.\n"),
5325 b->number);
bf469271
PA
5326 }
5327 watchpoint_del_at_next_stop (b);
5328 e = WP_DELETED;
5329 }
bf469271 5330
18a18393
VP
5331 switch (e)
5332 {
5333 case WP_DELETED:
5334 /* We've already printed what needs to be printed. */
5335 bs->print_it = print_it_done;
5336 /* Stop. */
5337 break;
60e1c644
PA
5338 case WP_IGNORE:
5339 bs->print_it = print_it_noop;
4ec2227a 5340 bs->stop = false;
60e1c644 5341 break;
18a18393 5342 case WP_VALUE_CHANGED:
c1fc2657 5343 if (b->type == bp_read_watchpoint)
18a18393 5344 {
85d721b8
PA
5345 /* There are two cases to consider here:
5346
4a64f543 5347 1. We're watching the triggered memory for reads.
85d721b8
PA
5348 In that case, trust the target, and always report
5349 the watchpoint hit to the user. Even though
5350 reads don't cause value changes, the value may
5351 have changed since the last time it was read, and
5352 since we're not trapping writes, we will not see
5353 those, and as such we should ignore our notion of
5354 old value.
5355
4a64f543 5356 2. We're watching the triggered memory for both
85d721b8
PA
5357 reads and writes. There are two ways this may
5358 happen:
5359
4a64f543 5360 2.1. This is a target that can't break on data
85d721b8
PA
5361 reads only, but can break on accesses (reads or
5362 writes), such as e.g., x86. We detect this case
5363 at the time we try to insert read watchpoints.
5364
4a64f543 5365 2.2. Otherwise, the target supports read
85d721b8
PA
5366 watchpoints, but, the user set an access or write
5367 watchpoint watching the same memory as this read
5368 watchpoint.
5369
5370 If we're watching memory writes as well as reads,
5371 ignore watchpoint hits when we find that the
5372 value hasn't changed, as reads don't cause
5373 changes. This still gives false positives when
5374 the program writes the same value to memory as
5375 what there was already in memory (we will confuse
5376 it for a read), but it's much better than
5377 nothing. */
5378
5379 int other_write_watchpoint = 0;
5380
5381 if (bl->watchpoint_type == hw_read)
5382 {
43892fdf 5383 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5384 if (other_b->type == bp_hardware_watchpoint
5385 || other_b->type == bp_access_watchpoint)
85d721b8 5386 {
3a5c3e22
PA
5387 struct watchpoint *other_w =
5388 (struct watchpoint *) other_b;
5389
5390 if (other_w->watchpoint_triggered
5391 == watch_triggered_yes)
5392 {
5393 other_write_watchpoint = 1;
5394 break;
5395 }
85d721b8
PA
5396 }
5397 }
5398
5399 if (other_write_watchpoint
5400 || bl->watchpoint_type == hw_access)
5401 {
5402 /* We're watching the same memory for writes,
5403 and the value changed since the last time we
5404 updated it, so this trap must be for a write.
5405 Ignore it. */
5406 bs->print_it = print_it_noop;
4ec2227a 5407 bs->stop = false;
85d721b8 5408 }
18a18393
VP
5409 }
5410 break;
5411 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5412 if (b->type == bp_hardware_watchpoint
5413 || b->type == bp_watchpoint)
18a18393
VP
5414 {
5415 /* Don't stop: write watchpoints shouldn't fire if
5416 the value hasn't changed. */
5417 bs->print_it = print_it_noop;
4ec2227a 5418 bs->stop = false;
18a18393
VP
5419 }
5420 /* Stop. */
5421 break;
5422 default:
5423 /* Can't happen. */
18a18393
VP
5424 break;
5425 }
5426 }
d8de7963 5427 else /* !must_check_value */
18a18393
VP
5428 {
5429 /* This is a case where some watchpoint(s) triggered, but
5430 not at the address of this watchpoint, or else no
5431 watchpoint triggered after all. So don't print
5432 anything for this watchpoint. */
5433 bs->print_it = print_it_noop;
4ec2227a 5434 bs->stop = false;
18a18393
VP
5435 }
5436 }
5437}
5438
7d4df6a4
DE
5439/* For breakpoints that are currently marked as telling gdb to stop,
5440 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5441 of breakpoint referred to by BS. If we should not stop for this
5442 breakpoint, set BS->stop to 0. */
f431efe5 5443
18a18393 5444static void
313f3b21 5445bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5446{
d8a77e4c
AB
5447 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5448
2bdf28a0
JK
5449 const struct bp_location *bl;
5450 struct breakpoint *b;
bf469271
PA
5451 /* Assume stop. */
5452 bool condition_result = true;
7d4df6a4
DE
5453 struct expression *cond;
5454
5455 gdb_assert (bs->stop);
2bdf28a0
JK
5456
5457 /* BS is built for existing struct breakpoint. */
b6433ede 5458 bl = bs->bp_location_at.get ();
2bdf28a0 5459 gdb_assert (bl != NULL);
f431efe5 5460 b = bs->breakpoint_at;
2bdf28a0 5461 gdb_assert (b != NULL);
18a18393 5462
d8a77e4c
AB
5463 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5464 thread->ptid.to_string ().c_str (),
5465 b->number, find_loc_num_by_location (bl));
5466
b775012e
LM
5467 /* Even if the target evaluated the condition on its end and notified GDB, we
5468 need to do so again since GDB does not know if we stopped due to a
5469 breakpoint or a single step breakpoint. */
5470
18a18393 5471 if (frame_id_p (b->frame_id)
a0cbd650 5472 && b->frame_id != get_stack_frame_id (get_current_frame ()))
18a18393 5473 {
d8a77e4c
AB
5474 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5475 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5476 b->frame_id.to_string ().c_str ());
4ec2227a 5477 bs->stop = false;
7d4df6a4
DE
5478 return;
5479 }
60e1c644 5480
12ab52e9
PA
5481 /* If this is a thread/task-specific breakpoint, don't waste cpu
5482 evaluating the condition if this isn't the specified
5483 thread/task. */
00431a78 5484 if ((b->thread != -1 && b->thread != thread->global_num)
2ecee236 5485 || (b->task != -1 && b->task != ada_get_task_number (thread)))
6c1b0f7b 5486 {
d8a77e4c 5487 infrun_debug_printf ("incorrect thread or task, not stopping");
4ec2227a 5488 bs->stop = false;
6c1b0f7b
DE
5489 return;
5490 }
5491
6dddc817
DE
5492 /* Evaluate extension language breakpoints that have a "stop" method
5493 implemented. */
5494 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5495
7d4df6a4
DE
5496 if (is_watchpoint (b))
5497 {
5498 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5499
4d01a485 5500 cond = w->cond_exp.get ();
7d4df6a4
DE
5501 }
5502 else
4d01a485 5503 cond = bl->cond.get ();
60e1c644 5504
80e0c6dc 5505 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
7d4df6a4 5506 {
d8de7963 5507 bool within_current_scope = true;
7d4df6a4 5508 struct watchpoint * w;
60e1c644 5509
65558ca5
TT
5510 /* We use scoped_value_mark because it could be a long time
5511 before we return to the command level and call
5512 free_all_values. We can't call free_all_values because we
5513 might be in the middle of evaluating a function call. */
5514 scoped_value_mark mark;
7d4df6a4
DE
5515
5516 if (is_watchpoint (b))
5517 w = (struct watchpoint *) b;
5518 else
5519 w = NULL;
5520
5521 /* Need to select the frame, with all that implies so that
5522 the conditions will have the right context. Because we
5523 use the frame, we will not see an inlined function's
5524 variables when we arrive at a breakpoint at the start
5525 of the inlined function; the current frame will be the
5526 call site. */
5527 if (w == NULL || w->cond_exp_valid_block == NULL)
5528 select_frame (get_current_frame ());
5529 else
18a18393 5530 {
bd2b40ac 5531 frame_info_ptr frame;
7d4df6a4
DE
5532
5533 /* For local watchpoint expressions, which particular
5534 instance of a local is being watched matters, so we
5535 keep track of the frame to evaluate the expression
5536 in. To evaluate the condition however, it doesn't
5537 really matter which instantiation of the function
5538 where the condition makes sense triggers the
5539 watchpoint. This allows an expression like "watch
5540 global if q > 10" set in `func', catch writes to
5541 global on all threads that call `func', or catch
5542 writes on all recursive calls of `func' by a single
5543 thread. We simply always evaluate the condition in
5544 the innermost frame that's executing where it makes
5545 sense to evaluate the condition. It seems
5546 intuitive. */
5547 frame = block_innermost_frame (w->cond_exp_valid_block);
5548 if (frame != NULL)
5549 select_frame (frame);
5550 else
d8de7963 5551 within_current_scope = false;
18a18393 5552 }
2e411b8c 5553 CORE_ADDR pc_before_check = get_frame_pc (get_selected_frame (nullptr));
7d4df6a4 5554 if (within_current_scope)
bf469271 5555 {
a70b8144 5556 try
bf469271
PA
5557 {
5558 condition_result = breakpoint_cond_eval (cond);
5559 }
b1ffd112 5560 catch (const gdb_exception_error &ex)
bf469271
PA
5561 {
5562 exception_fprintf (gdb_stderr, ex,
2dc3457a
AB
5563 "Error in testing condition for breakpoint %d:\n",
5564 b->number);
2e411b8c
AB
5565
5566 /* If the pc value changed as a result of evaluating the
5567 condition then we probably stopped within an inferior
5568 function call due to some unexpected stop, e.g. the thread
5569 hit another breakpoint, or the thread received an
5570 unexpected signal. In this case we don't want to also
5571 print the information about this breakpoint. */
5572 CORE_ADDR pc_after_check
5573 = get_frame_pc (get_selected_frame (nullptr));
5574 if (pc_before_check != pc_after_check)
5575 bs->print = 0;
bf469271 5576 }
bf469271 5577 }
7d4df6a4 5578 else
18a18393 5579 {
7d4df6a4
DE
5580 warning (_("Watchpoint condition cannot be tested "
5581 "in the current scope"));
5582 /* If we failed to set the right context for this
5583 watchpoint, unconditionally report it. */
18a18393 5584 }
7d4df6a4 5585 /* FIXME-someday, should give breakpoint #. */
18a18393 5586 }
7d4df6a4 5587
80e0c6dc 5588 if (cond != nullptr && !condition_result)
7d4df6a4 5589 {
d8a77e4c 5590 infrun_debug_printf ("condition_result = false, not stopping");
4ec2227a 5591 bs->stop = false;
d8a77e4c 5592 return;
7d4df6a4 5593 }
7d4df6a4
DE
5594 else if (b->ignore_count > 0)
5595 {
d8a77e4c
AB
5596 infrun_debug_printf ("ignore count %d, not stopping",
5597 b->ignore_count);
7d4df6a4 5598 b->ignore_count--;
4ec2227a 5599 bs->stop = false;
7d4df6a4
DE
5600 /* Increase the hit count even though we don't stop. */
5601 ++(b->hit_count);
76727919 5602 gdb::observers::breakpoint_modified.notify (b);
d8a77e4c
AB
5603 return;
5604 }
5605
5606 if (bs->stop)
5607 infrun_debug_printf ("stopping at this breakpoint");
5608 else
5609 infrun_debug_printf ("not stopping at this breakpoint");
18a18393
VP
5610}
5611
1cf4d951
PA
5612/* Returns true if we need to track moribund locations of LOC's type
5613 on the current target. */
5614
d8de7963 5615static bool
b2ff9ed3 5616need_moribund_for_location_type (const struct bp_location *loc)
1cf4d951
PA
5617{
5618 return ((loc->loc_type == bp_loc_software_breakpoint
5619 && !target_supports_stopped_by_sw_breakpoint ())
5620 || (loc->loc_type == bp_loc_hardware_breakpoint
5621 && !target_supports_stopped_by_hw_breakpoint ()));
5622}
5623
ddfe970e 5624/* See breakpoint.h. */
c906108c 5625
313f3b21 5626bpstat *
ddfe970e 5627build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5628 const target_waitstatus &ws)
c906108c 5629{
313f3b21 5630 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5631
43892fdf 5632 for (breakpoint *b : all_breakpoints ())
429374b8 5633 {
1a853c52 5634 if (!breakpoint_enabled (b))
429374b8 5635 continue;
a5606eee 5636
40cb8ca5 5637 for (bp_location *bl : b->locations ())
429374b8 5638 {
4a64f543
MS
5639 /* For hardware watchpoints, we look only at the first
5640 location. The watchpoint_check function will work on the
5641 entire expression, not the individual locations. For
5642 read watchpoints, the watchpoints_triggered function has
5643 checked all locations already. */
429374b8
JK
5644 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5645 break;
18a18393 5646
b5fa468f 5647 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5648 continue;
c5aa993b 5649
09ac7c10 5650 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5651 continue;
c5aa993b 5652
4a64f543
MS
5653 /* Come here if it's a watchpoint, or if the break address
5654 matches. */
c5aa993b 5655
313f3b21 5656 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5657 explain stop. */
c5aa993b 5658
f431efe5
PA
5659 /* Assume we stop. Should we find a watchpoint that is not
5660 actually triggered, or if the condition of the breakpoint
5661 evaluates as false, we'll reset 'stop' to 0. */
4ec2227a
TT
5662 bs->stop = true;
5663 bs->print = true;
d983da9c 5664
f431efe5
PA
5665 /* If this is a scope breakpoint, mark the associated
5666 watchpoint as triggered so that we will handle the
5667 out-of-scope event. We'll get to the watchpoint next
5668 iteration. */
d0fb5eae 5669 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5670 {
5671 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5672
5673 w->watchpoint_triggered = watch_triggered_yes;
5674 }
f431efe5
PA
5675 }
5676 }
5677
7c16b83e 5678 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5679 if (!target_supports_stopped_by_sw_breakpoint ()
5680 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5681 {
1123588c 5682 for (bp_location *loc : moribund_locations)
f431efe5 5683 {
1cf4d951
PA
5684 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5685 && need_moribund_for_location_type (loc))
5686 {
313f3b21 5687 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951 5688 /* For hits of moribund locations, we should just proceed. */
4ec2227a
TT
5689 bs->stop = false;
5690 bs->print = false;
1cf4d951
PA
5691 bs->print_it = print_it_noop;
5692 }
f431efe5
PA
5693 }
5694 }
5695
ddfe970e
KS
5696 return bs_head;
5697}
5698
5699/* See breakpoint.h. */
5700
313f3b21 5701bpstat *
ddfe970e 5702bpstat_stop_status (const address_space *aspace,
00431a78 5703 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5704 const target_waitstatus &ws,
313f3b21 5705 bpstat *stop_chain)
ddfe970e
KS
5706{
5707 struct breakpoint *b = NULL;
5708 /* First item of allocated bpstat's. */
313f3b21
SM
5709 bpstat *bs_head = stop_chain;
5710 bpstat *bs;
ddfe970e
KS
5711 int need_remove_insert;
5712 int removed_any;
5713
5714 /* First, build the bpstat chain with locations that explain a
5715 target stop, while being careful to not set the target running,
5716 as that may invalidate locations (in particular watchpoint
5717 locations are recreated). Resuming will happen here with
5718 breakpoint conditions or watchpoint expressions that include
5719 inferior function calls. */
5720 if (bs_head == NULL)
5721 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5722
edcc5120
TT
5723 /* A bit of special processing for shlib breakpoints. We need to
5724 process solib loading here, so that the lists of loaded and
5725 unloaded libraries are correct before we handle "catch load" and
5726 "catch unload". */
5727 for (bs = bs_head; bs != NULL; bs = bs->next)
5728 {
5d268276 5729 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5730 {
5731 handle_solib_event ();
5732 break;
5733 }
5734 }
5735
f431efe5
PA
5736 /* Now go through the locations that caused the target to stop, and
5737 check whether we're interested in reporting this stop to higher
5738 layers, or whether we should resume the target transparently. */
5739
5740 removed_any = 0;
5741
5760d0ab 5742 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5743 {
5744 if (!bs->stop)
5745 continue;
5746
f431efe5 5747 b = bs->breakpoint_at;
c47614fe 5748 b->check_status (bs);
348d480f 5749 if (bs->stop)
28010a5d 5750 {
00431a78 5751 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5752
429374b8
JK
5753 if (bs->stop)
5754 {
5755 ++(b->hit_count);
c906108c 5756
4a64f543 5757 /* We will stop here. */
429374b8
JK
5758 if (b->disposition == disp_disable)
5759 {
816338b5 5760 --(b->enable_count);
1a853c52 5761 if (b->enable_count <= 0)
429374b8 5762 b->enable_state = bp_disabled;
f431efe5 5763 removed_any = 1;
429374b8 5764 }
bd7ccaa9 5765 gdb::observers::breakpoint_modified.notify (b);
429374b8 5766 if (b->silent)
4ec2227a 5767 bs->print = false;
429374b8 5768 bs->commands = b->commands;
abf85f46 5769 if (command_line_is_silent (bs->commands
d1b0a7bf 5770 ? bs->commands.get () : NULL))
4ec2227a 5771 bs->print = false;
9d6e6e84 5772
c47614fe 5773 b->after_condition_true (bs);
429374b8
JK
5774 }
5775
348d480f 5776 }
a9b3a50f
PA
5777
5778 /* Print nothing for this entry if we don't stop or don't
5779 print. */
5780 if (!bs->stop || !bs->print)
5781 bs->print_it = print_it_noop;
429374b8 5782 }
876fa593 5783
d983da9c
DJ
5784 /* If we aren't stopping, the value of some hardware watchpoint may
5785 not have changed, but the intermediate memory locations we are
5786 watching may have. Don't bother if we're stopping; this will get
5787 done later. */
d832cb68 5788 need_remove_insert = 0;
5760d0ab
JK
5789 if (! bpstat_causes_stop (bs_head))
5790 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5791 if (!bs->stop
f431efe5
PA
5792 && bs->breakpoint_at
5793 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5794 {
3a5c3e22
PA
5795 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5796
d8de7963 5797 update_watchpoint (w, false /* don't reparse. */);
d832cb68 5798 need_remove_insert = 1;
d983da9c
DJ
5799 }
5800
d832cb68 5801 if (need_remove_insert)
44702360 5802 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5803 else if (removed_any)
44702360 5804 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5805
5760d0ab 5806 return bs_head;
c906108c 5807}
628fe4e4 5808
d37e0847
PA
5809/* See breakpoint.h. */
5810
5811bpstat *
5812bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5813 thread_info *thread, const target_waitstatus &ws)
5814{
5815 gdb_assert (!target_stopped_by_watchpoint ());
5816
5817 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5818 previous stop to avoid confusing bpstat_stop_status. */
5819 watchpoints_triggered (ws);
5820
5821 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5822}
5823
628fe4e4 5824static void
61c26be8 5825handle_jit_event (CORE_ADDR address)
628fe4e4 5826{
628fe4e4
JK
5827 struct gdbarch *gdbarch;
5828
1eb8556f 5829 infrun_debug_printf ("handling bp_jit_event");
243a9253 5830
628fe4e4
JK
5831 /* Switch terminal for any messages produced by
5832 breakpoint_re_set. */
223ffa71 5833 target_terminal::ours_for_output ();
628fe4e4 5834
61c26be8
MS
5835 gdbarch = get_frame_arch (get_current_frame ());
5836 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5837 thus it is expected that its objectfile can be found through
5838 minimal symbol lookup. If it doesn't work (and assert fails), it
5839 most likely means that `jit_breakpoint_re_set` was changes and this
5840 function needs to be updated too. */
5841 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5842 gdb_assert (jit_bp_sym.objfile != nullptr);
508ccf9b
TV
5843 objfile *objfile = jit_bp_sym.objfile;
5844 if (objfile->separate_debug_objfile_backlink)
5845 objfile = objfile->separate_debug_objfile_backlink;
5846 jit_event_handler (gdbarch, objfile);
628fe4e4 5847
223ffa71 5848 target_terminal::inferior ();
628fe4e4
JK
5849}
5850
5851/* Prepare WHAT final decision for infrun. */
5852
5853/* Decide what infrun needs to do with this bpstat. */
5854
c906108c 5855struct bpstat_what
313f3b21 5856bpstat_what (bpstat *bs_head)
c906108c 5857{
c906108c 5858 struct bpstat_what retval;
313f3b21 5859 bpstat *bs;
c906108c 5860
628fe4e4 5861 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5862 retval.call_dummy = STOP_NONE;
e2d0f980 5863 retval.is_longjmp = false;
628fe4e4 5864
0e30163f 5865 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5866 {
628fe4e4
JK
5867 /* Extract this BS's action. After processing each BS, we check
5868 if its action overrides all we've seem so far. */
5869 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5870 enum bptype bptype;
5871
c906108c 5872 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5873 {
5874 /* I suspect this can happen if it was a momentary
5875 breakpoint which has since been deleted. */
5876 bptype = bp_none;
5877 }
20874c92 5878 else
f431efe5 5879 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5880
5881 switch (bptype)
c906108c
SS
5882 {
5883 case bp_none:
628fe4e4 5884 break;
c906108c
SS
5885 case bp_breakpoint:
5886 case bp_hardware_breakpoint:
7c16b83e 5887 case bp_single_step:
c906108c
SS
5888 case bp_until:
5889 case bp_finish:
a9b3a50f 5890 case bp_shlib_event:
c906108c
SS
5891 if (bs->stop)
5892 {
5893 if (bs->print)
628fe4e4 5894 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5895 else
628fe4e4 5896 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5897 }
5898 else
628fe4e4 5899 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5900 break;
5901 case bp_watchpoint:
5902 case bp_hardware_watchpoint:
5903 case bp_read_watchpoint:
5904 case bp_access_watchpoint:
5905 if (bs->stop)
5906 {
5907 if (bs->print)
628fe4e4 5908 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5909 else
628fe4e4 5910 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5911 }
5912 else
628fe4e4
JK
5913 {
5914 /* There was a watchpoint, but we're not stopping.
5915 This requires no further action. */
5916 }
c906108c
SS
5917 break;
5918 case bp_longjmp:
e2e4d78b 5919 case bp_longjmp_call_dummy:
186c406b 5920 case bp_exception:
0a39bb32
PA
5921 if (bs->stop)
5922 {
5923 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5924 retval.is_longjmp = bptype != bp_exception;
5925 }
5926 else
5927 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5928 break;
5929 case bp_longjmp_resume:
186c406b 5930 case bp_exception_resume:
0a39bb32
PA
5931 if (bs->stop)
5932 {
5933 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5934 retval.is_longjmp = bptype == bp_longjmp_resume;
5935 }
5936 else
5937 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5938 break;
5939 case bp_step_resume:
5940 if (bs->stop)
628fe4e4
JK
5941 this_action = BPSTAT_WHAT_STEP_RESUME;
5942 else
c906108c 5943 {
628fe4e4
JK
5944 /* It is for the wrong frame. */
5945 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5946 }
c906108c 5947 break;
2c03e5be
PA
5948 case bp_hp_step_resume:
5949 if (bs->stop)
5950 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5951 else
5952 {
5953 /* It is for the wrong frame. */
5954 this_action = BPSTAT_WHAT_SINGLE;
5955 }
5956 break;
c906108c 5957 case bp_watchpoint_scope:
c4093a6a 5958 case bp_thread_event:
1900040c 5959 case bp_overlay_event:
0fd8e87f 5960 case bp_longjmp_master:
aa7d318d 5961 case bp_std_terminate_master:
186c406b 5962 case bp_exception_master:
628fe4e4 5963 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5964 break;
ce78b96d 5965 case bp_catchpoint:
c5aa993b
JM
5966 if (bs->stop)
5967 {
5968 if (bs->print)
628fe4e4 5969 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5970 else
628fe4e4 5971 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5972 }
5973 else
628fe4e4 5974 {
cb1e4e32
PA
5975 /* Some catchpoints are implemented with breakpoints.
5976 For those, we need to step over the breakpoint. */
6e14e441
PA
5977 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5978 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
cb1e4e32 5979 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5980 }
5981 break;
628fe4e4 5982 case bp_jit_event:
628fe4e4 5983 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5984 break;
c906108c 5985 case bp_call_dummy:
53a5351d
JM
5986 /* Make sure the action is stop (silent or noisy),
5987 so infrun.c pops the dummy frame. */
aa7d318d 5988 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5989 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5990 break;
5991 case bp_std_terminate:
5992 /* Make sure the action is stop (silent or noisy),
5993 so infrun.c pops the dummy frame. */
aa7d318d 5994 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5995 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5996 break;
1042e4c0 5997 case bp_tracepoint:
7a697b8d 5998 case bp_fast_tracepoint:
0fb4aa4b 5999 case bp_static_tracepoint:
7b572efb 6000 case bp_static_marker_tracepoint:
1042e4c0
SS
6001 /* Tracepoint hits should not be reported back to GDB, and
6002 if one got through somehow, it should have been filtered
6003 out already. */
f34652de 6004 internal_error (_("bpstat_what: tracepoint encountered"));
0e30163f
JK
6005 break;
6006 case bp_gnu_ifunc_resolver:
6007 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
6008 this_action = BPSTAT_WHAT_SINGLE;
6009 break;
6010 case bp_gnu_ifunc_resolver_return:
6011 /* The breakpoint will be removed, execution will restart from the
6012 PC of the former breakpoint. */
6013 this_action = BPSTAT_WHAT_KEEP_CHECKING;
6014 break;
e7e0cddf
SS
6015
6016 case bp_dprintf:
a11cfd87
HZ
6017 if (bs->stop)
6018 this_action = BPSTAT_WHAT_STOP_SILENT;
6019 else
6020 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
6021 break;
6022
628fe4e4 6023 default:
f34652de 6024 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 6025 }
628fe4e4 6026
325fac50 6027 retval.main_action = std::max (retval.main_action, this_action);
c906108c 6028 }
628fe4e4 6029
243a9253
PA
6030 return retval;
6031}
628fe4e4 6032
243a9253 6033void
313f3b21 6034bpstat_run_callbacks (bpstat *bs_head)
243a9253 6035{
313f3b21 6036 bpstat *bs;
628fe4e4 6037
0e30163f
JK
6038 for (bs = bs_head; bs != NULL; bs = bs->next)
6039 {
6040 struct breakpoint *b = bs->breakpoint_at;
6041
6042 if (b == NULL)
6043 continue;
6044 switch (b->type)
6045 {
243a9253 6046 case bp_jit_event:
61c26be8 6047 handle_jit_event (bs->bp_location_at->address);
243a9253 6048 break;
0e30163f 6049 case bp_gnu_ifunc_resolver:
74421c0b 6050 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
0e30163f
JK
6051 break;
6052 case bp_gnu_ifunc_resolver_return:
74421c0b 6053 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
0e30163f
JK
6054 break;
6055 }
6056 }
c906108c
SS
6057}
6058
4c462cb0 6059/* See breakpoint.h. */
c906108c 6060
4c462cb0
SM
6061bool
6062bpstat_should_step ()
c906108c 6063{
43892fdf 6064 for (breakpoint *b : all_breakpoints ())
717a8278 6065 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 6066 return true;
43892fdf 6067
4c462cb0 6068 return false;
c906108c
SS
6069}
6070
4c462cb0
SM
6071/* See breakpoint.h. */
6072
6073bool
313f3b21 6074bpstat_causes_stop (bpstat *bs)
67822962
PA
6075{
6076 for (; bs != NULL; bs = bs->next)
6077 if (bs->stop)
4c462cb0 6078 return true;
67822962 6079
4c462cb0 6080 return false;
67822962
PA
6081}
6082
c906108c 6083\f
c5aa993b 6084
6c92c339 6085/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
6086 so it starts at the position corresponding to the table column
6087 named COL_NAME in the currently active table of UIOUT. */
6088
6c92c339 6089static int
170b53b2
UW
6090wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6091{
170b53b2 6092 int i, total_width, width, align;
c5209615 6093 const char *text;
170b53b2
UW
6094
6095 total_width = 0;
112e8700 6096 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6097 {
6098 if (strcmp (text, col_name) == 0)
6c92c339 6099 return total_width;
170b53b2
UW
6100
6101 total_width += width + 1;
6102 }
6103
6c92c339 6104 return 0;
170b53b2
UW
6105}
6106
b775012e
LM
6107/* Determine if the locations of this breakpoint will have their conditions
6108 evaluated by the target, host or a mix of both. Returns the following:
6109
6110 "host": Host evals condition.
6111 "host or target": Host or Target evals condition.
6112 "target": Target evals condition.
6113*/
6114
6115static const char *
a67bcaba 6116bp_condition_evaluator (const breakpoint *b)
b775012e 6117{
b775012e
LM
6118 char host_evals = 0;
6119 char target_evals = 0;
6120
6121 if (!b)
6122 return NULL;
6123
6124 if (!is_breakpoint (b))
6125 return NULL;
6126
6127 if (gdb_evaluates_breakpoint_condition_p ()
6128 || !target_supports_evaluation_of_breakpoint_conditions ())
6129 return condition_evaluation_host;
6130
40cb8ca5 6131 for (bp_location *bl : b->locations ())
b775012e
LM
6132 {
6133 if (bl->cond_bytecode)
6134 target_evals++;
6135 else
6136 host_evals++;
6137 }
6138
6139 if (host_evals && target_evals)
6140 return condition_evaluation_both;
6141 else if (target_evals)
6142 return condition_evaluation_target;
6143 else
6144 return condition_evaluation_host;
6145}
6146
6147/* Determine the breakpoint location's condition evaluator. This is
6148 similar to bp_condition_evaluator, but for locations. */
6149
6150static const char *
b2ff9ed3 6151bp_location_condition_evaluator (const struct bp_location *bl)
b775012e
LM
6152{
6153 if (bl && !is_breakpoint (bl->owner))
6154 return NULL;
6155
6156 if (gdb_evaluates_breakpoint_condition_p ()
6157 || !target_supports_evaluation_of_breakpoint_conditions ())
6158 return condition_evaluation_host;
6159
6160 if (bl && bl->cond_bytecode)
6161 return condition_evaluation_target;
6162 else
6163 return condition_evaluation_host;
6164}
6165
859825b8
JK
6166/* Print the LOC location out of the list of B->LOC locations. */
6167
170b53b2 6168static void
a67bcaba 6169print_breakpoint_location (const breakpoint *b,
170b53b2 6170 struct bp_location *loc)
0d381245 6171{
79a45e25 6172 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6173
6174 scoped_restore_current_program_space restore_pspace;
6c95b8df 6175
859825b8
JK
6176 if (loc != NULL && loc->shlib_disabled)
6177 loc = NULL;
6178
6c95b8df
PA
6179 if (loc != NULL)
6180 set_current_program_space (loc->pspace);
6181
56435ebe 6182 if (b->display_canonical)
709438c7 6183 uiout->field_string ("what", b->locspec->to_string ());
2f202fde 6184 else if (loc && loc->symtab)
0d381245 6185 {
4a27f119
KS
6186 const struct symbol *sym = loc->symbol;
6187
0d381245
VP
6188 if (sym)
6189 {
112e8700 6190 uiout->text ("in ");
987012b8 6191 uiout->field_string ("func", sym->print_name (),
e43b10e1 6192 function_name_style.style ());
112e8700
SM
6193 uiout->text (" ");
6194 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6195 uiout->text ("at ");
0d381245 6196 }
112e8700 6197 uiout->field_string ("file",
cbe56571 6198 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6199 file_name_style.style ());
112e8700 6200 uiout->text (":");
05cba821 6201
112e8700
SM
6202 if (uiout->is_mi_like_p ())
6203 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6204
381befee 6205 uiout->field_signed ("line", loc->line_number);
0d381245 6206 }
859825b8 6207 else if (loc)
0d381245 6208 {
d7e74731 6209 string_file stb;
170b53b2 6210
d7e74731 6211 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6212 demangle, "");
112e8700 6213 uiout->field_stream ("at", stb);
0d381245 6214 }
859825b8 6215 else
f00aae0f 6216 {
709438c7 6217 uiout->field_string ("pending", b->locspec->to_string ());
f00aae0f
KS
6218 /* If extra_string is available, it could be holding a condition
6219 or dprintf arguments. In either case, make sure it is printed,
6220 too, but only for non-MI streams. */
112e8700 6221 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6222 {
6223 if (b->type == bp_dprintf)
112e8700 6224 uiout->text (",");
f00aae0f 6225 else
112e8700 6226 uiout->text (" ");
6f781ee3 6227 uiout->text (b->extra_string.get ());
f00aae0f
KS
6228 }
6229 }
6c95b8df 6230
b775012e
LM
6231 if (loc && is_breakpoint (b)
6232 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6233 && bp_condition_evaluator (b) == condition_evaluation_both)
6234 {
112e8700
SM
6235 uiout->text (" (");
6236 uiout->field_string ("evaluated-by",
b775012e 6237 bp_location_condition_evaluator (loc));
112e8700 6238 uiout->text (")");
b775012e 6239 }
0d381245
VP
6240}
6241
269b11a2
PA
6242static const char *
6243bptype_string (enum bptype type)
c906108c 6244{
c4093a6a
JM
6245 struct ep_type_description
6246 {
6247 enum bptype type;
a121b7c1 6248 const char *description;
c4093a6a
JM
6249 };
6250 static struct ep_type_description bptypes[] =
c906108c 6251 {
c5aa993b
JM
6252 {bp_none, "?deleted?"},
6253 {bp_breakpoint, "breakpoint"},
c906108c 6254 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6255 {bp_single_step, "sw single-step"},
c5aa993b
JM
6256 {bp_until, "until"},
6257 {bp_finish, "finish"},
6258 {bp_watchpoint, "watchpoint"},
c906108c 6259 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6260 {bp_read_watchpoint, "read watchpoint"},
6261 {bp_access_watchpoint, "acc watchpoint"},
6262 {bp_longjmp, "longjmp"},
6263 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6264 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6265 {bp_exception, "exception"},
6266 {bp_exception_resume, "exception resume"},
c5aa993b 6267 {bp_step_resume, "step resume"},
2c03e5be 6268 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6269 {bp_watchpoint_scope, "watchpoint scope"},
6270 {bp_call_dummy, "call dummy"},
aa7d318d 6271 {bp_std_terminate, "std::terminate"},
c5aa993b 6272 {bp_shlib_event, "shlib events"},
c4093a6a 6273 {bp_thread_event, "thread events"},
1900040c 6274 {bp_overlay_event, "overlay events"},
0fd8e87f 6275 {bp_longjmp_master, "longjmp master"},
aa7d318d 6276 {bp_std_terminate_master, "std::terminate master"},
186c406b 6277 {bp_exception_master, "exception master"},
ce78b96d 6278 {bp_catchpoint, "catchpoint"},
1042e4c0 6279 {bp_tracepoint, "tracepoint"},
7a697b8d 6280 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6281 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6282 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6283 {bp_dprintf, "dprintf"},
4efc6507 6284 {bp_jit_event, "jit events"},
0e30163f
JK
6285 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6286 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6287 };
269b11a2
PA
6288
6289 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6290 || ((int) type != bptypes[(int) type].type))
f34652de 6291 internal_error (_("bptypes table does not describe type #%d."),
269b11a2
PA
6292 (int) type);
6293
6294 return bptypes[(int) type].description;
6295}
6296
998580f1
MK
6297/* For MI, output a field named 'thread-groups' with a list as the value.
6298 For CLI, prefix the list with the string 'inf'. */
6299
6300static void
6301output_thread_groups (struct ui_out *uiout,
6302 const char *field_name,
5c632425 6303 const std::vector<int> &inf_nums,
998580f1
MK
6304 int mi_only)
6305{
112e8700 6306 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6307
6308 /* For backward compatibility, don't display inferiors in CLI unless
6309 there are several. Always display them for MI. */
6310 if (!is_mi && mi_only)
6311 return;
6312
10f489e5 6313 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6314
5c632425 6315 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6316 {
6317 if (is_mi)
6318 {
6319 char mi_group[10];
6320
5c632425 6321 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6322 uiout->field_string (NULL, mi_group);
998580f1
MK
6323 }
6324 else
6325 {
6326 if (i == 0)
112e8700 6327 uiout->text (" inf ");
998580f1 6328 else
112e8700 6329 uiout->text (", ");
998580f1 6330
5c632425 6331 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6332 }
6333 }
998580f1
MK
6334}
6335
9db0d853
SM
6336/* See breakpoint.h. */
6337
6338bool fix_breakpoint_script_output_globally = false;
6339
a38118e5
PA
6340/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6341 instead of going via breakpoint_ops::print_one. This makes "maint
6342 info breakpoints" show the software breakpoint locations of
6343 catchpoints, which are considered internal implementation
c01e038b
TT
6344 detail. Returns true if RAW_LOC is false and if the breakpoint's
6345 print_one method did something; false otherwise. */
269b11a2 6346
c01e038b 6347static bool
269b11a2
PA
6348print_one_breakpoint_location (struct breakpoint *b,
6349 struct bp_location *loc,
6350 int loc_number,
6351 struct bp_location **last_loc,
a38118e5 6352 int allflag, bool raw_loc)
269b11a2
PA
6353{
6354 struct command_line *l;
c2c6d25f 6355 static char bpenables[] = "nynny";
c906108c 6356
79a45e25 6357 struct ui_out *uiout = current_uiout;
d8de7963
AB
6358 bool header_of_multiple = false;
6359 bool part_of_multiple = (loc != NULL);
79a45b7d
TT
6360 struct value_print_options opts;
6361
6362 get_user_print_options (&opts);
0d381245
VP
6363
6364 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6365 /* See comment in print_one_breakpoint concerning treatment of
6366 breakpoints with single disabled location. */
0d381245
VP
6367 if (loc == NULL
6368 && (b->loc != NULL
b5fa468f
TBA
6369 && (b->loc->next != NULL
6370 || !b->loc->enabled || b->loc->disabled_by_cond)))
d8de7963 6371 header_of_multiple = true;
0d381245
VP
6372 if (loc == NULL)
6373 loc = b->loc;
6374
c4093a6a
JM
6375 annotate_record ();
6376
6377 /* 1 */
6378 annotate_field (0);
0d381245 6379 if (part_of_multiple)
528e1572 6380 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6381 else
381befee 6382 uiout->field_signed ("number", b->number);
c4093a6a
JM
6383
6384 /* 2 */
6385 annotate_field (1);
0d381245 6386 if (part_of_multiple)
112e8700 6387 uiout->field_skip ("type");
269b11a2 6388 else
112e8700 6389 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6390
6391 /* 3 */
6392 annotate_field (2);
0d381245 6393 if (part_of_multiple)
112e8700 6394 uiout->field_skip ("disp");
0d381245 6395 else
112e8700 6396 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6397
c4093a6a
JM
6398 /* 4 */
6399 annotate_field (3);
0d381245 6400 if (part_of_multiple)
fbcda577
PA
6401 {
6402 /* For locations that are disabled because of an invalid
6403 condition, display "N*" on the CLI, where "*" refers to a
6404 footnote below the table. For MI, simply display a "N"
6405 without a footnote. On the CLI, for enabled locations whose
6406 breakpoint is disabled, display "y-". */
6407 auto get_enable_state = [uiout, loc] () -> const char *
287de656 6408 {
fbcda577
PA
6409 if (uiout->is_mi_like_p ())
6410 {
6411 if (loc->disabled_by_cond)
6412 return "N";
6413 else if (!loc->enabled)
6414 return "n";
6415 else
6416 return "y";
6417 }
6418 else
6419 {
6420 if (loc->disabled_by_cond)
6421 return "N*";
6422 else if (!loc->enabled)
6423 return "n";
6424 else if (!breakpoint_enabled (loc->owner))
6425 return "y-";
6426 else
6427 return "y";
6428 }
6429 };
6430 uiout->field_string ("enabled", get_enable_state ());
6431 }
0d381245 6432 else
112e8700 6433 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6434
c4093a6a 6435 /* 5 and 6 */
c01e038b 6436 bool result = false;
c47614fe 6437 if (!raw_loc && b->print_one (last_loc))
c01e038b 6438 result = true;
3086aeae 6439 else
a38118e5
PA
6440 {
6441 if (is_watchpoint (b))
3a5c3e22
PA
6442 {
6443 struct watchpoint *w = (struct watchpoint *) b;
6444
6445 /* Field 4, the address, is omitted (which makes the columns
6446 not line up too nicely with the headers, but the effect
6447 is relatively readable). */
6448 if (opts.addressprint)
112e8700 6449 uiout->field_skip ("addr");
3a5c3e22 6450 annotate_field (5);
a4c50be3 6451 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6452 }
f06f1252
TT
6453 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6454 || is_ada_exception_catchpoint (b))
a38118e5
PA
6455 {
6456 if (opts.addressprint)
6457 {
6458 annotate_field (4);
6459 if (header_of_multiple)
7f6aba03
TT
6460 uiout->field_string ("addr", "<MULTIPLE>",
6461 metadata_style.style ());
a38118e5 6462 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6463 uiout->field_string ("addr", "<PENDING>",
6464 metadata_style.style ());
a38118e5
PA
6465 else
6466 uiout->field_core_addr ("addr",
6467 loc->gdbarch, loc->address);
6468 }
6469 annotate_field (5);
6470 if (!header_of_multiple)
6471 print_breakpoint_location (b, loc);
6472 if (b->loc)
6473 *last_loc = b->loc;
6474 }
6475 }
6c95b8df 6476
998580f1 6477 if (loc != NULL && !header_of_multiple)
6c95b8df 6478 {
5c632425 6479 std::vector<int> inf_nums;
998580f1 6480 int mi_only = 1;
6c95b8df 6481
08036331 6482 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6483 {
6484 if (inf->pspace == loc->pspace)
5c632425 6485 inf_nums.push_back (inf->num);
6c95b8df 6486 }
998580f1 6487
7594741e
AB
6488 /* For backward compatibility, don't display inferiors in CLI unless
6489 there are several. Always display for MI. */
6490 if (allflag
6491 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6492 && (program_spaces.size () > 1
6493 || number_of_inferiors () > 1)
6494 /* LOC is for existing B, it cannot be in
6495 moribund_locations and thus having NULL OWNER. */
6496 && loc->owner->type != bp_catchpoint))
998580f1 6497 mi_only = 0;
5c632425 6498 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6499 }
6500
944b1b18
AB
6501 /* In the MI output, each location of a thread or task specific
6502 breakpoint includes the relevant thread or task ID. This is done for
6503 backwards compatibility reasons.
6504
6505 For the CLI output, the thread/task information is printed on a
6506 separate line, see the 'stop only in thread' and 'stop only in task'
6507 output below. */
2fd9a436 6508 if (part_of_multiple && uiout->is_mi_like_p ())
c4093a6a 6509 {
4a306c9a 6510 if (b->thread != -1)
944b1b18 6511 uiout->field_signed ("thread", b->thread);
2ecee236 6512 else if (b->task != -1)
944b1b18 6513 uiout->field_signed ("task", b->task);
c4093a6a 6514 }
f1310107 6515
112e8700 6516 uiout->text ("\n");
f1310107 6517
348d480f 6518 if (!part_of_multiple)
c47614fe 6519 b->print_one_detail (uiout);
f1310107 6520
0d381245 6521 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6522 {
6523 annotate_field (6);
112e8700 6524 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6525 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6526 the frame ID. */
112e8700 6527 uiout->field_core_addr ("frame",
5af949e3 6528 b->gdbarch, b->frame_id.stack_addr);
112e8700 6529 uiout->text ("\n");
c4093a6a
JM
6530 }
6531
28010a5d 6532 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6533 {
6534 annotate_field (7);
d77f58be 6535 if (is_tracepoint (b))
112e8700 6536 uiout->text ("\ttrace only if ");
1042e4c0 6537 else
112e8700 6538 uiout->text ("\tstop only if ");
6f781ee3 6539 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6540
6541 /* Print whether the target is doing the breakpoint's condition
6542 evaluation. If GDB is doing the evaluation, don't print anything. */
6543 if (is_breakpoint (b)
6544 && breakpoint_condition_evaluation_mode ()
6545 == condition_evaluation_target)
6546 {
6a831f06
PA
6547 uiout->message (" (%pF evals)",
6548 string_field ("evaluated-by",
6549 bp_condition_evaluator (b)));
b775012e 6550 }
112e8700 6551 uiout->text ("\n");
0101ce28
JJ
6552 }
6553
0d381245 6554 if (!part_of_multiple && b->thread != -1)
c4093a6a 6555 {
4a64f543 6556 /* FIXME should make an annotation for this. */
112e8700
SM
6557 uiout->text ("\tstop only in thread ");
6558 if (uiout->is_mi_like_p ())
381befee 6559 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6560 else
6561 {
6562 struct thread_info *thr = find_thread_global_id (b->thread);
6563
112e8700 6564 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6565 }
112e8700 6566 uiout->text ("\n");
c4093a6a 6567 }
944b1b18 6568
2ecee236 6569 if (!part_of_multiple && b->task != -1)
944b1b18
AB
6570 {
6571 uiout->text ("\tstop only in task ");
6572 uiout->field_signed ("task", b->task);
6573 uiout->text ("\n");
6574 }
6575
556ec64d
YQ
6576 if (!part_of_multiple)
6577 {
6578 if (b->hit_count)
31f56a27
YQ
6579 {
6580 /* FIXME should make an annotation for this. */
6581 if (is_catchpoint (b))
112e8700 6582 uiout->text ("\tcatchpoint");
31f56a27 6583 else if (is_tracepoint (b))
112e8700 6584 uiout->text ("\ttracepoint");
31f56a27 6585 else
112e8700
SM
6586 uiout->text ("\tbreakpoint");
6587 uiout->text (" already hit ");
381befee 6588 uiout->field_signed ("times", b->hit_count);
31f56a27 6589 if (b->hit_count == 1)
112e8700 6590 uiout->text (" time\n");
31f56a27 6591 else
112e8700 6592 uiout->text (" times\n");
31f56a27 6593 }
556ec64d
YQ
6594 else
6595 {
31f56a27 6596 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6597 if (uiout->is_mi_like_p ())
381befee 6598 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6599 }
6600 }
8b93c638 6601
0d381245 6602 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6603 {
6604 annotate_field (8);
6a831f06
PA
6605 uiout->message ("\tignore next %pF hits\n",
6606 signed_field ("ignore", b->ignore_count));
c4093a6a 6607 }
059fb39f 6608
816338b5
SS
6609 /* Note that an enable count of 1 corresponds to "enable once"
6610 behavior, which is reported by the combination of enablement and
6611 disposition, so we don't need to mention it here. */
6612 if (!part_of_multiple && b->enable_count > 1)
6613 {
6614 annotate_field (8);
112e8700 6615 uiout->text ("\tdisable after ");
816338b5
SS
6616 /* Tweak the wording to clarify that ignore and enable counts
6617 are distinct, and have additive effect. */
6618 if (b->ignore_count)
112e8700 6619 uiout->text ("additional ");
816338b5 6620 else
112e8700 6621 uiout->text ("next ");
381befee 6622 uiout->field_signed ("enable", b->enable_count);
112e8700 6623 uiout->text (" hits\n");
816338b5
SS
6624 }
6625
f196051f
SS
6626 if (!part_of_multiple && is_tracepoint (b))
6627 {
6628 struct tracepoint *tp = (struct tracepoint *) b;
6629
6630 if (tp->traceframe_usage)
6631 {
112e8700 6632 uiout->text ("\ttrace buffer usage ");
381befee 6633 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6634 uiout->text (" bytes\n");
f196051f
SS
6635 }
6636 }
d3ce09f5 6637
d1b0a7bf 6638 l = b->commands ? b->commands.get () : NULL;
059fb39f 6639 if (!part_of_multiple && l)
c4093a6a
JM
6640 {
6641 annotate_field (9);
9db0d853
SM
6642
6643 bool use_fixed_output =
6644 (uiout->test_flags (fix_breakpoint_script_output)
287de656 6645 || fix_breakpoint_script_output_globally);
9db0d853
SM
6646
6647 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6648 gdb::optional<ui_out_emit_list> list_emitter;
6649
6650 if (use_fixed_output)
6651 list_emitter.emplace (uiout, "script");
6652 else
6653 tuple_emitter.emplace (uiout, "script");
6654
8b93c638 6655 print_command_lines (uiout, l, 4);
c4093a6a 6656 }
d24317b4 6657
d9b3f62e 6658 if (is_tracepoint (b))
1042e4c0 6659 {
d9b3f62e
PA
6660 struct tracepoint *t = (struct tracepoint *) b;
6661
6662 if (!part_of_multiple && t->pass_count)
6663 {
6664 annotate_field (10);
112e8700 6665 uiout->text ("\tpass count ");
381befee 6666 uiout->field_signed ("pass", t->pass_count);
112e8700 6667 uiout->text (" \n");
d9b3f62e 6668 }
f2a8bc8a
YQ
6669
6670 /* Don't display it when tracepoint or tracepoint location is
6671 pending. */
6672 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6673 {
6674 annotate_field (11);
6675
112e8700
SM
6676 if (uiout->is_mi_like_p ())
6677 uiout->field_string ("installed",
f2a8bc8a
YQ
6678 loc->inserted ? "y" : "n");
6679 else
6680 {
6681 if (loc->inserted)
112e8700 6682 uiout->text ("\t");
f2a8bc8a 6683 else
112e8700
SM
6684 uiout->text ("\tnot ");
6685 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6686 }
6687 }
1042e4c0
SS
6688 }
6689
112e8700 6690 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6691 {
3a5c3e22
PA
6692 if (is_watchpoint (b))
6693 {
6694 struct watchpoint *w = (struct watchpoint *) b;
6695
a4c50be3 6696 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6697 }
264f9890
PA
6698 else if (b->locspec != nullptr)
6699 {
709438c7 6700 const char *str = b->locspec->to_string ();
264f9890
PA
6701 if (str != nullptr)
6702 uiout->field_string ("original-location", str);
6703 }
d24317b4 6704 }
c01e038b
TT
6705
6706 return result;
c4093a6a 6707}
c5aa993b 6708
13674803
SM
6709/* See breakpoint.h. */
6710
6711bool fix_multi_location_breakpoint_output_globally = false;
6712
0d381245
VP
6713static void
6714print_one_breakpoint (struct breakpoint *b,
4a64f543 6715 struct bp_location **last_loc,
6c95b8df 6716 int allflag)
0d381245 6717{
79a45e25 6718 struct ui_out *uiout = current_uiout;
13674803
SM
6719 bool use_fixed_output
6720 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6721 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6722
b4be1b06 6723 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
c01e038b
TT
6724 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6725 allflag, false);
8d3788bd 6726
b4be1b06
SM
6727 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6728 are outside. */
6729 if (!use_fixed_output)
6730 bkpt_tuple_emitter.reset ();
0d381245
VP
6731
6732 /* If this breakpoint has custom print function,
6733 it's already printed. Otherwise, print individual
6734 locations, if any. */
c01e038b 6735 if (!printed || allflag)
0d381245 6736 {
4a64f543
MS
6737 /* If breakpoint has a single location that is disabled, we
6738 print it as if it had several locations, since otherwise it's
6739 hard to represent "breakpoint enabled, location disabled"
6740 situation.
6741
6742 Note that while hardware watchpoints have several locations
a38118e5
PA
6743 internally, that's not a property exposed to users.
6744
6745 Likewise, while catchpoints may be implemented with
6746 breakpoints (e.g., catch throw), that's not a property
6747 exposed to users. We do however display the internal
6748 breakpoint locations with "maint info breakpoints". */
6749 if (!is_hardware_watchpoint (b)
f06f1252
TT
6750 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6751 || is_ada_exception_catchpoint (b))
a38118e5 6752 && (allflag
b5fa468f
TBA
6753 || (b->loc && (b->loc->next
6754 || !b->loc->enabled
6755 || b->loc->disabled_by_cond))))
0d381245 6756 {
b4be1b06
SM
6757 gdb::optional<ui_out_emit_list> locations_list;
6758
6759 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6760 MI record. For later versions, place breakpoint locations in a
6761 list. */
6762 if (uiout->is_mi_like_p () && use_fixed_output)
6763 locations_list.emplace (uiout, "locations");
8d3788bd 6764
b4be1b06 6765 int n = 1;
40cb8ca5 6766 for (bp_location *loc : b->locations ())
8d3788bd 6767 {
b4be1b06 6768 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6769 print_one_breakpoint_location (b, loc, n, last_loc,
6770 allflag, allflag);
40cb8ca5 6771 n++;
8d3788bd 6772 }
0d381245
VP
6773 }
6774 }
6775}
6776
a6d9a66e
UW
6777static int
6778breakpoint_address_bits (struct breakpoint *b)
6779{
6780 int print_address_bits = 0;
a6d9a66e 6781
40cb8ca5 6782 for (bp_location *loc : b->locations ())
a6d9a66e 6783 {
6e14e441
PA
6784 if (!bl_address_is_meaningful (loc))
6785 continue;
c7437ca6 6786
6e14e441 6787 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6788 if (addr_bit > print_address_bits)
6789 print_address_bits = addr_bit;
6790 }
6791
6792 return print_address_bits;
6793}
0d381245 6794
65630365 6795/* See breakpoint.h. */
c5aa993b 6796
65630365
PA
6797void
6798print_breakpoint (breakpoint *b)
c4093a6a 6799{
a6d9a66e 6800 struct bp_location *dummy_loc = NULL;
65630365 6801 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6802}
c5aa993b 6803
09d682a4
TT
6804/* Return true if this breakpoint was set by the user, false if it is
6805 internal or momentary. */
6806
6807int
6808user_breakpoint_p (struct breakpoint *b)
6809{
46c6471b 6810 return b->number > 0;
09d682a4
TT
6811}
6812
93daf339
TT
6813/* See breakpoint.h. */
6814
6815int
6816pending_breakpoint_p (struct breakpoint *b)
6817{
6818 return b->loc == NULL;
6819}
6820
5c458ae8
SM
6821/* Print information on breakpoints (including watchpoints and tracepoints).
6822
6823 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6824 understood by number_or_range_parser. Only breakpoints included in this
6825 list are then printed.
6826
6827 If SHOW_INTERNAL is true, print internal breakpoints.
6828
6829 If FILTER is non-NULL, call it on each breakpoint and only include the
6830 ones for which it returns true.
6831
6832 Return the total number of breakpoints listed. */
c906108c 6833
d77f58be 6834static int
5c458ae8 6835breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6836 bool (*filter) (const struct breakpoint *))
c4093a6a 6837{
a6d9a66e 6838 struct bp_location *last_loc = NULL;
7f3b0473 6839 int nr_printable_breakpoints;
79a45b7d 6840 struct value_print_options opts;
a6d9a66e 6841 int print_address_bits = 0;
269b11a2 6842 int print_type_col_width = 14;
79a45e25 6843 struct ui_out *uiout = current_uiout;
b5fa468f 6844 bool has_disabled_by_cond_location = false;
269b11a2 6845
79a45b7d
TT
6846 get_user_print_options (&opts);
6847
4a64f543
MS
6848 /* Compute the number of rows in the table, as well as the size
6849 required for address fields. */
7f3b0473 6850 nr_printable_breakpoints = 0;
43892fdf 6851 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6852 {
6853 /* If we have a filter, only list the breakpoints it accepts. */
6854 if (filter && !filter (b))
6855 continue;
6856
5c458ae8 6857 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6858 accept. Skip the others. */
5c458ae8 6859 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6860 {
5c458ae8 6861 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6862 continue;
5c458ae8 6863 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6864 continue;
6865 }
269b11a2 6866
5c458ae8 6867 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6868 {
6869 int addr_bit, type_len;
a6d9a66e 6870
e5a67952
MS
6871 addr_bit = breakpoint_address_bits (b);
6872 if (addr_bit > print_address_bits)
6873 print_address_bits = addr_bit;
269b11a2 6874
e5a67952
MS
6875 type_len = strlen (bptype_string (b->type));
6876 if (type_len > print_type_col_width)
6877 print_type_col_width = type_len;
6878
6879 nr_printable_breakpoints++;
6880 }
6881 }
7f3b0473 6882
4a2b031d
TT
6883 {
6884 ui_out_emit_table table_emitter (uiout,
6885 opts.addressprint ? 6 : 5,
6886 nr_printable_breakpoints,
6887 "BreakpointTable");
6888
6889 if (nr_printable_breakpoints > 0)
6890 annotate_breakpoints_headers ();
6891 if (nr_printable_breakpoints > 0)
6892 annotate_field (0);
6893 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6894 if (nr_printable_breakpoints > 0)
6895 annotate_field (1);
6896 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6897 if (nr_printable_breakpoints > 0)
6898 annotate_field (2);
6899 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6900 if (nr_printable_breakpoints > 0)
6901 annotate_field (3);
6902 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6903 if (opts.addressprint)
6904 {
6905 if (nr_printable_breakpoints > 0)
6906 annotate_field (4);
6907 if (print_address_bits <= 32)
6908 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6909 else
6910 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6911 }
6912 if (nr_printable_breakpoints > 0)
6913 annotate_field (5);
6914 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6915 uiout->table_body ();
6916 if (nr_printable_breakpoints > 0)
6917 annotate_breakpoints_table ();
6918
43892fdf 6919 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6920 {
6921 QUIT;
6922 /* If we have a filter, only list the breakpoints it accepts. */
6923 if (filter && !filter (b))
6924 continue;
e5a67952 6925
5c458ae8 6926 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6927 accept. Skip the others. */
e5a67952 6928
5c458ae8 6929 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6930 {
5c458ae8 6931 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6932 {
5c458ae8 6933 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6934 continue;
6935 }
6936 else /* all others */
6937 {
5c458ae8 6938 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6939 continue;
6940 }
6941 }
6942 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6943 show_internal is set. */
6944 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6945 {
6946 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6947 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6948 if (loc->disabled_by_cond)
6949 has_disabled_by_cond_location = true;
6950 }
4a2b031d
TT
6951 }
6952 }
698384cd 6953
7f3b0473 6954 if (nr_printable_breakpoints == 0)
c906108c 6955 {
4a64f543
MS
6956 /* If there's a filter, let the caller decide how to report
6957 empty list. */
d77f58be
SS
6958 if (!filter)
6959 {
5c458ae8 6960 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6961 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6962 else
112e8700 6963 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6964 bp_num_list);
d77f58be 6965 }
c906108c
SS
6966 }
6967 else
c4093a6a 6968 {
a6d9a66e
UW
6969 if (last_loc && !server_command)
6970 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6971
85c88e2a 6972 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6973 uiout->message (_("(*): Breakpoint condition is invalid at this "
6974 "location.\n"));
c4093a6a 6975 }
c906108c 6976
4a64f543 6977 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6978 there have been breakpoints? */
c906108c 6979 annotate_breakpoints_table_end ();
d77f58be
SS
6980
6981 return nr_printable_breakpoints;
c906108c
SS
6982}
6983
ad443146
SS
6984/* Display the value of default-collect in a way that is generally
6985 compatible with the breakpoint list. */
6986
6987static void
6988default_collect_info (void)
6989{
79a45e25
PA
6990 struct ui_out *uiout = current_uiout;
6991
ad443146
SS
6992 /* If it has no value (which is frequently the case), say nothing; a
6993 message like "No default-collect." gets in user's face when it's
6994 not wanted. */
e0700ba4 6995 if (default_collect.empty ())
ad443146
SS
6996 return;
6997
6998 /* The following phrase lines up nicely with per-tracepoint collect
6999 actions. */
112e8700
SM
7000 uiout->text ("default collect ");
7001 uiout->field_string ("default-collect", default_collect);
7002 uiout->text (" \n");
ad443146
SS
7003}
7004
c906108c 7005static void
0b39b52e 7006info_breakpoints_command (const char *args, int from_tty)
c906108c 7007{
5c458ae8 7008 breakpoint_1 (args, false, NULL);
ad443146
SS
7009
7010 default_collect_info ();
d77f58be
SS
7011}
7012
7013static void
1d12d88f 7014info_watchpoints_command (const char *args, int from_tty)
d77f58be 7015{
5c458ae8 7016 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 7017 struct ui_out *uiout = current_uiout;
d77f58be
SS
7018
7019 if (num_printed == 0)
7020 {
e5a67952 7021 if (args == NULL || *args == '\0')
112e8700 7022 uiout->message ("No watchpoints.\n");
d77f58be 7023 else
112e8700 7024 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 7025 }
c906108c
SS
7026}
7027
7a292a7a 7028static void
4495129a 7029maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 7030{
5c458ae8 7031 breakpoint_1 (args, true, NULL);
ad443146
SS
7032
7033 default_collect_info ();
c906108c
SS
7034}
7035
d8de7963 7036static bool
714835d5 7037breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7038 struct program_space *pspace,
714835d5 7039 CORE_ADDR pc, struct obj_section *section)
0d381245 7040{
40cb8ca5 7041 for (bp_location *bl : b->locations ())
0d381245 7042 {
6c95b8df
PA
7043 if (bl->pspace == pspace
7044 && bl->address == pc
0d381245 7045 && (!overlay_debugging || bl->section == section))
d8de7963 7046 return true;
0d381245 7047 }
d8de7963 7048 return false;
0d381245
VP
7049}
7050
bd21b6c9 7051/* See breakpoint.h. */
c906108c 7052
bd21b6c9 7053void
6c95b8df
PA
7054describe_other_breakpoints (struct gdbarch *gdbarch,
7055 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7056 struct obj_section *section, int thread)
c906108c 7057{
52f0bd74 7058 int others = 0;
c906108c 7059
43892fdf 7060 for (breakpoint *b : all_breakpoints ())
672f9b60 7061 others += (user_breakpoint_p (b)
dda83cd7 7062 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 7063
c906108c
SS
7064 if (others > 0)
7065 {
a3f17187 7066 if (others == 1)
6cb06a8c 7067 gdb_printf (_("Note: breakpoint "));
a3f17187 7068 else /* if (others == ???) */
6cb06a8c 7069 gdb_printf (_("Note: breakpoints "));
43892fdf 7070 for (breakpoint *b : all_breakpoints ())
672f9b60 7071 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7072 {
7073 others--;
6cb06a8c 7074 gdb_printf ("%d", b->number);
0d381245 7075 if (b->thread == -1 && thread != -1)
6cb06a8c 7076 gdb_printf (" (all threads)");
0d381245 7077 else if (b->thread != -1)
ce068c5f
AB
7078 {
7079 struct thread_info *thr = find_thread_global_id (b->thread);
7080 gdb_printf (" (thread %s)", print_thread_id (thr));
7081 }
8282ad74 7082 else if (b->task != -1)
f1f517e8 7083 gdb_printf (" (task %d)", b->task);
6cb06a8c
TT
7084 gdb_printf ("%s%s ",
7085 ((b->enable_state == bp_disabled
7086 || b->enable_state == bp_call_disabled)
7087 ? " (disabled)"
7088 : ""),
7089 (others > 1) ? ","
7090 : ((others == 1) ? " and" : ""));
0d381245 7091 }
6a831f06
PA
7092 current_uiout->message (_("also set at pc %ps.\n"),
7093 styled_string (address_style.style (),
7094 paddress (gdbarch, pc)));
c906108c
SS
7095 }
7096}
7097\f
c906108c 7098
cb1e4e32
PA
7099/* Return true iff it is meaningful to use the address member of LOC.
7100 For some breakpoint types, the locations' address members are
7101 irrelevant and it makes no sense to attempt to compare them to
7102 other addresses (or use them for any other purpose either).
2d134ed3 7103
cb1e4e32
PA
7104 More specifically, software watchpoints and catchpoints that are
7105 not backed by breakpoints always have a zero valued location
7106 address and we don't want to mark breakpoints of any of these types
7107 to be a duplicate of an actual breakpoint location at address
7108 zero. */
e4f237da 7109
cb1e4e32
PA
7110static bool
7111bl_address_is_meaningful (bp_location *loc)
e4f237da 7112{
cb1e4e32 7113 return loc->loc_type != bp_loc_other;
2d134ed3
PA
7114}
7115
7116/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7117 true if LOC1 and LOC2 represent the same watchpoint location. */
7118
d8de7963 7119static bool
b2ff9ed3
AB
7120watchpoint_locations_match (const struct bp_location *loc1,
7121 const struct bp_location *loc2)
2d134ed3 7122{
3a5c3e22
PA
7123 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7124 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7125
7126 /* Both of them must exist. */
7127 gdb_assert (w1 != NULL);
7128 gdb_assert (w2 != NULL);
2bdf28a0 7129
4a64f543
MS
7130 /* If the target can evaluate the condition expression in hardware,
7131 then we we need to insert both watchpoints even if they are at
7132 the same place. Otherwise the watchpoint will only trigger when
7133 the condition of whichever watchpoint was inserted evaluates to
7134 true, not giving a chance for GDB to check the condition of the
7135 other watchpoint. */
3a5c3e22 7136 if ((w1->cond_exp
4a64f543
MS
7137 && target_can_accel_watchpoint_condition (loc1->address,
7138 loc1->length,
0cf6dd15 7139 loc1->watchpoint_type,
4d01a485 7140 w1->cond_exp.get ()))
3a5c3e22 7141 || (w2->cond_exp
4a64f543
MS
7142 && target_can_accel_watchpoint_condition (loc2->address,
7143 loc2->length,
0cf6dd15 7144 loc2->watchpoint_type,
4d01a485 7145 w2->cond_exp.get ())))
d8de7963 7146 return false;
0cf6dd15 7147
85d721b8
PA
7148 /* Note that this checks the owner's type, not the location's. In
7149 case the target does not support read watchpoints, but does
7150 support access watchpoints, we'll have bp_read_watchpoint
7151 watchpoints with hw_access locations. Those should be considered
7152 duplicates of hw_read locations. The hw_read locations will
7153 become hw_access locations later. */
2d134ed3
PA
7154 return (loc1->owner->type == loc2->owner->type
7155 && loc1->pspace->aspace == loc2->pspace->aspace
7156 && loc1->address == loc2->address
7157 && loc1->length == loc2->length);
e4f237da
KB
7158}
7159
31e77af2 7160/* See breakpoint.h. */
6c95b8df 7161
31e77af2 7162int
accd0bcd
YQ
7163breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7164 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 7165{
f5656ead 7166 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7167 || aspace1 == aspace2)
7168 && addr1 == addr2);
7169}
7170
f1310107
TJB
7171/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7172 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7173 matches ASPACE2. On targets that have global breakpoints, the address
7174 space doesn't really matter. */
7175
d8de7963 7176static bool
accd0bcd
YQ
7177breakpoint_address_match_range (const address_space *aspace1,
7178 CORE_ADDR addr1,
7179 int len1, const address_space *aspace2,
f1310107
TJB
7180 CORE_ADDR addr2)
7181{
f5656ead 7182 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7183 || aspace1 == aspace2)
7184 && addr2 >= addr1 && addr2 < addr1 + len1);
7185}
7186
7187/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7188 a ranged breakpoint. In most targets, a match happens only if ASPACE
7189 matches the breakpoint's address space. On targets that have global
7190 breakpoints, the address space doesn't really matter. */
7191
d8de7963 7192static bool
f1310107 7193breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 7194 const address_space *aspace,
f1310107
TJB
7195 CORE_ADDR addr)
7196{
7197 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7198 aspace, addr)
7199 || (bl->length
7200 && breakpoint_address_match_range (bl->pspace->aspace,
7201 bl->address, bl->length,
7202 aspace, addr)));
7203}
7204
d35ae833
PA
7205/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7206 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7207 match happens only if ASPACE matches the breakpoint's address
7208 space. On targets that have global breakpoints, the address space
7209 doesn't really matter. */
7210
d8de7963 7211static bool
d35ae833 7212breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 7213 const address_space *aspace,
d35ae833
PA
7214 CORE_ADDR addr, int len)
7215{
7216 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7217 || bl->pspace->aspace == aspace)
7218 {
7219 int bl_len = bl->length != 0 ? bl->length : 1;
7220
7221 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7222 return 1;
7223 }
7224 return 0;
7225}
7226
1e4d1764
YQ
7227/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7228 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7229 true, otherwise returns false. */
7230
d8de7963 7231static bool
b2ff9ed3
AB
7232tracepoint_locations_match (const struct bp_location *loc1,
7233 const struct bp_location *loc2)
1e4d1764
YQ
7234{
7235 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7236 /* Since tracepoint locations are never duplicated with others', tracepoint
7237 locations at the same address of different tracepoints are regarded as
7238 different locations. */
7239 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7240 else
d8de7963 7241 return false;
1e4d1764
YQ
7242}
7243
2d134ed3 7244/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7245 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7246 the same location. If SW_HW_BPS_MATCH is true, then software
7247 breakpoint locations and hardware breakpoint locations match,
7248 otherwise they don't. */
2d134ed3 7249
d8de7963 7250static bool
b2ff9ed3
AB
7251breakpoint_locations_match (const struct bp_location *loc1,
7252 const struct bp_location *loc2,
7f32a4d5 7253 bool sw_hw_bps_match)
2d134ed3 7254{
2bdf28a0
JK
7255 int hw_point1, hw_point2;
7256
7257 /* Both of them must not be in moribund_locations. */
7258 gdb_assert (loc1->owner != NULL);
7259 gdb_assert (loc2->owner != NULL);
7260
7261 hw_point1 = is_hardware_watchpoint (loc1->owner);
7262 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7263
7264 if (hw_point1 != hw_point2)
d8de7963 7265 return false;
2d134ed3
PA
7266 else if (hw_point1)
7267 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7268 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7269 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7270 else
7f32a4d5
PA
7271 /* We compare bp_location.length in order to cover ranged
7272 breakpoints. Keep this in sync with
7273 bp_location_is_less_than. */
f1310107
TJB
7274 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7275 loc2->pspace->aspace, loc2->address)
7f32a4d5 7276 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7277 && loc1->length == loc2->length);
2d134ed3
PA
7278}
7279
76897487
KB
7280static void
7281breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
d8de7963 7282 int bnum, bool have_bnum)
76897487 7283{
f63fbe86
MS
7284 /* The longest string possibly returned by hex_string_custom
7285 is 50 chars. These must be at least that big for safety. */
7286 char astr1[64];
7287 char astr2[64];
76897487 7288
bb599908
PH
7289 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7290 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7291 if (have_bnum)
8a3fe4f8 7292 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7293 bnum, astr1, astr2);
76897487 7294 else
8a3fe4f8 7295 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7296}
7297
4a64f543
MS
7298/* Adjust a breakpoint's address to account for architectural
7299 constraints on breakpoint placement. Return the adjusted address.
7300 Note: Very few targets require this kind of adjustment. For most
7301 targets, this function is simply the identity function. */
76897487
KB
7302
7303static CORE_ADDR
a6d9a66e 7304adjust_breakpoint_address (struct gdbarch *gdbarch,
60270718
AB
7305 CORE_ADDR bpaddr, enum bptype bptype,
7306 struct program_space *pspace)
76897487 7307{
60270718
AB
7308 gdb_assert (pspace != nullptr);
7309
a0de8c21
YQ
7310 if (bptype == bp_watchpoint
7311 || bptype == bp_hardware_watchpoint
7312 || bptype == bp_read_watchpoint
7313 || bptype == bp_access_watchpoint
7314 || bptype == bp_catchpoint)
88f7da05
KB
7315 {
7316 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7317 have their addresses modified. */
88f7da05
KB
7318 return bpaddr;
7319 }
7c16b83e
PA
7320 else if (bptype == bp_single_step)
7321 {
7322 /* Single-step breakpoints should not have their addresses
7323 modified. If there's any architectural constrain that
7324 applies to this address, then it should have already been
7325 taken into account when the breakpoint was created in the
7326 first place. If we didn't do this, stepping through e.g.,
7327 Thumb-2 IT blocks would break. */
7328 return bpaddr;
7329 }
76897487
KB
7330 else
7331 {
a0de8c21
YQ
7332 CORE_ADDR adjusted_bpaddr = bpaddr;
7333
60270718
AB
7334 /* Some targets have architectural constraints on the placement
7335 of breakpoint instructions. Obtain the adjusted address. */
a0de8c21
YQ
7336 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7337 {
60270718
AB
7338 /* Targets that implement this adjustment function will likely
7339 inspect either the symbol table, target memory at BPADDR, or
7340 even state registers, so ensure a suitable thread (and its
7341 associated program space) are currently selected. */
7342 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7343 switch_to_program_space_and_thread (pspace);
7344 adjusted_bpaddr
7345 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
a0de8c21 7346 }
76897487 7347
d88cb738
LM
7348 adjusted_bpaddr
7349 = gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
76897487
KB
7350
7351 /* An adjusted breakpoint address can significantly alter
dda83cd7 7352 a user's expectations. Print a warning if an adjustment
76897487
KB
7353 is required. */
7354 if (adjusted_bpaddr != bpaddr)
d8de7963 7355 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
76897487
KB
7356
7357 return adjusted_bpaddr;
7358 }
7359}
7360
cb1e4e32
PA
7361static bp_loc_type
7362bp_location_from_bp_type (bptype type)
7cc221ef 7363{
cb1e4e32 7364 switch (type)
e049a4b5
DJ
7365 {
7366 case bp_breakpoint:
7c16b83e 7367 case bp_single_step:
e049a4b5
DJ
7368 case bp_until:
7369 case bp_finish:
7370 case bp_longjmp:
7371 case bp_longjmp_resume:
e2e4d78b 7372 case bp_longjmp_call_dummy:
186c406b
TT
7373 case bp_exception:
7374 case bp_exception_resume:
e049a4b5 7375 case bp_step_resume:
2c03e5be 7376 case bp_hp_step_resume:
e049a4b5
DJ
7377 case bp_watchpoint_scope:
7378 case bp_call_dummy:
aa7d318d 7379 case bp_std_terminate:
e049a4b5
DJ
7380 case bp_shlib_event:
7381 case bp_thread_event:
7382 case bp_overlay_event:
4efc6507 7383 case bp_jit_event:
0fd8e87f 7384 case bp_longjmp_master:
aa7d318d 7385 case bp_std_terminate_master:
186c406b 7386 case bp_exception_master:
0e30163f
JK
7387 case bp_gnu_ifunc_resolver:
7388 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7389 case bp_dprintf:
cb1e4e32 7390 return bp_loc_software_breakpoint;
0b63c811 7391
e049a4b5 7392 case bp_hardware_breakpoint:
cb1e4e32 7393 return bp_loc_hardware_breakpoint;
0b63c811 7394
e049a4b5
DJ
7395 case bp_hardware_watchpoint:
7396 case bp_read_watchpoint:
7397 case bp_access_watchpoint:
cb1e4e32 7398 return bp_loc_hardware_watchpoint;
0b63c811 7399
e049a4b5 7400 case bp_watchpoint:
6e14e441 7401 return bp_loc_software_watchpoint;
0b63c811 7402
15c3d785
PA
7403 case bp_tracepoint:
7404 case bp_fast_tracepoint:
0fb4aa4b 7405 case bp_static_tracepoint:
7b572efb 7406 case bp_static_marker_tracepoint:
0b63c811
SM
7407 return bp_loc_tracepoint;
7408
7409 case bp_catchpoint:
cb1e4e32 7410 return bp_loc_other;
0b63c811 7411
e049a4b5 7412 default:
f34652de 7413 internal_error (_("unknown breakpoint type"));
e049a4b5 7414 }
cb1e4e32
PA
7415}
7416
7417bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7418{
7419 this->owner = owner;
7420 this->cond_bytecode = NULL;
7421 this->shlib_disabled = 0;
7422 this->enabled = 1;
b5fa468f 7423 this->disabled_by_cond = false;
cb1e4e32
PA
7424
7425 this->loc_type = type;
e049a4b5 7426
cb1e4e32
PA
7427 if (this->loc_type == bp_loc_software_breakpoint
7428 || this->loc_type == bp_loc_hardware_breakpoint)
7429 mark_breakpoint_location_modified (this);
7430
b6433ede 7431 incref ();
cb1e4e32
PA
7432}
7433
7434bp_location::bp_location (breakpoint *owner)
7435 : bp_location::bp_location (owner,
7436 bp_location_from_bp_type (owner->type))
7437{
28010a5d
PA
7438}
7439
f431efe5
PA
7440/* Decrement reference count. If the reference count reaches 0,
7441 destroy the bp_location. Sets *BLP to NULL. */
7442
7443static void
7444decref_bp_location (struct bp_location **blp)
7445{
b6433ede 7446 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7447 *blp = NULL;
7448}
7449
346774a9 7450/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7451
b270e6f9
TT
7452static breakpoint *
7453add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7454{
346774a9 7455 struct breakpoint *b1;
b270e6f9 7456 struct breakpoint *result = b.get ();
c906108c 7457
346774a9
PA
7458 /* Add this breakpoint to the end of the chain so that a list of
7459 breakpoints will come out in order of increasing numbers. */
7460
7461 b1 = breakpoint_chain;
7462 if (b1 == 0)
b270e6f9 7463 breakpoint_chain = b.release ();
346774a9
PA
7464 else
7465 {
7466 while (b1->next)
7467 b1 = b1->next;
b270e6f9 7468 b1->next = b.release ();
346774a9 7469 }
b270e6f9
TT
7470
7471 return result;
346774a9
PA
7472}
7473
0ba852ab 7474/* Initialize loc->function_name. */
0e30163f 7475
0d381245 7476static void
0ba852ab 7477set_breakpoint_location_function (struct bp_location *loc)
0d381245 7478{
2bdf28a0
JK
7479 gdb_assert (loc->owner != NULL);
7480
0d381245 7481 if (loc->owner->type == bp_breakpoint
1042e4c0 7482 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7483 || is_tracepoint (loc->owner))
0d381245 7484 {
2c02bd72 7485 const char *function_name;
0e30163f 7486
3467ec66 7487 if (loc->msymbol != NULL
60f62e2b
SM
7488 && (loc->msymbol->type () == mst_text_gnu_ifunc
7489 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7490 {
7491 struct breakpoint *b = loc->owner;
7492
c9d95fa3 7493 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7494
7495 if (b->type == bp_breakpoint && b->loc == loc
7496 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7497 {
7498 /* Create only the whole new breakpoint of this type but do not
7499 mess more complicated breakpoints with multiple locations. */
7500 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7501 /* Remember the resolver's address for use by the return
dda83cd7 7502 breakpoint. */
3467ec66 7503 loc->related_address = loc->address;
0e30163f
JK
7504 }
7505 }
3467ec66
PA
7506 else
7507 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7508
2c02bd72 7509 if (function_name)
23d6ee64 7510 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7511 }
7512}
7513
a6d9a66e 7514/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7515struct gdbarch *
a6d9a66e
UW
7516get_sal_arch (struct symtab_and_line sal)
7517{
7518 if (sal.section)
08feed99 7519 return sal.section->objfile->arch ();
a6d9a66e 7520 if (sal.symtab)
3c86fae3 7521 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7522
7523 return NULL;
7524}
7525
53a5351d 7526/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7527 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7528 initiated the operation. */
c906108c
SS
7529
7530void
186c406b 7531set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7532{
5d5658a1 7533 int thread = tp->global_num;
0fd8e87f
UW
7534
7535 /* To avoid having to rescan all objfile symbols at every step,
7536 we maintain a list of continually-inserted but always disabled
7537 longjmp "master" breakpoints. Here, we simply create momentary
7538 clones of those and enable them for the requested thread. */
1428b37a 7539 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7540 if (b->pspace == current_program_space
186c406b
TT
7541 && (b->type == bp_longjmp_master
7542 || b->type == bp_exception_master))
0fd8e87f 7543 {
06edf0c0 7544 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
e2e4d78b
JK
7545 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7546 after their removal. */
7ab97995 7547 momentary_breakpoint_from_master (b, type, 1, thread);
0fd8e87f 7548 }
186c406b
TT
7549
7550 tp->initiating_frame = frame;
c906108c
SS
7551}
7552
611c83ae 7553/* Delete all longjmp breakpoints from THREAD. */
c906108c 7554void
611c83ae 7555delete_longjmp_breakpoint (int thread)
c906108c 7556{
1428b37a 7557 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7558 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7559 {
7560 if (b->thread == thread)
7561 delete_breakpoint (b);
7562 }
c906108c
SS
7563}
7564
f59f708a
PA
7565void
7566delete_longjmp_breakpoint_at_next_stop (int thread)
7567{
1428b37a 7568 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7569 if (b->type == bp_longjmp || b->type == bp_exception)
7570 {
7571 if (b->thread == thread)
7572 b->disposition = disp_del_at_next_stop;
7573 }
7574}
7575
e2e4d78b
JK
7576/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7577 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7578 pointer to any of them. Return NULL if this system cannot place longjmp
7579 breakpoints. */
7580
7581struct breakpoint *
7582set_longjmp_breakpoint_for_call_dummy (void)
7583{
43892fdf 7584 breakpoint *retval = nullptr;
e2e4d78b 7585
43892fdf 7586 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7587 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7588 {
7ab97995
PA
7589 int thread = inferior_thread ()->global_num;
7590 breakpoint *new_b
7591 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7592 1, thread);
e2e4d78b
JK
7593
7594 /* Link NEW_B into the chain of RETVAL breakpoints. */
7595
7596 gdb_assert (new_b->related_breakpoint == new_b);
7597 if (retval == NULL)
7598 retval = new_b;
7599 new_b->related_breakpoint = retval;
7600 while (retval->related_breakpoint != new_b->related_breakpoint)
7601 retval = retval->related_breakpoint;
7602 retval->related_breakpoint = new_b;
7603 }
7604
7605 return retval;
7606}
7607
7608/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7609 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7610 stack.
7611
b4b3e2de
AB
7612 If the unwind fails then there is not sufficient information to discard
7613 dummy frames. In this case, elide the clean up and the dummy frames will
7614 be cleaned up next time this function is called from a location where
7615 unwinding is possible. */
e2e4d78b
JK
7616
7617void
b67a2c6f 7618check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7619{
7620 struct breakpoint *b, *b_tmp;
7621
7622 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7623 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7624 {
7625 struct breakpoint *dummy_b = b->related_breakpoint;
7626
b4b3e2de
AB
7627 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7628 chained off b->related_breakpoint. */
e2e4d78b
JK
7629 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7630 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7631
7632 /* If there was no bp_call_dummy breakpoint then there's nothing
7633 more to do. Or, if the dummy frame associated with the
7634 bp_call_dummy is still on the stack then we need to leave this
7635 bp_call_dummy in place. */
e2e4d78b
JK
7636 if (dummy_b->type != bp_call_dummy
7637 || frame_find_by_id (dummy_b->frame_id) != NULL)
7638 continue;
b4b3e2de
AB
7639
7640 /* We didn't find the dummy frame on the stack, this could be
7641 because we have longjmp'd to a stack frame that is previous to
7642 the dummy frame, or it could be because the stack unwind is
7643 broken at some point between the longjmp frame and the dummy
7644 frame.
7645
7646 Next we figure out why the stack unwind stopped. If it looks
7647 like the unwind is complete then we assume the dummy frame has
7648 been jumped over, however, if the unwind stopped for an
7649 unexpected reason then we assume the stack unwind is currently
7650 broken, and that we will (eventually) return to the dummy
7651 frame.
7652
7653 It might be tempting to consider using frame_id_inner here, but
7654 that is not safe. There is no guarantee that the stack frames
7655 we are looking at here are even on the same stack as the
7656 original dummy frame, hence frame_id_inner can't be used. See
7657 the comments on frame_id_inner for more details. */
7658 bool unwind_finished_unexpectedly = false;
bd2b40ac 7659 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
b4b3e2de 7660 {
bd2b40ac 7661 frame_info_ptr prev = get_prev_frame (fi);
b4b3e2de
AB
7662 if (prev == nullptr)
7663 {
7664 /* FI is the last stack frame. Why did this frame not
7665 unwind further? */
7666 auto stop_reason = get_frame_unwind_stop_reason (fi);
7667 if (stop_reason != UNWIND_NO_REASON
7668 && stop_reason != UNWIND_OUTERMOST)
7669 unwind_finished_unexpectedly = true;
7670 }
7671 fi = prev;
7672 }
7673 if (unwind_finished_unexpectedly)
7674 continue;
7675
00431a78 7676 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7677
7678 while (b->related_breakpoint != b)
7679 {
7680 if (b_tmp == b->related_breakpoint)
7681 b_tmp = b->related_breakpoint->next;
7682 delete_breakpoint (b->related_breakpoint);
7683 }
7684 delete_breakpoint (b);
7685 }
7686}
7687
1900040c
MS
7688void
7689enable_overlay_breakpoints (void)
7690{
43892fdf 7691 for (breakpoint *b : all_breakpoints ())
1900040c 7692 if (b->type == bp_overlay_event)
01add95b
SM
7693 {
7694 b->enable_state = bp_enabled;
7695 update_global_location_list (UGLL_MAY_INSERT);
7696 overlay_events_enabled = 1;
7697 }
1900040c
MS
7698}
7699
7700void
7701disable_overlay_breakpoints (void)
7702{
43892fdf 7703 for (breakpoint *b : all_breakpoints ())
1900040c 7704 if (b->type == bp_overlay_event)
01add95b
SM
7705 {
7706 b->enable_state = bp_disabled;
7707 update_global_location_list (UGLL_DONT_INSERT);
7708 overlay_events_enabled = 0;
7709 }
1900040c
MS
7710}
7711
aa7d318d
TT
7712/* Set an active std::terminate breakpoint for each std::terminate
7713 master breakpoint. */
7714void
7715set_std_terminate_breakpoint (void)
7716{
1428b37a 7717 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7718 if (b->pspace == current_program_space
7719 && b->type == bp_std_terminate_master)
7720 {
7ab97995
PA
7721 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7722 inferior_thread ()->global_num);
aa7d318d
TT
7723 }
7724}
7725
7726/* Delete all the std::terminate breakpoints. */
7727void
7728delete_std_terminate_breakpoint (void)
7729{
1428b37a 7730 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7731 if (b->type == bp_std_terminate)
7732 delete_breakpoint (b);
7733}
7734
c4093a6a 7735struct breakpoint *
a6d9a66e 7736create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7737{
7738 struct breakpoint *b;
c4093a6a 7739
9efa3c7f 7740 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 7741
b5de0fa7 7742 b->enable_state = bp_enabled;
264f9890
PA
7743 /* locspec has to be used or breakpoint_re_set will delete me. */
7744 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
c4093a6a 7745
44702360 7746 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7747
c4093a6a
JM
7748 return b;
7749}
7750
0101ce28
JJ
7751struct lang_and_radix
7752 {
7753 enum language lang;
7754 int radix;
7755 };
7756
4efc6507
DE
7757/* Create a breakpoint for JIT code registration and unregistration. */
7758
7759struct breakpoint *
7760create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7761{
9efa3c7f 7762 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 7763}
0101ce28 7764
03673fc7
PP
7765/* Remove JIT code registration and unregistration breakpoint(s). */
7766
7767void
7768remove_jit_event_breakpoints (void)
7769{
1428b37a 7770 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7771 if (b->type == bp_jit_event
7772 && b->loc->pspace == current_program_space)
7773 delete_breakpoint (b);
7774}
7775
cae688ec
JJ
7776void
7777remove_solib_event_breakpoints (void)
7778{
1428b37a 7779 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7780 if (b->type == bp_shlib_event
7781 && b->loc->pspace == current_program_space)
cae688ec
JJ
7782 delete_breakpoint (b);
7783}
7784
f37f681c
PA
7785/* See breakpoint.h. */
7786
7787void
7788remove_solib_event_breakpoints_at_next_stop (void)
7789{
1428b37a 7790 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7791 if (b->type == bp_shlib_event
7792 && b->loc->pspace == current_program_space)
7793 b->disposition = disp_del_at_next_stop;
7794}
7795
04086b45
PA
7796/* Helper for create_solib_event_breakpoint /
7797 create_and_insert_solib_event_breakpoint. Allows specifying which
7798 INSERT_MODE to pass through to update_global_location_list. */
7799
7800static struct breakpoint *
7801create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7802 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7803{
7804 struct breakpoint *b;
7805
9efa3c7f 7806 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 7807 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7808 return b;
7809}
7810
04086b45
PA
7811struct breakpoint *
7812create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7813{
7814 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7815}
7816
f37f681c
PA
7817/* See breakpoint.h. */
7818
7819struct breakpoint *
7820create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7821{
7822 struct breakpoint *b;
7823
04086b45
PA
7824 /* Explicitly tell update_global_location_list to insert
7825 locations. */
7826 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7827 if (!b->loc->inserted)
7828 {
7829 delete_breakpoint (b);
7830 return NULL;
7831 }
7832 return b;
7833}
7834
cae688ec
JJ
7835/* Disable any breakpoints that are on code in shared libraries. Only
7836 apply to enabled breakpoints, disabled ones can just stay disabled. */
7837
7838void
cb851954 7839disable_breakpoints_in_shlibs (void)
cae688ec 7840{
48d7020b 7841 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7842 {
7843 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7844 struct breakpoint *b = loc->owner;
7845
7846 /* We apply the check to all breakpoints, including disabled for
7847 those with loc->duplicate set. This is so that when breakpoint
7848 becomes enabled, or the duplicate is removed, gdb will try to
7849 insert all breakpoints. If we don't set shlib_disabled here,
7850 we'll try to insert those breakpoints and fail. */
7851 if (((b->type == bp_breakpoint)
7852 || (b->type == bp_jit_event)
7853 || (b->type == bp_hardware_breakpoint)
7854 || (is_tracepoint (b)))
7855 && loc->pspace == current_program_space
7856 && !loc->shlib_disabled
7857 && solib_name_from_address (loc->pspace, loc->address)
7858 )
7859 {
7860 loc->shlib_disabled = 1;
7861 }
7862 }
cae688ec
JJ
7863}
7864
63644780
NB
7865/* Disable any breakpoints and tracepoints that are in SOLIB upon
7866 notification of unloaded_shlib. Only apply to enabled breakpoints,
7867 disabled ones can just stay disabled. */
84acb35a 7868
75149521 7869static void
84acb35a
JJ
7870disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7871{
d8de7963 7872 bool disabled_shlib_breaks = false;
84acb35a 7873
48d7020b 7874 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7875 {
7876 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7877 struct breakpoint *b = loc->owner;
7878
7879 if (solib->pspace == loc->pspace
7880 && !loc->shlib_disabled
7881 && (((b->type == bp_breakpoint
7882 || b->type == bp_jit_event
7883 || b->type == bp_hardware_breakpoint)
7884 && (loc->loc_type == bp_loc_hardware_breakpoint
7885 || loc->loc_type == bp_loc_software_breakpoint))
7886 || is_tracepoint (b))
7887 && solib_contains_address_p (solib, loc->address))
7888 {
7889 loc->shlib_disabled = 1;
7890 /* At this point, we cannot rely on remove_breakpoint
7891 succeeding so we must mark the breakpoint as not inserted
7892 to prevent future errors occurring in remove_breakpoints. */
7893 loc->inserted = 0;
7894
7895 /* This may cause duplicate notifications for the same breakpoint. */
7896 gdb::observers::breakpoint_modified.notify (b);
7897
7898 if (!disabled_shlib_breaks)
7899 {
7900 target_terminal::ours_for_output ();
7901 warning (_("Temporarily disabling breakpoints "
7902 "for unloaded shared library \"%s\""),
7903 solib->so_name);
7904 }
d8de7963 7905 disabled_shlib_breaks = true;
01add95b
SM
7906 }
7907 }
84acb35a
JJ
7908}
7909
63644780
NB
7910/* Disable any breakpoints and tracepoints in OBJFILE upon
7911 notification of free_objfile. Only apply to enabled breakpoints,
7912 disabled ones can just stay disabled. */
7913
7914static void
7915disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7916{
63644780
NB
7917 if (objfile == NULL)
7918 return;
7919
d03de421
PA
7920 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7921 managed by the user with add-symbol-file/remove-symbol-file.
7922 Similarly to how breakpoints in shared libraries are handled in
7923 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7924 shlib_disabled so they end up uninserted on the next global
7925 location list update. Shared libraries not loaded by the user
7926 aren't handled here -- they're already handled in
7927 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7928 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7929 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7930 main objfile). */
7931 if ((objfile->flags & OBJF_SHARED) == 0
7932 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7933 return;
7934
43892fdf 7935 for (breakpoint *b : all_breakpoints ())
63644780 7936 {
d8de7963 7937 bool bp_modified = false;
63644780
NB
7938
7939 if (!is_breakpoint (b) && !is_tracepoint (b))
7940 continue;
7941
40cb8ca5 7942 for (bp_location *loc : b->locations ())
63644780
NB
7943 {
7944 CORE_ADDR loc_addr = loc->address;
7945
7946 if (loc->loc_type != bp_loc_hardware_breakpoint
7947 && loc->loc_type != bp_loc_software_breakpoint)
7948 continue;
7949
7950 if (loc->shlib_disabled != 0)
7951 continue;
7952
7953 if (objfile->pspace != loc->pspace)
7954 continue;
7955
7956 if (loc->loc_type != bp_loc_hardware_breakpoint
7957 && loc->loc_type != bp_loc_software_breakpoint)
7958 continue;
7959
7960 if (is_addr_in_objfile (loc_addr, objfile))
7961 {
7962 loc->shlib_disabled = 1;
08351840
PA
7963 /* At this point, we don't know whether the object was
7964 unmapped from the inferior or not, so leave the
7965 inserted flag alone. We'll handle failure to
7966 uninsert quietly, in case the object was indeed
7967 unmapped. */
63644780
NB
7968
7969 mark_breakpoint_location_modified (loc);
7970
d8de7963 7971 bp_modified = true;
63644780
NB
7972 }
7973 }
7974
7975 if (bp_modified)
76727919 7976 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7977 }
7978}
7979
b650a282 7980/* See breakpoint.h. */
ce78b96d 7981
92bb0228
PA
7982breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7983 bool temp, const char *cond_string_)
7984 : type (bptype),
7985 disposition (temp ? disp_del : disp_donttouch),
7986 gdbarch (gdbarch_),
7987 language (current_language->la_language),
7988 input_radix (::input_radix),
7989 cond_string (cond_string_ != nullptr
7990 ? make_unique_xstrdup (cond_string_)
7991 : nullptr),
7992 related_breakpoint (this)
7993{
7994}
7995
7996/* See breakpoint.h. */
7997
fed1c982 7998catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
92bb0228
PA
7999 const char *cond_string)
8000 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
c906108c 8001{
acd0955b 8002 add_dummy_location (this, current_program_space);
c5aa993b 8003
acd0955b 8004 pspace = current_program_space;
346774a9
PA
8005}
8006
b82d4ec9 8007breakpoint *
b270e6f9 8008install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8009{
b270e6f9 8010 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8011 set_breakpoint_number (internal, b);
558a9d82
YQ
8012 if (is_tracepoint (b))
8013 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8014 if (!internal)
8015 mention (b);
76727919 8016 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8017
8018 if (update_gll)
44702360 8019 update_global_location_list (UGLL_MAY_INSERT);
b82d4ec9
PA
8020
8021 return b;
c56053d2
PA
8022}
8023
c906108c 8024static int
fba45db2 8025hw_breakpoint_used_count (void)
c906108c 8026{
c906108c
SS
8027 int i = 0;
8028
43892fdf 8029 for (breakpoint *b : all_breakpoints ())
d6b74ac4 8030 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 8031 for (bp_location *bl : b->locations ())
f1310107
TJB
8032 {
8033 /* Special types of hardware breakpoints may use more than
8034 one register. */
c47614fe 8035 i += b->resources_needed (bl);
f1310107 8036 }
c906108c
SS
8037
8038 return i;
8039}
8040
a1398e0c
PA
8041/* Returns the resources B would use if it were a hardware
8042 watchpoint. */
8043
c906108c 8044static int
a1398e0c 8045hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8046{
c906108c
SS
8047 int i = 0;
8048
a1398e0c
PA
8049 if (!breakpoint_enabled (b))
8050 return 0;
8051
40cb8ca5 8052 for (bp_location *bl : b->locations ())
a1398e0c
PA
8053 {
8054 /* Special types of hardware watchpoints may use more than
8055 one register. */
c47614fe 8056 i += b->resources_needed (bl);
a1398e0c
PA
8057 }
8058
8059 return i;
8060}
8061
8062/* Returns the sum the used resources of all hardware watchpoints of
8063 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8064 the sum of the used resources of all hardware watchpoints of other
8065 types _not_ TYPE. */
8066
8067static int
8068hw_watchpoint_used_count_others (struct breakpoint *except,
8069 enum bptype type, int *other_type_used)
8070{
8071 int i = 0;
a1398e0c 8072
c906108c 8073 *other_type_used = 0;
43892fdf 8074 for (breakpoint *b : all_breakpoints ())
e09342b5 8075 {
a1398e0c
PA
8076 if (b == except)
8077 continue;
e09342b5
TJB
8078 if (!breakpoint_enabled (b))
8079 continue;
8080
a1398e0c
PA
8081 if (b->type == type)
8082 i += hw_watchpoint_use_count (b);
8083 else if (is_hardware_watchpoint (b))
8084 *other_type_used = 1;
e09342b5
TJB
8085 }
8086
c906108c
SS
8087 return i;
8088}
8089
c906108c 8090void
fba45db2 8091disable_watchpoints_before_interactive_call_start (void)
c906108c 8092{
43892fdf 8093 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8094 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8095 {
b5de0fa7 8096 b->enable_state = bp_call_disabled;
44702360 8097 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8098 }
c906108c
SS
8099}
8100
8101void
fba45db2 8102enable_watchpoints_after_interactive_call_stop (void)
c906108c 8103{
43892fdf 8104 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8105 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8106 {
b5de0fa7 8107 b->enable_state = bp_enabled;
44702360 8108 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8109 }
c906108c
SS
8110}
8111
8bea4e01
UW
8112void
8113disable_breakpoints_before_startup (void)
8114{
6c95b8df 8115 current_program_space->executing_startup = 1;
44702360 8116 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8117}
8118
8119void
8120enable_breakpoints_after_startup (void)
8121{
6c95b8df 8122 current_program_space->executing_startup = 0;
f8eba3c6 8123 breakpoint_re_set ();
8bea4e01
UW
8124}
8125
7a3e3265
PA
8126/* Allocate a new momentary breakpoint. */
8127
7ab97995 8128template<typename... Arg>
7a3e3265 8129static momentary_breakpoint *
7ab97995
PA
8130new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8131 Arg&&... args)
7a3e3265
PA
8132{
8133 if (type == bp_longjmp || type == bp_exception)
7ab97995
PA
8134 return new longjmp_breakpoint (gdbarch, type,
8135 std::forward<Arg> (args)...);
7a3e3265 8136 else
7ab97995
PA
8137 return new momentary_breakpoint (gdbarch, type,
8138 std::forward<Arg> (args)...);
7a3e3265
PA
8139}
8140
7c16b83e
PA
8141/* Set a momentary breakpoint of type TYPE at address specified by
8142 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8143 frame. */
c906108c 8144
454dafbd 8145breakpoint_up
a6d9a66e
UW
8146set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8147 struct frame_id frame_id, enum bptype type)
c906108c 8148{
193facb3
JK
8149 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8150 tail-called one. */
8151 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8152
7a3e3265 8153 std::unique_ptr<momentary_breakpoint> b
7ab97995
PA
8154 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8155 inferior_thread ()->global_num));
7a3e3265
PA
8156
8157 b->add_location (sal);
c906108c 8158
7a3e3265
PA
8159 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8160
44702360 8161 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8162
7a3e3265 8163 return bp;
c906108c 8164}
611c83ae 8165
06edf0c0 8166/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8167 The new breakpoint will have type TYPE, use OPS as its
8168 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8169
06edf0c0
PA
8170static struct breakpoint *
8171momentary_breakpoint_from_master (struct breakpoint *orig,
8172 enum bptype type,
7ab97995
PA
8173 int loc_enabled,
8174 int thread)
e58b0e63 8175{
7a3e3265 8176 std::unique_ptr<breakpoint> copy
7ab97995
PA
8177 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8178 orig->frame_id, thread));
ee89d0a4 8179 copy->loc = copy->allocate_location ();
0ba852ab 8180 set_breakpoint_location_function (copy->loc);
e58b0e63 8181
a6d9a66e 8182 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8183 copy->loc->requested_address = orig->loc->requested_address;
8184 copy->loc->address = orig->loc->address;
8185 copy->loc->section = orig->loc->section;
6c95b8df 8186 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8187 copy->loc->probe = orig->loc->probe;
f8eba3c6 8188 copy->loc->line_number = orig->loc->line_number;
2f202fde 8189 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8190 copy->loc->enabled = loc_enabled;
e58b0e63 8191
7a3e3265 8192 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
44702360 8193 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7a3e3265 8194 return b;
e58b0e63
PA
8195}
8196
06edf0c0
PA
8197/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8198 ORIG is NULL. */
8199
8200struct breakpoint *
8201clone_momentary_breakpoint (struct breakpoint *orig)
8202{
8203 /* If there's nothing to clone, then return nothing. */
8204 if (orig == NULL)
8205 return NULL;
8206
7ab97995
PA
8207 return momentary_breakpoint_from_master (orig, orig->type, 0,
8208 orig->thread);
06edf0c0
PA
8209}
8210
454dafbd 8211breakpoint_up
a6d9a66e
UW
8212set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8213 enum bptype type)
611c83ae
PA
8214{
8215 struct symtab_and_line sal;
8216
8217 sal = find_pc_line (pc, 0);
8218 sal.pc = pc;
8219 sal.section = find_pc_overlay (pc);
8220 sal.explicit_pc = 1;
8221
a6d9a66e 8222 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8223}
c906108c 8224\f
c5aa993b 8225
c906108c
SS
8226/* Tell the user we have just set a breakpoint B. */
8227
8228static void
b713485d 8229mention (const breakpoint *b)
c906108c 8230{
c47614fe 8231 b->print_mention ();
2d33446d 8232 current_uiout->text ("\n");
c906108c 8233}
c906108c 8234\f
c5aa993b 8235
5133a315 8236static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8237
7f32a4d5
PA
8238/* Handle "set breakpoint auto-hw on".
8239
8240 If the explicitly specified breakpoint type is not hardware
8241 breakpoint, check the memory map to see whether the breakpoint
8242 address is in read-only memory.
8243
8244 - location type is not hardware breakpoint, memory is read-only.
8245 We change the type of the location to hardware breakpoint.
8246
8247 - location type is hardware breakpoint, memory is read-write. This
8248 means we've previously made the location hardware one, but then the
8249 memory map changed, so we undo.
8250*/
8251
8252static void
8253handle_automatic_hardware_breakpoints (bp_location *bl)
8254{
8255 if (automatic_hardware_breakpoints
8256 && bl->owner->type != bp_hardware_breakpoint
8257 && (bl->loc_type == bp_loc_software_breakpoint
8258 || bl->loc_type == bp_loc_hardware_breakpoint))
8259 {
8260 /* When breakpoints are removed, remove_breakpoints will use
8261 location types we've just set here, the only possible problem
8262 is that memory map has changed during running program, but
8263 it's not going to work anyway with current gdb. */
8264 mem_region *mr = lookup_mem_region (bl->address);
8265
8266 if (mr != nullptr)
8267 {
8268 enum bp_loc_type new_type;
8269
8270 if (mr->attrib.mode != MEM_RW)
8271 new_type = bp_loc_hardware_breakpoint;
8272 else
8273 new_type = bp_loc_software_breakpoint;
8274
8275 if (new_type != bl->loc_type)
8276 {
8277 static bool said = false;
8278
8279 bl->loc_type = new_type;
8280 if (!said)
8281 {
6cb06a8c
TT
8282 gdb_printf (_("Note: automatically using "
8283 "hardware breakpoints for "
8284 "read-only addresses.\n"));
7f32a4d5
PA
8285 said = true;
8286 }
8287 }
8288 }
8289 }
8290}
8291
06615628 8292bp_location *
74421c0b 8293code_breakpoint::add_location (const symtab_and_line &sal)
0d381245 8294{
06615628 8295 struct bp_location *new_loc, **tmp;
3742cc8b 8296 CORE_ADDR adjusted_address;
06615628 8297 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8298
8299 if (loc_gdbarch == NULL)
06615628 8300 loc_gdbarch = gdbarch;
3742cc8b
YQ
8301
8302 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8303 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8304 location will be placed on the location chain. Adjustment of the
8305 breakpoint may cause target_read_memory() to be called and we do
8306 not want its scan of the location chain to find a breakpoint and
8307 location that's only been partially initialized. */
8308 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
60270718
AB
8309 sal.pc, type,
8310 sal.pspace);
0d381245 8311
d30113d4 8312 /* Sort the locations by their ADDRESS. */
06615628
PA
8313 new_loc = allocate_location ();
8314 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
d30113d4 8315 tmp = &((*tmp)->next))
0d381245 8316 ;
06615628
PA
8317 new_loc->next = *tmp;
8318 *tmp = new_loc;
8319
8320 new_loc->requested_address = sal.pc;
8321 new_loc->address = adjusted_address;
8322 new_loc->pspace = sal.pspace;
8323 new_loc->probe.prob = sal.prob;
8324 new_loc->probe.objfile = sal.objfile;
8325 gdb_assert (new_loc->pspace != NULL);
8326 new_loc->section = sal.section;
8327 new_loc->gdbarch = loc_gdbarch;
8328 new_loc->line_number = sal.line;
8329 new_loc->symtab = sal.symtab;
8330 new_loc->symbol = sal.symbol;
8331 new_loc->msymbol = sal.msymbol;
8332 new_loc->objfile = sal.objfile;
8333
8334 set_breakpoint_location_function (new_loc);
1a853c52 8335
6ae88661
LM
8336 /* While by definition, permanent breakpoints are already present in the
8337 code, we don't mark the location as inserted. Normally one would expect
8338 that GDB could rely on that breakpoint instruction to stop the program,
8339 thus removing the need to insert its own breakpoint, except that executing
8340 the breakpoint instruction can kill the target instead of reporting a
8341 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8342 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8343 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8344 breakpoint be inserted normally results in QEMU knowing about the GDB
8345 breakpoint, and thus trap before the breakpoint instruction is executed.
8346 (If GDB later needs to continue execution past the permanent breakpoint,
8347 it manually increments the PC, thus avoiding executing the breakpoint
8348 instruction.) */
06615628
PA
8349 if (bp_loc_is_permanent (new_loc))
8350 new_loc->permanent = 1;
1a853c52 8351
06615628 8352 return new_loc;
0d381245 8353}
514f746b
AR
8354\f
8355
5133a315
LM
8356/* Return true if LOC is pointing to a permanent breakpoint,
8357 return false otherwise. */
1cf4d951 8358
5133a315 8359static bool
1cf4d951
PA
8360bp_loc_is_permanent (struct bp_location *loc)
8361{
514f746b
AR
8362 gdb_assert (loc != NULL);
8363
cb1e4e32
PA
8364 /* If we have a non-breakpoint-backed catchpoint or a software
8365 watchpoint, just return 0. We should not attempt to read from
8366 the addresses the locations of these breakpoint types point to.
5133a315 8367 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8368 memory. */
cb1e4e32 8369 if (!bl_address_is_meaningful (loc))
5133a315 8370 return false;
244558af 8371
5ed8105e 8372 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8373 switch_to_program_space_and_thread (loc->pspace);
5133a315 8374 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8375}
8376
e7e0cddf
SS
8377/* Build a command list for the dprintf corresponding to the current
8378 settings of the dprintf style options. */
8379
8380static void
8381update_dprintf_command_list (struct breakpoint *b)
8382{
6f781ee3 8383 const char *dprintf_args = b->extra_string.get ();
8579fd13 8384 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf
SS
8385
8386 if (!dprintf_args)
8387 return;
8388
8389 dprintf_args = skip_spaces (dprintf_args);
8390
8391 /* Allow a comma, as it may have terminated a location, but don't
8392 insist on it. */
8393 if (*dprintf_args == ',')
8394 ++dprintf_args;
8395 dprintf_args = skip_spaces (dprintf_args);
8396
8397 if (*dprintf_args != '"')
8398 error (_("Bad format string, missing '\"'."));
8399
d3ce09f5 8400 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8401 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8402 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8403 {
e0700ba4 8404 if (dprintf_function.empty ())
e7e0cddf
SS
8405 error (_("No function supplied for dprintf call"));
8406
e0700ba4 8407 if (!dprintf_channel.empty ())
e7e0cddf 8408 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8409 dprintf_function.c_str (),
8410 dprintf_channel.c_str (),
e7e0cddf
SS
8411 dprintf_args);
8412 else
8413 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8414 dprintf_function.c_str (),
e7e0cddf
SS
8415 dprintf_args);
8416 }
d3ce09f5
SS
8417 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8418 {
8419 if (target_can_run_breakpoint_commands ())
8420 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8421 else
8422 {
8423 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8424 printf_line = xstrprintf ("printf %s", dprintf_args);
8425 }
8426 }
e7e0cddf 8427 else
f34652de 8428 internal_error (_("Invalid dprintf style."));
e7e0cddf 8429
f28045c2 8430 gdb_assert (printf_line != NULL);
e7e0cddf 8431
12973681
TT
8432 /* Manufacture a printf sequence. */
8433 struct command_line *printf_cmd_line
8579fd13 8434 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8435 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8436 command_lines_deleter ()));
e7e0cddf
SS
8437}
8438
8439/* Update all dprintf commands, making their command lists reflect
8440 current style settings. */
8441
8442static void
eb4c3f4a 8443update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8444 struct cmd_list_element *c)
8445{
43892fdf
SM
8446 for (breakpoint *b : all_breakpoints ())
8447 if (b->type == bp_dprintf)
e7e0cddf 8448 update_dprintf_command_list (b);
e7e0cddf 8449}
c3f6f71d 8450
74421c0b 8451code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
3b003a61
PA
8452 enum bptype type_,
8453 gdb::array_view<const symtab_and_line> sals,
264f9890 8454 location_spec_up &&locspec_,
3b003a61
PA
8455 gdb::unique_xmalloc_ptr<char> filter_,
8456 gdb::unique_xmalloc_ptr<char> cond_string_,
8457 gdb::unique_xmalloc_ptr<char> extra_string_,
8458 enum bpdisp disposition_,
8459 int thread_, int task_, int ignore_count_,
8460 int from_tty,
8461 int enabled_, unsigned flags,
8462 int display_canonical_)
8463 : breakpoint (gdbarch_, type_)
018d34a4 8464{
0d381245 8465 int i;
018d34a4
VP
8466
8467 if (type == bp_hardware_breakpoint)
8468 {
fbbd034e
AS
8469 int target_resources_ok;
8470
8471 i = hw_breakpoint_used_count ();
8472 target_resources_ok =
8473 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8474 i + 1, 0);
8475 if (target_resources_ok == 0)
8476 error (_("No hardware breakpoint support in the target."));
8477 else if (target_resources_ok < 0)
8478 error (_("Hardware breakpoints used exceeds limit."));
8479 }
8480
6c5b2ebe 8481 gdb_assert (!sals.empty ());
6c95b8df 8482
0a9ccb9d 8483 /* At most one of thread or task can be set on any breakpoint. */
2ecee236 8484 gdb_assert (thread == -1 || task == -1);
3b003a61
PA
8485 thread = thread_;
8486 task = task_;
0d381245 8487
3b003a61
PA
8488 cond_string = std::move (cond_string_);
8489 extra_string = std::move (extra_string_);
8490 ignore_count = ignore_count_;
8491 enable_state = enabled_ ? bp_enabled : bp_disabled;
8492 disposition = disposition_;
5af949e3 8493
3b003a61
PA
8494 if (type == bp_static_tracepoint
8495 || type == bp_static_marker_tracepoint)
8496 {
98ed24fb 8497 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
3b003a61 8498 struct static_tracepoint_marker marker;
0d381245 8499
3b003a61 8500 if (strace_marker_p (this))
0d381245 8501 {
3b003a61
PA
8502 /* We already know the marker exists, otherwise, we wouldn't
8503 see a sal for it. */
709438c7 8504 const char *p = &locspec_->to_string ()[3];
3b003a61 8505 const char *endp;
0fb4aa4b 8506
3b003a61 8507 p = skip_spaces (p);
0fb4aa4b 8508
3b003a61 8509 endp = skip_to_space (p);
0fb4aa4b 8510
3b003a61 8511 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8512
3b003a61
PA
8513 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8514 t->static_trace_marker_id.c_str ());
8515 }
8516 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8517 {
8518 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8519
3b003a61
PA
8520 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8521 t->static_trace_marker_id.c_str ());
0d381245
VP
8522 }
8523 else
3b003a61
PA
8524 warning (_("Couldn't determine the static tracepoint marker to probe"));
8525 }
8526
8527 for (const auto &sal : sals)
8528 {
8529 if (from_tty)
018d34a4 8530 {
3b003a61
PA
8531 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8532 if (loc_gdbarch == nullptr)
8533 loc_gdbarch = gdbarch;
8534
8535 describe_other_breakpoints (loc_gdbarch,
8536 sal.pspace, sal.pc, sal.section, thread);
0d381245
VP
8537 }
8538
3b003a61
PA
8539 bp_location *new_loc = add_location (sal);
8540 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8541 new_loc->inserted = 1;
8542
b5fa468f
TBA
8543 /* Do not set breakpoint locations conditions yet. As locations
8544 are inserted, they get sorted based on their addresses. Let
8545 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8546
8547 /* Dynamic printf requires and uses additional arguments on the
8548 command line, otherwise it's an error. */
8549 if (type == bp_dprintf)
8550 {
3b003a61
PA
8551 if (extra_string != nullptr)
8552 update_dprintf_command_list (this);
e7e0cddf
SS
8553 else
8554 error (_("Format string required"));
8555 }
3b003a61
PA
8556 else if (extra_string != nullptr)
8557 error (_("Garbage '%s' at end of command"), extra_string.get ());
855a6e68 8558 }
018d34a4 8559
b5fa468f
TBA
8560 /* The order of the locations is now stable. Set the location
8561 condition using the location's number. */
8562 int loc_num = 1;
3b003a61 8563 for (bp_location *bl : locations ())
b5fa468f 8564 {
3b003a61
PA
8565 if (cond_string != nullptr)
8566 set_breakpoint_location_condition (cond_string.get (), bl,
8567 number, loc_num);
b5fa468f
TBA
8568
8569 ++loc_num;
8570 }
8571
3b003a61 8572 display_canonical = display_canonical_;
264f9890
PA
8573 if (locspec_ != nullptr)
8574 locspec = std::move (locspec_);
018d34a4 8575 else
264f9890 8576 locspec = new_address_location_spec (this->loc->address, NULL, 0);
3b003a61 8577 filter = std::move (filter_);
d9b3f62e 8578}
018d34a4 8579
d9b3f62e
PA
8580static void
8581create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8582 gdb::array_view<const symtab_and_line> sals,
264f9890 8583 location_spec_up &&locspec,
e1e01040
PA
8584 gdb::unique_xmalloc_ptr<char> filter,
8585 gdb::unique_xmalloc_ptr<char> cond_string,
8586 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8587 enum bptype type, enum bpdisp disposition,
8588 int thread, int task, int ignore_count,
ef4848c7 8589 int from_tty,
44f238bb
PA
8590 int enabled, int internal, unsigned flags,
8591 int display_canonical)
d9b3f62e 8592{
74421c0b 8593 std::unique_ptr<code_breakpoint> b
3b003a61
PA
8594 = new_breakpoint_from_type (gdbarch,
8595 type,
8596 sals,
264f9890 8597 std::move (locspec),
3b003a61
PA
8598 std::move (filter),
8599 std::move (cond_string),
8600 std::move (extra_string),
8601 disposition,
8602 thread, task, ignore_count,
8603 from_tty,
8604 enabled, flags,
8605 display_canonical);
d9b3f62e 8606
b270e6f9 8607 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8608}
8609
8610/* Add SALS.nelts breakpoints to the breakpoint table. For each
8611 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8612 value. COND_STRING, if not NULL, specified the condition to be
8613 used for all breakpoints. Essentially the only case where
8614 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8615 function. In that case, it's still not possible to specify
8616 separate conditions for different overloaded functions, so
8617 we take just a single condition string.
8618
c3f6f71d 8619 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8620 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8621 array contents). If the function fails (error() is called), the
8622 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8623 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8624
8625static void
8cdf0e15 8626create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8627 struct linespec_result *canonical,
e1e01040
PA
8628 gdb::unique_xmalloc_ptr<char> cond_string,
8629 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8630 enum bptype type, enum bpdisp disposition,
8631 int thread, int task, int ignore_count,
ef4848c7 8632 int from_tty,
44f238bb 8633 int enabled, int internal, unsigned flags)
c906108c 8634{
f8eba3c6 8635 if (canonical->pre_expanded)
6c5b2ebe 8636 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8637
6c5b2ebe 8638 for (const auto &lsal : canonical->lsals)
c3f6f71d 8639 {
f00aae0f 8640 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8641 'break', without arguments. */
264f9890 8642 location_spec_up locspec
5c1ddcb6
PA
8643 = (canonical->locspec != nullptr
8644 ? canonical->locspec->clone ()
8645 : nullptr);
e1e01040 8646 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8647 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8648
6c5b2ebe 8649 create_breakpoint_sal (gdbarch, lsal.sals,
264f9890 8650 std::move (locspec),
e1e01040
PA
8651 std::move (filter_string),
8652 std::move (cond_string),
8653 std::move (extra_string),
e7e0cddf 8654 type, disposition,
ef4848c7 8655 thread, task, ignore_count,
44f238bb 8656 from_tty, enabled, internal, flags,
56435ebe 8657 canonical->special_display);
c3f6f71d 8658 }
c3f6f71d 8659}
c906108c 8660
264f9890 8661/* Parse LOCSPEC which is assumed to be a SAL specification possibly
c3f6f71d 8662 followed by conditionals. On return, SALS contains an array of SAL
264f9890
PA
8663 addresses found. LOCSPEC points to the end of the SAL (for
8664 linespec locspecs).
9998af43
TJB
8665
8666 The array and the line spec strings are allocated on the heap, it is
8667 the caller's responsibility to free them. */
c906108c 8668
b9362cc7 8669static void
264f9890 8670parse_breakpoint_sals (location_spec *locspec,
58438ac1 8671 struct linespec_result *canonical)
c3f6f71d 8672{
f00aae0f
KS
8673 struct symtab_and_line cursal;
8674
7464aeaa 8675 if (locspec->type () == LINESPEC_LOCATION_SPEC)
f00aae0f 8676 {
40d97ee2 8677 const char *spec = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 8678
a20714ff 8679 if (spec == NULL)
f00aae0f
KS
8680 {
8681 /* The last displayed codepoint, if it's valid, is our default
8682 breakpoint address. */
8683 if (last_displayed_sal_is_valid ())
8684 {
f00aae0f
KS
8685 /* Set sal's pspace, pc, symtab, and line to the values
8686 corresponding to the last call to print_frame_info.
8687 Be sure to reinitialize LINE with NOTCURRENT == 0
8688 as the breakpoint line number is inappropriate otherwise.
8689 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8690 symtab_and_line sal = get_last_displayed_sal ();
8691 CORE_ADDR pc = sal.pc;
8692
f00aae0f
KS
8693 sal = find_pc_line (pc, 0);
8694
8695 /* "break" without arguments is equivalent to "break *PC"
8696 where PC is the last displayed codepoint's address. So
8697 make sure to set sal.explicit_pc to prevent GDB from
8698 trying to expand the list of sals to include all other
8699 instances with the same symtab and line. */
8700 sal.pc = pc;
8701 sal.explicit_pc = 1;
8702
6c5b2ebe
PA
8703 struct linespec_sals lsal;
8704 lsal.sals = {sal};
f00aae0f
KS
8705 lsal.canonical = NULL;
8706
6c5b2ebe 8707 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8708 return;
8709 }
8710 else
8711 error (_("No default breakpoint address now."));
c906108c 8712 }
c906108c 8713 }
f00aae0f
KS
8714
8715 /* Force almost all breakpoints to be in terms of the
8716 current_source_symtab (which is decode_line_1's default).
8717 This should produce the results we want almost all of the
8718 time while leaving default_breakpoint_* alone.
8719
8720 ObjC: However, don't match an Objective-C method name which
8721 may have a '+' or '-' succeeded by a '['. */
8722 cursal = get_current_source_symtab_and_line ();
8723 if (last_displayed_sal_is_valid ())
c906108c 8724 {
a20714ff 8725 const char *spec = NULL;
cc80f267 8726
7464aeaa 8727 if (locspec->type () == LINESPEC_LOCATION_SPEC)
40d97ee2 8728 spec = as_linespec_location_spec (locspec)->spec_string;
cc80f267 8729
f00aae0f 8730 if (!cursal.symtab
a20714ff
PA
8731 || (spec != NULL
8732 && strchr ("+-", spec[0]) != NULL
8733 && spec[1] != '['))
f00aae0f 8734 {
264f9890 8735 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
8736 get_last_displayed_symtab (),
8737 get_last_displayed_line (),
8738 canonical, NULL, NULL);
8739 return;
8740 }
c906108c 8741 }
f00aae0f 8742
264f9890 8743 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 8744 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 8745}
c906108c 8746
c906108c 8747
c3f6f71d 8748/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8749 inserted as a breakpoint. If it can't throw an error. */
c906108c 8750
b9362cc7 8751static void
6c5b2ebe 8752breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 8753{
6c5b2ebe
PA
8754 for (auto &sal : sals)
8755 resolve_sal_pc (&sal);
c3f6f71d
JM
8756}
8757
7a697b8d
SS
8758/* Fast tracepoints may have restrictions on valid locations. For
8759 instance, a fast tracepoint using a jump instead of a trap will
8760 likely have to overwrite more bytes than a trap would, and so can
8761 only be placed where the instruction is longer than the jump, or a
8762 multi-instruction sequence does not have a jump into the middle of
8763 it, etc. */
8764
8765static void
8766check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 8767 gdb::array_view<const symtab_and_line> sals)
7a697b8d 8768{
6c5b2ebe 8769 for (const auto &sal : sals)
7a697b8d 8770 {
f8eba3c6
TT
8771 struct gdbarch *sarch;
8772
6c5b2ebe 8773 sarch = get_sal_arch (sal);
f8eba3c6
TT
8774 /* We fall back to GDBARCH if there is no architecture
8775 associated with SAL. */
8776 if (sarch == NULL)
8777 sarch = gdbarch;
281d762b
TT
8778 std::string msg;
8779 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 8780 error (_("May not have a fast tracepoint at %s%s"),
281d762b 8781 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
8782 }
8783}
8784
018d34a4
VP
8785/* Given TOK, a string specification of condition and thread, as
8786 accepted by the 'break' command, extract the condition
8787 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8788 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8789 If no condition is found, *COND_STRING is set to NULL.
8790 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8791
8792static void
bbc13ae3 8793find_condition_and_thread (const char *tok, CORE_ADDR pc,
6f781ee3
TT
8794 gdb::unique_xmalloc_ptr<char> *cond_string,
8795 int *thread, int *task,
8796 gdb::unique_xmalloc_ptr<char> *rest)
018d34a4 8797{
6f781ee3 8798 cond_string->reset ();
018d34a4 8799 *thread = -1;
2ecee236 8800 *task = -1;
6f781ee3 8801 rest->reset ();
733d554a 8802 bool force = false;
ed1d1739 8803
018d34a4
VP
8804 while (tok && *tok)
8805 {
bbc13ae3 8806 const char *end_tok;
018d34a4 8807 int toklen;
bbc13ae3
KS
8808 const char *cond_start = NULL;
8809 const char *cond_end = NULL;
cc59ec59 8810
f1735a53 8811 tok = skip_spaces (tok);
e7e0cddf
SS
8812
8813 if ((*tok == '"' || *tok == ',') && rest)
8814 {
6f781ee3 8815 rest->reset (savestring (tok, strlen (tok)));
e7e0cddf
SS
8816 return;
8817 }
8818
f1735a53 8819 end_tok = skip_to_space (tok);
d634f2de 8820
018d34a4 8821 toklen = end_tok - tok;
d634f2de 8822
018d34a4
VP
8823 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8824 {
8825 tok = cond_start = end_tok + 1;
733d554a
TBA
8826 try
8827 {
8828 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8829 }
8830 catch (const gdb_exception_error &)
8831 {
8832 if (!force)
8833 throw;
8834 else
8835 tok = tok + strlen (tok);
8836 }
018d34a4 8837 cond_end = tok;
6f781ee3 8838 cond_string->reset (savestring (cond_start, cond_end - cond_start));
018d34a4 8839 }
733d554a
TBA
8840 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8841 {
5759831a 8842 tok = tok + toklen;
733d554a
TBA
8843 force = true;
8844 }
018d34a4
VP
8845 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8846 {
5d5658a1
PA
8847 const char *tmptok;
8848 struct thread_info *thr;
d634f2de 8849
980dbf36
AB
8850 if (*thread != -1)
8851 error(_("You can specify only one thread."));
8852
2ecee236 8853 if (*task != -1)
0a9ccb9d
AB
8854 error (_("You can specify only one of thread or task."));
8855
018d34a4 8856 tok = end_tok + 1;
5d5658a1 8857 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
8858 if (tok == tmptok)
8859 error (_("Junk after thread keyword."));
5d5658a1 8860 *thread = thr->global_num;
bbc13ae3 8861 tok = tmptok;
018d34a4 8862 }
4a306c9a
JB
8863 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8864 {
8865 char *tmptok;
8866
2ecee236 8867 if (*task != -1)
980dbf36
AB
8868 error(_("You can specify only one task."));
8869
0a9ccb9d
AB
8870 if (*thread != -1)
8871 error (_("You can specify only one of thread or task."));
8872
4a306c9a 8873 tok = end_tok + 1;
bbc13ae3 8874 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
8875 if (tok == tmptok)
8876 error (_("Junk after task keyword."));
8877 if (!valid_task_id (*task))
b6199126 8878 error (_("Unknown task %d."), *task);
bbc13ae3 8879 tok = tmptok;
4a306c9a 8880 }
e7e0cddf
SS
8881 else if (rest)
8882 {
6f781ee3 8883 rest->reset (savestring (tok, strlen (tok)));
ccab2054 8884 return;
e7e0cddf 8885 }
018d34a4
VP
8886 else
8887 error (_("Junk at end of arguments."));
8888 }
8889}
8890
b5fa468f
TBA
8891/* Call 'find_condition_and_thread' for each sal in SALS until a parse
8892 succeeds. The parsed values are written to COND_STRING, THREAD,
8893 TASK, and REST. See the comment of 'find_condition_and_thread'
8894 for the description of these parameters and INPUT. */
8895
8896static void
8897find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
6f781ee3
TT
8898 const char *input,
8899 gdb::unique_xmalloc_ptr<char> *cond_string,
8900 int *thread, int *task,
8901 gdb::unique_xmalloc_ptr<char> *rest)
b5fa468f
TBA
8902{
8903 int num_failures = 0;
8904 for (auto &sal : sals)
8905 {
6f781ee3 8906 gdb::unique_xmalloc_ptr<char> cond;
0a9ccb9d 8907 int thread_id = -1;
2ecee236 8908 int task_id = -1;
6f781ee3 8909 gdb::unique_xmalloc_ptr<char> remaining;
b5fa468f
TBA
8910
8911 /* Here we want to parse 'arg' to separate condition from thread
8912 number. But because parsing happens in a context and the
8913 contexts of sals might be different, try each until there is
8914 success. Finding one successful parse is sufficient for our
8915 goal. When setting the breakpoint we'll re-parse the
8916 condition in the context of each sal. */
8917 try
8918 {
8919 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8920 &task_id, &remaining);
6f781ee3 8921 *cond_string = std::move (cond);
0a9ccb9d 8922 /* At most one of thread or task can be set. */
2ecee236 8923 gdb_assert (thread_id == -1 || task_id == -1);
b5fa468f
TBA
8924 *thread = thread_id;
8925 *task = task_id;
6f781ee3 8926 *rest = std::move (remaining);
b5fa468f
TBA
8927 break;
8928 }
8929 catch (const gdb_exception_error &e)
8930 {
8931 num_failures++;
8932 /* If no sal remains, do not continue. */
8933 if (num_failures == sals.size ())
8934 throw;
8935 }
8936 }
8937}
8938
0fb4aa4b
PA
8939/* Decode a static tracepoint marker spec. */
8940
6c5b2ebe 8941static std::vector<symtab_and_line>
f00aae0f 8942decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 8943{
f00aae0f
KS
8944 const char *p = &(*arg_p)[3];
8945 const char *endp;
0fb4aa4b 8946
f1735a53 8947 p = skip_spaces (p);
0fb4aa4b 8948
f1735a53 8949 endp = skip_to_space (p);
0fb4aa4b 8950
81b1e71c 8951 std::string marker_str (p, endp - p);
0fb4aa4b 8952
5d9310c4
SM
8953 std::vector<static_tracepoint_marker> markers
8954 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8955 if (markers.empty ())
81b1e71c
TT
8956 error (_("No known static tracepoint marker named %s"),
8957 marker_str.c_str ());
0fb4aa4b 8958
6c5b2ebe 8959 std::vector<symtab_and_line> sals;
5d9310c4 8960 sals.reserve (markers.size ());
0fb4aa4b 8961
5d9310c4 8962 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 8963 {
5d9310c4
SM
8964 symtab_and_line sal = find_pc_line (marker.address, 0);
8965 sal.pc = marker.address;
6c5b2ebe 8966 sals.push_back (sal);
5d9310c4 8967 }
0fb4aa4b 8968
0fb4aa4b
PA
8969 *arg_p = endp;
8970 return sals;
8971}
8972
bac7c5cf
GB
8973/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8974 according to IS_TRACEPOINT. */
8975
8976static const struct breakpoint_ops *
264f9890
PA
8977breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8978 bool is_tracepoint)
bac7c5cf
GB
8979{
8980 if (is_tracepoint)
8981 {
264f9890 8982 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8983 return &tracepoint_probe_breakpoint_ops;
8984 else
74421c0b 8985 return &code_breakpoint_ops;
bac7c5cf
GB
8986 }
8987 else
8988 {
264f9890 8989 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8990 return &bkpt_probe_breakpoint_ops;
8991 else
74421c0b 8992 return &code_breakpoint_ops;
bac7c5cf
GB
8993 }
8994}
8995
8996/* See breakpoint.h. */
8997
8998const struct breakpoint_ops *
264f9890
PA
8999breakpoint_ops_for_location_spec (const location_spec *locspec,
9000 bool is_tracepoint)
bac7c5cf 9001{
264f9890
PA
9002 if (locspec != nullptr)
9003 return (breakpoint_ops_for_location_spec_type
7464aeaa 9004 (locspec->type (), is_tracepoint));
74421c0b 9005 return &code_breakpoint_ops;
bac7c5cf
GB
9006}
9007
f00aae0f 9008/* See breakpoint.h. */
0101ce28 9009
8cdf0e15
VP
9010int
9011create_breakpoint (struct gdbarch *gdbarch,
264f9890 9012 location_spec *locspec,
e1e01040
PA
9013 const char *cond_string,
9014 int thread, const char *extra_string,
10a636cc 9015 bool force_condition, int parse_extra,
0fb4aa4b 9016 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9017 int ignore_count,
9018 enum auto_boolean pending_break_support,
c0a91b2b 9019 const struct breakpoint_ops *ops,
44f238bb
PA
9020 int from_tty, int enabled, int internal,
9021 unsigned flags)
c3f6f71d 9022{
7efd8fc2 9023 struct linespec_result canonical;
d8de7963 9024 bool pending = false;
2ecee236 9025 int task = -1;
86b17b60 9026 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9027
348d480f
PA
9028 gdb_assert (ops != NULL);
9029
f00aae0f
KS
9030 /* If extra_string isn't useful, set it to NULL. */
9031 if (extra_string != NULL && *extra_string == '\0')
9032 extra_string = NULL;
9033
a70b8144 9034 try
b78a6381 9035 {
264f9890 9036 ops->create_sals_from_location_spec (locspec, &canonical);
b78a6381 9037 }
230d2906 9038 catch (const gdb_exception_error &e)
0101ce28 9039 {
492d29ea
PA
9040 /* If caller is interested in rc value from parse, set
9041 value. */
9042 if (e.error == NOT_FOUND_ERROR)
0101ce28 9043 {
05ff989b
AC
9044 /* If pending breakpoint support is turned off, throw
9045 error. */
fa8d40ab
JJ
9046
9047 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9048 throw;
723a2275
VP
9049
9050 exception_print (gdb_stderr, e);
fa8d40ab 9051
dda83cd7 9052 /* If pending breakpoint support is auto query and the user
05ff989b 9053 selects no, then simply return the error code. */
059fb39f 9054 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9055 && !nquery (_("Make %s pending on future shared library load? "),
9056 bptype_string (type_wanted)))
fd9b8c24 9057 return 0;
fa8d40ab 9058
05ff989b
AC
9059 /* At this point, either the user was queried about setting
9060 a pending breakpoint and selected yes, or pending
9061 breakpoint behavior is on and thus a pending breakpoint
9062 is defaulted on behalf of the user. */
d8de7963 9063 pending = true;
0101ce28 9064 }
492d29ea 9065 else
eedc3f4f 9066 throw;
0101ce28 9067 }
492d29ea 9068
6c5b2ebe 9069 if (!pending && canonical.lsals.empty ())
492d29ea 9070 return 0;
c3f6f71d 9071
c3f6f71d
JM
9072 /* Resolve all line numbers to PC's and verify that the addresses
9073 are ok for the target. */
0101ce28 9074 if (!pending)
f8eba3c6 9075 {
6c5b2ebe
PA
9076 for (auto &lsal : canonical.lsals)
9077 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9078 }
c3f6f71d 9079
7a697b8d 9080 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9081 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9082 {
6c5b2ebe
PA
9083 for (const auto &lsal : canonical.lsals)
9084 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9085 }
7a697b8d 9086
c3f6f71d
JM
9087 /* Verify that condition can be parsed, before setting any
9088 breakpoints. Allocate a separate condition expression for each
4a64f543 9089 breakpoint. */
0101ce28 9090 if (!pending)
c3f6f71d 9091 {
e1e01040
PA
9092 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9093 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9094
f00aae0f 9095 if (parse_extra)
dda83cd7 9096 {
6f781ee3
TT
9097 gdb::unique_xmalloc_ptr<char> rest;
9098 gdb::unique_xmalloc_ptr<char> cond;
52d361e1 9099
6c5b2ebe 9100 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9101
b5fa468f
TBA
9102 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9103 &cond, &thread, &task, &rest);
6f781ee3
TT
9104 cond_string_copy = std::move (cond);
9105 extra_string_copy = std::move (rest);
dda83cd7 9106 }
2f069f6f 9107 else
dda83cd7 9108 {
f00aae0f
KS
9109 if (type_wanted != bp_dprintf
9110 && extra_string != NULL && *extra_string != '\0')
9111 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9112
10a636cc
TBA
9113 /* Check the validity of the condition. We should error out
9114 if the condition is invalid at all of the locations and
9115 if it is not forced. In the PARSE_EXTRA case above, this
9116 check is done when parsing the EXTRA_STRING. */
9117 if (cond_string != nullptr && !force_condition)
9118 {
9119 int num_failures = 0;
9120 const linespec_sals &lsal = canonical.lsals[0];
9121 for (const auto &sal : lsal.sals)
9122 {
9123 const char *cond = cond_string;
9124 try
9125 {
9126 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9127 /* One success is sufficient to keep going. */
9128 break;
9129 }
9130 catch (const gdb_exception_error &)
9131 {
9132 num_failures++;
9133 /* If this is the last sal, error out. */
9134 if (num_failures == lsal.sals.size ())
9135 throw;
9136 }
9137 }
9138 }
9139
0878d0fa
YQ
9140 /* Create a private copy of condition string. */
9141 if (cond_string)
e1e01040 9142 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9143 /* Create a private copy of any extra string. */
9144 if (extra_string)
e1e01040 9145 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9146 }
0fb4aa4b 9147
52d361e1 9148 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9149 std::move (cond_string_copy),
9150 std::move (extra_string_copy),
9151 type_wanted,
d9b3f62e 9152 tempflag ? disp_del : disp_donttouch,
ef4848c7 9153 thread, task, ignore_count,
44f238bb 9154 from_tty, enabled, internal, flags);
c906108c 9155 }
0101ce28
JJ
9156 else
9157 {
73063f51
TT
9158 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9159 type_wanted);
5c1ddcb6 9160 b->locspec = locspec->clone ();
bfccc43c 9161
f00aae0f
KS
9162 if (parse_extra)
9163 b->cond_string = NULL;
e12c7713
MK
9164 else
9165 {
9166 /* Create a private copy of condition string. */
6f781ee3
TT
9167 b->cond_string.reset (cond_string != NULL
9168 ? xstrdup (cond_string)
9169 : NULL);
15630549 9170 b->thread = thread;
e12c7713 9171 }
f00aae0f
KS
9172
9173 /* Create a private copy of any extra string. */
6f781ee3
TT
9174 b->extra_string.reset (extra_string != NULL
9175 ? xstrdup (extra_string)
9176 : NULL);
0101ce28 9177 b->ignore_count = ignore_count;
0101ce28 9178 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9179 b->condition_not_parsed = 1;
41447f92 9180 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9181 if ((type_wanted != bp_breakpoint
dda83cd7 9182 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9183 b->pspace = current_program_space;
8bea4e01 9184
b270e6f9 9185 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9186 }
9187
6c5b2ebe 9188 if (canonical.lsals.size () > 1)
95a42b64 9189 {
3e43a32a
MS
9190 warning (_("Multiple breakpoints were set.\nUse the "
9191 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9192 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9193 }
9194
44702360 9195 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9196
9197 return 1;
c3f6f71d 9198}
c906108c 9199
348d480f 9200/* Set a breakpoint.
72b2ff0e
VP
9201 ARG is a string describing breakpoint address,
9202 condition, and thread.
9203 FLAG specifies if a breakpoint is hardware on,
9204 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9205 and BP_TEMPFLAG. */
348d480f 9206
98deb0da 9207static void
f2fc3015 9208break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9209{
72b2ff0e 9210 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9211 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9212 ? bp_hardware_breakpoint
9213 : bp_breakpoint);
f00aae0f 9214
264f9890
PA
9215 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9216 const struct breakpoint_ops *ops
9217 = breakpoint_ops_for_location_spec (locspec.get (),
9218 false /* is_tracepoint */);
c3f6f71d 9219
8cdf0e15 9220 create_breakpoint (get_current_arch (),
264f9890 9221 locspec.get (),
10a636cc 9222 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9223 tempflag, type_wanted,
8cdf0e15
VP
9224 0 /* Ignore count */,
9225 pending_break_support,
55aa24fb 9226 ops,
8cdf0e15 9227 from_tty,
84f4c1fe 9228 1 /* enabled */,
44f238bb
PA
9229 0 /* internal */,
9230 0);
c906108c
SS
9231}
9232
c906108c
SS
9233/* Helper function for break_command_1 and disassemble_command. */
9234
9235void
fba45db2 9236resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9237{
9238 CORE_ADDR pc;
9239
9240 if (sal->pc == 0 && sal->symtab != NULL)
9241 {
9242 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9243 error (_("No line %d in file \"%s\"."),
05cba821 9244 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9245 sal->pc = pc;
6a048695 9246
4a64f543 9247 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9248 number, then skip the function prologue if necessary. */
6a048695 9249 if (sal->explicit_line)
059acae7 9250 skip_prologue_sal (sal);
c906108c
SS
9251 }
9252
9253 if (sal->section == 0 && sal->symtab != NULL)
9254 {
346d1dfe 9255 const struct blockvector *bv;
3977b71f 9256 const struct block *b;
c5aa993b 9257 struct symbol *sym;
c906108c 9258
43f3e411 9259 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9260 sal->symtab->compunit ());
c906108c
SS
9261 if (bv != NULL)
9262 {
3c9d0506 9263 sym = b->linkage_function ();
c906108c 9264 if (sym != NULL)
dae58e04
TT
9265 sal->section
9266 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9267 else
9268 {
4a64f543 9269 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9270 just have to look harder. This case can be executed
9271 if we have line numbers but no functions (as can
9272 happen in assembly source). */
c906108c 9273
5ed8105e 9274 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9275 switch_to_program_space_and_thread (sal->pspace);
c906108c 9276
5ed8105e 9277 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9278 if (msym.minsym)
1db66e34 9279 sal->section = msym.obj_section ();
c906108c
SS
9280 }
9281 }
9282 }
9283}
9284
9285void
0b39b52e 9286break_command (const char *arg, int from_tty)
c906108c 9287{
db107f19 9288 break_command_1 (arg, 0, from_tty);
c906108c
SS
9289}
9290
c906108c 9291void
0b39b52e 9292tbreak_command (const char *arg, int from_tty)
c906108c 9293{
db107f19 9294 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9295}
9296
c906108c 9297static void
0b39b52e 9298hbreak_command (const char *arg, int from_tty)
c906108c 9299{
db107f19 9300 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9301}
9302
9303static void
0b39b52e 9304thbreak_command (const char *arg, int from_tty)
c906108c 9305{
db107f19 9306 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9307}
9308
e7e0cddf
SS
9309/* The dynamic printf command is mostly like a regular breakpoint, but
9310 with a prewired command list consisting of a single output command,
9311 built from extra arguments supplied on the dprintf command
9312 line. */
9313
da821c7b 9314static void
0b39b52e 9315dprintf_command (const char *arg, int from_tty)
e7e0cddf 9316{
264f9890 9317 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f
KS
9318
9319 /* If non-NULL, ARG should have been advanced past the location;
9320 the next character must be ','. */
9321 if (arg != NULL)
9322 {
9323 if (arg[0] != ',' || arg[1] == '\0')
9324 error (_("Format string required"));
9325 else
9326 {
9327 /* Skip the comma. */
9328 ++arg;
9329 }
9330 }
9331
e7e0cddf 9332 create_breakpoint (get_current_arch (),
264f9890 9333 locspec.get (),
10a636cc 9334 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9335 0, bp_dprintf,
9336 0 /* Ignore count */,
9337 pending_break_support,
74421c0b 9338 &code_breakpoint_ops,
e7e0cddf
SS
9339 from_tty,
9340 1 /* enabled */,
9341 0 /* internal */,
9342 0);
9343}
9344
d3ce09f5 9345static void
0b39b52e 9346agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9347{
9348 error (_("May only run agent-printf on the target"));
9349}
9350
ec45bb67 9351/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9352
ec45bb67
TT
9353int
9354ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9355 const address_space *aspace,
9356 CORE_ADDR bp_addr,
9357 const target_waitstatus &ws)
f1310107 9358{
c272a98c
SM
9359 if (ws.kind () != TARGET_WAITKIND_STOPPED
9360 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9361 return 0;
9362
f1310107
TJB
9363 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9364 bl->length, aspace, bp_addr);
9365}
9366
ec45bb67 9367/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9368
ec45bb67
TT
9369int
9370ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9371{
9372 return target_ranged_break_num_registers ();
9373}
9374
ec45bb67 9375/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9376
ec45bb67 9377enum print_stop_action
7bd86313 9378ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9379{
ec45bb67 9380 struct bp_location *bl = loc;
79a45e25 9381 struct ui_out *uiout = current_uiout;
f1310107 9382
ec45bb67 9383 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9384
9385 /* Ranged breakpoints have only one location. */
9386 gdb_assert (bl && bl->next == NULL);
9387
ec45bb67 9388 annotate_breakpoint (number);
f303dbd6
PA
9389
9390 maybe_print_thread_hit_breakpoint (uiout);
9391
ec45bb67 9392 if (disposition == disp_del)
112e8700 9393 uiout->text ("Temporary ranged breakpoint ");
f1310107 9394 else
112e8700
SM
9395 uiout->text ("Ranged breakpoint ");
9396 if (uiout->is_mi_like_p ())
f1310107 9397 {
112e8700 9398 uiout->field_string ("reason",
f1310107 9399 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9400 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9401 }
78805ff8 9402 print_num_locno (bs, uiout);
112e8700 9403 uiout->text (", ");
f1310107
TJB
9404
9405 return PRINT_SRC_AND_LOC;
9406}
9407
ec45bb67 9408/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9409
ec45bb67 9410bool
a67bcaba 9411ranged_breakpoint::print_one (bp_location **last_loc) const
f1310107 9412{
ec45bb67 9413 struct bp_location *bl = loc;
f1310107 9414 struct value_print_options opts;
79a45e25 9415 struct ui_out *uiout = current_uiout;
f1310107
TJB
9416
9417 /* Ranged breakpoints have only one location. */
9418 gdb_assert (bl && bl->next == NULL);
9419
9420 get_user_print_options (&opts);
9421
9422 if (opts.addressprint)
9423 /* We don't print the address range here, it will be printed later
f7968895 9424 by ranged_breakpoint::print_one_detail. */
112e8700 9425 uiout->field_skip ("addr");
f1310107 9426 annotate_field (5);
ec45bb67 9427 print_breakpoint_location (this, bl);
f1310107 9428 *last_loc = bl;
c01e038b
TT
9429
9430 return true;
f1310107
TJB
9431}
9432
ec45bb67 9433/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9434
ec45bb67
TT
9435void
9436ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9437{
9438 CORE_ADDR address_start, address_end;
ec45bb67 9439 struct bp_location *bl = loc;
d7e74731 9440 string_file stb;
f1310107
TJB
9441
9442 gdb_assert (bl);
9443
9444 address_start = bl->address;
9445 address_end = address_start + bl->length - 1;
9446
112e8700 9447 uiout->text ("\taddress range: ");
d7e74731
PA
9448 stb.printf ("[%s, %s]",
9449 print_core_address (bl->gdbarch, address_start),
9450 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9451 uiout->field_stream ("addr", stb);
9452 uiout->text ("\n");
f1310107
TJB
9453}
9454
ec45bb67 9455/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9456
ec45bb67 9457void
b713485d 9458ranged_breakpoint::print_mention () const
f1310107 9459{
ec45bb67 9460 struct bp_location *bl = loc;
79a45e25 9461 struct ui_out *uiout = current_uiout;
f1310107
TJB
9462
9463 gdb_assert (bl);
ec45bb67 9464 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9465
2d33446d 9466 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
ec45bb67 9467 number, paddress (bl->gdbarch, bl->address),
2d33446d 9468 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9469}
9470
ec45bb67 9471/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9472
ec45bb67 9473void
4d1ae558 9474ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9475{
6cb06a8c 9476 gdb_printf (fp, "break-range %s, %s",
709438c7
PA
9477 locspec->to_string (),
9478 locspec_range_end->to_string ());
04d0163c 9479 print_recreate_thread (fp);
f1310107
TJB
9480}
9481
f1310107
TJB
9482/* Find the address where the end of the breakpoint range should be
9483 placed, given the SAL of the end of the range. This is so that if
9484 the user provides a line number, the end of the range is set to the
9485 last instruction of the given line. */
9486
9487static CORE_ADDR
9488find_breakpoint_range_end (struct symtab_and_line sal)
9489{
9490 CORE_ADDR end;
9491
9492 /* If the user provided a PC value, use it. Otherwise,
9493 find the address of the end of the given location. */
9494 if (sal.explicit_pc)
9495 end = sal.pc;
9496 else
9497 {
9498 int ret;
9499 CORE_ADDR start;
9500
9501 ret = find_line_pc_range (sal, &start, &end);
9502 if (!ret)
9503 error (_("Could not find location of the end of the range."));
9504
9505 /* find_line_pc_range returns the start of the next line. */
9506 end--;
9507 }
9508
9509 return end;
9510}
9511
9512/* Implement the "break-range" CLI command. */
9513
9514static void
0b39b52e 9515break_range_command (const char *arg, int from_tty)
f1310107 9516{
f2fc3015 9517 const char *arg_start;
f1310107
TJB
9518 struct linespec_result canonical_start, canonical_end;
9519 int bp_count, can_use_bp, length;
9520 CORE_ADDR end;
f1310107
TJB
9521
9522 /* We don't support software ranged breakpoints. */
9523 if (target_ranged_break_num_registers () < 0)
9524 error (_("This target does not support hardware ranged breakpoints."));
9525
9526 bp_count = hw_breakpoint_used_count ();
9527 bp_count += target_ranged_break_num_registers ();
9528 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9529 bp_count, 0);
9530 if (can_use_bp < 0)
9531 error (_("Hardware breakpoints used exceeds limit."));
9532
f8eba3c6 9533 arg = skip_spaces (arg);
f1310107
TJB
9534 if (arg == NULL || arg[0] == '\0')
9535 error(_("No address range specified."));
9536
f8eba3c6 9537 arg_start = arg;
264f9890
PA
9538 location_spec_up start_locspec
9539 = string_to_location_spec (&arg, current_language);
9540 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
f1310107
TJB
9541
9542 if (arg[0] != ',')
9543 error (_("Too few arguments."));
6c5b2ebe 9544 else if (canonical_start.lsals.empty ())
f1310107 9545 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9546
6c5b2ebe 9547 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9548
6c5b2ebe
PA
9549 if (canonical_start.lsals.size () > 1
9550 || lsal_start.sals.size () != 1)
f1310107
TJB
9551 error (_("Cannot create a ranged breakpoint with multiple locations."));
9552
6c5b2ebe 9553 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9554 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9555
9556 arg++; /* Skip the comma. */
f8eba3c6 9557 arg = skip_spaces (arg);
f1310107 9558
264f9890 9559 /* Parse the end location specification. */
f1310107 9560
f1310107
TJB
9561 arg_start = arg;
9562
f8eba3c6 9563 /* We call decode_line_full directly here instead of using
264f9890
PA
9564 parse_breakpoint_sals because we need to specify the start
9565 location spec's symtab and line as the default symtab and line
9566 for the end of the range. This makes it possible to have ranges
9567 like "foo.c:27, +14", where +14 means 14 lines from the start
9568 location spec. */
9569 location_spec_up end_locspec
9570 = string_to_location_spec (&arg, current_language);
9571 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9572 sal_start.symtab, sal_start.line,
9573 &canonical_end, NULL, NULL);
9574
6c5b2ebe 9575 if (canonical_end.lsals.empty ())
f1310107 9576 error (_("Could not find location of the end of the range."));
f8eba3c6 9577
6c5b2ebe
PA
9578 const linespec_sals &lsal_end = canonical_end.lsals[0];
9579 if (canonical_end.lsals.size () > 1
9580 || lsal_end.sals.size () != 1)
f1310107
TJB
9581 error (_("Cannot create a ranged breakpoint with multiple locations."));
9582
6c5b2ebe 9583 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9584
9585 end = find_breakpoint_range_end (sal_end);
9586 if (sal_start.pc > end)
177b42fe 9587 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9588
9589 length = end - sal_start.pc + 1;
9590 if (length < 0)
9591 /* Length overflowed. */
9592 error (_("Address range too large."));
9593 else if (length == 1)
9594 {
9595 /* This range is simple enough to be handled by
9596 the `hbreak' command. */
81b1e71c 9597 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9598
9599 return;
9600 }
9601
b925bf21
PA
9602 /* Now set up the breakpoint and install it. */
9603
9604 std::unique_ptr<breakpoint> br
9605 (new ranged_breakpoint (get_current_arch (),
9606 sal_start, length,
264f9890
PA
9607 std::move (start_locspec),
9608 std::move (end_locspec)));
f1310107 9609
27a62b43 9610 install_breakpoint (false, std::move (br), true);
f1310107
TJB
9611}
9612
4a64f543
MS
9613/* Return non-zero if EXP is verified as constant. Returned zero
9614 means EXP is variable. Also the constant detection may fail for
9615 some constant expressions and in such case still falsely return
9616 zero. */
2e6e3d9c 9617
dab72643 9618static bool
65d79d4b
SDJ
9619watchpoint_exp_is_const (const struct expression *exp)
9620{
1eaebe02 9621 return exp->op->constant_p ();
65d79d4b
SDJ
9622}
9623
3a292923 9624/* Implement the "re_set" method for watchpoints. */
348d480f 9625
3a292923
TT
9626void
9627watchpoint::re_set ()
348d480f
PA
9628{
9629 /* Watchpoint can be either on expression using entirely global
9630 variables, or it can be on local variables.
9631
9632 Watchpoints of the first kind are never auto-deleted, and even
9633 persist across program restarts. Since they can use variables
9634 from shared libraries, we need to reparse expression as libraries
9635 are loaded and unloaded.
9636
9637 Watchpoints on local variables can also change meaning as result
9638 of solib event. For example, if a watchpoint uses both a local
9639 and a global variables in expression, it's a local watchpoint,
9640 but unloading of a shared library will make the expression
9641 invalid. This is not a very common use case, but we still
9642 re-evaluate expression, to avoid surprises to the user.
9643
9644 Note that for local watchpoints, we re-evaluate it only if
9645 watchpoints frame id is still valid. If it's not, it means the
9646 watchpoint is out of scope and will be deleted soon. In fact,
9647 I'm not sure we'll ever be called in this case.
9648
9649 If a local watchpoint's frame id is still valid, then
3a292923 9650 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9651
3a5c3e22
PA
9652 Don't do anything about disabled watchpoints, since they will be
9653 reevaluated again when enabled. */
d8de7963 9654 update_watchpoint (this, true /* reparse */);
348d480f
PA
9655}
9656
3a292923 9657/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9658
3a292923
TT
9659int
9660watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9661{
3a292923 9662 int length = exact ? 1 : bl->length;
e09342b5
TJB
9663
9664 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9665 cond_exp.get ());
77b06cd7
TJB
9666}
9667
3a292923 9668/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9669
3a292923
TT
9670int
9671watchpoint::remove_location (struct bp_location *bl,
9672 enum remove_bp_reason reason)
77b06cd7 9673{
3a292923 9674 int length = exact ? 1 : bl->length;
e09342b5
TJB
9675
9676 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9677 cond_exp.get ());
e09342b5
TJB
9678}
9679
3a292923
TT
9680int
9681watchpoint::breakpoint_hit (const struct bp_location *bl,
9682 const address_space *aspace, CORE_ADDR bp_addr,
9683 const target_waitstatus &ws)
e09342b5 9684{
348d480f 9685 struct breakpoint *b = bl->owner;
77b06cd7 9686
348d480f
PA
9687 /* Continuable hardware watchpoints are treated as non-existent if the
9688 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9689 some data address). Otherwise gdb won't stop on a break instruction
9690 in the code (not from a breakpoint) when a hardware watchpoint has
9691 been defined. Also skip watchpoints which we know did not trigger
9692 (did not match the data address). */
9693 if (is_hardware_watchpoint (b)
3a292923 9694 && watchpoint_triggered == watch_triggered_no)
348d480f 9695 return 0;
9c06b0b4 9696
348d480f 9697 return 1;
9c06b0b4
TJB
9698}
9699
3a292923
TT
9700void
9701watchpoint::check_status (bpstat *bs)
9c06b0b4 9702{
348d480f 9703 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9704
348d480f 9705 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9706}
9707
3a292923
TT
9708/* Implement the "resources_needed" method for hardware
9709 watchpoints. */
9c06b0b4 9710
3a292923
TT
9711int
9712watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9713{
3a292923 9714 int length = exact? 1 : bl->length;
348d480f
PA
9715
9716 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9717}
9718
3a292923
TT
9719/* Implement the "works_in_software_mode" method for hardware
9720 watchpoints. */
9c06b0b4 9721
5a61e176 9722bool
3a292923 9723watchpoint::works_in_software_mode () const
9c06b0b4 9724{
efa80663 9725 /* Read and access watchpoints only work with hardware support. */
3a292923 9726 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9727}
9728
3a292923 9729enum print_stop_action
7bd86313 9730watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9731{
348d480f 9732 struct breakpoint *b;
348d480f 9733 enum print_stop_action result;
79a45e25 9734 struct ui_out *uiout = current_uiout;
348d480f
PA
9735
9736 gdb_assert (bs->bp_location_at != NULL);
9737
348d480f
PA
9738 b = bs->breakpoint_at;
9739
f303dbd6
PA
9740 annotate_watchpoint (b->number);
9741 maybe_print_thread_hit_breakpoint (uiout);
9742
d7e74731
PA
9743 string_file stb;
9744
76f9c9cf 9745 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
9746 switch (b->type)
9747 {
348d480f 9748 case bp_watchpoint:
9c06b0b4 9749 case bp_hardware_watchpoint:
112e8700
SM
9750 if (uiout->is_mi_like_p ())
9751 uiout->field_string
9752 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 9753 mention (b);
76f9c9cf 9754 tuple_emitter.emplace (uiout, "value");
112e8700 9755 uiout->text ("\nOld value = ");
850645cf 9756 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9757 uiout->field_stream ("old", stb);
9758 uiout->text ("\nNew value = ");
3a292923 9759 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9760 uiout->field_stream ("new", stb);
9761 uiout->text ("\n");
348d480f
PA
9762 /* More than one watchpoint may have been triggered. */
9763 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9764 break;
9765
9766 case bp_read_watchpoint:
112e8700
SM
9767 if (uiout->is_mi_like_p ())
9768 uiout->field_string
9769 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 9770 mention (b);
76f9c9cf 9771 tuple_emitter.emplace (uiout, "value");
112e8700 9772 uiout->text ("\nValue = ");
3a292923 9773 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9774 uiout->field_stream ("value", stb);
9775 uiout->text ("\n");
348d480f 9776 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9777 break;
9778
9779 case bp_access_watchpoint:
348d480f
PA
9780 if (bs->old_val != NULL)
9781 {
112e8700
SM
9782 if (uiout->is_mi_like_p ())
9783 uiout->field_string
9784 ("reason",
348d480f
PA
9785 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9786 mention (b);
76f9c9cf 9787 tuple_emitter.emplace (uiout, "value");
112e8700 9788 uiout->text ("\nOld value = ");
850645cf 9789 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9790 uiout->field_stream ("old", stb);
9791 uiout->text ("\nNew value = ");
348d480f
PA
9792 }
9793 else
9794 {
9795 mention (b);
112e8700
SM
9796 if (uiout->is_mi_like_p ())
9797 uiout->field_string
9798 ("reason",
348d480f 9799 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 9800 tuple_emitter.emplace (uiout, "value");
112e8700 9801 uiout->text ("\nValue = ");
348d480f 9802 }
3a292923 9803 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9804 uiout->field_stream ("new", stb);
9805 uiout->text ("\n");
348d480f 9806 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9807 break;
9808 default:
348d480f 9809 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9810 }
9811
348d480f
PA
9812 return result;
9813}
9814
3a292923 9815/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 9816
3a292923 9817void
b713485d 9818watchpoint::print_mention () const
348d480f 9819{
79a45e25 9820 struct ui_out *uiout = current_uiout;
46b9c129 9821 const char *tuple_name;
348d480f 9822
3a292923 9823 switch (type)
348d480f
PA
9824 {
9825 case bp_watchpoint:
112e8700 9826 uiout->text ("Watchpoint ");
46b9c129 9827 tuple_name = "wpt";
348d480f
PA
9828 break;
9829 case bp_hardware_watchpoint:
112e8700 9830 uiout->text ("Hardware watchpoint ");
46b9c129 9831 tuple_name = "wpt";
348d480f
PA
9832 break;
9833 case bp_read_watchpoint:
112e8700 9834 uiout->text ("Hardware read watchpoint ");
46b9c129 9835 tuple_name = "hw-rwpt";
348d480f
PA
9836 break;
9837 case bp_access_watchpoint:
112e8700 9838 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 9839 tuple_name = "hw-awpt";
348d480f
PA
9840 break;
9841 default:
f34652de 9842 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9843 }
9844
46b9c129 9845 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9846 uiout->field_signed ("number", number);
112e8700 9847 uiout->text (": ");
3a292923 9848 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
9849}
9850
3a292923 9851/* Implement the "print_recreate" method for watchpoints. */
348d480f 9852
3a292923 9853void
4d1ae558 9854watchpoint::print_recreate (struct ui_file *fp) const
348d480f 9855{
3a292923 9856 switch (type)
348d480f
PA
9857 {
9858 case bp_watchpoint:
9859 case bp_hardware_watchpoint:
6cb06a8c 9860 gdb_printf (fp, "watch");
348d480f
PA
9861 break;
9862 case bp_read_watchpoint:
6cb06a8c 9863 gdb_printf (fp, "rwatch");
348d480f
PA
9864 break;
9865 case bp_access_watchpoint:
6cb06a8c 9866 gdb_printf (fp, "awatch");
348d480f
PA
9867 break;
9868 default:
f34652de 9869 internal_error (_("Invalid watchpoint type."));
348d480f
PA
9870 }
9871
3a292923 9872 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 9873 print_recreate_thread (fp);
348d480f
PA
9874}
9875
3a292923 9876/* Implement the "explains_signal" method for watchpoints. */
427cd150 9877
a6860f3a 9878bool
3a292923 9879watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
9880{
9881 /* A software watchpoint cannot cause a signal other than
9882 GDB_SIGNAL_TRAP. */
3a292923 9883 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 9884 return false;
427cd150 9885
a6860f3a 9886 return true;
427cd150
TT
9887}
9888
3a292923
TT
9889struct masked_watchpoint : public watchpoint
9890{
73063f51
TT
9891 using watchpoint::watchpoint;
9892
3a292923
TT
9893 int insert_location (struct bp_location *) override;
9894 int remove_location (struct bp_location *,
9895 enum remove_bp_reason reason) override;
9896 int resources_needed (const struct bp_location *) override;
5a61e176 9897 bool works_in_software_mode () const override;
7bd86313 9898 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 9899 void print_one_detail (struct ui_out *) const override;
b713485d 9900 void print_mention () const override;
4d1ae558 9901 void print_recreate (struct ui_file *fp) const override;
3a292923 9902};
348d480f 9903
3a292923 9904/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 9905
3a292923
TT
9906int
9907masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 9908{
3a292923 9909 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
9910 bl->watchpoint_type);
9911}
9912
3a292923 9913/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 9914
3a292923
TT
9915int
9916masked_watchpoint::remove_location (struct bp_location *bl,
9917 enum remove_bp_reason reason)
348d480f 9918{
3a292923 9919 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 9920 bl->watchpoint_type);
348d480f
PA
9921}
9922
3a292923
TT
9923/* Implement the "resources_needed" method for masked hardware
9924 watchpoints. */
348d480f 9925
3a292923
TT
9926int
9927masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 9928{
3a292923 9929 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
9930}
9931
3a292923
TT
9932/* Implement the "works_in_software_mode" method for masked hardware
9933 watchpoints. */
348d480f 9934
5a61e176 9935bool
3a292923 9936masked_watchpoint::works_in_software_mode () const
348d480f 9937{
5a61e176 9938 return false;
348d480f
PA
9939}
9940
3a292923
TT
9941/* Implement the "print_it" method for masked hardware
9942 watchpoints. */
348d480f 9943
3a292923 9944enum print_stop_action
7bd86313 9945masked_watchpoint::print_it (const bpstat *bs) const
348d480f
PA
9946{
9947 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9948 struct ui_out *uiout = current_uiout;
348d480f
PA
9949
9950 /* Masked watchpoints have only one location. */
9951 gdb_assert (b->loc && b->loc->next == NULL);
9952
f303dbd6
PA
9953 annotate_watchpoint (b->number);
9954 maybe_print_thread_hit_breakpoint (uiout);
9955
348d480f
PA
9956 switch (b->type)
9957 {
9958 case bp_hardware_watchpoint:
112e8700
SM
9959 if (uiout->is_mi_like_p ())
9960 uiout->field_string
9961 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9962 break;
9963
9964 case bp_read_watchpoint:
112e8700
SM
9965 if (uiout->is_mi_like_p ())
9966 uiout->field_string
9967 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9968 break;
9969
9970 case bp_access_watchpoint:
112e8700
SM
9971 if (uiout->is_mi_like_p ())
9972 uiout->field_string
9973 ("reason",
348d480f
PA
9974 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9975 break;
9976 default:
f34652de 9977 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
9978 }
9979
9980 mention (b);
112e8700 9981 uiout->text (_("\n\
9c06b0b4
TJB
9982Check the underlying instruction at PC for the memory\n\
9983address and value which triggered this watchpoint.\n"));
112e8700 9984 uiout->text ("\n");
9c06b0b4
TJB
9985
9986 /* More than one watchpoint may have been triggered. */
9987 return PRINT_UNKNOWN;
9988}
9989
3a292923
TT
9990/* Implement the "print_one_detail" method for masked hardware
9991 watchpoints. */
9c06b0b4 9992
3a292923
TT
9993void
9994masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
9995{
9996 /* Masked watchpoints have only one location. */
3a292923 9997 gdb_assert (loc && loc->next == NULL);
9c06b0b4 9998
112e8700 9999 uiout->text ("\tmask ");
3a292923 10000 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
112e8700 10001 uiout->text ("\n");
9c06b0b4
TJB
10002}
10003
3a292923
TT
10004/* Implement the "print_mention" method for masked hardware
10005 watchpoints. */
9c06b0b4 10006
3a292923 10007void
b713485d 10008masked_watchpoint::print_mention () const
9c06b0b4 10009{
79a45e25 10010 struct ui_out *uiout = current_uiout;
46b9c129 10011 const char *tuple_name;
9c06b0b4 10012
3a292923 10013 switch (type)
9c06b0b4
TJB
10014 {
10015 case bp_hardware_watchpoint:
112e8700 10016 uiout->text ("Masked hardware watchpoint ");
46b9c129 10017 tuple_name = "wpt";
9c06b0b4
TJB
10018 break;
10019 case bp_read_watchpoint:
112e8700 10020 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10021 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10022 break;
10023 case bp_access_watchpoint:
112e8700 10024 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10025 tuple_name = "hw-awpt";
9c06b0b4
TJB
10026 break;
10027 default:
f34652de 10028 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10029 }
10030
46b9c129 10031 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 10032 uiout->field_signed ("number", number);
112e8700 10033 uiout->text (": ");
3a292923 10034 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
10035}
10036
3a292923
TT
10037/* Implement the "print_recreate" method for masked hardware
10038 watchpoints. */
9c06b0b4 10039
3a292923 10040void
4d1ae558 10041masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 10042{
3a292923 10043 switch (type)
9c06b0b4
TJB
10044 {
10045 case bp_hardware_watchpoint:
6cb06a8c 10046 gdb_printf (fp, "watch");
9c06b0b4
TJB
10047 break;
10048 case bp_read_watchpoint:
6cb06a8c 10049 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
10050 break;
10051 case bp_access_watchpoint:
6cb06a8c 10052 gdb_printf (fp, "awatch");
9c06b0b4
TJB
10053 break;
10054 default:
f34652de 10055 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10056 }
10057
3a292923
TT
10058 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10059 phex (hw_wp_mask, sizeof (CORE_ADDR)));
04d0163c 10060 print_recreate_thread (fp);
9c06b0b4
TJB
10061}
10062
9c06b0b4
TJB
10063/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10064
f2478a7e 10065static bool
9c06b0b4
TJB
10066is_masked_watchpoint (const struct breakpoint *b)
10067{
3a292923 10068 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
10069}
10070
53a5351d 10071/* accessflag: hw_write: watch write,
dda83cd7 10072 hw_read: watch read,
53a5351d 10073 hw_access: watch access (read or write) */
c906108c 10074static void
bbc13ae3 10075watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10076 bool just_location, bool internal)
c906108c 10077{
c1fc2657 10078 struct breakpoint *scope_breakpoint = NULL;
270140bd 10079 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10080 struct value *result;
bb9d5f81 10081 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10082 const char *exp_start = NULL;
10083 const char *exp_end = NULL;
10084 const char *tok, *end_tok;
9c06b0b4 10085 int toklen = -1;
bbc13ae3
KS
10086 const char *cond_start = NULL;
10087 const char *cond_end = NULL;
c906108c 10088 enum bptype bp_type;
37e4754d 10089 int thread = -1;
9c06b0b4
TJB
10090 /* Flag to indicate whether we are going to use masks for
10091 the hardware watchpoint. */
2e362716 10092 bool use_mask = false;
9c06b0b4 10093 CORE_ADDR mask = 0;
2ecee236 10094 int task = -1;
c906108c 10095
37e4754d
LM
10096 /* Make sure that we actually have parameters to parse. */
10097 if (arg != NULL && arg[0] != '\0')
10098 {
bbc13ae3
KS
10099 const char *value_start;
10100
10101 exp_end = arg + strlen (arg);
37e4754d 10102
9c06b0b4
TJB
10103 /* Look for "parameter value" pairs at the end
10104 of the arguments string. */
bbc13ae3 10105 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10106 {
10107 /* Skip whitespace at the end of the argument list. */
10108 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10109 tok--;
10110
10111 /* Find the beginning of the last token.
10112 This is the value of the parameter. */
10113 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10114 tok--;
10115 value_start = tok + 1;
10116
10117 /* Skip whitespace. */
10118 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10119 tok--;
10120
10121 end_tok = tok;
10122
10123 /* Find the beginning of the second to last token.
10124 This is the parameter itself. */
10125 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10126 tok--;
10127 tok++;
10128 toklen = end_tok - tok + 1;
10129
61012eef 10130 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10131 {
5d5658a1 10132 struct thread_info *thr;
9c06b0b4
TJB
10133 /* At this point we've found a "thread" token, which means
10134 the user is trying to set a watchpoint that triggers
10135 only in a specific thread. */
5d5658a1 10136 const char *endp;
37e4754d 10137
9c06b0b4
TJB
10138 if (thread != -1)
10139 error(_("You can specify only one thread."));
37e4754d 10140
2ecee236 10141 if (task != -1)
0a9ccb9d
AB
10142 error (_("You can specify only one of thread or task."));
10143
9c06b0b4 10144 /* Extract the thread ID from the next token. */
5d5658a1 10145 thr = parse_thread_id (value_start, &endp);
37e4754d 10146
5d5658a1 10147 /* Check if the user provided a valid thread ID. */
9c06b0b4 10148 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10149 invalid_thread_id_error (value_start);
9c06b0b4 10150
5d5658a1 10151 thread = thr->global_num;
9c06b0b4 10152 }
8a18382f
TT
10153 else if (toklen == 4 && startswith (tok, "task"))
10154 {
10155 char *tmp;
10156
2ecee236 10157 if (task != -1)
980dbf36
AB
10158 error(_("You can specify only one task."));
10159
0a9ccb9d
AB
10160 if (thread != -1)
10161 error (_("You can specify only one of thread or task."));
10162
8a18382f
TT
10163 task = strtol (value_start, &tmp, 0);
10164 if (tmp == value_start)
10165 error (_("Junk after task keyword."));
10166 if (!valid_task_id (task))
10167 error (_("Unknown task %d."), task);
10168 }
61012eef 10169 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10170 {
10171 /* We've found a "mask" token, which means the user wants to
10172 create a hardware watchpoint that is going to have the mask
10173 facility. */
65558ca5 10174 struct value *mask_value;
37e4754d 10175
9c06b0b4
TJB
10176 if (use_mask)
10177 error(_("You can specify only one mask."));
37e4754d 10178
2e362716 10179 use_mask = just_location = true;
37e4754d 10180
65558ca5 10181 scoped_value_mark mark;
9c06b0b4
TJB
10182 mask_value = parse_to_comma_and_eval (&value_start);
10183 mask = value_as_address (mask_value);
9c06b0b4
TJB
10184 }
10185 else
10186 /* We didn't recognize what we found. We should stop here. */
10187 break;
37e4754d 10188
9c06b0b4
TJB
10189 /* Truncate the string and get rid of the "parameter value" pair before
10190 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10191 exp_end = tok;
9c06b0b4 10192 }
37e4754d 10193 }
bbc13ae3
KS
10194 else
10195 exp_end = arg;
37e4754d 10196
bbc13ae3
KS
10197 /* Parse the rest of the arguments. From here on out, everything
10198 is in terms of a newly allocated string instead of the original
10199 ARG. */
81b1e71c
TT
10200 std::string expression (arg, exp_end - arg);
10201 exp_start = arg = expression.c_str ();
699bd4cf
TT
10202 innermost_block_tracker tracker;
10203 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10204 exp_end = arg;
fa8a61dc
TT
10205 /* Remove trailing whitespace from the expression before saving it.
10206 This makes the eventual display of the expression string a bit
10207 prettier. */
10208 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10209 --exp_end;
10210
65d79d4b 10211 /* Checking if the expression is not constant. */
4d01a485 10212 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10213 {
10214 int len;
10215
10216 len = exp_end - exp_start;
10217 while (len > 0 && isspace (exp_start[len - 1]))
10218 len--;
10219 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10220 }
10221
699bd4cf 10222 exp_valid_block = tracker.block ();
b926417a 10223 struct value *mark = value_mark ();
850645cf 10224 struct value *val_as_value = nullptr;
1eaebe02
TT
10225 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10226 just_location);
06a64a0b 10227
850645cf 10228 if (val_as_value != NULL && just_location)
bb9d5f81 10229 {
5011c493 10230 saved_bitpos = val_as_value->bitpos ();
f49d5fa2 10231 saved_bitsize = val_as_value->bitsize ();
bb9d5f81
PP
10232 }
10233
850645cf 10234 value_ref_ptr val;
06a64a0b
TT
10235 if (just_location)
10236 {
9c06b0b4
TJB
10237 int ret;
10238
06a64a0b 10239 exp_valid_block = NULL;
850645cf 10240 val = release_value (value_addr (result));
06a64a0b 10241 value_free_to_mark (mark);
9c06b0b4
TJB
10242
10243 if (use_mask)
10244 {
850645cf 10245 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10246 mask);
10247 if (ret == -1)
10248 error (_("This target does not support masked watchpoints."));
10249 else if (ret == -2)
10250 error (_("Invalid mask or memory region."));
10251 }
06a64a0b 10252 }
850645cf
TT
10253 else if (val_as_value != NULL)
10254 val = release_value (val_as_value);
c906108c 10255
f1735a53
TT
10256 tok = skip_spaces (arg);
10257 end_tok = skip_to_space (tok);
c906108c
SS
10258
10259 toklen = end_tok - tok;
10260 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10261 {
10262 tok = cond_start = end_tok + 1;
699bd4cf
TT
10263 innermost_block_tracker if_tracker;
10264 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10265
10266 /* The watchpoint expression may not be local, but the condition
10267 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10268 cond_exp_valid_block = if_tracker.block ();
60e1c644 10269
c906108c
SS
10270 cond_end = tok;
10271 }
10272 if (*tok)
8a3fe4f8 10273 error (_("Junk at end of command."));
c906108c 10274
bd2b40ac 10275 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
441d7c93
PA
10276
10277 /* Save this because create_internal_breakpoint below invalidates
10278 'wp_frame'. */
10279 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10280
10281 /* If the expression is "local", then set up a "watchpoint scope"
10282 breakpoint at the point where we've left the scope of the watchpoint
10283 expression. Create the scope breakpoint before the watchpoint, so
10284 that we will encounter it first in bpstat_stop_status. */
441d7c93 10285 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10286 {
441d7c93
PA
10287 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10288
10289 if (frame_id_p (caller_frame_id))
edb3359d 10290 {
441d7c93
PA
10291 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10292 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10293
24b21115 10294 scope_breakpoint
441d7c93 10295 = create_internal_breakpoint (caller_arch, caller_pc,
9efa3c7f 10296 bp_watchpoint_scope);
d983da9c 10297
441d7c93
PA
10298 /* create_internal_breakpoint could invalidate WP_FRAME. */
10299 wp_frame = NULL;
10300
edb3359d 10301 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10302
edb3359d
DJ
10303 /* Automatically delete the breakpoint when it hits. */
10304 scope_breakpoint->disposition = disp_del;
d983da9c 10305
edb3359d 10306 /* Only break in the proper frame (help with recursion). */
441d7c93 10307 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10308
edb3359d 10309 /* Set the address at which we will stop. */
441d7c93
PA
10310 scope_breakpoint->loc->gdbarch = caller_arch;
10311 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10312 scope_breakpoint->loc->address
a6d9a66e
UW
10313 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10314 scope_breakpoint->loc->requested_address,
60270718
AB
10315 scope_breakpoint->type,
10316 current_program_space);
edb3359d 10317 }
d983da9c
DJ
10318 }
10319
e8369a73
AB
10320 /* Now set up the breakpoint. We create all watchpoints as hardware
10321 watchpoints here even if hardware watchpoints are turned off, a call
10322 to update_watchpoint later in this function will cause the type to
10323 drop back to bp_watchpoint (software watchpoint) if required. */
10324
10325 if (accessflag == hw_read)
10326 bp_type = bp_read_watchpoint;
10327 else if (accessflag == hw_access)
10328 bp_type = bp_access_watchpoint;
10329 else
10330 bp_type = bp_hardware_watchpoint;
3a5c3e22 10331
3a292923 10332 std::unique_ptr<watchpoint> w;
348d480f 10333 if (use_mask)
73063f51 10334 w.reset (new masked_watchpoint (nullptr, bp_type));
348d480f 10335 else
73063f51 10336 w.reset (new watchpoint (nullptr, bp_type));
3a292923 10337
0a9ccb9d 10338 /* At most one of thread or task can be set on a watchpoint. */
2ecee236 10339 gdb_assert (thread == -1 || task == -1);
c1fc2657 10340 w->thread = thread;
8a18382f 10341 w->task = task;
c1fc2657
SM
10342 w->disposition = disp_donttouch;
10343 w->pspace = current_program_space;
b22e99fd 10344 w->exp = std::move (exp);
3a5c3e22
PA
10345 w->exp_valid_block = exp_valid_block;
10346 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10347 if (just_location)
10348 {
f28085df 10349 struct type *t = val->type ();
850645cf 10350 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10351
43cc5389 10352 w->exp_string_reparse
a4c50be3 10353 = current_language->watch_location_expression (t, addr);
06a64a0b 10354
8579fd13
AB
10355 w->exp_string = xstrprintf ("-location %.*s",
10356 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10357 }
10358 else
a4c50be3 10359 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10360
10361 if (use_mask)
10362 {
3a5c3e22 10363 w->hw_wp_mask = mask;
9c06b0b4
TJB
10364 }
10365 else
10366 {
3a5c3e22 10367 w->val = val;
bb9d5f81
PP
10368 w->val_bitpos = saved_bitpos;
10369 w->val_bitsize = saved_bitsize;
4c1d86d9 10370 w->val_valid = true;
9c06b0b4 10371 }
77b06cd7 10372
c906108c 10373 if (cond_start)
6f781ee3 10374 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10375 else
c1fc2657 10376 w->cond_string = 0;
c5aa993b 10377
441d7c93 10378 if (frame_id_p (watchpoint_frame))
f6bc2008 10379 {
441d7c93 10380 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10381 w->watchpoint_thread = inferior_ptid;
f6bc2008 10382 }
c906108c 10383 else
f6bc2008 10384 {
3a5c3e22
PA
10385 w->watchpoint_frame = null_frame_id;
10386 w->watchpoint_thread = null_ptid;
f6bc2008 10387 }
c906108c 10388
d983da9c 10389 if (scope_breakpoint != NULL)
c906108c 10390 {
d983da9c
DJ
10391 /* The scope breakpoint is related to the watchpoint. We will
10392 need to act on them together. */
c1fc2657 10393 w->related_breakpoint = scope_breakpoint;
b270e6f9 10394 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10395 }
d983da9c 10396
06a64a0b
TT
10397 if (!just_location)
10398 value_free_to_mark (mark);
2d134ed3 10399
b270e6f9
TT
10400 /* Finally update the new watchpoint. This creates the locations
10401 that should be inserted. */
d8de7963 10402 update_watchpoint (w.get (), true /* reparse */);
a9634178 10403
b270e6f9 10404 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10405}
10406
e09342b5 10407/* Return count of debug registers needed to watch the given expression.
e09342b5 10408 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10409
c906108c 10410static int
a6535de1 10411can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10412{
10413 int found_memory_cnt = 0;
10414
10415 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10416 if (!can_use_hw_watchpoints)
c906108c 10417 return 0;
c5aa993b 10418
a6535de1
TT
10419 gdb_assert (!vals.empty ());
10420 struct value *head = vals[0].get ();
10421
5c44784c
JM
10422 /* Make sure that the value of the expression depends only upon
10423 memory contents, and values computed from them within GDB. If we
10424 find any register references or function calls, we can't use a
10425 hardware watchpoint.
10426
10427 The idea here is that evaluating an expression generates a series
10428 of values, one holding the value of every subexpression. (The
10429 expression a*b+c has five subexpressions: a, b, a*b, c, and
10430 a*b+c.) GDB's values hold almost enough information to establish
10431 the criteria given above --- they identify memory lvalues,
10432 register lvalues, computed values, etcetera. So we can evaluate
10433 the expression, and then scan the chain of values that leaves
10434 behind to decide whether we can detect any possible change to the
10435 expression's final value using only hardware watchpoints.
10436
10437 However, I don't think that the values returned by inferior
10438 function calls are special in any way. So this function may not
10439 notice that an expression involving an inferior function call
10440 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10441 for (const value_ref_ptr &iter : vals)
c906108c 10442 {
a6535de1
TT
10443 struct value *v = iter.get ();
10444
736355f2 10445 if (v->lval () == lval_memory)
c906108c 10446 {
3ee3b270 10447 if (v != head && v->lazy ())
8464be76
DJ
10448 /* A lazy memory lvalue in the chain is one that GDB never
10449 needed to fetch; we either just used its address (e.g.,
10450 `a' in `a.b') or we never needed it at all (e.g., `a'
10451 in `a,b'). This doesn't apply to HEAD; if that is
10452 lazy then it was not readable, but watch it anyway. */
5c44784c 10453 ;
53a5351d 10454 else
5c44784c
JM
10455 {
10456 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10457 it with hardware watchpoints. */
d0c97917 10458 struct type *vtype = check_typedef (v->type ());
2e70b7b9
MS
10459
10460 /* We only watch structs and arrays if user asked for it
10461 explicitly, never if they just happen to appear in a
10462 middle of some value chain. */
10463 if (v == head
78134374
SM
10464 || (vtype->code () != TYPE_CODE_STRUCT
10465 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10466 {
9feb2d07 10467 CORE_ADDR vaddr = v->address ();
e09342b5
TJB
10468 int len;
10469 int num_regs;
10470
a9634178 10471 len = (target_exact_watchpoints
e09342b5 10472 && is_scalar_type_recursive (vtype))?
d0c97917 10473 1 : v->type ()->length ();
2e70b7b9 10474
e09342b5
TJB
10475 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10476 if (!num_regs)
2e70b7b9
MS
10477 return 0;
10478 else
e09342b5 10479 found_memory_cnt += num_regs;
2e70b7b9 10480 }
5c44784c 10481 }
c5aa993b 10482 }
b2227e67 10483 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
38b6c3b3 10484 return 0; /* These are values from the history (e.g., $1). */
736355f2 10485 else if (v->lval () == lval_register)
38b6c3b3 10486 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10487 }
10488
10489 /* The expression itself looks suitable for using a hardware
10490 watchpoint, but give the target machine a chance to reject it. */
10491 return found_memory_cnt;
10492}
10493
8b93c638 10494void
2e362716 10495watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10496{
84f4c1fe 10497 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10498}
10499
a15a5258
AB
10500/* Options for the watch, awatch, and rwatch commands. */
10501
10502struct watch_options
10503{
10504 /* For -location. */
10505 bool location = false;
10506};
10507
10508/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10509
10510 Historically GDB always accepted both '-location' and '-l' flags for
10511 these commands (both flags being synonyms). When converting to the
10512 newer option scheme only '-location' is added here. That's fine (for
10513 backward compatibility) as any non-ambiguous prefix of a flag will be
10514 accepted, so '-l', '-loc', are now all accepted.
10515
10516 What this means is that, if in the future, we add any new flag here
10517 that starts with '-l' then this will break backward compatibility, so
10518 please, don't do that! */
10519
10520static const gdb::option::option_def watch_option_defs[] = {
10521 gdb::option::flag_option_def<watch_options> {
10522 "location",
10523 [] (watch_options *opt) { return &opt->location; },
10524 N_("\
10525This evaluates EXPRESSION and watches the memory to which is refers.\n\
10526-l can be used as a short form of -location."),
10527 },
10528};
10529
10530/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10531 commands. */
10532
10533static gdb::option::option_def_group
10534make_watch_options_def_group (watch_options *opts)
10535{
10536 return {{watch_option_defs}, opts};
10537}
10538
06a64a0b
TT
10539/* A helper function that looks for the "-location" argument and then
10540 calls watch_command_1. */
10541
10542static void
0b39b52e 10543watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10544{
a15a5258
AB
10545 watch_options opts;
10546 auto grp = make_watch_options_def_group (&opts);
10547 gdb::option::process_options
10548 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10549 if (arg != nullptr && *arg == '\0')
10550 arg = nullptr;
10551
10552 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10553}
06a64a0b 10554
a15a5258
AB
10555/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10556static void
10557watch_command_completer (struct cmd_list_element *ignore,
10558 completion_tracker &tracker,
10559 const char *text, const char * /*word*/)
10560{
10561 const auto group = make_watch_options_def_group (nullptr);
10562 if (gdb::option::complete_options
10563 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10564 return;
06a64a0b 10565
a15a5258
AB
10566 const char *word = advance_to_expression_complete_word_point (tracker, text);
10567 expression_completer (ignore, tracker, text, word);
8b93c638 10568}
8926118c 10569
c5aa993b 10570static void
0b39b52e 10571watch_command (const char *arg, int from_tty)
c906108c 10572{
06a64a0b 10573 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10574}
10575
8b93c638 10576void
2e362716 10577rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10578{
84f4c1fe 10579 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10580}
8926118c 10581
c5aa993b 10582static void
0b39b52e 10583rwatch_command (const char *arg, int from_tty)
c906108c 10584{
06a64a0b 10585 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10586}
10587
8b93c638 10588void
2e362716 10589awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10590{
84f4c1fe 10591 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10592}
8926118c 10593
c5aa993b 10594static void
0b39b52e 10595awatch_command (const char *arg, int from_tty)
c906108c 10596{
06a64a0b 10597 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10598}
c906108c 10599\f
c5aa993b 10600
cfc31633
PA
10601/* Data for the FSM that manages the until(location)/advance commands
10602 in infcmd.c. Here because it uses the mechanisms of
10603 breakpoints. */
c906108c 10604
46e3ed7f 10605struct until_break_fsm : public thread_fsm
bfec99b2 10606{
46e3ed7f 10607 /* The thread that was current when the command was executed. */
cfc31633
PA
10608 int thread;
10609
df631783
PA
10610 /* The breakpoint set at the return address in the caller frame,
10611 plus breakpoints at all the destination locations. */
10612 std::vector<breakpoint_up> breakpoints;
cfc31633 10613
46e3ed7f 10614 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 10615 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
10616 : thread_fsm (cmd_interp),
10617 thread (thread),
df631783 10618 breakpoints (std::move (breakpoints))
46e3ed7f
TT
10619 {
10620 }
cfc31633 10621
46e3ed7f
TT
10622 void clean_up (struct thread_info *thread) override;
10623 bool should_stop (struct thread_info *thread) override;
10624 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10625};
10626
cfc31633
PA
10627/* Implementation of the 'should_stop' FSM method for the
10628 until(location)/advance commands. */
10629
46e3ed7f
TT
10630bool
10631until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10632{
df631783
PA
10633 for (const breakpoint_up &bp : breakpoints)
10634 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10635 bp.get ()) != NULL)
10636 {
10637 set_finished ();
10638 break;
10639 }
cfc31633 10640
46e3ed7f 10641 return true;
cfc31633
PA
10642}
10643
10644/* Implementation of the 'clean_up' FSM method for the
10645 until(location)/advance commands. */
10646
46e3ed7f
TT
10647void
10648until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10649{
cfc31633 10650 /* Clean up our temporary breakpoints. */
df631783 10651 breakpoints.clear ();
46e3ed7f 10652 delete_longjmp_breakpoint (thread);
cfc31633
PA
10653}
10654
10655/* Implementation of the 'async_reply_reason' FSM method for the
10656 until(location)/advance commands. */
10657
46e3ed7f
TT
10658enum async_reply_reason
10659until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10660{
10661 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10662}
10663
c906108c 10664void
f2fc3015 10665until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10666{
bd2b40ac 10667 frame_info_ptr frame;
8556afb4
PA
10668 struct gdbarch *frame_gdbarch;
10669 struct frame_id stack_frame_id;
10670 struct frame_id caller_frame_id;
186c406b
TT
10671 int thread;
10672 struct thread_info *tp;
c906108c 10673
70509625 10674 clear_proceed_status (0);
c906108c
SS
10675
10676 /* Set a breakpoint where the user wants it and at return from
4a64f543 10677 this function. */
c5aa993b 10678
264f9890 10679 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f 10680
6c5b2ebe
PA
10681 std::vector<symtab_and_line> sals
10682 = (last_displayed_sal_is_valid ()
264f9890 10683 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
6c5b2ebe
PA
10684 get_last_displayed_symtab (),
10685 get_last_displayed_line ())
264f9890 10686 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 10687 NULL, NULL, 0));
c5aa993b 10688
df631783 10689 if (sals.empty ())
8a3fe4f8 10690 error (_("Couldn't get information on specified line."));
c5aa993b 10691
c906108c 10692 if (*arg)
8a3fe4f8 10693 error (_("Junk at end of arguments."));
c5aa993b 10694
186c406b 10695 tp = inferior_thread ();
5d5658a1 10696 thread = tp->global_num;
186c406b 10697
8556afb4
PA
10698 /* Note linespec handling above invalidates the frame chain.
10699 Installing a breakpoint also invalidates the frame chain (as it
10700 may need to switch threads), so do any frame handling before
10701 that. */
10702
10703 frame = get_selected_frame (NULL);
10704 frame_gdbarch = get_frame_arch (frame);
10705 stack_frame_id = get_stack_frame_id (frame);
10706 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10707
ae66c1fc
EZ
10708 /* Keep within the current frame, or in frames called by the current
10709 one. */
edb3359d 10710
df631783 10711 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
10712
10713 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10714
883bc8d1 10715 if (frame_id_p (caller_frame_id))
c906108c 10716 {
883bc8d1 10717 struct symtab_and_line sal2;
cfc31633 10718 struct gdbarch *caller_gdbarch;
883bc8d1
PA
10719
10720 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10721 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 10722 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
10723
10724 breakpoint_up caller_breakpoint
10725 = set_momentary_breakpoint (caller_gdbarch, sal2,
10726 caller_frame_id, bp_until);
10727 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 10728
0f443d1b 10729 set_longjmp_breakpoint (tp, stack_frame_id);
5419bdae 10730 lj_deleter.emplace (thread);
c906108c 10731 }
c5aa993b 10732
c70a6932
JK
10733 /* set_momentary_breakpoint could invalidate FRAME. */
10734 frame = NULL;
10735
df631783
PA
10736 /* If the user told us to continue until a specified location, we
10737 don't specify a frame at which we need to stop. Otherwise,
10738 specify the selected frame, because we want to stop only at the
10739 very same frame. */
10740 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10741
10742 for (symtab_and_line &sal : sals)
10743 {
10744 resolve_sal_pc (&sal);
10745
10746 breakpoint_up location_breakpoint
10747 = set_momentary_breakpoint (frame_gdbarch, sal,
10748 stop_frame_id, bp_until);
10749 breakpoints.emplace_back (std::move (location_breakpoint));
10750 }
883bc8d1 10751
573269a8
LS
10752 tp->set_thread_fsm
10753 (std::unique_ptr<thread_fsm>
10754 (new until_break_fsm (command_interp (), tp->global_num,
10755 std::move (breakpoints))));
f107f563 10756
5419bdae
TT
10757 if (lj_deleter)
10758 lj_deleter->release ();
f107f563 10759
cfc31633 10760 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 10761}
ae66c1fc 10762
c906108c
SS
10763\f
10764
81b1e71c 10765/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
10766
10767static int
81b1e71c 10768compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 10769{
81b1e71c
TT
10770 uintptr_t ua = (uintptr_t) a;
10771 uintptr_t ub = (uintptr_t) b;
8a2c437b 10772
81b1e71c 10773 if (a->number < b->number)
8a2c437b 10774 return -1;
81b1e71c 10775 else if (a->number > b->number)
8a2c437b
TT
10776 return 1;
10777
10778 /* Now sort by address, in case we see, e..g, two breakpoints with
10779 the number 0. */
10780 if (ua < ub)
10781 return -1;
94b0e70d 10782 return ua > ub ? 1 : 0;
8a2c437b
TT
10783}
10784
80f8a6eb 10785/* Delete breakpoints by address or line. */
c906108c
SS
10786
10787static void
0b39b52e 10788clear_command (const char *arg, int from_tty)
c906108c 10789{
c906108c 10790 int default_match;
c906108c 10791
6c5b2ebe
PA
10792 std::vector<symtab_and_line> decoded_sals;
10793 symtab_and_line last_sal;
10794 gdb::array_view<symtab_and_line> sals;
c906108c
SS
10795 if (arg)
10796 {
6c5b2ebe
PA
10797 decoded_sals
10798 = decode_line_with_current_source (arg,
10799 (DECODE_LINE_FUNFIRSTLINE
10800 | DECODE_LINE_LIST_MODE));
c906108c 10801 default_match = 0;
6c5b2ebe 10802 sals = decoded_sals;
c906108c
SS
10803 }
10804 else
10805 {
1bfeeb0f
JL
10806 /* Set sal's line, symtab, pc, and pspace to the values
10807 corresponding to the last call to print_frame_info. If the
10808 codepoint is not valid, this will set all the fields to 0. */
51abb421 10809 last_sal = get_last_displayed_sal ();
6c5b2ebe 10810 if (last_sal.symtab == 0)
8a3fe4f8 10811 error (_("No source file specified."));
c906108c 10812
c906108c 10813 default_match = 1;
6c5b2ebe 10814 sals = last_sal;
c906108c
SS
10815 }
10816
4a64f543
MS
10817 /* We don't call resolve_sal_pc here. That's not as bad as it
10818 seems, because all existing breakpoints typically have both
10819 file/line and pc set. So, if clear is given file/line, we can
10820 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10821
10822 We only support clearing given the address explicitly
10823 present in breakpoint table. Say, we've set breakpoint
4a64f543 10824 at file:line. There were several PC values for that file:line,
ed0616c6 10825 due to optimization, all in one block.
4a64f543
MS
10826
10827 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10828 PC corresponding to the same file:line, the breakpoint won't
10829 be cleared. We probably can still clear the breakpoint, but
10830 since the other PC value is never presented to user, user
10831 can only find it by guessing, and it does not seem important
10832 to support that. */
10833
4a64f543
MS
10834 /* For each line spec given, delete bps which correspond to it. Do
10835 it in two passes, solely to preserve the current behavior that
10836 from_tty is forced true if we delete more than one
10837 breakpoint. */
c906108c 10838
81b1e71c 10839 std::vector<struct breakpoint *> found;
6c5b2ebe 10840 for (const auto &sal : sals)
c906108c 10841 {
05cba821
JK
10842 const char *sal_fullname;
10843
c906108c 10844 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
10845 If line given (pc == 0), clear all bpts on specified line.
10846 If defaulting, clear all bpts on default line
10847 or at default pc.
c5aa993b 10848
dda83cd7 10849 defaulting sal.pc != 0 tests to do
c5aa993b 10850
dda83cd7
SM
10851 0 1 pc
10852 1 1 pc _and_ line
10853 0 0 line
10854 1 0 <can't happen> */
c906108c 10855
05cba821
JK
10856 sal_fullname = (sal.symtab == NULL
10857 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 10858
4a64f543 10859 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 10860 for (breakpoint *b : all_breakpoints ())
c5aa993b 10861 {
0d381245 10862 int match = 0;
4a64f543 10863 /* Are we going to delete b? */
a5c69b1e
EL
10864 if (b->type != bp_none && !is_watchpoint (b)
10865 && user_breakpoint_p (b))
0d381245 10866 {
40cb8ca5 10867 for (bp_location *loc : b->locations ())
0d381245 10868 {
f8eba3c6
TT
10869 /* If the user specified file:line, don't allow a PC
10870 match. This matches historical gdb behavior. */
10871 int pc_match = (!sal.explicit_line
10872 && sal.pc
10873 && (loc->pspace == sal.pspace)
10874 && (loc->address == sal.pc)
10875 && (!section_is_overlay (loc->section)
10876 || loc->section == sal.section));
4aac40c8
TT
10877 int line_match = 0;
10878
10879 if ((default_match || sal.explicit_line)
2f202fde 10880 && loc->symtab != NULL
05cba821 10881 && sal_fullname != NULL
4aac40c8 10882 && sal.pspace == loc->pspace
05cba821
JK
10883 && loc->line_number == sal.line
10884 && filename_cmp (symtab_to_fullname (loc->symtab),
10885 sal_fullname) == 0)
10886 line_match = 1;
4aac40c8 10887
0d381245
VP
10888 if (pc_match || line_match)
10889 {
10890 match = 1;
10891 break;
10892 }
10893 }
10894 }
10895
10896 if (match)
81b1e71c 10897 found.push_back (b);
c906108c 10898 }
80f8a6eb 10899 }
8a2c437b 10900
80f8a6eb 10901 /* Now go thru the 'found' chain and delete them. */
81b1e71c 10902 if (found.empty ())
80f8a6eb
MS
10903 {
10904 if (arg)
8a3fe4f8 10905 error (_("No breakpoint at %s."), arg);
80f8a6eb 10906 else
8a3fe4f8 10907 error (_("No breakpoint at this line."));
80f8a6eb 10908 }
c906108c 10909
8a2c437b 10910 /* Remove duplicates from the vec. */
81b1e71c 10911 std::sort (found.begin (), found.end (),
b926417a 10912 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10913 {
b926417a 10914 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
10915 });
10916 found.erase (std::unique (found.begin (), found.end (),
b926417a 10917 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10918 {
b926417a 10919 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
10920 }),
10921 found.end ());
8a2c437b 10922
81b1e71c 10923 if (found.size () > 1)
4a64f543 10924 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10925 if (from_tty)
a3f17187 10926 {
81b1e71c 10927 if (found.size () == 1)
6cb06a8c 10928 gdb_printf (_("Deleted breakpoint "));
a3f17187 10929 else
6cb06a8c 10930 gdb_printf (_("Deleted breakpoints "));
a3f17187 10931 }
d6e956e5 10932
81b1e71c 10933 for (breakpoint *iter : found)
80f8a6eb 10934 {
c5aa993b 10935 if (from_tty)
6cb06a8c 10936 gdb_printf ("%d ", iter->number);
81b1e71c 10937 delete_breakpoint (iter);
c906108c 10938 }
80f8a6eb 10939 if (from_tty)
a11ac3b3 10940 gdb_putc ('\n');
c906108c
SS
10941}
10942\f
10943/* Delete breakpoint in BS if they are `delete' breakpoints and
10944 all breakpoints that are marked for deletion, whether hit or not.
10945 This is called after any breakpoint is hit, or after errors. */
10946
10947void
313f3b21 10948breakpoint_auto_delete (bpstat *bs)
c906108c 10949{
c906108c 10950 for (; bs; bs = bs->next)
f431efe5
PA
10951 if (bs->breakpoint_at
10952 && bs->breakpoint_at->disposition == disp_del
c906108c 10953 && bs->stop)
f431efe5 10954 delete_breakpoint (bs->breakpoint_at);
c906108c 10955
1428b37a 10956 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 10957 if (b->disposition == disp_del_at_next_stop)
c5aa993b 10958 delete_breakpoint (b);
c906108c
SS
10959}
10960
4a64f543 10961/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 10962 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
10963 bl_address_is_meaningful says), secondarily by ordering first
10964 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 10965 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
10966
10967static int
39ef2f62 10968bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 10969{
876fa593 10970 if (a->address != b->address)
39ef2f62 10971 return a->address < b->address;
876fa593 10972
dea2aa5f
LM
10973 /* Sort locations at the same address by their pspace number, keeping
10974 locations of the same inferior (in a multi-inferior environment)
10975 grouped. */
10976
10977 if (a->pspace->num != b->pspace->num)
39ef2f62 10978 return a->pspace->num < b->pspace->num;
dea2aa5f 10979
876fa593 10980 /* Sort permanent breakpoints first. */
1a853c52 10981 if (a->permanent != b->permanent)
39ef2f62 10982 return a->permanent > b->permanent;
876fa593 10983
7f32a4d5
PA
10984 /* Sort by type in order to make duplicate determination easier.
10985 See update_global_location_list. This is kept in sync with
10986 breakpoint_locations_match. */
10987 if (a->loc_type < b->loc_type)
10988 return true;
10989
10990 /* Likewise, for range-breakpoints, sort by length. */
10991 if (a->loc_type == bp_loc_hardware_breakpoint
10992 && b->loc_type == bp_loc_hardware_breakpoint
10993 && a->length < b->length)
10994 return true;
10995
c56a97f9
JK
10996 /* Make the internal GDB representation stable across GDB runs
10997 where A and B memory inside GDB can differ. Breakpoint locations of
10998 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
10999
11000 if (a->owner->number != b->owner->number)
39ef2f62 11001 return a->owner->number < b->owner->number;
876fa593 11002
39ef2f62 11003 return a < b;
876fa593
JK
11004}
11005
f5336ca5
PA
11006/* Set bp_locations_placed_address_before_address_max and
11007 bp_locations_shadow_len_after_address_max according to the current
11008 content of the bp_locations array. */
f7545552
TT
11009
11010static void
f5336ca5 11011bp_locations_target_extensions_update (void)
f7545552 11012{
f5336ca5
PA
11013 bp_locations_placed_address_before_address_max = 0;
11014 bp_locations_shadow_len_after_address_max = 0;
876fa593 11015
48d7020b 11016 for (bp_location *bl : all_bp_locations ())
876fa593
JK
11017 {
11018 CORE_ADDR start, end, addr;
11019
11020 if (!bp_location_has_shadow (bl))
11021 continue;
11022
11023 start = bl->target_info.placed_address;
11024 end = start + bl->target_info.shadow_len;
11025
11026 gdb_assert (bl->address >= start);
11027 addr = bl->address - start;
f5336ca5
PA
11028 if (addr > bp_locations_placed_address_before_address_max)
11029 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11030
11031 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11032
11033 gdb_assert (bl->address < end);
11034 addr = end - bl->address;
f5336ca5
PA
11035 if (addr > bp_locations_shadow_len_after_address_max)
11036 bp_locations_shadow_len_after_address_max = addr;
876fa593 11037 }
f7545552
TT
11038}
11039
1e4d1764
YQ
11040/* Download tracepoint locations if they haven't been. */
11041
11042static void
11043download_tracepoint_locations (void)
11044{
dd2e65cc 11045 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11046
5ed8105e 11047 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11048
f6d17b2b 11049 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
11050 {
11051 struct tracepoint *t;
d8de7963 11052 bool bp_location_downloaded = false;
1e4d1764 11053
7ed2c994 11054 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11055 ? !may_insert_fast_tracepoints
11056 : !may_insert_tracepoints))
11057 continue;
11058
dd2e65cc
YQ
11059 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11060 {
11061 if (target_can_download_tracepoint ())
11062 can_download_tracepoint = TRIBOOL_TRUE;
11063 else
11064 can_download_tracepoint = TRIBOOL_FALSE;
11065 }
11066
11067 if (can_download_tracepoint == TRIBOOL_FALSE)
11068 break;
11069
40cb8ca5 11070 for (bp_location *bl : b->locations ())
7ed2c994
YQ
11071 {
11072 /* In tracepoint, locations are _never_ duplicated, so
11073 should_be_inserted is equivalent to
11074 unduplicated_should_be_inserted. */
11075 if (!should_be_inserted (bl) || bl->inserted)
11076 continue;
1e4d1764 11077
7ed2c994 11078 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11079
7ed2c994 11080 target_download_tracepoint (bl);
1e4d1764 11081
7ed2c994 11082 bl->inserted = 1;
d8de7963 11083 bp_location_downloaded = true;
7ed2c994
YQ
11084 }
11085 t = (struct tracepoint *) b;
11086 t->number_on_target = b->number;
f2a8bc8a 11087 if (bp_location_downloaded)
76727919 11088 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11089 }
1e4d1764
YQ
11090}
11091
934709f0
PW
11092/* Swap the insertion/duplication state between two locations. */
11093
11094static void
11095swap_insertion (struct bp_location *left, struct bp_location *right)
11096{
11097 const int left_inserted = left->inserted;
11098 const int left_duplicate = left->duplicate;
b775012e 11099 const int left_needs_update = left->needs_update;
934709f0
PW
11100 const struct bp_target_info left_target_info = left->target_info;
11101
1e4d1764
YQ
11102 /* Locations of tracepoints can never be duplicated. */
11103 if (is_tracepoint (left->owner))
11104 gdb_assert (!left->duplicate);
11105 if (is_tracepoint (right->owner))
11106 gdb_assert (!right->duplicate);
11107
934709f0
PW
11108 left->inserted = right->inserted;
11109 left->duplicate = right->duplicate;
b775012e 11110 left->needs_update = right->needs_update;
934709f0
PW
11111 left->target_info = right->target_info;
11112 right->inserted = left_inserted;
11113 right->duplicate = left_duplicate;
b775012e 11114 right->needs_update = left_needs_update;
934709f0
PW
11115 right->target_info = left_target_info;
11116}
11117
b775012e
LM
11118/* Force the re-insertion of the locations at ADDRESS. This is called
11119 once a new/deleted/modified duplicate location is found and we are evaluating
11120 conditions on the target's side. Such conditions need to be updated on
11121 the target. */
11122
11123static void
11124force_breakpoint_reinsertion (struct bp_location *bl)
11125{
b775012e
LM
11126 CORE_ADDR address = 0;
11127 int pspace_num;
11128
11129 address = bl->address;
11130 pspace_num = bl->pspace->num;
11131
11132 /* This is only meaningful if the target is
11133 evaluating conditions and if the user has
11134 opted for condition evaluation on the target's
11135 side. */
11136 if (gdb_evaluates_breakpoint_condition_p ()
11137 || !target_supports_evaluation_of_breakpoint_conditions ())
11138 return;
11139
11140 /* Flag all breakpoint locations with this address and
11141 the same program space as the location
11142 as "its condition has changed". We need to
11143 update the conditions on the target's side. */
e0d9a270 11144 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11145 {
b775012e
LM
11146 if (!is_breakpoint (loc->owner)
11147 || pspace_num != loc->pspace->num)
11148 continue;
11149
11150 /* Flag the location appropriately. We use a different state to
11151 let everyone know that we already updated the set of locations
11152 with addr bl->address and program space bl->pspace. This is so
11153 we don't have to keep calling these functions just to mark locations
11154 that have already been marked. */
11155 loc->condition_changed = condition_updated;
11156
11157 /* Free the agent expression bytecode as well. We will compute
11158 it later on. */
833177a4 11159 loc->cond_bytecode.reset ();
b775012e
LM
11160 }
11161}
7f32a4d5 11162
44702360
PA
11163/* Called whether new breakpoints are created, or existing breakpoints
11164 deleted, to update the global location list and recompute which
11165 locations are duplicate of which.
b775012e 11166
04086b45
PA
11167 The INSERT_MODE flag determines whether locations may not, may, or
11168 shall be inserted now. See 'enum ugll_insert_mode' for more
11169 info. */
b60e7edf 11170
0d381245 11171static void
44702360 11172update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11173{
b775012e
LM
11174 /* Last breakpoint location address that was marked for update. */
11175 CORE_ADDR last_addr = 0;
11176 /* Last breakpoint location program space that was marked for update. */
11177 int last_pspace_num = -1;
f7545552 11178
2d134ed3
PA
11179 /* Used in the duplicates detection below. When iterating over all
11180 bp_locations, points to the first bp_location of a given address.
11181 Breakpoints and watchpoints of different types are never
11182 duplicates of each other. Keep one pointer for each type of
11183 breakpoint/watchpoint, so we only need to loop over all locations
11184 once. */
11185 struct bp_location *bp_loc_first; /* breakpoint */
11186 struct bp_location *wp_loc_first; /* hardware watchpoint */
11187 struct bp_location *awp_loc_first; /* access watchpoint */
11188 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11189
f5336ca5
PA
11190 /* Saved former bp_locations array which we compare against the newly
11191 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11192 std::vector<bp_location *> old_locations = std::move (bp_locations);
11193 bp_locations.clear ();
876fa593 11194
43892fdf 11195 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11196 for (bp_location *loc : b->locations ())
5d51cd5d 11197 bp_locations.push_back (loc);
7f32a4d5
PA
11198
11199 /* See if we need to "upgrade" a software breakpoint to a hardware
11200 breakpoint. Do this before deciding whether locations are
11201 duplicates. Also do this before sorting because sorting order
11202 depends on location type. */
5d51cd5d
SM
11203 for (bp_location *loc : bp_locations)
11204 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11205 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11206
5d51cd5d 11207 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11208 bp_location_is_less_than);
876fa593 11209
f5336ca5 11210 bp_locations_target_extensions_update ();
74960c60 11211
4a64f543
MS
11212 /* Identify bp_location instances that are no longer present in the
11213 new list, and therefore should be freed. Note that it's not
11214 necessary that those locations should be removed from inferior --
11215 if there's another location at the same address (previously
11216 marked as duplicate), we don't need to remove/insert the
11217 location.
876fa593 11218
4a64f543
MS
11219 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11220 and former bp_location array state respectively. */
876fa593 11221
5d51cd5d
SM
11222 size_t loc_i = 0;
11223 for (bp_location *old_loc : old_locations)
74960c60 11224 {
e5dd4106 11225 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11226 not, we have to free it. */
d8de7963 11227 bool found_object = false;
20874c92 11228 /* Tells if the location should remain inserted in the target. */
d8de7963
AB
11229 bool keep_in_target = false;
11230 bool removed = false;
876fa593 11231
4a64f543
MS
11232 /* Skip LOCP entries which will definitely never be needed.
11233 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11234 while (loc_i < bp_locations.size ()
11235 && bp_locations[loc_i]->address < old_loc->address)
11236 loc_i++;
c7d46a38 11237
5d51cd5d
SM
11238 for (size_t loc2_i = loc_i;
11239 (loc2_i < bp_locations.size ()
11240 && bp_locations[loc2_i]->address == old_loc->address);
11241 loc2_i++)
c7d46a38 11242 {
b775012e
LM
11243 /* Check if this is a new/duplicated location or a duplicated
11244 location that had its condition modified. If so, we want to send
11245 its condition to the target if evaluation of conditions is taking
11246 place there. */
5d51cd5d 11247 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11248 && (last_addr != old_loc->address
11249 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11250 {
5d51cd5d 11251 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11252 last_pspace_num = old_loc->pspace->num;
c7d46a38 11253 }
b775012e 11254
5d51cd5d 11255 if (bp_locations[loc2_i] == old_loc)
d8de7963 11256 found_object = true;
c7d46a38 11257 }
74960c60 11258
b775012e
LM
11259 /* We have already handled this address, update it so that we don't
11260 have to go through updates again. */
11261 last_addr = old_loc->address;
11262
11263 /* Target-side condition evaluation: Handle deleted locations. */
11264 if (!found_object)
11265 force_breakpoint_reinsertion (old_loc);
11266
4a64f543
MS
11267 /* If this location is no longer present, and inserted, look if
11268 there's maybe a new location at the same address. If so,
11269 mark that one inserted, and don't remove this one. This is
11270 needed so that we don't have a time window where a breakpoint
11271 at certain location is not inserted. */
74960c60 11272
876fa593 11273 if (old_loc->inserted)
0d381245 11274 {
4a64f543
MS
11275 /* If the location is inserted now, we might have to remove
11276 it. */
74960c60 11277
876fa593 11278 if (found_object && should_be_inserted (old_loc))
74960c60 11279 {
4a64f543
MS
11280 /* The location is still present in the location list,
11281 and still should be inserted. Don't do anything. */
d8de7963 11282 keep_in_target = true;
74960c60
VP
11283 }
11284 else
11285 {
b775012e
LM
11286 /* This location still exists, but it won't be kept in the
11287 target since it may have been disabled. We proceed to
11288 remove its target-side condition. */
11289
4a64f543
MS
11290 /* The location is either no longer present, or got
11291 disabled. See if there's another location at the
11292 same address, in which case we don't need to remove
11293 this one from the target. */
876fa593 11294
2bdf28a0 11295 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11296 if (bl_address_is_meaningful (old_loc))
876fa593 11297 {
5d51cd5d
SM
11298 for (size_t loc2_i = loc_i;
11299 (loc2_i < bp_locations.size ()
11300 && bp_locations[loc2_i]->address == old_loc->address);
11301 loc2_i++)
876fa593 11302 {
5d51cd5d 11303 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11304
7f32a4d5
PA
11305 if (loc2 == old_loc)
11306 continue;
11307
2d134ed3 11308 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11309 {
85d721b8
PA
11310 /* Read watchpoint locations are switched to
11311 access watchpoints, if the former are not
11312 supported, but the latter are. */
11313 if (is_hardware_watchpoint (old_loc->owner))
11314 {
11315 gdb_assert (is_hardware_watchpoint (loc2->owner));
11316 loc2->watchpoint_type = old_loc->watchpoint_type;
11317 }
11318
934709f0
PW
11319 /* loc2 is a duplicated location. We need to check
11320 if it should be inserted in case it will be
11321 unduplicated. */
7f32a4d5 11322 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11323 {
934709f0 11324 swap_insertion (old_loc, loc2);
d8de7963 11325 keep_in_target = true;
c7d46a38
PA
11326 break;
11327 }
876fa593
JK
11328 }
11329 }
11330 }
74960c60
VP
11331 }
11332
20874c92
VP
11333 if (!keep_in_target)
11334 {
834c0d03 11335 if (remove_breakpoint (old_loc))
20874c92 11336 {
4a64f543
MS
11337 /* This is just about all we can do. We could keep
11338 this location on the global list, and try to
11339 remove it next time, but there's no particular
11340 reason why we will succeed next time.
20874c92 11341
4a64f543
MS
11342 Note that at this point, old_loc->owner is still
11343 valid, as delete_breakpoint frees the breakpoint
11344 only after calling us. */
6cb06a8c
TT
11345 gdb_printf (_("warning: Error removing "
11346 "breakpoint %d\n"),
11347 old_loc->owner->number);
20874c92 11348 }
d8de7963 11349 removed = true;
20874c92 11350 }
0d381245 11351 }
74960c60
VP
11352
11353 if (!found_object)
1c5cfe86 11354 {
fbea99ea 11355 if (removed && target_is_non_stop_p ()
1cf4d951 11356 && need_moribund_for_location_type (old_loc))
20874c92 11357 {
db82e815
PA
11358 /* This location was removed from the target. In
11359 non-stop mode, a race condition is possible where
11360 we've removed a breakpoint, but stop events for that
11361 breakpoint are already queued and will arrive later.
11362 We apply an heuristic to be able to distinguish such
11363 SIGTRAPs from other random SIGTRAPs: we keep this
11364 breakpoint location for a bit, and will retire it
11365 after we see some number of events. The theory here
11366 is that reporting of events should, "on the average",
11367 be fair, so after a while we'll see events from all
11368 threads that have anything of interest, and no longer
11369 need to keep this breakpoint location around. We
11370 don't hold locations forever so to reduce chances of
11371 mistaking a non-breakpoint SIGTRAP for a breakpoint
11372 SIGTRAP.
11373
11374 The heuristic failing can be disastrous on
11375 decr_pc_after_break targets.
11376
11377 On decr_pc_after_break targets, like e.g., x86-linux,
11378 if we fail to recognize a late breakpoint SIGTRAP,
11379 because events_till_retirement has reached 0 too
11380 soon, we'll fail to do the PC adjustment, and report
11381 a random SIGTRAP to the user. When the user resumes
11382 the inferior, it will most likely immediately crash
2dec564e 11383 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11384 corrupted, because of being resumed e.g., in the
11385 middle of a multi-byte instruction, or skipped a
11386 one-byte instruction. This was actually seen happen
11387 on native x86-linux, and should be less rare on
11388 targets that do not support new thread events, like
11389 remote, due to the heuristic depending on
11390 thread_count.
11391
11392 Mistaking a random SIGTRAP for a breakpoint trap
11393 causes similar symptoms (PC adjustment applied when
11394 it shouldn't), but then again, playing with SIGTRAPs
11395 behind the debugger's back is asking for trouble.
11396
11397 Since hardware watchpoint traps are always
11398 distinguishable from other traps, so we don't need to
11399 apply keep hardware watchpoint moribund locations
11400 around. We simply always ignore hardware watchpoint
11401 traps we can no longer explain. */
11402
5b6d1e4f
PA
11403 process_stratum_target *proc_target = nullptr;
11404 for (inferior *inf : all_inferiors ())
11405 if (inf->pspace == old_loc->pspace)
11406 {
11407 proc_target = inf->process_target ();
11408 break;
11409 }
11410 if (proc_target != nullptr)
11411 old_loc->events_till_retirement
11412 = 3 * (thread_count (proc_target) + 1);
11413 else
11414 old_loc->events_till_retirement = 1;
876fa593 11415 old_loc->owner = NULL;
20874c92 11416
1123588c 11417 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11418 }
11419 else
f431efe5
PA
11420 {
11421 old_loc->owner = NULL;
11422 decref_bp_location (&old_loc);
11423 }
20874c92 11424 }
74960c60 11425 }
1c5cfe86 11426
348d480f
PA
11427 /* Rescan breakpoints at the same address and section, marking the
11428 first one as "first" and any others as "duplicates". This is so
11429 that the bpt instruction is only inserted once. If we have a
11430 permanent breakpoint at the same place as BPT, make that one the
11431 official one, and the rest as duplicates. Permanent breakpoints
11432 are sorted first for the same address.
11433
11434 Do the same for hardware watchpoints, but also considering the
11435 watchpoint's type (regular/access/read) and length. */
11436
11437 bp_loc_first = NULL;
11438 wp_loc_first = NULL;
11439 awp_loc_first = NULL;
11440 rwp_loc_first = NULL;
40cb8ca5 11441
48d7020b 11442 for (bp_location *loc : all_bp_locations ())
348d480f
PA
11443 {
11444 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11445 non-NULL. */
348d480f 11446 struct bp_location **loc_first_p;
43892fdf 11447 breakpoint *b = loc->owner;
348d480f 11448
6f380991 11449 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11450 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11451 /* Don't detect duplicate for tracepoint locations because they are
11452 never duplicated. See the comments in field `duplicate' of
11453 `struct bp_location'. */
348d480f 11454 || is_tracepoint (b))
b775012e
LM
11455 {
11456 /* Clear the condition modification flag. */
11457 loc->condition_changed = condition_unchanged;
11458 continue;
11459 }
348d480f 11460
348d480f
PA
11461 if (b->type == bp_hardware_watchpoint)
11462 loc_first_p = &wp_loc_first;
11463 else if (b->type == bp_read_watchpoint)
11464 loc_first_p = &rwp_loc_first;
11465 else if (b->type == bp_access_watchpoint)
11466 loc_first_p = &awp_loc_first;
11467 else
11468 loc_first_p = &bp_loc_first;
11469
11470 if (*loc_first_p == NULL
11471 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11472 || !breakpoint_locations_match (loc, *loc_first_p))
11473 {
11474 *loc_first_p = loc;
11475 loc->duplicate = 0;
b775012e
LM
11476
11477 if (is_breakpoint (loc->owner) && loc->condition_changed)
11478 {
11479 loc->needs_update = 1;
11480 /* Clear the condition modification flag. */
11481 loc->condition_changed = condition_unchanged;
11482 }
348d480f
PA
11483 continue;
11484 }
11485
934709f0
PW
11486
11487 /* This and the above ensure the invariant that the first location
11488 is not duplicated, and is the inserted one.
11489 All following are marked as duplicated, and are not inserted. */
11490 if (loc->inserted)
11491 swap_insertion (loc, *loc_first_p);
348d480f
PA
11492 loc->duplicate = 1;
11493
b775012e
LM
11494 /* Clear the condition modification flag. */
11495 loc->condition_changed = condition_unchanged;
348d480f
PA
11496 }
11497
a25a5a45 11498 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11499 {
04086b45 11500 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11501 insert_breakpoint_locations ();
11502 else
11503 {
44702360
PA
11504 /* Even though the caller told us to not insert new
11505 locations, we may still need to update conditions on the
11506 target's side of breakpoints that were already inserted
11507 if the target is evaluating breakpoint conditions. We
b775012e
LM
11508 only update conditions for locations that are marked
11509 "needs_update". */
11510 update_inserted_breakpoint_locations ();
11511 }
11512 }
348d480f 11513
04086b45 11514 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 11515 download_tracepoint_locations ();
348d480f
PA
11516}
11517
11518void
11519breakpoint_retire_moribund (void)
11520{
1123588c
TT
11521 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11522 {
11523 struct bp_location *loc = moribund_locations[ix];
11524 if (--(loc->events_till_retirement) == 0)
11525 {
11526 decref_bp_location (&loc);
11527 unordered_remove (moribund_locations, ix);
11528 --ix;
11529 }
11530 }
348d480f
PA
11531}
11532
11533static void
44702360 11534update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 11535{
348d480f 11536
a70b8144 11537 try
492d29ea
PA
11538 {
11539 update_global_location_list (insert_mode);
11540 }
230d2906 11541 catch (const gdb_exception_error &e)
492d29ea
PA
11542 {
11543 }
348d480f
PA
11544}
11545
11546/* Clear BKP from a BPS. */
11547
11548static void
313f3b21 11549bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
348d480f 11550{
313f3b21 11551 bpstat *bs;
348d480f
PA
11552
11553 for (bs = bps; bs; bs = bs->next)
11554 if (bs->breakpoint_at == bpt)
11555 {
11556 bs->breakpoint_at = NULL;
11557 bs->old_val = NULL;
11558 /* bs->commands will be freed later. */
11559 }
11560}
11561
11562/* Callback for iterate_over_threads. */
11563static int
11564bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11565{
9a3c8263 11566 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
11567
11568 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11569 return 0;
11570}
11571
7987c463 11572/* See breakpoint.h. */
348d480f 11573
7987c463
TT
11574void
11575code_breakpoint::say_where () const
348d480f
PA
11576{
11577 struct value_print_options opts;
11578
11579 get_user_print_options (&opts);
11580
11581 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11582 single string. */
7987c463 11583 if (loc == NULL)
348d480f 11584 {
f00aae0f 11585 /* For pending locations, the output differs slightly based
7987c463 11586 on extra_string. If this is non-NULL, it contains either
f00aae0f 11587 a condition or dprintf arguments. */
7987c463 11588 if (extra_string == NULL)
f00aae0f 11589 {
7987c463 11590 gdb_printf (_(" (%s) pending."), locspec->to_string ());
f00aae0f 11591 }
7987c463 11592 else if (type == bp_dprintf)
f00aae0f 11593 {
6cb06a8c 11594 gdb_printf (_(" (%s,%s) pending."),
7987c463
TT
11595 locspec->to_string (),
11596 extra_string.get ());
f00aae0f
KS
11597 }
11598 else
11599 {
6cb06a8c 11600 gdb_printf (_(" (%s %s) pending."),
7987c463
TT
11601 locspec->to_string (),
11602 extra_string.get ());
f00aae0f 11603 }
348d480f
PA
11604 }
11605 else
11606 {
7987c463 11607 if (opts.addressprint || loc->symtab == NULL)
6cb06a8c
TT
11608 gdb_printf (" at %ps",
11609 styled_string (address_style.style (),
7987c463
TT
11610 paddress (loc->gdbarch,
11611 loc->address)));
11612 if (loc->symtab != NULL)
f8eba3c6
TT
11613 {
11614 /* If there is a single location, we can print the location
11615 more nicely. */
7987c463 11616 if (loc->next == NULL)
0bb296cb 11617 {
6a831f06 11618 const char *filename
7987c463 11619 = symtab_to_filename_for_display (loc->symtab);
6cb06a8c
TT
11620 gdb_printf (": file %ps, line %d.",
11621 styled_string (file_name_style.style (),
11622 filename),
7987c463 11623 loc->line_number);
0bb296cb 11624 }
f8eba3c6
TT
11625 else
11626 /* This is not ideal, but each location may have a
11627 different file name, and this at least reflects the
11628 real situation somewhat. */
7987c463 11629 gdb_printf (": %s.", locspec->to_string ());
f8eba3c6 11630 }
348d480f 11631
7987c463 11632 if (loc->next)
348d480f 11633 {
066620dc 11634 struct bp_location *iter = loc;
348d480f 11635 int n = 0;
066620dc 11636 for (; iter; iter = iter->next)
348d480f 11637 ++n;
6cb06a8c 11638 gdb_printf (" (%d locations)", n);
348d480f
PA
11639 }
11640 }
11641}
11642
40cb8ca5
SM
11643/* See breakpoint.h. */
11644
a67bcaba 11645bp_location_range breakpoint::locations () const
40cb8ca5 11646{
9be25986 11647 return bp_location_range (this->loc);
40cb8ca5
SM
11648}
11649
4c6a92b1
TT
11650struct bp_location *
11651breakpoint::allocate_location ()
11652{
11653 return new bp_location (this);
11654}
11655
2060206e
PA
11656#define internal_error_pure_virtual_called() \
11657 gdb_assert_not_reached ("pure virtual function called")
11658
4c6a92b1
TT
11659int
11660breakpoint::insert_location (struct bp_location *bl)
11661{
11662 internal_error_pure_virtual_called ();
11663}
11664
4c6a92b1
TT
11665int
11666breakpoint::remove_location (struct bp_location *bl,
11667 enum remove_bp_reason reason)
11668{
11669 internal_error_pure_virtual_called ();
11670}
11671
4c6a92b1
TT
11672int
11673breakpoint::breakpoint_hit (const struct bp_location *bl,
11674 const address_space *aspace,
11675 CORE_ADDR bp_addr,
11676 const target_waitstatus &ws)
11677{
11678 internal_error_pure_virtual_called ();
11679}
11680
4c6a92b1
TT
11681int
11682breakpoint::resources_needed (const struct bp_location *bl)
11683{
11684 internal_error_pure_virtual_called ();
11685}
11686
4c6a92b1 11687enum print_stop_action
7bd86313 11688breakpoint::print_it (const bpstat *bs) const
4c6a92b1
TT
11689{
11690 internal_error_pure_virtual_called ();
11691}
11692
4c6a92b1 11693void
b713485d 11694breakpoint::print_mention () const
4c6a92b1
TT
11695{
11696 internal_error_pure_virtual_called ();
11697}
11698
4c6a92b1 11699void
4d1ae558 11700breakpoint::print_recreate (struct ui_file *fp) const
4c6a92b1
TT
11701{
11702 internal_error_pure_virtual_called ();
11703}
11704
2060206e
PA
11705/* Default breakpoint_ops methods. */
11706
2b5ab5b8 11707void
74421c0b 11708code_breakpoint::re_set ()
348d480f 11709{
06edf0c0 11710 /* FIXME: is this still reachable? */
264f9890 11711 if (breakpoint_location_spec_empty_p (this))
06edf0c0 11712 {
f00aae0f 11713 /* Anything without a location can't be re-set. */
2b5ab5b8 11714 delete_breakpoint (this);
06edf0c0 11715 return;
348d480f 11716 }
06edf0c0 11717
b3d5660a 11718 re_set_default ();
348d480f
PA
11719}
11720
2b5ab5b8 11721int
74421c0b 11722code_breakpoint::insert_location (struct bp_location *bl)
348d480f 11723{
cd6c3b4f
YQ
11724 CORE_ADDR addr = bl->target_info.reqstd_address;
11725
579c6ad9 11726 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
11727 bl->target_info.placed_address = addr;
11728
5d926615 11729 int result;
348d480f 11730 if (bl->loc_type == bp_loc_hardware_breakpoint)
5d926615 11731 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 11732 else
5d926615
TT
11733 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11734
11735 if (result == 0 && bl->probe.prob != nullptr)
11736 {
11737 /* The insertion was successful, now let's set the probe's semaphore
11738 if needed. */
11739 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11740 }
11741
11742 return result;
348d480f
PA
11743}
11744
2b5ab5b8 11745int
74421c0b 11746code_breakpoint::remove_location (struct bp_location *bl,
2b5ab5b8 11747 enum remove_bp_reason reason)
348d480f 11748{
5d926615
TT
11749 if (bl->probe.prob != nullptr)
11750 {
11751 /* Let's clear the semaphore before removing the location. */
11752 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11753 }
11754
348d480f
PA
11755 if (bl->loc_type == bp_loc_hardware_breakpoint)
11756 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11757 else
73971819 11758 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
11759}
11760
2b5ab5b8 11761int
74421c0b 11762code_breakpoint::breakpoint_hit (const struct bp_location *bl,
2b5ab5b8
TT
11763 const address_space *aspace,
11764 CORE_ADDR bp_addr,
11765 const target_waitstatus &ws)
348d480f 11766{
c272a98c
SM
11767 if (ws.kind () != TARGET_WAITKIND_STOPPED
11768 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
11769 return 0;
11770
348d480f
PA
11771 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11772 aspace, bp_addr))
11773 return 0;
11774
11775 if (overlay_debugging /* unmapped overlay section */
11776 && section_is_overlay (bl->section)
11777 && !section_is_mapped (bl->section))
11778 return 0;
11779
11780 return 1;
11781}
11782
f293a0b5
TT
11783int
11784dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11785 const address_space *aspace,
11786 CORE_ADDR bp_addr,
11787 const target_waitstatus &ws)
cd1608cc
PA
11788{
11789 if (dprintf_style == dprintf_style_agent
11790 && target_can_run_breakpoint_commands ())
11791 {
11792 /* An agent-style dprintf never causes a stop. If we see a trap
11793 for this address it must be for a breakpoint that happens to
11794 be set at the same address. */
11795 return 0;
11796 }
11797
f293a0b5 11798 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
cd1608cc
PA
11799}
11800
7dd8e7ae
TT
11801int
11802ordinary_breakpoint::resources_needed (const struct bp_location *bl)
348d480f 11803{
7dd8e7ae 11804 gdb_assert (type == bp_hardware_breakpoint);
348d480f
PA
11805
11806 return 1;
11807}
11808
7dd8e7ae 11809enum print_stop_action
7bd86313 11810ordinary_breakpoint::print_it (const bpstat *bs) const
348d480f 11811{
348d480f 11812 const struct bp_location *bl;
001c8c33 11813 int bp_temp;
79a45e25 11814 struct ui_out *uiout = current_uiout;
348d480f 11815
b6433ede 11816 bl = bs->bp_location_at.get ();
348d480f 11817
7dd8e7ae 11818 bp_temp = disposition == disp_del;
001c8c33
PA
11819 if (bl->address != bl->requested_address)
11820 breakpoint_adjustment_warning (bl->requested_address,
11821 bl->address,
d8de7963 11822 number, true);
7dd8e7ae 11823 annotate_breakpoint (number);
f303dbd6
PA
11824 maybe_print_thread_hit_breakpoint (uiout);
11825
112e8700 11826 if (uiout->is_mi_like_p ())
348d480f 11827 {
112e8700 11828 uiout->field_string ("reason",
001c8c33 11829 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7dd8e7ae 11830 uiout->field_string ("disp", bpdisp_text (disposition));
06edf0c0 11831 }
78805ff8 11832
6a831f06 11833 if (bp_temp)
78805ff8 11834 uiout->text ("Temporary breakpoint ");
6a831f06 11835 else
78805ff8
PW
11836 uiout->text ("Breakpoint ");
11837 print_num_locno (bs, uiout);
11838 uiout->text (", ");
06edf0c0 11839
001c8c33 11840 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11841}
11842
7dd8e7ae 11843void
b713485d 11844ordinary_breakpoint::print_mention () const
06edf0c0 11845{
112e8700 11846 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
11847 return;
11848
7dd8e7ae 11849 switch (type)
06edf0c0
PA
11850 {
11851 case bp_breakpoint:
11852 case bp_gnu_ifunc_resolver:
7dd8e7ae 11853 if (disposition == disp_del)
6cb06a8c 11854 gdb_printf (_("Temporary breakpoint"));
06edf0c0 11855 else
6cb06a8c 11856 gdb_printf (_("Breakpoint"));
7dd8e7ae
TT
11857 gdb_printf (_(" %d"), number);
11858 if (type == bp_gnu_ifunc_resolver)
6cb06a8c 11859 gdb_printf (_(" at gnu-indirect-function resolver"));
06edf0c0
PA
11860 break;
11861 case bp_hardware_breakpoint:
7dd8e7ae 11862 gdb_printf (_("Hardware assisted breakpoint %d"), number);
06edf0c0 11863 break;
e7e0cddf 11864 case bp_dprintf:
7dd8e7ae 11865 gdb_printf (_("Dprintf %d"), number);
e7e0cddf 11866 break;
06edf0c0
PA
11867 }
11868
7987c463 11869 say_where ();
06edf0c0
PA
11870}
11871
7dd8e7ae 11872void
4d1ae558 11873ordinary_breakpoint::print_recreate (struct ui_file *fp) const
06edf0c0 11874{
7dd8e7ae 11875 if (type == bp_breakpoint && disposition == disp_del)
6cb06a8c 11876 gdb_printf (fp, "tbreak");
7dd8e7ae 11877 else if (type == bp_breakpoint)
6cb06a8c 11878 gdb_printf (fp, "break");
7dd8e7ae
TT
11879 else if (type == bp_hardware_breakpoint
11880 && disposition == disp_del)
6cb06a8c 11881 gdb_printf (fp, "thbreak");
7dd8e7ae 11882 else if (type == bp_hardware_breakpoint)
6cb06a8c 11883 gdb_printf (fp, "hbreak");
06edf0c0 11884 else
f34652de 11885 internal_error (_("unhandled breakpoint type %d"), (int) type);
06edf0c0 11886
709438c7 11887 gdb_printf (fp, " %s", locspec->to_string ());
f00aae0f
KS
11888
11889 /* Print out extra_string if this breakpoint is pending. It might
11890 contain, for example, conditions that were set by the user. */
7dd8e7ae
TT
11891 if (loc == NULL && extra_string != NULL)
11892 gdb_printf (fp, " %s", extra_string.get ());
f00aae0f 11893
04d0163c 11894 print_recreate_thread (fp);
06edf0c0
PA
11895}
11896
2b5ab5b8 11897std::vector<symtab_and_line>
264f9890
PA
11898code_breakpoint::decode_location_spec (location_spec *locspec,
11899 program_space *search_pspace)
983af33b 11900{
7464aeaa 11901 if (locspec->type () == PROBE_LOCATION_SPEC)
264f9890 11902 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
5d926615 11903
8f5bc641
TT
11904 struct linespec_result canonical;
11905
11906 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
11907 NULL, 0, &canonical, multiple_symbols_all,
11908 filter.get ());
11909
11910 /* We should get 0 or 1 resulting SALs. */
11911 gdb_assert (canonical.lsals.size () < 2);
11912
11913 if (!canonical.lsals.empty ())
11914 {
11915 const linespec_sals &lsal = canonical.lsals[0];
11916 return std::move (lsal.sals);
11917 }
11918 return {};
983af33b
SDJ
11919}
11920
06edf0c0
PA
11921/* Virtual table for internal breakpoints. */
11922
c359fff5
TT
11923void
11924internal_breakpoint::re_set ()
06edf0c0 11925{
c359fff5 11926 switch (type)
06edf0c0
PA
11927 {
11928 /* Delete overlay event and longjmp master breakpoints; they
11929 will be reset later by breakpoint_re_set. */
11930 case bp_overlay_event:
11931 case bp_longjmp_master:
11932 case bp_std_terminate_master:
11933 case bp_exception_master:
c359fff5 11934 delete_breakpoint (this);
06edf0c0
PA
11935 break;
11936
11937 /* This breakpoint is special, it's set up when the inferior
dda83cd7 11938 starts and we really don't want to touch it. */
06edf0c0
PA
11939 case bp_shlib_event:
11940
11941 /* Like bp_shlib_event, this breakpoint type is special. Once
11942 it is set up, we do not want to touch it. */
11943 case bp_thread_event:
11944 break;
11945 }
11946}
11947
c359fff5
TT
11948void
11949internal_breakpoint::check_status (bpstat *bs)
06edf0c0 11950{
c359fff5 11951 if (type == bp_shlib_event)
a9b3a50f
PA
11952 {
11953 /* If requested, stop when the dynamic linker notifies GDB of
11954 events. This allows the user to get control and place
11955 breakpoints in initializer routines for dynamically loaded
11956 objects (among other things). */
4ec2227a
TT
11957 bs->stop = stop_on_solib_events != 0;
11958 bs->print = stop_on_solib_events != 0;
a9b3a50f
PA
11959 }
11960 else
4ec2227a 11961 bs->stop = false;
06edf0c0
PA
11962}
11963
c359fff5 11964enum print_stop_action
7bd86313 11965internal_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11966{
c359fff5 11967 switch (type)
06edf0c0 11968 {
348d480f
PA
11969 case bp_shlib_event:
11970 /* Did we stop because the user set the stop_on_solib_events
11971 variable? (If so, we report this as a generic, "Stopped due
11972 to shlib event" message.) */
4e9e993a 11973 print_solib_event (false);
348d480f
PA
11974 break;
11975
11976 case bp_thread_event:
11977 /* Not sure how we will get here.
11978 GDB should not stop for these breakpoints. */
6cb06a8c 11979 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11980 break;
11981
11982 case bp_overlay_event:
11983 /* By analogy with the thread event, GDB should not stop for these. */
6cb06a8c 11984 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11985 break;
11986
11987 case bp_longjmp_master:
11988 /* These should never be enabled. */
6cb06a8c 11989 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11990 break;
11991
11992 case bp_std_terminate_master:
11993 /* These should never be enabled. */
6cb06a8c
TT
11994 gdb_printf (_("std::terminate Master Breakpoint: "
11995 "gdb should not stop!\n"));
348d480f
PA
11996 break;
11997
11998 case bp_exception_master:
11999 /* These should never be enabled. */
6cb06a8c
TT
12000 gdb_printf (_("Exception Master Breakpoint: "
12001 "gdb should not stop!\n"));
06edf0c0
PA
12002 break;
12003 }
12004
001c8c33 12005 return PRINT_NOTHING;
06edf0c0
PA
12006}
12007
c359fff5 12008void
b713485d 12009internal_breakpoint::print_mention () const
06edf0c0
PA
12010{
12011 /* Nothing to mention. These breakpoints are internal. */
12012}
12013
06edf0c0
PA
12014/* Virtual table for momentary breakpoints */
12015
1fd30a47
TT
12016void
12017momentary_breakpoint::re_set ()
06edf0c0
PA
12018{
12019 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12020 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12021 Otherwise these should have been blown away via the cleanup chain
12022 or by breakpoint_init_inferior when we rerun the executable. */
12023}
12024
1fd30a47
TT
12025void
12026momentary_breakpoint::check_status (bpstat *bs)
06edf0c0
PA
12027{
12028 /* Nothing. The point of these breakpoints is causing a stop. */
12029}
12030
1fd30a47 12031enum print_stop_action
7bd86313 12032momentary_breakpoint::print_it (const bpstat *bs) const
06edf0c0 12033{
001c8c33 12034 return PRINT_UNKNOWN;
348d480f
PA
12035}
12036
1fd30a47 12037void
b713485d 12038momentary_breakpoint::print_mention () const
348d480f 12039{
06edf0c0 12040 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12041}
12042
e2e4d78b
JK
12043/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12044
12045 It gets cleared already on the removal of the first one of such placed
12046 breakpoints. This is OK as they get all removed altogether. */
12047
c1fc2657 12048longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12049{
c1fc2657 12050 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12051
c1fc2657 12052 if (tp != NULL)
e2e4d78b 12053 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12054}
12055
55aa24fb 12056static void
264f9890
PA
12057bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12058 struct linespec_result *canonical)
03ada39e 12059
55aa24fb
SDJ
12060{
12061 struct linespec_sals lsal;
12062
264f9890 12063 lsal.sals = parse_probes (locspec, NULL, canonical);
709438c7 12064 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12065 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12066}
12067
6c5b2ebe 12068static std::vector<symtab_and_line>
264f9890
PA
12069bkpt_probe_decode_location_spec (struct breakpoint *b,
12070 location_spec *locspec,
12071 program_space *search_pspace)
55aa24fb 12072{
264f9890
PA
12073 std::vector<symtab_and_line> sals
12074 = parse_probes (locspec, search_pspace, NULL);
6c5b2ebe 12075 if (sals.empty ())
55aa24fb 12076 error (_("probe not found"));
6c5b2ebe 12077 return sals;
55aa24fb
SDJ
12078}
12079
779dcceb
TT
12080int
12081tracepoint::breakpoint_hit (const struct bp_location *bl,
12082 const address_space *aspace, CORE_ADDR bp_addr,
12083 const target_waitstatus &ws)
348d480f
PA
12084{
12085 /* By definition, the inferior does not report stops at
12086 tracepoints. */
12087 return 0;
74960c60
VP
12088}
12089
779dcceb
TT
12090void
12091tracepoint::print_one_detail (struct ui_out *uiout) const
74960c60 12092{
779dcceb 12093 if (!static_trace_marker_id.empty ())
348d480f 12094 {
7b572efb
TT
12095 gdb_assert (type == bp_static_tracepoint
12096 || type == bp_static_marker_tracepoint);
cc59ec59 12097
6a831f06
PA
12098 uiout->message ("\tmarker id is %pF\n",
12099 string_field ("static-tracepoint-marker-string-id",
779dcceb 12100 static_trace_marker_id.c_str ()));
348d480f 12101 }
0d381245
VP
12102}
12103
779dcceb 12104void
b713485d 12105tracepoint::print_mention () const
a474d7c2 12106{
112e8700 12107 if (current_uiout->is_mi_like_p ())
348d480f 12108 return;
cc59ec59 12109
779dcceb 12110 switch (type)
348d480f
PA
12111 {
12112 case bp_tracepoint:
6cb06a8c 12113 gdb_printf (_("Tracepoint"));
779dcceb 12114 gdb_printf (_(" %d"), number);
348d480f
PA
12115 break;
12116 case bp_fast_tracepoint:
6cb06a8c 12117 gdb_printf (_("Fast tracepoint"));
779dcceb 12118 gdb_printf (_(" %d"), number);
348d480f
PA
12119 break;
12120 case bp_static_tracepoint:
7b572efb 12121 case bp_static_marker_tracepoint:
6cb06a8c 12122 gdb_printf (_("Static tracepoint"));
779dcceb 12123 gdb_printf (_(" %d"), number);
348d480f
PA
12124 break;
12125 default:
f34652de 12126 internal_error (_("unhandled tracepoint type %d"), (int) type);
348d480f
PA
12127 }
12128
7987c463 12129 say_where ();
a474d7c2
PA
12130}
12131
779dcceb 12132void
4d1ae558 12133tracepoint::print_recreate (struct ui_file *fp) const
a474d7c2 12134{
779dcceb 12135 if (type == bp_fast_tracepoint)
6cb06a8c 12136 gdb_printf (fp, "ftrace");
7b572efb
TT
12137 else if (type == bp_static_tracepoint
12138 || type == bp_static_marker_tracepoint)
6cb06a8c 12139 gdb_printf (fp, "strace");
779dcceb 12140 else if (type == bp_tracepoint)
6cb06a8c 12141 gdb_printf (fp, "trace");
348d480f 12142 else
f34652de 12143 internal_error (_("unhandled tracepoint type %d"), (int) type);
cc59ec59 12144
709438c7 12145 gdb_printf (fp, " %s", locspec->to_string ());
04d0163c 12146 print_recreate_thread (fp);
d9b3f62e 12147
779dcceb
TT
12148 if (pass_count)
12149 gdb_printf (fp, " passcount %d\n", pass_count);
a474d7c2
PA
12150}
12151
bac7c5cf 12152/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12153
12154static void
264f9890
PA
12155tracepoint_probe_create_sals_from_location_spec
12156 (location_spec *locspec,
03ada39e 12157 struct linespec_result *canonical)
55aa24fb
SDJ
12158{
12159 /* We use the same method for breakpoint on probes. */
264f9890 12160 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
55aa24fb
SDJ
12161}
12162
f293a0b5
TT
12163void
12164dprintf_breakpoint::re_set ()
5c2b4418 12165{
b3d5660a 12166 re_set_default ();
5c2b4418 12167
f00aae0f 12168 /* extra_string should never be non-NULL for dprintf. */
f293a0b5 12169 gdb_assert (extra_string != NULL);
5c2b4418
HZ
12170
12171 /* 1 - connect to target 1, that can run breakpoint commands.
12172 2 - create a dprintf, which resolves fine.
12173 3 - disconnect from target 1
12174 4 - connect to target 2, that can NOT run breakpoint commands.
12175
12176 After steps #3/#4, you'll want the dprintf command list to
12177 be updated, because target 1 and 2 may well return different
12178 answers for target_can_run_breakpoint_commands().
12179 Given absence of finer grained resetting, we get to do
12180 it all the time. */
f293a0b5
TT
12181 if (extra_string != NULL)
12182 update_dprintf_command_list (this);
5c2b4418
HZ
12183}
12184
f293a0b5 12185/* Implement the "print_recreate" method for dprintf. */
2d9442cc 12186
f293a0b5 12187void
4d1ae558 12188dprintf_breakpoint::print_recreate (struct ui_file *fp) const
2d9442cc 12189{
709438c7 12190 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
04d0163c 12191 print_recreate_thread (fp);
2d9442cc
HZ
12192}
12193
f293a0b5 12194/* Implement the "after_condition_true" method for dprintf.
9d6e6e84
HZ
12195
12196 dprintf's are implemented with regular commands in their command
12197 list, but we run the commands here instead of before presenting the
12198 stop to the user, as dprintf's don't actually cause a stop. This
12199 also makes it so that the commands of multiple dprintfs at the same
12200 address are all handled. */
12201
f293a0b5
TT
12202void
12203dprintf_breakpoint::after_condition_true (struct bpstat *bs)
9d6e6e84 12204{
9d6e6e84
HZ
12205 /* dprintf's never cause a stop. This wasn't set in the
12206 check_status hook instead because that would make the dprintf's
12207 condition not be evaluated. */
4ec2227a 12208 bs->stop = false;
9d6e6e84
HZ
12209
12210 /* Run the command list here. Take ownership of it instead of
12211 copying. We never want these commands to run later in
12212 bpstat_do_actions, if a breakpoint that causes a stop happens to
12213 be set at same address as this dprintf, or even if running the
12214 commands here throws. */
9c95aea1
KB
12215 counted_command_line cmds = std::move (bs->commands);
12216 gdb_assert (cmds != nullptr);
12217 execute_control_commands (cmds.get (), 0);
9d6e6e84
HZ
12218}
12219
983af33b
SDJ
12220/* The breakpoint_ops structure to be used on static tracepoints with
12221 markers (`-m'). */
12222
12223static void
264f9890
PA
12224strace_marker_create_sals_from_location_spec (location_spec *locspec,
12225 struct linespec_result *canonical)
983af33b
SDJ
12226{
12227 struct linespec_sals lsal;
f00aae0f 12228 const char *arg_start, *arg;
983af33b 12229
40d97ee2 12230 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 12231 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12232
f2fc3015
TT
12233 std::string str (arg_start, arg - arg_start);
12234 const char *ptr = str.c_str ();
264f9890
PA
12235 canonical->locspec
12236 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
983af33b 12237
709438c7 12238 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12239 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12240}
12241
12242static void
12243strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12244 struct linespec_result *canonical,
e1e01040
PA
12245 gdb::unique_xmalloc_ptr<char> cond_string,
12246 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12247 enum bptype type_wanted,
12248 enum bpdisp disposition,
12249 int thread,
12250 int task, int ignore_count,
983af33b 12251 int from_tty, int enabled,
44f238bb 12252 int internal, unsigned flags)
983af33b 12253{
6c5b2ebe 12254 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12255
12256 /* If the user is creating a static tracepoint by marker id
12257 (strace -m MARKER_ID), then store the sals index, so that
12258 breakpoint_re_set can try to match up which of the newly
12259 found markers corresponds to this one, and, don't try to
12260 expand multiple locations for each sal, given than SALS
12261 already should contain all sals for MARKER_ID. */
12262
6c5b2ebe 12263 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12264 {
5c1ddcb6 12265 location_spec_up locspec = canonical->locspec->clone ();
983af33b 12266
3b003a61
PA
12267 std::unique_ptr<tracepoint> tp
12268 (new tracepoint (gdbarch,
12269 type_wanted,
12270 lsal.sals[i],
264f9890 12271 std::move (locspec),
3b003a61
PA
12272 NULL,
12273 std::move (cond_string),
12274 std::move (extra_string),
12275 disposition,
12276 thread, task, ignore_count,
12277 from_tty, enabled, flags,
12278 canonical->special_display));
12279
983af33b
SDJ
12280 /* Given that its possible to have multiple markers with
12281 the same string id, if the user is creating a static
12282 tracepoint by marker id ("strace -m MARKER_ID"), then
12283 store the sals index, so that breakpoint_re_set can
12284 try to match up which of the newly found markers
12285 corresponds to this one */
12286 tp->static_trace_marker_id_idx = i;
12287
b270e6f9 12288 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12289 }
12290}
12291
6d7a8c56 12292std::vector<symtab_and_line>
264f9890
PA
12293static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12294 program_space *search_pspace)
983af33b 12295{
40d97ee2 12296 const char *s = as_linespec_location_spec (locspec)->spec_string;
983af33b 12297
6c5b2ebe 12298 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
6d7a8c56 12299 if (sals.size () > static_trace_marker_id_idx)
983af33b 12300 {
6d7a8c56 12301 sals[0] = sals[static_trace_marker_id_idx];
6c5b2ebe
PA
12302 sals.resize (1);
12303 return sals;
983af33b
SDJ
12304 }
12305 else
6d7a8c56 12306 error (_("marker %s not found"), static_trace_marker_id.c_str ());
983af33b
SDJ
12307}
12308
8613a476
TT
12309/* Static tracepoints with marker (`-m'). */
12310static struct breakpoint_ops strace_marker_breakpoint_ops =
12311{
264f9890 12312 strace_marker_create_sals_from_location_spec,
8613a476
TT
12313 strace_marker_create_breakpoints_sal,
12314};
983af33b 12315
d8de7963 12316static bool
983af33b
SDJ
12317strace_marker_p (struct breakpoint *b)
12318{
7b572efb 12319 return b->type == bp_static_marker_tracepoint;
983af33b
SDJ
12320}
12321
53a5351d 12322/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 12323 structures. */
c906108c
SS
12324
12325void
fba45db2 12326delete_breakpoint (struct breakpoint *bpt)
c906108c 12327{
8a3fe4f8 12328 gdb_assert (bpt != NULL);
c906108c 12329
4a64f543
MS
12330 /* Has this bp already been deleted? This can happen because
12331 multiple lists can hold pointers to bp's. bpstat lists are
12332 especial culprits.
12333
12334 One example of this happening is a watchpoint's scope bp. When
12335 the scope bp triggers, we notice that the watchpoint is out of
12336 scope, and delete it. We also delete its scope bp. But the
12337 scope bp is marked "auto-deleting", and is already on a bpstat.
12338 That bpstat is then checked for auto-deleting bp's, which are
12339 deleted.
12340
12341 A real solution to this problem might involve reference counts in
12342 bp's, and/or giving them pointers back to their referencing
12343 bpstat's, and teaching delete_breakpoint to only free a bp's
12344 storage when no more references were extent. A cheaper bandaid
12345 was chosen. */
c906108c
SS
12346 if (bpt->type == bp_none)
12347 return;
12348
4a64f543
MS
12349 /* At least avoid this stale reference until the reference counting
12350 of breakpoints gets resolved. */
d0fb5eae 12351 if (bpt->related_breakpoint != bpt)
e5a0a904 12352 {
d0fb5eae 12353 struct breakpoint *related;
3a5c3e22 12354 struct watchpoint *w;
d0fb5eae
JK
12355
12356 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 12357 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 12358 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
12359 w = (struct watchpoint *) bpt;
12360 else
12361 w = NULL;
12362 if (w != NULL)
12363 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
12364
12365 /* Unlink bpt from the bpt->related_breakpoint ring. */
12366 for (related = bpt; related->related_breakpoint != bpt;
12367 related = related->related_breakpoint);
12368 related->related_breakpoint = bpt->related_breakpoint;
12369 bpt->related_breakpoint = bpt;
e5a0a904
JK
12370 }
12371
a9634178
TJB
12372 /* watch_command_1 creates a watchpoint but only sets its number if
12373 update_watchpoint succeeds in creating its bp_locations. If there's
12374 a problem in that process, we'll be asked to delete the half-created
12375 watchpoint. In that case, don't announce the deletion. */
12376 if (bpt->number)
76727919 12377 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 12378
c906108c
SS
12379 if (breakpoint_chain == bpt)
12380 breakpoint_chain = bpt->next;
12381
43892fdf 12382 for (breakpoint *b : all_breakpoints ())
c906108c 12383 if (b->next == bpt)
01add95b
SM
12384 {
12385 b->next = bpt->next;
12386 break;
12387 }
c906108c 12388
f431efe5
PA
12389 /* Be sure no bpstat's are pointing at the breakpoint after it's
12390 been freed. */
12391 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 12392 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
12393 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12394 commands are associated with the bpstat; if we remove it here,
12395 then the later call to bpstat_do_actions (&stop_bpstat); in
12396 event-top.c won't do anything, and temporary breakpoints with
12397 commands won't work. */
12398
12399 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12400
4a64f543
MS
12401 /* Now that breakpoint is removed from breakpoint list, update the
12402 global location list. This will remove locations that used to
12403 belong to this breakpoint. Do this before freeing the breakpoint
12404 itself, since remove_breakpoint looks at location's owner. It
12405 might be better design to have location completely
12406 self-contained, but it's not the case now. */
44702360 12407 update_global_location_list (UGLL_DONT_INSERT);
74960c60 12408
4a64f543
MS
12409 /* On the chance that someone will soon try again to delete this
12410 same bp, we mark it as deleted before freeing its storage. */
c906108c 12411 bpt->type = bp_none;
4d01a485 12412 delete bpt;
c906108c
SS
12413}
12414
51be5b68
PA
12415/* Iterator function to call a user-provided callback function once
12416 for each of B and its related breakpoints. */
12417
12418static void
12419iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 12420 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
12421{
12422 struct breakpoint *related;
12423
12424 related = b;
12425 do
12426 {
12427 struct breakpoint *next;
12428
12429 /* FUNCTION may delete RELATED. */
12430 next = related->related_breakpoint;
12431
12432 if (next == related)
12433 {
12434 /* RELATED is the last ring entry. */
48649e1b 12435 function (related);
51be5b68
PA
12436
12437 /* FUNCTION may have deleted it, so we'd never reach back to
12438 B. There's nothing left to do anyway, so just break
12439 out. */
12440 break;
12441 }
12442 else
48649e1b 12443 function (related);
51be5b68
PA
12444
12445 related = next;
12446 }
12447 while (related != b);
12448}
95a42b64 12449
4495129a 12450static void
981a3fb3 12451delete_command (const char *arg, int from_tty)
c906108c 12452{
ea9365bb
TT
12453 dont_repeat ();
12454
c906108c
SS
12455 if (arg == 0)
12456 {
12457 int breaks_to_delete = 0;
12458
46c6471b 12459 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
12460 internal breakpoints, these have to be deleted with an
12461 explicit breakpoint number argument. */
43892fdf 12462 for (breakpoint *b : all_breakpoints ())
46c6471b 12463 if (user_breakpoint_p (b))
973d738b
DJ
12464 {
12465 breaks_to_delete = 1;
12466 break;
12467 }
c906108c
SS
12468
12469 /* Ask user only if there are some breakpoints to delete. */
12470 if (!from_tty
e2e0b3e5 12471 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
12472 for (breakpoint *b : all_breakpoints_safe ())
12473 if (user_breakpoint_p (b))
12474 delete_breakpoint (b);
c906108c
SS
12475 }
12476 else
48649e1b 12477 map_breakpoint_numbers
b926417a 12478 (arg, [&] (breakpoint *br)
48649e1b 12479 {
b926417a 12480 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 12481 });
c906108c
SS
12482}
12483
c2f4122d
PA
12484/* Return true if all locations of B bound to PSPACE are pending. If
12485 PSPACE is NULL, all locations of all program spaces are
12486 considered. */
12487
d8de7963 12488static bool
c2f4122d 12489all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 12490{
40cb8ca5 12491 for (bp_location *loc : b->locations ())
c2f4122d
PA
12492 if ((pspace == NULL
12493 || loc->pspace == pspace)
12494 && !loc->shlib_disabled
8645ff69 12495 && !loc->pspace->executing_startup)
d8de7963
AB
12496 return false;
12497 return true;
fe3f5fa8
VP
12498}
12499
776592bf 12500/* Subroutine of update_breakpoint_locations to simplify it.
d8de7963 12501 Return true if multiple fns in list LOC have the same name.
776592bf
DE
12502 Null names are ignored. */
12503
d8de7963 12504static bool
776592bf
DE
12505ambiguous_names_p (struct bp_location *loc)
12506{
12507 struct bp_location *l;
2698f5ea 12508 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 12509 xcalloc, xfree));
776592bf
DE
12510
12511 for (l = loc; l != NULL; l = l->next)
12512 {
12513 const char **slot;
23d6ee64 12514 const char *name = l->function_name.get ();
776592bf
DE
12515
12516 /* Allow for some names to be NULL, ignore them. */
12517 if (name == NULL)
12518 continue;
12519
c1fb9836 12520 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 12521 INSERT);
4a64f543
MS
12522 /* NOTE: We can assume slot != NULL here because xcalloc never
12523 returns NULL. */
776592bf 12524 if (*slot != NULL)
d8de7963 12525 return true;
776592bf
DE
12526 *slot = name;
12527 }
12528
d8de7963 12529 return false;
776592bf
DE
12530}
12531
0fb4aa4b
PA
12532/* When symbols change, it probably means the sources changed as well,
12533 and it might mean the static tracepoint markers are no longer at
12534 the same address or line numbers they used to be at last we
12535 checked. Losing your static tracepoints whenever you rebuild is
12536 undesirable. This function tries to resync/rematch gdb static
12537 tracepoints with the markers on the target, for static tracepoints
12538 that have not been set by marker id. Static tracepoint that have
12539 been set by marker id are reset by marker id in breakpoint_re_set.
12540 The heuristic is:
12541
12542 1) For a tracepoint set at a specific address, look for a marker at
12543 the old PC. If one is found there, assume to be the same marker.
12544 If the name / string id of the marker found is different from the
12545 previous known name, assume that means the user renamed the marker
12546 in the sources, and output a warning.
12547
12548 2) For a tracepoint set at a given line number, look for a marker
12549 at the new address of the old line number. If one is found there,
12550 assume to be the same marker. If the name / string id of the
12551 marker found is different from the previous known name, assume that
12552 means the user renamed the marker in the sources, and output a
12553 warning.
12554
12555 3) If a marker is no longer found at the same address or line, it
12556 may mean the marker no longer exists. But it may also just mean
12557 the code changed a bit. Maybe the user added a few lines of code
12558 that made the marker move up or down (in line number terms). Ask
12559 the target for info about the marker with the string id as we knew
12560 it. If found, update line number and address in the matching
12561 static tracepoint. This will get confused if there's more than one
12562 marker with the same ID (possible in UST, although unadvised
12563 precisely because it confuses tools). */
12564
12565static struct symtab_and_line
12566update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12567{
d9b3f62e 12568 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
12569 struct static_tracepoint_marker marker;
12570 CORE_ADDR pc;
0fb4aa4b
PA
12571
12572 pc = sal.pc;
12573 if (sal.line)
12574 find_line_pc (sal.symtab, sal.line, &pc);
12575
12576 if (target_static_tracepoint_marker_at (pc, &marker))
12577 {
5d9310c4 12578 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 12579 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
12580 b->number, tp->static_trace_marker_id.c_str (),
12581 marker.str_id.c_str ());
0fb4aa4b 12582
5d9310c4 12583 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
12584
12585 return sal;
12586 }
12587
12588 /* Old marker wasn't found on target at lineno. Try looking it up
12589 by string ID. */
12590 if (!sal.explicit_pc
12591 && sal.line != 0
12592 && sal.symtab != NULL
5d9310c4 12593 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 12594 {
5d9310c4
SM
12595 std::vector<static_tracepoint_marker> markers
12596 = target_static_tracepoint_markers_by_strid
12597 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 12598
5d9310c4 12599 if (!markers.empty ())
0fb4aa4b 12600 {
0fb4aa4b 12601 struct symbol *sym;
80e1d417 12602 struct static_tracepoint_marker *tpmarker;
79a45e25 12603 struct ui_out *uiout = current_uiout;
0fb4aa4b 12604
5d9310c4 12605 tpmarker = &markers[0];
0fb4aa4b 12606
5d9310c4 12607 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
12608
12609 warning (_("marker for static tracepoint %d (%s) not "
12610 "found at previous line number"),
5d9310c4 12611 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 12612
51abb421 12613 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 12614 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 12615 uiout->text ("Now in ");
0fb4aa4b
PA
12616 if (sym)
12617 {
987012b8 12618 uiout->field_string ("func", sym->print_name (),
e43b10e1 12619 function_name_style.style ());
112e8700 12620 uiout->text (" at ");
0fb4aa4b 12621 }
112e8700 12622 uiout->field_string ("file",
cbe56571 12623 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 12624 file_name_style.style ());
112e8700 12625 uiout->text (":");
0fb4aa4b 12626
112e8700 12627 if (uiout->is_mi_like_p ())
0fb4aa4b 12628 {
0b0865da 12629 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 12630
112e8700 12631 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
12632 }
12633
381befee 12634 uiout->field_signed ("line", sal2.line);
112e8700 12635 uiout->text ("\n");
0fb4aa4b 12636
80e1d417 12637 b->loc->line_number = sal2.line;
2f202fde 12638 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 12639
40d97ee2
PA
12640 std::unique_ptr<explicit_location_spec> els
12641 (new explicit_location_spec ());
12642 els->source_filename
12643 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12644 els->line_offset.offset = b->loc->line_number;
12645 els->line_offset.sign = LINE_OFFSET_NONE;
12646
12647 b->locspec = std::move (els);
0fb4aa4b
PA
12648
12649 /* Might be nice to check if function changed, and warn if
12650 so. */
0fb4aa4b
PA
12651 }
12652 }
12653 return sal;
12654}
12655
d8de7963 12656/* Returns true iff locations A and B are sufficiently same that
8d3788bd
VP
12657 we don't need to report breakpoint as changed. */
12658
d8de7963 12659static bool
8d3788bd
VP
12660locations_are_equal (struct bp_location *a, struct bp_location *b)
12661{
12662 while (a && b)
12663 {
12664 if (a->address != b->address)
d8de7963 12665 return false;
8d3788bd
VP
12666
12667 if (a->shlib_disabled != b->shlib_disabled)
d8de7963 12668 return false;
8d3788bd
VP
12669
12670 if (a->enabled != b->enabled)
d8de7963 12671 return false;
8d3788bd 12672
b5fa468f 12673 if (a->disabled_by_cond != b->disabled_by_cond)
d8de7963 12674 return false;
b5fa468f 12675
8d3788bd
VP
12676 a = a->next;
12677 b = b->next;
12678 }
12679
12680 if ((a == NULL) != (b == NULL))
d8de7963 12681 return false;
8d3788bd 12682
d8de7963 12683 return true;
8d3788bd
VP
12684}
12685
c2f4122d
PA
12686/* Split all locations of B that are bound to PSPACE out of B's
12687 location list to a separate list and return that list's head. If
12688 PSPACE is NULL, hoist out all locations of B. */
12689
12690static struct bp_location *
12691hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12692{
12693 struct bp_location head;
12694 struct bp_location *i = b->loc;
12695 struct bp_location **i_link = &b->loc;
12696 struct bp_location *hoisted = &head;
12697
12698 if (pspace == NULL)
12699 {
12700 i = b->loc;
12701 b->loc = NULL;
12702 return i;
12703 }
12704
12705 head.next = NULL;
12706
12707 while (i != NULL)
12708 {
12709 if (i->pspace == pspace)
12710 {
12711 *i_link = i->next;
12712 i->next = NULL;
12713 hoisted->next = i;
12714 hoisted = i;
12715 }
12716 else
12717 i_link = &i->next;
12718 i = *i_link;
12719 }
12720
12721 return head.next;
12722}
12723
12724/* Create new breakpoint locations for B (a hardware or software
12725 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12726 zero, then B is a ranged breakpoint. Only recreates locations for
12727 FILTER_PSPACE. Locations of other program spaces are left
12728 untouched. */
f1310107 12729
0e30163f 12730void
74421c0b 12731update_breakpoint_locations (code_breakpoint *b,
c2f4122d 12732 struct program_space *filter_pspace,
6c5b2ebe
PA
12733 gdb::array_view<const symtab_and_line> sals,
12734 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 12735{
c2f4122d 12736 struct bp_location *existing_locations;
0d381245 12737
6c5b2ebe 12738 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
12739 {
12740 /* Ranged breakpoints have only one start location and one end
12741 location. */
12742 b->enable_state = bp_disabled;
6cb06a8c
TT
12743 gdb_printf (gdb_stderr,
12744 _("Could not reset ranged breakpoint %d: "
12745 "multiple locations found\n"),
12746 b->number);
f8eba3c6
TT
12747 return;
12748 }
f1310107 12749
4a64f543
MS
12750 /* If there's no new locations, and all existing locations are
12751 pending, don't do anything. This optimizes the common case where
12752 all locations are in the same shared library, that was unloaded.
12753 We'd like to retain the location, so that when the library is
12754 loaded again, we don't loose the enabled/disabled status of the
12755 individual locations. */
6c5b2ebe 12756 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
12757 return;
12758
c2f4122d 12759 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 12760
6c5b2ebe 12761 for (const auto &sal : sals)
fe3f5fa8 12762 {
f8eba3c6
TT
12763 struct bp_location *new_loc;
12764
6c5b2ebe 12765 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 12766
06615628 12767 new_loc = b->add_location (sal);
fe3f5fa8 12768
0d381245
VP
12769 /* Reparse conditions, they might contain references to the
12770 old symtab. */
12771 if (b->cond_string != NULL)
12772 {
bbc13ae3 12773 const char *s;
fe3f5fa8 12774
6f781ee3 12775 s = b->cond_string.get ();
a70b8144 12776 try
0d381245 12777 {
6c5b2ebe
PA
12778 new_loc->cond = parse_exp_1 (&s, sal.pc,
12779 block_for_pc (sal.pc),
0d381245
VP
12780 0);
12781 }
230d2906 12782 catch (const gdb_exception_error &e)
0d381245 12783 {
b5fa468f 12784 new_loc->disabled_by_cond = true;
0d381245
VP
12785 }
12786 }
fe3f5fa8 12787
6c5b2ebe 12788 if (!sals_end.empty ())
f1310107 12789 {
6c5b2ebe 12790 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 12791
6c5b2ebe 12792 new_loc->length = end - sals[0].pc + 1;
f1310107 12793 }
0d381245 12794 }
fe3f5fa8 12795
4a64f543
MS
12796 /* If possible, carry over 'disable' status from existing
12797 breakpoints. */
0d381245
VP
12798 {
12799 struct bp_location *e = existing_locations;
776592bf
DE
12800 /* If there are multiple breakpoints with the same function name,
12801 e.g. for inline functions, comparing function names won't work.
12802 Instead compare pc addresses; this is just a heuristic as things
12803 may have moved, but in practice it gives the correct answer
12804 often enough until a better solution is found. */
12805 int have_ambiguous_names = ambiguous_names_p (b->loc);
12806
0d381245
VP
12807 for (; e; e = e->next)
12808 {
b5fa468f 12809 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 12810 {
776592bf
DE
12811 if (have_ambiguous_names)
12812 {
40cb8ca5 12813 for (bp_location *l : b->locations ())
7f32a4d5
PA
12814 {
12815 /* Ignore software vs hardware location type at
12816 this point, because with "set breakpoint
12817 auto-hw", after a re-set, locations that were
12818 hardware can end up as software, or vice versa.
12819 As mentioned above, this is an heuristic and in
12820 practice should give the correct answer often
12821 enough. */
12822 if (breakpoint_locations_match (e, l, true))
12823 {
b5fa468f
TBA
12824 l->enabled = e->enabled;
12825 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
12826 break;
12827 }
12828 }
776592bf
DE
12829 }
12830 else
12831 {
40cb8ca5 12832 for (bp_location *l : b->locations ())
776592bf 12833 if (l->function_name
23d6ee64
TT
12834 && strcmp (e->function_name.get (),
12835 l->function_name.get ()) == 0)
776592bf 12836 {
b5fa468f
TBA
12837 l->enabled = e->enabled;
12838 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
12839 break;
12840 }
12841 }
0d381245
VP
12842 }
12843 }
12844 }
fe3f5fa8 12845
8d3788bd 12846 if (!locations_are_equal (existing_locations, b->loc))
76727919 12847 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
12848}
12849
264f9890 12850/* Find the SaL locations corresponding to the given LOCSPEC.
ef23e705
TJB
12851 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12852
2c9a6d72
TT
12853std::vector<symtab_and_line>
12854code_breakpoint::location_spec_to_sals (location_spec *locspec,
12855 struct program_space *search_pspace,
12856 int *found)
ef23e705 12857{
cc06b668 12858 struct gdb_exception exception;
ef23e705 12859
6c5b2ebe
PA
12860 std::vector<symtab_and_line> sals;
12861
a70b8144 12862 try
ef23e705 12863 {
2c9a6d72 12864 sals = decode_location_spec (locspec, search_pspace);
ef23e705 12865 }
94aeb44b 12866 catch (gdb_exception_error &e)
ef23e705 12867 {
d8de7963 12868 int not_found_and_ok = false;
492d29ea 12869
ef23e705
TJB
12870 /* For pending breakpoints, it's expected that parsing will
12871 fail until the right shared library is loaded. User has
12872 already told to create pending breakpoints and don't need
12873 extra messages. If breakpoint is in bp_shlib_disabled
12874 state, then user already saw the message about that
12875 breakpoint being disabled, and don't want to see more
12876 errors. */
58438ac1 12877 if (e.error == NOT_FOUND_ERROR
2c9a6d72
TT
12878 && (condition_not_parsed
12879 || (loc != NULL
c2f4122d 12880 && search_pspace != NULL
2c9a6d72
TT
12881 && loc->pspace != search_pspace)
12882 || (loc && loc->shlib_disabled)
12883 || (loc && loc->pspace->executing_startup)
12884 || enable_state == bp_disabled))
d8de7963 12885 not_found_and_ok = true;
ef23e705
TJB
12886
12887 if (!not_found_and_ok)
12888 {
12889 /* We surely don't want to warn about the same breakpoint
12890 10 times. One solution, implemented here, is disable
12891 the breakpoint on error. Another solution would be to
12892 have separate 'warning emitted' flag. Since this
12893 happens only when a binary has changed, I don't know
12894 which approach is better. */
2c9a6d72 12895 enable_state = bp_disabled;
eedc3f4f 12896 throw;
ef23e705 12897 }
94aeb44b
TT
12898
12899 exception = std::move (e);
ef23e705
TJB
12900 }
12901
492d29ea 12902 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 12903 {
6c5b2ebe
PA
12904 for (auto &sal : sals)
12905 resolve_sal_pc (&sal);
2c9a6d72 12906 if (condition_not_parsed && extra_string != NULL)
ef23e705 12907 {
2c9a6d72
TT
12908 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12909 int local_thread, local_task;
ef23e705 12910
2c9a6d72
TT
12911 find_condition_and_thread_for_sals (sals, extra_string.get (),
12912 &local_cond, &local_thread,
12913 &local_task, &local_extra);
12914 gdb_assert (cond_string == nullptr);
12915 if (local_cond != nullptr)
12916 cond_string = std::move (local_cond);
12917 thread = local_thread;
12918 task = local_task;
12919 if (local_extra != nullptr)
12920 extra_string = std::move (local_extra);
12921 condition_not_parsed = 0;
ef23e705
TJB
12922 }
12923
2c9a6d72
TT
12924 if (type == bp_static_tracepoint)
12925 sals[0] = update_static_tracepoint (this, sals[0]);
ef23e705 12926
58438ac1
TT
12927 *found = 1;
12928 }
12929 else
12930 *found = 0;
ef23e705
TJB
12931
12932 return sals;
12933}
12934
348d480f
PA
12935/* The default re_set method, for typical hardware or software
12936 breakpoints. Reevaluate the breakpoint and recreate its
12937 locations. */
12938
b3d5660a
TT
12939void
12940code_breakpoint::re_set_default ()
ef23e705 12941{
c2f4122d 12942 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 12943 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 12944
6c5b2ebe 12945 int found;
2c9a6d72
TT
12946 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12947 filter_pspace,
12948 &found);
ef23e705 12949 if (found)
6c5b2ebe 12950 expanded = std::move (sals);
ef23e705 12951
b3d5660a 12952 if (locspec_range_end != nullptr)
f1310107 12953 {
6c5b2ebe 12954 std::vector<symtab_and_line> sals_end
2c9a6d72 12955 = location_spec_to_sals (locspec_range_end.get (),
264f9890 12956 filter_pspace, &found);
f1310107 12957 if (found)
6c5b2ebe 12958 expanded_end = std::move (sals_end);
f1310107
TJB
12959 }
12960
b3d5660a 12961 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
28010a5d
PA
12962}
12963
983af33b
SDJ
12964/* Default method for creating SALs from an address string. It basically
12965 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12966
12967static void
264f9890
PA
12968create_sals_from_location_spec_default (location_spec *locspec,
12969 struct linespec_result *canonical)
983af33b 12970{
264f9890 12971 parse_breakpoint_sals (locspec, canonical);
983af33b
SDJ
12972}
12973
bf469271 12974/* Reset a breakpoint. */
c906108c 12975
bf469271
PA
12976static void
12977breakpoint_re_set_one (breakpoint *b)
c906108c 12978{
fdf44873
TT
12979 input_radix = b->input_radix;
12980 set_language (b->language);
c906108c 12981
c47614fe 12982 b->re_set ();
c906108c
SS
12983}
12984
c2f4122d
PA
12985/* Re-set breakpoint locations for the current program space.
12986 Locations bound to other program spaces are left untouched. */
12987
c906108c 12988void
69de3c6a 12989breakpoint_re_set (void)
c906108c 12990{
c5aa993b 12991 {
fdf44873
TT
12992 scoped_restore_current_language save_language;
12993 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 12994 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 12995
8e817061
JB
12996 /* breakpoint_re_set_one sets the current_language to the language
12997 of the breakpoint it is resetting (see prepare_re_set_context)
12998 before re-evaluating the breakpoint's location. This change can
12999 unfortunately get undone by accident if the language_mode is set
13000 to auto, and we either switch frames, or more likely in this context,
13001 we select the current frame.
13002
13003 We prevent this by temporarily turning the language_mode to
13004 language_mode_manual. We restore it once all breakpoints
13005 have been reset. */
13006 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13007 language_mode = language_mode_manual;
13008
5ed8105e
PA
13009 /* Note: we must not try to insert locations until after all
13010 breakpoints have been re-set. Otherwise, e.g., when re-setting
13011 breakpoint 1, we'd insert the locations of breakpoint 2, which
13012 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13013
1428b37a 13014 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 13015 {
a70b8144 13016 try
bf469271
PA
13017 {
13018 breakpoint_re_set_one (b);
13019 }
230d2906 13020 catch (const gdb_exception &ex)
bf469271
PA
13021 {
13022 exception_fprintf (gdb_stderr, ex,
13023 "Error in re-setting breakpoint %d: ",
13024 b->number);
13025 }
5ed8105e 13026 }
5ed8105e
PA
13027
13028 jit_breakpoint_re_set ();
13029 }
6c95b8df 13030
af02033e
PP
13031 create_overlay_event_breakpoint ();
13032 create_longjmp_master_breakpoint ();
13033 create_std_terminate_master_breakpoint ();
186c406b 13034 create_exception_master_breakpoint ();
2a7f3dff
PA
13035
13036 /* Now we can insert. */
13037 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13038}
13039\f
c906108c
SS
13040/* Reset the thread number of this breakpoint:
13041
13042 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13043 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13044void
fba45db2 13045breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13046{
13047 if (b->thread != -1)
13048 {
00431a78 13049 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13050
13051 /* We're being called after following a fork. The new fork is
13052 selected as current, and unless this was a vfork will have a
13053 different program space from the original thread. Reset that
13054 as well. */
13055 b->loc->pspace = current_program_space;
c906108c
SS
13056 }
13057}
13058
03ac34d5
MS
13059/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13060 If from_tty is nonzero, it prints a message to that effect,
13061 which ends with a period (no newline). */
13062
c906108c 13063void
fba45db2 13064set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13065{
c906108c
SS
13066 if (count < 0)
13067 count = 0;
13068
43892fdf 13069 for (breakpoint *b : all_breakpoints ())
c906108c 13070 if (b->number == bptnum)
01add95b
SM
13071 {
13072 if (is_tracepoint (b))
13073 {
13074 if (from_tty && count != 0)
6cb06a8c
TT
13075 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13076 bptnum);
01add95b
SM
13077 return;
13078 }
13079
13080 b->ignore_count = count;
13081 if (from_tty)
13082 {
13083 if (count == 0)
6cb06a8c
TT
13084 gdb_printf (_("Will stop next time "
13085 "breakpoint %d is reached."),
13086 bptnum);
01add95b 13087 else if (count == 1)
6cb06a8c
TT
13088 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13089 bptnum);
01add95b 13090 else
6cb06a8c
TT
13091 gdb_printf (_("Will ignore next %d "
13092 "crossings of breakpoint %d."),
13093 count, bptnum);
01add95b
SM
13094 }
13095 gdb::observers::breakpoint_modified.notify (b);
13096 return;
13097 }
c906108c 13098
8a3fe4f8 13099 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13100}
13101
c906108c
SS
13102/* Command to set ignore-count of breakpoint N to COUNT. */
13103
13104static void
0b39b52e 13105ignore_command (const char *args, int from_tty)
c906108c 13106{
0b39b52e 13107 const char *p = args;
52f0bd74 13108 int num;
c906108c
SS
13109
13110 if (p == 0)
e2e0b3e5 13111 error_no_arg (_("a breakpoint number"));
c5aa993b 13112
c906108c 13113 num = get_number (&p);
5c44784c 13114 if (num == 0)
8a3fe4f8 13115 error (_("bad breakpoint number: '%s'"), args);
c906108c 13116 if (*p == 0)
8a3fe4f8 13117 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13118
13119 set_ignore_count (num,
13120 longest_to_int (value_as_long (parse_and_eval (p))),
13121 from_tty);
221ea385 13122 if (from_tty)
6cb06a8c 13123 gdb_printf ("\n");
c906108c
SS
13124}
13125\f
d0fe4701
XR
13126
13127/* Call FUNCTION on each of the breakpoints with numbers in the range
13128 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13129
13130static void
d0fe4701
XR
13131map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13132 gdb::function_view<void (breakpoint *)> function)
c906108c 13133{
d0fe4701
XR
13134 if (bp_num_range.first == 0)
13135 {
13136 warning (_("bad breakpoint number at or near '%d'"),
13137 bp_num_range.first);
13138 }
13139 else
c906108c 13140 {
d0fe4701 13141 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13142 {
d0fe4701
XR
13143 bool match = false;
13144
1428b37a 13145 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 13146 if (b->number == i)
5c44784c 13147 {
bfd28288 13148 match = true;
48649e1b 13149 function (b);
11cf8741 13150 break;
5c44784c 13151 }
bfd28288 13152 if (!match)
6cb06a8c 13153 gdb_printf (_("No breakpoint number %d.\n"), i);
c5aa993b 13154 }
c906108c
SS
13155 }
13156}
13157
d0fe4701
XR
13158/* Call FUNCTION on each of the breakpoints whose numbers are given in
13159 ARGS. */
13160
13161static void
13162map_breakpoint_numbers (const char *args,
13163 gdb::function_view<void (breakpoint *)> function)
13164{
13165 if (args == NULL || *args == '\0')
13166 error_no_arg (_("one or more breakpoint numbers"));
13167
13168 number_or_range_parser parser (args);
13169
13170 while (!parser.finished ())
13171 {
13172 int num = parser.get_number ();
13173 map_breakpoint_number_range (std::make_pair (num, num), function);
13174 }
13175}
13176
13177/* Return the breakpoint location structure corresponding to the
13178 BP_NUM and LOC_NUM values. */
13179
0d381245 13180static struct bp_location *
d0fe4701 13181find_location_by_number (int bp_num, int loc_num)
0d381245 13182{
43892fdf 13183 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
13184
13185 if (!b || b->number != bp_num)
d0fe4701 13186 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13187
0d381245 13188 if (loc_num == 0)
d0fe4701 13189 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13190
d0fe4701 13191 int n = 0;
40cb8ca5 13192 for (bp_location *loc : b->locations ())
d0fe4701
XR
13193 if (++n == loc_num)
13194 return loc;
13195
13196 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13197}
13198
95e95a6d
PA
13199/* Modes of operation for extract_bp_num. */
13200enum class extract_bp_kind
13201{
13202 /* Extracting a breakpoint number. */
13203 bp,
13204
13205 /* Extracting a location number. */
13206 loc,
13207};
13208
13209/* Extract a breakpoint or location number (as determined by KIND)
13210 from the string starting at START. TRAILER is a character which
13211 can be found after the number. If you don't want a trailer, use
13212 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13213 string. This always returns a positive integer. */
13214
13215static int
13216extract_bp_num (extract_bp_kind kind, const char *start,
13217 int trailer, const char **end_out = NULL)
13218{
13219 const char *end = start;
13220 int num = get_number_trailer (&end, trailer);
13221 if (num < 0)
13222 error (kind == extract_bp_kind::bp
13223 ? _("Negative breakpoint number '%.*s'")
13224 : _("Negative breakpoint location number '%.*s'"),
13225 int (end - start), start);
13226 if (num == 0)
13227 error (kind == extract_bp_kind::bp
13228 ? _("Bad breakpoint number '%.*s'")
13229 : _("Bad breakpoint location number '%.*s'"),
13230 int (end - start), start);
13231
13232 if (end_out != NULL)
13233 *end_out = end;
13234 return num;
13235}
13236
13237/* Extract a breakpoint or location range (as determined by KIND) in
13238 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13239 representing the (inclusive) range. The returned pair's elements
13240 are always positive integers. */
13241
13242static std::pair<int, int>
13243extract_bp_or_bp_range (extract_bp_kind kind,
13244 const std::string &arg,
13245 std::string::size_type arg_offset)
13246{
13247 std::pair<int, int> range;
13248 const char *bp_loc = &arg[arg_offset];
13249 std::string::size_type dash = arg.find ('-', arg_offset);
13250 if (dash != std::string::npos)
13251 {
13252 /* bp_loc is a range (x-z). */
13253 if (arg.length () == dash + 1)
13254 error (kind == extract_bp_kind::bp
13255 ? _("Bad breakpoint number at or near: '%s'")
13256 : _("Bad breakpoint location number at or near: '%s'"),
13257 bp_loc);
13258
13259 const char *end;
13260 const char *start_first = bp_loc;
13261 const char *start_second = &arg[dash + 1];
13262 range.first = extract_bp_num (kind, start_first, '-');
13263 range.second = extract_bp_num (kind, start_second, '\0', &end);
13264
13265 if (range.first > range.second)
13266 error (kind == extract_bp_kind::bp
13267 ? _("Inverted breakpoint range at '%.*s'")
13268 : _("Inverted breakpoint location range at '%.*s'"),
13269 int (end - start_first), start_first);
13270 }
13271 else
13272 {
13273 /* bp_loc is a single value. */
13274 range.first = extract_bp_num (kind, bp_loc, '\0');
13275 range.second = range.first;
13276 }
13277 return range;
13278}
13279
d0fe4701
XR
13280/* Extract the breakpoint/location range specified by ARG. Returns
13281 the breakpoint range in BP_NUM_RANGE, and the location range in
13282 BP_LOC_RANGE.
13283
13284 ARG may be in any of the following forms:
13285
13286 x where 'x' is a breakpoint number.
13287 x-y where 'x' and 'y' specify a breakpoint numbers range.
13288 x.y where 'x' is a breakpoint number and 'y' a location number.
13289 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13290 location number range.
13291*/
13292
cc638e86 13293static void
d0fe4701
XR
13294extract_bp_number_and_location (const std::string &arg,
13295 std::pair<int, int> &bp_num_range,
13296 std::pair<int, int> &bp_loc_range)
13297{
13298 std::string::size_type dot = arg.find ('.');
13299
13300 if (dot != std::string::npos)
13301 {
13302 /* Handle 'x.y' and 'x.y-z' cases. */
13303
13304 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 13305 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 13306
95e95a6d
PA
13307 bp_num_range.first
13308 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13309 bp_num_range.second = bp_num_range.first;
d0fe4701 13310
95e95a6d
PA
13311 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13312 arg, dot + 1);
d0fe4701
XR
13313 }
13314 else
13315 {
13316 /* Handle x and x-y cases. */
d0fe4701 13317
95e95a6d 13318 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
13319 bp_loc_range.first = 0;
13320 bp_loc_range.second = 0;
13321 }
d0fe4701
XR
13322}
13323
13324/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13325 specifies whether to enable or disable. */
13326
13327static void
13328enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13329{
13330 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13331 if (loc != NULL)
13332 {
b5fa468f
TBA
13333 if (loc->disabled_by_cond && enable)
13334 error (_("Breakpoint %d's condition is invalid at location %d, "
13335 "cannot enable."), bp_num, loc_num);
13336
d0fe4701
XR
13337 if (loc->enabled != enable)
13338 {
13339 loc->enabled = enable;
13340 mark_breakpoint_location_modified (loc);
13341 }
13342 if (target_supports_enable_disable_tracepoint ()
13343 && current_trace_status ()->running && loc->owner
13344 && is_tracepoint (loc->owner))
13345 target_disable_tracepoint (loc);
13346 }
13347 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13348
e5213e2c
SF
13349 gdb::observers::breakpoint_modified.notify (loc->owner);
13350}
13351
13352/* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13353 owner. 1-based indexing. -1 signals NOT FOUND. */
13354
13355static int
d8a77e4c 13356find_loc_num_by_location (const bp_location *loc)
e5213e2c
SF
13357{
13358 if (loc != nullptr && loc->owner != nullptr)
13359 {
13360 /* Locations use 1-based indexing. */
13361 int loc_num = 1;
13362 for (bp_location *it : loc->owner->locations ())
13363 {
13364 if (it == loc)
13365 return loc_num;
13366 loc_num++;
13367 }
13368 }
13369 return -1;
13370}
13371
13372/* Enable or disable a breakpoint location LOC. ENABLE
13373 specifies whether to enable or disable. */
13374
13375void
13376enable_disable_bp_location (bp_location *loc, bool enable)
13377{
13378 if (loc == nullptr)
13379 error (_("Breakpoint location is invalid."));
13380
13381 if (loc->owner == nullptr)
13382 error (_("Breakpoint location does not have an owner breakpoint."));
13383
13384 if (loc->disabled_by_cond && enable)
13385 {
13386 int loc_num = find_loc_num_by_location (loc);
13387 if (loc_num == -1)
13388 error (_("Breakpoint location LOC_NUM could not be found."));
13389 else
13390 error (_("Breakpoint %d's condition is invalid at location %d, "
13391 "cannot enable."), loc->owner->number, loc_num);
13392 }
13393
13394 if (loc->enabled != enable)
13395 {
13396 loc->enabled = enable;
13397 mark_breakpoint_location_modified (loc);
13398 }
13399
13400 if (target_supports_enable_disable_tracepoint ()
13401 && current_trace_status ()->running && loc->owner
13402 && is_tracepoint (loc->owner))
13403 target_disable_tracepoint (loc);
13404
13405 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13406 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
13407}
13408
13409/* Enable or disable a range of breakpoint locations. BP_NUM is the
13410 number of the breakpoint, and BP_LOC_RANGE specifies the
13411 (inclusive) range of location numbers of that breakpoint to
13412 enable/disable. ENABLE specifies whether to enable or disable the
13413 location. */
13414
13415static void
13416enable_disable_breakpoint_location_range (int bp_num,
13417 std::pair<int, int> &bp_loc_range,
13418 bool enable)
13419{
13420 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13421 enable_disable_bp_num_loc (bp_num, i, enable);
13422}
0d381245 13423
1900040c
MS
13424/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13425 If from_tty is nonzero, it prints a message to that effect,
13426 which ends with a period (no newline). */
13427
c906108c 13428void
fba45db2 13429disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
13430{
13431 /* Never disable a watchpoint scope breakpoint; we want to
13432 hit them when we leave scope so we can delete both the
13433 watchpoint and its scope breakpoint at that time. */
13434 if (bpt->type == bp_watchpoint_scope)
13435 return;
13436
b5de0fa7 13437 bpt->enable_state = bp_disabled;
c906108c 13438
b775012e
LM
13439 /* Mark breakpoint locations modified. */
13440 mark_breakpoint_modified (bpt);
13441
d248b706
KY
13442 if (target_supports_enable_disable_tracepoint ()
13443 && current_trace_status ()->running && is_tracepoint (bpt))
13444 {
40cb8ca5 13445 for (bp_location *location : bpt->locations ())
d248b706
KY
13446 target_disable_tracepoint (location);
13447 }
13448
44702360 13449 update_global_location_list (UGLL_DONT_INSERT);
c906108c 13450
76727919 13451 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13452}
13453
d0fe4701
XR
13454/* Enable or disable the breakpoint(s) or breakpoint location(s)
13455 specified in ARGS. ARGS may be in any of the formats handled by
13456 extract_bp_number_and_location. ENABLE specifies whether to enable
13457 or disable the breakpoints/locations. */
13458
c906108c 13459static void
d0fe4701 13460enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 13461{
c906108c 13462 if (args == 0)
46c6471b 13463 {
43892fdf 13464 for (breakpoint *bpt : all_breakpoints ())
46c6471b 13465 if (user_breakpoint_p (bpt))
d0fe4701
XR
13466 {
13467 if (enable)
13468 enable_breakpoint (bpt);
13469 else
13470 disable_breakpoint (bpt);
13471 }
46c6471b 13472 }
9eaabc75 13473 else
0d381245 13474 {
cb791d59 13475 std::string num = extract_arg (&args);
9eaabc75 13476
cb791d59 13477 while (!num.empty ())
d248b706 13478 {
d0fe4701 13479 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 13480
cc638e86
PA
13481 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13482
13483 if (bp_loc_range.first == bp_loc_range.second
78805ff8
PW
13484 && (bp_loc_range.first == 0
13485 || (bp_loc_range.first == 1
13486 && bp_num_range.first == bp_num_range.second
13487 && !has_multiple_locations (bp_num_range.first))))
d0fe4701 13488 {
78805ff8
PW
13489 /* Handle breakpoint ids with formats 'x' or 'x-z'
13490 or 'y.1' where y has only one code location. */
cc638e86
PA
13491 map_breakpoint_number_range (bp_num_range,
13492 enable
13493 ? enable_breakpoint
13494 : disable_breakpoint);
13495 }
13496 else
13497 {
13498 /* Handle breakpoint ids with formats 'x.y' or
13499 'x.y-z'. */
13500 enable_disable_breakpoint_location_range
13501 (bp_num_range.first, bp_loc_range, enable);
b775012e 13502 }
9eaabc75 13503 num = extract_arg (&args);
d248b706 13504 }
0d381245 13505 }
c906108c
SS
13506}
13507
d0fe4701
XR
13508/* The disable command disables the specified breakpoints/locations
13509 (or all defined breakpoints) so they're no longer effective in
13510 stopping the inferior. ARGS may be in any of the forms defined in
13511 extract_bp_number_and_location. */
13512
13513static void
13514disable_command (const char *args, int from_tty)
13515{
13516 enable_disable_command (args, from_tty, false);
13517}
13518
c906108c 13519static void
816338b5
SS
13520enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13521 int count)
c906108c 13522{
afe38095 13523 int target_resources_ok;
c906108c
SS
13524
13525 if (bpt->type == bp_hardware_breakpoint)
13526 {
13527 int i;
c5aa993b 13528 i = hw_breakpoint_used_count ();
53a5351d 13529 target_resources_ok =
d92524f1 13530 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 13531 i + 1, 0);
c906108c 13532 if (target_resources_ok == 0)
8a3fe4f8 13533 error (_("No hardware breakpoint support in the target."));
c906108c 13534 else if (target_resources_ok < 0)
8a3fe4f8 13535 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
13536 }
13537
cc60f2e3 13538 if (is_watchpoint (bpt))
c906108c 13539 {
d07205c2 13540 /* Initialize it just to avoid a GCC false warning. */
f486487f 13541 enum enable_state orig_enable_state = bp_disabled;
dde02812 13542
a70b8144 13543 try
c906108c 13544 {
3a5c3e22
PA
13545 struct watchpoint *w = (struct watchpoint *) bpt;
13546
1e718ff1
TJB
13547 orig_enable_state = bpt->enable_state;
13548 bpt->enable_state = bp_enabled;
d8de7963 13549 update_watchpoint (w, true /* reparse */);
c906108c 13550 }
b1ffd112 13551 catch (const gdb_exception_error &e)
c5aa993b 13552 {
1e718ff1 13553 bpt->enable_state = orig_enable_state;
dde02812
ES
13554 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13555 bpt->number);
13556 return;
c5aa993b 13557 }
c906108c 13558 }
0101ce28 13559
b775012e
LM
13560 bpt->enable_state = bp_enabled;
13561
13562 /* Mark breakpoint locations modified. */
13563 mark_breakpoint_modified (bpt);
13564
d248b706
KY
13565 if (target_supports_enable_disable_tracepoint ()
13566 && current_trace_status ()->running && is_tracepoint (bpt))
13567 {
40cb8ca5 13568 for (bp_location *location : bpt->locations ())
d248b706
KY
13569 target_enable_tracepoint (location);
13570 }
13571
b4c291bb 13572 bpt->disposition = disposition;
816338b5 13573 bpt->enable_count = count;
44702360 13574 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 13575
76727919 13576 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13577}
13578
fe3f5fa8 13579
c906108c 13580void
fba45db2 13581enable_breakpoint (struct breakpoint *bpt)
c906108c 13582{
816338b5 13583 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
13584}
13585
d0fe4701
XR
13586/* The enable command enables the specified breakpoints/locations (or
13587 all defined breakpoints) so they once again become (or continue to
13588 be) effective in stopping the inferior. ARGS may be in any of the
13589 forms defined in extract_bp_number_and_location. */
c906108c 13590
c906108c 13591static void
981a3fb3 13592enable_command (const char *args, int from_tty)
c906108c 13593{
d0fe4701 13594 enable_disable_command (args, from_tty, true);
c906108c
SS
13595}
13596
c906108c 13597static void
4495129a 13598enable_once_command (const char *args, int from_tty)
c906108c 13599{
48649e1b
TT
13600 map_breakpoint_numbers
13601 (args, [&] (breakpoint *b)
13602 {
13603 iterate_over_related_breakpoints
13604 (b, [&] (breakpoint *bpt)
13605 {
13606 enable_breakpoint_disp (bpt, disp_disable, 1);
13607 });
13608 });
816338b5
SS
13609}
13610
13611static void
4495129a 13612enable_count_command (const char *args, int from_tty)
816338b5 13613{
b9d61307
SM
13614 int count;
13615
13616 if (args == NULL)
13617 error_no_arg (_("hit count"));
13618
13619 count = get_number (&args);
816338b5 13620
48649e1b
TT
13621 map_breakpoint_numbers
13622 (args, [&] (breakpoint *b)
13623 {
13624 iterate_over_related_breakpoints
13625 (b, [&] (breakpoint *bpt)
13626 {
13627 enable_breakpoint_disp (bpt, disp_disable, count);
13628 });
13629 });
c906108c
SS
13630}
13631
c906108c 13632static void
4495129a 13633enable_delete_command (const char *args, int from_tty)
c906108c 13634{
48649e1b
TT
13635 map_breakpoint_numbers
13636 (args, [&] (breakpoint *b)
13637 {
13638 iterate_over_related_breakpoints
13639 (b, [&] (breakpoint *bpt)
13640 {
13641 enable_breakpoint_disp (bpt, disp_del, 1);
13642 });
13643 });
c906108c
SS
13644}
13645\f
1f3b5d1b
PP
13646/* Invalidate last known value of any hardware watchpoint if
13647 the memory which that value represents has been written to by
13648 GDB itself. */
13649
13650static void
8de0566d
YQ
13651invalidate_bp_value_on_memory_change (struct inferior *inferior,
13652 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
13653 const bfd_byte *data)
13654{
43892fdf 13655 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 13656 if (bp->enable_state == bp_enabled
3a5c3e22 13657 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 13658 {
3a5c3e22 13659 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 13660
850645cf 13661 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 13662 {
40cb8ca5 13663 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
13664 if (loc->loc_type == bp_loc_hardware_watchpoint
13665 && loc->address + loc->length > addr
13666 && addr + len > loc->address)
13667 {
3a5c3e22 13668 wp->val = NULL;
4c1d86d9 13669 wp->val_valid = false;
3a5c3e22
PA
13670 }
13671 }
1f3b5d1b
PP
13672 }
13673}
13674
8181d85f
DJ
13675/* Create and insert a breakpoint for software single step. */
13676
13677void
6c95b8df 13678insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 13679 const address_space *aspace,
4a64f543 13680 CORE_ADDR next_pc)
8181d85f 13681{
7c16b83e
PA
13682 struct thread_info *tp = inferior_thread ();
13683 struct symtab_and_line sal;
13684 CORE_ADDR pc = next_pc;
8181d85f 13685
34b7e8a6
PA
13686 if (tp->control.single_step_breakpoints == NULL)
13687 {
960bc2bd 13688 std::unique_ptr<breakpoint> b
7ab97995
PA
13689 (new momentary_breakpoint (gdbarch, bp_single_step,
13690 current_program_space,
13691 null_frame_id,
13692 tp->global_num));
960bc2bd 13693
34b7e8a6 13694 tp->control.single_step_breakpoints
960bc2bd 13695 = add_to_breakpoint_chain (std::move (b));
34b7e8a6 13696 }
8181d85f 13697
7c16b83e
PA
13698 sal = find_pc_line (pc, 0);
13699 sal.pc = pc;
13700 sal.section = find_pc_overlay (pc);
13701 sal.explicit_pc = 1;
960bc2bd
PA
13702
13703 auto *ss_bp
98ed24fb
TT
13704 = (gdb::checked_static_cast<momentary_breakpoint *>
13705 (tp->control.single_step_breakpoints));
960bc2bd 13706 ss_bp->add_location (sal);
8181d85f 13707
7c16b83e 13708 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
13709}
13710
93f9a11f
YQ
13711/* Insert single step breakpoints according to the current state. */
13712
13713int
13714insert_single_step_breakpoints (struct gdbarch *gdbarch)
13715{
f5ea389a 13716 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 13717 std::vector<CORE_ADDR> next_pcs;
93f9a11f 13718
f5ea389a 13719 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 13720
a0ff9e1a 13721 if (!next_pcs.empty ())
93f9a11f 13722 {
bd2b40ac 13723 frame_info_ptr frame = get_current_frame ();
8b86c959 13724 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 13725
a0ff9e1a 13726 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
13727 insert_single_step_breakpoint (gdbarch, aspace, pc);
13728
93f9a11f
YQ
13729 return 1;
13730 }
13731 else
13732 return 0;
13733}
13734
34b7e8a6 13735/* See breakpoint.h. */
f02253f1
HZ
13736
13737int
7c16b83e 13738breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 13739 const address_space *aspace,
7c16b83e 13740 CORE_ADDR pc)
1aafd4da 13741{
40cb8ca5 13742 for (bp_location *loc : bp->locations ())
7c16b83e
PA
13743 if (loc->inserted
13744 && breakpoint_location_address_match (loc, aspace, pc))
13745 return 1;
1aafd4da 13746
7c16b83e 13747 return 0;
ef370185
JB
13748}
13749
13750/* Check whether a software single-step breakpoint is inserted at
13751 PC. */
13752
13753int
accd0bcd 13754single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
13755 CORE_ADDR pc)
13756{
43892fdf 13757 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
13758 {
13759 if (bpt->type == bp_single_step
13760 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13761 return 1;
13762 }
13763 return 0;
1aafd4da
UW
13764}
13765
1042e4c0
SS
13766/* Tracepoint-specific operations. */
13767
13768/* Set tracepoint count to NUM. */
13769static void
13770set_tracepoint_count (int num)
13771{
13772 tracepoint_count = num;
4fa62494 13773 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
13774}
13775
70221824 13776static void
0b39b52e 13777trace_command (const char *arg, int from_tty)
1042e4c0 13778{
264f9890
PA
13779 location_spec_up locspec = string_to_location_spec (&arg,
13780 current_language);
13781 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13782 (locspec.get (), true /* is_tracepoint */);
55aa24fb 13783
558a9d82 13784 create_breakpoint (get_current_arch (),
264f9890 13785 locspec.get (),
10a636cc 13786 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13787 0 /* tempflag */,
13788 bp_tracepoint /* type_wanted */,
13789 0 /* Ignore count */,
13790 pending_break_support,
13791 ops,
13792 from_tty,
13793 1 /* enabled */,
13794 0 /* internal */, 0);
1042e4c0
SS
13795}
13796
70221824 13797static void
0b39b52e 13798ftrace_command (const char *arg, int from_tty)
7a697b8d 13799{
264f9890
PA
13800 location_spec_up locspec = string_to_location_spec (&arg,
13801 current_language);
558a9d82 13802 create_breakpoint (get_current_arch (),
264f9890 13803 locspec.get (),
10a636cc 13804 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13805 0 /* tempflag */,
13806 bp_fast_tracepoint /* type_wanted */,
13807 0 /* Ignore count */,
13808 pending_break_support,
74421c0b 13809 &code_breakpoint_ops,
558a9d82
YQ
13810 from_tty,
13811 1 /* enabled */,
13812 0 /* internal */, 0);
0fb4aa4b
PA
13813}
13814
13815/* strace command implementation. Creates a static tracepoint. */
13816
70221824 13817static void
0b39b52e 13818strace_command (const char *arg, int from_tty)
0fb4aa4b 13819{
a678887d 13820 const struct breakpoint_ops *ops;
264f9890 13821 location_spec_up locspec;
7b572efb 13822 enum bptype type;
983af33b
SDJ
13823
13824 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13825 or with a normal static tracepoint. */
61012eef 13826 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
13827 {
13828 ops = &strace_marker_breakpoint_ops;
264f9890
PA
13829 locspec = new_linespec_location_spec (&arg,
13830 symbol_name_match_type::FULL);
7b572efb 13831 type = bp_static_marker_tracepoint;
f00aae0f 13832 }
983af33b 13833 else
f00aae0f 13834 {
74421c0b 13835 ops = &code_breakpoint_ops;
264f9890 13836 locspec = string_to_location_spec (&arg, current_language);
7b572efb 13837 type = bp_static_tracepoint;
f00aae0f 13838 }
983af33b 13839
558a9d82 13840 create_breakpoint (get_current_arch (),
264f9890 13841 locspec.get (),
10a636cc 13842 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82 13843 0 /* tempflag */,
7b572efb 13844 type /* type_wanted */,
558a9d82
YQ
13845 0 /* Ignore count */,
13846 pending_break_support,
13847 ops,
13848 from_tty,
13849 1 /* enabled */,
13850 0 /* internal */, 0);
7a697b8d
SS
13851}
13852
409873ef
SS
13853/* Set up a fake reader function that gets command lines from a linked
13854 list that was acquired during tracepoint uploading. */
13855
13856static struct uploaded_tp *this_utp;
3149d8c1 13857static int next_cmd;
409873ef 13858
f8631e5e
SM
13859static const char *
13860read_uploaded_action (std::string &buffer)
409873ef 13861{
a18ba4e4 13862 char *rslt = nullptr;
409873ef 13863
a18ba4e4
SM
13864 if (next_cmd < this_utp->cmd_strings.size ())
13865 {
67aa1f3c 13866 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
13867 next_cmd++;
13868 }
409873ef
SS
13869
13870 return rslt;
13871}
13872
00bf0b85
SS
13873/* Given information about a tracepoint as recorded on a target (which
13874 can be either a live system or a trace file), attempt to create an
13875 equivalent GDB tracepoint. This is not a reliable process, since
13876 the target does not necessarily have all the information used when
13877 the tracepoint was originally defined. */
13878
d9b3f62e 13879struct tracepoint *
00bf0b85 13880create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 13881{
f2fc3015
TT
13882 const char *addr_str;
13883 char small_buf[100];
d9b3f62e 13884 struct tracepoint *tp;
fd9b8c24 13885
409873ef 13886 if (utp->at_string)
67aa1f3c 13887 addr_str = utp->at_string.get ();
409873ef
SS
13888 else
13889 {
13890 /* In the absence of a source location, fall back to raw
13891 address. Since there is no way to confirm that the address
13892 means the same thing as when the trace was started, warn the
13893 user. */
3e43a32a
MS
13894 warning (_("Uploaded tracepoint %d has no "
13895 "source location, using raw address"),
409873ef 13896 utp->number);
8c042590 13897 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
13898 addr_str = small_buf;
13899 }
13900
13901 /* There's not much we can do with a sequence of bytecodes. */
13902 if (utp->cond && !utp->cond_string)
3e43a32a
MS
13903 warning (_("Uploaded tracepoint %d condition "
13904 "has no source form, ignoring it"),
409873ef 13905 utp->number);
d5551862 13906
264f9890
PA
13907 location_spec_up locspec = string_to_location_spec (&addr_str,
13908 current_language);
8cdf0e15 13909 if (!create_breakpoint (get_current_arch (),
264f9890 13910 locspec.get (),
67aa1f3c 13911 utp->cond_string.get (), -1, addr_str,
10a636cc 13912 false /* force_condition */,
e7e0cddf 13913 0 /* parse cond/thread */,
8cdf0e15 13914 0 /* tempflag */,
0fb4aa4b 13915 utp->type /* type_wanted */,
8cdf0e15
VP
13916 0 /* Ignore count */,
13917 pending_break_support,
74421c0b 13918 &code_breakpoint_ops,
8cdf0e15 13919 0 /* from_tty */,
84f4c1fe 13920 utp->enabled /* enabled */,
44f238bb
PA
13921 0 /* internal */,
13922 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 13923 return NULL;
fd9b8c24 13924
409873ef 13925 /* Get the tracepoint we just created. */
fd9b8c24
PA
13926 tp = get_tracepoint (tracepoint_count);
13927 gdb_assert (tp != NULL);
d5551862 13928
00bf0b85
SS
13929 if (utp->pass > 0)
13930 {
8c042590 13931 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 13932 tp->number);
00bf0b85 13933
409873ef 13934 trace_pass_command (small_buf, 0);
00bf0b85
SS
13935 }
13936
409873ef
SS
13937 /* If we have uploaded versions of the original commands, set up a
13938 special-purpose "reader" function and call the usual command line
13939 reader, then pass the result to the breakpoint command-setting
13940 function. */
a18ba4e4 13941 if (!utp->cmd_strings.empty ())
00bf0b85 13942 {
12973681 13943 counted_command_line cmd_list;
00bf0b85 13944
409873ef 13945 this_utp = utp;
3149d8c1 13946 next_cmd = 0;
d5551862 13947
60b3cef2 13948 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 13949
c1fc2657 13950 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 13951 }
a18ba4e4
SM
13952 else if (!utp->actions.empty ()
13953 || !utp->step_actions.empty ())
3e43a32a
MS
13954 warning (_("Uploaded tracepoint %d actions "
13955 "have no source form, ignoring them"),
409873ef 13956 utp->number);
00bf0b85 13957
f196051f 13958 /* Copy any status information that might be available. */
c1fc2657 13959 tp->hit_count = utp->hit_count;
f196051f
SS
13960 tp->traceframe_usage = utp->traceframe_usage;
13961
00bf0b85 13962 return tp;
d9b3f62e 13963}
00bf0b85 13964
1042e4c0
SS
13965/* Print information on tracepoint number TPNUM_EXP, or all if
13966 omitted. */
13967
13968static void
1d12d88f 13969info_tracepoints_command (const char *args, int from_tty)
1042e4c0 13970{
79a45e25 13971 struct ui_out *uiout = current_uiout;
e5a67952 13972 int num_printed;
1042e4c0 13973
5c458ae8 13974 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
13975
13976 if (num_printed == 0)
1042e4c0 13977 {
e5a67952 13978 if (args == NULL || *args == '\0')
112e8700 13979 uiout->message ("No tracepoints.\n");
d77f58be 13980 else
112e8700 13981 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 13982 }
ad443146
SS
13983
13984 default_collect_info ();
1042e4c0
SS
13985}
13986
4a64f543 13987/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13988 Not supported by all targets. */
13989static void
5fed81ff 13990enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13991{
13992 enable_command (args, from_tty);
13993}
13994
4a64f543 13995/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13996 Not supported by all targets. */
13997static void
5fed81ff 13998disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13999{
14000 disable_command (args, from_tty);
14001}
14002
4a64f543 14003/* Remove a tracepoint (or all if no argument). */
1042e4c0 14004static void
4495129a 14005delete_trace_command (const char *arg, int from_tty)
1042e4c0 14006{
1042e4c0
SS
14007 dont_repeat ();
14008
14009 if (arg == 0)
14010 {
14011 int breaks_to_delete = 0;
14012
14013 /* Delete all breakpoints if no argument.
dda83cd7
SM
14014 Do not delete internal or call-dummy breakpoints, these
14015 have to be deleted with an explicit breakpoint number
4a64f543 14016 argument. */
f6d17b2b 14017 for (breakpoint *tp : all_tracepoints ())
1428b37a 14018 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
14019 {
14020 breaks_to_delete = 1;
14021 break;
14022 }
1042e4c0
SS
14023
14024 /* Ask user only if there are some breakpoints to delete. */
14025 if (!from_tty
14026 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14027 {
1428b37a 14028 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 14029 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14030 delete_breakpoint (b);
1042e4c0
SS
14031 }
14032 }
14033 else
48649e1b 14034 map_breakpoint_numbers
b926417a 14035 (arg, [&] (breakpoint *br)
48649e1b 14036 {
b926417a 14037 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14038 });
1042e4c0
SS
14039}
14040
197f0a60
TT
14041/* Helper function for trace_pass_command. */
14042
14043static void
d9b3f62e 14044trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14045{
d9b3f62e 14046 tp->pass_count = count;
76727919 14047 gdb::observers::breakpoint_modified.notify (tp);
197f0a60 14048 if (from_tty)
6cb06a8c
TT
14049 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14050 tp->number, count);
197f0a60
TT
14051}
14052
1042e4c0
SS
14053/* Set passcount for tracepoint.
14054
14055 First command argument is passcount, second is tracepoint number.
14056 If tracepoint number omitted, apply to most recently defined.
14057 Also accepts special argument "all". */
14058
14059static void
0b39b52e 14060trace_pass_command (const char *args, int from_tty)
1042e4c0 14061{
d9b3f62e 14062 struct tracepoint *t1;
0b39b52e 14063 ULONGEST count;
1042e4c0
SS
14064
14065 if (args == 0 || *args == 0)
3e43a32a
MS
14066 error (_("passcount command requires an "
14067 "argument (count + optional TP num)"));
1042e4c0 14068
0b39b52e 14069 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14070
529480d0 14071 args = skip_spaces (args);
1042e4c0
SS
14072 if (*args && strncasecmp (args, "all", 3) == 0)
14073 {
14074 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14075 if (*args)
14076 error (_("Junk at end of arguments."));
1042e4c0 14077
f6d17b2b 14078 for (breakpoint *b : all_tracepoints ())
01add95b
SM
14079 {
14080 t1 = (struct tracepoint *) b;
14081 trace_pass_set_count (t1, count, from_tty);
14082 }
197f0a60
TT
14083 }
14084 else if (*args == '\0')
1042e4c0 14085 {
5fa1d40e 14086 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14087 if (t1)
197f0a60
TT
14088 trace_pass_set_count (t1, count, from_tty);
14089 }
14090 else
14091 {
bfd28288
PA
14092 number_or_range_parser parser (args);
14093 while (!parser.finished ())
1042e4c0 14094 {
bfd28288 14095 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14096 if (t1)
14097 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14098 }
14099 }
1042e4c0
SS
14100}
14101
d9b3f62e 14102struct tracepoint *
1042e4c0
SS
14103get_tracepoint (int num)
14104{
f6d17b2b 14105 for (breakpoint *t : all_tracepoints ())
1042e4c0 14106 if (t->number == num)
d9b3f62e 14107 return (struct tracepoint *) t;
1042e4c0
SS
14108
14109 return NULL;
14110}
14111
d5551862
SS
14112/* Find the tracepoint with the given target-side number (which may be
14113 different from the tracepoint number after disconnecting and
14114 reconnecting). */
14115
d9b3f62e 14116struct tracepoint *
d5551862
SS
14117get_tracepoint_by_number_on_target (int num)
14118{
f6d17b2b 14119 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
14120 {
14121 struct tracepoint *t = (struct tracepoint *) b;
14122
14123 if (t->number_on_target == num)
14124 return t;
14125 }
d5551862
SS
14126
14127 return NULL;
14128}
14129
1042e4c0 14130/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14131 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14132 If the argument is missing, the most recent tracepoint
14133 (tracepoint_count) is returned. */
14134
d9b3f62e 14135struct tracepoint *
0b39b52e 14136get_tracepoint_by_number (const char **arg,
bfd28288 14137 number_or_range_parser *parser)
1042e4c0 14138{
1042e4c0 14139 int tpnum;
0b39b52e 14140 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14141
bfd28288 14142 if (parser != NULL)
197f0a60 14143 {
bfd28288
PA
14144 gdb_assert (!parser->finished ());
14145 tpnum = parser->get_number ();
197f0a60
TT
14146 }
14147 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14148 tpnum = tracepoint_count;
1042e4c0 14149 else
197f0a60 14150 tpnum = get_number (arg);
1042e4c0
SS
14151
14152 if (tpnum <= 0)
14153 {
14154 if (instring && *instring)
6cb06a8c
TT
14155 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14156 instring);
1042e4c0 14157 else
6cb06a8c 14158 gdb_printf (_("No previous tracepoint\n"));
1042e4c0
SS
14159 return NULL;
14160 }
14161
f6d17b2b 14162 for (breakpoint *t : all_tracepoints ())
1042e4c0 14163 if (t->number == tpnum)
d9b3f62e 14164 return (struct tracepoint *) t;
1042e4c0 14165
6cb06a8c 14166 gdb_printf ("No tracepoint number %d.\n", tpnum);
1042e4c0
SS
14167 return NULL;
14168}
14169
d9b3f62e 14170void
04d0163c 14171breakpoint::print_recreate_thread (struct ui_file *fp) const
d9b3f62e 14172{
04d0163c 14173 if (thread != -1)
442716d4
AB
14174 {
14175 struct thread_info *thr = find_thread_global_id (thread);
14176 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14177 }
d9b3f62e 14178
2ecee236 14179 if (task != -1)
04d0163c 14180 gdb_printf (fp, " task %d", task);
d9b3f62e 14181
6cb06a8c 14182 gdb_printf (fp, "\n");
d9b3f62e
PA
14183}
14184
6149aea9
PA
14185/* Save information on user settable breakpoints (watchpoints, etc) to
14186 a new script file named FILENAME. If FILTER is non-NULL, call it
14187 on each breakpoint and only include the ones for which it returns
f2478a7e 14188 true. */
6149aea9 14189
1042e4c0 14190static void
4495129a 14191save_breakpoints (const char *filename, int from_tty,
f2478a7e 14192 bool (*filter) (const struct breakpoint *))
1042e4c0 14193{
d8de7963 14194 bool any = false;
6149aea9 14195 int extra_trace_bits = 0;
1042e4c0 14196
6149aea9
PA
14197 if (filename == 0 || *filename == 0)
14198 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14199
14200 /* See if we have anything to save. */
43892fdf 14201 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14202 {
14203 /* Skip internal and momentary breakpoints. */
14204 if (!user_breakpoint_p (tp))
14205 continue;
6149aea9 14206
01add95b
SM
14207 /* If we have a filter, only save the breakpoints it accepts. */
14208 if (filter && !filter (tp))
14209 continue;
6149aea9 14210
d8de7963 14211 any = true;
6149aea9 14212
01add95b
SM
14213 if (is_tracepoint (tp))
14214 {
14215 extra_trace_bits = 1;
6149aea9 14216
01add95b
SM
14217 /* We can stop searching. */
14218 break;
14219 }
14220 }
6149aea9
PA
14221
14222 if (!any)
1042e4c0 14223 {
6149aea9 14224 warning (_("Nothing to save."));
1042e4c0
SS
14225 return;
14226 }
14227
ee0c3293 14228 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14229
14230 stdio_file fp;
14231
ee0c3293 14232 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14233 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14234 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14235
6149aea9 14236 if (extra_trace_bits)
d7e74731 14237 save_trace_state_variables (&fp);
8bf6485c 14238
43892fdf 14239 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14240 {
14241 /* Skip internal and momentary breakpoints. */
14242 if (!user_breakpoint_p (tp))
14243 continue;
8bf6485c 14244
01add95b
SM
14245 /* If we have a filter, only save the breakpoints it accepts. */
14246 if (filter && !filter (tp))
14247 continue;
6149aea9 14248
c47614fe 14249 tp->print_recreate (&fp);
1042e4c0 14250
01add95b
SM
14251 /* Note, we can't rely on tp->number for anything, as we can't
14252 assume the recreated breakpoint numbers will match. Use $bpnum
14253 instead. */
6149aea9 14254
01add95b 14255 if (tp->cond_string)
6f781ee3 14256 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
6149aea9 14257
01add95b
SM
14258 if (tp->ignore_count)
14259 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14260
01add95b
SM
14261 if (tp->type != bp_dprintf && tp->commands)
14262 {
14263 fp.puts (" commands\n");
1042e4c0 14264
992aeed8
TT
14265 ui_out_redirect_pop redir (current_uiout, &fp);
14266 print_command_lines (current_uiout, tp->commands.get (), 2);
6149aea9 14267
01add95b
SM
14268 fp.puts (" end\n");
14269 }
6149aea9 14270
01add95b
SM
14271 if (tp->enable_state == bp_disabled)
14272 fp.puts ("disable $bpnum\n");
6149aea9 14273
01add95b
SM
14274 /* If this is a multi-location breakpoint, check if the locations
14275 should be individually disabled. Watchpoint locations are
14276 special, and not user visible. */
14277 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14278 {
14279 int n = 1;
40cb8ca5 14280
01add95b
SM
14281 for (bp_location *loc : tp->locations ())
14282 {
14283 if (!loc->enabled)
14284 fp.printf ("disable $bpnum.%d\n", n);
14285
14286 n++;
14287 }
14288 }
14289 }
8bf6485c 14290
e0700ba4
SM
14291 if (extra_trace_bits && !default_collect.empty ())
14292 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14293
1042e4c0 14294 if (from_tty)
6cb06a8c 14295 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14296}
14297
14298/* The `save breakpoints' command. */
14299
14300static void
4495129a 14301save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14302{
14303 save_breakpoints (args, from_tty, NULL);
14304}
14305
14306/* The `save tracepoints' command. */
14307
14308static void
4495129a 14309save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14310{
14311 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14312}
14313
c906108c 14314\f
629500fa
KS
14315/* This help string is used to consolidate all the help string for specifying
14316 locations used by several commands. */
14317
264f9890 14318#define LOCATION_SPEC_HELP_STRING \
629500fa
KS
14319"Linespecs are colon-separated lists of location parameters, such as\n\
14320source filename, function name, label name, and line number.\n\
14321Example: To specify the start of a label named \"the_top\" in the\n\
14322function \"fact\" in the file \"factorial.c\", use\n\
14323\"factorial.c:fact:the_top\".\n\
14324\n\
14325Address locations begin with \"*\" and specify an exact address in the\n\
14326program. Example: To specify the fourth byte past the start function\n\
14327\"main\", use \"*main + 4\".\n\
14328\n\
14329Explicit locations are similar to linespecs but use an option/argument\n\
14330syntax to specify location parameters.\n\
14331Example: To specify the start of the label named \"the_top\" in the\n\
14332function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14333-function fact -label the_top\".\n\
14334\n\
14335By default, a specified function is matched against the program's\n\
14336functions in all scopes. For C++, this means in all namespaces and\n\
14337classes. For Ada, this means in all packages. E.g., in C++,\n\
14338\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14339\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14340specified name as a complete fully-qualified name instead."
629500fa 14341
4a64f543
MS
14342/* This help string is used for the break, hbreak, tbreak and thbreak
14343 commands. It is defined as a macro to prevent duplication.
14344 COMMAND should be a string constant containing the name of the
14345 command. */
629500fa 14346
31e2b00f 14347#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14348command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14349\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14350PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14351probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
14352guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14353`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14354LOCATION may be a linespec, address, or explicit location as described\n\
14355below.\n\
14356\n\
dc10affe
PA
14357With no LOCATION, uses current execution address of the selected\n\
14358stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14359\n\
14360THREADNUM is the number from \"info threads\".\n\
14361CONDITION is a boolean expression.\n\
733d554a
TBA
14362\n\
14363With the \"-force-condition\" flag, the condition is defined even when\n\
14364it is invalid for all current locations.\n\
264f9890 14365\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14366Multiple breakpoints at one place are permitted, and useful if their\n\
14367conditions are different.\n\
31e2b00f
AS
14368\n\
14369Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14370
44feb3ce
TT
14371/* List of subcommands for "catch". */
14372static struct cmd_list_element *catch_cmdlist;
14373
14374/* List of subcommands for "tcatch". */
14375static struct cmd_list_element *tcatch_cmdlist;
14376
9ac4176b 14377void
a121b7c1 14378add_catch_command (const char *name, const char *docstring,
5538b03c 14379 cmd_func_ftype *func,
625e8578 14380 completer_ftype *completer,
44feb3ce
TT
14381 void *user_data_catch,
14382 void *user_data_tcatch)
14383{
14384 struct cmd_list_element *command;
14385
0450cc4c 14386 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14387 &catch_cmdlist);
5538b03c 14388 command->func = func;
0f8e2034 14389 command->set_context (user_data_catch);
a96d9b2e 14390 set_cmd_completer (command, completer);
44feb3ce 14391
0450cc4c 14392 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14393 &tcatch_cmdlist);
5538b03c 14394 command->func = func;
0f8e2034 14395 command->set_context (user_data_tcatch);
a96d9b2e 14396 set_cmd_completer (command, completer);
44feb3ce
TT
14397}
14398
d8de7963
AB
14399/* False if any of the breakpoint's locations could be a location where
14400 functions have been inlined, true otherwise. */
0574c78f 14401
d8de7963 14402static bool
0574c78f
GB
14403is_non_inline_function (struct breakpoint *b)
14404{
14405 /* The shared library event breakpoint is set on the address of a
14406 non-inline function. */
d8de7963 14407 return (b->type == bp_shlib_event);
0574c78f
GB
14408}
14409
14410/* Nonzero if the specified PC cannot be a location where functions
14411 have been inlined. */
14412
14413int
accd0bcd 14414pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14415 const target_waitstatus &ws)
0574c78f 14416{
43892fdf 14417 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
14418 {
14419 if (!is_non_inline_function (b))
14420 continue;
14421
40cb8ca5 14422 for (bp_location *bl : b->locations ())
0574c78f
GB
14423 {
14424 if (!bl->shlib_disabled
09ac7c10 14425 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14426 return 1;
14427 }
14428 }
14429
14430 return 0;
14431}
14432
2f202fde
JK
14433/* Remove any references to OBJFILE which is going to be freed. */
14434
14435void
14436breakpoint_free_objfile (struct objfile *objfile)
14437{
48d7020b 14438 for (bp_location *loc : all_bp_locations ())
3c86fae3 14439 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
2f202fde
JK
14440 loc->symtab = NULL;
14441}
14442
8bfd80db
YQ
14443/* Chain containing all defined "enable breakpoint" subcommands. */
14444
14445static struct cmd_list_element *enablebreaklist = NULL;
14446
8588b356
SM
14447/* See breakpoint.h. */
14448
14449cmd_list_element *commands_cmd_element = nullptr;
14450
6c265988 14451void _initialize_breakpoint ();
c906108c 14452void
6c265988 14453_initialize_breakpoint ()
c906108c
SS
14454{
14455 struct cmd_list_element *c;
14456
c90e7d63
SM
14457 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14458 "breakpoint");
14459 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14460 "breakpoint");
14461 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14462 "breakpoint");
84acb35a 14463
c906108c
SS
14464 breakpoint_chain = 0;
14465 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14466 before a breakpoint is set. */
14467 breakpoint_count = 0;
14468
1042e4c0
SS
14469 tracepoint_count = 0;
14470
1bedd215
AC
14471 add_com ("ignore", class_breakpoint, ignore_command, _("\
14472Set ignore-count of breakpoint number N to COUNT.\n\
14473Usage is `ignore N COUNT'."));
c906108c 14474
8588b356
SM
14475 commands_cmd_element = add_com ("commands", class_breakpoint,
14476 commands_command, _("\
18da0c51
MG
14477Set commands to be executed when the given breakpoints are hit.\n\
14478Give a space-separated breakpoint list as argument after \"commands\".\n\
14479A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14480(e.g. `5-7').\n\
c906108c
SS
14481With no argument, the targeted breakpoint is the last one set.\n\
14482The commands themselves follow starting on the next line.\n\
14483Type a line containing \"end\" to indicate the end of them.\n\
14484Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14485then no output is printed when it is hit, except what the commands print."));
c906108c 14486
b1d4d8d1
TBA
14487 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14488 static std::string condition_command_help
14489 = gdb::option::build_help (_("\
1bedd215 14490Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14491Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14492is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14493\n\
14494Options:\n\
14495%OPTIONS%"), cc_opts);
14496
14497 c = add_com ("condition", class_breakpoint, condition_command,
14498 condition_command_help.c_str ());
14499 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14500
1bedd215 14501 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14502Set a temporary breakpoint.\n\
c906108c
SS
14503Like \"break\" except the breakpoint is only temporary,\n\
14504so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14505by using \"enable delete\" on the breakpoint number.\n\
14506\n"
14507BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14508 set_cmd_completer (c, location_completer);
c94fdfd0 14509
1bedd215 14510 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14511Set a hardware assisted breakpoint.\n\
c906108c 14512Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14513some target hardware may not have this support.\n\
14514\n"
14515BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14516 set_cmd_completer (c, location_completer);
c906108c 14517
1bedd215 14518 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14519Set a temporary hardware assisted breakpoint.\n\
c906108c 14520Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14521so it will be deleted when hit.\n\
14522\n"
14523BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14524 set_cmd_completer (c, location_completer);
c906108c 14525
3947f654
SM
14526 cmd_list_element *enable_cmd
14527 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14528Enable all or some breakpoints.\n\
14529Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14530Give breakpoint numbers (separated by spaces) as arguments.\n\
14531With no subcommand, breakpoints are enabled until you command otherwise.\n\
14532This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14533With a subcommand you can enable temporarily."),
3947f654 14534 &enablelist, 1, &cmdlist);
c906108c 14535
3947f654 14536 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14537
84951ab5 14538 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14539Enable all or some breakpoints.\n\
14540Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14541Give breakpoint numbers (separated by spaces) as arguments.\n\
14542This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14543May be abbreviated to simply \"enable\"."),
2f822da5 14544 &enablebreaklist, 1, &enablelist);
c906108c 14545
1a966eab 14546 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14547Enable some breakpoints for one hit.\n\
14548Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14549If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14550 &enablebreaklist);
14551
1a966eab 14552 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14553Enable some breakpoints and delete when hit.\n\
14554Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14555If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14556 &enablebreaklist);
14557
816338b5 14558 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14559Enable some breakpoints for COUNT hits.\n\
14560Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14561If a breakpoint is hit while enabled in this fashion,\n\
14562the count is decremented; when it reaches zero, the breakpoint is disabled."),
14563 &enablebreaklist);
14564
1a966eab 14565 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14566Enable some breakpoints and delete when hit.\n\
14567Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14568If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14569 &enablelist);
14570
1a966eab 14571 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14572Enable some breakpoints for one hit.\n\
14573Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14574If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14575 &enablelist);
14576
14577 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14578Enable some breakpoints for COUNT hits.\n\
14579Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14580If a breakpoint is hit while enabled in this fashion,\n\
14581the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14582 &enablelist);
14583
3947f654
SM
14584 cmd_list_element *disable_cmd
14585 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14586Disable all or some breakpoints.\n\
14587Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14588Arguments are breakpoint numbers with spaces in between.\n\
14589To disable all breakpoints, give no argument.\n\
64b9b334 14590A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14591 &disablelist, 1, &cmdlist);
14592 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14593 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14594
57b4f16e 14595 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14596Disable all or some breakpoints.\n\
14597Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14598Arguments are breakpoint numbers with spaces in between.\n\
14599To disable all breakpoints, give no argument.\n\
64b9b334 14600A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14601This command may be abbreviated \"disable\"."),
c906108c
SS
14602 &disablelist);
14603
3947f654
SM
14604 cmd_list_element *delete_cmd
14605 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14606Delete all or some breakpoints.\n\
14607Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
14608Arguments are breakpoint numbers with spaces in between.\n\
14609To delete all breakpoints, give no argument.\n\
14610\n\
590042fc 14611Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14612 &deletelist, 1, &cmdlist);
14613 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14614 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14615
57b4f16e 14616 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14617Delete all or some breakpoints or auto-display expressions.\n\
14618Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14619Arguments are breakpoint numbers with spaces in between.\n\
14620To delete all breakpoints, give no argument.\n\
1a966eab 14621This command may be abbreviated \"delete\"."),
c906108c
SS
14622 &deletelist);
14623
3947f654
SM
14624 cmd_list_element *clear_cmd
14625 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14626Clear breakpoint at specified location.\n\
14627Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14628\n\
14629With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14630is executing in.\n"
264f9890 14631"\n" LOCATION_SPEC_HELP_STRING "\n\n\
1bedd215 14632See also the \"delete\" command which clears breakpoints by number."));
3947f654 14633 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14634
3947f654
SM
14635 cmd_list_element *break_cmd
14636 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14637Set breakpoint at specified location.\n"
31e2b00f 14638BREAK_ARGS_HELP ("break")));
3947f654 14639 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14640
3947f654
SM
14641 add_com_alias ("b", break_cmd, class_run, 1);
14642 add_com_alias ("br", break_cmd, class_run, 1);
14643 add_com_alias ("bre", break_cmd, class_run, 1);
14644 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14645
e0f25bd9
SM
14646 cmd_list_element *info_breakpoints_cmd
14647 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14648Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14649The \"Type\" column indicates one of:\n\
14650\tbreakpoint - normal breakpoint\n\
14651\twatchpoint - watchpoint\n\
14652The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14653the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14654breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14655address and file/line number respectively.\n\
14656\n\
14657Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14658are set to the address of the last breakpoint listed unless the command\n\
14659is prefixed with \"server \".\n\n\
c906108c 14660Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14661breakpoint set."));
c906108c 14662
e0f25bd9 14663 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14664
1a966eab
AC
14665 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14666Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14667The \"Type\" column indicates one of:\n\
14668\tbreakpoint - normal breakpoint\n\
14669\twatchpoint - watchpoint\n\
14670\tlongjmp - internal breakpoint used to step through longjmp()\n\
14671\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14672\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14673\tfinish - internal breakpoint used by the \"finish\" command\n\
14674The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14675the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14676breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14677address and file/line number respectively.\n\
14678\n\
14679Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14680are set to the address of the last breakpoint listed unless the command\n\
14681is prefixed with \"server \".\n\n\
c906108c 14682Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14683breakpoint set."),
c906108c
SS
14684 &maintenanceinfolist);
14685
3b6acaee 14686 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 14687Set catchpoints to catch events."),
2f822da5 14688 &catch_cmdlist,
3b6acaee 14689 0/*allow-unknown*/, &cmdlist);
44feb3ce 14690
3b6acaee 14691 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 14692Set temporary catchpoints to catch events."),
2f822da5 14693 &tcatch_cmdlist,
3b6acaee 14694 0/*allow-unknown*/, &cmdlist);
44feb3ce 14695
a15a5258
AB
14696 const auto opts = make_watch_options_def_group (nullptr);
14697
14698 static const std::string watch_help = gdb::option::build_help (_("\
14699Set a watchpoint for EXPRESSION.\n\
14700Usage: watch [-location] EXPRESSION\n\
14701\n\
14702Options:\n\
14703%OPTIONS%\n\
14704\n\
c906108c 14705A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
14706an expression changes."), opts);
14707 c = add_com ("watch", class_breakpoint, watch_command,
14708 watch_help.c_str ());
14709 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14710
14711 static const std::string rwatch_help = gdb::option::build_help (_("\
14712Set a read watchpoint for EXPRESSION.\n\
14713Usage: rwatch [-location] EXPRESSION\n\
14714\n\
14715Options:\n\
14716%OPTIONS%\n\
14717\n\
14718A read watchpoint stops execution of your program whenever the value of\n\
14719an expression is read."), opts);
14720 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14721 rwatch_help.c_str ());
14722 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14723
14724 static const std::string awatch_help = gdb::option::build_help (_("\
14725Set an access watchpoint for EXPRESSION.\n\
14726Usage: awatch [-location] EXPRESSION\n\
14727\n\
14728Options:\n\
14729%OPTIONS%\n\
14730\n\
14731An access watchpoint stops execution of your program whenever the value\n\
14732of an expression is either read or written."), opts);
14733 c = add_com ("awatch", class_breakpoint, awatch_command,
14734 awatch_help.c_str ());
14735 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 14736
11db9430 14737 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 14738Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14739
920d2a44
AC
14740 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14741 respond to changes - contrary to the description. */
85c07804
AC
14742 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14743 &can_use_hw_watchpoints, _("\
14744Set debugger's willingness to use watchpoint hardware."), _("\
14745Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14746If zero, gdb will not use hardware for new watchpoints, even if\n\
14747such is available. (However, any hardware watchpoints that were\n\
14748created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14749hardware.)"),
14750 NULL,
920d2a44 14751 show_can_use_hw_watchpoints,
85c07804 14752 &setlist, &showlist);
c906108c
SS
14753
14754 can_use_hw_watchpoints = 1;
fa8d40ab 14755
1042e4c0
SS
14756 /* Tracepoint manipulation commands. */
14757
3947f654
SM
14758 cmd_list_element *trace_cmd
14759 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 14760Set a tracepoint at specified location.\n\
1042e4c0
SS
14761\n"
14762BREAK_ARGS_HELP ("trace") "\n\
14763Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 14764 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 14765
3947f654
SM
14766 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14767 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14768 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14769 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 14770
7a697b8d 14771 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 14772Set a fast tracepoint at specified location.\n\
7a697b8d
SS
14773\n"
14774BREAK_ARGS_HELP ("ftrace") "\n\
14775Do \"help tracepoints\" for info on other tracepoint commands."));
14776 set_cmd_completer (c, location_completer);
14777
0fb4aa4b 14778 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 14779Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
14780\n\
14781strace [LOCATION] [if CONDITION]\n\
629500fa
KS
14782LOCATION may be a linespec, explicit, or address location (described below) \n\
14783or -m MARKER_ID.\n\n\
14784If a marker id is specified, probe the marker with that name. With\n\
14785no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
14786Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14787This collects arbitrary user data passed in the probe point call to the\n\
14788tracing library. You can inspect it when analyzing the trace buffer,\n\
14789by printing the $_sdata variable like any other convenience variable.\n\
14790\n\
14791CONDITION is a boolean expression.\n\
264f9890 14792\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14793Multiple tracepoints at one place are permitted, and useful if their\n\
14794conditions are different.\n\
0fb4aa4b
PA
14795\n\
14796Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14797Do \"help tracepoints\" for info on other tracepoint commands."));
14798 set_cmd_completer (c, location_completer);
14799
e0f25bd9
SM
14800 cmd_list_element *info_tracepoints_cmd
14801 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 14802Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14803Convenience variable \"$tpnum\" contains the number of the\n\
14804last tracepoint set."));
14805
e0f25bd9 14806 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 14807
5e84b7ee
SM
14808 cmd_list_element *delete_tracepoints_cmd
14809 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
14810Delete specified tracepoints.\n\
14811Arguments are tracepoint numbers, separated by spaces.\n\
14812No argument means delete all tracepoints."),
14813 &deletelist);
5e84b7ee 14814 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
14815
14816 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14817Disable specified tracepoints.\n\
14818Arguments are tracepoint numbers, separated by spaces.\n\
14819No argument means disable all tracepoints."),
14820 &disablelist);
14821 deprecate_cmd (c, "disable");
14822
14823 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14824Enable specified tracepoints.\n\
14825Arguments are tracepoint numbers, separated by spaces.\n\
14826No argument means enable all tracepoints."),
14827 &enablelist);
14828 deprecate_cmd (c, "enable");
14829
14830 add_com ("passcount", class_trace, trace_pass_command, _("\
14831Set the passcount for a tracepoint.\n\
14832The trace will end when the tracepoint has been passed 'count' times.\n\
14833Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14834if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14835
0743fc83
TT
14836 add_basic_prefix_cmd ("save", class_breakpoint,
14837 _("Save breakpoint definitions as a script."),
2f822da5 14838 &save_cmdlist,
0743fc83 14839 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
14840
14841 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14842Save current breakpoint definitions as a script.\n\
cce7e648 14843This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14844catchpoints, tracepoints). Use the 'source' command in another debug\n\
14845session to restore them."),
14846 &save_cmdlist);
14847 set_cmd_completer (c, filename_completer);
14848
3947f654
SM
14849 cmd_list_element *save_tracepoints_cmd
14850 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14851Save current tracepoint definitions as a script.\n\
6149aea9
PA
14852Use the 'source' command in another debug session to restore them."),
14853 &save_cmdlist);
3947f654 14854 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 14855
3947f654 14856 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
14857 deprecate_cmd (c, "save tracepoints");
14858
f54bdb6d
SM
14859 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14860 _("\
590042fc 14861Breakpoint specific settings.\n\
fa8d40ab 14862Configure various breakpoint-specific variables such as\n\
590042fc 14863pending breakpoint behavior."),
f54bdb6d 14864 _("\
590042fc 14865Breakpoint specific settings.\n\
fa8d40ab 14866Configure various breakpoint-specific variables such as\n\
590042fc 14867pending breakpoint behavior."),
f54bdb6d
SM
14868 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14869 &setlist, &showlist);
fa8d40ab 14870
7915a72c
AC
14871 add_setshow_auto_boolean_cmd ("pending", no_class,
14872 &pending_break_support, _("\
14873Set debugger's behavior regarding pending breakpoints."), _("\
14874Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14875If on, an unrecognized breakpoint location will cause gdb to create a\n\
14876pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14877an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14878user-query to see if a pending breakpoint should be created."),
2c5b56ce 14879 NULL,
920d2a44 14880 show_pending_break_support,
6e1d7d6c
AC
14881 &breakpoint_set_cmdlist,
14882 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14883
14884 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14885
14886 add_setshow_boolean_cmd ("auto-hw", no_class,
14887 &automatic_hardware_breakpoints, _("\
14888Set automatic usage of hardware breakpoints."), _("\
14889Show automatic usage of hardware breakpoints."), _("\
14890If set, the debugger will automatically use hardware breakpoints for\n\
14891breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14892a warning will be emitted for such breakpoints."),
14893 NULL,
14894 show_automatic_hardware_breakpoints,
14895 &breakpoint_set_cmdlist,
14896 &breakpoint_show_cmdlist);
74960c60 14897
a25a5a45
PA
14898 add_setshow_boolean_cmd ("always-inserted", class_support,
14899 &always_inserted_mode, _("\
74960c60
VP
14900Set mode for inserting breakpoints."), _("\
14901Show mode for inserting breakpoints."), _("\
a25a5a45
PA
14902When this mode is on, breakpoints are inserted immediately as soon as\n\
14903they're created, kept inserted even when execution stops, and removed\n\
14904only when the user deletes them. When this mode is off (the default),\n\
14905breakpoints are inserted only when execution continues, and removed\n\
14906when execution stops."),
72d0e2c5
YQ
14907 NULL,
14908 &show_always_inserted_mode,
14909 &breakpoint_set_cmdlist,
14910 &breakpoint_show_cmdlist);
f1310107 14911
b775012e
LM
14912 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14913 condition_evaluation_enums,
14914 &condition_evaluation_mode_1, _("\
14915Set mode of breakpoint condition evaluation."), _("\
14916Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 14917When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
14918evaluated on the host's side by GDB. When it is set to \"target\",\n\
14919breakpoint conditions will be downloaded to the target (if the target\n\
14920supports such feature) and conditions will be evaluated on the target's side.\n\
14921If this is set to \"auto\" (default), this will be automatically set to\n\
14922\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 14923be set to \"host\"."),
b775012e
LM
14924 &set_condition_evaluation_mode,
14925 &show_condition_evaluation_mode,
14926 &breakpoint_set_cmdlist,
14927 &breakpoint_show_cmdlist);
14928
f1310107
TJB
14929 add_com ("break-range", class_breakpoint, break_range_command, _("\
14930Set a breakpoint for an address range.\n\
14931break-range START-LOCATION, END-LOCATION\n\
14932where START-LOCATION and END-LOCATION can be one of the following:\n\
14933 LINENUM, for that line in the current file,\n\
14934 FILE:LINENUM, for that line in that file,\n\
14935 +OFFSET, for that number of lines after the current line\n\
dda83cd7 14936 or the start of the range\n\
f1310107
TJB
14937 FUNCTION, for the first line in that function,\n\
14938 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14939 *ADDRESS, for the instruction at that address.\n\
14940\n\
14941The breakpoint will stop execution of the inferior whenever it executes\n\
14942an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14943range (including START-LOCATION and END-LOCATION)."));
14944
e7e0cddf 14945 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 14946Set a dynamic printf at specified location.\n\
e7e0cddf 14947dprintf location,format string,arg1,arg2,...\n\
629500fa 14948location may be a linespec, explicit, or address location.\n"
264f9890 14949"\n" LOCATION_SPEC_HELP_STRING));
e7e0cddf
SS
14950 set_cmd_completer (c, location_completer);
14951
14952 add_setshow_enum_cmd ("dprintf-style", class_support,
14953 dprintf_style_enums, &dprintf_style, _("\
14954Set the style of usage for dynamic printf."), _("\
14955Show the style of usage for dynamic printf."), _("\
14956This setting chooses how GDB will do a dynamic printf.\n\
14957If the value is \"gdb\", then the printing is done by GDB to its own\n\
14958console, as with the \"printf\" command.\n\
14959If the value is \"call\", the print is done by calling a function in your\n\
14960program; by default printf(), but you can choose a different function or\n\
14961output stream by setting dprintf-function and dprintf-channel."),
14962 update_dprintf_commands, NULL,
14963 &setlist, &showlist);
14964
e7e0cddf
SS
14965 add_setshow_string_cmd ("dprintf-function", class_support,
14966 &dprintf_function, _("\
590042fc
PW
14967Set the function to use for dynamic printf."), _("\
14968Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
14969 update_dprintf_commands, NULL,
14970 &setlist, &showlist);
14971
e7e0cddf
SS
14972 add_setshow_string_cmd ("dprintf-channel", class_support,
14973 &dprintf_channel, _("\
590042fc
PW
14974Set the channel to use for dynamic printf."), _("\
14975Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
14976 update_dprintf_commands, NULL,
14977 &setlist, &showlist);
14978
d3ce09f5
SS
14979 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14980 &disconnected_dprintf, _("\
14981Set whether dprintf continues after GDB disconnects."), _("\
14982Show whether dprintf continues after GDB disconnects."), _("\
14983Use this to let dprintf commands continue to hit and produce output\n\
14984even if GDB disconnects or detaches from the target."),
14985 NULL,
14986 NULL,
14987 &setlist, &showlist);
14988
14989 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
14990Target agent only formatted printing, like the C \"printf\" function.\n\
14991Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14992This supports most C printf format specifications, like %s, %d, etc.\n\
14993This is useful for formatted output in user-defined commands."));
d3ce09f5 14994
491144b5 14995 automatic_hardware_breakpoints = true;
f3b1572e 14996
c90e7d63
SM
14997 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14998 "breakpoint");
14999 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15000 "breakpoint");
c906108c 15001}