]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
d01e8234 3 Copyright (C) 1986-2025 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 19
d55e5aa6 20#include "arch-utils.h"
4de283e4 21#include <ctype.h>
e5dc0d5d 22#include "event-top.h"
05d9d66d 23#include "exceptions.h"
c8889b91 24#include "gdbsupport/gdb_vecs.h"
e77c31d2 25#include "gdbsupport/unordered_set.h"
4de283e4
TT
26#include "symtab.h"
27#include "frame.h"
c906108c 28#include "breakpoint.h"
4de283e4
TT
29#include "tracepoint.h"
30#include "gdbtypes.h"
c906108c 31#include "expression.h"
d55e5aa6 32#include "gdbcore.h"
5b9707eb 33#include "cli/cli-cmds.h"
4de283e4
TT
34#include "value.h"
35#include "command.h"
c906108c 36#include "inferior.h"
45741a9c 37#include "infrun.h"
4de283e4
TT
38#include "gdbthread.h"
39#include "target.h"
c906108c 40#include "language.h"
4de283e4
TT
41#include "gdb-demangle.h"
42#include "filenames.h"
43#include "annotate.h"
44#include "symfile.h"
d55e5aa6 45#include "objfiles.h"
4de283e4
TT
46#include "source.h"
47#include "linespec.h"
48#include "completer.h"
49#include "ui-out.h"
50#include "cli/cli-script.h"
51#include "block.h"
52#include "solib.h"
d55e5aa6 53#include "observable.h"
4de283e4
TT
54#include "memattr.h"
55#include "ada-lang.h"
56#include "top.h"
13d03262 57#include "ui.h"
4de283e4
TT
58#include "valprint.h"
59#include "jit.h"
65d79d4b 60#include "parser-defs.h"
55aa24fb 61#include "probe.h"
4de283e4 62#include "cli/cli-utils.h"
d55e5aa6 63#include "stack.h"
4de283e4
TT
64#include "ax-gdb.h"
65#include "dummy-frame.h"
66#include "interps.h"
268a13a5 67#include "gdbsupport/format.h"
cfc31633 68#include "thread-fsm.h"
5d5658a1 69#include "tid-parse.h"
4de283e4 70#include "cli/cli-style.h"
0f8e2034 71#include "cli/cli-decode.h"
c6b48675 72#include "break-cond-parse.h"
d3ce09f5 73
1042e4c0 74/* readline include files */
073bbbb0 75#include "readline/tilde.h"
1042e4c0
SS
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
6dddc817 81#include "extension.h"
325fac50 82#include <algorithm>
5ed8105e 83#include "progspace-and-thread.h"
268a13a5 84#include "gdbsupport/array-view.h"
6b09f134 85#include <optional>
3cdc2d7e 86#include "gdbsupport/common-utils.h"
104c1213 87
4a64f543 88/* Prototypes for local functions. */
c906108c 89
896b6bda 90static void map_breakpoint_numbers (const char *,
48649e1b 91 gdb::function_view<void (breakpoint *)>);
c906108c 92
6cce0251
AB
93static void parse_breakpoint_sals (location_spec *locspec,
94 linespec_result *canonical,
95 program_space *search_pspace);
96
97static void breakpoint_re_set_one (breakpoint *b,
98 program_space *filter_pspace);
983af33b 99
eee031e2
TT
100static void create_breakpoints_sal (struct gdbarch *,
101 struct linespec_result *,
102 gdb::unique_xmalloc_ptr<char>,
103 gdb::unique_xmalloc_ptr<char>,
104 enum bptype,
b080fe54 105 enum bpdisp, int, int, int,
eee031e2 106 int,
eee031e2 107 int, int, int, unsigned);
983af33b 108
a6535de1
TT
109static int can_use_hardware_watchpoint
110 (const std::vector<value_ref_ptr> &vals);
c906108c 111
b713485d 112static void mention (const breakpoint *);
c906108c 113
7a3e3265 114static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
c906108c 115
752a2291
PA
116static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
117
06edf0c0
PA
118static struct breakpoint *
119 momentary_breakpoint_from_master (struct breakpoint *orig,
120 enum bptype type,
7ab97995 121 int loc_enabled, int thread);
06edf0c0 122
d8de7963 123static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
76897487 124
a6d9a66e
UW
125static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
126 CORE_ADDR bpaddr,
60270718
AB
127 enum bptype bptype,
128 struct program_space *pspace);
76897487 129
d8de7963
AB
130static bool watchpoint_locations_match (const struct bp_location *loc1,
131 const struct bp_location *loc2);
85d721b8 132
d8de7963
AB
133static bool breakpoint_locations_match (const struct bp_location *loc1,
134 const struct bp_location *loc2,
135 bool sw_hw_bps_match = false);
7f32a4d5 136
d8de7963
AB
137static bool breakpoint_location_address_match (struct bp_location *bl,
138 const struct address_space *aspace,
139 CORE_ADDR addr);
f1310107 140
d8de7963
AB
141static bool breakpoint_location_address_range_overlap (struct bp_location *,
142 const address_space *,
143 CORE_ADDR, int);
d35ae833 144
834c0d03 145static int remove_breakpoint (struct bp_location *);
b2b6a7da 146static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 147
313f3b21 148static enum print_stop_action print_bp_stop_message (bpstat *bs);
c906108c 149
a14ed312 150static int hw_breakpoint_used_count (void);
c906108c 151
a1398e0c
PA
152static int hw_watchpoint_use_count (struct breakpoint *);
153
154static int hw_watchpoint_used_count_others (struct breakpoint *except,
155 enum bptype type,
156 int *other_type_used);
c906108c 157
816338b5
SS
158static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
159 int count);
c906108c 160
f431efe5 161static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 162
264f9890 163static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
5d926615 164 (struct breakpoint *b,
264f9890 165 location_spec *locspec,
5d926615
TT
166 struct program_space *search_pspace);
167
ae2a9665 168static bool bl_address_is_meaningful (const bp_location *loc);
6e14e441 169
d8a77e4c
AB
170static int find_loc_num_by_location (const bp_location *loc);
171
44702360
PA
172/* update_global_location_list's modes of operation wrt to whether to
173 insert locations now. */
174enum ugll_insert_mode
175{
176 /* Don't insert any breakpoint locations into the inferior, only
177 remove already-inserted locations that no longer should be
178 inserted. Functions that delete a breakpoint or breakpoints
179 should specify this mode, so that deleting a breakpoint doesn't
180 have the side effect of inserting the locations of other
181 breakpoints that are marked not-inserted, but should_be_inserted
182 returns true on them.
183
184 This behavior is useful is situations close to tear-down -- e.g.,
185 after an exec, while the target still has execution, but
186 breakpoint shadows of the previous executable image should *NOT*
187 be restored to the new image; or before detaching, where the
188 target still has execution and wants to delete breakpoints from
189 GDB's lists, and all breakpoints had already been removed from
190 the inferior. */
191 UGLL_DONT_INSERT,
192
a25a5a45
PA
193 /* May insert breakpoints iff breakpoints_should_be_inserted_now
194 claims breakpoints should be inserted now. */
04086b45
PA
195 UGLL_MAY_INSERT,
196
a25a5a45
PA
197 /* Insert locations now, irrespective of
198 breakpoints_should_be_inserted_now. E.g., say all threads are
199 stopped right now, and the user did "continue". We need to
200 insert breakpoints _before_ resuming the target, but
201 UGLL_MAY_INSERT wouldn't insert them, because
202 breakpoints_should_be_inserted_now returns false at that point,
203 as no thread is running yet. */
04086b45 204 UGLL_INSERT
44702360
PA
205};
206
3cdc2d7e
MS
207/* Return a textual version of INSERT_MODE. */
208
209static const char *
210ugll_insert_mode_text (ugll_insert_mode insert_mode)
211{
212/* Make sure the compiler warns if a new ugll_insert_mode enumerator is added
213 but not handled here. */
214DIAGNOSTIC_PUSH
215DIAGNOSTIC_ERROR_SWITCH
216 switch (insert_mode)
217 {
218 case UGLL_DONT_INSERT:
219 return "UGLL_DONT_INSERT";
220 case UGLL_MAY_INSERT:
221 return "UGLL_MAY_INSERT";
222 case UGLL_INSERT:
223 return "UGLL_INSERT";
224 }
225DIAGNOSTIC_POP
226
227 gdb_assert_not_reached ("must handle all enum values");
228}
229
230/* Return a textual version of REASON. */
231
232static const char *
233remove_bp_reason_str (remove_bp_reason reason)
234{
235/* Make sure the compiler warns if a new remove_bp_reason enumerator is added
236 but not handled here. */
237DIAGNOSTIC_PUSH
238DIAGNOSTIC_ERROR_SWITCH
239 switch (reason)
240 {
241 case REMOVE_BREAKPOINT:
242 return "regular remove";
243 case DETACH_BREAKPOINT:
244 return "detach";
245 }
246DIAGNOSTIC_POP
247
248 gdb_assert_not_reached ("must handle all enum values");
249}
250
251/* Return a textual version of breakpoint location BL describing number,
252 location and address. */
253
254static std::string
255breakpoint_location_address_str (const bp_location *bl)
256{
831b11ea 257 std::string str = string_printf ("Breakpoint %d (%s) ",
3cdc2d7e 258 bl->owner->number,
831b11ea 259 host_address_to_string (bl));
3cdc2d7e 260
831b11ea
AB
261 if (bl_address_is_meaningful (bl))
262 {
263 gdb_assert (bl->gdbarch != nullptr);
264 str += string_printf ("at address %s",
265 paddress (bl->gdbarch, bl->address));
266
267 std::string loc_string = bl->to_string ();
268 if (!loc_string.empty ())
269 str += string_printf (" %s", loc_string.c_str ());
270 }
271 else
272 str += "with dummy location";
3cdc2d7e
MS
273
274 return str;
275}
276
44702360 277static void update_global_location_list (enum ugll_insert_mode);
a5606eee 278
44702360 279static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 280
74960c60 281static void insert_breakpoint_locations (void);
a5606eee 282
0b39b52e 283static void trace_pass_command (const char *, int);
1042e4c0 284
558a9d82
YQ
285static void set_tracepoint_count (int num);
286
f2478a7e 287static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 288
d8de7963
AB
289/* Return true if B refers to a static tracepoint set by marker ("-m"),
290 zero otherwise. */
983af33b 291
d8de7963 292static bool strace_marker_p (struct breakpoint *b);
0fb4aa4b 293
264f9890
PA
294static void bkpt_probe_create_sals_from_location_spec
295 (location_spec *locspec,
6cce0251
AB
296 struct linespec_result *canonical,
297 struct program_space *search_pspace);
8613a476 298
74421c0b 299const struct breakpoint_ops code_breakpoint_ops =
8613a476 300{
6cce0251 301 parse_breakpoint_sals,
eee031e2 302 create_breakpoints_sal,
8613a476
TT
303};
304
55aa24fb 305/* Breakpoints set on probes. */
a678887d 306static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
8613a476 307{
264f9890 308 bkpt_probe_create_sals_from_location_spec,
eee031e2 309 create_breakpoints_sal,
8613a476 310};
55aa24fb 311
de5dc49e
AB
312/* Tracepoints set on probes. We use the same methods as for breakpoints
313 on probes. */
a678887d 314static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
8613a476 315{
de5dc49e 316 bkpt_probe_create_sals_from_location_spec,
eee031e2 317 create_breakpoints_sal,
8613a476 318};
bac7c5cf 319
1c2cbcf1
PA
320/* Implementation of abstract dtors. These must exist to satisfy the
321 linker. */
322
323breakpoint::~breakpoint ()
324{
325}
326
74421c0b 327code_breakpoint::~code_breakpoint ()
1c2cbcf1
PA
328{
329}
330
331catchpoint::~catchpoint ()
332{
333}
334
098f1272 335/* The structure to be used in regular breakpoints. */
74421c0b 336struct ordinary_breakpoint : public code_breakpoint
098f1272 337{
74421c0b 338 using code_breakpoint::code_breakpoint;
73063f51 339
7dd8e7ae 340 int resources_needed (const struct bp_location *) override;
7bd86313 341 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 342 void print_mention () const override;
4d1ae558 343 void print_recreate (struct ui_file *fp) const override;
098f1272
TT
344};
345
9a71ed14
PA
346/* Internal breakpoints. These typically have a lifetime the same as
347 the program, and they end up installed on the breakpoint chain with
348 a negative breakpoint number. They're visible in "maint info
349 breakpoints", but not "info breakpoints". */
74421c0b 350struct internal_breakpoint : public code_breakpoint
098f1272 351{
752a2291
PA
352 internal_breakpoint (struct gdbarch *gdbarch,
353 enum bptype type, CORE_ADDR address)
74421c0b 354 : code_breakpoint (gdbarch, type)
752a2291
PA
355 {
356 symtab_and_line sal;
357 sal.pc = address;
358 sal.section = find_pc_overlay (sal.pc);
359 sal.pspace = current_program_space;
360 add_location (sal);
361
362 pspace = current_program_space;
363 disposition = disp_donttouch;
364 }
73063f51 365
6cce0251 366 void re_set (program_space *pspace) override;
c359fff5 367 void check_status (struct bpstat *bs) override;
7bd86313 368 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 369 void print_mention () const override;
098f1272
TT
370};
371
9a71ed14
PA
372/* Momentary breakpoints. These typically have a lifetime of some run
373 control command only, are always thread-specific, and have 0 for
374 breakpoint number. I.e., there can be many momentary breakpoints
375 on the breakpoint chain and they all same the same number (zero).
376 They're visible in "maint info breakpoints", but not "info
377 breakpoints". */
74421c0b 378struct momentary_breakpoint : public code_breakpoint
098f1272 379{
7ab97995
PA
380 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
381 program_space *pspace_,
382 const struct frame_id &frame_id_,
383 int thread_)
74421c0b 384 : code_breakpoint (gdbarch_, bptype)
7ab97995
PA
385 {
386 /* If FRAME_ID is valid, it should be a real frame, not an inlined
387 or tail-called one. */
388 gdb_assert (!frame_id_artificial_p (frame_id));
389
390 /* Momentary breakpoints are always thread-specific. */
391 gdb_assert (thread_ > 0);
392
393 pspace = pspace_;
394 enable_state = bp_enabled;
395 disposition = disp_donttouch;
396 frame_id = frame_id_;
397 thread = thread_;
b080fe54
AB
398
399 /* The inferior should have been set by the parent constructor. */
400 gdb_assert (inferior == -1);
7ab97995 401 }
73063f51 402
6cce0251 403 void re_set (program_space *pspace) override;
1fd30a47 404 void check_status (struct bpstat *bs) override;
7bd86313 405 enum print_stop_action print_it (const bpstat *bs) const override;
b713485d 406 void print_mention () const override;
098f1272
TT
407};
408
409/* DPrintf breakpoints. */
5ad71d67 410struct dprintf_breakpoint : public ordinary_breakpoint
098f1272 411{
73063f51
TT
412 using ordinary_breakpoint::ordinary_breakpoint;
413
6cce0251 414 void re_set (program_space *pspace) override;
f293a0b5
TT
415 int breakpoint_hit (const struct bp_location *bl,
416 const address_space *aspace,
417 CORE_ADDR bp_addr,
418 const target_waitstatus &ws) override;
4d1ae558 419 void print_recreate (struct ui_file *fp) const override;
f293a0b5 420 void after_condition_true (struct bpstat *bs) override;
098f1272
TT
421};
422
ec45bb67
TT
423/* Ranged breakpoints. */
424struct ranged_breakpoint : public ordinary_breakpoint
425{
b925bf21
PA
426 explicit ranged_breakpoint (struct gdbarch *gdbarch,
427 const symtab_and_line &sal_start,
428 int length,
264f9890
PA
429 location_spec_up start_locspec,
430 location_spec_up end_locspec)
73063f51
TT
431 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
432 {
b925bf21
PA
433 bp_location *bl = add_location (sal_start);
434 bl->length = length;
435
436 disposition = disp_donttouch;
437
264f9890
PA
438 locspec = std::move (start_locspec);
439 locspec_range_end = std::move (end_locspec);
73063f51
TT
440 }
441
ec45bb67
TT
442 int breakpoint_hit (const struct bp_location *bl,
443 const address_space *aspace,
444 CORE_ADDR bp_addr,
445 const target_waitstatus &ws) override;
446 int resources_needed (const struct bp_location *) override;
7bd86313 447 enum print_stop_action print_it (const bpstat *bs) const override;
5e632eca 448 bool print_one (const bp_location **) const override;
ec45bb67 449 void print_one_detail (struct ui_out *) const override;
b713485d 450 void print_mention () const override;
4d1ae558 451 void print_recreate (struct ui_file *fp) const override;
ec45bb67
TT
452};
453
6d7a8c56
TT
454/* Static tracepoints with marker (`-m'). */
455struct static_marker_tracepoint : public tracepoint
456{
73063f51
TT
457 using tracepoint::tracepoint;
458
264f9890
PA
459 std::vector<symtab_and_line> decode_location_spec
460 (struct location_spec *locspec,
6d7a8c56
TT
461 struct program_space *search_pspace) override;
462};
463
d3ce09f5
SS
464/* The style in which to perform a dynamic printf. This is a user
465 option because different output options have different tradeoffs;
466 if GDB does the printing, there is better error handling if there
467 is a problem with any of the arguments, but using an inferior
468 function lets you have special-purpose printers and sending of
469 output to the same place as compiled-in print functions. */
470
471static const char dprintf_style_gdb[] = "gdb";
472static const char dprintf_style_call[] = "call";
473static const char dprintf_style_agent[] = "agent";
474static const char *const dprintf_style_enums[] = {
475 dprintf_style_gdb,
476 dprintf_style_call,
477 dprintf_style_agent,
478 NULL
479};
480static const char *dprintf_style = dprintf_style_gdb;
481
482/* The function to use for dynamic printf if the preferred style is to
483 call into the inferior. The value is simply a string that is
484 copied into the command, so it can be anything that GDB can
485 evaluate to a callable address, not necessarily a function name. */
486
e0700ba4 487static std::string dprintf_function = "printf";
d3ce09f5
SS
488
489/* The channel to use for dynamic printf if the preferred style is to
490 call into the inferior; if a nonempty string, it will be passed to
491 the call as the first argument, with the format string as the
492 second. As with the dprintf function, this can be anything that
493 GDB knows how to evaluate, so in addition to common choices like
494 "stderr", this could be an app-specific expression like
495 "mystreams[curlogger]". */
496
e0700ba4 497static std::string dprintf_channel;
d3ce09f5
SS
498
499/* True if dprintf commands should continue to operate even if GDB
500 has disconnected. */
491144b5 501static bool disconnected_dprintf = true;
d3ce09f5 502
5cea2a26
PA
503struct command_line *
504breakpoint_commands (struct breakpoint *b)
505{
d1b0a7bf 506 return b->commands ? b->commands.get () : NULL;
5cea2a26 507}
3daf8fe5 508
f3b1572e
PA
509/* Flag indicating that a command has proceeded the inferior past the
510 current breakpoint. */
511
491144b5 512static bool breakpoint_proceeded;
f3b1572e 513
956a9fb9 514const char *
2cec12e5
AR
515bpdisp_text (enum bpdisp disp)
516{
4a64f543
MS
517 /* NOTE: the following values are a part of MI protocol and
518 represent values of 'disp' field returned when inferior stops at
519 a breakpoint. */
bc043ef3 520 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 521
2cec12e5
AR
522 return bpdisps[(int) disp];
523}
c906108c 524
4a64f543 525/* Prototypes for exported functions. */
c906108c 526/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 527 if such is available. */
c906108c
SS
528static int can_use_hw_watchpoints;
529
920d2a44
AC
530static void
531show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
532 struct cmd_list_element *c,
533 const char *value)
534{
6cb06a8c
TT
535 gdb_printf (file,
536 _("Debugger's willingness to use "
537 "watchpoint hardware is %s.\n"),
538 value);
920d2a44
AC
539}
540
fa8d40ab
JJ
541/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
542 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 543 for unrecognized breakpoint locations.
fa8d40ab
JJ
544 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
545static enum auto_boolean pending_break_support;
920d2a44
AC
546static void
547show_pending_break_support (struct ui_file *file, int from_tty,
548 struct cmd_list_element *c,
549 const char *value)
550{
6cb06a8c
TT
551 gdb_printf (file,
552 _("Debugger's behavior regarding "
553 "pending breakpoints is %s.\n"),
554 value);
920d2a44 555}
fa8d40ab 556
491144b5 557/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 558 set with "break" but falling in read-only memory.
491144b5 559 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 560 use hardware breakpoints. */
491144b5 561static bool automatic_hardware_breakpoints;
765dc015
VP
562static void
563show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
564 struct cmd_list_element *c,
565 const char *value)
566{
6cb06a8c
TT
567 gdb_printf (file,
568 _("Automatic usage of hardware breakpoints is %s.\n"),
569 value);
765dc015
VP
570}
571
a25a5a45
PA
572/* If on, GDB keeps breakpoints inserted even if the inferior is
573 stopped, and immediately inserts any new breakpoints as soon as
574 they're created. If off (default), GDB keeps breakpoints off of
575 the target as long as possible. That is, it delays inserting
576 breakpoints until the next resume, and removes them again when the
577 target fully stops. This is a bit safer in case GDB crashes while
578 processing user input. */
491144b5 579static bool always_inserted_mode = false;
72d0e2c5 580
33e5cbd6 581static void
74960c60 582show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 583 struct cmd_list_element *c, const char *value)
74960c60 584{
6cb06a8c
TT
585 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
586 value);
74960c60
VP
587}
588
4764e221
AB
589/* See breakpoint.h. */
590bool debug_breakpoint = false;
3cdc2d7e
MS
591
592/* "show debug breakpoint" implementation. */
593static void
594show_debug_breakpoint (struct ui_file *file, int from_tty,
595 struct cmd_list_element *c, const char *value)
596{
597 gdb_printf (file, _("Breakpoint location debugging is %s.\n"), value);
598}
599
b57bacec
PA
600/* See breakpoint.h. */
601
33e5cbd6 602int
a25a5a45 603breakpoints_should_be_inserted_now (void)
33e5cbd6 604{
99d9c3b9 605 if (gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
a25a5a45
PA
606 {
607 /* If breakpoints are global, they should be inserted even if no
608 thread under gdb's control is running, or even if there are
609 no threads under GDB's control yet. */
610 return 1;
611 }
5b6d1e4f 612 else
a25a5a45 613 {
a25a5a45
PA
614 if (always_inserted_mode)
615 {
616 /* The user wants breakpoints inserted even if all threads
617 are stopped. */
618 return 1;
619 }
620
5b6d1e4f
PA
621 for (inferior *inf : all_inferiors ())
622 if (inf->has_execution ()
623 && threads_are_executing (inf->process_target ()))
624 return 1;
372316f1
PA
625
626 /* Don't remove breakpoints yet if, even though all threads are
627 stopped, we still have events to process. */
08036331 628 for (thread_info *tp : all_non_exited_threads ())
1edb66d8 629 if (tp->resumed () && tp->has_pending_waitstatus ())
372316f1 630 return 1;
a25a5a45
PA
631 }
632 return 0;
33e5cbd6 633}
765dc015 634
b775012e
LM
635static const char condition_evaluation_both[] = "host or target";
636
637/* Modes for breakpoint condition evaluation. */
638static const char condition_evaluation_auto[] = "auto";
639static const char condition_evaluation_host[] = "host";
640static const char condition_evaluation_target[] = "target";
641static const char *const condition_evaluation_enums[] = {
642 condition_evaluation_auto,
643 condition_evaluation_host,
644 condition_evaluation_target,
645 NULL
646};
647
648/* Global that holds the current mode for breakpoint condition evaluation. */
649static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
650
651/* Global that we use to display information to the user (gets its value from
652 condition_evaluation_mode_1. */
653static const char *condition_evaluation_mode = condition_evaluation_auto;
654
655/* Translate a condition evaluation mode MODE into either "host"
656 or "target". This is used mostly to translate from "auto" to the
657 real setting that is being used. It returns the translated
658 evaluation mode. */
659
660static const char *
661translate_condition_evaluation_mode (const char *mode)
662{
663 if (mode == condition_evaluation_auto)
664 {
665 if (target_supports_evaluation_of_breakpoint_conditions ())
666 return condition_evaluation_target;
667 else
668 return condition_evaluation_host;
669 }
670 else
671 return mode;
672}
673
674/* Discovers what condition_evaluation_auto translates to. */
675
676static const char *
677breakpoint_condition_evaluation_mode (void)
678{
679 return translate_condition_evaluation_mode (condition_evaluation_mode);
680}
681
682/* Return true if GDB should evaluate breakpoint conditions or false
683 otherwise. */
684
d8de7963 685static bool
b775012e
LM
686gdb_evaluates_breakpoint_condition_p (void)
687{
688 const char *mode = breakpoint_condition_evaluation_mode ();
689
690 return (mode == condition_evaluation_host);
691}
692
c906108c
SS
693/* Are we executing breakpoint commands? */
694static int executing_breakpoint_commands;
695
c02f5703
MS
696/* Are overlay event breakpoints enabled? */
697static int overlay_events_enabled;
698
e09342b5 699/* See description in breakpoint.h. */
491144b5 700bool target_exact_watchpoints = false;
e09342b5 701
7cc221ef 702/* Chains of all breakpoints defined. */
c906108c 703
e2a15788 704static intrusive_list<breakpoint> breakpoint_chain;
c906108c 705
240edef6 706/* See breakpoint.h. */
43892fdf 707
240edef6 708breakpoint_range
43892fdf
SM
709all_breakpoints ()
710{
a1decfc1 711 return breakpoint_range (breakpoint_chain.begin (), breakpoint_chain.end ());
43892fdf
SM
712}
713
240edef6 714/* See breakpoint.h. */
1428b37a 715
240edef6 716breakpoint_safe_range
1428b37a
SM
717all_breakpoints_safe ()
718{
719 return breakpoint_safe_range (all_breakpoints ());
720}
721
f6d17b2b
SM
722/* See breakpoint.h. */
723
724tracepoint_range
725all_tracepoints ()
726{
a1decfc1
SM
727 return tracepoint_range (tracepoint_iterator (breakpoint_chain.begin ()),
728 tracepoint_iterator (breakpoint_chain.end ()));
f6d17b2b
SM
729}
730
8dd54de0
TV
731/* Array is sorted by bp_location_ptr_is_less_than - primarily by the
732 ADDRESS. */
876fa593 733
5d51cd5d 734static std::vector<bp_location *> bp_locations;
876fa593 735
055c879f
SM
736/* See breakpoint.h. */
737
738const std::vector<bp_location *> &
48d7020b
SM
739all_bp_locations ()
740{
741 return bp_locations;
742}
743
e0d9a270
SM
744/* Range to iterate over breakpoint locations at a given address. */
745
746struct bp_locations_at_addr_range
747{
748 using iterator = std::vector<bp_location *>::iterator;
749
750 bp_locations_at_addr_range (CORE_ADDR addr)
751 {
752 struct compare
753 {
754 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
755 { return loc->address < addr_; }
756
757 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
758 { return addr_ < loc->address; }
759 };
760
761 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
762 addr, compare ());
763
764 m_begin = it_pair.first;
765 m_end = it_pair.second;
766 }
767
768 iterator begin () const
769 { return m_begin; }
770
771 iterator end () const
772 { return m_end; }
773
774private:
775 iterator m_begin;
776 iterator m_end;
777};
778
779/* Return a range to iterate over all breakpoint locations exactly at address
780 ADDR.
781
782 If it's needed to iterate multiple times on the same range, it's possible
783 to save the range in a local variable and use it multiple times:
784
785 auto range = all_bp_locations_at_addr (addr);
786
787 for (bp_location *loc : range)
788 // use loc
789
790 for (bp_location *loc : range)
791 // use loc
792
793 This saves a bit of time, as it avoids re-doing the binary searches to find
794 the range's boundaries. Just remember not to change the bp_locations vector
795 in the mean time, as it could make the range's iterators stale. */
796
797static bp_locations_at_addr_range
798all_bp_locations_at_addr (CORE_ADDR addr)
799{
800 return bp_locations_at_addr_range (addr);
801}
802
4a64f543 803/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 804 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 805 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 806 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 807 an address you need to read. */
876fa593 808
f5336ca5 809static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 810
4a64f543
MS
811/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
812 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
813 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
814 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 815 scan for shadow bytes for an address you need to read. */
876fa593 816
f5336ca5 817static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 818
4a64f543 819/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
820 from the bp_locations array, but for which a hit may still be
821 reported by a target. */
1123588c 822static std::vector<bp_location *> moribund_locations;
20874c92 823
c906108c
SS
824/* Number of last breakpoint made. */
825
95a42b64
TT
826static int breakpoint_count;
827
86b17b60
PA
828/* The value of `breakpoint_count' before the last command that
829 created breakpoints. If the last (break-like) command created more
830 than one breakpoint, then the difference between BREAKPOINT_COUNT
831 and PREV_BREAKPOINT_COUNT is more than one. */
832static int prev_breakpoint_count;
c906108c 833
1042e4c0
SS
834/* Number of last tracepoint made. */
835
95a42b64 836static int tracepoint_count;
1042e4c0 837
6149aea9
PA
838static struct cmd_list_element *breakpoint_set_cmdlist;
839static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 840struct cmd_list_element *save_cmdlist;
6149aea9 841
468d015d 842/* Return whether a breakpoint is an active enabled breakpoint. */
d8de7963 843static bool
468d015d
JJ
844breakpoint_enabled (struct breakpoint *b)
845{
0d381245 846 return (b->enable_state == bp_enabled);
468d015d
JJ
847}
848
c906108c
SS
849/* Set breakpoint count to NUM. */
850
95a42b64 851static void
fba45db2 852set_breakpoint_count (int num)
c906108c 853{
86b17b60 854 prev_breakpoint_count = breakpoint_count;
c906108c 855 breakpoint_count = num;
4fa62494 856 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
857}
858
86b17b60
PA
859/* Used by `start_rbreak_breakpoints' below, to record the current
860 breakpoint count before "rbreak" creates any breakpoint. */
861static int rbreak_start_breakpoint_count;
862
95a42b64
TT
863/* Called at the start an "rbreak" command to record the first
864 breakpoint made. */
86b17b60 865
c80049d3 866scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 867{
86b17b60 868 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
869}
870
871/* Called at the end of an "rbreak" command to record the last
872 breakpoint made. */
86b17b60 873
c80049d3 874scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 875{
86b17b60 876 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
877}
878
59d25b31
TT
879/* See breakpoint.h. */
880
881int
882scoped_rbreak_breakpoints::first_breakpoint () const
883{
884 return rbreak_start_breakpoint_count + 1;
885}
886
887/* See breakpoint.h. */
888
889int
890scoped_rbreak_breakpoints::last_breakpoint () const
891{
892 return (rbreak_start_breakpoint_count == breakpoint_count
893 ? -1
894 : breakpoint_count);
895}
896
4a64f543 897/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
898
899void
fba45db2 900clear_breakpoint_hit_counts (void)
c906108c 901{
a1decfc1
SM
902 for (breakpoint &b : all_breakpoints ())
903 b.hit_count = 0;
c906108c
SS
904}
905
c906108c 906\f
48cb2d85
VP
907/* Return the breakpoint with the specified number, or NULL
908 if the number does not refer to an existing breakpoint. */
909
910struct breakpoint *
911get_breakpoint (int num)
912{
a1decfc1
SM
913 for (breakpoint &b : all_breakpoints ())
914 if (b.number == num)
915 return &b;
48cb2d85 916
43892fdf 917 return nullptr;
48cb2d85 918}
5c44784c 919
78805ff8
PW
920/* Return TRUE if NUM refer to an existing breakpoint that has
921 multiple code locations. */
922
923static bool
924has_multiple_locations (int num)
925{
a1decfc1
SM
926 for (breakpoint &b : all_breakpoints ())
927 if (b.number == num)
928 return b.has_multiple_locations ();
78805ff8
PW
929
930 return false;
931}
932
c906108c 933\f
adc36818 934
b775012e
LM
935/* Mark locations as "conditions have changed" in case the target supports
936 evaluating conditions on its side. */
937
938static void
939mark_breakpoint_modified (struct breakpoint *b)
940{
b775012e
LM
941 /* This is only meaningful if the target is
942 evaluating conditions and if the user has
943 opted for condition evaluation on the target's
944 side. */
945 if (gdb_evaluates_breakpoint_condition_p ()
946 || !target_supports_evaluation_of_breakpoint_conditions ())
947 return;
948
949 if (!is_breakpoint (b))
950 return;
951
b00b30b2
SM
952 for (bp_location &loc : b->locations ())
953 loc.condition_changed = condition_modified;
b775012e
LM
954}
955
956/* Mark location as "conditions have changed" in case the target supports
957 evaluating conditions on its side. */
958
959static void
960mark_breakpoint_location_modified (struct bp_location *loc)
961{
962 /* This is only meaningful if the target is
963 evaluating conditions and if the user has
964 opted for condition evaluation on the target's
965 side. */
966 if (gdb_evaluates_breakpoint_condition_p ()
967 || !target_supports_evaluation_of_breakpoint_conditions ())
968
969 return;
970
971 if (!is_breakpoint (loc->owner))
972 return;
973
974 loc->condition_changed = condition_modified;
975}
976
977/* Sets the condition-evaluation mode using the static global
978 condition_evaluation_mode. */
979
980static void
eb4c3f4a 981set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
982 struct cmd_list_element *c)
983{
b775012e
LM
984 const char *old_mode, *new_mode;
985
986 if ((condition_evaluation_mode_1 == condition_evaluation_target)
987 && !target_supports_evaluation_of_breakpoint_conditions ())
988 {
989 condition_evaluation_mode_1 = condition_evaluation_mode;
990 warning (_("Target does not support breakpoint condition evaluation.\n"
991 "Using host evaluation mode instead."));
992 return;
993 }
994
995 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
996 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
997
abf1152a
JK
998 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
999 settings was "auto". */
1000 condition_evaluation_mode = condition_evaluation_mode_1;
1001
b775012e
LM
1002 /* Only update the mode if the user picked a different one. */
1003 if (new_mode != old_mode)
1004 {
b775012e
LM
1005 /* If the user switched to a different evaluation mode, we
1006 need to synch the changes with the target as follows:
1007
1008 "host" -> "target": Send all (valid) conditions to the target.
1009 "target" -> "host": Remove all the conditions from the target.
1010 */
1011
b775012e
LM
1012 if (new_mode == condition_evaluation_target)
1013 {
1014 /* Mark everything modified and synch conditions with the
1015 target. */
48d7020b 1016 for (bp_location *loc : all_bp_locations ())
b775012e 1017 mark_breakpoint_location_modified (loc);
24b21115 1018 }
b775012e
LM
1019 else
1020 {
1021 /* Manually mark non-duplicate locations to synch conditions
1022 with the target. We do this to remove all the conditions the
1023 target knows about. */
48d7020b 1024 for (bp_location *loc : all_bp_locations ())
b775012e
LM
1025 if (is_breakpoint (loc->owner) && loc->inserted)
1026 loc->needs_update = 1;
1027 }
1028
1029 /* Do the update. */
44702360 1030 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
1031 }
1032
1033 return;
1034}
1035
1036/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
1037 what "auto" is translating to. */
1038
1039static void
1040show_condition_evaluation_mode (struct ui_file *file, int from_tty,
1041 struct cmd_list_element *c, const char *value)
1042{
1043 if (condition_evaluation_mode == condition_evaluation_auto)
6cb06a8c
TT
1044 gdb_printf (file,
1045 _("Breakpoint condition evaluation "
1046 "mode is %s (currently %s).\n"),
1047 value,
1048 breakpoint_condition_evaluation_mode ());
b775012e 1049 else
6cb06a8c
TT
1050 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
1051 value);
b775012e
LM
1052}
1053
b5fa468f
TBA
1054/* Parse COND_STRING in the context of LOC and set as the condition
1055 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
1056 the number of LOC within its owner. In case of parsing error, mark
1057 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
1058
1059static void
1060set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
1061 int bp_num, int loc_num)
1062{
1063 bool has_junk = false;
1064 try
1065 {
1066 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
1067 block_for_pc (loc->address), 0);
1068 if (*cond_string != 0)
1069 has_junk = true;
1070 else
1071 {
1072 loc->cond = std::move (new_exp);
1073 if (loc->disabled_by_cond && loc->enabled)
6cb06a8c
TT
1074 gdb_printf (_("Breakpoint %d's condition is now valid at "
1075 "location %d, enabling.\n"),
1076 bp_num, loc_num);
b5fa468f
TBA
1077
1078 loc->disabled_by_cond = false;
1079 }
1080 }
1081 catch (const gdb_exception_error &e)
1082 {
1083 if (loc->enabled)
1084 {
1085 /* Warn if a user-enabled location is now becoming disabled-by-cond.
1086 BP_NUM is 0 if the breakpoint is being defined for the first
1087 time using the "break ... if ..." command, and non-zero if
1088 already defined. */
1089 if (bp_num != 0)
1090 warning (_("failed to validate condition at location %d.%d, "
1091 "disabling:\n %s"), bp_num, loc_num, e.what ());
1092 else
1093 warning (_("failed to validate condition at location %d, "
1094 "disabling:\n %s"), loc_num, e.what ());
1095 }
1096
1097 loc->disabled_by_cond = true;
1098 }
1099
1100 if (has_junk)
1101 error (_("Garbage '%s' follows condition"), cond_string);
1102}
1103
19081eb5
SM
1104/* See breakpoint.h. */
1105
1106void
1107notify_breakpoint_modified (breakpoint *b)
1108{
1109 interps_notify_breakpoint_modified (b);
1110 gdb::observers::breakpoint_modified.notify (b);
1111}
1112
adc36818 1113void
7a26bd4d 1114set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 1115 int from_tty, bool force)
adc36818 1116{
4c55e970 1117 if (*exp == 0)
3a5c3e22 1118 {
6f781ee3 1119 b->cond_string.reset ();
3a5c3e22 1120
4c55e970 1121 if (is_watchpoint (b))
98ed24fb 1122 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
1123 else
1124 {
b5fa468f 1125 int loc_num = 1;
b00b30b2 1126 for (bp_location &loc : b->locations ())
4c55e970 1127 {
b00b30b2
SM
1128 loc.cond.reset ();
1129 if (loc.disabled_by_cond && loc.enabled)
6cb06a8c
TT
1130 gdb_printf (_("Breakpoint %d's condition is now valid at "
1131 "location %d, enabling.\n"),
1132 b->number, loc_num);
b00b30b2 1133 loc.disabled_by_cond = false;
b5fa468f 1134 loc_num++;
4c55e970
TBA
1135
1136 /* No need to free the condition agent expression
1137 bytecode (if we have one). We will handle this
1138 when we go through update_global_location_list. */
1139 }
1140 }
1e620590 1141
adc36818 1142 if (from_tty)
6cb06a8c 1143 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
adc36818
PM
1144 }
1145 else
1146 {
adc36818
PM
1147 if (is_watchpoint (b))
1148 {
699bd4cf 1149 innermost_block_tracker tracker;
78319c15 1150 const char *arg = exp;
4c55e970 1151 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 1152 if (*arg != 0)
adc36818 1153 error (_("Junk at end of expression"));
98ed24fb 1154 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
4c55e970 1155 w->cond_exp = std::move (new_exp);
699bd4cf 1156 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
1157 }
1158 else
1159 {
4c55e970
TBA
1160 /* Parse and set condition expressions. We make two passes.
1161 In the first, we parse the condition string to see if it
b5fa468f
TBA
1162 is valid in at least one location. If so, the condition
1163 would be accepted. So we go ahead and set the locations'
1164 conditions. In case no valid case is found, we throw
4c55e970
TBA
1165 the error and the condition string will be rejected.
1166 This two-pass approach is taken to avoid setting the
1167 state of locations in case of a reject. */
b00b30b2 1168 for (const bp_location &loc : b->locations ())
4c55e970 1169 {
b5fa468f
TBA
1170 try
1171 {
1172 const char *arg = exp;
b00b30b2
SM
1173 parse_exp_1 (&arg, loc.address,
1174 block_for_pc (loc.address), 0);
b5fa468f
TBA
1175 if (*arg != 0)
1176 error (_("Junk at end of expression"));
1177 break;
1178 }
1179 catch (const gdb_exception_error &e)
1180 {
1181 /* Condition string is invalid. If this happens to
733d554a
TBA
1182 be the last loc, abandon (if not forced) or continue
1183 (if forced). */
b00b30b2 1184 if (&loc == &b->last_loc () && !force)
b5fa468f
TBA
1185 throw;
1186 }
4c55e970
TBA
1187 }
1188
b5fa468f 1189 /* If we reach here, the condition is valid at some locations. */
dda83cd7 1190 int loc_num = 1;
b00b30b2 1191 for (bp_location &loc : b->locations ())
40cb8ca5 1192 {
b00b30b2 1193 set_breakpoint_location_condition (exp, &loc, b->number, loc_num);
40cb8ca5
SM
1194 loc_num++;
1195 }
adc36818 1196 }
1e620590
TBA
1197
1198 /* We know that the new condition parsed successfully. The
1199 condition string of the breakpoint can be safely updated. */
6f781ee3 1200 b->cond_string = make_unique_xstrdup (exp);
1e620590 1201 b->condition_not_parsed = 0;
adc36818 1202 }
b775012e
LM
1203 mark_breakpoint_modified (b);
1204
19081eb5 1205 notify_breakpoint_modified (b);
adc36818
PM
1206}
1207
79aabb73
TBA
1208/* See breakpoint.h. */
1209
1210void
1211set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1212 bool force)
1213{
a1decfc1
SM
1214 for (breakpoint &b : all_breakpoints ())
1215 if (b.number == bpnum)
79aabb73
TBA
1216 {
1217 /* Check if this breakpoint has a "stop" method implemented in an
1218 extension language. This method and conditions entered into GDB
1219 from the CLI are mutually exclusive. */
1220 const struct extension_language_defn *extlang
a1decfc1 1221 = get_breakpoint_cond_ext_lang (&b, EXT_LANG_NONE);
79aabb73
TBA
1222
1223 if (extlang != NULL)
1224 {
1225 error (_("Only one stop condition allowed. There is currently"
1226 " a %s stop condition defined for this breakpoint."),
1227 ext_lang_capitalized_name (extlang));
1228 }
a1decfc1 1229 set_breakpoint_condition (&b, exp, from_tty, force);
79aabb73 1230
a1decfc1 1231 if (is_breakpoint (&b))
79aabb73
TBA
1232 update_global_location_list (UGLL_MAY_INSERT);
1233
1234 return;
1235 }
1236
1237 error (_("No breakpoint number %d."), bpnum);
1238}
1239
b1d4d8d1
TBA
1240/* The options for the "condition" command. */
1241
1242struct condition_command_opts
1243{
1244 /* For "-force". */
1245 bool force_condition = false;
1246};
1247
1248static const gdb::option::option_def condition_command_option_defs[] = {
1249
1250 gdb::option::flag_option_def<condition_command_opts> {
1251 "force",
1252 [] (condition_command_opts *opts) { return &opts->force_condition; },
1253 N_("Set the condition even if it is invalid for all current locations."),
1254 },
1255
1256};
1257
1258/* Create an option_def_group for the "condition" options, with
1259 CC_OPTS as context. */
1260
1261static inline gdb::option::option_def_group
1262make_condition_command_options_def_group (condition_command_opts *cc_opts)
1263{
1264 return {{condition_command_option_defs}, cc_opts};
1265}
1266
d55637df
TT
1267/* Completion for the "condition" command. */
1268
eb3ff9a5 1269static void
6f937416 1270condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1271 completion_tracker &tracker,
b1d4d8d1 1272 const char *text, const char * /*word*/)
d55637df 1273{
b1d4d8d1
TBA
1274 bool has_no_arguments = (*text == '\0');
1275 condition_command_opts cc_opts;
1276 const auto group = make_condition_command_options_def_group (&cc_opts);
1277 if (gdb::option::complete_options
1278 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1279 return;
d55637df 1280
f1735a53 1281 text = skip_spaces (text);
b1d4d8d1 1282 const char *space = skip_to_space (text);
d55637df
TT
1283 if (*space == '\0')
1284 {
1285 int len;
d55637df
TT
1286
1287 if (text[0] == '$')
1288 {
b1d4d8d1 1289 tracker.advance_custom_word_point_by (1);
d55637df 1290 /* We don't support completion of history indices. */
eb3ff9a5
PA
1291 if (!isdigit (text[1]))
1292 complete_internalvar (tracker, &text[1]);
1293 return;
d55637df
TT
1294 }
1295
b1d4d8d1
TBA
1296 /* Suggest the "-force" flag if no arguments are given. If
1297 arguments were passed, they either already include the flag,
1298 or we are beyond the point of suggesting it because it's
1299 positionally the first argument. */
1300 if (has_no_arguments)
1301 gdb::option::complete_on_all_options (tracker, group);
1302
d55637df
TT
1303 /* We're completing the breakpoint number. */
1304 len = strlen (text);
1305
a1decfc1 1306 for (breakpoint &b : all_breakpoints ())
58ce7251
SDJ
1307 {
1308 char number[50];
1309
a1decfc1 1310 xsnprintf (number, sizeof (number), "%d", b.number);
58ce7251
SDJ
1311
1312 if (strncmp (number, text, len) == 0)
b02f78f9 1313 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1314 }
d55637df 1315
eb3ff9a5 1316 return;
d55637df
TT
1317 }
1318
b1d4d8d1
TBA
1319 /* We're completing the expression part. Skip the breakpoint num. */
1320 const char *exp_start = skip_spaces (space);
1321 tracker.advance_custom_word_point_by (exp_start - text);
1322 text = exp_start;
1323 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1324 expression_completer (cmd, tracker, text, word);
d55637df
TT
1325}
1326
c906108c
SS
1327/* condition N EXP -- set break condition of breakpoint N to EXP. */
1328
1329static void
0b39b52e 1330condition_command (const char *arg, int from_tty)
c906108c 1331{
0b39b52e 1332 const char *p;
52f0bd74 1333 int bnum;
c906108c
SS
1334
1335 if (arg == 0)
e2e0b3e5 1336 error_no_arg (_("breakpoint number"));
c906108c
SS
1337
1338 p = arg;
733d554a
TBA
1339
1340 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1341 condition_command_opts cc_opts;
1342 const auto group = make_condition_command_options_def_group (&cc_opts);
1343 gdb::option::process_options
1344 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1345
c906108c 1346 bnum = get_number (&p);
5c44784c 1347 if (bnum == 0)
8a3fe4f8 1348 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1349
79aabb73 1350 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1351}
1352
a7bdde9e
VP
1353/* Check that COMMAND do not contain commands that are suitable
1354 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1355 Throw if any such commands is found. */
1356
a7bdde9e
VP
1357static void
1358check_no_tracepoint_commands (struct command_line *commands)
1359{
1360 struct command_line *c;
cc59ec59 1361
a7bdde9e
VP
1362 for (c = commands; c; c = c->next)
1363 {
a7bdde9e 1364 if (c->control_type == while_stepping_control)
3e43a32a
MS
1365 error (_("The 'while-stepping' command can "
1366 "only be used for tracepoints"));
a7bdde9e 1367
12973681
TT
1368 check_no_tracepoint_commands (c->body_list_0.get ());
1369 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1370
1371 /* Not that command parsing removes leading whitespace and comment
4a64f543 1372 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1373 command directly. */
1374 if (strstr (c->line, "collect ") == c->line)
1375 error (_("The 'collect' command can only be used for tracepoints"));
1376
51661e93
VP
1377 if (strstr (c->line, "teval ") == c->line)
1378 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1379 }
1380}
1381
098f1272 1382struct longjmp_breakpoint : public momentary_breakpoint
3b0871f4 1383{
73063f51
TT
1384 using momentary_breakpoint::momentary_breakpoint;
1385
c1fc2657 1386 ~longjmp_breakpoint () override;
3b0871f4
SM
1387};
1388
d77f58be
SS
1389/* Encapsulate tests for different types of tracepoints. */
1390
3b0871f4
SM
1391static bool
1392is_tracepoint_type (bptype type)
d9b3f62e
PA
1393{
1394 return (type == bp_tracepoint
1395 || type == bp_fast_tracepoint
7b572efb
TT
1396 || type == bp_static_tracepoint
1397 || type == bp_static_marker_tracepoint);
d9b3f62e
PA
1398}
1399
f2478a7e
SM
1400/* See breakpoint.h. */
1401
1402bool
d77f58be 1403is_tracepoint (const struct breakpoint *b)
a7bdde9e 1404{
d9b3f62e 1405 return is_tracepoint_type (b->type);
a7bdde9e 1406}
d9b3f62e 1407
a5e364af
SM
1408/* Factory function to create an appropriate instance of breakpoint given
1409 TYPE. */
1410
3b003a61 1411template<typename... Arg>
74421c0b 1412static std::unique_ptr<code_breakpoint>
3b003a61
PA
1413new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1414 Arg&&... args)
a5e364af 1415{
74421c0b 1416 code_breakpoint *b;
a5e364af 1417
098f1272
TT
1418 switch (type)
1419 {
1420 case bp_breakpoint:
1421 case bp_hardware_breakpoint:
3b003a61
PA
1422 b = new ordinary_breakpoint (gdbarch, type,
1423 std::forward<Arg> (args)...);
098f1272
TT
1424 break;
1425
1426 case bp_fast_tracepoint:
1427 case bp_static_tracepoint:
1428 case bp_tracepoint:
3b003a61
PA
1429 b = new tracepoint (gdbarch, type,
1430 std::forward<Arg> (args)...);
098f1272
TT
1431 break;
1432
6d7a8c56 1433 case bp_static_marker_tracepoint:
3b003a61
PA
1434 b = new static_marker_tracepoint (gdbarch, type,
1435 std::forward<Arg> (args)...);
6d7a8c56
TT
1436 break;
1437
098f1272 1438 case bp_dprintf:
3b003a61
PA
1439 b = new dprintf_breakpoint (gdbarch, type,
1440 std::forward<Arg> (args)...);
098f1272
TT
1441 break;
1442
098f1272
TT
1443 default:
1444 gdb_assert_not_reached ("invalid type");
1445 }
a5e364af 1446
74421c0b 1447 return std::unique_ptr<code_breakpoint> (b);
a5e364af
SM
1448}
1449
e5dd4106 1450/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1451 breakpoint. This function will throw an exception if a problem is
1452 found. */
48cb2d85 1453
95a42b64
TT
1454static void
1455validate_commands_for_breakpoint (struct breakpoint *b,
1456 struct command_line *commands)
48cb2d85 1457{
d77f58be 1458 if (is_tracepoint (b))
a7bdde9e 1459 {
01bccc56 1460 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
a7bdde9e
VP
1461 struct command_line *c;
1462 struct command_line *while_stepping = 0;
c9a6ce02
PA
1463
1464 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1465 might have included a while-stepping action, while the new
1466 ones might not. */
c9a6ce02
PA
1467 t->step_count = 0;
1468
1469 /* We need to verify that each top-level element of commands is
1470 valid for tracepoints, that there's at most one
1471 while-stepping element, and that the while-stepping's body
1472 has valid tracing commands excluding nested while-stepping.
1473 We also need to validate the tracepoint action line in the
1474 context of the tracepoint --- validate_actionline actually
1475 has side effects, like setting the tracepoint's
1476 while-stepping STEP_COUNT, in addition to checking if the
1477 collect/teval actions parse and make sense in the
1478 tracepoint's context. */
a7bdde9e
VP
1479 for (c = commands; c; c = c->next)
1480 {
a7bdde9e
VP
1481 if (c->control_type == while_stepping_control)
1482 {
1483 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1484 error (_("The 'while-stepping' command "
1485 "cannot be used for fast tracepoint"));
7b572efb
TT
1486 else if (b->type == bp_static_tracepoint
1487 || b->type == bp_static_marker_tracepoint)
3e43a32a
MS
1488 error (_("The 'while-stepping' command "
1489 "cannot be used for static tracepoint"));
a7bdde9e
VP
1490
1491 if (while_stepping)
3e43a32a
MS
1492 error (_("The 'while-stepping' command "
1493 "can be used only once"));
a7bdde9e
VP
1494 else
1495 while_stepping = c;
1496 }
c9a6ce02 1497
01bccc56 1498 validate_actionline (c->line, t);
a7bdde9e
VP
1499 }
1500 if (while_stepping)
1501 {
1502 struct command_line *c2;
1503
12973681
TT
1504 gdb_assert (while_stepping->body_list_1 == nullptr);
1505 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1506 for (; c2; c2 = c2->next)
1507 {
a7bdde9e
VP
1508 if (c2->control_type == while_stepping_control)
1509 error (_("The 'while-stepping' command cannot be nested"));
1510 }
1511 }
1512 }
1513 else
1514 {
1515 check_no_tracepoint_commands (commands);
1516 }
95a42b64
TT
1517}
1518
0fb4aa4b
PA
1519/* Return a vector of all the static tracepoints set at ADDR. The
1520 caller is responsible for releasing the vector. */
1521
f51e0e20 1522std::vector<breakpoint *>
0fb4aa4b
PA
1523static_tracepoints_here (CORE_ADDR addr)
1524{
f51e0e20 1525 std::vector<breakpoint *> found;
0fb4aa4b 1526
a1decfc1
SM
1527 for (breakpoint &b : all_breakpoints ())
1528 if (b.type == bp_static_tracepoint
1529 || b.type == bp_static_marker_tracepoint)
0fb4aa4b 1530 {
a1decfc1 1531 for (bp_location &loc : b.locations ())
b00b30b2 1532 if (loc.address == addr)
a1decfc1 1533 found.push_back (&b);
0fb4aa4b
PA
1534 }
1535
1536 return found;
1537}
1538
95a42b64 1539/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1540 validate that only allowed commands are included. */
95a42b64
TT
1541
1542void
4a64f543 1543breakpoint_set_commands (struct breakpoint *b,
12973681 1544 counted_command_line &&commands)
95a42b64 1545{
e83f6121
AB
1546 /* If the commands have not changed then there's no need to update
1547 anything, and no need to emit a breakpoint modified event. */
1548 if (commands_equal (b->commands.get (), commands.get ()))
1549 return;
1550
93921405 1551 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1552
d1b0a7bf 1553 b->commands = std::move (commands);
19081eb5 1554 notify_breakpoint_modified (b);
48cb2d85
VP
1555}
1556
45a43567
TT
1557/* Set the internal `silent' flag on the breakpoint. Note that this
1558 is not the same as the "silent" that may appear in the breakpoint's
1559 commands. */
1560
1561void
1562breakpoint_set_silent (struct breakpoint *b, int silent)
1563{
1564 int old_silent = b->silent;
1565
1566 b->silent = silent;
1567 if (old_silent != silent)
19081eb5 1568 notify_breakpoint_modified (b);
45a43567
TT
1569}
1570
0a9ccb9d 1571/* See breakpoint.h. */
45a43567
TT
1572
1573void
1574breakpoint_set_thread (struct breakpoint *b, int thread)
1575{
b080fe54
AB
1576 /* THREAD should be -1, meaning no thread restriction, or it should be a
1577 valid global thread-id, which are greater than zero. */
1578 gdb_assert (thread == -1 || thread > 0);
0a9ccb9d 1579
b080fe54
AB
1580 /* It is not valid to set a thread restriction for a breakpoint that
1581 already has task or inferior restriction. */
1582 gdb_assert (thread == -1 || (b->task == -1 && b->inferior == -1));
45a43567 1583
b080fe54 1584 int old_thread = b->thread;
45a43567
TT
1585 b->thread = thread;
1586 if (old_thread != thread)
6cce0251
AB
1587 {
1588 /* If THREAD is in a different program_space than OLD_THREAD, or the
1589 breakpoint has switched to or from being thread-specific, then we
1590 need to re-set the locations of this breakpoint. First, figure
1591 out the program_space for the old and new threads, use a value of
1592 nullptr to indicate the breakpoint is in all program spaces. */
1593 program_space *old_pspace = nullptr;
1594 if (old_thread != -1)
1595 {
1596 struct thread_info *thr = find_thread_global_id (old_thread);
1597 gdb_assert (thr != nullptr);
1598 old_pspace = thr->inf->pspace;
1599 }
1600
1601 program_space *new_pspace = nullptr;
1602 if (thread != -1)
1603 {
1604 struct thread_info *thr = find_thread_global_id (thread);
1605 gdb_assert (thr != nullptr);
1606 new_pspace = thr->inf->pspace;
1607 }
1608
1609 /* If the program space has changed for this breakpoint, then
1610 re-evaluate it's locations. */
1611 if (old_pspace != new_pspace)
16a6f7d2
AB
1612 {
1613 /* The breakpoint is now associated with a completely different
1614 program space. Discard all of the current locations and then
1615 re-set the breakpoint in the new program space, this will
1616 create the new locations. */
1617 b->clear_locations ();
1618 breakpoint_re_set_one (b, new_pspace);
1619 }
6cce0251 1620
16a6f7d2
AB
1621 /* If the program space didn't change, or the breakpoint didn't
1622 acquire any new locations after the clear_locations call, then we
1623 need to notify of the breakpoint modification now. */
1624 if (old_pspace == new_pspace || !b->has_locations ())
1625 notify_breakpoint_modified (b);
6cce0251 1626 }
45a43567
TT
1627}
1628
0a9ccb9d 1629/* See breakpoint.h. */
45a43567 1630
b080fe54
AB
1631void
1632breakpoint_set_inferior (struct breakpoint *b, int inferior)
1633{
1634 /* INFERIOR should be -1, meaning no inferior restriction, or it should
1635 be a valid inferior number, which are greater than zero. */
1636 gdb_assert (inferior == -1 || inferior > 0);
1637
1638 /* It is not valid to set an inferior restriction for a breakpoint that
1639 already has a task or thread restriction. */
1640 gdb_assert (inferior == -1 || (b->task == -1 && b->thread == -1));
1641
1642 int old_inferior = b->inferior;
1643 b->inferior = inferior;
1644 if (old_inferior != inferior)
6cce0251
AB
1645 {
1646 /* If INFERIOR is in a different program_space than OLD_INFERIOR, or
1647 the breakpoint has switch to or from inferior-specific, then we
1648 need to re-set the locations of this breakpoint. First, figure
1649 out the program_space for the old and new inferiors, use a value
1650 of nullptr to indicate the breakpoint is in all program
1651 spaces. */
1652 program_space *old_pspace = nullptr;
1653 if (old_inferior != -1)
1654 {
1655 struct inferior *inf = find_inferior_id (old_inferior);
1656 gdb_assert (inf != nullptr);
1657 old_pspace = inf->pspace;
1658 }
1659
1660 program_space *new_pspace = nullptr;
1661 if (inferior != -1)
1662 {
1663 struct inferior *inf = find_inferior_id (inferior);
1664 gdb_assert (inf != nullptr);
1665 new_pspace = inf->pspace;
1666 }
1667
1668 if (old_pspace != new_pspace)
16a6f7d2
AB
1669 {
1670 /* The breakpoint is now associated with a completely different
1671 program space. Discard all of the current locations and then
1672 re-set the breakpoint in the new program space, this will
1673 create the new locations. */
1674 b->clear_locations ();
1675 breakpoint_re_set_one (b, new_pspace);
1676 }
6cce0251 1677
16a6f7d2
AB
1678 /* If the program space didn't change, or the breakpoint didn't
1679 acquire any new locations after the clear_locations call, then we
1680 need to notify of the breakpoint modification now. */
1681 if (old_pspace == new_pspace || !b->has_locations ())
1682 notify_breakpoint_modified (b);
6cce0251 1683 }
b080fe54
AB
1684}
1685
1686/* See breakpoint.h. */
1687
45a43567
TT
1688void
1689breakpoint_set_task (struct breakpoint *b, int task)
1690{
b080fe54
AB
1691 /* TASK should be -1, meaning no task restriction, or it should be a
1692 valid task-id, which are greater than zero. */
1693 gdb_assert (task == -1 || task > 0);
0a9ccb9d 1694
b080fe54
AB
1695 /* It is not valid to set a task restriction for a breakpoint that
1696 already has a thread or inferior restriction. */
1697 gdb_assert (task == -1 || (b->thread == -1 && b->inferior == -1));
45a43567 1698
b080fe54 1699 int old_task = b->task;
45a43567
TT
1700 b->task = task;
1701 if (old_task != task)
19081eb5 1702 notify_breakpoint_modified (b);
45a43567
TT
1703}
1704
95a42b64 1705static void
896b6bda 1706commands_command_1 (const char *arg, int from_tty,
4a64f543 1707 struct command_line *control)
95a42b64 1708{
d1b0a7bf 1709 counted_command_line cmd;
999700cd
PW
1710 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1711 NULL after the call to read_command_lines if the user provides an empty
1712 list of command by just typing "end". */
1713 bool cmd_read = false;
95a42b64 1714
896b6bda
PA
1715 std::string new_arg;
1716
95a42b64
TT
1717 if (arg == NULL || !*arg)
1718 {
da1df1db 1719 /* Argument not explicitly given. Synthesize it. */
86b17b60 1720 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1721 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1722 breakpoint_count);
95a42b64 1723 else if (breakpoint_count > 0)
896b6bda 1724 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1725 }
da1df1db
TBA
1726 else
1727 {
1728 /* Create a copy of ARG. This is needed because the "commands"
1729 command may be coming from a script. In that case, the read
1730 line buffer is going to be overwritten in the lambda of
1731 'map_breakpoint_numbers' below when reading the next line
1732 before we are are done parsing the breakpoint numbers. */
1733 new_arg = arg;
1734 }
1735 arg = new_arg.c_str ();
48649e1b
TT
1736
1737 map_breakpoint_numbers
1738 (arg, [&] (breakpoint *b)
1739 {
999700cd 1740 if (!cmd_read)
48649e1b 1741 {
999700cd 1742 gdb_assert (cmd == NULL);
48649e1b 1743 if (control != NULL)
12973681 1744 cmd = control->body_list_0;
48649e1b
TT
1745 else
1746 {
81b1e71c
TT
1747 std::string str
1748 = string_printf (_("Type commands for breakpoint(s) "
1749 "%s, one per line."),
1750 arg);
48649e1b 1751
60b3cef2
TT
1752 auto do_validate = [=] (const char *line)
1753 {
01bccc56
TT
1754 tracepoint *t
1755 = gdb::checked_static_cast<tracepoint *> (b);
1756 validate_actionline (line, t);
60b3cef2
TT
1757 };
1758 gdb::function_view<void (const char *)> validator;
1759 if (is_tracepoint (b))
1760 validator = do_validate;
1761
1762 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1763 }
999700cd 1764 cmd_read = true;
48649e1b
TT
1765 }
1766
1767 /* If a breakpoint was on the list more than once, we don't need to
1768 do anything. */
1769 if (b->commands != cmd)
1770 {
d1b0a7bf 1771 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1772 b->commands = cmd;
19081eb5 1773 notify_breakpoint_modified (b);
48649e1b
TT
1774 }
1775 });
95a42b64
TT
1776}
1777
1778static void
0b39b52e 1779commands_command (const char *arg, int from_tty)
95a42b64
TT
1780{
1781 commands_command_1 (arg, from_tty, NULL);
c906108c 1782}
40c03ae8
EZ
1783
1784/* Like commands_command, but instead of reading the commands from
1785 input stream, takes them from an already parsed command structure.
1786
1787 This is used by cli-script.c to DTRT with breakpoint commands
1788 that are part of if and while bodies. */
1789enum command_control_type
896b6bda 1790commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1791{
95a42b64
TT
1792 commands_command_1 (arg, 0, cmd);
1793 return simple_control;
40c03ae8 1794}
876fa593 1795
d8de7963 1796/* Return true if BL->TARGET_INFO contains valid information. */
876fa593 1797
d8de7963 1798static bool
876fa593
JK
1799bp_location_has_shadow (struct bp_location *bl)
1800{
1801 if (bl->loc_type != bp_loc_software_breakpoint)
d8de7963 1802 return false;
876fa593 1803 if (!bl->inserted)
d8de7963 1804 return false;
876fa593 1805 if (bl->target_info.shadow_len == 0)
e5dd4106 1806 /* BL isn't valid, or doesn't shadow memory. */
d8de7963
AB
1807 return false;
1808 return true;
876fa593
JK
1809}
1810
9d497a19
PA
1811/* Update BUF, which is LEN bytes read from the target address
1812 MEMADDR, by replacing a memory breakpoint with its shadowed
1813 contents.
1814
1815 If READBUF is not NULL, this buffer must not overlap with the of
1816 the breakpoint location's shadow_contents buffer. Otherwise, a
1817 failed assertion internal error will be raised. */
1818
1819static void
1820one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1821 const gdb_byte *writebuf_org,
1822 ULONGEST memaddr, LONGEST len,
1823 struct bp_target_info *target_info,
1824 struct gdbarch *gdbarch)
1825{
1826 /* Now do full processing of the found relevant range of elements. */
1827 CORE_ADDR bp_addr = 0;
1828 int bp_size = 0;
1829 int bptoffset = 0;
1830
1831 if (!breakpoint_address_match (target_info->placed_address_space, 0,
f9582a22 1832 current_program_space->aspace.get (), 0))
9d497a19
PA
1833 {
1834 /* The breakpoint is inserted in a different address space. */
1835 return;
1836 }
1837
1838 /* Addresses and length of the part of the breakpoint that
1839 we need to copy. */
1840 bp_addr = target_info->placed_address;
1841 bp_size = target_info->shadow_len;
1842
1843 if (bp_addr + bp_size <= memaddr)
1844 {
1845 /* The breakpoint is entirely before the chunk of memory we are
1846 reading. */
1847 return;
1848 }
1849
1850 if (bp_addr >= memaddr + len)
1851 {
1852 /* The breakpoint is entirely after the chunk of memory we are
1853 reading. */
1854 return;
1855 }
1856
1857 /* Offset within shadow_contents. */
1858 if (bp_addr < memaddr)
1859 {
1860 /* Only copy the second part of the breakpoint. */
1861 bp_size -= memaddr - bp_addr;
1862 bptoffset = memaddr - bp_addr;
1863 bp_addr = memaddr;
1864 }
1865
1866 if (bp_addr + bp_size > memaddr + len)
1867 {
1868 /* Only copy the first part of the breakpoint. */
1869 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1870 }
1871
1872 if (readbuf != NULL)
1873 {
1874 /* Verify that the readbuf buffer does not overlap with the
1875 shadow_contents buffer. */
1876 gdb_assert (target_info->shadow_contents >= readbuf + len
1877 || readbuf >= (target_info->shadow_contents
1878 + target_info->shadow_len));
1879
1880 /* Update the read buffer with this inserted breakpoint's
1881 shadow. */
1882 memcpy (readbuf + bp_addr - memaddr,
1883 target_info->shadow_contents + bptoffset, bp_size);
1884 }
1885 else
1886 {
1887 const unsigned char *bp;
0d5ed153
MR
1888 CORE_ADDR addr = target_info->reqstd_address;
1889 int placed_size;
9d497a19
PA
1890
1891 /* Update the shadow with what we want to write to memory. */
1892 memcpy (target_info->shadow_contents + bptoffset,
1893 writebuf_org + bp_addr - memaddr, bp_size);
1894
1895 /* Determine appropriate breakpoint contents and size for this
1896 address. */
0d5ed153 1897 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1898
1899 /* Update the final write buffer with this inserted
1900 breakpoint's INSN. */
1901 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1902 }
1903}
1904
8defab1a 1905/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1906 by replacing any memory breakpoints with their shadowed contents.
1907
35c63cd8
JB
1908 If READBUF is not NULL, this buffer must not overlap with any of
1909 the breakpoint location's shadow_contents buffers. Otherwise,
1910 a failed assertion internal error will be raised.
1911
876fa593 1912 The range of shadowed area by each bp_location is:
f5336ca5
PA
1913 bl->address - bp_locations_placed_address_before_address_max
1914 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1915 The range we were requested to resolve shadows for is:
1916 memaddr ... memaddr + len
1917 Thus the safe cutoff boundaries for performance optimization are
35df4500 1918 memaddr + len <= (bl->address
f5336ca5 1919 - bp_locations_placed_address_before_address_max)
876fa593 1920 and:
f5336ca5 1921 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1922
8defab1a 1923void
f0ba3972
PA
1924breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1925 const gdb_byte *writebuf_org,
1926 ULONGEST memaddr, LONGEST len)
c906108c 1927{
4a64f543
MS
1928 /* Left boundary, right boundary and median element of our binary
1929 search. */
876fa593
JK
1930 unsigned bc_l, bc_r, bc;
1931
4a64f543
MS
1932 /* Find BC_L which is a leftmost element which may affect BUF
1933 content. It is safe to report lower value but a failure to
1934 report higher one. */
876fa593
JK
1935
1936 bc_l = 0;
5d51cd5d 1937 bc_r = bp_locations.size ();
876fa593
JK
1938 while (bc_l + 1 < bc_r)
1939 {
35df4500 1940 struct bp_location *bl;
876fa593
JK
1941
1942 bc = (bc_l + bc_r) / 2;
f5336ca5 1943 bl = bp_locations[bc];
876fa593 1944
4a64f543
MS
1945 /* Check first BL->ADDRESS will not overflow due to the added
1946 constant. Then advance the left boundary only if we are sure
1947 the BC element can in no way affect the BUF content (MEMADDR
1948 to MEMADDR + LEN range).
876fa593 1949
f5336ca5 1950 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1951 offset so that we cannot miss a breakpoint with its shadow
1952 range tail still reaching MEMADDR. */
c5aa993b 1953
f5336ca5 1954 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1955 >= bl->address)
f5336ca5 1956 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1957 <= memaddr))
876fa593
JK
1958 bc_l = bc;
1959 else
1960 bc_r = bc;
1961 }
1962
128070bb
PA
1963 /* Due to the binary search above, we need to make sure we pick the
1964 first location that's at BC_L's address. E.g., if there are
1965 multiple locations at the same address, BC_L may end up pointing
1966 at a duplicate location, and miss the "master"/"inserted"
1967 location. Say, given locations L1, L2 and L3 at addresses A and
1968 B:
1969
1970 L1@A, L2@A, L3@B, ...
1971
1972 BC_L could end up pointing at location L2, while the "master"
1973 location could be L1. Since the `loc->inserted' flag is only set
1974 on "master" locations, we'd forget to restore the shadow of L1
1975 and L2. */
1976 while (bc_l > 0
f5336ca5 1977 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1978 bc_l--;
1979
876fa593
JK
1980 /* Now do full processing of the found relevant range of elements. */
1981
5d51cd5d 1982 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1983 {
1984 struct bp_location *bl = bp_locations[bc];
876fa593 1985
01add95b
SM
1986 /* bp_location array has BL->OWNER always non-NULL. */
1987 if (bl->owner->type == bp_none)
1988 warning (_("reading through apparently deleted breakpoint #%d?"),
1989 bl->owner->number);
ffce0d52 1990
01add95b
SM
1991 /* Performance optimization: any further element can no longer affect BUF
1992 content. */
876fa593 1993
01add95b
SM
1994 if (bl->address >= bp_locations_placed_address_before_address_max
1995 && (memaddr + len
1996 <= (bl->address
1997 - bp_locations_placed_address_before_address_max)))
1998 break;
876fa593 1999
01add95b
SM
2000 if (!bp_location_has_shadow (bl))
2001 continue;
6c95b8df 2002
01add95b
SM
2003 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
2004 memaddr, len, &bl->target_info, bl->gdbarch);
2005 }
c906108c 2006}
9d497a19 2007
f2478a7e 2008/* See breakpoint.h. */
b775012e 2009
f2478a7e 2010bool
b775012e
LM
2011is_breakpoint (const struct breakpoint *bpt)
2012{
2013 return (bpt->type == bp_breakpoint
e7e0cddf
SS
2014 || bpt->type == bp_hardware_breakpoint
2015 || bpt->type == bp_dprintf);
b775012e
LM
2016}
2017
60e1c644
PA
2018/* Return true if BPT is of any hardware watchpoint kind. */
2019
f2478a7e 2020static bool
d77f58be 2021is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
2022{
2023 return (bpt->type == bp_hardware_watchpoint
2024 || bpt->type == bp_read_watchpoint
2025 || bpt->type == bp_access_watchpoint);
2026}
7270d8f2 2027
f2478a7e 2028/* See breakpoint.h. */
60e1c644 2029
f2478a7e 2030bool
d77f58be 2031is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
2032{
2033 return (is_hardware_watchpoint (bpt)
2034 || bpt->type == bp_watchpoint);
2035}
2036
3a5c3e22
PA
2037/* Returns true if the current thread and its running state are safe
2038 to evaluate or update watchpoint B. Watchpoints on local
2039 expressions need to be evaluated in the context of the thread that
2040 was current when the watchpoint was created, and, that thread needs
2041 to be stopped to be able to select the correct frame context.
2042 Watchpoints on global expressions can be evaluated on any thread,
2043 and in any state. It is presently left to the target allowing
2044 memory accesses when threads are running. */
f6bc2008 2045
d8de7963 2046static bool
3a5c3e22 2047watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 2048{
c1fc2657 2049 return (b->pspace == current_program_space
d7e15655
TT
2050 && (b->watchpoint_thread == null_ptid
2051 || (inferior_ptid == b->watchpoint_thread
611841bb 2052 && !inferior_thread ()->executing ())));
f6bc2008
PA
2053}
2054
d0fb5eae
JK
2055/* Set watchpoint B to disp_del_at_next_stop, even including its possible
2056 associated bp_watchpoint_scope breakpoint. */
2057
2058static void
3a5c3e22 2059watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 2060{
c1fc2657 2061 if (w->related_breakpoint != w)
d0fb5eae 2062 {
c1fc2657
SM
2063 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
2064 gdb_assert (w->related_breakpoint->related_breakpoint == w);
2065 w->related_breakpoint->disposition = disp_del_at_next_stop;
2066 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
2067 w->related_breakpoint = w;
d0fb5eae 2068 }
c1fc2657 2069 w->disposition = disp_del_at_next_stop;
6e96d8a9 2070 disable_breakpoint (w);
d0fb5eae
JK
2071}
2072
bb9d5f81
PP
2073/* Extract a bitfield value from value VAL using the bit parameters contained in
2074 watchpoint W. */
2075
2076static struct value *
2077extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
2078{
2079 struct value *bit_val;
2080
2081 if (val == NULL)
2082 return NULL;
2083
317c3ed9 2084 bit_val = value::allocate (val->type ());
bb9d5f81 2085
6c49729e
TT
2086 val->unpack_bitfield (bit_val,
2087 w->val_bitpos,
2088 w->val_bitsize,
2089 val->contents_for_printing ().data (),
2090 val->offset ());
bb9d5f81
PP
2091
2092 return bit_val;
2093}
2094
6e14e441
PA
2095/* Allocate a dummy location and add it to B. This is required
2096 because bpstat_stop_status requires a location to be able to report
2097 stops. */
c6d81124
PA
2098
2099static void
6e14e441
PA
2100add_dummy_location (struct breakpoint *b,
2101 struct program_space *pspace)
c6d81124 2102{
9dc1523b 2103 gdb_assert (!b->has_locations ());
c6d81124 2104
20afe380
SM
2105 bp_location *loc = new bp_location (b, bp_loc_other);
2106 loc->pspace = pspace;
2107 b->add_location (*loc);
c6d81124
PA
2108}
2109
567e1b4e 2110/* Assuming that B is a watchpoint:
d8de7963 2111 - Reparse watchpoint expression, if REPARSE is true
a5606eee 2112 - Evaluate expression and store the result in B->val
567e1b4e
JB
2113 - Evaluate the condition if there is one, and store the result
2114 in b->loc->cond.
a5606eee
VP
2115 - Update the list of values that must be watched in B->loc.
2116
4a64f543
MS
2117 If the watchpoint disposition is disp_del_at_next_stop, then do
2118 nothing. If this is local watchpoint that is out of scope, delete
2119 it.
2120
2121 Even with `set breakpoint always-inserted on' the watchpoints are
2122 removed + inserted on each stop here. Normal breakpoints must
2123 never be removed because they might be missed by a running thread
2124 when debugging in non-stop mode. On the other hand, hardware
2125 watchpoints (is_hardware_watchpoint; processed here) are specific
2126 to each LWP since they are stored in each LWP's hardware debug
2127 registers. Therefore, such LWP must be stopped first in order to
2128 be able to modify its hardware watchpoints.
2129
2130 Hardware watchpoints must be reset exactly once after being
2131 presented to the user. It cannot be done sooner, because it would
2132 reset the data used to present the watchpoint hit to the user. And
2133 it must not be done later because it could display the same single
2134 watchpoint hit during multiple GDB stops. Note that the latter is
2135 relevant only to the hardware watchpoint types bp_read_watchpoint
2136 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
2137 not user-visible - its hit is suppressed if the memory content has
2138 not changed.
2139
2140 The following constraints influence the location where we can reset
2141 hardware watchpoints:
2142
2143 * target_stopped_by_watchpoint and target_stopped_data_address are
2144 called several times when GDB stops.
2145
2146 [linux]
2147 * Multiple hardware watchpoints can be hit at the same time,
2148 causing GDB to stop. GDB only presents one hardware watchpoint
2149 hit at a time as the reason for stopping, and all the other hits
2150 are presented later, one after the other, each time the user
2151 requests the execution to be resumed. Execution is not resumed
2152 for the threads still having pending hit event stored in
2153 LWP_INFO->STATUS. While the watchpoint is already removed from
2154 the inferior on the first stop the thread hit event is kept being
2155 reported from its cached value by linux_nat_stopped_data_address
2156 until the real thread resume happens after the watchpoint gets
2157 presented and thus its LWP_INFO->STATUS gets reset.
2158
2159 Therefore the hardware watchpoint hit can get safely reset on the
2160 watchpoint removal from inferior. */
a79d3c27 2161
b40ce68a 2162static void
d8de7963 2163update_watchpoint (struct watchpoint *b, bool reparse)
7270d8f2 2164{
705b6305 2165 bool within_current_scope;
a5606eee 2166
f6bc2008
PA
2167 /* If this is a local watchpoint, we only want to check if the
2168 watchpoint frame is in scope if the current thread is the thread
2169 that was used to create the watchpoint. */
2170 if (!watchpoint_in_thread_scope (b))
2171 return;
2172
c1fc2657 2173 if (b->disposition == disp_del_at_next_stop)
a5606eee 2174 return;
705b6305 2175
6b09f134 2176 std::optional<scoped_restore_selected_frame> restore_frame;
a5606eee
VP
2177
2178 /* Determine if the watchpoint is within scope. */
2179 if (b->exp_valid_block == NULL)
705b6305 2180 within_current_scope = true;
a5606eee
VP
2181 else
2182 {
bd2b40ac 2183 frame_info_ptr fi = get_current_frame ();
b5db5dfc
UW
2184 struct gdbarch *frame_arch = get_frame_arch (fi);
2185 CORE_ADDR frame_pc = get_frame_pc (fi);
2186
c9cf6e20
MG
2187 /* If we're at a point where the stack has been destroyed
2188 (e.g. in a function epilogue), unwinding may not work
2189 properly. Do not attempt to recreate locations at this
b5db5dfc 2190 point. See similar comments in watchpoint_check. */
c9cf6e20 2191 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 2192 return;
66076460
DJ
2193
2194 /* Save the current frame's ID so we can restore it after
dda83cd7 2195 evaluating the watchpoint expression on its own frame. */
66076460 2196 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
2197 took a frame parameter, so that we didn't have to change the
2198 selected frame. */
705b6305 2199 restore_frame.emplace ();
66076460 2200
a5606eee
VP
2201 fi = frame_find_by_id (b->watchpoint_frame);
2202 within_current_scope = (fi != NULL);
2203 if (within_current_scope)
2204 select_frame (fi);
2205 }
2206
b5db5dfc
UW
2207 /* We don't free locations. They are stored in the bp_location array
2208 and update_global_location_list will eventually delete them and
2209 remove breakpoints if needed. */
20afe380 2210 b->clear_locations ();
b5db5dfc 2211
a5606eee
VP
2212 if (within_current_scope && reparse)
2213 {
bbc13ae3 2214 const char *s;
d63d0675 2215
4d01a485 2216 b->exp.reset ();
a4c50be3
TT
2217 s = (b->exp_string_reparse
2218 ? b->exp_string_reparse.get ()
2219 : b->exp_string.get ());
1bb9788d 2220 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
2221 /* If the meaning of expression itself changed, the old value is
2222 no longer relevant. We don't want to report a watchpoint hit
2223 to the user when the old value and the new value may actually
2224 be completely different objects. */
fa4727a6 2225 b->val = NULL;
4c1d86d9 2226 b->val_valid = false;
60e1c644
PA
2227
2228 /* Note that unlike with breakpoints, the watchpoint's condition
2229 expression is stored in the breakpoint object, not in the
2230 locations (re)created below. */
c1fc2657 2231 if (b->cond_string != NULL)
60e1c644 2232 {
4d01a485 2233 b->cond_exp.reset ();
60e1c644 2234
6f781ee3 2235 s = b->cond_string.get ();
1bb9788d 2236 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 2237 }
a5606eee 2238 }
a5606eee
VP
2239
2240 /* If we failed to parse the expression, for example because
2241 it refers to a global variable in a not-yet-loaded shared library,
2242 don't try to insert watchpoint. We don't automatically delete
2243 such watchpoint, though, since failure to parse expression
2244 is different from out-of-scope watchpoint. */
55f6301a 2245 if (!target_has_execution ())
2d134ed3
PA
2246 {
2247 /* Without execution, memory can't change. No use to try and
2248 set watchpoint locations. The watchpoint will be reset when
2249 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
2250 if (!can_use_hw_watchpoints)
2251 {
c47614fe 2252 if (b->works_in_software_mode ())
c1fc2657 2253 b->type = bp_watchpoint;
e8369a73 2254 else
638aa5a1
AB
2255 error (_("Can't set read/access watchpoint when "
2256 "hardware watchpoints are disabled."));
e8369a73 2257 }
2d134ed3
PA
2258 }
2259 else if (within_current_scope && b->exp)
a5606eee 2260 {
a6535de1 2261 std::vector<value_ref_ptr> val_chain;
8d49165d 2262 struct value *v, *result;
c223d373 2263 struct program_space *wp_pspace;
a5606eee 2264
1eaebe02 2265 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 2266 &val_chain, false);
a5606eee 2267
a5606eee
VP
2268 /* Avoid setting b->val if it's already set. The meaning of
2269 b->val is 'the last value' user saw, and we should update
2270 it only if we reported that last value to user. As it
9c06b0b4
TJB
2271 happens, the code that reports it updates b->val directly.
2272 We don't keep track of the memory value for masked
2273 watchpoints. */
c1fc2657 2274 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 2275 {
bb9d5f81 2276 if (b->val_bitsize != 0)
850645cf
TT
2277 v = extract_bitfield_from_watchpoint_value (b, v);
2278 b->val = release_value (v);
4c1d86d9 2279 b->val_valid = true;
fa4727a6 2280 }
a5606eee 2281
c223d373
PA
2282 if (b->exp_valid_block == nullptr)
2283 wp_pspace = current_program_space;
2284 else
2285 wp_pspace = get_frame_program_space (get_selected_frame (NULL));
2d134ed3 2286
a5606eee 2287 /* Look at each value on the value chain. */
a6535de1
TT
2288 gdb_assert (!val_chain.empty ());
2289 for (const value_ref_ptr &iter : val_chain)
a5606eee 2290 {
a6535de1
TT
2291 v = iter.get ();
2292
a5606eee
VP
2293 /* If it's a memory location, and GDB actually needed
2294 its contents to evaluate the expression, then we
fa4727a6
DJ
2295 must watch it. If the first value returned is
2296 still lazy, that means an error occurred reading it;
2297 watch it anyway in case it becomes readable. */
736355f2 2298 if (v->lval () == lval_memory
3ee3b270 2299 && (v == val_chain[0] || ! v->lazy ()))
a5606eee 2300 {
d0c97917 2301 struct type *vtype = check_typedef (v->type ());
7270d8f2 2302
a5606eee
VP
2303 /* We only watch structs and arrays if user asked
2304 for it explicitly, never if they just happen to
2305 appear in the middle of some value chain. */
fa4727a6 2306 if (v == result
78134374
SM
2307 || (vtype->code () != TYPE_CODE_STRUCT
2308 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
2309 {
2310 CORE_ADDR addr;
f486487f 2311 enum target_hw_bp_type type;
bb9d5f81
PP
2312 int bitpos = 0, bitsize = 0;
2313
f49d5fa2 2314 if (v->bitsize () != 0)
bb9d5f81
PP
2315 {
2316 /* Extract the bit parameters out from the bitfield
2317 sub-expression. */
5011c493 2318 bitpos = v->bitpos ();
f49d5fa2 2319 bitsize = v->bitsize ();
bb9d5f81
PP
2320 }
2321 else if (v == result && b->val_bitsize != 0)
2322 {
2323 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2324 lvalue whose bit parameters are saved in the fields
2325 VAL_BITPOS and VAL_BITSIZE. */
2326 bitpos = b->val_bitpos;
2327 bitsize = b->val_bitsize;
2328 }
a5606eee 2329
9feb2d07 2330 addr = v->address ();
bb9d5f81
PP
2331 if (bitsize != 0)
2332 {
2333 /* Skip the bytes that don't contain the bitfield. */
2334 addr += bitpos / 8;
2335 }
2336
a5606eee 2337 type = hw_write;
c1fc2657 2338 if (b->type == bp_read_watchpoint)
a5606eee 2339 type = hw_read;
c1fc2657 2340 else if (b->type == bp_access_watchpoint)
a5606eee 2341 type = hw_access;
3a5c3e22 2342
20afe380 2343 bp_location *loc = b->allocate_location ();
d0c97917 2344 loc->gdbarch = v->type ()->arch ();
c223d373 2345 loc->pspace = wp_pspace;
d88cb738 2346 loc->address
86bb38ce
CS
2347 = gdbarch_remove_non_address_bits_watchpoint (loc->gdbarch,
2348 addr);
20afe380 2349 b->add_location (*loc);
bb9d5f81
PP
2350
2351 if (bitsize != 0)
2352 {
2353 /* Just cover the bytes that make up the bitfield. */
2354 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2355 }
2356 else
d0c97917 2357 loc->length = v->type ()->length ();
bb9d5f81 2358
a5606eee
VP
2359 loc->watchpoint_type = type;
2360 }
2361 }
9fa40276
TJB
2362 }
2363
07c1c91d
AB
2364 /* Helper function to bundle possibly emitting a warning along with
2365 changing the type of B to bp_watchpoint. */
2366 auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
2367 {
2368 /* Only warn for breakpoints that have been assigned a +ve number,
2369 anything else is either an internal watchpoint (which we don't
2370 currently create) or has not yet been finalized, in which case
2371 this change of type will be occurring before the user is told
2372 the type of this watchpoint. */
2373 if (bp->type == bp_hardware_watchpoint && bp->number > 0)
2374 warning (_("watchpoint %d downgraded to software watchpoint"),
2375 bp->number);
2376 bp->type = bp_watchpoint;
2377 };
2378
9fa40276 2379 /* Change the type of breakpoint between hardware assisted or
bc45f536
CL
2380 an ordinary watchpoint depending on the hardware support and
2381 free hardware slots. Recheck the number of free hardware slots
2382 as the value chain may have changed. */
9fa40276 2383 {
e09342b5 2384 int reg_cnt;
9fa40276 2385 enum bp_loc_type loc_type;
a5606eee 2386
a9634178 2387 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2388
2389 if (reg_cnt)
9fa40276
TJB
2390 {
2391 int i, target_resources_ok, other_type_used;
a1398e0c 2392 enum bptype type;
9fa40276 2393
a9634178
TJB
2394 /* Use an exact watchpoint when there's only one memory region to be
2395 watched, and only one debug register is needed to watch it. */
2396 b->exact = target_exact_watchpoints && reg_cnt == 1;
2397
9fa40276 2398 /* We need to determine how many resources are already
e09342b5
TJB
2399 used for all other hardware watchpoints plus this one
2400 to see if we still have enough resources to also fit
a1398e0c
PA
2401 this watchpoint in as well. */
2402
2403 /* If this is a software watchpoint, we try to turn it
2404 to a hardware one -- count resources as if B was of
2405 hardware watchpoint type. */
c1fc2657 2406 type = b->type;
a1398e0c
PA
2407 if (type == bp_watchpoint)
2408 type = bp_hardware_watchpoint;
2409
2410 /* This watchpoint may or may not have been placed on
2411 the list yet at this point (it won't be in the list
2412 if we're trying to create it for the first time,
2413 through watch_command), so always account for it
2414 manually. */
2415
2416 /* Count resources used by all watchpoints except B. */
c1fc2657 2417 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2418
2419 /* Add in the resources needed for B. */
c1fc2657 2420 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2421
2422 target_resources_ok
2423 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2424 if (target_resources_ok <= 0)
a9634178 2425 {
5a61e176 2426 bool sw_mode = b->works_in_software_mode ();
9c06b0b4
TJB
2427
2428 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2429 error (_("Target does not support this type of "
2430 "hardware watchpoint."));
9c06b0b4
TJB
2431 else if (target_resources_ok < 0 && !sw_mode)
2432 error (_("There are not enough available hardware "
2433 "resources for this watchpoint."));
a1398e0c
PA
2434
2435 /* Downgrade to software watchpoint. */
07c1c91d 2436 change_type_to_bp_watchpoint (b);
a1398e0c
PA
2437 }
2438 else
2439 {
2440 /* If this was a software watchpoint, we've just
2441 found we have enough resources to turn it to a
2442 hardware watchpoint. Otherwise, this is a
2443 nop. */
c1fc2657 2444 b->type = type;
a9634178 2445 }
9fa40276 2446 }
c47614fe 2447 else if (!b->works_in_software_mode ())
638aa5a1
AB
2448 {
2449 if (!can_use_hw_watchpoints)
2450 error (_("Can't set read/access watchpoint when "
2451 "hardware watchpoints are disabled."));
2452 else
2453 error (_("Expression cannot be implemented with "
2454 "read/access watchpoint."));
2455 }
9fa40276 2456 else
07c1c91d 2457 change_type_to_bp_watchpoint (b);
9fa40276 2458
6e14e441 2459 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
9fa40276 2460 : bp_loc_hardware_watchpoint);
b00b30b2
SM
2461
2462 for (bp_location &bl : b->locations ())
2463 bl.loc_type = loc_type;
9fa40276
TJB
2464 }
2465
c7437ca6
PA
2466 /* If a software watchpoint is not watching any memory, then the
2467 above left it without any location set up. But,
2468 bpstat_stop_status requires a location to be able to report
2469 stops, so make sure there's at least a dummy one. */
9dc1523b 2470 if (b->type == bp_watchpoint && !b->has_locations ())
c223d373 2471 add_dummy_location (b, wp_pspace);
a5606eee
VP
2472 }
2473 else if (!within_current_scope)
7270d8f2 2474 {
6cb06a8c 2475 gdb_printf (_("\
ac74f770
MS
2476Watchpoint %d deleted because the program has left the block\n\
2477in which its expression is valid.\n"),
6cb06a8c 2478 b->number);
d0fb5eae 2479 watchpoint_del_at_next_stop (b);
7270d8f2 2480 }
7270d8f2
OF
2481}
2482
d8de7963 2483/* Returns true iff breakpoint location should be
1e4d1764
YQ
2484 inserted in the inferior. We don't differentiate the type of BL's owner
2485 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2486 breakpoint_ops is not defined, because in insert_bp_location,
2487 tracepoint's insert_location will not be called. */
d8de7963
AB
2488
2489static bool
35df4500 2490should_be_inserted (struct bp_location *bl)
74960c60 2491{
35df4500 2492 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
d8de7963 2493 return false;
74960c60 2494
35df4500 2495 if (bl->owner->disposition == disp_del_at_next_stop)
d8de7963 2496 return false;
74960c60 2497
b5fa468f
TBA
2498 if (!bl->enabled || bl->disabled_by_cond
2499 || bl->shlib_disabled || bl->duplicate)
d8de7963 2500 return false;
74960c60 2501
f8eba3c6 2502 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
d8de7963 2503 return false;
f8eba3c6 2504
56710373
PA
2505 /* This is set for example, when we're attached to the parent of a
2506 vfork, and have detached from the child. The child is running
2507 free, and we expect it to do an exec or exit, at which point the
2508 OS makes the parent schedulable again (and the target reports
2509 that the vfork is done). Until the child is done with the shared
2510 memory region, do not insert breakpoints in the parent, otherwise
2511 the child could still trip on the parent's breakpoints. Since
2512 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2513 if (bl->pspace->breakpoints_not_allowed)
d8de7963 2514 return false;
56710373 2515
31e77af2 2516 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2517 location, except if the breakpoint is a single-step breakpoint,
2518 and the breakpoint's thread is the thread which is stepping past
2519 a breakpoint. */
31e77af2
PA
2520 if ((bl->loc_type == bp_loc_software_breakpoint
2521 || bl->loc_type == bp_loc_hardware_breakpoint)
f9582a22 2522 && stepping_past_instruction_at (bl->pspace->aspace.get (),
21edc42f
YQ
2523 bl->address)
2524 /* The single-step breakpoint may be inserted at the location
2525 we're trying to step if the instruction branches to itself.
2526 However, the instruction won't be executed at all and it may
2527 break the semantics of the instruction, for example, the
2528 instruction is a conditional branch or updates some flags.
2529 We can't fix it unless GDB is able to emulate the instruction
2530 or switch to displaced stepping. */
2531 && !(bl->owner->type == bp_single_step
2532 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2533 {
1eb8556f
SM
2534 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2535 paddress (bl->gdbarch, bl->address));
d8de7963 2536 return false;
e558d7c1 2537 }
31e77af2 2538
963f9c80
PA
2539 /* Don't insert watchpoints if we're trying to step past the
2540 instruction that triggered one. */
2541 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2542 && stepping_past_nonsteppable_watchpoint ())
2543 {
1eb8556f 2544 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2545 "skipping watchpoint at %s:%d",
1eb8556f 2546 paddress (bl->gdbarch, bl->address), bl->length);
d8de7963 2547 return false;
963f9c80
PA
2548 }
2549
d8de7963 2550 return true;
74960c60
VP
2551}
2552
934709f0
PW
2553/* Same as should_be_inserted but does the check assuming
2554 that the location is not duplicated. */
2555
aaa141a0 2556static bool
934709f0
PW
2557unduplicated_should_be_inserted (struct bp_location *bl)
2558{
aaa141a0
AB
2559 scoped_restore restore_bl_duplicate
2560 = make_scoped_restore (&bl->duplicate, 0);
934709f0 2561
aaa141a0 2562 return should_be_inserted (bl);
934709f0
PW
2563}
2564
b775012e
LM
2565/* Parses a conditional described by an expression COND into an
2566 agent expression bytecode suitable for evaluation
2567 by the bytecode interpreter. Return NULL if there was
2568 any error during parsing. */
2569
833177a4 2570static agent_expr_up
b775012e
LM
2571parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2572{
833177a4 2573 if (cond == NULL)
b775012e
LM
2574 return NULL;
2575
833177a4
PA
2576 agent_expr_up aexpr;
2577
b775012e
LM
2578 /* We don't want to stop processing, so catch any errors
2579 that may show up. */
a70b8144 2580 try
b775012e 2581 {
036e657b 2582 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2583 }
2584
230d2906 2585 catch (const gdb_exception_error &ex)
b775012e
LM
2586 {
2587 /* If we got here, it means the condition could not be parsed to a valid
2588 bytecode expression and thus can't be evaluated on the target's side.
2589 It's no use iterating through the conditions. */
b775012e
LM
2590 }
2591
2592 /* We have a valid agent expression. */
2593 return aexpr;
2594}
2595
2596/* Based on location BL, create a list of breakpoint conditions to be
2597 passed on to the target. If we have duplicated locations with different
2598 conditions, we will add such conditions to the list. The idea is that the
2599 target will evaluate the list of conditions and will only notify GDB when
2600 one of them is true. */
2601
2602static void
2603build_target_condition_list (struct bp_location *bl)
2604{
d8de7963 2605 bool null_condition_or_parse_error = false;
b775012e 2606 int modified = bl->needs_update;
b775012e 2607
8b4f3082 2608 /* Release conditions left over from a previous insert. */
3cde5c42 2609 bl->target_info.conditions.clear ();
8b4f3082 2610
b775012e
LM
2611 /* This is only meaningful if the target is
2612 evaluating conditions and if the user has
2613 opted for condition evaluation on the target's
2614 side. */
2615 if (gdb_evaluates_breakpoint_condition_p ()
2616 || !target_supports_evaluation_of_breakpoint_conditions ())
2617 return;
2618
e0d9a270
SM
2619 auto loc_range = all_bp_locations_at_addr (bl->address);
2620
b775012e 2621 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2622 conditions or conditions that fail to parse to a valid agent
2623 expression bytecode. If any of these happen, then it's no use to
2624 send conditions to the target since this location will always
2625 trigger and generate a response back to GDB. Note we consider
2626 all locations at the same address irrespective of type, i.e.,
2627 even if the locations aren't considered duplicates (e.g.,
2628 software breakpoint and hardware breakpoint at the same
2629 address). */
e0d9a270 2630 for (bp_location *loc : loc_range)
b775012e 2631 {
b775012e
LM
2632 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2633 {
2634 if (modified)
2635 {
b775012e
LM
2636 /* Re-parse the conditions since something changed. In that
2637 case we already freed the condition bytecodes (see
2638 force_breakpoint_reinsertion). We just
2639 need to parse the condition to bytecodes again. */
833177a4
PA
2640 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2641 loc->cond.get ());
b775012e
LM
2642 }
2643
2644 /* If we have a NULL bytecode expression, it means something
2645 went wrong or we have a null condition expression. */
2646 if (!loc->cond_bytecode)
2647 {
d8de7963 2648 null_condition_or_parse_error = true;
b775012e
LM
2649 break;
2650 }
2651 }
2652 }
2653
2654 /* If any of these happened, it means we will have to evaluate the conditions
2655 for the location's address on gdb's side. It is no use keeping bytecodes
2656 for all the other duplicate locations, thus we free all of them here.
2657
2658 This is so we have a finer control over which locations' conditions are
2659 being evaluated by GDB or the remote stub. */
2660 if (null_condition_or_parse_error)
2661 {
e0d9a270 2662 for (bp_location *loc : loc_range)
b775012e 2663 {
b775012e
LM
2664 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2665 {
2666 /* Only go as far as the first NULL bytecode is
2667 located. */
2668 if (!loc->cond_bytecode)
2669 return;
2670
833177a4 2671 loc->cond_bytecode.reset ();
b775012e
LM
2672 }
2673 }
2674 }
2675
7f32a4d5
PA
2676 /* No NULL conditions or failed bytecode generation. Build a
2677 condition list for this location's address. If we have software
2678 and hardware locations at the same address, they aren't
3bfdcabb 2679 considered duplicates, but we still merge all the conditions
7f32a4d5
PA
2680 anyway, as it's simpler, and doesn't really make a practical
2681 difference. */
e0d9a270
SM
2682 for (bp_location *loc : loc_range)
2683 if (loc->cond
2684 && is_breakpoint (loc->owner)
2685 && loc->pspace->num == bl->pspace->num
2686 && loc->owner->enable_state == bp_enabled
2687 && loc->enabled
2688 && !loc->disabled_by_cond)
2689 {
2690 /* Add the condition to the vector. This will be used later
2691 to send the conditions to the target. */
2692 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2693 }
b775012e
LM
2694
2695 return;
2696}
2697
d3ce09f5
SS
2698/* Parses a command described by string CMD into an agent expression
2699 bytecode suitable for evaluation by the bytecode interpreter.
2700 Return NULL if there was any error during parsing. */
2701
833177a4 2702static agent_expr_up
d3ce09f5
SS
2703parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2704{
bbc13ae3
KS
2705 const char *cmdrest;
2706 const char *format_start, *format_end;
d3ce09f5
SS
2707 struct gdbarch *gdbarch = get_current_arch ();
2708
833177a4 2709 if (cmd == NULL)
d3ce09f5
SS
2710 return NULL;
2711
2712 cmdrest = cmd;
2713
2714 if (*cmdrest == ',')
2715 ++cmdrest;
f1735a53 2716 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2717
2718 if (*cmdrest++ != '"')
2719 error (_("No format string following the location"));
2720
2721 format_start = cmdrest;
2722
8e481c3b 2723 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2724
2725 format_end = cmdrest;
2726
2727 if (*cmdrest++ != '"')
2728 error (_("Bad format string, non-terminated '\"'."));
2729
f1735a53 2730 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2731
2732 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2733 error (_("Invalid argument syntax"));
2734
2735 if (*cmdrest == ',')
2736 cmdrest++;
f1735a53 2737 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2738
2739 /* For each argument, make an expression. */
2740
8e481c3b 2741 std::vector<struct expression *> argvec;
d3ce09f5
SS
2742 while (*cmdrest != '\0')
2743 {
bbc13ae3 2744 const char *cmd1;
d3ce09f5
SS
2745
2746 cmd1 = cmdrest;
b8c03634
TT
2747 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope),
2748 PARSER_COMMA_TERMINATES);
8e481c3b 2749 argvec.push_back (expr.release ());
d3ce09f5
SS
2750 cmdrest = cmd1;
2751 if (*cmdrest == ',')
2752 ++cmdrest;
2753 }
2754
833177a4
PA
2755 agent_expr_up aexpr;
2756
d3ce09f5
SS
2757 /* We don't want to stop processing, so catch any errors
2758 that may show up. */
a70b8144 2759 try
d3ce09f5 2760 {
036e657b
JB
2761 aexpr = gen_printf (scope, gdbarch, 0, 0,
2762 format_start, format_end - format_start,
8e481c3b 2763 argvec.size (), argvec.data ());
d3ce09f5 2764 }
230d2906 2765 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2766 {
2767 /* If we got here, it means the command could not be parsed to a valid
2768 bytecode expression and thus can't be evaluated on the target's side.
2769 It's no use iterating through the other commands. */
d3ce09f5 2770 }
492d29ea 2771
d3ce09f5
SS
2772 /* We have a valid agent expression, return it. */
2773 return aexpr;
2774}
2775
2776/* Based on location BL, create a list of breakpoint commands to be
2777 passed on to the target. If we have duplicated locations with
2778 different commands, we will add any such to the list. */
2779
2780static void
2781build_target_command_list (struct bp_location *bl)
2782{
d8de7963 2783 bool null_command_or_parse_error = false;
d3ce09f5 2784 int modified = bl->needs_update;
d3ce09f5 2785
3cde5c42
PA
2786 /* Clear commands left over from a previous insert. */
2787 bl->target_info.tcommands.clear ();
8b4f3082 2788
41fac0cf 2789 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2790 return;
2791
41fac0cf
PA
2792 /* For now, limit to agent-style dprintf breakpoints. */
2793 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2794 return;
2795
e0d9a270
SM
2796 auto loc_range = all_bp_locations_at_addr (bl->address);
2797
7f32a4d5
PA
2798 /* For now, if we have any location at the same address that isn't a
2799 dprintf, don't install the target-side commands, as that would
2800 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2801 control. */
e0d9a270
SM
2802 for (bp_location *loc : loc_range)
2803 if (is_breakpoint (loc->owner)
2804 && loc->pspace->num == bl->pspace->num
2805 && loc->owner->type != bp_dprintf)
2806 return;
41fac0cf 2807
d3ce09f5
SS
2808 /* Do a first pass to check for locations with no assigned
2809 conditions or conditions that fail to parse to a valid agent expression
2810 bytecode. If any of these happen, then it's no use to send conditions
2811 to the target since this location will always trigger and generate a
2812 response back to GDB. */
e0d9a270 2813 for (bp_location *loc : loc_range)
d3ce09f5 2814 {
d3ce09f5
SS
2815 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2816 {
2817 if (modified)
2818 {
d3ce09f5
SS
2819 /* Re-parse the commands since something changed. In that
2820 case we already freed the command bytecodes (see
2821 force_breakpoint_reinsertion). We just
2822 need to parse the command to bytecodes again. */
833177a4
PA
2823 loc->cmd_bytecode
2824 = parse_cmd_to_aexpr (bl->address,
6f781ee3 2825 loc->owner->extra_string.get ());
d3ce09f5
SS
2826 }
2827
2828 /* If we have a NULL bytecode expression, it means something
2829 went wrong or we have a null command expression. */
2830 if (!loc->cmd_bytecode)
2831 {
d8de7963 2832 null_command_or_parse_error = true;
d3ce09f5
SS
2833 break;
2834 }
2835 }
2836 }
2837
2838 /* If anything failed, then we're not doing target-side commands,
2839 and so clean up. */
2840 if (null_command_or_parse_error)
2841 {
e0d9a270
SM
2842 for (bp_location *loc : loc_range)
2843 if (is_breakpoint (loc->owner)
2844 && loc->pspace->num == bl->pspace->num)
2845 {
2846 /* Only go as far as the first NULL bytecode is
2847 located. */
2848 if (loc->cmd_bytecode == NULL)
2849 return;
d3ce09f5 2850
e0d9a270
SM
2851 loc->cmd_bytecode.reset ();
2852 }
d3ce09f5
SS
2853 }
2854
7f32a4d5
PA
2855 /* No NULL commands or failed bytecode generation. Build a command
2856 list for all duplicate locations at this location's address.
2857 Note that here we must care for whether the breakpoint location
2858 types are considered duplicates, otherwise, say, if we have a
2859 software and hardware location at the same address, the target
2860 could end up running the commands twice. For the moment, we only
2861 support targets-side commands with dprintf, but it doesn't hurt
2862 to be pedantically correct in case that changes. */
e0d9a270
SM
2863 for (bp_location *loc : loc_range)
2864 if (breakpoint_locations_match (bl, loc)
2865 && loc->owner->extra_string
2866 && is_breakpoint (loc->owner)
2867 && loc->pspace->num == bl->pspace->num
2868 && loc->owner->enable_state == bp_enabled
2869 && loc->enabled
2870 && !loc->disabled_by_cond)
2871 {
2872 /* Add the command to the vector. This will be used later
2873 to send the commands to the target. */
2874 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2875 }
d3ce09f5
SS
2876
2877 bl->target_info.persist = 0;
2878 /* Maybe flag this location as persistent. */
2879 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2880 bl->target_info.persist = 1;
2881}
2882
833b7ab5
YQ
2883/* Return the kind of breakpoint on address *ADDR. Get the kind
2884 of breakpoint according to ADDR except single-step breakpoint.
2885 Get the kind of single-step breakpoint according to the current
2886 registers state. */
cd6c3b4f
YQ
2887
2888static int
b2ff9ed3 2889breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
cd6c3b4f 2890{
833b7ab5
YQ
2891 if (bl->owner->type == bp_single_step)
2892 {
2893 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2894 struct regcache *regcache;
2895
00431a78 2896 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2897
2898 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2899 regcache, addr);
2900 }
2901 else
2902 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2903}
2904
47e2c30a
TV
2905/* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2906 E is either the currently handled exception, or a copy, or a sliced copy,
2907 so we can't rethrow that one, but we can use it to inspect the properties
2908 of the currently handled exception. */
2909
2910static void
2911rethrow_on_target_close_error (const gdb_exception &e)
2912{
2913 if (e.reason == 0)
2914 return;
2915 /* Can't set the breakpoint. */
2916
2917 if (e.error != TARGET_CLOSE_ERROR)
2918 return;
2919
2920 /* If the target has closed then it will have deleted any breakpoints
2921 inserted within the target inferior, as a result any further attempts
2922 to interact with the breakpoint objects is not possible. Just rethrow
2923 the error. Don't use e to rethrow, to prevent object slicing of the
2924 exception. */
2925 throw;
2926}
b2829fcf 2927
35df4500
TJB
2928/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2929 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2930 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2931 Returns 0 for success, 1 if the bp_location type is not supported or
2932 -1 for failure.
879bfdc2 2933
4a64f543
MS
2934 NOTE drow/2003-09-09: This routine could be broken down to an
2935 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2936static int
35df4500 2937insert_bp_location (struct bp_location *bl,
26bb91f3 2938 struct ui_file *tmp_error_stream,
3fbb6ffa 2939 int *disabled_breaks,
dd61ec5c
MW
2940 int *hw_breakpoint_error,
2941 int *hw_bp_error_explained_already)
879bfdc2 2942{
cc06b668 2943 gdb_exception bp_excpt;
879bfdc2 2944
b775012e 2945 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2946 return 0;
2947
3cdc2d7e
MS
2948 breakpoint_debug_printf ("%s", breakpoint_location_address_str (bl).c_str ());
2949
35c63cd8
JB
2950 /* Note we don't initialize bl->target_info, as that wipes out
2951 the breakpoint location's shadow_contents if the breakpoint
2952 is still inserted at that location. This in turn breaks
2953 target_read_memory which depends on these buffers when
2954 a memory read is requested at the breakpoint location:
2955 Once the target_info has been wiped, we fail to see that
2956 we have a breakpoint inserted at that address and thus
2957 read the breakpoint instead of returning the data saved in
2958 the breakpoint location's shadow contents. */
0d5ed153 2959 bl->target_info.reqstd_address = bl->address;
f9582a22 2960 bl->target_info.placed_address_space = bl->pspace->aspace.get ();
f1310107 2961 bl->target_info.length = bl->length;
8181d85f 2962
b775012e
LM
2963 /* When working with target-side conditions, we must pass all the conditions
2964 for the same breakpoint address down to the target since GDB will not
2965 insert those locations. With a list of breakpoint conditions, the target
2966 can decide when to stop and notify GDB. */
2967
2968 if (is_breakpoint (bl->owner))
2969 {
2970 build_target_condition_list (bl);
d3ce09f5
SS
2971 build_target_command_list (bl);
2972 /* Reset the modification marker. */
b775012e
LM
2973 bl->needs_update = 0;
2974 }
2975
7f32a4d5
PA
2976 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2977 set at a read-only address, then a breakpoint location will have
2978 been changed to hardware breakpoint before we get here. If it is
2979 "off" however, error out before actually trying to insert the
2980 breakpoint, with a nicer error message. */
35df4500 2981 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2982 && !automatic_hardware_breakpoints)
879bfdc2 2983 {
7f32a4d5 2984 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2985
7f32a4d5
PA
2986 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2987 {
6cb06a8c
TT
2988 gdb_printf (tmp_error_stream,
2989 _("Cannot insert breakpoint %d.\n"
2990 "Cannot set software breakpoint "
2991 "at read-only address %s\n"),
2992 bl->owner->number,
2993 paddress (bl->gdbarch, bl->address));
7f32a4d5 2994 return 1;
765dc015 2995 }
7f32a4d5
PA
2996 }
2997
2998 if (bl->loc_type == bp_loc_software_breakpoint
2999 || bl->loc_type == bp_loc_hardware_breakpoint)
3000 {
879bfdc2
DJ
3001 /* First check to see if we have to handle an overlay. */
3002 if (overlay_debugging == ovly_off
35df4500
TJB
3003 || bl->section == NULL
3004 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
3005 {
3006 /* No overlay handling: just set the breakpoint. */
a70b8144 3007 try
dd61ec5c 3008 {
0000e5cc
PA
3009 int val;
3010
c47614fe 3011 val = bl->owner->insert_location (bl);
0000e5cc 3012 if (val)
688fca4f 3013 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 3014 }
94aeb44b 3015 catch (gdb_exception &e)
dd61ec5c 3016 {
47e2c30a 3017 rethrow_on_target_close_error (e);
94aeb44b 3018 bp_excpt = std::move (e);
dd61ec5c 3019 }
879bfdc2
DJ
3020 }
3021 else
3022 {
4a64f543 3023 /* This breakpoint is in an overlay section.
879bfdc2
DJ
3024 Shall we set a breakpoint at the LMA? */
3025 if (!overlay_events_enabled)
3026 {
3027 /* Yes -- overlay event support is not active,
3028 so we must try to set a breakpoint at the LMA.
3029 This will not work for a hardware breakpoint. */
35df4500 3030 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 3031 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 3032 bl->owner->number);
879bfdc2
DJ
3033 else
3034 {
35df4500
TJB
3035 CORE_ADDR addr = overlay_unmapped_address (bl->address,
3036 bl->section);
879bfdc2 3037 /* Set a software (trap) breakpoint at the LMA. */
35df4500 3038 bl->overlay_target_info = bl->target_info;
0d5ed153 3039 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
3040
3041 /* No overlay handling: just set the breakpoint. */
a70b8144 3042 try
0000e5cc
PA
3043 {
3044 int val;
3045
579c6ad9 3046 bl->overlay_target_info.kind
cd6c3b4f
YQ
3047 = breakpoint_kind (bl, &addr);
3048 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
3049 val = target_insert_breakpoint (bl->gdbarch,
3050 &bl->overlay_target_info);
3051 if (val)
688fca4f
PA
3052 bp_excpt
3053 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 3054 }
94aeb44b 3055 catch (gdb_exception &e)
0000e5cc 3056 {
47e2c30a 3057 rethrow_on_target_close_error (e);
94aeb44b 3058 bp_excpt = std::move (e);
0000e5cc
PA
3059 }
3060
688fca4f 3061 if (bp_excpt.reason != 0)
6cb06a8c
TT
3062 gdb_printf (tmp_error_stream,
3063 "Overlay breakpoint %d "
3064 "failed: in ROM?\n",
3065 bl->owner->number);
879bfdc2
DJ
3066 }
3067 }
3068 /* Shall we set a breakpoint at the VMA? */
35df4500 3069 if (section_is_mapped (bl->section))
879bfdc2
DJ
3070 {
3071 /* Yes. This overlay section is mapped into memory. */
a70b8144 3072 try
dda83cd7 3073 {
0000e5cc
PA
3074 int val;
3075
c47614fe 3076 val = bl->owner->insert_location (bl);
0000e5cc 3077 if (val)
688fca4f 3078 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 3079 }
b1ffd112 3080 catch (gdb_exception_error &e)
dda83cd7 3081 {
47e2c30a 3082 rethrow_on_target_close_error (e);
94aeb44b 3083 bp_excpt = std::move (e);
dda83cd7 3084 }
879bfdc2
DJ
3085 }
3086 else
3087 {
3088 /* No. This breakpoint will not be inserted.
3089 No error, but do not mark the bp as 'inserted'. */
3090 return 0;
3091 }
3092 }
3093
688fca4f 3094 if (bp_excpt.reason != 0)
879bfdc2
DJ
3095 {
3096 /* Can't set the breakpoint. */
80dc83fd
AB
3097 gdb_assert (bl->owner != nullptr);
3098
0000e5cc
PA
3099 /* In some cases, we might not be able to insert a
3100 breakpoint in a shared library that has already been
3101 removed, but we have not yet processed the shlib unload
3102 event. Unfortunately, some targets that implement
076855f9
PA
3103 breakpoint insertion themselves can't tell why the
3104 breakpoint insertion failed (e.g., the remote target
3105 doesn't define error codes), so we must treat generic
3106 errors as memory errors. */
688fca4f
PA
3107 if (bp_excpt.reason == RETURN_ERROR
3108 && (bp_excpt.error == GENERIC_ERROR
3109 || bp_excpt.error == MEMORY_ERROR)
076855f9 3110 && bl->loc_type == bp_loc_software_breakpoint
08351840 3111 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3112 || shared_objfile_contains_address_p (bl->pspace,
3113 bl->address)))
879bfdc2 3114 {
35df4500 3115 bl->shlib_disabled = 1;
19081eb5 3116 notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
3117 if (!*disabled_breaks)
3118 {
6cb06a8c
TT
3119 gdb_printf (tmp_error_stream,
3120 "Cannot insert breakpoint %d.\n",
3121 bl->owner->number);
3122 gdb_printf (tmp_error_stream,
3123 "Temporarily disabling shared "
3124 "library breakpoints:\n");
3fbb6ffa
TJB
3125 }
3126 *disabled_breaks = 1;
6cb06a8c
TT
3127 gdb_printf (tmp_error_stream,
3128 "breakpoint #%d\n", bl->owner->number);
0000e5cc 3129 return 0;
879bfdc2
DJ
3130 }
3131 else
879bfdc2 3132 {
35df4500 3133 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 3134 {
0000e5cc 3135 *hw_breakpoint_error = 1;
688fca4f 3136 *hw_bp_error_explained_already = bp_excpt.message != NULL;
6cb06a8c
TT
3137 gdb_printf (tmp_error_stream,
3138 "Cannot insert hardware breakpoint %d%s",
3139 bl->owner->number,
3140 bp_excpt.message ? ":" : ".\n");
dda83cd7 3141 if (bp_excpt.message != NULL)
6cb06a8c
TT
3142 gdb_printf (tmp_error_stream, "%s.\n",
3143 bp_excpt.what ());
879bfdc2
DJ
3144 }
3145 else
3146 {
688fca4f 3147 if (bp_excpt.message == NULL)
0000e5cc 3148 {
1ccbe998 3149 std::string message
0000e5cc
PA
3150 = memory_error_message (TARGET_XFER_E_IO,
3151 bl->gdbarch, bl->address);
0000e5cc 3152
6cb06a8c
TT
3153 gdb_printf (tmp_error_stream,
3154 "Cannot insert breakpoint %d.\n"
3155 "%s\n",
3156 bl->owner->number, message.c_str ());
0000e5cc
PA
3157 }
3158 else
3159 {
6cb06a8c
TT
3160 gdb_printf (tmp_error_stream,
3161 "Cannot insert breakpoint %d: %s\n",
3162 bl->owner->number,
3163 bp_excpt.what ());
0000e5cc 3164 }
879bfdc2 3165 }
0000e5cc 3166 return 1;
879bfdc2
DJ
3167
3168 }
3169 }
3170 else
35df4500 3171 bl->inserted = 1;
879bfdc2 3172
0000e5cc 3173 return 0;
879bfdc2
DJ
3174 }
3175
35df4500 3176 else if (bl->loc_type == bp_loc_hardware_watchpoint
35df4500 3177 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 3178 {
0000e5cc
PA
3179 int val;
3180
c47614fe 3181 val = bl->owner->insert_location (bl);
85d721b8
PA
3182
3183 /* If trying to set a read-watchpoint, and it turns out it's not
3184 supported, try emulating one with an access watchpoint. */
35df4500 3185 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 3186 {
85d721b8
PA
3187 /* But don't try to insert it, if there's already another
3188 hw_access location that would be considered a duplicate
3189 of this one. */
48d7020b 3190 for (bp_location *loc : all_bp_locations ())
35df4500 3191 if (loc != bl
85d721b8 3192 && loc->watchpoint_type == hw_access
35df4500 3193 && watchpoint_locations_match (bl, loc))
85d721b8 3194 {
35df4500
TJB
3195 bl->duplicate = 1;
3196 bl->inserted = 1;
3197 bl->target_info = loc->target_info;
3198 bl->watchpoint_type = hw_access;
85d721b8
PA
3199 val = 0;
3200 break;
3201 }
3202
3203 if (val == 1)
3204 {
77b06cd7 3205 bl->watchpoint_type = hw_access;
c47614fe 3206 val = bl->owner->insert_location (bl);
77b06cd7
TJB
3207
3208 if (val)
3209 /* Back to the original value. */
3210 bl->watchpoint_type = hw_read;
85d721b8
PA
3211 }
3212 }
3213
35df4500 3214 bl->inserted = (val == 0);
879bfdc2
DJ
3215 }
3216
35df4500 3217 else if (bl->owner->type == bp_catchpoint)
879bfdc2 3218 {
0000e5cc
PA
3219 int val;
3220
c47614fe 3221 val = bl->owner->insert_location (bl);
77b06cd7
TJB
3222 if (val)
3223 {
3224 bl->owner->enable_state = bp_disabled;
3225
3226 if (val == 1)
3227 warning (_("\
3228Error inserting catchpoint %d: Your system does not support this type\n\
3229of catchpoint."), bl->owner->number);
3230 else
3231 warning (_("Error inserting catchpoint %d."), bl->owner->number);
3232 }
3233
3234 bl->inserted = (val == 0);
1640b821
DJ
3235
3236 /* We've already printed an error message if there was a problem
3237 inserting this catchpoint, and we've disabled the catchpoint,
3238 so just return success. */
3239 return 0;
879bfdc2
DJ
3240 }
3241
3242 return 0;
3243}
3244
6c95b8df
PA
3245/* This function is called when program space PSPACE is about to be
3246 deleted. It takes care of updating breakpoints to not reference
3247 PSPACE anymore. */
3248
3249void
3250breakpoint_program_space_exit (struct program_space *pspace)
3251{
6c95b8df 3252 /* Remove any breakpoint that was set through this program space. */
a1decfc1
SM
3253 for (breakpoint &b : all_breakpoints_safe ())
3254 if (b.pspace == pspace)
3255 delete_breakpoint (&b);
6c95b8df
PA
3256
3257 /* Breakpoints set through other program spaces could have locations
3258 bound to PSPACE as well. Remove those. */
48d7020b 3259 for (bp_location *loc : all_bp_locations ())
20afe380
SM
3260 if (loc->pspace == pspace)
3261 {
3262 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3263 loc->owner->unadd_location (*loc);
3264 }
6c95b8df
PA
3265
3266 /* Now update the global location list to permanently delete the
3267 removed locations above. */
44702360 3268 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3269}
3270
74960c60
VP
3271/* Make sure all breakpoints are inserted in inferior.
3272 Throws exception on any error.
3273 A breakpoint that is already inserted won't be inserted
3274 again, so calling this function twice is safe. */
3275void
3276insert_breakpoints (void)
3277{
a1decfc1
SM
3278 for (breakpoint &bpt : all_breakpoints ())
3279 if (is_hardware_watchpoint (&bpt))
3a5c3e22 3280 {
a1decfc1 3281 watchpoint &w = gdb::checked_static_cast<watchpoint &> (bpt);
3a5c3e22 3282
a1decfc1 3283 update_watchpoint (&w, false /* don't reparse. */);
3a5c3e22 3284 }
74960c60 3285
04086b45
PA
3286 /* Updating watchpoints creates new locations, so update the global
3287 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
3288 ignore breakpoints_always_inserted_mode. Also,
3289 update_global_location_list tries to "upgrade" software
3290 breakpoints to hardware breakpoints to handle "set breakpoint
3291 auto-hw", so we need to call it even if we don't have new
3292 locations. */
04086b45 3293 update_global_location_list (UGLL_INSERT);
74960c60
VP
3294}
3295
b775012e
LM
3296/* This is used when we need to synch breakpoint conditions between GDB and the
3297 target. It is the case with deleting and disabling of breakpoints when using
3298 always-inserted mode. */
3299
3300static void
3301update_inserted_breakpoint_locations (void)
3302{
b775012e
LM
3303 int error_flag = 0;
3304 int val = 0;
3305 int disabled_breaks = 0;
3306 int hw_breakpoint_error = 0;
dd61ec5c 3307 int hw_bp_details_reported = 0;
b775012e 3308
d7e74731 3309 string_file tmp_error_stream;
b775012e
LM
3310
3311 /* Explicitly mark the warning -- this will only be printed if
3312 there was an error. */
d7e74731 3313 tmp_error_stream.puts ("Warning:\n");
b775012e 3314
5ed8105e 3315 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 3316
48d7020b 3317 for (bp_location *bl : all_bp_locations ())
b775012e
LM
3318 {
3319 /* We only want to update software breakpoints and hardware
3320 breakpoints. */
3321 if (!is_breakpoint (bl->owner))
3322 continue;
3323
3324 /* We only want to update locations that are already inserted
3325 and need updating. This is to avoid unwanted insertion during
3326 deletion of breakpoints. */
4daf1902 3327 if (!bl->inserted || !bl->needs_update)
b775012e
LM
3328 continue;
3329
3330 switch_to_program_space_and_thread (bl->pspace);
3331
3332 /* For targets that support global breakpoints, there's no need
3333 to select an inferior to insert breakpoint to. In fact, even
3334 if we aren't attached to any process yet, we should still
3335 insert breakpoints. */
99d9c3b9 3336 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
55f6301a 3337 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
3338 continue;
3339
d7e74731 3340 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3341 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3342 if (val)
3343 error_flag = val;
3344 }
3345
3346 if (error_flag)
3347 {
223ffa71 3348 target_terminal::ours_for_output ();
e29e6304 3349 error (("%s"), tmp_error_stream.c_str ());
b775012e 3350 }
b775012e
LM
3351}
3352
c30eee59 3353/* Used when starting or continuing the program. */
c906108c 3354
74960c60
VP
3355static void
3356insert_breakpoint_locations (void)
c906108c 3357{
eacd795a 3358 int error_flag = 0;
c906108c 3359 int val = 0;
3fbb6ffa 3360 int disabled_breaks = 0;
81d0cc19 3361 int hw_breakpoint_error = 0;
dd61ec5c 3362 int hw_bp_error_explained_already = 0;
c906108c 3363
d7e74731
PA
3364 string_file tmp_error_stream;
3365
81d0cc19
GS
3366 /* Explicitly mark the warning -- this will only be printed if
3367 there was an error. */
d7e74731 3368 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3369
5ed8105e 3370 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3371
48d7020b 3372 for (bp_location *bl : all_bp_locations ())
879bfdc2 3373 {
b775012e 3374 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3375 continue;
3376
4a64f543
MS
3377 /* There is no point inserting thread-specific breakpoints if
3378 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3379 has BL->OWNER always non-NULL. */
35df4500 3380 if (bl->owner->thread != -1
5d5658a1 3381 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3382 continue;
3383
b080fe54
AB
3384 /* Or inferior specific breakpoints if the inferior no longer
3385 exists. */
3386 if (bl->owner->inferior != -1
3387 && !valid_global_inferior_id (bl->owner->inferior))
3388 continue;
3389
35df4500 3390 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3391
3392 /* For targets that support global breakpoints, there's no need
3393 to select an inferior to insert breakpoint to. In fact, even
3394 if we aren't attached to any process yet, we should still
3395 insert breakpoints. */
99d9c3b9 3396 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
55f6301a 3397 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3398 continue;
3399
d7e74731 3400 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3401 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3402 if (val)
eacd795a 3403 error_flag = val;
879bfdc2 3404 }
c906108c 3405
4a64f543
MS
3406 /* If we failed to insert all locations of a watchpoint, remove
3407 them, as half-inserted watchpoint is of limited use. */
a1decfc1 3408 for (breakpoint &bpt : all_breakpoints ())
a5606eee 3409 {
d8de7963 3410 bool some_failed = false;
a5606eee 3411
a1decfc1 3412 if (!is_hardware_watchpoint (&bpt))
a5606eee
VP
3413 continue;
3414
a1decfc1 3415 if (!breakpoint_enabled (&bpt))
a5606eee 3416 continue;
74960c60 3417
a1decfc1 3418 if (bpt.disposition == disp_del_at_next_stop)
74960c60 3419 continue;
b00b30b2 3420
a1decfc1 3421 for (bp_location &loc : bpt.locations ())
b00b30b2 3422 if (!loc.inserted && should_be_inserted (&loc))
a5606eee 3423 {
d8de7963 3424 some_failed = true;
a5606eee
VP
3425 break;
3426 }
40cb8ca5 3427
a5606eee
VP
3428 if (some_failed)
3429 {
a1decfc1 3430 for (bp_location &loc : bpt.locations ())
b00b30b2
SM
3431 if (loc.inserted)
3432 remove_breakpoint (&loc);
a5606eee
VP
3433
3434 hw_breakpoint_error = 1;
d7e74731
PA
3435 tmp_error_stream.printf ("Could not insert "
3436 "hardware watchpoint %d.\n",
a1decfc1 3437 bpt.number);
eacd795a 3438 error_flag = -1;
a5606eee
VP
3439 }
3440 }
3441
eacd795a 3442 if (error_flag)
81d0cc19
GS
3443 {
3444 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3445 message about possibly exhausted resources. */
dd61ec5c 3446 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3447 {
d7e74731 3448 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3449You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3450 }
223ffa71 3451 target_terminal::ours_for_output ();
e29e6304 3452 error (("%s"), tmp_error_stream.c_str ());
81d0cc19 3453 }
c906108c
SS
3454}
3455
c30eee59
TJB
3456/* Used when the program stops.
3457 Returns zero if successful, or non-zero if there was a problem
3458 removing a breakpoint location. */
3459
c906108c 3460int
fba45db2 3461remove_breakpoints (void)
c906108c 3462{
3a1bae8e 3463 int val = 0;
c906108c 3464
48d7020b 3465 for (bp_location *bl : all_bp_locations ())
1e4d1764 3466 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3467 val |= remove_breakpoint (bl);
01add95b 3468
3a1bae8e 3469 return val;
c906108c
SS
3470}
3471
49fa26b0
PA
3472/* When a thread exits, remove breakpoints that are related to
3473 that thread. */
3474
3475static void
9d7d58e7 3476remove_threaded_breakpoints (thread_info *tp,
6b09f134 3477 std::optional<ULONGEST> /* exit_code */,
9d7d58e7 3478 int /* silent */)
49fa26b0 3479{
a1decfc1 3480 for (breakpoint &b : all_breakpoints_safe ())
49fa26b0 3481 {
a1decfc1 3482 if (b.thread == tp->global_num && user_breakpoint_p (&b))
49fa26b0 3483 {
6cb06a8c 3484 gdb_printf (_("\
43792cf0 3485Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
a1decfc1
SM
3486 b.number, print_thread_id (tp));
3487 delete_breakpoint (&b);
49fa26b0
PA
3488 }
3489 }
3490}
3491
b080fe54
AB
3492/* Called when inferior INF has been removed from GDB. Remove associated
3493 per-inferior breakpoints. */
3494
3495static void
3496remove_inferior_breakpoints (struct inferior *inf)
3497{
3498 for (breakpoint &b : all_breakpoints_safe ())
3499 {
3500 if (b.inferior == inf->num && user_breakpoint_p (&b))
3501 {
3502 /* Tell the user the breakpoint has been deleted. But only for
3503 breakpoints that would not normally have been deleted at the
3504 next stop anyway. */
3505 if (b.disposition != disp_del
3506 && b.disposition != disp_del_at_next_stop)
3507 gdb_printf (_("\
3508Inferior-specific breakpoint %d deleted - inferior %d has been removed.\n"),
3509 b.number, inf->num);
3510 delete_breakpoint (&b);
3511 }
3512 }
3513}
3514
f3869b1a 3515/* See breakpoint.h. */
6c95b8df 3516
f3869b1a 3517void
00431a78 3518remove_breakpoints_inf (inferior *inf)
6c95b8df 3519{
6c95b8df 3520 int val;
6c95b8df 3521
3cdc2d7e
MS
3522 breakpoint_debug_printf ("inf->num = %d", inf->num);
3523
48d7020b 3524 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3525 {
3526 if (bl->pspace != inf->pspace)
3527 continue;
6c95b8df 3528
01add95b
SM
3529 if (bl->inserted && !bl->target_info.persist)
3530 {
3531 val = remove_breakpoint (bl);
3532 if (val != 0)
3533 return;
3534 }
3535 }
6c95b8df
PA
3536}
3537
e58b0e63
PA
3538static int internal_breakpoint_number = -1;
3539
84f4c1fe
PM
3540/* Set the breakpoint number of B, depending on the value of INTERNAL.
3541 If INTERNAL is non-zero, the breakpoint number will be populated
3542 from internal_breakpoint_number and that variable decremented.
e5dd4106 3543 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3544 breakpoint_count and that value incremented. Internal breakpoints
3545 do not set the internal var bpnum. */
3546static void
3547set_breakpoint_number (int internal, struct breakpoint *b)
3548{
3549 if (internal)
3550 b->number = internal_breakpoint_number--;
3551 else
3552 {
3553 set_breakpoint_count (breakpoint_count + 1);
3554 b->number = breakpoint_count;
3555 }
3556}
3557
829b6b37
TV
3558/* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3559
e62c965a 3560static struct breakpoint *
a6d9a66e 3561create_internal_breakpoint (struct gdbarch *gdbarch,
9efa3c7f 3562 CORE_ADDR address, enum bptype type)
e62c965a 3563{
752a2291
PA
3564 std::unique_ptr<internal_breakpoint> b
3565 (new internal_breakpoint (gdbarch, type, address));
e62c965a 3566
e62c965a 3567 b->number = internal_breakpoint_number--;
e62c965a 3568
752a2291 3569 return add_to_breakpoint_chain (std::move (b));
e62c965a
PP
3570}
3571
829b6b37
TV
3572/* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3573 GDBARCH. */
3574
3575static struct breakpoint *
3576create_internal_breakpoint (struct gdbarch *gdbarch,
03b40f6f 3577 bound_minimal_symbol &msym, enum bptype type)
829b6b37
TV
3578{
3579 CORE_ADDR address;
3580
3581 address = msym.value_address ();
3582
3583 address = gdbarch_convert_from_func_ptr_addr
3584 (gdbarch, address, current_inferior ()->top_target ());
3585
3586 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3587 related to addresses in $pc. We're getting the address from the
3588 minimal symbol table. */
3589
3590 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3591 with elsewhere? Needs testing on vax. */
3592
3593 if (gdbarch_skip_entrypoint_p (gdbarch))
3594 address = gdbarch_skip_entrypoint (gdbarch, address);
3595
3596 return create_internal_breakpoint (gdbarch, address, type);
3597}
3598
17450429
PP
3599static const char *const longjmp_names[] =
3600 {
3601 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3602 };
3603#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3604
3605/* Per-objfile data private to breakpoint.c. */
3606struct breakpoint_objfile_data
3607{
3608 /* Minimal symbol for "_ovly_debug_event" (if any). */
03b40f6f 3609 bound_minimal_symbol overlay_msym;
17450429
PP
3610
3611 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
03b40f6f 3612 bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3613
28106bc2 3614 /* True if we have looked for longjmp probes. */
43dce439 3615 int longjmp_searched = 0;
28106bc2 3616
45461e0d
SM
3617 /* SystemTap probe points for longjmp (if any). These are non-owning
3618 references. */
3619 std::vector<probe *> longjmp_probes;
28106bc2 3620
17450429 3621 /* Minimal symbol for "std::terminate()" (if any). */
03b40f6f 3622 bound_minimal_symbol terminate_msym;
17450429
PP
3623
3624 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
03b40f6f 3625 bound_minimal_symbol exception_msym;
28106bc2
SDJ
3626
3627 /* True if we have looked for exception probes. */
43dce439 3628 int exception_searched = 0;
28106bc2 3629
45461e0d
SM
3630 /* SystemTap probe points for unwinding (if any). These are non-owning
3631 references. */
3632 std::vector<probe *> exception_probes;
17450429
PP
3633};
3634
08b8a139 3635static const registry<objfile>::key<breakpoint_objfile_data>
51d3063a 3636 breakpoint_objfile_key;
17450429
PP
3637
3638/* Minimal symbol not found sentinel. */
3639static struct minimal_symbol msym_not_found;
3640
3641/* Returns TRUE if MSYM point to the "not found" sentinel. */
3642
d8de7963 3643static bool
17450429
PP
3644msym_not_found_p (const struct minimal_symbol *msym)
3645{
3646 return msym == &msym_not_found;
3647}
3648
3649/* Return per-objfile data needed by breakpoint.c.
3650 Allocate the data if necessary. */
3651
3652static struct breakpoint_objfile_data *
3653get_breakpoint_objfile_data (struct objfile *objfile)
3654{
3655 struct breakpoint_objfile_data *bp_objfile_data;
3656
51d3063a 3657 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3658 if (bp_objfile_data == NULL)
51d3063a 3659 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3660 return bp_objfile_data;
3661}
3662
e62c965a 3663static void
af02033e 3664create_overlay_event_breakpoint (void)
e62c965a 3665{
af02033e 3666 const char *const func_name = "_ovly_debug_event";
e62c965a 3667
2030c079 3668 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3669 {
3670 struct breakpoint *b;
17450429
PP
3671 struct breakpoint_objfile_data *bp_objfile_data;
3672 CORE_ADDR addr;
69de3c6a 3673
17450429
PP
3674 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3675
3b7344d5 3676 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3677 continue;
3678
3b7344d5 3679 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3680 {
03b40f6f 3681 bound_minimal_symbol m
cb9f919f
SM
3682 = lookup_minimal_symbol_text (current_program_space, func_name,
3683 objfile);
3b7344d5 3684 if (m.minsym == NULL)
17450429
PP
3685 {
3686 /* Avoid future lookups in this objfile. */
3b7344d5 3687 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3688 continue;
3689 }
3690 bp_objfile_data->overlay_msym = m;
3691 }
e62c965a 3692
4aeddc50 3693 addr = bp_objfile_data->overlay_msym.value_address ();
08feed99 3694 b = create_internal_breakpoint (objfile->arch (), addr,
9efa3c7f 3695 bp_overlay_event);
40d97ee2 3696 b->locspec = new_explicit_location_spec_function (func_name);
e62c965a 3697
69de3c6a 3698 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3699 {
3700 b->enable_state = bp_enabled;
3701 overlay_events_enabled = 1;
3702 }
69de3c6a
PP
3703 else
3704 {
dda83cd7
SM
3705 b->enable_state = bp_disabled;
3706 overlay_events_enabled = 0;
69de3c6a 3707 }
e62c965a 3708 }
e62c965a
PP
3709}
3710
2a7f6487
TV
3711/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3712 true if a breakpoint was installed. */
3713
3714static bool
3715create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3716{
2a7f6487
TV
3717 struct gdbarch *gdbarch = objfile->arch ();
3718 struct breakpoint_objfile_data *bp_objfile_data
3719 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3720
2a7f6487 3721 if (!bp_objfile_data->longjmp_searched)
94c93c35 3722 {
2a7f6487
TV
3723 std::vector<probe *> ret
3724 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3725
2a7f6487 3726 if (!ret.empty ())
94c93c35 3727 {
2a7f6487
TV
3728 /* We are only interested in checking one element. */
3729 probe *p = ret[0];
0fd8e87f 3730
2a7f6487
TV
3731 if (!p->can_evaluate_arguments ())
3732 {
3733 /* We cannot use the probe interface here,
3734 because it does not know how to evaluate
3735 arguments. */
3736 ret.clear ();
3737 }
3738 }
3739 bp_objfile_data->longjmp_probes = ret;
3740 bp_objfile_data->longjmp_searched = 1;
3741 }
0fd8e87f 3742
2a7f6487
TV
3743 if (bp_objfile_data->longjmp_probes.empty ())
3744 return false;
17450429 3745
2a7f6487
TV
3746 for (probe *p : bp_objfile_data->longjmp_probes)
3747 {
3748 struct breakpoint *b;
25f9533e 3749
2a7f6487
TV
3750 b = create_internal_breakpoint (gdbarch,
3751 p->get_relocated_address (objfile),
9efa3c7f 3752 bp_longjmp_master);
264f9890 3753 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
2a7f6487
TV
3754 b->enable_state = bp_disabled;
3755 }
aed57c53 3756
2a7f6487
TV
3757 return true;
3758}
25f9533e 3759
2a7f6487
TV
3760/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3761 Return true if at least one breakpoint was installed. */
3762
3763static bool
3764create_longjmp_master_breakpoint_names (objfile *objfile)
3765{
3766 struct gdbarch *gdbarch = objfile->arch ();
3767 if (!gdbarch_get_longjmp_target_p (gdbarch))
3768 return false;
3769
3770 struct breakpoint_objfile_data *bp_objfile_data
3771 = get_breakpoint_objfile_data (objfile);
3772 unsigned int installed_bp = 0;
3773
3774 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3775 {
3776 struct breakpoint *b;
3777 const char *func_name;
3778 CORE_ADDR addr;
2a7f6487
TV
3779
3780 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3781 continue;
28106bc2 3782
2a7f6487
TV
3783 func_name = longjmp_names[i];
3784 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3785 {
03b40f6f 3786 bound_minimal_symbol m
cb9f919f
SM
3787 = lookup_minimal_symbol_text (objfile->pspace (), func_name,
3788 objfile);
2a7f6487
TV
3789 if (m.minsym == NULL)
3790 {
3791 /* Prevent future lookups in this objfile. */
3792 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3793 continue;
3794 }
2a7f6487
TV
3795 bp_objfile_data->longjmp_msym[i] = m;
3796 }
28106bc2 3797
4aeddc50 3798 addr = bp_objfile_data->longjmp_msym[i].value_address ();
9efa3c7f 3799 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
40d97ee2 3800 b->locspec = new_explicit_location_spec_function (func_name);
2a7f6487
TV
3801 b->enable_state = bp_disabled;
3802 installed_bp++;
3803 }
28106bc2 3804
2a7f6487
TV
3805 return installed_bp > 0;
3806}
0fd8e87f 3807
2a7f6487 3808/* Create a master longjmp breakpoint. */
17450429 3809
2a7f6487
TV
3810static void
3811create_longjmp_master_breakpoint (void)
3812{
3813 scoped_restore_current_program_space restore_pspace;
aed57c53 3814
2a7f6487
TV
3815 for (struct program_space *pspace : program_spaces)
3816 {
3817 set_current_program_space (pspace);
17450429 3818
574b77fc 3819 for (objfile *obj : pspace->objfiles ())
2a7f6487
TV
3820 {
3821 /* Skip separate debug object, it's handled in the loop below. */
3822 if (obj->separate_debug_objfile_backlink != nullptr)
3823 continue;
3824
3825 /* Try a probe kind breakpoint on main objfile. */
3826 if (create_longjmp_master_breakpoint_probe (obj))
3827 continue;
3828
3829 /* Try longjmp_names kind breakpoints on main and separate_debug
3830 objfiles. */
3831 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3832 if (create_longjmp_master_breakpoint_names (debug_objfile))
3833 break;
94c93c35
TT
3834 }
3835 }
0fd8e87f
UW
3836}
3837
af02033e 3838/* Create a master std::terminate breakpoint. */
aa7d318d 3839static void
af02033e 3840create_std_terminate_master_breakpoint (void)
aa7d318d 3841{
af02033e 3842 const char *const func_name = "std::terminate()";
aa7d318d 3843
5ed8105e 3844 scoped_restore_current_program_space restore_pspace;
d47600c8 3845 scoped_restore_current_language save_language (language_cplus);
aa7d318d 3846
94c93c35
TT
3847 for (struct program_space *pspace : program_spaces)
3848 {
94c93c35 3849 set_current_program_space (pspace);
17450429 3850
574b77fc 3851 for (objfile *objfile : pspace->objfiles ())
94c93c35
TT
3852 {
3853 struct breakpoint *b;
3854 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3855
94c93c35 3856 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3857
94c93c35
TT
3858 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3859 continue;
17450429 3860
94c93c35
TT
3861 if (bp_objfile_data->terminate_msym.minsym == NULL)
3862 {
03b40f6f 3863 bound_minimal_symbol m
4144d36a
SM
3864 = lookup_minimal_symbol (current_program_space, func_name,
3865 objfile);
60f62e2b
SM
3866 if (m.minsym == NULL || (m.minsym->type () != mst_text
3867 && m.minsym->type () != mst_file_text))
94c93c35
TT
3868 {
3869 /* Prevent future lookups in this objfile. */
3870 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3871 continue;
3872 }
3873 bp_objfile_data->terminate_msym = m;
3874 }
aa7d318d 3875
829b6b37
TV
3876 b = create_internal_breakpoint (objfile->arch (),
3877 bp_objfile_data->terminate_msym,
9efa3c7f 3878 bp_std_terminate_master);
40d97ee2 3879 b->locspec = new_explicit_location_spec_function (func_name);
94c93c35
TT
3880 b->enable_state = bp_disabled;
3881 }
3882 }
aa7d318d
TT
3883}
3884
1940319c
TV
3885/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3886 probe. Return true if a breakpoint was installed. */
186c406b 3887
1940319c
TV
3888static bool
3889create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3890{
1940319c
TV
3891 struct breakpoint *b;
3892 struct gdbarch *gdbarch;
3893 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3894
1940319c 3895 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3896
1940319c
TV
3897 /* We prefer the SystemTap probe point if it exists. */
3898 if (!bp_objfile_data->exception_searched)
3899 {
3900 std::vector<probe *> ret
3901 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3902
1940319c 3903 if (!ret.empty ())
28106bc2 3904 {
1940319c
TV
3905 /* We are only interested in checking one element. */
3906 probe *p = ret[0];
25f9533e 3907
1940319c 3908 if (!p->can_evaluate_arguments ())
25f9533e 3909 {
1940319c
TV
3910 /* We cannot use the probe interface here, because it does
3911 not know how to evaluate arguments. */
3912 ret.clear ();
25f9533e 3913 }
28106bc2 3914 }
1940319c
TV
3915 bp_objfile_data->exception_probes = ret;
3916 bp_objfile_data->exception_searched = 1;
3917 }
28106bc2 3918
1940319c
TV
3919 if (bp_objfile_data->exception_probes.empty ())
3920 return false;
45461e0d 3921
1940319c 3922 gdbarch = objfile->arch ();
28106bc2 3923
1940319c
TV
3924 for (probe *p : bp_objfile_data->exception_probes)
3925 {
3926 b = create_internal_breakpoint (gdbarch,
3927 p->get_relocated_address (objfile),
9efa3c7f 3928 bp_exception_master);
264f9890 3929 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
1940319c
TV
3930 b->enable_state = bp_disabled;
3931 }
28106bc2 3932
1940319c
TV
3933 return true;
3934}
28106bc2 3935
1940319c
TV
3936/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3937 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3938
1940319c
TV
3939static bool
3940create_exception_master_breakpoint_hook (objfile *objfile)
3941{
3942 const char *const func_name = "_Unwind_DebugHook";
3943 struct breakpoint *b;
3944 struct gdbarch *gdbarch;
3945 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3946
1940319c 3947 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3948
1940319c
TV
3949 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3950 return false;
17450429 3951
1940319c
TV
3952 gdbarch = objfile->arch ();
3953
3954 if (bp_objfile_data->exception_msym.minsym == NULL)
3955 {
03b40f6f 3956 bound_minimal_symbol debug_hook
cb9f919f 3957 = lookup_minimal_symbol_text (objfile->pspace (), func_name, objfile);
1940319c
TV
3958 if (debug_hook.minsym == NULL)
3959 {
3960 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3961 return false;
186c406b 3962 }
17450429 3963
1940319c
TV
3964 bp_objfile_data->exception_msym = debug_hook;
3965 }
3966
829b6b37
TV
3967 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3968 bp_exception_master);
40d97ee2 3969 b->locspec = new_explicit_location_spec_function (func_name);
1940319c
TV
3970 b->enable_state = bp_disabled;
3971
3972 return true;
3973}
3974
3975/* Install a master breakpoint on the unwinder's debug hook. */
3976
3977static void
3978create_exception_master_breakpoint (void)
3979{
3980 for (objfile *obj : current_program_space->objfiles ())
3981 {
3982 /* Skip separate debug object. */
3983 if (obj->separate_debug_objfile_backlink)
3984 continue;
3985
3986 /* Try a probe kind breakpoint. */
3987 if (create_exception_master_breakpoint_probe (obj))
3988 continue;
3989
7c6944ab
TV
3990 /* Iterate over main and separate debug objects and try an
3991 _Unwind_DebugHook kind breakpoint. */
3992 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3993 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3994 break;
186c406b 3995 }
186c406b
TT
3996}
3997
9ef9e6a6
KS
3998/* Does B have a location spec? */
3999
d8de7963 4000static bool
264f9890 4001breakpoint_location_spec_empty_p (const struct breakpoint *b)
9ef9e6a6 4002{
238dc9af 4003 return (b->locspec != nullptr && b->locspec->empty_p ());
9ef9e6a6
KS
4004}
4005
c906108c 4006void
fba45db2 4007update_breakpoints_after_exec (void)
c906108c 4008{
25b22b0a
PA
4009 /* We're about to delete breakpoints from GDB's lists. If the
4010 INSERTED flag is true, GDB will try to lift the breakpoints by
4011 writing the breakpoints' "shadow contents" back into memory. The
4012 "shadow contents" are NOT valid after an exec, so GDB should not
4013 do that. Instead, the target is responsible from marking
4014 breakpoints out as soon as it detects an exec. We don't do that
4015 here instead, because there may be other attempts to delete
4016 breakpoints after detecting an exec and before reaching here. */
48d7020b 4017 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
4018 if (bploc->pspace == current_program_space)
4019 gdb_assert (!bploc->inserted);
c906108c 4020
a1decfc1 4021 for (breakpoint &b : all_breakpoints_safe ())
01add95b 4022 {
a1decfc1 4023 if (b.pspace != current_program_space)
c5aa993b 4024 continue;
c906108c 4025
01add95b 4026 /* Solib breakpoints must be explicitly reset after an exec(). */
a1decfc1 4027 if (b.type == bp_shlib_event)
01add95b 4028 {
a1decfc1 4029 delete_breakpoint (&b);
01add95b
SM
4030 continue;
4031 }
4efc6507 4032
01add95b 4033 /* JIT breakpoints must be explicitly reset after an exec(). */
a1decfc1 4034 if (b.type == bp_jit_event)
01add95b 4035 {
a1decfc1 4036 delete_breakpoint (&b);
01add95b
SM
4037 continue;
4038 }
c4093a6a 4039
01add95b
SM
4040 /* Thread event breakpoints must be set anew after an exec(),
4041 as must overlay event and longjmp master breakpoints. */
a1decfc1
SM
4042 if (b.type == bp_thread_event || b.type == bp_overlay_event
4043 || b.type == bp_longjmp_master || b.type == bp_std_terminate_master
4044 || b.type == bp_exception_master)
01add95b 4045 {
a1decfc1 4046 delete_breakpoint (&b);
01add95b
SM
4047 continue;
4048 }
c5aa993b 4049
01add95b 4050 /* Step-resume breakpoints are meaningless after an exec(). */
a1decfc1 4051 if (b.type == bp_step_resume || b.type == bp_hp_step_resume)
01add95b 4052 {
a1decfc1 4053 delete_breakpoint (&b);
01add95b
SM
4054 continue;
4055 }
7c16b83e 4056
01add95b 4057 /* Just like single-step breakpoints. */
a1decfc1 4058 if (b.type == bp_single_step)
01add95b 4059 {
a1decfc1 4060 delete_breakpoint (&b);
01add95b
SM
4061 continue;
4062 }
611c83ae 4063
01add95b
SM
4064 /* Longjmp and longjmp-resume breakpoints are also meaningless
4065 after an exec. */
a1decfc1
SM
4066 if (b.type == bp_longjmp || b.type == bp_longjmp_resume
4067 || b.type == bp_longjmp_call_dummy
4068 || b.type == bp_exception || b.type == bp_exception_resume)
01add95b 4069 {
a1decfc1 4070 delete_breakpoint (&b);
01add95b
SM
4071 continue;
4072 }
ce78b96d 4073
a1decfc1 4074 if (b.type == bp_catchpoint)
01add95b
SM
4075 {
4076 /* For now, none of the bp_catchpoint breakpoints need to
4077 do anything at this point. In the future, if some of
4078 the catchpoints need to something, we will need to add
4079 a new method, and call this method from here. */
4080 continue;
4081 }
c5aa993b 4082
01add95b
SM
4083 /* bp_finish is a special case. The only way we ought to be able
4084 to see one of these when an exec() has happened, is if the user
4085 caught a vfork, and then said "finish". Ordinarily a finish just
4086 carries them to the call-site of the current callee, by setting
4087 a temporary bp there and resuming. But in this case, the finish
4088 will carry them entirely through the vfork & exec.
4089
4090 We don't want to allow a bp_finish to remain inserted now. But
4091 we can't safely delete it, 'cause finish_command has a handle to
4092 the bp on a bpstat, and will later want to delete it. There's a
4093 chance (and I've seen it happen) that if we delete the bp_finish
4094 here, that its storage will get reused by the time finish_command
4095 gets 'round to deleting the "use to be a bp_finish" breakpoint.
4096 We really must allow finish_command to delete a bp_finish.
4097
4098 In the absence of a general solution for the "how do we know
4099 it's safe to delete something others may have handles to?"
4100 problem, what we'll do here is just uninsert the bp_finish, and
4101 let finish_command delete it.
4102
4103 (We know the bp_finish is "doomed" in the sense that it's
4104 momentary, and will be deleted as soon as finish_command sees
4105 the inferior stopped. So it doesn't matter that the bp's
4106 address is probably bogus in the new a.out, unlike e.g., the
4107 solib breakpoints.) */
4108
a1decfc1 4109 if (b.type == bp_finish)
01add95b
SM
4110 {
4111 continue;
4112 }
4113
4114 /* Without a symbolic address, we have little hope of the
4115 pre-exec() address meaning the same thing in the post-exec()
4116 a.out. */
a1decfc1 4117 if (breakpoint_location_spec_empty_p (&b))
01add95b 4118 {
a1decfc1 4119 delete_breakpoint (&b);
01add95b
SM
4120 continue;
4121 }
4122 }
c906108c
SS
4123}
4124
4125int
d80ee84f 4126detach_breakpoints (ptid_t ptid)
c906108c 4127{
3a1bae8e 4128 int val = 0;
2989a365 4129 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 4130 struct inferior *inf = current_inferior ();
c5aa993b 4131
e99b03dc 4132 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 4133 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 4134
6c95b8df 4135 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 4136 inferior_ptid = ptid;
48d7020b 4137 for (bp_location *bl : all_bp_locations ())
01add95b
SM
4138 {
4139 if (bl->pspace != inf->pspace)
4140 continue;
4141
4142 /* This function must physically remove breakpoints locations
4143 from the specified ptid, without modifying the breakpoint
6e14e441
PA
4144 package's state. Locations of type bp_loc_other and
4145 bp_loc_software_watchpoint are only maintained at GDB side,
4146 so there is no need to remove them. Moreover, removing these
01add95b 4147 would modify the breakpoint package's state. */
6e14e441
PA
4148 if (bl->loc_type == bp_loc_other
4149 || bl->loc_type == bp_loc_software_watchpoint)
01add95b
SM
4150 continue;
4151
4152 if (bl->inserted)
4153 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
4154 }
d03285ec 4155
3a1bae8e 4156 return val;
c906108c
SS
4157}
4158
35df4500 4159/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
4160 Note that this is used to detach breakpoints from a child fork.
4161 When we get here, the child isn't in the inferior list, and neither
4162 do we have objects to represent its address space --- we should
35df4500 4163 *not* look at bl->pspace->aspace here. */
6c95b8df 4164
c906108c 4165static int
b2b6a7da 4166remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c 4167{
3cdc2d7e
MS
4168 breakpoint_debug_printf ("%s due to %s",
4169 breakpoint_location_address_str (bl).c_str (),
4170 remove_bp_reason_str (reason));
4171
c906108c 4172 int val;
c5aa993b 4173
35df4500
TJB
4174 /* BL is never in moribund_locations by our callers. */
4175 gdb_assert (bl->owner != NULL);
2bdf28a0 4176
74960c60
VP
4177 /* The type of none suggests that owner is actually deleted.
4178 This should not ever happen. */
35df4500 4179 gdb_assert (bl->owner->type != bp_none);
0bde7532 4180
35df4500
TJB
4181 if (bl->loc_type == bp_loc_software_breakpoint
4182 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 4183 {
c02f5703
MS
4184 /* "Normal" instruction breakpoint: either the standard
4185 trap-instruction bp (bp_breakpoint), or a
4186 bp_hardware_breakpoint. */
4187
4188 /* First check to see if we have to handle an overlay. */
4189 if (overlay_debugging == ovly_off
35df4500
TJB
4190 || bl->section == NULL
4191 || !(section_is_overlay (bl->section)))
c02f5703
MS
4192 {
4193 /* No overlay handling: just remove the breakpoint. */
08351840
PA
4194
4195 /* If we're trying to uninsert a memory breakpoint that we
4196 know is set in a dynamic object that is marked
4197 shlib_disabled, then either the dynamic object was
4198 removed with "remove-symbol-file" or with
4199 "nosharedlibrary". In the former case, we don't know
4200 whether another dynamic object might have loaded over the
4201 breakpoint's address -- the user might well let us know
4202 about it next with add-symbol-file (the whole point of
d03de421 4203 add-symbol-file is letting the user manually maintain a
08351840
PA
4204 list of dynamically loaded objects). If we have the
4205 breakpoint's shadow memory, that is, this is a software
4206 breakpoint managed by GDB, check whether the breakpoint
4207 is still inserted in memory, to avoid overwriting wrong
4208 code with stale saved shadow contents. Note that HW
4209 breakpoints don't have shadow memory, as they're
4210 implemented using a mechanism that is not dependent on
4211 being able to modify the target's memory, and as such
4212 they should always be removed. */
4213 if (bl->shlib_disabled
4214 && bl->target_info.shadow_len != 0
4215 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
4216 val = 0;
4217 else
c47614fe 4218 val = bl->owner->remove_location (bl, reason);
c02f5703 4219 }
c906108c
SS
4220 else
4221 {
4a64f543 4222 /* This breakpoint is in an overlay section.
c02f5703
MS
4223 Did we set a breakpoint at the LMA? */
4224 if (!overlay_events_enabled)
4225 {
4226 /* Yes -- overlay event support is not active, so we
4227 should have set a breakpoint at the LMA. Remove it.
4228 */
c02f5703
MS
4229 /* Ignore any failures: if the LMA is in ROM, we will
4230 have already warned when we failed to insert it. */
35df4500
TJB
4231 if (bl->loc_type == bp_loc_hardware_breakpoint)
4232 target_remove_hw_breakpoint (bl->gdbarch,
4233 &bl->overlay_target_info);
c02f5703 4234 else
35df4500 4235 target_remove_breakpoint (bl->gdbarch,
73971819
PA
4236 &bl->overlay_target_info,
4237 reason);
c02f5703
MS
4238 }
4239 /* Did we set a breakpoint at the VMA?
4240 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4241 if (bl->inserted)
c906108c 4242 {
c02f5703
MS
4243 /* Yes -- remove it. Previously we did not bother to
4244 remove the breakpoint if the section had been
4245 unmapped, but let's not rely on that being safe. We
4246 don't know what the overlay manager might do. */
aa67235e
UW
4247
4248 /* However, we should remove *software* breakpoints only
4249 if the section is still mapped, or else we overwrite
4250 wrong code with the saved shadow contents. */
348d480f
PA
4251 if (bl->loc_type == bp_loc_hardware_breakpoint
4252 || section_is_mapped (bl->section))
c47614fe 4253 val = bl->owner->remove_location (bl, reason);
aa67235e
UW
4254 else
4255 val = 0;
c906108c 4256 }
c02f5703
MS
4257 else
4258 {
4259 /* No -- not inserted, so no need to remove. No error. */
4260 val = 0;
4261 }
c906108c 4262 }
879d1e6b 4263
08351840
PA
4264 /* In some cases, we might not be able to remove a breakpoint in
4265 a shared library that has already been removed, but we have
4266 not yet processed the shlib unload event. Similarly for an
4267 unloaded add-symbol-file object - the user might not yet have
4268 had the chance to remove-symbol-file it. shlib_disabled will
4269 be set if the library/object has already been removed, but
4270 the breakpoint hasn't been uninserted yet, e.g., after
4271 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4272 always-inserted mode. */
076855f9 4273 if (val
08351840
PA
4274 && (bl->loc_type == bp_loc_software_breakpoint
4275 && (bl->shlib_disabled
4276 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4277 || shared_objfile_contains_address_p (bl->pspace,
4278 bl->address))))
879d1e6b
UW
4279 val = 0;
4280
c906108c
SS
4281 if (val)
4282 return val;
b2b6a7da 4283 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4284 }
35df4500 4285 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4286 {
b2b6a7da 4287 bl->inserted = (reason == DETACH_BREAKPOINT);
c47614fe 4288 bl->owner->remove_location (bl, reason);
2e70b7b9 4289
c906108c 4290 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4291 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4292 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4293 bl->owner->number);
c906108c 4294 }
35df4500 4295 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
4296 && breakpoint_enabled (bl->owner)
4297 && !bl->duplicate)
ce78b96d 4298 {
c47614fe 4299 val = bl->owner->remove_location (bl, reason);
ce78b96d
JB
4300 if (val)
4301 return val;
77b06cd7 4302
b2b6a7da 4303 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4304 }
c906108c
SS
4305
4306 return 0;
4307}
4308
6c95b8df 4309static int
834c0d03 4310remove_breakpoint (struct bp_location *bl)
6c95b8df 4311{
35df4500
TJB
4312 /* BL is never in moribund_locations by our callers. */
4313 gdb_assert (bl->owner != NULL);
2bdf28a0 4314
6c95b8df
PA
4315 /* The type of none suggests that owner is actually deleted.
4316 This should not ever happen. */
35df4500 4317 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4318
5ed8105e 4319 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4320
35df4500 4321 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4322
5ed8105e 4323 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4324}
4325
c72348e3 4326/* See breakpoint.h. */
c906108c 4327
25b22b0a 4328void
c72348e3 4329mark_breakpoints_out (program_space *pspace)
c906108c 4330{
48d7020b 4331 for (bp_location *bl : all_bp_locations ())
c72348e3 4332 if (bl->pspace == pspace)
35df4500 4333 bl->inserted = 0;
c906108c
SS
4334}
4335
f5928702 4336/* See breakpoint.h. */
c906108c
SS
4337
4338void
f5928702 4339breakpoint_init_inferior (inferior *inf, inf_context context)
c906108c 4340{
50c71eaf
PA
4341 /* If breakpoint locations are shared across processes, then there's
4342 nothing to do. */
f5928702 4343 if (gdbarch_has_global_breakpoints (inf->arch ()))
50c71eaf
PA
4344 return;
4345
f5928702 4346 mark_breakpoints_out (inf->pspace);
075f6582 4347
a1decfc1 4348 for (breakpoint &b : all_breakpoints_safe ())
01add95b 4349 {
f5928702 4350 if (b.has_locations () && b.first_loc ().pspace != inf->pspace)
01add95b 4351 continue;
6c95b8df 4352
a1decfc1 4353 switch (b.type)
01add95b
SM
4354 {
4355 case bp_call_dummy:
4356 case bp_longjmp_call_dummy:
c906108c 4357
01add95b
SM
4358 /* If the call dummy breakpoint is at the entry point it will
4359 cause problems when the inferior is rerun, so we better get
4360 rid of it. */
ab92d69b 4361
01add95b 4362 case bp_watchpoint_scope:
ab92d69b 4363
01add95b 4364 /* Also get rid of scope breakpoints. */
ab92d69b 4365
01add95b 4366 case bp_shlib_event:
ab92d69b 4367
01add95b
SM
4368 /* Also remove solib event breakpoints. Their addresses may
4369 have changed since the last time we ran the program.
4370 Actually we may now be debugging against different target;
4371 and so the solib backend that installed this breakpoint may
4372 not be used in by the target. E.g.,
ab92d69b 4373
01add95b
SM
4374 (gdb) file prog-linux
4375 (gdb) run # native linux target
4376 ...
4377 (gdb) kill
4378 (gdb) file prog-win.exe
4379 (gdb) tar rem :9999 # remote Windows gdbserver.
4380 */
c906108c 4381
01add95b 4382 case bp_step_resume:
f59f708a 4383
01add95b 4384 /* Also remove step-resume breakpoints. */
f59f708a 4385
01add95b 4386 case bp_single_step:
7c16b83e 4387
01add95b 4388 /* Also remove single-step breakpoints. */
7c16b83e 4389
a1decfc1 4390 delete_breakpoint (&b);
01add95b 4391 break;
c906108c 4392
01add95b
SM
4393 case bp_watchpoint:
4394 case bp_hardware_watchpoint:
4395 case bp_read_watchpoint:
4396 case bp_access_watchpoint:
4397 {
a1decfc1 4398 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
c906108c 4399
01add95b 4400 /* Likewise for watchpoints on local expressions. */
a1decfc1
SM
4401 if (w.exp_valid_block != NULL)
4402 delete_breakpoint (&b);
01add95b
SM
4403 else
4404 {
4405 /* Get rid of existing locations, which are no longer
4406 valid. New ones will be created in
4407 update_watchpoint, when the inferior is restarted.
4408 The next update_global_location_list call will
4409 garbage collect them. */
a1decfc1 4410 b.clear_locations ();
01add95b
SM
4411
4412 if (context == inf_starting)
4413 {
4414 /* Reset val field to force reread of starting value in
4415 insert_breakpoints. */
a1decfc1
SM
4416 w.val.reset (nullptr);
4417 w.val_valid = false;
01add95b
SM
4418 }
4419 }
4420 }
4421 break;
4422 default:
4423 break;
3a5c3e22 4424 }
01add95b 4425 }
1c5cfe86
PA
4426
4427 /* Get rid of the moribund locations. */
1123588c 4428 for (bp_location *bl : moribund_locations)
35df4500 4429 decref_bp_location (&bl);
1123588c 4430 moribund_locations.clear ();
c906108c
SS
4431}
4432
6c95b8df
PA
4433/* These functions concern about actual breakpoints inserted in the
4434 target --- to e.g. check if we need to do decr_pc adjustment or if
4435 we need to hop over the bkpt --- so we check for address space
4436 match, not program space. */
4437
c2c6d25f
JM
4438/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4439 exists at PC. It returns ordinary_breakpoint_here if it's an
4440 ordinary breakpoint, or permanent_breakpoint_here if it's a
4441 permanent breakpoint.
4442 - When continuing from a location with an ordinary breakpoint, we
4443 actually single step once before calling insert_breakpoints.
e5dd4106 4444 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4445 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4446 the target, to advance the PC past the breakpoint. */
c906108c 4447
c2c6d25f 4448enum breakpoint_here
accd0bcd 4449breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4450{
d8de7963 4451 bool any_breakpoint_here = false;
c906108c 4452
48d7020b 4453 for (bp_location *bl : all_bp_locations ())
075f6582 4454 {
35df4500
TJB
4455 if (bl->loc_type != bp_loc_software_breakpoint
4456 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4457 continue;
4458
f1310107 4459 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4460 if ((breakpoint_enabled (bl->owner)
1a853c52 4461 || bl->permanent)
f1310107 4462 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4463 {
4464 if (overlay_debugging
35df4500
TJB
4465 && section_is_overlay (bl->section)
4466 && !section_is_mapped (bl->section))
075f6582 4467 continue; /* unmapped overlay -- can't be a match */
1a853c52 4468 else if (bl->permanent)
075f6582
DJ
4469 return permanent_breakpoint_here;
4470 else
d8de7963 4471 any_breakpoint_here = true;
075f6582
DJ
4472 }
4473 }
c906108c 4474
f486487f 4475 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4476}
4477
d35ae833
PA
4478/* See breakpoint.h. */
4479
4480int
accd0bcd 4481breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4482 CORE_ADDR addr, ULONGEST len)
4483{
48d7020b 4484 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4485 {
4486 if (bl->loc_type != bp_loc_software_breakpoint
4487 && bl->loc_type != bp_loc_hardware_breakpoint)
4488 continue;
4489
4490 if ((breakpoint_enabled (bl->owner)
4491 || bl->permanent)
4492 && breakpoint_location_address_range_overlap (bl, aspace,
4493 addr, len))
4494 {
4495 if (overlay_debugging
4496 && section_is_overlay (bl->section)
4497 && !section_is_mapped (bl->section))
4498 {
4499 /* Unmapped overlay -- can't be a match. */
4500 continue;
4501 }
4502
4503 return 1;
4504 }
4505 }
4506
4507 return 0;
4508}
4509
1c5cfe86
PA
4510/* Return true if there's a moribund breakpoint at PC. */
4511
4512int
accd0bcd 4513moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4514{
1123588c 4515 for (bp_location *loc : moribund_locations)
f1310107 4516 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4517 return 1;
4518
4519 return 0;
4520}
c2c6d25f 4521
d8de7963 4522/* Returns true iff BL is inserted at PC, in address space ASPACE. */
f7ce857f 4523
d8de7963 4524static bool
b2ff9ed3 4525bp_location_inserted_here_p (const struct bp_location *bl,
accd0bcd 4526 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4527{
4528 if (bl->inserted
f9582a22 4529 && breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
f7ce857f
PA
4530 aspace, pc))
4531 {
d8de7963
AB
4532 /* An unmapped overlay can't be a match. */
4533 return !(overlay_debugging
4534 && section_is_overlay (bl->section)
4535 && !section_is_mapped (bl->section));
f7ce857f 4536 }
d8de7963 4537 return false;
f7ce857f
PA
4538}
4539
a1fd2fa5 4540/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4541
4542int
accd0bcd 4543breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4544{
e0d9a270 4545 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4546 {
35df4500
TJB
4547 if (bl->loc_type != bp_loc_software_breakpoint
4548 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4549 continue;
4550
f7ce857f
PA
4551 if (bp_location_inserted_here_p (bl, aspace, pc))
4552 return 1;
c5aa993b 4553 }
c36b740a
VP
4554 return 0;
4555}
4556
a1fd2fa5
PA
4557/* This function returns non-zero iff there is a software breakpoint
4558 inserted at PC. */
c36b740a
VP
4559
4560int
accd0bcd 4561software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4562 CORE_ADDR pc)
4fa8626c 4563{
e0d9a270 4564 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4565 {
35df4500 4566 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4567 continue;
4568
f7ce857f
PA
4569 if (bp_location_inserted_here_p (bl, aspace, pc))
4570 return 1;
4fa8626c
DJ
4571 }
4572
4573 return 0;
9c02b525
PA
4574}
4575
4576/* See breakpoint.h. */
4577
4578int
accd0bcd 4579hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4580 CORE_ADDR pc)
4581{
e0d9a270 4582 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4583 {
9c02b525
PA
4584 if (bl->loc_type != bp_loc_hardware_breakpoint)
4585 continue;
4586
4587 if (bp_location_inserted_here_p (bl, aspace, pc))
4588 return 1;
4589 }
4590
4591 return 0;
4fa8626c
DJ
4592}
4593
9093389c 4594int
accd0bcd 4595hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4596 CORE_ADDR addr, ULONGEST len)
4597{
a1decfc1 4598 for (breakpoint &bpt : all_breakpoints ())
9093389c 4599 {
a1decfc1
SM
4600 if (bpt.type != bp_hardware_watchpoint
4601 && bpt.type != bp_access_watchpoint)
9093389c
PA
4602 continue;
4603
a1decfc1 4604 if (!breakpoint_enabled (&bpt))
9093389c
PA
4605 continue;
4606
a1decfc1 4607 for (bp_location &loc : bpt.locations ())
f9582a22 4608 if (loc.pspace->aspace.get () == aspace && loc.inserted)
9093389c
PA
4609 {
4610 CORE_ADDR l, h;
4611
4612 /* Check for intersection. */
b00b30b2
SM
4613 l = std::max<CORE_ADDR> (loc.address, addr);
4614 h = std::min<CORE_ADDR> (loc.address + loc.length, addr + len);
9093389c
PA
4615 if (l < h)
4616 return 1;
4617 }
4618 }
4619 return 0;
4620}
c5aa993b 4621
f2478a7e 4622/* See breakpoint.h. */
c906108c 4623
f2478a7e
SM
4624bool
4625is_catchpoint (struct breakpoint *b)
c906108c 4626{
f2478a7e 4627 return (b->type == bp_catchpoint);
c906108c
SS
4628}
4629
c906108c
SS
4630/* Clear a bpstat so that it says we are not at any breakpoint.
4631 Also free any storage that is part of a bpstat. */
4632
4633void
313f3b21 4634bpstat_clear (bpstat **bsp)
c906108c 4635{
313f3b21
SM
4636 bpstat *p;
4637 bpstat *q;
c906108c
SS
4638
4639 if (bsp == 0)
4640 return;
4641 p = *bsp;
4642 while (p != NULL)
4643 {
4644 q = p->next;
04afa70c 4645 delete p;
c906108c
SS
4646 p = q;
4647 }
4648 *bsp = NULL;
4649}
4650
313f3b21 4651bpstat::bpstat (const bpstat &other)
04afa70c
TT
4652 : next (NULL),
4653 bp_location_at (other.bp_location_at),
4654 breakpoint_at (other.breakpoint_at),
4655 commands (other.commands),
04afa70c
TT
4656 print (other.print),
4657 stop (other.stop),
4658 print_it (other.print_it)
4659{
850645cf 4660 if (other.old_val != NULL)
f28085df 4661 old_val = release_value (other.old_val->copy ());
04afa70c
TT
4662}
4663
c906108c
SS
4664/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4665 is part of the bpstat is copied as well. */
4666
313f3b21
SM
4667bpstat *
4668bpstat_copy (bpstat *bs)
c906108c 4669{
313f3b21
SM
4670 bpstat *p = nullptr;
4671 bpstat *tmp;
4672 bpstat *retval = nullptr;
c906108c
SS
4673
4674 if (bs == NULL)
4675 return bs;
4676
4677 for (; bs != NULL; bs = bs->next)
4678 {
313f3b21 4679 tmp = new bpstat (*bs);
31cc81e9 4680
c906108c
SS
4681 if (p == NULL)
4682 /* This is the first thing in the chain. */
4683 retval = tmp;
4684 else
4685 p->next = tmp;
4686 p = tmp;
4687 }
4688 p->next = NULL;
4689 return retval;
4690}
4691
4a64f543 4692/* Find the bpstat associated with this breakpoint. */
c906108c 4693
313f3b21
SM
4694bpstat *
4695bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
c906108c 4696{
c5aa993b
JM
4697 if (bsp == NULL)
4698 return NULL;
c906108c 4699
c5aa993b
JM
4700 for (; bsp != NULL; bsp = bsp->next)
4701 {
f431efe5 4702 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4703 return bsp;
4704 }
c906108c
SS
4705 return NULL;
4706}
4707
ab04a2af
TT
4708/* See breakpoint.h. */
4709
4c462cb0 4710bool
313f3b21 4711bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
ab04a2af 4712{
ab04a2af
TT
4713 for (; bsp != NULL; bsp = bsp->next)
4714 {
427cd150
TT
4715 if (bsp->breakpoint_at == NULL)
4716 {
4717 /* A moribund location can never explain a signal other than
4718 GDB_SIGNAL_TRAP. */
4719 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4720 return true;
427cd150
TT
4721 }
4722 else
47591c29 4723 {
c47614fe 4724 if (bsp->breakpoint_at->explains_signal (sig))
4c462cb0 4725 return true;
47591c29 4726 }
ab04a2af
TT
4727 }
4728
4c462cb0 4729 return false;
ab04a2af
TT
4730}
4731
78805ff8 4732/* See breakpoint.h. */
c906108c
SS
4733
4734int
313f3b21 4735bpstat_num (bpstat **bsp, int *num)
c906108c
SS
4736{
4737 struct breakpoint *b;
4738
4739 if ((*bsp) == NULL)
4740 return 0; /* No more breakpoint values */
8671a17b 4741
4a64f543
MS
4742 /* We assume we'll never have several bpstats that correspond to a
4743 single breakpoint -- otherwise, this function might return the
4744 same number more than once and this will look ugly. */
f431efe5 4745 b = (*bsp)->breakpoint_at;
8671a17b
PA
4746 *bsp = (*bsp)->next;
4747 if (b == NULL)
4748 return -1; /* breakpoint that's been deleted since */
4749
4750 *num = b->number; /* We have its number */
4751 return 1;
c906108c
SS
4752}
4753
78805ff8
PW
4754/* See breakpoint.h */
4755
4756int
4757bpstat_locno (const bpstat *bs)
4758{
4759 const struct breakpoint *b = bs->breakpoint_at;
4760 const struct bp_location *bl = bs->bp_location_at.get ();
4761
9dc1523b 4762 if (b != nullptr && b->has_multiple_locations ())
78805ff8 4763 {
20afe380 4764 int locno = 1;
78805ff8 4765
b00b30b2 4766 for (bp_location &loc : b->locations ())
78805ff8 4767 {
b00b30b2 4768 if (bl == &loc)
20afe380
SM
4769 return locno;
4770
4771 ++locno;
78805ff8 4772 }
20afe380
SM
4773
4774 warning (_("location number not found for breakpoint %d address %s."),
4775 b->number, paddress (bl->gdbarch, bl->address));
78805ff8
PW
4776 }
4777
20afe380 4778 return 0;
78805ff8
PW
4779}
4780
4781/* See breakpoint.h. */
4782
4783void
4784print_num_locno (const bpstat *bs, struct ui_out *uiout)
4785{
4786 struct breakpoint *b = bs->breakpoint_at;
4787
4788 if (b == nullptr)
4789 uiout->text (_("deleted breakpoint"));
4790 else
4791 {
4792 uiout->field_signed ("bkptno", b->number);
4793
4794 int locno = bpstat_locno (bs);
4795 if (locno != 0)
4796 uiout->message (".%pF", signed_field ("locno", locno));
4797 }
4798}
4799
e93ca019 4800/* See breakpoint.h. */
c906108c
SS
4801
4802void
e93ca019 4803bpstat_clear_actions (void)
c906108c 4804{
313f3b21 4805 bpstat *bs;
e93ca019 4806
00431a78 4807 if (inferior_ptid == null_ptid)
e93ca019
JK
4808 return;
4809
00431a78 4810 thread_info *tp = inferior_thread ();
e93ca019 4811 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4812 {
d1b0a7bf 4813 bs->commands = NULL;
850645cf 4814 bs->old_val.reset (nullptr);
c906108c
SS
4815 }
4816}
4817
f3b1572e
PA
4818/* Called when a command is about to proceed the inferior. */
4819
4820static void
4821breakpoint_about_to_proceed (void)
4822{
d7e15655 4823 if (inferior_ptid != null_ptid)
f3b1572e
PA
4824 {
4825 struct thread_info *tp = inferior_thread ();
4826
4827 /* Allow inferior function calls in breakpoint commands to not
4828 interrupt the command list. When the call finishes
4829 successfully, the inferior will be standing at the same
4830 breakpoint as if nothing happened. */
16c381f0 4831 if (tp->control.in_infcall)
f3b1572e
PA
4832 return;
4833 }
4834
4835 breakpoint_proceeded = 1;
4836}
4837
d8de7963 4838/* Return true iff CMD as the first line of a command sequence is `silent'
abf85f46
JK
4839 or its equivalent. */
4840
d8de7963 4841static bool
abf85f46
JK
4842command_line_is_silent (struct command_line *cmd)
4843{
4f45d445 4844 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4845}
4846
83f35083
PW
4847/* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4848 A locno 0 is changed to 1 to e.g. let the user do
4849 (gdb) disable $_hit_bpnum.$_hit_locno
4850 for a single location breakpoint. */
4851
78805ff8 4852static void
83f35083 4853set_hit_convenience_vars (int bpnum, int locno)
78805ff8 4854{
83f35083
PW
4855 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4856 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4857 (locno > 0 ? locno : 1));
78805ff8
PW
4858}
4859
4a64f543
MS
4860/* Execute all the commands associated with all the breakpoints at
4861 this location. Any of these commands could cause the process to
4862 proceed beyond this point, etc. We look out for such changes by
4863 checking the global "breakpoint_proceeded" after each command.
c906108c 4864
347bddb7
PA
4865 Returns true if a breakpoint command resumed the inferior. In that
4866 case, it is the caller's responsibility to recall it again with the
4867 bpstat of the current thread. */
4868
d8de7963 4869static bool
313f3b21 4870bpstat_do_actions_1 (bpstat **bsp)
c906108c 4871{
313f3b21 4872 bpstat *bs;
d8de7963 4873 bool again = false;
c906108c
SS
4874
4875 /* Avoid endless recursion if a `source' command is contained
4876 in bs->commands. */
4877 if (executing_breakpoint_commands)
d8de7963 4878 return false;
c906108c 4879
81b1e71c
TT
4880 scoped_restore save_executing
4881 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4882
1ac32117 4883 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4884
4a64f543 4885 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4886 bs = *bsp;
4887
78805ff8 4888 /* The $_hit_* convenience variables are set before running the
83f35083
PW
4889 commands of BS. In case we have several bs, after the loop,
4890 we set again the variables to the first printed bpnum and locno.
4891 For multiple breakpoints, this ensures the variables are set to the
4892 breakpoint printed for the user. */
4893 int printed_hit_bpnum = -1;
4894 int printed_hit_locno = -1;
78805ff8 4895
c906108c
SS
4896 breakpoint_proceeded = 0;
4897 for (; bs != NULL; bs = bs->next)
4898 {
d1b0a7bf 4899 struct command_line *cmd = NULL;
6c50ab1c 4900
83f35083
PW
4901 /* Set the _hit_* convenience variables before running BS's commands. */
4902 {
4903 const struct breakpoint *b = bs->breakpoint_at;
4904 if (b != nullptr)
4905 {
4906 int locno = bpstat_locno (bs);
4907
4908 set_hit_convenience_vars (b->number, locno);
4909 if (printed_hit_locno == -1 && bs->print)
4910 {
4911 printed_hit_bpnum = b->number;
4912 printed_hit_locno = locno;
4913 }
4914 }
4915 }
78805ff8 4916
6c50ab1c
JB
4917 /* Take ownership of the BSP's command tree, if it has one.
4918
dda83cd7
SM
4919 The command tree could legitimately contain commands like
4920 'step' and 'next', which call clear_proceed_status, which
83f35083 4921 frees the bpstat BS and its command tree. To make sure this doesn't
dda83cd7
SM
4922 free the tree we're executing out from under us, we need to
4923 take ownership of the tree ourselves. Since a given bpstat's
4924 commands are only executed once, we don't need to copy it; we
4925 can clear the pointer in the bpstat, and make sure we free
4926 the tree when we're done. */
d1b0a7bf 4927 counted_command_line ccmd = bs->commands;
9add0f1b 4928 bs->commands = NULL;
d1b0a7bf
TT
4929 if (ccmd != NULL)
4930 cmd = ccmd.get ();
abf85f46
JK
4931 if (command_line_is_silent (cmd))
4932 {
4933 /* The action has been already done by bpstat_stop_status. */
4934 cmd = cmd->next;
4935 }
6c50ab1c 4936
c906108c
SS
4937 while (cmd != NULL)
4938 {
4939 execute_control_command (cmd);
83f35083
PW
4940 /* After execute_control_command, if breakpoint_proceeded is true,
4941 BS has been freed and cannot be accessed anymore. */
c906108c
SS
4942
4943 if (breakpoint_proceeded)
4944 break;
4945 else
4946 cmd = cmd->next;
4947 }
6c50ab1c 4948
c906108c 4949 if (breakpoint_proceeded)
32c1e744 4950 {
cb814510 4951 if (current_ui->async)
347bddb7
PA
4952 /* If we are in async mode, then the target might be still
4953 running, not stopped at any breakpoint, so nothing for
4954 us to do here -- just return to the event loop. */
4955 ;
32c1e744
VP
4956 else
4957 /* In sync mode, when execute_control_command returns
4958 we're already standing on the next breakpoint.
347bddb7
PA
4959 Breakpoint commands for that stop were not run, since
4960 execute_command does not run breakpoint commands --
4961 only command_line_handler does, but that one is not
4962 involved in execution of breakpoint commands. So, we
4963 can now execute breakpoint commands. It should be
4964 noted that making execute_command do bpstat actions is
4965 not an option -- in this case we'll have recursive
4966 invocation of bpstat for each breakpoint with a
4967 command, and can easily blow up GDB stack. Instead, we
4968 return true, which will trigger the caller to recall us
4969 with the new stop_bpstat. */
d8de7963 4970 again = true;
347bddb7 4971 break;
32c1e744 4972 }
c906108c 4973 }
78805ff8
PW
4974
4975 /* Now that we have executed the commands of all bs, set the _hit_*
83f35083
PW
4976 convenience variables to the printed values. */
4977 if (printed_hit_locno != -1)
4978 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
78805ff8 4979
347bddb7
PA
4980 return again;
4981}
4982
00431a78
PA
4983/* Helper for bpstat_do_actions. Get the current thread, if there's
4984 one, is alive and has execution. Return NULL otherwise. */
4985
4986static thread_info *
4987get_bpstat_thread ()
4988{
55f6301a 4989 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4990 return NULL;
4991
4992 thread_info *tp = inferior_thread ();
611841bb 4993 if (tp->state == THREAD_EXITED || tp->executing ())
00431a78
PA
4994 return NULL;
4995 return tp;
4996}
4997
347bddb7
PA
4998void
4999bpstat_do_actions (void)
5000{
694c6bf5 5001 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 5002 thread_info *tp;
353d1d73 5003
347bddb7 5004 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
5005 while ((tp = get_bpstat_thread ()) != NULL)
5006 {
5007 /* Since in sync mode, bpstat_do_actions may resume the
5008 inferior, and only return when it is stopped at the next
5009 breakpoint, we keep doing breakpoint actions until it returns
5010 false to indicate the inferior was not resumed. */
5011 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
5012 break;
5013 }
353d1d73 5014
694c6bf5 5015 cleanup_if_error.release ();
c906108c
SS
5016}
5017
fa4727a6
DJ
5018/* Print out the (old or new) value associated with a watchpoint. */
5019
5020static void
5021watchpoint_value_print (struct value *val, struct ui_file *stream)
5022{
5023 if (val == NULL)
7f6aba03 5024 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 5025 else
79a45b7d
TT
5026 {
5027 struct value_print_options opts;
5028 get_user_print_options (&opts);
5029 value_print (val, stream, &opts);
5030 }
fa4727a6
DJ
5031}
5032
f303dbd6
PA
5033/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
5034 debugging multiple threads. */
5035
5036void
5037maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
5038{
112e8700 5039 if (uiout->is_mi_like_p ())
f303dbd6
PA
5040 return;
5041
112e8700 5042 uiout->text ("\n");
f303dbd6
PA
5043
5044 if (show_thread_that_caused_stop ())
5045 {
f303dbd6
PA
5046 struct thread_info *thr = inferior_thread ();
5047
112e8700 5048 uiout->text ("Thread ");
33eca680 5049 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6 5050
25558938 5051 const char *name = thread_name (thr);
f303dbd6
PA
5052 if (name != NULL)
5053 {
112e8700 5054 uiout->text (" \"");
33eca680 5055 uiout->field_string ("name", name);
112e8700 5056 uiout->text ("\"");
f303dbd6
PA
5057 }
5058
112e8700 5059 uiout->text (" hit ");
f303dbd6
PA
5060 }
5061}
5062
e514a9d6 5063/* Generic routine for printing messages indicating why we
4a64f543 5064 stopped. The behavior of this function depends on the value
e514a9d6
JM
5065 'print_it' in the bpstat structure. Under some circumstances we
5066 may decide not to print anything here and delegate the task to
4a64f543 5067 normal_stop(). */
e514a9d6
JM
5068
5069static enum print_stop_action
313f3b21 5070print_bp_stop_message (bpstat *bs)
e514a9d6
JM
5071{
5072 switch (bs->print_it)
5073 {
5074 case print_it_noop:
4a64f543 5075 /* Nothing should be printed for this bpstat entry. */
e514a9d6 5076 return PRINT_UNKNOWN;
e514a9d6
JM
5077
5078 case print_it_done:
5079 /* We still want to print the frame, but we already printed the
dda83cd7 5080 relevant messages. */
e514a9d6 5081 return PRINT_SRC_AND_LOC;
e514a9d6
JM
5082
5083 case print_it_normal:
4f8d1dc6 5084 {
f431efe5
PA
5085 struct breakpoint *b = bs->breakpoint_at;
5086
1a6a67de
TJB
5087 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
5088 which has since been deleted. */
5089 if (b == NULL)
5090 return PRINT_UNKNOWN;
5091
348d480f 5092 /* Normal case. Call the breakpoint's print_it method. */
c47614fe 5093 return b->print_it (bs);
4f8d1dc6 5094 }
3086aeae 5095
e514a9d6 5096 default:
f34652de 5097 internal_error (_("print_bp_stop_message: unrecognized enum value"));
c906108c 5098 }
c906108c
SS
5099}
5100
2f9ee862 5101/* See breakpoint.h. */
edcc5120 5102
2f9ee862 5103void
4e9e993a 5104print_solib_event (bool is_catchpoint)
edcc5120 5105{
6fb16ce6 5106 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 5107 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
5108
5109 if (!is_catchpoint)
5110 {
5111 if (any_added || any_deleted)
112e8700 5112 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 5113 else
112e8700
SM
5114 current_uiout->text (_("Stopped due to shared library event (no "
5115 "libraries added or removed)\n"));
edcc5120
TT
5116 }
5117
112e8700
SM
5118 if (current_uiout->is_mi_like_p ())
5119 current_uiout->field_string ("reason",
5120 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
5121
5122 if (any_deleted)
5123 {
112e8700 5124 current_uiout->text (_(" Inferior unloaded "));
10f489e5 5125 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 5126 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 5127 {
6fb16ce6
SM
5128 const std::string &name = current_program_space->deleted_solibs[ix];
5129
edcc5120 5130 if (ix > 0)
112e8700
SM
5131 current_uiout->text (" ");
5132 current_uiout->field_string ("library", name);
5133 current_uiout->text ("\n");
edcc5120 5134 }
edcc5120
TT
5135 }
5136
5137 if (any_added)
5138 {
112e8700 5139 current_uiout->text (_(" Inferior loaded "));
10f489e5 5140 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 5141 bool first = true;
7b323785 5142 for (solib *iter : current_program_space->added_solibs)
edcc5120 5143 {
bcb430e4 5144 if (!first)
112e8700 5145 current_uiout->text (" ");
bcb430e4 5146 first = false;
6896e625 5147 current_uiout->field_string ("library", iter->name);
112e8700 5148 current_uiout->text ("\n");
edcc5120 5149 }
edcc5120
TT
5150 }
5151}
5152
e514a9d6
JM
5153/* Print a message indicating what happened. This is called from
5154 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
5155 list - a list of the eventpoints that caused this stop. KIND is
5156 the target_waitkind for the stopping event. This
e514a9d6
JM
5157 routine calls the generic print routine for printing a message
5158 about reasons for stopping. This will print (for example) the
5159 "Breakpoint n," part of the output. The return value of this
5160 routine is one of:
c906108c 5161
4a64f543 5162 PRINT_UNKNOWN: Means we printed nothing.
917317f4 5163 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 5164 code to print the location. An example is
c5aa993b
JM
5165 "Breakpoint 1, " which should be followed by
5166 the location.
917317f4 5167 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
5168 to also print the location part of the message.
5169 An example is the catch/throw messages, which
4a64f543 5170 don't require a location appended to the end.
917317f4 5171 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 5172 further info to be printed. */
c906108c 5173
917317f4 5174enum print_stop_action
403c71fd 5175bpstat_print (bpstat *bs, target_waitkind kind)
c906108c 5176{
f486487f 5177 enum print_stop_action val;
c5aa993b 5178
c906108c 5179 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
5180 (Currently all watchpoints go on the bpstat whether hit or not.
5181 That probably could (should) be changed, provided care is taken
c906108c 5182 with respect to bpstat_explains_signal). */
e514a9d6
JM
5183 for (; bs; bs = bs->next)
5184 {
5185 val = print_bp_stop_message (bs);
5186 if (val == PRINT_SRC_ONLY
78805ff8 5187 || val == PRINT_SRC_AND_LOC
e514a9d6
JM
5188 || val == PRINT_NOTHING)
5189 return val;
5190 }
c906108c 5191
36dfb11c
TT
5192 /* If we had hit a shared library event breakpoint,
5193 print_bp_stop_message would print out this message. If we hit an
5194 OS-level shared library event, do the same thing. */
5195 if (kind == TARGET_WAITKIND_LOADED)
5196 {
4e9e993a 5197 print_solib_event (false);
36dfb11c
TT
5198 return PRINT_NOTHING;
5199 }
5200
e514a9d6 5201 /* We reached the end of the chain, or we got a null BS to start
4a64f543 5202 with and nothing was printed. */
917317f4 5203 return PRINT_UNKNOWN;
c906108c
SS
5204}
5205
bf469271 5206/* Evaluate the boolean expression EXP and return the result. */
c906108c 5207
bf469271
PA
5208static bool
5209breakpoint_cond_eval (expression *exp)
c906108c 5210{
65558ca5 5211 scoped_value_mark mark;
43048e46 5212 return value_true (exp->evaluate ());
c906108c
SS
5213}
5214
5760d0ab 5215/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 5216
313f3b21 5217bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
04afa70c 5218 : next (NULL),
b6433ede 5219 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
5220 breakpoint_at (bl->owner),
5221 commands (NULL),
04afa70c
TT
5222 print (0),
5223 stop (0),
5224 print_it (print_it_normal)
c906108c 5225{
04afa70c
TT
5226 **bs_link_pointer = this;
5227 *bs_link_pointer = &next;
5228}
5229
313f3b21 5230bpstat::bpstat ()
04afa70c 5231 : next (NULL),
04afa70c
TT
5232 breakpoint_at (NULL),
5233 commands (NULL),
04afa70c
TT
5234 print (0),
5235 stop (0),
5236 print_it (print_it_normal)
5237{
c906108c
SS
5238}
5239\f
d983da9c
DJ
5240/* The target has stopped with waitstatus WS. Check if any hardware
5241 watchpoints have triggered, according to the target. */
5242
5243int
c272a98c 5244watchpoints_triggered (const target_waitstatus &ws)
d983da9c 5245{
57810aa7 5246 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 5247 CORE_ADDR addr;
d983da9c
DJ
5248
5249 if (!stopped_by_watchpoint)
5250 {
5251 /* We were not stopped by a watchpoint. Mark all watchpoints
5252 as not triggered. */
a1decfc1
SM
5253 for (breakpoint &b : all_breakpoints ())
5254 if (is_hardware_watchpoint (&b))
3a5c3e22 5255 {
a1decfc1 5256 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
3a5c3e22 5257
a1decfc1 5258 w.watchpoint_triggered = watch_triggered_no;
3a5c3e22 5259 }
d983da9c
DJ
5260
5261 return 0;
5262 }
5263
328d42d8 5264 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
5265 {
5266 /* We were stopped by a watchpoint, but we don't know where.
5267 Mark all watchpoints as unknown. */
a1decfc1
SM
5268 for (breakpoint &b : all_breakpoints ())
5269 if (is_hardware_watchpoint (&b))
3a5c3e22 5270 {
a1decfc1 5271 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
3a5c3e22 5272
a1decfc1 5273 w.watchpoint_triggered = watch_triggered_unknown;
3a5c3e22 5274 }
d983da9c 5275
3c4797ba 5276 return 1;
d983da9c
DJ
5277 }
5278
5279 /* The target could report the data address. Mark watchpoints
5280 affected by this data address as triggered, and all others as not
5281 triggered. */
5282
a1decfc1
SM
5283 for (breakpoint &b : all_breakpoints ())
5284 if (is_hardware_watchpoint (&b))
d983da9c 5285 {
a1decfc1 5286 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
d983da9c 5287
a1decfc1
SM
5288 w.watchpoint_triggered = watch_triggered_no;
5289 for (bp_location &loc : b.locations ())
9c06b0b4 5290 {
a1decfc1 5291 if (is_masked_watchpoint (&b))
9c06b0b4 5292 {
a1decfc1
SM
5293 CORE_ADDR newaddr = addr & w.hw_wp_mask;
5294 CORE_ADDR start = loc.address & w.hw_wp_mask;
9c06b0b4
TJB
5295
5296 if (newaddr == start)
5297 {
a1decfc1 5298 w.watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5299 break;
5300 }
5301 }
5302 /* Exact match not required. Within range is sufficient. */
328d42d8 5303 else if (target_watchpoint_addr_within_range
b00b30b2
SM
5304 (current_inferior ()->top_target (), addr, loc.address,
5305 loc.length))
9c06b0b4 5306 {
a1decfc1 5307 w.watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5308 break;
5309 }
5310 }
d983da9c
DJ
5311 }
5312
5313 return 1;
5314}
5315
bf469271
PA
5316/* Possible return values for watchpoint_check. */
5317enum wp_check_result
5318 {
5319 /* The watchpoint has been deleted. */
5320 WP_DELETED = 1,
5321
5322 /* The value has changed. */
5323 WP_VALUE_CHANGED = 2,
5324
5325 /* The value has not changed. */
5326 WP_VALUE_NOT_CHANGED = 3,
5327
5328 /* Ignore this watchpoint, no matter if the value changed or not. */
5329 WP_IGNORE = 4,
5330 };
c906108c
SS
5331
5332#define BP_TEMPFLAG 1
5333#define BP_HARDWAREFLAG 2
5334
4a64f543 5335/* Evaluate watchpoint condition expression and check if its value
bf469271 5336 changed. */
553e4c11 5337
bf469271 5338static wp_check_result
313f3b21 5339watchpoint_check (bpstat *bs)
c906108c 5340{
bd2b40ac 5341 frame_info_ptr fr;
d8de7963 5342 bool within_current_scope;
c906108c 5343
f431efe5 5344 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5345 gdb_assert (bs->breakpoint_at != NULL);
bcafd1c1 5346 watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
d0fb5eae 5347
f6bc2008
PA
5348 /* If this is a local watchpoint, we only want to check if the
5349 watchpoint frame is in scope if the current thread is the thread
5350 that was used to create the watchpoint. */
5351 if (!watchpoint_in_thread_scope (b))
60e1c644 5352 return WP_IGNORE;
f6bc2008 5353
c906108c 5354 if (b->exp_valid_block == NULL)
d8de7963 5355 within_current_scope = true;
c906108c
SS
5356 else
5357 {
bd2b40ac 5358 frame_info_ptr frame = get_current_frame ();
edb3359d
DJ
5359 struct gdbarch *frame_arch = get_frame_arch (frame);
5360 CORE_ADDR frame_pc = get_frame_pc (frame);
5361
c9cf6e20 5362 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5363 still in the function but the stack frame has already been
5364 invalidated. Since we can't rely on the values of local
5365 variables after the stack has been destroyed, we are treating
5366 the watchpoint in that state as `not changed' without further
5367 checking. Don't mark watchpoints as changed if the current
5368 frame is in an epilogue - even if they are in some other
5369 frame, our view of the stack is likely to be wrong and
5370 frame_find_by_id could error out. */
c9cf6e20 5371 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5372 return WP_IGNORE;
a0f49112 5373
101dcfbe 5374 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5375 within_current_scope = (fr != NULL);
69fbadd5
DJ
5376
5377 /* If we've gotten confused in the unwinder, we might have
5378 returned a frame that can't describe this variable. */
edb3359d
DJ
5379 if (within_current_scope)
5380 {
5381 struct symbol *function;
5382
5383 function = get_frame_function (fr);
5384 if (function == NULL
0d191295 5385 || !function->value_block ()->contains (b->exp_valid_block))
d8de7963 5386 within_current_scope = false;
edb3359d 5387 }
69fbadd5 5388
edb3359d 5389 if (within_current_scope)
c906108c
SS
5390 /* If we end up stopping, the current frame will get selected
5391 in normal_stop. So this call to select_frame won't affect
5392 the user. */
0f7d239c 5393 select_frame (fr);
c906108c 5394 }
c5aa993b 5395
c906108c
SS
5396 if (within_current_scope)
5397 {
4a64f543 5398 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
5399 time before we return to the command level and call
5400 free_all_values. We can't call free_all_values because we
5401 might be in the middle of evaluating a function call. */
c906108c 5402
9c06b0b4 5403 struct value *mark;
fa4727a6
DJ
5404 struct value *new_val;
5405
c1fc2657 5406 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5407 /* Since we don't know the exact trigger address (from
5408 stopped_data_address), just tell the user we've triggered
5409 a mask watchpoint. */
5410 return WP_VALUE_CHANGED;
5411
5412 mark = value_mark ();
1eaebe02 5413 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 5414 NULL, NULL, false);
218d2fc6 5415
bb9d5f81
PP
5416 if (b->val_bitsize != 0)
5417 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5418
4a64f543
MS
5419 /* We use value_equal_contents instead of value_equal because
5420 the latter coerces an array to a pointer, thus comparing just
5421 the address of the array instead of its contents. This is
5422 not what we want. */
fa4727a6 5423 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
5424 || (b->val != NULL && !value_equal_contents (b->val.get (),
5425 new_val)))
c906108c 5426 {
c906108c 5427 bs->old_val = b->val;
850645cf 5428 b->val = release_value (new_val);
4c1d86d9 5429 b->val_valid = true;
850645cf
TT
5430 if (new_val != NULL)
5431 value_free_to_mark (mark);
c906108c
SS
5432 return WP_VALUE_CHANGED;
5433 }
5434 else
5435 {
60e1c644 5436 /* Nothing changed. */
c906108c 5437 value_free_to_mark (mark);
c906108c
SS
5438 return WP_VALUE_NOT_CHANGED;
5439 }
5440 }
5441 else
5442 {
5443 /* This seems like the only logical thing to do because
dda83cd7
SM
5444 if we temporarily ignored the watchpoint, then when
5445 we reenter the block in which it is valid it contains
5446 garbage (in the case of a function, it may have two
5447 garbage values, one before and one after the prologue).
5448 So we can't even detect the first assignment to it and
5449 watch after that (since the garbage may or may not equal
5450 the first value assigned). */
348d480f 5451 /* We print all the stop information in
c47614fe
TT
5452 breakpointprint_it, but in this case, by the time we
5453 call breakpoint->print_it this bp will be deleted
348d480f
PA
5454 already. So we have no choice but print the information
5455 here. */
468afe6c 5456
0e454242 5457 SWITCH_THRU_ALL_UIS ()
dda83cd7 5458 {
468afe6c
PA
5459 struct ui_out *uiout = current_uiout;
5460
112e8700
SM
5461 if (uiout->is_mi_like_p ())
5462 uiout->field_string
5463 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5464 uiout->message ("\nWatchpoint %pF deleted because the program has "
5465 "left the block in\n"
5466 "which its expression is valid.\n",
5467 signed_field ("wpnum", b->number));
468afe6c 5468 }
4ce44c66 5469
cdac0397 5470 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5471 b->commands = NULL;
d0fb5eae 5472 watchpoint_del_at_next_stop (b);
c906108c
SS
5473
5474 return WP_DELETED;
5475 }
5476}
5477
18a18393 5478/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5479 breakpoint location BL. This function does not check if we should
5480 stop, only if BL explains the stop. */
5481
d8de7963 5482static bool
6c95b8df 5483bpstat_check_location (const struct bp_location *bl,
accd0bcd 5484 const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5485 const target_waitstatus &ws)
18a18393
VP
5486{
5487 struct breakpoint *b = bl->owner;
5488
348d480f 5489 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5490 gdb_assert (b != NULL);
5491
c47614fe 5492 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5493}
5494
3a5c3e22 5495/* Determine if the watched values have actually changed, and we
4ec2227a 5496 should stop. If not, set BS->stop to false. */
3a5c3e22 5497
18a18393 5498static void
313f3b21 5499bpstat_check_watchpoint (bpstat *bs)
18a18393 5500{
2bdf28a0 5501 const struct bp_location *bl;
2bdf28a0
JK
5502
5503 /* BS is built for existing struct breakpoint. */
b6433ede 5504 bl = bs->bp_location_at.get ();
2bdf28a0 5505 gdb_assert (bl != NULL);
bcafd1c1
TT
5506 watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5507
5508 bool must_check_value = false;
5509
5510 if (b->type == bp_watchpoint)
5511 /* For a software watchpoint, we must always check the
5512 watched value. */
5513 must_check_value = true;
5514 else if (b->watchpoint_triggered == watch_triggered_yes)
5515 /* We have a hardware watchpoint (read, write, or access)
5516 and the target earlier reported an address watched by
5517 this watchpoint. */
5518 must_check_value = true;
5519 else if (b->watchpoint_triggered == watch_triggered_unknown
5520 && b->type == bp_hardware_watchpoint)
5521 /* We were stopped by a hardware watchpoint, but the target could
5522 not report the data address. We must check the watchpoint's
5523 value. Access and read watchpoints are out of luck; without
5524 a data address, we can't figure it out. */
5525 must_check_value = true;
5526
5527 if (must_check_value)
5528 {
5529 wp_check_result e;
3a5c3e22 5530
bcafd1c1
TT
5531 try
5532 {
5533 e = watchpoint_check (bs);
5534 }
5535 catch (const gdb_exception_error &ex)
18a18393 5536 {
bcafd1c1
TT
5537 exception_fprintf (gdb_stderr, ex,
5538 "Error evaluating expression "
5539 "for watchpoint %d\n",
5540 b->number);
bf469271 5541
bcafd1c1 5542 SWITCH_THRU_ALL_UIS ()
bf469271 5543 {
bcafd1c1
TT
5544 gdb_printf (_("Watchpoint %d deleted.\n"),
5545 b->number);
bf469271 5546 }
bcafd1c1
TT
5547 watchpoint_del_at_next_stop (b);
5548 e = WP_DELETED;
5549 }
bf469271 5550
bcafd1c1
TT
5551 switch (e)
5552 {
5553 case WP_DELETED:
5554 /* We've already printed what needs to be printed. */
5555 bs->print_it = print_it_done;
5556 /* Stop. */
5557 break;
5558 case WP_IGNORE:
5559 bs->print_it = print_it_noop;
5560 bs->stop = false;
5561 break;
5562 case WP_VALUE_CHANGED:
5563 if (b->type == bp_read_watchpoint)
18a18393 5564 {
bcafd1c1
TT
5565 /* There are two cases to consider here:
5566
5567 1. We're watching the triggered memory for reads.
5568 In that case, trust the target, and always report
5569 the watchpoint hit to the user. Even though
5570 reads don't cause value changes, the value may
5571 have changed since the last time it was read, and
5572 since we're not trapping writes, we will not see
5573 those, and as such we should ignore our notion of
5574 old value.
5575
5576 2. We're watching the triggered memory for both
5577 reads and writes. There are two ways this may
5578 happen:
5579
5580 2.1. This is a target that can't break on data
5581 reads only, but can break on accesses (reads or
5582 writes), such as e.g., x86. We detect this case
5583 at the time we try to insert read watchpoints.
5584
5585 2.2. Otherwise, the target supports read
5586 watchpoints, but, the user set an access or write
5587 watchpoint watching the same memory as this read
5588 watchpoint.
5589
5590 If we're watching memory writes as well as reads,
5591 ignore watchpoint hits when we find that the
5592 value hasn't changed, as reads don't cause
5593 changes. This still gives false positives when
5594 the program writes the same value to memory as
5595 what there was already in memory (we will confuse
5596 it for a read), but it's much better than
5597 nothing. */
5598
5599 int other_write_watchpoint = 0;
5600
5601 if (bl->watchpoint_type == hw_read)
18a18393 5602 {
bcafd1c1
TT
5603 for (breakpoint &other_b : all_breakpoints ())
5604 if (other_b.type == bp_hardware_watchpoint
5605 || other_b.type == bp_access_watchpoint)
5606 {
5607 watchpoint &other_w =
5608 gdb::checked_static_cast<watchpoint &> (other_b);
5609
5610 if (other_w.watchpoint_triggered
5611 == watch_triggered_yes)
85d721b8 5612 {
bcafd1c1
TT
5613 other_write_watchpoint = 1;
5614 break;
85d721b8 5615 }
bcafd1c1 5616 }
18a18393 5617 }
bcafd1c1
TT
5618
5619 if (other_write_watchpoint
5620 || bl->watchpoint_type == hw_access)
18a18393 5621 {
bcafd1c1
TT
5622 /* We're watching the same memory for writes,
5623 and the value changed since the last time we
5624 updated it, so this trap must be for a write.
5625 Ignore it. */
18a18393 5626 bs->print_it = print_it_noop;
4ec2227a 5627 bs->stop = false;
18a18393 5628 }
18a18393 5629 }
bcafd1c1
TT
5630 break;
5631 case WP_VALUE_NOT_CHANGED:
5632 if (b->type == bp_hardware_watchpoint
5633 || b->type == bp_watchpoint)
5634 {
5635 /* Don't stop: write watchpoints shouldn't fire if
5636 the value hasn't changed. */
5637 bs->print_it = print_it_noop;
5638 bs->stop = false;
5639 }
5640 /* Stop. */
5641 break;
5642 default:
5643 /* Can't happen. */
5644 break;
18a18393
VP
5645 }
5646 }
bcafd1c1
TT
5647 else /* !must_check_value */
5648 {
5649 /* This is a case where some watchpoint(s) triggered, but
5650 not at the address of this watchpoint, or else no
5651 watchpoint triggered after all. So don't print
5652 anything for this watchpoint. */
5653 bs->print_it = print_it_noop;
5654 bs->stop = false;
5655 }
18a18393
VP
5656}
5657
7d4df6a4
DE
5658/* For breakpoints that are currently marked as telling gdb to stop,
5659 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5660 of breakpoint referred to by BS. If we should not stop for this
5661 breakpoint, set BS->stop to 0. */
f431efe5 5662
18a18393 5663static void
313f3b21 5664bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
18a18393 5665{
d8a77e4c
AB
5666 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5667
2bdf28a0
JK
5668 const struct bp_location *bl;
5669 struct breakpoint *b;
bf469271
PA
5670 /* Assume stop. */
5671 bool condition_result = true;
7d4df6a4
DE
5672 struct expression *cond;
5673
5674 gdb_assert (bs->stop);
2bdf28a0
JK
5675
5676 /* BS is built for existing struct breakpoint. */
b6433ede 5677 bl = bs->bp_location_at.get ();
2bdf28a0 5678 gdb_assert (bl != NULL);
f431efe5 5679 b = bs->breakpoint_at;
2bdf28a0 5680 gdb_assert (b != NULL);
18a18393 5681
d8a77e4c
AB
5682 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5683 thread->ptid.to_string ().c_str (),
5684 b->number, find_loc_num_by_location (bl));
5685
b775012e
LM
5686 /* Even if the target evaluated the condition on its end and notified GDB, we
5687 need to do so again since GDB does not know if we stopped due to a
5688 breakpoint or a single step breakpoint. */
5689
18a18393 5690 if (frame_id_p (b->frame_id)
a0cbd650 5691 && b->frame_id != get_stack_frame_id (get_current_frame ()))
18a18393 5692 {
d8a77e4c
AB
5693 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5694 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5695 b->frame_id.to_string ().c_str ());
4ec2227a 5696 bs->stop = false;
7d4df6a4
DE
5697 return;
5698 }
60e1c644 5699
12ab52e9
PA
5700 /* If this is a thread/task-specific breakpoint, don't waste cpu
5701 evaluating the condition if this isn't the specified
5702 thread/task. */
00431a78 5703 if ((b->thread != -1 && b->thread != thread->global_num)
b080fe54 5704 || (b->inferior != -1 && b->inferior != thread->inf->num)
2ecee236 5705 || (b->task != -1 && b->task != ada_get_task_number (thread)))
6c1b0f7b 5706 {
d8a77e4c 5707 infrun_debug_printf ("incorrect thread or task, not stopping");
4ec2227a 5708 bs->stop = false;
6c1b0f7b
DE
5709 return;
5710 }
5711
6dddc817
DE
5712 /* Evaluate extension language breakpoints that have a "stop" method
5713 implemented. */
5714 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5715
7d4df6a4
DE
5716 if (is_watchpoint (b))
5717 {
bcafd1c1 5718 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
3a5c3e22 5719
4d01a485 5720 cond = w->cond_exp.get ();
7d4df6a4
DE
5721 }
5722 else
4d01a485 5723 cond = bl->cond.get ();
60e1c644 5724
80e0c6dc 5725 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
7d4df6a4 5726 {
d8de7963 5727 bool within_current_scope = true;
60e1c644 5728
65558ca5
TT
5729 /* We use scoped_value_mark because it could be a long time
5730 before we return to the command level and call
5731 free_all_values. We can't call free_all_values because we
5732 might be in the middle of evaluating a function call. */
5733 scoped_value_mark mark;
7d4df6a4 5734
bcafd1c1 5735 watchpoint *w = nullptr;
7d4df6a4 5736 if (is_watchpoint (b))
bcafd1c1 5737 w = gdb::checked_static_cast<watchpoint *> (b);
7d4df6a4
DE
5738
5739 /* Need to select the frame, with all that implies so that
5740 the conditions will have the right context. Because we
5741 use the frame, we will not see an inlined function's
5742 variables when we arrive at a breakpoint at the start
5743 of the inlined function; the current frame will be the
5744 call site. */
5745 if (w == NULL || w->cond_exp_valid_block == NULL)
5746 select_frame (get_current_frame ());
5747 else
18a18393 5748 {
bd2b40ac 5749 frame_info_ptr frame;
7d4df6a4
DE
5750
5751 /* For local watchpoint expressions, which particular
5752 instance of a local is being watched matters, so we
5753 keep track of the frame to evaluate the expression
5754 in. To evaluate the condition however, it doesn't
5755 really matter which instantiation of the function
5756 where the condition makes sense triggers the
5757 watchpoint. This allows an expression like "watch
5758 global if q > 10" set in `func', catch writes to
5759 global on all threads that call `func', or catch
5760 writes on all recursive calls of `func' by a single
5761 thread. We simply always evaluate the condition in
5762 the innermost frame that's executing where it makes
5763 sense to evaluate the condition. It seems
5764 intuitive. */
5765 frame = block_innermost_frame (w->cond_exp_valid_block);
5766 if (frame != NULL)
5767 select_frame (frame);
5768 else
d8de7963 5769 within_current_scope = false;
18a18393 5770 }
7d4df6a4 5771 if (within_current_scope)
bf469271 5772 {
a70b8144 5773 try
bf469271 5774 {
3df78436
AB
5775 scoped_restore reset_in_cond_eval
5776 = make_scoped_restore (&thread->control.in_cond_eval, true);
bf469271
PA
5777 condition_result = breakpoint_cond_eval (cond);
5778 }
b1ffd112 5779 catch (const gdb_exception_error &ex)
bf469271 5780 {
3f3ffaca
AB
5781 int locno = bpstat_locno (bs);
5782 if (locno != 0)
5783 exception_fprintf
5784 (gdb_stderr, ex,
5785 "Error in testing condition for breakpoint %d.%d:\n",
5786 b->number, locno);
5787 else
5788 exception_fprintf
5789 (gdb_stderr, ex,
5790 "Error in testing condition for breakpoint %d:\n",
5791 b->number);
bf469271 5792 }
bf469271 5793 }
7d4df6a4 5794 else
18a18393 5795 {
7d4df6a4
DE
5796 warning (_("Watchpoint condition cannot be tested "
5797 "in the current scope"));
5798 /* If we failed to set the right context for this
5799 watchpoint, unconditionally report it. */
18a18393 5800 }
7d4df6a4 5801 /* FIXME-someday, should give breakpoint #. */
18a18393 5802 }
7d4df6a4 5803
80e0c6dc 5804 if (cond != nullptr && !condition_result)
7d4df6a4 5805 {
d8a77e4c 5806 infrun_debug_printf ("condition_result = false, not stopping");
4ec2227a 5807 bs->stop = false;
d8a77e4c 5808 return;
7d4df6a4 5809 }
7d4df6a4
DE
5810 else if (b->ignore_count > 0)
5811 {
d8a77e4c
AB
5812 infrun_debug_printf ("ignore count %d, not stopping",
5813 b->ignore_count);
7d4df6a4 5814 b->ignore_count--;
4ec2227a 5815 bs->stop = false;
7d4df6a4
DE
5816 /* Increase the hit count even though we don't stop. */
5817 ++(b->hit_count);
19081eb5 5818 notify_breakpoint_modified (b);
d8a77e4c
AB
5819 return;
5820 }
5821
5822 if (bs->stop)
5823 infrun_debug_printf ("stopping at this breakpoint");
5824 else
5825 infrun_debug_printf ("not stopping at this breakpoint");
18a18393
VP
5826}
5827
1cf4d951
PA
5828/* Returns true if we need to track moribund locations of LOC's type
5829 on the current target. */
5830
d8de7963 5831static bool
b2ff9ed3 5832need_moribund_for_location_type (const struct bp_location *loc)
1cf4d951
PA
5833{
5834 return ((loc->loc_type == bp_loc_software_breakpoint
5835 && !target_supports_stopped_by_sw_breakpoint ())
5836 || (loc->loc_type == bp_loc_hardware_breakpoint
5837 && !target_supports_stopped_by_hw_breakpoint ()));
5838}
5839
ddfe970e 5840/* See breakpoint.h. */
c906108c 5841
313f3b21 5842bpstat *
ddfe970e 5843build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
c272a98c 5844 const target_waitstatus &ws)
c906108c 5845{
313f3b21 5846 bpstat *bs_head = nullptr, **bs_link = &bs_head;
c5aa993b 5847
a1decfc1 5848 for (breakpoint &b : all_breakpoints ())
429374b8 5849 {
a1decfc1 5850 if (!breakpoint_enabled (&b))
429374b8 5851 continue;
a5606eee 5852
a1decfc1 5853 for (bp_location &bl : b.locations ())
429374b8 5854 {
4a64f543
MS
5855 /* For hardware watchpoints, we look only at the first
5856 location. The watchpoint_check function will work on the
5857 entire expression, not the individual locations. For
5858 read watchpoints, the watchpoints_triggered function has
5859 checked all locations already. */
a1decfc1 5860 if (b.type == bp_hardware_watchpoint && &bl != &b.first_loc ())
429374b8 5861 break;
18a18393 5862
b00b30b2 5863 if (!bl.enabled || bl.disabled_by_cond || bl.shlib_disabled)
429374b8 5864 continue;
c5aa993b 5865
b00b30b2 5866 if (!bpstat_check_location (&bl, aspace, bp_addr, ws))
429374b8 5867 continue;
c5aa993b 5868
4a64f543
MS
5869 /* Come here if it's a watchpoint, or if the break address
5870 matches. */
c5aa993b 5871
b00b30b2 5872 bpstat *bs = new bpstat (&bl, &bs_link); /* Alloc a bpstat to
ddfe970e 5873 explain stop. */
c5aa993b 5874
f431efe5
PA
5875 /* Assume we stop. Should we find a watchpoint that is not
5876 actually triggered, or if the condition of the breakpoint
5877 evaluates as false, we'll reset 'stop' to 0. */
4ec2227a
TT
5878 bs->stop = true;
5879 bs->print = true;
d983da9c 5880
f431efe5
PA
5881 /* If this is a scope breakpoint, mark the associated
5882 watchpoint as triggered so that we will handle the
5883 out-of-scope event. We'll get to the watchpoint next
5884 iteration. */
a1decfc1 5885 if (b.type == bp_watchpoint_scope && b.related_breakpoint != &b)
3a5c3e22 5886 {
bcafd1c1
TT
5887 watchpoint *w
5888 = gdb::checked_static_cast<watchpoint *> (b.related_breakpoint);
3a5c3e22
PA
5889
5890 w->watchpoint_triggered = watch_triggered_yes;
5891 }
f431efe5
PA
5892 }
5893 }
5894
7c16b83e 5895 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5896 if (!target_supports_stopped_by_sw_breakpoint ()
5897 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5898 {
1123588c 5899 for (bp_location *loc : moribund_locations)
f431efe5 5900 {
1cf4d951
PA
5901 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5902 && need_moribund_for_location_type (loc))
5903 {
313f3b21 5904 bpstat *bs = new bpstat (loc, &bs_link);
1cf4d951 5905 /* For hits of moribund locations, we should just proceed. */
4ec2227a
TT
5906 bs->stop = false;
5907 bs->print = false;
1cf4d951
PA
5908 bs->print_it = print_it_noop;
5909 }
f431efe5
PA
5910 }
5911 }
5912
ddfe970e
KS
5913 return bs_head;
5914}
5915
5916/* See breakpoint.h. */
5917
313f3b21 5918bpstat *
ddfe970e 5919bpstat_stop_status (const address_space *aspace,
00431a78 5920 CORE_ADDR bp_addr, thread_info *thread,
c272a98c 5921 const target_waitstatus &ws,
313f3b21 5922 bpstat *stop_chain)
ddfe970e
KS
5923{
5924 struct breakpoint *b = NULL;
5925 /* First item of allocated bpstat's. */
313f3b21
SM
5926 bpstat *bs_head = stop_chain;
5927 bpstat *bs;
ddfe970e
KS
5928 int need_remove_insert;
5929 int removed_any;
5930
5931 /* First, build the bpstat chain with locations that explain a
5932 target stop, while being careful to not set the target running,
5933 as that may invalidate locations (in particular watchpoint
5934 locations are recreated). Resuming will happen here with
5935 breakpoint conditions or watchpoint expressions that include
5936 inferior function calls. */
5937 if (bs_head == NULL)
5938 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5939
edcc5120
TT
5940 /* A bit of special processing for shlib breakpoints. We need to
5941 process solib loading here, so that the lists of loaded and
5942 unloaded libraries are correct before we handle "catch load" and
5943 "catch unload". */
5944 for (bs = bs_head; bs != NULL; bs = bs->next)
5945 {
5d268276 5946 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5947 {
5948 handle_solib_event ();
5949 break;
5950 }
5951 }
5952
f431efe5
PA
5953 /* Now go through the locations that caused the target to stop, and
5954 check whether we're interested in reporting this stop to higher
5955 layers, or whether we should resume the target transparently. */
5956
5957 removed_any = 0;
5958
5760d0ab 5959 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5960 {
5961 if (!bs->stop)
5962 continue;
5963
f431efe5 5964 b = bs->breakpoint_at;
c47614fe 5965 b->check_status (bs);
348d480f 5966 if (bs->stop)
28010a5d 5967 {
00431a78 5968 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5969
429374b8
JK
5970 if (bs->stop)
5971 {
5972 ++(b->hit_count);
c906108c 5973
4a64f543 5974 /* We will stop here. */
429374b8
JK
5975 if (b->disposition == disp_disable)
5976 {
816338b5 5977 --(b->enable_count);
1a853c52 5978 if (b->enable_count <= 0)
429374b8 5979 b->enable_state = bp_disabled;
f431efe5 5980 removed_any = 1;
429374b8 5981 }
19081eb5 5982 notify_breakpoint_modified (b);
429374b8 5983 if (b->silent)
4ec2227a 5984 bs->print = false;
429374b8 5985 bs->commands = b->commands;
abf85f46 5986 if (command_line_is_silent (bs->commands
d1b0a7bf 5987 ? bs->commands.get () : NULL))
4ec2227a 5988 bs->print = false;
9d6e6e84 5989
c47614fe 5990 b->after_condition_true (bs);
429374b8
JK
5991 }
5992
348d480f 5993 }
a9b3a50f
PA
5994
5995 /* Print nothing for this entry if we don't stop or don't
5996 print. */
5997 if (!bs->stop || !bs->print)
5998 bs->print_it = print_it_noop;
429374b8 5999 }
876fa593 6000
d983da9c
DJ
6001 /* If we aren't stopping, the value of some hardware watchpoint may
6002 not have changed, but the intermediate memory locations we are
6003 watching may have. Don't bother if we're stopping; this will get
6004 done later. */
d832cb68 6005 need_remove_insert = 0;
5760d0ab
JK
6006 if (! bpstat_causes_stop (bs_head))
6007 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 6008 if (!bs->stop
f431efe5
PA
6009 && bs->breakpoint_at
6010 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 6011 {
bcafd1c1
TT
6012 watchpoint *w
6013 = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
3a5c3e22 6014
d8de7963 6015 update_watchpoint (w, false /* don't reparse. */);
d832cb68 6016 need_remove_insert = 1;
d983da9c
DJ
6017 }
6018
d832cb68 6019 if (need_remove_insert)
44702360 6020 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 6021 else if (removed_any)
44702360 6022 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 6023
5760d0ab 6024 return bs_head;
c906108c 6025}
628fe4e4 6026
d37e0847
PA
6027/* See breakpoint.h. */
6028
6029bpstat *
6030bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
6031 thread_info *thread, const target_waitstatus &ws)
6032{
6033 gdb_assert (!target_stopped_by_watchpoint ());
6034
6035 /* Clear all watchpoints' 'watchpoint_triggered' value from a
6036 previous stop to avoid confusing bpstat_stop_status. */
6037 watchpoints_triggered (ws);
6038
6039 return bpstat_stop_status (aspace, bp_addr, thread, ws);
6040}
6041
628fe4e4 6042static void
61c26be8 6043handle_jit_event (CORE_ADDR address)
628fe4e4 6044{
628fe4e4
JK
6045 struct gdbarch *gdbarch;
6046
1eb8556f 6047 infrun_debug_printf ("handling bp_jit_event");
243a9253 6048
628fe4e4
JK
6049 /* Switch terminal for any messages produced by
6050 breakpoint_re_set. */
223ffa71 6051 target_terminal::ours_for_output ();
628fe4e4 6052
61c26be8
MS
6053 gdbarch = get_frame_arch (get_current_frame ());
6054 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
6055 thus it is expected that its objectfile can be found through
6056 minimal symbol lookup. If it doesn't work (and assert fails), it
6057 most likely means that `jit_breakpoint_re_set` was changes and this
6058 function needs to be updated too. */
6059 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
6060 gdb_assert (jit_bp_sym.objfile != nullptr);
508ccf9b
TV
6061 objfile *objfile = jit_bp_sym.objfile;
6062 if (objfile->separate_debug_objfile_backlink)
6063 objfile = objfile->separate_debug_objfile_backlink;
6064 jit_event_handler (gdbarch, objfile);
628fe4e4 6065
223ffa71 6066 target_terminal::inferior ();
628fe4e4
JK
6067}
6068
6069/* Prepare WHAT final decision for infrun. */
6070
6071/* Decide what infrun needs to do with this bpstat. */
6072
c906108c 6073struct bpstat_what
313f3b21 6074bpstat_what (bpstat *bs_head)
c906108c 6075{
c906108c 6076 struct bpstat_what retval;
313f3b21 6077 bpstat *bs;
c906108c 6078
628fe4e4 6079 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 6080 retval.call_dummy = STOP_NONE;
e2d0f980 6081 retval.is_longjmp = false;
628fe4e4 6082
0e30163f 6083 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 6084 {
628fe4e4
JK
6085 /* Extract this BS's action. After processing each BS, we check
6086 if its action overrides all we've seem so far. */
6087 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
6088 enum bptype bptype;
6089
c906108c 6090 if (bs->breakpoint_at == NULL)
628fe4e4
JK
6091 {
6092 /* I suspect this can happen if it was a momentary
6093 breakpoint which has since been deleted. */
6094 bptype = bp_none;
6095 }
20874c92 6096 else
f431efe5 6097 bptype = bs->breakpoint_at->type;
628fe4e4
JK
6098
6099 switch (bptype)
c906108c
SS
6100 {
6101 case bp_none:
628fe4e4 6102 break;
c906108c
SS
6103 case bp_breakpoint:
6104 case bp_hardware_breakpoint:
7c16b83e 6105 case bp_single_step:
c906108c
SS
6106 case bp_until:
6107 case bp_finish:
a9b3a50f 6108 case bp_shlib_event:
c906108c
SS
6109 if (bs->stop)
6110 {
6111 if (bs->print)
628fe4e4 6112 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 6113 else
628fe4e4 6114 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
6115 }
6116 else
628fe4e4 6117 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
6118 break;
6119 case bp_watchpoint:
6120 case bp_hardware_watchpoint:
6121 case bp_read_watchpoint:
6122 case bp_access_watchpoint:
6123 if (bs->stop)
6124 {
6125 if (bs->print)
628fe4e4 6126 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 6127 else
628fe4e4 6128 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
6129 }
6130 else
628fe4e4
JK
6131 {
6132 /* There was a watchpoint, but we're not stopping.
6133 This requires no further action. */
6134 }
c906108c
SS
6135 break;
6136 case bp_longjmp:
e2e4d78b 6137 case bp_longjmp_call_dummy:
186c406b 6138 case bp_exception:
0a39bb32
PA
6139 if (bs->stop)
6140 {
6141 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
6142 retval.is_longjmp = bptype != bp_exception;
6143 }
6144 else
6145 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
6146 break;
6147 case bp_longjmp_resume:
186c406b 6148 case bp_exception_resume:
0a39bb32
PA
6149 if (bs->stop)
6150 {
6151 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
6152 retval.is_longjmp = bptype == bp_longjmp_resume;
6153 }
6154 else
6155 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
6156 break;
6157 case bp_step_resume:
6158 if (bs->stop)
628fe4e4
JK
6159 this_action = BPSTAT_WHAT_STEP_RESUME;
6160 else
c906108c 6161 {
628fe4e4
JK
6162 /* It is for the wrong frame. */
6163 this_action = BPSTAT_WHAT_SINGLE;
c906108c 6164 }
c906108c 6165 break;
2c03e5be
PA
6166 case bp_hp_step_resume:
6167 if (bs->stop)
6168 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
6169 else
6170 {
6171 /* It is for the wrong frame. */
6172 this_action = BPSTAT_WHAT_SINGLE;
6173 }
6174 break;
c906108c 6175 case bp_watchpoint_scope:
c4093a6a 6176 case bp_thread_event:
1900040c 6177 case bp_overlay_event:
0fd8e87f 6178 case bp_longjmp_master:
aa7d318d 6179 case bp_std_terminate_master:
186c406b 6180 case bp_exception_master:
628fe4e4 6181 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 6182 break;
ce78b96d 6183 case bp_catchpoint:
c5aa993b
JM
6184 if (bs->stop)
6185 {
6186 if (bs->print)
628fe4e4 6187 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 6188 else
628fe4e4 6189 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
6190 }
6191 else
628fe4e4 6192 {
cb1e4e32
PA
6193 /* Some catchpoints are implemented with breakpoints.
6194 For those, we need to step over the breakpoint. */
6e14e441
PA
6195 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
6196 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
cb1e4e32 6197 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
6198 }
6199 break;
628fe4e4 6200 case bp_jit_event:
628fe4e4 6201 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 6202 break;
c906108c 6203 case bp_call_dummy:
53a5351d
JM
6204 /* Make sure the action is stop (silent or noisy),
6205 so infrun.c pops the dummy frame. */
aa7d318d 6206 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 6207 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
6208 break;
6209 case bp_std_terminate:
6210 /* Make sure the action is stop (silent or noisy),
6211 so infrun.c pops the dummy frame. */
aa7d318d 6212 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 6213 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 6214 break;
1042e4c0 6215 case bp_tracepoint:
7a697b8d 6216 case bp_fast_tracepoint:
0fb4aa4b 6217 case bp_static_tracepoint:
7b572efb 6218 case bp_static_marker_tracepoint:
1042e4c0
SS
6219 /* Tracepoint hits should not be reported back to GDB, and
6220 if one got through somehow, it should have been filtered
6221 out already. */
f34652de 6222 internal_error (_("bpstat_what: tracepoint encountered"));
0e30163f
JK
6223 break;
6224 case bp_gnu_ifunc_resolver:
6225 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
6226 this_action = BPSTAT_WHAT_SINGLE;
6227 break;
6228 case bp_gnu_ifunc_resolver_return:
6229 /* The breakpoint will be removed, execution will restart from the
6230 PC of the former breakpoint. */
6231 this_action = BPSTAT_WHAT_KEEP_CHECKING;
6232 break;
e7e0cddf
SS
6233
6234 case bp_dprintf:
a11cfd87
HZ
6235 if (bs->stop)
6236 this_action = BPSTAT_WHAT_STOP_SILENT;
6237 else
6238 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
6239 break;
6240
628fe4e4 6241 default:
f34652de 6242 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 6243 }
628fe4e4 6244
325fac50 6245 retval.main_action = std::max (retval.main_action, this_action);
c906108c 6246 }
628fe4e4 6247
243a9253
PA
6248 return retval;
6249}
628fe4e4 6250
243a9253 6251void
313f3b21 6252bpstat_run_callbacks (bpstat *bs_head)
243a9253 6253{
313f3b21 6254 bpstat *bs;
628fe4e4 6255
0e30163f
JK
6256 for (bs = bs_head; bs != NULL; bs = bs->next)
6257 {
6258 struct breakpoint *b = bs->breakpoint_at;
6259
6260 if (b == NULL)
6261 continue;
6262 switch (b->type)
6263 {
243a9253 6264 case bp_jit_event:
61c26be8 6265 handle_jit_event (bs->bp_location_at->address);
243a9253 6266 break;
0e30163f 6267 case bp_gnu_ifunc_resolver:
6a9196a6
TT
6268 gnu_ifunc_resolver_stop
6269 (gdb::checked_static_cast<code_breakpoint *> (b));
0e30163f
JK
6270 break;
6271 case bp_gnu_ifunc_resolver_return:
6a9196a6
TT
6272 gnu_ifunc_resolver_return_stop
6273 (gdb::checked_static_cast<code_breakpoint *> (b));
0e30163f
JK
6274 break;
6275 }
6276 }
c906108c
SS
6277}
6278
4c462cb0 6279/* See breakpoint.h. */
c906108c 6280
4c462cb0
SM
6281bool
6282bpstat_should_step ()
c906108c 6283{
a1decfc1
SM
6284 for (breakpoint &b : all_breakpoints ())
6285 if (breakpoint_enabled (&b)
6286 && b.type == bp_watchpoint
6287 && b.has_locations ())
4c462cb0 6288 return true;
43892fdf 6289
4c462cb0 6290 return false;
c906108c
SS
6291}
6292
4c462cb0
SM
6293/* See breakpoint.h. */
6294
6295bool
313f3b21 6296bpstat_causes_stop (bpstat *bs)
67822962
PA
6297{
6298 for (; bs != NULL; bs = bs->next)
6299 if (bs->stop)
4c462cb0 6300 return true;
67822962 6301
4c462cb0 6302 return false;
67822962
PA
6303}
6304
c906108c 6305\f
c5aa993b 6306
6c92c339 6307/* Compute a number of spaces suitable to indent the next line
170b53b2
UW
6308 so it starts at the position corresponding to the table column
6309 named COL_NAME in the currently active table of UIOUT. */
6310
6c92c339 6311static int
170b53b2
UW
6312wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6313{
170b53b2 6314 int i, total_width, width, align;
c5209615 6315 const char *text;
170b53b2
UW
6316
6317 total_width = 0;
112e8700 6318 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6319 {
6320 if (strcmp (text, col_name) == 0)
6c92c339 6321 return total_width;
170b53b2
UW
6322
6323 total_width += width + 1;
6324 }
6325
6c92c339 6326 return 0;
170b53b2
UW
6327}
6328
b775012e
LM
6329/* Determine if the locations of this breakpoint will have their conditions
6330 evaluated by the target, host or a mix of both. Returns the following:
6331
6332 "host": Host evals condition.
6333 "host or target": Host or Target evals condition.
6334 "target": Target evals condition.
6335*/
6336
6337static const char *
a67bcaba 6338bp_condition_evaluator (const breakpoint *b)
b775012e 6339{
b775012e
LM
6340 char host_evals = 0;
6341 char target_evals = 0;
6342
6343 if (!b)
6344 return NULL;
6345
6346 if (!is_breakpoint (b))
6347 return NULL;
6348
6349 if (gdb_evaluates_breakpoint_condition_p ()
6350 || !target_supports_evaluation_of_breakpoint_conditions ())
6351 return condition_evaluation_host;
6352
b00b30b2 6353 for (bp_location &bl : b->locations ())
b775012e 6354 {
b00b30b2 6355 if (bl.cond_bytecode)
b775012e
LM
6356 target_evals++;
6357 else
6358 host_evals++;
6359 }
6360
6361 if (host_evals && target_evals)
6362 return condition_evaluation_both;
6363 else if (target_evals)
6364 return condition_evaluation_target;
6365 else
6366 return condition_evaluation_host;
6367}
6368
6369/* Determine the breakpoint location's condition evaluator. This is
6370 similar to bp_condition_evaluator, but for locations. */
6371
6372static const char *
b2ff9ed3 6373bp_location_condition_evaluator (const struct bp_location *bl)
b775012e
LM
6374{
6375 if (bl && !is_breakpoint (bl->owner))
6376 return NULL;
6377
6378 if (gdb_evaluates_breakpoint_condition_p ()
6379 || !target_supports_evaluation_of_breakpoint_conditions ())
6380 return condition_evaluation_host;
6381
6382 if (bl && bl->cond_bytecode)
6383 return condition_evaluation_target;
6384 else
6385 return condition_evaluation_host;
6386}
6387
859825b8
JK
6388/* Print the LOC location out of the list of B->LOC locations. */
6389
170b53b2 6390static void
f5951b9f 6391print_breakpoint_location (const breakpoint *b, const bp_location *loc)
0d381245 6392{
79a45e25 6393 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6394
6395 scoped_restore_current_program_space restore_pspace;
6c95b8df 6396
859825b8
JK
6397 if (loc != NULL && loc->shlib_disabled)
6398 loc = NULL;
6399
6c95b8df
PA
6400 if (loc != NULL)
6401 set_current_program_space (loc->pspace);
6402
56435ebe 6403 if (b->display_canonical)
709438c7 6404 uiout->field_string ("what", b->locspec->to_string ());
2f202fde 6405 else if (loc && loc->symtab)
0d381245 6406 {
4a27f119
KS
6407 const struct symbol *sym = loc->symbol;
6408
0d381245
VP
6409 if (sym)
6410 {
112e8700 6411 uiout->text ("in ");
987012b8 6412 uiout->field_string ("func", sym->print_name (),
e43b10e1 6413 function_name_style.style ());
112e8700
SM
6414 uiout->text (" ");
6415 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6416 uiout->text ("at ");
0d381245 6417 }
112e8700 6418 uiout->field_string ("file",
cbe56571 6419 symtab_to_filename_for_display (loc->symtab),
e43b10e1 6420 file_name_style.style ());
112e8700 6421 uiout->text (":");
05cba821 6422
112e8700
SM
6423 if (uiout->is_mi_like_p ())
6424 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6425
887ae0cf
TT
6426 uiout->field_signed ("line", loc->line_number,
6427 line_number_style.style ());
0d381245 6428 }
859825b8 6429 else if (loc)
0d381245 6430 {
d7e74731 6431 string_file stb;
170b53b2 6432
d7e74731 6433 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6434 demangle, "");
112e8700 6435 uiout->field_stream ("at", stb);
0d381245 6436 }
859825b8 6437 else
5770f680
AB
6438 {
6439 /* Internal breakpoints don't have a locspec string, but can become
6440 pending if the shared library the breakpoint is in is unloaded.
6441 For most internal breakpoint types though, after unloading the
6442 shared library, the breakpoint will be deleted and never recreated
6443 (see internal_breakpoint::re_set). But for two internal
6444 breakpoint types bp_shlib_event and bp_thread_event this is not
6445 true. Usually we don't expect the libraries that contain these
6446 breakpoints to ever be unloaded, but a buggy inferior might do
6447 such a thing, in which case GDB should be prepared to handle this
6448 case.
6449
6450 If these two breakpoint types become pending then there will be no
6451 locspec string. */
6452 gdb_assert (b->locspec != nullptr
6453 || (!user_breakpoint_p (b)
6454 && (b->type == bp_shlib_event
6455 || b->type == bp_thread_event)));
6456 const char *locspec_str
6457 = (b->locspec != nullptr ? b->locspec->to_string () : "");
6458 uiout->field_string ("pending", locspec_str);
6459 }
6c95b8df 6460
b775012e
LM
6461 if (loc && is_breakpoint (b)
6462 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6463 && bp_condition_evaluator (b) == condition_evaluation_both)
6464 {
112e8700
SM
6465 uiout->text (" (");
6466 uiout->field_string ("evaluated-by",
b775012e 6467 bp_location_condition_evaluator (loc));
112e8700 6468 uiout->text (")");
b775012e 6469 }
0d381245
VP
6470}
6471
269b11a2
PA
6472static const char *
6473bptype_string (enum bptype type)
c906108c 6474{
c4093a6a
JM
6475 struct ep_type_description
6476 {
6477 enum bptype type;
a121b7c1 6478 const char *description;
c4093a6a
JM
6479 };
6480 static struct ep_type_description bptypes[] =
c906108c 6481 {
c5aa993b
JM
6482 {bp_none, "?deleted?"},
6483 {bp_breakpoint, "breakpoint"},
c906108c 6484 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6485 {bp_single_step, "sw single-step"},
c5aa993b
JM
6486 {bp_until, "until"},
6487 {bp_finish, "finish"},
6488 {bp_watchpoint, "watchpoint"},
c906108c 6489 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6490 {bp_read_watchpoint, "read watchpoint"},
6491 {bp_access_watchpoint, "acc watchpoint"},
6492 {bp_longjmp, "longjmp"},
6493 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6494 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6495 {bp_exception, "exception"},
6496 {bp_exception_resume, "exception resume"},
c5aa993b 6497 {bp_step_resume, "step resume"},
2c03e5be 6498 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6499 {bp_watchpoint_scope, "watchpoint scope"},
6500 {bp_call_dummy, "call dummy"},
aa7d318d 6501 {bp_std_terminate, "std::terminate"},
c5aa993b 6502 {bp_shlib_event, "shlib events"},
c4093a6a 6503 {bp_thread_event, "thread events"},
1900040c 6504 {bp_overlay_event, "overlay events"},
0fd8e87f 6505 {bp_longjmp_master, "longjmp master"},
aa7d318d 6506 {bp_std_terminate_master, "std::terminate master"},
186c406b 6507 {bp_exception_master, "exception master"},
ce78b96d 6508 {bp_catchpoint, "catchpoint"},
1042e4c0 6509 {bp_tracepoint, "tracepoint"},
7a697b8d 6510 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6511 {bp_static_tracepoint, "static tracepoint"},
7b572efb 6512 {bp_static_marker_tracepoint, "static marker tracepoint"},
e7e0cddf 6513 {bp_dprintf, "dprintf"},
4efc6507 6514 {bp_jit_event, "jit events"},
0e30163f
JK
6515 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6516 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6517 };
269b11a2
PA
6518
6519 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6520 || ((int) type != bptypes[(int) type].type))
f34652de 6521 internal_error (_("bptypes table does not describe type #%d."),
269b11a2
PA
6522 (int) type);
6523
6524 return bptypes[(int) type].description;
6525}
6526
998580f1
MK
6527/* For MI, output a field named 'thread-groups' with a list as the value.
6528 For CLI, prefix the list with the string 'inf'. */
6529
6530static void
6531output_thread_groups (struct ui_out *uiout,
6532 const char *field_name,
5c632425 6533 const std::vector<int> &inf_nums,
998580f1
MK
6534 int mi_only)
6535{
112e8700 6536 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6537
6538 /* For backward compatibility, don't display inferiors in CLI unless
6539 there are several. Always display them for MI. */
6540 if (!is_mi && mi_only)
6541 return;
6542
10f489e5 6543 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6544
5c632425 6545 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6546 {
6547 if (is_mi)
6548 {
6549 char mi_group[10];
6550
5c632425 6551 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6552 uiout->field_string (NULL, mi_group);
998580f1
MK
6553 }
6554 else
6555 {
6556 if (i == 0)
112e8700 6557 uiout->text (" inf ");
998580f1 6558 else
112e8700 6559 uiout->text (", ");
998580f1 6560
5c632425 6561 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6562 }
6563 }
998580f1
MK
6564}
6565
9db0d853
SM
6566/* See breakpoint.h. */
6567
6568bool fix_breakpoint_script_output_globally = false;
6569
a38118e5
PA
6570/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6571 instead of going via breakpoint_ops::print_one. This makes "maint
6572 info breakpoints" show the software breakpoint locations of
6573 catchpoints, which are considered internal implementation
c01e038b
TT
6574 detail. Returns true if RAW_LOC is false and if the breakpoint's
6575 print_one method did something; false otherwise. */
269b11a2 6576
c01e038b 6577static bool
269b11a2
PA
6578print_one_breakpoint_location (struct breakpoint *b,
6579 struct bp_location *loc,
6580 int loc_number,
5e632eca 6581 const bp_location **last_loc,
a38118e5 6582 int allflag, bool raw_loc)
269b11a2
PA
6583{
6584 struct command_line *l;
c2c6d25f 6585 static char bpenables[] = "nynny";
c906108c 6586
79a45e25 6587 struct ui_out *uiout = current_uiout;
d8de7963
AB
6588 bool header_of_multiple = false;
6589 bool part_of_multiple = (loc != NULL);
79a45b7d
TT
6590 struct value_print_options opts;
6591
6592 get_user_print_options (&opts);
0d381245
VP
6593
6594 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6595 /* See comment in print_one_breakpoint concerning treatment of
6596 breakpoints with single disabled location. */
0d381245 6597 if (loc == NULL
9dc1523b
SM
6598 && (b->has_locations ()
6599 && (b->has_multiple_locations ()
f5951b9f 6600 || !b->first_loc ().enabled || b->first_loc ().disabled_by_cond)))
d8de7963 6601 header_of_multiple = true;
9dc1523b 6602
f5951b9f
SM
6603 if (loc == NULL && b->has_locations ())
6604 loc = &b->first_loc ();
0d381245 6605
c4093a6a
JM
6606 annotate_record ();
6607
6608 /* 1 */
6609 annotate_field (0);
0d381245 6610 if (part_of_multiple)
528e1572 6611 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6612 else
381befee 6613 uiout->field_signed ("number", b->number);
c4093a6a
JM
6614
6615 /* 2 */
6616 annotate_field (1);
0d381245 6617 if (part_of_multiple)
112e8700 6618 uiout->field_skip ("type");
269b11a2 6619 else
112e8700 6620 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6621
6622 /* 3 */
6623 annotate_field (2);
0d381245 6624 if (part_of_multiple)
112e8700 6625 uiout->field_skip ("disp");
0d381245 6626 else
112e8700 6627 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6628
c4093a6a
JM
6629 /* 4 */
6630 annotate_field (3);
0d381245 6631 if (part_of_multiple)
fbcda577
PA
6632 {
6633 /* For locations that are disabled because of an invalid
6634 condition, display "N*" on the CLI, where "*" refers to a
6635 footnote below the table. For MI, simply display a "N"
6636 without a footnote. On the CLI, for enabled locations whose
6637 breakpoint is disabled, display "y-". */
6638 auto get_enable_state = [uiout, loc] () -> const char *
287de656 6639 {
fbcda577
PA
6640 if (uiout->is_mi_like_p ())
6641 {
6642 if (loc->disabled_by_cond)
6643 return "N";
6644 else if (!loc->enabled)
6645 return "n";
6646 else
6647 return "y";
6648 }
6649 else
6650 {
6651 if (loc->disabled_by_cond)
6652 return "N*";
6653 else if (!loc->enabled)
6654 return "n";
6655 else if (!breakpoint_enabled (loc->owner))
6656 return "y-";
6657 else
6658 return "y";
6659 }
6660 };
6661 uiout->field_string ("enabled", get_enable_state ());
6662 }
0d381245 6663 else
112e8700 6664 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6665
c4093a6a 6666 /* 5 and 6 */
c01e038b 6667 bool result = false;
c47614fe 6668 if (!raw_loc && b->print_one (last_loc))
c01e038b 6669 result = true;
3086aeae 6670 else
a38118e5
PA
6671 {
6672 if (is_watchpoint (b))
3a5c3e22 6673 {
bcafd1c1 6674 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
3a5c3e22
PA
6675
6676 /* Field 4, the address, is omitted (which makes the columns
6677 not line up too nicely with the headers, but the effect
6678 is relatively readable). */
6679 if (opts.addressprint)
112e8700 6680 uiout->field_skip ("addr");
3a5c3e22 6681 annotate_field (5);
a4c50be3 6682 uiout->field_string ("what", w->exp_string.get ());
3a5c3e22 6683 }
f06f1252
TT
6684 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6685 || is_ada_exception_catchpoint (b))
a38118e5
PA
6686 {
6687 if (opts.addressprint)
6688 {
6689 annotate_field (4);
6690 if (header_of_multiple)
7f6aba03
TT
6691 uiout->field_string ("addr", "<MULTIPLE>",
6692 metadata_style.style ());
9dc1523b 6693 else if (!b->has_locations () || loc->shlib_disabled)
7f6aba03
TT
6694 uiout->field_string ("addr", "<PENDING>",
6695 metadata_style.style ());
a38118e5
PA
6696 else
6697 uiout->field_core_addr ("addr",
6698 loc->gdbarch, loc->address);
6699 }
6700 annotate_field (5);
6701 if (!header_of_multiple)
6702 print_breakpoint_location (b, loc);
9dc1523b 6703 if (b->has_locations ())
f5951b9f 6704 *last_loc = &b->first_loc ();
a38118e5
PA
6705 }
6706 }
6c95b8df 6707
7934cb13 6708 if (loc != nullptr && !header_of_multiple && !loc->shlib_disabled)
6c95b8df 6709 {
5c632425 6710 std::vector<int> inf_nums;
998580f1 6711 int mi_only = 1;
6c95b8df 6712
08036331 6713 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6714 {
6715 if (inf->pspace == loc->pspace)
5c632425 6716 inf_nums.push_back (inf->num);
6c95b8df 6717 }
998580f1 6718
7594741e
AB
6719 /* For backward compatibility, don't display inferiors in CLI unless
6720 there are several. Always display for MI. */
6721 if (allflag
99d9c3b9 6722 || (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7594741e
AB
6723 && (program_spaces.size () > 1
6724 || number_of_inferiors () > 1)
6725 /* LOC is for existing B, it cannot be in
6726 moribund_locations and thus having NULL OWNER. */
6727 && loc->owner->type != bp_catchpoint))
998580f1 6728 mi_only = 0;
5c632425 6729 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6730 }
6731
944b1b18
AB
6732 /* In the MI output, each location of a thread or task specific
6733 breakpoint includes the relevant thread or task ID. This is done for
6734 backwards compatibility reasons.
6735
6736 For the CLI output, the thread/task information is printed on a
6737 separate line, see the 'stop only in thread' and 'stop only in task'
6738 output below. */
2fd9a436 6739 if (part_of_multiple && uiout->is_mi_like_p ())
c4093a6a 6740 {
4a306c9a 6741 if (b->thread != -1)
944b1b18 6742 uiout->field_signed ("thread", b->thread);
2ecee236 6743 else if (b->task != -1)
944b1b18 6744 uiout->field_signed ("task", b->task);
b080fe54
AB
6745 else if (b->inferior != -1)
6746 uiout->field_signed ("inferior", b->inferior);
c4093a6a 6747 }
f1310107 6748
112e8700 6749 uiout->text ("\n");
f1310107 6750
348d480f 6751 if (!part_of_multiple)
c47614fe 6752 b->print_one_detail (uiout);
f1310107 6753
0d381245 6754 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6755 {
6756 annotate_field (6);
112e8700 6757 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6758 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6759 the frame ID. */
112e8700 6760 uiout->field_core_addr ("frame",
5af949e3 6761 b->gdbarch, b->frame_id.stack_addr);
112e8700 6762 uiout->text ("\n");
c4093a6a
JM
6763 }
6764
28010a5d 6765 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6766 {
6767 annotate_field (7);
d77f58be 6768 if (is_tracepoint (b))
112e8700 6769 uiout->text ("\ttrace only if ");
1042e4c0 6770 else
112e8700 6771 uiout->text ("\tstop only if ");
6f781ee3 6772 uiout->field_string ("cond", b->cond_string.get ());
b775012e
LM
6773
6774 /* Print whether the target is doing the breakpoint's condition
6775 evaluation. If GDB is doing the evaluation, don't print anything. */
6776 if (is_breakpoint (b)
6777 && breakpoint_condition_evaluation_mode ()
6778 == condition_evaluation_target)
6779 {
6a831f06
PA
6780 uiout->message (" (%pF evals)",
6781 string_field ("evaluated-by",
6782 bp_condition_evaluator (b)));
b775012e 6783 }
112e8700 6784 uiout->text ("\n");
0101ce28
JJ
6785 }
6786
0d381245 6787 if (!part_of_multiple && b->thread != -1)
c4093a6a 6788 {
4a64f543 6789 /* FIXME should make an annotation for this. */
112e8700
SM
6790 uiout->text ("\tstop only in thread ");
6791 if (uiout->is_mi_like_p ())
381befee 6792 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6793 else
6794 {
6795 struct thread_info *thr = find_thread_global_id (b->thread);
6796
112e8700 6797 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6798 }
112e8700 6799 uiout->text ("\n");
c4093a6a 6800 }
944b1b18 6801
2ecee236 6802 if (!part_of_multiple && b->task != -1)
944b1b18
AB
6803 {
6804 uiout->text ("\tstop only in task ");
6805 uiout->field_signed ("task", b->task);
6806 uiout->text ("\n");
6807 }
6808
b080fe54
AB
6809 if (!part_of_multiple && b->inferior != -1)
6810 {
6811 uiout->text ("\tstop only in inferior ");
6812 uiout->field_signed ("inferior", b->inferior);
6813 uiout->text ("\n");
6814 }
6815
556ec64d
YQ
6816 if (!part_of_multiple)
6817 {
6818 if (b->hit_count)
31f56a27
YQ
6819 {
6820 /* FIXME should make an annotation for this. */
6821 if (is_catchpoint (b))
112e8700 6822 uiout->text ("\tcatchpoint");
31f56a27 6823 else if (is_tracepoint (b))
112e8700 6824 uiout->text ("\ttracepoint");
31f56a27 6825 else
112e8700
SM
6826 uiout->text ("\tbreakpoint");
6827 uiout->text (" already hit ");
381befee 6828 uiout->field_signed ("times", b->hit_count);
31f56a27 6829 if (b->hit_count == 1)
112e8700 6830 uiout->text (" time\n");
31f56a27 6831 else
112e8700 6832 uiout->text (" times\n");
31f56a27 6833 }
556ec64d
YQ
6834 else
6835 {
31f56a27 6836 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6837 if (uiout->is_mi_like_p ())
381befee 6838 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6839 }
6840 }
8b93c638 6841
0d381245 6842 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6843 {
6844 annotate_field (8);
6a831f06
PA
6845 uiout->message ("\tignore next %pF hits\n",
6846 signed_field ("ignore", b->ignore_count));
c4093a6a 6847 }
059fb39f 6848
816338b5
SS
6849 /* Note that an enable count of 1 corresponds to "enable once"
6850 behavior, which is reported by the combination of enablement and
6851 disposition, so we don't need to mention it here. */
6852 if (!part_of_multiple && b->enable_count > 1)
6853 {
6854 annotate_field (8);
112e8700 6855 uiout->text ("\tdisable after ");
816338b5
SS
6856 /* Tweak the wording to clarify that ignore and enable counts
6857 are distinct, and have additive effect. */
6858 if (b->ignore_count)
112e8700 6859 uiout->text ("additional ");
816338b5 6860 else
112e8700 6861 uiout->text ("next ");
381befee 6862 uiout->field_signed ("enable", b->enable_count);
112e8700 6863 uiout->text (" hits\n");
816338b5
SS
6864 }
6865
f196051f
SS
6866 if (!part_of_multiple && is_tracepoint (b))
6867 {
01bccc56 6868 tracepoint *tp = gdb::checked_static_cast<tracepoint *> (b);
f196051f
SS
6869
6870 if (tp->traceframe_usage)
6871 {
112e8700 6872 uiout->text ("\ttrace buffer usage ");
381befee 6873 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6874 uiout->text (" bytes\n");
f196051f
SS
6875 }
6876 }
d3ce09f5 6877
d1b0a7bf 6878 l = b->commands ? b->commands.get () : NULL;
059fb39f 6879 if (!part_of_multiple && l)
c4093a6a
JM
6880 {
6881 annotate_field (9);
9db0d853
SM
6882
6883 bool use_fixed_output =
6884 (uiout->test_flags (fix_breakpoint_script_output)
287de656 6885 || fix_breakpoint_script_output_globally);
9db0d853 6886
6b09f134
LS
6887 std::optional<ui_out_emit_tuple> tuple_emitter;
6888 std::optional<ui_out_emit_list> list_emitter;
9db0d853
SM
6889
6890 if (use_fixed_output)
6891 list_emitter.emplace (uiout, "script");
6892 else
6893 tuple_emitter.emplace (uiout, "script");
6894
8b93c638 6895 print_command_lines (uiout, l, 4);
c4093a6a 6896 }
d24317b4 6897
d9b3f62e 6898 if (is_tracepoint (b))
1042e4c0 6899 {
01bccc56 6900 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
d9b3f62e
PA
6901
6902 if (!part_of_multiple && t->pass_count)
6903 {
6904 annotate_field (10);
112e8700 6905 uiout->text ("\tpass count ");
381befee 6906 uiout->field_signed ("pass", t->pass_count);
112e8700 6907 uiout->text (" \n");
d9b3f62e 6908 }
f2a8bc8a
YQ
6909
6910 /* Don't display it when tracepoint or tracepoint location is
6911 pending. */
6912 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6913 {
6914 annotate_field (11);
6915
112e8700
SM
6916 if (uiout->is_mi_like_p ())
6917 uiout->field_string ("installed",
f2a8bc8a
YQ
6918 loc->inserted ? "y" : "n");
6919 else
6920 {
6921 if (loc->inserted)
112e8700 6922 uiout->text ("\t");
f2a8bc8a 6923 else
112e8700
SM
6924 uiout->text ("\tnot ");
6925 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6926 }
6927 }
1042e4c0
SS
6928 }
6929
112e8700 6930 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6931 {
3a5c3e22
PA
6932 if (is_watchpoint (b))
6933 {
bcafd1c1 6934 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
3a5c3e22 6935
a4c50be3 6936 uiout->field_string ("original-location", w->exp_string.get ());
3a5c3e22 6937 }
264f9890
PA
6938 else if (b->locspec != nullptr)
6939 {
709438c7 6940 const char *str = b->locspec->to_string ();
264f9890
PA
6941 if (str != nullptr)
6942 uiout->field_string ("original-location", str);
6943 }
d24317b4 6944 }
c01e038b
TT
6945
6946 return result;
c4093a6a 6947}
c5aa993b 6948
13674803
SM
6949/* See breakpoint.h. */
6950
6951bool fix_multi_location_breakpoint_output_globally = false;
6952
0d381245 6953static void
5e632eca 6954print_one_breakpoint (breakpoint *b, const bp_location **last_loc, int allflag)
0d381245 6955{
79a45e25 6956 struct ui_out *uiout = current_uiout;
13674803
SM
6957 bool use_fixed_output
6958 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6959 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6960
6b09f134
LS
6961 std::optional<ui_out_emit_tuple> bkpt_tuple_emitter (std::in_place, uiout,
6962 "bkpt");
c01e038b
TT
6963 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6964 allflag, false);
8d3788bd 6965
b4be1b06
SM
6966 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6967 are outside. */
6968 if (!use_fixed_output)
6969 bkpt_tuple_emitter.reset ();
0d381245
VP
6970
6971 /* If this breakpoint has custom print function,
6972 it's already printed. Otherwise, print individual
6973 locations, if any. */
c01e038b 6974 if (!printed || allflag)
0d381245 6975 {
4a64f543
MS
6976 /* If breakpoint has a single location that is disabled, we
6977 print it as if it had several locations, since otherwise it's
6978 hard to represent "breakpoint enabled, location disabled"
6979 situation.
6980
6981 Note that while hardware watchpoints have several locations
a38118e5
PA
6982 internally, that's not a property exposed to users.
6983
6984 Likewise, while catchpoints may be implemented with
6985 breakpoints (e.g., catch throw), that's not a property
6986 exposed to users. We do however display the internal
6987 breakpoint locations with "maint info breakpoints". */
6988 if (!is_hardware_watchpoint (b)
f06f1252
TT
6989 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6990 || is_ada_exception_catchpoint (b))
a38118e5 6991 && (allflag
9dc1523b
SM
6992 || (b->has_locations ()
6993 && (b->has_multiple_locations ()
f5951b9f
SM
6994 || !b->first_loc ().enabled
6995 || b->first_loc ().disabled_by_cond))))
0d381245 6996 {
6b09f134 6997 std::optional<ui_out_emit_list> locations_list;
b4be1b06
SM
6998
6999 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
7000 MI record. For later versions, place breakpoint locations in a
7001 list. */
7002 if (uiout->is_mi_like_p () && use_fixed_output)
7003 locations_list.emplace (uiout, "locations");
8d3788bd 7004
b4be1b06 7005 int n = 1;
b00b30b2 7006 for (bp_location &loc : b->locations ())
8d3788bd 7007 {
b4be1b06 7008 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
b00b30b2 7009 print_one_breakpoint_location (b, &loc, n, last_loc,
a38118e5 7010 allflag, allflag);
40cb8ca5 7011 n++;
8d3788bd 7012 }
0d381245
VP
7013 }
7014 }
7015}
7016
a6d9a66e
UW
7017static int
7018breakpoint_address_bits (struct breakpoint *b)
7019{
7020 int print_address_bits = 0;
a6d9a66e 7021
b00b30b2 7022 for (bp_location &loc : b->locations ())
a6d9a66e 7023 {
b00b30b2 7024 if (!bl_address_is_meaningful (&loc))
6e14e441 7025 continue;
c7437ca6 7026
b00b30b2 7027 int addr_bit = gdbarch_addr_bit (loc.gdbarch);
a6d9a66e
UW
7028 if (addr_bit > print_address_bits)
7029 print_address_bits = addr_bit;
7030 }
7031
7032 return print_address_bits;
7033}
0d381245 7034
65630365 7035/* See breakpoint.h. */
c5aa993b 7036
65630365
PA
7037void
7038print_breakpoint (breakpoint *b)
c4093a6a 7039{
5e632eca 7040 const bp_location *dummy_loc = nullptr;
65630365 7041 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 7042}
c5aa993b 7043
09d682a4
TT
7044/* Return true if this breakpoint was set by the user, false if it is
7045 internal or momentary. */
7046
7047int
ae2a9665 7048user_breakpoint_p (const breakpoint *b)
09d682a4 7049{
46c6471b 7050 return b->number > 0;
09d682a4
TT
7051}
7052
93daf339
TT
7053/* See breakpoint.h. */
7054
7055int
ae2a9665 7056pending_breakpoint_p (const breakpoint *b)
93daf339 7057{
9dc1523b 7058 return !b->has_locations ();
93daf339
TT
7059}
7060
5c458ae8
SM
7061/* Print information on breakpoints (including watchpoints and tracepoints).
7062
7063 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
7064 understood by number_or_range_parser. Only breakpoints included in this
7065 list are then printed.
7066
7067 If SHOW_INTERNAL is true, print internal breakpoints.
7068
7069 If FILTER is non-NULL, call it on each breakpoint and only include the
7070 ones for which it returns true.
7071
7072 Return the total number of breakpoints listed. */
c906108c 7073
d77f58be 7074static int
5c458ae8 7075breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 7076 bool (*filter) (const struct breakpoint *))
c4093a6a 7077{
5e632eca 7078 const bp_location *last_loc = nullptr;
7f3b0473 7079 int nr_printable_breakpoints;
79a45b7d 7080 struct value_print_options opts;
a6d9a66e 7081 int print_address_bits = 0;
269b11a2 7082 int print_type_col_width = 14;
79a45e25 7083 struct ui_out *uiout = current_uiout;
b5fa468f 7084 bool has_disabled_by_cond_location = false;
269b11a2 7085
79a45b7d
TT
7086 get_user_print_options (&opts);
7087
4a64f543
MS
7088 /* Compute the number of rows in the table, as well as the size
7089 required for address fields. */
7f3b0473 7090 nr_printable_breakpoints = 0;
a1decfc1 7091 for (breakpoint &b : all_breakpoints ())
e5a67952
MS
7092 {
7093 /* If we have a filter, only list the breakpoints it accepts. */
a1decfc1 7094 if (filter && !filter (&b))
e5a67952
MS
7095 continue;
7096
5c458ae8 7097 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 7098 accept. Skip the others. */
5c458ae8 7099 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 7100 {
a1decfc1 7101 if (show_internal && parse_and_eval_long (bp_num_list) != b.number)
e5a67952 7102 continue;
a1decfc1 7103 if (!show_internal && !number_is_in_list (bp_num_list, b.number))
e5a67952
MS
7104 continue;
7105 }
269b11a2 7106
a1decfc1 7107 if (show_internal || user_breakpoint_p (&b))
e5a67952
MS
7108 {
7109 int addr_bit, type_len;
a6d9a66e 7110
a1decfc1 7111 addr_bit = breakpoint_address_bits (&b);
e5a67952
MS
7112 if (addr_bit > print_address_bits)
7113 print_address_bits = addr_bit;
269b11a2 7114
a1decfc1 7115 type_len = strlen (bptype_string (b.type));
e5a67952
MS
7116 if (type_len > print_type_col_width)
7117 print_type_col_width = type_len;
7118
7119 nr_printable_breakpoints++;
7120 }
7121 }
7f3b0473 7122
4a2b031d
TT
7123 {
7124 ui_out_emit_table table_emitter (uiout,
7125 opts.addressprint ? 6 : 5,
7126 nr_printable_breakpoints,
7127 "BreakpointTable");
7128
7129 if (nr_printable_breakpoints > 0)
7130 annotate_breakpoints_headers ();
7131 if (nr_printable_breakpoints > 0)
7132 annotate_field (0);
7133 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
7134 if (nr_printable_breakpoints > 0)
7135 annotate_field (1);
7136 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
7137 if (nr_printable_breakpoints > 0)
7138 annotate_field (2);
7139 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
7140 if (nr_printable_breakpoints > 0)
7141 annotate_field (3);
7142 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
7143 if (opts.addressprint)
7144 {
7145 if (nr_printable_breakpoints > 0)
7146 annotate_field (4);
7147 if (print_address_bits <= 32)
7148 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
7149 else
7150 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
7151 }
7152 if (nr_printable_breakpoints > 0)
7153 annotate_field (5);
7154 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
7155 uiout->table_body ();
7156 if (nr_printable_breakpoints > 0)
7157 annotate_breakpoints_table ();
7158
a1decfc1 7159 for (breakpoint &b : all_breakpoints ())
4a2b031d
TT
7160 {
7161 QUIT;
7162 /* If we have a filter, only list the breakpoints it accepts. */
a1decfc1 7163 if (filter && !filter (&b))
4a2b031d 7164 continue;
e5a67952 7165
5c458ae8 7166 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 7167 accept. Skip the others. */
e5a67952 7168
5c458ae8 7169 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 7170 {
5c458ae8 7171 if (show_internal) /* maintenance info breakpoint */
4a2b031d 7172 {
a1decfc1 7173 if (parse_and_eval_long (bp_num_list) != b.number)
4a2b031d
TT
7174 continue;
7175 }
7176 else /* all others */
7177 {
a1decfc1 7178 if (!number_is_in_list (bp_num_list, b.number))
4a2b031d
TT
7179 continue;
7180 }
7181 }
7182 /* We only print out user settable breakpoints unless the
5c458ae8 7183 show_internal is set. */
a1decfc1 7184 if (show_internal || user_breakpoint_p (&b))
b5fa468f 7185 {
a1decfc1
SM
7186 print_one_breakpoint (&b, &last_loc, show_internal);
7187 for (bp_location &loc : b.locations ())
b00b30b2 7188 if (loc.disabled_by_cond)
b5fa468f
TBA
7189 has_disabled_by_cond_location = true;
7190 }
4a2b031d
TT
7191 }
7192 }
698384cd 7193
7f3b0473 7194 if (nr_printable_breakpoints == 0)
c906108c 7195 {
4a64f543
MS
7196 /* If there's a filter, let the caller decide how to report
7197 empty list. */
d77f58be
SS
7198 if (!filter)
7199 {
5c458ae8 7200 if (bp_num_list == NULL || *bp_num_list == '\0')
4a4fd10d
TY
7201 uiout->message ("No breakpoints, watchpoints, tracepoints, "
7202 "or catchpoints.\n");
d77f58be 7203 else
4a4fd10d
TY
7204 uiout->message ("No breakpoint, watchpoint, tracepoint, "
7205 "or catchpoint matching '%s'.\n", bp_num_list);
d77f58be 7206 }
c906108c
SS
7207 }
7208 else
c4093a6a 7209 {
a6d9a66e
UW
7210 if (last_loc && !server_command)
7211 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 7212
85c88e2a 7213 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
7214 uiout->message (_("(*): Breakpoint condition is invalid at this "
7215 "location.\n"));
c4093a6a 7216 }
c906108c 7217
4a64f543 7218 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 7219 there have been breakpoints? */
c906108c 7220 annotate_breakpoints_table_end ();
d77f58be
SS
7221
7222 return nr_printable_breakpoints;
c906108c
SS
7223}
7224
ad443146
SS
7225/* Display the value of default-collect in a way that is generally
7226 compatible with the breakpoint list. */
7227
7228static void
7229default_collect_info (void)
7230{
79a45e25
PA
7231 struct ui_out *uiout = current_uiout;
7232
ad443146
SS
7233 /* If it has no value (which is frequently the case), say nothing; a
7234 message like "No default-collect." gets in user's face when it's
7235 not wanted. */
e0700ba4 7236 if (default_collect.empty ())
ad443146
SS
7237 return;
7238
7239 /* The following phrase lines up nicely with per-tracepoint collect
7240 actions. */
112e8700
SM
7241 uiout->text ("default collect ");
7242 uiout->field_string ("default-collect", default_collect);
7243 uiout->text (" \n");
ad443146
SS
7244}
7245
c906108c 7246static void
0b39b52e 7247info_breakpoints_command (const char *args, int from_tty)
c906108c 7248{
5c458ae8 7249 breakpoint_1 (args, false, NULL);
ad443146
SS
7250
7251 default_collect_info ();
d77f58be
SS
7252}
7253
7254static void
1d12d88f 7255info_watchpoints_command (const char *args, int from_tty)
d77f58be 7256{
5c458ae8 7257 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 7258 struct ui_out *uiout = current_uiout;
d77f58be
SS
7259
7260 if (num_printed == 0)
7261 {
e5a67952 7262 if (args == NULL || *args == '\0')
112e8700 7263 uiout->message ("No watchpoints.\n");
d77f58be 7264 else
112e8700 7265 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 7266 }
c906108c
SS
7267}
7268
7a292a7a 7269static void
4495129a 7270maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 7271{
5c458ae8 7272 breakpoint_1 (args, true, NULL);
ad443146
SS
7273
7274 default_collect_info ();
c906108c
SS
7275}
7276
d8de7963 7277static bool
714835d5 7278breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7279 struct program_space *pspace,
714835d5 7280 CORE_ADDR pc, struct obj_section *section)
0d381245 7281{
b00b30b2 7282 for (bp_location &bl : b->locations ())
0d381245 7283 {
b00b30b2
SM
7284 if (bl.pspace == pspace
7285 && bl.address == pc
7286 && (!overlay_debugging || bl.section == section))
d8de7963 7287 return true;
0d381245 7288 }
d8de7963 7289 return false;
0d381245
VP
7290}
7291
bd21b6c9 7292/* See breakpoint.h. */
c906108c 7293
bd21b6c9 7294void
6c95b8df
PA
7295describe_other_breakpoints (struct gdbarch *gdbarch,
7296 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7297 struct obj_section *section, int thread)
c906108c 7298{
52f0bd74 7299 int others = 0;
c906108c 7300
a1decfc1
SM
7301 for (breakpoint &b : all_breakpoints ())
7302 others += (user_breakpoint_p (&b)
7303 && breakpoint_has_pc (&b, pspace, pc, section));
43892fdf 7304
c906108c
SS
7305 if (others > 0)
7306 {
a3f17187 7307 if (others == 1)
6cb06a8c 7308 gdb_printf (_("Note: breakpoint "));
a3f17187 7309 else /* if (others == ???) */
6cb06a8c 7310 gdb_printf (_("Note: breakpoints "));
a1decfc1
SM
7311 for (breakpoint &b : all_breakpoints ())
7312 if (user_breakpoint_p (&b)
7313 && breakpoint_has_pc (&b, pspace, pc, section))
0d381245
VP
7314 {
7315 others--;
a1decfc1
SM
7316 gdb_printf ("%d", b.number);
7317 if (b.thread == -1 && thread != -1)
6cb06a8c 7318 gdb_printf (" (all threads)");
a1decfc1 7319 else if (b.thread != -1)
ce068c5f 7320 {
a1decfc1 7321 struct thread_info *thr = find_thread_global_id (b.thread);
ce068c5f
AB
7322 gdb_printf (" (thread %s)", print_thread_id (thr));
7323 }
a1decfc1
SM
7324 else if (b.task != -1)
7325 gdb_printf (" (task %d)", b.task);
6cb06a8c 7326 gdb_printf ("%s%s ",
a1decfc1
SM
7327 ((b.enable_state == bp_disabled
7328 || b.enable_state == bp_call_disabled)
6cb06a8c
TT
7329 ? " (disabled)"
7330 : ""),
7331 (others > 1) ? ","
7332 : ((others == 1) ? " and" : ""));
0d381245 7333 }
6a831f06
PA
7334 current_uiout->message (_("also set at pc %ps.\n"),
7335 styled_string (address_style.style (),
7336 paddress (gdbarch, pc)));
c906108c
SS
7337 }
7338}
7339\f
c906108c 7340
cb1e4e32
PA
7341/* Return true iff it is meaningful to use the address member of LOC.
7342 For some breakpoint types, the locations' address members are
7343 irrelevant and it makes no sense to attempt to compare them to
7344 other addresses (or use them for any other purpose either).
2d134ed3 7345
cb1e4e32
PA
7346 More specifically, software watchpoints and catchpoints that are
7347 not backed by breakpoints always have a zero valued location
7348 address and we don't want to mark breakpoints of any of these types
7349 to be a duplicate of an actual breakpoint location at address
7350 zero. */
e4f237da 7351
cb1e4e32 7352static bool
ae2a9665 7353bl_address_is_meaningful (const bp_location *loc)
e4f237da 7354{
cb1e4e32 7355 return loc->loc_type != bp_loc_other;
2d134ed3
PA
7356}
7357
7358/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7359 true if LOC1 and LOC2 represent the same watchpoint location. */
7360
d8de7963 7361static bool
b2ff9ed3
AB
7362watchpoint_locations_match (const struct bp_location *loc1,
7363 const struct bp_location *loc2)
2d134ed3 7364{
bcafd1c1
TT
7365 watchpoint *w1 = gdb::checked_static_cast<watchpoint *> (loc1->owner);
7366 watchpoint *w2 = gdb::checked_static_cast<watchpoint *> (loc2->owner);
3a5c3e22
PA
7367
7368 /* Both of them must exist. */
7369 gdb_assert (w1 != NULL);
7370 gdb_assert (w2 != NULL);
2bdf28a0 7371
4a64f543
MS
7372 /* If the target can evaluate the condition expression in hardware,
7373 then we we need to insert both watchpoints even if they are at
7374 the same place. Otherwise the watchpoint will only trigger when
7375 the condition of whichever watchpoint was inserted evaluates to
7376 true, not giving a chance for GDB to check the condition of the
7377 other watchpoint. */
3a5c3e22 7378 if ((w1->cond_exp
4a64f543
MS
7379 && target_can_accel_watchpoint_condition (loc1->address,
7380 loc1->length,
0cf6dd15 7381 loc1->watchpoint_type,
4d01a485 7382 w1->cond_exp.get ()))
3a5c3e22 7383 || (w2->cond_exp
4a64f543
MS
7384 && target_can_accel_watchpoint_condition (loc2->address,
7385 loc2->length,
0cf6dd15 7386 loc2->watchpoint_type,
4d01a485 7387 w2->cond_exp.get ())))
d8de7963 7388 return false;
0cf6dd15 7389
85d721b8
PA
7390 /* Note that this checks the owner's type, not the location's. In
7391 case the target does not support read watchpoints, but does
7392 support access watchpoints, we'll have bp_read_watchpoint
7393 watchpoints with hw_access locations. Those should be considered
7394 duplicates of hw_read locations. The hw_read locations will
7395 become hw_access locations later. */
2d134ed3
PA
7396 return (loc1->owner->type == loc2->owner->type
7397 && loc1->pspace->aspace == loc2->pspace->aspace
7398 && loc1->address == loc2->address
7399 && loc1->length == loc2->length);
e4f237da
KB
7400}
7401
31e77af2 7402/* See breakpoint.h. */
6c95b8df 7403
31e77af2 7404int
accd0bcd
YQ
7405breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7406 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 7407{
99d9c3b9 7408 return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
6c95b8df
PA
7409 || aspace1 == aspace2)
7410 && addr1 == addr2);
7411}
7412
f1310107
TJB
7413/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7414 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7415 matches ASPACE2. On targets that have global breakpoints, the address
7416 space doesn't really matter. */
7417
d8de7963 7418static bool
accd0bcd
YQ
7419breakpoint_address_match_range (const address_space *aspace1,
7420 CORE_ADDR addr1,
7421 int len1, const address_space *aspace2,
f1310107
TJB
7422 CORE_ADDR addr2)
7423{
99d9c3b9 7424 return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
f1310107
TJB
7425 || aspace1 == aspace2)
7426 && addr2 >= addr1 && addr2 < addr1 + len1);
7427}
7428
7429/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7430 a ranged breakpoint. In most targets, a match happens only if ASPACE
7431 matches the breakpoint's address space. On targets that have global
7432 breakpoints, the address space doesn't really matter. */
7433
d8de7963 7434static bool
f1310107 7435breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 7436 const address_space *aspace,
f1310107
TJB
7437 CORE_ADDR addr)
7438{
f9582a22 7439 return (breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
f1310107
TJB
7440 aspace, addr)
7441 || (bl->length
f9582a22 7442 && breakpoint_address_match_range (bl->pspace->aspace.get (),
f1310107
TJB
7443 bl->address, bl->length,
7444 aspace, addr)));
7445}
7446
d35ae833
PA
7447/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7448 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7449 match happens only if ASPACE matches the breakpoint's address
7450 space. On targets that have global breakpoints, the address space
7451 doesn't really matter. */
7452
d8de7963 7453static bool
d35ae833 7454breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 7455 const address_space *aspace,
d35ae833
PA
7456 CORE_ADDR addr, int len)
7457{
99d9c3b9 7458 if (gdbarch_has_global_breakpoints (current_inferior ()->arch ())
f9582a22 7459 || bl->pspace->aspace.get () == aspace)
d35ae833
PA
7460 {
7461 int bl_len = bl->length != 0 ? bl->length : 1;
7462
7463 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7464 return 1;
7465 }
7466 return 0;
7467}
7468
1e4d1764
YQ
7469/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7470 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7471 true, otherwise returns false. */
7472
d8de7963 7473static bool
b2ff9ed3
AB
7474tracepoint_locations_match (const struct bp_location *loc1,
7475 const struct bp_location *loc2)
1e4d1764
YQ
7476{
7477 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7478 /* Since tracepoint locations are never duplicated with others', tracepoint
7479 locations at the same address of different tracepoints are regarded as
7480 different locations. */
7481 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7482 else
d8de7963 7483 return false;
1e4d1764
YQ
7484}
7485
2d134ed3 7486/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 7487 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
7488 the same location. If SW_HW_BPS_MATCH is true, then software
7489 breakpoint locations and hardware breakpoint locations match,
7490 otherwise they don't. */
2d134ed3 7491
d8de7963 7492static bool
b2ff9ed3
AB
7493breakpoint_locations_match (const struct bp_location *loc1,
7494 const struct bp_location *loc2,
7f32a4d5 7495 bool sw_hw_bps_match)
2d134ed3 7496{
2bdf28a0
JK
7497 int hw_point1, hw_point2;
7498
7499 /* Both of them must not be in moribund_locations. */
7500 gdb_assert (loc1->owner != NULL);
7501 gdb_assert (loc2->owner != NULL);
7502
7503 hw_point1 = is_hardware_watchpoint (loc1->owner);
7504 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7505
7506 if (hw_point1 != hw_point2)
d8de7963 7507 return false;
2d134ed3
PA
7508 else if (hw_point1)
7509 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7510 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7511 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7512 else
7f32a4d5
PA
7513 /* We compare bp_location.length in order to cover ranged
7514 breakpoints. Keep this in sync with
8dd54de0 7515 bp_location_ptr_is_less_than. */
f9582a22
TV
7516 return (breakpoint_address_match (loc1->pspace->aspace.get (),
7517 loc1->address,
7518 loc2->pspace->aspace.get (),
7519 loc2->address)
7f32a4d5 7520 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 7521 && loc1->length == loc2->length);
2d134ed3
PA
7522}
7523
76897487
KB
7524static void
7525breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
d8de7963 7526 int bnum, bool have_bnum)
76897487 7527{
f63fbe86
MS
7528 /* The longest string possibly returned by hex_string_custom
7529 is 50 chars. These must be at least that big for safety. */
7530 char astr1[64];
7531 char astr2[64];
76897487 7532
bb599908
PH
7533 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7534 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7535 if (have_bnum)
8a3fe4f8 7536 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 7537 bnum, astr1, astr2);
76897487 7538 else
8a3fe4f8 7539 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7540}
7541
4a64f543
MS
7542/* Adjust a breakpoint's address to account for architectural
7543 constraints on breakpoint placement. Return the adjusted address.
7544 Note: Very few targets require this kind of adjustment. For most
7545 targets, this function is simply the identity function. */
76897487
KB
7546
7547static CORE_ADDR
a6d9a66e 7548adjust_breakpoint_address (struct gdbarch *gdbarch,
60270718
AB
7549 CORE_ADDR bpaddr, enum bptype bptype,
7550 struct program_space *pspace)
76897487 7551{
60270718
AB
7552 gdb_assert (pspace != nullptr);
7553
a0de8c21
YQ
7554 if (bptype == bp_watchpoint
7555 || bptype == bp_hardware_watchpoint
7556 || bptype == bp_read_watchpoint
7557 || bptype == bp_access_watchpoint
7558 || bptype == bp_catchpoint)
88f7da05
KB
7559 {
7560 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 7561 have their addresses modified. */
88f7da05
KB
7562 return bpaddr;
7563 }
7c16b83e
PA
7564 else if (bptype == bp_single_step)
7565 {
7566 /* Single-step breakpoints should not have their addresses
7567 modified. If there's any architectural constrain that
7568 applies to this address, then it should have already been
7569 taken into account when the breakpoint was created in the
7570 first place. If we didn't do this, stepping through e.g.,
7571 Thumb-2 IT blocks would break. */
7572 return bpaddr;
7573 }
76897487
KB
7574 else
7575 {
a0de8c21
YQ
7576 CORE_ADDR adjusted_bpaddr = bpaddr;
7577
60270718
AB
7578 /* Some targets have architectural constraints on the placement
7579 of breakpoint instructions. Obtain the adjusted address. */
a0de8c21
YQ
7580 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7581 {
60270718
AB
7582 /* Targets that implement this adjustment function will likely
7583 inspect either the symbol table, target memory at BPADDR, or
7584 even state registers, so ensure a suitable thread (and its
7585 associated program space) are currently selected. */
7586 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7587 switch_to_program_space_and_thread (pspace);
7588 adjusted_bpaddr
7589 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
a0de8c21 7590 }
76897487 7591
d88cb738 7592 adjusted_bpaddr
86bb38ce 7593 = gdbarch_remove_non_address_bits_breakpoint (gdbarch, adjusted_bpaddr);
76897487
KB
7594
7595 /* An adjusted breakpoint address can significantly alter
dda83cd7 7596 a user's expectations. Print a warning if an adjustment
76897487
KB
7597 is required. */
7598 if (adjusted_bpaddr != bpaddr)
d8de7963 7599 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
76897487
KB
7600
7601 return adjusted_bpaddr;
7602 }
7603}
7604
cb1e4e32
PA
7605static bp_loc_type
7606bp_location_from_bp_type (bptype type)
7cc221ef 7607{
cb1e4e32 7608 switch (type)
e049a4b5
DJ
7609 {
7610 case bp_breakpoint:
7c16b83e 7611 case bp_single_step:
e049a4b5
DJ
7612 case bp_until:
7613 case bp_finish:
7614 case bp_longjmp:
7615 case bp_longjmp_resume:
e2e4d78b 7616 case bp_longjmp_call_dummy:
186c406b
TT
7617 case bp_exception:
7618 case bp_exception_resume:
e049a4b5 7619 case bp_step_resume:
2c03e5be 7620 case bp_hp_step_resume:
e049a4b5
DJ
7621 case bp_watchpoint_scope:
7622 case bp_call_dummy:
aa7d318d 7623 case bp_std_terminate:
e049a4b5
DJ
7624 case bp_shlib_event:
7625 case bp_thread_event:
7626 case bp_overlay_event:
4efc6507 7627 case bp_jit_event:
0fd8e87f 7628 case bp_longjmp_master:
aa7d318d 7629 case bp_std_terminate_master:
186c406b 7630 case bp_exception_master:
0e30163f
JK
7631 case bp_gnu_ifunc_resolver:
7632 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7633 case bp_dprintf:
cb1e4e32 7634 return bp_loc_software_breakpoint;
0b63c811 7635
e049a4b5 7636 case bp_hardware_breakpoint:
cb1e4e32 7637 return bp_loc_hardware_breakpoint;
0b63c811 7638
e049a4b5
DJ
7639 case bp_hardware_watchpoint:
7640 case bp_read_watchpoint:
7641 case bp_access_watchpoint:
cb1e4e32 7642 return bp_loc_hardware_watchpoint;
0b63c811 7643
e049a4b5 7644 case bp_watchpoint:
6e14e441 7645 return bp_loc_software_watchpoint;
0b63c811 7646
15c3d785
PA
7647 case bp_tracepoint:
7648 case bp_fast_tracepoint:
0fb4aa4b 7649 case bp_static_tracepoint:
7b572efb 7650 case bp_static_marker_tracepoint:
0b63c811
SM
7651 return bp_loc_tracepoint;
7652
7653 case bp_catchpoint:
cb1e4e32 7654 return bp_loc_other;
0b63c811 7655
e049a4b5 7656 default:
f34652de 7657 internal_error (_("unknown breakpoint type"));
e049a4b5 7658 }
cb1e4e32
PA
7659}
7660
7661bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7662{
7663 this->owner = owner;
7664 this->cond_bytecode = NULL;
7665 this->shlib_disabled = 0;
7666 this->enabled = 1;
b5fa468f 7667 this->disabled_by_cond = false;
cb1e4e32
PA
7668
7669 this->loc_type = type;
e049a4b5 7670
cb1e4e32
PA
7671 if (this->loc_type == bp_loc_software_breakpoint
7672 || this->loc_type == bp_loc_hardware_breakpoint)
7673 mark_breakpoint_location_modified (this);
7674
b6433ede 7675 incref ();
cb1e4e32
PA
7676}
7677
7678bp_location::bp_location (breakpoint *owner)
7679 : bp_location::bp_location (owner,
7680 bp_location_from_bp_type (owner->type))
7681{
28010a5d
PA
7682}
7683
3cdc2d7e
MS
7684/* See breakpoint.h. */
7685
7686std::string
7687bp_location::to_string () const
7688{
7689 string_file stb;
7690 ui_out_redirect_pop redir (current_uiout, &stb);
7691 print_breakpoint_location (this->owner, this);
01285426 7692 return stb.release ();
3cdc2d7e
MS
7693}
7694
f431efe5
PA
7695/* Decrement reference count. If the reference count reaches 0,
7696 destroy the bp_location. Sets *BLP to NULL. */
7697
7698static void
7699decref_bp_location (struct bp_location **blp)
7700{
b6433ede 7701 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7702 *blp = NULL;
7703}
7704
346774a9 7705/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7706
b270e6f9
TT
7707static breakpoint *
7708add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7709{
346774a9
PA
7710 /* Add this breakpoint to the end of the chain so that a list of
7711 breakpoints will come out in order of increasing numbers. */
7712
e2a15788 7713 breakpoint_chain.push_back (*b.release ());
b270e6f9 7714
e2a15788 7715 return &breakpoint_chain.back ();
346774a9
PA
7716}
7717
0ba852ab 7718/* Initialize loc->function_name. */
0e30163f 7719
0d381245 7720static void
0ba852ab 7721set_breakpoint_location_function (struct bp_location *loc)
0d381245 7722{
2bdf28a0
JK
7723 gdb_assert (loc->owner != NULL);
7724
0d381245 7725 if (loc->owner->type == bp_breakpoint
1042e4c0 7726 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7727 || is_tracepoint (loc->owner))
0d381245 7728 {
2c02bd72 7729 const char *function_name;
0e30163f 7730
3467ec66 7731 if (loc->msymbol != NULL
60f62e2b
SM
7732 && (loc->msymbol->type () == mst_text_gnu_ifunc
7733 || loc->msymbol->type () == mst_data_gnu_ifunc))
0e30163f
JK
7734 {
7735 struct breakpoint *b = loc->owner;
7736
c9d95fa3 7737 function_name = loc->msymbol->linkage_name ();
3467ec66 7738
9dc1523b
SM
7739 if (b->type == bp_breakpoint
7740 && b->has_single_location ()
7741 && b->related_breakpoint == b)
0e30163f
JK
7742 {
7743 /* Create only the whole new breakpoint of this type but do not
7744 mess more complicated breakpoints with multiple locations. */
7745 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7746 /* Remember the resolver's address for use by the return
dda83cd7 7747 breakpoint. */
3467ec66 7748 loc->related_address = loc->address;
0e30163f
JK
7749 }
7750 }
3467ec66
PA
7751 else
7752 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7753
2c02bd72 7754 if (function_name)
23d6ee64 7755 loc->function_name = make_unique_xstrdup (function_name);
0d381245
VP
7756 }
7757}
7758
a6d9a66e 7759/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7760struct gdbarch *
a6d9a66e
UW
7761get_sal_arch (struct symtab_and_line sal)
7762{
7763 if (sal.section)
08feed99 7764 return sal.section->objfile->arch ();
a6d9a66e 7765 if (sal.symtab)
3c86fae3 7766 return sal.symtab->compunit ()->objfile ()->arch ();
a6d9a66e
UW
7767
7768 return NULL;
7769}
7770
53a5351d 7771/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7772 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7773 initiated the operation. */
c906108c
SS
7774
7775void
186c406b 7776set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7777{
5d5658a1 7778 int thread = tp->global_num;
0fd8e87f
UW
7779
7780 /* To avoid having to rescan all objfile symbols at every step,
7781 we maintain a list of continually-inserted but always disabled
7782 longjmp "master" breakpoints. Here, we simply create momentary
7783 clones of those and enable them for the requested thread. */
a1decfc1
SM
7784 for (breakpoint &b : all_breakpoints_safe ())
7785 if (b.pspace == current_program_space
7786 && (b.type == bp_longjmp_master
7787 || b.type == bp_exception_master))
0fd8e87f 7788 {
a1decfc1 7789 bptype type = b.type == bp_longjmp_master ? bp_longjmp : bp_exception;
e2e4d78b
JK
7790 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7791 after their removal. */
a1decfc1 7792 momentary_breakpoint_from_master (&b, type, 1, thread);
0fd8e87f 7793 }
186c406b
TT
7794
7795 tp->initiating_frame = frame;
c906108c
SS
7796}
7797
611c83ae 7798/* Delete all longjmp breakpoints from THREAD. */
c906108c 7799void
611c83ae 7800delete_longjmp_breakpoint (int thread)
c906108c 7801{
a1decfc1
SM
7802 for (breakpoint &b : all_breakpoints_safe ())
7803 if (b.type == bp_longjmp || b.type == bp_exception)
611c83ae 7804 {
a1decfc1 7805 if (b.thread == thread)
b080fe54
AB
7806 {
7807 gdb_assert (b.inferior == -1);
7808 delete_breakpoint (&b);
7809 }
611c83ae 7810 }
c906108c
SS
7811}
7812
f59f708a
PA
7813void
7814delete_longjmp_breakpoint_at_next_stop (int thread)
7815{
a1decfc1
SM
7816 for (breakpoint &b : all_breakpoints_safe ())
7817 if (b.type == bp_longjmp || b.type == bp_exception)
f59f708a 7818 {
a1decfc1 7819 if (b.thread == thread)
b080fe54
AB
7820 {
7821 gdb_assert (b.inferior == -1);
7822 b.disposition = disp_del_at_next_stop;
7823 }
f59f708a
PA
7824 }
7825}
7826
e2e4d78b
JK
7827/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7828 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7829 pointer to any of them. Return NULL if this system cannot place longjmp
7830 breakpoints. */
7831
7832struct breakpoint *
7833set_longjmp_breakpoint_for_call_dummy (void)
7834{
43892fdf 7835 breakpoint *retval = nullptr;
e2e4d78b 7836
a1decfc1
SM
7837 for (breakpoint &b : all_breakpoints ())
7838 if (b.pspace == current_program_space && b.type == bp_longjmp_master)
e2e4d78b 7839 {
7ab97995
PA
7840 int thread = inferior_thread ()->global_num;
7841 breakpoint *new_b
a1decfc1 7842 = momentary_breakpoint_from_master (&b, bp_longjmp_call_dummy,
7ab97995 7843 1, thread);
e2e4d78b
JK
7844
7845 /* Link NEW_B into the chain of RETVAL breakpoints. */
7846
7847 gdb_assert (new_b->related_breakpoint == new_b);
7848 if (retval == NULL)
7849 retval = new_b;
7850 new_b->related_breakpoint = retval;
7851 while (retval->related_breakpoint != new_b->related_breakpoint)
7852 retval = retval->related_breakpoint;
7853 retval->related_breakpoint = new_b;
7854 }
7855
7856 return retval;
7857}
7858
7859/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7860 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7861 stack.
7862
b4b3e2de
AB
7863 If the unwind fails then there is not sufficient information to discard
7864 dummy frames. In this case, elide the clean up and the dummy frames will
7865 be cleaned up next time this function is called from a location where
7866 unwinding is possible. */
e2e4d78b
JK
7867
7868void
b67a2c6f 7869check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b 7870{
1fba7b3a
SM
7871 /* We would need to delete breakpoints other than the current one while
7872 iterating, so all_breakpoints_safe is not sufficient to make that safe.
7873 Save all breakpoints to delete in that set and delete them at the end. */
72a66913 7874 gdb::unordered_set<breakpoint *> to_delete;
1fba7b3a 7875
a1decfc1 7876 for (struct breakpoint &b : all_breakpoints ())
7a8de0c3 7877 {
a1decfc1 7878 if (b.type == bp_longjmp_call_dummy && b.thread == tp->global_num)
7a8de0c3 7879 {
b080fe54 7880 gdb_assert (b.inferior == -1);
a1decfc1 7881 struct breakpoint *dummy_b = b.related_breakpoint;
7a8de0c3
TT
7882
7883 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7884 chained off b->related_breakpoint. */
a1decfc1 7885 while (dummy_b != &b && dummy_b->type != bp_call_dummy)
7a8de0c3
TT
7886 dummy_b = dummy_b->related_breakpoint;
7887
7888 /* If there was no bp_call_dummy breakpoint then there's nothing
7889 more to do. Or, if the dummy frame associated with the
7890 bp_call_dummy is still on the stack then we need to leave this
7891 bp_call_dummy in place. */
7892 if (dummy_b->type != bp_call_dummy
7893 || frame_find_by_id (dummy_b->frame_id) != NULL)
7894 continue;
b4b3e2de 7895
7a8de0c3
TT
7896 /* We didn't find the dummy frame on the stack, this could be
7897 because we have longjmp'd to a stack frame that is previous to
7898 the dummy frame, or it could be because the stack unwind is
7899 broken at some point between the longjmp frame and the dummy
7900 frame.
7901
7902 Next we figure out why the stack unwind stopped. If it looks
7903 like the unwind is complete then we assume the dummy frame has
7904 been jumped over, however, if the unwind stopped for an
7905 unexpected reason then we assume the stack unwind is currently
7906 broken, and that we will (eventually) return to the dummy
7907 frame.
7908
7909 It might be tempting to consider using frame_id_inner here, but
7910 that is not safe. There is no guarantee that the stack frames
7911 we are looking at here are even on the same stack as the
7912 original dummy frame, hence frame_id_inner can't be used. See
7913 the comments on frame_id_inner for more details. */
7914 bool unwind_finished_unexpectedly = false;
7915 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7916 {
7917 frame_info_ptr prev = get_prev_frame (fi);
7918 if (prev == nullptr)
7919 {
7920 /* FI is the last stack frame. Why did this frame not
7921 unwind further? */
7922 auto stop_reason = get_frame_unwind_stop_reason (fi);
7923 if (stop_reason != UNWIND_NO_REASON
7924 && stop_reason != UNWIND_OUTERMOST)
7925 unwind_finished_unexpectedly = true;
7926 }
7927 fi = prev;
7928 }
7929 if (unwind_finished_unexpectedly)
7930 continue;
b4b3e2de 7931
7a8de0c3 7932 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b 7933
a1decfc1
SM
7934 for (breakpoint *related_breakpoint = b.related_breakpoint;
7935 related_breakpoint != &b;
1fba7b3a 7936 related_breakpoint = related_breakpoint->related_breakpoint)
a1decfc1 7937 to_delete.insert (b.related_breakpoint);
1fba7b3a 7938
a1decfc1 7939 to_delete.insert (&b);
7a8de0c3
TT
7940 }
7941 }
1fba7b3a
SM
7942
7943 for (breakpoint *b : to_delete)
7944 delete_breakpoint (b);
e2e4d78b
JK
7945}
7946
1900040c
MS
7947void
7948enable_overlay_breakpoints (void)
7949{
a1decfc1
SM
7950 for (breakpoint &b : all_breakpoints ())
7951 if (b.type == bp_overlay_event)
01add95b 7952 {
a1decfc1 7953 b.enable_state = bp_enabled;
01add95b
SM
7954 update_global_location_list (UGLL_MAY_INSERT);
7955 overlay_events_enabled = 1;
7956 }
1900040c
MS
7957}
7958
7959void
7960disable_overlay_breakpoints (void)
7961{
a1decfc1
SM
7962 for (breakpoint &b : all_breakpoints ())
7963 if (b.type == bp_overlay_event)
01add95b 7964 {
a1decfc1 7965 b.enable_state = bp_disabled;
01add95b
SM
7966 update_global_location_list (UGLL_DONT_INSERT);
7967 overlay_events_enabled = 0;
7968 }
1900040c
MS
7969}
7970
aa7d318d
TT
7971/* Set an active std::terminate breakpoint for each std::terminate
7972 master breakpoint. */
7973void
7974set_std_terminate_breakpoint (void)
7975{
a1decfc1
SM
7976 for (breakpoint &b : all_breakpoints_safe ())
7977 if (b.pspace == current_program_space
7978 && b.type == bp_std_terminate_master)
aa7d318d 7979 {
a1decfc1 7980 momentary_breakpoint_from_master (&b, bp_std_terminate, 1,
7ab97995 7981 inferior_thread ()->global_num);
aa7d318d
TT
7982 }
7983}
7984
7985/* Delete all the std::terminate breakpoints. */
7986void
7987delete_std_terminate_breakpoint (void)
7988{
a1decfc1
SM
7989 for (breakpoint &b : all_breakpoints_safe ())
7990 if (b.type == bp_std_terminate)
7991 delete_breakpoint (&b);
aa7d318d
TT
7992}
7993
c4093a6a 7994struct breakpoint *
a6d9a66e 7995create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7996{
7997 struct breakpoint *b;
c4093a6a 7998
9efa3c7f 7999 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
06edf0c0 8000
b5de0fa7 8001 b->enable_state = bp_enabled;
264f9890 8002 /* locspec has to be used or breakpoint_re_set will delete me. */
f5951b9f 8003 b->locspec = new_address_location_spec (b->first_loc ().address, NULL, 0);
c4093a6a 8004
44702360 8005 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8006
c4093a6a
JM
8007 return b;
8008}
8009
0101ce28
JJ
8010struct lang_and_radix
8011 {
8012 enum language lang;
8013 int radix;
8014 };
8015
4efc6507
DE
8016/* Create a breakpoint for JIT code registration and unregistration. */
8017
8018struct breakpoint *
8019create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
8020{
9efa3c7f 8021 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
4efc6507 8022}
0101ce28 8023
03673fc7
PP
8024/* Remove JIT code registration and unregistration breakpoint(s). */
8025
8026void
8027remove_jit_event_breakpoints (void)
8028{
a1decfc1
SM
8029 for (breakpoint &b : all_breakpoints_safe ())
8030 if (b.type == bp_jit_event
8031 && b.first_loc ().pspace == current_program_space)
8032 delete_breakpoint (&b);
03673fc7
PP
8033}
8034
cae688ec
JJ
8035void
8036remove_solib_event_breakpoints (void)
8037{
a1decfc1
SM
8038 for (breakpoint &b : all_breakpoints_safe ())
8039 if (b.type == bp_shlib_event
8040 && b.first_loc ().pspace == current_program_space)
8041 delete_breakpoint (&b);
cae688ec
JJ
8042}
8043
f37f681c
PA
8044/* See breakpoint.h. */
8045
8046void
8047remove_solib_event_breakpoints_at_next_stop (void)
8048{
a1decfc1
SM
8049 for (breakpoint &b : all_breakpoints_safe ())
8050 if (b.type == bp_shlib_event
8051 && b.first_loc ().pspace == current_program_space)
8052 b.disposition = disp_del_at_next_stop;
f37f681c
PA
8053}
8054
04086b45
PA
8055/* Helper for create_solib_event_breakpoint /
8056 create_and_insert_solib_event_breakpoint. Allows specifying which
8057 INSERT_MODE to pass through to update_global_location_list. */
8058
8059static struct breakpoint *
8060create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
8061 enum ugll_insert_mode insert_mode)
cae688ec
JJ
8062{
8063 struct breakpoint *b;
8064
9efa3c7f 8065 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
04086b45 8066 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
8067 return b;
8068}
8069
04086b45
PA
8070struct breakpoint *
8071create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
8072{
8073 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
8074}
8075
f37f681c
PA
8076/* See breakpoint.h. */
8077
8078struct breakpoint *
8079create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
8080{
8081 struct breakpoint *b;
8082
04086b45
PA
8083 /* Explicitly tell update_global_location_list to insert
8084 locations. */
8085 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f5951b9f 8086 if (!b->first_loc ().inserted)
f37f681c
PA
8087 {
8088 delete_breakpoint (b);
8089 return NULL;
8090 }
8091 return b;
8092}
8093
63644780
NB
8094/* Disable any breakpoints and tracepoints that are in SOLIB upon
8095 notification of unloaded_shlib. Only apply to enabled breakpoints,
9da3b735
AB
8096 disabled ones can just stay disabled.
8097
8098 When STILL_IN_USE is true, SOLIB hasn't really been unmapped from
4bc72a45
AB
8099 the inferior. In this case, don't disable anything.
8100
8101 When SILENT is false notify the user if any breakpoints are disabled,
8102 otherwise, still disable the breakpoints, but don't tell the user. */
84acb35a 8103
75149521 8104static void
9da3b735 8105disable_breakpoints_in_unloaded_shlib (program_space *pspace, const solib &solib,
4bc72a45 8106 bool still_in_use, bool silent)
84acb35a 8107{
9da3b735
AB
8108 if (still_in_use)
8109 return;
8110
d8de7963 8111 bool disabled_shlib_breaks = false;
84acb35a 8112
e9709998 8113 for (breakpoint &b : all_breakpoints ())
01add95b 8114 {
e9709998 8115 bool bp_modified = false;
01add95b 8116
e9709998 8117 for (bp_location &loc : b.locations ())
01add95b 8118 {
e9709998
AB
8119 if (pspace != loc.pspace || loc.shlib_disabled)
8120 continue;
8121
8122 if (loc.loc_type != bp_loc_hardware_breakpoint
8123 && loc.loc_type != bp_loc_software_breakpoint
8124 && !is_tracepoint (&b))
8125 continue;
8126
8127 if (!solib_contains_address_p (solib, loc.address))
8128 continue;
8129
8130 loc.shlib_disabled = 1;
8131
0e9794f1
AB
8132 /* At this point, we don't know whether the shared library
8133 was unmapped from the inferior or not, so leave the
8134 inserted flag alone. We'll handle failure to uninsert
8135 quietly, in case the library was indeed unmapped.
8136
8137 The test gdb.base/nostdlib.exp when run on AArch64
8138 GNU/Linux using glibc will cause the dynamic linker to be
8139 unloaded from the inferior, but the linker will never be
8140 unmapped. Additionally, at the time the dynamic linker
8141 is unloaded the inferior will be stopped within the
8142 dynamic linker.
8143
8144 If we clear the inserted flag here then GDB will fail to
8145 remove the internal breakpoints from the dynamic linker
8146 leading to unexpected SIGTRAPs. */
01add95b 8147
e9709998 8148 bp_modified = true;
01add95b 8149
4bc72a45 8150 if (!disabled_shlib_breaks && !silent && user_breakpoint_p (&b))
01add95b
SM
8151 {
8152 target_terminal::ours_for_output ();
8153 warning (_("Temporarily disabling breakpoints "
8154 "for unloaded shared library \"%s\""),
6896e625 8155 solib.name.c_str ());
e9709998 8156 disabled_shlib_breaks = true;
01add95b 8157 }
01add95b 8158 }
e9709998
AB
8159
8160 if (bp_modified)
8161 notify_breakpoint_modified (&b);
01add95b 8162 }
84acb35a
JJ
8163}
8164
63644780
NB
8165/* Disable any breakpoints and tracepoints in OBJFILE upon
8166 notification of free_objfile. Only apply to enabled breakpoints,
8167 disabled ones can just stay disabled. */
8168
8169static void
8170disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8171{
d03de421
PA
8172 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8173 managed by the user with add-symbol-file/remove-symbol-file.
8174 Similarly to how breakpoints in shared libraries are handled in
8175 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
8176 shlib_disabled so they end up uninserted on the next global
8177 location list update. Shared libraries not loaded by the user
8178 aren't handled here -- they're already handled in
8179 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8180 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
8181 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8182 main objfile). */
8183 if ((objfile->flags & OBJF_SHARED) == 0
8184 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
8185 return;
8186
a1decfc1 8187 for (breakpoint &b : all_breakpoints ())
63644780 8188 {
d8de7963 8189 bool bp_modified = false;
63644780 8190
a1decfc1 8191 if (!is_breakpoint (&b) && !is_tracepoint (&b))
63644780
NB
8192 continue;
8193
a1decfc1 8194 for (bp_location &loc : b.locations ())
63644780 8195 {
b00b30b2 8196 CORE_ADDR loc_addr = loc.address;
63644780 8197
b00b30b2
SM
8198 if (loc.loc_type != bp_loc_hardware_breakpoint
8199 && loc.loc_type != bp_loc_software_breakpoint)
63644780
NB
8200 continue;
8201
b00b30b2 8202 if (loc.shlib_disabled != 0)
63644780
NB
8203 continue;
8204
134a0a10 8205 if (objfile->pspace () != loc.pspace)
63644780
NB
8206 continue;
8207
63644780
NB
8208 if (is_addr_in_objfile (loc_addr, objfile))
8209 {
b00b30b2 8210 loc.shlib_disabled = 1;
08351840
PA
8211 /* At this point, we don't know whether the object was
8212 unmapped from the inferior or not, so leave the
8213 inserted flag alone. We'll handle failure to
8214 uninsert quietly, in case the object was indeed
8215 unmapped. */
63644780 8216
b00b30b2 8217 mark_breakpoint_location_modified (&loc);
63644780 8218
d8de7963 8219 bp_modified = true;
63644780
NB
8220 }
8221 }
8222
8223 if (bp_modified)
19081eb5 8224 notify_breakpoint_modified (&b);
63644780
NB
8225 }
8226}
8227
b650a282 8228/* See breakpoint.h. */
ce78b96d 8229
92bb0228
PA
8230breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
8231 bool temp, const char *cond_string_)
8232 : type (bptype),
8233 disposition (temp ? disp_del : disp_donttouch),
8234 gdbarch (gdbarch_),
8235 language (current_language->la_language),
8236 input_radix (::input_radix),
8237 cond_string (cond_string_ != nullptr
8238 ? make_unique_xstrdup (cond_string_)
8239 : nullptr),
8240 related_breakpoint (this)
8241{
8242}
8243
8244/* See breakpoint.h. */
8245
fed1c982 8246catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
92bb0228
PA
8247 const char *cond_string)
8248 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
c906108c 8249{
acd0955b 8250 add_dummy_location (this, current_program_space);
c5aa993b 8251
acd0955b 8252 pspace = current_program_space;
346774a9
PA
8253}
8254
a92e9430
AB
8255/* See breakpoint.h. */
8256
8257void
6cce0251 8258catchpoint::re_set (program_space *filter_pspace)
a92e9430
AB
8259{
8260 /* All catchpoints are associated with a specific program_space. */
8261 gdb_assert (pspace != nullptr);
8262
6cce0251
AB
8263 /* If only a single program space changed, and it's not the program space
8264 for which this catchpoint applies, then there's nothing to do. */
8265 if (filter_pspace != nullptr && filter_pspace != pspace)
8266 return;
8267
a92e9430
AB
8268 /* Catchpoints have a single dummy location. */
8269 gdb_assert (locations ().size () == 1);
8270 bp_location &bl = m_locations.front ();
8271
8272 if (cond_string == nullptr)
8273 {
8274 /* It shouldn't be possible to have a parsed condition expression
8275 cached on this location if the catchpoint doesn't have a condition
8276 string set. */
8277 gdb_assert (bl.cond == nullptr);
8278
8279 /* Nothing to re-compute, and the catchpoint cannot change. */
8280 return;
8281 }
8282
8283 bool previous_disabled_by_cond = bl.disabled_by_cond;
8284
8285 /* Start by marking the location disabled and discarding the previously
8286 computed condition expression. Now if we get an exception, even if
8287 it's a quit exception, we'll leave the location disabled and there
8288 will be no (possibly invalid) expression cached. */
8289 bl.disabled_by_cond = true;
8290 bl.cond = nullptr;
8291
8292 const char *s = cond_string.get ();
8293 try
8294 {
8295 switch_to_program_space_and_thread (pspace);
8296
8297 bl.cond = parse_exp_1 (&s, bl.address, block_for_pc (bl.address),
8298 nullptr);
8299 bl.disabled_by_cond = false;
8300 }
8301 catch (const gdb_exception_error &e)
8302 {
8303 /* Any exception thrown must be from either the parse_exp_1 or
8304 earlier in the try block. As such the following two asserts
8305 should be true. */
8306 gdb_assert (bl.disabled_by_cond);
8307 gdb_assert (bl.cond == nullptr);
8308 }
8309
8310 if (previous_disabled_by_cond != bl.disabled_by_cond)
8311 notify_breakpoint_modified (this);
8312}
8313
e7692320
SM
8314/* Notify interpreters and observers that breakpoint B was created. */
8315
8316static void
8317notify_breakpoint_created (breakpoint *b)
8318{
8319 interps_notify_breakpoint_created (b);
8320 gdb::observers::breakpoint_created.notify (b);
8321}
8322
b82d4ec9 8323breakpoint *
b270e6f9 8324install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8325{
b270e6f9 8326 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8327 set_breakpoint_number (internal, b);
558a9d82
YQ
8328 if (is_tracepoint (b))
8329 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8330 if (!internal)
8331 mention (b);
e7692320
SM
8332
8333 notify_breakpoint_created (b);
3ea46bff
YQ
8334
8335 if (update_gll)
44702360 8336 update_global_location_list (UGLL_MAY_INSERT);
b82d4ec9
PA
8337
8338 return b;
c56053d2
PA
8339}
8340
c906108c 8341static int
fba45db2 8342hw_breakpoint_used_count (void)
c906108c 8343{
c906108c
SS
8344 int i = 0;
8345
a1decfc1
SM
8346 for (breakpoint &b : all_breakpoints ())
8347 if (b.type == bp_hardware_breakpoint && breakpoint_enabled (&b))
8348 for (bp_location &bl : b.locations ())
f1310107
TJB
8349 {
8350 /* Special types of hardware breakpoints may use more than
8351 one register. */
a1decfc1 8352 i += b.resources_needed (&bl);
f1310107 8353 }
c906108c
SS
8354
8355 return i;
8356}
8357
a1398e0c
PA
8358/* Returns the resources B would use if it were a hardware
8359 watchpoint. */
8360
c906108c 8361static int
a1398e0c 8362hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8363{
c906108c
SS
8364 int i = 0;
8365
a1398e0c
PA
8366 if (!breakpoint_enabled (b))
8367 return 0;
8368
b00b30b2 8369 for (bp_location &bl : b->locations ())
a1398e0c
PA
8370 {
8371 /* Special types of hardware watchpoints may use more than
8372 one register. */
b00b30b2 8373 i += b->resources_needed (&bl);
a1398e0c
PA
8374 }
8375
8376 return i;
8377}
8378
8379/* Returns the sum the used resources of all hardware watchpoints of
8380 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8381 the sum of the used resources of all hardware watchpoints of other
8382 types _not_ TYPE. */
8383
8384static int
8385hw_watchpoint_used_count_others (struct breakpoint *except,
8386 enum bptype type, int *other_type_used)
8387{
8388 int i = 0;
a1398e0c 8389
c906108c 8390 *other_type_used = 0;
a1decfc1 8391 for (breakpoint &b : all_breakpoints ())
e09342b5 8392 {
a1decfc1 8393 if (&b == except)
a1398e0c 8394 continue;
a1decfc1 8395 if (!breakpoint_enabled (&b))
e09342b5
TJB
8396 continue;
8397
a1decfc1
SM
8398 if (b.type == type)
8399 i += hw_watchpoint_use_count (&b);
8400 else if (is_hardware_watchpoint (&b))
a1398e0c 8401 *other_type_used = 1;
e09342b5
TJB
8402 }
8403
c906108c
SS
8404 return i;
8405}
8406
c906108c 8407void
fba45db2 8408disable_watchpoints_before_interactive_call_start (void)
c906108c 8409{
a1decfc1
SM
8410 for (breakpoint &b : all_breakpoints ())
8411 if (is_watchpoint (&b) && breakpoint_enabled (&b))
c5aa993b 8412 {
a1decfc1 8413 b.enable_state = bp_call_disabled;
44702360 8414 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8415 }
c906108c
SS
8416}
8417
8418void
fba45db2 8419enable_watchpoints_after_interactive_call_stop (void)
c906108c 8420{
a1decfc1
SM
8421 for (breakpoint &b : all_breakpoints ())
8422 if (is_watchpoint (&b) && b.enable_state == bp_call_disabled)
c5aa993b 8423 {
a1decfc1 8424 b.enable_state = bp_enabled;
44702360 8425 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8426 }
c906108c
SS
8427}
8428
8bea4e01
UW
8429void
8430disable_breakpoints_before_startup (void)
8431{
6c95b8df 8432 current_program_space->executing_startup = 1;
44702360 8433 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8434}
8435
8436void
8437enable_breakpoints_after_startup (void)
8438{
6c95b8df 8439 current_program_space->executing_startup = 0;
f8eba3c6 8440 breakpoint_re_set ();
8bea4e01
UW
8441}
8442
7a3e3265
PA
8443/* Allocate a new momentary breakpoint. */
8444
7ab97995 8445template<typename... Arg>
7a3e3265 8446static momentary_breakpoint *
7ab97995
PA
8447new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8448 Arg&&... args)
7a3e3265
PA
8449{
8450 if (type == bp_longjmp || type == bp_exception)
7ab97995
PA
8451 return new longjmp_breakpoint (gdbarch, type,
8452 std::forward<Arg> (args)...);
7a3e3265 8453 else
7ab97995
PA
8454 return new momentary_breakpoint (gdbarch, type,
8455 std::forward<Arg> (args)...);
7a3e3265
PA
8456}
8457
7c16b83e
PA
8458/* Set a momentary breakpoint of type TYPE at address specified by
8459 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8460 frame. */
c906108c 8461
454dafbd 8462breakpoint_up
a6d9a66e
UW
8463set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8464 struct frame_id frame_id, enum bptype type)
c906108c 8465{
193facb3
JK
8466 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8467 tail-called one. */
8468 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8469
7a3e3265 8470 std::unique_ptr<momentary_breakpoint> b
7ab97995
PA
8471 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8472 inferior_thread ()->global_num));
7a3e3265
PA
8473
8474 b->add_location (sal);
c906108c 8475
7a3e3265
PA
8476 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8477
44702360 8478 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8479
7a3e3265 8480 return bp;
c906108c 8481}
611c83ae 8482
06edf0c0 8483/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8484 The new breakpoint will have type TYPE, use OPS as its
8485 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8486
06edf0c0
PA
8487static struct breakpoint *
8488momentary_breakpoint_from_master (struct breakpoint *orig,
8489 enum bptype type,
7ab97995
PA
8490 int loc_enabled,
8491 int thread)
e58b0e63 8492{
7a3e3265 8493 std::unique_ptr<breakpoint> copy
7ab97995
PA
8494 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8495 orig->frame_id, thread));
f5951b9f 8496 const bp_location &orig_loc = orig->first_loc ();
20afe380
SM
8497 bp_location *copy_loc = copy->allocate_location ();
8498 copy->add_location (*copy_loc);
8499 set_breakpoint_location_function (copy_loc);
8500
8501 copy_loc->gdbarch = orig_loc.gdbarch;
8502 copy_loc->requested_address = orig_loc.requested_address;
8503 copy_loc->address = orig_loc.address;
8504 copy_loc->section = orig_loc.section;
8505 copy_loc->pspace = orig_loc.pspace;
8506 copy_loc->probe = orig_loc.probe;
8507 copy_loc->line_number = orig_loc.line_number;
8508 copy_loc->symtab = orig_loc.symtab;
8509 copy_loc->enabled = loc_enabled;
e58b0e63 8510
7a3e3265 8511 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
44702360 8512 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7a3e3265 8513 return b;
e58b0e63
PA
8514}
8515
06edf0c0
PA
8516/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8517 ORIG is NULL. */
8518
8519struct breakpoint *
8520clone_momentary_breakpoint (struct breakpoint *orig)
8521{
8522 /* If there's nothing to clone, then return nothing. */
8523 if (orig == NULL)
8524 return NULL;
8525
7ab97995
PA
8526 return momentary_breakpoint_from_master (orig, orig->type, 0,
8527 orig->thread);
06edf0c0
PA
8528}
8529
454dafbd 8530breakpoint_up
a6d9a66e
UW
8531set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8532 enum bptype type)
611c83ae
PA
8533{
8534 struct symtab_and_line sal;
8535
8536 sal = find_pc_line (pc, 0);
8537 sal.pc = pc;
8538 sal.section = find_pc_overlay (pc);
8539 sal.explicit_pc = 1;
8540
a6d9a66e 8541 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8542}
c906108c 8543\f
c5aa993b 8544
c906108c
SS
8545/* Tell the user we have just set a breakpoint B. */
8546
8547static void
b713485d 8548mention (const breakpoint *b)
c906108c 8549{
c47614fe 8550 b->print_mention ();
2d33446d 8551 current_uiout->text ("\n");
c906108c 8552}
c906108c 8553\f
c5aa993b 8554
5133a315 8555static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8556
7f32a4d5
PA
8557/* Handle "set breakpoint auto-hw on".
8558
8559 If the explicitly specified breakpoint type is not hardware
8560 breakpoint, check the memory map to see whether the breakpoint
8561 address is in read-only memory.
8562
8563 - location type is not hardware breakpoint, memory is read-only.
8564 We change the type of the location to hardware breakpoint.
8565
8566 - location type is hardware breakpoint, memory is read-write. This
8567 means we've previously made the location hardware one, but then the
8568 memory map changed, so we undo.
8569*/
8570
8571static void
8572handle_automatic_hardware_breakpoints (bp_location *bl)
8573{
8574 if (automatic_hardware_breakpoints
8575 && bl->owner->type != bp_hardware_breakpoint
8576 && (bl->loc_type == bp_loc_software_breakpoint
8577 || bl->loc_type == bp_loc_hardware_breakpoint))
8578 {
8579 /* When breakpoints are removed, remove_breakpoints will use
8580 location types we've just set here, the only possible problem
8581 is that memory map has changed during running program, but
8582 it's not going to work anyway with current gdb. */
8583 mem_region *mr = lookup_mem_region (bl->address);
8584
8585 if (mr != nullptr)
8586 {
8587 enum bp_loc_type new_type;
8588
8589 if (mr->attrib.mode != MEM_RW)
8590 new_type = bp_loc_hardware_breakpoint;
8591 else
8592 new_type = bp_loc_software_breakpoint;
8593
8594 if (new_type != bl->loc_type)
8595 {
8596 static bool said = false;
8597
8598 bl->loc_type = new_type;
8599 if (!said)
8600 {
6cb06a8c
TT
8601 gdb_printf (_("Note: automatically using "
8602 "hardware breakpoints for "
8603 "read-only addresses.\n"));
7f32a4d5
PA
8604 said = true;
8605 }
8606 }
8607 }
8608 }
8609}
8610
06615628 8611bp_location *
74421c0b 8612code_breakpoint::add_location (const symtab_and_line &sal)
0d381245 8613{
3742cc8b 8614 CORE_ADDR adjusted_address;
06615628 8615 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
3742cc8b
YQ
8616
8617 if (loc_gdbarch == NULL)
06615628 8618 loc_gdbarch = gdbarch;
3742cc8b
YQ
8619
8620 /* Adjust the breakpoint's address prior to allocating a location.
ee89d0a4 8621 Once we call allocate_location(), that mostly uninitialized
3742cc8b
YQ
8622 location will be placed on the location chain. Adjustment of the
8623 breakpoint may cause target_read_memory() to be called and we do
8624 not want its scan of the location chain to find a breakpoint and
8625 location that's only been partially initialized. */
8626 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
60270718
AB
8627 sal.pc, type,
8628 sal.pspace);
0d381245 8629
d30113d4 8630 /* Sort the locations by their ADDRESS. */
20afe380 8631 bp_location *new_loc = this->allocate_location ();
06615628
PA
8632
8633 new_loc->requested_address = sal.pc;
8634 new_loc->address = adjusted_address;
8635 new_loc->pspace = sal.pspace;
8636 new_loc->probe.prob = sal.prob;
8637 new_loc->probe.objfile = sal.objfile;
8638 gdb_assert (new_loc->pspace != NULL);
8639 new_loc->section = sal.section;
8640 new_loc->gdbarch = loc_gdbarch;
8641 new_loc->line_number = sal.line;
8642 new_loc->symtab = sal.symtab;
8643 new_loc->symbol = sal.symbol;
8644 new_loc->msymbol = sal.msymbol;
06615628 8645
20afe380
SM
8646 breakpoint::add_location (*new_loc);
8647
06615628 8648 set_breakpoint_location_function (new_loc);
1a853c52 8649
6ae88661
LM
8650 /* While by definition, permanent breakpoints are already present in the
8651 code, we don't mark the location as inserted. Normally one would expect
8652 that GDB could rely on that breakpoint instruction to stop the program,
8653 thus removing the need to insert its own breakpoint, except that executing
8654 the breakpoint instruction can kill the target instead of reporting a
8655 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8656 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8657 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8658 breakpoint be inserted normally results in QEMU knowing about the GDB
8659 breakpoint, and thus trap before the breakpoint instruction is executed.
8660 (If GDB later needs to continue execution past the permanent breakpoint,
8661 it manually increments the PC, thus avoiding executing the breakpoint
8662 instruction.) */
06615628
PA
8663 if (bp_loc_is_permanent (new_loc))
8664 new_loc->permanent = 1;
1a853c52 8665
06615628 8666 return new_loc;
0d381245 8667}
514f746b
AR
8668\f
8669
5133a315
LM
8670/* Return true if LOC is pointing to a permanent breakpoint,
8671 return false otherwise. */
1cf4d951 8672
5133a315 8673static bool
1cf4d951
PA
8674bp_loc_is_permanent (struct bp_location *loc)
8675{
514f746b
AR
8676 gdb_assert (loc != NULL);
8677
cb1e4e32
PA
8678 /* If we have a non-breakpoint-backed catchpoint or a software
8679 watchpoint, just return 0. We should not attempt to read from
8680 the addresses the locations of these breakpoint types point to.
5133a315 8681 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8682 memory. */
cb1e4e32 8683 if (!bl_address_is_meaningful (loc))
5133a315 8684 return false;
244558af 8685
5ed8105e 8686 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8687 switch_to_program_space_and_thread (loc->pspace);
5133a315 8688 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8689}
8690
e7e0cddf
SS
8691/* Build a command list for the dprintf corresponding to the current
8692 settings of the dprintf style options. */
8693
8694static void
8695update_dprintf_command_list (struct breakpoint *b)
8696{
437d237a
AB
8697 gdb_assert (b->type == bp_dprintf);
8698 gdb_assert (b->extra_string != nullptr);
8699
6f781ee3 8700 const char *dprintf_args = b->extra_string.get ();
8579fd13 8701 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
e7e0cddf 8702
3d42db97
AB
8703 /* Trying to create a dprintf breakpoint without a format and args
8704 string should be detected at creation time. */
8705 gdb_assert (dprintf_args != nullptr);
e7e0cddf
SS
8706
8707 dprintf_args = skip_spaces (dprintf_args);
8708
8709 /* Allow a comma, as it may have terminated a location, but don't
8710 insist on it. */
8711 if (*dprintf_args == ',')
8712 ++dprintf_args;
8713 dprintf_args = skip_spaces (dprintf_args);
8714
8715 if (*dprintf_args != '"')
8716 error (_("Bad format string, missing '\"'."));
8717
d3ce09f5 8718 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8719 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8720 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf 8721 {
e0700ba4 8722 if (dprintf_function.empty ())
e7e0cddf
SS
8723 error (_("No function supplied for dprintf call"));
8724
e0700ba4 8725 if (!dprintf_channel.empty ())
e7e0cddf 8726 printf_line = xstrprintf ("call (void) %s (%s,%s)",
e0700ba4
SM
8727 dprintf_function.c_str (),
8728 dprintf_channel.c_str (),
e7e0cddf
SS
8729 dprintf_args);
8730 else
8731 printf_line = xstrprintf ("call (void) %s (%s)",
e0700ba4 8732 dprintf_function.c_str (),
e7e0cddf
SS
8733 dprintf_args);
8734 }
d3ce09f5
SS
8735 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8736 {
8737 if (target_can_run_breakpoint_commands ())
8738 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8739 else
8740 {
8741 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8742 printf_line = xstrprintf ("printf %s", dprintf_args);
8743 }
8744 }
e7e0cddf 8745 else
f34652de 8746 internal_error (_("Invalid dprintf style."));
e7e0cddf 8747
f28045c2 8748 gdb_assert (printf_line != NULL);
e7e0cddf 8749
12973681
TT
8750 /* Manufacture a printf sequence. */
8751 struct command_line *printf_cmd_line
8579fd13 8752 = new struct command_line (simple_control, printf_line.release ());
12973681
TT
8753 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8754 command_lines_deleter ()));
e7e0cddf
SS
8755}
8756
8757/* Update all dprintf commands, making their command lists reflect
8758 current style settings. */
8759
8760static void
eb4c3f4a 8761update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8762 struct cmd_list_element *c)
8763{
a1decfc1
SM
8764 for (breakpoint &b : all_breakpoints ())
8765 if (b.type == bp_dprintf)
8766 update_dprintf_command_list (&b);
e7e0cddf 8767}
c3f6f71d 8768
74421c0b 8769code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
3b003a61
PA
8770 enum bptype type_,
8771 gdb::array_view<const symtab_and_line> sals,
264f9890 8772 location_spec_up &&locspec_,
3b003a61
PA
8773 gdb::unique_xmalloc_ptr<char> filter_,
8774 gdb::unique_xmalloc_ptr<char> cond_string_,
8775 gdb::unique_xmalloc_ptr<char> extra_string_,
8776 enum bpdisp disposition_,
b080fe54
AB
8777 int thread_, int task_, int inferior_,
8778 int ignore_count_,
3b003a61
PA
8779 int from_tty,
8780 int enabled_, unsigned flags,
8781 int display_canonical_)
8782 : breakpoint (gdbarch_, type_)
018d34a4 8783{
0d381245 8784 int i;
018d34a4
VP
8785
8786 if (type == bp_hardware_breakpoint)
8787 {
fbbd034e
AS
8788 int target_resources_ok;
8789
8790 i = hw_breakpoint_used_count ();
8791 target_resources_ok =
8792 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8793 i + 1, 0);
8794 if (target_resources_ok == 0)
8795 error (_("No hardware breakpoint support in the target."));
8796 else if (target_resources_ok < 0)
8797 error (_("Hardware breakpoints used exceeds limit."));
8798 }
8799
6c5b2ebe 8800 gdb_assert (!sals.empty ());
6c95b8df 8801
b080fe54
AB
8802 /* At most one of thread, task, or inferior can be set on any breakpoint. */
8803 gdb_assert (((thread == -1 ? 0 : 1)
8804 + (task == -1 ? 0 : 1)
8805 + (inferior == -1 ? 0 : 1)) <= 1);
8806
3b003a61
PA
8807 thread = thread_;
8808 task = task_;
b080fe54 8809 inferior = inferior_;
0d381245 8810
3b003a61
PA
8811 cond_string = std::move (cond_string_);
8812 extra_string = std::move (extra_string_);
8813 ignore_count = ignore_count_;
8814 enable_state = enabled_ ? bp_enabled : bp_disabled;
8815 disposition = disposition_;
5af949e3 8816
3b003a61
PA
8817 if (type == bp_static_tracepoint
8818 || type == bp_static_marker_tracepoint)
8819 {
01bccc56 8820 auto *t = gdb::checked_static_cast<tracepoint *> (this);
3b003a61 8821 struct static_tracepoint_marker marker;
0d381245 8822
3b003a61 8823 if (strace_marker_p (this))
0d381245 8824 {
3b003a61
PA
8825 /* We already know the marker exists, otherwise, we wouldn't
8826 see a sal for it. */
709438c7 8827 const char *p = &locspec_->to_string ()[3];
3b003a61 8828 const char *endp;
0fb4aa4b 8829
3b003a61 8830 p = skip_spaces (p);
0fb4aa4b 8831
3b003a61 8832 endp = skip_to_space (p);
0fb4aa4b 8833
3b003a61 8834 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8835
3b003a61
PA
8836 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8837 t->static_trace_marker_id.c_str ());
8838 }
8839 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8840 {
8841 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8842
3b003a61
PA
8843 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8844 t->static_trace_marker_id.c_str ());
0d381245
VP
8845 }
8846 else
3b003a61
PA
8847 warning (_("Couldn't determine the static tracepoint marker to probe"));
8848 }
8849
8850 for (const auto &sal : sals)
8851 {
8852 if (from_tty)
018d34a4 8853 {
3b003a61
PA
8854 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8855 if (loc_gdbarch == nullptr)
8856 loc_gdbarch = gdbarch;
8857
8858 describe_other_breakpoints (loc_gdbarch,
8859 sal.pspace, sal.pc, sal.section, thread);
0d381245
VP
8860 }
8861
3b003a61
PA
8862 bp_location *new_loc = add_location (sal);
8863 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8864 new_loc->inserted = 1;
8865
b5fa468f
TBA
8866 /* Do not set breakpoint locations conditions yet. As locations
8867 are inserted, they get sorted based on their addresses. Let
8868 the list stabilize to have reliable location numbers. */
855a6e68 8869 }
018d34a4 8870
8b4141cd
AB
8871 /* Dynamic printf requires and uses additional arguments on the
8872 command line, otherwise it's an error. */
8873 if (type == bp_dprintf)
8874 update_dprintf_command_list (this);
c6b48675
AB
8875 else
8876 gdb_assert (extra_string == nullptr);
8b4141cd 8877
b5fa468f
TBA
8878 /* The order of the locations is now stable. Set the location
8879 condition using the location's number. */
8880 int loc_num = 1;
b00b30b2 8881 for (bp_location &bl : locations ())
b5fa468f 8882 {
3b003a61 8883 if (cond_string != nullptr)
b00b30b2 8884 set_breakpoint_location_condition (cond_string.get (), &bl,
3b003a61 8885 number, loc_num);
b5fa468f
TBA
8886
8887 ++loc_num;
8888 }
8889
3b003a61 8890 display_canonical = display_canonical_;
264f9890
PA
8891 if (locspec_ != nullptr)
8892 locspec = std::move (locspec_);
018d34a4 8893 else
f5951b9f 8894 locspec = new_address_location_spec (this->first_loc ().address, NULL, 0);
3b003a61 8895 filter = std::move (filter_);
d9b3f62e 8896}
018d34a4 8897
d9b3f62e
PA
8898static void
8899create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8900 gdb::array_view<const symtab_and_line> sals,
264f9890 8901 location_spec_up &&locspec,
e1e01040
PA
8902 gdb::unique_xmalloc_ptr<char> filter,
8903 gdb::unique_xmalloc_ptr<char> cond_string,
8904 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e 8905 enum bptype type, enum bpdisp disposition,
b080fe54 8906 int thread, int task, int inferior, int ignore_count,
ef4848c7 8907 int from_tty,
44f238bb
PA
8908 int enabled, int internal, unsigned flags,
8909 int display_canonical)
d9b3f62e 8910{
74421c0b 8911 std::unique_ptr<code_breakpoint> b
3b003a61
PA
8912 = new_breakpoint_from_type (gdbarch,
8913 type,
8914 sals,
264f9890 8915 std::move (locspec),
3b003a61
PA
8916 std::move (filter),
8917 std::move (cond_string),
8918 std::move (extra_string),
8919 disposition,
b080fe54 8920 thread, task, inferior, ignore_count,
3b003a61
PA
8921 from_tty,
8922 enabled, flags,
8923 display_canonical);
d9b3f62e 8924
b270e6f9 8925 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8926}
8927
8928/* Add SALS.nelts breakpoints to the breakpoint table. For each
8929 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8930 value. COND_STRING, if not NULL, specified the condition to be
8931 used for all breakpoints. Essentially the only case where
8932 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8933 function. In that case, it's still not possible to specify
8934 separate conditions for different overloaded functions, so
8935 we take just a single condition string.
8936
c3f6f71d 8937 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8938 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8939 array contents). If the function fails (error() is called), the
8940 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8941 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8942
8943static void
8cdf0e15 8944create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8945 struct linespec_result *canonical,
e1e01040
PA
8946 gdb::unique_xmalloc_ptr<char> cond_string,
8947 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15 8948 enum bptype type, enum bpdisp disposition,
b080fe54
AB
8949 int thread, int task, int inferior,
8950 int ignore_count,
ef4848c7 8951 int from_tty,
44f238bb 8952 int enabled, int internal, unsigned flags)
c906108c 8953{
f8eba3c6 8954 if (canonical->pre_expanded)
6c5b2ebe 8955 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8956
6c5b2ebe 8957 for (const auto &lsal : canonical->lsals)
c3f6f71d 8958 {
f00aae0f 8959 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8960 'break', without arguments. */
264f9890 8961 location_spec_up locspec
5c1ddcb6
PA
8962 = (canonical->locspec != nullptr
8963 ? canonical->locspec->clone ()
8964 : nullptr);
e1e01040 8965 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8966 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8967
6c5b2ebe 8968 create_breakpoint_sal (gdbarch, lsal.sals,
264f9890 8969 std::move (locspec),
e1e01040
PA
8970 std::move (filter_string),
8971 std::move (cond_string),
8972 std::move (extra_string),
e7e0cddf 8973 type, disposition,
b080fe54 8974 thread, task, inferior, ignore_count,
44f238bb 8975 from_tty, enabled, internal, flags,
56435ebe 8976 canonical->special_display);
c3f6f71d 8977 }
c3f6f71d 8978}
c906108c 8979
264f9890 8980/* Parse LOCSPEC which is assumed to be a SAL specification possibly
c3f6f71d 8981 followed by conditionals. On return, SALS contains an array of SAL
264f9890
PA
8982 addresses found. LOCSPEC points to the end of the SAL (for
8983 linespec locspecs).
9998af43
TJB
8984
8985 The array and the line spec strings are allocated on the heap, it is
8986 the caller's responsibility to free them. */
c906108c 8987
b9362cc7 8988static void
264f9890 8989parse_breakpoint_sals (location_spec *locspec,
6cce0251
AB
8990 struct linespec_result *canonical,
8991 struct program_space *search_pspace)
c3f6f71d 8992{
7464aeaa 8993 if (locspec->type () == LINESPEC_LOCATION_SPEC)
f00aae0f 8994 {
e7cdec66
TT
8995 const char *spec
8996 = as_linespec_location_spec (locspec)->spec_string.get ();
f00aae0f 8997
a20714ff 8998 if (spec == NULL)
f00aae0f
KS
8999 {
9000 /* The last displayed codepoint, if it's valid, is our default
9001 breakpoint address. */
9002 if (last_displayed_sal_is_valid ())
9003 {
f00aae0f
KS
9004 /* Set sal's pspace, pc, symtab, and line to the values
9005 corresponding to the last call to print_frame_info.
9006 Be sure to reinitialize LINE with NOTCURRENT == 0
9007 as the breakpoint line number is inappropriate otherwise.
9008 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9009 symtab_and_line sal = get_last_displayed_sal ();
9010 CORE_ADDR pc = sal.pc;
9011
f00aae0f
KS
9012 sal = find_pc_line (pc, 0);
9013
9014 /* "break" without arguments is equivalent to "break *PC"
9015 where PC is the last displayed codepoint's address. So
9016 make sure to set sal.explicit_pc to prevent GDB from
9017 trying to expand the list of sals to include all other
9018 instances with the same symtab and line. */
9019 sal.pc = pc;
9020 sal.explicit_pc = 1;
9021
6c5b2ebe
PA
9022 struct linespec_sals lsal;
9023 lsal.sals = {sal};
f00aae0f
KS
9024 lsal.canonical = NULL;
9025
6c5b2ebe 9026 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9027 return;
9028 }
9029 else
9030 error (_("No default breakpoint address now."));
c906108c 9031 }
c906108c 9032 }
f00aae0f
KS
9033
9034 /* Force almost all breakpoints to be in terms of the
9035 current_source_symtab (which is decode_line_1's default).
9036 This should produce the results we want almost all of the
9037 time while leaving default_breakpoint_* alone.
9038
9039 ObjC: However, don't match an Objective-C method name which
9040 may have a '+' or '-' succeeded by a '['. */
3bae94c0
SM
9041 symtab_and_line cursal
9042 = get_current_source_symtab_and_line (current_program_space);
f00aae0f 9043 if (last_displayed_sal_is_valid ())
c906108c 9044 {
a20714ff 9045 const char *spec = NULL;
cc80f267 9046
7464aeaa 9047 if (locspec->type () == LINESPEC_LOCATION_SPEC)
e7cdec66 9048 spec = as_linespec_location_spec (locspec)->spec_string.get ();
cc80f267 9049
f00aae0f 9050 if (!cursal.symtab
a20714ff
PA
9051 || (spec != NULL
9052 && strchr ("+-", spec[0]) != NULL
9053 && spec[1] != '['))
f00aae0f 9054 {
6cce0251 9055 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
f00aae0f
KS
9056 get_last_displayed_symtab (),
9057 get_last_displayed_line (),
9058 canonical, NULL, NULL);
9059 return;
9060 }
c906108c 9061 }
f00aae0f 9062
6cce0251 9063 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
f00aae0f 9064 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9065}
c906108c 9066
c906108c 9067
c3f6f71d 9068/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9069 inserted as a breakpoint. If it can't throw an error. */
c906108c 9070
b9362cc7 9071static void
6c5b2ebe 9072breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9073{
6c5b2ebe
PA
9074 for (auto &sal : sals)
9075 resolve_sal_pc (&sal);
c3f6f71d
JM
9076}
9077
7a697b8d
SS
9078/* Fast tracepoints may have restrictions on valid locations. For
9079 instance, a fast tracepoint using a jump instead of a trap will
9080 likely have to overwrite more bytes than a trap would, and so can
9081 only be placed where the instruction is longer than the jump, or a
9082 multi-instruction sequence does not have a jump into the middle of
9083 it, etc. */
9084
9085static void
9086check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9087 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9088{
6c5b2ebe 9089 for (const auto &sal : sals)
7a697b8d 9090 {
f8eba3c6
TT
9091 struct gdbarch *sarch;
9092
6c5b2ebe 9093 sarch = get_sal_arch (sal);
f8eba3c6
TT
9094 /* We fall back to GDBARCH if there is no architecture
9095 associated with SAL. */
9096 if (sarch == NULL)
9097 sarch = gdbarch;
281d762b
TT
9098 std::string msg;
9099 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9100 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9101 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9102 }
9103}
9104
0fb4aa4b
PA
9105/* Decode a static tracepoint marker spec. */
9106
6c5b2ebe 9107static std::vector<symtab_and_line>
f00aae0f 9108decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9109{
f00aae0f
KS
9110 const char *p = &(*arg_p)[3];
9111 const char *endp;
0fb4aa4b 9112
f1735a53 9113 p = skip_spaces (p);
0fb4aa4b 9114
f1735a53 9115 endp = skip_to_space (p);
0fb4aa4b 9116
81b1e71c 9117 std::string marker_str (p, endp - p);
0fb4aa4b 9118
5d9310c4
SM
9119 std::vector<static_tracepoint_marker> markers
9120 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9121 if (markers.empty ())
81b1e71c
TT
9122 error (_("No known static tracepoint marker named %s"),
9123 marker_str.c_str ());
0fb4aa4b 9124
6c5b2ebe 9125 std::vector<symtab_and_line> sals;
5d9310c4 9126 sals.reserve (markers.size ());
0fb4aa4b 9127
5d9310c4 9128 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9129 {
5d9310c4
SM
9130 symtab_and_line sal = find_pc_line (marker.address, 0);
9131 sal.pc = marker.address;
6c5b2ebe 9132 sals.push_back (sal);
5d9310c4 9133 }
0fb4aa4b 9134
0fb4aa4b
PA
9135 *arg_p = endp;
9136 return sals;
9137}
9138
bac7c5cf
GB
9139/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9140 according to IS_TRACEPOINT. */
9141
9142static const struct breakpoint_ops *
264f9890
PA
9143breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
9144 bool is_tracepoint)
bac7c5cf
GB
9145{
9146 if (is_tracepoint)
9147 {
264f9890 9148 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
9149 return &tracepoint_probe_breakpoint_ops;
9150 else
74421c0b 9151 return &code_breakpoint_ops;
bac7c5cf
GB
9152 }
9153 else
9154 {
264f9890 9155 if (locspec_type == PROBE_LOCATION_SPEC)
bac7c5cf
GB
9156 return &bkpt_probe_breakpoint_ops;
9157 else
74421c0b 9158 return &code_breakpoint_ops;
bac7c5cf
GB
9159 }
9160}
9161
6cce0251
AB
9162/* Return the program space to use as a filter when searching for locations
9163 of a breakpoint specific to THREAD or INFERIOR. If THREAD and INFERIOR
9164 are both -1, meaning all threads/inferiors, then this function returns
9165 nullptr, indicating no program space filtering should be performed.
9166 Otherwise, this function returns the program space for the inferior that
9167 contains THREAD (when THREAD is not -1), or the program space for
9168 INFERIOR (when INFERIOR is not -1). */
9169
9170static struct program_space *
9171find_program_space_for_breakpoint (int thread, int inferior)
9172{
9173 if (thread != -1)
9174 {
9175 gdb_assert (inferior == -1);
9176
9177 struct thread_info *thr = find_thread_global_id (thread);
9178 gdb_assert (thr != nullptr);
9179 gdb_assert (thr->inf != nullptr);
9180 return thr->inf->pspace;
9181 }
9182 else if (inferior != -1)
9183 {
9184 gdb_assert (thread == -1);
9185
9186 struct inferior *inf = find_inferior_id (inferior);
9187 gdb_assert (inf != nullptr);
9188
9189 return inf->pspace;
9190 }
9191
9192 return nullptr;
9193}
9194
bac7c5cf
GB
9195/* See breakpoint.h. */
9196
9197const struct breakpoint_ops *
264f9890
PA
9198breakpoint_ops_for_location_spec (const location_spec *locspec,
9199 bool is_tracepoint)
bac7c5cf 9200{
264f9890
PA
9201 if (locspec != nullptr)
9202 return (breakpoint_ops_for_location_spec_type
7464aeaa 9203 (locspec->type (), is_tracepoint));
74421c0b 9204 return &code_breakpoint_ops;
bac7c5cf
GB
9205}
9206
f00aae0f 9207/* See breakpoint.h. */
0101ce28 9208
8cdf0e15
VP
9209int
9210create_breakpoint (struct gdbarch *gdbarch,
264f9890 9211 location_spec *locspec,
e1e01040 9212 const char *cond_string,
b080fe54
AB
9213 int thread, int inferior,
9214 const char *extra_string,
10a636cc 9215 bool force_condition, int parse_extra,
0fb4aa4b 9216 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9217 int ignore_count,
9218 enum auto_boolean pending_break_support,
c0a91b2b 9219 const struct breakpoint_ops *ops,
44f238bb
PA
9220 int from_tty, int enabled, int internal,
9221 unsigned flags)
c3f6f71d 9222{
7efd8fc2 9223 struct linespec_result canonical;
d8de7963 9224 bool pending = false;
2ecee236 9225 int task = -1;
86b17b60 9226 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9227
b080fe54
AB
9228 gdb_assert (thread == -1 || thread > 0);
9229 gdb_assert (inferior == -1 || inferior > 0);
9230 gdb_assert (thread == -1 || inferior == -1);
9231
32f5a989
AB
9232 /* If PARSE_EXTRA is true then the thread and inferior details will be
9233 parsed from the EXTRA_STRING, the THREAD and INFERIOR arguments
9234 should be -1. */
9235 gdb_assert (!parse_extra || thread == -1);
9236 gdb_assert (!parse_extra || inferior == -1);
9237
348d480f
PA
9238 gdb_assert (ops != NULL);
9239
f00aae0f
KS
9240 /* If extra_string isn't useful, set it to NULL. */
9241 if (extra_string != NULL && *extra_string == '\0')
9242 extra_string = NULL;
9243
ea020765
AB
9244 /* A bp_dprintf must always have an accompanying EXTRA_STRING containing
9245 the dprintf format and arguments -- PARSE_EXTRA should always be false
9246 in this case.
9247
9248 For all other breakpoint types, EXTRA_STRING should be nullptr unless
9249 PARSE_EXTRA is true. */
9250 gdb_assert ((type_wanted == bp_dprintf)
9251 ? (extra_string != nullptr && !parse_extra)
9252 : (extra_string == nullptr || parse_extra));
9253
c6b48675
AB
9254 /* Will hold either copies of the similarly named function argument, or
9255 will hold a modified version of the function argument, depending on
9256 the value of PARSE_EXTRA. */
9257 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9258 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9259
9260 if (parse_extra)
9261 {
9262 /* Parse EXTRA_STRING splitting the parts out. */
9263 create_breakpoint_parse_arg_string (extra_string, &cond_string_copy,
9264 &thread, &inferior, &task,
9265 &extra_string_copy,
9266 &force_condition);
9267
9268 /* We could check that EXTRA_STRING_COPY is empty at this point -- it
9269 should be, as we only get here for things that are not bp_dprintf,
9270 however, we prefer to give the location spec parser a chance to
9271 run first, this means the user will get errors about invalid
9272 location spec instead of an error about garbage at the end of the
9273 command line.
9274
9275 We still do the EXTRA_STRING_COPY is empty check, just later in
9276 this function. */
9277
9278 gdb_assert (thread == -1 || thread > 0);
9279 gdb_assert (task == -1 || task > 0);
9280 gdb_assert (inferior == -1 || inferior > 0);
9281 }
9282 else
9283 {
9284 if (cond_string != nullptr)
9285 cond_string_copy.reset (xstrdup (cond_string));
9286 if (extra_string != nullptr)
9287 extra_string_copy.reset (xstrdup (extra_string));
9288 }
9289
9290 /* Clear these. Updated values are now held in the *_copy locals. */
9291 cond_string = nullptr;
9292 extra_string = nullptr;
9293
a70b8144 9294 try
b78a6381 9295 {
6cce0251
AB
9296 struct program_space *search_pspace
9297 = find_program_space_for_breakpoint (thread, inferior);
9298 ops->create_sals_from_location_spec (locspec, &canonical,
9299 search_pspace);
b78a6381 9300 }
230d2906 9301 catch (const gdb_exception_error &e)
0101ce28 9302 {
492d29ea
PA
9303 /* If caller is interested in rc value from parse, set
9304 value. */
9305 if (e.error == NOT_FOUND_ERROR)
0101ce28 9306 {
05ff989b
AC
9307 /* If pending breakpoint support is turned off, throw
9308 error. */
fa8d40ab
JJ
9309
9310 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9311 throw;
723a2275
VP
9312
9313 exception_print (gdb_stderr, e);
fa8d40ab 9314
dda83cd7 9315 /* If pending breakpoint support is auto query and the user
05ff989b 9316 selects no, then simply return the error code. */
059fb39f 9317 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9318 && !nquery (_("Make %s pending on future shared library load? "),
9319 bptype_string (type_wanted)))
fd9b8c24 9320 return 0;
fa8d40ab 9321
05ff989b
AC
9322 /* At this point, either the user was queried about setting
9323 a pending breakpoint and selected yes, or pending
9324 breakpoint behavior is on and thus a pending breakpoint
9325 is defaulted on behalf of the user. */
d8de7963 9326 pending = true;
0101ce28 9327 }
492d29ea 9328 else
eedc3f4f 9329 throw;
0101ce28 9330 }
492d29ea 9331
c6b48675
AB
9332 /* Only bp_dprintf breakpoints should have anything in EXTRA_STRING_COPY
9333 by this point. For all other breakpoints this indicates an error. We
9334 could place this check earlier in the function, but we prefer to see
9335 errors from the location spec parser before we see this error message. */
9336 if (type_wanted != bp_dprintf && extra_string_copy.get () != nullptr)
9337 error (_("Garbage '%s' at end of command"), extra_string_copy.get ());
9338
6c5b2ebe 9339 if (!pending && canonical.lsals.empty ())
492d29ea 9340 return 0;
c3f6f71d 9341
c3f6f71d
JM
9342 /* Resolve all line numbers to PC's and verify that the addresses
9343 are ok for the target. */
0101ce28 9344 if (!pending)
f8eba3c6 9345 {
6c5b2ebe
PA
9346 for (auto &lsal : canonical.lsals)
9347 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9348 }
c3f6f71d 9349
7a697b8d 9350 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9351 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9352 {
6c5b2ebe
PA
9353 for (const auto &lsal : canonical.lsals)
9354 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9355 }
7a697b8d 9356
c3f6f71d
JM
9357 /* Verify that condition can be parsed, before setting any
9358 breakpoints. Allocate a separate condition expression for each
4a64f543 9359 breakpoint. */
0101ce28 9360 if (!pending)
c3f6f71d 9361 {
c6b48675
AB
9362 /* Check the validity of the condition. We should error out if the
9363 condition is invalid at all of the locations and if it is not
9364 forced. In the PARSE_EXTRA case above, this check is done when
9365 parsing the EXTRA_STRING. */
9366 if (cond_string_copy.get () != nullptr && !force_condition)
dda83cd7 9367 {
c6b48675 9368 int num_failures = 0;
6c5b2ebe 9369 const linespec_sals &lsal = canonical.lsals[0];
c6b48675 9370 for (const auto &sal : lsal.sals)
10a636cc 9371 {
c6b48675
AB
9372 const char *cond = cond_string_copy.get ();
9373 try
10a636cc 9374 {
c6b48675
AB
9375 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9376 /* One success is sufficient to keep going. */
9377 break;
9378 }
9379 catch (const gdb_exception_error &)
9380 {
9381 num_failures++;
9382 /* If this is the last sal, error out. */
9383 if (num_failures == lsal.sals.size ())
9384 throw;
10a636cc
TBA
9385 }
9386 }
dda83cd7 9387 }
0fb4aa4b 9388
52d361e1 9389 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9390 std::move (cond_string_copy),
9391 std::move (extra_string_copy),
9392 type_wanted,
d9b3f62e 9393 tempflag ? disp_del : disp_donttouch,
b080fe54 9394 thread, task, inferior, ignore_count,
44f238bb 9395 from_tty, enabled, internal, flags);
c906108c 9396 }
0101ce28
JJ
9397 else
9398 {
73063f51
TT
9399 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9400 type_wanted);
5c1ddcb6 9401 b->locspec = locspec->clone ();
bfccc43c 9402
c6b48675
AB
9403 /* Create a private copy of the condition string. */
9404 b->cond_string = std::move (cond_string_copy);
9405
9406 b->thread = thread;
9407 b->task = task;
9408 b->inferior = inferior;
f00aae0f
KS
9409
9410 /* Create a private copy of any extra string. */
c6b48675
AB
9411 b->extra_string = std::move (extra_string_copy);
9412
0101ce28 9413 b->ignore_count = ignore_count;
0101ce28 9414 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9415 b->condition_not_parsed = 1;
41447f92 9416 b->enable_state = enabled ? bp_enabled : bp_disabled;
8bea4e01 9417
c6b48675
AB
9418 if (b->type == bp_dprintf)
9419 update_dprintf_command_list (b.get ());
9420
b270e6f9 9421 install_breakpoint (internal, std::move (b), 0);
0101ce28 9422 }
c6b48675 9423
6c5b2ebe 9424 if (canonical.lsals.size () > 1)
95a42b64 9425 {
3e43a32a 9426 warning (_("Multiple breakpoints were set.\nUse the "
9e69a2e1
TT
9427 "\"%ps\" command to delete unwanted breakpoints."),
9428 styled_string (command_style.style (), "delete"));
86b17b60 9429 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9430 }
9431
44702360 9432 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9433
9434 return 1;
c3f6f71d 9435}
c906108c 9436
348d480f 9437/* Set a breakpoint.
72b2ff0e
VP
9438 ARG is a string describing breakpoint address,
9439 condition, and thread.
9440 FLAG specifies if a breakpoint is hardware on,
9441 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9442 and BP_TEMPFLAG. */
348d480f 9443
98deb0da 9444static void
f2fc3015 9445break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9446{
72b2ff0e 9447 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9448 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9449 ? bp_hardware_breakpoint
9450 : bp_breakpoint);
f00aae0f 9451
264f9890
PA
9452 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9453 const struct breakpoint_ops *ops
9454 = breakpoint_ops_for_location_spec (locspec.get (),
9455 false /* is_tracepoint */);
c3f6f71d 9456
8cdf0e15 9457 create_breakpoint (get_current_arch (),
264f9890 9458 locspec.get (),
b080fe54
AB
9459 NULL,
9460 -1 /* thread */, -1 /* inferior */,
9461 arg, false, 1 /* parse arg */,
0fb4aa4b 9462 tempflag, type_wanted,
8cdf0e15
VP
9463 0 /* Ignore count */,
9464 pending_break_support,
55aa24fb 9465 ops,
8cdf0e15 9466 from_tty,
84f4c1fe 9467 1 /* enabled */,
44f238bb
PA
9468 0 /* internal */,
9469 0);
c906108c
SS
9470}
9471
c906108c
SS
9472/* Helper function for break_command_1 and disassemble_command. */
9473
9474void
fba45db2 9475resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9476{
9477 CORE_ADDR pc;
9478
9479 if (sal->pc == 0 && sal->symtab != NULL)
9480 {
9481 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9482 error (_("No line %d in file \"%s\"."),
05cba821 9483 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9484 sal->pc = pc;
6a048695 9485
4a64f543 9486 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9487 number, then skip the function prologue if necessary. */
6a048695 9488 if (sal->explicit_line)
059acae7 9489 skip_prologue_sal (sal);
c906108c
SS
9490 }
9491
9492 if (sal->section == 0 && sal->symtab != NULL)
9493 {
346d1dfe 9494 const struct blockvector *bv;
3977b71f 9495 const struct block *b;
c5aa993b 9496 struct symbol *sym;
c906108c 9497
43f3e411 9498 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
c6159652 9499 sal->symtab->compunit ());
c906108c
SS
9500 if (bv != NULL)
9501 {
3c9d0506 9502 sym = b->linkage_function ();
c906108c 9503 if (sym != NULL)
dae58e04
TT
9504 sal->section
9505 = sym->obj_section (sal->symtab->compunit ()->objfile ());
c906108c
SS
9506 else
9507 {
4a64f543 9508 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9509 just have to look harder. This case can be executed
9510 if we have line numbers but no functions (as can
9511 happen in assembly source). */
c906108c 9512
5ed8105e 9513 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9514 switch_to_program_space_and_thread (sal->pspace);
c906108c 9515
5ed8105e 9516 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9517 if (msym.minsym)
1db66e34 9518 sal->section = msym.obj_section ();
c906108c
SS
9519 }
9520 }
9521 }
9522}
9523
9524void
0b39b52e 9525break_command (const char *arg, int from_tty)
c906108c 9526{
db107f19 9527 break_command_1 (arg, 0, from_tty);
c906108c
SS
9528}
9529
c906108c 9530void
0b39b52e 9531tbreak_command (const char *arg, int from_tty)
c906108c 9532{
db107f19 9533 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9534}
9535
c906108c 9536static void
0b39b52e 9537hbreak_command (const char *arg, int from_tty)
c906108c 9538{
db107f19 9539 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9540}
9541
9542static void
0b39b52e 9543thbreak_command (const char *arg, int from_tty)
c906108c 9544{
db107f19 9545 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9546}
9547
e7e0cddf
SS
9548/* The dynamic printf command is mostly like a regular breakpoint, but
9549 with a prewired command list consisting of a single output command,
9550 built from extra arguments supplied on the dprintf command
9551 line. */
9552
da821c7b 9553static void
0b39b52e 9554dprintf_command (const char *arg, int from_tty)
e7e0cddf 9555{
264f9890 9556 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f
KS
9557
9558 /* If non-NULL, ARG should have been advanced past the location;
9559 the next character must be ','. */
ea020765
AB
9560 if (arg == nullptr || arg[0] != ',' || arg[1] == '\0')
9561 error (_("Format string required"));
9562 else
f00aae0f 9563 {
ea020765
AB
9564 /* Skip the comma. */
9565 ++arg;
f00aae0f
KS
9566 }
9567
e7e0cddf 9568 create_breakpoint (get_current_arch (),
264f9890 9569 locspec.get (),
b080fe54 9570 NULL, -1, -1,
ea020765 9571 arg, false, 0 /* parse arg */,
e7e0cddf
SS
9572 0, bp_dprintf,
9573 0 /* Ignore count */,
9574 pending_break_support,
74421c0b 9575 &code_breakpoint_ops,
e7e0cddf
SS
9576 from_tty,
9577 1 /* enabled */,
9578 0 /* internal */,
9579 0);
9580}
9581
d3ce09f5 9582static void
0b39b52e 9583agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9584{
9585 error (_("May only run agent-printf on the target"));
9586}
9587
ec45bb67 9588/* Implement the "breakpoint_hit" method for ranged breakpoints. */
f1310107 9589
ec45bb67
TT
9590int
9591ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9592 const address_space *aspace,
9593 CORE_ADDR bp_addr,
9594 const target_waitstatus &ws)
f1310107 9595{
c272a98c
SM
9596 if (ws.kind () != TARGET_WAITKIND_STOPPED
9597 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
9598 return 0;
9599
f9582a22
TV
9600 return breakpoint_address_match_range (bl->pspace->aspace.get (),
9601 bl->address, bl->length, aspace,
9602 bp_addr);
f1310107
TJB
9603}
9604
ec45bb67 9605/* Implement the "resources_needed" method for ranged breakpoints. */
f1310107 9606
ec45bb67
TT
9607int
9608ranged_breakpoint::resources_needed (const struct bp_location *bl)
f1310107
TJB
9609{
9610 return target_ranged_break_num_registers ();
9611}
9612
ec45bb67 9613/* Implement the "print_it" method for ranged breakpoints. */
f1310107 9614
ec45bb67 9615enum print_stop_action
7bd86313 9616ranged_breakpoint::print_it (const bpstat *bs) const
f1310107 9617{
79a45e25 9618 struct ui_out *uiout = current_uiout;
f1310107 9619
ec45bb67 9620 gdb_assert (type == bp_hardware_breakpoint);
f1310107
TJB
9621
9622 /* Ranged breakpoints have only one location. */
9dc1523b 9623 gdb_assert (this->has_single_location ());
f1310107 9624
ec45bb67 9625 annotate_breakpoint (number);
f303dbd6
PA
9626
9627 maybe_print_thread_hit_breakpoint (uiout);
9628
ec45bb67 9629 if (disposition == disp_del)
112e8700 9630 uiout->text ("Temporary ranged breakpoint ");
f1310107 9631 else
112e8700
SM
9632 uiout->text ("Ranged breakpoint ");
9633 if (uiout->is_mi_like_p ())
f1310107 9634 {
112e8700 9635 uiout->field_string ("reason",
f1310107 9636 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
ec45bb67 9637 uiout->field_string ("disp", bpdisp_text (disposition));
f1310107 9638 }
78805ff8 9639 print_num_locno (bs, uiout);
112e8700 9640 uiout->text (", ");
f1310107
TJB
9641
9642 return PRINT_SRC_AND_LOC;
9643}
9644
ec45bb67 9645/* Implement the "print_one" method for ranged breakpoints. */
f1310107 9646
ec45bb67 9647bool
5e632eca 9648ranged_breakpoint::print_one (const bp_location **last_loc) const
f1310107 9649{
f1310107 9650 struct value_print_options opts;
79a45e25 9651 struct ui_out *uiout = current_uiout;
f1310107
TJB
9652
9653 /* Ranged breakpoints have only one location. */
9dc1523b 9654 gdb_assert (this->has_single_location ());
f1310107
TJB
9655
9656 get_user_print_options (&opts);
9657
9658 if (opts.addressprint)
9659 /* We don't print the address range here, it will be printed later
f7968895 9660 by ranged_breakpoint::print_one_detail. */
112e8700 9661 uiout->field_skip ("addr");
f1310107 9662 annotate_field (5);
f5951b9f
SM
9663 print_breakpoint_location (this, &this->first_loc ());
9664 *last_loc = &this->first_loc ();
c01e038b
TT
9665
9666 return true;
f1310107
TJB
9667}
9668
ec45bb67 9669/* Implement the "print_one_detail" method for ranged breakpoints. */
f1310107 9670
ec45bb67
TT
9671void
9672ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
f1310107
TJB
9673{
9674 CORE_ADDR address_start, address_end;
f5951b9f 9675 const bp_location &bl = this->first_loc ();
d7e74731 9676 string_file stb;
f1310107 9677
f5951b9f
SM
9678 address_start = bl.address;
9679 address_end = address_start + bl.length - 1;
f1310107 9680
112e8700 9681 uiout->text ("\taddress range: ");
d7e74731 9682 stb.printf ("[%s, %s]",
f5951b9f
SM
9683 print_core_address (bl.gdbarch, address_start),
9684 print_core_address (bl.gdbarch, address_end));
112e8700
SM
9685 uiout->field_stream ("addr", stb);
9686 uiout->text ("\n");
f1310107
TJB
9687}
9688
ec45bb67 9689/* Implement the "print_mention" method for ranged breakpoints. */
f1310107 9690
ec45bb67 9691void
b713485d 9692ranged_breakpoint::print_mention () const
f1310107 9693{
f5951b9f 9694 const bp_location &bl = this->first_loc ();
79a45e25 9695 struct ui_out *uiout = current_uiout;
f1310107 9696
ec45bb67 9697 gdb_assert (type == bp_hardware_breakpoint);
f1310107 9698
2d33446d 9699 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
f5951b9f
SM
9700 number, paddress (bl.gdbarch, bl.address),
9701 paddress (bl.gdbarch, bl.address + bl.length - 1));
f1310107
TJB
9702}
9703
ec45bb67 9704/* Implement the "print_recreate" method for ranged breakpoints. */
f1310107 9705
ec45bb67 9706void
4d1ae558 9707ranged_breakpoint::print_recreate (struct ui_file *fp) const
f1310107 9708{
6cb06a8c 9709 gdb_printf (fp, "break-range %s, %s",
709438c7
PA
9710 locspec->to_string (),
9711 locspec_range_end->to_string ());
04d0163c 9712 print_recreate_thread (fp);
f1310107
TJB
9713}
9714
f1310107
TJB
9715/* Find the address where the end of the breakpoint range should be
9716 placed, given the SAL of the end of the range. This is so that if
9717 the user provides a line number, the end of the range is set to the
9718 last instruction of the given line. */
9719
9720static CORE_ADDR
9721find_breakpoint_range_end (struct symtab_and_line sal)
9722{
9723 CORE_ADDR end;
9724
9725 /* If the user provided a PC value, use it. Otherwise,
9726 find the address of the end of the given location. */
9727 if (sal.explicit_pc)
9728 end = sal.pc;
9729 else
9730 {
9731 int ret;
9732 CORE_ADDR start;
9733
9734 ret = find_line_pc_range (sal, &start, &end);
9735 if (!ret)
9736 error (_("Could not find location of the end of the range."));
9737
9738 /* find_line_pc_range returns the start of the next line. */
9739 end--;
9740 }
9741
9742 return end;
9743}
9744
9745/* Implement the "break-range" CLI command. */
9746
9747static void
0b39b52e 9748break_range_command (const char *arg, int from_tty)
f1310107 9749{
f2fc3015 9750 const char *arg_start;
f1310107
TJB
9751 struct linespec_result canonical_start, canonical_end;
9752 int bp_count, can_use_bp, length;
9753 CORE_ADDR end;
f1310107
TJB
9754
9755 /* We don't support software ranged breakpoints. */
9756 if (target_ranged_break_num_registers () < 0)
9757 error (_("This target does not support hardware ranged breakpoints."));
9758
9759 bp_count = hw_breakpoint_used_count ();
9760 bp_count += target_ranged_break_num_registers ();
9761 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9762 bp_count, 0);
9763 if (can_use_bp < 0)
9764 error (_("Hardware breakpoints used exceeds limit."));
9765
f8eba3c6 9766 arg = skip_spaces (arg);
f1310107
TJB
9767 if (arg == NULL || arg[0] == '\0')
9768 error(_("No address range specified."));
9769
f8eba3c6 9770 arg_start = arg;
264f9890
PA
9771 location_spec_up start_locspec
9772 = string_to_location_spec (&arg, current_language);
6cce0251 9773 parse_breakpoint_sals (start_locspec.get (), &canonical_start, nullptr);
f1310107
TJB
9774
9775 if (arg[0] != ',')
9776 error (_("Too few arguments."));
6c5b2ebe 9777 else if (canonical_start.lsals.empty ())
f1310107 9778 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9779
6c5b2ebe 9780 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9781
6c5b2ebe
PA
9782 if (canonical_start.lsals.size () > 1
9783 || lsal_start.sals.size () != 1)
f1310107
TJB
9784 error (_("Cannot create a ranged breakpoint with multiple locations."));
9785
6c5b2ebe 9786 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9787 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9788
9789 arg++; /* Skip the comma. */
f8eba3c6 9790 arg = skip_spaces (arg);
f1310107 9791
264f9890 9792 /* Parse the end location specification. */
f1310107 9793
f1310107
TJB
9794 arg_start = arg;
9795
f8eba3c6 9796 /* We call decode_line_full directly here instead of using
264f9890
PA
9797 parse_breakpoint_sals because we need to specify the start
9798 location spec's symtab and line as the default symtab and line
9799 for the end of the range. This makes it possible to have ranges
9800 like "foo.c:27, +14", where +14 means 14 lines from the start
9801 location spec. */
9802 location_spec_up end_locspec
9803 = string_to_location_spec (&arg, current_language);
9804 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9805 sal_start.symtab, sal_start.line,
9806 &canonical_end, NULL, NULL);
9807
6c5b2ebe 9808 if (canonical_end.lsals.empty ())
f1310107 9809 error (_("Could not find location of the end of the range."));
f8eba3c6 9810
6c5b2ebe
PA
9811 const linespec_sals &lsal_end = canonical_end.lsals[0];
9812 if (canonical_end.lsals.size () > 1
9813 || lsal_end.sals.size () != 1)
f1310107
TJB
9814 error (_("Cannot create a ranged breakpoint with multiple locations."));
9815
6c5b2ebe 9816 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9817
9818 end = find_breakpoint_range_end (sal_end);
9819 if (sal_start.pc > end)
177b42fe 9820 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9821
9822 length = end - sal_start.pc + 1;
9823 if (length < 0)
9824 /* Length overflowed. */
9825 error (_("Address range too large."));
9826 else if (length == 1)
9827 {
9828 /* This range is simple enough to be handled by
9829 the `hbreak' command. */
81b1e71c 9830 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
9831
9832 return;
9833 }
9834
b925bf21
PA
9835 /* Now set up the breakpoint and install it. */
9836
9837 std::unique_ptr<breakpoint> br
9838 (new ranged_breakpoint (get_current_arch (),
9839 sal_start, length,
264f9890
PA
9840 std::move (start_locspec),
9841 std::move (end_locspec)));
f1310107 9842
27a62b43 9843 install_breakpoint (false, std::move (br), true);
f1310107
TJB
9844}
9845
093da43d
MB
9846/* See breakpoint.h. */
9847
9848watchpoint::~watchpoint ()
9849{
9850 /* Make sure to unlink the destroyed watchpoint from the related
9851 breakpoint ring. */
9852
9853 breakpoint *bpt = this;
9854 while (bpt->related_breakpoint != this)
9855 bpt = bpt->related_breakpoint;
9856
9857 bpt->related_breakpoint = this->related_breakpoint;
9858}
9859
4a64f543
MS
9860/* Return non-zero if EXP is verified as constant. Returned zero
9861 means EXP is variable. Also the constant detection may fail for
9862 some constant expressions and in such case still falsely return
9863 zero. */
2e6e3d9c 9864
dab72643 9865static bool
65d79d4b
SDJ
9866watchpoint_exp_is_const (const struct expression *exp)
9867{
1eaebe02 9868 return exp->op->constant_p ();
65d79d4b
SDJ
9869}
9870
3a292923 9871/* Implement the "re_set" method for watchpoints. */
348d480f 9872
3a292923 9873void
6cce0251 9874watchpoint::re_set (struct program_space *pspace)
348d480f
PA
9875{
9876 /* Watchpoint can be either on expression using entirely global
9877 variables, or it can be on local variables.
9878
9879 Watchpoints of the first kind are never auto-deleted, and even
9880 persist across program restarts. Since they can use variables
9881 from shared libraries, we need to reparse expression as libraries
9882 are loaded and unloaded.
9883
9884 Watchpoints on local variables can also change meaning as result
9885 of solib event. For example, if a watchpoint uses both a local
9886 and a global variables in expression, it's a local watchpoint,
9887 but unloading of a shared library will make the expression
9888 invalid. This is not a very common use case, but we still
9889 re-evaluate expression, to avoid surprises to the user.
9890
9891 Note that for local watchpoints, we re-evaluate it only if
9892 watchpoints frame id is still valid. If it's not, it means the
9893 watchpoint is out of scope and will be deleted soon. In fact,
9894 I'm not sure we'll ever be called in this case.
9895
9896 If a local watchpoint's frame id is still valid, then
3a292923 9897 exp_valid_block is likewise valid, and we can safely use it.
348d480f 9898
3a5c3e22
PA
9899 Don't do anything about disabled watchpoints, since they will be
9900 reevaluated again when enabled. */
d8de7963 9901 update_watchpoint (this, true /* reparse */);
348d480f
PA
9902}
9903
3a292923 9904/* Implement the "insert" method for hardware watchpoints. */
77b06cd7 9905
3a292923
TT
9906int
9907watchpoint::insert_location (struct bp_location *bl)
77b06cd7 9908{
3a292923 9909 int length = exact ? 1 : bl->length;
e09342b5
TJB
9910
9911 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9912 cond_exp.get ());
77b06cd7
TJB
9913}
9914
3a292923 9915/* Implement the "remove" method for hardware watchpoints. */
77b06cd7 9916
3a292923
TT
9917int
9918watchpoint::remove_location (struct bp_location *bl,
9919 enum remove_bp_reason reason)
77b06cd7 9920{
3a292923 9921 int length = exact ? 1 : bl->length;
e09342b5
TJB
9922
9923 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a292923 9924 cond_exp.get ());
e09342b5
TJB
9925}
9926
3a292923
TT
9927int
9928watchpoint::breakpoint_hit (const struct bp_location *bl,
9929 const address_space *aspace, CORE_ADDR bp_addr,
9930 const target_waitstatus &ws)
e09342b5 9931{
348d480f 9932 struct breakpoint *b = bl->owner;
77b06cd7 9933
348d480f
PA
9934 /* Continuable hardware watchpoints are treated as non-existent if the
9935 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9936 some data address). Otherwise gdb won't stop on a break instruction
9937 in the code (not from a breakpoint) when a hardware watchpoint has
9938 been defined. Also skip watchpoints which we know did not trigger
9939 (did not match the data address). */
9940 if (is_hardware_watchpoint (b)
3a292923 9941 && watchpoint_triggered == watch_triggered_no)
348d480f 9942 return 0;
9c06b0b4 9943
348d480f 9944 return 1;
9c06b0b4
TJB
9945}
9946
3a292923
TT
9947void
9948watchpoint::check_status (bpstat *bs)
9c06b0b4 9949{
348d480f 9950 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9951}
9952
3a292923
TT
9953/* Implement the "resources_needed" method for hardware
9954 watchpoints. */
9c06b0b4 9955
3a292923
TT
9956int
9957watchpoint::resources_needed (const struct bp_location *bl)
9c06b0b4 9958{
3a292923 9959 int length = exact? 1 : bl->length;
348d480f
PA
9960
9961 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9962}
9963
3a292923
TT
9964/* Implement the "works_in_software_mode" method for hardware
9965 watchpoints. */
9c06b0b4 9966
5a61e176 9967bool
3a292923 9968watchpoint::works_in_software_mode () const
9c06b0b4 9969{
efa80663 9970 /* Read and access watchpoints only work with hardware support. */
3a292923 9971 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9c06b0b4
TJB
9972}
9973
3a292923 9974enum print_stop_action
7bd86313 9975watchpoint::print_it (const bpstat *bs) const
9c06b0b4 9976{
348d480f 9977 enum print_stop_action result;
79a45e25 9978 struct ui_out *uiout = current_uiout;
348d480f
PA
9979
9980 gdb_assert (bs->bp_location_at != NULL);
9981
dbaa3bf6 9982 annotate_watchpoint (this->number);
f303dbd6
PA
9983 maybe_print_thread_hit_breakpoint (uiout);
9984
d7e74731
PA
9985 string_file stb;
9986
6b09f134 9987 std::optional<ui_out_emit_tuple> tuple_emitter;
dbaa3bf6 9988 switch (this->type)
9c06b0b4 9989 {
348d480f 9990 case bp_watchpoint:
9c06b0b4 9991 case bp_hardware_watchpoint:
112e8700
SM
9992 if (uiout->is_mi_like_p ())
9993 uiout->field_string
9994 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
dbaa3bf6 9995 mention (this);
76f9c9cf 9996 tuple_emitter.emplace (uiout, "value");
112e8700 9997 uiout->text ("\nOld value = ");
850645cf 9998 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
9999 uiout->field_stream ("old", stb);
10000 uiout->text ("\nNew value = ");
3a292923 10001 watchpoint_value_print (val.get (), &stb);
112e8700
SM
10002 uiout->field_stream ("new", stb);
10003 uiout->text ("\n");
348d480f
PA
10004 /* More than one watchpoint may have been triggered. */
10005 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10006 break;
10007
10008 case bp_read_watchpoint:
112e8700
SM
10009 if (uiout->is_mi_like_p ())
10010 uiout->field_string
10011 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
dbaa3bf6 10012 mention (this);
76f9c9cf 10013 tuple_emitter.emplace (uiout, "value");
112e8700 10014 uiout->text ("\nValue = ");
3a292923 10015 watchpoint_value_print (val.get (), &stb);
112e8700
SM
10016 uiout->field_stream ("value", stb);
10017 uiout->text ("\n");
348d480f 10018 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10019 break;
10020
10021 case bp_access_watchpoint:
348d480f
PA
10022 if (bs->old_val != NULL)
10023 {
112e8700
SM
10024 if (uiout->is_mi_like_p ())
10025 uiout->field_string
10026 ("reason",
348d480f 10027 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
dbaa3bf6 10028 mention (this);
76f9c9cf 10029 tuple_emitter.emplace (uiout, "value");
112e8700 10030 uiout->text ("\nOld value = ");
850645cf 10031 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10032 uiout->field_stream ("old", stb);
10033 uiout->text ("\nNew value = ");
348d480f
PA
10034 }
10035 else
10036 {
dbaa3bf6 10037 mention (this);
112e8700
SM
10038 if (uiout->is_mi_like_p ())
10039 uiout->field_string
10040 ("reason",
348d480f 10041 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10042 tuple_emitter.emplace (uiout, "value");
112e8700 10043 uiout->text ("\nValue = ");
348d480f 10044 }
3a292923 10045 watchpoint_value_print (val.get (), &stb);
112e8700
SM
10046 uiout->field_stream ("new", stb);
10047 uiout->text ("\n");
348d480f 10048 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10049 break;
10050 default:
348d480f 10051 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10052 }
10053
348d480f
PA
10054 return result;
10055}
10056
3a292923 10057/* Implement the "print_mention" method for hardware watchpoints. */
348d480f 10058
3a292923 10059void
b713485d 10060watchpoint::print_mention () const
348d480f 10061{
79a45e25 10062 struct ui_out *uiout = current_uiout;
46b9c129 10063 const char *tuple_name;
348d480f 10064
3a292923 10065 switch (type)
348d480f
PA
10066 {
10067 case bp_watchpoint:
112e8700 10068 uiout->text ("Watchpoint ");
46b9c129 10069 tuple_name = "wpt";
348d480f
PA
10070 break;
10071 case bp_hardware_watchpoint:
112e8700 10072 uiout->text ("Hardware watchpoint ");
46b9c129 10073 tuple_name = "wpt";
348d480f
PA
10074 break;
10075 case bp_read_watchpoint:
112e8700 10076 uiout->text ("Hardware read watchpoint ");
46b9c129 10077 tuple_name = "hw-rwpt";
348d480f
PA
10078 break;
10079 case bp_access_watchpoint:
112e8700 10080 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10081 tuple_name = "hw-awpt";
348d480f
PA
10082 break;
10083 default:
f34652de 10084 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
10085 }
10086
46b9c129 10087 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 10088 uiout->field_signed ("number", number);
112e8700 10089 uiout->text (": ");
3a292923 10090 uiout->field_string ("exp", exp_string.get ());
348d480f
PA
10091}
10092
3a292923 10093/* Implement the "print_recreate" method for watchpoints. */
348d480f 10094
3a292923 10095void
4d1ae558 10096watchpoint::print_recreate (struct ui_file *fp) const
348d480f 10097{
3a292923 10098 switch (type)
348d480f
PA
10099 {
10100 case bp_watchpoint:
10101 case bp_hardware_watchpoint:
6cb06a8c 10102 gdb_printf (fp, "watch");
348d480f
PA
10103 break;
10104 case bp_read_watchpoint:
6cb06a8c 10105 gdb_printf (fp, "rwatch");
348d480f
PA
10106 break;
10107 case bp_access_watchpoint:
6cb06a8c 10108 gdb_printf (fp, "awatch");
348d480f
PA
10109 break;
10110 default:
f34652de 10111 internal_error (_("Invalid watchpoint type."));
348d480f
PA
10112 }
10113
3a292923 10114 gdb_printf (fp, " %s", exp_string.get ());
04d0163c 10115 print_recreate_thread (fp);
348d480f
PA
10116}
10117
3a292923 10118/* Implement the "explains_signal" method for watchpoints. */
427cd150 10119
a6860f3a 10120bool
3a292923 10121watchpoint::explains_signal (enum gdb_signal sig)
427cd150
TT
10122{
10123 /* A software watchpoint cannot cause a signal other than
10124 GDB_SIGNAL_TRAP. */
3a292923 10125 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
a6860f3a 10126 return false;
427cd150 10127
a6860f3a 10128 return true;
427cd150
TT
10129}
10130
3a292923
TT
10131struct masked_watchpoint : public watchpoint
10132{
73063f51
TT
10133 using watchpoint::watchpoint;
10134
3a292923
TT
10135 int insert_location (struct bp_location *) override;
10136 int remove_location (struct bp_location *,
10137 enum remove_bp_reason reason) override;
10138 int resources_needed (const struct bp_location *) override;
5a61e176 10139 bool works_in_software_mode () const override;
7bd86313 10140 enum print_stop_action print_it (const bpstat *bs) const override;
3a292923 10141 void print_one_detail (struct ui_out *) const override;
b713485d 10142 void print_mention () const override;
4d1ae558 10143 void print_recreate (struct ui_file *fp) const override;
3a292923 10144};
348d480f 10145
3a292923 10146/* Implement the "insert" method for masked hardware watchpoints. */
348d480f 10147
3a292923
TT
10148int
10149masked_watchpoint::insert_location (struct bp_location *bl)
348d480f 10150{
3a292923 10151 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
348d480f
PA
10152 bl->watchpoint_type);
10153}
10154
3a292923 10155/* Implement the "remove" method for masked hardware watchpoints. */
348d480f 10156
3a292923
TT
10157int
10158masked_watchpoint::remove_location (struct bp_location *bl,
10159 enum remove_bp_reason reason)
348d480f 10160{
3a292923 10161 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
dda83cd7 10162 bl->watchpoint_type);
348d480f
PA
10163}
10164
3a292923
TT
10165/* Implement the "resources_needed" method for masked hardware
10166 watchpoints. */
348d480f 10167
3a292923
TT
10168int
10169masked_watchpoint::resources_needed (const struct bp_location *bl)
348d480f 10170{
3a292923 10171 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
348d480f
PA
10172}
10173
3a292923
TT
10174/* Implement the "works_in_software_mode" method for masked hardware
10175 watchpoints. */
348d480f 10176
5a61e176 10177bool
3a292923 10178masked_watchpoint::works_in_software_mode () const
348d480f 10179{
5a61e176 10180 return false;
348d480f
PA
10181}
10182
3a292923
TT
10183/* Implement the "print_it" method for masked hardware
10184 watchpoints. */
348d480f 10185
3a292923 10186enum print_stop_action
7bd86313 10187masked_watchpoint::print_it (const bpstat *bs) const
348d480f 10188{
79a45e25 10189 struct ui_out *uiout = current_uiout;
348d480f
PA
10190
10191 /* Masked watchpoints have only one location. */
9dc1523b 10192 gdb_assert (this->has_single_location ());
348d480f 10193
dbaa3bf6 10194 annotate_watchpoint (this->number);
f303dbd6
PA
10195 maybe_print_thread_hit_breakpoint (uiout);
10196
dbaa3bf6 10197 switch (this->type)
348d480f
PA
10198 {
10199 case bp_hardware_watchpoint:
112e8700
SM
10200 if (uiout->is_mi_like_p ())
10201 uiout->field_string
10202 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10203 break;
10204
10205 case bp_read_watchpoint:
112e8700
SM
10206 if (uiout->is_mi_like_p ())
10207 uiout->field_string
10208 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10209 break;
10210
10211 case bp_access_watchpoint:
112e8700
SM
10212 if (uiout->is_mi_like_p ())
10213 uiout->field_string
10214 ("reason",
348d480f
PA
10215 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10216 break;
10217 default:
f34652de 10218 internal_error (_("Invalid hardware watchpoint type."));
348d480f
PA
10219 }
10220
dbaa3bf6 10221 mention (this);
112e8700 10222 uiout->text (_("\n\
9c06b0b4
TJB
10223Check the underlying instruction at PC for the memory\n\
10224address and value which triggered this watchpoint.\n"));
112e8700 10225 uiout->text ("\n");
9c06b0b4
TJB
10226
10227 /* More than one watchpoint may have been triggered. */
10228 return PRINT_UNKNOWN;
10229}
10230
3a292923
TT
10231/* Implement the "print_one_detail" method for masked hardware
10232 watchpoints. */
9c06b0b4 10233
3a292923
TT
10234void
10235masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9c06b0b4
TJB
10236{
10237 /* Masked watchpoints have only one location. */
9dc1523b 10238 gdb_assert (this->has_single_location ());
9c06b0b4 10239
112e8700 10240 uiout->text ("\tmask ");
f5951b9f 10241 uiout->field_core_addr ("mask", this->first_loc ().gdbarch, hw_wp_mask);
112e8700 10242 uiout->text ("\n");
9c06b0b4
TJB
10243}
10244
3a292923
TT
10245/* Implement the "print_mention" method for masked hardware
10246 watchpoints. */
9c06b0b4 10247
3a292923 10248void
b713485d 10249masked_watchpoint::print_mention () const
9c06b0b4 10250{
79a45e25 10251 struct ui_out *uiout = current_uiout;
46b9c129 10252 const char *tuple_name;
9c06b0b4 10253
3a292923 10254 switch (type)
9c06b0b4
TJB
10255 {
10256 case bp_hardware_watchpoint:
112e8700 10257 uiout->text ("Masked hardware watchpoint ");
46b9c129 10258 tuple_name = "wpt";
9c06b0b4
TJB
10259 break;
10260 case bp_read_watchpoint:
112e8700 10261 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10262 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10263 break;
10264 case bp_access_watchpoint:
112e8700 10265 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10266 tuple_name = "hw-awpt";
9c06b0b4
TJB
10267 break;
10268 default:
f34652de 10269 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10270 }
10271
46b9c129 10272 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
3a292923 10273 uiout->field_signed ("number", number);
112e8700 10274 uiout->text (": ");
3a292923 10275 uiout->field_string ("exp", exp_string.get ());
9c06b0b4
TJB
10276}
10277
3a292923
TT
10278/* Implement the "print_recreate" method for masked hardware
10279 watchpoints. */
9c06b0b4 10280
3a292923 10281void
4d1ae558 10282masked_watchpoint::print_recreate (struct ui_file *fp) const
9c06b0b4 10283{
3a292923 10284 switch (type)
9c06b0b4
TJB
10285 {
10286 case bp_hardware_watchpoint:
6cb06a8c 10287 gdb_printf (fp, "watch");
9c06b0b4
TJB
10288 break;
10289 case bp_read_watchpoint:
6cb06a8c 10290 gdb_printf (fp, "rwatch");
9c06b0b4
TJB
10291 break;
10292 case bp_access_watchpoint:
6cb06a8c 10293 gdb_printf (fp, "awatch");
9c06b0b4
TJB
10294 break;
10295 default:
f34652de 10296 internal_error (_("Invalid hardware watchpoint type."));
9c06b0b4
TJB
10297 }
10298
3a292923 10299 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9c1f84c9 10300 phex (hw_wp_mask));
04d0163c 10301 print_recreate_thread (fp);
9c06b0b4
TJB
10302}
10303
9c06b0b4
TJB
10304/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10305
f2478a7e 10306static bool
9c06b0b4
TJB
10307is_masked_watchpoint (const struct breakpoint *b)
10308{
3a292923 10309 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9c06b0b4
TJB
10310}
10311
53a5351d 10312/* accessflag: hw_write: watch write,
dda83cd7 10313 hw_read: watch read,
53a5351d 10314 hw_access: watch access (read or write) */
c906108c 10315static void
bbc13ae3 10316watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10317 bool just_location, bool internal)
c906108c 10318{
c1fc2657 10319 struct breakpoint *scope_breakpoint = NULL;
270140bd 10320 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10321 struct value *result;
bb9d5f81 10322 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10323 const char *exp_start = NULL;
10324 const char *exp_end = NULL;
10325 const char *tok, *end_tok;
9c06b0b4 10326 int toklen = -1;
bbc13ae3
KS
10327 const char *cond_start = NULL;
10328 const char *cond_end = NULL;
c906108c 10329 enum bptype bp_type;
37e4754d 10330 int thread = -1;
b080fe54 10331 int inferior = -1;
9c06b0b4
TJB
10332 /* Flag to indicate whether we are going to use masks for
10333 the hardware watchpoint. */
2e362716 10334 bool use_mask = false;
9c06b0b4 10335 CORE_ADDR mask = 0;
2ecee236 10336 int task = -1;
c906108c 10337
37e4754d
LM
10338 /* Make sure that we actually have parameters to parse. */
10339 if (arg != NULL && arg[0] != '\0')
10340 {
bbc13ae3
KS
10341 const char *value_start;
10342
10343 exp_end = arg + strlen (arg);
37e4754d 10344
9c06b0b4
TJB
10345 /* Look for "parameter value" pairs at the end
10346 of the arguments string. */
bbc13ae3 10347 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10348 {
10349 /* Skip whitespace at the end of the argument list. */
10350 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10351 tok--;
10352
10353 /* Find the beginning of the last token.
10354 This is the value of the parameter. */
10355 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10356 tok--;
10357 value_start = tok + 1;
10358
10359 /* Skip whitespace. */
10360 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10361 tok--;
10362
10363 end_tok = tok;
10364
10365 /* Find the beginning of the second to last token.
10366 This is the parameter itself. */
10367 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10368 tok--;
10369 tok++;
10370 toklen = end_tok - tok + 1;
10371
61012eef 10372 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10373 {
5d5658a1 10374 struct thread_info *thr;
9c06b0b4
TJB
10375 /* At this point we've found a "thread" token, which means
10376 the user is trying to set a watchpoint that triggers
10377 only in a specific thread. */
5d5658a1 10378 const char *endp;
37e4754d 10379
9c06b0b4
TJB
10380 if (thread != -1)
10381 error(_("You can specify only one thread."));
37e4754d 10382
2ecee236 10383 if (task != -1)
0a9ccb9d
AB
10384 error (_("You can specify only one of thread or task."));
10385
b080fe54
AB
10386 if (inferior != -1)
10387 error (_("You can specify only one of inferior or thread."));
10388
9c06b0b4 10389 /* Extract the thread ID from the next token. */
5d5658a1 10390 thr = parse_thread_id (value_start, &endp);
b080fe54
AB
10391 if (value_start == endp)
10392 error (_("Junk after thread keyword."));
9c06b0b4 10393
5d5658a1 10394 thread = thr->global_num;
9c06b0b4 10395 }
8a18382f
TT
10396 else if (toklen == 4 && startswith (tok, "task"))
10397 {
10398 char *tmp;
10399
2ecee236 10400 if (task != -1)
980dbf36
AB
10401 error(_("You can specify only one task."));
10402
0a9ccb9d
AB
10403 if (thread != -1)
10404 error (_("You can specify only one of thread or task."));
10405
b080fe54
AB
10406 if (inferior != -1)
10407 error (_("You can specify only one of inferior or task."));
10408
8a18382f
TT
10409 task = strtol (value_start, &tmp, 0);
10410 if (tmp == value_start)
10411 error (_("Junk after task keyword."));
10412 if (!valid_task_id (task))
10413 error (_("Unknown task %d."), task);
10414 }
b080fe54
AB
10415 else if (toklen == 8 && startswith (tok, "inferior"))
10416 {
10417 /* Support for watchpoints will be added in a later commit. */
10418 error (_("Cannot use 'inferior' keyword with watchpoints"));
10419 }
61012eef 10420 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10421 {
10422 /* We've found a "mask" token, which means the user wants to
10423 create a hardware watchpoint that is going to have the mask
10424 facility. */
65558ca5 10425 struct value *mask_value;
37e4754d 10426
9c06b0b4
TJB
10427 if (use_mask)
10428 error(_("You can specify only one mask."));
37e4754d 10429
2e362716 10430 use_mask = just_location = true;
37e4754d 10431
65558ca5 10432 scoped_value_mark mark;
9c06b0b4
TJB
10433 mask_value = parse_to_comma_and_eval (&value_start);
10434 mask = value_as_address (mask_value);
9c06b0b4
TJB
10435 }
10436 else
10437 /* We didn't recognize what we found. We should stop here. */
10438 break;
37e4754d 10439
9c06b0b4
TJB
10440 /* Truncate the string and get rid of the "parameter value" pair before
10441 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10442 exp_end = tok;
9c06b0b4 10443 }
37e4754d 10444 }
bbc13ae3
KS
10445 else
10446 exp_end = arg;
37e4754d 10447
bbc13ae3
KS
10448 /* Parse the rest of the arguments. From here on out, everything
10449 is in terms of a newly allocated string instead of the original
10450 ARG. */
81b1e71c
TT
10451 std::string expression (arg, exp_end - arg);
10452 exp_start = arg = expression.c_str ();
699bd4cf
TT
10453 innermost_block_tracker tracker;
10454 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10455 exp_end = arg;
fa8a61dc
TT
10456 /* Remove trailing whitespace from the expression before saving it.
10457 This makes the eventual display of the expression string a bit
10458 prettier. */
10459 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10460 --exp_end;
10461
65d79d4b 10462 /* Checking if the expression is not constant. */
4d01a485 10463 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10464 {
10465 int len;
10466
10467 len = exp_end - exp_start;
10468 while (len > 0 && isspace (exp_start[len - 1]))
10469 len--;
10470 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10471 }
10472
699bd4cf 10473 exp_valid_block = tracker.block ();
b926417a 10474 struct value *mark = value_mark ();
850645cf 10475 struct value *val_as_value = nullptr;
1eaebe02
TT
10476 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10477 just_location);
06a64a0b 10478
850645cf 10479 if (val_as_value != NULL && just_location)
bb9d5f81 10480 {
5011c493 10481 saved_bitpos = val_as_value->bitpos ();
f49d5fa2 10482 saved_bitsize = val_as_value->bitsize ();
bb9d5f81
PP
10483 }
10484
850645cf 10485 value_ref_ptr val;
06a64a0b
TT
10486 if (just_location)
10487 {
9c06b0b4
TJB
10488 int ret;
10489
06a64a0b 10490 exp_valid_block = NULL;
850645cf 10491 val = release_value (value_addr (result));
06a64a0b 10492 value_free_to_mark (mark);
9c06b0b4
TJB
10493
10494 if (use_mask)
10495 {
850645cf 10496 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10497 mask);
10498 if (ret == -1)
10499 error (_("This target does not support masked watchpoints."));
10500 else if (ret == -2)
10501 error (_("Invalid mask or memory region."));
10502 }
06a64a0b 10503 }
850645cf
TT
10504 else if (val_as_value != NULL)
10505 val = release_value (val_as_value);
c906108c 10506
f1735a53
TT
10507 tok = skip_spaces (arg);
10508 end_tok = skip_to_space (tok);
c906108c
SS
10509
10510 toklen = end_tok - tok;
10511 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10512 {
10513 tok = cond_start = end_tok + 1;
699bd4cf
TT
10514 innermost_block_tracker if_tracker;
10515 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10516
10517 /* The watchpoint expression may not be local, but the condition
10518 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10519 cond_exp_valid_block = if_tracker.block ();
60e1c644 10520
c906108c
SS
10521 cond_end = tok;
10522 }
10523 if (*tok)
8a3fe4f8 10524 error (_("Junk at end of command."));
c906108c 10525
bd2b40ac 10526 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
441d7c93
PA
10527
10528 /* Save this because create_internal_breakpoint below invalidates
10529 'wp_frame'. */
10530 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c 10531
e8369a73
AB
10532 /* Now set up the breakpoint. We create all watchpoints as hardware
10533 watchpoints here even if hardware watchpoints are turned off, a call
10534 to update_watchpoint later in this function will cause the type to
10535 drop back to bp_watchpoint (software watchpoint) if required. */
10536
10537 if (accessflag == hw_read)
10538 bp_type = bp_read_watchpoint;
10539 else if (accessflag == hw_access)
10540 bp_type = bp_access_watchpoint;
10541 else
10542 bp_type = bp_hardware_watchpoint;
3a5c3e22 10543
3a292923 10544 std::unique_ptr<watchpoint> w;
348d480f 10545 if (use_mask)
40ae603e 10546 w = std::make_unique<masked_watchpoint> (nullptr, bp_type);
348d480f 10547 else
40ae603e 10548 w = std::make_unique<watchpoint> (nullptr, bp_type);
3a292923 10549
0a9ccb9d 10550 /* At most one of thread or task can be set on a watchpoint. */
2ecee236 10551 gdb_assert (thread == -1 || task == -1);
c1fc2657 10552 w->thread = thread;
b080fe54 10553 w->inferior = inferior;
8a18382f 10554 w->task = task;
c1fc2657
SM
10555 w->disposition = disp_donttouch;
10556 w->pspace = current_program_space;
b22e99fd 10557 w->exp = std::move (exp);
3a5c3e22
PA
10558 w->exp_valid_block = exp_valid_block;
10559 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10560 if (just_location)
10561 {
f28085df 10562 struct type *t = val->type ();
850645cf 10563 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10564
43cc5389 10565 w->exp_string_reparse
a4c50be3 10566 = current_language->watch_location_expression (t, addr);
06a64a0b 10567
8579fd13
AB
10568 w->exp_string = xstrprintf ("-location %.*s",
10569 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10570 }
10571 else
a4c50be3 10572 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
9c06b0b4
TJB
10573
10574 if (use_mask)
10575 {
3a5c3e22 10576 w->hw_wp_mask = mask;
9c06b0b4
TJB
10577 }
10578 else
10579 {
3a5c3e22 10580 w->val = val;
bb9d5f81
PP
10581 w->val_bitpos = saved_bitpos;
10582 w->val_bitsize = saved_bitsize;
4c1d86d9 10583 w->val_valid = true;
9c06b0b4 10584 }
77b06cd7 10585
c906108c 10586 if (cond_start)
6f781ee3 10587 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
c906108c 10588 else
c1fc2657 10589 w->cond_string = 0;
c5aa993b 10590
441d7c93 10591 if (frame_id_p (watchpoint_frame))
f6bc2008 10592 {
441d7c93 10593 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10594 w->watchpoint_thread = inferior_ptid;
f6bc2008 10595 }
c906108c 10596 else
f6bc2008 10597 {
3a5c3e22
PA
10598 w->watchpoint_frame = null_frame_id;
10599 w->watchpoint_thread = null_ptid;
f6bc2008 10600 }
c906108c 10601
06a64a0b
TT
10602 if (!just_location)
10603 value_free_to_mark (mark);
2d134ed3 10604
b270e6f9
TT
10605 /* Finally update the new watchpoint. This creates the locations
10606 that should be inserted. */
d8de7963 10607 update_watchpoint (w.get (), true /* reparse */);
a9634178 10608
f04b2702
TV
10609 /* If the expression is "local", then set up a "watchpoint scope"
10610 breakpoint at the point where we've left the scope of the watchpoint
10611 expression. Create the scope breakpoint before the watchpoint, so
10612 that we will encounter it first in bpstat_stop_status. */
10613 if (exp_valid_block != nullptr && wp_frame != nullptr)
10614 {
10615 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10616
10617 if (frame_id_p (caller_frame_id))
10618 {
10619 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10620 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10621
10622 scope_breakpoint
10623 = create_internal_breakpoint (caller_arch, caller_pc,
10624 bp_watchpoint_scope);
10625
10626 /* create_internal_breakpoint could invalidate WP_FRAME. */
10627 wp_frame = nullptr;
10628
10629 scope_breakpoint->enable_state = bp_enabled;
10630
10631 /* Automatically delete the breakpoint when it hits. */
10632 scope_breakpoint->disposition = disp_del;
10633
10634 /* Only break in the proper frame (help with recursion). */
10635 scope_breakpoint->frame_id = caller_frame_id;
10636
10637 /* Set the address at which we will stop. */
10638 bp_location &loc = scope_breakpoint->first_loc ();
10639 loc.gdbarch = caller_arch;
10640 loc.requested_address = caller_pc;
10641 loc.address
10642 = adjust_breakpoint_address (loc.gdbarch, loc.requested_address,
10643 scope_breakpoint->type,
10644 current_program_space);
10645 }
10646 }
10647
10648 if (scope_breakpoint != nullptr)
10649 {
10650 /* The scope breakpoint is related to the watchpoint. We will
10651 need to act on them together. */
10652 w->related_breakpoint = scope_breakpoint;
10653 scope_breakpoint->related_breakpoint = w.get ();
10654 }
10655
10656 /* Verify that the scope breakpoint comes before the watchpoint in the
10657 breakpoint chain. */
10658 gdb_assert (scope_breakpoint == nullptr
10659 || &breakpoint_chain.back () == scope_breakpoint);
10660 watchpoint *watchpoint_ptr = w.get ();
b270e6f9 10661 install_breakpoint (internal, std::move (w), 1);
f04b2702 10662 gdb_assert (&breakpoint_chain.back () == watchpoint_ptr);
c906108c
SS
10663}
10664
e09342b5 10665/* Return count of debug registers needed to watch the given expression.
e09342b5 10666 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10667
c906108c 10668static int
a6535de1 10669can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10670{
10671 int found_memory_cnt = 0;
10672
10673 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10674 if (!can_use_hw_watchpoints)
c906108c 10675 return 0;
c5aa993b 10676
a6535de1
TT
10677 gdb_assert (!vals.empty ());
10678 struct value *head = vals[0].get ();
10679
5c44784c
JM
10680 /* Make sure that the value of the expression depends only upon
10681 memory contents, and values computed from them within GDB. If we
10682 find any register references or function calls, we can't use a
10683 hardware watchpoint.
10684
10685 The idea here is that evaluating an expression generates a series
10686 of values, one holding the value of every subexpression. (The
10687 expression a*b+c has five subexpressions: a, b, a*b, c, and
10688 a*b+c.) GDB's values hold almost enough information to establish
10689 the criteria given above --- they identify memory lvalues,
10690 register lvalues, computed values, etcetera. So we can evaluate
10691 the expression, and then scan the chain of values that leaves
10692 behind to decide whether we can detect any possible change to the
10693 expression's final value using only hardware watchpoints.
10694
10695 However, I don't think that the values returned by inferior
10696 function calls are special in any way. So this function may not
10697 notice that an expression involving an inferior function call
10698 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10699 for (const value_ref_ptr &iter : vals)
c906108c 10700 {
a6535de1
TT
10701 struct value *v = iter.get ();
10702
736355f2 10703 if (v->lval () == lval_memory)
c906108c 10704 {
3ee3b270 10705 if (v != head && v->lazy ())
8464be76
DJ
10706 /* A lazy memory lvalue in the chain is one that GDB never
10707 needed to fetch; we either just used its address (e.g.,
10708 `a' in `a.b') or we never needed it at all (e.g., `a'
10709 in `a,b'). This doesn't apply to HEAD; if that is
10710 lazy then it was not readable, but watch it anyway. */
5c44784c 10711 ;
53a5351d 10712 else
5c44784c
JM
10713 {
10714 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10715 it with hardware watchpoints. */
d0c97917 10716 struct type *vtype = check_typedef (v->type ());
2e70b7b9
MS
10717
10718 /* We only watch structs and arrays if user asked for it
10719 explicitly, never if they just happen to appear in a
10720 middle of some value chain. */
10721 if (v == head
78134374
SM
10722 || (vtype->code () != TYPE_CODE_STRUCT
10723 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10724 {
9feb2d07 10725 CORE_ADDR vaddr = v->address ();
e09342b5
TJB
10726 int len;
10727 int num_regs;
10728
a9634178 10729 len = (target_exact_watchpoints
e09342b5 10730 && is_scalar_type_recursive (vtype))?
d0c97917 10731 1 : v->type ()->length ();
2e70b7b9 10732
e09342b5
TJB
10733 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10734 if (!num_regs)
2e70b7b9
MS
10735 return 0;
10736 else
e09342b5 10737 found_memory_cnt += num_regs;
2e70b7b9 10738 }
5c44784c 10739 }
c5aa993b 10740 }
b2227e67 10741 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
38b6c3b3 10742 return 0; /* These are values from the history (e.g., $1). */
736355f2 10743 else if (v->lval () == lval_register)
38b6c3b3 10744 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10745 }
10746
10747 /* The expression itself looks suitable for using a hardware
10748 watchpoint, but give the target machine a chance to reject it. */
10749 return found_memory_cnt;
10750}
10751
8b93c638 10752void
2e362716 10753watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10754{
84f4c1fe 10755 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10756}
10757
a15a5258
AB
10758/* Options for the watch, awatch, and rwatch commands. */
10759
10760struct watch_options
10761{
10762 /* For -location. */
10763 bool location = false;
10764};
10765
10766/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10767
10768 Historically GDB always accepted both '-location' and '-l' flags for
10769 these commands (both flags being synonyms). When converting to the
10770 newer option scheme only '-location' is added here. That's fine (for
10771 backward compatibility) as any non-ambiguous prefix of a flag will be
10772 accepted, so '-l', '-loc', are now all accepted.
10773
10774 What this means is that, if in the future, we add any new flag here
10775 that starts with '-l' then this will break backward compatibility, so
10776 please, don't do that! */
10777
10778static const gdb::option::option_def watch_option_defs[] = {
10779 gdb::option::flag_option_def<watch_options> {
10780 "location",
10781 [] (watch_options *opt) { return &opt->location; },
10782 N_("\
358ada8b 10783This evaluates EXPRESSION and watches the memory to which it refers.\n\
a15a5258
AB
10784-l can be used as a short form of -location."),
10785 },
10786};
10787
10788/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10789 commands. */
10790
10791static gdb::option::option_def_group
10792make_watch_options_def_group (watch_options *opts)
10793{
10794 return {{watch_option_defs}, opts};
10795}
10796
06a64a0b
TT
10797/* A helper function that looks for the "-location" argument and then
10798 calls watch_command_1. */
10799
10800static void
0b39b52e 10801watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10802{
a15a5258
AB
10803 watch_options opts;
10804 auto grp = make_watch_options_def_group (&opts);
10805 gdb::option::process_options
10806 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10807 if (arg != nullptr && *arg == '\0')
10808 arg = nullptr;
10809
10810 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10811}
06a64a0b 10812
a15a5258
AB
10813/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10814static void
10815watch_command_completer (struct cmd_list_element *ignore,
10816 completion_tracker &tracker,
10817 const char *text, const char * /*word*/)
10818{
10819 const auto group = make_watch_options_def_group (nullptr);
10820 if (gdb::option::complete_options
10821 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10822 return;
06a64a0b 10823
a15a5258
AB
10824 const char *word = advance_to_expression_complete_word_point (tracker, text);
10825 expression_completer (ignore, tracker, text, word);
8b93c638 10826}
8926118c 10827
c5aa993b 10828static void
0b39b52e 10829watch_command (const char *arg, int from_tty)
c906108c 10830{
06a64a0b 10831 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10832}
10833
8b93c638 10834void
2e362716 10835rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10836{
84f4c1fe 10837 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10838}
8926118c 10839
c5aa993b 10840static void
0b39b52e 10841rwatch_command (const char *arg, int from_tty)
c906108c 10842{
06a64a0b 10843 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10844}
10845
8b93c638 10846void
2e362716 10847awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10848{
84f4c1fe 10849 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10850}
8926118c 10851
c5aa993b 10852static void
0b39b52e 10853awatch_command (const char *arg, int from_tty)
c906108c 10854{
06a64a0b 10855 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10856}
c906108c 10857\f
c5aa993b 10858
cfc31633
PA
10859/* Data for the FSM that manages the until(location)/advance commands
10860 in infcmd.c. Here because it uses the mechanisms of
10861 breakpoints. */
c906108c 10862
46e3ed7f 10863struct until_break_fsm : public thread_fsm
bfec99b2 10864{
46e3ed7f 10865 /* The thread that was current when the command was executed. */
cfc31633
PA
10866 int thread;
10867
df631783
PA
10868 /* The breakpoint set at the return address in the caller frame,
10869 plus breakpoints at all the destination locations. */
10870 std::vector<breakpoint_up> breakpoints;
cfc31633 10871
46e3ed7f 10872 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 10873 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
10874 : thread_fsm (cmd_interp),
10875 thread (thread),
df631783 10876 breakpoints (std::move (breakpoints))
46e3ed7f
TT
10877 {
10878 }
cfc31633 10879
46e3ed7f
TT
10880 void clean_up (struct thread_info *thread) override;
10881 bool should_stop (struct thread_info *thread) override;
10882 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
10883};
10884
cfc31633
PA
10885/* Implementation of the 'should_stop' FSM method for the
10886 until(location)/advance commands. */
10887
46e3ed7f
TT
10888bool
10889until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 10890{
df631783
PA
10891 for (const breakpoint_up &bp : breakpoints)
10892 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10893 bp.get ()) != NULL)
10894 {
10895 set_finished ();
10896 break;
10897 }
cfc31633 10898
46e3ed7f 10899 return true;
cfc31633
PA
10900}
10901
10902/* Implementation of the 'clean_up' FSM method for the
10903 until(location)/advance commands. */
10904
46e3ed7f
TT
10905void
10906until_break_fsm::clean_up (struct thread_info *)
43ff13b4 10907{
cfc31633 10908 /* Clean up our temporary breakpoints. */
df631783 10909 breakpoints.clear ();
46e3ed7f 10910 delete_longjmp_breakpoint (thread);
cfc31633
PA
10911}
10912
10913/* Implementation of the 'async_reply_reason' FSM method for the
10914 until(location)/advance commands. */
10915
46e3ed7f
TT
10916enum async_reply_reason
10917until_break_fsm::do_async_reply_reason ()
cfc31633
PA
10918{
10919 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
10920}
10921
c906108c 10922void
f2fc3015 10923until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 10924{
bd2b40ac 10925 frame_info_ptr frame;
8556afb4
PA
10926 struct gdbarch *frame_gdbarch;
10927 struct frame_id stack_frame_id;
10928 struct frame_id caller_frame_id;
186c406b
TT
10929 int thread;
10930 struct thread_info *tp;
c906108c 10931
70509625 10932 clear_proceed_status (0);
c906108c
SS
10933
10934 /* Set a breakpoint where the user wants it and at return from
4a64f543 10935 this function. */
c5aa993b 10936
264f9890 10937 location_spec_up locspec = string_to_location_spec (&arg, current_language);
f00aae0f 10938
6c5b2ebe
PA
10939 std::vector<symtab_and_line> sals
10940 = (last_displayed_sal_is_valid ()
264f9890 10941 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
6c5b2ebe
PA
10942 get_last_displayed_symtab (),
10943 get_last_displayed_line ())
264f9890 10944 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 10945 NULL, NULL, 0));
c5aa993b 10946
df631783 10947 if (sals.empty ())
8a3fe4f8 10948 error (_("Couldn't get information on specified line."));
c5aa993b 10949
c906108c 10950 if (*arg)
8a3fe4f8 10951 error (_("Junk at end of arguments."));
c5aa993b 10952
186c406b 10953 tp = inferior_thread ();
5d5658a1 10954 thread = tp->global_num;
186c406b 10955
8556afb4
PA
10956 /* Note linespec handling above invalidates the frame chain.
10957 Installing a breakpoint also invalidates the frame chain (as it
10958 may need to switch threads), so do any frame handling before
10959 that. */
10960
10961 frame = get_selected_frame (NULL);
10962 frame_gdbarch = get_frame_arch (frame);
10963 stack_frame_id = get_stack_frame_id (frame);
10964 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10965
ae66c1fc
EZ
10966 /* Keep within the current frame, or in frames called by the current
10967 one. */
edb3359d 10968
df631783 10969 std::vector<breakpoint_up> breakpoints;
5419bdae 10970
6b09f134 10971 std::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
5419bdae 10972
883bc8d1 10973 if (frame_id_p (caller_frame_id))
c906108c 10974 {
883bc8d1 10975 struct symtab_and_line sal2;
cfc31633 10976 struct gdbarch *caller_gdbarch;
883bc8d1
PA
10977
10978 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10979 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 10980 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
10981
10982 breakpoint_up caller_breakpoint
10983 = set_momentary_breakpoint (caller_gdbarch, sal2,
10984 caller_frame_id, bp_until);
10985 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 10986
0f443d1b 10987 set_longjmp_breakpoint (tp, stack_frame_id);
5419bdae 10988 lj_deleter.emplace (thread);
c906108c 10989 }
c5aa993b 10990
df631783
PA
10991 /* If the user told us to continue until a specified location, we
10992 don't specify a frame at which we need to stop. Otherwise,
10993 specify the selected frame, because we want to stop only at the
10994 very same frame. */
10995 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10996
10997 for (symtab_and_line &sal : sals)
10998 {
10999 resolve_sal_pc (&sal);
11000
11001 breakpoint_up location_breakpoint
11002 = set_momentary_breakpoint (frame_gdbarch, sal,
11003 stop_frame_id, bp_until);
11004 breakpoints.emplace_back (std::move (location_breakpoint));
11005 }
883bc8d1 11006
573269a8
LS
11007 tp->set_thread_fsm
11008 (std::unique_ptr<thread_fsm>
11009 (new until_break_fsm (command_interp (), tp->global_num,
11010 std::move (breakpoints))));
f107f563 11011
5419bdae
TT
11012 if (lj_deleter)
11013 lj_deleter->release ();
f107f563 11014
cfc31633 11015 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11016}
ae66c1fc 11017
c906108c
SS
11018\f
11019
81b1e71c 11020/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11021
11022static int
81b1e71c 11023compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11024{
81b1e71c
TT
11025 uintptr_t ua = (uintptr_t) a;
11026 uintptr_t ub = (uintptr_t) b;
8a2c437b 11027
81b1e71c 11028 if (a->number < b->number)
8a2c437b 11029 return -1;
81b1e71c 11030 else if (a->number > b->number)
8a2c437b
TT
11031 return 1;
11032
11033 /* Now sort by address, in case we see, e..g, two breakpoints with
11034 the number 0. */
11035 if (ua < ub)
11036 return -1;
94b0e70d 11037 return ua > ub ? 1 : 0;
8a2c437b
TT
11038}
11039
80f8a6eb 11040/* Delete breakpoints by address or line. */
c906108c
SS
11041
11042static void
0b39b52e 11043clear_command (const char *arg, int from_tty)
c906108c 11044{
c906108c 11045 int default_match;
c906108c 11046
6c5b2ebe
PA
11047 std::vector<symtab_and_line> decoded_sals;
11048 symtab_and_line last_sal;
11049 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11050 if (arg)
11051 {
6c5b2ebe
PA
11052 decoded_sals
11053 = decode_line_with_current_source (arg,
11054 (DECODE_LINE_FUNFIRSTLINE
11055 | DECODE_LINE_LIST_MODE));
c906108c 11056 default_match = 0;
6c5b2ebe 11057 sals = decoded_sals;
c906108c
SS
11058 }
11059 else
11060 {
1bfeeb0f
JL
11061 /* Set sal's line, symtab, pc, and pspace to the values
11062 corresponding to the last call to print_frame_info. If the
11063 codepoint is not valid, this will set all the fields to 0. */
51abb421 11064 last_sal = get_last_displayed_sal ();
6c5b2ebe 11065 if (last_sal.symtab == 0)
8a3fe4f8 11066 error (_("No source file specified."));
c906108c 11067
c906108c 11068 default_match = 1;
6c5b2ebe 11069 sals = last_sal;
c906108c
SS
11070 }
11071
4a64f543
MS
11072 /* We don't call resolve_sal_pc here. That's not as bad as it
11073 seems, because all existing breakpoints typically have both
11074 file/line and pc set. So, if clear is given file/line, we can
11075 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11076
11077 We only support clearing given the address explicitly
11078 present in breakpoint table. Say, we've set breakpoint
4a64f543 11079 at file:line. There were several PC values for that file:line,
ed0616c6 11080 due to optimization, all in one block.
4a64f543
MS
11081
11082 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11083 PC corresponding to the same file:line, the breakpoint won't
11084 be cleared. We probably can still clear the breakpoint, but
11085 since the other PC value is never presented to user, user
11086 can only find it by guessing, and it does not seem important
11087 to support that. */
11088
4a64f543
MS
11089 /* For each line spec given, delete bps which correspond to it. Do
11090 it in two passes, solely to preserve the current behavior that
11091 from_tty is forced true if we delete more than one
11092 breakpoint. */
c906108c 11093
81b1e71c 11094 std::vector<struct breakpoint *> found;
6c5b2ebe 11095 for (const auto &sal : sals)
c906108c 11096 {
05cba821
JK
11097 const char *sal_fullname;
11098
c906108c 11099 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
11100 If line given (pc == 0), clear all bpts on specified line.
11101 If defaulting, clear all bpts on default line
11102 or at default pc.
c5aa993b 11103
dda83cd7 11104 defaulting sal.pc != 0 tests to do
c5aa993b 11105
dda83cd7
SM
11106 0 1 pc
11107 1 1 pc _and_ line
11108 0 0 line
11109 1 0 <can't happen> */
c906108c 11110
05cba821
JK
11111 sal_fullname = (sal.symtab == NULL
11112 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11113
4a64f543 11114 /* Find all matching breakpoints and add them to 'found'. */
a1decfc1 11115 for (breakpoint &b : all_breakpoints ())
c5aa993b 11116 {
0d381245 11117 int match = 0;
4a64f543 11118 /* Are we going to delete b? */
a1decfc1
SM
11119 if (b.type != bp_none && !is_watchpoint (&b)
11120 && user_breakpoint_p (&b))
0d381245 11121 {
a1decfc1 11122 for (bp_location &loc : b.locations ())
0d381245 11123 {
f8eba3c6
TT
11124 /* If the user specified file:line, don't allow a PC
11125 match. This matches historical gdb behavior. */
11126 int pc_match = (!sal.explicit_line
11127 && sal.pc
b00b30b2
SM
11128 && (loc.pspace == sal.pspace)
11129 && (loc.address == sal.pc)
11130 && (!section_is_overlay (loc.section)
11131 || loc.section == sal.section));
4aac40c8
TT
11132 int line_match = 0;
11133
11134 if ((default_match || sal.explicit_line)
b00b30b2 11135 && loc.symtab != NULL
05cba821 11136 && sal_fullname != NULL
b00b30b2
SM
11137 && sal.pspace == loc.pspace
11138 && loc.line_number == sal.line
11139 && filename_cmp (symtab_to_fullname (loc.symtab),
05cba821
JK
11140 sal_fullname) == 0)
11141 line_match = 1;
4aac40c8 11142
0d381245
VP
11143 if (pc_match || line_match)
11144 {
11145 match = 1;
11146 break;
11147 }
11148 }
11149 }
11150
11151 if (match)
a1decfc1 11152 found.push_back (&b);
c906108c 11153 }
80f8a6eb 11154 }
8a2c437b 11155
8f6606b6 11156 /* Now go through the 'found' chain and delete them. */
81b1e71c 11157 if (found.empty ())
80f8a6eb
MS
11158 {
11159 if (arg)
8a3fe4f8 11160 error (_("No breakpoint at %s."), arg);
80f8a6eb 11161 else
8a3fe4f8 11162 error (_("No breakpoint at this line."));
80f8a6eb 11163 }
c906108c 11164
8a2c437b 11165 /* Remove duplicates from the vec. */
81b1e71c 11166 std::sort (found.begin (), found.end (),
b926417a 11167 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11168 {
b926417a 11169 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11170 });
11171 found.erase (std::unique (found.begin (), found.end (),
b926417a 11172 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11173 {
b926417a 11174 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11175 }),
11176 found.end ());
8a2c437b 11177
81b1e71c 11178 if (found.size () > 1)
4a64f543 11179 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11180 if (from_tty)
a3f17187 11181 {
81b1e71c 11182 if (found.size () == 1)
6cb06a8c 11183 gdb_printf (_("Deleted breakpoint "));
a3f17187 11184 else
6cb06a8c 11185 gdb_printf (_("Deleted breakpoints "));
a3f17187 11186 }
d6e956e5 11187
81b1e71c 11188 for (breakpoint *iter : found)
80f8a6eb 11189 {
c5aa993b 11190 if (from_tty)
6cb06a8c 11191 gdb_printf ("%d ", iter->number);
81b1e71c 11192 delete_breakpoint (iter);
c906108c 11193 }
80f8a6eb 11194 if (from_tty)
a11ac3b3 11195 gdb_putc ('\n');
c906108c
SS
11196}
11197\f
11198/* Delete breakpoint in BS if they are `delete' breakpoints and
11199 all breakpoints that are marked for deletion, whether hit or not.
11200 This is called after any breakpoint is hit, or after errors. */
11201
11202void
313f3b21 11203breakpoint_auto_delete (bpstat *bs)
c906108c 11204{
c906108c 11205 for (; bs; bs = bs->next)
f431efe5
PA
11206 if (bs->breakpoint_at
11207 && bs->breakpoint_at->disposition == disp_del
c906108c 11208 && bs->stop)
f431efe5 11209 delete_breakpoint (bs->breakpoint_at);
c906108c 11210
a1decfc1
SM
11211 for (breakpoint &b : all_breakpoints_safe ())
11212 if (b.disposition == disp_del_at_next_stop)
11213 delete_breakpoint (&b);
c906108c
SS
11214}
11215
8dd54de0
TV
11216/* A comparison function for bp_location pointers A and B being interfaced to
11217 std::sort, for instance to sort an std::vector<bp_location *>. Sort
11218 elements:
11219 - primarily by their ADDRESS (no matter what bl_address_is_meaningful
11220 says),
11221 - secondarily by ordering first permanent elements, and
11222 - tertiarily just ensuring the array is sorted in a stable way despite
11223 std::sort being an unstable algorithm. */
876fa593 11224
8dd54de0
TV
11225static bool
11226bp_location_ptr_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11227{
876fa593 11228 if (a->address != b->address)
39ef2f62 11229 return a->address < b->address;
876fa593 11230
dea2aa5f
LM
11231 /* Sort locations at the same address by their pspace number, keeping
11232 locations of the same inferior (in a multi-inferior environment)
11233 grouped. */
11234
11235 if (a->pspace->num != b->pspace->num)
39ef2f62 11236 return a->pspace->num < b->pspace->num;
dea2aa5f 11237
876fa593 11238 /* Sort permanent breakpoints first. */
1a853c52 11239 if (a->permanent != b->permanent)
39ef2f62 11240 return a->permanent > b->permanent;
876fa593 11241
7f32a4d5
PA
11242 /* Sort by type in order to make duplicate determination easier.
11243 See update_global_location_list. This is kept in sync with
11244 breakpoint_locations_match. */
11245 if (a->loc_type < b->loc_type)
11246 return true;
11247
11248 /* Likewise, for range-breakpoints, sort by length. */
11249 if (a->loc_type == bp_loc_hardware_breakpoint
11250 && b->loc_type == bp_loc_hardware_breakpoint
11251 && a->length < b->length)
11252 return true;
11253
c56a97f9
JK
11254 /* Make the internal GDB representation stable across GDB runs
11255 where A and B memory inside GDB can differ. Breakpoint locations of
11256 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11257
11258 if (a->owner->number != b->owner->number)
39ef2f62 11259 return a->owner->number < b->owner->number;
876fa593 11260
39ef2f62 11261 return a < b;
876fa593
JK
11262}
11263
6b4f72a0
TV
11264/* A comparison function for bp_locations A and B being interfaced to
11265 std::sort, for instance to sort an std::vector<bp_location>. */
11266
11267static bool
11268bp_location_is_less_than (const bp_location &a, const bp_location &b)
11269{
11270 return bp_location_ptr_is_less_than (&a, &b);
11271}
11272
f5336ca5
PA
11273/* Set bp_locations_placed_address_before_address_max and
11274 bp_locations_shadow_len_after_address_max according to the current
11275 content of the bp_locations array. */
f7545552
TT
11276
11277static void
f5336ca5 11278bp_locations_target_extensions_update (void)
f7545552 11279{
f5336ca5
PA
11280 bp_locations_placed_address_before_address_max = 0;
11281 bp_locations_shadow_len_after_address_max = 0;
876fa593 11282
48d7020b 11283 for (bp_location *bl : all_bp_locations ())
876fa593
JK
11284 {
11285 CORE_ADDR start, end, addr;
11286
11287 if (!bp_location_has_shadow (bl))
11288 continue;
11289
11290 start = bl->target_info.placed_address;
11291 end = start + bl->target_info.shadow_len;
11292
11293 gdb_assert (bl->address >= start);
11294 addr = bl->address - start;
f5336ca5
PA
11295 if (addr > bp_locations_placed_address_before_address_max)
11296 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11297
11298 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11299
11300 gdb_assert (bl->address < end);
11301 addr = end - bl->address;
f5336ca5
PA
11302 if (addr > bp_locations_shadow_len_after_address_max)
11303 bp_locations_shadow_len_after_address_max = addr;
876fa593 11304 }
f7545552
TT
11305}
11306
1e4d1764
YQ
11307/* Download tracepoint locations if they haven't been. */
11308
11309static void
11310download_tracepoint_locations (void)
11311{
dd2e65cc 11312 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11313
5ed8105e 11314 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11315
a1decfc1 11316 for (breakpoint &b : all_tracepoints ())
1e4d1764 11317 {
d8de7963 11318 bool bp_location_downloaded = false;
1e4d1764 11319
a1decfc1 11320 if ((b.type == bp_fast_tracepoint
1e4d1764
YQ
11321 ? !may_insert_fast_tracepoints
11322 : !may_insert_tracepoints))
11323 continue;
11324
dd2e65cc
YQ
11325 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11326 {
11327 if (target_can_download_tracepoint ())
11328 can_download_tracepoint = TRIBOOL_TRUE;
11329 else
11330 can_download_tracepoint = TRIBOOL_FALSE;
11331 }
11332
11333 if (can_download_tracepoint == TRIBOOL_FALSE)
11334 break;
11335
a1decfc1 11336 for (bp_location &bl : b.locations ())
7ed2c994
YQ
11337 {
11338 /* In tracepoint, locations are _never_ duplicated, so
11339 should_be_inserted is equivalent to
11340 unduplicated_should_be_inserted. */
b00b30b2 11341 if (!should_be_inserted (&bl) || bl.inserted)
7ed2c994 11342 continue;
1e4d1764 11343
b00b30b2 11344 switch_to_program_space_and_thread (bl.pspace);
1e4d1764 11345
b00b30b2 11346 target_download_tracepoint (&bl);
1e4d1764 11347
b00b30b2 11348 bl.inserted = 1;
d8de7963 11349 bp_location_downloaded = true;
7ed2c994 11350 }
a1decfc1
SM
11351
11352 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
11353 t.number_on_target = b.number;
f2a8bc8a 11354 if (bp_location_downloaded)
19081eb5 11355 notify_breakpoint_modified (&b);
1e4d1764 11356 }
1e4d1764
YQ
11357}
11358
934709f0
PW
11359/* Swap the insertion/duplication state between two locations. */
11360
11361static void
11362swap_insertion (struct bp_location *left, struct bp_location *right)
11363{
11364 const int left_inserted = left->inserted;
11365 const int left_duplicate = left->duplicate;
b775012e 11366 const int left_needs_update = left->needs_update;
934709f0
PW
11367 const struct bp_target_info left_target_info = left->target_info;
11368
1e4d1764
YQ
11369 /* Locations of tracepoints can never be duplicated. */
11370 if (is_tracepoint (left->owner))
11371 gdb_assert (!left->duplicate);
11372 if (is_tracepoint (right->owner))
11373 gdb_assert (!right->duplicate);
11374
934709f0
PW
11375 left->inserted = right->inserted;
11376 left->duplicate = right->duplicate;
b775012e 11377 left->needs_update = right->needs_update;
934709f0
PW
11378 left->target_info = right->target_info;
11379 right->inserted = left_inserted;
11380 right->duplicate = left_duplicate;
b775012e 11381 right->needs_update = left_needs_update;
934709f0
PW
11382 right->target_info = left_target_info;
11383}
11384
b775012e
LM
11385/* Force the re-insertion of the locations at ADDRESS. This is called
11386 once a new/deleted/modified duplicate location is found and we are evaluating
11387 conditions on the target's side. Such conditions need to be updated on
11388 the target. */
11389
11390static void
11391force_breakpoint_reinsertion (struct bp_location *bl)
11392{
b775012e
LM
11393 CORE_ADDR address = 0;
11394 int pspace_num;
11395
11396 address = bl->address;
11397 pspace_num = bl->pspace->num;
11398
11399 /* This is only meaningful if the target is
11400 evaluating conditions and if the user has
11401 opted for condition evaluation on the target's
11402 side. */
11403 if (gdb_evaluates_breakpoint_condition_p ()
11404 || !target_supports_evaluation_of_breakpoint_conditions ())
11405 return;
11406
11407 /* Flag all breakpoint locations with this address and
11408 the same program space as the location
11409 as "its condition has changed". We need to
11410 update the conditions on the target's side. */
e0d9a270 11411 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11412 {
b775012e
LM
11413 if (!is_breakpoint (loc->owner)
11414 || pspace_num != loc->pspace->num)
11415 continue;
11416
11417 /* Flag the location appropriately. We use a different state to
11418 let everyone know that we already updated the set of locations
11419 with addr bl->address and program space bl->pspace. This is so
11420 we don't have to keep calling these functions just to mark locations
11421 that have already been marked. */
11422 loc->condition_changed = condition_updated;
11423
11424 /* Free the agent expression bytecode as well. We will compute
11425 it later on. */
833177a4 11426 loc->cond_bytecode.reset ();
b775012e
LM
11427 }
11428}
7f32a4d5 11429
44702360
PA
11430/* Called whether new breakpoints are created, or existing breakpoints
11431 deleted, to update the global location list and recompute which
11432 locations are duplicate of which.
b775012e 11433
04086b45
PA
11434 The INSERT_MODE flag determines whether locations may not, may, or
11435 shall be inserted now. See 'enum ugll_insert_mode' for more
11436 info. */
b60e7edf 11437
0d381245 11438static void
44702360 11439update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11440{
b775012e
LM
11441 /* Last breakpoint location address that was marked for update. */
11442 CORE_ADDR last_addr = 0;
11443 /* Last breakpoint location program space that was marked for update. */
11444 int last_pspace_num = -1;
f7545552 11445
3cdc2d7e
MS
11446 breakpoint_debug_printf ("insert_mode = %s",
11447 ugll_insert_mode_text (insert_mode));
11448
2d134ed3
PA
11449 /* Used in the duplicates detection below. When iterating over all
11450 bp_locations, points to the first bp_location of a given address.
11451 Breakpoints and watchpoints of different types are never
11452 duplicates of each other. Keep one pointer for each type of
11453 breakpoint/watchpoint, so we only need to loop over all locations
11454 once. */
11455 struct bp_location *bp_loc_first; /* breakpoint */
11456 struct bp_location *wp_loc_first; /* hardware watchpoint */
11457 struct bp_location *awp_loc_first; /* access watchpoint */
11458 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11459
f5336ca5
PA
11460 /* Saved former bp_locations array which we compare against the newly
11461 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11462 std::vector<bp_location *> old_locations = std::move (bp_locations);
11463 bp_locations.clear ();
876fa593 11464
a1decfc1
SM
11465 for (breakpoint &b : all_breakpoints ())
11466 for (bp_location &loc : b.locations ())
b00b30b2 11467 bp_locations.push_back (&loc);
7f32a4d5
PA
11468
11469 /* See if we need to "upgrade" a software breakpoint to a hardware
11470 breakpoint. Do this before deciding whether locations are
11471 duplicates. Also do this before sorting because sorting order
11472 depends on location type. */
5d51cd5d
SM
11473 for (bp_location *loc : bp_locations)
11474 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11475 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11476
5d51cd5d 11477 std::sort (bp_locations.begin (), bp_locations.end (),
8dd54de0 11478 bp_location_ptr_is_less_than);
876fa593 11479
f5336ca5 11480 bp_locations_target_extensions_update ();
74960c60 11481
4a64f543
MS
11482 /* Identify bp_location instances that are no longer present in the
11483 new list, and therefore should be freed. Note that it's not
11484 necessary that those locations should be removed from inferior --
11485 if there's another location at the same address (previously
11486 marked as duplicate), we don't need to remove/insert the
11487 location.
876fa593 11488
4a64f543
MS
11489 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11490 and former bp_location array state respectively. */
876fa593 11491
5d51cd5d
SM
11492 size_t loc_i = 0;
11493 for (bp_location *old_loc : old_locations)
74960c60 11494 {
e5dd4106 11495 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11496 not, we have to free it. */
d8de7963 11497 bool found_object = false;
20874c92 11498 /* Tells if the location should remain inserted in the target. */
d8de7963
AB
11499 bool keep_in_target = false;
11500 bool removed = false;
876fa593 11501
4a64f543
MS
11502 /* Skip LOCP entries which will definitely never be needed.
11503 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11504 while (loc_i < bp_locations.size ()
11505 && bp_locations[loc_i]->address < old_loc->address)
11506 loc_i++;
c7d46a38 11507
5d51cd5d
SM
11508 for (size_t loc2_i = loc_i;
11509 (loc2_i < bp_locations.size ()
11510 && bp_locations[loc2_i]->address == old_loc->address);
11511 loc2_i++)
c7d46a38 11512 {
b775012e
LM
11513 /* Check if this is a new/duplicated location or a duplicated
11514 location that had its condition modified. If so, we want to send
11515 its condition to the target if evaluation of conditions is taking
11516 place there. */
5d51cd5d 11517 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11518 && (last_addr != old_loc->address
11519 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11520 {
5d51cd5d 11521 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11522 last_pspace_num = old_loc->pspace->num;
c7d46a38 11523 }
b775012e 11524
5d51cd5d 11525 if (bp_locations[loc2_i] == old_loc)
d8de7963 11526 found_object = true;
c7d46a38 11527 }
74960c60 11528
b775012e
LM
11529 /* We have already handled this address, update it so that we don't
11530 have to go through updates again. */
11531 last_addr = old_loc->address;
11532
11533 /* Target-side condition evaluation: Handle deleted locations. */
11534 if (!found_object)
11535 force_breakpoint_reinsertion (old_loc);
11536
4a64f543
MS
11537 /* If this location is no longer present, and inserted, look if
11538 there's maybe a new location at the same address. If so,
11539 mark that one inserted, and don't remove this one. This is
11540 needed so that we don't have a time window where a breakpoint
11541 at certain location is not inserted. */
74960c60 11542
876fa593 11543 if (old_loc->inserted)
0d381245 11544 {
4a64f543
MS
11545 /* If the location is inserted now, we might have to remove
11546 it. */
74960c60 11547
876fa593 11548 if (found_object && should_be_inserted (old_loc))
74960c60 11549 {
4a64f543
MS
11550 /* The location is still present in the location list,
11551 and still should be inserted. Don't do anything. */
d8de7963 11552 keep_in_target = true;
74960c60
VP
11553 }
11554 else
11555 {
b775012e
LM
11556 /* This location still exists, but it won't be kept in the
11557 target since it may have been disabled. We proceed to
11558 remove its target-side condition. */
11559
4a64f543
MS
11560 /* The location is either no longer present, or got
11561 disabled. See if there's another location at the
11562 same address, in which case we don't need to remove
11563 this one from the target. */
876fa593 11564
2bdf28a0 11565 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11566 if (bl_address_is_meaningful (old_loc))
876fa593 11567 {
5d51cd5d
SM
11568 for (size_t loc2_i = loc_i;
11569 (loc2_i < bp_locations.size ()
11570 && bp_locations[loc2_i]->address == old_loc->address);
11571 loc2_i++)
876fa593 11572 {
5d51cd5d 11573 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11574
7f32a4d5
PA
11575 if (loc2 == old_loc)
11576 continue;
11577
2d134ed3 11578 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11579 {
85d721b8
PA
11580 /* Read watchpoint locations are switched to
11581 access watchpoints, if the former are not
11582 supported, but the latter are. */
11583 if (is_hardware_watchpoint (old_loc->owner))
11584 {
11585 gdb_assert (is_hardware_watchpoint (loc2->owner));
11586 loc2->watchpoint_type = old_loc->watchpoint_type;
11587 }
11588
934709f0
PW
11589 /* loc2 is a duplicated location. We need to check
11590 if it should be inserted in case it will be
11591 unduplicated. */
7f32a4d5 11592 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11593 {
934709f0 11594 swap_insertion (old_loc, loc2);
d8de7963 11595 keep_in_target = true;
c7d46a38
PA
11596 break;
11597 }
876fa593
JK
11598 }
11599 }
11600 }
74960c60
VP
11601 }
11602
20874c92
VP
11603 if (!keep_in_target)
11604 {
834c0d03 11605 if (remove_breakpoint (old_loc))
20874c92 11606 {
4a64f543
MS
11607 /* This is just about all we can do. We could keep
11608 this location on the global list, and try to
11609 remove it next time, but there's no particular
11610 reason why we will succeed next time.
20874c92 11611
4a64f543
MS
11612 Note that at this point, old_loc->owner is still
11613 valid, as delete_breakpoint frees the breakpoint
11614 only after calling us. */
a500c3d8
AB
11615 warning (_("error removing breakpoint %d at %s"),
11616 old_loc->owner->number,
11617 paddress (old_loc->gdbarch, old_loc->address));
20874c92 11618 }
d8de7963 11619 removed = true;
20874c92 11620 }
0d381245 11621 }
74960c60
VP
11622
11623 if (!found_object)
1c5cfe86 11624 {
fbea99ea 11625 if (removed && target_is_non_stop_p ()
1cf4d951 11626 && need_moribund_for_location_type (old_loc))
20874c92 11627 {
db82e815
PA
11628 /* This location was removed from the target. In
11629 non-stop mode, a race condition is possible where
11630 we've removed a breakpoint, but stop events for that
11631 breakpoint are already queued and will arrive later.
11632 We apply an heuristic to be able to distinguish such
11633 SIGTRAPs from other random SIGTRAPs: we keep this
11634 breakpoint location for a bit, and will retire it
11635 after we see some number of events. The theory here
11636 is that reporting of events should, "on the average",
11637 be fair, so after a while we'll see events from all
11638 threads that have anything of interest, and no longer
11639 need to keep this breakpoint location around. We
11640 don't hold locations forever so to reduce chances of
11641 mistaking a non-breakpoint SIGTRAP for a breakpoint
11642 SIGTRAP.
11643
11644 The heuristic failing can be disastrous on
11645 decr_pc_after_break targets.
11646
11647 On decr_pc_after_break targets, like e.g., x86-linux,
11648 if we fail to recognize a late breakpoint SIGTRAP,
11649 because events_till_retirement has reached 0 too
11650 soon, we'll fail to do the PC adjustment, and report
11651 a random SIGTRAP to the user. When the user resumes
11652 the inferior, it will most likely immediately crash
2dec564e 11653 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11654 corrupted, because of being resumed e.g., in the
11655 middle of a multi-byte instruction, or skipped a
11656 one-byte instruction. This was actually seen happen
11657 on native x86-linux, and should be less rare on
11658 targets that do not support new thread events, like
11659 remote, due to the heuristic depending on
11660 thread_count.
11661
11662 Mistaking a random SIGTRAP for a breakpoint trap
11663 causes similar symptoms (PC adjustment applied when
11664 it shouldn't), but then again, playing with SIGTRAPs
11665 behind the debugger's back is asking for trouble.
11666
11667 Since hardware watchpoint traps are always
11668 distinguishable from other traps, so we don't need to
11669 apply keep hardware watchpoint moribund locations
11670 around. We simply always ignore hardware watchpoint
11671 traps we can no longer explain. */
11672
5b6d1e4f
PA
11673 process_stratum_target *proc_target = nullptr;
11674 for (inferior *inf : all_inferiors ())
11675 if (inf->pspace == old_loc->pspace)
11676 {
11677 proc_target = inf->process_target ();
11678 break;
11679 }
11680 if (proc_target != nullptr)
11681 old_loc->events_till_retirement
11682 = 3 * (thread_count (proc_target) + 1);
11683 else
11684 old_loc->events_till_retirement = 1;
876fa593 11685 old_loc->owner = NULL;
20874c92 11686
1123588c 11687 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11688 }
11689 else
f431efe5
PA
11690 {
11691 old_loc->owner = NULL;
11692 decref_bp_location (&old_loc);
11693 }
20874c92 11694 }
74960c60 11695 }
1c5cfe86 11696
348d480f
PA
11697 /* Rescan breakpoints at the same address and section, marking the
11698 first one as "first" and any others as "duplicates". This is so
11699 that the bpt instruction is only inserted once. If we have a
11700 permanent breakpoint at the same place as BPT, make that one the
11701 official one, and the rest as duplicates. Permanent breakpoints
11702 are sorted first for the same address.
11703
11704 Do the same for hardware watchpoints, but also considering the
11705 watchpoint's type (regular/access/read) and length. */
11706
11707 bp_loc_first = NULL;
11708 wp_loc_first = NULL;
11709 awp_loc_first = NULL;
11710 rwp_loc_first = NULL;
40cb8ca5 11711
48d7020b 11712 for (bp_location *loc : all_bp_locations ())
348d480f
PA
11713 {
11714 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11715 non-NULL. */
348d480f 11716 struct bp_location **loc_first_p;
43892fdf 11717 breakpoint *b = loc->owner;
348d480f 11718
6f380991 11719 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 11720 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
11721 /* Don't detect duplicate for tracepoint locations because they are
11722 never duplicated. See the comments in field `duplicate' of
11723 `struct bp_location'. */
348d480f 11724 || is_tracepoint (b))
b775012e
LM
11725 {
11726 /* Clear the condition modification flag. */
11727 loc->condition_changed = condition_unchanged;
11728 continue;
11729 }
348d480f 11730
348d480f
PA
11731 if (b->type == bp_hardware_watchpoint)
11732 loc_first_p = &wp_loc_first;
11733 else if (b->type == bp_read_watchpoint)
11734 loc_first_p = &rwp_loc_first;
11735 else if (b->type == bp_access_watchpoint)
11736 loc_first_p = &awp_loc_first;
11737 else
11738 loc_first_p = &bp_loc_first;
11739
11740 if (*loc_first_p == NULL
11741 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11742 || !breakpoint_locations_match (loc, *loc_first_p))
11743 {
11744 *loc_first_p = loc;
11745 loc->duplicate = 0;
b775012e
LM
11746
11747 if (is_breakpoint (loc->owner) && loc->condition_changed)
11748 {
11749 loc->needs_update = 1;
11750 /* Clear the condition modification flag. */
11751 loc->condition_changed = condition_unchanged;
11752 }
348d480f
PA
11753 continue;
11754 }
11755
934709f0
PW
11756
11757 /* This and the above ensure the invariant that the first location
11758 is not duplicated, and is the inserted one.
11759 All following are marked as duplicated, and are not inserted. */
11760 if (loc->inserted)
11761 swap_insertion (loc, *loc_first_p);
348d480f
PA
11762 loc->duplicate = 1;
11763
b775012e
LM
11764 /* Clear the condition modification flag. */
11765 loc->condition_changed = condition_unchanged;
348d480f
PA
11766 }
11767
a25a5a45 11768 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 11769 {
04086b45 11770 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
11771 insert_breakpoint_locations ();
11772 else
11773 {
44702360
PA
11774 /* Even though the caller told us to not insert new
11775 locations, we may still need to update conditions on the
11776 target's side of breakpoints that were already inserted
11777 if the target is evaluating breakpoint conditions. We
b775012e
LM
11778 only update conditions for locations that are marked
11779 "needs_update". */
11780 update_inserted_breakpoint_locations ();
11781 }
11782 }
348d480f 11783
04086b45 11784 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 11785 download_tracepoint_locations ();
348d480f
PA
11786}
11787
11788void
11789breakpoint_retire_moribund (void)
11790{
1123588c
TT
11791 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11792 {
11793 struct bp_location *loc = moribund_locations[ix];
11794 if (--(loc->events_till_retirement) == 0)
11795 {
11796 decref_bp_location (&loc);
11797 unordered_remove (moribund_locations, ix);
11798 --ix;
11799 }
11800 }
348d480f
PA
11801}
11802
11803static void
44702360 11804update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 11805{
348d480f 11806
a70b8144 11807 try
492d29ea
PA
11808 {
11809 update_global_location_list (insert_mode);
11810 }
230d2906 11811 catch (const gdb_exception_error &e)
492d29ea
PA
11812 {
11813 }
348d480f
PA
11814}
11815
11816/* Clear BKP from a BPS. */
11817
11818static void
313f3b21 11819bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
348d480f 11820{
313f3b21 11821 bpstat *bs;
348d480f
PA
11822
11823 for (bs = bps; bs; bs = bs->next)
11824 if (bs->breakpoint_at == bpt)
11825 {
11826 bs->breakpoint_at = NULL;
11827 bs->old_val = NULL;
11828 /* bs->commands will be freed later. */
11829 }
11830}
11831
7987c463 11832/* See breakpoint.h. */
348d480f 11833
7987c463
TT
11834void
11835code_breakpoint::say_where () const
348d480f
PA
11836{
11837 struct value_print_options opts;
11838
11839 get_user_print_options (&opts);
11840
11841 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11842 single string. */
9dc1523b 11843 if (!this->has_locations ())
348d480f 11844 {
f00aae0f 11845 /* For pending locations, the output differs slightly based
7987c463 11846 on extra_string. If this is non-NULL, it contains either
f00aae0f 11847 a condition or dprintf arguments. */
7987c463 11848 if (extra_string == NULL)
f00aae0f 11849 {
7987c463 11850 gdb_printf (_(" (%s) pending."), locspec->to_string ());
f00aae0f 11851 }
7987c463 11852 else if (type == bp_dprintf)
f00aae0f 11853 {
6cb06a8c 11854 gdb_printf (_(" (%s,%s) pending."),
7987c463
TT
11855 locspec->to_string (),
11856 extra_string.get ());
f00aae0f
KS
11857 }
11858 else
11859 {
6cb06a8c 11860 gdb_printf (_(" (%s %s) pending."),
7987c463
TT
11861 locspec->to_string (),
11862 extra_string.get ());
f00aae0f 11863 }
348d480f
PA
11864 }
11865 else
11866 {
f5951b9f
SM
11867 const bp_location &bl = this->first_loc ();
11868 if (opts.addressprint || bl.symtab == nullptr)
6cb06a8c
TT
11869 gdb_printf (" at %ps",
11870 styled_string (address_style.style (),
f5951b9f
SM
11871 paddress (bl.gdbarch,
11872 bl.address)));
11873 if (bl.symtab != NULL)
f8eba3c6
TT
11874 {
11875 /* If there is a single location, we can print the location
11876 more nicely. */
9dc1523b 11877 if (!this->has_multiple_locations ())
0bb296cb 11878 {
6a831f06 11879 const char *filename
f5951b9f 11880 = symtab_to_filename_for_display (bl.symtab);
887ae0cf 11881 gdb_printf (": file %ps, line %ps.",
6cb06a8c
TT
11882 styled_string (file_name_style.style (),
11883 filename),
887ae0cf
TT
11884 styled_string (line_number_style.style (),
11885 pulongest (bl.line_number)));
0bb296cb 11886 }
f8eba3c6
TT
11887 else
11888 /* This is not ideal, but each location may have a
11889 different file name, and this at least reflects the
11890 real situation somewhat. */
7987c463 11891 gdb_printf (": %s.", locspec->to_string ());
f8eba3c6 11892 }
348d480f 11893
9dc1523b 11894 if (this->has_multiple_locations ())
348d480f 11895 {
20afe380 11896 int n = std::distance (m_locations.begin (), m_locations.end ());
6cb06a8c 11897 gdb_printf (" (%d locations)", n);
348d480f
PA
11898 }
11899 }
11900}
11901
40cb8ca5
SM
11902/* See breakpoint.h. */
11903
71e08508
TT
11904bp_location_range
11905breakpoint::locations () const
40cb8ca5 11906{
b00b30b2 11907 return bp_location_range (m_locations.begin (), m_locations.end ());
40cb8ca5
SM
11908}
11909
4c6a92b1
TT
11910struct bp_location *
11911breakpoint::allocate_location ()
11912{
11913 return new bp_location (this);
11914}
11915
20afe380
SM
11916/* See breakpoint.h. */
11917
11918void
11919breakpoint::add_location (bp_location &loc)
11920{
11921 gdb_assert (loc.owner == this);
11922 gdb_assert (!loc.is_linked ());
11923
11924 auto ub = std::upper_bound (m_locations.begin (), m_locations.end (),
11925 loc,
6b4f72a0 11926 bp_location_is_less_than);
20afe380
SM
11927 m_locations.insert (ub, loc);
11928}
11929
11930/* See breakpoint.h. */
11931
11932void
11933breakpoint::unadd_location (bp_location &loc)
11934{
11935 gdb_assert (loc.owner == this);
11936 gdb_assert (loc.is_linked ());
11937
11938 m_locations.erase (m_locations.iterator_to (loc));
11939}
11940
2060206e
PA
11941#define internal_error_pure_virtual_called() \
11942 gdb_assert_not_reached ("pure virtual function called")
11943
4c6a92b1
TT
11944int
11945breakpoint::insert_location (struct bp_location *bl)
11946{
11947 internal_error_pure_virtual_called ();
11948}
11949
4c6a92b1
TT
11950int
11951breakpoint::remove_location (struct bp_location *bl,
11952 enum remove_bp_reason reason)
11953{
11954 internal_error_pure_virtual_called ();
11955}
11956
4c6a92b1
TT
11957int
11958breakpoint::breakpoint_hit (const struct bp_location *bl,
11959 const address_space *aspace,
11960 CORE_ADDR bp_addr,
11961 const target_waitstatus &ws)
11962{
11963 internal_error_pure_virtual_called ();
11964}
11965
4c6a92b1
TT
11966int
11967breakpoint::resources_needed (const struct bp_location *bl)
11968{
11969 internal_error_pure_virtual_called ();
11970}
11971
4c6a92b1 11972enum print_stop_action
7bd86313 11973breakpoint::print_it (const bpstat *bs) const
4c6a92b1
TT
11974{
11975 internal_error_pure_virtual_called ();
11976}
11977
4c6a92b1 11978void
b713485d 11979breakpoint::print_mention () const
4c6a92b1
TT
11980{
11981 internal_error_pure_virtual_called ();
11982}
11983
4c6a92b1 11984void
4d1ae558 11985breakpoint::print_recreate (struct ui_file *fp) const
4c6a92b1
TT
11986{
11987 internal_error_pure_virtual_called ();
11988}
11989
2060206e
PA
11990/* Default breakpoint_ops methods. */
11991
2b5ab5b8 11992void
6cce0251 11993code_breakpoint::re_set (struct program_space *pspace)
348d480f 11994{
06edf0c0 11995 /* FIXME: is this still reachable? */
264f9890 11996 if (breakpoint_location_spec_empty_p (this))
06edf0c0 11997 {
f00aae0f 11998 /* Anything without a location can't be re-set. */
2b5ab5b8 11999 delete_breakpoint (this);
06edf0c0 12000 return;
348d480f 12001 }
06edf0c0 12002
6cce0251 12003 re_set_default (pspace);
348d480f
PA
12004}
12005
2b5ab5b8 12006int
74421c0b 12007code_breakpoint::insert_location (struct bp_location *bl)
348d480f 12008{
cd6c3b4f
YQ
12009 CORE_ADDR addr = bl->target_info.reqstd_address;
12010
579c6ad9 12011 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12012 bl->target_info.placed_address = addr;
12013
5d926615 12014 int result;
348d480f 12015 if (bl->loc_type == bp_loc_hardware_breakpoint)
5d926615 12016 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12017 else
5d926615
TT
12018 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12019
12020 if (result == 0 && bl->probe.prob != nullptr)
12021 {
12022 /* The insertion was successful, now let's set the probe's semaphore
12023 if needed. */
12024 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12025 }
12026
12027 return result;
348d480f
PA
12028}
12029
2b5ab5b8 12030int
74421c0b 12031code_breakpoint::remove_location (struct bp_location *bl,
2b5ab5b8 12032 enum remove_bp_reason reason)
348d480f 12033{
5d926615
TT
12034 if (bl->probe.prob != nullptr)
12035 {
12036 /* Let's clear the semaphore before removing the location. */
12037 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12038 }
12039
348d480f
PA
12040 if (bl->loc_type == bp_loc_hardware_breakpoint)
12041 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12042 else
73971819 12043 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12044}
12045
2b5ab5b8 12046int
74421c0b 12047code_breakpoint::breakpoint_hit (const struct bp_location *bl,
2b5ab5b8
TT
12048 const address_space *aspace,
12049 CORE_ADDR bp_addr,
12050 const target_waitstatus &ws)
348d480f 12051{
c272a98c
SM
12052 if (ws.kind () != TARGET_WAITKIND_STOPPED
12053 || ws.sig () != GDB_SIGNAL_TRAP)
09ac7c10
TT
12054 return 0;
12055
f9582a22 12056 if (!breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
348d480f
PA
12057 aspace, bp_addr))
12058 return 0;
12059
12060 if (overlay_debugging /* unmapped overlay section */
12061 && section_is_overlay (bl->section)
12062 && !section_is_mapped (bl->section))
12063 return 0;
12064
12065 return 1;
12066}
12067
f293a0b5
TT
12068int
12069dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
12070 const address_space *aspace,
12071 CORE_ADDR bp_addr,
12072 const target_waitstatus &ws)
cd1608cc
PA
12073{
12074 if (dprintf_style == dprintf_style_agent
12075 && target_can_run_breakpoint_commands ())
12076 {
12077 /* An agent-style dprintf never causes a stop. If we see a trap
12078 for this address it must be for a breakpoint that happens to
12079 be set at the same address. */
12080 return 0;
12081 }
12082
f293a0b5 12083 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
cd1608cc
PA
12084}
12085
7dd8e7ae
TT
12086int
12087ordinary_breakpoint::resources_needed (const struct bp_location *bl)
348d480f 12088{
7dd8e7ae 12089 gdb_assert (type == bp_hardware_breakpoint);
348d480f
PA
12090
12091 return 1;
12092}
12093
7dd8e7ae 12094enum print_stop_action
7bd86313 12095ordinary_breakpoint::print_it (const bpstat *bs) const
348d480f 12096{
348d480f 12097 const struct bp_location *bl;
001c8c33 12098 int bp_temp;
79a45e25 12099 struct ui_out *uiout = current_uiout;
348d480f 12100
b6433ede 12101 bl = bs->bp_location_at.get ();
348d480f 12102
7dd8e7ae 12103 bp_temp = disposition == disp_del;
001c8c33
PA
12104 if (bl->address != bl->requested_address)
12105 breakpoint_adjustment_warning (bl->requested_address,
12106 bl->address,
d8de7963 12107 number, true);
7dd8e7ae 12108 annotate_breakpoint (number);
f303dbd6
PA
12109 maybe_print_thread_hit_breakpoint (uiout);
12110
112e8700 12111 if (uiout->is_mi_like_p ())
348d480f 12112 {
112e8700 12113 uiout->field_string ("reason",
001c8c33 12114 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7dd8e7ae 12115 uiout->field_string ("disp", bpdisp_text (disposition));
06edf0c0 12116 }
78805ff8 12117
6a831f06 12118 if (bp_temp)
78805ff8 12119 uiout->text ("Temporary breakpoint ");
6a831f06 12120 else
78805ff8
PW
12121 uiout->text ("Breakpoint ");
12122 print_num_locno (bs, uiout);
12123 uiout->text (", ");
06edf0c0 12124
001c8c33 12125 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12126}
12127
7dd8e7ae 12128void
b713485d 12129ordinary_breakpoint::print_mention () const
06edf0c0 12130{
112e8700 12131 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12132 return;
12133
7dd8e7ae 12134 switch (type)
06edf0c0
PA
12135 {
12136 case bp_breakpoint:
12137 case bp_gnu_ifunc_resolver:
7dd8e7ae 12138 if (disposition == disp_del)
6cb06a8c 12139 gdb_printf (_("Temporary breakpoint"));
06edf0c0 12140 else
6cb06a8c 12141 gdb_printf (_("Breakpoint"));
7dd8e7ae
TT
12142 gdb_printf (_(" %d"), number);
12143 if (type == bp_gnu_ifunc_resolver)
6cb06a8c 12144 gdb_printf (_(" at gnu-indirect-function resolver"));
06edf0c0
PA
12145 break;
12146 case bp_hardware_breakpoint:
7dd8e7ae 12147 gdb_printf (_("Hardware assisted breakpoint %d"), number);
06edf0c0 12148 break;
e7e0cddf 12149 case bp_dprintf:
7dd8e7ae 12150 gdb_printf (_("Dprintf %d"), number);
e7e0cddf 12151 break;
06edf0c0
PA
12152 }
12153
7987c463 12154 say_where ();
06edf0c0
PA
12155}
12156
7dd8e7ae 12157void
4d1ae558 12158ordinary_breakpoint::print_recreate (struct ui_file *fp) const
06edf0c0 12159{
7dd8e7ae 12160 if (type == bp_breakpoint && disposition == disp_del)
6cb06a8c 12161 gdb_printf (fp, "tbreak");
7dd8e7ae 12162 else if (type == bp_breakpoint)
6cb06a8c 12163 gdb_printf (fp, "break");
7dd8e7ae
TT
12164 else if (type == bp_hardware_breakpoint
12165 && disposition == disp_del)
6cb06a8c 12166 gdb_printf (fp, "thbreak");
7dd8e7ae 12167 else if (type == bp_hardware_breakpoint)
6cb06a8c 12168 gdb_printf (fp, "hbreak");
06edf0c0 12169 else
f34652de 12170 internal_error (_("unhandled breakpoint type %d"), (int) type);
06edf0c0 12171
709438c7 12172 gdb_printf (fp, " %s", locspec->to_string ());
f00aae0f
KS
12173
12174 /* Print out extra_string if this breakpoint is pending. It might
12175 contain, for example, conditions that were set by the user. */
9dc1523b 12176 if (!this->has_locations () && extra_string != NULL)
7dd8e7ae 12177 gdb_printf (fp, " %s", extra_string.get ());
f00aae0f 12178
04d0163c 12179 print_recreate_thread (fp);
06edf0c0
PA
12180}
12181
2b5ab5b8 12182std::vector<symtab_and_line>
264f9890
PA
12183code_breakpoint::decode_location_spec (location_spec *locspec,
12184 program_space *search_pspace)
983af33b 12185{
7464aeaa 12186 if (locspec->type () == PROBE_LOCATION_SPEC)
264f9890 12187 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
5d926615 12188
8f5bc641
TT
12189 struct linespec_result canonical;
12190
12191 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12192 NULL, 0, &canonical, multiple_symbols_all,
12193 filter.get ());
12194
12195 /* We should get 0 or 1 resulting SALs. */
12196 gdb_assert (canonical.lsals.size () < 2);
12197
12198 if (!canonical.lsals.empty ())
12199 {
12200 const linespec_sals &lsal = canonical.lsals[0];
12201 return std::move (lsal.sals);
12202 }
12203 return {};
983af33b
SDJ
12204}
12205
06edf0c0
PA
12206/* Virtual table for internal breakpoints. */
12207
c359fff5 12208void
6cce0251 12209internal_breakpoint::re_set (struct program_space *pspace)
06edf0c0 12210{
c359fff5 12211 switch (type)
06edf0c0
PA
12212 {
12213 /* Delete overlay event and longjmp master breakpoints; they
12214 will be reset later by breakpoint_re_set. */
12215 case bp_overlay_event:
12216 case bp_longjmp_master:
12217 case bp_std_terminate_master:
12218 case bp_exception_master:
c359fff5 12219 delete_breakpoint (this);
06edf0c0
PA
12220 break;
12221
12222 /* This breakpoint is special, it's set up when the inferior
dda83cd7 12223 starts and we really don't want to touch it. */
06edf0c0
PA
12224 case bp_shlib_event:
12225
12226 /* Like bp_shlib_event, this breakpoint type is special. Once
12227 it is set up, we do not want to touch it. */
12228 case bp_thread_event:
12229 break;
12230 }
12231}
12232
c359fff5
TT
12233void
12234internal_breakpoint::check_status (bpstat *bs)
06edf0c0 12235{
c359fff5 12236 if (type == bp_shlib_event)
a9b3a50f
PA
12237 {
12238 /* If requested, stop when the dynamic linker notifies GDB of
12239 events. This allows the user to get control and place
12240 breakpoints in initializer routines for dynamically loaded
12241 objects (among other things). */
4ec2227a
TT
12242 bs->stop = stop_on_solib_events != 0;
12243 bs->print = stop_on_solib_events != 0;
a9b3a50f
PA
12244 }
12245 else
4ec2227a 12246 bs->stop = false;
06edf0c0
PA
12247}
12248
c359fff5 12249enum print_stop_action
7bd86313 12250internal_breakpoint::print_it (const bpstat *bs) const
06edf0c0 12251{
c359fff5 12252 switch (type)
06edf0c0 12253 {
348d480f
PA
12254 case bp_shlib_event:
12255 /* Did we stop because the user set the stop_on_solib_events
12256 variable? (If so, we report this as a generic, "Stopped due
12257 to shlib event" message.) */
4e9e993a 12258 print_solib_event (false);
348d480f
PA
12259 break;
12260
12261 case bp_thread_event:
12262 /* Not sure how we will get here.
12263 GDB should not stop for these breakpoints. */
6cb06a8c 12264 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12265 break;
12266
12267 case bp_overlay_event:
12268 /* By analogy with the thread event, GDB should not stop for these. */
6cb06a8c 12269 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12270 break;
12271
12272 case bp_longjmp_master:
12273 /* These should never be enabled. */
6cb06a8c 12274 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12275 break;
12276
12277 case bp_std_terminate_master:
12278 /* These should never be enabled. */
6cb06a8c
TT
12279 gdb_printf (_("std::terminate Master Breakpoint: "
12280 "gdb should not stop!\n"));
348d480f
PA
12281 break;
12282
12283 case bp_exception_master:
12284 /* These should never be enabled. */
6cb06a8c
TT
12285 gdb_printf (_("Exception Master Breakpoint: "
12286 "gdb should not stop!\n"));
06edf0c0
PA
12287 break;
12288 }
12289
001c8c33 12290 return PRINT_NOTHING;
06edf0c0
PA
12291}
12292
c359fff5 12293void
b713485d 12294internal_breakpoint::print_mention () const
06edf0c0
PA
12295{
12296 /* Nothing to mention. These breakpoints are internal. */
12297}
12298
06edf0c0
PA
12299/* Virtual table for momentary breakpoints */
12300
1fd30a47 12301void
6cce0251 12302momentary_breakpoint::re_set (struct program_space *pspace)
06edf0c0
PA
12303{
12304 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12305 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12306 Otherwise these should have been blown away via the cleanup chain
12307 or by breakpoint_init_inferior when we rerun the executable. */
12308}
12309
1fd30a47
TT
12310void
12311momentary_breakpoint::check_status (bpstat *bs)
06edf0c0
PA
12312{
12313 /* Nothing. The point of these breakpoints is causing a stop. */
12314}
12315
1fd30a47 12316enum print_stop_action
7bd86313 12317momentary_breakpoint::print_it (const bpstat *bs) const
06edf0c0 12318{
001c8c33 12319 return PRINT_UNKNOWN;
348d480f
PA
12320}
12321
1fd30a47 12322void
b713485d 12323momentary_breakpoint::print_mention () const
348d480f 12324{
06edf0c0 12325 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12326}
12327
e2e4d78b
JK
12328/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12329
12330 It gets cleared already on the removal of the first one of such placed
12331 breakpoints. This is OK as they get all removed altogether. */
12332
c1fc2657 12333longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12334{
c1fc2657 12335 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12336
c1fc2657 12337 if (tp != NULL)
e2e4d78b 12338 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12339}
12340
55aa24fb 12341static void
264f9890 12342bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
6cce0251
AB
12343 struct linespec_result *canonical,
12344 struct program_space *search_pspace)
03ada39e 12345
55aa24fb
SDJ
12346{
12347 struct linespec_sals lsal;
12348
6cce0251 12349 lsal.sals = parse_probes (locspec, search_pspace, canonical);
709438c7 12350 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12351 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12352}
12353
6c5b2ebe 12354static std::vector<symtab_and_line>
264f9890
PA
12355bkpt_probe_decode_location_spec (struct breakpoint *b,
12356 location_spec *locspec,
12357 program_space *search_pspace)
55aa24fb 12358{
264f9890
PA
12359 std::vector<symtab_and_line> sals
12360 = parse_probes (locspec, search_pspace, NULL);
6c5b2ebe 12361 if (sals.empty ())
55aa24fb 12362 error (_("probe not found"));
6c5b2ebe 12363 return sals;
55aa24fb
SDJ
12364}
12365
779dcceb
TT
12366int
12367tracepoint::breakpoint_hit (const struct bp_location *bl,
12368 const address_space *aspace, CORE_ADDR bp_addr,
12369 const target_waitstatus &ws)
348d480f
PA
12370{
12371 /* By definition, the inferior does not report stops at
12372 tracepoints. */
12373 return 0;
74960c60
VP
12374}
12375
779dcceb
TT
12376void
12377tracepoint::print_one_detail (struct ui_out *uiout) const
74960c60 12378{
779dcceb 12379 if (!static_trace_marker_id.empty ())
348d480f 12380 {
7b572efb
TT
12381 gdb_assert (type == bp_static_tracepoint
12382 || type == bp_static_marker_tracepoint);
cc59ec59 12383
6a831f06
PA
12384 uiout->message ("\tmarker id is %pF\n",
12385 string_field ("static-tracepoint-marker-string-id",
779dcceb 12386 static_trace_marker_id.c_str ()));
348d480f 12387 }
0d381245
VP
12388}
12389
779dcceb 12390void
b713485d 12391tracepoint::print_mention () const
a474d7c2 12392{
112e8700 12393 if (current_uiout->is_mi_like_p ())
348d480f 12394 return;
cc59ec59 12395
779dcceb 12396 switch (type)
348d480f
PA
12397 {
12398 case bp_tracepoint:
6cb06a8c 12399 gdb_printf (_("Tracepoint"));
779dcceb 12400 gdb_printf (_(" %d"), number);
348d480f
PA
12401 break;
12402 case bp_fast_tracepoint:
6cb06a8c 12403 gdb_printf (_("Fast tracepoint"));
779dcceb 12404 gdb_printf (_(" %d"), number);
348d480f
PA
12405 break;
12406 case bp_static_tracepoint:
7b572efb 12407 case bp_static_marker_tracepoint:
6cb06a8c 12408 gdb_printf (_("Static tracepoint"));
779dcceb 12409 gdb_printf (_(" %d"), number);
348d480f
PA
12410 break;
12411 default:
f34652de 12412 internal_error (_("unhandled tracepoint type %d"), (int) type);
348d480f
PA
12413 }
12414
7987c463 12415 say_where ();
a474d7c2
PA
12416}
12417
779dcceb 12418void
4d1ae558 12419tracepoint::print_recreate (struct ui_file *fp) const
a474d7c2 12420{
779dcceb 12421 if (type == bp_fast_tracepoint)
6cb06a8c 12422 gdb_printf (fp, "ftrace");
7b572efb
TT
12423 else if (type == bp_static_tracepoint
12424 || type == bp_static_marker_tracepoint)
6cb06a8c 12425 gdb_printf (fp, "strace");
779dcceb 12426 else if (type == bp_tracepoint)
6cb06a8c 12427 gdb_printf (fp, "trace");
348d480f 12428 else
f34652de 12429 internal_error (_("unhandled tracepoint type %d"), (int) type);
cc59ec59 12430
709438c7 12431 gdb_printf (fp, " %s", locspec->to_string ());
04d0163c 12432 print_recreate_thread (fp);
d9b3f62e 12433
779dcceb
TT
12434 if (pass_count)
12435 gdb_printf (fp, " passcount %d\n", pass_count);
a474d7c2
PA
12436}
12437
f293a0b5 12438void
6cce0251 12439dprintf_breakpoint::re_set (struct program_space *pspace)
5c2b4418 12440{
6cce0251 12441 re_set_default (pspace);
5c2b4418 12442
5c2b4418
HZ
12443 /* 1 - connect to target 1, that can run breakpoint commands.
12444 2 - create a dprintf, which resolves fine.
12445 3 - disconnect from target 1
12446 4 - connect to target 2, that can NOT run breakpoint commands.
12447
12448 After steps #3/#4, you'll want the dprintf command list to
12449 be updated, because target 1 and 2 may well return different
12450 answers for target_can_run_breakpoint_commands().
12451 Given absence of finer grained resetting, we get to do
12452 it all the time. */
437d237a 12453 update_dprintf_command_list (this);
5c2b4418
HZ
12454}
12455
f293a0b5 12456/* Implement the "print_recreate" method for dprintf. */
2d9442cc 12457
f293a0b5 12458void
4d1ae558 12459dprintf_breakpoint::print_recreate (struct ui_file *fp) const
2d9442cc 12460{
709438c7 12461 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
04d0163c 12462 print_recreate_thread (fp);
2d9442cc
HZ
12463}
12464
f293a0b5 12465/* Implement the "after_condition_true" method for dprintf.
9d6e6e84
HZ
12466
12467 dprintf's are implemented with regular commands in their command
12468 list, but we run the commands here instead of before presenting the
12469 stop to the user, as dprintf's don't actually cause a stop. This
12470 also makes it so that the commands of multiple dprintfs at the same
12471 address are all handled. */
12472
f293a0b5
TT
12473void
12474dprintf_breakpoint::after_condition_true (struct bpstat *bs)
9d6e6e84 12475{
9d6e6e84
HZ
12476 /* dprintf's never cause a stop. This wasn't set in the
12477 check_status hook instead because that would make the dprintf's
12478 condition not be evaluated. */
4ec2227a 12479 bs->stop = false;
9d6e6e84
HZ
12480
12481 /* Run the command list here. Take ownership of it instead of
12482 copying. We never want these commands to run later in
12483 bpstat_do_actions, if a breakpoint that causes a stop happens to
12484 be set at same address as this dprintf, or even if running the
12485 commands here throws. */
9c95aea1
KB
12486 counted_command_line cmds = std::move (bs->commands);
12487 gdb_assert (cmds != nullptr);
12488 execute_control_commands (cmds.get (), 0);
9d6e6e84
HZ
12489}
12490
983af33b
SDJ
12491/* The breakpoint_ops structure to be used on static tracepoints with
12492 markers (`-m'). */
12493
12494static void
6cce0251
AB
12495strace_marker_create_sals_from_location_spec
12496 (location_spec *locspec,
12497 struct linespec_result *canonical,
12498 struct program_space *search_pspace)
983af33b
SDJ
12499{
12500 struct linespec_sals lsal;
f00aae0f 12501 const char *arg_start, *arg;
983af33b 12502
e7cdec66 12503 arg = arg_start = as_linespec_location_spec (locspec)->spec_string.get ();
f00aae0f 12504 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 12505
f2fc3015
TT
12506 std::string str (arg_start, arg - arg_start);
12507 const char *ptr = str.c_str ();
264f9890
PA
12508 canonical->locspec
12509 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
983af33b 12510
709438c7 12511 lsal.canonical = xstrdup (canonical->locspec->to_string ());
6c5b2ebe 12512 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
12513}
12514
12515static void
12516strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12517 struct linespec_result *canonical,
e1e01040
PA
12518 gdb::unique_xmalloc_ptr<char> cond_string,
12519 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12520 enum bptype type_wanted,
12521 enum bpdisp disposition,
12522 int thread,
b080fe54
AB
12523 int task, int inferior,
12524 int ignore_count,
983af33b 12525 int from_tty, int enabled,
44f238bb 12526 int internal, unsigned flags)
983af33b 12527{
6c5b2ebe 12528 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
12529
12530 /* If the user is creating a static tracepoint by marker id
12531 (strace -m MARKER_ID), then store the sals index, so that
12532 breakpoint_re_set can try to match up which of the newly
12533 found markers corresponds to this one, and, don't try to
12534 expand multiple locations for each sal, given than SALS
12535 already should contain all sals for MARKER_ID. */
12536
6c5b2ebe 12537 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 12538 {
5c1ddcb6 12539 location_spec_up locspec = canonical->locspec->clone ();
983af33b 12540
3b003a61
PA
12541 std::unique_ptr<tracepoint> tp
12542 (new tracepoint (gdbarch,
12543 type_wanted,
12544 lsal.sals[i],
264f9890 12545 std::move (locspec),
3b003a61
PA
12546 NULL,
12547 std::move (cond_string),
12548 std::move (extra_string),
12549 disposition,
b080fe54 12550 thread, task, inferior, ignore_count,
3b003a61
PA
12551 from_tty, enabled, flags,
12552 canonical->special_display));
12553
983af33b
SDJ
12554 /* Given that its possible to have multiple markers with
12555 the same string id, if the user is creating a static
12556 tracepoint by marker id ("strace -m MARKER_ID"), then
12557 store the sals index, so that breakpoint_re_set can
12558 try to match up which of the newly found markers
12559 corresponds to this one */
12560 tp->static_trace_marker_id_idx = i;
12561
b270e6f9 12562 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
12563 }
12564}
12565
6d7a8c56 12566std::vector<symtab_and_line>
264f9890
PA
12567static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12568 program_space *search_pspace)
983af33b 12569{
e7cdec66 12570 const char *s = as_linespec_location_spec (locspec)->spec_string.get ();
983af33b 12571
6c5b2ebe 12572 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
6d7a8c56 12573 if (sals.size () > static_trace_marker_id_idx)
983af33b 12574 {
6d7a8c56 12575 sals[0] = sals[static_trace_marker_id_idx];
6c5b2ebe
PA
12576 sals.resize (1);
12577 return sals;
983af33b
SDJ
12578 }
12579 else
6d7a8c56 12580 error (_("marker %s not found"), static_trace_marker_id.c_str ());
983af33b
SDJ
12581}
12582
8613a476
TT
12583/* Static tracepoints with marker (`-m'). */
12584static struct breakpoint_ops strace_marker_breakpoint_ops =
12585{
264f9890 12586 strace_marker_create_sals_from_location_spec,
8613a476
TT
12587 strace_marker_create_breakpoints_sal,
12588};
983af33b 12589
d8de7963 12590static bool
983af33b
SDJ
12591strace_marker_p (struct breakpoint *b)
12592{
7b572efb 12593 return b->type == bp_static_marker_tracepoint;
983af33b
SDJ
12594}
12595
e4239559
SM
12596/* Notify interpreters and observers that breakpoint B was deleted. */
12597
12598static void
12599notify_breakpoint_deleted (breakpoint *b)
12600{
12601 interps_notify_breakpoint_deleted (b);
12602 gdb::observers::breakpoint_deleted.notify (b);
12603}
12604
53a5351d 12605/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 12606 structures. */
c906108c
SS
12607
12608void
fba45db2 12609delete_breakpoint (struct breakpoint *bpt)
c906108c 12610{
8a3fe4f8 12611 gdb_assert (bpt != NULL);
c906108c 12612
4a64f543
MS
12613 /* Has this bp already been deleted? This can happen because
12614 multiple lists can hold pointers to bp's. bpstat lists are
12615 especial culprits.
12616
12617 One example of this happening is a watchpoint's scope bp. When
12618 the scope bp triggers, we notice that the watchpoint is out of
12619 scope, and delete it. We also delete its scope bp. But the
12620 scope bp is marked "auto-deleting", and is already on a bpstat.
12621 That bpstat is then checked for auto-deleting bp's, which are
12622 deleted.
12623
12624 A real solution to this problem might involve reference counts in
12625 bp's, and/or giving them pointers back to their referencing
12626 bpstat's, and teaching delete_breakpoint to only free a bp's
12627 storage when no more references were extent. A cheaper bandaid
12628 was chosen. */
c906108c
SS
12629 if (bpt->type == bp_none)
12630 return;
12631
4a64f543
MS
12632 /* At least avoid this stale reference until the reference counting
12633 of breakpoints gets resolved. */
d0fb5eae 12634 if (bpt->related_breakpoint != bpt)
e5a0a904 12635 {
d0fb5eae 12636 struct breakpoint *related;
3a5c3e22 12637 struct watchpoint *w;
d0fb5eae
JK
12638
12639 if (bpt->type == bp_watchpoint_scope)
bcafd1c1 12640 w = gdb::checked_static_cast<watchpoint *> (bpt->related_breakpoint);
d0fb5eae 12641 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
bcafd1c1 12642 w = gdb::checked_static_cast<watchpoint *> (bpt);
3a5c3e22
PA
12643 else
12644 w = NULL;
12645 if (w != NULL)
12646 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
12647
12648 /* Unlink bpt from the bpt->related_breakpoint ring. */
12649 for (related = bpt; related->related_breakpoint != bpt;
12650 related = related->related_breakpoint);
12651 related->related_breakpoint = bpt->related_breakpoint;
12652 bpt->related_breakpoint = bpt;
e5a0a904
JK
12653 }
12654
a9634178
TJB
12655 /* watch_command_1 creates a watchpoint but only sets its number if
12656 update_watchpoint succeeds in creating its bp_locations. If there's
12657 a problem in that process, we'll be asked to delete the half-created
12658 watchpoint. In that case, don't announce the deletion. */
12659 if (bpt->number)
e4239559 12660 notify_breakpoint_deleted (bpt);
c906108c 12661
e2a15788 12662 breakpoint_chain.erase (breakpoint_chain.iterator_to (*bpt));
c906108c 12663
f431efe5
PA
12664 /* Be sure no bpstat's are pointing at the breakpoint after it's
12665 been freed. */
12666 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 12667 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
12668 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12669 commands are associated with the bpstat; if we remove it here,
12670 then the later call to bpstat_do_actions (&stop_bpstat); in
12671 event-top.c won't do anything, and temporary breakpoints with
12672 commands won't work. */
12673
58984e4a
TT
12674 iterate_over_threads ([&] (struct thread_info *th)
12675 {
12676 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12677 return false;
12678 });
f431efe5 12679
4a64f543
MS
12680 /* Now that breakpoint is removed from breakpoint list, update the
12681 global location list. This will remove locations that used to
12682 belong to this breakpoint. Do this before freeing the breakpoint
12683 itself, since remove_breakpoint looks at location's owner. It
12684 might be better design to have location completely
20afe380
SM
12685 self-contained, but it's not the case now.
12686
12687 Clear the location linked list first, otherwise, the intrusive_list
12688 destructor accesses the locations after they are freed. */
12689 bpt->clear_locations ();
44702360 12690 update_global_location_list (UGLL_DONT_INSERT);
74960c60 12691
4a64f543
MS
12692 /* On the chance that someone will soon try again to delete this
12693 same bp, we mark it as deleted before freeing its storage. */
c906108c 12694 bpt->type = bp_none;
4d01a485 12695 delete bpt;
c906108c
SS
12696}
12697
51be5b68
PA
12698/* Iterator function to call a user-provided callback function once
12699 for each of B and its related breakpoints. */
12700
12701static void
12702iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 12703 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
12704{
12705 struct breakpoint *related;
12706
12707 related = b;
12708 do
12709 {
12710 struct breakpoint *next;
12711
12712 /* FUNCTION may delete RELATED. */
12713 next = related->related_breakpoint;
12714
12715 if (next == related)
12716 {
12717 /* RELATED is the last ring entry. */
48649e1b 12718 function (related);
51be5b68
PA
12719
12720 /* FUNCTION may have deleted it, so we'd never reach back to
12721 B. There's nothing left to do anyway, so just break
12722 out. */
12723 break;
12724 }
12725 else
48649e1b 12726 function (related);
51be5b68
PA
12727
12728 related = next;
12729 }
12730 while (related != b);
12731}
95a42b64 12732
4495129a 12733static void
981a3fb3 12734delete_command (const char *arg, int from_tty)
c906108c 12735{
ea9365bb
TT
12736 dont_repeat ();
12737
c906108c
SS
12738 if (arg == 0)
12739 {
12740 int breaks_to_delete = 0;
12741
4a4fd10d
TY
12742 /* Delete all breakpoints, watchpoints, tracepoints, and catchpoints
12743 if no argument. Do not delete internal breakpoints, these have to
12744 be deleted with an explicit breakpoint number argument. */
a1decfc1
SM
12745 for (breakpoint &b : all_breakpoints ())
12746 if (user_breakpoint_p (&b))
973d738b
DJ
12747 {
12748 breaks_to_delete = 1;
12749 break;
12750 }
c906108c
SS
12751
12752 /* Ask user only if there are some breakpoints to delete. */
12753 if (!from_tty
4a4fd10d
TY
12754 || (breaks_to_delete
12755 && query (_("Delete all breakpoints, watchpoints, tracepoints, "
12756 "and catchpoints? "))))
a1decfc1
SM
12757 for (breakpoint &b : all_breakpoints_safe ())
12758 if (user_breakpoint_p (&b))
12759 delete_breakpoint (&b);
c906108c
SS
12760 }
12761 else
48649e1b 12762 map_breakpoint_numbers
b926417a 12763 (arg, [&] (breakpoint *br)
48649e1b 12764 {
b926417a 12765 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 12766 });
c906108c
SS
12767}
12768
c2f4122d
PA
12769/* Return true if all locations of B bound to PSPACE are pending. If
12770 PSPACE is NULL, all locations of all program spaces are
12771 considered. */
12772
d8de7963 12773static bool
c2f4122d 12774all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 12775{
b00b30b2 12776 for (bp_location &loc : b->locations ())
c2f4122d 12777 if ((pspace == NULL
b00b30b2
SM
12778 || loc.pspace == pspace)
12779 && !loc.shlib_disabled
12780 && !loc.pspace->executing_startup)
d8de7963
AB
12781 return false;
12782 return true;
fe3f5fa8
VP
12783}
12784
776592bf 12785/* Subroutine of update_breakpoint_locations to simplify it.
20afe380 12786 Return true if multiple fns in list LOCS have the same name.
776592bf
DE
12787 Null names are ignored. */
12788
d8de7963 12789static bool
20afe380 12790ambiguous_names_p (const bp_location_range &locs)
776592bf 12791{
e77c31d2 12792 gdb::unordered_set<std::string_view> htab;
776592bf 12793
b00b30b2 12794 for (const bp_location &l : locs)
776592bf 12795 {
b00b30b2 12796 const char *name = l.function_name.get ();
776592bf
DE
12797
12798 /* Allow for some names to be NULL, ignore them. */
12799 if (name == NULL)
12800 continue;
12801
e77c31d2 12802 if (!htab.insert (name).second)
d8de7963 12803 return true;
776592bf
DE
12804 }
12805
d8de7963 12806 return false;
776592bf
DE
12807}
12808
0fb4aa4b
PA
12809/* When symbols change, it probably means the sources changed as well,
12810 and it might mean the static tracepoint markers are no longer at
12811 the same address or line numbers they used to be at last we
12812 checked. Losing your static tracepoints whenever you rebuild is
12813 undesirable. This function tries to resync/rematch gdb static
12814 tracepoints with the markers on the target, for static tracepoints
12815 that have not been set by marker id. Static tracepoint that have
12816 been set by marker id are reset by marker id in breakpoint_re_set.
12817 The heuristic is:
12818
12819 1) For a tracepoint set at a specific address, look for a marker at
12820 the old PC. If one is found there, assume to be the same marker.
12821 If the name / string id of the marker found is different from the
12822 previous known name, assume that means the user renamed the marker
12823 in the sources, and output a warning.
12824
12825 2) For a tracepoint set at a given line number, look for a marker
12826 at the new address of the old line number. If one is found there,
12827 assume to be the same marker. If the name / string id of the
12828 marker found is different from the previous known name, assume that
12829 means the user renamed the marker in the sources, and output a
12830 warning.
12831
12832 3) If a marker is no longer found at the same address or line, it
12833 may mean the marker no longer exists. But it may also just mean
12834 the code changed a bit. Maybe the user added a few lines of code
12835 that made the marker move up or down (in line number terms). Ask
12836 the target for info about the marker with the string id as we knew
12837 it. If found, update line number and address in the matching
12838 static tracepoint. This will get confused if there's more than one
12839 marker with the same ID (possible in UST, although unadvised
12840 precisely because it confuses tools). */
12841
12842static struct symtab_and_line
01bccc56 12843update_static_tracepoint (tracepoint *tp, struct symtab_and_line sal)
0fb4aa4b
PA
12844{
12845 struct static_tracepoint_marker marker;
12846 CORE_ADDR pc;
0fb4aa4b
PA
12847
12848 pc = sal.pc;
12849 if (sal.line)
12850 find_line_pc (sal.symtab, sal.line, &pc);
12851
12852 if (target_static_tracepoint_marker_at (pc, &marker))
12853 {
5d9310c4 12854 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 12855 warning (_("static tracepoint %d changed probed marker from %s to %s"),
01bccc56 12856 tp->number, tp->static_trace_marker_id.c_str (),
5d9310c4 12857 marker.str_id.c_str ());
0fb4aa4b 12858
5d9310c4 12859 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
12860
12861 return sal;
12862 }
12863
12864 /* Old marker wasn't found on target at lineno. Try looking it up
12865 by string ID. */
12866 if (!sal.explicit_pc
12867 && sal.line != 0
12868 && sal.symtab != NULL
5d9310c4 12869 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 12870 {
5d9310c4
SM
12871 std::vector<static_tracepoint_marker> markers
12872 = target_static_tracepoint_markers_by_strid
12873 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 12874
5d9310c4 12875 if (!markers.empty ())
0fb4aa4b 12876 {
0fb4aa4b 12877 struct symbol *sym;
80e1d417 12878 struct static_tracepoint_marker *tpmarker;
79a45e25 12879 struct ui_out *uiout = current_uiout;
0fb4aa4b 12880
5d9310c4 12881 tpmarker = &markers[0];
0fb4aa4b 12882
5d9310c4 12883 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
12884
12885 warning (_("marker for static tracepoint %d (%s) not "
12886 "found at previous line number"),
01bccc56 12887 tp->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 12888
51abb421 12889 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 12890 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 12891 uiout->text ("Now in ");
0fb4aa4b
PA
12892 if (sym)
12893 {
987012b8 12894 uiout->field_string ("func", sym->print_name (),
e43b10e1 12895 function_name_style.style ());
112e8700 12896 uiout->text (" at ");
0fb4aa4b 12897 }
112e8700 12898 uiout->field_string ("file",
cbe56571 12899 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 12900 file_name_style.style ());
112e8700 12901 uiout->text (":");
0fb4aa4b 12902
112e8700 12903 if (uiout->is_mi_like_p ())
0fb4aa4b 12904 {
0b0865da 12905 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 12906
112e8700 12907 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
12908 }
12909
887ae0cf 12910 uiout->field_signed ("line", sal2.line, line_number_style.style ());
112e8700 12911 uiout->text ("\n");
0fb4aa4b 12912
01bccc56
TT
12913 tp->first_loc ().line_number = sal2.line;
12914 tp->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 12915
40d97ee2 12916 std::unique_ptr<explicit_location_spec> els
fde84194
TT
12917 (new explicit_location_spec
12918 (symtab_to_filename_for_display (sal2.symtab)));
01bccc56 12919 els->line_offset.offset = tp->first_loc ().line_number;
40d97ee2
PA
12920 els->line_offset.sign = LINE_OFFSET_NONE;
12921
01bccc56 12922 tp->locspec = std::move (els);
0fb4aa4b
PA
12923
12924 /* Might be nice to check if function changed, and warn if
12925 so. */
0fb4aa4b
PA
12926 }
12927 }
12928 return sal;
12929}
12930
20afe380 12931/* Returns true iff location lists A and B are sufficiently same that
8d3788bd
VP
12932 we don't need to report breakpoint as changed. */
12933
d8de7963 12934static bool
20afe380 12935locations_are_equal (const bp_location_list &a, const bp_location_range &b)
8d3788bd 12936{
20afe380
SM
12937 auto a_iter = a.begin ();
12938 auto b_iter = b.begin ();
12939
12940 for (; a_iter != a.end () && b_iter != b.end (); ++a_iter, ++b_iter)
8d3788bd 12941 {
b00b30b2 12942 if (a_iter->address != b_iter->address)
d8de7963 12943 return false;
8d3788bd 12944
b00b30b2 12945 if (a_iter->shlib_disabled != b_iter->shlib_disabled)
d8de7963 12946 return false;
8d3788bd 12947
b00b30b2 12948 if (a_iter->enabled != b_iter->enabled)
d8de7963 12949 return false;
8d3788bd 12950
b00b30b2 12951 if (a_iter->disabled_by_cond != b_iter->disabled_by_cond)
d8de7963 12952 return false;
d1b72c26
TT
12953
12954 /* When a breakpoint is set by address, it is not created as
12955 pending; but then during an solib event or the like it may
12956 acquire a symbol. So, check this here. */
12957 if (a_iter->symbol != b_iter->symbol
12958 || a_iter->msymbol != b_iter->msymbol)
12959 return false;
8d3788bd
VP
12960 }
12961
20afe380 12962 return (a_iter == a.end ()) == (b_iter == b.end ());
8d3788bd
VP
12963}
12964
20afe380 12965/* See breakpoint.h. */
c2f4122d 12966
20afe380
SM
12967bp_location_list
12968breakpoint::steal_locations (program_space *pspace)
c2f4122d 12969{
c2f4122d 12970 if (pspace == NULL)
20afe380 12971 return std::move (m_locations);
c2f4122d 12972
20afe380 12973 bp_location_list ret;
c2f4122d 12974
20afe380 12975 for (auto it = m_locations.begin (); it != m_locations.end (); )
c2f4122d 12976 {
20afe380 12977 if (it->pspace == pspace)
c2f4122d 12978 {
20afe380
SM
12979 bp_location &loc = *it;
12980 it = m_locations.erase (it);
12981 ret.push_back (loc);
c2f4122d
PA
12982 }
12983 else
20afe380 12984 ++it;
c2f4122d
PA
12985 }
12986
20afe380 12987 return ret;
c2f4122d
PA
12988}
12989
12990/* Create new breakpoint locations for B (a hardware or software
12991 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12992 zero, then B is a ranged breakpoint. Only recreates locations for
12993 FILTER_PSPACE. Locations of other program spaces are left
12994 untouched. */
f1310107 12995
0e30163f 12996void
74421c0b 12997update_breakpoint_locations (code_breakpoint *b,
c2f4122d 12998 struct program_space *filter_pspace,
6c5b2ebe
PA
12999 gdb::array_view<const symtab_and_line> sals,
13000 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13001{
6c5b2ebe 13002 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13003 {
13004 /* Ranged breakpoints have only one start location and one end
13005 location. */
13006 b->enable_state = bp_disabled;
6cb06a8c
TT
13007 gdb_printf (gdb_stderr,
13008 _("Could not reset ranged breakpoint %d: "
13009 "multiple locations found\n"),
13010 b->number);
f8eba3c6
TT
13011 return;
13012 }
f1310107 13013
4a64f543
MS
13014 /* If there's no new locations, and all existing locations are
13015 pending, don't do anything. This optimizes the common case where
13016 all locations are in the same shared library, that was unloaded.
13017 We'd like to retain the location, so that when the library is
13018 loaded again, we don't loose the enabled/disabled status of the
16a6f7d2 13019 individual locations. */
6c5b2ebe 13020 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
16a6f7d2 13021 return;
fe3f5fa8 13022
20afe380 13023 bp_location_list existing_locations = b->steal_locations (filter_pspace);
fe3f5fa8 13024
6c5b2ebe 13025 for (const auto &sal : sals)
fe3f5fa8 13026 {
f8eba3c6
TT
13027 struct bp_location *new_loc;
13028
6c5b2ebe 13029 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13030
06615628 13031 new_loc = b->add_location (sal);
fe3f5fa8 13032
0d381245
VP
13033 /* Reparse conditions, they might contain references to the
13034 old symtab. */
13035 if (b->cond_string != NULL)
13036 {
bbc13ae3 13037 const char *s;
fe3f5fa8 13038
6f781ee3 13039 s = b->cond_string.get ();
a70b8144 13040 try
0d381245 13041 {
6c5b2ebe
PA
13042 new_loc->cond = parse_exp_1 (&s, sal.pc,
13043 block_for_pc (sal.pc),
0d381245
VP
13044 0);
13045 }
230d2906 13046 catch (const gdb_exception_error &e)
0d381245 13047 {
b5fa468f 13048 new_loc->disabled_by_cond = true;
0d381245
VP
13049 }
13050 }
fe3f5fa8 13051
6c5b2ebe 13052 if (!sals_end.empty ())
f1310107 13053 {
6c5b2ebe 13054 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13055
6c5b2ebe 13056 new_loc->length = end - sals[0].pc + 1;
f1310107 13057 }
0d381245 13058 }
fe3f5fa8 13059
4a64f543 13060 /* If possible, carry over 'disable' status from existing
df63932c
AB
13061 breakpoints.
13062
13063 If there are multiple breakpoints with the same function name,
13064 e.g. for inline functions, comparing function names won't work.
13065 Instead compare pc addresses; this is just a heuristic as things
13066 may have moved, but in practice it gives the correct answer
13067 often enough until a better solution is found. */
13068 bool have_ambiguous_names = ambiguous_names_p (b->locations ());
13069
13070 for (const bp_location &e : existing_locations)
13071 {
ffd09b62 13072 if (e.function_name == nullptr || e.enabled)
df63932c
AB
13073 continue;
13074
13075 if (have_ambiguous_names)
13076 {
13077 for (bp_location &l : b->locations ())
13078 {
13079 /* Ignore software vs hardware location type at
13080 this point, because with "set breakpoint
13081 auto-hw", after a re-set, locations that were
13082 hardware can end up as software, or vice versa.
13083 As mentioned above, this is an heuristic and in
13084 practice should give the correct answer often
13085 enough. */
13086 if (breakpoint_locations_match (&e, &l, true))
13087 {
13088 l.enabled = e.enabled;
df63932c
AB
13089 break;
13090 }
13091 }
13092 }
13093 else
13094 {
13095 for (bp_location &l : b->locations ())
13096 if (l.function_name
13097 && strcmp (e.function_name.get (),
13098 l.function_name.get ()) == 0)
776592bf 13099 {
df63932c 13100 l.enabled = e.enabled;
df63932c 13101 break;
776592bf 13102 }
df63932c
AB
13103 }
13104 }
fe3f5fa8 13105
20afe380 13106 if (!locations_are_equal (existing_locations, b->locations ()))
19081eb5 13107 notify_breakpoint_modified (b);
fe3f5fa8
VP
13108}
13109
264f9890 13110/* Find the SaL locations corresponding to the given LOCSPEC.
ef23e705
TJB
13111 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13112
2c9a6d72
TT
13113std::vector<symtab_and_line>
13114code_breakpoint::location_spec_to_sals (location_spec *locspec,
13115 struct program_space *search_pspace,
13116 int *found)
ef23e705 13117{
cc06b668 13118 struct gdb_exception exception;
ef23e705 13119
6c5b2ebe
PA
13120 std::vector<symtab_and_line> sals;
13121
a70b8144 13122 try
ef23e705 13123 {
2c9a6d72 13124 sals = decode_location_spec (locspec, search_pspace);
ef23e705 13125 }
94aeb44b 13126 catch (gdb_exception_error &e)
ef23e705 13127 {
d8de7963 13128 int not_found_and_ok = false;
492d29ea 13129
ef23e705
TJB
13130 /* For pending breakpoints, it's expected that parsing will
13131 fail until the right shared library is loaded. User has
13132 already told to create pending breakpoints and don't need
13133 extra messages. If breakpoint is in bp_shlib_disabled
13134 state, then user already saw the message about that
13135 breakpoint being disabled, and don't want to see more
13136 errors. */
58438ac1 13137 if (e.error == NOT_FOUND_ERROR
2c9a6d72 13138 && (condition_not_parsed
9dc1523b 13139 || (this->has_locations ()
c2f4122d 13140 && search_pspace != NULL
f5951b9f
SM
13141 && this->first_loc ().pspace != search_pspace)
13142 || (this->has_locations () && this->first_loc ().shlib_disabled)
13143 || (this->has_locations ()
13144 && this->first_loc ().pspace->executing_startup)
2c9a6d72 13145 || enable_state == bp_disabled))
d8de7963 13146 not_found_and_ok = true;
ef23e705
TJB
13147
13148 if (!not_found_and_ok)
13149 {
13150 /* We surely don't want to warn about the same breakpoint
13151 10 times. One solution, implemented here, is disable
13152 the breakpoint on error. Another solution would be to
13153 have separate 'warning emitted' flag. Since this
13154 happens only when a binary has changed, I don't know
13155 which approach is better. */
2c9a6d72 13156 enable_state = bp_disabled;
eedc3f4f 13157 throw;
ef23e705 13158 }
94aeb44b
TT
13159
13160 exception = std::move (e);
ef23e705
TJB
13161 }
13162
492d29ea 13163 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13164 {
6c5b2ebe
PA
13165 for (auto &sal : sals)
13166 resolve_sal_pc (&sal);
ef23e705 13167
2c9a6d72 13168 if (type == bp_static_tracepoint)
01bccc56
TT
13169 {
13170 tracepoint *t = gdb::checked_static_cast<tracepoint *> (this);
13171 sals[0] = update_static_tracepoint (t, sals[0]);
13172 }
ef23e705 13173
58438ac1
TT
13174 *found = 1;
13175 }
13176 else
13177 *found = 0;
ef23e705
TJB
13178
13179 return sals;
13180}
13181
348d480f
PA
13182/* The default re_set method, for typical hardware or software
13183 breakpoints. Reevaluate the breakpoint and recreate its
13184 locations. */
13185
b3d5660a 13186void
6cce0251 13187code_breakpoint::re_set_default (struct program_space *filter_pspace)
ef23e705 13188{
6c5b2ebe 13189 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13190
43a1fffa
AB
13191 /* If this breakpoint is thread- or inferior-specific, then find the
13192 program space in which this breakpoint exists. Otherwise, for
13193 breakpoints that are not thread- or inferior-specific, BP_PSPACE will
13194 be nullptr. */
6cce0251
AB
13195 program_space *bp_pspace
13196 = find_program_space_for_breakpoint (this->thread, this->inferior);
13197
13198 /* If this is not a thread or inferior specific breakpoint, or it is a
13199 thread or inferior specific breakpoint but we are looking for new
13200 locations in the program space that the specific thread or inferior is
13201 running, then look for new locations for this breakpoint. */
13202 if (bp_pspace == nullptr || filter_pspace == bp_pspace)
13203 {
13204 int found;
13205 std::vector<symtab_and_line> sals
13206 = location_spec_to_sals (locspec.get (), filter_pspace, &found);
f1310107 13207 if (found)
6cce0251
AB
13208 expanded = std::move (sals);
13209
13210 if (locspec_range_end != nullptr)
13211 {
13212 std::vector<symtab_and_line> sals_end
13213 = location_spec_to_sals (locspec_range_end.get (),
13214 filter_pspace, &found);
13215 if (found)
13216 expanded_end = std::move (sals_end);
13217 }
f1310107
TJB
13218 }
13219
6cce0251
AB
13220 /* Update the locations for this breakpoint. For thread-specific
13221 breakpoints this will remove any old locations that are for the wrong
13222 program space -- this can happen if the user changes the thread of a
13223 thread-specific breakpoint. */
b3d5660a 13224 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
28010a5d
PA
13225}
13226
c2f4122d
PA
13227/* Re-set breakpoint locations for the current program space.
13228 Locations bound to other program spaces are left untouched. */
13229
c906108c 13230void
69de3c6a 13231breakpoint_re_set (void)
c906108c 13232{
c5aa993b 13233 {
fdf44873
TT
13234 scoped_restore_current_language save_language;
13235 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13236 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13237
b172d94b
AB
13238 /* To ::re_set each breakpoint we set the current_language to the
13239 language of the breakpoint before re-evaluating the breakpoint's
13240 location. This change can unfortunately get undone by accident if
13241 the language_mode is set to auto, and we either switch frames, or
13242 more likely in this context, we select the current frame.
8e817061
JB
13243
13244 We prevent this by temporarily turning the language_mode to
13245 language_mode_manual. We restore it once all breakpoints
13246 have been reset. */
13247 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13248 language_mode = language_mode_manual;
13249
5ed8105e
PA
13250 /* Note: we must not try to insert locations until after all
13251 breakpoints have been re-set. Otherwise, e.g., when re-setting
13252 breakpoint 1, we'd insert the locations of breakpoint 2, which
13253 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13254
a1decfc1 13255 for (breakpoint &b : all_breakpoints_safe ())
5ed8105e 13256 {
a70b8144 13257 try
bf469271 13258 {
b172d94b
AB
13259 input_radix = b.input_radix;
13260 set_language (b.language);
6cce0251 13261 b.re_set (current_program_space);
bf469271 13262 }
230d2906 13263 catch (const gdb_exception &ex)
bf469271
PA
13264 {
13265 exception_fprintf (gdb_stderr, ex,
13266 "Error in re-setting breakpoint %d: ",
a1decfc1 13267 b.number);
bf469271 13268 }
5ed8105e 13269 }
5ed8105e
PA
13270
13271 jit_breakpoint_re_set ();
13272 }
6c95b8df 13273
af02033e
PP
13274 create_overlay_event_breakpoint ();
13275 create_longjmp_master_breakpoint ();
13276 create_std_terminate_master_breakpoint ();
186c406b 13277 create_exception_master_breakpoint ();
2a7f3dff
PA
13278
13279 /* Now we can insert. */
13280 update_global_location_list (UGLL_MAY_INSERT);
c906108c 13281}
6cce0251
AB
13282
13283/* Re-set locations for breakpoint B in FILTER_PSPACE. If FILTER_PSPACE is
13284 nullptr then re-set locations for B in all program spaces. Locations
13285 bound to program spaces other than FILTER_PSPACE are left untouched. */
13286
13287static void
13288breakpoint_re_set_one (breakpoint *b, program_space *filter_pspace)
13289{
13290 {
13291 scoped_restore_current_language save_language;
13292 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13293 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13294
13295 /* To ::re_set each breakpoint we set the current_language to the
13296 language of the breakpoint before re-evaluating the breakpoint's
13297 location. This change can unfortunately get undone by accident if
13298 the language_mode is set to auto, and we either switch frames, or
13299 more likely in this context, we select the current frame.
13300
13301 We prevent this by temporarily turning the language_mode to
13302 language_mode_manual. We restore it once all breakpoints
13303 have been reset. */
13304 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13305 language_mode = language_mode_manual;
13306
13307 /* Note: we must not try to insert locations until after all
13308 breakpoints have been re-set. Otherwise, e.g., when re-setting
13309 breakpoint 1, we'd insert the locations of breakpoint 2, which
13310 hadn't been re-set yet, and thus may have stale locations. */
13311
13312 try
13313 {
13314 input_radix = b->input_radix;
13315 set_language (b->language);
13316 b->re_set (filter_pspace);
13317 }
13318 catch (const gdb_exception &ex)
13319 {
13320 exception_fprintf (gdb_stderr, ex,
13321 "Error in re-setting breakpoint %d: ",
13322 b->number);
13323 }
13324 }
13325
13326 /* Now we can insert. */
13327 update_global_location_list (UGLL_MAY_INSERT);
13328}
c906108c 13329\f
c906108c
SS
13330/* Reset the thread number of this breakpoint:
13331
13332 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13333 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13334void
fba45db2 13335breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13336{
13337 if (b->thread != -1)
13338 {
00431a78 13339 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13340
13341 /* We're being called after following a fork. The new fork is
13342 selected as current, and unless this was a vfork will have a
13343 different program space from the original thread. Reset that
13344 as well. */
f5951b9f 13345 b->first_loc ().pspace = current_program_space;
c906108c
SS
13346 }
13347}
13348
03ac34d5
MS
13349/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13350 If from_tty is nonzero, it prints a message to that effect,
13351 which ends with a period (no newline). */
13352
c906108c 13353void
fba45db2 13354set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13355{
c906108c
SS
13356 if (count < 0)
13357 count = 0;
13358
a1decfc1
SM
13359 for (breakpoint &b : all_breakpoints ())
13360 if (b.number == bptnum)
01add95b 13361 {
a1decfc1 13362 if (is_tracepoint (&b))
01add95b
SM
13363 {
13364 if (from_tty && count != 0)
6cb06a8c
TT
13365 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13366 bptnum);
01add95b
SM
13367 return;
13368 }
13369
a1decfc1 13370 b.ignore_count = count;
01add95b
SM
13371 if (from_tty)
13372 {
13373 if (count == 0)
6cb06a8c
TT
13374 gdb_printf (_("Will stop next time "
13375 "breakpoint %d is reached."),
13376 bptnum);
01add95b 13377 else if (count == 1)
6cb06a8c
TT
13378 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13379 bptnum);
01add95b 13380 else
6cb06a8c
TT
13381 gdb_printf (_("Will ignore next %d "
13382 "crossings of breakpoint %d."),
13383 count, bptnum);
01add95b 13384 }
19081eb5 13385 notify_breakpoint_modified (&b);
01add95b
SM
13386 return;
13387 }
c906108c 13388
8a3fe4f8 13389 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13390}
13391
c906108c
SS
13392/* Command to set ignore-count of breakpoint N to COUNT. */
13393
13394static void
0b39b52e 13395ignore_command (const char *args, int from_tty)
c906108c 13396{
0b39b52e 13397 const char *p = args;
52f0bd74 13398 int num;
c906108c
SS
13399
13400 if (p == 0)
e2e0b3e5 13401 error_no_arg (_("a breakpoint number"));
c5aa993b 13402
c906108c 13403 num = get_number (&p);
5c44784c 13404 if (num == 0)
8a3fe4f8 13405 error (_("bad breakpoint number: '%s'"), args);
c906108c 13406 if (*p == 0)
8a3fe4f8 13407 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13408
13409 set_ignore_count (num,
13410 longest_to_int (value_as_long (parse_and_eval (p))),
13411 from_tty);
221ea385 13412 if (from_tty)
6cb06a8c 13413 gdb_printf ("\n");
c906108c
SS
13414}
13415\f
d0fe4701
XR
13416
13417/* Call FUNCTION on each of the breakpoints with numbers in the range
13418 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13419
13420static void
d0fe4701
XR
13421map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13422 gdb::function_view<void (breakpoint *)> function)
c906108c 13423{
d0fe4701
XR
13424 if (bp_num_range.first == 0)
13425 {
13426 warning (_("bad breakpoint number at or near '%d'"),
13427 bp_num_range.first);
13428 }
13429 else
c906108c 13430 {
d0fe4701 13431 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13432 {
d0fe4701
XR
13433 bool match = false;
13434
a1decfc1
SM
13435 for (breakpoint &b : all_breakpoints_safe ())
13436 if (b.number == i)
5c44784c 13437 {
bfd28288 13438 match = true;
a1decfc1 13439 function (&b);
11cf8741 13440 break;
5c44784c 13441 }
bfd28288 13442 if (!match)
6cb06a8c 13443 gdb_printf (_("No breakpoint number %d.\n"), i);
c5aa993b 13444 }
c906108c
SS
13445 }
13446}
13447
d0fe4701
XR
13448/* Call FUNCTION on each of the breakpoints whose numbers are given in
13449 ARGS. */
13450
13451static void
13452map_breakpoint_numbers (const char *args,
13453 gdb::function_view<void (breakpoint *)> function)
13454{
13455 if (args == NULL || *args == '\0')
13456 error_no_arg (_("one or more breakpoint numbers"));
13457
13458 number_or_range_parser parser (args);
13459
13460 while (!parser.finished ())
13461 {
13462 int num = parser.get_number ();
13463 map_breakpoint_number_range (std::make_pair (num, num), function);
13464 }
13465}
13466
13467/* Return the breakpoint location structure corresponding to the
13468 BP_NUM and LOC_NUM values. */
13469
0d381245 13470static struct bp_location *
d0fe4701 13471find_location_by_number (int bp_num, int loc_num)
0d381245 13472{
43892fdf 13473 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
13474
13475 if (!b || b->number != bp_num)
d0fe4701 13476 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 13477
0d381245 13478 if (loc_num == 0)
d0fe4701 13479 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 13480
d0fe4701 13481 int n = 0;
b00b30b2 13482 for (bp_location &loc : b->locations ())
d0fe4701 13483 if (++n == loc_num)
b00b30b2 13484 return &loc;
d0fe4701
XR
13485
13486 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
13487}
13488
95e95a6d
PA
13489/* Modes of operation for extract_bp_num. */
13490enum class extract_bp_kind
13491{
13492 /* Extracting a breakpoint number. */
13493 bp,
13494
13495 /* Extracting a location number. */
13496 loc,
13497};
13498
13499/* Extract a breakpoint or location number (as determined by KIND)
13500 from the string starting at START. TRAILER is a character which
13501 can be found after the number. If you don't want a trailer, use
13502 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13503 string. This always returns a positive integer. */
13504
13505static int
13506extract_bp_num (extract_bp_kind kind, const char *start,
13507 int trailer, const char **end_out = NULL)
13508{
13509 const char *end = start;
13510 int num = get_number_trailer (&end, trailer);
13511 if (num < 0)
13512 error (kind == extract_bp_kind::bp
13513 ? _("Negative breakpoint number '%.*s'")
13514 : _("Negative breakpoint location number '%.*s'"),
13515 int (end - start), start);
13516 if (num == 0)
13517 error (kind == extract_bp_kind::bp
13518 ? _("Bad breakpoint number '%.*s'")
13519 : _("Bad breakpoint location number '%.*s'"),
13520 int (end - start), start);
13521
13522 if (end_out != NULL)
13523 *end_out = end;
13524 return num;
13525}
13526
13527/* Extract a breakpoint or location range (as determined by KIND) in
13528 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13529 representing the (inclusive) range. The returned pair's elements
13530 are always positive integers. */
13531
13532static std::pair<int, int>
13533extract_bp_or_bp_range (extract_bp_kind kind,
13534 const std::string &arg,
13535 std::string::size_type arg_offset)
13536{
13537 std::pair<int, int> range;
13538 const char *bp_loc = &arg[arg_offset];
13539 std::string::size_type dash = arg.find ('-', arg_offset);
13540 if (dash != std::string::npos)
13541 {
13542 /* bp_loc is a range (x-z). */
13543 if (arg.length () == dash + 1)
13544 error (kind == extract_bp_kind::bp
13545 ? _("Bad breakpoint number at or near: '%s'")
13546 : _("Bad breakpoint location number at or near: '%s'"),
13547 bp_loc);
13548
13549 const char *end;
13550 const char *start_first = bp_loc;
13551 const char *start_second = &arg[dash + 1];
13552 range.first = extract_bp_num (kind, start_first, '-');
13553 range.second = extract_bp_num (kind, start_second, '\0', &end);
13554
13555 if (range.first > range.second)
13556 error (kind == extract_bp_kind::bp
13557 ? _("Inverted breakpoint range at '%.*s'")
13558 : _("Inverted breakpoint location range at '%.*s'"),
13559 int (end - start_first), start_first);
13560 }
13561 else
13562 {
13563 /* bp_loc is a single value. */
13564 range.first = extract_bp_num (kind, bp_loc, '\0');
13565 range.second = range.first;
13566 }
13567 return range;
13568}
13569
d0fe4701
XR
13570/* Extract the breakpoint/location range specified by ARG. Returns
13571 the breakpoint range in BP_NUM_RANGE, and the location range in
13572 BP_LOC_RANGE.
13573
13574 ARG may be in any of the following forms:
13575
13576 x where 'x' is a breakpoint number.
13577 x-y where 'x' and 'y' specify a breakpoint numbers range.
13578 x.y where 'x' is a breakpoint number and 'y' a location number.
13579 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13580 location number range.
13581*/
13582
cc638e86 13583static void
d0fe4701
XR
13584extract_bp_number_and_location (const std::string &arg,
13585 std::pair<int, int> &bp_num_range,
13586 std::pair<int, int> &bp_loc_range)
13587{
13588 std::string::size_type dot = arg.find ('.');
13589
13590 if (dot != std::string::npos)
13591 {
13592 /* Handle 'x.y' and 'x.y-z' cases. */
13593
13594 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 13595 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 13596
95e95a6d
PA
13597 bp_num_range.first
13598 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13599 bp_num_range.second = bp_num_range.first;
d0fe4701 13600
95e95a6d
PA
13601 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13602 arg, dot + 1);
d0fe4701
XR
13603 }
13604 else
13605 {
13606 /* Handle x and x-y cases. */
d0fe4701 13607
95e95a6d 13608 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
13609 bp_loc_range.first = 0;
13610 bp_loc_range.second = 0;
13611 }
d0fe4701
XR
13612}
13613
13614/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13615 specifies whether to enable or disable. */
13616
13617static void
13618enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13619{
13620 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13621 if (loc != NULL)
13622 {
b5fa468f
TBA
13623 if (loc->disabled_by_cond && enable)
13624 error (_("Breakpoint %d's condition is invalid at location %d, "
13625 "cannot enable."), bp_num, loc_num);
13626
d0fe4701
XR
13627 if (loc->enabled != enable)
13628 {
13629 loc->enabled = enable;
13630 mark_breakpoint_location_modified (loc);
13631 }
13632 if (target_supports_enable_disable_tracepoint ()
13633 && current_trace_status ()->running && loc->owner
13634 && is_tracepoint (loc->owner))
13635 target_disable_tracepoint (loc);
13636 }
13637 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d 13638
19081eb5 13639 notify_breakpoint_modified (loc->owner);
e5213e2c
SF
13640}
13641
13642/* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13643 owner. 1-based indexing. -1 signals NOT FOUND. */
13644
13645static int
d8a77e4c 13646find_loc_num_by_location (const bp_location *loc)
e5213e2c
SF
13647{
13648 if (loc != nullptr && loc->owner != nullptr)
13649 {
13650 /* Locations use 1-based indexing. */
13651 int loc_num = 1;
b00b30b2 13652 for (bp_location &it : loc->owner->locations ())
e5213e2c 13653 {
b00b30b2 13654 if (&it == loc)
e5213e2c
SF
13655 return loc_num;
13656 loc_num++;
13657 }
13658 }
13659 return -1;
13660}
13661
13662/* Enable or disable a breakpoint location LOC. ENABLE
13663 specifies whether to enable or disable. */
13664
13665void
13666enable_disable_bp_location (bp_location *loc, bool enable)
13667{
13668 if (loc == nullptr)
13669 error (_("Breakpoint location is invalid."));
13670
13671 if (loc->owner == nullptr)
13672 error (_("Breakpoint location does not have an owner breakpoint."));
13673
13674 if (loc->disabled_by_cond && enable)
13675 {
13676 int loc_num = find_loc_num_by_location (loc);
13677 if (loc_num == -1)
13678 error (_("Breakpoint location LOC_NUM could not be found."));
13679 else
13680 error (_("Breakpoint %d's condition is invalid at location %d, "
13681 "cannot enable."), loc->owner->number, loc_num);
13682 }
13683
13684 if (loc->enabled != enable)
13685 {
13686 loc->enabled = enable;
13687 mark_breakpoint_location_modified (loc);
13688 }
13689
13690 if (target_supports_enable_disable_tracepoint ()
13691 && current_trace_status ()->running && loc->owner
13692 && is_tracepoint (loc->owner))
13693 target_disable_tracepoint (loc);
13694
13695 update_global_location_list (UGLL_DONT_INSERT);
19081eb5 13696 notify_breakpoint_modified (loc->owner);
d0fe4701
XR
13697}
13698
13699/* Enable or disable a range of breakpoint locations. BP_NUM is the
13700 number of the breakpoint, and BP_LOC_RANGE specifies the
13701 (inclusive) range of location numbers of that breakpoint to
13702 enable/disable. ENABLE specifies whether to enable or disable the
13703 location. */
13704
13705static void
13706enable_disable_breakpoint_location_range (int bp_num,
13707 std::pair<int, int> &bp_loc_range,
13708 bool enable)
13709{
13710 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13711 enable_disable_bp_num_loc (bp_num, i, enable);
13712}
0d381245 13713
1900040c
MS
13714/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13715 If from_tty is nonzero, it prints a message to that effect,
13716 which ends with a period (no newline). */
13717
c906108c 13718void
fba45db2 13719disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
13720{
13721 /* Never disable a watchpoint scope breakpoint; we want to
13722 hit them when we leave scope so we can delete both the
13723 watchpoint and its scope breakpoint at that time. */
13724 if (bpt->type == bp_watchpoint_scope)
13725 return;
13726
b5de0fa7 13727 bpt->enable_state = bp_disabled;
c906108c 13728
b775012e
LM
13729 /* Mark breakpoint locations modified. */
13730 mark_breakpoint_modified (bpt);
13731
d248b706
KY
13732 if (target_supports_enable_disable_tracepoint ()
13733 && current_trace_status ()->running && is_tracepoint (bpt))
13734 {
b00b30b2
SM
13735 for (bp_location &location : bpt->locations ())
13736 target_disable_tracepoint (&location);
d248b706
KY
13737 }
13738
44702360 13739 update_global_location_list (UGLL_DONT_INSERT);
c906108c 13740
19081eb5 13741 notify_breakpoint_modified (bpt);
c906108c
SS
13742}
13743
d0fe4701
XR
13744/* Enable or disable the breakpoint(s) or breakpoint location(s)
13745 specified in ARGS. ARGS may be in any of the formats handled by
13746 extract_bp_number_and_location. ENABLE specifies whether to enable
13747 or disable the breakpoints/locations. */
13748
c906108c 13749static void
d0fe4701 13750enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 13751{
c906108c 13752 if (args == 0)
46c6471b 13753 {
a1decfc1
SM
13754 for (breakpoint &bpt : all_breakpoints ())
13755 if (user_breakpoint_p (&bpt))
d0fe4701
XR
13756 {
13757 if (enable)
a1decfc1 13758 enable_breakpoint (&bpt);
d0fe4701 13759 else
a1decfc1 13760 disable_breakpoint (&bpt);
d0fe4701 13761 }
46c6471b 13762 }
9eaabc75 13763 else
0d381245 13764 {
cb791d59 13765 std::string num = extract_arg (&args);
9eaabc75 13766
cb791d59 13767 while (!num.empty ())
d248b706 13768 {
d0fe4701 13769 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 13770
cc638e86
PA
13771 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13772
13773 if (bp_loc_range.first == bp_loc_range.second
78805ff8
PW
13774 && (bp_loc_range.first == 0
13775 || (bp_loc_range.first == 1
13776 && bp_num_range.first == bp_num_range.second
13777 && !has_multiple_locations (bp_num_range.first))))
d0fe4701 13778 {
78805ff8
PW
13779 /* Handle breakpoint ids with formats 'x' or 'x-z'
13780 or 'y.1' where y has only one code location. */
cc638e86
PA
13781 map_breakpoint_number_range (bp_num_range,
13782 enable
13783 ? enable_breakpoint
13784 : disable_breakpoint);
13785 }
13786 else
13787 {
13788 /* Handle breakpoint ids with formats 'x.y' or
13789 'x.y-z'. */
13790 enable_disable_breakpoint_location_range
13791 (bp_num_range.first, bp_loc_range, enable);
b775012e 13792 }
9eaabc75 13793 num = extract_arg (&args);
d248b706 13794 }
0d381245 13795 }
c906108c
SS
13796}
13797
d0fe4701
XR
13798/* The disable command disables the specified breakpoints/locations
13799 (or all defined breakpoints) so they're no longer effective in
13800 stopping the inferior. ARGS may be in any of the forms defined in
13801 extract_bp_number_and_location. */
13802
13803static void
13804disable_command (const char *args, int from_tty)
13805{
13806 enable_disable_command (args, from_tty, false);
13807}
13808
c906108c 13809static void
816338b5
SS
13810enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13811 int count)
c906108c 13812{
afe38095 13813 int target_resources_ok;
c906108c
SS
13814
13815 if (bpt->type == bp_hardware_breakpoint)
13816 {
13817 int i;
c5aa993b 13818 i = hw_breakpoint_used_count ();
53a5351d 13819 target_resources_ok =
d92524f1 13820 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 13821 i + 1, 0);
c906108c 13822 if (target_resources_ok == 0)
8a3fe4f8 13823 error (_("No hardware breakpoint support in the target."));
c906108c 13824 else if (target_resources_ok < 0)
8a3fe4f8 13825 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
13826 }
13827
cc60f2e3 13828 if (is_watchpoint (bpt))
c906108c 13829 {
d07205c2 13830 /* Initialize it just to avoid a GCC false warning. */
f486487f 13831 enum enable_state orig_enable_state = bp_disabled;
dde02812 13832
a70b8144 13833 try
c906108c 13834 {
bcafd1c1 13835 watchpoint *w = gdb::checked_static_cast<watchpoint *> (bpt);
3a5c3e22 13836
1e718ff1
TJB
13837 orig_enable_state = bpt->enable_state;
13838 bpt->enable_state = bp_enabled;
d8de7963 13839 update_watchpoint (w, true /* reparse */);
c906108c 13840 }
b1ffd112 13841 catch (const gdb_exception_error &e)
c5aa993b 13842 {
1e718ff1 13843 bpt->enable_state = orig_enable_state;
dde02812
ES
13844 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13845 bpt->number);
13846 return;
c5aa993b 13847 }
c906108c 13848 }
0101ce28 13849
b775012e
LM
13850 bpt->enable_state = bp_enabled;
13851
13852 /* Mark breakpoint locations modified. */
13853 mark_breakpoint_modified (bpt);
13854
d248b706
KY
13855 if (target_supports_enable_disable_tracepoint ()
13856 && current_trace_status ()->running && is_tracepoint (bpt))
13857 {
b00b30b2
SM
13858 for (bp_location &location : bpt->locations ())
13859 target_enable_tracepoint (&location);
d248b706
KY
13860 }
13861
b4c291bb 13862 bpt->disposition = disposition;
816338b5 13863 bpt->enable_count = count;
44702360 13864 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 13865
19081eb5 13866 notify_breakpoint_modified (bpt);
c906108c
SS
13867}
13868
fe3f5fa8 13869
c906108c 13870void
fba45db2 13871enable_breakpoint (struct breakpoint *bpt)
c906108c 13872{
816338b5 13873 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
13874}
13875
d0fe4701
XR
13876/* The enable command enables the specified breakpoints/locations (or
13877 all defined breakpoints) so they once again become (or continue to
13878 be) effective in stopping the inferior. ARGS may be in any of the
13879 forms defined in extract_bp_number_and_location. */
c906108c 13880
c906108c 13881static void
981a3fb3 13882enable_command (const char *args, int from_tty)
c906108c 13883{
d0fe4701 13884 enable_disable_command (args, from_tty, true);
c906108c
SS
13885}
13886
c906108c 13887static void
4495129a 13888enable_once_command (const char *args, int from_tty)
c906108c 13889{
48649e1b
TT
13890 map_breakpoint_numbers
13891 (args, [&] (breakpoint *b)
13892 {
13893 iterate_over_related_breakpoints
13894 (b, [&] (breakpoint *bpt)
13895 {
13896 enable_breakpoint_disp (bpt, disp_disable, 1);
13897 });
13898 });
816338b5
SS
13899}
13900
13901static void
4495129a 13902enable_count_command (const char *args, int from_tty)
816338b5 13903{
b9d61307
SM
13904 int count;
13905
13906 if (args == NULL)
13907 error_no_arg (_("hit count"));
13908
13909 count = get_number (&args);
816338b5 13910
48649e1b
TT
13911 map_breakpoint_numbers
13912 (args, [&] (breakpoint *b)
13913 {
13914 iterate_over_related_breakpoints
13915 (b, [&] (breakpoint *bpt)
13916 {
13917 enable_breakpoint_disp (bpt, disp_disable, count);
13918 });
13919 });
c906108c
SS
13920}
13921
c906108c 13922static void
4495129a 13923enable_delete_command (const char *args, int from_tty)
c906108c 13924{
48649e1b
TT
13925 map_breakpoint_numbers
13926 (args, [&] (breakpoint *b)
13927 {
13928 iterate_over_related_breakpoints
13929 (b, [&] (breakpoint *bpt)
13930 {
13931 enable_breakpoint_disp (bpt, disp_del, 1);
13932 });
13933 });
c906108c
SS
13934}
13935\f
1f3b5d1b
PP
13936/* Invalidate last known value of any hardware watchpoint if
13937 the memory which that value represents has been written to by
13938 GDB itself. */
13939
13940static void
8de0566d
YQ
13941invalidate_bp_value_on_memory_change (struct inferior *inferior,
13942 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
13943 const bfd_byte *data)
13944{
a1decfc1
SM
13945 for (breakpoint &bp : all_breakpoints ())
13946 if (bp.enable_state == bp_enabled
13947 && bp.type == bp_hardware_watchpoint)
1f3b5d1b 13948 {
a1decfc1 13949 watchpoint &wp = gdb::checked_static_cast<watchpoint &> (bp);
1f3b5d1b 13950
a1decfc1 13951 if (wp.val_valid && wp.val != nullptr)
3a5c3e22 13952 {
a1decfc1 13953 for (bp_location &loc : bp.locations ())
b00b30b2
SM
13954 if (loc.loc_type == bp_loc_hardware_watchpoint
13955 && loc.address + loc.length > addr
13956 && addr + len > loc.address)
3a5c3e22 13957 {
a1decfc1
SM
13958 wp.val = NULL;
13959 wp.val_valid = false;
3a5c3e22
PA
13960 }
13961 }
1f3b5d1b
PP
13962 }
13963}
13964
8181d85f
DJ
13965/* Create and insert a breakpoint for software single step. */
13966
13967void
6c95b8df 13968insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 13969 const address_space *aspace,
4a64f543 13970 CORE_ADDR next_pc)
8181d85f 13971{
7c16b83e
PA
13972 struct thread_info *tp = inferior_thread ();
13973 struct symtab_and_line sal;
13974 CORE_ADDR pc = next_pc;
8181d85f 13975
34b7e8a6
PA
13976 if (tp->control.single_step_breakpoints == NULL)
13977 {
960bc2bd 13978 std::unique_ptr<breakpoint> b
7ab97995
PA
13979 (new momentary_breakpoint (gdbarch, bp_single_step,
13980 current_program_space,
13981 null_frame_id,
13982 tp->global_num));
960bc2bd 13983
34b7e8a6 13984 tp->control.single_step_breakpoints
960bc2bd 13985 = add_to_breakpoint_chain (std::move (b));
34b7e8a6 13986 }
8181d85f 13987
7c16b83e
PA
13988 sal = find_pc_line (pc, 0);
13989 sal.pc = pc;
13990 sal.section = find_pc_overlay (pc);
13991 sal.explicit_pc = 1;
960bc2bd
PA
13992
13993 auto *ss_bp
98ed24fb
TT
13994 = (gdb::checked_static_cast<momentary_breakpoint *>
13995 (tp->control.single_step_breakpoints));
960bc2bd 13996 ss_bp->add_location (sal);
8181d85f 13997
7c16b83e 13998 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
13999}
14000
93f9a11f
YQ
14001/* Insert single step breakpoints according to the current state. */
14002
14003int
14004insert_single_step_breakpoints (struct gdbarch *gdbarch)
14005{
9c742269 14006 regcache *regcache = get_thread_regcache (inferior_thread ());
a0ff9e1a 14007 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14008
f5ea389a 14009 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14010
a0ff9e1a 14011 if (!next_pcs.empty ())
93f9a11f 14012 {
bd2b40ac 14013 frame_info_ptr frame = get_current_frame ();
8b86c959 14014 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14015
a0ff9e1a 14016 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14017 insert_single_step_breakpoint (gdbarch, aspace, pc);
14018
93f9a11f
YQ
14019 return 1;
14020 }
14021 else
14022 return 0;
14023}
14024
34b7e8a6 14025/* See breakpoint.h. */
f02253f1
HZ
14026
14027int
7c16b83e 14028breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14029 const address_space *aspace,
7c16b83e 14030 CORE_ADDR pc)
1aafd4da 14031{
b00b30b2
SM
14032 for (bp_location &loc : bp->locations ())
14033 if (loc.inserted
14034 && breakpoint_location_address_match (&loc, aspace, pc))
7c16b83e 14035 return 1;
1aafd4da 14036
7c16b83e 14037 return 0;
ef370185
JB
14038}
14039
14040/* Check whether a software single-step breakpoint is inserted at
14041 PC. */
14042
14043int
accd0bcd 14044single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14045 CORE_ADDR pc)
14046{
a1decfc1 14047 for (breakpoint &bpt : all_breakpoints ())
34b7e8a6 14048 {
a1decfc1
SM
14049 if (bpt.type == bp_single_step
14050 && breakpoint_has_location_inserted_here (&bpt, aspace, pc))
34b7e8a6
PA
14051 return 1;
14052 }
14053 return 0;
1aafd4da
UW
14054}
14055
1042e4c0
SS
14056/* Tracepoint-specific operations. */
14057
14058/* Set tracepoint count to NUM. */
14059static void
14060set_tracepoint_count (int num)
14061{
14062 tracepoint_count = num;
4fa62494 14063 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14064}
14065
70221824 14066static void
0b39b52e 14067trace_command (const char *arg, int from_tty)
1042e4c0 14068{
264f9890
PA
14069 location_spec_up locspec = string_to_location_spec (&arg,
14070 current_language);
14071 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
14072 (locspec.get (), true /* is_tracepoint */);
55aa24fb 14073
558a9d82 14074 create_breakpoint (get_current_arch (),
264f9890 14075 locspec.get (),
b080fe54 14076 NULL, -1, -1, arg, false, 1 /* parse arg */,
558a9d82
YQ
14077 0 /* tempflag */,
14078 bp_tracepoint /* type_wanted */,
14079 0 /* Ignore count */,
14080 pending_break_support,
14081 ops,
14082 from_tty,
14083 1 /* enabled */,
14084 0 /* internal */, 0);
1042e4c0
SS
14085}
14086
70221824 14087static void
0b39b52e 14088ftrace_command (const char *arg, int from_tty)
7a697b8d 14089{
264f9890
PA
14090 location_spec_up locspec = string_to_location_spec (&arg,
14091 current_language);
558a9d82 14092 create_breakpoint (get_current_arch (),
264f9890 14093 locspec.get (),
b080fe54 14094 NULL, -1, -1, arg, false, 1 /* parse arg */,
558a9d82
YQ
14095 0 /* tempflag */,
14096 bp_fast_tracepoint /* type_wanted */,
14097 0 /* Ignore count */,
14098 pending_break_support,
74421c0b 14099 &code_breakpoint_ops,
558a9d82
YQ
14100 from_tty,
14101 1 /* enabled */,
14102 0 /* internal */, 0);
0fb4aa4b
PA
14103}
14104
14105/* strace command implementation. Creates a static tracepoint. */
14106
70221824 14107static void
0b39b52e 14108strace_command (const char *arg, int from_tty)
0fb4aa4b 14109{
a678887d 14110 const struct breakpoint_ops *ops;
264f9890 14111 location_spec_up locspec;
7b572efb 14112 enum bptype type;
983af33b
SDJ
14113
14114 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14115 or with a normal static tracepoint. */
61012eef 14116 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14117 {
14118 ops = &strace_marker_breakpoint_ops;
264f9890
PA
14119 locspec = new_linespec_location_spec (&arg,
14120 symbol_name_match_type::FULL);
7b572efb 14121 type = bp_static_marker_tracepoint;
f00aae0f 14122 }
983af33b 14123 else
f00aae0f 14124 {
74421c0b 14125 ops = &code_breakpoint_ops;
264f9890 14126 locspec = string_to_location_spec (&arg, current_language);
7b572efb 14127 type = bp_static_tracepoint;
f00aae0f 14128 }
983af33b 14129
558a9d82 14130 create_breakpoint (get_current_arch (),
264f9890 14131 locspec.get (),
b080fe54 14132 NULL, -1, -1, arg, false, 1 /* parse arg */,
558a9d82 14133 0 /* tempflag */,
7b572efb 14134 type /* type_wanted */,
558a9d82
YQ
14135 0 /* Ignore count */,
14136 pending_break_support,
14137 ops,
14138 from_tty,
14139 1 /* enabled */,
14140 0 /* internal */, 0);
7a697b8d
SS
14141}
14142
409873ef
SS
14143/* Set up a fake reader function that gets command lines from a linked
14144 list that was acquired during tracepoint uploading. */
14145
14146static struct uploaded_tp *this_utp;
3149d8c1 14147static int next_cmd;
409873ef 14148
f8631e5e
SM
14149static const char *
14150read_uploaded_action (std::string &buffer)
409873ef 14151{
a18ba4e4 14152 char *rslt = nullptr;
409873ef 14153
a18ba4e4
SM
14154 if (next_cmd < this_utp->cmd_strings.size ())
14155 {
67aa1f3c 14156 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14157 next_cmd++;
14158 }
409873ef
SS
14159
14160 return rslt;
14161}
14162
00bf0b85
SS
14163/* Given information about a tracepoint as recorded on a target (which
14164 can be either a live system or a trace file), attempt to create an
14165 equivalent GDB tracepoint. This is not a reliable process, since
14166 the target does not necessarily have all the information used when
14167 the tracepoint was originally defined. */
14168
d9b3f62e 14169struct tracepoint *
00bf0b85 14170create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14171{
f2fc3015
TT
14172 const char *addr_str;
14173 char small_buf[100];
d9b3f62e 14174 struct tracepoint *tp;
fd9b8c24 14175
409873ef 14176 if (utp->at_string)
67aa1f3c 14177 addr_str = utp->at_string.get ();
409873ef
SS
14178 else
14179 {
14180 /* In the absence of a source location, fall back to raw
14181 address. Since there is no way to confirm that the address
14182 means the same thing as when the trace was started, warn the
14183 user. */
3e43a32a
MS
14184 warning (_("Uploaded tracepoint %d has no "
14185 "source location, using raw address"),
409873ef 14186 utp->number);
8c042590 14187 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14188 addr_str = small_buf;
14189 }
14190
14191 /* There's not much we can do with a sequence of bytecodes. */
14192 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14193 warning (_("Uploaded tracepoint %d condition "
14194 "has no source form, ignoring it"),
409873ef 14195 utp->number);
d5551862 14196
264f9890
PA
14197 location_spec_up locspec = string_to_location_spec (&addr_str,
14198 current_language);
ea020765
AB
14199
14200
14201 gdb_assert (addr_str != nullptr);
14202 if (*addr_str != '\0')
14203 error (_("Garbage '%s' at end of location"), addr_str);
14204
8cdf0e15 14205 if (!create_breakpoint (get_current_arch (),
264f9890 14206 locspec.get (),
b080fe54 14207 utp->cond_string.get (), -1, -1, addr_str,
10a636cc 14208 false /* force_condition */,
e7e0cddf 14209 0 /* parse cond/thread */,
8cdf0e15 14210 0 /* tempflag */,
0fb4aa4b 14211 utp->type /* type_wanted */,
8cdf0e15
VP
14212 0 /* Ignore count */,
14213 pending_break_support,
74421c0b 14214 &code_breakpoint_ops,
8cdf0e15 14215 0 /* from_tty */,
84f4c1fe 14216 utp->enabled /* enabled */,
44f238bb
PA
14217 0 /* internal */,
14218 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14219 return NULL;
fd9b8c24 14220
409873ef 14221 /* Get the tracepoint we just created. */
fd9b8c24
PA
14222 tp = get_tracepoint (tracepoint_count);
14223 gdb_assert (tp != NULL);
d5551862 14224
00bf0b85
SS
14225 if (utp->pass > 0)
14226 {
8c042590 14227 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14228 tp->number);
00bf0b85 14229
409873ef 14230 trace_pass_command (small_buf, 0);
00bf0b85
SS
14231 }
14232
409873ef
SS
14233 /* If we have uploaded versions of the original commands, set up a
14234 special-purpose "reader" function and call the usual command line
14235 reader, then pass the result to the breakpoint command-setting
14236 function. */
a18ba4e4 14237 if (!utp->cmd_strings.empty ())
00bf0b85 14238 {
12973681 14239 counted_command_line cmd_list;
00bf0b85 14240
409873ef 14241 this_utp = utp;
3149d8c1 14242 next_cmd = 0;
d5551862 14243
60b3cef2 14244 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14245
c1fc2657 14246 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14247 }
a18ba4e4
SM
14248 else if (!utp->actions.empty ()
14249 || !utp->step_actions.empty ())
3e43a32a
MS
14250 warning (_("Uploaded tracepoint %d actions "
14251 "have no source form, ignoring them"),
409873ef 14252 utp->number);
00bf0b85 14253
f196051f 14254 /* Copy any status information that might be available. */
c1fc2657 14255 tp->hit_count = utp->hit_count;
f196051f
SS
14256 tp->traceframe_usage = utp->traceframe_usage;
14257
00bf0b85 14258 return tp;
d9b3f62e 14259}
00bf0b85 14260
1042e4c0
SS
14261/* Print information on tracepoint number TPNUM_EXP, or all if
14262 omitted. */
14263
14264static void
1d12d88f 14265info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14266{
79a45e25 14267 struct ui_out *uiout = current_uiout;
e5a67952 14268 int num_printed;
1042e4c0 14269
5c458ae8 14270 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14271
14272 if (num_printed == 0)
1042e4c0 14273 {
e5a67952 14274 if (args == NULL || *args == '\0')
112e8700 14275 uiout->message ("No tracepoints.\n");
d77f58be 14276 else
112e8700 14277 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14278 }
ad443146
SS
14279
14280 default_collect_info ();
1042e4c0
SS
14281}
14282
4a64f543 14283/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14284 Not supported by all targets. */
14285static void
5fed81ff 14286enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14287{
14288 enable_command (args, from_tty);
14289}
14290
4a64f543 14291/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14292 Not supported by all targets. */
14293static void
5fed81ff 14294disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14295{
14296 disable_command (args, from_tty);
14297}
14298
4a64f543 14299/* Remove a tracepoint (or all if no argument). */
1042e4c0 14300static void
4495129a 14301delete_trace_command (const char *arg, int from_tty)
1042e4c0 14302{
1042e4c0
SS
14303 dont_repeat ();
14304
14305 if (arg == 0)
14306 {
14307 int breaks_to_delete = 0;
14308
4a4fd10d 14309 /* Delete all tracepoints if no argument.
dda83cd7
SM
14310 Do not delete internal or call-dummy breakpoints, these
14311 have to be deleted with an explicit breakpoint number
4a64f543 14312 argument. */
a1decfc1
SM
14313 for (breakpoint &tp : all_tracepoints ())
14314 if (is_tracepoint (&tp) && user_breakpoint_p (&tp))
1042e4c0
SS
14315 {
14316 breaks_to_delete = 1;
14317 break;
14318 }
1042e4c0
SS
14319
14320 /* Ask user only if there are some breakpoints to delete. */
14321 if (!from_tty
14322 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14323 {
a1decfc1
SM
14324 for (breakpoint &b : all_breakpoints_safe ())
14325 if (is_tracepoint (&b) && user_breakpoint_p (&b))
14326 delete_breakpoint (&b);
1042e4c0
SS
14327 }
14328 }
14329 else
48649e1b 14330 map_breakpoint_numbers
b926417a 14331 (arg, [&] (breakpoint *br)
48649e1b 14332 {
b926417a 14333 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14334 });
1042e4c0
SS
14335}
14336
197f0a60
TT
14337/* Helper function for trace_pass_command. */
14338
14339static void
d9b3f62e 14340trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14341{
d9b3f62e 14342 tp->pass_count = count;
19081eb5 14343 notify_breakpoint_modified (tp);
197f0a60 14344 if (from_tty)
6cb06a8c
TT
14345 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14346 tp->number, count);
197f0a60
TT
14347}
14348
1042e4c0
SS
14349/* Set passcount for tracepoint.
14350
14351 First command argument is passcount, second is tracepoint number.
14352 If tracepoint number omitted, apply to most recently defined.
14353 Also accepts special argument "all". */
14354
14355static void
0b39b52e 14356trace_pass_command (const char *args, int from_tty)
1042e4c0 14357{
0b39b52e 14358 ULONGEST count;
1042e4c0
SS
14359
14360 if (args == 0 || *args == 0)
3e43a32a
MS
14361 error (_("passcount command requires an "
14362 "argument (count + optional TP num)"));
1042e4c0 14363
0b39b52e 14364 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14365
529480d0 14366 args = skip_spaces (args);
1042e4c0
SS
14367 if (*args && strncasecmp (args, "all", 3) == 0)
14368 {
14369 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14370 if (*args)
14371 error (_("Junk at end of arguments."));
1042e4c0 14372
a1decfc1 14373 for (breakpoint &b : all_tracepoints ())
01add95b 14374 {
a1decfc1
SM
14375 tracepoint &t1 = gdb::checked_static_cast<tracepoint &> (b);
14376 trace_pass_set_count (&t1, count, from_tty);
01add95b 14377 }
197f0a60
TT
14378 }
14379 else if (*args == '\0')
1042e4c0 14380 {
a1decfc1 14381 tracepoint *t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14382 if (t1)
197f0a60
TT
14383 trace_pass_set_count (t1, count, from_tty);
14384 }
14385 else
14386 {
bfd28288
PA
14387 number_or_range_parser parser (args);
14388 while (!parser.finished ())
1042e4c0 14389 {
a1decfc1 14390 tracepoint *t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14391 if (t1)
14392 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14393 }
14394 }
1042e4c0
SS
14395}
14396
d9b3f62e 14397struct tracepoint *
1042e4c0
SS
14398get_tracepoint (int num)
14399{
a1decfc1
SM
14400 for (breakpoint &t : all_tracepoints ())
14401 if (t.number == num)
14402 return gdb::checked_static_cast<tracepoint *> (&t);
1042e4c0
SS
14403
14404 return NULL;
14405}
14406
d5551862
SS
14407/* Find the tracepoint with the given target-side number (which may be
14408 different from the tracepoint number after disconnecting and
14409 reconnecting). */
14410
d9b3f62e 14411struct tracepoint *
d5551862
SS
14412get_tracepoint_by_number_on_target (int num)
14413{
a1decfc1 14414 for (breakpoint &b : all_tracepoints ())
d9b3f62e 14415 {
a1decfc1 14416 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
d9b3f62e 14417
a1decfc1
SM
14418 if (t.number_on_target == num)
14419 return &t;
d9b3f62e 14420 }
d5551862
SS
14421
14422 return NULL;
14423}
14424
1042e4c0 14425/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14426 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14427 If the argument is missing, the most recent tracepoint
14428 (tracepoint_count) is returned. */
14429
d9b3f62e 14430struct tracepoint *
0b39b52e 14431get_tracepoint_by_number (const char **arg,
bfd28288 14432 number_or_range_parser *parser)
1042e4c0 14433{
1042e4c0 14434 int tpnum;
0b39b52e 14435 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14436
bfd28288 14437 if (parser != NULL)
197f0a60 14438 {
bfd28288
PA
14439 gdb_assert (!parser->finished ());
14440 tpnum = parser->get_number ();
197f0a60
TT
14441 }
14442 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14443 tpnum = tracepoint_count;
1042e4c0 14444 else
197f0a60 14445 tpnum = get_number (arg);
1042e4c0
SS
14446
14447 if (tpnum <= 0)
14448 {
14449 if (instring && *instring)
6cb06a8c
TT
14450 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14451 instring);
1042e4c0 14452 else
6cb06a8c 14453 gdb_printf (_("No previous tracepoint\n"));
1042e4c0
SS
14454 return NULL;
14455 }
14456
a1decfc1
SM
14457 for (breakpoint &t : all_tracepoints ())
14458 if (t.number == tpnum)
14459 return gdb::checked_static_cast<tracepoint *> (&t);
1042e4c0 14460
6cb06a8c 14461 gdb_printf ("No tracepoint number %d.\n", tpnum);
1042e4c0
SS
14462 return NULL;
14463}
14464
d9b3f62e 14465void
04d0163c 14466breakpoint::print_recreate_thread (struct ui_file *fp) const
d9b3f62e 14467{
04d0163c 14468 if (thread != -1)
442716d4
AB
14469 {
14470 struct thread_info *thr = find_thread_global_id (thread);
14471 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14472 }
d9b3f62e 14473
2ecee236 14474 if (task != -1)
04d0163c 14475 gdb_printf (fp, " task %d", task);
d9b3f62e 14476
6cb06a8c 14477 gdb_printf (fp, "\n");
d9b3f62e
PA
14478}
14479
6149aea9
PA
14480/* Save information on user settable breakpoints (watchpoints, etc) to
14481 a new script file named FILENAME. If FILTER is non-NULL, call it
14482 on each breakpoint and only include the ones for which it returns
f2478a7e 14483 true. */
6149aea9 14484
1042e4c0 14485static void
4495129a 14486save_breakpoints (const char *filename, int from_tty,
f2478a7e 14487 bool (*filter) (const struct breakpoint *))
1042e4c0 14488{
d8de7963 14489 bool any = false;
6149aea9 14490 int extra_trace_bits = 0;
1042e4c0 14491
6149aea9
PA
14492 if (filename == 0 || *filename == 0)
14493 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14494
14495 /* See if we have anything to save. */
a1decfc1 14496 for (breakpoint &tp : all_breakpoints ())
01add95b
SM
14497 {
14498 /* Skip internal and momentary breakpoints. */
a1decfc1 14499 if (!user_breakpoint_p (&tp))
01add95b 14500 continue;
6149aea9 14501
01add95b 14502 /* If we have a filter, only save the breakpoints it accepts. */
a1decfc1 14503 if (filter && !filter (&tp))
01add95b 14504 continue;
6149aea9 14505
d8de7963 14506 any = true;
6149aea9 14507
a1decfc1 14508 if (is_tracepoint (&tp))
01add95b
SM
14509 {
14510 extra_trace_bits = 1;
6149aea9 14511
01add95b
SM
14512 /* We can stop searching. */
14513 break;
14514 }
14515 }
6149aea9
PA
14516
14517 if (!any)
1042e4c0 14518 {
6149aea9 14519 warning (_("Nothing to save."));
1042e4c0
SS
14520 return;
14521 }
14522
ee0c3293 14523 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
14524
14525 stdio_file fp;
14526
ee0c3293 14527 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 14528 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 14529 expanded_filename.get (), safe_strerror (errno));
8bf6485c 14530
6149aea9 14531 if (extra_trace_bits)
d7e74731 14532 save_trace_state_variables (&fp);
8bf6485c 14533
a1decfc1 14534 for (breakpoint &tp : all_breakpoints ())
01add95b
SM
14535 {
14536 /* Skip internal and momentary breakpoints. */
a1decfc1 14537 if (!user_breakpoint_p (&tp))
01add95b 14538 continue;
8bf6485c 14539
01add95b 14540 /* If we have a filter, only save the breakpoints it accepts. */
a1decfc1 14541 if (filter && !filter (&tp))
01add95b 14542 continue;
6149aea9 14543
a1decfc1 14544 tp.print_recreate (&fp);
1042e4c0 14545
01add95b
SM
14546 /* Note, we can't rely on tp->number for anything, as we can't
14547 assume the recreated breakpoint numbers will match. Use $bpnum
14548 instead. */
6149aea9 14549
a1decfc1
SM
14550 if (tp.cond_string)
14551 fp.printf (" condition $bpnum %s\n", tp.cond_string.get ());
6149aea9 14552
a1decfc1
SM
14553 if (tp.ignore_count)
14554 fp.printf (" ignore $bpnum %d\n", tp.ignore_count);
6149aea9 14555
a1decfc1 14556 if (tp.type != bp_dprintf && tp.commands)
01add95b
SM
14557 {
14558 fp.puts (" commands\n");
1042e4c0 14559
992aeed8 14560 ui_out_redirect_pop redir (current_uiout, &fp);
a1decfc1 14561 print_command_lines (current_uiout, tp.commands.get (), 2);
6149aea9 14562
01add95b
SM
14563 fp.puts (" end\n");
14564 }
6149aea9 14565
a1decfc1 14566 if (tp.enable_state == bp_disabled)
01add95b 14567 fp.puts ("disable $bpnum\n");
6149aea9 14568
01add95b
SM
14569 /* If this is a multi-location breakpoint, check if the locations
14570 should be individually disabled. Watchpoint locations are
14571 special, and not user visible. */
a1decfc1 14572 if (!is_watchpoint (&tp) && tp.has_multiple_locations ())
01add95b
SM
14573 {
14574 int n = 1;
40cb8ca5 14575
a1decfc1 14576 for (bp_location &loc : tp.locations ())
01add95b 14577 {
b00b30b2 14578 if (!loc.enabled)
01add95b
SM
14579 fp.printf ("disable $bpnum.%d\n", n);
14580
14581 n++;
14582 }
14583 }
14584 }
8bf6485c 14585
e0700ba4
SM
14586 if (extra_trace_bits && !default_collect.empty ())
14587 fp.printf ("set default-collect %s\n", default_collect.c_str ());
8bf6485c 14588
1042e4c0 14589 if (from_tty)
6cb06a8c 14590 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
14591}
14592
14593/* The `save breakpoints' command. */
14594
14595static void
4495129a 14596save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
14597{
14598 save_breakpoints (args, from_tty, NULL);
14599}
14600
14601/* The `save tracepoints' command. */
14602
14603static void
4495129a 14604save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
14605{
14606 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
14607}
14608
c906108c 14609\f
629500fa
KS
14610/* This help string is used to consolidate all the help string for specifying
14611 locations used by several commands. */
14612
264f9890 14613#define LOCATION_SPEC_HELP_STRING \
629500fa
KS
14614"Linespecs are colon-separated lists of location parameters, such as\n\
14615source filename, function name, label name, and line number.\n\
14616Example: To specify the start of a label named \"the_top\" in the\n\
14617function \"fact\" in the file \"factorial.c\", use\n\
14618\"factorial.c:fact:the_top\".\n\
14619\n\
14620Address locations begin with \"*\" and specify an exact address in the\n\
14621program. Example: To specify the fourth byte past the start function\n\
14622\"main\", use \"*main + 4\".\n\
14623\n\
14624Explicit locations are similar to linespecs but use an option/argument\n\
14625syntax to specify location parameters.\n\
14626Example: To specify the start of the label named \"the_top\" in the\n\
14627function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
14628-function fact -label the_top\".\n\
14629\n\
14630By default, a specified function is matched against the program's\n\
14631functions in all scopes. For C++, this means in all namespaces and\n\
14632classes. For Ada, this means in all packages. E.g., in C++,\n\
14633\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14634\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 14635specified name as a complete fully-qualified name instead."
629500fa 14636
4a64f543
MS
14637/* This help string is used for the break, hbreak, tbreak and thbreak
14638 commands. It is defined as a macro to prevent duplication.
14639 COMMAND should be a string constant containing the name of the
14640 command. */
629500fa 14641
31e2b00f 14642#define BREAK_ARGS_HELP(command) \
733d554a
TBA
14643command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14644\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
14645PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14646probe point. Accepted values are `-probe' (for a generic, automatically\n\
be2a6a58 14647guessed probe type), `-probe-stap' (for a SystemTap probe) or\n\
d4777acb 14648`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
14649LOCATION may be a linespec, address, or explicit location as described\n\
14650below.\n\
14651\n\
dc10affe
PA
14652With no LOCATION, uses current execution address of the selected\n\
14653stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
14654\n\
14655THREADNUM is the number from \"info threads\".\n\
14656CONDITION is a boolean expression.\n\
733d554a
TBA
14657\n\
14658With the \"-force-condition\" flag, the condition is defined even when\n\
14659it is invalid for all current locations.\n\
264f9890 14660\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
14661Multiple breakpoints at one place are permitted, and useful if their\n\
14662conditions are different.\n\
31e2b00f
AS
14663\n\
14664Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14665
44feb3ce
TT
14666/* List of subcommands for "catch". */
14667static struct cmd_list_element *catch_cmdlist;
14668
14669/* List of subcommands for "tcatch". */
14670static struct cmd_list_element *tcatch_cmdlist;
14671
9ac4176b 14672void
a121b7c1 14673add_catch_command (const char *name, const char *docstring,
5538b03c 14674 cmd_func_ftype *func,
625e8578 14675 completer_ftype *completer,
44feb3ce
TT
14676 void *user_data_catch,
14677 void *user_data_tcatch)
14678{
14679 struct cmd_list_element *command;
14680
0450cc4c 14681 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14682 &catch_cmdlist);
5538b03c 14683 command->func = func;
0f8e2034 14684 command->set_context (user_data_catch);
a96d9b2e 14685 set_cmd_completer (command, completer);
44feb3ce 14686
0450cc4c 14687 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce 14688 &tcatch_cmdlist);
5538b03c 14689 command->func = func;
0f8e2034 14690 command->set_context (user_data_tcatch);
a96d9b2e 14691 set_cmd_completer (command, completer);
44feb3ce
TT
14692}
14693
d8de7963
AB
14694/* False if any of the breakpoint's locations could be a location where
14695 functions have been inlined, true otherwise. */
0574c78f 14696
d8de7963 14697static bool
0574c78f
GB
14698is_non_inline_function (struct breakpoint *b)
14699{
14700 /* The shared library event breakpoint is set on the address of a
14701 non-inline function. */
d8de7963 14702 return (b->type == bp_shlib_event);
0574c78f
GB
14703}
14704
14705/* Nonzero if the specified PC cannot be a location where functions
14706 have been inlined. */
14707
14708int
accd0bcd 14709pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
c272a98c 14710 const target_waitstatus &ws)
0574c78f 14711{
a1decfc1 14712 for (breakpoint &b : all_breakpoints ())
0574c78f 14713 {
a1decfc1 14714 if (!is_non_inline_function (&b))
0574c78f
GB
14715 continue;
14716
a1decfc1 14717 for (bp_location &bl : b.locations ())
0574c78f 14718 {
b00b30b2
SM
14719 if (!bl.shlib_disabled
14720 && bpstat_check_location (&bl, aspace, pc, ws))
0574c78f
GB
14721 return 1;
14722 }
14723 }
14724
14725 return 0;
14726}
14727
2f202fde
JK
14728/* Remove any references to OBJFILE which is going to be freed. */
14729
14730void
14731breakpoint_free_objfile (struct objfile *objfile)
14732{
48d7020b 14733 for (bp_location *loc : all_bp_locations ())
5066f368
AB
14734 {
14735 if (loc->symtab != nullptr
14736 && loc->symtab->compunit ()->objfile () == objfile)
14737 {
14738 loc->symtab = nullptr;
14739 loc->symbol = nullptr;
14740 loc->msymbol = nullptr;
14741 }
14742
14743 if (loc->section != nullptr
14744 && loc->section->objfile == objfile)
14745 {
14746 /* If symtab was set then it should have already been cleared.
14747 But if bp_location::msymbol was set then the symbol and symtab
14748 might already have been nullptr. */
14749 gdb_assert (loc->symtab == nullptr);
14750 loc->section = nullptr;
14751 loc->symbol = nullptr;
14752 loc->msymbol = nullptr;
14753 }
14754
14755 if (loc->probe.objfile == objfile)
14756 loc->probe = bound_probe ();
14757 }
2f202fde
JK
14758}
14759
8bfd80db
YQ
14760/* Chain containing all defined "enable breakpoint" subcommands. */
14761
14762static struct cmd_list_element *enablebreaklist = NULL;
14763
8588b356
SM
14764/* See breakpoint.h. */
14765
14766cmd_list_element *commands_cmd_element = nullptr;
14767
5fe70629 14768INIT_GDB_FILE (breakpoint)
c906108c
SS
14769{
14770 struct cmd_list_element *c;
14771
c90e7d63
SM
14772 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14773 "breakpoint");
14774 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14775 "breakpoint");
14776 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14777 "breakpoint");
84acb35a 14778
c906108c
SS
14779 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14780 before a breakpoint is set. */
14781 breakpoint_count = 0;
14782
1042e4c0
SS
14783 tracepoint_count = 0;
14784
1bedd215
AC
14785 add_com ("ignore", class_breakpoint, ignore_command, _("\
14786Set ignore-count of breakpoint number N to COUNT.\n\
14787Usage is `ignore N COUNT'."));
c906108c 14788
8588b356
SM
14789 commands_cmd_element = add_com ("commands", class_breakpoint,
14790 commands_command, _("\
18da0c51
MG
14791Set commands to be executed when the given breakpoints are hit.\n\
14792Give a space-separated breakpoint list as argument after \"commands\".\n\
14793A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14794(e.g. `5-7').\n\
c906108c
SS
14795With no argument, the targeted breakpoint is the last one set.\n\
14796The commands themselves follow starting on the next line.\n\
14797Type a line containing \"end\" to indicate the end of them.\n\
14798Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 14799then no output is printed when it is hit, except what the commands print."));
c906108c 14800
b1d4d8d1
TBA
14801 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14802 static std::string condition_command_help
14803 = gdb::option::build_help (_("\
1bedd215 14804Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 14805Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 14806is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
14807\n\
14808Options:\n\
14809%OPTIONS%"), cc_opts);
14810
14811 c = add_com ("condition", class_breakpoint, condition_command,
14812 condition_command_help.c_str ());
14813 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 14814
1bedd215 14815 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 14816Set a temporary breakpoint.\n\
c906108c
SS
14817Like \"break\" except the breakpoint is only temporary,\n\
14818so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
14819by using \"enable delete\" on the breakpoint number.\n\
14820\n"
14821BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 14822 set_cmd_completer (c, location_completer);
c94fdfd0 14823
1bedd215 14824 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 14825Set a hardware assisted breakpoint.\n\
c906108c 14826Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
14827some target hardware may not have this support.\n\
14828\n"
14829BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 14830 set_cmd_completer (c, location_completer);
c906108c 14831
1bedd215 14832 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 14833Set a temporary hardware assisted breakpoint.\n\
c906108c 14834Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
14835so it will be deleted when hit.\n\
14836\n"
14837BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 14838 set_cmd_completer (c, location_completer);
c906108c 14839
3947f654
SM
14840 cmd_list_element *enable_cmd
14841 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
14842Enable all or some breakpoints.\n\
14843Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
14844Give breakpoint numbers (separated by spaces) as arguments.\n\
14845With no subcommand, breakpoints are enabled until you command otherwise.\n\
14846This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 14847With a subcommand you can enable temporarily."),
3947f654 14848 &enablelist, 1, &cmdlist);
c906108c 14849
3947f654 14850 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 14851
84951ab5 14852 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
14853Enable all or some breakpoints.\n\
14854Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14855Give breakpoint numbers (separated by spaces) as arguments.\n\
14856This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 14857May be abbreviated to simply \"enable\"."),
2f822da5 14858 &enablebreaklist, 1, &enablelist);
c906108c 14859
1a966eab 14860 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14861Enable some breakpoints for one hit.\n\
14862Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 14863If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
14864 &enablebreaklist);
14865
1a966eab 14866 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14867Enable some breakpoints and delete when hit.\n\
14868Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 14869If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14870 &enablebreaklist);
14871
816338b5 14872 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14873Enable some breakpoints for COUNT hits.\n\
14874Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14875If a breakpoint is hit while enabled in this fashion,\n\
14876the count is decremented; when it reaches zero, the breakpoint is disabled."),
14877 &enablebreaklist);
14878
1a966eab 14879 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
14880Enable some breakpoints and delete when hit.\n\
14881Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 14882If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
14883 &enablelist);
14884
1a966eab 14885 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
14886Enable some breakpoints for one hit.\n\
14887Usage: enable once BREAKPOINTNUM...\n\
1a966eab 14888If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
14889 &enablelist);
14890
14891 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
14892Enable some breakpoints for COUNT hits.\n\
14893Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
14894If a breakpoint is hit while enabled in this fashion,\n\
14895the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
14896 &enablelist);
14897
3947f654
SM
14898 cmd_list_element *disable_cmd
14899 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
14900Disable all or some breakpoints.\n\
14901Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
14902Arguments are breakpoint numbers with spaces in between.\n\
14903To disable all breakpoints, give no argument.\n\
64b9b334 14904A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
14905 &disablelist, 1, &cmdlist);
14906 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14907 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 14908
57b4f16e 14909 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
14910Disable all or some breakpoints.\n\
14911Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
14912Arguments are breakpoint numbers with spaces in between.\n\
14913To disable all breakpoints, give no argument.\n\
64b9b334 14914A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 14915This command may be abbreviated \"disable\"."),
c906108c
SS
14916 &disablelist);
14917
3947f654
SM
14918 cmd_list_element *delete_cmd
14919 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
14920Delete all or some breakpoints.\n\
14921Usage: delete [BREAKPOINTNUM]...\n\
c906108c 14922Arguments are breakpoint numbers with spaces in between.\n\
4a4fd10d
TY
14923To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
14924give no argument.\n\
c906108c 14925\n\
590042fc 14926Also a prefix command for deletion of other GDB objects."),
3947f654
SM
14927 &deletelist, 1, &cmdlist);
14928 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14929 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 14930
57b4f16e 14931 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
14932Delete all or some breakpoints or auto-display expressions.\n\
14933Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c 14934Arguments are breakpoint numbers with spaces in between.\n\
4a4fd10d
TY
14935To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
14936give no argument.\n\
1a966eab 14937This command may be abbreviated \"delete\"."),
c906108c
SS
14938 &deletelist);
14939
3947f654
SM
14940 cmd_list_element *clear_cmd
14941 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
14942Clear breakpoint at specified location.\n\
14943Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
14944\n\
14945With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 14946is executing in.\n"
264f9890 14947"\n" LOCATION_SPEC_HELP_STRING "\n\n\
1bedd215 14948See also the \"delete\" command which clears breakpoints by number."));
3947f654 14949 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 14950
3947f654
SM
14951 cmd_list_element *break_cmd
14952 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 14953Set breakpoint at specified location.\n"
31e2b00f 14954BREAK_ARGS_HELP ("break")));
3947f654 14955 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 14956
3947f654
SM
14957 add_com_alias ("b", break_cmd, class_run, 1);
14958 add_com_alias ("br", break_cmd, class_run, 1);
14959 add_com_alias ("bre", break_cmd, class_run, 1);
14960 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 14961
e0f25bd9
SM
14962 cmd_list_element *info_breakpoints_cmd
14963 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 14964Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
14965The \"Type\" column indicates one of:\n\
14966\tbreakpoint - normal breakpoint\n\
14967\twatchpoint - watchpoint\n\
14968The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14969the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14970breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
14971address and file/line number respectively.\n\
14972\n\
14973Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14974are set to the address of the last breakpoint listed unless the command\n\
14975is prefixed with \"server \".\n\n\
c906108c 14976Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 14977breakpoint set."));
c906108c 14978
e0f25bd9 14979 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 14980
1a966eab
AC
14981 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14982Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
14983The \"Type\" column indicates one of:\n\
14984\tbreakpoint - normal breakpoint\n\
14985\twatchpoint - watchpoint\n\
14986\tlongjmp - internal breakpoint used to step through longjmp()\n\
14987\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14988\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
14989\tfinish - internal breakpoint used by the \"finish\" command\n\
14990The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
14991the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14992breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
14993address and file/line number respectively.\n\
14994\n\
14995Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
14996are set to the address of the last breakpoint listed unless the command\n\
14997is prefixed with \"server \".\n\n\
c906108c 14998Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 14999breakpoint set."),
c906108c
SS
15000 &maintenanceinfolist);
15001
3b6acaee 15002 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15003Set catchpoints to catch events."),
2f822da5 15004 &catch_cmdlist,
3b6acaee 15005 0/*allow-unknown*/, &cmdlist);
44feb3ce 15006
3b6acaee 15007 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15008Set temporary catchpoints to catch events."),
2f822da5 15009 &tcatch_cmdlist,
3b6acaee 15010 0/*allow-unknown*/, &cmdlist);
44feb3ce 15011
a15a5258
AB
15012 const auto opts = make_watch_options_def_group (nullptr);
15013
15014 static const std::string watch_help = gdb::option::build_help (_("\
15015Set a watchpoint for EXPRESSION.\n\
15016Usage: watch [-location] EXPRESSION\n\
15017\n\
15018Options:\n\
15019%OPTIONS%\n\
15020\n\
c906108c 15021A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
15022an expression changes."), opts);
15023 c = add_com ("watch", class_breakpoint, watch_command,
15024 watch_help.c_str ());
15025 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15026
15027 static const std::string rwatch_help = gdb::option::build_help (_("\
15028Set a read watchpoint for EXPRESSION.\n\
15029Usage: rwatch [-location] EXPRESSION\n\
15030\n\
15031Options:\n\
15032%OPTIONS%\n\
15033\n\
15034A read watchpoint stops execution of your program whenever the value of\n\
15035an expression is read."), opts);
15036 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15037 rwatch_help.c_str ());
15038 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15039
15040 static const std::string awatch_help = gdb::option::build_help (_("\
15041Set an access watchpoint for EXPRESSION.\n\
15042Usage: awatch [-location] EXPRESSION\n\
15043\n\
15044Options:\n\
15045%OPTIONS%\n\
15046\n\
15047An access watchpoint stops execution of your program whenever the value\n\
15048of an expression is either read or written."), opts);
15049 c = add_com ("awatch", class_breakpoint, awatch_command,
15050 awatch_help.c_str ());
15051 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 15052
11db9430 15053 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15054Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15055
920d2a44
AC
15056 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15057 respond to changes - contrary to the description. */
85c07804
AC
15058 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15059 &can_use_hw_watchpoints, _("\
15060Set debugger's willingness to use watchpoint hardware."), _("\
15061Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15062If zero, gdb will not use hardware for new watchpoints, even if\n\
15063such is available. (However, any hardware watchpoints that were\n\
15064created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15065hardware.)"),
15066 NULL,
920d2a44 15067 show_can_use_hw_watchpoints,
85c07804 15068 &setlist, &showlist);
c906108c
SS
15069
15070 can_use_hw_watchpoints = 1;
fa8d40ab 15071
1042e4c0
SS
15072 /* Tracepoint manipulation commands. */
15073
3947f654
SM
15074 cmd_list_element *trace_cmd
15075 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15076Set a tracepoint at specified location.\n\
1042e4c0
SS
15077\n"
15078BREAK_ARGS_HELP ("trace") "\n\
15079Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 15080 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 15081
3947f654
SM
15082 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15083 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15084 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15085 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 15086
7a697b8d 15087 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15088Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15089\n"
15090BREAK_ARGS_HELP ("ftrace") "\n\
15091Do \"help tracepoints\" for info on other tracepoint commands."));
15092 set_cmd_completer (c, location_completer);
15093
0fb4aa4b 15094 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15095Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15096\n\
15097strace [LOCATION] [if CONDITION]\n\
be2a6a58 15098LOCATION may be a linespec, explicit, or address location (described below)\n\
629500fa
KS
15099or -m MARKER_ID.\n\n\
15100If a marker id is specified, probe the marker with that name. With\n\
15101no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15102Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15103This collects arbitrary user data passed in the probe point call to the\n\
15104tracing library. You can inspect it when analyzing the trace buffer,\n\
15105by printing the $_sdata variable like any other convenience variable.\n\
15106\n\
15107CONDITION is a boolean expression.\n\
264f9890 15108\n" LOCATION_SPEC_HELP_STRING "\n\n\
d41c0fc8
PA
15109Multiple tracepoints at one place are permitted, and useful if their\n\
15110conditions are different.\n\
0fb4aa4b
PA
15111\n\
15112Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15113Do \"help tracepoints\" for info on other tracepoint commands."));
15114 set_cmd_completer (c, location_completer);
15115
e0f25bd9
SM
15116 cmd_list_element *info_tracepoints_cmd
15117 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15118Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15119Convenience variable \"$tpnum\" contains the number of the\n\
15120last tracepoint set."));
15121
e0f25bd9 15122 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 15123
5e84b7ee
SM
15124 cmd_list_element *delete_tracepoints_cmd
15125 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
15126Delete specified tracepoints.\n\
15127Arguments are tracepoint numbers, separated by spaces.\n\
15128No argument means delete all tracepoints."),
15129 &deletelist);
5e84b7ee 15130 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
15131
15132 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15133Disable specified tracepoints.\n\
15134Arguments are tracepoint numbers, separated by spaces.\n\
15135No argument means disable all tracepoints."),
15136 &disablelist);
15137 deprecate_cmd (c, "disable");
15138
15139 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15140Enable specified tracepoints.\n\
15141Arguments are tracepoint numbers, separated by spaces.\n\
15142No argument means enable all tracepoints."),
15143 &enablelist);
15144 deprecate_cmd (c, "enable");
15145
15146 add_com ("passcount", class_trace, trace_pass_command, _("\
15147Set the passcount for a tracepoint.\n\
15148The trace will end when the tracepoint has been passed 'count' times.\n\
15149Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15150if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15151
0743fc83
TT
15152 add_basic_prefix_cmd ("save", class_breakpoint,
15153 _("Save breakpoint definitions as a script."),
2f822da5 15154 &save_cmdlist,
0743fc83 15155 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
15156
15157 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15158Save current breakpoint definitions as a script.\n\
cce7e648 15159This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15160catchpoints, tracepoints). Use the 'source' command in another debug\n\
15161session to restore them."),
15162 &save_cmdlist);
dc22ab49 15163 set_cmd_completer (c, deprecated_filename_completer);
6149aea9 15164
3947f654
SM
15165 cmd_list_element *save_tracepoints_cmd
15166 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15167Save current tracepoint definitions as a script.\n\
6149aea9
PA
15168Use the 'source' command in another debug session to restore them."),
15169 &save_cmdlist);
dc22ab49 15170 set_cmd_completer (save_tracepoints_cmd, deprecated_filename_completer);
1042e4c0 15171
3947f654 15172 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
15173 deprecate_cmd (c, "save tracepoints");
15174
f54bdb6d
SM
15175 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
15176 _("\
590042fc 15177Breakpoint specific settings.\n\
fa8d40ab 15178Configure various breakpoint-specific variables such as\n\
590042fc 15179pending breakpoint behavior."),
f54bdb6d 15180 _("\
590042fc 15181Breakpoint specific settings.\n\
fa8d40ab 15182Configure various breakpoint-specific variables such as\n\
590042fc 15183pending breakpoint behavior."),
f54bdb6d
SM
15184 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
15185 &setlist, &showlist);
fa8d40ab 15186
7915a72c
AC
15187 add_setshow_auto_boolean_cmd ("pending", no_class,
15188 &pending_break_support, _("\
15189Set debugger's behavior regarding pending breakpoints."), _("\
15190Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15191If on, an unrecognized breakpoint location will cause gdb to create a\n\
15192pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15193an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15194user-query to see if a pending breakpoint should be created."),
2c5b56ce 15195 NULL,
920d2a44 15196 show_pending_break_support,
6e1d7d6c
AC
15197 &breakpoint_set_cmdlist,
15198 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15199
15200 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15201
15202 add_setshow_boolean_cmd ("auto-hw", no_class,
15203 &automatic_hardware_breakpoints, _("\
15204Set automatic usage of hardware breakpoints."), _("\
15205Show automatic usage of hardware breakpoints."), _("\
15206If set, the debugger will automatically use hardware breakpoints for\n\
15207breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15208a warning will be emitted for such breakpoints."),
15209 NULL,
15210 show_automatic_hardware_breakpoints,
15211 &breakpoint_set_cmdlist,
15212 &breakpoint_show_cmdlist);
74960c60 15213
a25a5a45
PA
15214 add_setshow_boolean_cmd ("always-inserted", class_support,
15215 &always_inserted_mode, _("\
74960c60
VP
15216Set mode for inserting breakpoints."), _("\
15217Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15218When this mode is on, breakpoints are inserted immediately as soon as\n\
15219they're created, kept inserted even when execution stops, and removed\n\
15220only when the user deletes them. When this mode is off (the default),\n\
15221breakpoints are inserted only when execution continues, and removed\n\
15222when execution stops."),
72d0e2c5
YQ
15223 NULL,
15224 &show_always_inserted_mode,
15225 &breakpoint_set_cmdlist,
15226 &breakpoint_show_cmdlist);
3cdc2d7e
MS
15227
15228 add_setshow_boolean_cmd ("breakpoint", class_maintenance,
15229 &debug_breakpoint, _("\
15230Set breakpoint location debugging."), _("\
15231Show breakpoint location debugging."), _("\
15232When on, breakpoint location specific debugging is enabled."),
15233 NULL,
15234 show_debug_breakpoint,
15235 &setdebuglist, &showdebuglist);
f1310107 15236
b775012e
LM
15237 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15238 condition_evaluation_enums,
15239 &condition_evaluation_mode_1, _("\
15240Set mode of breakpoint condition evaluation."), _("\
15241Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15242When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15243evaluated on the host's side by GDB. When it is set to \"target\",\n\
15244breakpoint conditions will be downloaded to the target (if the target\n\
15245supports such feature) and conditions will be evaluated on the target's side.\n\
15246If this is set to \"auto\" (default), this will be automatically set to\n\
15247\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 15248be set to \"host\"."),
b775012e
LM
15249 &set_condition_evaluation_mode,
15250 &show_condition_evaluation_mode,
15251 &breakpoint_set_cmdlist,
15252 &breakpoint_show_cmdlist);
15253
f1310107
TJB
15254 add_com ("break-range", class_breakpoint, break_range_command, _("\
15255Set a breakpoint for an address range.\n\
15256break-range START-LOCATION, END-LOCATION\n\
15257where START-LOCATION and END-LOCATION can be one of the following:\n\
15258 LINENUM, for that line in the current file,\n\
15259 FILE:LINENUM, for that line in that file,\n\
15260 +OFFSET, for that number of lines after the current line\n\
dda83cd7 15261 or the start of the range\n\
f1310107
TJB
15262 FUNCTION, for the first line in that function,\n\
15263 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15264 *ADDRESS, for the instruction at that address.\n\
15265\n\
15266The breakpoint will stop execution of the inferior whenever it executes\n\
15267an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15268range (including START-LOCATION and END-LOCATION)."));
15269
e7e0cddf 15270 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 15271Set a dynamic printf at specified location.\n\
e7e0cddf 15272dprintf location,format string,arg1,arg2,...\n\
629500fa 15273location may be a linespec, explicit, or address location.\n"
264f9890 15274"\n" LOCATION_SPEC_HELP_STRING));
e7e0cddf
SS
15275 set_cmd_completer (c, location_completer);
15276
15277 add_setshow_enum_cmd ("dprintf-style", class_support,
15278 dprintf_style_enums, &dprintf_style, _("\
15279Set the style of usage for dynamic printf."), _("\
15280Show the style of usage for dynamic printf."), _("\
15281This setting chooses how GDB will do a dynamic printf.\n\
15282If the value is \"gdb\", then the printing is done by GDB to its own\n\
15283console, as with the \"printf\" command.\n\
15284If the value is \"call\", the print is done by calling a function in your\n\
15285program; by default printf(), but you can choose a different function or\n\
15286output stream by setting dprintf-function and dprintf-channel."),
15287 update_dprintf_commands, NULL,
15288 &setlist, &showlist);
15289
e7e0cddf
SS
15290 add_setshow_string_cmd ("dprintf-function", class_support,
15291 &dprintf_function, _("\
590042fc
PW
15292Set the function to use for dynamic printf."), _("\
15293Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
15294 update_dprintf_commands, NULL,
15295 &setlist, &showlist);
15296
e7e0cddf
SS
15297 add_setshow_string_cmd ("dprintf-channel", class_support,
15298 &dprintf_channel, _("\
590042fc
PW
15299Set the channel to use for dynamic printf."), _("\
15300Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
15301 update_dprintf_commands, NULL,
15302 &setlist, &showlist);
15303
d3ce09f5
SS
15304 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15305 &disconnected_dprintf, _("\
15306Set whether dprintf continues after GDB disconnects."), _("\
15307Show whether dprintf continues after GDB disconnects."), _("\
15308Use this to let dprintf commands continue to hit and produce output\n\
15309even if GDB disconnects or detaches from the target."),
15310 NULL,
15311 NULL,
15312 &setlist, &showlist);
15313
15314 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
15315Target agent only formatted printing, like the C \"printf\" function.\n\
15316Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15317This supports most C printf format specifications, like %s, %d, etc.\n\
15318This is useful for formatted output in user-defined commands."));
d3ce09f5 15319
491144b5 15320 automatic_hardware_breakpoints = true;
f3b1572e 15321
c90e7d63
SM
15322 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
15323 "breakpoint");
15324 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15325 "breakpoint");
b080fe54
AB
15326 gdb::observers::inferior_removed.attach (remove_inferior_breakpoints,
15327 "breakpoint");
c906108c 15328}