]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
gas: NEWS: Mention the T-Head extensions that were recently added
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
4a94e368 3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
d55e5aa6 21#include "arch-utils.h"
4de283e4
TT
22#include <ctype.h>
23#include "hashtab.h"
24#include "symtab.h"
25#include "frame.h"
c906108c 26#include "breakpoint.h"
4de283e4
TT
27#include "tracepoint.h"
28#include "gdbtypes.h"
c906108c 29#include "expression.h"
d55e5aa6 30#include "gdbcore.h"
4de283e4
TT
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
c906108c 34#include "inferior.h"
45741a9c 35#include "infrun.h"
4de283e4
TT
36#include "gdbthread.h"
37#include "target.h"
c906108c 38#include "language.h"
4de283e4
TT
39#include "gdb-demangle.h"
40#include "filenames.h"
41#include "annotate.h"
42#include "symfile.h"
d55e5aa6 43#include "objfiles.h"
4de283e4
TT
44#include "source.h"
45#include "linespec.h"
46#include "completer.h"
47#include "ui-out.h"
48#include "cli/cli-script.h"
49#include "block.h"
50#include "solib.h"
51#include "solist.h"
d55e5aa6 52#include "observable.h"
4de283e4
TT
53#include "memattr.h"
54#include "ada-lang.h"
55#include "top.h"
56#include "valprint.h"
57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
d322d6d6 59#include "gdbsupport/gdb_regex.h"
55aa24fb 60#include "probe.h"
4de283e4 61#include "cli/cli-utils.h"
d55e5aa6 62#include "stack.h"
4de283e4
TT
63#include "ax-gdb.h"
64#include "dummy-frame.h"
65#include "interps.h"
268a13a5 66#include "gdbsupport/format.h"
cfc31633 67#include "thread-fsm.h"
5d5658a1 68#include "tid-parse.h"
4de283e4 69#include "cli/cli-style.h"
0f8e2034 70#include "cli/cli-decode.h"
d3ce09f5 71
1042e4c0 72/* readline include files */
073bbbb0 73#include "readline/tilde.h"
1042e4c0
SS
74
75/* readline defines this. */
76#undef savestring
77
034dad6f 78#include "mi/mi-common.h"
6dddc817 79#include "extension.h"
325fac50 80#include <algorithm>
5ed8105e 81#include "progspace-and-thread.h"
268a13a5
TT
82#include "gdbsupport/array-view.h"
83#include "gdbsupport/gdb_optional.h"
104c1213 84
4a64f543 85/* Prototypes for local functions. */
c906108c 86
896b6bda 87static void map_breakpoint_numbers (const char *,
48649e1b 88 gdb::function_view<void (breakpoint *)>);
c906108c 89
f00aae0f 90static void
264f9890
PA
91 create_sals_from_location_spec_default (location_spec *locspec,
92 linespec_result *canonical);
983af33b 93
eee031e2
TT
94static void create_breakpoints_sal (struct gdbarch *,
95 struct linespec_result *,
96 gdb::unique_xmalloc_ptr<char>,
97 gdb::unique_xmalloc_ptr<char>,
98 enum bptype,
99 enum bpdisp, int, int,
100 int,
eee031e2 101 int, int, int, unsigned);
983af33b 102
264f9890
PA
103static std::vector<symtab_and_line> decode_location_spec_default
104 (struct breakpoint *b, struct location_spec *locspec,
6c5b2ebe 105 struct program_space *search_pspace);
983af33b 106
a6535de1
TT
107static int can_use_hardware_watchpoint
108 (const std::vector<value_ref_ptr> &vals);
c906108c 109
b713485d 110static void mention (const breakpoint *);
c906108c 111
7a3e3265 112static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
c906108c 113
752a2291
PA
114static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
115
06edf0c0
PA
116static struct breakpoint *
117 momentary_breakpoint_from_master (struct breakpoint *orig,
118 enum bptype type,
7ab97995 119 int loc_enabled, int thread);
06edf0c0 120
76897487
KB
121static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
122
a6d9a66e
UW
123static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
124 CORE_ADDR bpaddr,
60270718
AB
125 enum bptype bptype,
126 struct program_space *pspace);
76897487 127
85d721b8
PA
128static int watchpoint_locations_match (struct bp_location *loc1,
129 struct bp_location *loc2);
130
7f32a4d5
PA
131static int breakpoint_locations_match (struct bp_location *loc1,
132 struct bp_location *loc2,
133 bool sw_hw_bps_match = false);
134
f1310107 135static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 136 const struct address_space *aspace,
f1310107
TJB
137 CORE_ADDR addr);
138
d35ae833 139static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 140 const address_space *,
d35ae833
PA
141 CORE_ADDR, int);
142
834c0d03 143static int remove_breakpoint (struct bp_location *);
b2b6a7da 144static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 145
313f3b21 146static enum print_stop_action print_bp_stop_message (bpstat *bs);
c906108c 147
a14ed312 148static int hw_breakpoint_used_count (void);
c906108c 149
a1398e0c
PA
150static int hw_watchpoint_use_count (struct breakpoint *);
151
152static int hw_watchpoint_used_count_others (struct breakpoint *except,
153 enum bptype type,
154 int *other_type_used);
c906108c 155
816338b5
SS
156static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
157 int count);
c906108c 158
f431efe5 159static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 160
264f9890 161static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
5d926615 162 (struct breakpoint *b,
264f9890 163 location_spec *locspec,
5d926615
TT
164 struct program_space *search_pspace);
165
6e14e441
PA
166static bool bl_address_is_meaningful (bp_location *loc);
167
44702360
PA
168/* update_global_location_list's modes of operation wrt to whether to
169 insert locations now. */
170enum ugll_insert_mode
171{
172 /* Don't insert any breakpoint locations into the inferior, only
173 remove already-inserted locations that no longer should be
174 inserted. Functions that delete a breakpoint or breakpoints
175 should specify this mode, so that deleting a breakpoint doesn't
176 have the side effect of inserting the locations of other
177 breakpoints that are marked not-inserted, but should_be_inserted
178 returns true on them.
179
180 This behavior is useful is situations close to tear-down -- e.g.,
181 after an exec, while the target still has execution, but
182 breakpoint shadows of the previous executable image should *NOT*
183 be restored to the new image; or before detaching, where the
184 target still has execution and wants to delete breakpoints from
185 GDB's lists, and all breakpoints had already been removed from
186 the inferior. */
187 UGLL_DONT_INSERT,
188
a25a5a45
PA
189 /* May insert breakpoints iff breakpoints_should_be_inserted_now
190 claims breakpoints should be inserted now. */
04086b45
PA
191 UGLL_MAY_INSERT,
192
a25a5a45
PA
193 /* Insert locations now, irrespective of
194 breakpoints_should_be_inserted_now. E.g., say all threads are
195 stopped right now, and the user did "continue". We need to
196 insert breakpoints _before_ resuming the target, but
197 UGLL_MAY_INSERT wouldn't insert them, because
198 breakpoints_should_be_inserted_now returns false at that point,
199 as no thread is running yet. */
04086b45 200 UGLL_INSERT
44702360
PA
201};
202
203static void update_global_location_list (enum ugll_insert_mode);
a5606eee 204
44702360 205static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 206
74960c60 207static void insert_breakpoint_locations (void);
a5606eee 208
0b39b52e 209static void trace_pass_command (const char *, int);
1042e4c0 210
558a9d82
YQ
211static void set_tracepoint_count (int num);
212
f2478a7e 213static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 214
983af33b
SDJ
215/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
216 otherwise. */
217
218static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 219
264f9890
PA
220static void bkpt_probe_create_sals_from_location_spec
221 (location_spec *locspec,
03ada39e 222 struct linespec_result *canonical);
264f9890
PA
223static void tracepoint_probe_create_sals_from_location_spec
224 (location_spec *locspec,
03ada39e 225 struct linespec_result *canonical);
8613a476 226
74421c0b 227const struct breakpoint_ops code_breakpoint_ops =
8613a476 228{
264f9890 229 create_sals_from_location_spec_default,
eee031e2 230 create_breakpoints_sal,
8613a476
TT
231};
232
55aa24fb 233/* Breakpoints set on probes. */
a678887d 234static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
8613a476 235{
264f9890 236 bkpt_probe_create_sals_from_location_spec,
eee031e2 237 create_breakpoints_sal,
8613a476 238};
55aa24fb 239
bac7c5cf 240/* Tracepoints set on probes. */
a678887d 241static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
8613a476 242{
264f9890 243 tracepoint_probe_create_sals_from_location_spec,
eee031e2 244 create_breakpoints_sal,
8613a476 245};
bac7c5cf 246
1c2cbcf1
PA
247/* Implementation of abstract dtors. These must exist to satisfy the
248 linker. */
249
250breakpoint::~breakpoint ()
251{
252}
253
74421c0b 254code_breakpoint::~code_breakpoint ()
1c2cbcf1
PA
255{
256}
257
258catchpoint::~catchpoint ()
259{
260}
261
098f1272 262/* The structure to be used in regular breakpoints. */
74421c0b 263struct ordinary_breakpoint : public code_breakpoint
098f1272 264{
74421c0b 265 using code_breakpoint::code_breakpoint;
73063f51 266
7dd8e7ae 267 int resources_needed (const struct bp_location *) override;
7bd86313 268 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 269 void print_mention () const override;
4d1ae558 270 void print_recreate (struct ui_file *fp) const override;
098f1272
TT
271};
272
9a71ed14
PA
273/* Internal breakpoints. These typically have a lifetime the same as
274 the program, and they end up installed on the breakpoint chain with
275 a negative breakpoint number. They're visible in "maint info
276 breakpoints", but not "info breakpoints". */
74421c0b 277struct internal_breakpoint : public code_breakpoint
098f1272 278{
752a2291
PA
279 internal_breakpoint (struct gdbarch *gdbarch,
280 enum bptype type, CORE_ADDR address)
74421c0b 281 : code_breakpoint (gdbarch, type)
752a2291
PA
282 {
283 symtab_and_line sal;
284 sal.pc = address;
285 sal.section = find_pc_overlay (sal.pc);
286 sal.pspace = current_program_space;
287 add_location (sal);
288
289 pspace = current_program_space;
290 disposition = disp_donttouch;
291 }
73063f51 292
c359fff5
TT
293 void re_set () override;
294 void check_status (struct bpstat *bs) override;
7bd86313 295 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 296 void print_mention () const override;
098f1272
TT
297};
298
9a71ed14
PA
299/* Momentary breakpoints. These typically have a lifetime of some run
300 control command only, are always thread-specific, and have 0 for
301 breakpoint number. I.e., there can be many momentary breakpoints
302 on the breakpoint chain and they all same the same number (zero).
303 They're visible in "maint info breakpoints", but not "info
304 breakpoints". */
74421c0b 305struct momentary_breakpoint : public code_breakpoint
098f1272 306{
7ab97995
PA
307 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
308 program_space *pspace_,
309 const struct frame_id &frame_id_,
310 int thread_)
74421c0b 311 : code_breakpoint (gdbarch_, bptype)
7ab97995
PA
312 {
313 /* If FRAME_ID is valid, it should be a real frame, not an inlined
314 or tail-called one. */
315 gdb_assert (!frame_id_artificial_p (frame_id));
316
317 /* Momentary breakpoints are always thread-specific. */
318 gdb_assert (thread_ > 0);
319
320 pspace = pspace_;
321 enable_state = bp_enabled;
322 disposition = disp_donttouch;
323 frame_id = frame_id_;
324 thread = thread_;
325 }
73063f51 326
1fd30a47
TT
327 void re_set () override;
328 void check_status (struct bpstat *bs) override;
7bd86313 329 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 330 void print_mention () const override;
098f1272
TT
331};
332
333/* DPrintf breakpoints. */
5ad71d67 334struct dprintf_breakpoint : public ordinary_breakpoint
098f1272 335{
73063f51
TT
336 using ordinary_breakpoint::ordinary_breakpoint;
337
f293a0b5
TT
338 void re_set () override;
339 int breakpoint_hit (const struct bp_location *bl,
340 const address_space *aspace,
341 CORE_ADDR bp_addr,
342 const target_waitstatus &ws) override;
4d1ae558 343 void print_recreate (struct ui_file *fp) const override;
f293a0b5 344 void after_condition_true (struct bpstat *bs) override;
098f1272
TT
345};
346
ec45bb67
TT
347/* Ranged breakpoints. */
348struct ranged_breakpoint : public ordinary_breakpoint
349{
b925bf21
PA
350 explicit ranged_breakpoint (struct gdbarch *gdbarch,
351 const symtab_and_line &sal_start,
352 int length,
264f9890
PA
353 location_spec_up start_locspec,
354 location_spec_up end_locspec)
73063f51
TT
355 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
356 {
b925bf21
PA
357 bp_location *bl = add_location (sal_start);
358 bl->length = length;
359
360 disposition = disp_donttouch;
361
264f9890
PA
362 locspec = std::move (start_locspec);
363 locspec_range_end = std::move (end_locspec);
73063f51
TT
364 }
365
ec45bb67
TT
366 int breakpoint_hit (const struct bp_location *bl,
367 const address_space *aspace,
368 CORE_ADDR bp_addr,
369 const target_waitstatus &ws) override;
370 int resources_needed (const struct bp_location *) override;
7bd86313 371 enum print_stop_action print_it (const bpstat *bs) const override;
a67bcaba 372 bool print_one (bp_location **) const override;
ec45bb67 373 void print_one_detail (struct ui_out *) const override;
b713485d 374 void print_mention () const override;
4d1ae558 375 void print_recreate (struct ui_file *fp) const override;
ec45bb67
TT
376};
377
6d7a8c56
TT
378/* Static tracepoints with marker (`-m'). */
379struct static_marker_tracepoint : public tracepoint
380{
73063f51
TT
381 using tracepoint::tracepoint;
382
264f9890
PA
383 std::vector<symtab_and_line> decode_location_spec
384 (struct location_spec *locspec,
6d7a8c56
TT
385 struct program_space *search_pspace) override;
386};
387
d3ce09f5
SS
388/* The style in which to perform a dynamic printf. This is a user
389 option because different output options have different tradeoffs;
390 if GDB does the printing, there is better error handling if there
391 is a problem with any of the arguments, but using an inferior
392 function lets you have special-purpose printers and sending of
393 output to the same place as compiled-in print functions. */
394
395static const char dprintf_style_gdb[] = "gdb";
396static const char dprintf_style_call[] = "call";
397static const char dprintf_style_agent[] = "agent";
398static const char *const dprintf_style_enums[] = {
399 dprintf_style_gdb,
400 dprintf_style_call,
401 dprintf_style_agent,
402 NULL
403};
404static const char *dprintf_style = dprintf_style_gdb;
405
406/* The function to use for dynamic printf if the preferred style is to
407 call into the inferior. The value is simply a string that is
408 copied into the command, so it can be anything that GDB can
409 evaluate to a callable address, not necessarily a function name. */
410
e0700ba4 411static std::string dprintf_function = "printf";
d3ce09f5
SS
412
413/* The channel to use for dynamic printf if the preferred style is to
414 call into the inferior; if a nonempty string, it will be passed to
415 the call as the first argument, with the format string as the
416 second. As with the dprintf function, this can be anything that
417 GDB knows how to evaluate, so in addition to common choices like
418 "stderr", this could be an app-specific expression like
419 "mystreams[curlogger]". */
420
e0700ba4 421static std::string dprintf_channel;
d3ce09f5
SS
422
423/* True if dprintf commands should continue to operate even if GDB
424 has disconnected. */
491144b5 425static bool disconnected_dprintf = true;
d3ce09f5 426
5cea2a26
PA
427struct command_line *
428breakpoint_commands (struct breakpoint *b)
429{
d1b0a7bf 430 return b->commands ? b->commands.get () : NULL;
5cea2a26 431}
3daf8fe5 432
f3b1572e
PA
433/* Flag indicating that a command has proceeded the inferior past the
434 current breakpoint. */
435
491144b5 436static bool breakpoint_proceeded;
f3b1572e 437
956a9fb9 438const char *
2cec12e5
AR
439bpdisp_text (enum bpdisp disp)
440{
4a64f543
MS
441 /* NOTE: the following values are a part of MI protocol and
442 represent values of 'disp' field returned when inferior stops at
443 a breakpoint. */
bc043ef3 444 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 445
2cec12e5
AR
446 return bpdisps[(int) disp];
447}
c906108c 448
4a64f543 449/* Prototypes for exported functions. */
c906108c 450/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 451 if such is available. */
c906108c
SS
452static int can_use_hw_watchpoints;
453
920d2a44
AC
454static void
455show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
456 struct cmd_list_element *c,
457 const char *value)
458{
6cb06a8c
TT
459 gdb_printf (file,
460 _("Debugger's willingness to use "
461 "watchpoint hardware is %s.\n"),
462 value);
920d2a44
AC
463}
464
fa8d40ab
JJ
465/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
466 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 467 for unrecognized breakpoint locations.
fa8d40ab
JJ
468 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
469static enum auto_boolean pending_break_support;
920d2a44
AC
470static void
471show_pending_break_support (struct ui_file *file, int from_tty,
472 struct cmd_list_element *c,
473 const char *value)
474{
6cb06a8c
TT
475 gdb_printf (file,
476 _("Debugger's behavior regarding "
477 "pending breakpoints is %s.\n"),
478 value);
920d2a44 479}
fa8d40ab 480
491144b5 481/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 482 set with "break" but falling in read-only memory.
491144b5 483 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 484 use hardware breakpoints. */
491144b5 485static bool automatic_hardware_breakpoints;
765dc015
VP
486static void
487show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
488 struct cmd_list_element *c,
489 const char *value)
490{
6cb06a8c
TT
491 gdb_printf (file,
492 _("Automatic usage of hardware breakpoints is %s.\n"),
493 value);
765dc015
VP
494}
495
a25a5a45
PA
496/* If on, GDB keeps breakpoints inserted even if the inferior is
497 stopped, and immediately inserts any new breakpoints as soon as
498 they're created. If off (default), GDB keeps breakpoints off of
499 the target as long as possible. That is, it delays inserting
500 breakpoints until the next resume, and removes them again when the
501 target fully stops. This is a bit safer in case GDB crashes while
502 processing user input. */
491144b5 503static bool always_inserted_mode = false;
72d0e2c5 504
33e5cbd6 505static void
74960c60 506show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 507 struct cmd_list_element *c, const char *value)
74960c60 508{
6cb06a8c
TT
509 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
510 value);
74960c60
VP
511}
512
b57bacec
PA
513/* See breakpoint.h. */
514
33e5cbd6 515int
a25a5a45 516breakpoints_should_be_inserted_now (void)
33e5cbd6 517{
a25a5a45
PA
518 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
519 {
520 /* If breakpoints are global, they should be inserted even if no
521 thread under gdb's control is running, or even if there are
522 no threads under GDB's control yet. */
523 return 1;
524 }
5b6d1e4f 525 else
a25a5a45 526 {
a25a5a45
PA
527 if (always_inserted_mode)
528 {
529 /* The user wants breakpoints inserted even if all threads
530 are stopped. */
531 return 1;
532 }
533
5b6d1e4f
PA
534 for (inferior *inf : all_inferiors ())
535 if (inf->has_execution ()
536 && threads_are_executing (inf->process_target ()))
537 return 1;
372316f1
PA
538
539 /* Don't remove breakpoints yet if, even though all threads are
540 stopped, we still have events to process. */
08036331 541 for (thread_info *tp : all_non_exited_threads ())
1edb66d8 542 if (tp->resumed () && tp->has_pending_waitstatus ())
372316f1 543 return 1;
a25a5a45
PA
544 }
545 return 0;
33e5cbd6 546}
765dc015 547
b775012e
LM
548static const char condition_evaluation_both[] = "host or target";
549
550/* Modes for breakpoint condition evaluation. */
551static const char condition_evaluation_auto[] = "auto";
552static const char condition_evaluation_host[] = "host";
553static const char condition_evaluation_target[] = "target";
554static const char *const condition_evaluation_enums[] = {
555 condition_evaluation_auto,
556 condition_evaluation_host,
557 condition_evaluation_target,
558 NULL
559};
560
561/* Global that holds the current mode for breakpoint condition evaluation. */
562static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
563
564/* Global that we use to display information to the user (gets its value from
565 condition_evaluation_mode_1. */
566static const char *condition_evaluation_mode = condition_evaluation_auto;
567
568/* Translate a condition evaluation mode MODE into either "host"
569 or "target". This is used mostly to translate from "auto" to the
570 real setting that is being used. It returns the translated
571 evaluation mode. */
572
573static const char *
574translate_condition_evaluation_mode (const char *mode)
575{
576 if (mode == condition_evaluation_auto)
577 {
578 if (target_supports_evaluation_of_breakpoint_conditions ())
579 return condition_evaluation_target;
580 else
581 return condition_evaluation_host;
582 }
583 else
584 return mode;
585}
586
587/* Discovers what condition_evaluation_auto translates to. */
588
589static const char *
590breakpoint_condition_evaluation_mode (void)
591{
592 return translate_condition_evaluation_mode (condition_evaluation_mode);
593}
594
595/* Return true if GDB should evaluate breakpoint conditions or false
596 otherwise. */
597
598static int
599gdb_evaluates_breakpoint_condition_p (void)
600{
601 const char *mode = breakpoint_condition_evaluation_mode ();
602
603 return (mode == condition_evaluation_host);
604}
605
c906108c
SS
606/* Are we executing breakpoint commands? */
607static int executing_breakpoint_commands;
608
c02f5703
MS
609/* Are overlay event breakpoints enabled? */
610static int overlay_events_enabled;
611
e09342b5 612/* See description in breakpoint.h. */
491144b5 613bool target_exact_watchpoints = false;
e09342b5 614
c906108c 615/* Walk the following statement or block through all breakpoints.
e5dd4106 616 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 617 current breakpoint. */
c906108c 618
5c44784c
JM
619#define ALL_BREAKPOINTS_SAFE(B,TMP) \
620 for (B = breakpoint_chain; \
621 B ? (TMP=B->next, 1): 0; \
622 B = TMP)
c906108c 623
7cc221ef 624/* Chains of all breakpoints defined. */
c906108c 625
81e6b8eb 626static struct breakpoint *breakpoint_chain;
c906108c 627
240edef6 628/* See breakpoint.h. */
43892fdf 629
240edef6 630breakpoint_range
43892fdf
SM
631all_breakpoints ()
632{
633 return breakpoint_range (breakpoint_chain);
634}
635
240edef6 636/* See breakpoint.h. */
1428b37a 637
240edef6 638breakpoint_safe_range
1428b37a
SM
639all_breakpoints_safe ()
640{
641 return breakpoint_safe_range (all_breakpoints ());
642}
643
f6d17b2b
SM
644/* See breakpoint.h. */
645
646tracepoint_range
647all_tracepoints ()
648{
649 return tracepoint_range (breakpoint_chain);
650}
651
39ef2f62 652/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 653
5d51cd5d 654static std::vector<bp_location *> bp_locations;
876fa593 655
055c879f
SM
656/* See breakpoint.h. */
657
658const std::vector<bp_location *> &
48d7020b
SM
659all_bp_locations ()
660{
661 return bp_locations;
662}
663
e0d9a270
SM
664/* Range to iterate over breakpoint locations at a given address. */
665
666struct bp_locations_at_addr_range
667{
668 using iterator = std::vector<bp_location *>::iterator;
669
670 bp_locations_at_addr_range (CORE_ADDR addr)
671 {
672 struct compare
673 {
674 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
675 { return loc->address < addr_; }
676
677 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
678 { return addr_ < loc->address; }
679 };
680
681 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
682 addr, compare ());
683
684 m_begin = it_pair.first;
685 m_end = it_pair.second;
686 }
687
688 iterator begin () const
689 { return m_begin; }
690
691 iterator end () const
692 { return m_end; }
693
694private:
695 iterator m_begin;
696 iterator m_end;
697};
698
699/* Return a range to iterate over all breakpoint locations exactly at address
700 ADDR.
701
702 If it's needed to iterate multiple times on the same range, it's possible
703 to save the range in a local variable and use it multiple times:
704
705 auto range = all_bp_locations_at_addr (addr);
706
707 for (bp_location *loc : range)
708 // use loc
709
710 for (bp_location *loc : range)
711 // use loc
712
713 This saves a bit of time, as it avoids re-doing the binary searches to find
714 the range's boundaries. Just remember not to change the bp_locations vector
715 in the mean time, as it could make the range's iterators stale. */
716
717static bp_locations_at_addr_range
718all_bp_locations_at_addr (CORE_ADDR addr)
719{
720 return bp_locations_at_addr_range (addr);
721}
722
4a64f543 723/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 724 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 725 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 726 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 727 an address you need to read. */
876fa593 728
f5336ca5 729static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 730
4a64f543
MS
731/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
732 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
733 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
734 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 735 scan for shadow bytes for an address you need to read. */
876fa593 736
f5336ca5 737static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 738
4a64f543 739/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
740 from the bp_locations array, but for which a hit may still be
741 reported by a target. */
1123588c 742static std::vector<bp_location *> moribund_locations;
20874c92 743
c906108c
SS
744/* Number of last breakpoint made. */
745
95a42b64
TT
746static int breakpoint_count;
747
86b17b60
PA
748/* The value of `breakpoint_count' before the last command that
749 created breakpoints. If the last (break-like) command created more
750 than one breakpoint, then the difference between BREAKPOINT_COUNT
751 and PREV_BREAKPOINT_COUNT is more than one. */
752static int prev_breakpoint_count;
c906108c 753
1042e4c0
SS
754/* Number of last tracepoint made. */
755
95a42b64 756static int tracepoint_count;
1042e4c0 757
6149aea9
PA
758static struct cmd_list_element *breakpoint_set_cmdlist;
759static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 760struct cmd_list_element *save_cmdlist;
6149aea9 761
468d015d
JJ
762/* Return whether a breakpoint is an active enabled breakpoint. */
763static int
764breakpoint_enabled (struct breakpoint *b)
765{
0d381245 766 return (b->enable_state == bp_enabled);
468d015d
JJ
767}
768
c906108c
SS
769/* Set breakpoint count to NUM. */
770
95a42b64 771static void
fba45db2 772set_breakpoint_count (int num)
c906108c 773{
86b17b60 774 prev_breakpoint_count = breakpoint_count;
c906108c 775 breakpoint_count = num;
4fa62494 776 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
777}
778
86b17b60
PA
779/* Used by `start_rbreak_breakpoints' below, to record the current
780 breakpoint count before "rbreak" creates any breakpoint. */
781static int rbreak_start_breakpoint_count;
782
95a42b64
TT
783/* Called at the start an "rbreak" command to record the first
784 breakpoint made. */
86b17b60 785
c80049d3 786scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 787{
86b17b60 788 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
789}
790
791/* Called at the end of an "rbreak" command to record the last
792 breakpoint made. */
86b17b60 793
c80049d3 794scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 795{
86b17b60 796 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
797}
798
4a64f543 799/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
800
801void
fba45db2 802clear_breakpoint_hit_counts (void)
c906108c 803{
43892fdf 804 for (breakpoint *b : all_breakpoints ())
c906108c
SS
805 b->hit_count = 0;
806}
807
c906108c 808\f
48cb2d85
VP
809/* Return the breakpoint with the specified number, or NULL
810 if the number does not refer to an existing breakpoint. */
811
812struct breakpoint *
813get_breakpoint (int num)
814{
43892fdf 815 for (breakpoint *b : all_breakpoints ())
48cb2d85
VP
816 if (b->number == num)
817 return b;
818
43892fdf 819 return nullptr;
48cb2d85 820}
5c44784c 821
c906108c 822\f
adc36818 823
b775012e
LM
824/* Mark locations as "conditions have changed" in case the target supports
825 evaluating conditions on its side. */
826
827static void
828mark_breakpoint_modified (struct breakpoint *b)
829{
b775012e
LM
830 /* This is only meaningful if the target is
831 evaluating conditions and if the user has
832 opted for condition evaluation on the target's
833 side. */
834 if (gdb_evaluates_breakpoint_condition_p ()
835 || !target_supports_evaluation_of_breakpoint_conditions ())
836 return;
837
838 if (!is_breakpoint (b))
839 return;
840
40cb8ca5 841 for (bp_location *loc : b->locations ())
b775012e
LM
842 loc->condition_changed = condition_modified;
843}
844
845/* Mark location as "conditions have changed" in case the target supports
846 evaluating conditions on its side. */
847
848static void
849mark_breakpoint_location_modified (struct bp_location *loc)
850{
851 /* This is only meaningful if the target is
852 evaluating conditions and if the user has
853 opted for condition evaluation on the target's
854 side. */
855 if (gdb_evaluates_breakpoint_condition_p ()
856 || !target_supports_evaluation_of_breakpoint_conditions ())
857
858 return;
859
860 if (!is_breakpoint (loc->owner))
861 return;
862
863 loc->condition_changed = condition_modified;
864}
865
866/* Sets the condition-evaluation mode using the static global
867 condition_evaluation_mode. */
868
869static void
eb4c3f4a 870set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
871 struct cmd_list_element *c)
872{
b775012e
LM
873 const char *old_mode, *new_mode;
874
875 if ((condition_evaluation_mode_1 == condition_evaluation_target)
876 && !target_supports_evaluation_of_breakpoint_conditions ())
877 {
878 condition_evaluation_mode_1 = condition_evaluation_mode;
879 warning (_("Target does not support breakpoint condition evaluation.\n"
880 "Using host evaluation mode instead."));
881 return;
882 }
883
884 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
885 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
886
abf1152a
JK
887 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
888 settings was "auto". */
889 condition_evaluation_mode = condition_evaluation_mode_1;
890
b775012e
LM
891 /* Only update the mode if the user picked a different one. */
892 if (new_mode != old_mode)
893 {
b775012e
LM
894 /* If the user switched to a different evaluation mode, we
895 need to synch the changes with the target as follows:
896
897 "host" -> "target": Send all (valid) conditions to the target.
898 "target" -> "host": Remove all the conditions from the target.
899 */
900
b775012e
LM
901 if (new_mode == condition_evaluation_target)
902 {
903 /* Mark everything modified and synch conditions with the
904 target. */
48d7020b 905 for (bp_location *loc : all_bp_locations ())
b775012e 906 mark_breakpoint_location_modified (loc);
24b21115 907 }
b775012e
LM
908 else
909 {
910 /* Manually mark non-duplicate locations to synch conditions
911 with the target. We do this to remove all the conditions the
912 target knows about. */
48d7020b 913 for (bp_location *loc : all_bp_locations ())
b775012e
LM
914 if (is_breakpoint (loc->owner) && loc->inserted)
915 loc->needs_update = 1;
916 }
917
918 /* Do the update. */
44702360 919 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
920 }
921
922 return;
923}
924
925/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
926 what "auto" is translating to. */
927
928static void
929show_condition_evaluation_mode (struct ui_file *file, int from_tty,
930 struct cmd_list_element *c, const char *value)
931{
932 if (condition_evaluation_mode == condition_evaluation_auto)
6cb06a8c
TT
933 gdb_printf (file,
934 _("Breakpoint condition evaluation "
935 "mode is %s (currently %s).\n"),
936 value,
937 breakpoint_condition_evaluation_mode ());
b775012e 938 else
6cb06a8c
TT
939 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
940 value);
b775012e
LM
941}
942
b5fa468f
TBA
943/* Parse COND_STRING in the context of LOC and set as the condition
944 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
945 the number of LOC within its owner. In case of parsing error, mark
946 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
947
948static void
949set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
950 int bp_num, int loc_num)
951{
952 bool has_junk = false;
953 try
954 {
955 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
956 block_for_pc (loc->address), 0);
957 if (*cond_string != 0)
958 has_junk = true;
959 else
960 {
961 loc->cond = std::move (new_exp);
962 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
963 gdb_printf (_("Breakpoint %d's condition is now valid at "
964 "location %d, enabling.\n"),
965 bp_num, loc_num);
b5fa468f
TBA
966
967 loc->disabled_by_cond = false;
968 }
969 }
970 catch (const gdb_exception_error &e)
971 {
972 if (loc->enabled)
973 {
974 /* Warn if a user-enabled location is now becoming disabled-by-cond.
975 BP_NUM is 0 if the breakpoint is being defined for the first
976 time using the "break ... if ..." command, and non-zero if
977 already defined. */
978 if (bp_num != 0)
979 warning (_("failed to validate condition at location %d.%d, "
980 "disabling:\n %s"), bp_num, loc_num, e.what ());
981 else
982 warning (_("failed to validate condition at location %d, "
983 "disabling:\n %s"), loc_num, e.what ());
984 }
985
986 loc->disabled_by_cond = true;
987 }
988
989 if (has_junk)
990 error (_("Garbage '%s' follows condition"), cond_string);
991}
992
adc36818 993void
7a26bd4d 994set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 995 int from_tty, bool force)
adc36818 996{
4c55e970 997 if (*exp == 0)
3a5c3e22 998 {
6f781ee3 999 b->cond_string.reset ();
3a5c3e22 1000
4c55e970 1001 if (is_watchpoint (b))
78319c15 1002 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
1003 else
1004 {
b5fa468f 1005 int loc_num = 1;
40cb8ca5 1006 for (bp_location *loc : b->locations ())
4c55e970
TBA
1007 {
1008 loc->cond.reset ();
b5fa468f 1009 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
1010 gdb_printf (_("Breakpoint %d's condition is now valid at "
1011 "location %d, enabling.\n"),
1012 b->number, loc_num);
b5fa468f
TBA
1013 loc->disabled_by_cond = false;
1014 loc_num++;
4c55e970
TBA
1015
1016 /* No need to free the condition agent expression
1017 bytecode (if we have one). We will handle this
1018 when we go through update_global_location_list. */
1019 }
1020 }
1e620590 1021
adc36818 1022 if (from_tty)
6cb06a8c 1023 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
adc36818
PM
1024 }
1025 else
1026 {
adc36818
PM
1027 if (is_watchpoint (b))
1028 {
699bd4cf 1029 innermost_block_tracker tracker;
78319c15 1030 const char *arg = exp;
4c55e970 1031 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 1032 if (*arg != 0)
adc36818 1033 error (_("Junk at end of expression"));
78319c15 1034 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 1035 w->cond_exp = std::move (new_exp);
699bd4cf 1036 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
1037 }
1038 else
1039 {
4c55e970
TBA
1040 /* Parse and set condition expressions. We make two passes.
1041 In the first, we parse the condition string to see if it
b5fa468f
TBA
1042 is valid in at least one location. If so, the condition
1043 would be accepted. So we go ahead and set the locations'
1044 conditions. In case no valid case is found, we throw
4c55e970
TBA
1045 the error and the condition string will be rejected.
1046 This two-pass approach is taken to avoid setting the
1047 state of locations in case of a reject. */
40cb8ca5 1048 for (bp_location *loc : b->locations ())
4c55e970 1049 {
b5fa468f
TBA
1050 try
1051 {
1052 const char *arg = exp;
1053 parse_exp_1 (&arg, loc->address,
1054 block_for_pc (loc->address), 0);
1055 if (*arg != 0)
1056 error (_("Junk at end of expression"));
1057 break;
1058 }
1059 catch (const gdb_exception_error &e)
1060 {
1061 /* Condition string is invalid. If this happens to
733d554a
TBA
1062 be the last loc, abandon (if not forced) or continue
1063 (if forced). */
1064 if (loc->next == nullptr && !force)
b5fa468f
TBA
1065 throw;
1066 }
4c55e970
TBA
1067 }
1068
b5fa468f 1069 /* If we reach here, the condition is valid at some locations. */
dda83cd7 1070 int loc_num = 1;
40cb8ca5
SM
1071 for (bp_location *loc : b->locations ())
1072 {
1073 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1074 loc_num++;
1075 }
adc36818 1076 }
1e620590
TBA
1077
1078 /* We know that the new condition parsed successfully. The
1079 condition string of the breakpoint can be safely updated. */
6f781ee3 1080 b->cond_string = make_unique_xstrdup (exp);
1e620590 1081 b->condition_not_parsed = 0;
adc36818 1082 }
b775012e
LM
1083 mark_breakpoint_modified (b);
1084
76727919 1085 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
1086}
1087
79aabb73
TBA
1088/* See breakpoint.h. */
1089
1090void
1091set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1092 bool force)
1093{
43892fdf 1094 for (breakpoint *b : all_breakpoints ())
79aabb73
TBA
1095 if (b->number == bpnum)
1096 {
1097 /* Check if this breakpoint has a "stop" method implemented in an
1098 extension language. This method and conditions entered into GDB
1099 from the CLI are mutually exclusive. */
1100 const struct extension_language_defn *extlang
1101 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1102
1103 if (extlang != NULL)
1104 {
1105 error (_("Only one stop condition allowed. There is currently"
1106 " a %s stop condition defined for this breakpoint."),
1107 ext_lang_capitalized_name (extlang));
1108 }
1109 set_breakpoint_condition (b, exp, from_tty, force);
1110
1111 if (is_breakpoint (b))
1112 update_global_location_list (UGLL_MAY_INSERT);
1113
1114 return;
1115 }
1116
1117 error (_("No breakpoint number %d."), bpnum);
1118}
1119
b1d4d8d1
TBA
1120/* The options for the "condition" command. */
1121
1122struct condition_command_opts
1123{
1124 /* For "-force". */
1125 bool force_condition = false;
1126};
1127
1128static const gdb::option::option_def condition_command_option_defs[] = {
1129
1130 gdb::option::flag_option_def<condition_command_opts> {
1131 "force",
1132 [] (condition_command_opts *opts) { return &opts->force_condition; },
1133 N_("Set the condition even if it is invalid for all current locations."),
1134 },
1135
1136};
1137
1138/* Create an option_def_group for the "condition" options, with
1139 CC_OPTS as context. */
1140
1141static inline gdb::option::option_def_group
1142make_condition_command_options_def_group (condition_command_opts *cc_opts)
1143{
1144 return {{condition_command_option_defs}, cc_opts};
1145}
1146
d55637df
TT
1147/* Completion for the "condition" command. */
1148
eb3ff9a5 1149static void
6f937416 1150condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1151 completion_tracker &tracker,
b1d4d8d1 1152 const char *text, const char * /*word*/)
d55637df 1153{
b1d4d8d1
TBA
1154 bool has_no_arguments = (*text == '\0');
1155 condition_command_opts cc_opts;
1156 const auto group = make_condition_command_options_def_group (&cc_opts);
1157 if (gdb::option::complete_options
1158 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1159 return;
d55637df 1160
f1735a53 1161 text = skip_spaces (text);
b1d4d8d1 1162 const char *space = skip_to_space (text);
d55637df
TT
1163 if (*space == '\0')
1164 {
1165 int len;
d55637df
TT
1166
1167 if (text[0] == '$')
1168 {
b1d4d8d1 1169 tracker.advance_custom_word_point_by (1);
d55637df 1170 /* We don't support completion of history indices. */
eb3ff9a5
PA
1171 if (!isdigit (text[1]))
1172 complete_internalvar (tracker, &text[1]);
1173 return;
d55637df
TT
1174 }
1175
b1d4d8d1
TBA
1176 /* Suggest the "-force" flag if no arguments are given. If
1177 arguments were passed, they either already include the flag,
1178 or we are beyond the point of suggesting it because it's
1179 positionally the first argument. */
1180 if (has_no_arguments)
1181 gdb::option::complete_on_all_options (tracker, group);
1182
d55637df
TT
1183 /* We're completing the breakpoint number. */
1184 len = strlen (text);
1185
43892fdf 1186 for (breakpoint *b : all_breakpoints ())
58ce7251
SDJ
1187 {
1188 char number[50];
1189
1190 xsnprintf (number, sizeof (number), "%d", b->number);
1191
1192 if (strncmp (number, text, len) == 0)
b02f78f9 1193 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1194 }
d55637df 1195
eb3ff9a5 1196 return;
d55637df
TT
1197 }
1198
b1d4d8d1
TBA
1199 /* We're completing the expression part. Skip the breakpoint num. */
1200 const char *exp_start = skip_spaces (space);
1201 tracker.advance_custom_word_point_by (exp_start - text);
1202 text = exp_start;
1203 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1204 expression_completer (cmd, tracker, text, word);
d55637df
TT
1205}
1206
c906108c
SS
1207/* condition N EXP -- set break condition of breakpoint N to EXP. */
1208
1209static void
0b39b52e 1210condition_command (const char *arg, int from_tty)
c906108c 1211{
0b39b52e 1212 const char *p;
52f0bd74 1213 int bnum;
c906108c
SS
1214
1215 if (arg == 0)
e2e0b3e5 1216 error_no_arg (_("breakpoint number"));
c906108c
SS
1217
1218 p = arg;
733d554a
TBA
1219
1220 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1221 condition_command_opts cc_opts;
1222 const auto group = make_condition_command_options_def_group (&cc_opts);
1223 gdb::option::process_options
1224 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1225
c906108c 1226 bnum = get_number (&p);
5c44784c 1227 if (bnum == 0)
8a3fe4f8 1228 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1229
79aabb73 1230 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1231}
1232
a7bdde9e
VP
1233/* Check that COMMAND do not contain commands that are suitable
1234 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1235 Throw if any such commands is found. */
1236
a7bdde9e
VP
1237static void
1238check_no_tracepoint_commands (struct command_line *commands)
1239{
1240 struct command_line *c;
cc59ec59 1241
a7bdde9e
VP
1242 for (c = commands; c; c = c->next)
1243 {
a7bdde9e 1244 if (c->control_type == while_stepping_control)
3e43a32a
MS
1245 error (_("The 'while-stepping' command can "
1246 "only be used for tracepoints"));
a7bdde9e 1247
12973681
TT
1248 check_no_tracepoint_commands (c->body_list_0.get ());
1249 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1250
1251 /* Not that command parsing removes leading whitespace and comment
4a64f543 1252 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1253 command directly. */
1254 if (strstr (c->line, "collect ") == c->line)
1255 error (_("The 'collect' command can only be used for tracepoints"));
1256
51661e93
VP
1257 if (strstr (c->line, "teval ") == c->line)
1258 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1259 }
1260}
1261
098f1272 1262struct longjmp_breakpoint : public momentary_breakpoint
3b0871f4 1263{
73063f51
TT
1264 using momentary_breakpoint::momentary_breakpoint;
1265
c1fc2657 1266 ~longjmp_breakpoint () override;
3b0871f4
SM
1267};
1268
d77f58be
SS
1269/* Encapsulate tests for different types of tracepoints. */
1270
3b0871f4
SM
1271static bool
1272is_tracepoint_type (bptype type)
d9b3f62e
PA
1273{
1274 return (type == bp_tracepoint
1275 || type == bp_fast_tracepoint
7b572efb
TT
1276 || type == bp_static_tracepoint
1277 || type == bp_static_marker_tracepoint);
d9b3f62e
PA
1278}
1279
f2478a7e
SM
1280/* See breakpoint.h. */
1281
1282bool
d77f58be 1283is_tracepoint (const struct breakpoint *b)
a7bdde9e 1284{
d9b3f62e 1285 return is_tracepoint_type (b->type);
a7bdde9e 1286}
d9b3f62e 1287
a5e364af
SM
1288/* Factory function to create an appropriate instance of breakpoint given
1289 TYPE. */
1290
3b003a61 1291template<typename... Arg>
74421c0b 1292static std::unique_ptr<code_breakpoint>
3b003a61
PA
1293new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1294 Arg&&... args)
a5e364af 1295{
74421c0b 1296 code_breakpoint *b;
a5e364af 1297
098f1272
TT
1298 switch (type)
1299 {
1300 case bp_breakpoint:
1301 case bp_hardware_breakpoint:
3b003a61
PA
1302 b = new ordinary_breakpoint (gdbarch, type,
1303 std::forward<Arg> (args)...);
098f1272
TT
1304 break;
1305
1306 case bp_fast_tracepoint:
1307 case bp_static_tracepoint:
1308 case bp_tracepoint:
3b003a61
PA
1309 b = new tracepoint (gdbarch, type,
1310 std::forward<Arg> (args)...);
098f1272
TT
1311 break;
1312
6d7a8c56 1313 case bp_static_marker_tracepoint:
3b003a61
PA
1314 b = new static_marker_tracepoint (gdbarch, type,
1315 std::forward<Arg> (args)...);
6d7a8c56
TT
1316 break;
1317
098f1272 1318 case bp_dprintf:
3b003a61
PA
1319 b = new dprintf_breakpoint (gdbarch, type,
1320 std::forward<Arg> (args)...);
098f1272
TT
1321 break;
1322
098f1272
TT
1323 default:
1324 gdb_assert_not_reached ("invalid type");
1325 }
a5e364af 1326
74421c0b 1327 return std::unique_ptr<code_breakpoint> (b);
a5e364af
SM
1328}
1329
e5dd4106 1330/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1331 breakpoint. This function will throw an exception if a problem is
1332 found. */
48cb2d85 1333
95a42b64
TT
1334static void
1335validate_commands_for_breakpoint (struct breakpoint *b,
1336 struct command_line *commands)
48cb2d85 1337{
d77f58be 1338 if (is_tracepoint (b))
a7bdde9e 1339 {
c9a6ce02 1340 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1341 struct command_line *c;
1342 struct command_line *while_stepping = 0;
c9a6ce02
PA
1343
1344 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1345 might have included a while-stepping action, while the new
1346 ones might not. */
c9a6ce02
PA
1347 t->step_count = 0;
1348
1349 /* We need to verify that each top-level element of commands is
1350 valid for tracepoints, that there's at most one
1351 while-stepping element, and that the while-stepping's body
1352 has valid tracing commands excluding nested while-stepping.
1353 We also need to validate the tracepoint action line in the
1354 context of the tracepoint --- validate_actionline actually
1355 has side effects, like setting the tracepoint's
1356 while-stepping STEP_COUNT, in addition to checking if the
1357 collect/teval actions parse and make sense in the
1358 tracepoint's context. */
a7bdde9e
VP
1359 for (c = commands; c; c = c->next)
1360 {
a7bdde9e
VP
1361 if (c->control_type == while_stepping_control)
1362 {
1363 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1364 error (_("The 'while-stepping' command "
1365 "cannot be used for fast tracepoint"));
7b572efb
TT
1366 else if (b->type == bp_static_tracepoint
1367 || b->type == bp_static_marker_tracepoint)
3e43a32a
MS
1368 error (_("The 'while-stepping' command "
1369 "cannot be used for static tracepoint"));
a7bdde9e
VP
1370
1371 if (while_stepping)
3e43a32a
MS
1372 error (_("The 'while-stepping' command "
1373 "can be used only once"));
a7bdde9e
VP
1374 else
1375 while_stepping = c;
1376 }
c9a6ce02
PA
1377
1378 validate_actionline (c->line, b);
a7bdde9e
VP
1379 }
1380 if (while_stepping)
1381 {
1382 struct command_line *c2;
1383
12973681
TT
1384 gdb_assert (while_stepping->body_list_1 == nullptr);
1385 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1386 for (; c2; c2 = c2->next)
1387 {
a7bdde9e
VP
1388 if (c2->control_type == while_stepping_control)
1389 error (_("The 'while-stepping' command cannot be nested"));
1390 }
1391 }
1392 }
1393 else
1394 {
1395 check_no_tracepoint_commands (commands);
1396 }
95a42b64
TT
1397}
1398
0fb4aa4b
PA
1399/* Return a vector of all the static tracepoints set at ADDR. The
1400 caller is responsible for releasing the vector. */
1401
f51e0e20 1402std::vector<breakpoint *>
0fb4aa4b
PA
1403static_tracepoints_here (CORE_ADDR addr)
1404{
f51e0e20 1405 std::vector<breakpoint *> found;
0fb4aa4b 1406
43892fdf 1407 for (breakpoint *b : all_breakpoints ())
7b572efb
TT
1408 if (b->type == bp_static_tracepoint
1409 || b->type == bp_static_marker_tracepoint)
0fb4aa4b 1410 {
40cb8ca5 1411 for (bp_location *loc : b->locations ())
0fb4aa4b 1412 if (loc->address == addr)
f51e0e20 1413 found.push_back (b);
0fb4aa4b
PA
1414 }
1415
1416 return found;
1417}
1418
95a42b64 1419/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1420 validate that only allowed commands are included. */
95a42b64
TT
1421
1422void
4a64f543 1423breakpoint_set_commands (struct breakpoint *b,
12973681 1424 counted_command_line &&commands)
95a42b64 1425{
93921405 1426 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1427
d1b0a7bf 1428 b->commands = std::move (commands);
76727919 1429 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1430}
1431
45a43567
TT
1432/* Set the internal `silent' flag on the breakpoint. Note that this
1433 is not the same as the "silent" that may appear in the breakpoint's
1434 commands. */
1435
1436void
1437breakpoint_set_silent (struct breakpoint *b, int silent)
1438{
1439 int old_silent = b->silent;
1440
1441 b->silent = silent;
1442 if (old_silent != silent)
76727919 1443 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1444}
1445
1446/* Set the thread for this breakpoint. If THREAD is -1, make the
1447 breakpoint work for any thread. */
1448
1449void
1450breakpoint_set_thread (struct breakpoint *b, int thread)
1451{
1452 int old_thread = b->thread;
1453
1454 b->thread = thread;
1455 if (old_thread != thread)
76727919 1456 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1457}
1458
1459/* Set the task for this breakpoint. If TASK is 0, make the
1460 breakpoint work for any task. */
1461
1462void
1463breakpoint_set_task (struct breakpoint *b, int task)
1464{
1465 int old_task = b->task;
1466
1467 b->task = task;
1468 if (old_task != task)
76727919 1469 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1470}
1471
95a42b64 1472static void
896b6bda 1473commands_command_1 (const char *arg, int from_tty,
4a64f543 1474 struct command_line *control)
95a42b64 1475{
d1b0a7bf 1476 counted_command_line cmd;
999700cd
PW
1477 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1478 NULL after the call to read_command_lines if the user provides an empty
1479 list of command by just typing "end". */
1480 bool cmd_read = false;
95a42b64 1481
896b6bda
PA
1482 std::string new_arg;
1483
95a42b64
TT
1484 if (arg == NULL || !*arg)
1485 {
da1df1db 1486 /* Argument not explicitly given. Synthesize it. */
86b17b60 1487 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1488 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1489 breakpoint_count);
95a42b64 1490 else if (breakpoint_count > 0)
896b6bda 1491 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1492 }
da1df1db
TBA
1493 else
1494 {
1495 /* Create a copy of ARG. This is needed because the "commands"
1496 command may be coming from a script. In that case, the read
1497 line buffer is going to be overwritten in the lambda of
1498 'map_breakpoint_numbers' below when reading the next line
1499 before we are are done parsing the breakpoint numbers. */
1500 new_arg = arg;
1501 }
1502 arg = new_arg.c_str ();
48649e1b
TT
1503
1504 map_breakpoint_numbers
1505 (arg, [&] (breakpoint *b)
1506 {
999700cd 1507 if (!cmd_read)
48649e1b 1508 {
999700cd 1509 gdb_assert (cmd == NULL);
48649e1b 1510 if (control != NULL)
12973681 1511 cmd = control->body_list_0;
48649e1b
TT
1512 else
1513 {
81b1e71c
TT
1514 std::string str
1515 = string_printf (_("Type commands for breakpoint(s) "
1516 "%s, one per line."),
1517 arg);
48649e1b 1518
60b3cef2
TT
1519 auto do_validate = [=] (const char *line)
1520 {
1521 validate_actionline (line, b);
1522 };
1523 gdb::function_view<void (const char *)> validator;
1524 if (is_tracepoint (b))
1525 validator = do_validate;
1526
1527 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1528 }
999700cd 1529 cmd_read = true;
48649e1b
TT
1530 }
1531
1532 /* If a breakpoint was on the list more than once, we don't need to
1533 do anything. */
1534 if (b->commands != cmd)
1535 {
d1b0a7bf 1536 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1537 b->commands = cmd;
76727919 1538 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1539 }
1540 });
95a42b64
TT
1541}
1542
1543static void
0b39b52e 1544commands_command (const char *arg, int from_tty)
95a42b64
TT
1545{
1546 commands_command_1 (arg, from_tty, NULL);
c906108c 1547}
40c03ae8
EZ
1548
1549/* Like commands_command, but instead of reading the commands from
1550 input stream, takes them from an already parsed command structure.
1551
1552 This is used by cli-script.c to DTRT with breakpoint commands
1553 that are part of if and while bodies. */
1554enum command_control_type
896b6bda 1555commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1556{
95a42b64
TT
1557 commands_command_1 (arg, 0, cmd);
1558 return simple_control;
40c03ae8 1559}
876fa593
JK
1560
1561/* Return non-zero if BL->TARGET_INFO contains valid information. */
1562
1563static int
1564bp_location_has_shadow (struct bp_location *bl)
1565{
1566 if (bl->loc_type != bp_loc_software_breakpoint)
1567 return 0;
1568 if (!bl->inserted)
1569 return 0;
1570 if (bl->target_info.shadow_len == 0)
e5dd4106 1571 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1572 return 0;
1573 return 1;
1574}
1575
9d497a19
PA
1576/* Update BUF, which is LEN bytes read from the target address
1577 MEMADDR, by replacing a memory breakpoint with its shadowed
1578 contents.
1579
1580 If READBUF is not NULL, this buffer must not overlap with the of
1581 the breakpoint location's shadow_contents buffer. Otherwise, a
1582 failed assertion internal error will be raised. */
1583
1584static void
1585one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1586 const gdb_byte *writebuf_org,
1587 ULONGEST memaddr, LONGEST len,
1588 struct bp_target_info *target_info,
1589 struct gdbarch *gdbarch)
1590{
1591 /* Now do full processing of the found relevant range of elements. */
1592 CORE_ADDR bp_addr = 0;
1593 int bp_size = 0;
1594 int bptoffset = 0;
1595
1596 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1597 current_program_space->aspace, 0))
1598 {
1599 /* The breakpoint is inserted in a different address space. */
1600 return;
1601 }
1602
1603 /* Addresses and length of the part of the breakpoint that
1604 we need to copy. */
1605 bp_addr = target_info->placed_address;
1606 bp_size = target_info->shadow_len;
1607
1608 if (bp_addr + bp_size <= memaddr)
1609 {
1610 /* The breakpoint is entirely before the chunk of memory we are
1611 reading. */
1612 return;
1613 }
1614
1615 if (bp_addr >= memaddr + len)
1616 {
1617 /* The breakpoint is entirely after the chunk of memory we are
1618 reading. */
1619 return;
1620 }
1621
1622 /* Offset within shadow_contents. */
1623 if (bp_addr < memaddr)
1624 {
1625 /* Only copy the second part of the breakpoint. */
1626 bp_size -= memaddr - bp_addr;
1627 bptoffset = memaddr - bp_addr;
1628 bp_addr = memaddr;
1629 }
1630
1631 if (bp_addr + bp_size > memaddr + len)
1632 {
1633 /* Only copy the first part of the breakpoint. */
1634 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1635 }
1636
1637 if (readbuf != NULL)
1638 {
1639 /* Verify that the readbuf buffer does not overlap with the
1640 shadow_contents buffer. */
1641 gdb_assert (target_info->shadow_contents >= readbuf + len
1642 || readbuf >= (target_info->shadow_contents
1643 + target_info->shadow_len));
1644
1645 /* Update the read buffer with this inserted breakpoint's
1646 shadow. */
1647 memcpy (readbuf + bp_addr - memaddr,
1648 target_info->shadow_contents + bptoffset, bp_size);
1649 }
1650 else
1651 {
1652 const unsigned char *bp;
0d5ed153
MR
1653 CORE_ADDR addr = target_info->reqstd_address;
1654 int placed_size;
9d497a19
PA
1655
1656 /* Update the shadow with what we want to write to memory. */
1657 memcpy (target_info->shadow_contents + bptoffset,
1658 writebuf_org + bp_addr - memaddr, bp_size);
1659
1660 /* Determine appropriate breakpoint contents and size for this
1661 address. */
0d5ed153 1662 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1663
1664 /* Update the final write buffer with this inserted
1665 breakpoint's INSN. */
1666 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1667 }
1668}
1669
8defab1a 1670/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1671 by replacing any memory breakpoints with their shadowed contents.
1672
35c63cd8
JB
1673 If READBUF is not NULL, this buffer must not overlap with any of
1674 the breakpoint location's shadow_contents buffers. Otherwise,
1675 a failed assertion internal error will be raised.
1676
876fa593 1677 The range of shadowed area by each bp_location is:
f5336ca5
PA
1678 bl->address - bp_locations_placed_address_before_address_max
1679 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1680 The range we were requested to resolve shadows for is:
1681 memaddr ... memaddr + len
1682 Thus the safe cutoff boundaries for performance optimization are
35df4500 1683 memaddr + len <= (bl->address
f5336ca5 1684 - bp_locations_placed_address_before_address_max)
876fa593 1685 and:
f5336ca5 1686 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1687
8defab1a 1688void
f0ba3972
PA
1689breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1690 const gdb_byte *writebuf_org,
1691 ULONGEST memaddr, LONGEST len)
c906108c 1692{
4a64f543
MS
1693 /* Left boundary, right boundary and median element of our binary
1694 search. */
876fa593
JK
1695 unsigned bc_l, bc_r, bc;
1696
4a64f543
MS
1697 /* Find BC_L which is a leftmost element which may affect BUF
1698 content. It is safe to report lower value but a failure to
1699 report higher one. */
876fa593
JK
1700
1701 bc_l = 0;
5d51cd5d 1702 bc_r = bp_locations.size ();
876fa593
JK
1703 while (bc_l + 1 < bc_r)
1704 {
35df4500 1705 struct bp_location *bl;
876fa593
JK
1706
1707 bc = (bc_l + bc_r) / 2;
f5336ca5 1708 bl = bp_locations[bc];
876fa593 1709
4a64f543
MS
1710 /* Check first BL->ADDRESS will not overflow due to the added
1711 constant. Then advance the left boundary only if we are sure
1712 the BC element can in no way affect the BUF content (MEMADDR
1713 to MEMADDR + LEN range).
876fa593 1714
f5336ca5 1715 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1716 offset so that we cannot miss a breakpoint with its shadow
1717 range tail still reaching MEMADDR. */
c5aa993b 1718
f5336ca5 1719 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1720 >= bl->address)
f5336ca5 1721 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1722 <= memaddr))
876fa593
JK
1723 bc_l = bc;
1724 else
1725 bc_r = bc;
1726 }
1727
128070bb
PA
1728 /* Due to the binary search above, we need to make sure we pick the
1729 first location that's at BC_L's address. E.g., if there are
1730 multiple locations at the same address, BC_L may end up pointing
1731 at a duplicate location, and miss the "master"/"inserted"
1732 location. Say, given locations L1, L2 and L3 at addresses A and
1733 B:
1734
1735 L1@A, L2@A, L3@B, ...
1736
1737 BC_L could end up pointing at location L2, while the "master"
1738 location could be L1. Since the `loc->inserted' flag is only set
1739 on "master" locations, we'd forget to restore the shadow of L1
1740 and L2. */
1741 while (bc_l > 0
f5336ca5 1742 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1743 bc_l--;
1744
876fa593
JK
1745 /* Now do full processing of the found relevant range of elements. */
1746
5d51cd5d 1747 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1748 {
1749 struct bp_location *bl = bp_locations[bc];
876fa593 1750
01add95b
SM
1751 /* bp_location array has BL->OWNER always non-NULL. */
1752 if (bl->owner->type == bp_none)
1753 warning (_("reading through apparently deleted breakpoint #%d?"),
1754 bl->owner->number);
ffce0d52 1755
01add95b
SM
1756 /* Performance optimization: any further element can no longer affect BUF
1757 content. */
876fa593 1758
01add95b
SM
1759 if (bl->address >= bp_locations_placed_address_before_address_max
1760 && (memaddr + len
1761 <= (bl->address
1762 - bp_locations_placed_address_before_address_max)))
1763 break;
876fa593 1764
01add95b
SM
1765 if (!bp_location_has_shadow (bl))
1766 continue;
6c95b8df 1767
01add95b
SM
1768 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1769 memaddr, len, &bl->target_info, bl->gdbarch);
1770 }
c906108c 1771}
9d497a19 1772
f2478a7e 1773/* See breakpoint.h. */
b775012e 1774
f2478a7e 1775bool
b775012e
LM
1776is_breakpoint (const struct breakpoint *bpt)
1777{
1778 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1779 || bpt->type == bp_hardware_breakpoint
1780 || bpt->type == bp_dprintf);
b775012e
LM
1781}
1782
60e1c644
PA
1783/* Return true if BPT is of any hardware watchpoint kind. */
1784
f2478a7e 1785static bool
d77f58be 1786is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1787{
1788 return (bpt->type == bp_hardware_watchpoint
1789 || bpt->type == bp_read_watchpoint
1790 || bpt->type == bp_access_watchpoint);
1791}
7270d8f2 1792
f2478a7e 1793/* See breakpoint.h. */
60e1c644 1794
f2478a7e 1795bool
d77f58be 1796is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1797{
1798 return (is_hardware_watchpoint (bpt)
1799 || bpt->type == bp_watchpoint);
1800}
1801
3a5c3e22
PA
1802/* Returns true if the current thread and its running state are safe
1803 to evaluate or update watchpoint B. Watchpoints on local
1804 expressions need to be evaluated in the context of the thread that
1805 was current when the watchpoint was created, and, that thread needs
1806 to be stopped to be able to select the correct frame context.
1807 Watchpoints on global expressions can be evaluated on any thread,
1808 and in any state. It is presently left to the target allowing
1809 memory accesses when threads are running. */
f6bc2008
PA
1810
1811static int
3a5c3e22 1812watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1813{
c1fc2657 1814 return (b->pspace == current_program_space
d7e15655
TT
1815 && (b->watchpoint_thread == null_ptid
1816 || (inferior_ptid == b->watchpoint_thread
611841bb 1817 && !inferior_thread ()->executing ())));
f6bc2008
PA
1818}
1819
d0fb5eae
JK
1820/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1821 associated bp_watchpoint_scope breakpoint. */
1822
1823static void
3a5c3e22 1824watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1825{
c1fc2657 1826 if (w->related_breakpoint != w)
d0fb5eae 1827 {
c1fc2657
SM
1828 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1829 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1830 w->related_breakpoint->disposition = disp_del_at_next_stop;
1831 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1832 w->related_breakpoint = w;
d0fb5eae 1833 }
c1fc2657 1834 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1835}
1836
bb9d5f81
PP
1837/* Extract a bitfield value from value VAL using the bit parameters contained in
1838 watchpoint W. */
1839
1840static struct value *
1841extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1842{
1843 struct value *bit_val;
1844
1845 if (val == NULL)
1846 return NULL;
1847
1848 bit_val = allocate_value (value_type (val));
1849
1850 unpack_value_bitfield (bit_val,
1851 w->val_bitpos,
1852 w->val_bitsize,
50888e42 1853 value_contents_for_printing (val).data (),
bb9d5f81
PP
1854 value_offset (val),
1855 val);
1856
1857 return bit_val;
1858}
1859
6e14e441
PA
1860/* Allocate a dummy location and add it to B. This is required
1861 because bpstat_stop_status requires a location to be able to report
1862 stops. */
c6d81124
PA
1863
1864static void
6e14e441
PA
1865add_dummy_location (struct breakpoint *b,
1866 struct program_space *pspace)
c6d81124 1867{
6e14e441 1868 gdb_assert (b->loc == NULL);
c6d81124 1869
6e14e441 1870 b->loc = new bp_location (b, bp_loc_other);
c6d81124 1871 b->loc->pspace = pspace;
c6d81124
PA
1872}
1873
567e1b4e
JB
1874/* Assuming that B is a watchpoint:
1875 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1876 - Evaluate expression and store the result in B->val
567e1b4e
JB
1877 - Evaluate the condition if there is one, and store the result
1878 in b->loc->cond.
a5606eee
VP
1879 - Update the list of values that must be watched in B->loc.
1880
4a64f543
MS
1881 If the watchpoint disposition is disp_del_at_next_stop, then do
1882 nothing. If this is local watchpoint that is out of scope, delete
1883 it.
1884
1885 Even with `set breakpoint always-inserted on' the watchpoints are
1886 removed + inserted on each stop here. Normal breakpoints must
1887 never be removed because they might be missed by a running thread
1888 when debugging in non-stop mode. On the other hand, hardware
1889 watchpoints (is_hardware_watchpoint; processed here) are specific
1890 to each LWP since they are stored in each LWP's hardware debug
1891 registers. Therefore, such LWP must be stopped first in order to
1892 be able to modify its hardware watchpoints.
1893
1894 Hardware watchpoints must be reset exactly once after being
1895 presented to the user. It cannot be done sooner, because it would
1896 reset the data used to present the watchpoint hit to the user. And
1897 it must not be done later because it could display the same single
1898 watchpoint hit during multiple GDB stops. Note that the latter is
1899 relevant only to the hardware watchpoint types bp_read_watchpoint
1900 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1901 not user-visible - its hit is suppressed if the memory content has
1902 not changed.
1903
1904 The following constraints influence the location where we can reset
1905 hardware watchpoints:
1906
1907 * target_stopped_by_watchpoint and target_stopped_data_address are
1908 called several times when GDB stops.
1909
1910 [linux]
1911 * Multiple hardware watchpoints can be hit at the same time,
1912 causing GDB to stop. GDB only presents one hardware watchpoint
1913 hit at a time as the reason for stopping, and all the other hits
1914 are presented later, one after the other, each time the user
1915 requests the execution to be resumed. Execution is not resumed
1916 for the threads still having pending hit event stored in
1917 LWP_INFO->STATUS. While the watchpoint is already removed from
1918 the inferior on the first stop the thread hit event is kept being
1919 reported from its cached value by linux_nat_stopped_data_address
1920 until the real thread resume happens after the watchpoint gets
1921 presented and thus its LWP_INFO->STATUS gets reset.
1922
1923 Therefore the hardware watchpoint hit can get safely reset on the
1924 watchpoint removal from inferior. */
a79d3c27 1925
b40ce68a 1926static void
3a5c3e22 1927update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1928{
a5606eee 1929 int within_current_scope;
a5606eee 1930 struct frame_id saved_frame_id;
66076460 1931 int frame_saved;
a5606eee 1932
f6bc2008
PA
1933 /* If this is a local watchpoint, we only want to check if the
1934 watchpoint frame is in scope if the current thread is the thread
1935 that was used to create the watchpoint. */
1936 if (!watchpoint_in_thread_scope (b))
1937 return;
1938
c1fc2657 1939 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1940 return;
1941
66076460 1942 frame_saved = 0;
a5606eee
VP
1943
1944 /* Determine if the watchpoint is within scope. */
1945 if (b->exp_valid_block == NULL)
1946 within_current_scope = 1;
1947 else
1948 {
b5db5dfc
UW
1949 struct frame_info *fi = get_current_frame ();
1950 struct gdbarch *frame_arch = get_frame_arch (fi);
1951 CORE_ADDR frame_pc = get_frame_pc (fi);
1952
c9cf6e20
MG
1953 /* If we're at a point where the stack has been destroyed
1954 (e.g. in a function epilogue), unwinding may not work
1955 properly. Do not attempt to recreate locations at this
b5db5dfc 1956 point. See similar comments in watchpoint_check. */
c9cf6e20 1957 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1958 return;
66076460
DJ
1959
1960 /* Save the current frame's ID so we can restore it after
dda83cd7 1961 evaluating the watchpoint expression on its own frame. */
66076460 1962 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1963 took a frame parameter, so that we didn't have to change the
1964 selected frame. */
66076460
DJ
1965 frame_saved = 1;
1966 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1967
a5606eee
VP
1968 fi = frame_find_by_id (b->watchpoint_frame);
1969 within_current_scope = (fi != NULL);
1970 if (within_current_scope)
1971 select_frame (fi);
1972 }
1973
b5db5dfc
UW
1974 /* We don't free locations. They are stored in the bp_location array
1975 and update_global_location_list will eventually delete them and
1976 remove breakpoints if needed. */
c1fc2657 1977 b->loc = NULL;
b5db5dfc 1978
a5606eee
VP
1979 if (within_current_scope && reparse)
1980 {
bbc13ae3 1981 const char *s;
d63d0675 1982
4d01a485 1983 b->exp.reset ();
a4c50be3
TT
1984 s = (b->exp_string_reparse
1985 ? b->exp_string_reparse.get ()
1986 : b->exp_string.get ());
1bb9788d 1987 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1988 /* If the meaning of expression itself changed, the old value is
1989 no longer relevant. We don't want to report a watchpoint hit
1990 to the user when the old value and the new value may actually
1991 be completely different objects. */
fa4727a6 1992 b->val = NULL;
4c1d86d9 1993 b->val_valid = false;
60e1c644
PA
1994
1995 /* Note that unlike with breakpoints, the watchpoint's condition
1996 expression is stored in the breakpoint object, not in the
1997 locations (re)created below. */
c1fc2657 1998 if (b->cond_string != NULL)
60e1c644 1999 {
4d01a485 2000 b->cond_exp.reset ();
60e1c644 2001
6f781ee3 2002 s = b->cond_string.get ();
1bb9788d 2003 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 2004 }
a5606eee 2005 }
a5606eee
VP
2006
2007 /* If we failed to parse the expression, for example because
2008 it refers to a global variable in a not-yet-loaded shared library,
2009 don't try to insert watchpoint. We don't automatically delete
2010 such watchpoint, though, since failure to parse expression
2011 is different from out-of-scope watchpoint. */
55f6301a 2012 if (!target_has_execution ())
2d134ed3
PA
2013 {
2014 /* Without execution, memory can't change. No use to try and
2015 set watchpoint locations. The watchpoint will be reset when
2016 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
2017 if (!can_use_hw_watchpoints)
2018 {
c47614fe 2019 if (b->works_in_software_mode ())
c1fc2657 2020 b->type = bp_watchpoint;
e8369a73 2021 else
638aa5a1
AB
2022 error (_("Can't set read/access watchpoint when "
2023 "hardware watchpoints are disabled."));
e8369a73 2024 }
2d134ed3
PA
2025 }
2026 else if (within_current_scope && b->exp)
a5606eee 2027 {
a6535de1 2028 std::vector<value_ref_ptr> val_chain;
8d49165d 2029 struct value *v, *result;
2d134ed3 2030 struct program_space *frame_pspace;
a5606eee 2031
1eaebe02 2032 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 2033 &val_chain, false);
a5606eee 2034
a5606eee
VP
2035 /* Avoid setting b->val if it's already set. The meaning of
2036 b->val is 'the last value' user saw, and we should update
2037 it only if we reported that last value to user. As it
9c06b0b4
TJB
2038 happens, the code that reports it updates b->val directly.
2039 We don't keep track of the memory value for masked
2040 watchpoints. */
c1fc2657 2041 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 2042 {
bb9d5f81 2043 if (b->val_bitsize != 0)
850645cf
TT
2044 v = extract_bitfield_from_watchpoint_value (b, v);
2045 b->val = release_value (v);
4c1d86d9 2046 b->val_valid = true;
fa4727a6 2047 }
a5606eee 2048
2d134ed3
PA
2049 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2050
a5606eee 2051 /* Look at each value on the value chain. */
a6535de1
TT
2052 gdb_assert (!val_chain.empty ());
2053 for (const value_ref_ptr &iter : val_chain)
a5606eee 2054 {
a6535de1
TT
2055 v = iter.get ();
2056
a5606eee
VP
2057 /* If it's a memory location, and GDB actually needed
2058 its contents to evaluate the expression, then we
fa4727a6
DJ
2059 must watch it. If the first value returned is
2060 still lazy, that means an error occurred reading it;
2061 watch it anyway in case it becomes readable. */
a5606eee 2062 if (VALUE_LVAL (v) == lval_memory
a6535de1 2063 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
2064 {
2065 struct type *vtype = check_typedef (value_type (v));
7270d8f2 2066
a5606eee
VP
2067 /* We only watch structs and arrays if user asked
2068 for it explicitly, never if they just happen to
2069 appear in the middle of some value chain. */
fa4727a6 2070 if (v == result
78134374
SM
2071 || (vtype->code () != TYPE_CODE_STRUCT
2072 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
2073 {
2074 CORE_ADDR addr;
f486487f 2075 enum target_hw_bp_type type;
a5606eee 2076 struct bp_location *loc, **tmp;
bb9d5f81
PP
2077 int bitpos = 0, bitsize = 0;
2078
2079 if (value_bitsize (v) != 0)
2080 {
2081 /* Extract the bit parameters out from the bitfield
2082 sub-expression. */
2083 bitpos = value_bitpos (v);
2084 bitsize = value_bitsize (v);
2085 }
2086 else if (v == result && b->val_bitsize != 0)
2087 {
2088 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2089 lvalue whose bit parameters are saved in the fields
2090 VAL_BITPOS and VAL_BITSIZE. */
2091 bitpos = b->val_bitpos;
2092 bitsize = b->val_bitsize;
2093 }
a5606eee 2094
42ae5230 2095 addr = value_address (v);
bb9d5f81
PP
2096 if (bitsize != 0)
2097 {
2098 /* Skip the bytes that don't contain the bitfield. */
2099 addr += bitpos / 8;
2100 }
2101
a5606eee 2102 type = hw_write;
c1fc2657 2103 if (b->type == bp_read_watchpoint)
a5606eee 2104 type = hw_read;
c1fc2657 2105 else if (b->type == bp_access_watchpoint)
a5606eee 2106 type = hw_access;
3a5c3e22 2107
ee89d0a4 2108 loc = b->allocate_location ();
c1fc2657 2109 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2110 ;
2111 *tmp = loc;
8ee511af 2112 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
2113
2114 loc->pspace = frame_pspace;
f17d9474 2115 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
2116
2117 if (bitsize != 0)
2118 {
2119 /* Just cover the bytes that make up the bitfield. */
2120 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2121 }
2122 else
df86565b 2123 loc->length = value_type (v)->length ();
bb9d5f81 2124
a5606eee
VP
2125 loc->watchpoint_type = type;
2126 }
2127 }
9fa40276
TJB
2128 }
2129
2130 /* Change the type of breakpoint between hardware assisted or
2131 an ordinary watchpoint depending on the hardware support
2132 and free hardware slots. REPARSE is set when the inferior
2133 is started. */
a9634178 2134 if (reparse)
9fa40276 2135 {
e09342b5 2136 int reg_cnt;
9fa40276 2137 enum bp_loc_type loc_type;
a5606eee 2138
a9634178 2139 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2140
2141 if (reg_cnt)
9fa40276
TJB
2142 {
2143 int i, target_resources_ok, other_type_used;
a1398e0c 2144 enum bptype type;
9fa40276 2145
a9634178
TJB
2146 /* Use an exact watchpoint when there's only one memory region to be
2147 watched, and only one debug register is needed to watch it. */
2148 b->exact = target_exact_watchpoints && reg_cnt == 1;
2149
9fa40276 2150 /* We need to determine how many resources are already
e09342b5
TJB
2151 used for all other hardware watchpoints plus this one
2152 to see if we still have enough resources to also fit
a1398e0c
PA
2153 this watchpoint in as well. */
2154
2155 /* If this is a software watchpoint, we try to turn it
2156 to a hardware one -- count resources as if B was of
2157 hardware watchpoint type. */
c1fc2657 2158 type = b->type;
a1398e0c
PA
2159 if (type == bp_watchpoint)
2160 type = bp_hardware_watchpoint;
2161
2162 /* This watchpoint may or may not have been placed on
2163 the list yet at this point (it won't be in the list
2164 if we're trying to create it for the first time,
2165 through watch_command), so always account for it
2166 manually. */
2167
2168 /* Count resources used by all watchpoints except B. */
c1fc2657 2169 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2170
2171 /* Add in the resources needed for B. */
c1fc2657 2172 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2173
2174 target_resources_ok
2175 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2176 if (target_resources_ok <= 0)
a9634178 2177 {
5a61e176 2178 bool sw_mode = b->works_in_software_mode ();
9c06b0b4
TJB
2179
2180 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2181 error (_("Target does not support this type of "
2182 "hardware watchpoint."));
9c06b0b4
TJB
2183 else if (target_resources_ok < 0 && !sw_mode)
2184 error (_("There are not enough available hardware "
2185 "resources for this watchpoint."));
a1398e0c
PA
2186
2187 /* Downgrade to software watchpoint. */
c1fc2657 2188 b->type = bp_watchpoint;
a1398e0c
PA
2189 }
2190 else
2191 {
2192 /* If this was a software watchpoint, we've just
2193 found we have enough resources to turn it to a
2194 hardware watchpoint. Otherwise, this is a
2195 nop. */
c1fc2657 2196 b->type = type;
a9634178 2197 }
9fa40276 2198 }
c47614fe 2199 else if (!b->works_in_software_mode ())
638aa5a1
AB
2200 {
2201 if (!can_use_hw_watchpoints)
2202 error (_("Can't set read/access watchpoint when "
2203 "hardware watchpoints are disabled."));
2204 else
2205 error (_("Expression cannot be implemented with "
2206 "read/access watchpoint."));
2207 }
9fa40276 2208 else
c1fc2657 2209 b->type = bp_watchpoint;
9fa40276 2210
6e14e441 2211 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
9fa40276 2212 : bp_loc_hardware_watchpoint);
40cb8ca5 2213 for (bp_location *bl : b->locations ())
9fa40276
TJB
2214 bl->loc_type = loc_type;
2215 }
2216
c7437ca6
PA
2217 /* If a software watchpoint is not watching any memory, then the
2218 above left it without any location set up. But,
2219 bpstat_stop_status requires a location to be able to report
2220 stops, so make sure there's at least a dummy one. */
c1fc2657 2221 if (b->type == bp_watchpoint && b->loc == NULL)
6e14e441 2222 add_dummy_location (b, frame_pspace);
a5606eee
VP
2223 }
2224 else if (!within_current_scope)
7270d8f2 2225 {
6cb06a8c 2226 gdb_printf (_("\
ac74f770
MS
2227Watchpoint %d deleted because the program has left the block\n\
2228in which its expression is valid.\n"),
6cb06a8c 2229 b->number);
d0fb5eae 2230 watchpoint_del_at_next_stop (b);
7270d8f2 2231 }
a5606eee
VP
2232
2233 /* Restore the selected frame. */
66076460
DJ
2234 if (frame_saved)
2235 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2236}
2237
a5606eee 2238
74960c60 2239/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2240 inserted in the inferior. We don't differentiate the type of BL's owner
2241 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2242 breakpoint_ops is not defined, because in insert_bp_location,
2243 tracepoint's insert_location will not be called. */
74960c60 2244static int
35df4500 2245should_be_inserted (struct bp_location *bl)
74960c60 2246{
35df4500 2247 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2248 return 0;
2249
35df4500 2250 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2251 return 0;
2252
b5fa468f
TBA
2253 if (!bl->enabled || bl->disabled_by_cond
2254 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2255 return 0;
2256
f8eba3c6
TT
2257 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2258 return 0;
2259
56710373
PA
2260 /* This is set for example, when we're attached to the parent of a
2261 vfork, and have detached from the child. The child is running
2262 free, and we expect it to do an exec or exit, at which point the
2263 OS makes the parent schedulable again (and the target reports
2264 that the vfork is done). Until the child is done with the shared
2265 memory region, do not insert breakpoints in the parent, otherwise
2266 the child could still trip on the parent's breakpoints. Since
2267 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2268 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2269 return 0;
2270
31e77af2 2271 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2272 location, except if the breakpoint is a single-step breakpoint,
2273 and the breakpoint's thread is the thread which is stepping past
2274 a breakpoint. */
31e77af2
PA
2275 if ((bl->loc_type == bp_loc_software_breakpoint
2276 || bl->loc_type == bp_loc_hardware_breakpoint)
2277 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2278 bl->address)
2279 /* The single-step breakpoint may be inserted at the location
2280 we're trying to step if the instruction branches to itself.
2281 However, the instruction won't be executed at all and it may
2282 break the semantics of the instruction, for example, the
2283 instruction is a conditional branch or updates some flags.
2284 We can't fix it unless GDB is able to emulate the instruction
2285 or switch to displaced stepping. */
2286 && !(bl->owner->type == bp_single_step
2287 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2288 {
1eb8556f
SM
2289 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2290 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2291 return 0;
2292 }
31e77af2 2293
963f9c80
PA
2294 /* Don't insert watchpoints if we're trying to step past the
2295 instruction that triggered one. */
2296 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2297 && stepping_past_nonsteppable_watchpoint ())
2298 {
1eb8556f 2299 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2300 "skipping watchpoint at %s:%d",
1eb8556f 2301 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2302 return 0;
2303 }
2304
74960c60
VP
2305 return 1;
2306}
2307
934709f0
PW
2308/* Same as should_be_inserted but does the check assuming
2309 that the location is not duplicated. */
2310
2311static int
2312unduplicated_should_be_inserted (struct bp_location *bl)
2313{
2314 int result;
2315 const int save_duplicate = bl->duplicate;
2316
2317 bl->duplicate = 0;
2318 result = should_be_inserted (bl);
2319 bl->duplicate = save_duplicate;
2320 return result;
2321}
2322
b775012e
LM
2323/* Parses a conditional described by an expression COND into an
2324 agent expression bytecode suitable for evaluation
2325 by the bytecode interpreter. Return NULL if there was
2326 any error during parsing. */
2327
833177a4 2328static agent_expr_up
b775012e
LM
2329parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2330{
833177a4 2331 if (cond == NULL)
b775012e
LM
2332 return NULL;
2333
833177a4
PA
2334 agent_expr_up aexpr;
2335
b775012e
LM
2336 /* We don't want to stop processing, so catch any errors
2337 that may show up. */
a70b8144 2338 try
b775012e 2339 {
036e657b 2340 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2341 }
2342
230d2906 2343 catch (const gdb_exception_error &ex)
b775012e
LM
2344 {
2345 /* If we got here, it means the condition could not be parsed to a valid
2346 bytecode expression and thus can't be evaluated on the target's side.
2347 It's no use iterating through the conditions. */
b775012e
LM
2348 }
2349
2350 /* We have a valid agent expression. */
2351 return aexpr;
2352}
2353
2354/* Based on location BL, create a list of breakpoint conditions to be
2355 passed on to the target. If we have duplicated locations with different
2356 conditions, we will add such conditions to the list. The idea is that the
2357 target will evaluate the list of conditions and will only notify GDB when
2358 one of them is true. */
2359
2360static void
2361build_target_condition_list (struct bp_location *bl)
2362{
b775012e
LM
2363 int null_condition_or_parse_error = 0;
2364 int modified = bl->needs_update;
b775012e 2365
8b4f3082 2366 /* Release conditions left over from a previous insert. */
3cde5c42 2367 bl->target_info.conditions.clear ();
8b4f3082 2368
b775012e
LM
2369 /* This is only meaningful if the target is
2370 evaluating conditions and if the user has
2371 opted for condition evaluation on the target's
2372 side. */
2373 if (gdb_evaluates_breakpoint_condition_p ()
2374 || !target_supports_evaluation_of_breakpoint_conditions ())
2375 return;
2376
e0d9a270
SM
2377 auto loc_range = all_bp_locations_at_addr (bl->address);
2378
b775012e 2379 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2380 conditions or conditions that fail to parse to a valid agent
2381 expression bytecode. If any of these happen, then it's no use to
2382 send conditions to the target since this location will always
2383 trigger and generate a response back to GDB. Note we consider
2384 all locations at the same address irrespective of type, i.e.,
2385 even if the locations aren't considered duplicates (e.g.,
2386 software breakpoint and hardware breakpoint at the same
2387 address). */
e0d9a270 2388 for (bp_location *loc : loc_range)
b775012e 2389 {
b775012e
LM
2390 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2391 {
2392 if (modified)
2393 {
b775012e
LM
2394 /* Re-parse the conditions since something changed. In that
2395 case we already freed the condition bytecodes (see
2396 force_breakpoint_reinsertion). We just
2397 need to parse the condition to bytecodes again. */
833177a4
PA
2398 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2399 loc->cond.get ());
b775012e
LM
2400 }
2401
2402 /* If we have a NULL bytecode expression, it means something
2403 went wrong or we have a null condition expression. */
2404 if (!loc->cond_bytecode)
2405 {
2406 null_condition_or_parse_error = 1;
2407 break;
2408 }
2409 }
2410 }
2411
2412 /* If any of these happened, it means we will have to evaluate the conditions
2413 for the location's address on gdb's side. It is no use keeping bytecodes
2414 for all the other duplicate locations, thus we free all of them here.
2415
2416 This is so we have a finer control over which locations' conditions are
2417 being evaluated by GDB or the remote stub. */
2418 if (null_condition_or_parse_error)
2419 {
e0d9a270 2420 for (bp_location *loc : loc_range)
b775012e 2421 {
b775012e
LM
2422 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2423 {
2424 /* Only go as far as the first NULL bytecode is
2425 located. */
2426 if (!loc->cond_bytecode)
2427 return;
2428
833177a4 2429 loc->cond_bytecode.reset ();
b775012e
LM
2430 }
2431 }
2432 }
2433
7f32a4d5
PA
2434 /* No NULL conditions or failed bytecode generation. Build a
2435 condition list for this location's address. If we have software
2436 and hardware locations at the same address, they aren't
2437 considered duplicates, but we still marge all the conditions
2438 anyway, as it's simpler, and doesn't really make a practical
2439 difference. */
e0d9a270
SM
2440 for (bp_location *loc : loc_range)
2441 if (loc->cond
2442 && is_breakpoint (loc->owner)
2443 && loc->pspace->num == bl->pspace->num
2444 && loc->owner->enable_state == bp_enabled
2445 && loc->enabled
2446 && !loc->disabled_by_cond)
2447 {
2448 /* Add the condition to the vector. This will be used later
2449 to send the conditions to the target. */
2450 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2451 }
b775012e
LM
2452
2453 return;
2454}
2455
d3ce09f5
SS
2456/* Parses a command described by string CMD into an agent expression
2457 bytecode suitable for evaluation by the bytecode interpreter.
2458 Return NULL if there was any error during parsing. */
2459
833177a4 2460static agent_expr_up
d3ce09f5
SS
2461parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2462{
bbc13ae3
KS
2463 const char *cmdrest;
2464 const char *format_start, *format_end;
d3ce09f5
SS
2465 struct gdbarch *gdbarch = get_current_arch ();
2466
833177a4 2467 if (cmd == NULL)
d3ce09f5
SS
2468 return NULL;
2469
2470 cmdrest = cmd;
2471
2472 if (*cmdrest == ',')
2473 ++cmdrest;
f1735a53 2474 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2475
2476 if (*cmdrest++ != '"')
2477 error (_("No format string following the location"));
2478
2479 format_start = cmdrest;
2480
8e481c3b 2481 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2482
2483 format_end = cmdrest;
2484
2485 if (*cmdrest++ != '"')
2486 error (_("Bad format string, non-terminated '\"'."));
2487
f1735a53 2488 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2489
2490 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2491 error (_("Invalid argument syntax"));
2492
2493 if (*cmdrest == ',')
2494 cmdrest++;
f1735a53 2495 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2496
2497 /* For each argument, make an expression. */
2498
8e481c3b 2499 std::vector<struct expression *> argvec;
d3ce09f5
SS
2500 while (*cmdrest != '\0')
2501 {
bbc13ae3 2502 const char *cmd1;
d3ce09f5
SS
2503
2504 cmd1 = cmdrest;
4d01a485 2505 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2506 argvec.push_back (expr.release ());
d3ce09f5
SS
2507 cmdrest = cmd1;
2508 if (*cmdrest == ',')
2509 ++cmdrest;
2510 }
2511
833177a4
PA
2512 agent_expr_up aexpr;
2513
d3ce09f5
SS
2514 /* We don't want to stop processing, so catch any errors
2515 that may show up. */
a70b8144 2516 try
d3ce09f5 2517 {
036e657b
JB
2518 aexpr = gen_printf (scope, gdbarch, 0, 0,
2519 format_start, format_end - format_start,
8e481c3b 2520 argvec.size (), argvec.data ());
d3ce09f5 2521 }
230d2906 2522 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2523 {
2524 /* If we got here, it means the command could not be parsed to a valid
2525 bytecode expression and thus can't be evaluated on the target's side.
2526 It's no use iterating through the other commands. */
d3ce09f5 2527 }
492d29ea 2528
d3ce09f5
SS
2529 /* We have a valid agent expression, return it. */
2530 return aexpr;
2531}
2532
2533/* Based on location BL, create a list of breakpoint commands to be
2534 passed on to the target. If we have duplicated locations with
2535 different commands, we will add any such to the list. */
2536
2537static void
2538build_target_command_list (struct bp_location *bl)
2539{
d3ce09f5
SS
2540 int null_command_or_parse_error = 0;
2541 int modified = bl->needs_update;
d3ce09f5 2542
3cde5c42
PA
2543 /* Clear commands left over from a previous insert. */
2544 bl->target_info.tcommands.clear ();
8b4f3082 2545
41fac0cf 2546 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2547 return;
2548
41fac0cf
PA
2549 /* For now, limit to agent-style dprintf breakpoints. */
2550 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2551 return;
2552
e0d9a270
SM
2553 auto loc_range = all_bp_locations_at_addr (bl->address);
2554
7f32a4d5
PA
2555 /* For now, if we have any location at the same address that isn't a
2556 dprintf, don't install the target-side commands, as that would
2557 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2558 control. */
e0d9a270
SM
2559 for (bp_location *loc : loc_range)
2560 if (is_breakpoint (loc->owner)
2561 && loc->pspace->num == bl->pspace->num
2562 && loc->owner->type != bp_dprintf)
2563 return;
41fac0cf 2564
d3ce09f5
SS
2565 /* Do a first pass to check for locations with no assigned
2566 conditions or conditions that fail to parse to a valid agent expression
2567 bytecode. If any of these happen, then it's no use to send conditions
2568 to the target since this location will always trigger and generate a
2569 response back to GDB. */
e0d9a270 2570 for (bp_location *loc : loc_range)
d3ce09f5 2571 {
d3ce09f5
SS
2572 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2573 {
2574 if (modified)
2575 {
d3ce09f5
SS
2576 /* Re-parse the commands since something changed. In that
2577 case we already freed the command bytecodes (see
2578 force_breakpoint_reinsertion). We just
2579 need to parse the command to bytecodes again. */
833177a4
PA
2580 loc->cmd_bytecode
2581 = parse_cmd_to_aexpr (bl->address,
6f781ee3 2582 loc->owner->extra_string.get ());
d3ce09f5
SS
2583 }
2584
2585 /* If we have a NULL bytecode expression, it means something
2586 went wrong or we have a null command expression. */
2587 if (!loc->cmd_bytecode)
2588 {
2589 null_command_or_parse_error = 1;
2590 break;
2591 }
2592 }
2593 }
2594
2595 /* If anything failed, then we're not doing target-side commands,
2596 and so clean up. */
2597 if (null_command_or_parse_error)
2598 {
e0d9a270
SM
2599 for (bp_location *loc : loc_range)
2600 if (is_breakpoint (loc->owner)
2601 && loc->pspace->num == bl->pspace->num)
2602 {
2603 /* Only go as far as the first NULL bytecode is
2604 located. */
2605 if (loc->cmd_bytecode == NULL)
2606 return;
d3ce09f5 2607
e0d9a270
SM
2608 loc->cmd_bytecode.reset ();
2609 }
d3ce09f5
SS
2610 }
2611
7f32a4d5
PA
2612 /* No NULL commands or failed bytecode generation. Build a command
2613 list for all duplicate locations at this location's address.
2614 Note that here we must care for whether the breakpoint location
2615 types are considered duplicates, otherwise, say, if we have a
2616 software and hardware location at the same address, the target
2617 could end up running the commands twice. For the moment, we only
2618 support targets-side commands with dprintf, but it doesn't hurt
2619 to be pedantically correct in case that changes. */
e0d9a270
SM
2620 for (bp_location *loc : loc_range)
2621 if (breakpoint_locations_match (bl, loc)
2622 && loc->owner->extra_string
2623 && is_breakpoint (loc->owner)
2624 && loc->pspace->num == bl->pspace->num
2625 && loc->owner->enable_state == bp_enabled
2626 && loc->enabled
2627 && !loc->disabled_by_cond)
2628 {
2629 /* Add the command to the vector. This will be used later
2630 to send the commands to the target. */
2631 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2632 }
d3ce09f5
SS
2633
2634 bl->target_info.persist = 0;
2635 /* Maybe flag this location as persistent. */
2636 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2637 bl->target_info.persist = 1;
2638}
2639
833b7ab5
YQ
2640/* Return the kind of breakpoint on address *ADDR. Get the kind
2641 of breakpoint according to ADDR except single-step breakpoint.
2642 Get the kind of single-step breakpoint according to the current
2643 registers state. */
cd6c3b4f
YQ
2644
2645static int
2646breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2647{
833b7ab5
YQ
2648 if (bl->owner->type == bp_single_step)
2649 {
2650 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2651 struct regcache *regcache;
2652
00431a78 2653 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2654
2655 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2656 regcache, addr);
2657 }
2658 else
2659 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2660}
2661
35df4500
TJB
2662/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2663 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2664 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2665 Returns 0 for success, 1 if the bp_location type is not supported or
2666 -1 for failure.
879bfdc2 2667
4a64f543
MS
2668 NOTE drow/2003-09-09: This routine could be broken down to an
2669 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2670static int
35df4500 2671insert_bp_location (struct bp_location *bl,
26bb91f3 2672 struct ui_file *tmp_error_stream,
3fbb6ffa 2673 int *disabled_breaks,
dd61ec5c
MW
2674 int *hw_breakpoint_error,
2675 int *hw_bp_error_explained_already)
879bfdc2 2676{
cc06b668 2677 gdb_exception bp_excpt;
879bfdc2 2678
b775012e 2679 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2680 return 0;
2681
35c63cd8
JB
2682 /* Note we don't initialize bl->target_info, as that wipes out
2683 the breakpoint location's shadow_contents if the breakpoint
2684 is still inserted at that location. This in turn breaks
2685 target_read_memory which depends on these buffers when
2686 a memory read is requested at the breakpoint location:
2687 Once the target_info has been wiped, we fail to see that
2688 we have a breakpoint inserted at that address and thus
2689 read the breakpoint instead of returning the data saved in
2690 the breakpoint location's shadow contents. */
0d5ed153 2691 bl->target_info.reqstd_address = bl->address;
35df4500 2692 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2693 bl->target_info.length = bl->length;
8181d85f 2694
b775012e
LM
2695 /* When working with target-side conditions, we must pass all the conditions
2696 for the same breakpoint address down to the target since GDB will not
2697 insert those locations. With a list of breakpoint conditions, the target
2698 can decide when to stop and notify GDB. */
2699
2700 if (is_breakpoint (bl->owner))
2701 {
2702 build_target_condition_list (bl);
d3ce09f5
SS
2703 build_target_command_list (bl);
2704 /* Reset the modification marker. */
b775012e
LM
2705 bl->needs_update = 0;
2706 }
2707
7f32a4d5
PA
2708 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2709 set at a read-only address, then a breakpoint location will have
2710 been changed to hardware breakpoint before we get here. If it is
2711 "off" however, error out before actually trying to insert the
2712 breakpoint, with a nicer error message. */
35df4500 2713 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2714 && !automatic_hardware_breakpoints)
879bfdc2 2715 {
7f32a4d5 2716 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2717
7f32a4d5
PA
2718 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2719 {
6cb06a8c
TT
2720 gdb_printf (tmp_error_stream,
2721 _("Cannot insert breakpoint %d.\n"
2722 "Cannot set software breakpoint "
2723 "at read-only address %s\n"),
2724 bl->owner->number,
2725 paddress (bl->gdbarch, bl->address));
7f32a4d5 2726 return 1;
765dc015 2727 }
7f32a4d5
PA
2728 }
2729
2730 if (bl->loc_type == bp_loc_software_breakpoint
2731 || bl->loc_type == bp_loc_hardware_breakpoint)
2732 {
879bfdc2
DJ
2733 /* First check to see if we have to handle an overlay. */
2734 if (overlay_debugging == ovly_off
35df4500
TJB
2735 || bl->section == NULL
2736 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2737 {
2738 /* No overlay handling: just set the breakpoint. */
a70b8144 2739 try
dd61ec5c 2740 {
0000e5cc
PA
2741 int val;
2742
c47614fe 2743 val = bl->owner->insert_location (bl);
0000e5cc 2744 if (val)
688fca4f 2745 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2746 }
94aeb44b 2747 catch (gdb_exception &e)
dd61ec5c 2748 {
94aeb44b 2749 bp_excpt = std::move (e);
dd61ec5c 2750 }
879bfdc2
DJ
2751 }
2752 else
2753 {
4a64f543 2754 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2755 Shall we set a breakpoint at the LMA? */
2756 if (!overlay_events_enabled)
2757 {
2758 /* Yes -- overlay event support is not active,
2759 so we must try to set a breakpoint at the LMA.
2760 This will not work for a hardware breakpoint. */
35df4500 2761 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2762 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2763 bl->owner->number);
879bfdc2
DJ
2764 else
2765 {
35df4500
TJB
2766 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2767 bl->section);
879bfdc2 2768 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2769 bl->overlay_target_info = bl->target_info;
0d5ed153 2770 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2771
2772 /* No overlay handling: just set the breakpoint. */
a70b8144 2773 try
0000e5cc
PA
2774 {
2775 int val;
2776
579c6ad9 2777 bl->overlay_target_info.kind
cd6c3b4f
YQ
2778 = breakpoint_kind (bl, &addr);
2779 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2780 val = target_insert_breakpoint (bl->gdbarch,
2781 &bl->overlay_target_info);
2782 if (val)
688fca4f
PA
2783 bp_excpt
2784 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2785 }
94aeb44b 2786 catch (gdb_exception &e)
0000e5cc 2787 {
94aeb44b 2788 bp_excpt = std::move (e);
0000e5cc
PA
2789 }
2790
688fca4f 2791 if (bp_excpt.reason != 0)
6cb06a8c
TT
2792 gdb_printf (tmp_error_stream,
2793 "Overlay breakpoint %d "
2794 "failed: in ROM?\n",
2795 bl->owner->number);
879bfdc2
DJ
2796 }
2797 }
2798 /* Shall we set a breakpoint at the VMA? */
35df4500 2799 if (section_is_mapped (bl->section))
879bfdc2
DJ
2800 {
2801 /* Yes. This overlay section is mapped into memory. */
a70b8144 2802 try
dda83cd7 2803 {
0000e5cc
PA
2804 int val;
2805
c47614fe 2806 val = bl->owner->insert_location (bl);
0000e5cc 2807 if (val)
688fca4f 2808 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2809 }
94aeb44b 2810 catch (gdb_exception &e)
dda83cd7 2811 {
94aeb44b 2812 bp_excpt = std::move (e);
dda83cd7 2813 }
879bfdc2
DJ
2814 }
2815 else
2816 {
2817 /* No. This breakpoint will not be inserted.
2818 No error, but do not mark the bp as 'inserted'. */
2819 return 0;
2820 }
2821 }
2822
688fca4f 2823 if (bp_excpt.reason != 0)
879bfdc2
DJ
2824 {
2825 /* Can't set the breakpoint. */
0000e5cc 2826
80dc83fd
AB
2827 /* If the target has closed then it will have deleted any
2828 breakpoints inserted within the target inferior, as a result
2829 any further attempts to interact with the breakpoint objects
2830 is not possible. Just rethrow the error. */
2831 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2832 throw bp_excpt;
2833 gdb_assert (bl->owner != nullptr);
2834
0000e5cc
PA
2835 /* In some cases, we might not be able to insert a
2836 breakpoint in a shared library that has already been
2837 removed, but we have not yet processed the shlib unload
2838 event. Unfortunately, some targets that implement
076855f9
PA
2839 breakpoint insertion themselves can't tell why the
2840 breakpoint insertion failed (e.g., the remote target
2841 doesn't define error codes), so we must treat generic
2842 errors as memory errors. */
688fca4f
PA
2843 if (bp_excpt.reason == RETURN_ERROR
2844 && (bp_excpt.error == GENERIC_ERROR
2845 || bp_excpt.error == MEMORY_ERROR)
076855f9 2846 && bl->loc_type == bp_loc_software_breakpoint
08351840 2847 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2848 || shared_objfile_contains_address_p (bl->pspace,
2849 bl->address)))
879bfdc2 2850 {
4a64f543 2851 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2852 bl->shlib_disabled = 1;
76727919 2853 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2854 if (!*disabled_breaks)
2855 {
6cb06a8c
TT
2856 gdb_printf (tmp_error_stream,
2857 "Cannot insert breakpoint %d.\n",
2858 bl->owner->number);
2859 gdb_printf (tmp_error_stream,
2860 "Temporarily disabling shared "
2861 "library breakpoints:\n");
3fbb6ffa
TJB
2862 }
2863 *disabled_breaks = 1;
6cb06a8c
TT
2864 gdb_printf (tmp_error_stream,
2865 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2866 return 0;
879bfdc2
DJ
2867 }
2868 else
879bfdc2 2869 {
35df4500 2870 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2871 {
0000e5cc 2872 *hw_breakpoint_error = 1;
688fca4f 2873 *hw_bp_error_explained_already = bp_excpt.message != NULL;
6cb06a8c
TT
2874 gdb_printf (tmp_error_stream,
2875 "Cannot insert hardware breakpoint %d%s",
2876 bl->owner->number,
2877 bp_excpt.message ? ":" : ".\n");
dda83cd7 2878 if (bp_excpt.message != NULL)
6cb06a8c
TT
2879 gdb_printf (tmp_error_stream, "%s.\n",
2880 bp_excpt.what ());
879bfdc2
DJ
2881 }
2882 else
2883 {
688fca4f 2884 if (bp_excpt.message == NULL)
0000e5cc 2885 {
1ccbe998 2886 std::string message
0000e5cc
PA
2887 = memory_error_message (TARGET_XFER_E_IO,
2888 bl->gdbarch, bl->address);
0000e5cc 2889
6cb06a8c
TT
2890 gdb_printf (tmp_error_stream,
2891 "Cannot insert breakpoint %d.\n"
2892 "%s\n",
2893 bl->owner->number, message.c_str ());
0000e5cc
PA
2894 }
2895 else
2896 {
6cb06a8c
TT
2897 gdb_printf (tmp_error_stream,
2898 "Cannot insert breakpoint %d: %s\n",
2899 bl->owner->number,
2900 bp_excpt.what ());
0000e5cc 2901 }
879bfdc2 2902 }
0000e5cc 2903 return 1;
879bfdc2
DJ
2904
2905 }
2906 }
2907 else
35df4500 2908 bl->inserted = 1;
879bfdc2 2909
0000e5cc 2910 return 0;
879bfdc2
DJ
2911 }
2912
35df4500 2913 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2914 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2915 watchpoints. It's not clear that it's necessary... */
35df4500 2916 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2917 {
0000e5cc
PA
2918 int val;
2919
c47614fe 2920 val = bl->owner->insert_location (bl);
85d721b8
PA
2921
2922 /* If trying to set a read-watchpoint, and it turns out it's not
2923 supported, try emulating one with an access watchpoint. */
35df4500 2924 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2925 {
85d721b8
PA
2926 /* But don't try to insert it, if there's already another
2927 hw_access location that would be considered a duplicate
2928 of this one. */
48d7020b 2929 for (bp_location *loc : all_bp_locations ())
35df4500 2930 if (loc != bl
85d721b8 2931 && loc->watchpoint_type == hw_access
35df4500 2932 && watchpoint_locations_match (bl, loc))
85d721b8 2933 {
35df4500
TJB
2934 bl->duplicate = 1;
2935 bl->inserted = 1;
2936 bl->target_info = loc->target_info;
2937 bl->watchpoint_type = hw_access;
85d721b8
PA
2938 val = 0;
2939 break;
2940 }
2941
2942 if (val == 1)
2943 {
77b06cd7 2944 bl->watchpoint_type = hw_access;
c47614fe 2945 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2946
2947 if (val)
2948 /* Back to the original value. */
2949 bl->watchpoint_type = hw_read;
85d721b8
PA
2950 }
2951 }
2952
35df4500 2953 bl->inserted = (val == 0);
879bfdc2
DJ
2954 }
2955
35df4500 2956 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2957 {
0000e5cc
PA
2958 int val;
2959
c47614fe 2960 val = bl->owner->insert_location (bl);
77b06cd7
TJB
2961 if (val)
2962 {
2963 bl->owner->enable_state = bp_disabled;
2964
2965 if (val == 1)
2966 warning (_("\
2967Error inserting catchpoint %d: Your system does not support this type\n\
2968of catchpoint."), bl->owner->number);
2969 else
2970 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2971 }
2972
2973 bl->inserted = (val == 0);
1640b821
DJ
2974
2975 /* We've already printed an error message if there was a problem
2976 inserting this catchpoint, and we've disabled the catchpoint,
2977 so just return success. */
2978 return 0;
879bfdc2
DJ
2979 }
2980
2981 return 0;
2982}
2983
6c95b8df
PA
2984/* This function is called when program space PSPACE is about to be
2985 deleted. It takes care of updating breakpoints to not reference
2986 PSPACE anymore. */
2987
2988void
2989breakpoint_program_space_exit (struct program_space *pspace)
2990{
6c95b8df 2991 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
2992 for (breakpoint *b : all_breakpoints_safe ())
2993 if (b->pspace == pspace)
2994 delete_breakpoint (b);
6c95b8df
PA
2995
2996 /* Breakpoints set through other program spaces could have locations
2997 bound to PSPACE as well. Remove those. */
48d7020b 2998 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
2999 {
3000 struct bp_location *tmp;
3001
3002 if (loc->pspace == pspace)
3003 {
2bdf28a0 3004 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3005 if (loc->owner->loc == loc)
3006 loc->owner->loc = loc->next;
3007 else
3008 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3009 if (tmp->next == loc)
3010 {
3011 tmp->next = loc->next;
3012 break;
3013 }
3014 }
3015 }
3016
3017 /* Now update the global location list to permanently delete the
3018 removed locations above. */
44702360 3019 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3020}
3021
74960c60
VP
3022/* Make sure all breakpoints are inserted in inferior.
3023 Throws exception on any error.
3024 A breakpoint that is already inserted won't be inserted
3025 again, so calling this function twice is safe. */
3026void
3027insert_breakpoints (void)
3028{
43892fdf 3029 for (breakpoint *bpt : all_breakpoints ())
74960c60 3030 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3031 {
3032 struct watchpoint *w = (struct watchpoint *) bpt;
3033
3034 update_watchpoint (w, 0 /* don't reparse. */);
3035 }
74960c60 3036
04086b45
PA
3037 /* Updating watchpoints creates new locations, so update the global
3038 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3039 ignore breakpoints_always_inserted_mode. Also,
3040 update_global_location_list tries to "upgrade" software
3041 breakpoints to hardware breakpoints to handle "set breakpoint
3042 auto-hw", so we need to call it even if we don't have new
3043 locations. */
04086b45 3044 update_global_location_list (UGLL_INSERT);
74960c60
VP
3045}
3046
b775012e
LM
3047/* This is used when we need to synch breakpoint conditions between GDB and the
3048 target. It is the case with deleting and disabling of breakpoints when using
3049 always-inserted mode. */
3050
3051static void
3052update_inserted_breakpoint_locations (void)
3053{
b775012e
LM
3054 int error_flag = 0;
3055 int val = 0;
3056 int disabled_breaks = 0;
3057 int hw_breakpoint_error = 0;
dd61ec5c 3058 int hw_bp_details_reported = 0;
b775012e 3059
d7e74731 3060 string_file tmp_error_stream;
b775012e
LM
3061
3062 /* Explicitly mark the warning -- this will only be printed if
3063 there was an error. */
d7e74731 3064 tmp_error_stream.puts ("Warning:\n");
b775012e 3065
5ed8105e 3066 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3067
48d7020b 3068 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3069 {
3070 /* We only want to update software breakpoints and hardware
3071 breakpoints. */
3072 if (!is_breakpoint (bl->owner))
3073 continue;
3074
3075 /* We only want to update locations that are already inserted
3076 and need updating. This is to avoid unwanted insertion during
3077 deletion of breakpoints. */
4daf1902 3078 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3079 continue;
3080
3081 switch_to_program_space_and_thread (bl->pspace);
3082
3083 /* For targets that support global breakpoints, there's no need
3084 to select an inferior to insert breakpoint to. In fact, even
3085 if we aren't attached to any process yet, we should still
3086 insert breakpoints. */
f5656ead 3087 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3088 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3089 continue;
3090
d7e74731 3091 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3092 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3093 if (val)
3094 error_flag = val;
3095 }
3096
3097 if (error_flag)
3098 {
223ffa71 3099 target_terminal::ours_for_output ();
b775012e
LM
3100 error_stream (tmp_error_stream);
3101 }
b775012e
LM
3102}
3103
c30eee59 3104/* Used when starting or continuing the program. */
c906108c 3105
74960c60
VP
3106static void
3107insert_breakpoint_locations (void)
c906108c 3108{
eacd795a 3109 int error_flag = 0;
c906108c 3110 int val = 0;
3fbb6ffa 3111 int disabled_breaks = 0;
81d0cc19 3112 int hw_breakpoint_error = 0;
dd61ec5c 3113 int hw_bp_error_explained_already = 0;
c906108c 3114
d7e74731
PA
3115 string_file tmp_error_stream;
3116
81d0cc19
GS
3117 /* Explicitly mark the warning -- this will only be printed if
3118 there was an error. */
d7e74731 3119 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3120
5ed8105e 3121 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3122
48d7020b 3123 for (bp_location *bl : all_bp_locations ())
879bfdc2 3124 {
b775012e 3125 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3126 continue;
3127
4a64f543
MS
3128 /* There is no point inserting thread-specific breakpoints if
3129 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3130 has BL->OWNER always non-NULL. */
35df4500 3131 if (bl->owner->thread != -1
5d5658a1 3132 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3133 continue;
3134
35df4500 3135 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3136
3137 /* For targets that support global breakpoints, there's no need
3138 to select an inferior to insert breakpoint to. In fact, even
3139 if we aren't attached to any process yet, we should still
3140 insert breakpoints. */
f5656ead 3141 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3142 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3143 continue;
3144
d7e74731 3145 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3146 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3147 if (val)
eacd795a 3148 error_flag = val;
879bfdc2 3149 }
c906108c 3150
4a64f543
MS
3151 /* If we failed to insert all locations of a watchpoint, remove
3152 them, as half-inserted watchpoint is of limited use. */
43892fdf 3153 for (breakpoint *bpt : all_breakpoints ())
a5606eee
VP
3154 {
3155 int some_failed = 0;
a5606eee
VP
3156
3157 if (!is_hardware_watchpoint (bpt))
3158 continue;
3159
d6b74ac4 3160 if (!breakpoint_enabled (bpt))
a5606eee 3161 continue;
74960c60
VP
3162
3163 if (bpt->disposition == disp_del_at_next_stop)
3164 continue;
a5606eee 3165
40cb8ca5 3166 for (bp_location *loc : bpt->locations ())
56710373 3167 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3168 {
3169 some_failed = 1;
3170 break;
3171 }
40cb8ca5 3172
a5606eee
VP
3173 if (some_failed)
3174 {
40cb8ca5 3175 for (bp_location *loc : bpt->locations ())
a5606eee 3176 if (loc->inserted)
834c0d03 3177 remove_breakpoint (loc);
a5606eee
VP
3178
3179 hw_breakpoint_error = 1;
d7e74731
PA
3180 tmp_error_stream.printf ("Could not insert "
3181 "hardware watchpoint %d.\n",
3182 bpt->number);
eacd795a 3183 error_flag = -1;
a5606eee
VP
3184 }
3185 }
3186
eacd795a 3187 if (error_flag)
81d0cc19
GS
3188 {
3189 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3190 message about possibly exhausted resources. */
dd61ec5c 3191 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3192 {
d7e74731 3193 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3194You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3195 }
223ffa71 3196 target_terminal::ours_for_output ();
81d0cc19
GS
3197 error_stream (tmp_error_stream);
3198 }
c906108c
SS
3199}
3200
c30eee59
TJB
3201/* Used when the program stops.
3202 Returns zero if successful, or non-zero if there was a problem
3203 removing a breakpoint location. */
3204
c906108c 3205int
fba45db2 3206remove_breakpoints (void)
c906108c 3207{
3a1bae8e 3208 int val = 0;
c906108c 3209
48d7020b 3210 for (bp_location *bl : all_bp_locations ())
1e4d1764 3211 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3212 val |= remove_breakpoint (bl);
01add95b 3213
3a1bae8e 3214 return val;
c906108c
SS
3215}
3216
49fa26b0
PA
3217/* When a thread exits, remove breakpoints that are related to
3218 that thread. */
3219
3220static void
3221remove_threaded_breakpoints (struct thread_info *tp, int silent)
3222{
1428b37a 3223 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3224 {
5d5658a1 3225 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3226 {
3227 b->disposition = disp_del_at_next_stop;
3228
6cb06a8c 3229 gdb_printf (_("\
43792cf0 3230Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
6cb06a8c 3231 b->number, print_thread_id (tp));
49fa26b0
PA
3232
3233 /* Hide it from the user. */
3234 b->number = 0;
3235 }
3236 }
3237}
3238
f3869b1a 3239/* See breakpoint.h. */
6c95b8df 3240
f3869b1a 3241void
00431a78 3242remove_breakpoints_inf (inferior *inf)
6c95b8df 3243{
6c95b8df 3244 int val;
6c95b8df 3245
48d7020b 3246 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3247 {
3248 if (bl->pspace != inf->pspace)
3249 continue;
6c95b8df 3250
01add95b
SM
3251 if (bl->inserted && !bl->target_info.persist)
3252 {
3253 val = remove_breakpoint (bl);
3254 if (val != 0)
3255 return;
3256 }
3257 }
6c95b8df
PA
3258}
3259
e58b0e63
PA
3260static int internal_breakpoint_number = -1;
3261
84f4c1fe
PM
3262/* Set the breakpoint number of B, depending on the value of INTERNAL.
3263 If INTERNAL is non-zero, the breakpoint number will be populated
3264 from internal_breakpoint_number and that variable decremented.
e5dd4106 3265 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3266 breakpoint_count and that value incremented. Internal breakpoints
3267 do not set the internal var bpnum. */
3268static void
3269set_breakpoint_number (int internal, struct breakpoint *b)
3270{
3271 if (internal)
3272 b->number = internal_breakpoint_number--;
3273 else
3274 {
3275 set_breakpoint_count (breakpoint_count + 1);
3276 b->number = breakpoint_count;
3277 }
3278}
3279
e62c965a 3280static struct breakpoint *
a6d9a66e 3281create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3282 CORE_ADDR address, enum bptype type)
e62c965a 3283{
752a2291
PA
3284 std::unique_ptr<internal_breakpoint> b
3285 (new internal_breakpoint (gdbarch, type, address));
e62c965a 3286
e62c965a 3287 b->number = internal_breakpoint_number--;
e62c965a 3288
752a2291 3289 return add_to_breakpoint_chain (std::move (b));
e62c965a
PP
3290}
3291
17450429
PP
3292static const char *const longjmp_names[] =
3293 {
3294 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3295 };
3296#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3297
3298/* Per-objfile data private to breakpoint.c. */
3299struct breakpoint_objfile_data
3300{
3301 /* Minimal symbol for "_ovly_debug_event" (if any). */
f6b3ad54 3302 struct bound_minimal_symbol overlay_msym;
17450429
PP
3303
3304 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
f6b3ad54 3305 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3306
28106bc2 3307 /* True if we have looked for longjmp probes. */
43dce439 3308 int longjmp_searched = 0;
28106bc2 3309
45461e0d
SM
3310 /* SystemTap probe points for longjmp (if any). These are non-owning
3311 references. */
3312 std::vector<probe *> longjmp_probes;
28106bc2 3313
17450429 3314 /* Minimal symbol for "std::terminate()" (if any). */
f6b3ad54 3315 struct bound_minimal_symbol terminate_msym;
17450429
PP
3316
3317 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
f6b3ad54 3318 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3319
3320 /* True if we have looked for exception probes. */
43dce439 3321 int exception_searched = 0;
28106bc2 3322
45461e0d
SM
3323 /* SystemTap probe points for unwinding (if any). These are non-owning
3324 references. */
3325 std::vector<probe *> exception_probes;
17450429
PP
3326};
3327
08b8a139 3328static const registry<objfile>::key<breakpoint_objfile_data>
51d3063a 3329 breakpoint_objfile_key;
17450429
PP
3330
3331/* Minimal symbol not found sentinel. */
3332static struct minimal_symbol msym_not_found;
3333
3334/* Returns TRUE if MSYM point to the "not found" sentinel. */
3335
3336static int
3337msym_not_found_p (const struct minimal_symbol *msym)
3338{
3339 return msym == &msym_not_found;
3340}
3341
3342/* Return per-objfile data needed by breakpoint.c.
3343 Allocate the data if necessary. */
3344
3345static struct breakpoint_objfile_data *
3346get_breakpoint_objfile_data (struct objfile *objfile)
3347{
3348 struct breakpoint_objfile_data *bp_objfile_data;
3349
51d3063a 3350 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3351 if (bp_objfile_data == NULL)
51d3063a 3352 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3353 return bp_objfile_data;
3354}
3355
e62c965a 3356static void
af02033e 3357create_overlay_event_breakpoint (void)
e62c965a 3358{
af02033e 3359 const char *const func_name = "_ovly_debug_event";
e62c965a 3360
2030c079 3361 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3362 {
3363 struct breakpoint *b;
17450429
PP
3364 struct breakpoint_objfile_data *bp_objfile_data;
3365 CORE_ADDR addr;
69de3c6a 3366
17450429
PP
3367 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3368
3b7344d5 3369 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3370 continue;
3371
3b7344d5 3372 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3373 {
3b7344d5 3374 struct bound_minimal_symbol m;
17450429
PP
3375
3376 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3377 if (m.minsym == NULL)
17450429
PP
3378 {
3379 /* Avoid future lookups in this objfile. */
3b7344d5 3380 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3381 continue;
3382 }
3383 bp_objfile_data->overlay_msym = m;
3384 }
e62c965a 3385
4aeddc50 3386 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3387 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3388 bp_overlay_event);
40d97ee2 3389 b->locspec = new_explicit_location_spec_function (func_name);
e62c965a 3390
69de3c6a 3391 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3392 {
3393 b->enable_state = bp_enabled;
3394 overlay_events_enabled = 1;
3395 }
69de3c6a
PP
3396 else
3397 {
dda83cd7
SM
3398 b->enable_state = bp_disabled;
3399 overlay_events_enabled = 0;
69de3c6a 3400 }
e62c965a 3401 }
e62c965a
PP
3402}
3403
2a7f6487
TV
3404/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3405 true if a breakpoint was installed. */
3406
3407static bool
3408create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3409{
2a7f6487
TV
3410 struct gdbarch *gdbarch = objfile->arch ();
3411 struct breakpoint_objfile_data *bp_objfile_data
3412 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3413
2a7f6487 3414 if (!bp_objfile_data->longjmp_searched)
94c93c35 3415 {
2a7f6487
TV
3416 std::vector<probe *> ret
3417 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3418
2a7f6487 3419 if (!ret.empty ())
94c93c35 3420 {
2a7f6487
TV
3421 /* We are only interested in checking one element. */
3422 probe *p = ret[0];
0fd8e87f 3423
2a7f6487
TV
3424 if (!p->can_evaluate_arguments ())
3425 {
3426 /* We cannot use the probe interface here,
3427 because it does not know how to evaluate
3428 arguments. */
3429 ret.clear ();
3430 }
3431 }
3432 bp_objfile_data->longjmp_probes = ret;
3433 bp_objfile_data->longjmp_searched = 1;
3434 }
0fd8e87f 3435
2a7f6487
TV
3436 if (bp_objfile_data->longjmp_probes.empty ())
3437 return false;
17450429 3438
2a7f6487
TV
3439 for (probe *p : bp_objfile_data->longjmp_probes)
3440 {
3441 struct breakpoint *b;
25f9533e 3442
2a7f6487
TV
3443 b = create_internal_breakpoint (gdbarch,
3444 p->get_relocated_address (objfile),
9efa3c7f 3445 bp_longjmp_master);
264f9890 3446 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
2a7f6487
TV
3447 b->enable_state = bp_disabled;
3448 }
aed57c53 3449
2a7f6487
TV
3450 return true;
3451}
25f9533e 3452
2a7f6487
TV
3453/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3454 Return true if at least one breakpoint was installed. */
3455
3456static bool
3457create_longjmp_master_breakpoint_names (objfile *objfile)
3458{
3459 struct gdbarch *gdbarch = objfile->arch ();
3460 if (!gdbarch_get_longjmp_target_p (gdbarch))
3461 return false;
3462
3463 struct breakpoint_objfile_data *bp_objfile_data
3464 = get_breakpoint_objfile_data (objfile);
3465 unsigned int installed_bp = 0;
3466
3467 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3468 {
3469 struct breakpoint *b;
3470 const char *func_name;
3471 CORE_ADDR addr;
2a7f6487
TV
3472
3473 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3474 continue;
28106bc2 3475
2a7f6487
TV
3476 func_name = longjmp_names[i];
3477 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3478 {
3479 struct bound_minimal_symbol m;
3480
3481 m = lookup_minimal_symbol_text (func_name, objfile);
3482 if (m.minsym == NULL)
3483 {
3484 /* Prevent future lookups in this objfile. */
3485 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3486 continue;
3487 }
2a7f6487
TV
3488 bp_objfile_data->longjmp_msym[i] = m;
3489 }
28106bc2 3490
4aeddc50 3491 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3492 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
40d97ee2 3493 b->locspec = new_explicit_location_spec_function (func_name);
2a7f6487
TV
3494 b->enable_state = bp_disabled;
3495 installed_bp++;
3496 }
28106bc2 3497
2a7f6487
TV
3498 return installed_bp > 0;
3499}
0fd8e87f 3500
2a7f6487 3501/* Create a master longjmp breakpoint. */
17450429 3502
2a7f6487
TV
3503static void
3504create_longjmp_master_breakpoint (void)
3505{
3506 scoped_restore_current_program_space restore_pspace;
aed57c53 3507
2a7f6487
TV
3508 for (struct program_space *pspace : program_spaces)
3509 {
3510 set_current_program_space (pspace);
17450429 3511
2a7f6487
TV
3512 for (objfile *obj : current_program_space->objfiles ())
3513 {
3514 /* Skip separate debug object, it's handled in the loop below. */
3515 if (obj->separate_debug_objfile_backlink != nullptr)
3516 continue;
3517
3518 /* Try a probe kind breakpoint on main objfile. */
3519 if (create_longjmp_master_breakpoint_probe (obj))
3520 continue;
3521
3522 /* Try longjmp_names kind breakpoints on main and separate_debug
3523 objfiles. */
3524 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3525 if (create_longjmp_master_breakpoint_names (debug_objfile))
3526 break;
94c93c35
TT
3527 }
3528 }
0fd8e87f
UW
3529}
3530
af02033e 3531/* Create a master std::terminate breakpoint. */
aa7d318d 3532static void
af02033e 3533create_std_terminate_master_breakpoint (void)
aa7d318d 3534{
af02033e 3535 const char *const func_name = "std::terminate()";
aa7d318d 3536
5ed8105e 3537 scoped_restore_current_program_space restore_pspace;
aa7d318d 3538
94c93c35
TT
3539 for (struct program_space *pspace : program_spaces)
3540 {
3541 CORE_ADDR addr;
17450429 3542
94c93c35 3543 set_current_program_space (pspace);
17450429 3544
94c93c35
TT
3545 for (objfile *objfile : current_program_space->objfiles ())
3546 {
3547 struct breakpoint *b;
3548 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3549
94c93c35 3550 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3551
94c93c35
TT
3552 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3553 continue;
17450429 3554
94c93c35
TT
3555 if (bp_objfile_data->terminate_msym.minsym == NULL)
3556 {
3557 struct bound_minimal_symbol m;
17450429 3558
94c93c35 3559 m = lookup_minimal_symbol (func_name, NULL, objfile);
60f62e2b
SM
3560 if (m.minsym == NULL || (m.minsym->type () != mst_text
3561 && m.minsym->type () != mst_file_text))
94c93c35
TT
3562 {
3563 /* Prevent future lookups in this objfile. */
3564 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3565 continue;
3566 }
3567 bp_objfile_data->terminate_msym = m;
3568 }
aa7d318d 3569
4aeddc50 3570 addr = bp_objfile_data->terminate_msym.value_address ();
94c93c35 3571 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3572 bp_std_terminate_master);
40d97ee2 3573 b->locspec = new_explicit_location_spec_function (func_name);
94c93c35
TT
3574 b->enable_state = bp_disabled;
3575 }
3576 }
aa7d318d
TT
3577}
3578
1940319c
TV
3579/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3580 probe. Return true if a breakpoint was installed. */
186c406b 3581
1940319c
TV
3582static bool
3583create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3584{
1940319c
TV
3585 struct breakpoint *b;
3586 struct gdbarch *gdbarch;
3587 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3588
1940319c 3589 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3590
1940319c
TV
3591 /* We prefer the SystemTap probe point if it exists. */
3592 if (!bp_objfile_data->exception_searched)
3593 {
3594 std::vector<probe *> ret
3595 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3596
1940319c 3597 if (!ret.empty ())
28106bc2 3598 {
1940319c
TV
3599 /* We are only interested in checking one element. */
3600 probe *p = ret[0];
25f9533e 3601
1940319c 3602 if (!p->can_evaluate_arguments ())
25f9533e 3603 {
1940319c
TV
3604 /* We cannot use the probe interface here, because it does
3605 not know how to evaluate arguments. */
3606 ret.clear ();
25f9533e 3607 }
28106bc2 3608 }
1940319c
TV
3609 bp_objfile_data->exception_probes = ret;
3610 bp_objfile_data->exception_searched = 1;
3611 }
28106bc2 3612
1940319c
TV
3613 if (bp_objfile_data->exception_probes.empty ())
3614 return false;
45461e0d 3615
1940319c 3616 gdbarch = objfile->arch ();
28106bc2 3617
1940319c
TV
3618 for (probe *p : bp_objfile_data->exception_probes)
3619 {
3620 b = create_internal_breakpoint (gdbarch,
3621 p->get_relocated_address (objfile),
9efa3c7f 3622 bp_exception_master);
264f9890 3623 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
1940319c
TV
3624 b->enable_state = bp_disabled;
3625 }
28106bc2 3626
1940319c
TV
3627 return true;
3628}
28106bc2 3629
1940319c
TV
3630/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3631 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3632
1940319c
TV
3633static bool
3634create_exception_master_breakpoint_hook (objfile *objfile)
3635{
3636 const char *const func_name = "_Unwind_DebugHook";
3637 struct breakpoint *b;
3638 struct gdbarch *gdbarch;
3639 struct breakpoint_objfile_data *bp_objfile_data;
3640 CORE_ADDR addr;
186c406b 3641
1940319c 3642 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3643
1940319c
TV
3644 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3645 return false;
17450429 3646
1940319c
TV
3647 gdbarch = objfile->arch ();
3648
3649 if (bp_objfile_data->exception_msym.minsym == NULL)
3650 {
3651 struct bound_minimal_symbol debug_hook;
3652
3653 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3654 if (debug_hook.minsym == NULL)
3655 {
3656 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3657 return false;
186c406b 3658 }
17450429 3659
1940319c
TV
3660 bp_objfile_data->exception_msym = debug_hook;
3661 }
3662
4aeddc50 3663 addr = bp_objfile_data->exception_msym.value_address ();
328d42d8
SM
3664 addr = gdbarch_convert_from_func_ptr_addr
3665 (gdbarch, addr, current_inferior ()->top_target ());
9efa3c7f 3666 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
40d97ee2 3667 b->locspec = new_explicit_location_spec_function (func_name);
1940319c
TV
3668 b->enable_state = bp_disabled;
3669
3670 return true;
3671}
3672
3673/* Install a master breakpoint on the unwinder's debug hook. */
3674
3675static void
3676create_exception_master_breakpoint (void)
3677{
3678 for (objfile *obj : current_program_space->objfiles ())
3679 {
3680 /* Skip separate debug object. */
3681 if (obj->separate_debug_objfile_backlink)
3682 continue;
3683
3684 /* Try a probe kind breakpoint. */
3685 if (create_exception_master_breakpoint_probe (obj))
3686 continue;
3687
7c6944ab
TV
3688 /* Iterate over main and separate debug objects and try an
3689 _Unwind_DebugHook kind breakpoint. */
3690 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3691 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3692 break;
186c406b 3693 }
186c406b
TT
3694}
3695
9ef9e6a6
KS
3696/* Does B have a location spec? */
3697
3698static int
264f9890 3699breakpoint_location_spec_empty_p (const struct breakpoint *b)
9ef9e6a6 3700{
238dc9af 3701 return (b->locspec != nullptr && b->locspec->empty_p ());
9ef9e6a6
KS
3702}
3703
c906108c 3704void
fba45db2 3705update_breakpoints_after_exec (void)
c906108c 3706{
25b22b0a
PA
3707 /* We're about to delete breakpoints from GDB's lists. If the
3708 INSERTED flag is true, GDB will try to lift the breakpoints by
3709 writing the breakpoints' "shadow contents" back into memory. The
3710 "shadow contents" are NOT valid after an exec, so GDB should not
3711 do that. Instead, the target is responsible from marking
3712 breakpoints out as soon as it detects an exec. We don't do that
3713 here instead, because there may be other attempts to delete
3714 breakpoints after detecting an exec and before reaching here. */
48d7020b 3715 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3716 if (bploc->pspace == current_program_space)
3717 gdb_assert (!bploc->inserted);
c906108c 3718
1428b37a 3719 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3720 {
3721 if (b->pspace != current_program_space)
c5aa993b 3722 continue;
c906108c 3723
01add95b
SM
3724 /* Solib breakpoints must be explicitly reset after an exec(). */
3725 if (b->type == bp_shlib_event)
3726 {
3727 delete_breakpoint (b);
3728 continue;
3729 }
4efc6507 3730
01add95b
SM
3731 /* JIT breakpoints must be explicitly reset after an exec(). */
3732 if (b->type == bp_jit_event)
3733 {
3734 delete_breakpoint (b);
3735 continue;
3736 }
c4093a6a 3737
01add95b
SM
3738 /* Thread event breakpoints must be set anew after an exec(),
3739 as must overlay event and longjmp master breakpoints. */
3740 if (b->type == bp_thread_event || b->type == bp_overlay_event
3741 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3742 || b->type == bp_exception_master)
3743 {
3744 delete_breakpoint (b);
3745 continue;
3746 }
c5aa993b 3747
01add95b
SM
3748 /* Step-resume breakpoints are meaningless after an exec(). */
3749 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3750 {
3751 delete_breakpoint (b);
3752 continue;
3753 }
7c16b83e 3754
01add95b
SM
3755 /* Just like single-step breakpoints. */
3756 if (b->type == bp_single_step)
3757 {
3758 delete_breakpoint (b);
3759 continue;
3760 }
611c83ae 3761
01add95b
SM
3762 /* Longjmp and longjmp-resume breakpoints are also meaningless
3763 after an exec. */
3764 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3765 || b->type == bp_longjmp_call_dummy
3766 || b->type == bp_exception || b->type == bp_exception_resume)
3767 {
3768 delete_breakpoint (b);
3769 continue;
3770 }
ce78b96d 3771
01add95b
SM
3772 if (b->type == bp_catchpoint)
3773 {
3774 /* For now, none of the bp_catchpoint breakpoints need to
3775 do anything at this point. In the future, if some of
3776 the catchpoints need to something, we will need to add
3777 a new method, and call this method from here. */
3778 continue;
3779 }
c5aa993b 3780
01add95b
SM
3781 /* bp_finish is a special case. The only way we ought to be able
3782 to see one of these when an exec() has happened, is if the user
3783 caught a vfork, and then said "finish". Ordinarily a finish just
3784 carries them to the call-site of the current callee, by setting
3785 a temporary bp there and resuming. But in this case, the finish
3786 will carry them entirely through the vfork & exec.
3787
3788 We don't want to allow a bp_finish to remain inserted now. But
3789 we can't safely delete it, 'cause finish_command has a handle to
3790 the bp on a bpstat, and will later want to delete it. There's a
3791 chance (and I've seen it happen) that if we delete the bp_finish
3792 here, that its storage will get reused by the time finish_command
3793 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3794 We really must allow finish_command to delete a bp_finish.
3795
3796 In the absence of a general solution for the "how do we know
3797 it's safe to delete something others may have handles to?"
3798 problem, what we'll do here is just uninsert the bp_finish, and
3799 let finish_command delete it.
3800
3801 (We know the bp_finish is "doomed" in the sense that it's
3802 momentary, and will be deleted as soon as finish_command sees
3803 the inferior stopped. So it doesn't matter that the bp's
3804 address is probably bogus in the new a.out, unlike e.g., the
3805 solib breakpoints.) */
3806
3807 if (b->type == bp_finish)
3808 {
3809 continue;
3810 }
3811
3812 /* Without a symbolic address, we have little hope of the
3813 pre-exec() address meaning the same thing in the post-exec()
3814 a.out. */
264f9890 3815 if (breakpoint_location_spec_empty_p (b))
01add95b
SM
3816 {
3817 delete_breakpoint (b);
3818 continue;
3819 }
3820 }
c906108c
SS
3821}
3822
3823int
d80ee84f 3824detach_breakpoints (ptid_t ptid)
c906108c 3825{
3a1bae8e 3826 int val = 0;
2989a365 3827 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3828 struct inferior *inf = current_inferior ();
c5aa993b 3829
e99b03dc 3830 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3831 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3832
6c95b8df 3833 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3834 inferior_ptid = ptid;
48d7020b 3835 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3836 {
3837 if (bl->pspace != inf->pspace)
3838 continue;
3839
3840 /* This function must physically remove breakpoints locations
3841 from the specified ptid, without modifying the breakpoint
6e14e441
PA
3842 package's state. Locations of type bp_loc_other and
3843 bp_loc_software_watchpoint are only maintained at GDB side,
3844 so there is no need to remove them. Moreover, removing these
01add95b 3845 would modify the breakpoint package's state. */
6e14e441
PA
3846 if (bl->loc_type == bp_loc_other
3847 || bl->loc_type == bp_loc_software_watchpoint)
01add95b
SM
3848 continue;
3849
3850 if (bl->inserted)
3851 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3852 }
d03285ec 3853
3a1bae8e 3854 return val;
c906108c
SS
3855}
3856
35df4500 3857/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3858 Note that this is used to detach breakpoints from a child fork.
3859 When we get here, the child isn't in the inferior list, and neither
3860 do we have objects to represent its address space --- we should
35df4500 3861 *not* look at bl->pspace->aspace here. */
6c95b8df 3862
c906108c 3863static int
b2b6a7da 3864remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3865{
3866 int val;
c5aa993b 3867
35df4500
TJB
3868 /* BL is never in moribund_locations by our callers. */
3869 gdb_assert (bl->owner != NULL);
2bdf28a0 3870
74960c60
VP
3871 /* The type of none suggests that owner is actually deleted.
3872 This should not ever happen. */
35df4500 3873 gdb_assert (bl->owner->type != bp_none);
0bde7532 3874
35df4500
TJB
3875 if (bl->loc_type == bp_loc_software_breakpoint
3876 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3877 {
c02f5703
MS
3878 /* "Normal" instruction breakpoint: either the standard
3879 trap-instruction bp (bp_breakpoint), or a
3880 bp_hardware_breakpoint. */
3881
3882 /* First check to see if we have to handle an overlay. */
3883 if (overlay_debugging == ovly_off
35df4500
TJB
3884 || bl->section == NULL
3885 || !(section_is_overlay (bl->section)))
c02f5703
MS
3886 {
3887 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3888
3889 /* If we're trying to uninsert a memory breakpoint that we
3890 know is set in a dynamic object that is marked
3891 shlib_disabled, then either the dynamic object was
3892 removed with "remove-symbol-file" or with
3893 "nosharedlibrary". In the former case, we don't know
3894 whether another dynamic object might have loaded over the
3895 breakpoint's address -- the user might well let us know
3896 about it next with add-symbol-file (the whole point of
d03de421 3897 add-symbol-file is letting the user manually maintain a
08351840
PA
3898 list of dynamically loaded objects). If we have the
3899 breakpoint's shadow memory, that is, this is a software
3900 breakpoint managed by GDB, check whether the breakpoint
3901 is still inserted in memory, to avoid overwriting wrong
3902 code with stale saved shadow contents. Note that HW
3903 breakpoints don't have shadow memory, as they're
3904 implemented using a mechanism that is not dependent on
3905 being able to modify the target's memory, and as such
3906 they should always be removed. */
3907 if (bl->shlib_disabled
3908 && bl->target_info.shadow_len != 0
3909 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3910 val = 0;
3911 else
c47614fe 3912 val = bl->owner->remove_location (bl, reason);
c02f5703 3913 }
c906108c
SS
3914 else
3915 {
4a64f543 3916 /* This breakpoint is in an overlay section.
c02f5703
MS
3917 Did we set a breakpoint at the LMA? */
3918 if (!overlay_events_enabled)
3919 {
3920 /* Yes -- overlay event support is not active, so we
3921 should have set a breakpoint at the LMA. Remove it.
3922 */
c02f5703
MS
3923 /* Ignore any failures: if the LMA is in ROM, we will
3924 have already warned when we failed to insert it. */
35df4500
TJB
3925 if (bl->loc_type == bp_loc_hardware_breakpoint)
3926 target_remove_hw_breakpoint (bl->gdbarch,
3927 &bl->overlay_target_info);
c02f5703 3928 else
35df4500 3929 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3930 &bl->overlay_target_info,
3931 reason);
c02f5703
MS
3932 }
3933 /* Did we set a breakpoint at the VMA?
3934 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3935 if (bl->inserted)
c906108c 3936 {
c02f5703
MS
3937 /* Yes -- remove it. Previously we did not bother to
3938 remove the breakpoint if the section had been
3939 unmapped, but let's not rely on that being safe. We
3940 don't know what the overlay manager might do. */
aa67235e
UW
3941
3942 /* However, we should remove *software* breakpoints only
3943 if the section is still mapped, or else we overwrite
3944 wrong code with the saved shadow contents. */
348d480f
PA
3945 if (bl->loc_type == bp_loc_hardware_breakpoint
3946 || section_is_mapped (bl->section))
c47614fe 3947 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
3948 else
3949 val = 0;
c906108c 3950 }
c02f5703
MS
3951 else
3952 {
3953 /* No -- not inserted, so no need to remove. No error. */
3954 val = 0;
3955 }
c906108c 3956 }
879d1e6b 3957
08351840
PA
3958 /* In some cases, we might not be able to remove a breakpoint in
3959 a shared library that has already been removed, but we have
3960 not yet processed the shlib unload event. Similarly for an
3961 unloaded add-symbol-file object - the user might not yet have
3962 had the chance to remove-symbol-file it. shlib_disabled will
3963 be set if the library/object has already been removed, but
3964 the breakpoint hasn't been uninserted yet, e.g., after
3965 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3966 always-inserted mode. */
076855f9 3967 if (val
08351840
PA
3968 && (bl->loc_type == bp_loc_software_breakpoint
3969 && (bl->shlib_disabled
3970 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3971 || shared_objfile_contains_address_p (bl->pspace,
3972 bl->address))))
879d1e6b
UW
3973 val = 0;
3974
c906108c
SS
3975 if (val)
3976 return val;
b2b6a7da 3977 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3978 }
35df4500 3979 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3980 {
b2b6a7da 3981 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 3982 bl->owner->remove_location (bl, reason);
2e70b7b9 3983
c906108c 3984 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3985 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3986 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3987 bl->owner->number);
c906108c 3988 }
35df4500 3989 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
3990 && breakpoint_enabled (bl->owner)
3991 && !bl->duplicate)
ce78b96d 3992 {
c47614fe 3993 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
3994 if (val)
3995 return val;
77b06cd7 3996
b2b6a7da 3997 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3998 }
c906108c
SS
3999
4000 return 0;
4001}
4002
6c95b8df 4003static int
834c0d03 4004remove_breakpoint (struct bp_location *bl)
6c95b8df 4005{
35df4500
TJB
4006 /* BL is never in moribund_locations by our callers. */
4007 gdb_assert (bl->owner != NULL);
2bdf28a0 4008
6c95b8df
PA
4009 /* The type of none suggests that owner is actually deleted.
4010 This should not ever happen. */
35df4500 4011 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4012
5ed8105e 4013 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4014
35df4500 4015 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4016
5ed8105e 4017 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4018}
4019
c906108c
SS
4020/* Clear the "inserted" flag in all breakpoints. */
4021
25b22b0a 4022void
fba45db2 4023mark_breakpoints_out (void)
c906108c 4024{
48d7020b 4025 for (bp_location *bl : all_bp_locations ())
66c4b3e8 4026 if (bl->pspace == current_program_space)
35df4500 4027 bl->inserted = 0;
c906108c
SS
4028}
4029
53a5351d
JM
4030/* Clear the "inserted" flag in all breakpoints and delete any
4031 breakpoints which should go away between runs of the program.
c906108c
SS
4032
4033 Plus other such housekeeping that has to be done for breakpoints
4034 between runs.
4035
53a5351d
JM
4036 Note: this function gets called at the end of a run (by
4037 generic_mourn_inferior) and when a run begins (by
4a64f543 4038 init_wait_for_inferior). */
c906108c
SS
4039
4040
4041
4042void
fba45db2 4043breakpoint_init_inferior (enum inf_context context)
c906108c 4044{
6c95b8df 4045 struct program_space *pspace = current_program_space;
c906108c 4046
50c71eaf
PA
4047 /* If breakpoint locations are shared across processes, then there's
4048 nothing to do. */
f5656ead 4049 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4050 return;
4051
1a853c52 4052 mark_breakpoints_out ();
075f6582 4053
1428b37a 4054 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
4055 {
4056 if (b->loc && b->loc->pspace != pspace)
4057 continue;
6c95b8df 4058
01add95b
SM
4059 switch (b->type)
4060 {
4061 case bp_call_dummy:
4062 case bp_longjmp_call_dummy:
c906108c 4063
01add95b
SM
4064 /* If the call dummy breakpoint is at the entry point it will
4065 cause problems when the inferior is rerun, so we better get
4066 rid of it. */
ab92d69b 4067
01add95b 4068 case bp_watchpoint_scope:
ab92d69b 4069
01add95b 4070 /* Also get rid of scope breakpoints. */
ab92d69b 4071
01add95b 4072 case bp_shlib_event:
ab92d69b 4073
01add95b
SM
4074 /* Also remove solib event breakpoints. Their addresses may
4075 have changed since the last time we ran the program.
4076 Actually we may now be debugging against different target;
4077 and so the solib backend that installed this breakpoint may
4078 not be used in by the target. E.g.,
ab92d69b 4079
01add95b
SM
4080 (gdb) file prog-linux
4081 (gdb) run # native linux target
4082 ...
4083 (gdb) kill
4084 (gdb) file prog-win.exe
4085 (gdb) tar rem :9999 # remote Windows gdbserver.
4086 */
c906108c 4087
01add95b 4088 case bp_step_resume:
f59f708a 4089
01add95b 4090 /* Also remove step-resume breakpoints. */
f59f708a 4091
01add95b 4092 case bp_single_step:
7c16b83e 4093
01add95b 4094 /* Also remove single-step breakpoints. */
7c16b83e 4095
01add95b
SM
4096 delete_breakpoint (b);
4097 break;
c906108c 4098
01add95b
SM
4099 case bp_watchpoint:
4100 case bp_hardware_watchpoint:
4101 case bp_read_watchpoint:
4102 case bp_access_watchpoint:
4103 {
4104 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4105
01add95b
SM
4106 /* Likewise for watchpoints on local expressions. */
4107 if (w->exp_valid_block != NULL)
4108 delete_breakpoint (b);
4109 else
4110 {
4111 /* Get rid of existing locations, which are no longer
4112 valid. New ones will be created in
4113 update_watchpoint, when the inferior is restarted.
4114 The next update_global_location_list call will
4115 garbage collect them. */
4116 b->loc = NULL;
4117
4118 if (context == inf_starting)
4119 {
4120 /* Reset val field to force reread of starting value in
4121 insert_breakpoints. */
4122 w->val.reset (nullptr);
4123 w->val_valid = false;
4124 }
4125 }
4126 }
4127 break;
4128 default:
4129 break;
3a5c3e22 4130 }
01add95b 4131 }
1c5cfe86
PA
4132
4133 /* Get rid of the moribund locations. */
1123588c 4134 for (bp_location *bl : moribund_locations)
35df4500 4135 decref_bp_location (&bl);
1123588c 4136 moribund_locations.clear ();
c906108c
SS
4137}
4138
6c95b8df
PA
4139/* These functions concern about actual breakpoints inserted in the
4140 target --- to e.g. check if we need to do decr_pc adjustment or if
4141 we need to hop over the bkpt --- so we check for address space
4142 match, not program space. */
4143
c2c6d25f
JM
4144/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4145 exists at PC. It returns ordinary_breakpoint_here if it's an
4146 ordinary breakpoint, or permanent_breakpoint_here if it's a
4147 permanent breakpoint.
4148 - When continuing from a location with an ordinary breakpoint, we
4149 actually single step once before calling insert_breakpoints.
e5dd4106 4150 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4151 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4152 the target, to advance the PC past the breakpoint. */
c906108c 4153
c2c6d25f 4154enum breakpoint_here
accd0bcd 4155breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4156{
c2c6d25f 4157 int any_breakpoint_here = 0;
c906108c 4158
48d7020b 4159 for (bp_location *bl : all_bp_locations ())
075f6582 4160 {
35df4500
TJB
4161 if (bl->loc_type != bp_loc_software_breakpoint
4162 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4163 continue;
4164
f1310107 4165 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4166 if ((breakpoint_enabled (bl->owner)
1a853c52 4167 || bl->permanent)
f1310107 4168 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4169 {
4170 if (overlay_debugging
35df4500
TJB
4171 && section_is_overlay (bl->section)
4172 && !section_is_mapped (bl->section))
075f6582 4173 continue; /* unmapped overlay -- can't be a match */
1a853c52 4174 else if (bl->permanent)
075f6582
DJ
4175 return permanent_breakpoint_here;
4176 else
4177 any_breakpoint_here = 1;
4178 }
4179 }
c906108c 4180
f486487f 4181 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4182}
4183
d35ae833
PA
4184/* See breakpoint.h. */
4185
4186int
accd0bcd 4187breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4188 CORE_ADDR addr, ULONGEST len)
4189{
48d7020b 4190 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4191 {
4192 if (bl->loc_type != bp_loc_software_breakpoint
4193 && bl->loc_type != bp_loc_hardware_breakpoint)
4194 continue;
4195
4196 if ((breakpoint_enabled (bl->owner)
4197 || bl->permanent)
4198 && breakpoint_location_address_range_overlap (bl, aspace,
4199 addr, len))
4200 {
4201 if (overlay_debugging
4202 && section_is_overlay (bl->section)
4203 && !section_is_mapped (bl->section))
4204 {
4205 /* Unmapped overlay -- can't be a match. */
4206 continue;
4207 }
4208
4209 return 1;
4210 }
4211 }
4212
4213 return 0;
4214}
4215
1c5cfe86
PA
4216/* Return true if there's a moribund breakpoint at PC. */
4217
4218int
accd0bcd 4219moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4220{
1123588c 4221 for (bp_location *loc : moribund_locations)
f1310107 4222 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4223 return 1;
4224
4225 return 0;
4226}
c2c6d25f 4227
f7ce857f
PA
4228/* Returns non-zero iff BL is inserted at PC, in address space
4229 ASPACE. */
4230
4231static int
4232bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4233 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4234{
4235 if (bl->inserted
4236 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4237 aspace, pc))
4238 {
4239 if (overlay_debugging
4240 && section_is_overlay (bl->section)
4241 && !section_is_mapped (bl->section))
4242 return 0; /* unmapped overlay -- can't be a match */
4243 else
4244 return 1;
4245 }
4246 return 0;
4247}
4248
a1fd2fa5 4249/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4250
4251int
accd0bcd 4252breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4253{
e0d9a270 4254 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4255 {
35df4500
TJB
4256 if (bl->loc_type != bp_loc_software_breakpoint
4257 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4258 continue;
4259
f7ce857f
PA
4260 if (bp_location_inserted_here_p (bl, aspace, pc))
4261 return 1;
c5aa993b 4262 }
c36b740a
VP
4263 return 0;
4264}
4265
a1fd2fa5
PA
4266/* This function returns non-zero iff there is a software breakpoint
4267 inserted at PC. */
c36b740a
VP
4268
4269int
accd0bcd 4270software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4271 CORE_ADDR pc)
4fa8626c 4272{
e0d9a270 4273 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4274 {
35df4500 4275 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4276 continue;
4277
f7ce857f
PA
4278 if (bp_location_inserted_here_p (bl, aspace, pc))
4279 return 1;
4fa8626c
DJ
4280 }
4281
4282 return 0;
9c02b525
PA
4283}
4284
4285/* See breakpoint.h. */
4286
4287int
accd0bcd 4288hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4289 CORE_ADDR pc)
4290{
e0d9a270 4291 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4292 {
9c02b525
PA
4293 if (bl->loc_type != bp_loc_hardware_breakpoint)
4294 continue;
4295
4296 if (bp_location_inserted_here_p (bl, aspace, pc))
4297 return 1;
4298 }
4299
4300 return 0;
4fa8626c
DJ
4301}
4302
9093389c 4303int
accd0bcd 4304hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4305 CORE_ADDR addr, ULONGEST len)
4306{
43892fdf 4307 for (breakpoint *bpt : all_breakpoints ())
9093389c 4308 {
9093389c
PA
4309 if (bpt->type != bp_hardware_watchpoint
4310 && bpt->type != bp_access_watchpoint)
4311 continue;
4312
4313 if (!breakpoint_enabled (bpt))
4314 continue;
4315
40cb8ca5 4316 for (bp_location *loc : bpt->locations ())
9093389c
PA
4317 if (loc->pspace->aspace == aspace && loc->inserted)
4318 {
4319 CORE_ADDR l, h;
4320
4321 /* Check for intersection. */
768adc05
PA
4322 l = std::max<CORE_ADDR> (loc->address, addr);
4323 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4324 if (l < h)
4325 return 1;
4326 }
4327 }
4328 return 0;
4329}
c5aa993b 4330
f2478a7e 4331/* See breakpoint.h. */
c906108c 4332
f2478a7e
SM
4333bool
4334is_catchpoint (struct breakpoint *b)
c906108c 4335{
f2478a7e 4336 return (b->type == bp_catchpoint);
c906108c
SS
4337}
4338
c906108c
SS
4339/* Clear a bpstat so that it says we are not at any breakpoint.
4340 Also free any storage that is part of a bpstat. */
4341
4342void
313f3b21 4343bpstat_clear (bpstat **bsp)
c906108c 4344{
313f3b21
SM
4345 bpstat *p;
4346 bpstat *q;
c906108c
SS
4347
4348 if (bsp == 0)
4349 return;
4350 p = *bsp;
4351 while (p != NULL)
4352 {
4353 q = p->next;
04afa70c 4354 delete p;
c906108c
SS
4355 p = q;
4356 }
4357 *bsp = NULL;
4358}
4359
313f3b21 4360bpstat::bpstat (const bpstat &other)
04afa70c
TT
4361 : next (NULL),
4362 bp_location_at (other.bp_location_at),
4363 breakpoint_at (other.breakpoint_at),
4364 commands (other.commands),
04afa70c
TT
4365 print (other.print),
4366 stop (other.stop),
4367 print_it (other.print_it)
4368{
850645cf
TT
4369 if (other.old_val != NULL)
4370 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4371}
4372
c906108c
SS
4373/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4374 is part of the bpstat is copied as well. */
4375
313f3b21
SM
4376bpstat *
4377bpstat_copy (bpstat *bs)
c906108c 4378{
313f3b21
SM
4379 bpstat *p = nullptr;
4380 bpstat *tmp;
4381 bpstat *retval = nullptr;
c906108c
SS
4382
4383 if (bs == NULL)
4384 return bs;
4385
4386 for (; bs != NULL; bs = bs->next)
4387 {
313f3b21 4388 tmp = new bpstat (*bs);
31cc81e9 4389
c906108c
SS
4390 if (p == NULL)
4391 /* This is the first thing in the chain. */
4392 retval = tmp;
4393 else
4394 p->next = tmp;
4395 p = tmp;
4396 }
4397 p->next = NULL;
4398 return retval;
4399}
4400
4a64f543 4401/* Find the bpstat associated with this breakpoint. */
c906108c 4402
313f3b21
SM
4403bpstat *
4404bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4405{
c5aa993b
JM
4406 if (bsp == NULL)
4407 return NULL;
c906108c 4408
c5aa993b
JM
4409 for (; bsp != NULL; bsp = bsp->next)
4410 {
f431efe5 4411 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4412 return bsp;
4413 }
c906108c
SS
4414 return NULL;
4415}
4416
ab04a2af
TT
4417/* See breakpoint.h. */
4418
4c462cb0 4419bool
313f3b21 4420bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4421{
ab04a2af
TT
4422 for (; bsp != NULL; bsp = bsp->next)
4423 {
427cd150
TT
4424 if (bsp->breakpoint_at == NULL)
4425 {
4426 /* A moribund location can never explain a signal other than
4427 GDB_SIGNAL_TRAP. */
4428 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4429 return true;
427cd150
TT
4430 }
4431 else
47591c29 4432 {
c47614fe 4433 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4434 return true;
47591c29 4435 }
ab04a2af
TT
4436 }
4437
4c462cb0 4438 return false;
ab04a2af
TT
4439}
4440
4a64f543
MS
4441/* Put in *NUM the breakpoint number of the first breakpoint we are
4442 stopped at. *BSP upon return is a bpstat which points to the
4443 remaining breakpoints stopped at (but which is not guaranteed to be
4444 good for anything but further calls to bpstat_num).
4445
8671a17b
PA
4446 Return 0 if passed a bpstat which does not indicate any breakpoints.
4447 Return -1 if stopped at a breakpoint that has been deleted since
4448 we set it.
4449 Return 1 otherwise. */
c906108c
SS
4450
4451int
313f3b21 4452bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4453{
4454 struct breakpoint *b;
4455
4456 if ((*bsp) == NULL)
4457 return 0; /* No more breakpoint values */
8671a17b 4458
4a64f543
MS
4459 /* We assume we'll never have several bpstats that correspond to a
4460 single breakpoint -- otherwise, this function might return the
4461 same number more than once and this will look ugly. */
f431efe5 4462 b = (*bsp)->breakpoint_at;
8671a17b
PA
4463 *bsp = (*bsp)->next;
4464 if (b == NULL)
4465 return -1; /* breakpoint that's been deleted since */
4466
4467 *num = b->number; /* We have its number */
4468 return 1;
c906108c
SS
4469}
4470
e93ca019 4471/* See breakpoint.h. */
c906108c
SS
4472
4473void
e93ca019 4474bpstat_clear_actions (void)
c906108c 4475{
313f3b21 4476 bpstat *bs;
e93ca019 4477
00431a78 4478 if (inferior_ptid == null_ptid)
e93ca019
JK
4479 return;
4480
00431a78 4481 thread_info *tp = inferior_thread ();
e93ca019 4482 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4483 {
d1b0a7bf 4484 bs->commands = NULL;
850645cf 4485 bs->old_val.reset (nullptr);
c906108c
SS
4486 }
4487}
4488
f3b1572e
PA
4489/* Called when a command is about to proceed the inferior. */
4490
4491static void
4492breakpoint_about_to_proceed (void)
4493{
d7e15655 4494 if (inferior_ptid != null_ptid)
f3b1572e
PA
4495 {
4496 struct thread_info *tp = inferior_thread ();
4497
4498 /* Allow inferior function calls in breakpoint commands to not
4499 interrupt the command list. When the call finishes
4500 successfully, the inferior will be standing at the same
4501 breakpoint as if nothing happened. */
16c381f0 4502 if (tp->control.in_infcall)
f3b1572e
PA
4503 return;
4504 }
4505
4506 breakpoint_proceeded = 1;
4507}
4508
abf85f46
JK
4509/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4510 or its equivalent. */
4511
4512static int
4513command_line_is_silent (struct command_line *cmd)
4514{
4f45d445 4515 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4516}
4517
4a64f543
MS
4518/* Execute all the commands associated with all the breakpoints at
4519 this location. Any of these commands could cause the process to
4520 proceed beyond this point, etc. We look out for such changes by
4521 checking the global "breakpoint_proceeded" after each command.
c906108c 4522
347bddb7
PA
4523 Returns true if a breakpoint command resumed the inferior. In that
4524 case, it is the caller's responsibility to recall it again with the
4525 bpstat of the current thread. */
4526
4527static int
313f3b21 4528bpstat_do_actions_1 (bpstat **bsp)
c906108c 4529{
313f3b21 4530 bpstat *bs;
347bddb7 4531 int again = 0;
c906108c
SS
4532
4533 /* Avoid endless recursion if a `source' command is contained
4534 in bs->commands. */
4535 if (executing_breakpoint_commands)
347bddb7 4536 return 0;
c906108c 4537
81b1e71c
TT
4538 scoped_restore save_executing
4539 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4540
1ac32117 4541 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4542
4a64f543 4543 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4544 bs = *bsp;
4545
4546 breakpoint_proceeded = 0;
4547 for (; bs != NULL; bs = bs->next)
4548 {
d1b0a7bf 4549 struct command_line *cmd = NULL;
6c50ab1c
JB
4550
4551 /* Take ownership of the BSP's command tree, if it has one.
4552
dda83cd7
SM
4553 The command tree could legitimately contain commands like
4554 'step' and 'next', which call clear_proceed_status, which
4555 frees stop_bpstat's command tree. To make sure this doesn't
4556 free the tree we're executing out from under us, we need to
4557 take ownership of the tree ourselves. Since a given bpstat's
4558 commands are only executed once, we don't need to copy it; we
4559 can clear the pointer in the bpstat, and make sure we free
4560 the tree when we're done. */
d1b0a7bf 4561 counted_command_line ccmd = bs->commands;
9add0f1b 4562 bs->commands = NULL;
d1b0a7bf
TT
4563 if (ccmd != NULL)
4564 cmd = ccmd.get ();
abf85f46
JK
4565 if (command_line_is_silent (cmd))
4566 {
4567 /* The action has been already done by bpstat_stop_status. */
4568 cmd = cmd->next;
4569 }
6c50ab1c 4570
c906108c
SS
4571 while (cmd != NULL)
4572 {
4573 execute_control_command (cmd);
4574
4575 if (breakpoint_proceeded)
4576 break;
4577 else
4578 cmd = cmd->next;
4579 }
6c50ab1c 4580
c906108c 4581 if (breakpoint_proceeded)
32c1e744 4582 {
cb814510 4583 if (current_ui->async)
347bddb7
PA
4584 /* If we are in async mode, then the target might be still
4585 running, not stopped at any breakpoint, so nothing for
4586 us to do here -- just return to the event loop. */
4587 ;
32c1e744
VP
4588 else
4589 /* In sync mode, when execute_control_command returns
4590 we're already standing on the next breakpoint.
347bddb7
PA
4591 Breakpoint commands for that stop were not run, since
4592 execute_command does not run breakpoint commands --
4593 only command_line_handler does, but that one is not
4594 involved in execution of breakpoint commands. So, we
4595 can now execute breakpoint commands. It should be
4596 noted that making execute_command do bpstat actions is
4597 not an option -- in this case we'll have recursive
4598 invocation of bpstat for each breakpoint with a
4599 command, and can easily blow up GDB stack. Instead, we
4600 return true, which will trigger the caller to recall us
4601 with the new stop_bpstat. */
4602 again = 1;
4603 break;
32c1e744 4604 }
c906108c 4605 }
347bddb7
PA
4606 return again;
4607}
4608
00431a78
PA
4609/* Helper for bpstat_do_actions. Get the current thread, if there's
4610 one, is alive and has execution. Return NULL otherwise. */
4611
4612static thread_info *
4613get_bpstat_thread ()
4614{
55f6301a 4615 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4616 return NULL;
4617
4618 thread_info *tp = inferior_thread ();
611841bb 4619 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4620 return NULL;
4621 return tp;
4622}
4623
347bddb7
PA
4624void
4625bpstat_do_actions (void)
4626{
694c6bf5 4627 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4628 thread_info *tp;
353d1d73 4629
347bddb7 4630 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4631 while ((tp = get_bpstat_thread ()) != NULL)
4632 {
4633 /* Since in sync mode, bpstat_do_actions may resume the
4634 inferior, and only return when it is stopped at the next
4635 breakpoint, we keep doing breakpoint actions until it returns
4636 false to indicate the inferior was not resumed. */
4637 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4638 break;
4639 }
353d1d73 4640
694c6bf5 4641 cleanup_if_error.release ();
c906108c
SS
4642}
4643
fa4727a6
DJ
4644/* Print out the (old or new) value associated with a watchpoint. */
4645
4646static void
4647watchpoint_value_print (struct value *val, struct ui_file *stream)
4648{
4649 if (val == NULL)
7f6aba03 4650 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4651 else
79a45b7d
TT
4652 {
4653 struct value_print_options opts;
4654 get_user_print_options (&opts);
4655 value_print (val, stream, &opts);
4656 }
fa4727a6
DJ
4657}
4658
f303dbd6
PA
4659/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4660 debugging multiple threads. */
4661
4662void
4663maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4664{
112e8700 4665 if (uiout->is_mi_like_p ())
f303dbd6
PA
4666 return;
4667
112e8700 4668 uiout->text ("\n");
f303dbd6
PA
4669
4670 if (show_thread_that_caused_stop ())
4671 {
f303dbd6
PA
4672 struct thread_info *thr = inferior_thread ();
4673
112e8700 4674 uiout->text ("Thread ");
33eca680 4675 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 4676
25558938 4677 const char *name = thread_name (thr);
f303dbd6
PA
4678 if (name != NULL)
4679 {
112e8700 4680 uiout->text (" \"");
33eca680 4681 uiout->field_string ("name", name);
112e8700 4682 uiout->text ("\"");
f303dbd6
PA
4683 }
4684
112e8700 4685 uiout->text (" hit ");
f303dbd6
PA
4686 }
4687}
4688
e514a9d6 4689/* Generic routine for printing messages indicating why we
4a64f543 4690 stopped. The behavior of this function depends on the value
e514a9d6
JM
4691 'print_it' in the bpstat structure. Under some circumstances we
4692 may decide not to print anything here and delegate the task to
4a64f543 4693 normal_stop(). */
e514a9d6
JM
4694
4695static enum print_stop_action
313f3b21 4696print_bp_stop_message (bpstat *bs)
e514a9d6
JM
4697{
4698 switch (bs->print_it)
4699 {
4700 case print_it_noop:
4a64f543 4701 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4702 return PRINT_UNKNOWN;
4703 break;
4704
4705 case print_it_done:
4706 /* We still want to print the frame, but we already printed the
dda83cd7 4707 relevant messages. */
e514a9d6
JM
4708 return PRINT_SRC_AND_LOC;
4709 break;
4710
4711 case print_it_normal:
4f8d1dc6 4712 {
f431efe5
PA
4713 struct breakpoint *b = bs->breakpoint_at;
4714
1a6a67de
TJB
4715 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4716 which has since been deleted. */
4717 if (b == NULL)
4718 return PRINT_UNKNOWN;
4719
348d480f 4720 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 4721 return b->print_it (bs);
4f8d1dc6 4722 }
348d480f 4723 break;
3086aeae 4724
e514a9d6 4725 default:
8e65ff28 4726 internal_error (__FILE__, __LINE__,
e2e0b3e5 4727 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4728 break;
c906108c 4729 }
c906108c
SS
4730}
4731
2f9ee862 4732/* See breakpoint.h. */
edcc5120 4733
2f9ee862 4734void
4e9e993a 4735print_solib_event (bool is_catchpoint)
edcc5120 4736{
6fb16ce6 4737 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4738 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4739
4740 if (!is_catchpoint)
4741 {
4742 if (any_added || any_deleted)
112e8700 4743 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4744 else
112e8700
SM
4745 current_uiout->text (_("Stopped due to shared library event (no "
4746 "libraries added or removed)\n"));
edcc5120
TT
4747 }
4748
112e8700
SM
4749 if (current_uiout->is_mi_like_p ())
4750 current_uiout->field_string ("reason",
4751 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4752
4753 if (any_deleted)
4754 {
112e8700 4755 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4756 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4757 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4758 {
6fb16ce6
SM
4759 const std::string &name = current_program_space->deleted_solibs[ix];
4760
edcc5120 4761 if (ix > 0)
112e8700
SM
4762 current_uiout->text (" ");
4763 current_uiout->field_string ("library", name);
4764 current_uiout->text ("\n");
edcc5120 4765 }
edcc5120
TT
4766 }
4767
4768 if (any_added)
4769 {
112e8700 4770 current_uiout->text (_(" Inferior loaded "));
10f489e5 4771 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4772 bool first = true;
52941706 4773 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4774 {
bcb430e4 4775 if (!first)
112e8700 4776 current_uiout->text (" ");
bcb430e4 4777 first = false;
112e8700
SM
4778 current_uiout->field_string ("library", iter->so_name);
4779 current_uiout->text ("\n");
edcc5120 4780 }
edcc5120
TT
4781 }
4782}
4783
e514a9d6
JM
4784/* Print a message indicating what happened. This is called from
4785 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4786 list - a list of the eventpoints that caused this stop. KIND is
4787 the target_waitkind for the stopping event. This
e514a9d6
JM
4788 routine calls the generic print routine for printing a message
4789 about reasons for stopping. This will print (for example) the
4790 "Breakpoint n," part of the output. The return value of this
4791 routine is one of:
c906108c 4792
4a64f543 4793 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4794 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4795 code to print the location. An example is
c5aa993b
JM
4796 "Breakpoint 1, " which should be followed by
4797 the location.
917317f4 4798 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4799 to also print the location part of the message.
4800 An example is the catch/throw messages, which
4a64f543 4801 don't require a location appended to the end.
917317f4 4802 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4803 further info to be printed. */
c906108c 4804
917317f4 4805enum print_stop_action
403c71fd 4806bpstat_print (bpstat *bs, target_waitkind kind)
c906108c 4807{
f486487f 4808 enum print_stop_action val;
c5aa993b 4809
c906108c 4810 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4811 (Currently all watchpoints go on the bpstat whether hit or not.
4812 That probably could (should) be changed, provided care is taken
c906108c 4813 with respect to bpstat_explains_signal). */
e514a9d6
JM
4814 for (; bs; bs = bs->next)
4815 {
4816 val = print_bp_stop_message (bs);
4817 if (val == PRINT_SRC_ONLY
4818 || val == PRINT_SRC_AND_LOC
4819 || val == PRINT_NOTHING)
4820 return val;
4821 }
c906108c 4822
36dfb11c
TT
4823 /* If we had hit a shared library event breakpoint,
4824 print_bp_stop_message would print out this message. If we hit an
4825 OS-level shared library event, do the same thing. */
4826 if (kind == TARGET_WAITKIND_LOADED)
4827 {
4e9e993a 4828 print_solib_event (false);
36dfb11c
TT
4829 return PRINT_NOTHING;
4830 }
4831
e514a9d6 4832 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4833 with and nothing was printed. */
917317f4 4834 return PRINT_UNKNOWN;
c906108c
SS
4835}
4836
bf469271 4837/* Evaluate the boolean expression EXP and return the result. */
c906108c 4838
bf469271
PA
4839static bool
4840breakpoint_cond_eval (expression *exp)
c906108c 4841{
278cd55f 4842 struct value *mark = value_mark ();
bf469271 4843 bool res = value_true (evaluate_expression (exp));
cc59ec59 4844
c906108c 4845 value_free_to_mark (mark);
bf469271 4846 return res;
c906108c
SS
4847}
4848
5760d0ab 4849/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4850
313f3b21 4851bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 4852 : next (NULL),
b6433ede 4853 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4854 breakpoint_at (bl->owner),
4855 commands (NULL),
04afa70c
TT
4856 print (0),
4857 stop (0),
4858 print_it (print_it_normal)
c906108c 4859{
04afa70c
TT
4860 **bs_link_pointer = this;
4861 *bs_link_pointer = &next;
4862}
4863
313f3b21 4864bpstat::bpstat ()
04afa70c 4865 : next (NULL),
04afa70c
TT
4866 breakpoint_at (NULL),
4867 commands (NULL),
04afa70c
TT
4868 print (0),
4869 stop (0),
4870 print_it (print_it_normal)
4871{
c906108c
SS
4872}
4873\f
d983da9c
DJ
4874/* The target has stopped with waitstatus WS. Check if any hardware
4875 watchpoints have triggered, according to the target. */
4876
4877int
c272a98c 4878watchpoints_triggered (const target_waitstatus &ws)
d983da9c 4879{
57810aa7 4880 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 4881 CORE_ADDR addr;
d983da9c
DJ
4882
4883 if (!stopped_by_watchpoint)
4884 {
4885 /* We were not stopped by a watchpoint. Mark all watchpoints
4886 as not triggered. */
43892fdf 4887 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4888 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4889 {
4890 struct watchpoint *w = (struct watchpoint *) b;
4891
4892 w->watchpoint_triggered = watch_triggered_no;
4893 }
d983da9c
DJ
4894
4895 return 0;
4896 }
4897
328d42d8 4898 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4899 {
4900 /* We were stopped by a watchpoint, but we don't know where.
4901 Mark all watchpoints as unknown. */
43892fdf 4902 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4903 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4904 {
4905 struct watchpoint *w = (struct watchpoint *) b;
4906
4907 w->watchpoint_triggered = watch_triggered_unknown;
4908 }
d983da9c 4909
3c4797ba 4910 return 1;
d983da9c
DJ
4911 }
4912
4913 /* The target could report the data address. Mark watchpoints
4914 affected by this data address as triggered, and all others as not
4915 triggered. */
4916
43892fdf 4917 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4918 if (is_hardware_watchpoint (b))
d983da9c 4919 {
3a5c3e22 4920 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 4921
3a5c3e22 4922 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 4923 for (bp_location *loc : b->locations ())
9c06b0b4 4924 {
3a5c3e22 4925 if (is_masked_watchpoint (b))
9c06b0b4 4926 {
3a5c3e22
PA
4927 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4928 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4929
4930 if (newaddr == start)
4931 {
3a5c3e22 4932 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4933 break;
4934 }
4935 }
4936 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4937 else if (target_watchpoint_addr_within_range
4938 (current_inferior ()->top_target (), addr, loc->address,
4939 loc->length))
9c06b0b4 4940 {
3a5c3e22 4941 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4942 break;
4943 }
4944 }
d983da9c
DJ
4945 }
4946
4947 return 1;
4948}
4949
bf469271
PA
4950/* Possible return values for watchpoint_check. */
4951enum wp_check_result
4952 {
4953 /* The watchpoint has been deleted. */
4954 WP_DELETED = 1,
4955
4956 /* The value has changed. */
4957 WP_VALUE_CHANGED = 2,
4958
4959 /* The value has not changed. */
4960 WP_VALUE_NOT_CHANGED = 3,
4961
4962 /* Ignore this watchpoint, no matter if the value changed or not. */
4963 WP_IGNORE = 4,
4964 };
c906108c
SS
4965
4966#define BP_TEMPFLAG 1
4967#define BP_HARDWAREFLAG 2
4968
4a64f543 4969/* Evaluate watchpoint condition expression and check if its value
bf469271 4970 changed. */
553e4c11 4971
bf469271 4972static wp_check_result
313f3b21 4973watchpoint_check (bpstat *bs)
c906108c 4974{
3a5c3e22 4975 struct watchpoint *b;
c906108c
SS
4976 struct frame_info *fr;
4977 int within_current_scope;
4978
f431efe5 4979 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4980 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4981 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4982
f6bc2008
PA
4983 /* If this is a local watchpoint, we only want to check if the
4984 watchpoint frame is in scope if the current thread is the thread
4985 that was used to create the watchpoint. */
4986 if (!watchpoint_in_thread_scope (b))
60e1c644 4987 return WP_IGNORE;
f6bc2008 4988
c906108c
SS
4989 if (b->exp_valid_block == NULL)
4990 within_current_scope = 1;
4991 else
4992 {
edb3359d
DJ
4993 struct frame_info *frame = get_current_frame ();
4994 struct gdbarch *frame_arch = get_frame_arch (frame);
4995 CORE_ADDR frame_pc = get_frame_pc (frame);
4996
c9cf6e20 4997 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4998 still in the function but the stack frame has already been
4999 invalidated. Since we can't rely on the values of local
5000 variables after the stack has been destroyed, we are treating
5001 the watchpoint in that state as `not changed' without further
5002 checking. Don't mark watchpoints as changed if the current
5003 frame is in an epilogue - even if they are in some other
5004 frame, our view of the stack is likely to be wrong and
5005 frame_find_by_id could error out. */
c9cf6e20 5006 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5007 return WP_IGNORE;
a0f49112 5008
101dcfbe 5009 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5010 within_current_scope = (fr != NULL);
69fbadd5
DJ
5011
5012 /* If we've gotten confused in the unwinder, we might have
5013 returned a frame that can't describe this variable. */
edb3359d
DJ
5014 if (within_current_scope)
5015 {
5016 struct symbol *function;
5017
5018 function = get_frame_function (fr);
5019 if (function == NULL
4aeddc50 5020 || !contained_in (b->exp_valid_block, function->value_block ()))
edb3359d
DJ
5021 within_current_scope = 0;
5022 }
69fbadd5 5023
edb3359d 5024 if (within_current_scope)
c906108c
SS
5025 /* If we end up stopping, the current frame will get selected
5026 in normal_stop. So this call to select_frame won't affect
5027 the user. */
0f7d239c 5028 select_frame (fr);
c906108c 5029 }
c5aa993b 5030
c906108c
SS
5031 if (within_current_scope)
5032 {
4a64f543 5033 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5034 time before we return to the command level and call
5035 free_all_values. We can't call free_all_values because we
5036 might be in the middle of evaluating a function call. */
c906108c 5037
9c06b0b4 5038 struct value *mark;
fa4727a6
DJ
5039 struct value *new_val;
5040
c1fc2657 5041 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5042 /* Since we don't know the exact trigger address (from
5043 stopped_data_address), just tell the user we've triggered
5044 a mask watchpoint. */
5045 return WP_VALUE_CHANGED;
5046
5047 mark = value_mark ();
1eaebe02 5048 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5049 NULL, NULL, false);
218d2fc6 5050
bb9d5f81
PP
5051 if (b->val_bitsize != 0)
5052 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5053
4a64f543
MS
5054 /* We use value_equal_contents instead of value_equal because
5055 the latter coerces an array to a pointer, thus comparing just
5056 the address of the array instead of its contents. This is
5057 not what we want. */
fa4727a6 5058 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5059 || (b->val != NULL && !value_equal_contents (b->val.get (),
5060 new_val)))
c906108c 5061 {
c906108c 5062 bs->old_val = b->val;
850645cf 5063 b->val = release_value (new_val);
4c1d86d9 5064 b->val_valid = true;
850645cf
TT
5065 if (new_val != NULL)
5066 value_free_to_mark (mark);
c906108c
SS
5067 return WP_VALUE_CHANGED;
5068 }
5069 else
5070 {
60e1c644 5071 /* Nothing changed. */
c906108c 5072 value_free_to_mark (mark);
c906108c
SS
5073 return WP_VALUE_NOT_CHANGED;
5074 }
5075 }
5076 else
5077 {
5078 /* This seems like the only logical thing to do because
dda83cd7
SM
5079 if we temporarily ignored the watchpoint, then when
5080 we reenter the block in which it is valid it contains
5081 garbage (in the case of a function, it may have two
5082 garbage values, one before and one after the prologue).
5083 So we can't even detect the first assignment to it and
5084 watch after that (since the garbage may or may not equal
5085 the first value assigned). */
348d480f 5086 /* We print all the stop information in
c47614fe
TT
5087 breakpointprint_it, but in this case, by the time we
5088 call breakpoint->print_it this bp will be deleted
348d480f
PA
5089 already. So we have no choice but print the information
5090 here. */
468afe6c 5091
0e454242 5092 SWITCH_THRU_ALL_UIS ()
dda83cd7 5093 {
468afe6c
PA
5094 struct ui_out *uiout = current_uiout;
5095
112e8700
SM
5096 if (uiout->is_mi_like_p ())
5097 uiout->field_string
5098 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5099 uiout->message ("\nWatchpoint %pF deleted because the program has "
5100 "left the block in\n"
5101 "which its expression is valid.\n",
5102 signed_field ("wpnum", b->number));
468afe6c 5103 }
4ce44c66 5104
cdac0397 5105 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5106 b->commands = NULL;
d0fb5eae 5107 watchpoint_del_at_next_stop (b);
c906108c
SS
5108
5109 return WP_DELETED;
5110 }
5111}
5112
18a18393 5113/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5114 breakpoint location BL. This function does not check if we should
5115 stop, only if BL explains the stop. */
5116
18a18393 5117static int
6c95b8df 5118bpstat_check_location (const struct bp_location *bl,
accd0bcd 5119 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5120 const target_waitstatus &ws)
18a18393
VP
5121{
5122 struct breakpoint *b = bl->owner;
5123
348d480f 5124 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5125 gdb_assert (b != NULL);
5126
c47614fe 5127 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5128}
5129
3a5c3e22
PA
5130/* Determine if the watched values have actually changed, and we
5131 should stop. If not, set BS->stop to 0. */
5132
18a18393 5133static void
313f3b21 5134bpstat_check_watchpoint (bpstat *bs)
18a18393 5135{
2bdf28a0 5136 const struct bp_location *bl;
3a5c3e22 5137 struct watchpoint *b;
2bdf28a0
JK
5138
5139 /* BS is built for existing struct breakpoint. */
b6433ede 5140 bl = bs->bp_location_at.get ();
2bdf28a0 5141 gdb_assert (bl != NULL);
3a5c3e22 5142 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5143 gdb_assert (b != NULL);
18a18393 5144
18a18393 5145 {
18a18393
VP
5146 int must_check_value = 0;
5147
c1fc2657 5148 if (b->type == bp_watchpoint)
18a18393
VP
5149 /* For a software watchpoint, we must always check the
5150 watched value. */
5151 must_check_value = 1;
5152 else if (b->watchpoint_triggered == watch_triggered_yes)
5153 /* We have a hardware watchpoint (read, write, or access)
5154 and the target earlier reported an address watched by
5155 this watchpoint. */
5156 must_check_value = 1;
5157 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5158 && b->type == bp_hardware_watchpoint)
18a18393
VP
5159 /* We were stopped by a hardware watchpoint, but the target could
5160 not report the data address. We must check the watchpoint's
5161 value. Access and read watchpoints are out of luck; without
5162 a data address, we can't figure it out. */
5163 must_check_value = 1;
3a5c3e22 5164
18a18393
VP
5165 if (must_check_value)
5166 {
bf469271
PA
5167 wp_check_result e;
5168
a70b8144 5169 try
bf469271
PA
5170 {
5171 e = watchpoint_check (bs);
5172 }
230d2906 5173 catch (const gdb_exception &ex)
bf469271
PA
5174 {
5175 exception_fprintf (gdb_stderr, ex,
5176 "Error evaluating expression "
5177 "for watchpoint %d\n",
5178 b->number);
5179
5180 SWITCH_THRU_ALL_UIS ()
5181 {
6cb06a8c
TT
5182 gdb_printf (_("Watchpoint %d deleted.\n"),
5183 b->number);
bf469271
PA
5184 }
5185 watchpoint_del_at_next_stop (b);
5186 e = WP_DELETED;
5187 }
bf469271 5188
18a18393
VP
5189 switch (e)
5190 {
5191 case WP_DELETED:
5192 /* We've already printed what needs to be printed. */
5193 bs->print_it = print_it_done;
5194 /* Stop. */
5195 break;
60e1c644
PA
5196 case WP_IGNORE:
5197 bs->print_it = print_it_noop;
5198 bs->stop = 0;
5199 break;
18a18393 5200 case WP_VALUE_CHANGED:
c1fc2657 5201 if (b->type == bp_read_watchpoint)
18a18393 5202 {
85d721b8
PA
5203 /* There are two cases to consider here:
5204
4a64f543 5205 1. We're watching the triggered memory for reads.
85d721b8
PA
5206 In that case, trust the target, and always report
5207 the watchpoint hit to the user. Even though
5208 reads don't cause value changes, the value may
5209 have changed since the last time it was read, and
5210 since we're not trapping writes, we will not see
5211 those, and as such we should ignore our notion of
5212 old value.
5213
4a64f543 5214 2. We're watching the triggered memory for both
85d721b8
PA
5215 reads and writes. There are two ways this may
5216 happen:
5217
4a64f543 5218 2.1. This is a target that can't break on data
85d721b8
PA
5219 reads only, but can break on accesses (reads or
5220 writes), such as e.g., x86. We detect this case
5221 at the time we try to insert read watchpoints.
5222
4a64f543 5223 2.2. Otherwise, the target supports read
85d721b8
PA
5224 watchpoints, but, the user set an access or write
5225 watchpoint watching the same memory as this read
5226 watchpoint.
5227
5228 If we're watching memory writes as well as reads,
5229 ignore watchpoint hits when we find that the
5230 value hasn't changed, as reads don't cause
5231 changes. This still gives false positives when
5232 the program writes the same value to memory as
5233 what there was already in memory (we will confuse
5234 it for a read), but it's much better than
5235 nothing. */
5236
5237 int other_write_watchpoint = 0;
5238
5239 if (bl->watchpoint_type == hw_read)
5240 {
43892fdf 5241 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5242 if (other_b->type == bp_hardware_watchpoint
5243 || other_b->type == bp_access_watchpoint)
85d721b8 5244 {
3a5c3e22
PA
5245 struct watchpoint *other_w =
5246 (struct watchpoint *) other_b;
5247
5248 if (other_w->watchpoint_triggered
5249 == watch_triggered_yes)
5250 {
5251 other_write_watchpoint = 1;
5252 break;
5253 }
85d721b8
PA
5254 }
5255 }
5256
5257 if (other_write_watchpoint
5258 || bl->watchpoint_type == hw_access)
5259 {
5260 /* We're watching the same memory for writes,
5261 and the value changed since the last time we
5262 updated it, so this trap must be for a write.
5263 Ignore it. */
5264 bs->print_it = print_it_noop;
5265 bs->stop = 0;
5266 }
18a18393
VP
5267 }
5268 break;
5269 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5270 if (b->type == bp_hardware_watchpoint
5271 || b->type == bp_watchpoint)
18a18393
VP
5272 {
5273 /* Don't stop: write watchpoints shouldn't fire if
5274 the value hasn't changed. */
5275 bs->print_it = print_it_noop;
5276 bs->stop = 0;
5277 }
5278 /* Stop. */
5279 break;
5280 default:
5281 /* Can't happen. */
18a18393
VP
5282 break;
5283 }
5284 }
5285 else /* must_check_value == 0 */
5286 {
5287 /* This is a case where some watchpoint(s) triggered, but
5288 not at the address of this watchpoint, or else no
5289 watchpoint triggered after all. So don't print
5290 anything for this watchpoint. */
5291 bs->print_it = print_it_noop;
5292 bs->stop = 0;
5293 }
5294 }
5295}
5296
7d4df6a4
DE
5297/* For breakpoints that are currently marked as telling gdb to stop,
5298 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5299 of breakpoint referred to by BS. If we should not stop for this
5300 breakpoint, set BS->stop to 0. */
f431efe5 5301
18a18393 5302static void
313f3b21 5303bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5304{
2bdf28a0
JK
5305 const struct bp_location *bl;
5306 struct breakpoint *b;
bf469271
PA
5307 /* Assume stop. */
5308 bool condition_result = true;
7d4df6a4
DE
5309 struct expression *cond;
5310
5311 gdb_assert (bs->stop);
2bdf28a0
JK
5312
5313 /* BS is built for existing struct breakpoint. */
b6433ede 5314 bl = bs->bp_location_at.get ();
2bdf28a0 5315 gdb_assert (bl != NULL);
f431efe5 5316 b = bs->breakpoint_at;
2bdf28a0 5317 gdb_assert (b != NULL);
18a18393 5318
b775012e
LM
5319 /* Even if the target evaluated the condition on its end and notified GDB, we
5320 need to do so again since GDB does not know if we stopped due to a
5321 breakpoint or a single step breakpoint. */
5322
18a18393 5323 if (frame_id_p (b->frame_id)
edb3359d 5324 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5325 {
7d4df6a4
DE
5326 bs->stop = 0;
5327 return;
5328 }
60e1c644 5329
12ab52e9
PA
5330 /* If this is a thread/task-specific breakpoint, don't waste cpu
5331 evaluating the condition if this isn't the specified
5332 thread/task. */
00431a78
PA
5333 if ((b->thread != -1 && b->thread != thread->global_num)
5334 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5335 {
5336 bs->stop = 0;
5337 return;
5338 }
5339
6dddc817
DE
5340 /* Evaluate extension language breakpoints that have a "stop" method
5341 implemented. */
5342 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5343
7d4df6a4
DE
5344 if (is_watchpoint (b))
5345 {
5346 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5347
4d01a485 5348 cond = w->cond_exp.get ();
7d4df6a4
DE
5349 }
5350 else
4d01a485 5351 cond = bl->cond.get ();
60e1c644 5352
7d4df6a4
DE
5353 if (cond && b->disposition != disp_del_at_next_stop)
5354 {
5355 int within_current_scope = 1;
5356 struct watchpoint * w;
60e1c644 5357
7d4df6a4
DE
5358 /* We use value_mark and value_free_to_mark because it could
5359 be a long time before we return to the command level and
5360 call free_all_values. We can't call free_all_values
5361 because we might be in the middle of evaluating a
5362 function call. */
5363 struct value *mark = value_mark ();
5364
5365 if (is_watchpoint (b))
5366 w = (struct watchpoint *) b;
5367 else
5368 w = NULL;
5369
5370 /* Need to select the frame, with all that implies so that
5371 the conditions will have the right context. Because we
5372 use the frame, we will not see an inlined function's
5373 variables when we arrive at a breakpoint at the start
5374 of the inlined function; the current frame will be the
5375 call site. */
5376 if (w == NULL || w->cond_exp_valid_block == NULL)
5377 select_frame (get_current_frame ());
5378 else
18a18393 5379 {
7d4df6a4
DE
5380 struct frame_info *frame;
5381
5382 /* For local watchpoint expressions, which particular
5383 instance of a local is being watched matters, so we
5384 keep track of the frame to evaluate the expression
5385 in. To evaluate the condition however, it doesn't
5386 really matter which instantiation of the function
5387 where the condition makes sense triggers the
5388 watchpoint. This allows an expression like "watch
5389 global if q > 10" set in `func', catch writes to
5390 global on all threads that call `func', or catch
5391 writes on all recursive calls of `func' by a single
5392 thread. We simply always evaluate the condition in
5393 the innermost frame that's executing where it makes
5394 sense to evaluate the condition. It seems
5395 intuitive. */
5396 frame = block_innermost_frame (w->cond_exp_valid_block);
5397 if (frame != NULL)
5398 select_frame (frame);
5399 else
5400 within_current_scope = 0;
18a18393 5401 }
7d4df6a4 5402 if (within_current_scope)
bf469271 5403 {
a70b8144 5404 try
bf469271
PA
5405 {
5406 condition_result = breakpoint_cond_eval (cond);
5407 }
230d2906 5408 catch (const gdb_exception &ex)
bf469271
PA
5409 {
5410 exception_fprintf (gdb_stderr, ex,
5411 "Error in testing breakpoint condition:\n");
5412 }
bf469271 5413 }
7d4df6a4 5414 else
18a18393 5415 {
7d4df6a4
DE
5416 warning (_("Watchpoint condition cannot be tested "
5417 "in the current scope"));
5418 /* If we failed to set the right context for this
5419 watchpoint, unconditionally report it. */
18a18393 5420 }
7d4df6a4
DE
5421 /* FIXME-someday, should give breakpoint #. */
5422 value_free_to_mark (mark);
18a18393 5423 }
7d4df6a4 5424
bf469271 5425 if (cond && !condition_result)
7d4df6a4
DE
5426 {
5427 bs->stop = 0;
5428 }
7d4df6a4
DE
5429 else if (b->ignore_count > 0)
5430 {
5431 b->ignore_count--;
5432 bs->stop = 0;
5433 /* Increase the hit count even though we don't stop. */
5434 ++(b->hit_count);
76727919 5435 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5436 }
18a18393
VP
5437}
5438
1cf4d951
PA
5439/* Returns true if we need to track moribund locations of LOC's type
5440 on the current target. */
5441
5442static int
5443need_moribund_for_location_type (struct bp_location *loc)
5444{
5445 return ((loc->loc_type == bp_loc_software_breakpoint
5446 && !target_supports_stopped_by_sw_breakpoint ())
5447 || (loc->loc_type == bp_loc_hardware_breakpoint
5448 && !target_supports_stopped_by_hw_breakpoint ()));
5449}
5450
ddfe970e 5451/* See breakpoint.h. */
c906108c 5452
313f3b21 5453bpstat *
ddfe970e 5454build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5455 const target_waitstatus &ws)
c906108c 5456{
313f3b21 5457 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5458
43892fdf 5459 for (breakpoint *b : all_breakpoints ())
429374b8 5460 {
1a853c52 5461 if (!breakpoint_enabled (b))
429374b8 5462 continue;
a5606eee 5463
40cb8ca5 5464 for (bp_location *bl : b->locations ())
429374b8 5465 {
4a64f543
MS
5466 /* For hardware watchpoints, we look only at the first
5467 location. The watchpoint_check function will work on the
5468 entire expression, not the individual locations. For
5469 read watchpoints, the watchpoints_triggered function has
5470 checked all locations already. */
429374b8
JK
5471 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5472 break;
18a18393 5473
b5fa468f 5474 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5475 continue;
c5aa993b 5476
09ac7c10 5477 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5478 continue;
c5aa993b 5479
4a64f543
MS
5480 /* Come here if it's a watchpoint, or if the break address
5481 matches. */
c5aa993b 5482
313f3b21 5483 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5484 explain stop. */
c5aa993b 5485
f431efe5
PA
5486 /* Assume we stop. Should we find a watchpoint that is not
5487 actually triggered, or if the condition of the breakpoint
5488 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5489 bs->stop = 1;
5490 bs->print = 1;
d983da9c 5491
f431efe5
PA
5492 /* If this is a scope breakpoint, mark the associated
5493 watchpoint as triggered so that we will handle the
5494 out-of-scope event. We'll get to the watchpoint next
5495 iteration. */
d0fb5eae 5496 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5497 {
5498 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5499
5500 w->watchpoint_triggered = watch_triggered_yes;
5501 }
f431efe5
PA
5502 }
5503 }
5504
7c16b83e 5505 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5506 if (!target_supports_stopped_by_sw_breakpoint ()
5507 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5508 {
1123588c 5509 for (bp_location *loc : moribund_locations)
f431efe5 5510 {
1cf4d951
PA
5511 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5512 && need_moribund_for_location_type (loc))
5513 {
313f3b21 5514 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951
PA
5515 /* For hits of moribund locations, we should just proceed. */
5516 bs->stop = 0;
5517 bs->print = 0;
5518 bs->print_it = print_it_noop;
5519 }
f431efe5
PA
5520 }
5521 }
5522
ddfe970e
KS
5523 return bs_head;
5524}
5525
5526/* See breakpoint.h. */
5527
313f3b21 5528bpstat *
ddfe970e 5529bpstat_stop_status (const address_space *aspace,
00431a78 5530 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5531 const target_waitstatus &ws,
313f3b21 5532 bpstat *stop_chain)
ddfe970e
KS
5533{
5534 struct breakpoint *b = NULL;
5535 /* First item of allocated bpstat's. */
313f3b21
SM
5536 bpstat *bs_head = stop_chain;
5537 bpstat *bs;
ddfe970e
KS
5538 int need_remove_insert;
5539 int removed_any;
5540
5541 /* First, build the bpstat chain with locations that explain a
5542 target stop, while being careful to not set the target running,
5543 as that may invalidate locations (in particular watchpoint
5544 locations are recreated). Resuming will happen here with
5545 breakpoint conditions or watchpoint expressions that include
5546 inferior function calls. */
5547 if (bs_head == NULL)
5548 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5549
edcc5120
TT
5550 /* A bit of special processing for shlib breakpoints. We need to
5551 process solib loading here, so that the lists of loaded and
5552 unloaded libraries are correct before we handle "catch load" and
5553 "catch unload". */
5554 for (bs = bs_head; bs != NULL; bs = bs->next)
5555 {
5d268276 5556 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5557 {
5558 handle_solib_event ();
5559 break;
5560 }
5561 }
5562
f431efe5
PA
5563 /* Now go through the locations that caused the target to stop, and
5564 check whether we're interested in reporting this stop to higher
5565 layers, or whether we should resume the target transparently. */
5566
5567 removed_any = 0;
5568
5760d0ab 5569 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5570 {
5571 if (!bs->stop)
5572 continue;
5573
f431efe5 5574 b = bs->breakpoint_at;
c47614fe 5575 b->check_status (bs);
348d480f 5576 if (bs->stop)
28010a5d 5577 {
00431a78 5578 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5579
429374b8
JK
5580 if (bs->stop)
5581 {
5582 ++(b->hit_count);
c906108c 5583
4a64f543 5584 /* We will stop here. */
429374b8
JK
5585 if (b->disposition == disp_disable)
5586 {
816338b5 5587 --(b->enable_count);
1a853c52 5588 if (b->enable_count <= 0)
429374b8 5589 b->enable_state = bp_disabled;
f431efe5 5590 removed_any = 1;
429374b8 5591 }
bd7ccaa9 5592 gdb::observers::breakpoint_modified.notify (b);
429374b8
JK
5593 if (b->silent)
5594 bs->print = 0;
5595 bs->commands = b->commands;
abf85f46 5596 if (command_line_is_silent (bs->commands
d1b0a7bf 5597 ? bs->commands.get () : NULL))
abf85f46 5598 bs->print = 0;
9d6e6e84 5599
c47614fe 5600 b->after_condition_true (bs);
429374b8
JK
5601 }
5602
348d480f 5603 }
a9b3a50f
PA
5604
5605 /* Print nothing for this entry if we don't stop or don't
5606 print. */
5607 if (!bs->stop || !bs->print)
5608 bs->print_it = print_it_noop;
429374b8 5609 }
876fa593 5610
d983da9c
DJ
5611 /* If we aren't stopping, the value of some hardware watchpoint may
5612 not have changed, but the intermediate memory locations we are
5613 watching may have. Don't bother if we're stopping; this will get
5614 done later. */
d832cb68 5615 need_remove_insert = 0;
5760d0ab
JK
5616 if (! bpstat_causes_stop (bs_head))
5617 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5618 if (!bs->stop
f431efe5
PA
5619 && bs->breakpoint_at
5620 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5621 {
3a5c3e22
PA
5622 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5623
5624 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5625 need_remove_insert = 1;
d983da9c
DJ
5626 }
5627
d832cb68 5628 if (need_remove_insert)
44702360 5629 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5630 else if (removed_any)
44702360 5631 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5632
5760d0ab 5633 return bs_head;
c906108c 5634}
628fe4e4 5635
d37e0847
PA
5636/* See breakpoint.h. */
5637
5638bpstat *
5639bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5640 thread_info *thread, const target_waitstatus &ws)
5641{
5642 gdb_assert (!target_stopped_by_watchpoint ());
5643
5644 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5645 previous stop to avoid confusing bpstat_stop_status. */
5646 watchpoints_triggered (ws);
5647
5648 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5649}
5650
628fe4e4 5651static void
61c26be8 5652handle_jit_event (CORE_ADDR address)
628fe4e4 5653{
628fe4e4
JK
5654 struct gdbarch *gdbarch;
5655
1eb8556f 5656 infrun_debug_printf ("handling bp_jit_event");
243a9253 5657
628fe4e4
JK
5658 /* Switch terminal for any messages produced by
5659 breakpoint_re_set. */
223ffa71 5660 target_terminal::ours_for_output ();
628fe4e4 5661
61c26be8
MS
5662 gdbarch = get_frame_arch (get_current_frame ());
5663 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5664 thus it is expected that its objectfile can be found through
5665 minimal symbol lookup. If it doesn't work (and assert fails), it
5666 most likely means that `jit_breakpoint_re_set` was changes and this
5667 function needs to be updated too. */
5668 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5669 gdb_assert (jit_bp_sym.objfile != nullptr);
5670 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5671
223ffa71 5672 target_terminal::inferior ();
628fe4e4
JK
5673}
5674
5675/* Prepare WHAT final decision for infrun. */
5676
5677/* Decide what infrun needs to do with this bpstat. */
5678
c906108c 5679struct bpstat_what
313f3b21 5680bpstat_what (bpstat *bs_head)
c906108c 5681{
c906108c 5682 struct bpstat_what retval;
313f3b21 5683 bpstat *bs;
c906108c 5684
628fe4e4 5685 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5686 retval.call_dummy = STOP_NONE;
e2d0f980 5687 retval.is_longjmp = false;
628fe4e4 5688
0e30163f 5689 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5690 {
628fe4e4
JK
5691 /* Extract this BS's action. After processing each BS, we check
5692 if its action overrides all we've seem so far. */
5693 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5694 enum bptype bptype;
5695
c906108c 5696 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5697 {
5698 /* I suspect this can happen if it was a momentary
5699 breakpoint which has since been deleted. */
5700 bptype = bp_none;
5701 }
20874c92 5702 else
f431efe5 5703 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5704
5705 switch (bptype)
c906108c
SS
5706 {
5707 case bp_none:
628fe4e4 5708 break;
c906108c
SS
5709 case bp_breakpoint:
5710 case bp_hardware_breakpoint:
7c16b83e 5711 case bp_single_step:
c906108c
SS
5712 case bp_until:
5713 case bp_finish:
a9b3a50f 5714 case bp_shlib_event:
c906108c
SS
5715 if (bs->stop)
5716 {
5717 if (bs->print)
628fe4e4 5718 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5719 else
628fe4e4 5720 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5721 }
5722 else
628fe4e4 5723 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5724 break;
5725 case bp_watchpoint:
5726 case bp_hardware_watchpoint:
5727 case bp_read_watchpoint:
5728 case bp_access_watchpoint:
5729 if (bs->stop)
5730 {
5731 if (bs->print)
628fe4e4 5732 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5733 else
628fe4e4 5734 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5735 }
5736 else
628fe4e4
JK
5737 {
5738 /* There was a watchpoint, but we're not stopping.
5739 This requires no further action. */
5740 }
c906108c
SS
5741 break;
5742 case bp_longjmp:
e2e4d78b 5743 case bp_longjmp_call_dummy:
186c406b 5744 case bp_exception:
0a39bb32
PA
5745 if (bs->stop)
5746 {
5747 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5748 retval.is_longjmp = bptype != bp_exception;
5749 }
5750 else
5751 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5752 break;
5753 case bp_longjmp_resume:
186c406b 5754 case bp_exception_resume:
0a39bb32
PA
5755 if (bs->stop)
5756 {
5757 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5758 retval.is_longjmp = bptype == bp_longjmp_resume;
5759 }
5760 else
5761 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5762 break;
5763 case bp_step_resume:
5764 if (bs->stop)
628fe4e4
JK
5765 this_action = BPSTAT_WHAT_STEP_RESUME;
5766 else
c906108c 5767 {
628fe4e4
JK
5768 /* It is for the wrong frame. */
5769 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5770 }
c906108c 5771 break;
2c03e5be
PA
5772 case bp_hp_step_resume:
5773 if (bs->stop)
5774 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5775 else
5776 {
5777 /* It is for the wrong frame. */
5778 this_action = BPSTAT_WHAT_SINGLE;
5779 }
5780 break;
c906108c 5781 case bp_watchpoint_scope:
c4093a6a 5782 case bp_thread_event:
1900040c 5783 case bp_overlay_event:
0fd8e87f 5784 case bp_longjmp_master:
aa7d318d 5785 case bp_std_terminate_master:
186c406b 5786 case bp_exception_master:
628fe4e4 5787 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5788 break;
ce78b96d 5789 case bp_catchpoint:
c5aa993b
JM
5790 if (bs->stop)
5791 {
5792 if (bs->print)
628fe4e4 5793 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5794 else
628fe4e4 5795 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5796 }
5797 else
628fe4e4 5798 {
cb1e4e32
PA
5799 /* Some catchpoints are implemented with breakpoints.
5800 For those, we need to step over the breakpoint. */
6e14e441
PA
5801 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5802 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
cb1e4e32 5803 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5804 }
5805 break;
628fe4e4 5806 case bp_jit_event:
628fe4e4 5807 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5808 break;
c906108c 5809 case bp_call_dummy:
53a5351d
JM
5810 /* Make sure the action is stop (silent or noisy),
5811 so infrun.c pops the dummy frame. */
aa7d318d 5812 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5813 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5814 break;
5815 case bp_std_terminate:
5816 /* Make sure the action is stop (silent or noisy),
5817 so infrun.c pops the dummy frame. */
aa7d318d 5818 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5819 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5820 break;
1042e4c0 5821 case bp_tracepoint:
7a697b8d 5822 case bp_fast_tracepoint:
0fb4aa4b 5823 case bp_static_tracepoint:
7b572efb 5824 case bp_static_marker_tracepoint:
1042e4c0
SS
5825 /* Tracepoint hits should not be reported back to GDB, and
5826 if one got through somehow, it should have been filtered
5827 out already. */
5828 internal_error (__FILE__, __LINE__,
7a697b8d 5829 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5830 break;
5831 case bp_gnu_ifunc_resolver:
5832 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5833 this_action = BPSTAT_WHAT_SINGLE;
5834 break;
5835 case bp_gnu_ifunc_resolver_return:
5836 /* The breakpoint will be removed, execution will restart from the
5837 PC of the former breakpoint. */
5838 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5839 break;
e7e0cddf
SS
5840
5841 case bp_dprintf:
a11cfd87
HZ
5842 if (bs->stop)
5843 this_action = BPSTAT_WHAT_STOP_SILENT;
5844 else
5845 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5846 break;
5847
628fe4e4
JK
5848 default:
5849 internal_error (__FILE__, __LINE__,
5850 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5851 }
628fe4e4 5852
325fac50 5853 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5854 }
628fe4e4 5855
243a9253
PA
5856 return retval;
5857}
628fe4e4 5858
243a9253 5859void
313f3b21 5860bpstat_run_callbacks (bpstat *bs_head)
243a9253 5861{
313f3b21 5862 bpstat *bs;
628fe4e4 5863
0e30163f
JK
5864 for (bs = bs_head; bs != NULL; bs = bs->next)
5865 {
5866 struct breakpoint *b = bs->breakpoint_at;
5867
5868 if (b == NULL)
5869 continue;
5870 switch (b->type)
5871 {
243a9253 5872 case bp_jit_event:
61c26be8 5873 handle_jit_event (bs->bp_location_at->address);
243a9253 5874 break;
0e30163f 5875 case bp_gnu_ifunc_resolver:
74421c0b 5876 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
0e30163f
JK
5877 break;
5878 case bp_gnu_ifunc_resolver_return:
74421c0b 5879 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
0e30163f
JK
5880 break;
5881 }
5882 }
c906108c
SS
5883}
5884
4c462cb0 5885/* See breakpoint.h. */
c906108c 5886
4c462cb0
SM
5887bool
5888bpstat_should_step ()
c906108c 5889{
43892fdf 5890 for (breakpoint *b : all_breakpoints ())
717a8278 5891 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 5892 return true;
43892fdf 5893
4c462cb0 5894 return false;
c906108c
SS
5895}
5896
4c462cb0
SM
5897/* See breakpoint.h. */
5898
5899bool
313f3b21 5900bpstat_causes_stop (bpstat *bs)
67822962
PA
5901{
5902 for (; bs != NULL; bs = bs->next)
5903 if (bs->stop)
4c462cb0 5904 return true;
67822962 5905
4c462cb0 5906 return false;
67822962
PA
5907}
5908
c906108c 5909\f
c5aa993b 5910
6c92c339 5911/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
5912 so it starts at the position corresponding to the table column
5913 named COL_NAME in the currently active table of UIOUT. */
5914
6c92c339 5915static int
170b53b2
UW
5916wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5917{
170b53b2 5918 int i, total_width, width, align;
c5209615 5919 const char *text;
170b53b2
UW
5920
5921 total_width = 0;
112e8700 5922 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5923 {
5924 if (strcmp (text, col_name) == 0)
6c92c339 5925 return total_width;
170b53b2
UW
5926
5927 total_width += width + 1;
5928 }
5929
6c92c339 5930 return 0;
170b53b2
UW
5931}
5932
b775012e
LM
5933/* Determine if the locations of this breakpoint will have their conditions
5934 evaluated by the target, host or a mix of both. Returns the following:
5935
5936 "host": Host evals condition.
5937 "host or target": Host or Target evals condition.
5938 "target": Target evals condition.
5939*/
5940
5941static const char *
a67bcaba 5942bp_condition_evaluator (const breakpoint *b)
b775012e 5943{
b775012e
LM
5944 char host_evals = 0;
5945 char target_evals = 0;
5946
5947 if (!b)
5948 return NULL;
5949
5950 if (!is_breakpoint (b))
5951 return NULL;
5952
5953 if (gdb_evaluates_breakpoint_condition_p ()
5954 || !target_supports_evaluation_of_breakpoint_conditions ())
5955 return condition_evaluation_host;
5956
40cb8ca5 5957 for (bp_location *bl : b->locations ())
b775012e
LM
5958 {
5959 if (bl->cond_bytecode)
5960 target_evals++;
5961 else
5962 host_evals++;
5963 }
5964
5965 if (host_evals && target_evals)
5966 return condition_evaluation_both;
5967 else if (target_evals)
5968 return condition_evaluation_target;
5969 else
5970 return condition_evaluation_host;
5971}
5972
5973/* Determine the breakpoint location's condition evaluator. This is
5974 similar to bp_condition_evaluator, but for locations. */
5975
5976static const char *
5977bp_location_condition_evaluator (struct bp_location *bl)
5978{
5979 if (bl && !is_breakpoint (bl->owner))
5980 return NULL;
5981
5982 if (gdb_evaluates_breakpoint_condition_p ()
5983 || !target_supports_evaluation_of_breakpoint_conditions ())
5984 return condition_evaluation_host;
5985
5986 if (bl && bl->cond_bytecode)
5987 return condition_evaluation_target;
5988 else
5989 return condition_evaluation_host;
5990}
5991
859825b8
JK
5992/* Print the LOC location out of the list of B->LOC locations. */
5993
170b53b2 5994static void
a67bcaba 5995print_breakpoint_location (const breakpoint *b,
170b53b2 5996 struct bp_location *loc)
0d381245 5997{
79a45e25 5998 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5999
6000 scoped_restore_current_program_space restore_pspace;
6c95b8df 6001
859825b8
JK
6002 if (loc != NULL && loc->shlib_disabled)
6003 loc = NULL;
6004
6c95b8df
PA
6005 if (loc != NULL)
6006 set_current_program_space (loc->pspace);
6007
56435ebe 6008 if (b->display_canonical)
709438c7 6009 uiout->field_string ("what", b->locspec->to_string ());
2f202fde 6010 else if (loc && loc->symtab)
0d381245 6011 {
4a27f119
KS
6012 const struct symbol *sym = loc->symbol;
6013
0d381245
VP
6014 if (sym)
6015 {
112e8700 6016 uiout->text ("in ");
987012b8 6017 uiout->field_string ("func", sym->print_name (),
e43b10e1 6018 function_name_style.style ());
112e8700
SM
6019 uiout->text (" ");
6020 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6021 uiout->text ("at ");
0d381245 6022 }
112e8700 6023 uiout->field_string ("file",
cbe56571 6024 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6025 file_name_style.style ());
112e8700 6026 uiout->text (":");
05cba821 6027
112e8700
SM
6028 if (uiout->is_mi_like_p ())
6029 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6030
381befee 6031 uiout->field_signed ("line", loc->line_number);
0d381245 6032 }
859825b8 6033 else if (loc)
0d381245 6034 {
d7e74731 6035 string_file stb;
170b53b2 6036
d7e74731 6037 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6038 demangle, "");
112e8700 6039 uiout->field_stream ("at", stb);
0d381245 6040 }
859825b8 6041 else
f00aae0f 6042 {
709438c7 6043 uiout->field_string ("pending", b->locspec->to_string ());
f00aae0f
KS
6044 /* If extra_string is available, it could be holding a condition
6045 or dprintf arguments. In either case, make sure it is printed,
6046 too, but only for non-MI streams. */
112e8700 6047 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6048 {
6049 if (b->type == bp_dprintf)
112e8700 6050 uiout->text (",");
f00aae0f 6051 else
112e8700 6052 uiout->text (" ");
6f781ee3 6053 uiout->text (b->extra_string.get ());
f00aae0f
KS
6054 }
6055 }
6c95b8df 6056
b775012e
LM
6057 if (loc && is_breakpoint (b)
6058 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6059 && bp_condition_evaluator (b) == condition_evaluation_both)
6060 {
112e8700
SM
6061 uiout->text (" (");
6062 uiout->field_string ("evaluated-by",
b775012e 6063 bp_location_condition_evaluator (loc));
112e8700 6064 uiout->text (")");
b775012e 6065 }
0d381245
VP
6066}
6067
269b11a2
PA
6068static const char *
6069bptype_string (enum bptype type)
c906108c 6070{
c4093a6a
JM
6071 struct ep_type_description
6072 {
6073 enum bptype type;
a121b7c1 6074 const char *description;
c4093a6a
JM
6075 };
6076 static struct ep_type_description bptypes[] =
c906108c 6077 {
c5aa993b
JM
6078 {bp_none, "?deleted?"},
6079 {bp_breakpoint, "breakpoint"},
c906108c 6080 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6081 {bp_single_step, "sw single-step"},
c5aa993b
JM
6082 {bp_until, "until"},
6083 {bp_finish, "finish"},
6084 {bp_watchpoint, "watchpoint"},
c906108c 6085 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6086 {bp_read_watchpoint, "read watchpoint"},
6087 {bp_access_watchpoint, "acc watchpoint"},
6088 {bp_longjmp, "longjmp"},
6089 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6090 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6091 {bp_exception, "exception"},
6092 {bp_exception_resume, "exception resume"},
c5aa993b 6093 {bp_step_resume, "step resume"},
2c03e5be 6094 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6095 {bp_watchpoint_scope, "watchpoint scope"},
6096 {bp_call_dummy, "call dummy"},
aa7d318d 6097 {bp_std_terminate, "std::terminate"},
c5aa993b 6098 {bp_shlib_event, "shlib events"},
c4093a6a 6099 {bp_thread_event, "thread events"},
1900040c 6100 {bp_overlay_event, "overlay events"},
0fd8e87f 6101 {bp_longjmp_master, "longjmp master"},
aa7d318d 6102 {bp_std_terminate_master, "std::terminate master"},
186c406b 6103 {bp_exception_master, "exception master"},
ce78b96d 6104 {bp_catchpoint, "catchpoint"},
1042e4c0 6105 {bp_tracepoint, "tracepoint"},
7a697b8d 6106 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6107 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6108 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6109 {bp_dprintf, "dprintf"},
4efc6507 6110 {bp_jit_event, "jit events"},
0e30163f
JK
6111 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6112 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6113 };
269b11a2
PA
6114
6115 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6116 || ((int) type != bptypes[(int) type].type))
6117 internal_error (__FILE__, __LINE__,
6118 _("bptypes table does not describe type #%d."),
6119 (int) type);
6120
6121 return bptypes[(int) type].description;
6122}
6123
998580f1
MK
6124/* For MI, output a field named 'thread-groups' with a list as the value.
6125 For CLI, prefix the list with the string 'inf'. */
6126
6127static void
6128output_thread_groups (struct ui_out *uiout,
6129 const char *field_name,
5c632425 6130 const std::vector<int> &inf_nums,
998580f1
MK
6131 int mi_only)
6132{
112e8700 6133 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6134
6135 /* For backward compatibility, don't display inferiors in CLI unless
6136 there are several. Always display them for MI. */
6137 if (!is_mi && mi_only)
6138 return;
6139
10f489e5 6140 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6141
5c632425 6142 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6143 {
6144 if (is_mi)
6145 {
6146 char mi_group[10];
6147
5c632425 6148 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6149 uiout->field_string (NULL, mi_group);
998580f1
MK
6150 }
6151 else
6152 {
6153 if (i == 0)
112e8700 6154 uiout->text (" inf ");
998580f1 6155 else
112e8700 6156 uiout->text (", ");
998580f1 6157
5c632425 6158 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6159 }
6160 }
998580f1
MK
6161}
6162
9db0d853
SM
6163/* See breakpoint.h. */
6164
6165bool fix_breakpoint_script_output_globally = false;
6166
a38118e5
PA
6167/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6168 instead of going via breakpoint_ops::print_one. This makes "maint
6169 info breakpoints" show the software breakpoint locations of
6170 catchpoints, which are considered internal implementation
c01e038b
TT
6171 detail. Returns true if RAW_LOC is false and if the breakpoint's
6172 print_one method did something; false otherwise. */
269b11a2 6173
c01e038b 6174static bool
269b11a2
PA
6175print_one_breakpoint_location (struct breakpoint *b,
6176 struct bp_location *loc,
6177 int loc_number,
6178 struct bp_location **last_loc,
a38118e5 6179 int allflag, bool raw_loc)
269b11a2
PA
6180{
6181 struct command_line *l;
c2c6d25f 6182 static char bpenables[] = "nynny";
c906108c 6183
79a45e25 6184 struct ui_out *uiout = current_uiout;
0d381245
VP
6185 int header_of_multiple = 0;
6186 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6187 struct value_print_options opts;
6188
6189 get_user_print_options (&opts);
0d381245
VP
6190
6191 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6192 /* See comment in print_one_breakpoint concerning treatment of
6193 breakpoints with single disabled location. */
0d381245
VP
6194 if (loc == NULL
6195 && (b->loc != NULL
b5fa468f
TBA
6196 && (b->loc->next != NULL
6197 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6198 header_of_multiple = 1;
6199 if (loc == NULL)
6200 loc = b->loc;
6201
c4093a6a
JM
6202 annotate_record ();
6203
6204 /* 1 */
6205 annotate_field (0);
0d381245 6206 if (part_of_multiple)
528e1572 6207 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6208 else
381befee 6209 uiout->field_signed ("number", b->number);
c4093a6a
JM
6210
6211 /* 2 */
6212 annotate_field (1);
0d381245 6213 if (part_of_multiple)
112e8700 6214 uiout->field_skip ("type");
269b11a2 6215 else
112e8700 6216 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6217
6218 /* 3 */
6219 annotate_field (2);
0d381245 6220 if (part_of_multiple)
112e8700 6221 uiout->field_skip ("disp");
0d381245 6222 else
112e8700 6223 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6224
c4093a6a
JM
6225 /* 4 */
6226 annotate_field (3);
0d381245 6227 if (part_of_multiple)
fbcda577
PA
6228 {
6229 /* For locations that are disabled because of an invalid
6230 condition, display "N*" on the CLI, where "*" refers to a
6231 footnote below the table. For MI, simply display a "N"
6232 without a footnote. On the CLI, for enabled locations whose
6233 breakpoint is disabled, display "y-". */
6234 auto get_enable_state = [uiout, loc] () -> const char *
6235 {
6236 if (uiout->is_mi_like_p ())
6237 {
6238 if (loc->disabled_by_cond)
6239 return "N";
6240 else if (!loc->enabled)
6241 return "n";
6242 else
6243 return "y";
6244 }
6245 else
6246 {
6247 if (loc->disabled_by_cond)
6248 return "N*";
6249 else if (!loc->enabled)
6250 return "n";
6251 else if (!breakpoint_enabled (loc->owner))
6252 return "y-";
6253 else
6254 return "y";
6255 }
6256 };
6257 uiout->field_string ("enabled", get_enable_state ());
6258 }
0d381245 6259 else
112e8700 6260 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6261
c4093a6a 6262 /* 5 and 6 */
c01e038b 6263 bool result = false;
c47614fe 6264 if (!raw_loc && b->print_one (last_loc))
c01e038b 6265 result = true;
3086aeae 6266 else
a38118e5
PA
6267 {
6268 if (is_watchpoint (b))
3a5c3e22
PA
6269 {
6270 struct watchpoint *w = (struct watchpoint *) b;
6271
6272 /* Field 4, the address, is omitted (which makes the columns
6273 not line up too nicely with the headers, but the effect
6274 is relatively readable). */
6275 if (opts.addressprint)
112e8700 6276 uiout->field_skip ("addr");
3a5c3e22 6277 annotate_field (5);
a4c50be3 6278 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6279 }
f06f1252
TT
6280 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6281 || is_ada_exception_catchpoint (b))
a38118e5
PA
6282 {
6283 if (opts.addressprint)
6284 {
6285 annotate_field (4);
6286 if (header_of_multiple)
7f6aba03
TT
6287 uiout->field_string ("addr", "<MULTIPLE>",
6288 metadata_style.style ());
a38118e5 6289 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6290 uiout->field_string ("addr", "<PENDING>",
6291 metadata_style.style ());
a38118e5
PA
6292 else
6293 uiout->field_core_addr ("addr",
6294 loc->gdbarch, loc->address);
6295 }
6296 annotate_field (5);
6297 if (!header_of_multiple)
6298 print_breakpoint_location (b, loc);
6299 if (b->loc)
6300 *last_loc = b->loc;
6301 }
6302 }
6c95b8df 6303
998580f1 6304 if (loc != NULL && !header_of_multiple)
6c95b8df 6305 {
5c632425 6306 std::vector<int> inf_nums;
998580f1 6307 int mi_only = 1;
6c95b8df 6308
08036331 6309 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6310 {
6311 if (inf->pspace == loc->pspace)
5c632425 6312 inf_nums.push_back (inf->num);
6c95b8df 6313 }
998580f1 6314
dda83cd7 6315 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6316 there are several. Always display for MI. */
6317 if (allflag
6318 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6319 && (program_spaces.size () > 1
998580f1
MK
6320 || number_of_inferiors () > 1)
6321 /* LOC is for existing B, it cannot be in
6322 moribund_locations and thus having NULL OWNER. */
6323 && loc->owner->type != bp_catchpoint))
6324 mi_only = 0;
5c632425 6325 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6326 }
6327
4a306c9a 6328 if (!part_of_multiple)
c4093a6a 6329 {
4a306c9a
JB
6330 if (b->thread != -1)
6331 {
6332 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6333 "stop only in" line a little further down. */
112e8700 6334 uiout->text (" thread ");
381befee 6335 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6336 }
6337 else if (b->task != 0)
6338 {
112e8700 6339 uiout->text (" task ");
381befee 6340 uiout->field_signed ("task", b->task);
4a306c9a 6341 }
c4093a6a 6342 }
f1310107 6343
112e8700 6344 uiout->text ("\n");
f1310107 6345
348d480f 6346 if (!part_of_multiple)
c47614fe 6347 b->print_one_detail (uiout);
f1310107 6348
0d381245 6349 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6350 {
6351 annotate_field (6);
112e8700 6352 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6353 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6354 the frame ID. */
112e8700 6355 uiout->field_core_addr ("frame",
5af949e3 6356 b->gdbarch, b->frame_id.stack_addr);
112e8700 6357 uiout->text ("\n");
c4093a6a
JM
6358 }
6359
28010a5d 6360 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6361 {
6362 annotate_field (7);
d77f58be 6363 if (is_tracepoint (b))
112e8700 6364 uiout->text ("\ttrace only if ");
1042e4c0 6365 else
112e8700 6366 uiout->text ("\tstop only if ");
6f781ee3 6367 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6368
6369 /* Print whether the target is doing the breakpoint's condition
6370 evaluation. If GDB is doing the evaluation, don't print anything. */
6371 if (is_breakpoint (b)
6372 && breakpoint_condition_evaluation_mode ()
6373 == condition_evaluation_target)
6374 {
6a831f06
PA
6375 uiout->message (" (%pF evals)",
6376 string_field ("evaluated-by",
6377 bp_condition_evaluator (b)));
b775012e 6378 }
112e8700 6379 uiout->text ("\n");
0101ce28
JJ
6380 }
6381
0d381245 6382 if (!part_of_multiple && b->thread != -1)
c4093a6a 6383 {
4a64f543 6384 /* FIXME should make an annotation for this. */
112e8700
SM
6385 uiout->text ("\tstop only in thread ");
6386 if (uiout->is_mi_like_p ())
381befee 6387 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6388 else
6389 {
6390 struct thread_info *thr = find_thread_global_id (b->thread);
6391
112e8700 6392 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6393 }
112e8700 6394 uiout->text ("\n");
c4093a6a
JM
6395 }
6396
556ec64d
YQ
6397 if (!part_of_multiple)
6398 {
6399 if (b->hit_count)
31f56a27
YQ
6400 {
6401 /* FIXME should make an annotation for this. */
6402 if (is_catchpoint (b))
112e8700 6403 uiout->text ("\tcatchpoint");
31f56a27 6404 else if (is_tracepoint (b))
112e8700 6405 uiout->text ("\ttracepoint");
31f56a27 6406 else
112e8700
SM
6407 uiout->text ("\tbreakpoint");
6408 uiout->text (" already hit ");
381befee 6409 uiout->field_signed ("times", b->hit_count);
31f56a27 6410 if (b->hit_count == 1)
112e8700 6411 uiout->text (" time\n");
31f56a27 6412 else
112e8700 6413 uiout->text (" times\n");
31f56a27 6414 }
556ec64d
YQ
6415 else
6416 {
31f56a27 6417 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6418 if (uiout->is_mi_like_p ())
381befee 6419 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6420 }
6421 }
8b93c638 6422
0d381245 6423 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6424 {
6425 annotate_field (8);
6a831f06
PA
6426 uiout->message ("\tignore next %pF hits\n",
6427 signed_field ("ignore", b->ignore_count));
c4093a6a 6428 }
059fb39f 6429
816338b5
SS
6430 /* Note that an enable count of 1 corresponds to "enable once"
6431 behavior, which is reported by the combination of enablement and
6432 disposition, so we don't need to mention it here. */
6433 if (!part_of_multiple && b->enable_count > 1)
6434 {
6435 annotate_field (8);
112e8700 6436 uiout->text ("\tdisable after ");
816338b5
SS
6437 /* Tweak the wording to clarify that ignore and enable counts
6438 are distinct, and have additive effect. */
6439 if (b->ignore_count)
112e8700 6440 uiout->text ("additional ");
816338b5 6441 else
112e8700 6442 uiout->text ("next ");
381befee 6443 uiout->field_signed ("enable", b->enable_count);
112e8700 6444 uiout->text (" hits\n");
816338b5
SS
6445 }
6446
f196051f
SS
6447 if (!part_of_multiple && is_tracepoint (b))
6448 {
6449 struct tracepoint *tp = (struct tracepoint *) b;
6450
6451 if (tp->traceframe_usage)
6452 {
112e8700 6453 uiout->text ("\ttrace buffer usage ");
381befee 6454 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6455 uiout->text (" bytes\n");
f196051f
SS
6456 }
6457 }
d3ce09f5 6458
d1b0a7bf 6459 l = b->commands ? b->commands.get () : NULL;
059fb39f 6460 if (!part_of_multiple && l)
c4093a6a
JM
6461 {
6462 annotate_field (9);
9db0d853
SM
6463
6464 bool use_fixed_output =
6465 (uiout->test_flags (fix_breakpoint_script_output)
6466 || fix_breakpoint_script_output_globally);
6467
6468 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6469 gdb::optional<ui_out_emit_list> list_emitter;
6470
6471 if (use_fixed_output)
6472 list_emitter.emplace (uiout, "script");
6473 else
6474 tuple_emitter.emplace (uiout, "script");
6475
8b93c638 6476 print_command_lines (uiout, l, 4);
c4093a6a 6477 }
d24317b4 6478
d9b3f62e 6479 if (is_tracepoint (b))
1042e4c0 6480 {
d9b3f62e
PA
6481 struct tracepoint *t = (struct tracepoint *) b;
6482
6483 if (!part_of_multiple && t->pass_count)
6484 {
6485 annotate_field (10);
112e8700 6486 uiout->text ("\tpass count ");
381befee 6487 uiout->field_signed ("pass", t->pass_count);
112e8700 6488 uiout->text (" \n");
d9b3f62e 6489 }
f2a8bc8a
YQ
6490
6491 /* Don't display it when tracepoint or tracepoint location is
6492 pending. */
6493 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6494 {
6495 annotate_field (11);
6496
112e8700
SM
6497 if (uiout->is_mi_like_p ())
6498 uiout->field_string ("installed",
f2a8bc8a
YQ
6499 loc->inserted ? "y" : "n");
6500 else
6501 {
6502 if (loc->inserted)
112e8700 6503 uiout->text ("\t");
f2a8bc8a 6504 else
112e8700
SM
6505 uiout->text ("\tnot ");
6506 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6507 }
6508 }
1042e4c0
SS
6509 }
6510
112e8700 6511 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6512 {
3a5c3e22
PA
6513 if (is_watchpoint (b))
6514 {
6515 struct watchpoint *w = (struct watchpoint *) b;
6516
a4c50be3 6517 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6518 }
264f9890
PA
6519 else if (b->locspec != nullptr)
6520 {
709438c7 6521 const char *str = b->locspec->to_string ();
264f9890
PA
6522 if (str != nullptr)
6523 uiout->field_string ("original-location", str);
6524 }
d24317b4 6525 }
c01e038b
TT
6526
6527 return result;
c4093a6a 6528}
c5aa993b 6529
13674803
SM
6530/* See breakpoint.h. */
6531
6532bool fix_multi_location_breakpoint_output_globally = false;
6533
0d381245
VP
6534static void
6535print_one_breakpoint (struct breakpoint *b,
4a64f543 6536 struct bp_location **last_loc,
6c95b8df 6537 int allflag)
0d381245 6538{
79a45e25 6539 struct ui_out *uiout = current_uiout;
13674803
SM
6540 bool use_fixed_output
6541 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6542 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6543
b4be1b06 6544 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
c01e038b
TT
6545 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6546 allflag, false);
8d3788bd 6547
b4be1b06
SM
6548 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6549 are outside. */
6550 if (!use_fixed_output)
6551 bkpt_tuple_emitter.reset ();
0d381245
VP
6552
6553 /* If this breakpoint has custom print function,
6554 it's already printed. Otherwise, print individual
6555 locations, if any. */
c01e038b 6556 if (!printed || allflag)
0d381245 6557 {
4a64f543
MS
6558 /* If breakpoint has a single location that is disabled, we
6559 print it as if it had several locations, since otherwise it's
6560 hard to represent "breakpoint enabled, location disabled"
6561 situation.
6562
6563 Note that while hardware watchpoints have several locations
a38118e5
PA
6564 internally, that's not a property exposed to users.
6565
6566 Likewise, while catchpoints may be implemented with
6567 breakpoints (e.g., catch throw), that's not a property
6568 exposed to users. We do however display the internal
6569 breakpoint locations with "maint info breakpoints". */
6570 if (!is_hardware_watchpoint (b)
f06f1252
TT
6571 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6572 || is_ada_exception_catchpoint (b))
a38118e5 6573 && (allflag
b5fa468f
TBA
6574 || (b->loc && (b->loc->next
6575 || !b->loc->enabled
6576 || b->loc->disabled_by_cond))))
0d381245 6577 {
b4be1b06
SM
6578 gdb::optional<ui_out_emit_list> locations_list;
6579
6580 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6581 MI record. For later versions, place breakpoint locations in a
6582 list. */
6583 if (uiout->is_mi_like_p () && use_fixed_output)
6584 locations_list.emplace (uiout, "locations");
8d3788bd 6585
b4be1b06 6586 int n = 1;
40cb8ca5 6587 for (bp_location *loc : b->locations ())
8d3788bd 6588 {
b4be1b06 6589 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6590 print_one_breakpoint_location (b, loc, n, last_loc,
6591 allflag, allflag);
40cb8ca5 6592 n++;
8d3788bd 6593 }
0d381245
VP
6594 }
6595 }
6596}
6597
a6d9a66e
UW
6598static int
6599breakpoint_address_bits (struct breakpoint *b)
6600{
6601 int print_address_bits = 0;
a6d9a66e 6602
40cb8ca5 6603 for (bp_location *loc : b->locations ())
a6d9a66e 6604 {
6e14e441
PA
6605 if (!bl_address_is_meaningful (loc))
6606 continue;
c7437ca6 6607
6e14e441 6608 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6609 if (addr_bit > print_address_bits)
6610 print_address_bits = addr_bit;
6611 }
6612
6613 return print_address_bits;
6614}
0d381245 6615
65630365 6616/* See breakpoint.h. */
c5aa993b 6617
65630365
PA
6618void
6619print_breakpoint (breakpoint *b)
c4093a6a 6620{
a6d9a66e 6621 struct bp_location *dummy_loc = NULL;
65630365 6622 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6623}
c5aa993b 6624
09d682a4
TT
6625/* Return true if this breakpoint was set by the user, false if it is
6626 internal or momentary. */
6627
6628int
6629user_breakpoint_p (struct breakpoint *b)
6630{
46c6471b 6631 return b->number > 0;
09d682a4
TT
6632}
6633
93daf339
TT
6634/* See breakpoint.h. */
6635
6636int
6637pending_breakpoint_p (struct breakpoint *b)
6638{
6639 return b->loc == NULL;
6640}
6641
5c458ae8
SM
6642/* Print information on breakpoints (including watchpoints and tracepoints).
6643
6644 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6645 understood by number_or_range_parser. Only breakpoints included in this
6646 list are then printed.
6647
6648 If SHOW_INTERNAL is true, print internal breakpoints.
6649
6650 If FILTER is non-NULL, call it on each breakpoint and only include the
6651 ones for which it returns true.
6652
6653 Return the total number of breakpoints listed. */
c906108c 6654
d77f58be 6655static int
5c458ae8 6656breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6657 bool (*filter) (const struct breakpoint *))
c4093a6a 6658{
a6d9a66e 6659 struct bp_location *last_loc = NULL;
7f3b0473 6660 int nr_printable_breakpoints;
79a45b7d 6661 struct value_print_options opts;
a6d9a66e 6662 int print_address_bits = 0;
269b11a2 6663 int print_type_col_width = 14;
79a45e25 6664 struct ui_out *uiout = current_uiout;
b5fa468f 6665 bool has_disabled_by_cond_location = false;
269b11a2 6666
79a45b7d
TT
6667 get_user_print_options (&opts);
6668
4a64f543
MS
6669 /* Compute the number of rows in the table, as well as the size
6670 required for address fields. */
7f3b0473 6671 nr_printable_breakpoints = 0;
43892fdf 6672 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6673 {
6674 /* If we have a filter, only list the breakpoints it accepts. */
6675 if (filter && !filter (b))
6676 continue;
6677
5c458ae8 6678 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6679 accept. Skip the others. */
5c458ae8 6680 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6681 {
5c458ae8 6682 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6683 continue;
5c458ae8 6684 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6685 continue;
6686 }
269b11a2 6687
5c458ae8 6688 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6689 {
6690 int addr_bit, type_len;
a6d9a66e 6691
e5a67952
MS
6692 addr_bit = breakpoint_address_bits (b);
6693 if (addr_bit > print_address_bits)
6694 print_address_bits = addr_bit;
269b11a2 6695
e5a67952
MS
6696 type_len = strlen (bptype_string (b->type));
6697 if (type_len > print_type_col_width)
6698 print_type_col_width = type_len;
6699
6700 nr_printable_breakpoints++;
6701 }
6702 }
7f3b0473 6703
4a2b031d
TT
6704 {
6705 ui_out_emit_table table_emitter (uiout,
6706 opts.addressprint ? 6 : 5,
6707 nr_printable_breakpoints,
6708 "BreakpointTable");
6709
6710 if (nr_printable_breakpoints > 0)
6711 annotate_breakpoints_headers ();
6712 if (nr_printable_breakpoints > 0)
6713 annotate_field (0);
6714 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6715 if (nr_printable_breakpoints > 0)
6716 annotate_field (1);
6717 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6718 if (nr_printable_breakpoints > 0)
6719 annotate_field (2);
6720 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6721 if (nr_printable_breakpoints > 0)
6722 annotate_field (3);
6723 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6724 if (opts.addressprint)
6725 {
6726 if (nr_printable_breakpoints > 0)
6727 annotate_field (4);
6728 if (print_address_bits <= 32)
6729 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6730 else
6731 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6732 }
6733 if (nr_printable_breakpoints > 0)
6734 annotate_field (5);
6735 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6736 uiout->table_body ();
6737 if (nr_printable_breakpoints > 0)
6738 annotate_breakpoints_table ();
6739
43892fdf 6740 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6741 {
6742 QUIT;
6743 /* If we have a filter, only list the breakpoints it accepts. */
6744 if (filter && !filter (b))
6745 continue;
e5a67952 6746
5c458ae8 6747 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6748 accept. Skip the others. */
e5a67952 6749
5c458ae8 6750 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6751 {
5c458ae8 6752 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6753 {
5c458ae8 6754 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6755 continue;
6756 }
6757 else /* all others */
6758 {
5c458ae8 6759 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6760 continue;
6761 }
6762 }
6763 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6764 show_internal is set. */
6765 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6766 {
6767 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6768 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6769 if (loc->disabled_by_cond)
6770 has_disabled_by_cond_location = true;
6771 }
4a2b031d
TT
6772 }
6773 }
698384cd 6774
7f3b0473 6775 if (nr_printable_breakpoints == 0)
c906108c 6776 {
4a64f543
MS
6777 /* If there's a filter, let the caller decide how to report
6778 empty list. */
d77f58be
SS
6779 if (!filter)
6780 {
5c458ae8 6781 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6782 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6783 else
112e8700 6784 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6785 bp_num_list);
d77f58be 6786 }
c906108c
SS
6787 }
6788 else
c4093a6a 6789 {
a6d9a66e
UW
6790 if (last_loc && !server_command)
6791 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6792
85c88e2a 6793 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6794 uiout->message (_("(*): Breakpoint condition is invalid at this "
6795 "location.\n"));
c4093a6a 6796 }
c906108c 6797
4a64f543 6798 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6799 there have been breakpoints? */
c906108c 6800 annotate_breakpoints_table_end ();
d77f58be
SS
6801
6802 return nr_printable_breakpoints;
c906108c
SS
6803}
6804
ad443146
SS
6805/* Display the value of default-collect in a way that is generally
6806 compatible with the breakpoint list. */
6807
6808static void
6809default_collect_info (void)
6810{
79a45e25
PA
6811 struct ui_out *uiout = current_uiout;
6812
ad443146
SS
6813 /* If it has no value (which is frequently the case), say nothing; a
6814 message like "No default-collect." gets in user's face when it's
6815 not wanted. */
e0700ba4 6816 if (default_collect.empty ())
ad443146
SS
6817 return;
6818
6819 /* The following phrase lines up nicely with per-tracepoint collect
6820 actions. */
112e8700
SM
6821 uiout->text ("default collect ");
6822 uiout->field_string ("default-collect", default_collect);
6823 uiout->text (" \n");
ad443146
SS
6824}
6825
c906108c 6826static void
0b39b52e 6827info_breakpoints_command (const char *args, int from_tty)
c906108c 6828{
5c458ae8 6829 breakpoint_1 (args, false, NULL);
ad443146
SS
6830
6831 default_collect_info ();
d77f58be
SS
6832}
6833
6834static void
1d12d88f 6835info_watchpoints_command (const char *args, int from_tty)
d77f58be 6836{
5c458ae8 6837 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6838 struct ui_out *uiout = current_uiout;
d77f58be
SS
6839
6840 if (num_printed == 0)
6841 {
e5a67952 6842 if (args == NULL || *args == '\0')
112e8700 6843 uiout->message ("No watchpoints.\n");
d77f58be 6844 else
112e8700 6845 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6846 }
c906108c
SS
6847}
6848
7a292a7a 6849static void
4495129a 6850maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6851{
5c458ae8 6852 breakpoint_1 (args, true, NULL);
ad443146
SS
6853
6854 default_collect_info ();
c906108c
SS
6855}
6856
0d381245 6857static int
714835d5 6858breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6859 struct program_space *pspace,
714835d5 6860 CORE_ADDR pc, struct obj_section *section)
0d381245 6861{
40cb8ca5 6862 for (bp_location *bl : b->locations ())
0d381245 6863 {
6c95b8df
PA
6864 if (bl->pspace == pspace
6865 && bl->address == pc
0d381245
VP
6866 && (!overlay_debugging || bl->section == section))
6867 return 1;
6868 }
6869 return 0;
6870}
6871
bd21b6c9 6872/* See breakpoint.h. */
c906108c 6873
bd21b6c9 6874void
6c95b8df
PA
6875describe_other_breakpoints (struct gdbarch *gdbarch,
6876 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6877 struct obj_section *section, int thread)
c906108c 6878{
52f0bd74 6879 int others = 0;
c906108c 6880
43892fdf 6881 for (breakpoint *b : all_breakpoints ())
672f9b60 6882 others += (user_breakpoint_p (b)
dda83cd7 6883 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 6884
c906108c
SS
6885 if (others > 0)
6886 {
a3f17187 6887 if (others == 1)
6cb06a8c 6888 gdb_printf (_("Note: breakpoint "));
a3f17187 6889 else /* if (others == ???) */
6cb06a8c 6890 gdb_printf (_("Note: breakpoints "));
43892fdf 6891 for (breakpoint *b : all_breakpoints ())
672f9b60 6892 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6893 {
6894 others--;
6cb06a8c 6895 gdb_printf ("%d", b->number);
0d381245 6896 if (b->thread == -1 && thread != -1)
6cb06a8c 6897 gdb_printf (" (all threads)");
0d381245 6898 else if (b->thread != -1)
6cb06a8c
TT
6899 gdb_printf (" (thread %d)", b->thread);
6900 gdb_printf ("%s%s ",
6901 ((b->enable_state == bp_disabled
6902 || b->enable_state == bp_call_disabled)
6903 ? " (disabled)"
6904 : ""),
6905 (others > 1) ? ","
6906 : ((others == 1) ? " and" : ""));
0d381245 6907 }
6a831f06
PA
6908 current_uiout->message (_("also set at pc %ps.\n"),
6909 styled_string (address_style.style (),
6910 paddress (gdbarch, pc)));
c906108c
SS
6911 }
6912}
6913\f
c906108c 6914
cb1e4e32
PA
6915/* Return true iff it is meaningful to use the address member of LOC.
6916 For some breakpoint types, the locations' address members are
6917 irrelevant and it makes no sense to attempt to compare them to
6918 other addresses (or use them for any other purpose either).
2d134ed3 6919
cb1e4e32
PA
6920 More specifically, software watchpoints and catchpoints that are
6921 not backed by breakpoints always have a zero valued location
6922 address and we don't want to mark breakpoints of any of these types
6923 to be a duplicate of an actual breakpoint location at address
6924 zero. */
e4f237da 6925
cb1e4e32
PA
6926static bool
6927bl_address_is_meaningful (bp_location *loc)
e4f237da 6928{
cb1e4e32 6929 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6930}
6931
6932/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6933 true if LOC1 and LOC2 represent the same watchpoint location. */
6934
6935static int
4a64f543
MS
6936watchpoint_locations_match (struct bp_location *loc1,
6937 struct bp_location *loc2)
2d134ed3 6938{
3a5c3e22
PA
6939 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6940 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6941
6942 /* Both of them must exist. */
6943 gdb_assert (w1 != NULL);
6944 gdb_assert (w2 != NULL);
2bdf28a0 6945
4a64f543
MS
6946 /* If the target can evaluate the condition expression in hardware,
6947 then we we need to insert both watchpoints even if they are at
6948 the same place. Otherwise the watchpoint will only trigger when
6949 the condition of whichever watchpoint was inserted evaluates to
6950 true, not giving a chance for GDB to check the condition of the
6951 other watchpoint. */
3a5c3e22 6952 if ((w1->cond_exp
4a64f543
MS
6953 && target_can_accel_watchpoint_condition (loc1->address,
6954 loc1->length,
0cf6dd15 6955 loc1->watchpoint_type,
4d01a485 6956 w1->cond_exp.get ()))
3a5c3e22 6957 || (w2->cond_exp
4a64f543
MS
6958 && target_can_accel_watchpoint_condition (loc2->address,
6959 loc2->length,
0cf6dd15 6960 loc2->watchpoint_type,
4d01a485 6961 w2->cond_exp.get ())))
0cf6dd15
TJB
6962 return 0;
6963
85d721b8
PA
6964 /* Note that this checks the owner's type, not the location's. In
6965 case the target does not support read watchpoints, but does
6966 support access watchpoints, we'll have bp_read_watchpoint
6967 watchpoints with hw_access locations. Those should be considered
6968 duplicates of hw_read locations. The hw_read locations will
6969 become hw_access locations later. */
2d134ed3
PA
6970 return (loc1->owner->type == loc2->owner->type
6971 && loc1->pspace->aspace == loc2->pspace->aspace
6972 && loc1->address == loc2->address
6973 && loc1->length == loc2->length);
e4f237da
KB
6974}
6975
31e77af2 6976/* See breakpoint.h. */
6c95b8df 6977
31e77af2 6978int
accd0bcd
YQ
6979breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6980 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6981{
f5656ead 6982 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6983 || aspace1 == aspace2)
6984 && addr1 == addr2);
6985}
6986
f1310107
TJB
6987/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6988 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6989 matches ASPACE2. On targets that have global breakpoints, the address
6990 space doesn't really matter. */
6991
6992static int
accd0bcd
YQ
6993breakpoint_address_match_range (const address_space *aspace1,
6994 CORE_ADDR addr1,
6995 int len1, const address_space *aspace2,
f1310107
TJB
6996 CORE_ADDR addr2)
6997{
f5656ead 6998 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6999 || aspace1 == aspace2)
7000 && addr2 >= addr1 && addr2 < addr1 + len1);
7001}
7002
7003/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7004 a ranged breakpoint. In most targets, a match happens only if ASPACE
7005 matches the breakpoint's address space. On targets that have global
7006 breakpoints, the address space doesn't really matter. */
7007
7008static int
7009breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 7010 const address_space *aspace,
f1310107
TJB
7011 CORE_ADDR addr)
7012{
7013 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7014 aspace, addr)
7015 || (bl->length
7016 && breakpoint_address_match_range (bl->pspace->aspace,
7017 bl->address, bl->length,
7018 aspace, addr)));
7019}
7020
d35ae833
PA
7021/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7022 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7023 match happens only if ASPACE matches the breakpoint's address
7024 space. On targets that have global breakpoints, the address space
7025 doesn't really matter. */
7026
7027static int
7028breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 7029 const address_space *aspace,
d35ae833
PA
7030 CORE_ADDR addr, int len)
7031{
7032 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7033 || bl->pspace->aspace == aspace)
7034 {
7035 int bl_len = bl->length != 0 ? bl->length : 1;
7036
7037 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7038 return 1;
7039 }
7040 return 0;
7041}
7042
1e4d1764
YQ
7043/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7044 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7045 true, otherwise returns false. */
7046
7047static int
7048tracepoint_locations_match (struct bp_location *loc1,
7049 struct bp_location *loc2)
7050{
7051 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7052 /* Since tracepoint locations are never duplicated with others', tracepoint
7053 locations at the same address of different tracepoints are regarded as
7054 different locations. */
7055 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7056 else
7057 return 0;
7058}
7059
2d134ed3 7060/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7061 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7062 the same location. If SW_HW_BPS_MATCH is true, then software
7063 breakpoint locations and hardware breakpoint locations match,
7064 otherwise they don't. */
2d134ed3
PA
7065
7066static int
7f32a4d5
PA
7067breakpoint_locations_match (struct bp_location *loc1,
7068 struct bp_location *loc2,
7069 bool sw_hw_bps_match)
2d134ed3 7070{
2bdf28a0
JK
7071 int hw_point1, hw_point2;
7072
7073 /* Both of them must not be in moribund_locations. */
7074 gdb_assert (loc1->owner != NULL);
7075 gdb_assert (loc2->owner != NULL);
7076
7077 hw_point1 = is_hardware_watchpoint (loc1->owner);
7078 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7079
7080 if (hw_point1 != hw_point2)
7081 return 0;
7082 else if (hw_point1)
7083 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7084 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7085 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7086 else
7f32a4d5
PA
7087 /* We compare bp_location.length in order to cover ranged
7088 breakpoints. Keep this in sync with
7089 bp_location_is_less_than. */
f1310107
TJB
7090 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7091 loc2->pspace->aspace, loc2->address)
7f32a4d5 7092 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7093 && loc1->length == loc2->length);
2d134ed3
PA
7094}
7095
76897487
KB
7096static void
7097breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 7098 int bnum, int have_bnum)
76897487 7099{
f63fbe86
MS
7100 /* The longest string possibly returned by hex_string_custom
7101 is 50 chars. These must be at least that big for safety. */
7102 char astr1[64];
7103 char astr2[64];
76897487 7104
bb599908
PH
7105 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7106 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7107 if (have_bnum)
8a3fe4f8 7108 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7109 bnum, astr1, astr2);
76897487 7110 else
8a3fe4f8 7111 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7112}
7113
4a64f543
MS
7114/* Adjust a breakpoint's address to account for architectural
7115 constraints on breakpoint placement. Return the adjusted address.
7116 Note: Very few targets require this kind of adjustment. For most
7117 targets, this function is simply the identity function. */
76897487
KB
7118
7119static CORE_ADDR
a6d9a66e 7120adjust_breakpoint_address (struct gdbarch *gdbarch,
60270718
AB
7121 CORE_ADDR bpaddr, enum bptype bptype,
7122 struct program_space *pspace)
76897487 7123{
60270718
AB
7124 gdb_assert (pspace != nullptr);
7125
a0de8c21
YQ
7126 if (bptype == bp_watchpoint
7127 || bptype == bp_hardware_watchpoint
7128 || bptype == bp_read_watchpoint
7129 || bptype == bp_access_watchpoint
7130 || bptype == bp_catchpoint)
88f7da05
KB
7131 {
7132 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7133 have their addresses modified. */
88f7da05
KB
7134 return bpaddr;
7135 }
7c16b83e
PA
7136 else if (bptype == bp_single_step)
7137 {
7138 /* Single-step breakpoints should not have their addresses
7139 modified. If there's any architectural constrain that
7140 applies to this address, then it should have already been
7141 taken into account when the breakpoint was created in the
7142 first place. If we didn't do this, stepping through e.g.,
7143 Thumb-2 IT blocks would break. */
7144 return bpaddr;
7145 }
76897487
KB
7146 else
7147 {
a0de8c21
YQ
7148 CORE_ADDR adjusted_bpaddr = bpaddr;
7149
60270718
AB
7150 /* Some targets have architectural constraints on the placement
7151 of breakpoint instructions. Obtain the adjusted address. */
a0de8c21
YQ
7152 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7153 {
60270718
AB
7154 /* Targets that implement this adjustment function will likely
7155 inspect either the symbol table, target memory at BPADDR, or
7156 even state registers, so ensure a suitable thread (and its
7157 associated program space) are currently selected. */
7158 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7159 switch_to_program_space_and_thread (pspace);
7160 adjusted_bpaddr
7161 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
a0de8c21 7162 }
76897487 7163
a0de8c21 7164 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7165
7166 /* An adjusted breakpoint address can significantly alter
dda83cd7 7167 a user's expectations. Print a warning if an adjustment
76897487
KB
7168 is required. */
7169 if (adjusted_bpaddr != bpaddr)
7170 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7171
7172 return adjusted_bpaddr;
7173 }
7174}
7175
cb1e4e32
PA
7176static bp_loc_type
7177bp_location_from_bp_type (bptype type)
7cc221ef 7178{
cb1e4e32 7179 switch (type)
e049a4b5
DJ
7180 {
7181 case bp_breakpoint:
7c16b83e 7182 case bp_single_step:
e049a4b5
DJ
7183 case bp_until:
7184 case bp_finish:
7185 case bp_longjmp:
7186 case bp_longjmp_resume:
e2e4d78b 7187 case bp_longjmp_call_dummy:
186c406b
TT
7188 case bp_exception:
7189 case bp_exception_resume:
e049a4b5 7190 case bp_step_resume:
2c03e5be 7191 case bp_hp_step_resume:
e049a4b5
DJ
7192 case bp_watchpoint_scope:
7193 case bp_call_dummy:
aa7d318d 7194 case bp_std_terminate:
e049a4b5
DJ
7195 case bp_shlib_event:
7196 case bp_thread_event:
7197 case bp_overlay_event:
4efc6507 7198 case bp_jit_event:
0fd8e87f 7199 case bp_longjmp_master:
aa7d318d 7200 case bp_std_terminate_master:
186c406b 7201 case bp_exception_master:
0e30163f
JK
7202 case bp_gnu_ifunc_resolver:
7203 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7204 case bp_dprintf:
cb1e4e32 7205 return bp_loc_software_breakpoint;
e049a4b5 7206 case bp_hardware_breakpoint:
cb1e4e32 7207 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7208 case bp_hardware_watchpoint:
7209 case bp_read_watchpoint:
7210 case bp_access_watchpoint:
cb1e4e32 7211 return bp_loc_hardware_watchpoint;
e049a4b5 7212 case bp_watchpoint:
6e14e441 7213 return bp_loc_software_watchpoint;
ce78b96d 7214 case bp_catchpoint:
15c3d785
PA
7215 case bp_tracepoint:
7216 case bp_fast_tracepoint:
0fb4aa4b 7217 case bp_static_tracepoint:
7b572efb 7218 case bp_static_marker_tracepoint:
cb1e4e32 7219 return bp_loc_other;
e049a4b5 7220 default:
e2e0b3e5 7221 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7222 }
cb1e4e32
PA
7223}
7224
7225bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7226{
7227 this->owner = owner;
7228 this->cond_bytecode = NULL;
7229 this->shlib_disabled = 0;
7230 this->enabled = 1;
b5fa468f 7231 this->disabled_by_cond = false;
cb1e4e32
PA
7232
7233 this->loc_type = type;
e049a4b5 7234
cb1e4e32
PA
7235 if (this->loc_type == bp_loc_software_breakpoint
7236 || this->loc_type == bp_loc_hardware_breakpoint)
7237 mark_breakpoint_location_modified (this);
7238
b6433ede 7239 incref ();
cb1e4e32
PA
7240}
7241
7242bp_location::bp_location (breakpoint *owner)
7243 : bp_location::bp_location (owner,
7244 bp_location_from_bp_type (owner->type))
7245{
28010a5d
PA
7246}
7247
f431efe5
PA
7248/* Decrement reference count. If the reference count reaches 0,
7249 destroy the bp_location. Sets *BLP to NULL. */
7250
7251static void
7252decref_bp_location (struct bp_location **blp)
7253{
b6433ede 7254 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7255 *blp = NULL;
7256}
7257
346774a9 7258/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7259
b270e6f9
TT
7260static breakpoint *
7261add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7262{
346774a9 7263 struct breakpoint *b1;
b270e6f9 7264 struct breakpoint *result = b.get ();
c906108c 7265
346774a9
PA
7266 /* Add this breakpoint to the end of the chain so that a list of
7267 breakpoints will come out in order of increasing numbers. */
7268
7269 b1 = breakpoint_chain;
7270 if (b1 == 0)
b270e6f9 7271 breakpoint_chain = b.release ();
346774a9
PA
7272 else
7273 {
7274 while (b1->next)
7275 b1 = b1->next;
b270e6f9 7276 b1->next = b.release ();
346774a9 7277 }
b270e6f9
TT
7278
7279 return result;
346774a9
PA
7280}
7281
0ba852ab 7282/* Initialize loc->function_name. */
0e30163f 7283
0d381245 7284static void
0ba852ab 7285set_breakpoint_location_function (struct bp_location *loc)
0d381245 7286{
2bdf28a0
JK
7287 gdb_assert (loc->owner != NULL);
7288
0d381245 7289 if (loc->owner->type == bp_breakpoint
1042e4c0 7290 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7291 || is_tracepoint (loc->owner))
0d381245 7292 {
2c02bd72 7293 const char *function_name;
0e30163f 7294
3467ec66 7295 if (loc->msymbol != NULL
60f62e2b
SM
7296 && (loc->msymbol->type () == mst_text_gnu_ifunc
7297 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7298 {
7299 struct breakpoint *b = loc->owner;
7300
c9d95fa3 7301 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7302
7303 if (b->type == bp_breakpoint && b->loc == loc
7304 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7305 {
7306 /* Create only the whole new breakpoint of this type but do not
7307 mess more complicated breakpoints with multiple locations. */
7308 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7309 /* Remember the resolver's address for use by the return
dda83cd7 7310 breakpoint. */
3467ec66 7311 loc->related_address = loc->address;
0e30163f
JK
7312 }
7313 }
3467ec66
PA
7314 else
7315 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7316
2c02bd72 7317 if (function_name)
23d6ee64 7318 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7319 }
7320}
7321
a6d9a66e 7322/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7323struct gdbarch *
a6d9a66e
UW
7324get_sal_arch (struct symtab_and_line sal)
7325{
7326 if (sal.section)
08feed99 7327 return sal.section->objfile->arch ();
a6d9a66e 7328 if (sal.symtab)
3c86fae3 7329 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7330
7331 return NULL;
7332}
7333
53a5351d 7334/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7335 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7336 initiated the operation. */
c906108c
SS
7337
7338void
186c406b 7339set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7340{
5d5658a1 7341 int thread = tp->global_num;
0fd8e87f
UW
7342
7343 /* To avoid having to rescan all objfile symbols at every step,
7344 we maintain a list of continually-inserted but always disabled
7345 longjmp "master" breakpoints. Here, we simply create momentary
7346 clones of those and enable them for the requested thread. */
1428b37a 7347 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7348 if (b->pspace == current_program_space
186c406b
TT
7349 && (b->type == bp_longjmp_master
7350 || b->type == bp_exception_master))
0fd8e87f 7351 {
06edf0c0 7352 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
e2e4d78b
JK
7353 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7354 after their removal. */
7ab97995 7355 momentary_breakpoint_from_master (b, type, 1, thread);
0fd8e87f 7356 }
186c406b
TT
7357
7358 tp->initiating_frame = frame;
c906108c
SS
7359}
7360
611c83ae 7361/* Delete all longjmp breakpoints from THREAD. */
c906108c 7362void
611c83ae 7363delete_longjmp_breakpoint (int thread)
c906108c 7364{
1428b37a 7365 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7366 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7367 {
7368 if (b->thread == thread)
7369 delete_breakpoint (b);
7370 }
c906108c
SS
7371}
7372
f59f708a
PA
7373void
7374delete_longjmp_breakpoint_at_next_stop (int thread)
7375{
1428b37a 7376 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7377 if (b->type == bp_longjmp || b->type == bp_exception)
7378 {
7379 if (b->thread == thread)
7380 b->disposition = disp_del_at_next_stop;
7381 }
7382}
7383
e2e4d78b
JK
7384/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7385 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7386 pointer to any of them. Return NULL if this system cannot place longjmp
7387 breakpoints. */
7388
7389struct breakpoint *
7390set_longjmp_breakpoint_for_call_dummy (void)
7391{
43892fdf 7392 breakpoint *retval = nullptr;
e2e4d78b 7393
43892fdf 7394 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7395 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7396 {
7ab97995
PA
7397 int thread = inferior_thread ()->global_num;
7398 breakpoint *new_b
7399 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7400 1, thread);
e2e4d78b
JK
7401
7402 /* Link NEW_B into the chain of RETVAL breakpoints. */
7403
7404 gdb_assert (new_b->related_breakpoint == new_b);
7405 if (retval == NULL)
7406 retval = new_b;
7407 new_b->related_breakpoint = retval;
7408 while (retval->related_breakpoint != new_b->related_breakpoint)
7409 retval = retval->related_breakpoint;
7410 retval->related_breakpoint = new_b;
7411 }
7412
7413 return retval;
7414}
7415
7416/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7417 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7418 stack.
7419
b4b3e2de
AB
7420 If the unwind fails then there is not sufficient information to discard
7421 dummy frames. In this case, elide the clean up and the dummy frames will
7422 be cleaned up next time this function is called from a location where
7423 unwinding is possible. */
e2e4d78b
JK
7424
7425void
b67a2c6f 7426check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7427{
7428 struct breakpoint *b, *b_tmp;
7429
7430 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7431 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7432 {
7433 struct breakpoint *dummy_b = b->related_breakpoint;
7434
b4b3e2de
AB
7435 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7436 chained off b->related_breakpoint. */
e2e4d78b
JK
7437 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7438 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7439
7440 /* If there was no bp_call_dummy breakpoint then there's nothing
7441 more to do. Or, if the dummy frame associated with the
7442 bp_call_dummy is still on the stack then we need to leave this
7443 bp_call_dummy in place. */
e2e4d78b
JK
7444 if (dummy_b->type != bp_call_dummy
7445 || frame_find_by_id (dummy_b->frame_id) != NULL)
7446 continue;
b4b3e2de
AB
7447
7448 /* We didn't find the dummy frame on the stack, this could be
7449 because we have longjmp'd to a stack frame that is previous to
7450 the dummy frame, or it could be because the stack unwind is
7451 broken at some point between the longjmp frame and the dummy
7452 frame.
7453
7454 Next we figure out why the stack unwind stopped. If it looks
7455 like the unwind is complete then we assume the dummy frame has
7456 been jumped over, however, if the unwind stopped for an
7457 unexpected reason then we assume the stack unwind is currently
7458 broken, and that we will (eventually) return to the dummy
7459 frame.
7460
7461 It might be tempting to consider using frame_id_inner here, but
7462 that is not safe. There is no guarantee that the stack frames
7463 we are looking at here are even on the same stack as the
7464 original dummy frame, hence frame_id_inner can't be used. See
7465 the comments on frame_id_inner for more details. */
7466 bool unwind_finished_unexpectedly = false;
7467 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7468 {
7469 struct frame_info *prev = get_prev_frame (fi);
7470 if (prev == nullptr)
7471 {
7472 /* FI is the last stack frame. Why did this frame not
7473 unwind further? */
7474 auto stop_reason = get_frame_unwind_stop_reason (fi);
7475 if (stop_reason != UNWIND_NO_REASON
7476 && stop_reason != UNWIND_OUTERMOST)
7477 unwind_finished_unexpectedly = true;
7478 }
7479 fi = prev;
7480 }
7481 if (unwind_finished_unexpectedly)
7482 continue;
7483
00431a78 7484 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7485
7486 while (b->related_breakpoint != b)
7487 {
7488 if (b_tmp == b->related_breakpoint)
7489 b_tmp = b->related_breakpoint->next;
7490 delete_breakpoint (b->related_breakpoint);
7491 }
7492 delete_breakpoint (b);
7493 }
7494}
7495
1900040c
MS
7496void
7497enable_overlay_breakpoints (void)
7498{
43892fdf 7499 for (breakpoint *b : all_breakpoints ())
1900040c 7500 if (b->type == bp_overlay_event)
01add95b
SM
7501 {
7502 b->enable_state = bp_enabled;
7503 update_global_location_list (UGLL_MAY_INSERT);
7504 overlay_events_enabled = 1;
7505 }
1900040c
MS
7506}
7507
7508void
7509disable_overlay_breakpoints (void)
7510{
43892fdf 7511 for (breakpoint *b : all_breakpoints ())
1900040c 7512 if (b->type == bp_overlay_event)
01add95b
SM
7513 {
7514 b->enable_state = bp_disabled;
7515 update_global_location_list (UGLL_DONT_INSERT);
7516 overlay_events_enabled = 0;
7517 }
1900040c
MS
7518}
7519
aa7d318d
TT
7520/* Set an active std::terminate breakpoint for each std::terminate
7521 master breakpoint. */
7522void
7523set_std_terminate_breakpoint (void)
7524{
1428b37a 7525 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7526 if (b->pspace == current_program_space
7527 && b->type == bp_std_terminate_master)
7528 {
7ab97995
PA
7529 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7530 inferior_thread ()->global_num);
aa7d318d
TT
7531 }
7532}
7533
7534/* Delete all the std::terminate breakpoints. */
7535void
7536delete_std_terminate_breakpoint (void)
7537{
1428b37a 7538 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7539 if (b->type == bp_std_terminate)
7540 delete_breakpoint (b);
7541}
7542
c4093a6a 7543struct breakpoint *
a6d9a66e 7544create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7545{
7546 struct breakpoint *b;
c4093a6a 7547
9efa3c7f 7548 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 7549
b5de0fa7 7550 b->enable_state = bp_enabled;
264f9890
PA
7551 /* locspec has to be used or breakpoint_re_set will delete me. */
7552 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
c4093a6a 7553
44702360 7554 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7555
c4093a6a
JM
7556 return b;
7557}
7558
0101ce28
JJ
7559struct lang_and_radix
7560 {
7561 enum language lang;
7562 int radix;
7563 };
7564
4efc6507
DE
7565/* Create a breakpoint for JIT code registration and unregistration. */
7566
7567struct breakpoint *
7568create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7569{
9efa3c7f 7570 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 7571}
0101ce28 7572
03673fc7
PP
7573/* Remove JIT code registration and unregistration breakpoint(s). */
7574
7575void
7576remove_jit_event_breakpoints (void)
7577{
1428b37a 7578 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7579 if (b->type == bp_jit_event
7580 && b->loc->pspace == current_program_space)
7581 delete_breakpoint (b);
7582}
7583
cae688ec
JJ
7584void
7585remove_solib_event_breakpoints (void)
7586{
1428b37a 7587 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7588 if (b->type == bp_shlib_event
7589 && b->loc->pspace == current_program_space)
cae688ec
JJ
7590 delete_breakpoint (b);
7591}
7592
f37f681c
PA
7593/* See breakpoint.h. */
7594
7595void
7596remove_solib_event_breakpoints_at_next_stop (void)
7597{
1428b37a 7598 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7599 if (b->type == bp_shlib_event
7600 && b->loc->pspace == current_program_space)
7601 b->disposition = disp_del_at_next_stop;
7602}
7603
04086b45
PA
7604/* Helper for create_solib_event_breakpoint /
7605 create_and_insert_solib_event_breakpoint. Allows specifying which
7606 INSERT_MODE to pass through to update_global_location_list. */
7607
7608static struct breakpoint *
7609create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7610 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7611{
7612 struct breakpoint *b;
7613
9efa3c7f 7614 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 7615 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7616 return b;
7617}
7618
04086b45
PA
7619struct breakpoint *
7620create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7621{
7622 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7623}
7624
f37f681c
PA
7625/* See breakpoint.h. */
7626
7627struct breakpoint *
7628create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7629{
7630 struct breakpoint *b;
7631
04086b45
PA
7632 /* Explicitly tell update_global_location_list to insert
7633 locations. */
7634 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7635 if (!b->loc->inserted)
7636 {
7637 delete_breakpoint (b);
7638 return NULL;
7639 }
7640 return b;
7641}
7642
cae688ec
JJ
7643/* Disable any breakpoints that are on code in shared libraries. Only
7644 apply to enabled breakpoints, disabled ones can just stay disabled. */
7645
7646void
cb851954 7647disable_breakpoints_in_shlibs (void)
cae688ec 7648{
48d7020b 7649 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7650 {
7651 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7652 struct breakpoint *b = loc->owner;
7653
7654 /* We apply the check to all breakpoints, including disabled for
7655 those with loc->duplicate set. This is so that when breakpoint
7656 becomes enabled, or the duplicate is removed, gdb will try to
7657 insert all breakpoints. If we don't set shlib_disabled here,
7658 we'll try to insert those breakpoints and fail. */
7659 if (((b->type == bp_breakpoint)
7660 || (b->type == bp_jit_event)
7661 || (b->type == bp_hardware_breakpoint)
7662 || (is_tracepoint (b)))
7663 && loc->pspace == current_program_space
7664 && !loc->shlib_disabled
7665 && solib_name_from_address (loc->pspace, loc->address)
7666 )
7667 {
7668 loc->shlib_disabled = 1;
7669 }
7670 }
cae688ec
JJ
7671}
7672
63644780
NB
7673/* Disable any breakpoints and tracepoints that are in SOLIB upon
7674 notification of unloaded_shlib. Only apply to enabled breakpoints,
7675 disabled ones can just stay disabled. */
84acb35a 7676
75149521 7677static void
84acb35a
JJ
7678disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7679{
84acb35a
JJ
7680 int disabled_shlib_breaks = 0;
7681
48d7020b 7682 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7683 {
7684 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7685 struct breakpoint *b = loc->owner;
7686
7687 if (solib->pspace == loc->pspace
7688 && !loc->shlib_disabled
7689 && (((b->type == bp_breakpoint
7690 || b->type == bp_jit_event
7691 || b->type == bp_hardware_breakpoint)
7692 && (loc->loc_type == bp_loc_hardware_breakpoint
7693 || loc->loc_type == bp_loc_software_breakpoint))
7694 || is_tracepoint (b))
7695 && solib_contains_address_p (solib, loc->address))
7696 {
7697 loc->shlib_disabled = 1;
7698 /* At this point, we cannot rely on remove_breakpoint
7699 succeeding so we must mark the breakpoint as not inserted
7700 to prevent future errors occurring in remove_breakpoints. */
7701 loc->inserted = 0;
7702
7703 /* This may cause duplicate notifications for the same breakpoint. */
7704 gdb::observers::breakpoint_modified.notify (b);
7705
7706 if (!disabled_shlib_breaks)
7707 {
7708 target_terminal::ours_for_output ();
7709 warning (_("Temporarily disabling breakpoints "
7710 "for unloaded shared library \"%s\""),
7711 solib->so_name);
7712 }
7713 disabled_shlib_breaks = 1;
7714 }
7715 }
84acb35a
JJ
7716}
7717
63644780
NB
7718/* Disable any breakpoints and tracepoints in OBJFILE upon
7719 notification of free_objfile. Only apply to enabled breakpoints,
7720 disabled ones can just stay disabled. */
7721
7722static void
7723disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7724{
63644780
NB
7725 if (objfile == NULL)
7726 return;
7727
d03de421
PA
7728 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7729 managed by the user with add-symbol-file/remove-symbol-file.
7730 Similarly to how breakpoints in shared libraries are handled in
7731 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7732 shlib_disabled so they end up uninserted on the next global
7733 location list update. Shared libraries not loaded by the user
7734 aren't handled here -- they're already handled in
7735 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7736 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7737 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7738 main objfile). */
7739 if ((objfile->flags & OBJF_SHARED) == 0
7740 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7741 return;
7742
43892fdf 7743 for (breakpoint *b : all_breakpoints ())
63644780 7744 {
63644780
NB
7745 int bp_modified = 0;
7746
7747 if (!is_breakpoint (b) && !is_tracepoint (b))
7748 continue;
7749
40cb8ca5 7750 for (bp_location *loc : b->locations ())
63644780
NB
7751 {
7752 CORE_ADDR loc_addr = loc->address;
7753
7754 if (loc->loc_type != bp_loc_hardware_breakpoint
7755 && loc->loc_type != bp_loc_software_breakpoint)
7756 continue;
7757
7758 if (loc->shlib_disabled != 0)
7759 continue;
7760
7761 if (objfile->pspace != loc->pspace)
7762 continue;
7763
7764 if (loc->loc_type != bp_loc_hardware_breakpoint
7765 && loc->loc_type != bp_loc_software_breakpoint)
7766 continue;
7767
7768 if (is_addr_in_objfile (loc_addr, objfile))
7769 {
7770 loc->shlib_disabled = 1;
08351840
PA
7771 /* At this point, we don't know whether the object was
7772 unmapped from the inferior or not, so leave the
7773 inserted flag alone. We'll handle failure to
7774 uninsert quietly, in case the object was indeed
7775 unmapped. */
63644780
NB
7776
7777 mark_breakpoint_location_modified (loc);
7778
7779 bp_modified = 1;
7780 }
7781 }
7782
7783 if (bp_modified)
76727919 7784 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7785 }
7786}
7787
b650a282 7788/* See breakpoint.h. */
ce78b96d 7789
92bb0228
PA
7790breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7791 bool temp, const char *cond_string_)
7792 : type (bptype),
7793 disposition (temp ? disp_del : disp_donttouch),
7794 gdbarch (gdbarch_),
7795 language (current_language->la_language),
7796 input_radix (::input_radix),
7797 cond_string (cond_string_ != nullptr
7798 ? make_unique_xstrdup (cond_string_)
7799 : nullptr),
7800 related_breakpoint (this)
7801{
7802}
7803
7804/* See breakpoint.h. */
7805
fed1c982 7806catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
92bb0228
PA
7807 const char *cond_string)
7808 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
c906108c 7809{
acd0955b 7810 add_dummy_location (this, current_program_space);
c5aa993b 7811
acd0955b 7812 pspace = current_program_space;
346774a9
PA
7813}
7814
28010a5d 7815void
b270e6f9 7816install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 7817{
b270e6f9 7818 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 7819 set_breakpoint_number (internal, b);
558a9d82
YQ
7820 if (is_tracepoint (b))
7821 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
7822 if (!internal)
7823 mention (b);
76727919 7824 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
7825
7826 if (update_gll)
44702360 7827 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
7828}
7829
c906108c 7830static int
fba45db2 7831hw_breakpoint_used_count (void)
c906108c 7832{
c906108c
SS
7833 int i = 0;
7834
43892fdf 7835 for (breakpoint *b : all_breakpoints ())
d6b74ac4 7836 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 7837 for (bp_location *bl : b->locations ())
f1310107
TJB
7838 {
7839 /* Special types of hardware breakpoints may use more than
7840 one register. */
c47614fe 7841 i += b->resources_needed (bl);
f1310107 7842 }
c906108c
SS
7843
7844 return i;
7845}
7846
a1398e0c
PA
7847/* Returns the resources B would use if it were a hardware
7848 watchpoint. */
7849
c906108c 7850static int
a1398e0c 7851hw_watchpoint_use_count (struct breakpoint *b)
c906108c 7852{
c906108c
SS
7853 int i = 0;
7854
a1398e0c
PA
7855 if (!breakpoint_enabled (b))
7856 return 0;
7857
40cb8ca5 7858 for (bp_location *bl : b->locations ())
a1398e0c
PA
7859 {
7860 /* Special types of hardware watchpoints may use more than
7861 one register. */
c47614fe 7862 i += b->resources_needed (bl);
a1398e0c
PA
7863 }
7864
7865 return i;
7866}
7867
7868/* Returns the sum the used resources of all hardware watchpoints of
7869 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7870 the sum of the used resources of all hardware watchpoints of other
7871 types _not_ TYPE. */
7872
7873static int
7874hw_watchpoint_used_count_others (struct breakpoint *except,
7875 enum bptype type, int *other_type_used)
7876{
7877 int i = 0;
a1398e0c 7878
c906108c 7879 *other_type_used = 0;
43892fdf 7880 for (breakpoint *b : all_breakpoints ())
e09342b5 7881 {
a1398e0c
PA
7882 if (b == except)
7883 continue;
e09342b5
TJB
7884 if (!breakpoint_enabled (b))
7885 continue;
7886
a1398e0c
PA
7887 if (b->type == type)
7888 i += hw_watchpoint_use_count (b);
7889 else if (is_hardware_watchpoint (b))
7890 *other_type_used = 1;
e09342b5
TJB
7891 }
7892
c906108c
SS
7893 return i;
7894}
7895
c906108c 7896void
fba45db2 7897disable_watchpoints_before_interactive_call_start (void)
c906108c 7898{
43892fdf 7899 for (breakpoint *b : all_breakpoints ())
cc60f2e3 7900 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 7901 {
b5de0fa7 7902 b->enable_state = bp_call_disabled;
44702360 7903 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 7904 }
c906108c
SS
7905}
7906
7907void
fba45db2 7908enable_watchpoints_after_interactive_call_stop (void)
c906108c 7909{
43892fdf 7910 for (breakpoint *b : all_breakpoints ())
cc60f2e3 7911 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7912 {
b5de0fa7 7913 b->enable_state = bp_enabled;
44702360 7914 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 7915 }
c906108c
SS
7916}
7917
8bea4e01
UW
7918void
7919disable_breakpoints_before_startup (void)
7920{
6c95b8df 7921 current_program_space->executing_startup = 1;
44702360 7922 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
7923}
7924
7925void
7926enable_breakpoints_after_startup (void)
7927{
6c95b8df 7928 current_program_space->executing_startup = 0;
f8eba3c6 7929 breakpoint_re_set ();
8bea4e01
UW
7930}
7931
7a3e3265
PA
7932/* Allocate a new momentary breakpoint. */
7933
7ab97995 7934template<typename... Arg>
7a3e3265 7935static momentary_breakpoint *
7ab97995
PA
7936new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
7937 Arg&&... args)
7a3e3265
PA
7938{
7939 if (type == bp_longjmp || type == bp_exception)
7ab97995
PA
7940 return new longjmp_breakpoint (gdbarch, type,
7941 std::forward<Arg> (args)...);
7a3e3265 7942 else
7ab97995
PA
7943 return new momentary_breakpoint (gdbarch, type,
7944 std::forward<Arg> (args)...);
7a3e3265
PA
7945}
7946
7c16b83e
PA
7947/* Set a momentary breakpoint of type TYPE at address specified by
7948 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7949 frame. */
c906108c 7950
454dafbd 7951breakpoint_up
a6d9a66e
UW
7952set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7953 struct frame_id frame_id, enum bptype type)
c906108c 7954{
193facb3
JK
7955 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7956 tail-called one. */
7957 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 7958
7a3e3265 7959 std::unique_ptr<momentary_breakpoint> b
7ab97995
PA
7960 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
7961 inferior_thread ()->global_num));
7a3e3265
PA
7962
7963 b->add_location (sal);
c906108c 7964
7a3e3265
PA
7965 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
7966
44702360 7967 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7968
7a3e3265 7969 return bp;
c906108c 7970}
611c83ae 7971
06edf0c0 7972/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
7973 The new breakpoint will have type TYPE, use OPS as its
7974 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 7975
06edf0c0
PA
7976static struct breakpoint *
7977momentary_breakpoint_from_master (struct breakpoint *orig,
7978 enum bptype type,
7ab97995
PA
7979 int loc_enabled,
7980 int thread)
e58b0e63 7981{
7a3e3265 7982 std::unique_ptr<breakpoint> copy
7ab97995
PA
7983 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
7984 orig->frame_id, thread));
ee89d0a4 7985 copy->loc = copy->allocate_location ();
0ba852ab 7986 set_breakpoint_location_function (copy->loc);
e58b0e63 7987
a6d9a66e 7988 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7989 copy->loc->requested_address = orig->loc->requested_address;
7990 copy->loc->address = orig->loc->address;
7991 copy->loc->section = orig->loc->section;
6c95b8df 7992 copy->loc->pspace = orig->loc->pspace;
55aa24fb 7993 copy->loc->probe = orig->loc->probe;
f8eba3c6 7994 copy->loc->line_number = orig->loc->line_number;
2f202fde 7995 copy->loc->symtab = orig->loc->symtab;
a1aa2221 7996 copy->loc->enabled = loc_enabled;
e58b0e63 7997
7a3e3265 7998 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
44702360 7999 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7a3e3265 8000 return b;
e58b0e63
PA
8001}
8002
06edf0c0
PA
8003/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8004 ORIG is NULL. */
8005
8006struct breakpoint *
8007clone_momentary_breakpoint (struct breakpoint *orig)
8008{
8009 /* If there's nothing to clone, then return nothing. */
8010 if (orig == NULL)
8011 return NULL;
8012
7ab97995
PA
8013 return momentary_breakpoint_from_master (orig, orig->type, 0,
8014 orig->thread);
06edf0c0
PA
8015}
8016
454dafbd 8017breakpoint_up
a6d9a66e
UW
8018set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8019 enum bptype type)
611c83ae
PA
8020{
8021 struct symtab_and_line sal;
8022
8023 sal = find_pc_line (pc, 0);
8024 sal.pc = pc;
8025 sal.section = find_pc_overlay (pc);
8026 sal.explicit_pc = 1;
8027
a6d9a66e 8028 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8029}
c906108c 8030\f
c5aa993b 8031
c906108c
SS
8032/* Tell the user we have just set a breakpoint B. */
8033
8034static void
b713485d 8035mention (const breakpoint *b)
c906108c 8036{
c47614fe 8037 b->print_mention ();
2d33446d 8038 current_uiout->text ("\n");
c906108c 8039}
c906108c 8040\f
c5aa993b 8041
5133a315 8042static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8043
7f32a4d5
PA
8044/* Handle "set breakpoint auto-hw on".
8045
8046 If the explicitly specified breakpoint type is not hardware
8047 breakpoint, check the memory map to see whether the breakpoint
8048 address is in read-only memory.
8049
8050 - location type is not hardware breakpoint, memory is read-only.
8051 We change the type of the location to hardware breakpoint.
8052
8053 - location type is hardware breakpoint, memory is read-write. This
8054 means we've previously made the location hardware one, but then the
8055 memory map changed, so we undo.
8056*/
8057
8058static void
8059handle_automatic_hardware_breakpoints (bp_location *bl)
8060{
8061 if (automatic_hardware_breakpoints
8062 && bl->owner->type != bp_hardware_breakpoint
8063 && (bl->loc_type == bp_loc_software_breakpoint
8064 || bl->loc_type == bp_loc_hardware_breakpoint))
8065 {
8066 /* When breakpoints are removed, remove_breakpoints will use
8067 location types we've just set here, the only possible problem
8068 is that memory map has changed during running program, but
8069 it's not going to work anyway with current gdb. */
8070 mem_region *mr = lookup_mem_region (bl->address);
8071
8072 if (mr != nullptr)
8073 {
8074 enum bp_loc_type new_type;
8075
8076 if (mr->attrib.mode != MEM_RW)
8077 new_type = bp_loc_hardware_breakpoint;
8078 else
8079 new_type = bp_loc_software_breakpoint;
8080
8081 if (new_type != bl->loc_type)
8082 {
8083 static bool said = false;
8084
8085 bl->loc_type = new_type;
8086 if (!said)
8087 {
6cb06a8c
TT
8088 gdb_printf (_("Note: automatically using "
8089 "hardware breakpoints for "
8090 "read-only addresses.\n"));
7f32a4d5
PA
8091 said = true;
8092 }
8093 }
8094 }
8095 }
8096}
8097
06615628 8098bp_location *
74421c0b 8099code_breakpoint::add_location (const symtab_and_line &sal)
0d381245 8100{
06615628 8101 struct bp_location *new_loc, **tmp;
3742cc8b 8102 CORE_ADDR adjusted_address;
06615628 8103 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8104
8105 if (loc_gdbarch == NULL)
06615628 8106 loc_gdbarch = gdbarch;
3742cc8b
YQ
8107
8108 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8109 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8110 location will be placed on the location chain. Adjustment of the
8111 breakpoint may cause target_read_memory() to be called and we do
8112 not want its scan of the location chain to find a breakpoint and
8113 location that's only been partially initialized. */
8114 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
60270718
AB
8115 sal.pc, type,
8116 sal.pspace);
0d381245 8117
d30113d4 8118 /* Sort the locations by their ADDRESS. */
06615628
PA
8119 new_loc = allocate_location ();
8120 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
d30113d4 8121 tmp = &((*tmp)->next))
0d381245 8122 ;
06615628
PA
8123 new_loc->next = *tmp;
8124 *tmp = new_loc;
8125
8126 new_loc->requested_address = sal.pc;
8127 new_loc->address = adjusted_address;
8128 new_loc->pspace = sal.pspace;
8129 new_loc->probe.prob = sal.prob;
8130 new_loc->probe.objfile = sal.objfile;
8131 gdb_assert (new_loc->pspace != NULL);
8132 new_loc->section = sal.section;
8133 new_loc->gdbarch = loc_gdbarch;
8134 new_loc->line_number = sal.line;
8135 new_loc->symtab = sal.symtab;
8136 new_loc->symbol = sal.symbol;
8137 new_loc->msymbol = sal.msymbol;
8138 new_loc->objfile = sal.objfile;
8139
8140 set_breakpoint_location_function (new_loc);
1a853c52 8141
6ae88661
LM
8142 /* While by definition, permanent breakpoints are already present in the
8143 code, we don't mark the location as inserted. Normally one would expect
8144 that GDB could rely on that breakpoint instruction to stop the program,
8145 thus removing the need to insert its own breakpoint, except that executing
8146 the breakpoint instruction can kill the target instead of reporting a
8147 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8148 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8149 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8150 breakpoint be inserted normally results in QEMU knowing about the GDB
8151 breakpoint, and thus trap before the breakpoint instruction is executed.
8152 (If GDB later needs to continue execution past the permanent breakpoint,
8153 it manually increments the PC, thus avoiding executing the breakpoint
8154 instruction.) */
06615628
PA
8155 if (bp_loc_is_permanent (new_loc))
8156 new_loc->permanent = 1;
1a853c52 8157
06615628 8158 return new_loc;
0d381245 8159}
514f746b
AR
8160\f
8161
5133a315
LM
8162/* Return true if LOC is pointing to a permanent breakpoint,
8163 return false otherwise. */
1cf4d951 8164
5133a315 8165static bool
1cf4d951
PA
8166bp_loc_is_permanent (struct bp_location *loc)
8167{
514f746b
AR
8168 gdb_assert (loc != NULL);
8169
cb1e4e32
PA
8170 /* If we have a non-breakpoint-backed catchpoint or a software
8171 watchpoint, just return 0. We should not attempt to read from
8172 the addresses the locations of these breakpoint types point to.
5133a315 8173 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8174 memory. */
cb1e4e32 8175 if (!bl_address_is_meaningful (loc))
5133a315 8176 return false;
244558af 8177
5ed8105e 8178 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8179 switch_to_program_space_and_thread (loc->pspace);
5133a315 8180 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8181}
8182
e7e0cddf
SS
8183/* Build a command list for the dprintf corresponding to the current
8184 settings of the dprintf style options. */
8185
8186static void
8187update_dprintf_command_list (struct breakpoint *b)
8188{
6f781ee3 8189 const char *dprintf_args = b->extra_string.get ();
8579fd13 8190 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf
SS
8191
8192 if (!dprintf_args)
8193 return;
8194
8195 dprintf_args = skip_spaces (dprintf_args);
8196
8197 /* Allow a comma, as it may have terminated a location, but don't
8198 insist on it. */
8199 if (*dprintf_args == ',')
8200 ++dprintf_args;
8201 dprintf_args = skip_spaces (dprintf_args);
8202
8203 if (*dprintf_args != '"')
8204 error (_("Bad format string, missing '\"'."));
8205
d3ce09f5 8206 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8207 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8208 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8209 {
e0700ba4 8210 if (dprintf_function.empty ())
e7e0cddf
SS
8211 error (_("No function supplied for dprintf call"));
8212
e0700ba4 8213 if (!dprintf_channel.empty ())
e7e0cddf 8214 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8215 dprintf_function.c_str (),
8216 dprintf_channel.c_str (),
e7e0cddf
SS
8217 dprintf_args);
8218 else
8219 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8220 dprintf_function.c_str (),
e7e0cddf
SS
8221 dprintf_args);
8222 }
d3ce09f5
SS
8223 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8224 {
8225 if (target_can_run_breakpoint_commands ())
8226 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8227 else
8228 {
8229 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8230 printf_line = xstrprintf ("printf %s", dprintf_args);
8231 }
8232 }
e7e0cddf
SS
8233 else
8234 internal_error (__FILE__, __LINE__,
8235 _("Invalid dprintf style."));
8236
f28045c2 8237 gdb_assert (printf_line != NULL);
e7e0cddf 8238
12973681
TT
8239 /* Manufacture a printf sequence. */
8240 struct command_line *printf_cmd_line
8579fd13 8241 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8242 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8243 command_lines_deleter ()));
e7e0cddf
SS
8244}
8245
8246/* Update all dprintf commands, making their command lists reflect
8247 current style settings. */
8248
8249static void
eb4c3f4a 8250update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8251 struct cmd_list_element *c)
8252{
43892fdf
SM
8253 for (breakpoint *b : all_breakpoints ())
8254 if (b->type == bp_dprintf)
e7e0cddf 8255 update_dprintf_command_list (b);
e7e0cddf 8256}
c3f6f71d 8257
74421c0b 8258code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
3b003a61
PA
8259 enum bptype type_,
8260 gdb::array_view<const symtab_and_line> sals,
264f9890 8261 location_spec_up &&locspec_,
3b003a61
PA
8262 gdb::unique_xmalloc_ptr<char> filter_,
8263 gdb::unique_xmalloc_ptr<char> cond_string_,
8264 gdb::unique_xmalloc_ptr<char> extra_string_,
8265 enum bpdisp disposition_,
8266 int thread_, int task_, int ignore_count_,
8267 int from_tty,
8268 int enabled_, unsigned flags,
8269 int display_canonical_)
8270 : breakpoint (gdbarch_, type_)
018d34a4 8271{
0d381245 8272 int i;
018d34a4
VP
8273
8274 if (type == bp_hardware_breakpoint)
8275 {
fbbd034e
AS
8276 int target_resources_ok;
8277
8278 i = hw_breakpoint_used_count ();
8279 target_resources_ok =
8280 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8281 i + 1, 0);
8282 if (target_resources_ok == 0)
8283 error (_("No hardware breakpoint support in the target."));
8284 else if (target_resources_ok < 0)
8285 error (_("Hardware breakpoints used exceeds limit."));
8286 }
8287
6c5b2ebe 8288 gdb_assert (!sals.empty ());
6c95b8df 8289
3b003a61
PA
8290 thread = thread_;
8291 task = task_;
0d381245 8292
3b003a61
PA
8293 cond_string = std::move (cond_string_);
8294 extra_string = std::move (extra_string_);
8295 ignore_count = ignore_count_;
8296 enable_state = enabled_ ? bp_enabled : bp_disabled;
8297 disposition = disposition_;
5af949e3 8298
3b003a61
PA
8299 if (type == bp_static_tracepoint
8300 || type == bp_static_marker_tracepoint)
8301 {
8302 auto *t = static_cast<struct tracepoint *> (this);
8303 struct static_tracepoint_marker marker;
0d381245 8304
3b003a61 8305 if (strace_marker_p (this))
0d381245 8306 {
3b003a61
PA
8307 /* We already know the marker exists, otherwise, we wouldn't
8308 see a sal for it. */
709438c7 8309 const char *p = &locspec_->to_string ()[3];
3b003a61 8310 const char *endp;
0fb4aa4b 8311
3b003a61 8312 p = skip_spaces (p);
0fb4aa4b 8313
3b003a61 8314 endp = skip_to_space (p);
0fb4aa4b 8315
3b003a61 8316 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8317
3b003a61
PA
8318 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8319 t->static_trace_marker_id.c_str ());
8320 }
8321 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8322 {
8323 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8324
3b003a61
PA
8325 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8326 t->static_trace_marker_id.c_str ());
0d381245
VP
8327 }
8328 else
3b003a61
PA
8329 warning (_("Couldn't determine the static tracepoint marker to probe"));
8330 }
8331
8332 for (const auto &sal : sals)
8333 {
8334 if (from_tty)
018d34a4 8335 {
3b003a61
PA
8336 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8337 if (loc_gdbarch == nullptr)
8338 loc_gdbarch = gdbarch;
8339
8340 describe_other_breakpoints (loc_gdbarch,
8341 sal.pspace, sal.pc, sal.section, thread);
0d381245
VP
8342 }
8343
3b003a61
PA
8344 bp_location *new_loc = add_location (sal);
8345 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8346 new_loc->inserted = 1;
8347
b5fa468f
TBA
8348 /* Do not set breakpoint locations conditions yet. As locations
8349 are inserted, they get sorted based on their addresses. Let
8350 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8351
8352 /* Dynamic printf requires and uses additional arguments on the
8353 command line, otherwise it's an error. */
8354 if (type == bp_dprintf)
8355 {
3b003a61
PA
8356 if (extra_string != nullptr)
8357 update_dprintf_command_list (this);
e7e0cddf
SS
8358 else
8359 error (_("Format string required"));
8360 }
3b003a61
PA
8361 else if (extra_string != nullptr)
8362 error (_("Garbage '%s' at end of command"), extra_string.get ());
855a6e68 8363 }
018d34a4 8364
b5fa468f
TBA
8365 /* The order of the locations is now stable. Set the location
8366 condition using the location's number. */
8367 int loc_num = 1;
3b003a61 8368 for (bp_location *bl : locations ())
b5fa468f 8369 {
3b003a61
PA
8370 if (cond_string != nullptr)
8371 set_breakpoint_location_condition (cond_string.get (), bl,
8372 number, loc_num);
b5fa468f
TBA
8373
8374 ++loc_num;
8375 }
8376
3b003a61 8377 display_canonical = display_canonical_;
264f9890
PA
8378 if (locspec_ != nullptr)
8379 locspec = std::move (locspec_);
018d34a4 8380 else
264f9890 8381 locspec = new_address_location_spec (this->loc->address, NULL, 0);
3b003a61 8382 filter = std::move (filter_);
d9b3f62e 8383}
018d34a4 8384
d9b3f62e
PA
8385static void
8386create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8387 gdb::array_view<const symtab_and_line> sals,
264f9890 8388 location_spec_up &&locspec,
e1e01040
PA
8389 gdb::unique_xmalloc_ptr<char> filter,
8390 gdb::unique_xmalloc_ptr<char> cond_string,
8391 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8392 enum bptype type, enum bpdisp disposition,
8393 int thread, int task, int ignore_count,
ef4848c7 8394 int from_tty,
44f238bb
PA
8395 int enabled, int internal, unsigned flags,
8396 int display_canonical)
d9b3f62e 8397{
74421c0b 8398 std::unique_ptr<code_breakpoint> b
3b003a61
PA
8399 = new_breakpoint_from_type (gdbarch,
8400 type,
8401 sals,
264f9890 8402 std::move (locspec),
3b003a61
PA
8403 std::move (filter),
8404 std::move (cond_string),
8405 std::move (extra_string),
8406 disposition,
8407 thread, task, ignore_count,
8408 from_tty,
8409 enabled, flags,
8410 display_canonical);
d9b3f62e 8411
b270e6f9 8412 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8413}
8414
8415/* Add SALS.nelts breakpoints to the breakpoint table. For each
8416 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8417 value. COND_STRING, if not NULL, specified the condition to be
8418 used for all breakpoints. Essentially the only case where
8419 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8420 function. In that case, it's still not possible to specify
8421 separate conditions for different overloaded functions, so
8422 we take just a single condition string.
8423
c3f6f71d 8424 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8425 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8426 array contents). If the function fails (error() is called), the
8427 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8428 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8429
8430static void
8cdf0e15 8431create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8432 struct linespec_result *canonical,
e1e01040
PA
8433 gdb::unique_xmalloc_ptr<char> cond_string,
8434 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8435 enum bptype type, enum bpdisp disposition,
8436 int thread, int task, int ignore_count,
ef4848c7 8437 int from_tty,
44f238bb 8438 int enabled, int internal, unsigned flags)
c906108c 8439{
f8eba3c6 8440 if (canonical->pre_expanded)
6c5b2ebe 8441 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8442
6c5b2ebe 8443 for (const auto &lsal : canonical->lsals)
c3f6f71d 8444 {
f00aae0f 8445 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8446 'break', without arguments. */
264f9890 8447 location_spec_up locspec
5c1ddcb6
PA
8448 = (canonical->locspec != nullptr
8449 ? canonical->locspec->clone ()
8450 : nullptr);
e1e01040 8451 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8452 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8453
6c5b2ebe 8454 create_breakpoint_sal (gdbarch, lsal.sals,
264f9890 8455 std::move (locspec),
e1e01040
PA
8456 std::move (filter_string),
8457 std::move (cond_string),
8458 std::move (extra_string),
e7e0cddf 8459 type, disposition,
ef4848c7 8460 thread, task, ignore_count,
44f238bb 8461 from_tty, enabled, internal, flags,
56435ebe 8462 canonical->special_display);
c3f6f71d 8463 }
c3f6f71d 8464}
c906108c 8465
264f9890 8466/* Parse LOCSPEC which is assumed to be a SAL specification possibly
c3f6f71d 8467 followed by conditionals. On return, SALS contains an array of SAL
264f9890
PA
8468 addresses found. LOCSPEC points to the end of the SAL (for
8469 linespec locspecs).
9998af43
TJB
8470
8471 The array and the line spec strings are allocated on the heap, it is
8472 the caller's responsibility to free them. */
c906108c 8473
b9362cc7 8474static void
264f9890 8475parse_breakpoint_sals (location_spec *locspec,
58438ac1 8476 struct linespec_result *canonical)
c3f6f71d 8477{
f00aae0f
KS
8478 struct symtab_and_line cursal;
8479
7464aeaa 8480 if (locspec->type () == LINESPEC_LOCATION_SPEC)
f00aae0f 8481 {
40d97ee2 8482 const char *spec = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 8483
a20714ff 8484 if (spec == NULL)
f00aae0f
KS
8485 {
8486 /* The last displayed codepoint, if it's valid, is our default
8487 breakpoint address. */
8488 if (last_displayed_sal_is_valid ())
8489 {
f00aae0f
KS
8490 /* Set sal's pspace, pc, symtab, and line to the values
8491 corresponding to the last call to print_frame_info.
8492 Be sure to reinitialize LINE with NOTCURRENT == 0
8493 as the breakpoint line number is inappropriate otherwise.
8494 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
8495 symtab_and_line sal = get_last_displayed_sal ();
8496 CORE_ADDR pc = sal.pc;
8497
f00aae0f
KS
8498 sal = find_pc_line (pc, 0);
8499
8500 /* "break" without arguments is equivalent to "break *PC"
8501 where PC is the last displayed codepoint's address. So
8502 make sure to set sal.explicit_pc to prevent GDB from
8503 trying to expand the list of sals to include all other
8504 instances with the same symtab and line. */
8505 sal.pc = pc;
8506 sal.explicit_pc = 1;
8507
6c5b2ebe
PA
8508 struct linespec_sals lsal;
8509 lsal.sals = {sal};
f00aae0f
KS
8510 lsal.canonical = NULL;
8511
6c5b2ebe 8512 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
8513 return;
8514 }
8515 else
8516 error (_("No default breakpoint address now."));
c906108c 8517 }
c906108c 8518 }
f00aae0f
KS
8519
8520 /* Force almost all breakpoints to be in terms of the
8521 current_source_symtab (which is decode_line_1's default).
8522 This should produce the results we want almost all of the
8523 time while leaving default_breakpoint_* alone.
8524
8525 ObjC: However, don't match an Objective-C method name which
8526 may have a '+' or '-' succeeded by a '['. */
8527 cursal = get_current_source_symtab_and_line ();
8528 if (last_displayed_sal_is_valid ())
c906108c 8529 {
a20714ff 8530 const char *spec = NULL;
cc80f267 8531
7464aeaa 8532 if (locspec->type () == LINESPEC_LOCATION_SPEC)
40d97ee2 8533 spec = as_linespec_location_spec (locspec)->spec_string;
cc80f267 8534
f00aae0f 8535 if (!cursal.symtab
a20714ff
PA
8536 || (spec != NULL
8537 && strchr ("+-", spec[0]) != NULL
8538 && spec[1] != '['))
f00aae0f 8539 {
264f9890 8540 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
8541 get_last_displayed_symtab (),
8542 get_last_displayed_line (),
8543 canonical, NULL, NULL);
8544 return;
8545 }
c906108c 8546 }
f00aae0f 8547
264f9890 8548 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 8549 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 8550}
c906108c 8551
c906108c 8552
c3f6f71d 8553/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8554 inserted as a breakpoint. If it can't throw an error. */
c906108c 8555
b9362cc7 8556static void
6c5b2ebe 8557breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 8558{
6c5b2ebe
PA
8559 for (auto &sal : sals)
8560 resolve_sal_pc (&sal);
c3f6f71d
JM
8561}
8562
7a697b8d
SS
8563/* Fast tracepoints may have restrictions on valid locations. For
8564 instance, a fast tracepoint using a jump instead of a trap will
8565 likely have to overwrite more bytes than a trap would, and so can
8566 only be placed where the instruction is longer than the jump, or a
8567 multi-instruction sequence does not have a jump into the middle of
8568 it, etc. */
8569
8570static void
8571check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 8572 gdb::array_view<const symtab_and_line> sals)
7a697b8d 8573{
6c5b2ebe 8574 for (const auto &sal : sals)
7a697b8d 8575 {
f8eba3c6
TT
8576 struct gdbarch *sarch;
8577
6c5b2ebe 8578 sarch = get_sal_arch (sal);
f8eba3c6
TT
8579 /* We fall back to GDBARCH if there is no architecture
8580 associated with SAL. */
8581 if (sarch == NULL)
8582 sarch = gdbarch;
281d762b
TT
8583 std::string msg;
8584 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 8585 error (_("May not have a fast tracepoint at %s%s"),
281d762b 8586 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
8587 }
8588}
8589
018d34a4
VP
8590/* Given TOK, a string specification of condition and thread, as
8591 accepted by the 'break' command, extract the condition
8592 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8593 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8594 If no condition is found, *COND_STRING is set to NULL.
8595 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8596
8597static void
bbc13ae3 8598find_condition_and_thread (const char *tok, CORE_ADDR pc,
6f781ee3
TT
8599 gdb::unique_xmalloc_ptr<char> *cond_string,
8600 int *thread, int *task,
8601 gdb::unique_xmalloc_ptr<char> *rest)
018d34a4 8602{
6f781ee3 8603 cond_string->reset ();
018d34a4 8604 *thread = -1;
ed1d1739 8605 *task = 0;
6f781ee3 8606 rest->reset ();
733d554a 8607 bool force = false;
ed1d1739 8608
018d34a4
VP
8609 while (tok && *tok)
8610 {
bbc13ae3 8611 const char *end_tok;
018d34a4 8612 int toklen;
bbc13ae3
KS
8613 const char *cond_start = NULL;
8614 const char *cond_end = NULL;
cc59ec59 8615
f1735a53 8616 tok = skip_spaces (tok);
e7e0cddf
SS
8617
8618 if ((*tok == '"' || *tok == ',') && rest)
8619 {
6f781ee3 8620 rest->reset (savestring (tok, strlen (tok)));
e7e0cddf
SS
8621 return;
8622 }
8623
f1735a53 8624 end_tok = skip_to_space (tok);
d634f2de 8625
018d34a4 8626 toklen = end_tok - tok;
d634f2de 8627
018d34a4
VP
8628 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8629 {
8630 tok = cond_start = end_tok + 1;
733d554a
TBA
8631 try
8632 {
8633 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8634 }
8635 catch (const gdb_exception_error &)
8636 {
8637 if (!force)
8638 throw;
8639 else
8640 tok = tok + strlen (tok);
8641 }
018d34a4 8642 cond_end = tok;
6f781ee3 8643 cond_string->reset (savestring (cond_start, cond_end - cond_start));
018d34a4 8644 }
733d554a
TBA
8645 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8646 {
5759831a 8647 tok = tok + toklen;
733d554a
TBA
8648 force = true;
8649 }
018d34a4
VP
8650 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8651 {
5d5658a1
PA
8652 const char *tmptok;
8653 struct thread_info *thr;
d634f2de 8654
018d34a4 8655 tok = end_tok + 1;
5d5658a1 8656 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
8657 if (tok == tmptok)
8658 error (_("Junk after thread keyword."));
5d5658a1 8659 *thread = thr->global_num;
bbc13ae3 8660 tok = tmptok;
018d34a4 8661 }
4a306c9a
JB
8662 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8663 {
8664 char *tmptok;
8665
8666 tok = end_tok + 1;
bbc13ae3 8667 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
8668 if (tok == tmptok)
8669 error (_("Junk after task keyword."));
8670 if (!valid_task_id (*task))
b6199126 8671 error (_("Unknown task %d."), *task);
bbc13ae3 8672 tok = tmptok;
4a306c9a 8673 }
e7e0cddf
SS
8674 else if (rest)
8675 {
6f781ee3 8676 rest->reset (savestring (tok, strlen (tok)));
ccab2054 8677 return;
e7e0cddf 8678 }
018d34a4
VP
8679 else
8680 error (_("Junk at end of arguments."));
8681 }
8682}
8683
b5fa468f
TBA
8684/* Call 'find_condition_and_thread' for each sal in SALS until a parse
8685 succeeds. The parsed values are written to COND_STRING, THREAD,
8686 TASK, and REST. See the comment of 'find_condition_and_thread'
8687 for the description of these parameters and INPUT. */
8688
8689static void
8690find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
6f781ee3
TT
8691 const char *input,
8692 gdb::unique_xmalloc_ptr<char> *cond_string,
8693 int *thread, int *task,
8694 gdb::unique_xmalloc_ptr<char> *rest)
b5fa468f
TBA
8695{
8696 int num_failures = 0;
8697 for (auto &sal : sals)
8698 {
6f781ee3 8699 gdb::unique_xmalloc_ptr<char> cond;
b5fa468f
TBA
8700 int thread_id = 0;
8701 int task_id = 0;
6f781ee3 8702 gdb::unique_xmalloc_ptr<char> remaining;
b5fa468f
TBA
8703
8704 /* Here we want to parse 'arg' to separate condition from thread
8705 number. But because parsing happens in a context and the
8706 contexts of sals might be different, try each until there is
8707 success. Finding one successful parse is sufficient for our
8708 goal. When setting the breakpoint we'll re-parse the
8709 condition in the context of each sal. */
8710 try
8711 {
8712 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8713 &task_id, &remaining);
6f781ee3 8714 *cond_string = std::move (cond);
b5fa468f
TBA
8715 *thread = thread_id;
8716 *task = task_id;
6f781ee3 8717 *rest = std::move (remaining);
b5fa468f
TBA
8718 break;
8719 }
8720 catch (const gdb_exception_error &e)
8721 {
8722 num_failures++;
8723 /* If no sal remains, do not continue. */
8724 if (num_failures == sals.size ())
8725 throw;
8726 }
8727 }
8728}
8729
0fb4aa4b
PA
8730/* Decode a static tracepoint marker spec. */
8731
6c5b2ebe 8732static std::vector<symtab_and_line>
f00aae0f 8733decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 8734{
f00aae0f
KS
8735 const char *p = &(*arg_p)[3];
8736 const char *endp;
0fb4aa4b 8737
f1735a53 8738 p = skip_spaces (p);
0fb4aa4b 8739
f1735a53 8740 endp = skip_to_space (p);
0fb4aa4b 8741
81b1e71c 8742 std::string marker_str (p, endp - p);
0fb4aa4b 8743
5d9310c4
SM
8744 std::vector<static_tracepoint_marker> markers
8745 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8746 if (markers.empty ())
81b1e71c
TT
8747 error (_("No known static tracepoint marker named %s"),
8748 marker_str.c_str ());
0fb4aa4b 8749
6c5b2ebe 8750 std::vector<symtab_and_line> sals;
5d9310c4 8751 sals.reserve (markers.size ());
0fb4aa4b 8752
5d9310c4 8753 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 8754 {
5d9310c4
SM
8755 symtab_and_line sal = find_pc_line (marker.address, 0);
8756 sal.pc = marker.address;
6c5b2ebe 8757 sals.push_back (sal);
5d9310c4 8758 }
0fb4aa4b 8759
0fb4aa4b
PA
8760 *arg_p = endp;
8761 return sals;
8762}
8763
bac7c5cf
GB
8764/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8765 according to IS_TRACEPOINT. */
8766
8767static const struct breakpoint_ops *
264f9890
PA
8768breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8769 bool is_tracepoint)
bac7c5cf
GB
8770{
8771 if (is_tracepoint)
8772 {
264f9890 8773 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8774 return &tracepoint_probe_breakpoint_ops;
8775 else
74421c0b 8776 return &code_breakpoint_ops;
bac7c5cf
GB
8777 }
8778 else
8779 {
264f9890 8780 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
8781 return &bkpt_probe_breakpoint_ops;
8782 else
74421c0b 8783 return &code_breakpoint_ops;
bac7c5cf
GB
8784 }
8785}
8786
8787/* See breakpoint.h. */
8788
8789const struct breakpoint_ops *
264f9890
PA
8790breakpoint_ops_for_location_spec (const location_spec *locspec,
8791 bool is_tracepoint)
bac7c5cf 8792{
264f9890
PA
8793 if (locspec != nullptr)
8794 return (breakpoint_ops_for_location_spec_type
7464aeaa 8795 (locspec->type (), is_tracepoint));
74421c0b 8796 return &code_breakpoint_ops;
bac7c5cf
GB
8797}
8798
f00aae0f 8799/* See breakpoint.h. */
0101ce28 8800
8cdf0e15
VP
8801int
8802create_breakpoint (struct gdbarch *gdbarch,
264f9890 8803 location_spec *locspec,
e1e01040
PA
8804 const char *cond_string,
8805 int thread, const char *extra_string,
10a636cc 8806 bool force_condition, int parse_extra,
0fb4aa4b 8807 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8808 int ignore_count,
8809 enum auto_boolean pending_break_support,
c0a91b2b 8810 const struct breakpoint_ops *ops,
44f238bb
PA
8811 int from_tty, int enabled, int internal,
8812 unsigned flags)
c3f6f71d 8813{
7efd8fc2 8814 struct linespec_result canonical;
0101ce28 8815 int pending = 0;
4a306c9a 8816 int task = 0;
86b17b60 8817 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8818
348d480f
PA
8819 gdb_assert (ops != NULL);
8820
f00aae0f
KS
8821 /* If extra_string isn't useful, set it to NULL. */
8822 if (extra_string != NULL && *extra_string == '\0')
8823 extra_string = NULL;
8824
a70b8144 8825 try
b78a6381 8826 {
264f9890 8827 ops->create_sals_from_location_spec (locspec, &canonical);
b78a6381 8828 }
230d2906 8829 catch (const gdb_exception_error &e)
0101ce28 8830 {
492d29ea
PA
8831 /* If caller is interested in rc value from parse, set
8832 value. */
8833 if (e.error == NOT_FOUND_ERROR)
0101ce28 8834 {
05ff989b
AC
8835 /* If pending breakpoint support is turned off, throw
8836 error. */
fa8d40ab
JJ
8837
8838 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 8839 throw;
723a2275
VP
8840
8841 exception_print (gdb_stderr, e);
fa8d40ab 8842
dda83cd7 8843 /* If pending breakpoint support is auto query and the user
05ff989b 8844 selects no, then simply return the error code. */
059fb39f 8845 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
8846 && !nquery (_("Make %s pending on future shared library load? "),
8847 bptype_string (type_wanted)))
fd9b8c24 8848 return 0;
fa8d40ab 8849
05ff989b
AC
8850 /* At this point, either the user was queried about setting
8851 a pending breakpoint and selected yes, or pending
8852 breakpoint behavior is on and thus a pending breakpoint
8853 is defaulted on behalf of the user. */
f00aae0f 8854 pending = 1;
0101ce28 8855 }
492d29ea 8856 else
eedc3f4f 8857 throw;
0101ce28 8858 }
492d29ea 8859
6c5b2ebe 8860 if (!pending && canonical.lsals.empty ())
492d29ea 8861 return 0;
c3f6f71d 8862
c3f6f71d
JM
8863 /* Resolve all line numbers to PC's and verify that the addresses
8864 are ok for the target. */
0101ce28 8865 if (!pending)
f8eba3c6 8866 {
6c5b2ebe
PA
8867 for (auto &lsal : canonical.lsals)
8868 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 8869 }
c3f6f71d 8870
7a697b8d 8871 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 8872 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 8873 {
6c5b2ebe
PA
8874 for (const auto &lsal : canonical.lsals)
8875 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 8876 }
7a697b8d 8877
c3f6f71d
JM
8878 /* Verify that condition can be parsed, before setting any
8879 breakpoints. Allocate a separate condition expression for each
4a64f543 8880 breakpoint. */
0101ce28 8881 if (!pending)
c3f6f71d 8882 {
e1e01040
PA
8883 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8884 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8885
f00aae0f 8886 if (parse_extra)
dda83cd7 8887 {
6f781ee3
TT
8888 gdb::unique_xmalloc_ptr<char> rest;
8889 gdb::unique_xmalloc_ptr<char> cond;
52d361e1 8890
6c5b2ebe 8891 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 8892
b5fa468f
TBA
8893 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8894 &cond, &thread, &task, &rest);
6f781ee3
TT
8895 cond_string_copy = std::move (cond);
8896 extra_string_copy = std::move (rest);
dda83cd7 8897 }
2f069f6f 8898 else
dda83cd7 8899 {
f00aae0f
KS
8900 if (type_wanted != bp_dprintf
8901 && extra_string != NULL && *extra_string != '\0')
8902 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 8903
10a636cc
TBA
8904 /* Check the validity of the condition. We should error out
8905 if the condition is invalid at all of the locations and
8906 if it is not forced. In the PARSE_EXTRA case above, this
8907 check is done when parsing the EXTRA_STRING. */
8908 if (cond_string != nullptr && !force_condition)
8909 {
8910 int num_failures = 0;
8911 const linespec_sals &lsal = canonical.lsals[0];
8912 for (const auto &sal : lsal.sals)
8913 {
8914 const char *cond = cond_string;
8915 try
8916 {
8917 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8918 /* One success is sufficient to keep going. */
8919 break;
8920 }
8921 catch (const gdb_exception_error &)
8922 {
8923 num_failures++;
8924 /* If this is the last sal, error out. */
8925 if (num_failures == lsal.sals.size ())
8926 throw;
8927 }
8928 }
8929 }
8930
0878d0fa
YQ
8931 /* Create a private copy of condition string. */
8932 if (cond_string)
e1e01040 8933 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
8934 /* Create a private copy of any extra string. */
8935 if (extra_string)
e1e01040 8936 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 8937 }
0fb4aa4b 8938
52d361e1 8939 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
8940 std::move (cond_string_copy),
8941 std::move (extra_string_copy),
8942 type_wanted,
d9b3f62e 8943 tempflag ? disp_del : disp_donttouch,
ef4848c7 8944 thread, task, ignore_count,
44f238bb 8945 from_tty, enabled, internal, flags);
c906108c 8946 }
0101ce28
JJ
8947 else
8948 {
73063f51
TT
8949 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
8950 type_wanted);
5c1ddcb6 8951 b->locspec = locspec->clone ();
bfccc43c 8952
f00aae0f
KS
8953 if (parse_extra)
8954 b->cond_string = NULL;
e12c7713
MK
8955 else
8956 {
8957 /* Create a private copy of condition string. */
6f781ee3
TT
8958 b->cond_string.reset (cond_string != NULL
8959 ? xstrdup (cond_string)
8960 : NULL);
15630549 8961 b->thread = thread;
e12c7713 8962 }
f00aae0f
KS
8963
8964 /* Create a private copy of any extra string. */
6f781ee3
TT
8965 b->extra_string.reset (extra_string != NULL
8966 ? xstrdup (extra_string)
8967 : NULL);
0101ce28 8968 b->ignore_count = ignore_count;
0101ce28 8969 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8970 b->condition_not_parsed = 1;
41447f92 8971 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 8972 if ((type_wanted != bp_breakpoint
dda83cd7 8973 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 8974 b->pspace = current_program_space;
8bea4e01 8975
b270e6f9 8976 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
8977 }
8978
6c5b2ebe 8979 if (canonical.lsals.size () > 1)
95a42b64 8980 {
3e43a32a
MS
8981 warning (_("Multiple breakpoints were set.\nUse the "
8982 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8983 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8984 }
8985
44702360 8986 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
8987
8988 return 1;
c3f6f71d 8989}
c906108c 8990
348d480f 8991/* Set a breakpoint.
72b2ff0e
VP
8992 ARG is a string describing breakpoint address,
8993 condition, and thread.
8994 FLAG specifies if a breakpoint is hardware on,
8995 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8996 and BP_TEMPFLAG. */
348d480f 8997
98deb0da 8998static void
f2fc3015 8999break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9000{
72b2ff0e 9001 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9002 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9003 ? bp_hardware_breakpoint
9004 : bp_breakpoint);
f00aae0f 9005
264f9890
PA
9006 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9007 const struct breakpoint_ops *ops
9008 = breakpoint_ops_for_location_spec (locspec.get (),
9009 false /* is_tracepoint */);
c3f6f71d 9010
8cdf0e15 9011 create_breakpoint (get_current_arch (),
264f9890 9012 locspec.get (),
10a636cc 9013 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9014 tempflag, type_wanted,
8cdf0e15
VP
9015 0 /* Ignore count */,
9016 pending_break_support,
55aa24fb 9017 ops,
8cdf0e15 9018 from_tty,
84f4c1fe 9019 1 /* enabled */,
44f238bb
PA
9020 0 /* internal */,
9021 0);
c906108c
SS
9022}
9023
c906108c
SS
9024/* Helper function for break_command_1 and disassemble_command. */
9025
9026void
fba45db2 9027resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9028{
9029 CORE_ADDR pc;
9030
9031 if (sal->pc == 0 && sal->symtab != NULL)
9032 {
9033 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9034 error (_("No line %d in file \"%s\"."),
05cba821 9035 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9036 sal->pc = pc;
6a048695 9037
4a64f543 9038 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9039 number, then skip the function prologue if necessary. */
6a048695 9040 if (sal->explicit_line)
059acae7 9041 skip_prologue_sal (sal);
c906108c
SS
9042 }
9043
9044 if (sal->section == 0 && sal->symtab != NULL)
9045 {
346d1dfe 9046 const struct blockvector *bv;
3977b71f 9047 const struct block *b;
c5aa993b 9048 struct symbol *sym;
c906108c 9049
43f3e411 9050 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9051 sal->symtab->compunit ());
c906108c
SS
9052 if (bv != NULL)
9053 {
7f0df278 9054 sym = block_linkage_function (b);
c906108c
SS
9055 if (sym != NULL)
9056 {
3c86fae3
SM
9057 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9058 sal->section
9059 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9060 }
9061 else
9062 {
4a64f543 9063 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9064 just have to look harder. This case can be executed
9065 if we have line numbers but no functions (as can
9066 happen in assembly source). */
c906108c 9067
5ed8105e 9068 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9069 switch_to_program_space_and_thread (sal->pspace);
c906108c 9070
5ed8105e 9071 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9072 if (msym.minsym)
1db66e34 9073 sal->section = msym.obj_section ();
c906108c
SS
9074 }
9075 }
9076 }
9077}
9078
9079void
0b39b52e 9080break_command (const char *arg, int from_tty)
c906108c 9081{
db107f19 9082 break_command_1 (arg, 0, from_tty);
c906108c
SS
9083}
9084
c906108c 9085void
0b39b52e 9086tbreak_command (const char *arg, int from_tty)
c906108c 9087{
db107f19 9088 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9089}
9090
c906108c 9091static void
0b39b52e 9092hbreak_command (const char *arg, int from_tty)
c906108c 9093{
db107f19 9094 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9095}
9096
9097static void
0b39b52e 9098thbreak_command (const char *arg, int from_tty)
c906108c 9099{
db107f19 9100 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9101}
9102
e7e0cddf
SS
9103/* The dynamic printf command is mostly like a regular breakpoint, but
9104 with a prewired command list consisting of a single output command,
9105 built from extra arguments supplied on the dprintf command
9106 line. */
9107
da821c7b 9108static void
0b39b52e 9109dprintf_command (const char *arg, int from_tty)
e7e0cddf 9110{
264f9890 9111 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f
KS
9112
9113 /* If non-NULL, ARG should have been advanced past the location;
9114 the next character must be ','. */
9115 if (arg != NULL)
9116 {
9117 if (arg[0] != ',' || arg[1] == '\0')
9118 error (_("Format string required"));
9119 else
9120 {
9121 /* Skip the comma. */
9122 ++arg;
9123 }
9124 }
9125
e7e0cddf 9126 create_breakpoint (get_current_arch (),
264f9890 9127 locspec.get (),
10a636cc 9128 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9129 0, bp_dprintf,
9130 0 /* Ignore count */,
9131 pending_break_support,
74421c0b 9132 &code_breakpoint_ops,
e7e0cddf
SS
9133 from_tty,
9134 1 /* enabled */,
9135 0 /* internal */,
9136 0);
9137}
9138
d3ce09f5 9139static void
0b39b52e 9140agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9141{
9142 error (_("May only run agent-printf on the target"));
9143}
9144
ec45bb67 9145/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9146
ec45bb67
TT
9147int
9148ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9149 const address_space *aspace,
9150 CORE_ADDR bp_addr,
9151 const target_waitstatus &ws)
f1310107 9152{
c272a98c
SM
9153 if (ws.kind () != TARGET_WAITKIND_STOPPED
9154 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9155 return 0;
9156
f1310107
TJB
9157 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9158 bl->length, aspace, bp_addr);
9159}
9160
ec45bb67 9161/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9162
ec45bb67
TT
9163int
9164ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9165{
9166 return target_ranged_break_num_registers ();
9167}
9168
ec45bb67 9169/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9170
ec45bb67 9171enum print_stop_action
7bd86313 9172ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9173{
ec45bb67 9174 struct bp_location *bl = loc;
79a45e25 9175 struct ui_out *uiout = current_uiout;
f1310107 9176
ec45bb67 9177 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9178
9179 /* Ranged breakpoints have only one location. */
9180 gdb_assert (bl && bl->next == NULL);
9181
ec45bb67 9182 annotate_breakpoint (number);
f303dbd6
PA
9183
9184 maybe_print_thread_hit_breakpoint (uiout);
9185
ec45bb67 9186 if (disposition == disp_del)
112e8700 9187 uiout->text ("Temporary ranged breakpoint ");
f1310107 9188 else
112e8700
SM
9189 uiout->text ("Ranged breakpoint ");
9190 if (uiout->is_mi_like_p ())
f1310107 9191 {
112e8700 9192 uiout->field_string ("reason",
f1310107 9193 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9194 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9195 }
ec45bb67 9196 uiout->field_signed ("bkptno", number);
112e8700 9197 uiout->text (", ");
f1310107
TJB
9198
9199 return PRINT_SRC_AND_LOC;
9200}
9201
ec45bb67 9202/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9203
ec45bb67 9204bool
a67bcaba 9205ranged_breakpoint::print_one (bp_location **last_loc) const
f1310107 9206{
ec45bb67 9207 struct bp_location *bl = loc;
f1310107 9208 struct value_print_options opts;
79a45e25 9209 struct ui_out *uiout = current_uiout;
f1310107
TJB
9210
9211 /* Ranged breakpoints have only one location. */
9212 gdb_assert (bl && bl->next == NULL);
9213
9214 get_user_print_options (&opts);
9215
9216 if (opts.addressprint)
9217 /* We don't print the address range here, it will be printed later
f7968895 9218 by ranged_breakpoint::print_one_detail. */
112e8700 9219 uiout->field_skip ("addr");
f1310107 9220 annotate_field (5);
ec45bb67 9221 print_breakpoint_location (this, bl);
f1310107 9222 *last_loc = bl;
c01e038b
TT
9223
9224 return true;
f1310107
TJB
9225}
9226
ec45bb67 9227/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9228
ec45bb67
TT
9229void
9230ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9231{
9232 CORE_ADDR address_start, address_end;
ec45bb67 9233 struct bp_location *bl = loc;
d7e74731 9234 string_file stb;
f1310107
TJB
9235
9236 gdb_assert (bl);
9237
9238 address_start = bl->address;
9239 address_end = address_start + bl->length - 1;
9240
112e8700 9241 uiout->text ("\taddress range: ");
d7e74731
PA
9242 stb.printf ("[%s, %s]",
9243 print_core_address (bl->gdbarch, address_start),
9244 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9245 uiout->field_stream ("addr", stb);
9246 uiout->text ("\n");
f1310107
TJB
9247}
9248
ec45bb67 9249/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9250
ec45bb67 9251void
b713485d 9252ranged_breakpoint::print_mention () const
f1310107 9253{
ec45bb67 9254 struct bp_location *bl = loc;
79a45e25 9255 struct ui_out *uiout = current_uiout;
f1310107
TJB
9256
9257 gdb_assert (bl);
ec45bb67 9258 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9259
2d33446d 9260 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
ec45bb67 9261 number, paddress (bl->gdbarch, bl->address),
2d33446d 9262 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9263}
9264
ec45bb67 9265/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9266
ec45bb67 9267void
4d1ae558 9268ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9269{
6cb06a8c 9270 gdb_printf (fp, "break-range %s, %s",
709438c7
PA
9271 locspec->to_string (),
9272 locspec_range_end->to_string ());
04d0163c 9273 print_recreate_thread (fp);
f1310107
TJB
9274}
9275
f1310107
TJB
9276/* Find the address where the end of the breakpoint range should be
9277 placed, given the SAL of the end of the range. This is so that if
9278 the user provides a line number, the end of the range is set to the
9279 last instruction of the given line. */
9280
9281static CORE_ADDR
9282find_breakpoint_range_end (struct symtab_and_line sal)
9283{
9284 CORE_ADDR end;
9285
9286 /* If the user provided a PC value, use it. Otherwise,
9287 find the address of the end of the given location. */
9288 if (sal.explicit_pc)
9289 end = sal.pc;
9290 else
9291 {
9292 int ret;
9293 CORE_ADDR start;
9294
9295 ret = find_line_pc_range (sal, &start, &end);
9296 if (!ret)
9297 error (_("Could not find location of the end of the range."));
9298
9299 /* find_line_pc_range returns the start of the next line. */
9300 end--;
9301 }
9302
9303 return end;
9304}
9305
9306/* Implement the "break-range" CLI command. */
9307
9308static void
0b39b52e 9309break_range_command (const char *arg, int from_tty)
f1310107 9310{
f2fc3015 9311 const char *arg_start;
f1310107
TJB
9312 struct linespec_result canonical_start, canonical_end;
9313 int bp_count, can_use_bp, length;
9314 CORE_ADDR end;
f1310107
TJB
9315
9316 /* We don't support software ranged breakpoints. */
9317 if (target_ranged_break_num_registers () < 0)
9318 error (_("This target does not support hardware ranged breakpoints."));
9319
9320 bp_count = hw_breakpoint_used_count ();
9321 bp_count += target_ranged_break_num_registers ();
9322 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9323 bp_count, 0);
9324 if (can_use_bp < 0)
9325 error (_("Hardware breakpoints used exceeds limit."));
9326
f8eba3c6 9327 arg = skip_spaces (arg);
f1310107
TJB
9328 if (arg == NULL || arg[0] == '\0')
9329 error(_("No address range specified."));
9330
f8eba3c6 9331 arg_start = arg;
264f9890
PA
9332 location_spec_up start_locspec
9333 = string_to_location_spec (&arg, current_language);
9334 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
f1310107
TJB
9335
9336 if (arg[0] != ',')
9337 error (_("Too few arguments."));
6c5b2ebe 9338 else if (canonical_start.lsals.empty ())
f1310107 9339 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9340
6c5b2ebe 9341 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9342
6c5b2ebe
PA
9343 if (canonical_start.lsals.size () > 1
9344 || lsal_start.sals.size () != 1)
f1310107
TJB
9345 error (_("Cannot create a ranged breakpoint with multiple locations."));
9346
6c5b2ebe 9347 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9348 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9349
9350 arg++; /* Skip the comma. */
f8eba3c6 9351 arg = skip_spaces (arg);
f1310107 9352
264f9890 9353 /* Parse the end location specification. */
f1310107 9354
f1310107
TJB
9355 arg_start = arg;
9356
f8eba3c6 9357 /* We call decode_line_full directly here instead of using
264f9890
PA
9358 parse_breakpoint_sals because we need to specify the start
9359 location spec's symtab and line as the default symtab and line
9360 for the end of the range. This makes it possible to have ranges
9361 like "foo.c:27, +14", where +14 means 14 lines from the start
9362 location spec. */
9363 location_spec_up end_locspec
9364 = string_to_location_spec (&arg, current_language);
9365 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9366 sal_start.symtab, sal_start.line,
9367 &canonical_end, NULL, NULL);
9368
6c5b2ebe 9369 if (canonical_end.lsals.empty ())
f1310107 9370 error (_("Could not find location of the end of the range."));
f8eba3c6 9371
6c5b2ebe
PA
9372 const linespec_sals &lsal_end = canonical_end.lsals[0];
9373 if (canonical_end.lsals.size () > 1
9374 || lsal_end.sals.size () != 1)
f1310107
TJB
9375 error (_("Cannot create a ranged breakpoint with multiple locations."));
9376
6c5b2ebe 9377 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9378
9379 end = find_breakpoint_range_end (sal_end);
9380 if (sal_start.pc > end)
177b42fe 9381 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9382
9383 length = end - sal_start.pc + 1;
9384 if (length < 0)
9385 /* Length overflowed. */
9386 error (_("Address range too large."));
9387 else if (length == 1)
9388 {
9389 /* This range is simple enough to be handled by
9390 the `hbreak' command. */
81b1e71c 9391 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9392
9393 return;
9394 }
9395
b925bf21
PA
9396 /* Now set up the breakpoint and install it. */
9397
9398 std::unique_ptr<breakpoint> br
9399 (new ranged_breakpoint (get_current_arch (),
9400 sal_start, length,
264f9890
PA
9401 std::move (start_locspec),
9402 std::move (end_locspec)));
f1310107 9403
27a62b43 9404 install_breakpoint (false, std::move (br), true);
f1310107
TJB
9405}
9406
4a64f543
MS
9407/* Return non-zero if EXP is verified as constant. Returned zero
9408 means EXP is variable. Also the constant detection may fail for
9409 some constant expressions and in such case still falsely return
9410 zero. */
2e6e3d9c 9411
dab72643 9412static bool
65d79d4b
SDJ
9413watchpoint_exp_is_const (const struct expression *exp)
9414{
1eaebe02 9415 return exp->op->constant_p ();
65d79d4b
SDJ
9416}
9417
3a292923 9418/* Implement the "re_set" method for watchpoints. */
348d480f 9419
3a292923
TT
9420void
9421watchpoint::re_set ()
348d480f
PA
9422{
9423 /* Watchpoint can be either on expression using entirely global
9424 variables, or it can be on local variables.
9425
9426 Watchpoints of the first kind are never auto-deleted, and even
9427 persist across program restarts. Since they can use variables
9428 from shared libraries, we need to reparse expression as libraries
9429 are loaded and unloaded.
9430
9431 Watchpoints on local variables can also change meaning as result
9432 of solib event. For example, if a watchpoint uses both a local
9433 and a global variables in expression, it's a local watchpoint,
9434 but unloading of a shared library will make the expression
9435 invalid. This is not a very common use case, but we still
9436 re-evaluate expression, to avoid surprises to the user.
9437
9438 Note that for local watchpoints, we re-evaluate it only if
9439 watchpoints frame id is still valid. If it's not, it means the
9440 watchpoint is out of scope and will be deleted soon. In fact,
9441 I'm not sure we'll ever be called in this case.
9442
9443 If a local watchpoint's frame id is still valid, then
3a292923 9444 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9445
3a5c3e22
PA
9446 Don't do anything about disabled watchpoints, since they will be
9447 reevaluated again when enabled. */
3a292923 9448 update_watchpoint (this, 1 /* reparse */);
348d480f
PA
9449}
9450
3a292923 9451/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9452
3a292923
TT
9453int
9454watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9455{
3a292923 9456 int length = exact ? 1 : bl->length;
e09342b5
TJB
9457
9458 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9459 cond_exp.get ());
77b06cd7
TJB
9460}
9461
3a292923 9462/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9463
3a292923
TT
9464int
9465watchpoint::remove_location (struct bp_location *bl,
9466 enum remove_bp_reason reason)
77b06cd7 9467{
3a292923 9468 int length = exact ? 1 : bl->length;
e09342b5
TJB
9469
9470 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9471 cond_exp.get ());
e09342b5
TJB
9472}
9473
3a292923
TT
9474int
9475watchpoint::breakpoint_hit (const struct bp_location *bl,
9476 const address_space *aspace, CORE_ADDR bp_addr,
9477 const target_waitstatus &ws)
e09342b5 9478{
348d480f 9479 struct breakpoint *b = bl->owner;
77b06cd7 9480
348d480f
PA
9481 /* Continuable hardware watchpoints are treated as non-existent if the
9482 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9483 some data address). Otherwise gdb won't stop on a break instruction
9484 in the code (not from a breakpoint) when a hardware watchpoint has
9485 been defined. Also skip watchpoints which we know did not trigger
9486 (did not match the data address). */
9487 if (is_hardware_watchpoint (b)
3a292923 9488 && watchpoint_triggered == watch_triggered_no)
348d480f 9489 return 0;
9c06b0b4 9490
348d480f 9491 return 1;
9c06b0b4
TJB
9492}
9493
3a292923
TT
9494void
9495watchpoint::check_status (bpstat *bs)
9c06b0b4 9496{
348d480f 9497 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9498
348d480f 9499 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9500}
9501
3a292923
TT
9502/* Implement the "resources_needed" method for hardware
9503 watchpoints. */
9c06b0b4 9504
3a292923
TT
9505int
9506watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9507{
3a292923 9508 int length = exact? 1 : bl->length;
348d480f
PA
9509
9510 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9511}
9512
3a292923
TT
9513/* Implement the "works_in_software_mode" method for hardware
9514 watchpoints. */
9c06b0b4 9515
5a61e176 9516bool
3a292923 9517watchpoint::works_in_software_mode () const
9c06b0b4 9518{
efa80663 9519 /* Read and access watchpoints only work with hardware support. */
3a292923 9520 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9521}
9522
3a292923 9523enum print_stop_action
7bd86313 9524watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9525{
348d480f 9526 struct breakpoint *b;
348d480f 9527 enum print_stop_action result;
79a45e25 9528 struct ui_out *uiout = current_uiout;
348d480f
PA
9529
9530 gdb_assert (bs->bp_location_at != NULL);
9531
348d480f
PA
9532 b = bs->breakpoint_at;
9533
f303dbd6
PA
9534 annotate_watchpoint (b->number);
9535 maybe_print_thread_hit_breakpoint (uiout);
9536
d7e74731
PA
9537 string_file stb;
9538
76f9c9cf 9539 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
9540 switch (b->type)
9541 {
348d480f 9542 case bp_watchpoint:
9c06b0b4 9543 case bp_hardware_watchpoint:
112e8700
SM
9544 if (uiout->is_mi_like_p ())
9545 uiout->field_string
9546 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 9547 mention (b);
76f9c9cf 9548 tuple_emitter.emplace (uiout, "value");
112e8700 9549 uiout->text ("\nOld value = ");
850645cf 9550 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9551 uiout->field_stream ("old", stb);
9552 uiout->text ("\nNew value = ");
3a292923 9553 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9554 uiout->field_stream ("new", stb);
9555 uiout->text ("\n");
348d480f
PA
9556 /* More than one watchpoint may have been triggered. */
9557 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9558 break;
9559
9560 case bp_read_watchpoint:
112e8700
SM
9561 if (uiout->is_mi_like_p ())
9562 uiout->field_string
9563 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 9564 mention (b);
76f9c9cf 9565 tuple_emitter.emplace (uiout, "value");
112e8700 9566 uiout->text ("\nValue = ");
3a292923 9567 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9568 uiout->field_stream ("value", stb);
9569 uiout->text ("\n");
348d480f 9570 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9571 break;
9572
9573 case bp_access_watchpoint:
348d480f
PA
9574 if (bs->old_val != NULL)
9575 {
112e8700
SM
9576 if (uiout->is_mi_like_p ())
9577 uiout->field_string
9578 ("reason",
348d480f
PA
9579 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9580 mention (b);
76f9c9cf 9581 tuple_emitter.emplace (uiout, "value");
112e8700 9582 uiout->text ("\nOld value = ");
850645cf 9583 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9584 uiout->field_stream ("old", stb);
9585 uiout->text ("\nNew value = ");
348d480f
PA
9586 }
9587 else
9588 {
9589 mention (b);
112e8700
SM
9590 if (uiout->is_mi_like_p ())
9591 uiout->field_string
9592 ("reason",
348d480f 9593 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 9594 tuple_emitter.emplace (uiout, "value");
112e8700 9595 uiout->text ("\nValue = ");
348d480f 9596 }
3a292923 9597 watchpoint_value_print (val.get (), &stb);
112e8700
SM
9598 uiout->field_stream ("new", stb);
9599 uiout->text ("\n");
348d480f 9600 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9601 break;
9602 default:
348d480f 9603 result = PRINT_UNKNOWN;
9c06b0b4
TJB
9604 }
9605
348d480f
PA
9606 return result;
9607}
9608
3a292923 9609/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 9610
3a292923 9611void
b713485d 9612watchpoint::print_mention () const
348d480f 9613{
79a45e25 9614 struct ui_out *uiout = current_uiout;
46b9c129 9615 const char *tuple_name;
348d480f 9616
3a292923 9617 switch (type)
348d480f
PA
9618 {
9619 case bp_watchpoint:
112e8700 9620 uiout->text ("Watchpoint ");
46b9c129 9621 tuple_name = "wpt";
348d480f
PA
9622 break;
9623 case bp_hardware_watchpoint:
112e8700 9624 uiout->text ("Hardware watchpoint ");
46b9c129 9625 tuple_name = "wpt";
348d480f
PA
9626 break;
9627 case bp_read_watchpoint:
112e8700 9628 uiout->text ("Hardware read watchpoint ");
46b9c129 9629 tuple_name = "hw-rwpt";
348d480f
PA
9630 break;
9631 case bp_access_watchpoint:
112e8700 9632 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 9633 tuple_name = "hw-awpt";
348d480f
PA
9634 break;
9635 default:
9636 internal_error (__FILE__, __LINE__,
9637 _("Invalid hardware watchpoint type."));
9638 }
9639
46b9c129 9640 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9641 uiout->field_signed ("number", number);
112e8700 9642 uiout->text (": ");
3a292923 9643 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
9644}
9645
3a292923 9646/* Implement the "print_recreate" method for watchpoints. */
348d480f 9647
3a292923 9648void
4d1ae558 9649watchpoint::print_recreate (struct ui_file *fp) const
348d480f 9650{
3a292923 9651 switch (type)
348d480f
PA
9652 {
9653 case bp_watchpoint:
9654 case bp_hardware_watchpoint:
6cb06a8c 9655 gdb_printf (fp, "watch");
348d480f
PA
9656 break;
9657 case bp_read_watchpoint:
6cb06a8c 9658 gdb_printf (fp, "rwatch");
348d480f
PA
9659 break;
9660 case bp_access_watchpoint:
6cb06a8c 9661 gdb_printf (fp, "awatch");
348d480f
PA
9662 break;
9663 default:
9664 internal_error (__FILE__, __LINE__,
9665 _("Invalid watchpoint type."));
9666 }
9667
3a292923 9668 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 9669 print_recreate_thread (fp);
348d480f
PA
9670}
9671
3a292923 9672/* Implement the "explains_signal" method for watchpoints. */
427cd150 9673
a6860f3a 9674bool
3a292923 9675watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
9676{
9677 /* A software watchpoint cannot cause a signal other than
9678 GDB_SIGNAL_TRAP. */
3a292923 9679 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 9680 return false;
427cd150 9681
a6860f3a 9682 return true;
427cd150
TT
9683}
9684
3a292923
TT
9685struct masked_watchpoint : public watchpoint
9686{
73063f51
TT
9687 using watchpoint::watchpoint;
9688
3a292923
TT
9689 int insert_location (struct bp_location *) override;
9690 int remove_location (struct bp_location *,
9691 enum remove_bp_reason reason) override;
9692 int resources_needed (const struct bp_location *) override;
5a61e176 9693 bool works_in_software_mode () const override;
7bd86313 9694 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 9695 void print_one_detail (struct ui_out *) const override;
b713485d 9696 void print_mention () const override;
4d1ae558 9697 void print_recreate (struct ui_file *fp) const override;
3a292923 9698};
348d480f 9699
3a292923 9700/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 9701
3a292923
TT
9702int
9703masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 9704{
3a292923 9705 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
9706 bl->watchpoint_type);
9707}
9708
3a292923 9709/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 9710
3a292923
TT
9711int
9712masked_watchpoint::remove_location (struct bp_location *bl,
9713 enum remove_bp_reason reason)
348d480f 9714{
3a292923 9715 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 9716 bl->watchpoint_type);
348d480f
PA
9717}
9718
3a292923
TT
9719/* Implement the "resources_needed" method for masked hardware
9720 watchpoints. */
348d480f 9721
3a292923
TT
9722int
9723masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 9724{
3a292923 9725 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
9726}
9727
3a292923
TT
9728/* Implement the "works_in_software_mode" method for masked hardware
9729 watchpoints. */
348d480f 9730
5a61e176 9731bool
3a292923 9732masked_watchpoint::works_in_software_mode () const
348d480f 9733{
5a61e176 9734 return false;
348d480f
PA
9735}
9736
3a292923
TT
9737/* Implement the "print_it" method for masked hardware
9738 watchpoints. */
348d480f 9739
3a292923 9740enum print_stop_action
7bd86313 9741masked_watchpoint::print_it (const bpstat *bs) const
348d480f
PA
9742{
9743 struct breakpoint *b = bs->breakpoint_at;
79a45e25 9744 struct ui_out *uiout = current_uiout;
348d480f
PA
9745
9746 /* Masked watchpoints have only one location. */
9747 gdb_assert (b->loc && b->loc->next == NULL);
9748
f303dbd6
PA
9749 annotate_watchpoint (b->number);
9750 maybe_print_thread_hit_breakpoint (uiout);
9751
348d480f
PA
9752 switch (b->type)
9753 {
9754 case bp_hardware_watchpoint:
112e8700
SM
9755 if (uiout->is_mi_like_p ())
9756 uiout->field_string
9757 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
9758 break;
9759
9760 case bp_read_watchpoint:
112e8700
SM
9761 if (uiout->is_mi_like_p ())
9762 uiout->field_string
9763 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
9764 break;
9765
9766 case bp_access_watchpoint:
112e8700
SM
9767 if (uiout->is_mi_like_p ())
9768 uiout->field_string
9769 ("reason",
348d480f
PA
9770 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9771 break;
9772 default:
9773 internal_error (__FILE__, __LINE__,
9774 _("Invalid hardware watchpoint type."));
9775 }
9776
9777 mention (b);
112e8700 9778 uiout->text (_("\n\
9c06b0b4
TJB
9779Check the underlying instruction at PC for the memory\n\
9780address and value which triggered this watchpoint.\n"));
112e8700 9781 uiout->text ("\n");
9c06b0b4
TJB
9782
9783 /* More than one watchpoint may have been triggered. */
9784 return PRINT_UNKNOWN;
9785}
9786
3a292923
TT
9787/* Implement the "print_one_detail" method for masked hardware
9788 watchpoints. */
9c06b0b4 9789
3a292923
TT
9790void
9791masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
9792{
9793 /* Masked watchpoints have only one location. */
3a292923 9794 gdb_assert (loc && loc->next == NULL);
9c06b0b4 9795
112e8700 9796 uiout->text ("\tmask ");
3a292923 9797 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
112e8700 9798 uiout->text ("\n");
9c06b0b4
TJB
9799}
9800
3a292923
TT
9801/* Implement the "print_mention" method for masked hardware
9802 watchpoints. */
9c06b0b4 9803
3a292923 9804void
b713485d 9805masked_watchpoint::print_mention () const
9c06b0b4 9806{
79a45e25 9807 struct ui_out *uiout = current_uiout;
46b9c129 9808 const char *tuple_name;
9c06b0b4 9809
3a292923 9810 switch (type)
9c06b0b4
TJB
9811 {
9812 case bp_hardware_watchpoint:
112e8700 9813 uiout->text ("Masked hardware watchpoint ");
46b9c129 9814 tuple_name = "wpt";
9c06b0b4
TJB
9815 break;
9816 case bp_read_watchpoint:
112e8700 9817 uiout->text ("Masked hardware read watchpoint ");
46b9c129 9818 tuple_name = "hw-rwpt";
9c06b0b4
TJB
9819 break;
9820 case bp_access_watchpoint:
112e8700 9821 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 9822 tuple_name = "hw-awpt";
9c06b0b4
TJB
9823 break;
9824 default:
9825 internal_error (__FILE__, __LINE__,
9826 _("Invalid hardware watchpoint type."));
9827 }
9828
46b9c129 9829 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 9830 uiout->field_signed ("number", number);
112e8700 9831 uiout->text (": ");
3a292923 9832 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
9833}
9834
3a292923
TT
9835/* Implement the "print_recreate" method for masked hardware
9836 watchpoints. */
9c06b0b4 9837
3a292923 9838void
4d1ae558 9839masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 9840{
3a292923 9841 switch (type)
9c06b0b4
TJB
9842 {
9843 case bp_hardware_watchpoint:
6cb06a8c 9844 gdb_printf (fp, "watch");
9c06b0b4
TJB
9845 break;
9846 case bp_read_watchpoint:
6cb06a8c 9847 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
9848 break;
9849 case bp_access_watchpoint:
6cb06a8c 9850 gdb_printf (fp, "awatch");
9c06b0b4
TJB
9851 break;
9852 default:
9853 internal_error (__FILE__, __LINE__,
9854 _("Invalid hardware watchpoint type."));
9855 }
9856
3a292923
TT
9857 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9858 phex (hw_wp_mask, sizeof (CORE_ADDR)));
04d0163c 9859 print_recreate_thread (fp);
9c06b0b4
TJB
9860}
9861
9c06b0b4
TJB
9862/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9863
f2478a7e 9864static bool
9c06b0b4
TJB
9865is_masked_watchpoint (const struct breakpoint *b)
9866{
3a292923 9867 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
9868}
9869
53a5351d 9870/* accessflag: hw_write: watch write,
dda83cd7 9871 hw_read: watch read,
53a5351d 9872 hw_access: watch access (read or write) */
c906108c 9873static void
bbc13ae3 9874watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 9875 bool just_location, bool internal)
c906108c 9876{
c1fc2657 9877 struct breakpoint *scope_breakpoint = NULL;
270140bd 9878 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 9879 struct value *result;
bb9d5f81 9880 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
9881 const char *exp_start = NULL;
9882 const char *exp_end = NULL;
9883 const char *tok, *end_tok;
9c06b0b4 9884 int toklen = -1;
bbc13ae3
KS
9885 const char *cond_start = NULL;
9886 const char *cond_end = NULL;
c906108c 9887 enum bptype bp_type;
37e4754d 9888 int thread = -1;
9c06b0b4
TJB
9889 /* Flag to indicate whether we are going to use masks for
9890 the hardware watchpoint. */
2e362716 9891 bool use_mask = false;
9c06b0b4 9892 CORE_ADDR mask = 0;
8a18382f 9893 int task = 0;
c906108c 9894
37e4754d
LM
9895 /* Make sure that we actually have parameters to parse. */
9896 if (arg != NULL && arg[0] != '\0')
9897 {
bbc13ae3
KS
9898 const char *value_start;
9899
9900 exp_end = arg + strlen (arg);
37e4754d 9901
9c06b0b4
TJB
9902 /* Look for "parameter value" pairs at the end
9903 of the arguments string. */
bbc13ae3 9904 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
9905 {
9906 /* Skip whitespace at the end of the argument list. */
9907 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9908 tok--;
9909
9910 /* Find the beginning of the last token.
9911 This is the value of the parameter. */
9912 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9913 tok--;
9914 value_start = tok + 1;
9915
9916 /* Skip whitespace. */
9917 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9918 tok--;
9919
9920 end_tok = tok;
9921
9922 /* Find the beginning of the second to last token.
9923 This is the parameter itself. */
9924 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9925 tok--;
9926 tok++;
9927 toklen = end_tok - tok + 1;
9928
61012eef 9929 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 9930 {
5d5658a1 9931 struct thread_info *thr;
9c06b0b4
TJB
9932 /* At this point we've found a "thread" token, which means
9933 the user is trying to set a watchpoint that triggers
9934 only in a specific thread. */
5d5658a1 9935 const char *endp;
37e4754d 9936
9c06b0b4
TJB
9937 if (thread != -1)
9938 error(_("You can specify only one thread."));
37e4754d 9939
9c06b0b4 9940 /* Extract the thread ID from the next token. */
5d5658a1 9941 thr = parse_thread_id (value_start, &endp);
37e4754d 9942
5d5658a1 9943 /* Check if the user provided a valid thread ID. */
9c06b0b4 9944 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 9945 invalid_thread_id_error (value_start);
9c06b0b4 9946
5d5658a1 9947 thread = thr->global_num;
9c06b0b4 9948 }
8a18382f
TT
9949 else if (toklen == 4 && startswith (tok, "task"))
9950 {
9951 char *tmp;
9952
9953 task = strtol (value_start, &tmp, 0);
9954 if (tmp == value_start)
9955 error (_("Junk after task keyword."));
9956 if (!valid_task_id (task))
9957 error (_("Unknown task %d."), task);
9958 }
61012eef 9959 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
9960 {
9961 /* We've found a "mask" token, which means the user wants to
9962 create a hardware watchpoint that is going to have the mask
9963 facility. */
9964 struct value *mask_value, *mark;
37e4754d 9965
9c06b0b4
TJB
9966 if (use_mask)
9967 error(_("You can specify only one mask."));
37e4754d 9968
2e362716 9969 use_mask = just_location = true;
37e4754d 9970
9c06b0b4
TJB
9971 mark = value_mark ();
9972 mask_value = parse_to_comma_and_eval (&value_start);
9973 mask = value_as_address (mask_value);
9974 value_free_to_mark (mark);
9975 }
9976 else
9977 /* We didn't recognize what we found. We should stop here. */
9978 break;
37e4754d 9979
9c06b0b4
TJB
9980 /* Truncate the string and get rid of the "parameter value" pair before
9981 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 9982 exp_end = tok;
9c06b0b4 9983 }
37e4754d 9984 }
bbc13ae3
KS
9985 else
9986 exp_end = arg;
37e4754d 9987
bbc13ae3
KS
9988 /* Parse the rest of the arguments. From here on out, everything
9989 is in terms of a newly allocated string instead of the original
9990 ARG. */
81b1e71c
TT
9991 std::string expression (arg, exp_end - arg);
9992 exp_start = arg = expression.c_str ();
699bd4cf
TT
9993 innermost_block_tracker tracker;
9994 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 9995 exp_end = arg;
fa8a61dc
TT
9996 /* Remove trailing whitespace from the expression before saving it.
9997 This makes the eventual display of the expression string a bit
9998 prettier. */
9999 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10000 --exp_end;
10001
65d79d4b 10002 /* Checking if the expression is not constant. */
4d01a485 10003 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10004 {
10005 int len;
10006
10007 len = exp_end - exp_start;
10008 while (len > 0 && isspace (exp_start[len - 1]))
10009 len--;
10010 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10011 }
10012
699bd4cf 10013 exp_valid_block = tracker.block ();
b926417a 10014 struct value *mark = value_mark ();
850645cf 10015 struct value *val_as_value = nullptr;
1eaebe02
TT
10016 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10017 just_location);
06a64a0b 10018
850645cf 10019 if (val_as_value != NULL && just_location)
bb9d5f81 10020 {
850645cf
TT
10021 saved_bitpos = value_bitpos (val_as_value);
10022 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10023 }
10024
850645cf 10025 value_ref_ptr val;
06a64a0b
TT
10026 if (just_location)
10027 {
9c06b0b4
TJB
10028 int ret;
10029
06a64a0b 10030 exp_valid_block = NULL;
850645cf 10031 val = release_value (value_addr (result));
06a64a0b 10032 value_free_to_mark (mark);
9c06b0b4
TJB
10033
10034 if (use_mask)
10035 {
850645cf 10036 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10037 mask);
10038 if (ret == -1)
10039 error (_("This target does not support masked watchpoints."));
10040 else if (ret == -2)
10041 error (_("Invalid mask or memory region."));
10042 }
06a64a0b 10043 }
850645cf
TT
10044 else if (val_as_value != NULL)
10045 val = release_value (val_as_value);
c906108c 10046
f1735a53
TT
10047 tok = skip_spaces (arg);
10048 end_tok = skip_to_space (tok);
c906108c
SS
10049
10050 toklen = end_tok - tok;
10051 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10052 {
10053 tok = cond_start = end_tok + 1;
699bd4cf
TT
10054 innermost_block_tracker if_tracker;
10055 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10056
10057 /* The watchpoint expression may not be local, but the condition
10058 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10059 cond_exp_valid_block = if_tracker.block ();
60e1c644 10060
c906108c
SS
10061 cond_end = tok;
10062 }
10063 if (*tok)
8a3fe4f8 10064 error (_("Junk at end of command."));
c906108c 10065
441d7c93
PA
10066 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10067
10068 /* Save this because create_internal_breakpoint below invalidates
10069 'wp_frame'. */
10070 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10071
10072 /* If the expression is "local", then set up a "watchpoint scope"
10073 breakpoint at the point where we've left the scope of the watchpoint
10074 expression. Create the scope breakpoint before the watchpoint, so
10075 that we will encounter it first in bpstat_stop_status. */
441d7c93 10076 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10077 {
441d7c93
PA
10078 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10079
10080 if (frame_id_p (caller_frame_id))
edb3359d 10081 {
441d7c93
PA
10082 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10083 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10084
24b21115 10085 scope_breakpoint
441d7c93 10086 = create_internal_breakpoint (caller_arch, caller_pc,
9efa3c7f 10087 bp_watchpoint_scope);
d983da9c 10088
441d7c93
PA
10089 /* create_internal_breakpoint could invalidate WP_FRAME. */
10090 wp_frame = NULL;
10091
edb3359d 10092 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10093
edb3359d
DJ
10094 /* Automatically delete the breakpoint when it hits. */
10095 scope_breakpoint->disposition = disp_del;
d983da9c 10096
edb3359d 10097 /* Only break in the proper frame (help with recursion). */
441d7c93 10098 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10099
edb3359d 10100 /* Set the address at which we will stop. */
441d7c93
PA
10101 scope_breakpoint->loc->gdbarch = caller_arch;
10102 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10103 scope_breakpoint->loc->address
a6d9a66e
UW
10104 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10105 scope_breakpoint->loc->requested_address,
60270718
AB
10106 scope_breakpoint->type,
10107 current_program_space);
edb3359d 10108 }
d983da9c
DJ
10109 }
10110
e8369a73
AB
10111 /* Now set up the breakpoint. We create all watchpoints as hardware
10112 watchpoints here even if hardware watchpoints are turned off, a call
10113 to update_watchpoint later in this function will cause the type to
10114 drop back to bp_watchpoint (software watchpoint) if required. */
10115
10116 if (accessflag == hw_read)
10117 bp_type = bp_read_watchpoint;
10118 else if (accessflag == hw_access)
10119 bp_type = bp_access_watchpoint;
10120 else
10121 bp_type = bp_hardware_watchpoint;
3a5c3e22 10122
3a292923 10123 std::unique_ptr<watchpoint> w;
348d480f 10124 if (use_mask)
73063f51 10125 w.reset (new masked_watchpoint (nullptr, bp_type));
348d480f 10126 else
73063f51 10127 w.reset (new watchpoint (nullptr, bp_type));
3a292923 10128
c1fc2657 10129 w->thread = thread;
8a18382f 10130 w->task = task;
c1fc2657
SM
10131 w->disposition = disp_donttouch;
10132 w->pspace = current_program_space;
b22e99fd 10133 w->exp = std::move (exp);
3a5c3e22
PA
10134 w->exp_valid_block = exp_valid_block;
10135 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10136 if (just_location)
10137 {
850645cf
TT
10138 struct type *t = value_type (val.get ());
10139 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10140
43cc5389 10141 w->exp_string_reparse
a4c50be3 10142 = current_language->watch_location_expression (t, addr);
06a64a0b 10143
8579fd13
AB
10144 w->exp_string = xstrprintf ("-location %.*s",
10145 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10146 }
10147 else
a4c50be3 10148 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10149
10150 if (use_mask)
10151 {
3a5c3e22 10152 w->hw_wp_mask = mask;
9c06b0b4
TJB
10153 }
10154 else
10155 {
3a5c3e22 10156 w->val = val;
bb9d5f81
PP
10157 w->val_bitpos = saved_bitpos;
10158 w->val_bitsize = saved_bitsize;
4c1d86d9 10159 w->val_valid = true;
9c06b0b4 10160 }
77b06cd7 10161
c906108c 10162 if (cond_start)
6f781ee3 10163 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10164 else
c1fc2657 10165 w->cond_string = 0;
c5aa993b 10166
441d7c93 10167 if (frame_id_p (watchpoint_frame))
f6bc2008 10168 {
441d7c93 10169 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10170 w->watchpoint_thread = inferior_ptid;
f6bc2008 10171 }
c906108c 10172 else
f6bc2008 10173 {
3a5c3e22
PA
10174 w->watchpoint_frame = null_frame_id;
10175 w->watchpoint_thread = null_ptid;
f6bc2008 10176 }
c906108c 10177
d983da9c 10178 if (scope_breakpoint != NULL)
c906108c 10179 {
d983da9c
DJ
10180 /* The scope breakpoint is related to the watchpoint. We will
10181 need to act on them together. */
c1fc2657 10182 w->related_breakpoint = scope_breakpoint;
b270e6f9 10183 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10184 }
d983da9c 10185
06a64a0b
TT
10186 if (!just_location)
10187 value_free_to_mark (mark);
2d134ed3 10188
b270e6f9
TT
10189 /* Finally update the new watchpoint. This creates the locations
10190 that should be inserted. */
10191 update_watchpoint (w.get (), 1);
a9634178 10192
b270e6f9 10193 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10194}
10195
e09342b5 10196/* Return count of debug registers needed to watch the given expression.
e09342b5 10197 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10198
c906108c 10199static int
a6535de1 10200can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10201{
10202 int found_memory_cnt = 0;
10203
10204 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10205 if (!can_use_hw_watchpoints)
c906108c 10206 return 0;
c5aa993b 10207
a6535de1
TT
10208 gdb_assert (!vals.empty ());
10209 struct value *head = vals[0].get ();
10210
5c44784c
JM
10211 /* Make sure that the value of the expression depends only upon
10212 memory contents, and values computed from them within GDB. If we
10213 find any register references or function calls, we can't use a
10214 hardware watchpoint.
10215
10216 The idea here is that evaluating an expression generates a series
10217 of values, one holding the value of every subexpression. (The
10218 expression a*b+c has five subexpressions: a, b, a*b, c, and
10219 a*b+c.) GDB's values hold almost enough information to establish
10220 the criteria given above --- they identify memory lvalues,
10221 register lvalues, computed values, etcetera. So we can evaluate
10222 the expression, and then scan the chain of values that leaves
10223 behind to decide whether we can detect any possible change to the
10224 expression's final value using only hardware watchpoints.
10225
10226 However, I don't think that the values returned by inferior
10227 function calls are special in any way. So this function may not
10228 notice that an expression involving an inferior function call
10229 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10230 for (const value_ref_ptr &iter : vals)
c906108c 10231 {
a6535de1
TT
10232 struct value *v = iter.get ();
10233
5c44784c 10234 if (VALUE_LVAL (v) == lval_memory)
c906108c 10235 {
8464be76
DJ
10236 if (v != head && value_lazy (v))
10237 /* A lazy memory lvalue in the chain is one that GDB never
10238 needed to fetch; we either just used its address (e.g.,
10239 `a' in `a.b') or we never needed it at all (e.g., `a'
10240 in `a,b'). This doesn't apply to HEAD; if that is
10241 lazy then it was not readable, but watch it anyway. */
5c44784c 10242 ;
53a5351d 10243 else
5c44784c
JM
10244 {
10245 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10246 it with hardware watchpoints. */
df407dfe 10247 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10248
10249 /* We only watch structs and arrays if user asked for it
10250 explicitly, never if they just happen to appear in a
10251 middle of some value chain. */
10252 if (v == head
78134374
SM
10253 || (vtype->code () != TYPE_CODE_STRUCT
10254 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10255 {
42ae5230 10256 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10257 int len;
10258 int num_regs;
10259
a9634178 10260 len = (target_exact_watchpoints
e09342b5 10261 && is_scalar_type_recursive (vtype))?
df86565b 10262 1 : value_type (v)->length ();
2e70b7b9 10263
e09342b5
TJB
10264 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10265 if (!num_regs)
2e70b7b9
MS
10266 return 0;
10267 else
e09342b5 10268 found_memory_cnt += num_regs;
2e70b7b9 10269 }
5c44784c 10270 }
c5aa993b 10271 }
5086187c
AC
10272 else if (VALUE_LVAL (v) != not_lval
10273 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10274 return 0; /* These are values from the history (e.g., $1). */
5086187c 10275 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10276 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10277 }
10278
10279 /* The expression itself looks suitable for using a hardware
10280 watchpoint, but give the target machine a chance to reject it. */
10281 return found_memory_cnt;
10282}
10283
8b93c638 10284void
2e362716 10285watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10286{
84f4c1fe 10287 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10288}
10289
a15a5258
AB
10290/* Options for the watch, awatch, and rwatch commands. */
10291
10292struct watch_options
10293{
10294 /* For -location. */
10295 bool location = false;
10296};
10297
10298/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10299
10300 Historically GDB always accepted both '-location' and '-l' flags for
10301 these commands (both flags being synonyms). When converting to the
10302 newer option scheme only '-location' is added here. That's fine (for
10303 backward compatibility) as any non-ambiguous prefix of a flag will be
10304 accepted, so '-l', '-loc', are now all accepted.
10305
10306 What this means is that, if in the future, we add any new flag here
10307 that starts with '-l' then this will break backward compatibility, so
10308 please, don't do that! */
10309
10310static const gdb::option::option_def watch_option_defs[] = {
10311 gdb::option::flag_option_def<watch_options> {
10312 "location",
10313 [] (watch_options *opt) { return &opt->location; },
10314 N_("\
10315This evaluates EXPRESSION and watches the memory to which is refers.\n\
10316-l can be used as a short form of -location."),
10317 },
10318};
10319
10320/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10321 commands. */
10322
10323static gdb::option::option_def_group
10324make_watch_options_def_group (watch_options *opts)
10325{
10326 return {{watch_option_defs}, opts};
10327}
10328
06a64a0b
TT
10329/* A helper function that looks for the "-location" argument and then
10330 calls watch_command_1. */
10331
10332static void
0b39b52e 10333watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10334{
a15a5258
AB
10335 watch_options opts;
10336 auto grp = make_watch_options_def_group (&opts);
10337 gdb::option::process_options
10338 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10339 if (arg != nullptr && *arg == '\0')
10340 arg = nullptr;
10341
10342 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10343}
06a64a0b 10344
a15a5258
AB
10345/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10346static void
10347watch_command_completer (struct cmd_list_element *ignore,
10348 completion_tracker &tracker,
10349 const char *text, const char * /*word*/)
10350{
10351 const auto group = make_watch_options_def_group (nullptr);
10352 if (gdb::option::complete_options
10353 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10354 return;
06a64a0b 10355
a15a5258
AB
10356 const char *word = advance_to_expression_complete_word_point (tracker, text);
10357 expression_completer (ignore, tracker, text, word);
8b93c638 10358}
8926118c 10359
c5aa993b 10360static void
0b39b52e 10361watch_command (const char *arg, int from_tty)
c906108c 10362{
06a64a0b 10363 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10364}
10365
8b93c638 10366void
2e362716 10367rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10368{
84f4c1fe 10369 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10370}
8926118c 10371
c5aa993b 10372static void
0b39b52e 10373rwatch_command (const char *arg, int from_tty)
c906108c 10374{
06a64a0b 10375 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10376}
10377
8b93c638 10378void
2e362716 10379awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10380{
84f4c1fe 10381 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10382}
8926118c 10383
c5aa993b 10384static void
0b39b52e 10385awatch_command (const char *arg, int from_tty)
c906108c 10386{
06a64a0b 10387 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10388}
c906108c 10389\f
c5aa993b 10390
cfc31633
PA
10391/* Data for the FSM that manages the until(location)/advance commands
10392 in infcmd.c. Here because it uses the mechanisms of
10393 breakpoints. */
c906108c 10394
46e3ed7f 10395struct until_break_fsm : public thread_fsm
bfec99b2 10396{
46e3ed7f 10397 /* The thread that was current when the command was executed. */
cfc31633
PA
10398 int thread;
10399
df631783
PA
10400 /* The breakpoint set at the return address in the caller frame,
10401 plus breakpoints at all the destination locations. */
10402 std::vector<breakpoint_up> breakpoints;
cfc31633 10403
46e3ed7f 10404 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 10405 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
10406 : thread_fsm (cmd_interp),
10407 thread (thread),
df631783 10408 breakpoints (std::move (breakpoints))
46e3ed7f
TT
10409 {
10410 }
cfc31633 10411
46e3ed7f
TT
10412 void clean_up (struct thread_info *thread) override;
10413 bool should_stop (struct thread_info *thread) override;
10414 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10415};
10416
cfc31633
PA
10417/* Implementation of the 'should_stop' FSM method for the
10418 until(location)/advance commands. */
10419
46e3ed7f
TT
10420bool
10421until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10422{
df631783
PA
10423 for (const breakpoint_up &bp : breakpoints)
10424 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10425 bp.get ()) != NULL)
10426 {
10427 set_finished ();
10428 break;
10429 }
cfc31633 10430
46e3ed7f 10431 return true;
cfc31633
PA
10432}
10433
10434/* Implementation of the 'clean_up' FSM method for the
10435 until(location)/advance commands. */
10436
46e3ed7f
TT
10437void
10438until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10439{
cfc31633 10440 /* Clean up our temporary breakpoints. */
df631783 10441 breakpoints.clear ();
46e3ed7f 10442 delete_longjmp_breakpoint (thread);
cfc31633
PA
10443}
10444
10445/* Implementation of the 'async_reply_reason' FSM method for the
10446 until(location)/advance commands. */
10447
46e3ed7f
TT
10448enum async_reply_reason
10449until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10450{
10451 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10452}
10453
c906108c 10454void
f2fc3015 10455until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10456{
8556afb4
PA
10457 struct frame_info *frame;
10458 struct gdbarch *frame_gdbarch;
10459 struct frame_id stack_frame_id;
10460 struct frame_id caller_frame_id;
186c406b
TT
10461 int thread;
10462 struct thread_info *tp;
c906108c 10463
70509625 10464 clear_proceed_status (0);
c906108c
SS
10465
10466 /* Set a breakpoint where the user wants it and at return from
4a64f543 10467 this function. */
c5aa993b 10468
264f9890 10469 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f 10470
6c5b2ebe
PA
10471 std::vector<symtab_and_line> sals
10472 = (last_displayed_sal_is_valid ()
264f9890 10473 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
6c5b2ebe
PA
10474 get_last_displayed_symtab (),
10475 get_last_displayed_line ())
264f9890 10476 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 10477 NULL, NULL, 0));
c5aa993b 10478
df631783 10479 if (sals.empty ())
8a3fe4f8 10480 error (_("Couldn't get information on specified line."));
c5aa993b 10481
c906108c 10482 if (*arg)
8a3fe4f8 10483 error (_("Junk at end of arguments."));
c5aa993b 10484
186c406b 10485 tp = inferior_thread ();
5d5658a1 10486 thread = tp->global_num;
186c406b 10487
8556afb4
PA
10488 /* Note linespec handling above invalidates the frame chain.
10489 Installing a breakpoint also invalidates the frame chain (as it
10490 may need to switch threads), so do any frame handling before
10491 that. */
10492
10493 frame = get_selected_frame (NULL);
10494 frame_gdbarch = get_frame_arch (frame);
10495 stack_frame_id = get_stack_frame_id (frame);
10496 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10497
ae66c1fc
EZ
10498 /* Keep within the current frame, or in frames called by the current
10499 one. */
edb3359d 10500
df631783 10501 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
10502
10503 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10504
883bc8d1 10505 if (frame_id_p (caller_frame_id))
c906108c 10506 {
883bc8d1 10507 struct symtab_and_line sal2;
cfc31633 10508 struct gdbarch *caller_gdbarch;
883bc8d1
PA
10509
10510 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10511 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 10512 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
10513
10514 breakpoint_up caller_breakpoint
10515 = set_momentary_breakpoint (caller_gdbarch, sal2,
10516 caller_frame_id, bp_until);
10517 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 10518
0f443d1b 10519 set_longjmp_breakpoint (tp, stack_frame_id);
5419bdae 10520 lj_deleter.emplace (thread);
c906108c 10521 }
c5aa993b 10522
c70a6932
JK
10523 /* set_momentary_breakpoint could invalidate FRAME. */
10524 frame = NULL;
10525
df631783
PA
10526 /* If the user told us to continue until a specified location, we
10527 don't specify a frame at which we need to stop. Otherwise,
10528 specify the selected frame, because we want to stop only at the
10529 very same frame. */
10530 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10531
10532 for (symtab_and_line &sal : sals)
10533 {
10534 resolve_sal_pc (&sal);
10535
10536 breakpoint_up location_breakpoint
10537 = set_momentary_breakpoint (frame_gdbarch, sal,
10538 stop_frame_id, bp_until);
10539 breakpoints.emplace_back (std::move (location_breakpoint));
10540 }
883bc8d1 10541
573269a8
LS
10542 tp->set_thread_fsm
10543 (std::unique_ptr<thread_fsm>
10544 (new until_break_fsm (command_interp (), tp->global_num,
10545 std::move (breakpoints))));
f107f563 10546
5419bdae
TT
10547 if (lj_deleter)
10548 lj_deleter->release ();
f107f563 10549
cfc31633 10550 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 10551}
ae66c1fc 10552
c906108c
SS
10553\f
10554
81b1e71c 10555/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
10556
10557static int
81b1e71c 10558compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 10559{
81b1e71c
TT
10560 uintptr_t ua = (uintptr_t) a;
10561 uintptr_t ub = (uintptr_t) b;
8a2c437b 10562
81b1e71c 10563 if (a->number < b->number)
8a2c437b 10564 return -1;
81b1e71c 10565 else if (a->number > b->number)
8a2c437b
TT
10566 return 1;
10567
10568 /* Now sort by address, in case we see, e..g, two breakpoints with
10569 the number 0. */
10570 if (ua < ub)
10571 return -1;
94b0e70d 10572 return ua > ub ? 1 : 0;
8a2c437b
TT
10573}
10574
80f8a6eb 10575/* Delete breakpoints by address or line. */
c906108c
SS
10576
10577static void
0b39b52e 10578clear_command (const char *arg, int from_tty)
c906108c 10579{
c906108c 10580 int default_match;
c906108c 10581
6c5b2ebe
PA
10582 std::vector<symtab_and_line> decoded_sals;
10583 symtab_and_line last_sal;
10584 gdb::array_view<symtab_and_line> sals;
c906108c
SS
10585 if (arg)
10586 {
6c5b2ebe
PA
10587 decoded_sals
10588 = decode_line_with_current_source (arg,
10589 (DECODE_LINE_FUNFIRSTLINE
10590 | DECODE_LINE_LIST_MODE));
c906108c 10591 default_match = 0;
6c5b2ebe 10592 sals = decoded_sals;
c906108c
SS
10593 }
10594 else
10595 {
1bfeeb0f
JL
10596 /* Set sal's line, symtab, pc, and pspace to the values
10597 corresponding to the last call to print_frame_info. If the
10598 codepoint is not valid, this will set all the fields to 0. */
51abb421 10599 last_sal = get_last_displayed_sal ();
6c5b2ebe 10600 if (last_sal.symtab == 0)
8a3fe4f8 10601 error (_("No source file specified."));
c906108c 10602
c906108c 10603 default_match = 1;
6c5b2ebe 10604 sals = last_sal;
c906108c
SS
10605 }
10606
4a64f543
MS
10607 /* We don't call resolve_sal_pc here. That's not as bad as it
10608 seems, because all existing breakpoints typically have both
10609 file/line and pc set. So, if clear is given file/line, we can
10610 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10611
10612 We only support clearing given the address explicitly
10613 present in breakpoint table. Say, we've set breakpoint
4a64f543 10614 at file:line. There were several PC values for that file:line,
ed0616c6 10615 due to optimization, all in one block.
4a64f543
MS
10616
10617 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10618 PC corresponding to the same file:line, the breakpoint won't
10619 be cleared. We probably can still clear the breakpoint, but
10620 since the other PC value is never presented to user, user
10621 can only find it by guessing, and it does not seem important
10622 to support that. */
10623
4a64f543
MS
10624 /* For each line spec given, delete bps which correspond to it. Do
10625 it in two passes, solely to preserve the current behavior that
10626 from_tty is forced true if we delete more than one
10627 breakpoint. */
c906108c 10628
81b1e71c 10629 std::vector<struct breakpoint *> found;
6c5b2ebe 10630 for (const auto &sal : sals)
c906108c 10631 {
05cba821
JK
10632 const char *sal_fullname;
10633
c906108c 10634 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
10635 If line given (pc == 0), clear all bpts on specified line.
10636 If defaulting, clear all bpts on default line
10637 or at default pc.
c5aa993b 10638
dda83cd7 10639 defaulting sal.pc != 0 tests to do
c5aa993b 10640
dda83cd7
SM
10641 0 1 pc
10642 1 1 pc _and_ line
10643 0 0 line
10644 1 0 <can't happen> */
c906108c 10645
05cba821
JK
10646 sal_fullname = (sal.symtab == NULL
10647 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 10648
4a64f543 10649 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 10650 for (breakpoint *b : all_breakpoints ())
c5aa993b 10651 {
0d381245 10652 int match = 0;
4a64f543 10653 /* Are we going to delete b? */
a5c69b1e
EL
10654 if (b->type != bp_none && !is_watchpoint (b)
10655 && user_breakpoint_p (b))
0d381245 10656 {
40cb8ca5 10657 for (bp_location *loc : b->locations ())
0d381245 10658 {
f8eba3c6
TT
10659 /* If the user specified file:line, don't allow a PC
10660 match. This matches historical gdb behavior. */
10661 int pc_match = (!sal.explicit_line
10662 && sal.pc
10663 && (loc->pspace == sal.pspace)
10664 && (loc->address == sal.pc)
10665 && (!section_is_overlay (loc->section)
10666 || loc->section == sal.section));
4aac40c8
TT
10667 int line_match = 0;
10668
10669 if ((default_match || sal.explicit_line)
2f202fde 10670 && loc->symtab != NULL
05cba821 10671 && sal_fullname != NULL
4aac40c8 10672 && sal.pspace == loc->pspace
05cba821
JK
10673 && loc->line_number == sal.line
10674 && filename_cmp (symtab_to_fullname (loc->symtab),
10675 sal_fullname) == 0)
10676 line_match = 1;
4aac40c8 10677
0d381245
VP
10678 if (pc_match || line_match)
10679 {
10680 match = 1;
10681 break;
10682 }
10683 }
10684 }
10685
10686 if (match)
81b1e71c 10687 found.push_back (b);
c906108c 10688 }
80f8a6eb 10689 }
8a2c437b 10690
80f8a6eb 10691 /* Now go thru the 'found' chain and delete them. */
81b1e71c 10692 if (found.empty ())
80f8a6eb
MS
10693 {
10694 if (arg)
8a3fe4f8 10695 error (_("No breakpoint at %s."), arg);
80f8a6eb 10696 else
8a3fe4f8 10697 error (_("No breakpoint at this line."));
80f8a6eb 10698 }
c906108c 10699
8a2c437b 10700 /* Remove duplicates from the vec. */
81b1e71c 10701 std::sort (found.begin (), found.end (),
b926417a 10702 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10703 {
b926417a 10704 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
10705 });
10706 found.erase (std::unique (found.begin (), found.end (),
b926417a 10707 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 10708 {
b926417a 10709 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
10710 }),
10711 found.end ());
8a2c437b 10712
81b1e71c 10713 if (found.size () > 1)
4a64f543 10714 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10715 if (from_tty)
a3f17187 10716 {
81b1e71c 10717 if (found.size () == 1)
6cb06a8c 10718 gdb_printf (_("Deleted breakpoint "));
a3f17187 10719 else
6cb06a8c 10720 gdb_printf (_("Deleted breakpoints "));
a3f17187 10721 }
d6e956e5 10722
81b1e71c 10723 for (breakpoint *iter : found)
80f8a6eb 10724 {
c5aa993b 10725 if (from_tty)
6cb06a8c 10726 gdb_printf ("%d ", iter->number);
81b1e71c 10727 delete_breakpoint (iter);
c906108c 10728 }
80f8a6eb 10729 if (from_tty)
a11ac3b3 10730 gdb_putc ('\n');
c906108c
SS
10731}
10732\f
10733/* Delete breakpoint in BS if they are `delete' breakpoints and
10734 all breakpoints that are marked for deletion, whether hit or not.
10735 This is called after any breakpoint is hit, or after errors. */
10736
10737void
313f3b21 10738breakpoint_auto_delete (bpstat *bs)
c906108c 10739{
c906108c 10740 for (; bs; bs = bs->next)
f431efe5
PA
10741 if (bs->breakpoint_at
10742 && bs->breakpoint_at->disposition == disp_del
c906108c 10743 && bs->stop)
f431efe5 10744 delete_breakpoint (bs->breakpoint_at);
c906108c 10745
1428b37a 10746 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 10747 if (b->disposition == disp_del_at_next_stop)
c5aa993b 10748 delete_breakpoint (b);
c906108c
SS
10749}
10750
4a64f543 10751/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 10752 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
10753 bl_address_is_meaningful says), secondarily by ordering first
10754 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 10755 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
10756
10757static int
39ef2f62 10758bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 10759{
876fa593 10760 if (a->address != b->address)
39ef2f62 10761 return a->address < b->address;
876fa593 10762
dea2aa5f
LM
10763 /* Sort locations at the same address by their pspace number, keeping
10764 locations of the same inferior (in a multi-inferior environment)
10765 grouped. */
10766
10767 if (a->pspace->num != b->pspace->num)
39ef2f62 10768 return a->pspace->num < b->pspace->num;
dea2aa5f 10769
876fa593 10770 /* Sort permanent breakpoints first. */
1a853c52 10771 if (a->permanent != b->permanent)
39ef2f62 10772 return a->permanent > b->permanent;
876fa593 10773
7f32a4d5
PA
10774 /* Sort by type in order to make duplicate determination easier.
10775 See update_global_location_list. This is kept in sync with
10776 breakpoint_locations_match. */
10777 if (a->loc_type < b->loc_type)
10778 return true;
10779
10780 /* Likewise, for range-breakpoints, sort by length. */
10781 if (a->loc_type == bp_loc_hardware_breakpoint
10782 && b->loc_type == bp_loc_hardware_breakpoint
10783 && a->length < b->length)
10784 return true;
10785
c56a97f9
JK
10786 /* Make the internal GDB representation stable across GDB runs
10787 where A and B memory inside GDB can differ. Breakpoint locations of
10788 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
10789
10790 if (a->owner->number != b->owner->number)
39ef2f62 10791 return a->owner->number < b->owner->number;
876fa593 10792
39ef2f62 10793 return a < b;
876fa593
JK
10794}
10795
f5336ca5
PA
10796/* Set bp_locations_placed_address_before_address_max and
10797 bp_locations_shadow_len_after_address_max according to the current
10798 content of the bp_locations array. */
f7545552
TT
10799
10800static void
f5336ca5 10801bp_locations_target_extensions_update (void)
f7545552 10802{
f5336ca5
PA
10803 bp_locations_placed_address_before_address_max = 0;
10804 bp_locations_shadow_len_after_address_max = 0;
876fa593 10805
48d7020b 10806 for (bp_location *bl : all_bp_locations ())
876fa593
JK
10807 {
10808 CORE_ADDR start, end, addr;
10809
10810 if (!bp_location_has_shadow (bl))
10811 continue;
10812
10813 start = bl->target_info.placed_address;
10814 end = start + bl->target_info.shadow_len;
10815
10816 gdb_assert (bl->address >= start);
10817 addr = bl->address - start;
f5336ca5
PA
10818 if (addr > bp_locations_placed_address_before_address_max)
10819 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
10820
10821 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10822
10823 gdb_assert (bl->address < end);
10824 addr = end - bl->address;
f5336ca5
PA
10825 if (addr > bp_locations_shadow_len_after_address_max)
10826 bp_locations_shadow_len_after_address_max = addr;
876fa593 10827 }
f7545552
TT
10828}
10829
1e4d1764
YQ
10830/* Download tracepoint locations if they haven't been. */
10831
10832static void
10833download_tracepoint_locations (void)
10834{
dd2e65cc 10835 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 10836
5ed8105e 10837 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 10838
f6d17b2b 10839 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
10840 {
10841 struct tracepoint *t;
f2a8bc8a 10842 int bp_location_downloaded = 0;
1e4d1764 10843
7ed2c994 10844 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
10845 ? !may_insert_fast_tracepoints
10846 : !may_insert_tracepoints))
10847 continue;
10848
dd2e65cc
YQ
10849 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10850 {
10851 if (target_can_download_tracepoint ())
10852 can_download_tracepoint = TRIBOOL_TRUE;
10853 else
10854 can_download_tracepoint = TRIBOOL_FALSE;
10855 }
10856
10857 if (can_download_tracepoint == TRIBOOL_FALSE)
10858 break;
10859
40cb8ca5 10860 for (bp_location *bl : b->locations ())
7ed2c994
YQ
10861 {
10862 /* In tracepoint, locations are _never_ duplicated, so
10863 should_be_inserted is equivalent to
10864 unduplicated_should_be_inserted. */
10865 if (!should_be_inserted (bl) || bl->inserted)
10866 continue;
1e4d1764 10867
7ed2c994 10868 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 10869
7ed2c994 10870 target_download_tracepoint (bl);
1e4d1764 10871
7ed2c994 10872 bl->inserted = 1;
f2a8bc8a 10873 bp_location_downloaded = 1;
7ed2c994
YQ
10874 }
10875 t = (struct tracepoint *) b;
10876 t->number_on_target = b->number;
f2a8bc8a 10877 if (bp_location_downloaded)
76727919 10878 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 10879 }
1e4d1764
YQ
10880}
10881
934709f0
PW
10882/* Swap the insertion/duplication state between two locations. */
10883
10884static void
10885swap_insertion (struct bp_location *left, struct bp_location *right)
10886{
10887 const int left_inserted = left->inserted;
10888 const int left_duplicate = left->duplicate;
b775012e 10889 const int left_needs_update = left->needs_update;
934709f0
PW
10890 const struct bp_target_info left_target_info = left->target_info;
10891
1e4d1764
YQ
10892 /* Locations of tracepoints can never be duplicated. */
10893 if (is_tracepoint (left->owner))
10894 gdb_assert (!left->duplicate);
10895 if (is_tracepoint (right->owner))
10896 gdb_assert (!right->duplicate);
10897
934709f0
PW
10898 left->inserted = right->inserted;
10899 left->duplicate = right->duplicate;
b775012e 10900 left->needs_update = right->needs_update;
934709f0
PW
10901 left->target_info = right->target_info;
10902 right->inserted = left_inserted;
10903 right->duplicate = left_duplicate;
b775012e 10904 right->needs_update = left_needs_update;
934709f0
PW
10905 right->target_info = left_target_info;
10906}
10907
b775012e
LM
10908/* Force the re-insertion of the locations at ADDRESS. This is called
10909 once a new/deleted/modified duplicate location is found and we are evaluating
10910 conditions on the target's side. Such conditions need to be updated on
10911 the target. */
10912
10913static void
10914force_breakpoint_reinsertion (struct bp_location *bl)
10915{
b775012e
LM
10916 CORE_ADDR address = 0;
10917 int pspace_num;
10918
10919 address = bl->address;
10920 pspace_num = bl->pspace->num;
10921
10922 /* This is only meaningful if the target is
10923 evaluating conditions and if the user has
10924 opted for condition evaluation on the target's
10925 side. */
10926 if (gdb_evaluates_breakpoint_condition_p ()
10927 || !target_supports_evaluation_of_breakpoint_conditions ())
10928 return;
10929
10930 /* Flag all breakpoint locations with this address and
10931 the same program space as the location
10932 as "its condition has changed". We need to
10933 update the conditions on the target's side. */
e0d9a270 10934 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 10935 {
b775012e
LM
10936 if (!is_breakpoint (loc->owner)
10937 || pspace_num != loc->pspace->num)
10938 continue;
10939
10940 /* Flag the location appropriately. We use a different state to
10941 let everyone know that we already updated the set of locations
10942 with addr bl->address and program space bl->pspace. This is so
10943 we don't have to keep calling these functions just to mark locations
10944 that have already been marked. */
10945 loc->condition_changed = condition_updated;
10946
10947 /* Free the agent expression bytecode as well. We will compute
10948 it later on. */
833177a4 10949 loc->cond_bytecode.reset ();
b775012e
LM
10950 }
10951}
7f32a4d5 10952
44702360
PA
10953/* Called whether new breakpoints are created, or existing breakpoints
10954 deleted, to update the global location list and recompute which
10955 locations are duplicate of which.
b775012e 10956
04086b45
PA
10957 The INSERT_MODE flag determines whether locations may not, may, or
10958 shall be inserted now. See 'enum ugll_insert_mode' for more
10959 info. */
b60e7edf 10960
0d381245 10961static void
44702360 10962update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 10963{
b775012e
LM
10964 /* Last breakpoint location address that was marked for update. */
10965 CORE_ADDR last_addr = 0;
10966 /* Last breakpoint location program space that was marked for update. */
10967 int last_pspace_num = -1;
f7545552 10968
2d134ed3
PA
10969 /* Used in the duplicates detection below. When iterating over all
10970 bp_locations, points to the first bp_location of a given address.
10971 Breakpoints and watchpoints of different types are never
10972 duplicates of each other. Keep one pointer for each type of
10973 breakpoint/watchpoint, so we only need to loop over all locations
10974 once. */
10975 struct bp_location *bp_loc_first; /* breakpoint */
10976 struct bp_location *wp_loc_first; /* hardware watchpoint */
10977 struct bp_location *awp_loc_first; /* access watchpoint */
10978 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10979
f5336ca5
PA
10980 /* Saved former bp_locations array which we compare against the newly
10981 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
10982 std::vector<bp_location *> old_locations = std::move (bp_locations);
10983 bp_locations.clear ();
876fa593 10984
43892fdf 10985 for (breakpoint *b : all_breakpoints ())
40cb8ca5 10986 for (bp_location *loc : b->locations ())
5d51cd5d 10987 bp_locations.push_back (loc);
7f32a4d5
PA
10988
10989 /* See if we need to "upgrade" a software breakpoint to a hardware
10990 breakpoint. Do this before deciding whether locations are
10991 duplicates. Also do this before sorting because sorting order
10992 depends on location type. */
5d51cd5d
SM
10993 for (bp_location *loc : bp_locations)
10994 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 10995 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 10996
5d51cd5d 10997 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 10998 bp_location_is_less_than);
876fa593 10999
f5336ca5 11000 bp_locations_target_extensions_update ();
74960c60 11001
4a64f543
MS
11002 /* Identify bp_location instances that are no longer present in the
11003 new list, and therefore should be freed. Note that it's not
11004 necessary that those locations should be removed from inferior --
11005 if there's another location at the same address (previously
11006 marked as duplicate), we don't need to remove/insert the
11007 location.
876fa593 11008
4a64f543
MS
11009 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11010 and former bp_location array state respectively. */
876fa593 11011
5d51cd5d
SM
11012 size_t loc_i = 0;
11013 for (bp_location *old_loc : old_locations)
74960c60 11014 {
e5dd4106 11015 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11016 not, we have to free it. */
c7d46a38 11017 int found_object = 0;
20874c92
VP
11018 /* Tells if the location should remain inserted in the target. */
11019 int keep_in_target = 0;
11020 int removed = 0;
876fa593 11021
4a64f543
MS
11022 /* Skip LOCP entries which will definitely never be needed.
11023 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11024 while (loc_i < bp_locations.size ()
11025 && bp_locations[loc_i]->address < old_loc->address)
11026 loc_i++;
c7d46a38 11027
5d51cd5d
SM
11028 for (size_t loc2_i = loc_i;
11029 (loc2_i < bp_locations.size ()
11030 && bp_locations[loc2_i]->address == old_loc->address);
11031 loc2_i++)
c7d46a38 11032 {
b775012e
LM
11033 /* Check if this is a new/duplicated location or a duplicated
11034 location that had its condition modified. If so, we want to send
11035 its condition to the target if evaluation of conditions is taking
11036 place there. */
5d51cd5d 11037 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11038 && (last_addr != old_loc->address
11039 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11040 {
5d51cd5d 11041 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11042 last_pspace_num = old_loc->pspace->num;
c7d46a38 11043 }
b775012e 11044
5d51cd5d 11045 if (bp_locations[loc2_i] == old_loc)
b775012e 11046 found_object = 1;
c7d46a38 11047 }
74960c60 11048
b775012e
LM
11049 /* We have already handled this address, update it so that we don't
11050 have to go through updates again. */
11051 last_addr = old_loc->address;
11052
11053 /* Target-side condition evaluation: Handle deleted locations. */
11054 if (!found_object)
11055 force_breakpoint_reinsertion (old_loc);
11056
4a64f543
MS
11057 /* If this location is no longer present, and inserted, look if
11058 there's maybe a new location at the same address. If so,
11059 mark that one inserted, and don't remove this one. This is
11060 needed so that we don't have a time window where a breakpoint
11061 at certain location is not inserted. */
74960c60 11062
876fa593 11063 if (old_loc->inserted)
0d381245 11064 {
4a64f543
MS
11065 /* If the location is inserted now, we might have to remove
11066 it. */
74960c60 11067
876fa593 11068 if (found_object && should_be_inserted (old_loc))
74960c60 11069 {
4a64f543
MS
11070 /* The location is still present in the location list,
11071 and still should be inserted. Don't do anything. */
20874c92 11072 keep_in_target = 1;
74960c60
VP
11073 }
11074 else
11075 {
b775012e
LM
11076 /* This location still exists, but it won't be kept in the
11077 target since it may have been disabled. We proceed to
11078 remove its target-side condition. */
11079
4a64f543
MS
11080 /* The location is either no longer present, or got
11081 disabled. See if there's another location at the
11082 same address, in which case we don't need to remove
11083 this one from the target. */
876fa593 11084
2bdf28a0 11085 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11086 if (bl_address_is_meaningful (old_loc))
876fa593 11087 {
5d51cd5d
SM
11088 for (size_t loc2_i = loc_i;
11089 (loc2_i < bp_locations.size ()
11090 && bp_locations[loc2_i]->address == old_loc->address);
11091 loc2_i++)
876fa593 11092 {
5d51cd5d 11093 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11094
7f32a4d5
PA
11095 if (loc2 == old_loc)
11096 continue;
11097
2d134ed3 11098 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11099 {
85d721b8
PA
11100 /* Read watchpoint locations are switched to
11101 access watchpoints, if the former are not
11102 supported, but the latter are. */
11103 if (is_hardware_watchpoint (old_loc->owner))
11104 {
11105 gdb_assert (is_hardware_watchpoint (loc2->owner));
11106 loc2->watchpoint_type = old_loc->watchpoint_type;
11107 }
11108
934709f0
PW
11109 /* loc2 is a duplicated location. We need to check
11110 if it should be inserted in case it will be
11111 unduplicated. */
7f32a4d5 11112 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11113 {
934709f0 11114 swap_insertion (old_loc, loc2);
c7d46a38
PA
11115 keep_in_target = 1;
11116 break;
11117 }
876fa593
JK
11118 }
11119 }
11120 }
74960c60
VP
11121 }
11122
20874c92
VP
11123 if (!keep_in_target)
11124 {
834c0d03 11125 if (remove_breakpoint (old_loc))
20874c92 11126 {
4a64f543
MS
11127 /* This is just about all we can do. We could keep
11128 this location on the global list, and try to
11129 remove it next time, but there's no particular
11130 reason why we will succeed next time.
20874c92 11131
4a64f543
MS
11132 Note that at this point, old_loc->owner is still
11133 valid, as delete_breakpoint frees the breakpoint
11134 only after calling us. */
6cb06a8c
TT
11135 gdb_printf (_("warning: Error removing "
11136 "breakpoint %d\n"),
11137 old_loc->owner->number);
20874c92
VP
11138 }
11139 removed = 1;
11140 }
0d381245 11141 }
74960c60
VP
11142
11143 if (!found_object)
1c5cfe86 11144 {
fbea99ea 11145 if (removed && target_is_non_stop_p ()
1cf4d951 11146 && need_moribund_for_location_type (old_loc))
20874c92 11147 {
db82e815
PA
11148 /* This location was removed from the target. In
11149 non-stop mode, a race condition is possible where
11150 we've removed a breakpoint, but stop events for that
11151 breakpoint are already queued and will arrive later.
11152 We apply an heuristic to be able to distinguish such
11153 SIGTRAPs from other random SIGTRAPs: we keep this
11154 breakpoint location for a bit, and will retire it
11155 after we see some number of events. The theory here
11156 is that reporting of events should, "on the average",
11157 be fair, so after a while we'll see events from all
11158 threads that have anything of interest, and no longer
11159 need to keep this breakpoint location around. We
11160 don't hold locations forever so to reduce chances of
11161 mistaking a non-breakpoint SIGTRAP for a breakpoint
11162 SIGTRAP.
11163
11164 The heuristic failing can be disastrous on
11165 decr_pc_after_break targets.
11166
11167 On decr_pc_after_break targets, like e.g., x86-linux,
11168 if we fail to recognize a late breakpoint SIGTRAP,
11169 because events_till_retirement has reached 0 too
11170 soon, we'll fail to do the PC adjustment, and report
11171 a random SIGTRAP to the user. When the user resumes
11172 the inferior, it will most likely immediately crash
2dec564e 11173 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11174 corrupted, because of being resumed e.g., in the
11175 middle of a multi-byte instruction, or skipped a
11176 one-byte instruction. This was actually seen happen
11177 on native x86-linux, and should be less rare on
11178 targets that do not support new thread events, like
11179 remote, due to the heuristic depending on
11180 thread_count.
11181
11182 Mistaking a random SIGTRAP for a breakpoint trap
11183 causes similar symptoms (PC adjustment applied when
11184 it shouldn't), but then again, playing with SIGTRAPs
11185 behind the debugger's back is asking for trouble.
11186
11187 Since hardware watchpoint traps are always
11188 distinguishable from other traps, so we don't need to
11189 apply keep hardware watchpoint moribund locations
11190 around. We simply always ignore hardware watchpoint
11191 traps we can no longer explain. */
11192
5b6d1e4f
PA
11193 process_stratum_target *proc_target = nullptr;
11194 for (inferior *inf : all_inferiors ())
11195 if (inf->pspace == old_loc->pspace)
11196 {
11197 proc_target = inf->process_target ();
11198 break;
11199 }
11200 if (proc_target != nullptr)
11201 old_loc->events_till_retirement
11202 = 3 * (thread_count (proc_target) + 1);
11203 else
11204 old_loc->events_till_retirement = 1;
876fa593 11205 old_loc->owner = NULL;
20874c92 11206
1123588c 11207 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11208 }
11209 else
f431efe5
PA
11210 {
11211 old_loc->owner = NULL;
11212 decref_bp_location (&old_loc);
11213 }
20874c92 11214 }
74960c60 11215 }
1c5cfe86 11216
348d480f
PA
11217 /* Rescan breakpoints at the same address and section, marking the
11218 first one as "first" and any others as "duplicates". This is so
11219 that the bpt instruction is only inserted once. If we have a
11220 permanent breakpoint at the same place as BPT, make that one the
11221 official one, and the rest as duplicates. Permanent breakpoints
11222 are sorted first for the same address.
11223
11224 Do the same for hardware watchpoints, but also considering the
11225 watchpoint's type (regular/access/read) and length. */
11226
11227 bp_loc_first = NULL;
11228 wp_loc_first = NULL;
11229 awp_loc_first = NULL;
11230 rwp_loc_first = NULL;
40cb8ca5 11231
48d7020b 11232 for (bp_location *loc : all_bp_locations ())
348d480f
PA
11233 {
11234 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11235 non-NULL. */
348d480f 11236 struct bp_location **loc_first_p;
43892fdf 11237 breakpoint *b = loc->owner;
348d480f 11238
6f380991 11239 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11240 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11241 /* Don't detect duplicate for tracepoint locations because they are
11242 never duplicated. See the comments in field `duplicate' of
11243 `struct bp_location'. */
348d480f 11244 || is_tracepoint (b))
b775012e
LM
11245 {
11246 /* Clear the condition modification flag. */
11247 loc->condition_changed = condition_unchanged;
11248 continue;
11249 }
348d480f 11250
348d480f
PA
11251 if (b->type == bp_hardware_watchpoint)
11252 loc_first_p = &wp_loc_first;
11253 else if (b->type == bp_read_watchpoint)
11254 loc_first_p = &rwp_loc_first;
11255 else if (b->type == bp_access_watchpoint)
11256 loc_first_p = &awp_loc_first;
11257 else
11258 loc_first_p = &bp_loc_first;
11259
11260 if (*loc_first_p == NULL
11261 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11262 || !breakpoint_locations_match (loc, *loc_first_p))
11263 {
11264 *loc_first_p = loc;
11265 loc->duplicate = 0;
b775012e
LM
11266
11267 if (is_breakpoint (loc->owner) && loc->condition_changed)
11268 {
11269 loc->needs_update = 1;
11270 /* Clear the condition modification flag. */
11271 loc->condition_changed = condition_unchanged;
11272 }
348d480f
PA
11273 continue;
11274 }
11275
934709f0
PW
11276
11277 /* This and the above ensure the invariant that the first location
11278 is not duplicated, and is the inserted one.
11279 All following are marked as duplicated, and are not inserted. */
11280 if (loc->inserted)
11281 swap_insertion (loc, *loc_first_p);
348d480f
PA
11282 loc->duplicate = 1;
11283
b775012e
LM
11284 /* Clear the condition modification flag. */
11285 loc->condition_changed = condition_unchanged;
348d480f
PA
11286 }
11287
a25a5a45 11288 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11289 {
04086b45 11290 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11291 insert_breakpoint_locations ();
11292 else
11293 {
44702360
PA
11294 /* Even though the caller told us to not insert new
11295 locations, we may still need to update conditions on the
11296 target's side of breakpoints that were already inserted
11297 if the target is evaluating breakpoint conditions. We
b775012e
LM
11298 only update conditions for locations that are marked
11299 "needs_update". */
11300 update_inserted_breakpoint_locations ();
11301 }
11302 }
348d480f 11303
04086b45 11304 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 11305 download_tracepoint_locations ();
348d480f
PA
11306}
11307
11308void
11309breakpoint_retire_moribund (void)
11310{
1123588c
TT
11311 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11312 {
11313 struct bp_location *loc = moribund_locations[ix];
11314 if (--(loc->events_till_retirement) == 0)
11315 {
11316 decref_bp_location (&loc);
11317 unordered_remove (moribund_locations, ix);
11318 --ix;
11319 }
11320 }
348d480f
PA
11321}
11322
11323static void
44702360 11324update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 11325{
348d480f 11326
a70b8144 11327 try
492d29ea
PA
11328 {
11329 update_global_location_list (insert_mode);
11330 }
230d2906 11331 catch (const gdb_exception_error &e)
492d29ea
PA
11332 {
11333 }
348d480f
PA
11334}
11335
11336/* Clear BKP from a BPS. */
11337
11338static void
313f3b21 11339bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
348d480f 11340{
313f3b21 11341 bpstat *bs;
348d480f
PA
11342
11343 for (bs = bps; bs; bs = bs->next)
11344 if (bs->breakpoint_at == bpt)
11345 {
11346 bs->breakpoint_at = NULL;
11347 bs->old_val = NULL;
11348 /* bs->commands will be freed later. */
11349 }
11350}
11351
11352/* Callback for iterate_over_threads. */
11353static int
11354bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11355{
9a3c8263 11356 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
11357
11358 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11359 return 0;
11360}
11361
c47614fe 11362/* Helper for breakpoint and tracepoint breakpoint->mention
348d480f
PA
11363 callbacks. */
11364
11365static void
b713485d 11366say_where (const breakpoint *b)
348d480f
PA
11367{
11368 struct value_print_options opts;
11369
11370 get_user_print_options (&opts);
11371
11372 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11373 single string. */
11374 if (b->loc == NULL)
11375 {
f00aae0f
KS
11376 /* For pending locations, the output differs slightly based
11377 on b->extra_string. If this is non-NULL, it contains either
11378 a condition or dprintf arguments. */
11379 if (b->extra_string == NULL)
11380 {
709438c7 11381 gdb_printf (_(" (%s) pending."), b->locspec->to_string ());
f00aae0f
KS
11382 }
11383 else if (b->type == bp_dprintf)
11384 {
6cb06a8c 11385 gdb_printf (_(" (%s,%s) pending."),
709438c7 11386 b->locspec->to_string (),
6cb06a8c 11387 b->extra_string.get ());
f00aae0f
KS
11388 }
11389 else
11390 {
6cb06a8c 11391 gdb_printf (_(" (%s %s) pending."),
709438c7 11392 b->locspec->to_string (),
6cb06a8c 11393 b->extra_string.get ());
f00aae0f 11394 }
348d480f
PA
11395 }
11396 else
11397 {
2f202fde 11398 if (opts.addressprint || b->loc->symtab == NULL)
6cb06a8c
TT
11399 gdb_printf (" at %ps",
11400 styled_string (address_style.style (),
11401 paddress (b->loc->gdbarch,
11402 b->loc->address)));
2f202fde 11403 if (b->loc->symtab != NULL)
f8eba3c6
TT
11404 {
11405 /* If there is a single location, we can print the location
11406 more nicely. */
11407 if (b->loc->next == NULL)
0bb296cb 11408 {
6a831f06
PA
11409 const char *filename
11410 = symtab_to_filename_for_display (b->loc->symtab);
6cb06a8c
TT
11411 gdb_printf (": file %ps, line %d.",
11412 styled_string (file_name_style.style (),
11413 filename),
11414 b->loc->line_number);
0bb296cb 11415 }
f8eba3c6
TT
11416 else
11417 /* This is not ideal, but each location may have a
11418 different file name, and this at least reflects the
11419 real situation somewhat. */
709438c7 11420 gdb_printf (": %s.", b->locspec->to_string ());
f8eba3c6 11421 }
348d480f
PA
11422
11423 if (b->loc->next)
11424 {
11425 struct bp_location *loc = b->loc;
11426 int n = 0;
11427 for (; loc; loc = loc->next)
11428 ++n;
6cb06a8c 11429 gdb_printf (" (%d locations)", n);
348d480f
PA
11430 }
11431 }
11432}
11433
40cb8ca5
SM
11434/* See breakpoint.h. */
11435
a67bcaba 11436bp_location_range breakpoint::locations () const
40cb8ca5 11437{
9be25986 11438 return bp_location_range (this->loc);
40cb8ca5
SM
11439}
11440
4c6a92b1
TT
11441struct bp_location *
11442breakpoint::allocate_location ()
11443{
11444 return new bp_location (this);
11445}
11446
2060206e
PA
11447#define internal_error_pure_virtual_called() \
11448 gdb_assert_not_reached ("pure virtual function called")
11449
4c6a92b1
TT
11450int
11451breakpoint::insert_location (struct bp_location *bl)
11452{
11453 internal_error_pure_virtual_called ();
11454}
11455
4c6a92b1
TT
11456int
11457breakpoint::remove_location (struct bp_location *bl,
11458 enum remove_bp_reason reason)
11459{
11460 internal_error_pure_virtual_called ();
11461}
11462
4c6a92b1
TT
11463int
11464breakpoint::breakpoint_hit (const struct bp_location *bl,
11465 const address_space *aspace,
11466 CORE_ADDR bp_addr,
11467 const target_waitstatus &ws)
11468{
11469 internal_error_pure_virtual_called ();
11470}
11471
4c6a92b1
TT
11472int
11473breakpoint::resources_needed (const struct bp_location *bl)
11474{
11475 internal_error_pure_virtual_called ();
11476}
11477
4c6a92b1 11478enum print_stop_action
7bd86313 11479breakpoint::print_it (const bpstat *bs) const
4c6a92b1
TT
11480{
11481 internal_error_pure_virtual_called ();
11482}
11483
4c6a92b1 11484void
b713485d 11485breakpoint::print_mention () const
4c6a92b1
TT
11486{
11487 internal_error_pure_virtual_called ();
11488}
11489
4c6a92b1 11490void
4d1ae558 11491breakpoint::print_recreate (struct ui_file *fp) const
4c6a92b1
TT
11492{
11493 internal_error_pure_virtual_called ();
11494}
11495
2060206e
PA
11496/* Default breakpoint_ops methods. */
11497
2b5ab5b8 11498void
74421c0b 11499code_breakpoint::re_set ()
348d480f 11500{
06edf0c0 11501 /* FIXME: is this still reachable? */
264f9890 11502 if (breakpoint_location_spec_empty_p (this))
06edf0c0 11503 {
f00aae0f 11504 /* Anything without a location can't be re-set. */
2b5ab5b8 11505 delete_breakpoint (this);
06edf0c0 11506 return;
348d480f 11507 }
06edf0c0 11508
b3d5660a 11509 re_set_default ();
348d480f
PA
11510}
11511
2b5ab5b8 11512int
74421c0b 11513code_breakpoint::insert_location (struct bp_location *bl)
348d480f 11514{
cd6c3b4f
YQ
11515 CORE_ADDR addr = bl->target_info.reqstd_address;
11516
579c6ad9 11517 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
11518 bl->target_info.placed_address = addr;
11519
5d926615 11520 int result;
348d480f 11521 if (bl->loc_type == bp_loc_hardware_breakpoint)
5d926615 11522 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 11523 else
5d926615
TT
11524 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11525
11526 if (result == 0 && bl->probe.prob != nullptr)
11527 {
11528 /* The insertion was successful, now let's set the probe's semaphore
11529 if needed. */
11530 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11531 }
11532
11533 return result;
348d480f
PA
11534}
11535
2b5ab5b8 11536int
74421c0b 11537code_breakpoint::remove_location (struct bp_location *bl,
2b5ab5b8 11538 enum remove_bp_reason reason)
348d480f 11539{
5d926615
TT
11540 if (bl->probe.prob != nullptr)
11541 {
11542 /* Let's clear the semaphore before removing the location. */
11543 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11544 }
11545
348d480f
PA
11546 if (bl->loc_type == bp_loc_hardware_breakpoint)
11547 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11548 else
73971819 11549 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
11550}
11551
2b5ab5b8 11552int
74421c0b 11553code_breakpoint::breakpoint_hit (const struct bp_location *bl,
2b5ab5b8
TT
11554 const address_space *aspace,
11555 CORE_ADDR bp_addr,
11556 const target_waitstatus &ws)
348d480f 11557{
c272a98c
SM
11558 if (ws.kind () != TARGET_WAITKIND_STOPPED
11559 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
11560 return 0;
11561
348d480f
PA
11562 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11563 aspace, bp_addr))
11564 return 0;
11565
11566 if (overlay_debugging /* unmapped overlay section */
11567 && section_is_overlay (bl->section)
11568 && !section_is_mapped (bl->section))
11569 return 0;
11570
11571 return 1;
11572}
11573
f293a0b5
TT
11574int
11575dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11576 const address_space *aspace,
11577 CORE_ADDR bp_addr,
11578 const target_waitstatus &ws)
cd1608cc
PA
11579{
11580 if (dprintf_style == dprintf_style_agent
11581 && target_can_run_breakpoint_commands ())
11582 {
11583 /* An agent-style dprintf never causes a stop. If we see a trap
11584 for this address it must be for a breakpoint that happens to
11585 be set at the same address. */
11586 return 0;
11587 }
11588
f293a0b5 11589 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
cd1608cc
PA
11590}
11591
7dd8e7ae
TT
11592int
11593ordinary_breakpoint::resources_needed (const struct bp_location *bl)
348d480f 11594{
7dd8e7ae 11595 gdb_assert (type == bp_hardware_breakpoint);
348d480f
PA
11596
11597 return 1;
11598}
11599
7dd8e7ae 11600enum print_stop_action
7bd86313 11601ordinary_breakpoint::print_it (const bpstat *bs) const
348d480f 11602{
348d480f 11603 const struct bp_location *bl;
001c8c33 11604 int bp_temp;
79a45e25 11605 struct ui_out *uiout = current_uiout;
348d480f 11606
b6433ede 11607 bl = bs->bp_location_at.get ();
348d480f 11608
7dd8e7ae 11609 bp_temp = disposition == disp_del;
001c8c33
PA
11610 if (bl->address != bl->requested_address)
11611 breakpoint_adjustment_warning (bl->requested_address,
11612 bl->address,
7dd8e7ae
TT
11613 number, 1);
11614 annotate_breakpoint (number);
f303dbd6
PA
11615 maybe_print_thread_hit_breakpoint (uiout);
11616
112e8700 11617 if (uiout->is_mi_like_p ())
348d480f 11618 {
112e8700 11619 uiout->field_string ("reason",
001c8c33 11620 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7dd8e7ae 11621 uiout->field_string ("disp", bpdisp_text (disposition));
06edf0c0 11622 }
6a831f06
PA
11623 if (bp_temp)
11624 uiout->message ("Temporary breakpoint %pF, ",
7dd8e7ae 11625 signed_field ("bkptno", number));
6a831f06
PA
11626 else
11627 uiout->message ("Breakpoint %pF, ",
7dd8e7ae 11628 signed_field ("bkptno", number));
06edf0c0 11629
001c8c33 11630 return PRINT_SRC_AND_LOC;
06edf0c0
PA
11631}
11632
7dd8e7ae 11633void
b713485d 11634ordinary_breakpoint::print_mention () const
06edf0c0 11635{
112e8700 11636 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
11637 return;
11638
7dd8e7ae 11639 switch (type)
06edf0c0
PA
11640 {
11641 case bp_breakpoint:
11642 case bp_gnu_ifunc_resolver:
7dd8e7ae 11643 if (disposition == disp_del)
6cb06a8c 11644 gdb_printf (_("Temporary breakpoint"));
06edf0c0 11645 else
6cb06a8c 11646 gdb_printf (_("Breakpoint"));
7dd8e7ae
TT
11647 gdb_printf (_(" %d"), number);
11648 if (type == bp_gnu_ifunc_resolver)
6cb06a8c 11649 gdb_printf (_(" at gnu-indirect-function resolver"));
06edf0c0
PA
11650 break;
11651 case bp_hardware_breakpoint:
7dd8e7ae 11652 gdb_printf (_("Hardware assisted breakpoint %d"), number);
06edf0c0 11653 break;
e7e0cddf 11654 case bp_dprintf:
7dd8e7ae 11655 gdb_printf (_("Dprintf %d"), number);
e7e0cddf 11656 break;
06edf0c0
PA
11657 }
11658
7dd8e7ae 11659 say_where (this);
06edf0c0
PA
11660}
11661
7dd8e7ae 11662void
4d1ae558 11663ordinary_breakpoint::print_recreate (struct ui_file *fp) const
06edf0c0 11664{
7dd8e7ae 11665 if (type == bp_breakpoint && disposition == disp_del)
6cb06a8c 11666 gdb_printf (fp, "tbreak");
7dd8e7ae 11667 else if (type == bp_breakpoint)
6cb06a8c 11668 gdb_printf (fp, "break");
7dd8e7ae
TT
11669 else if (type == bp_hardware_breakpoint
11670 && disposition == disp_del)
6cb06a8c 11671 gdb_printf (fp, "thbreak");
7dd8e7ae 11672 else if (type == bp_hardware_breakpoint)
6cb06a8c 11673 gdb_printf (fp, "hbreak");
06edf0c0
PA
11674 else
11675 internal_error (__FILE__, __LINE__,
7dd8e7ae 11676 _("unhandled breakpoint type %d"), (int) type);
06edf0c0 11677
709438c7 11678 gdb_printf (fp, " %s", locspec->to_string ());
f00aae0f
KS
11679
11680 /* Print out extra_string if this breakpoint is pending. It might
11681 contain, for example, conditions that were set by the user. */
7dd8e7ae
TT
11682 if (loc == NULL && extra_string != NULL)
11683 gdb_printf (fp, " %s", extra_string.get ());
f00aae0f 11684
04d0163c 11685 print_recreate_thread (fp);
06edf0c0
PA
11686}
11687
2b5ab5b8 11688std::vector<symtab_and_line>
264f9890
PA
11689code_breakpoint::decode_location_spec (location_spec *locspec,
11690 program_space *search_pspace)
983af33b 11691{
7464aeaa 11692 if (locspec->type () == PROBE_LOCATION_SPEC)
264f9890 11693 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
5d926615 11694
264f9890 11695 return decode_location_spec_default (this, locspec, search_pspace);
983af33b
SDJ
11696}
11697
06edf0c0
PA
11698/* Virtual table for internal breakpoints. */
11699
c359fff5
TT
11700void
11701internal_breakpoint::re_set ()
06edf0c0 11702{
c359fff5 11703 switch (type)
06edf0c0
PA
11704 {
11705 /* Delete overlay event and longjmp master breakpoints; they
11706 will be reset later by breakpoint_re_set. */
11707 case bp_overlay_event:
11708 case bp_longjmp_master:
11709 case bp_std_terminate_master:
11710 case bp_exception_master:
c359fff5 11711 delete_breakpoint (this);
06edf0c0
PA
11712 break;
11713
11714 /* This breakpoint is special, it's set up when the inferior
dda83cd7 11715 starts and we really don't want to touch it. */
06edf0c0
PA
11716 case bp_shlib_event:
11717
11718 /* Like bp_shlib_event, this breakpoint type is special. Once
11719 it is set up, we do not want to touch it. */
11720 case bp_thread_event:
11721 break;
11722 }
11723}
11724
c359fff5
TT
11725void
11726internal_breakpoint::check_status (bpstat *bs)
06edf0c0 11727{
c359fff5 11728 if (type == bp_shlib_event)
a9b3a50f
PA
11729 {
11730 /* If requested, stop when the dynamic linker notifies GDB of
11731 events. This allows the user to get control and place
11732 breakpoints in initializer routines for dynamically loaded
11733 objects (among other things). */
11734 bs->stop = stop_on_solib_events;
11735 bs->print = stop_on_solib_events;
11736 }
11737 else
11738 bs->stop = 0;
06edf0c0
PA
11739}
11740
c359fff5 11741enum print_stop_action
7bd86313 11742internal_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11743{
c359fff5 11744 switch (type)
06edf0c0 11745 {
348d480f
PA
11746 case bp_shlib_event:
11747 /* Did we stop because the user set the stop_on_solib_events
11748 variable? (If so, we report this as a generic, "Stopped due
11749 to shlib event" message.) */
4e9e993a 11750 print_solib_event (false);
348d480f
PA
11751 break;
11752
11753 case bp_thread_event:
11754 /* Not sure how we will get here.
11755 GDB should not stop for these breakpoints. */
6cb06a8c 11756 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11757 break;
11758
11759 case bp_overlay_event:
11760 /* By analogy with the thread event, GDB should not stop for these. */
6cb06a8c 11761 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11762 break;
11763
11764 case bp_longjmp_master:
11765 /* These should never be enabled. */
6cb06a8c 11766 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11767 break;
11768
11769 case bp_std_terminate_master:
11770 /* These should never be enabled. */
6cb06a8c
TT
11771 gdb_printf (_("std::terminate Master Breakpoint: "
11772 "gdb should not stop!\n"));
348d480f
PA
11773 break;
11774
11775 case bp_exception_master:
11776 /* These should never be enabled. */
6cb06a8c
TT
11777 gdb_printf (_("Exception Master Breakpoint: "
11778 "gdb should not stop!\n"));
06edf0c0
PA
11779 break;
11780 }
11781
001c8c33 11782 return PRINT_NOTHING;
06edf0c0
PA
11783}
11784
c359fff5 11785void
b713485d 11786internal_breakpoint::print_mention () const
06edf0c0
PA
11787{
11788 /* Nothing to mention. These breakpoints are internal. */
11789}
11790
06edf0c0
PA
11791/* Virtual table for momentary breakpoints */
11792
1fd30a47
TT
11793void
11794momentary_breakpoint::re_set ()
06edf0c0
PA
11795{
11796 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 11797 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
11798 Otherwise these should have been blown away via the cleanup chain
11799 or by breakpoint_init_inferior when we rerun the executable. */
11800}
11801
1fd30a47
TT
11802void
11803momentary_breakpoint::check_status (bpstat *bs)
06edf0c0
PA
11804{
11805 /* Nothing. The point of these breakpoints is causing a stop. */
11806}
11807
1fd30a47 11808enum print_stop_action
7bd86313 11809momentary_breakpoint::print_it (const bpstat *bs) const
06edf0c0 11810{
001c8c33 11811 return PRINT_UNKNOWN;
348d480f
PA
11812}
11813
1fd30a47 11814void
b713485d 11815momentary_breakpoint::print_mention () const
348d480f 11816{
06edf0c0 11817 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11818}
11819
e2e4d78b
JK
11820/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11821
11822 It gets cleared already on the removal of the first one of such placed
11823 breakpoints. This is OK as they get all removed altogether. */
11824
c1fc2657 11825longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 11826{
c1fc2657 11827 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 11828
c1fc2657 11829 if (tp != NULL)
e2e4d78b 11830 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
11831}
11832
55aa24fb 11833static void
264f9890
PA
11834bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
11835 struct linespec_result *canonical)
03ada39e 11836
55aa24fb
SDJ
11837{
11838 struct linespec_sals lsal;
11839
264f9890 11840 lsal.sals = parse_probes (locspec, NULL, canonical);
709438c7 11841 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 11842 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
11843}
11844
6c5b2ebe 11845static std::vector<symtab_and_line>
264f9890
PA
11846bkpt_probe_decode_location_spec (struct breakpoint *b,
11847 location_spec *locspec,
11848 program_space *search_pspace)
55aa24fb 11849{
264f9890
PA
11850 std::vector<symtab_and_line> sals
11851 = parse_probes (locspec, search_pspace, NULL);
6c5b2ebe 11852 if (sals.empty ())
55aa24fb 11853 error (_("probe not found"));
6c5b2ebe 11854 return sals;
55aa24fb
SDJ
11855}
11856
779dcceb
TT
11857int
11858tracepoint::breakpoint_hit (const struct bp_location *bl,
11859 const address_space *aspace, CORE_ADDR bp_addr,
11860 const target_waitstatus &ws)
348d480f
PA
11861{
11862 /* By definition, the inferior does not report stops at
11863 tracepoints. */
11864 return 0;
74960c60
VP
11865}
11866
779dcceb
TT
11867void
11868tracepoint::print_one_detail (struct ui_out *uiout) const
74960c60 11869{
779dcceb 11870 if (!static_trace_marker_id.empty ())
348d480f 11871 {
7b572efb
TT
11872 gdb_assert (type == bp_static_tracepoint
11873 || type == bp_static_marker_tracepoint);
cc59ec59 11874
6a831f06
PA
11875 uiout->message ("\tmarker id is %pF\n",
11876 string_field ("static-tracepoint-marker-string-id",
779dcceb 11877 static_trace_marker_id.c_str ()));
348d480f 11878 }
0d381245
VP
11879}
11880
779dcceb 11881void
b713485d 11882tracepoint::print_mention () const
a474d7c2 11883{
112e8700 11884 if (current_uiout->is_mi_like_p ())
348d480f 11885 return;
cc59ec59 11886
779dcceb 11887 switch (type)
348d480f
PA
11888 {
11889 case bp_tracepoint:
6cb06a8c 11890 gdb_printf (_("Tracepoint"));
779dcceb 11891 gdb_printf (_(" %d"), number);
348d480f
PA
11892 break;
11893 case bp_fast_tracepoint:
6cb06a8c 11894 gdb_printf (_("Fast tracepoint"));
779dcceb 11895 gdb_printf (_(" %d"), number);
348d480f
PA
11896 break;
11897 case bp_static_tracepoint:
7b572efb 11898 case bp_static_marker_tracepoint:
6cb06a8c 11899 gdb_printf (_("Static tracepoint"));
779dcceb 11900 gdb_printf (_(" %d"), number);
348d480f
PA
11901 break;
11902 default:
11903 internal_error (__FILE__, __LINE__,
779dcceb 11904 _("unhandled tracepoint type %d"), (int) type);
348d480f
PA
11905 }
11906
779dcceb 11907 say_where (this);
a474d7c2
PA
11908}
11909
779dcceb 11910void
4d1ae558 11911tracepoint::print_recreate (struct ui_file *fp) const
a474d7c2 11912{
779dcceb 11913 if (type == bp_fast_tracepoint)
6cb06a8c 11914 gdb_printf (fp, "ftrace");
7b572efb
TT
11915 else if (type == bp_static_tracepoint
11916 || type == bp_static_marker_tracepoint)
6cb06a8c 11917 gdb_printf (fp, "strace");
779dcceb 11918 else if (type == bp_tracepoint)
6cb06a8c 11919 gdb_printf (fp, "trace");
348d480f
PA
11920 else
11921 internal_error (__FILE__, __LINE__,
779dcceb 11922 _("unhandled tracepoint type %d"), (int) type);
cc59ec59 11923
709438c7 11924 gdb_printf (fp, " %s", locspec->to_string ());
04d0163c 11925 print_recreate_thread (fp);
d9b3f62e 11926
779dcceb
TT
11927 if (pass_count)
11928 gdb_printf (fp, " passcount %d\n", pass_count);
a474d7c2
PA
11929}
11930
bac7c5cf 11931/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
11932
11933static void
264f9890
PA
11934tracepoint_probe_create_sals_from_location_spec
11935 (location_spec *locspec,
03ada39e 11936 struct linespec_result *canonical)
55aa24fb
SDJ
11937{
11938 /* We use the same method for breakpoint on probes. */
264f9890 11939 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
55aa24fb
SDJ
11940}
11941
f293a0b5
TT
11942void
11943dprintf_breakpoint::re_set ()
5c2b4418 11944{
b3d5660a 11945 re_set_default ();
5c2b4418 11946
f00aae0f 11947 /* extra_string should never be non-NULL for dprintf. */
f293a0b5 11948 gdb_assert (extra_string != NULL);
5c2b4418
HZ
11949
11950 /* 1 - connect to target 1, that can run breakpoint commands.
11951 2 - create a dprintf, which resolves fine.
11952 3 - disconnect from target 1
11953 4 - connect to target 2, that can NOT run breakpoint commands.
11954
11955 After steps #3/#4, you'll want the dprintf command list to
11956 be updated, because target 1 and 2 may well return different
11957 answers for target_can_run_breakpoint_commands().
11958 Given absence of finer grained resetting, we get to do
11959 it all the time. */
f293a0b5
TT
11960 if (extra_string != NULL)
11961 update_dprintf_command_list (this);
5c2b4418
HZ
11962}
11963
f293a0b5 11964/* Implement the "print_recreate" method for dprintf. */
2d9442cc 11965
f293a0b5 11966void
4d1ae558 11967dprintf_breakpoint::print_recreate (struct ui_file *fp) const
2d9442cc 11968{
709438c7 11969 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
04d0163c 11970 print_recreate_thread (fp);
2d9442cc
HZ
11971}
11972
f293a0b5 11973/* Implement the "after_condition_true" method for dprintf.
9d6e6e84
HZ
11974
11975 dprintf's are implemented with regular commands in their command
11976 list, but we run the commands here instead of before presenting the
11977 stop to the user, as dprintf's don't actually cause a stop. This
11978 also makes it so that the commands of multiple dprintfs at the same
11979 address are all handled. */
11980
f293a0b5
TT
11981void
11982dprintf_breakpoint::after_condition_true (struct bpstat *bs)
9d6e6e84 11983{
9d6e6e84
HZ
11984 /* dprintf's never cause a stop. This wasn't set in the
11985 check_status hook instead because that would make the dprintf's
11986 condition not be evaluated. */
11987 bs->stop = 0;
11988
11989 /* Run the command list here. Take ownership of it instead of
11990 copying. We never want these commands to run later in
11991 bpstat_do_actions, if a breakpoint that causes a stop happens to
11992 be set at same address as this dprintf, or even if running the
11993 commands here throws. */
9c95aea1
KB
11994 counted_command_line cmds = std::move (bs->commands);
11995 gdb_assert (cmds != nullptr);
11996 execute_control_commands (cmds.get (), 0);
9d6e6e84
HZ
11997}
11998
983af33b
SDJ
11999/* The breakpoint_ops structure to be used on static tracepoints with
12000 markers (`-m'). */
12001
12002static void
264f9890
PA
12003strace_marker_create_sals_from_location_spec (location_spec *locspec,
12004 struct linespec_result *canonical)
983af33b
SDJ
12005{
12006 struct linespec_sals lsal;
f00aae0f 12007 const char *arg_start, *arg;
983af33b 12008
40d97ee2 12009 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
f00aae0f 12010 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12011
f2fc3015
TT
12012 std::string str (arg_start, arg - arg_start);
12013 const char *ptr = str.c_str ();
264f9890
PA
12014 canonical->locspec
12015 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
983af33b 12016
709438c7 12017 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12018 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12019}
12020
12021static void
12022strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12023 struct linespec_result *canonical,
e1e01040
PA
12024 gdb::unique_xmalloc_ptr<char> cond_string,
12025 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12026 enum bptype type_wanted,
12027 enum bpdisp disposition,
12028 int thread,
12029 int task, int ignore_count,
983af33b 12030 int from_tty, int enabled,
44f238bb 12031 int internal, unsigned flags)
983af33b 12032{
6c5b2ebe 12033 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12034
12035 /* If the user is creating a static tracepoint by marker id
12036 (strace -m MARKER_ID), then store the sals index, so that
12037 breakpoint_re_set can try to match up which of the newly
12038 found markers corresponds to this one, and, don't try to
12039 expand multiple locations for each sal, given than SALS
12040 already should contain all sals for MARKER_ID. */
12041
6c5b2ebe 12042 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12043 {
5c1ddcb6 12044 location_spec_up locspec = canonical->locspec->clone ();
983af33b 12045
3b003a61
PA
12046 std::unique_ptr<tracepoint> tp
12047 (new tracepoint (gdbarch,
12048 type_wanted,
12049 lsal.sals[i],
264f9890 12050 std::move (locspec),
3b003a61
PA
12051 NULL,
12052 std::move (cond_string),
12053 std::move (extra_string),
12054 disposition,
12055 thread, task, ignore_count,
12056 from_tty, enabled, flags,
12057 canonical->special_display));
12058
983af33b
SDJ
12059 /* Given that its possible to have multiple markers with
12060 the same string id, if the user is creating a static
12061 tracepoint by marker id ("strace -m MARKER_ID"), then
12062 store the sals index, so that breakpoint_re_set can
12063 try to match up which of the newly found markers
12064 corresponds to this one */
12065 tp->static_trace_marker_id_idx = i;
12066
b270e6f9 12067 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12068 }
12069}
12070
6d7a8c56 12071std::vector<symtab_and_line>
264f9890
PA
12072static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12073 program_space *search_pspace)
983af33b 12074{
40d97ee2 12075 const char *s = as_linespec_location_spec (locspec)->spec_string;
983af33b 12076
6c5b2ebe 12077 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
6d7a8c56 12078 if (sals.size () > static_trace_marker_id_idx)
983af33b 12079 {
6d7a8c56 12080 sals[0] = sals[static_trace_marker_id_idx];
6c5b2ebe
PA
12081 sals.resize (1);
12082 return sals;
983af33b
SDJ
12083 }
12084 else
6d7a8c56 12085 error (_("marker %s not found"), static_trace_marker_id.c_str ());
983af33b
SDJ
12086}
12087
8613a476
TT
12088/* Static tracepoints with marker (`-m'). */
12089static struct breakpoint_ops strace_marker_breakpoint_ops =
12090{
264f9890 12091 strace_marker_create_sals_from_location_spec,
8613a476
TT
12092 strace_marker_create_breakpoints_sal,
12093};
983af33b
SDJ
12094
12095static int
12096strace_marker_p (struct breakpoint *b)
12097{
7b572efb 12098 return b->type == bp_static_marker_tracepoint;
983af33b
SDJ
12099}
12100
53a5351d 12101/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 12102 structures. */
c906108c
SS
12103
12104void
fba45db2 12105delete_breakpoint (struct breakpoint *bpt)
c906108c 12106{
8a3fe4f8 12107 gdb_assert (bpt != NULL);
c906108c 12108
4a64f543
MS
12109 /* Has this bp already been deleted? This can happen because
12110 multiple lists can hold pointers to bp's. bpstat lists are
12111 especial culprits.
12112
12113 One example of this happening is a watchpoint's scope bp. When
12114 the scope bp triggers, we notice that the watchpoint is out of
12115 scope, and delete it. We also delete its scope bp. But the
12116 scope bp is marked "auto-deleting", and is already on a bpstat.
12117 That bpstat is then checked for auto-deleting bp's, which are
12118 deleted.
12119
12120 A real solution to this problem might involve reference counts in
12121 bp's, and/or giving them pointers back to their referencing
12122 bpstat's, and teaching delete_breakpoint to only free a bp's
12123 storage when no more references were extent. A cheaper bandaid
12124 was chosen. */
c906108c
SS
12125 if (bpt->type == bp_none)
12126 return;
12127
4a64f543
MS
12128 /* At least avoid this stale reference until the reference counting
12129 of breakpoints gets resolved. */
d0fb5eae 12130 if (bpt->related_breakpoint != bpt)
e5a0a904 12131 {
d0fb5eae 12132 struct breakpoint *related;
3a5c3e22 12133 struct watchpoint *w;
d0fb5eae
JK
12134
12135 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 12136 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 12137 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
12138 w = (struct watchpoint *) bpt;
12139 else
12140 w = NULL;
12141 if (w != NULL)
12142 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
12143
12144 /* Unlink bpt from the bpt->related_breakpoint ring. */
12145 for (related = bpt; related->related_breakpoint != bpt;
12146 related = related->related_breakpoint);
12147 related->related_breakpoint = bpt->related_breakpoint;
12148 bpt->related_breakpoint = bpt;
e5a0a904
JK
12149 }
12150
a9634178
TJB
12151 /* watch_command_1 creates a watchpoint but only sets its number if
12152 update_watchpoint succeeds in creating its bp_locations. If there's
12153 a problem in that process, we'll be asked to delete the half-created
12154 watchpoint. In that case, don't announce the deletion. */
12155 if (bpt->number)
76727919 12156 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 12157
c906108c
SS
12158 if (breakpoint_chain == bpt)
12159 breakpoint_chain = bpt->next;
12160
43892fdf 12161 for (breakpoint *b : all_breakpoints ())
c906108c 12162 if (b->next == bpt)
01add95b
SM
12163 {
12164 b->next = bpt->next;
12165 break;
12166 }
c906108c 12167
f431efe5
PA
12168 /* Be sure no bpstat's are pointing at the breakpoint after it's
12169 been freed. */
12170 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 12171 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
12172 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12173 commands are associated with the bpstat; if we remove it here,
12174 then the later call to bpstat_do_actions (&stop_bpstat); in
12175 event-top.c won't do anything, and temporary breakpoints with
12176 commands won't work. */
12177
12178 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12179
4a64f543
MS
12180 /* Now that breakpoint is removed from breakpoint list, update the
12181 global location list. This will remove locations that used to
12182 belong to this breakpoint. Do this before freeing the breakpoint
12183 itself, since remove_breakpoint looks at location's owner. It
12184 might be better design to have location completely
12185 self-contained, but it's not the case now. */
44702360 12186 update_global_location_list (UGLL_DONT_INSERT);
74960c60 12187
4a64f543
MS
12188 /* On the chance that someone will soon try again to delete this
12189 same bp, we mark it as deleted before freeing its storage. */
c906108c 12190 bpt->type = bp_none;
4d01a485 12191 delete bpt;
c906108c
SS
12192}
12193
51be5b68
PA
12194/* Iterator function to call a user-provided callback function once
12195 for each of B and its related breakpoints. */
12196
12197static void
12198iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 12199 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
12200{
12201 struct breakpoint *related;
12202
12203 related = b;
12204 do
12205 {
12206 struct breakpoint *next;
12207
12208 /* FUNCTION may delete RELATED. */
12209 next = related->related_breakpoint;
12210
12211 if (next == related)
12212 {
12213 /* RELATED is the last ring entry. */
48649e1b 12214 function (related);
51be5b68
PA
12215
12216 /* FUNCTION may have deleted it, so we'd never reach back to
12217 B. There's nothing left to do anyway, so just break
12218 out. */
12219 break;
12220 }
12221 else
48649e1b 12222 function (related);
51be5b68
PA
12223
12224 related = next;
12225 }
12226 while (related != b);
12227}
95a42b64 12228
4495129a 12229static void
981a3fb3 12230delete_command (const char *arg, int from_tty)
c906108c 12231{
ea9365bb
TT
12232 dont_repeat ();
12233
c906108c
SS
12234 if (arg == 0)
12235 {
12236 int breaks_to_delete = 0;
12237
46c6471b 12238 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
12239 internal breakpoints, these have to be deleted with an
12240 explicit breakpoint number argument. */
43892fdf 12241 for (breakpoint *b : all_breakpoints ())
46c6471b 12242 if (user_breakpoint_p (b))
973d738b
DJ
12243 {
12244 breaks_to_delete = 1;
12245 break;
12246 }
c906108c
SS
12247
12248 /* Ask user only if there are some breakpoints to delete. */
12249 if (!from_tty
e2e0b3e5 12250 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
12251 for (breakpoint *b : all_breakpoints_safe ())
12252 if (user_breakpoint_p (b))
12253 delete_breakpoint (b);
c906108c
SS
12254 }
12255 else
48649e1b 12256 map_breakpoint_numbers
b926417a 12257 (arg, [&] (breakpoint *br)
48649e1b 12258 {
b926417a 12259 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 12260 });
c906108c
SS
12261}
12262
c2f4122d
PA
12263/* Return true if all locations of B bound to PSPACE are pending. If
12264 PSPACE is NULL, all locations of all program spaces are
12265 considered. */
12266
0d381245 12267static int
c2f4122d 12268all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 12269{
40cb8ca5 12270 for (bp_location *loc : b->locations ())
c2f4122d
PA
12271 if ((pspace == NULL
12272 || loc->pspace == pspace)
12273 && !loc->shlib_disabled
8645ff69 12274 && !loc->pspace->executing_startup)
0d381245
VP
12275 return 0;
12276 return 1;
fe3f5fa8
VP
12277}
12278
776592bf
DE
12279/* Subroutine of update_breakpoint_locations to simplify it.
12280 Return non-zero if multiple fns in list LOC have the same name.
12281 Null names are ignored. */
12282
12283static int
12284ambiguous_names_p (struct bp_location *loc)
12285{
12286 struct bp_location *l;
2698f5ea 12287 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 12288 xcalloc, xfree));
776592bf
DE
12289
12290 for (l = loc; l != NULL; l = l->next)
12291 {
12292 const char **slot;
23d6ee64 12293 const char *name = l->function_name.get ();
776592bf
DE
12294
12295 /* Allow for some names to be NULL, ignore them. */
12296 if (name == NULL)
12297 continue;
12298
c1fb9836 12299 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 12300 INSERT);
4a64f543
MS
12301 /* NOTE: We can assume slot != NULL here because xcalloc never
12302 returns NULL. */
776592bf 12303 if (*slot != NULL)
c1fb9836 12304 return 1;
776592bf
DE
12305 *slot = name;
12306 }
12307
776592bf
DE
12308 return 0;
12309}
12310
0fb4aa4b
PA
12311/* When symbols change, it probably means the sources changed as well,
12312 and it might mean the static tracepoint markers are no longer at
12313 the same address or line numbers they used to be at last we
12314 checked. Losing your static tracepoints whenever you rebuild is
12315 undesirable. This function tries to resync/rematch gdb static
12316 tracepoints with the markers on the target, for static tracepoints
12317 that have not been set by marker id. Static tracepoint that have
12318 been set by marker id are reset by marker id in breakpoint_re_set.
12319 The heuristic is:
12320
12321 1) For a tracepoint set at a specific address, look for a marker at
12322 the old PC. If one is found there, assume to be the same marker.
12323 If the name / string id of the marker found is different from the
12324 previous known name, assume that means the user renamed the marker
12325 in the sources, and output a warning.
12326
12327 2) For a tracepoint set at a given line number, look for a marker
12328 at the new address of the old line number. If one is found there,
12329 assume to be the same marker. If the name / string id of the
12330 marker found is different from the previous known name, assume that
12331 means the user renamed the marker in the sources, and output a
12332 warning.
12333
12334 3) If a marker is no longer found at the same address or line, it
12335 may mean the marker no longer exists. But it may also just mean
12336 the code changed a bit. Maybe the user added a few lines of code
12337 that made the marker move up or down (in line number terms). Ask
12338 the target for info about the marker with the string id as we knew
12339 it. If found, update line number and address in the matching
12340 static tracepoint. This will get confused if there's more than one
12341 marker with the same ID (possible in UST, although unadvised
12342 precisely because it confuses tools). */
12343
12344static struct symtab_and_line
12345update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12346{
d9b3f62e 12347 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
12348 struct static_tracepoint_marker marker;
12349 CORE_ADDR pc;
0fb4aa4b
PA
12350
12351 pc = sal.pc;
12352 if (sal.line)
12353 find_line_pc (sal.symtab, sal.line, &pc);
12354
12355 if (target_static_tracepoint_marker_at (pc, &marker))
12356 {
5d9310c4 12357 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 12358 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
12359 b->number, tp->static_trace_marker_id.c_str (),
12360 marker.str_id.c_str ());
0fb4aa4b 12361
5d9310c4 12362 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
12363
12364 return sal;
12365 }
12366
12367 /* Old marker wasn't found on target at lineno. Try looking it up
12368 by string ID. */
12369 if (!sal.explicit_pc
12370 && sal.line != 0
12371 && sal.symtab != NULL
5d9310c4 12372 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 12373 {
5d9310c4
SM
12374 std::vector<static_tracepoint_marker> markers
12375 = target_static_tracepoint_markers_by_strid
12376 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 12377
5d9310c4 12378 if (!markers.empty ())
0fb4aa4b 12379 {
0fb4aa4b 12380 struct symbol *sym;
80e1d417 12381 struct static_tracepoint_marker *tpmarker;
79a45e25 12382 struct ui_out *uiout = current_uiout;
0fb4aa4b 12383
5d9310c4 12384 tpmarker = &markers[0];
0fb4aa4b 12385
5d9310c4 12386 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
12387
12388 warning (_("marker for static tracepoint %d (%s) not "
12389 "found at previous line number"),
5d9310c4 12390 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 12391
51abb421 12392 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 12393 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 12394 uiout->text ("Now in ");
0fb4aa4b
PA
12395 if (sym)
12396 {
987012b8 12397 uiout->field_string ("func", sym->print_name (),
e43b10e1 12398 function_name_style.style ());
112e8700 12399 uiout->text (" at ");
0fb4aa4b 12400 }
112e8700 12401 uiout->field_string ("file",
cbe56571 12402 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 12403 file_name_style.style ());
112e8700 12404 uiout->text (":");
0fb4aa4b 12405
112e8700 12406 if (uiout->is_mi_like_p ())
0fb4aa4b 12407 {
0b0865da 12408 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 12409
112e8700 12410 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
12411 }
12412
381befee 12413 uiout->field_signed ("line", sal2.line);
112e8700 12414 uiout->text ("\n");
0fb4aa4b 12415
80e1d417 12416 b->loc->line_number = sal2.line;
2f202fde 12417 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 12418
40d97ee2
PA
12419 std::unique_ptr<explicit_location_spec> els
12420 (new explicit_location_spec ());
12421 els->source_filename
12422 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12423 els->line_offset.offset = b->loc->line_number;
12424 els->line_offset.sign = LINE_OFFSET_NONE;
12425
12426 b->locspec = std::move (els);
0fb4aa4b
PA
12427
12428 /* Might be nice to check if function changed, and warn if
12429 so. */
0fb4aa4b
PA
12430 }
12431 }
12432 return sal;
12433}
12434
8d3788bd
VP
12435/* Returns 1 iff locations A and B are sufficiently same that
12436 we don't need to report breakpoint as changed. */
12437
12438static int
12439locations_are_equal (struct bp_location *a, struct bp_location *b)
12440{
12441 while (a && b)
12442 {
12443 if (a->address != b->address)
12444 return 0;
12445
12446 if (a->shlib_disabled != b->shlib_disabled)
12447 return 0;
12448
12449 if (a->enabled != b->enabled)
12450 return 0;
12451
b5fa468f
TBA
12452 if (a->disabled_by_cond != b->disabled_by_cond)
12453 return 0;
12454
8d3788bd
VP
12455 a = a->next;
12456 b = b->next;
12457 }
12458
12459 if ((a == NULL) != (b == NULL))
12460 return 0;
12461
12462 return 1;
12463}
12464
c2f4122d
PA
12465/* Split all locations of B that are bound to PSPACE out of B's
12466 location list to a separate list and return that list's head. If
12467 PSPACE is NULL, hoist out all locations of B. */
12468
12469static struct bp_location *
12470hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12471{
12472 struct bp_location head;
12473 struct bp_location *i = b->loc;
12474 struct bp_location **i_link = &b->loc;
12475 struct bp_location *hoisted = &head;
12476
12477 if (pspace == NULL)
12478 {
12479 i = b->loc;
12480 b->loc = NULL;
12481 return i;
12482 }
12483
12484 head.next = NULL;
12485
12486 while (i != NULL)
12487 {
12488 if (i->pspace == pspace)
12489 {
12490 *i_link = i->next;
12491 i->next = NULL;
12492 hoisted->next = i;
12493 hoisted = i;
12494 }
12495 else
12496 i_link = &i->next;
12497 i = *i_link;
12498 }
12499
12500 return head.next;
12501}
12502
12503/* Create new breakpoint locations for B (a hardware or software
12504 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12505 zero, then B is a ranged breakpoint. Only recreates locations for
12506 FILTER_PSPACE. Locations of other program spaces are left
12507 untouched. */
f1310107 12508
0e30163f 12509void
74421c0b 12510update_breakpoint_locations (code_breakpoint *b,
c2f4122d 12511 struct program_space *filter_pspace,
6c5b2ebe
PA
12512 gdb::array_view<const symtab_and_line> sals,
12513 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 12514{
c2f4122d 12515 struct bp_location *existing_locations;
0d381245 12516
6c5b2ebe 12517 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
12518 {
12519 /* Ranged breakpoints have only one start location and one end
12520 location. */
12521 b->enable_state = bp_disabled;
6cb06a8c
TT
12522 gdb_printf (gdb_stderr,
12523 _("Could not reset ranged breakpoint %d: "
12524 "multiple locations found\n"),
12525 b->number);
f8eba3c6
TT
12526 return;
12527 }
f1310107 12528
4a64f543
MS
12529 /* If there's no new locations, and all existing locations are
12530 pending, don't do anything. This optimizes the common case where
12531 all locations are in the same shared library, that was unloaded.
12532 We'd like to retain the location, so that when the library is
12533 loaded again, we don't loose the enabled/disabled status of the
12534 individual locations. */
6c5b2ebe 12535 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
12536 return;
12537
c2f4122d 12538 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 12539
6c5b2ebe 12540 for (const auto &sal : sals)
fe3f5fa8 12541 {
f8eba3c6
TT
12542 struct bp_location *new_loc;
12543
6c5b2ebe 12544 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 12545
06615628 12546 new_loc = b->add_location (sal);
fe3f5fa8 12547
0d381245
VP
12548 /* Reparse conditions, they might contain references to the
12549 old symtab. */
12550 if (b->cond_string != NULL)
12551 {
bbc13ae3 12552 const char *s;
fe3f5fa8 12553
6f781ee3 12554 s = b->cond_string.get ();
a70b8144 12555 try
0d381245 12556 {
6c5b2ebe
PA
12557 new_loc->cond = parse_exp_1 (&s, sal.pc,
12558 block_for_pc (sal.pc),
0d381245
VP
12559 0);
12560 }
230d2906 12561 catch (const gdb_exception_error &e)
0d381245 12562 {
b5fa468f 12563 new_loc->disabled_by_cond = true;
0d381245
VP
12564 }
12565 }
fe3f5fa8 12566
6c5b2ebe 12567 if (!sals_end.empty ())
f1310107 12568 {
6c5b2ebe 12569 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 12570
6c5b2ebe 12571 new_loc->length = end - sals[0].pc + 1;
f1310107 12572 }
0d381245 12573 }
fe3f5fa8 12574
4a64f543
MS
12575 /* If possible, carry over 'disable' status from existing
12576 breakpoints. */
0d381245
VP
12577 {
12578 struct bp_location *e = existing_locations;
776592bf
DE
12579 /* If there are multiple breakpoints with the same function name,
12580 e.g. for inline functions, comparing function names won't work.
12581 Instead compare pc addresses; this is just a heuristic as things
12582 may have moved, but in practice it gives the correct answer
12583 often enough until a better solution is found. */
12584 int have_ambiguous_names = ambiguous_names_p (b->loc);
12585
0d381245
VP
12586 for (; e; e = e->next)
12587 {
b5fa468f 12588 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 12589 {
776592bf
DE
12590 if (have_ambiguous_names)
12591 {
40cb8ca5 12592 for (bp_location *l : b->locations ())
7f32a4d5
PA
12593 {
12594 /* Ignore software vs hardware location type at
12595 this point, because with "set breakpoint
12596 auto-hw", after a re-set, locations that were
12597 hardware can end up as software, or vice versa.
12598 As mentioned above, this is an heuristic and in
12599 practice should give the correct answer often
12600 enough. */
12601 if (breakpoint_locations_match (e, l, true))
12602 {
b5fa468f
TBA
12603 l->enabled = e->enabled;
12604 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
12605 break;
12606 }
12607 }
776592bf
DE
12608 }
12609 else
12610 {
40cb8ca5 12611 for (bp_location *l : b->locations ())
776592bf 12612 if (l->function_name
23d6ee64
TT
12613 && strcmp (e->function_name.get (),
12614 l->function_name.get ()) == 0)
776592bf 12615 {
b5fa468f
TBA
12616 l->enabled = e->enabled;
12617 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
12618 break;
12619 }
12620 }
0d381245
VP
12621 }
12622 }
12623 }
fe3f5fa8 12624
8d3788bd 12625 if (!locations_are_equal (existing_locations, b->loc))
76727919 12626 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
12627}
12628
264f9890 12629/* Find the SaL locations corresponding to the given LOCSPEC.
ef23e705
TJB
12630 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12631
2c9a6d72
TT
12632std::vector<symtab_and_line>
12633code_breakpoint::location_spec_to_sals (location_spec *locspec,
12634 struct program_space *search_pspace,
12635 int *found)
ef23e705 12636{
cc06b668 12637 struct gdb_exception exception;
ef23e705 12638
6c5b2ebe
PA
12639 std::vector<symtab_and_line> sals;
12640
a70b8144 12641 try
ef23e705 12642 {
2c9a6d72 12643 sals = decode_location_spec (locspec, search_pspace);
ef23e705 12644 }
94aeb44b 12645 catch (gdb_exception_error &e)
ef23e705
TJB
12646 {
12647 int not_found_and_ok = 0;
492d29ea 12648
ef23e705
TJB
12649 /* For pending breakpoints, it's expected that parsing will
12650 fail until the right shared library is loaded. User has
12651 already told to create pending breakpoints and don't need
12652 extra messages. If breakpoint is in bp_shlib_disabled
12653 state, then user already saw the message about that
12654 breakpoint being disabled, and don't want to see more
12655 errors. */
58438ac1 12656 if (e.error == NOT_FOUND_ERROR
2c9a6d72
TT
12657 && (condition_not_parsed
12658 || (loc != NULL
c2f4122d 12659 && search_pspace != NULL
2c9a6d72
TT
12660 && loc->pspace != search_pspace)
12661 || (loc && loc->shlib_disabled)
12662 || (loc && loc->pspace->executing_startup)
12663 || enable_state == bp_disabled))
ef23e705
TJB
12664 not_found_and_ok = 1;
12665
12666 if (!not_found_and_ok)
12667 {
12668 /* We surely don't want to warn about the same breakpoint
12669 10 times. One solution, implemented here, is disable
12670 the breakpoint on error. Another solution would be to
12671 have separate 'warning emitted' flag. Since this
12672 happens only when a binary has changed, I don't know
12673 which approach is better. */
2c9a6d72 12674 enable_state = bp_disabled;
eedc3f4f 12675 throw;
ef23e705 12676 }
94aeb44b
TT
12677
12678 exception = std::move (e);
ef23e705
TJB
12679 }
12680
492d29ea 12681 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 12682 {
6c5b2ebe
PA
12683 for (auto &sal : sals)
12684 resolve_sal_pc (&sal);
2c9a6d72 12685 if (condition_not_parsed && extra_string != NULL)
ef23e705 12686 {
2c9a6d72
TT
12687 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12688 int local_thread, local_task;
ef23e705 12689
2c9a6d72
TT
12690 find_condition_and_thread_for_sals (sals, extra_string.get (),
12691 &local_cond, &local_thread,
12692 &local_task, &local_extra);
12693 gdb_assert (cond_string == nullptr);
12694 if (local_cond != nullptr)
12695 cond_string = std::move (local_cond);
12696 thread = local_thread;
12697 task = local_task;
12698 if (local_extra != nullptr)
12699 extra_string = std::move (local_extra);
12700 condition_not_parsed = 0;
ef23e705
TJB
12701 }
12702
2c9a6d72
TT
12703 if (type == bp_static_tracepoint)
12704 sals[0] = update_static_tracepoint (this, sals[0]);
ef23e705 12705
58438ac1
TT
12706 *found = 1;
12707 }
12708 else
12709 *found = 0;
ef23e705
TJB
12710
12711 return sals;
12712}
12713
348d480f
PA
12714/* The default re_set method, for typical hardware or software
12715 breakpoints. Reevaluate the breakpoint and recreate its
12716 locations. */
12717
b3d5660a
TT
12718void
12719code_breakpoint::re_set_default ()
ef23e705 12720{
c2f4122d 12721 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 12722 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 12723
6c5b2ebe 12724 int found;
2c9a6d72
TT
12725 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12726 filter_pspace,
12727 &found);
ef23e705 12728 if (found)
6c5b2ebe 12729 expanded = std::move (sals);
ef23e705 12730
b3d5660a 12731 if (locspec_range_end != nullptr)
f1310107 12732 {
6c5b2ebe 12733 std::vector<symtab_and_line> sals_end
2c9a6d72 12734 = location_spec_to_sals (locspec_range_end.get (),
264f9890 12735 filter_pspace, &found);
f1310107 12736 if (found)
6c5b2ebe 12737 expanded_end = std::move (sals_end);
f1310107
TJB
12738 }
12739
b3d5660a 12740 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
28010a5d
PA
12741}
12742
983af33b
SDJ
12743/* Default method for creating SALs from an address string. It basically
12744 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12745
12746static void
264f9890
PA
12747create_sals_from_location_spec_default (location_spec *locspec,
12748 struct linespec_result *canonical)
983af33b 12749{
264f9890 12750 parse_breakpoint_sals (locspec, canonical);
983af33b
SDJ
12751}
12752
983af33b 12753/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 12754 default function for the `decode_location' method of breakpoint_ops. */
983af33b 12755
6c5b2ebe 12756static std::vector<symtab_and_line>
264f9890
PA
12757decode_location_spec_default (struct breakpoint *b,
12758 location_spec *locspec,
12759 program_space *search_pspace)
983af33b
SDJ
12760{
12761 struct linespec_result canonical;
12762
264f9890 12763 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 12764 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 12765 b->filter.get ());
983af33b
SDJ
12766
12767 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 12768 gdb_assert (canonical.lsals.size () < 2);
983af33b 12769
6c5b2ebe 12770 if (!canonical.lsals.empty ())
983af33b 12771 {
6c5b2ebe
PA
12772 const linespec_sals &lsal = canonical.lsals[0];
12773 return std::move (lsal.sals);
983af33b 12774 }
6c5b2ebe 12775 return {};
983af33b
SDJ
12776}
12777
bf469271 12778/* Reset a breakpoint. */
c906108c 12779
bf469271
PA
12780static void
12781breakpoint_re_set_one (breakpoint *b)
c906108c 12782{
fdf44873
TT
12783 input_radix = b->input_radix;
12784 set_language (b->language);
c906108c 12785
c47614fe 12786 b->re_set ();
c906108c
SS
12787}
12788
c2f4122d
PA
12789/* Re-set breakpoint locations for the current program space.
12790 Locations bound to other program spaces are left untouched. */
12791
c906108c 12792void
69de3c6a 12793breakpoint_re_set (void)
c906108c 12794{
c5aa993b 12795 {
fdf44873
TT
12796 scoped_restore_current_language save_language;
12797 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 12798 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 12799
8e817061
JB
12800 /* breakpoint_re_set_one sets the current_language to the language
12801 of the breakpoint it is resetting (see prepare_re_set_context)
12802 before re-evaluating the breakpoint's location. This change can
12803 unfortunately get undone by accident if the language_mode is set
12804 to auto, and we either switch frames, or more likely in this context,
12805 we select the current frame.
12806
12807 We prevent this by temporarily turning the language_mode to
12808 language_mode_manual. We restore it once all breakpoints
12809 have been reset. */
12810 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12811 language_mode = language_mode_manual;
12812
5ed8105e
PA
12813 /* Note: we must not try to insert locations until after all
12814 breakpoints have been re-set. Otherwise, e.g., when re-setting
12815 breakpoint 1, we'd insert the locations of breakpoint 2, which
12816 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 12817
1428b37a 12818 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 12819 {
a70b8144 12820 try
bf469271
PA
12821 {
12822 breakpoint_re_set_one (b);
12823 }
230d2906 12824 catch (const gdb_exception &ex)
bf469271
PA
12825 {
12826 exception_fprintf (gdb_stderr, ex,
12827 "Error in re-setting breakpoint %d: ",
12828 b->number);
12829 }
5ed8105e 12830 }
5ed8105e
PA
12831
12832 jit_breakpoint_re_set ();
12833 }
6c95b8df 12834
af02033e
PP
12835 create_overlay_event_breakpoint ();
12836 create_longjmp_master_breakpoint ();
12837 create_std_terminate_master_breakpoint ();
186c406b 12838 create_exception_master_breakpoint ();
2a7f3dff
PA
12839
12840 /* Now we can insert. */
12841 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
12842}
12843\f
c906108c
SS
12844/* Reset the thread number of this breakpoint:
12845
12846 - If the breakpoint is for all threads, leave it as-is.
4a64f543 12847 - Else, reset it to the current thread for inferior_ptid. */
c906108c 12848void
fba45db2 12849breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
12850{
12851 if (b->thread != -1)
12852 {
00431a78 12853 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
12854
12855 /* We're being called after following a fork. The new fork is
12856 selected as current, and unless this was a vfork will have a
12857 different program space from the original thread. Reset that
12858 as well. */
12859 b->loc->pspace = current_program_space;
c906108c
SS
12860 }
12861}
12862
03ac34d5
MS
12863/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12864 If from_tty is nonzero, it prints a message to that effect,
12865 which ends with a period (no newline). */
12866
c906108c 12867void
fba45db2 12868set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12869{
c906108c
SS
12870 if (count < 0)
12871 count = 0;
12872
43892fdf 12873 for (breakpoint *b : all_breakpoints ())
c906108c 12874 if (b->number == bptnum)
01add95b
SM
12875 {
12876 if (is_tracepoint (b))
12877 {
12878 if (from_tty && count != 0)
6cb06a8c
TT
12879 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12880 bptnum);
01add95b
SM
12881 return;
12882 }
12883
12884 b->ignore_count = count;
12885 if (from_tty)
12886 {
12887 if (count == 0)
6cb06a8c
TT
12888 gdb_printf (_("Will stop next time "
12889 "breakpoint %d is reached."),
12890 bptnum);
01add95b 12891 else if (count == 1)
6cb06a8c
TT
12892 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
12893 bptnum);
01add95b 12894 else
6cb06a8c
TT
12895 gdb_printf (_("Will ignore next %d "
12896 "crossings of breakpoint %d."),
12897 count, bptnum);
01add95b
SM
12898 }
12899 gdb::observers::breakpoint_modified.notify (b);
12900 return;
12901 }
c906108c 12902
8a3fe4f8 12903 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12904}
12905
c906108c
SS
12906/* Command to set ignore-count of breakpoint N to COUNT. */
12907
12908static void
0b39b52e 12909ignore_command (const char *args, int from_tty)
c906108c 12910{
0b39b52e 12911 const char *p = args;
52f0bd74 12912 int num;
c906108c
SS
12913
12914 if (p == 0)
e2e0b3e5 12915 error_no_arg (_("a breakpoint number"));
c5aa993b 12916
c906108c 12917 num = get_number (&p);
5c44784c 12918 if (num == 0)
8a3fe4f8 12919 error (_("bad breakpoint number: '%s'"), args);
c906108c 12920 if (*p == 0)
8a3fe4f8 12921 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12922
12923 set_ignore_count (num,
12924 longest_to_int (value_as_long (parse_and_eval (p))),
12925 from_tty);
221ea385 12926 if (from_tty)
6cb06a8c 12927 gdb_printf ("\n");
c906108c
SS
12928}
12929\f
d0fe4701
XR
12930
12931/* Call FUNCTION on each of the breakpoints with numbers in the range
12932 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
12933
12934static void
d0fe4701
XR
12935map_breakpoint_number_range (std::pair<int, int> bp_num_range,
12936 gdb::function_view<void (breakpoint *)> function)
c906108c 12937{
d0fe4701
XR
12938 if (bp_num_range.first == 0)
12939 {
12940 warning (_("bad breakpoint number at or near '%d'"),
12941 bp_num_range.first);
12942 }
12943 else
c906108c 12944 {
d0fe4701 12945 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 12946 {
d0fe4701
XR
12947 bool match = false;
12948
1428b37a 12949 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 12950 if (b->number == i)
5c44784c 12951 {
bfd28288 12952 match = true;
48649e1b 12953 function (b);
11cf8741 12954 break;
5c44784c 12955 }
bfd28288 12956 if (!match)
6cb06a8c 12957 gdb_printf (_("No breakpoint number %d.\n"), i);
c5aa993b 12958 }
c906108c
SS
12959 }
12960}
12961
d0fe4701
XR
12962/* Call FUNCTION on each of the breakpoints whose numbers are given in
12963 ARGS. */
12964
12965static void
12966map_breakpoint_numbers (const char *args,
12967 gdb::function_view<void (breakpoint *)> function)
12968{
12969 if (args == NULL || *args == '\0')
12970 error_no_arg (_("one or more breakpoint numbers"));
12971
12972 number_or_range_parser parser (args);
12973
12974 while (!parser.finished ())
12975 {
12976 int num = parser.get_number ();
12977 map_breakpoint_number_range (std::make_pair (num, num), function);
12978 }
12979}
12980
12981/* Return the breakpoint location structure corresponding to the
12982 BP_NUM and LOC_NUM values. */
12983
0d381245 12984static struct bp_location *
d0fe4701 12985find_location_by_number (int bp_num, int loc_num)
0d381245 12986{
43892fdf 12987 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
12988
12989 if (!b || b->number != bp_num)
d0fe4701 12990 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 12991
0d381245 12992 if (loc_num == 0)
d0fe4701 12993 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 12994
d0fe4701 12995 int n = 0;
40cb8ca5 12996 for (bp_location *loc : b->locations ())
d0fe4701
XR
12997 if (++n == loc_num)
12998 return loc;
12999
13000 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13001}
13002
95e95a6d
PA
13003/* Modes of operation for extract_bp_num. */
13004enum class extract_bp_kind
13005{
13006 /* Extracting a breakpoint number. */
13007 bp,
13008
13009 /* Extracting a location number. */
13010 loc,
13011};
13012
13013/* Extract a breakpoint or location number (as determined by KIND)
13014 from the string starting at START. TRAILER is a character which
13015 can be found after the number. If you don't want a trailer, use
13016 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13017 string. This always returns a positive integer. */
13018
13019static int
13020extract_bp_num (extract_bp_kind kind, const char *start,
13021 int trailer, const char **end_out = NULL)
13022{
13023 const char *end = start;
13024 int num = get_number_trailer (&end, trailer);
13025 if (num < 0)
13026 error (kind == extract_bp_kind::bp
13027 ? _("Negative breakpoint number '%.*s'")
13028 : _("Negative breakpoint location number '%.*s'"),
13029 int (end - start), start);
13030 if (num == 0)
13031 error (kind == extract_bp_kind::bp
13032 ? _("Bad breakpoint number '%.*s'")
13033 : _("Bad breakpoint location number '%.*s'"),
13034 int (end - start), start);
13035
13036 if (end_out != NULL)
13037 *end_out = end;
13038 return num;
13039}
13040
13041/* Extract a breakpoint or location range (as determined by KIND) in
13042 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13043 representing the (inclusive) range. The returned pair's elements
13044 are always positive integers. */
13045
13046static std::pair<int, int>
13047extract_bp_or_bp_range (extract_bp_kind kind,
13048 const std::string &arg,
13049 std::string::size_type arg_offset)
13050{
13051 std::pair<int, int> range;
13052 const char *bp_loc = &arg[arg_offset];
13053 std::string::size_type dash = arg.find ('-', arg_offset);
13054 if (dash != std::string::npos)
13055 {
13056 /* bp_loc is a range (x-z). */
13057 if (arg.length () == dash + 1)
13058 error (kind == extract_bp_kind::bp
13059 ? _("Bad breakpoint number at or near: '%s'")
13060 : _("Bad breakpoint location number at or near: '%s'"),
13061 bp_loc);
13062
13063 const char *end;
13064 const char *start_first = bp_loc;
13065 const char *start_second = &arg[dash + 1];
13066 range.first = extract_bp_num (kind, start_first, '-');
13067 range.second = extract_bp_num (kind, start_second, '\0', &end);
13068
13069 if (range.first > range.second)
13070 error (kind == extract_bp_kind::bp
13071 ? _("Inverted breakpoint range at '%.*s'")
13072 : _("Inverted breakpoint location range at '%.*s'"),
13073 int (end - start_first), start_first);
13074 }
13075 else
13076 {
13077 /* bp_loc is a single value. */
13078 range.first = extract_bp_num (kind, bp_loc, '\0');
13079 range.second = range.first;
13080 }
13081 return range;
13082}
13083
d0fe4701
XR
13084/* Extract the breakpoint/location range specified by ARG. Returns
13085 the breakpoint range in BP_NUM_RANGE, and the location range in
13086 BP_LOC_RANGE.
13087
13088 ARG may be in any of the following forms:
13089
13090 x where 'x' is a breakpoint number.
13091 x-y where 'x' and 'y' specify a breakpoint numbers range.
13092 x.y where 'x' is a breakpoint number and 'y' a location number.
13093 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13094 location number range.
13095*/
13096
cc638e86 13097static void
d0fe4701
XR
13098extract_bp_number_and_location (const std::string &arg,
13099 std::pair<int, int> &bp_num_range,
13100 std::pair<int, int> &bp_loc_range)
13101{
13102 std::string::size_type dot = arg.find ('.');
13103
13104 if (dot != std::string::npos)
13105 {
13106 /* Handle 'x.y' and 'x.y-z' cases. */
13107
13108 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 13109 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 13110
95e95a6d
PA
13111 bp_num_range.first
13112 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13113 bp_num_range.second = bp_num_range.first;
d0fe4701 13114
95e95a6d
PA
13115 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13116 arg, dot + 1);
d0fe4701
XR
13117 }
13118 else
13119 {
13120 /* Handle x and x-y cases. */
d0fe4701 13121
95e95a6d 13122 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
13123 bp_loc_range.first = 0;
13124 bp_loc_range.second = 0;
13125 }
d0fe4701
XR
13126}
13127
13128/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13129 specifies whether to enable or disable. */
13130
13131static void
13132enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13133{
13134 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13135 if (loc != NULL)
13136 {
b5fa468f
TBA
13137 if (loc->disabled_by_cond && enable)
13138 error (_("Breakpoint %d's condition is invalid at location %d, "
13139 "cannot enable."), bp_num, loc_num);
13140
d0fe4701
XR
13141 if (loc->enabled != enable)
13142 {
13143 loc->enabled = enable;
13144 mark_breakpoint_location_modified (loc);
13145 }
13146 if (target_supports_enable_disable_tracepoint ()
13147 && current_trace_status ()->running && loc->owner
13148 && is_tracepoint (loc->owner))
13149 target_disable_tracepoint (loc);
13150 }
13151 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13152
e5213e2c
SF
13153 gdb::observers::breakpoint_modified.notify (loc->owner);
13154}
13155
13156/* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13157 owner. 1-based indexing. -1 signals NOT FOUND. */
13158
13159static int
13160find_loc_num_by_location (bp_location *loc)
13161{
13162 if (loc != nullptr && loc->owner != nullptr)
13163 {
13164 /* Locations use 1-based indexing. */
13165 int loc_num = 1;
13166 for (bp_location *it : loc->owner->locations ())
13167 {
13168 if (it == loc)
13169 return loc_num;
13170 loc_num++;
13171 }
13172 }
13173 return -1;
13174}
13175
13176/* Enable or disable a breakpoint location LOC. ENABLE
13177 specifies whether to enable or disable. */
13178
13179void
13180enable_disable_bp_location (bp_location *loc, bool enable)
13181{
13182 if (loc == nullptr)
13183 error (_("Breakpoint location is invalid."));
13184
13185 if (loc->owner == nullptr)
13186 error (_("Breakpoint location does not have an owner breakpoint."));
13187
13188 if (loc->disabled_by_cond && enable)
13189 {
13190 int loc_num = find_loc_num_by_location (loc);
13191 if (loc_num == -1)
13192 error (_("Breakpoint location LOC_NUM could not be found."));
13193 else
13194 error (_("Breakpoint %d's condition is invalid at location %d, "
13195 "cannot enable."), loc->owner->number, loc_num);
13196 }
13197
13198 if (loc->enabled != enable)
13199 {
13200 loc->enabled = enable;
13201 mark_breakpoint_location_modified (loc);
13202 }
13203
13204 if (target_supports_enable_disable_tracepoint ()
13205 && current_trace_status ()->running && loc->owner
13206 && is_tracepoint (loc->owner))
13207 target_disable_tracepoint (loc);
13208
13209 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13210 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
13211}
13212
13213/* Enable or disable a range of breakpoint locations. BP_NUM is the
13214 number of the breakpoint, and BP_LOC_RANGE specifies the
13215 (inclusive) range of location numbers of that breakpoint to
13216 enable/disable. ENABLE specifies whether to enable or disable the
13217 location. */
13218
13219static void
13220enable_disable_breakpoint_location_range (int bp_num,
13221 std::pair<int, int> &bp_loc_range,
13222 bool enable)
13223{
13224 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13225 enable_disable_bp_num_loc (bp_num, i, enable);
13226}
0d381245 13227
1900040c
MS
13228/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13229 If from_tty is nonzero, it prints a message to that effect,
13230 which ends with a period (no newline). */
13231
c906108c 13232void
fba45db2 13233disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
13234{
13235 /* Never disable a watchpoint scope breakpoint; we want to
13236 hit them when we leave scope so we can delete both the
13237 watchpoint and its scope breakpoint at that time. */
13238 if (bpt->type == bp_watchpoint_scope)
13239 return;
13240
b5de0fa7 13241 bpt->enable_state = bp_disabled;
c906108c 13242
b775012e
LM
13243 /* Mark breakpoint locations modified. */
13244 mark_breakpoint_modified (bpt);
13245
d248b706
KY
13246 if (target_supports_enable_disable_tracepoint ()
13247 && current_trace_status ()->running && is_tracepoint (bpt))
13248 {
40cb8ca5 13249 for (bp_location *location : bpt->locations ())
d248b706
KY
13250 target_disable_tracepoint (location);
13251 }
13252
44702360 13253 update_global_location_list (UGLL_DONT_INSERT);
c906108c 13254
76727919 13255 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13256}
13257
d0fe4701
XR
13258/* Enable or disable the breakpoint(s) or breakpoint location(s)
13259 specified in ARGS. ARGS may be in any of the formats handled by
13260 extract_bp_number_and_location. ENABLE specifies whether to enable
13261 or disable the breakpoints/locations. */
13262
c906108c 13263static void
d0fe4701 13264enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 13265{
c906108c 13266 if (args == 0)
46c6471b 13267 {
43892fdf 13268 for (breakpoint *bpt : all_breakpoints ())
46c6471b 13269 if (user_breakpoint_p (bpt))
d0fe4701
XR
13270 {
13271 if (enable)
13272 enable_breakpoint (bpt);
13273 else
13274 disable_breakpoint (bpt);
13275 }
46c6471b 13276 }
9eaabc75 13277 else
0d381245 13278 {
cb791d59 13279 std::string num = extract_arg (&args);
9eaabc75 13280
cb791d59 13281 while (!num.empty ())
d248b706 13282 {
d0fe4701 13283 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 13284
cc638e86
PA
13285 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13286
13287 if (bp_loc_range.first == bp_loc_range.second
13288 && bp_loc_range.first == 0)
d0fe4701 13289 {
cc638e86
PA
13290 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13291 map_breakpoint_number_range (bp_num_range,
13292 enable
13293 ? enable_breakpoint
13294 : disable_breakpoint);
13295 }
13296 else
13297 {
13298 /* Handle breakpoint ids with formats 'x.y' or
13299 'x.y-z'. */
13300 enable_disable_breakpoint_location_range
13301 (bp_num_range.first, bp_loc_range, enable);
b775012e 13302 }
9eaabc75 13303 num = extract_arg (&args);
d248b706 13304 }
0d381245 13305 }
c906108c
SS
13306}
13307
d0fe4701
XR
13308/* The disable command disables the specified breakpoints/locations
13309 (or all defined breakpoints) so they're no longer effective in
13310 stopping the inferior. ARGS may be in any of the forms defined in
13311 extract_bp_number_and_location. */
13312
13313static void
13314disable_command (const char *args, int from_tty)
13315{
13316 enable_disable_command (args, from_tty, false);
13317}
13318
c906108c 13319static void
816338b5
SS
13320enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13321 int count)
c906108c 13322{
afe38095 13323 int target_resources_ok;
c906108c
SS
13324
13325 if (bpt->type == bp_hardware_breakpoint)
13326 {
13327 int i;
c5aa993b 13328 i = hw_breakpoint_used_count ();
53a5351d 13329 target_resources_ok =
d92524f1 13330 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 13331 i + 1, 0);
c906108c 13332 if (target_resources_ok == 0)
8a3fe4f8 13333 error (_("No hardware breakpoint support in the target."));
c906108c 13334 else if (target_resources_ok < 0)
8a3fe4f8 13335 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
13336 }
13337
cc60f2e3 13338 if (is_watchpoint (bpt))
c906108c 13339 {
d07205c2 13340 /* Initialize it just to avoid a GCC false warning. */
f486487f 13341 enum enable_state orig_enable_state = bp_disabled;
dde02812 13342
a70b8144 13343 try
c906108c 13344 {
3a5c3e22
PA
13345 struct watchpoint *w = (struct watchpoint *) bpt;
13346
1e718ff1
TJB
13347 orig_enable_state = bpt->enable_state;
13348 bpt->enable_state = bp_enabled;
3a5c3e22 13349 update_watchpoint (w, 1 /* reparse */);
c906108c 13350 }
230d2906 13351 catch (const gdb_exception &e)
c5aa993b 13352 {
1e718ff1 13353 bpt->enable_state = orig_enable_state;
dde02812
ES
13354 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13355 bpt->number);
13356 return;
c5aa993b 13357 }
c906108c 13358 }
0101ce28 13359
b775012e
LM
13360 bpt->enable_state = bp_enabled;
13361
13362 /* Mark breakpoint locations modified. */
13363 mark_breakpoint_modified (bpt);
13364
d248b706
KY
13365 if (target_supports_enable_disable_tracepoint ()
13366 && current_trace_status ()->running && is_tracepoint (bpt))
13367 {
40cb8ca5 13368 for (bp_location *location : bpt->locations ())
d248b706
KY
13369 target_enable_tracepoint (location);
13370 }
13371
b4c291bb 13372 bpt->disposition = disposition;
816338b5 13373 bpt->enable_count = count;
44702360 13374 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 13375
76727919 13376 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
13377}
13378
fe3f5fa8 13379
c906108c 13380void
fba45db2 13381enable_breakpoint (struct breakpoint *bpt)
c906108c 13382{
816338b5 13383 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
13384}
13385
d0fe4701
XR
13386/* The enable command enables the specified breakpoints/locations (or
13387 all defined breakpoints) so they once again become (or continue to
13388 be) effective in stopping the inferior. ARGS may be in any of the
13389 forms defined in extract_bp_number_and_location. */
c906108c 13390
c906108c 13391static void
981a3fb3 13392enable_command (const char *args, int from_tty)
c906108c 13393{
d0fe4701 13394 enable_disable_command (args, from_tty, true);
c906108c
SS
13395}
13396
c906108c 13397static void
4495129a 13398enable_once_command (const char *args, int from_tty)
c906108c 13399{
48649e1b
TT
13400 map_breakpoint_numbers
13401 (args, [&] (breakpoint *b)
13402 {
13403 iterate_over_related_breakpoints
13404 (b, [&] (breakpoint *bpt)
13405 {
13406 enable_breakpoint_disp (bpt, disp_disable, 1);
13407 });
13408 });
816338b5
SS
13409}
13410
13411static void
4495129a 13412enable_count_command (const char *args, int from_tty)
816338b5 13413{
b9d61307
SM
13414 int count;
13415
13416 if (args == NULL)
13417 error_no_arg (_("hit count"));
13418
13419 count = get_number (&args);
816338b5 13420
48649e1b
TT
13421 map_breakpoint_numbers
13422 (args, [&] (breakpoint *b)
13423 {
13424 iterate_over_related_breakpoints
13425 (b, [&] (breakpoint *bpt)
13426 {
13427 enable_breakpoint_disp (bpt, disp_disable, count);
13428 });
13429 });
c906108c
SS
13430}
13431
c906108c 13432static void
4495129a 13433enable_delete_command (const char *args, int from_tty)
c906108c 13434{
48649e1b
TT
13435 map_breakpoint_numbers
13436 (args, [&] (breakpoint *b)
13437 {
13438 iterate_over_related_breakpoints
13439 (b, [&] (breakpoint *bpt)
13440 {
13441 enable_breakpoint_disp (bpt, disp_del, 1);
13442 });
13443 });
c906108c
SS
13444}
13445\f
1f3b5d1b
PP
13446/* Invalidate last known value of any hardware watchpoint if
13447 the memory which that value represents has been written to by
13448 GDB itself. */
13449
13450static void
8de0566d
YQ
13451invalidate_bp_value_on_memory_change (struct inferior *inferior,
13452 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
13453 const bfd_byte *data)
13454{
43892fdf 13455 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 13456 if (bp->enable_state == bp_enabled
3a5c3e22 13457 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 13458 {
3a5c3e22 13459 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 13460
850645cf 13461 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 13462 {
40cb8ca5 13463 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
13464 if (loc->loc_type == bp_loc_hardware_watchpoint
13465 && loc->address + loc->length > addr
13466 && addr + len > loc->address)
13467 {
3a5c3e22 13468 wp->val = NULL;
4c1d86d9 13469 wp->val_valid = false;
3a5c3e22
PA
13470 }
13471 }
1f3b5d1b
PP
13472 }
13473}
13474
8181d85f
DJ
13475/* Create and insert a breakpoint for software single step. */
13476
13477void
6c95b8df 13478insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 13479 const address_space *aspace,
4a64f543 13480 CORE_ADDR next_pc)
8181d85f 13481{
7c16b83e
PA
13482 struct thread_info *tp = inferior_thread ();
13483 struct symtab_and_line sal;
13484 CORE_ADDR pc = next_pc;
8181d85f 13485
34b7e8a6
PA
13486 if (tp->control.single_step_breakpoints == NULL)
13487 {
960bc2bd 13488 std::unique_ptr<breakpoint> b
7ab97995
PA
13489 (new momentary_breakpoint (gdbarch, bp_single_step,
13490 current_program_space,
13491 null_frame_id,
13492 tp->global_num));
960bc2bd 13493
34b7e8a6 13494 tp->control.single_step_breakpoints
960bc2bd 13495 = add_to_breakpoint_chain (std::move (b));
34b7e8a6 13496 }
8181d85f 13497
7c16b83e
PA
13498 sal = find_pc_line (pc, 0);
13499 sal.pc = pc;
13500 sal.section = find_pc_overlay (pc);
13501 sal.explicit_pc = 1;
960bc2bd
PA
13502
13503 auto *ss_bp
13504 = static_cast<momentary_breakpoint *> (tp->control.single_step_breakpoints);
13505 ss_bp->add_location (sal);
8181d85f 13506
7c16b83e 13507 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
13508}
13509
93f9a11f
YQ
13510/* Insert single step breakpoints according to the current state. */
13511
13512int
13513insert_single_step_breakpoints (struct gdbarch *gdbarch)
13514{
f5ea389a 13515 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 13516 std::vector<CORE_ADDR> next_pcs;
93f9a11f 13517
f5ea389a 13518 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 13519
a0ff9e1a 13520 if (!next_pcs.empty ())
93f9a11f 13521 {
f5ea389a 13522 struct frame_info *frame = get_current_frame ();
8b86c959 13523 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 13524
a0ff9e1a 13525 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
13526 insert_single_step_breakpoint (gdbarch, aspace, pc);
13527
93f9a11f
YQ
13528 return 1;
13529 }
13530 else
13531 return 0;
13532}
13533
34b7e8a6 13534/* See breakpoint.h. */
f02253f1
HZ
13535
13536int
7c16b83e 13537breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 13538 const address_space *aspace,
7c16b83e 13539 CORE_ADDR pc)
1aafd4da 13540{
40cb8ca5 13541 for (bp_location *loc : bp->locations ())
7c16b83e
PA
13542 if (loc->inserted
13543 && breakpoint_location_address_match (loc, aspace, pc))
13544 return 1;
1aafd4da 13545
7c16b83e 13546 return 0;
ef370185
JB
13547}
13548
13549/* Check whether a software single-step breakpoint is inserted at
13550 PC. */
13551
13552int
accd0bcd 13553single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
13554 CORE_ADDR pc)
13555{
43892fdf 13556 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
13557 {
13558 if (bpt->type == bp_single_step
13559 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13560 return 1;
13561 }
13562 return 0;
1aafd4da
UW
13563}
13564
1042e4c0
SS
13565/* Tracepoint-specific operations. */
13566
13567/* Set tracepoint count to NUM. */
13568static void
13569set_tracepoint_count (int num)
13570{
13571 tracepoint_count = num;
4fa62494 13572 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
13573}
13574
70221824 13575static void
0b39b52e 13576trace_command (const char *arg, int from_tty)
1042e4c0 13577{
264f9890
PA
13578 location_spec_up locspec = string_to_location_spec (&arg,
13579 current_language);
13580 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13581 (locspec.get (), true /* is_tracepoint */);
55aa24fb 13582
558a9d82 13583 create_breakpoint (get_current_arch (),
264f9890 13584 locspec.get (),
10a636cc 13585 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13586 0 /* tempflag */,
13587 bp_tracepoint /* type_wanted */,
13588 0 /* Ignore count */,
13589 pending_break_support,
13590 ops,
13591 from_tty,
13592 1 /* enabled */,
13593 0 /* internal */, 0);
1042e4c0
SS
13594}
13595
70221824 13596static void
0b39b52e 13597ftrace_command (const char *arg, int from_tty)
7a697b8d 13598{
264f9890
PA
13599 location_spec_up locspec = string_to_location_spec (&arg,
13600 current_language);
558a9d82 13601 create_breakpoint (get_current_arch (),
264f9890 13602 locspec.get (),
10a636cc 13603 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
13604 0 /* tempflag */,
13605 bp_fast_tracepoint /* type_wanted */,
13606 0 /* Ignore count */,
13607 pending_break_support,
74421c0b 13608 &code_breakpoint_ops,
558a9d82
YQ
13609 from_tty,
13610 1 /* enabled */,
13611 0 /* internal */, 0);
0fb4aa4b
PA
13612}
13613
13614/* strace command implementation. Creates a static tracepoint. */
13615
70221824 13616static void
0b39b52e 13617strace_command (const char *arg, int from_tty)
0fb4aa4b 13618{
a678887d 13619 const struct breakpoint_ops *ops;
264f9890 13620 location_spec_up locspec;
7b572efb 13621 enum bptype type;
983af33b
SDJ
13622
13623 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13624 or with a normal static tracepoint. */
61012eef 13625 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
13626 {
13627 ops = &strace_marker_breakpoint_ops;
264f9890
PA
13628 locspec = new_linespec_location_spec (&arg,
13629 symbol_name_match_type::FULL);
7b572efb 13630 type = bp_static_marker_tracepoint;
f00aae0f 13631 }
983af33b 13632 else
f00aae0f 13633 {
74421c0b 13634 ops = &code_breakpoint_ops;
264f9890 13635 locspec = string_to_location_spec (&arg, current_language);
7b572efb 13636 type = bp_static_tracepoint;
f00aae0f 13637 }
983af33b 13638
558a9d82 13639 create_breakpoint (get_current_arch (),
264f9890 13640 locspec.get (),
10a636cc 13641 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82 13642 0 /* tempflag */,
7b572efb 13643 type /* type_wanted */,
558a9d82
YQ
13644 0 /* Ignore count */,
13645 pending_break_support,
13646 ops,
13647 from_tty,
13648 1 /* enabled */,
13649 0 /* internal */, 0);
7a697b8d
SS
13650}
13651
409873ef
SS
13652/* Set up a fake reader function that gets command lines from a linked
13653 list that was acquired during tracepoint uploading. */
13654
13655static struct uploaded_tp *this_utp;
3149d8c1 13656static int next_cmd;
409873ef
SS
13657
13658static char *
13659read_uploaded_action (void)
13660{
a18ba4e4 13661 char *rslt = nullptr;
409873ef 13662
a18ba4e4
SM
13663 if (next_cmd < this_utp->cmd_strings.size ())
13664 {
67aa1f3c 13665 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
13666 next_cmd++;
13667 }
409873ef
SS
13668
13669 return rslt;
13670}
13671
00bf0b85
SS
13672/* Given information about a tracepoint as recorded on a target (which
13673 can be either a live system or a trace file), attempt to create an
13674 equivalent GDB tracepoint. This is not a reliable process, since
13675 the target does not necessarily have all the information used when
13676 the tracepoint was originally defined. */
13677
d9b3f62e 13678struct tracepoint *
00bf0b85 13679create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 13680{
f2fc3015
TT
13681 const char *addr_str;
13682 char small_buf[100];
d9b3f62e 13683 struct tracepoint *tp;
fd9b8c24 13684
409873ef 13685 if (utp->at_string)
67aa1f3c 13686 addr_str = utp->at_string.get ();
409873ef
SS
13687 else
13688 {
13689 /* In the absence of a source location, fall back to raw
13690 address. Since there is no way to confirm that the address
13691 means the same thing as when the trace was started, warn the
13692 user. */
3e43a32a
MS
13693 warning (_("Uploaded tracepoint %d has no "
13694 "source location, using raw address"),
409873ef 13695 utp->number);
8c042590 13696 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
13697 addr_str = small_buf;
13698 }
13699
13700 /* There's not much we can do with a sequence of bytecodes. */
13701 if (utp->cond && !utp->cond_string)
3e43a32a
MS
13702 warning (_("Uploaded tracepoint %d condition "
13703 "has no source form, ignoring it"),
409873ef 13704 utp->number);
d5551862 13705
264f9890
PA
13706 location_spec_up locspec = string_to_location_spec (&addr_str,
13707 current_language);
8cdf0e15 13708 if (!create_breakpoint (get_current_arch (),
264f9890 13709 locspec.get (),
67aa1f3c 13710 utp->cond_string.get (), -1, addr_str,
10a636cc 13711 false /* force_condition */,
e7e0cddf 13712 0 /* parse cond/thread */,
8cdf0e15 13713 0 /* tempflag */,
0fb4aa4b 13714 utp->type /* type_wanted */,
8cdf0e15
VP
13715 0 /* Ignore count */,
13716 pending_break_support,
74421c0b 13717 &code_breakpoint_ops,
8cdf0e15 13718 0 /* from_tty */,
84f4c1fe 13719 utp->enabled /* enabled */,
44f238bb
PA
13720 0 /* internal */,
13721 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 13722 return NULL;
fd9b8c24 13723
409873ef 13724 /* Get the tracepoint we just created. */
fd9b8c24
PA
13725 tp = get_tracepoint (tracepoint_count);
13726 gdb_assert (tp != NULL);
d5551862 13727
00bf0b85
SS
13728 if (utp->pass > 0)
13729 {
8c042590 13730 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 13731 tp->number);
00bf0b85 13732
409873ef 13733 trace_pass_command (small_buf, 0);
00bf0b85
SS
13734 }
13735
409873ef
SS
13736 /* If we have uploaded versions of the original commands, set up a
13737 special-purpose "reader" function and call the usual command line
13738 reader, then pass the result to the breakpoint command-setting
13739 function. */
a18ba4e4 13740 if (!utp->cmd_strings.empty ())
00bf0b85 13741 {
12973681 13742 counted_command_line cmd_list;
00bf0b85 13743
409873ef 13744 this_utp = utp;
3149d8c1 13745 next_cmd = 0;
d5551862 13746
60b3cef2 13747 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 13748
c1fc2657 13749 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 13750 }
a18ba4e4
SM
13751 else if (!utp->actions.empty ()
13752 || !utp->step_actions.empty ())
3e43a32a
MS
13753 warning (_("Uploaded tracepoint %d actions "
13754 "have no source form, ignoring them"),
409873ef 13755 utp->number);
00bf0b85 13756
f196051f 13757 /* Copy any status information that might be available. */
c1fc2657 13758 tp->hit_count = utp->hit_count;
f196051f
SS
13759 tp->traceframe_usage = utp->traceframe_usage;
13760
00bf0b85 13761 return tp;
d9b3f62e 13762}
00bf0b85 13763
1042e4c0
SS
13764/* Print information on tracepoint number TPNUM_EXP, or all if
13765 omitted. */
13766
13767static void
1d12d88f 13768info_tracepoints_command (const char *args, int from_tty)
1042e4c0 13769{
79a45e25 13770 struct ui_out *uiout = current_uiout;
e5a67952 13771 int num_printed;
1042e4c0 13772
5c458ae8 13773 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
13774
13775 if (num_printed == 0)
1042e4c0 13776 {
e5a67952 13777 if (args == NULL || *args == '\0')
112e8700 13778 uiout->message ("No tracepoints.\n");
d77f58be 13779 else
112e8700 13780 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 13781 }
ad443146
SS
13782
13783 default_collect_info ();
1042e4c0
SS
13784}
13785
4a64f543 13786/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
13787 Not supported by all targets. */
13788static void
5fed81ff 13789enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13790{
13791 enable_command (args, from_tty);
13792}
13793
4a64f543 13794/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
13795 Not supported by all targets. */
13796static void
5fed81ff 13797disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
13798{
13799 disable_command (args, from_tty);
13800}
13801
4a64f543 13802/* Remove a tracepoint (or all if no argument). */
1042e4c0 13803static void
4495129a 13804delete_trace_command (const char *arg, int from_tty)
1042e4c0 13805{
1042e4c0
SS
13806 dont_repeat ();
13807
13808 if (arg == 0)
13809 {
13810 int breaks_to_delete = 0;
13811
13812 /* Delete all breakpoints if no argument.
dda83cd7
SM
13813 Do not delete internal or call-dummy breakpoints, these
13814 have to be deleted with an explicit breakpoint number
4a64f543 13815 argument. */
f6d17b2b 13816 for (breakpoint *tp : all_tracepoints ())
1428b37a 13817 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
13818 {
13819 breaks_to_delete = 1;
13820 break;
13821 }
1042e4c0
SS
13822
13823 /* Ask user only if there are some breakpoints to delete. */
13824 if (!from_tty
13825 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13826 {
1428b37a 13827 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 13828 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 13829 delete_breakpoint (b);
1042e4c0
SS
13830 }
13831 }
13832 else
48649e1b 13833 map_breakpoint_numbers
b926417a 13834 (arg, [&] (breakpoint *br)
48649e1b 13835 {
b926417a 13836 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13837 });
1042e4c0
SS
13838}
13839
197f0a60
TT
13840/* Helper function for trace_pass_command. */
13841
13842static void
d9b3f62e 13843trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 13844{
d9b3f62e 13845 tp->pass_count = count;
76727919 13846 gdb::observers::breakpoint_modified.notify (tp);
197f0a60 13847 if (from_tty)
6cb06a8c
TT
13848 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13849 tp->number, count);
197f0a60
TT
13850}
13851
1042e4c0
SS
13852/* Set passcount for tracepoint.
13853
13854 First command argument is passcount, second is tracepoint number.
13855 If tracepoint number omitted, apply to most recently defined.
13856 Also accepts special argument "all". */
13857
13858static void
0b39b52e 13859trace_pass_command (const char *args, int from_tty)
1042e4c0 13860{
d9b3f62e 13861 struct tracepoint *t1;
0b39b52e 13862 ULONGEST count;
1042e4c0
SS
13863
13864 if (args == 0 || *args == 0)
3e43a32a
MS
13865 error (_("passcount command requires an "
13866 "argument (count + optional TP num)"));
1042e4c0 13867
0b39b52e 13868 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 13869
529480d0 13870 args = skip_spaces (args);
1042e4c0
SS
13871 if (*args && strncasecmp (args, "all", 3) == 0)
13872 {
13873 args += 3; /* Skip special argument "all". */
1042e4c0
SS
13874 if (*args)
13875 error (_("Junk at end of arguments."));
1042e4c0 13876
f6d17b2b 13877 for (breakpoint *b : all_tracepoints ())
01add95b
SM
13878 {
13879 t1 = (struct tracepoint *) b;
13880 trace_pass_set_count (t1, count, from_tty);
13881 }
197f0a60
TT
13882 }
13883 else if (*args == '\0')
1042e4c0 13884 {
5fa1d40e 13885 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 13886 if (t1)
197f0a60
TT
13887 trace_pass_set_count (t1, count, from_tty);
13888 }
13889 else
13890 {
bfd28288
PA
13891 number_or_range_parser parser (args);
13892 while (!parser.finished ())
1042e4c0 13893 {
bfd28288 13894 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
13895 if (t1)
13896 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
13897 }
13898 }
1042e4c0
SS
13899}
13900
d9b3f62e 13901struct tracepoint *
1042e4c0
SS
13902get_tracepoint (int num)
13903{
f6d17b2b 13904 for (breakpoint *t : all_tracepoints ())
1042e4c0 13905 if (t->number == num)
d9b3f62e 13906 return (struct tracepoint *) t;
1042e4c0
SS
13907
13908 return NULL;
13909}
13910
d5551862
SS
13911/* Find the tracepoint with the given target-side number (which may be
13912 different from the tracepoint number after disconnecting and
13913 reconnecting). */
13914
d9b3f62e 13915struct tracepoint *
d5551862
SS
13916get_tracepoint_by_number_on_target (int num)
13917{
f6d17b2b 13918 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
13919 {
13920 struct tracepoint *t = (struct tracepoint *) b;
13921
13922 if (t->number_on_target == num)
13923 return t;
13924 }
d5551862
SS
13925
13926 return NULL;
13927}
13928
1042e4c0 13929/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 13930 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
13931 If the argument is missing, the most recent tracepoint
13932 (tracepoint_count) is returned. */
13933
d9b3f62e 13934struct tracepoint *
0b39b52e 13935get_tracepoint_by_number (const char **arg,
bfd28288 13936 number_or_range_parser *parser)
1042e4c0 13937{
1042e4c0 13938 int tpnum;
0b39b52e 13939 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 13940
bfd28288 13941 if (parser != NULL)
197f0a60 13942 {
bfd28288
PA
13943 gdb_assert (!parser->finished ());
13944 tpnum = parser->get_number ();
197f0a60
TT
13945 }
13946 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 13947 tpnum = tracepoint_count;
1042e4c0 13948 else
197f0a60 13949 tpnum = get_number (arg);
1042e4c0
SS
13950
13951 if (tpnum <= 0)
13952 {
13953 if (instring && *instring)
6cb06a8c
TT
13954 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
13955 instring);
1042e4c0 13956 else
6cb06a8c 13957 gdb_printf (_("No previous tracepoint\n"));
1042e4c0
SS
13958 return NULL;
13959 }
13960
f6d17b2b 13961 for (breakpoint *t : all_tracepoints ())
1042e4c0 13962 if (t->number == tpnum)
d9b3f62e 13963 return (struct tracepoint *) t;
1042e4c0 13964
6cb06a8c 13965 gdb_printf ("No tracepoint number %d.\n", tpnum);
1042e4c0
SS
13966 return NULL;
13967}
13968
d9b3f62e 13969void
04d0163c 13970breakpoint::print_recreate_thread (struct ui_file *fp) const
d9b3f62e 13971{
04d0163c
TT
13972 if (thread != -1)
13973 gdb_printf (fp, " thread %d", thread);
d9b3f62e 13974
04d0163c
TT
13975 if (task != 0)
13976 gdb_printf (fp, " task %d", task);
d9b3f62e 13977
6cb06a8c 13978 gdb_printf (fp, "\n");
d9b3f62e
PA
13979}
13980
6149aea9
PA
13981/* Save information on user settable breakpoints (watchpoints, etc) to
13982 a new script file named FILENAME. If FILTER is non-NULL, call it
13983 on each breakpoint and only include the ones for which it returns
f2478a7e 13984 true. */
6149aea9 13985
1042e4c0 13986static void
4495129a 13987save_breakpoints (const char *filename, int from_tty,
f2478a7e 13988 bool (*filter) (const struct breakpoint *))
1042e4c0 13989{
6149aea9 13990 int any = 0;
6149aea9 13991 int extra_trace_bits = 0;
1042e4c0 13992
6149aea9
PA
13993 if (filename == 0 || *filename == 0)
13994 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13995
13996 /* See if we have anything to save. */
43892fdf 13997 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
13998 {
13999 /* Skip internal and momentary breakpoints. */
14000 if (!user_breakpoint_p (tp))
14001 continue;
6149aea9 14002
01add95b
SM
14003 /* If we have a filter, only save the breakpoints it accepts. */
14004 if (filter && !filter (tp))
14005 continue;
6149aea9 14006
01add95b 14007 any = 1;
6149aea9 14008
01add95b
SM
14009 if (is_tracepoint (tp))
14010 {
14011 extra_trace_bits = 1;
6149aea9 14012
01add95b
SM
14013 /* We can stop searching. */
14014 break;
14015 }
14016 }
6149aea9
PA
14017
14018 if (!any)
1042e4c0 14019 {
6149aea9 14020 warning (_("Nothing to save."));
1042e4c0
SS
14021 return;
14022 }
14023
ee0c3293 14024 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14025
14026 stdio_file fp;
14027
ee0c3293 14028 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14029 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14030 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14031
6149aea9 14032 if (extra_trace_bits)
d7e74731 14033 save_trace_state_variables (&fp);
8bf6485c 14034
43892fdf 14035 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
14036 {
14037 /* Skip internal and momentary breakpoints. */
14038 if (!user_breakpoint_p (tp))
14039 continue;
8bf6485c 14040
01add95b
SM
14041 /* If we have a filter, only save the breakpoints it accepts. */
14042 if (filter && !filter (tp))
14043 continue;
6149aea9 14044
c47614fe 14045 tp->print_recreate (&fp);
1042e4c0 14046
01add95b
SM
14047 /* Note, we can't rely on tp->number for anything, as we can't
14048 assume the recreated breakpoint numbers will match. Use $bpnum
14049 instead. */
6149aea9 14050
01add95b 14051 if (tp->cond_string)
6f781ee3 14052 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
6149aea9 14053
01add95b
SM
14054 if (tp->ignore_count)
14055 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 14056
01add95b
SM
14057 if (tp->type != bp_dprintf && tp->commands)
14058 {
14059 fp.puts (" commands\n");
1042e4c0 14060
992aeed8
TT
14061 ui_out_redirect_pop redir (current_uiout, &fp);
14062 print_command_lines (current_uiout, tp->commands.get (), 2);
6149aea9 14063
01add95b
SM
14064 fp.puts (" end\n");
14065 }
6149aea9 14066
01add95b
SM
14067 if (tp->enable_state == bp_disabled)
14068 fp.puts ("disable $bpnum\n");
6149aea9 14069
01add95b
SM
14070 /* If this is a multi-location breakpoint, check if the locations
14071 should be individually disabled. Watchpoint locations are
14072 special, and not user visible. */
14073 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14074 {
14075 int n = 1;
40cb8ca5 14076
01add95b
SM
14077 for (bp_location *loc : tp->locations ())
14078 {
14079 if (!loc->enabled)
14080 fp.printf ("disable $bpnum.%d\n", n);
14081
14082 n++;
14083 }
14084 }
14085 }
8bf6485c 14086
e0700ba4
SM
14087 if (extra_trace_bits && !default_collect.empty ())
14088 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14089
1042e4c0 14090 if (from_tty)
6cb06a8c 14091 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14092}
14093
14094/* The `save breakpoints' command. */
14095
14096static void
4495129a 14097save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14098{
14099 save_breakpoints (args, from_tty, NULL);
14100}
14101
14102/* The `save tracepoints' command. */
14103
14104static void
4495129a 14105save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14106{
14107 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14108}
14109
c906108c 14110\f
629500fa
KS
14111/* This help string is used to consolidate all the help string for specifying
14112 locations used by several commands. */
14113
264f9890 14114#define LOCATION_SPEC_HELP_STRING \
629500fa
KS
14115"Linespecs are colon-separated lists of location parameters, such as\n\
14116source filename, function name, label name, and line number.\n\
14117Example: To specify the start of a label named \"the_top\" in the\n\
14118function \"fact\" in the file \"factorial.c\", use\n\
14119\"factorial.c:fact:the_top\".\n\
14120\n\
14121Address locations begin with \"*\" and specify an exact address in the\n\
14122program. Example: To specify the fourth byte past the start function\n\
14123\"main\", use \"*main + 4\".\n\
14124\n\
14125Explicit locations are similar to linespecs but use an option/argument\n\
14126syntax to specify location parameters.\n\
14127Example: To specify the start of the label named \"the_top\" in the\n\
14128function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14129-function fact -label the_top\".\n\
14130\n\
14131By default, a specified function is matched against the program's\n\
14132functions in all scopes. For C++, this means in all namespaces and\n\
14133classes. For Ada, this means in all packages. E.g., in C++,\n\
14134\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14135\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14136specified name as a complete fully-qualified name instead."
629500fa 14137
4a64f543
MS
14138/* This help string is used for the break, hbreak, tbreak and thbreak
14139 commands. It is defined as a macro to prevent duplication.
14140 COMMAND should be a string constant containing the name of the
14141 command. */
629500fa 14142
31e2b00f 14143#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14144command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14145\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14146PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14147probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
14148guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14149`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14150LOCATION may be a linespec, address, or explicit location as described\n\
14151below.\n\
14152\n\
dc10affe
PA
14153With no LOCATION, uses current execution address of the selected\n\
14154stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14155\n\
14156THREADNUM is the number from \"info threads\".\n\
14157CONDITION is a boolean expression.\n\
733d554a
TBA
14158\n\
14159With the \"-force-condition\" flag, the condition is defined even when\n\
14160it is invalid for all current locations.\n\
264f9890 14161\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14162Multiple breakpoints at one place are permitted, and useful if their\n\
14163conditions are different.\n\
31e2b00f
AS
14164\n\
14165Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14166
44feb3ce
TT
14167/* List of subcommands for "catch". */
14168static struct cmd_list_element *catch_cmdlist;
14169
14170/* List of subcommands for "tcatch". */
14171static struct cmd_list_element *tcatch_cmdlist;
14172
9ac4176b 14173void
a121b7c1 14174add_catch_command (const char *name, const char *docstring,
5538b03c 14175 cmd_func_ftype *func,
625e8578 14176 completer_ftype *completer,
44feb3ce
TT
14177 void *user_data_catch,
14178 void *user_data_tcatch)
14179{
14180 struct cmd_list_element *command;
14181
0450cc4c 14182 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14183 &catch_cmdlist);
5538b03c 14184 command->func = func;
0f8e2034 14185 command->set_context (user_data_catch);
a96d9b2e 14186 set_cmd_completer (command, completer);
44feb3ce 14187
0450cc4c 14188 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14189 &tcatch_cmdlist);
5538b03c 14190 command->func = func;
0f8e2034 14191 command->set_context (user_data_tcatch);
a96d9b2e 14192 set_cmd_completer (command, completer);
44feb3ce
TT
14193}
14194
0574c78f
GB
14195/* Zero if any of the breakpoint's locations could be a location where
14196 functions have been inlined, nonzero otherwise. */
14197
14198static int
14199is_non_inline_function (struct breakpoint *b)
14200{
14201 /* The shared library event breakpoint is set on the address of a
14202 non-inline function. */
14203 if (b->type == bp_shlib_event)
14204 return 1;
14205
14206 return 0;
14207}
14208
14209/* Nonzero if the specified PC cannot be a location where functions
14210 have been inlined. */
14211
14212int
accd0bcd 14213pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14214 const target_waitstatus &ws)
0574c78f 14215{
43892fdf 14216 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
14217 {
14218 if (!is_non_inline_function (b))
14219 continue;
14220
40cb8ca5 14221 for (bp_location *bl : b->locations ())
0574c78f
GB
14222 {
14223 if (!bl->shlib_disabled
09ac7c10 14224 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
14225 return 1;
14226 }
14227 }
14228
14229 return 0;
14230}
14231
2f202fde
JK
14232/* Remove any references to OBJFILE which is going to be freed. */
14233
14234void
14235breakpoint_free_objfile (struct objfile *objfile)
14236{
48d7020b 14237 for (bp_location *loc : all_bp_locations ())
3c86fae3 14238 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
2f202fde
JK
14239 loc->symtab = NULL;
14240}
14241
8bfd80db
YQ
14242/* Chain containing all defined "enable breakpoint" subcommands. */
14243
14244static struct cmd_list_element *enablebreaklist = NULL;
14245
8588b356
SM
14246/* See breakpoint.h. */
14247
14248cmd_list_element *commands_cmd_element = nullptr;
14249
6c265988 14250void _initialize_breakpoint ();
c906108c 14251void
6c265988 14252_initialize_breakpoint ()
c906108c
SS
14253{
14254 struct cmd_list_element *c;
14255
c90e7d63
SM
14256 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14257 "breakpoint");
14258 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14259 "breakpoint");
14260 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14261 "breakpoint");
84acb35a 14262
c906108c
SS
14263 breakpoint_chain = 0;
14264 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14265 before a breakpoint is set. */
14266 breakpoint_count = 0;
14267
1042e4c0
SS
14268 tracepoint_count = 0;
14269
1bedd215
AC
14270 add_com ("ignore", class_breakpoint, ignore_command, _("\
14271Set ignore-count of breakpoint number N to COUNT.\n\
14272Usage is `ignore N COUNT'."));
c906108c 14273
8588b356
SM
14274 commands_cmd_element = add_com ("commands", class_breakpoint,
14275 commands_command, _("\
18da0c51
MG
14276Set commands to be executed when the given breakpoints are hit.\n\
14277Give a space-separated breakpoint list as argument after \"commands\".\n\
14278A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14279(e.g. `5-7').\n\
c906108c
SS
14280With no argument, the targeted breakpoint is the last one set.\n\
14281The commands themselves follow starting on the next line.\n\
14282Type a line containing \"end\" to indicate the end of them.\n\
14283Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14284then no output is printed when it is hit, except what the commands print."));
c906108c 14285
b1d4d8d1
TBA
14286 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14287 static std::string condition_command_help
14288 = gdb::option::build_help (_("\
1bedd215 14289Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14290Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14291is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14292\n\
14293Options:\n\
14294%OPTIONS%"), cc_opts);
14295
14296 c = add_com ("condition", class_breakpoint, condition_command,
14297 condition_command_help.c_str ());
14298 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14299
1bedd215 14300 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14301Set a temporary breakpoint.\n\
c906108c
SS
14302Like \"break\" except the breakpoint is only temporary,\n\
14303so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14304by using \"enable delete\" on the breakpoint number.\n\
14305\n"
14306BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14307 set_cmd_completer (c, location_completer);
c94fdfd0 14308
1bedd215 14309 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14310Set a hardware assisted breakpoint.\n\
c906108c 14311Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14312some target hardware may not have this support.\n\
14313\n"
14314BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14315 set_cmd_completer (c, location_completer);
c906108c 14316
1bedd215 14317 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14318Set a temporary hardware assisted breakpoint.\n\
c906108c 14319Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14320so it will be deleted when hit.\n\
14321\n"
14322BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14323 set_cmd_completer (c, location_completer);
c906108c 14324
3947f654
SM
14325 cmd_list_element *enable_cmd
14326 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14327Enable all or some breakpoints.\n\
14328Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14329Give breakpoint numbers (separated by spaces) as arguments.\n\
14330With no subcommand, breakpoints are enabled until you command otherwise.\n\
14331This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14332With a subcommand you can enable temporarily."),
3947f654 14333 &enablelist, 1, &cmdlist);
c906108c 14334
3947f654 14335 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14336
84951ab5 14337 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14338Enable all or some breakpoints.\n\
14339Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14340Give breakpoint numbers (separated by spaces) as arguments.\n\
14341This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14342May be abbreviated to simply \"enable\"."),
2f822da5 14343 &enablebreaklist, 1, &enablelist);
c906108c 14344
1a966eab 14345 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14346Enable some breakpoints for one hit.\n\
14347Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14348If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14349 &enablebreaklist);
14350
1a966eab 14351 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14352Enable some breakpoints and delete when hit.\n\
14353Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14354If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14355 &enablebreaklist);
14356
816338b5 14357 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14358Enable some breakpoints for COUNT hits.\n\
14359Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14360If a breakpoint is hit while enabled in this fashion,\n\
14361the count is decremented; when it reaches zero, the breakpoint is disabled."),
14362 &enablebreaklist);
14363
1a966eab 14364 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14365Enable some breakpoints and delete when hit.\n\
14366Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14367If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14368 &enablelist);
14369
1a966eab 14370 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14371Enable some breakpoints for one hit.\n\
14372Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14373If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14374 &enablelist);
14375
14376 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14377Enable some breakpoints for COUNT hits.\n\
14378Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14379If a breakpoint is hit while enabled in this fashion,\n\
14380the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14381 &enablelist);
14382
3947f654
SM
14383 cmd_list_element *disable_cmd
14384 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14385Disable all or some breakpoints.\n\
14386Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14387Arguments are breakpoint numbers with spaces in between.\n\
14388To disable all breakpoints, give no argument.\n\
64b9b334 14389A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14390 &disablelist, 1, &cmdlist);
14391 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14392 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14393
57b4f16e 14394 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14395Disable all or some breakpoints.\n\
14396Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14397Arguments are breakpoint numbers with spaces in between.\n\
14398To disable all breakpoints, give no argument.\n\
64b9b334 14399A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14400This command may be abbreviated \"disable\"."),
c906108c
SS
14401 &disablelist);
14402
3947f654
SM
14403 cmd_list_element *delete_cmd
14404 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14405Delete all or some breakpoints.\n\
14406Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
14407Arguments are breakpoint numbers with spaces in between.\n\
14408To delete all breakpoints, give no argument.\n\
14409\n\
590042fc 14410Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14411 &deletelist, 1, &cmdlist);
14412 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14413 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14414
57b4f16e 14415 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14416Delete all or some breakpoints or auto-display expressions.\n\
14417Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14418Arguments are breakpoint numbers with spaces in between.\n\
14419To delete all breakpoints, give no argument.\n\
1a966eab 14420This command may be abbreviated \"delete\"."),
c906108c
SS
14421 &deletelist);
14422
3947f654
SM
14423 cmd_list_element *clear_cmd
14424 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14425Clear breakpoint at specified location.\n\
14426Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14427\n\
14428With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14429is executing in.\n"
264f9890 14430"\n" LOCATION_SPEC_HELP_STRING "\n\n\
1bedd215 14431See also the \"delete\" command which clears breakpoints by number."));
3947f654 14432 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14433
3947f654
SM
14434 cmd_list_element *break_cmd
14435 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14436Set breakpoint at specified location.\n"
31e2b00f 14437BREAK_ARGS_HELP ("break")));
3947f654 14438 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14439
3947f654
SM
14440 add_com_alias ("b", break_cmd, class_run, 1);
14441 add_com_alias ("br", break_cmd, class_run, 1);
14442 add_com_alias ("bre", break_cmd, class_run, 1);
14443 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14444
e0f25bd9
SM
14445 cmd_list_element *info_breakpoints_cmd
14446 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14447Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14448The \"Type\" column indicates one of:\n\
14449\tbreakpoint - normal breakpoint\n\
14450\twatchpoint - watchpoint\n\
14451The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14452the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14453breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14454address and file/line number respectively.\n\
14455\n\
14456Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14457are set to the address of the last breakpoint listed unless the command\n\
14458is prefixed with \"server \".\n\n\
c906108c 14459Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14460breakpoint set."));
c906108c 14461
e0f25bd9 14462 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14463
1a966eab
AC
14464 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14465Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14466The \"Type\" column indicates one of:\n\
14467\tbreakpoint - normal breakpoint\n\
14468\twatchpoint - watchpoint\n\
14469\tlongjmp - internal breakpoint used to step through longjmp()\n\
14470\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14471\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14472\tfinish - internal breakpoint used by the \"finish\" command\n\
14473The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14474the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14475breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14476address and file/line number respectively.\n\
14477\n\
14478Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14479are set to the address of the last breakpoint listed unless the command\n\
14480is prefixed with \"server \".\n\n\
c906108c 14481Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14482breakpoint set."),
c906108c
SS
14483 &maintenanceinfolist);
14484
3b6acaee 14485 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 14486Set catchpoints to catch events."),
2f822da5 14487 &catch_cmdlist,
3b6acaee 14488 0/*allow-unknown*/, &cmdlist);
44feb3ce 14489
3b6acaee 14490 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 14491Set temporary catchpoints to catch events."),
2f822da5 14492 &tcatch_cmdlist,
3b6acaee 14493 0/*allow-unknown*/, &cmdlist);
44feb3ce 14494
a15a5258
AB
14495 const auto opts = make_watch_options_def_group (nullptr);
14496
14497 static const std::string watch_help = gdb::option::build_help (_("\
14498Set a watchpoint for EXPRESSION.\n\
14499Usage: watch [-location] EXPRESSION\n\
14500\n\
14501Options:\n\
14502%OPTIONS%\n\
14503\n\
c906108c 14504A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
14505an expression changes."), opts);
14506 c = add_com ("watch", class_breakpoint, watch_command,
14507 watch_help.c_str ());
14508 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14509
14510 static const std::string rwatch_help = gdb::option::build_help (_("\
14511Set a read watchpoint for EXPRESSION.\n\
14512Usage: rwatch [-location] EXPRESSION\n\
14513\n\
14514Options:\n\
14515%OPTIONS%\n\
14516\n\
14517A read watchpoint stops execution of your program whenever the value of\n\
14518an expression is read."), opts);
14519 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14520 rwatch_help.c_str ());
14521 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14522
14523 static const std::string awatch_help = gdb::option::build_help (_("\
14524Set an access watchpoint for EXPRESSION.\n\
14525Usage: awatch [-location] EXPRESSION\n\
14526\n\
14527Options:\n\
14528%OPTIONS%\n\
14529\n\
14530An access watchpoint stops execution of your program whenever the value\n\
14531of an expression is either read or written."), opts);
14532 c = add_com ("awatch", class_breakpoint, awatch_command,
14533 awatch_help.c_str ());
14534 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 14535
11db9430 14536 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 14537Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 14538
920d2a44
AC
14539 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14540 respond to changes - contrary to the description. */
85c07804
AC
14541 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14542 &can_use_hw_watchpoints, _("\
14543Set debugger's willingness to use watchpoint hardware."), _("\
14544Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
14545If zero, gdb will not use hardware for new watchpoints, even if\n\
14546such is available. (However, any hardware watchpoints that were\n\
14547created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
14548hardware.)"),
14549 NULL,
920d2a44 14550 show_can_use_hw_watchpoints,
85c07804 14551 &setlist, &showlist);
c906108c
SS
14552
14553 can_use_hw_watchpoints = 1;
fa8d40ab 14554
1042e4c0
SS
14555 /* Tracepoint manipulation commands. */
14556
3947f654
SM
14557 cmd_list_element *trace_cmd
14558 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 14559Set a tracepoint at specified location.\n\
1042e4c0
SS
14560\n"
14561BREAK_ARGS_HELP ("trace") "\n\
14562Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 14563 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 14564
3947f654
SM
14565 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14566 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14567 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14568 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 14569
7a697b8d 14570 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 14571Set a fast tracepoint at specified location.\n\
7a697b8d
SS
14572\n"
14573BREAK_ARGS_HELP ("ftrace") "\n\
14574Do \"help tracepoints\" for info on other tracepoint commands."));
14575 set_cmd_completer (c, location_completer);
14576
0fb4aa4b 14577 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 14578Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
14579\n\
14580strace [LOCATION] [if CONDITION]\n\
629500fa
KS
14581LOCATION may be a linespec, explicit, or address location (described below) \n\
14582or -m MARKER_ID.\n\n\
14583If a marker id is specified, probe the marker with that name. With\n\
14584no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
14585Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14586This collects arbitrary user data passed in the probe point call to the\n\
14587tracing library. You can inspect it when analyzing the trace buffer,\n\
14588by printing the $_sdata variable like any other convenience variable.\n\
14589\n\
14590CONDITION is a boolean expression.\n\
264f9890 14591\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14592Multiple tracepoints at one place are permitted, and useful if their\n\
14593conditions are different.\n\
0fb4aa4b
PA
14594\n\
14595Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14596Do \"help tracepoints\" for info on other tracepoint commands."));
14597 set_cmd_completer (c, location_completer);
14598
e0f25bd9
SM
14599 cmd_list_element *info_tracepoints_cmd
14600 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 14601Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
14602Convenience variable \"$tpnum\" contains the number of the\n\
14603last tracepoint set."));
14604
e0f25bd9 14605 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 14606
5e84b7ee
SM
14607 cmd_list_element *delete_tracepoints_cmd
14608 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
14609Delete specified tracepoints.\n\
14610Arguments are tracepoint numbers, separated by spaces.\n\
14611No argument means delete all tracepoints."),
14612 &deletelist);
5e84b7ee 14613 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
14614
14615 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14616Disable specified tracepoints.\n\
14617Arguments are tracepoint numbers, separated by spaces.\n\
14618No argument means disable all tracepoints."),
14619 &disablelist);
14620 deprecate_cmd (c, "disable");
14621
14622 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14623Enable specified tracepoints.\n\
14624Arguments are tracepoint numbers, separated by spaces.\n\
14625No argument means enable all tracepoints."),
14626 &enablelist);
14627 deprecate_cmd (c, "enable");
14628
14629 add_com ("passcount", class_trace, trace_pass_command, _("\
14630Set the passcount for a tracepoint.\n\
14631The trace will end when the tracepoint has been passed 'count' times.\n\
14632Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14633if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14634
0743fc83
TT
14635 add_basic_prefix_cmd ("save", class_breakpoint,
14636 _("Save breakpoint definitions as a script."),
2f822da5 14637 &save_cmdlist,
0743fc83 14638 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
14639
14640 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14641Save current breakpoint definitions as a script.\n\
cce7e648 14642This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
14643catchpoints, tracepoints). Use the 'source' command in another debug\n\
14644session to restore them."),
14645 &save_cmdlist);
14646 set_cmd_completer (c, filename_completer);
14647
3947f654
SM
14648 cmd_list_element *save_tracepoints_cmd
14649 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 14650Save current tracepoint definitions as a script.\n\
6149aea9
PA
14651Use the 'source' command in another debug session to restore them."),
14652 &save_cmdlist);
3947f654 14653 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 14654
3947f654 14655 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
14656 deprecate_cmd (c, "save tracepoints");
14657
f54bdb6d
SM
14658 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14659 _("\
590042fc 14660Breakpoint specific settings.\n\
fa8d40ab 14661Configure various breakpoint-specific variables such as\n\
590042fc 14662pending breakpoint behavior."),
f54bdb6d 14663 _("\
590042fc 14664Breakpoint specific settings.\n\
fa8d40ab 14665Configure various breakpoint-specific variables such as\n\
590042fc 14666pending breakpoint behavior."),
f54bdb6d
SM
14667 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14668 &setlist, &showlist);
fa8d40ab 14669
7915a72c
AC
14670 add_setshow_auto_boolean_cmd ("pending", no_class,
14671 &pending_break_support, _("\
14672Set debugger's behavior regarding pending breakpoints."), _("\
14673Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
14674If on, an unrecognized breakpoint location will cause gdb to create a\n\
14675pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14676an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 14677user-query to see if a pending breakpoint should be created."),
2c5b56ce 14678 NULL,
920d2a44 14679 show_pending_break_support,
6e1d7d6c
AC
14680 &breakpoint_set_cmdlist,
14681 &breakpoint_show_cmdlist);
fa8d40ab
JJ
14682
14683 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
14684
14685 add_setshow_boolean_cmd ("auto-hw", no_class,
14686 &automatic_hardware_breakpoints, _("\
14687Set automatic usage of hardware breakpoints."), _("\
14688Show automatic usage of hardware breakpoints."), _("\
14689If set, the debugger will automatically use hardware breakpoints for\n\
14690breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14691a warning will be emitted for such breakpoints."),
14692 NULL,
14693 show_automatic_hardware_breakpoints,
14694 &breakpoint_set_cmdlist,
14695 &breakpoint_show_cmdlist);
74960c60 14696
a25a5a45
PA
14697 add_setshow_boolean_cmd ("always-inserted", class_support,
14698 &always_inserted_mode, _("\
74960c60
VP
14699Set mode for inserting breakpoints."), _("\
14700Show mode for inserting breakpoints."), _("\
a25a5a45
PA
14701When this mode is on, breakpoints are inserted immediately as soon as\n\
14702they're created, kept inserted even when execution stops, and removed\n\
14703only when the user deletes them. When this mode is off (the default),\n\
14704breakpoints are inserted only when execution continues, and removed\n\
14705when execution stops."),
72d0e2c5
YQ
14706 NULL,
14707 &show_always_inserted_mode,
14708 &breakpoint_set_cmdlist,
14709 &breakpoint_show_cmdlist);
f1310107 14710
b775012e
LM
14711 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14712 condition_evaluation_enums,
14713 &condition_evaluation_mode_1, _("\
14714Set mode of breakpoint condition evaluation."), _("\
14715Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 14716When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
14717evaluated on the host's side by GDB. When it is set to \"target\",\n\
14718breakpoint conditions will be downloaded to the target (if the target\n\
14719supports such feature) and conditions will be evaluated on the target's side.\n\
14720If this is set to \"auto\" (default), this will be automatically set to\n\
14721\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 14722be set to \"host\"."),
b775012e
LM
14723 &set_condition_evaluation_mode,
14724 &show_condition_evaluation_mode,
14725 &breakpoint_set_cmdlist,
14726 &breakpoint_show_cmdlist);
14727
f1310107
TJB
14728 add_com ("break-range", class_breakpoint, break_range_command, _("\
14729Set a breakpoint for an address range.\n\
14730break-range START-LOCATION, END-LOCATION\n\
14731where START-LOCATION and END-LOCATION can be one of the following:\n\
14732 LINENUM, for that line in the current file,\n\
14733 FILE:LINENUM, for that line in that file,\n\
14734 +OFFSET, for that number of lines after the current line\n\
dda83cd7 14735 or the start of the range\n\
f1310107
TJB
14736 FUNCTION, for the first line in that function,\n\
14737 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14738 *ADDRESS, for the instruction at that address.\n\
14739\n\
14740The breakpoint will stop execution of the inferior whenever it executes\n\
14741an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14742range (including START-LOCATION and END-LOCATION)."));
14743
e7e0cddf 14744 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 14745Set a dynamic printf at specified location.\n\
e7e0cddf 14746dprintf location,format string,arg1,arg2,...\n\
629500fa 14747location may be a linespec, explicit, or address location.\n"
264f9890 14748"\n" LOCATION_SPEC_HELP_STRING));
e7e0cddf
SS
14749 set_cmd_completer (c, location_completer);
14750
14751 add_setshow_enum_cmd ("dprintf-style", class_support,
14752 dprintf_style_enums, &dprintf_style, _("\
14753Set the style of usage for dynamic printf."), _("\
14754Show the style of usage for dynamic printf."), _("\
14755This setting chooses how GDB will do a dynamic printf.\n\
14756If the value is \"gdb\", then the printing is done by GDB to its own\n\
14757console, as with the \"printf\" command.\n\
14758If the value is \"call\", the print is done by calling a function in your\n\
14759program; by default printf(), but you can choose a different function or\n\
14760output stream by setting dprintf-function and dprintf-channel."),
14761 update_dprintf_commands, NULL,
14762 &setlist, &showlist);
14763
e7e0cddf
SS
14764 add_setshow_string_cmd ("dprintf-function", class_support,
14765 &dprintf_function, _("\
590042fc
PW
14766Set the function to use for dynamic printf."), _("\
14767Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
14768 update_dprintf_commands, NULL,
14769 &setlist, &showlist);
14770
e7e0cddf
SS
14771 add_setshow_string_cmd ("dprintf-channel", class_support,
14772 &dprintf_channel, _("\
590042fc
PW
14773Set the channel to use for dynamic printf."), _("\
14774Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
14775 update_dprintf_commands, NULL,
14776 &setlist, &showlist);
14777
d3ce09f5
SS
14778 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14779 &disconnected_dprintf, _("\
14780Set whether dprintf continues after GDB disconnects."), _("\
14781Show whether dprintf continues after GDB disconnects."), _("\
14782Use this to let dprintf commands continue to hit and produce output\n\
14783even if GDB disconnects or detaches from the target."),
14784 NULL,
14785 NULL,
14786 &setlist, &showlist);
14787
14788 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
14789Target agent only formatted printing, like the C \"printf\" function.\n\
14790Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14791This supports most C printf format specifications, like %s, %d, etc.\n\
14792This is useful for formatted output in user-defined commands."));
d3ce09f5 14793
491144b5 14794 automatic_hardware_breakpoints = true;
f3b1572e 14795
c90e7d63
SM
14796 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14797 "breakpoint");
14798 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14799 "breakpoint");
c906108c 14800}